1 ;;; org.el --- Outline-based notes management and organizer
3 ;; Carstens outline-mode for keeping track of everything.
4 ;; Copyright (C) 2004-2013 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" t 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 (declare-function orgtbl-mode
"org-table" (&optional arg
))
123 (declare-function org-clock-out
"org-clock" (&optional switch-to-state fail-quietly at-time
))
124 (declare-function org-beamer-mode
"org-beamer" ())
125 (declare-function org-table-edit-field
"org-table" (arg))
126 (declare-function org-table-justify-field-maybe
"org-table" (&optional new
))
127 (declare-function org-id-get-create
"org-id" (&optional force
))
128 (declare-function org-id-find-id-file
"org-id" (id))
129 (declare-function org-tags-view
"org-agenda" (&optional todo-only match
))
130 (declare-function org-agenda-list
"org-agenda" (&optional arg start-day span
))
131 (declare-function org-table-align
"org-table" ())
132 (declare-function org-table-paste-rectangle
"org-table" ())
133 (declare-function org-table-maybe-eval-formula
"org-table" ())
134 (declare-function org-table-maybe-recalculate-line
"org-table" ())
136 ;; load languages based on value of `org-babel-load-languages'
137 (defvar org-babel-load-languages
)
140 (defun org-babel-do-load-languages (sym value
)
141 "Load the languages defined in `org-babel-load-languages'."
142 (set-default sym value
)
144 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
147 (require (intern (concat "ob-" lang
))))
149 (funcall 'fmakunbound
150 (intern (concat "org-babel-execute:" lang
)))
151 (funcall 'fmakunbound
152 (intern (concat "org-babel-expand-body:" lang
)))))))
153 org-babel-load-languages
))
155 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
156 "Languages which can be evaluated in Org-mode buffers.
157 This list can be used to load support for any of the languages
158 below, note that each language will depend on a different set of
159 system executables and/or Emacs modes. When a language is
160 \"loaded\", then code blocks in that language can be evaluated
161 with `org-babel-execute-src-block' bound by default to C-c
162 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
163 be set to remove code block evaluation from the C-c C-c
164 keybinding. By default only Emacs Lisp (which has no
165 requirements) is loaded."
167 :set
'org-babel-do-load-languages
169 :type
'(alist :tag
"Babel Languages"
172 (const :tag
"Awk" awk
)
175 (const :tag
"Asymptote" asymptote
)
176 (const :tag
"Calc" calc
)
177 (const :tag
"Clojure" clojure
)
178 (const :tag
"CSS" css
)
179 (const :tag
"Ditaa" ditaa
)
180 (const :tag
"Dot" dot
)
181 (const :tag
"Emacs Lisp" emacs-lisp
)
182 (const :tag
"Fortran" fortran
)
183 (const :tag
"Gnuplot" gnuplot
)
184 (const :tag
"Haskell" haskell
)
186 (const :tag
"Java" java
)
187 (const :tag
"Javascript" js
)
188 (const :tag
"LaTeX" latex
)
189 (const :tag
"Ledger" ledger
)
190 (const :tag
"Lilypond" lilypond
)
191 (const :tag
"Lisp" lisp
)
192 (const :tag
"Maxima" maxima
)
193 (const :tag
"Matlab" matlab
)
194 (const :tag
"Mscgen" mscgen
)
195 (const :tag
"Ocaml" ocaml
)
196 (const :tag
"Octave" octave
)
197 (const :tag
"Org" org
)
198 (const :tag
"Perl" perl
)
199 (const :tag
"Pico Lisp" picolisp
)
200 (const :tag
"PlantUML" plantuml
)
201 (const :tag
"Python" python
)
202 (const :tag
"Ruby" ruby
)
203 (const :tag
"Sass" sass
)
204 (const :tag
"Scala" scala
)
205 (const :tag
"Scheme" scheme
)
206 (const :tag
"Screen" screen
)
207 (const :tag
"Shell Script" sh
)
208 (const :tag
"Shen" shen
)
209 (const :tag
"Sql" sql
)
210 (const :tag
"Sqlite" sqlite
))
211 :value-type
(boolean :tag
"Activate" :value t
)))
213 ;;;; Customization variables
214 (defcustom org-clone-delete-id nil
215 "Remove ID property of clones of a subtree.
216 When non-nil, clones of a subtree don't inherit the ID property.
217 Otherwise they inherit the ID property with a new unique
224 (require 'org-compat
)
228 (defun org-version (&optional here full message
)
229 "Show the org-mode version in the echo area.
230 With prefix argument HERE, insert it at point.
231 When FULL is non-nil, use a verbose version string.
232 When MESSAGE is non-nil, display a message with the version."
234 (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
235 (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs.el")))
237 (and (fboundp 'org-release
) (fboundp 'org-git-version
))
238 (load (concat org-dir
"org-version.el")
239 'noerror
'nomessage
'nosuffix
)))
240 (org-version (org-release))
241 (git-version (org-git-version))
242 (version (format "Org-mode version %s (%s @ %s)"
246 (if (string= org-dir org-install-dir
)
248 (concat "mixed installation! " org-install-dir
" and " org-dir
))
249 "org-loaddefs.el can not be found!")))
250 (_version (if full version org-version
)))
251 (if (org-called-interactively-p 'interactive
)
255 (if message
(message _version
))
258 (defconst org-version
(org-version))
260 ;;; Compatibility constants
262 ;;; The custom variables
265 "Outline-based notes management and organizer."
270 (defcustom org-mode-hook nil
271 "Mode hook for Org-mode, run after the mode was turned on."
275 (defcustom org-load-hook nil
276 "Hook that is run after org.el has been loaded."
280 (defcustom org-log-buffer-setup-hook nil
281 "Hook that is run after an Org log buffer is created."
286 (defvar org-modules
) ; defined below
287 (defvar org-modules-loaded nil
288 "Have the modules been loaded already?")
290 (defun org-load-modules-maybe (&optional force
)
291 "Load all extensions listed in `org-modules'."
292 (when (or force
(not org-modules-loaded
))
294 (condition-case nil
(require ext
)
295 (error (message "Problems while trying to load feature `%s'" ext
))))
297 (setq org-modules-loaded t
)))
299 (defun org-set-modules (var value
)
300 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
302 (when (featurep 'org
)
303 (org-load-modules-maybe 'force
)))
305 (when (org-bound-and-true-p org-modules
)
306 (let ((a (member 'org-infojs org-modules
)))
307 (and a
(setcar a
'org-jsinfo
))))
309 (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
)
310 "Modules that should always be loaded together with org.el.
311 If a description starts with <C>, the file is not part of Emacs
312 and loading it will require that you have downloaded and properly installed
313 the org-mode distribution.
315 You can also use this system to load external packages (i.e. neither Org
316 core modules, nor modules from the CONTRIB directory). Just add symbols
317 to the end of the list. If the package is called org-xyz.el, then you need
318 to add the symbol `xyz', and the package must have a call to
322 :set
'org-set-modules
325 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
326 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
327 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
328 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
329 (const :tag
" docview: Links to doc-view buffers" org-docview
)
330 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
331 (const :tag
" id: Global IDs for identifying entries" org-id
)
332 (const :tag
" info: Links to Info nodes" org-info
)
333 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
334 (const :tag
" habit: Track your consistency with habits" org-habit
)
335 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
336 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
337 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
338 (const :tag
" mew Links to Mew folders/messages" org-mew
)
339 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
340 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
341 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
342 (const :tag
" special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks
)
343 (const :tag
" vm: Links to VM folders/messages" org-vm
)
344 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
345 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
346 (const :tag
" mouse: Additional mouse support" org-mouse
)
347 (const :tag
" TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler
)
349 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
350 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
351 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
352 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
353 (const :tag
"C collector: Collect properties into tables" org-collector
)
354 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
355 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
356 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
357 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
358 (const :tag
"C eval: Include command output as text" org-eval
)
359 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
360 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
361 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
362 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
363 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
365 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
367 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
368 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
369 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
370 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
371 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
372 (const :tag
"C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber
)
373 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
374 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
375 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
376 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
377 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
378 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
379 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
380 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
381 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
382 (const :tag
"C track: Keep up with Org-mode development" org-track
)
383 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
384 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
385 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
387 (defcustom org-support-shift-select nil
388 "Non-nil means make shift-cursor commands select text when possible.
390 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
391 start selecting a region, or enlarge regions started in this way.
392 In Org-mode, in special contexts, these same keys are used for
393 other purposes, important enough to compete with shift selection.
394 Org tries to balance these needs by supporting `shift-select-mode'
395 outside these special contexts, under control of this variable.
397 The default of this variable is nil, to avoid confusing behavior. Shifted
398 cursor keys will then execute Org commands in the following contexts:
399 - on a headline, changing TODO state (left/right) and priority (up/down)
400 - on a time stamp, changing the time
401 - in a plain list item, changing the bullet type
402 - in a property definition line, switching between allowed values
403 - in the BEGIN line of a clock table (changing the time block).
404 Outside these contexts, the commands will throw an error.
406 When this variable is t and the cursor is not in a special
407 context, Org-mode will support shift-selection for making and
408 enlarging regions. To make this more effective, the bullet
409 cycling will no longer happen anywhere in an item line, but only
410 if the cursor is exactly on the bullet.
412 If you set this variable to the symbol `always', then the keys
413 will not be special in headlines, property lines, and item lines,
414 to make shift selection work there as well. If this is what you
415 want, you can use the following alternative commands: `C-c C-t'
416 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
417 can be used to switch TODO sets, `C-c -' to cycle item bullet
418 types, and properties can be edited by hand or in column view.
420 However, when the cursor is on a timestamp, shift-cursor commands
421 will still edit the time stamp - this is just too good to give up.
423 XEmacs user should have this variable set to nil, because
424 `shift-select-mode' is in Emacs 23 or later only."
427 (const :tag
"Never" nil
)
428 (const :tag
"When outside special context" t
)
429 (const :tag
"Everywhere except timestamps" always
)))
431 (defcustom org-loop-over-headlines-in-active-region nil
432 "Shall some commands act upon headlines in the active region?
434 When set to `t', some commands will be performed in all headlines
435 within the active region.
437 When set to `start-level', some commands will be performed in all
438 headlines within the active region, provided that these headlines
439 are of the same level than the first one.
441 When set to a string, those commands will be performed on the
442 matching headlines within the active region. Such string must be
443 a tags/property/todo match as it is used in the agenda tags view.
445 The list of commands is: `org-schedule', `org-deadline',
446 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
447 `org-archive-to-archive-sibling'. The archiving commands skip
448 already archived entries."
449 :type
'(choice (const :tag
"Don't loop" nil
)
450 (const :tag
"All headlines in active region" t
)
451 (const :tag
"In active region, headlines at the same level than the first one" 'start-level
)
452 (string :tag
"Tags/Property/Todo matcher"))
457 (defgroup org-startup nil
458 "Options concerning startup of Org-mode."
462 (defcustom org-startup-folded t
463 "Non-nil means entering Org-mode will switch to OVERVIEW.
464 This can also be configured on a per-file basis by adding one of
465 the following lines anywhere in the buffer:
467 #+STARTUP: fold (or `overview', this is equivalent)
468 #+STARTUP: nofold (or `showall', this is equivalent)
470 #+STARTUP: showeverything"
473 (const :tag
"nofold: show all" nil
)
474 (const :tag
"fold: overview" t
)
475 (const :tag
"content: all headlines" content
)
476 (const :tag
"show everything, even drawers" showeverything
)))
478 (defcustom org-startup-truncated t
479 "Non-nil means entering Org-mode will set `truncate-lines'.
480 This is useful since some lines containing links can be very long and
481 uninteresting. Also tables look terrible when wrapped."
485 (defcustom org-startup-indented nil
486 "Non-nil means turn on `org-indent-mode' on startup.
487 This can also be configured on a per-file basis by adding one of
488 the following lines anywhere in the buffer:
492 :group
'org-structure
494 (const :tag
"Not" nil
)
495 (const :tag
"Globally (slow on startup in large files)" t
)))
497 (defcustom org-use-sub-superscripts t
498 "Non-nil means interpret \"_\" and \"^\" for export.
499 When this option is turned on, you can use TeX-like syntax for sub- and
500 superscripts. Several characters after \"_\" or \"^\" will be
501 considered as a single item - so grouping with {} is normally not
502 needed. For example, the following things will be parsed as single
503 sub- or superscripts.
505 10^24 or 10^tau several digits will be considered 1 item.
506 10^-12 or 10^-tau a leading sign with digits or a word
507 x^2-y^3 will be read as x^2 - y^3, because items are
508 terminated by almost any nonword/nondigit char.
509 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
511 Still, ambiguity is possible - so when in doubt use {} to enclose the
512 sub/superscript. If you set this variable to the symbol `{}',
513 the braces are *required* in order to trigger interpretations as
514 sub/superscript. This can be helpful in documents that need \"_\"
515 frequently in plain text.
517 Not all export backends support this, but HTML does.
519 This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
521 :group
'org-export-translation
524 (const :tag
"Always interpret" t
)
525 (const :tag
"Only with braces" {})
526 (const :tag
"Never interpret" nil
)))
528 (if (fboundp 'defvaralias
)
529 (defvaralias 'org-export-with-sub-superscripts
'org-use-sub-superscripts
))
532 (defcustom org-startup-with-beamer-mode nil
533 "Non-nil means turn on `org-beamer-mode' on startup.
534 This can also be configured on a per-file basis by adding one of
535 the following lines anywhere in the buffer:
542 (defcustom org-startup-align-all-tables nil
543 "Non-nil means align all tables when visiting a file.
544 This is useful when the column width in tables is forced with <N> cookies
545 in table fields. Such tables will look correct only after the first re-align.
546 This can also be configured on a per-file basis by adding one of
547 the following lines anywhere in the buffer:
553 (defcustom org-startup-with-inline-images nil
554 "Non-nil means show inline images when loading a new Org file.
555 This can also be configured on a per-file basis by adding one of
556 the following lines anywhere in the buffer:
557 #+STARTUP: inlineimages
558 #+STARTUP: noinlineimages"
563 (defcustom org-insert-mode-line-in-empty-file nil
564 "Non-nil means insert the first line setting Org-mode in empty files.
565 When the function `org-mode' is called interactively in an empty file, this
566 normally means that the file name does not automatically trigger Org-mode.
567 To ensure that the file will always be in Org-mode in the future, a
568 line enforcing Org-mode will be inserted into the buffer, if this option
573 (defcustom org-replace-disputed-keys nil
574 "Non-nil means use alternative key bindings for some keys.
575 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
576 These keys are also used by other packages like shift-selection-mode'
577 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
578 If you want to use Org-mode together with one of these other modes,
579 or more generally if you would like to move some Org-mode commands to
580 other keys, set this variable and configure the keys with the variable
583 This option is only relevant at load-time of Org-mode, and must be set
584 *before* org.el is loaded. Changing it requires a restart of Emacs to
589 (defcustom org-use-extra-keys nil
590 "Non-nil means use extra key sequence definitions for certain commands.
591 This happens automatically if you run XEmacs or if `window-system'
592 is nil. This variable lets you do the same manually. You must
593 set it before loading org.
595 Example: on Carbon Emacs 22 running graphically, with an external
596 keyboard on a Powerbook, the default way of setting M-left might
597 not work for either Alt or ESC. Setting this variable will make
602 (if (fboundp 'defvaralias
)
603 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
605 (defcustom org-disputed-keys
606 '(([(shift up
)] .
[(meta p
)])
607 ([(shift down
)] .
[(meta n
)])
608 ([(shift left
)] .
[(meta -
)])
609 ([(shift right
)] .
[(meta +)])
610 ([(control shift right
)] .
[(meta shift
+)])
611 ([(control shift left
)] .
[(meta shift -
)]))
612 "Keys for which Org-mode and other modes compete.
613 This is an alist, cars are the default keys, second element specifies
614 the alternative to use when `org-replace-disputed-keys' is t.
616 Keys can be specified in any syntax supported by `define-key'.
617 The value of this option takes effect only at Org-mode's startup,
618 therefore you'll have to restart Emacs to apply it after changing."
623 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
624 Or return the original if not disputed.
625 Also apply the translations defined in `org-xemacs-key-equivalents'."
626 (when org-replace-disputed-keys
627 (let* ((nkey (key-description key
))
628 (x (org-find-if (lambda (x)
629 (equal (key-description (car x
)) nkey
))
631 (setq key
(if x
(cdr x
) key
))))
632 (when (featurep 'xemacs
)
633 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
636 (defun org-find-if (predicate seq
)
639 (if (funcall predicate
(car seq
))
640 (throw 'exit
(car seq
))
643 (defun org-defkey (keymap key def
)
644 "Define a key, possibly translated, as returned by `org-key'."
645 (define-key keymap
(org-key key
) def
))
647 (defcustom org-ellipsis nil
648 "The ellipsis to use in the Org-mode outline.
649 When nil, just use the standard three dots. When a string, use that instead,
650 When a face, use the standard 3 dots, but with the specified face.
651 The change affects only Org-mode (which will then use its own display table).
652 Changing this requires executing `M-x org-mode' in a buffer to become
655 :type
'(choice (const :tag
"Default" nil
)
656 (face :tag
"Face" :value org-warning
)
657 (string :tag
"String" :value
"...#")))
659 (defvar org-display-table nil
660 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
662 (defgroup org-keywords nil
663 "Keywords in Org-mode."
667 (defcustom org-deadline-string
"DEADLINE:"
668 "String to mark deadline entries.
669 A deadline is this string, followed by a time stamp. Should be a word,
670 terminated by a colon. You can insert a schedule keyword and
671 a timestamp with \\[org-deadline].
672 Changes become only effective after restarting Emacs."
676 (defcustom org-scheduled-string
"SCHEDULED:"
677 "String to mark scheduled TODO entries.
678 A schedule is this string, followed by a time stamp. Should be a word,
679 terminated by a colon. You can insert a schedule keyword and
680 a timestamp with \\[org-schedule].
681 Changes become only effective after restarting Emacs."
685 (defcustom org-closed-string
"CLOSED:"
686 "String used as the prefix for timestamps logging closing a TODO entry."
690 (defcustom org-clock-string
"CLOCK:"
691 "String used as prefix for timestamps clocking work hours on an item."
695 (defconst org-planning-or-clock-line-re
(concat "^[ \t]*\\("
696 org-scheduled-string
"\\|"
697 org-deadline-string
"\\|"
698 org-closed-string
"\\|"
699 org-clock-string
"\\)")
700 "Matches a line with planning or clock info.")
702 (defcustom org-comment-string
"COMMENT"
703 "Entries starting with this keyword will never be exported.
704 An entry can be toggled between COMMENT and normal with
705 \\[org-toggle-comment].
706 Changes become only effective after restarting Emacs."
710 (defcustom org-quote-string
"QUOTE"
711 "Entries starting with this keyword will be exported in fixed-width font.
712 Quoting applies only to the text in the entry following the headline, and does
713 not extend beyond the next headline, even if that is lower level.
714 An entry can be toggled between QUOTE and normal with
715 \\[org-toggle-fixed-width-section]."
719 (defconst org-repeat-re
720 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
721 "Regular expression for specifying repeated events.
722 After a match, group 1 contains the repeat expression.")
724 (defgroup org-structure nil
725 "Options concerning the general structure of Org-mode files."
729 (defgroup org-reveal-location nil
730 "Options about how to make context of a location visible."
731 :tag
"Org Reveal Location"
732 :group
'org-structure
)
734 (defconst org-context-choice
736 (const :tag
"Always" t
)
737 (const :tag
"Never" nil
)
738 (repeat :greedy t
:tag
"Individual contexts"
740 (choice :tag
"Context"
747 (const bookmark-jump
)
751 "Contexts for the reveal options.")
753 (defcustom org-show-hierarchy-above
'((default . t
))
754 "Non-nil means show full hierarchy when revealing a location.
755 Org-mode often shows locations in an org-mode file which might have
756 been invisible before. When this is set, the hierarchy of headings
757 above the exposed location is shown.
758 Turning this off for example for sparse trees makes them very compact.
759 Instead of t, this can also be an alist specifying this option for different
760 contexts. Valid contexts are
761 agenda when exposing an entry from the agenda
762 org-goto when using the command `org-goto' on key C-c C-j
763 occur-tree when using the command `org-occur' on key C-c /
764 tags-tree when constructing a sparse tree based on tags matches
765 link-search when exposing search matches associated with a link
766 mark-goto when exposing the jump goal of a mark
767 bookmark-jump when exposing a bookmark location
768 isearch when exiting from an incremental search
769 default default for all contexts not set explicitly"
770 :group
'org-reveal-location
771 :type org-context-choice
)
773 (defcustom org-show-following-heading
'((default . nil
))
774 "Non-nil means show following heading when revealing a location.
775 Org-mode often shows locations in an org-mode file which might have
776 been invisible before. When this is set, the heading following the
778 Turning this off for example for sparse trees makes them very compact,
779 but makes it harder to edit the location of the match. In such a case,
780 use the command \\[org-reveal] to show more context.
781 Instead of t, this can also be an alist specifying this option for different
782 contexts. See `org-show-hierarchy-above' for valid contexts."
783 :group
'org-reveal-location
784 :type org-context-choice
)
786 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
787 "Non-nil means show all sibling heading when revealing a location.
788 Org-mode often shows locations in an org-mode file which might have
789 been invisible before. When this is set, the sibling of the current entry
790 heading are all made visible. If `org-show-hierarchy-above' is t,
791 the same happens on each level of the hierarchy above the current entry.
793 By default this is on for the isearch context, off for all other contexts.
794 Turning this off for example for sparse trees makes them very compact,
795 but makes it harder to edit the location of the match. In such a case,
796 use the command \\[org-reveal] to show more context.
797 Instead of t, this can also be an alist specifying this option for different
798 contexts. See `org-show-hierarchy-above' for valid contexts."
799 :group
'org-reveal-location
800 :type org-context-choice
)
802 (defcustom org-show-entry-below
'((default . nil
))
803 "Non-nil means show the entry below a headline when revealing a location.
804 Org-mode often shows locations in an org-mode file which might have
805 been invisible before. When this is set, the text below the headline that is
806 exposed is also shown.
808 By default this is off for all contexts.
809 Instead of t, this can also be an alist specifying this option for different
810 contexts. See `org-show-hierarchy-above' for valid contexts."
811 :group
'org-reveal-location
812 :type org-context-choice
)
814 (defcustom org-indirect-buffer-display
'other-window
815 "How should indirect tree buffers be displayed?
816 This applies to indirect buffers created with the commands
817 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
819 current-window Display in the current window
820 other-window Just display in another window.
821 dedicated-frame Create one new frame, and re-use it each time.
822 new-frame Make a new frame each time. Note that in this case
823 previously-made indirect buffers are kept, and you need to
824 kill these buffers yourself."
825 :group
'org-structure
826 :group
'org-agenda-windows
828 (const :tag
"In current window" current-window
)
829 (const :tag
"In current frame, other window" other-window
)
830 (const :tag
"Each time a new frame" new-frame
)
831 (const :tag
"One dedicated frame" dedicated-frame
)))
833 (defcustom org-use-speed-commands nil
834 "Non-nil means activate single letter commands at beginning of a headline.
835 This may also be a function to test for appropriate locations where speed
836 commands should be active."
837 :group
'org-structure
839 (const :tag
"Never" nil
)
840 (const :tag
"At beginning of headline stars" t
)
843 (defcustom org-speed-commands-user nil
844 "Alist of additional speed commands.
845 This list will be checked before `org-speed-commands-default'
846 when the variable `org-use-speed-commands' is non-nil
847 and when the cursor is at the beginning of a headline.
848 The car if each entry is a string with a single letter, which must
849 be assigned to `self-insert-command' in the global map.
850 The cdr is either a command to be called interactively, a function
851 to be called, or a form to be evaluated.
852 An entry that is just a list with a single string will be interpreted
853 as a descriptive headline that will be added when listing the speed
854 commands in the Help buffer using the `?' speed command."
855 :group
'org-structure
856 :type
'(repeat :value
("k" . ignore
)
857 (choice :value
("k" . ignore
)
858 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
859 (cons :tag
"Letter and Command"
860 (string :tag
"Command letter")
865 (defgroup org-cycle nil
866 "Options concerning visibility cycling in Org-mode."
868 :group
'org-structure
)
870 (defcustom org-cycle-skip-children-state-if-no-children t
871 "Non-nil means skip CHILDREN state in entries that don't have any."
875 (defcustom org-cycle-max-level nil
876 "Maximum level which should still be subject to visibility cycling.
877 Levels higher than this will, for cycling, be treated as text, not a headline.
878 When `org-odd-levels-only' is set, a value of N in this variable actually
879 means 2N-1 stars as the limiting headline.
880 When nil, cycle all levels.
881 Note that the limiting level of cycling is also influenced by
882 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
883 `org-inlinetask-min-level' is, cycling will be limited to levels one less
887 (const :tag
"No limit" nil
)
888 (integer :tag
"Maximum level")))
890 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
891 "Names of drawers. Drawers are not opened by cycling on the headline above.
892 Drawers only open with a TAB on the drawer line itself. A drawer looks like
897 The drawer \"PROPERTIES\" is special for capturing properties through
900 Drawers can be defined on the per-file basis with a line like:
902 #+DRAWERS: HIDDEN STATE PROPERTIES"
903 :group
'org-structure
905 :type
'(repeat (string :tag
"Drawer Name")))
907 (defcustom org-hide-block-startup nil
908 "Non-nil means entering Org-mode will fold all blocks.
909 This can also be set in on a per-file basis with
911 #+STARTUP: hideblocks
912 #+STARTUP: showblocks"
917 (defcustom org-cycle-global-at-bob nil
918 "Cycle globally if cursor is at beginning of buffer and not at a headline.
919 This makes it possible to do global cycling without having to use S-TAB or
920 \\[universal-argument] TAB. For this special case to work, the first line
921 of the buffer must not be a headline -- it may be empty or some other text.
922 When used in this way, `org-cycle-hook' is disabled temporarily to make
923 sure the cursor stays at the beginning of the buffer. When this option is
924 nil, don't do anything special at the beginning of the buffer."
928 (defcustom org-cycle-level-after-item
/entry-creation t
929 "Non-nil means cycle entry level or item indentation in new empty entries.
931 When the cursor is at the end of an empty headline, i.e with only stars
932 and maybe a TODO keyword, TAB will then switch the entry to become a child,
933 and then all possible ancestor states, before returning to the original state.
934 This makes data entry extremely fast: M-RET to create a new headline,
935 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
937 When the cursor is at the end of an empty plain list item, one TAB will
938 make it a subitem, two or more tabs will back up to make this an item
939 higher up in the item hierarchy."
943 (defcustom org-cycle-emulate-tab t
944 "Where should `org-cycle' emulate TAB.
946 white Only in completely white lines
947 whitestart Only at the beginning of lines, before the first non-white char
948 t Everywhere except in headlines
949 exc-hl-bol Everywhere except at the start of a headline
950 If TAB is used in a place where it does not emulate TAB, the current subtree
951 visibility is cycled."
953 :type
'(choice (const :tag
"Never" nil
)
954 (const :tag
"Only in completely white lines" white
)
955 (const :tag
"Before first char in a line" whitestart
)
956 (const :tag
"Everywhere except in headlines" t
)
957 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
960 (defcustom org-cycle-separator-lines
2
961 "Number of empty lines needed to keep an empty line between collapsed trees.
962 If you leave an empty line between the end of a subtree and the following
963 headline, this empty line is hidden when the subtree is folded.
964 Org-mode will leave (exactly) one empty line visible if the number of
965 empty lines is equal or larger to the number given in this variable.
966 So the default 2 means at least 2 empty lines after the end of a subtree
967 are needed to produce free space between a collapsed subtree and the
970 If the number is negative, and the number of empty lines is at least -N,
971 all empty lines are shown.
973 Special case: when 0, never leave empty lines in collapsed view."
976 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
978 (defcustom org-pre-cycle-hook nil
979 "Hook that is run before visibility cycling is happening.
980 The function(s) in this hook must accept a single argument which indicates
981 the new state that will be set right after running this hook. The
982 argument is a symbol. Before a global state change, it can have the values
983 `overview', `content', or `all'. Before a local state change, it can have
984 the values `folded', `children', or `subtree'."
988 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
989 org-cycle-hide-drawers
990 org-cycle-show-empty-lines
991 org-optimize-window-after-visibility-change
)
992 "Hook that is run after `org-cycle' has changed the buffer visibility.
993 The function(s) in this hook must accept a single argument which indicates
994 the new state that was set by the most recent `org-cycle' command. The
995 argument is a symbol. After a global state change, it can have the values
996 `overview', `contents', or `all'. After a local state change, it can have
997 the values `folded', `children', or `subtree'."
1001 (defgroup org-edit-structure nil
1002 "Options concerning structure editing in Org-mode."
1003 :tag
"Org Edit Structure"
1004 :group
'org-structure
)
1006 (defcustom org-odd-levels-only nil
1007 "Non-nil means skip even levels and only use odd levels for the outline.
1008 This has the effect that two stars are being added/taken away in
1009 promotion/demotion commands. It also influences how levels are
1010 handled by the exporters.
1011 Changing it requires restart of `font-lock-mode' to become effective
1012 for fontification also in regions already fontified.
1013 You may also set this on a per-file basis by adding one of the following
1014 lines to the buffer:
1018 :group
'org-edit-structure
1019 :group
'org-appearance
1022 (defcustom org-adapt-indentation t
1023 "Non-nil means adapt indentation to outline node level.
1025 When this variable is set, Org assumes that you write outlines by
1026 indenting text in each node to align with the headline (after the stars).
1027 The following issues are influenced by this variable:
1029 - When this is set and the *entire* text in an entry is indented, the
1030 indentation is increased by one space in a demotion command, and
1031 decreased by one in a promotion command. If any line in the entry
1032 body starts with text at column 0, indentation is not changed at all.
1034 - Property drawers and planning information is inserted indented when
1035 this variable s set. When nil, they will not be indented.
1037 - TAB indents a line relative to context. The lines below a headline
1038 will be indented when this variable is set.
1040 Note that this is all about true indentation, by adding and removing
1041 space characters. See also `org-indent.el' which does level-dependent
1042 indentation in a virtual way, i.e. at display time in Emacs."
1043 :group
'org-edit-structure
1046 (defcustom org-special-ctrl-a
/e nil
1047 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1049 When t, `C-a' will bring back the cursor to the beginning of the
1050 headline text, i.e. after the stars and after a possible TODO
1051 keyword. In an item, this will be the position after bullet and
1052 check-box, if any. When the cursor is already at that position,
1053 another `C-a' will bring it to the beginning of the line.
1055 `C-e' will jump to the end of the headline, ignoring the presence
1056 of tags in the headline. A second `C-e' will then jump to the
1057 true end of the line, after any tags. This also means that, when
1058 this variable is non-nil, `C-e' also will never jump beyond the
1059 end of the heading of a folded section, i.e. not after the
1062 When set to the symbol `reversed', the first `C-a' or `C-e' works
1063 normally, going to the true line boundary first. Only a directly
1064 following, identical keypress will bring the cursor to the
1067 This may also be a cons cell where the behavior for `C-a' and
1068 `C-e' is set separately."
1069 :group
'org-edit-structure
1071 (const :tag
"off" nil
)
1072 (const :tag
"on: after stars/bullet and before tags first" t
)
1073 (const :tag
"reversed: true line boundary first" reversed
)
1074 (cons :tag
"Set C-a and C-e separately"
1075 (choice :tag
"Special C-a"
1076 (const :tag
"off" nil
)
1077 (const :tag
"on: after stars/bullet first" t
)
1078 (const :tag
"reversed: before stars/bullet first" reversed
))
1079 (choice :tag
"Special C-e"
1080 (const :tag
"off" nil
)
1081 (const :tag
"on: before tags first" t
)
1082 (const :tag
"reversed: after tags first" reversed
)))))
1083 (if (fboundp 'defvaralias
)
1084 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
1086 (defcustom org-special-ctrl-k nil
1087 "Non-nil means `C-k' will behave specially in headlines.
1088 When nil, `C-k' will call the default `kill-line' command.
1089 When t, the following will happen while the cursor is in the headline:
1091 - When the cursor is at the beginning of a headline, kill the entire
1092 line and possible the folded subtree below the line.
1093 - When in the middle of the headline text, kill the headline up to the tags.
1094 - When after the headline text, kill the tags."
1095 :group
'org-edit-structure
1098 (defcustom org-ctrl-k-protect-subtree nil
1099 "Non-nil means, do not delete a hidden subtree with C-k.
1100 When set to the symbol `error', simply throw an error when C-k is
1101 used to kill (part-of) a headline that has hidden text behind it.
1102 Any other non-nil value will result in a query to the user, if it is
1103 OK to kill that hidden subtree. When nil, kill without remorse."
1104 :group
'org-edit-structure
1107 (const :tag
"Do not protect hidden subtrees" nil
)
1108 (const :tag
"Protect hidden subtrees with a security query" t
)
1109 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1111 (defcustom org-catch-invisible-edits nil
1112 "Check if in invisible region before inserting or deleting a character.
1115 nil Do not check, so just do invisible edits.
1116 error Throw an error and do nothing.
1117 show Make point visible, and do the requested edit.
1118 show-and-error Make point visible, then throw an error and abort the edit.
1119 smart Make point visible, and do insertion/deletion if it is
1120 adjacent to visible text and the change feels predictable.
1121 Never delete a previously invisible character or add in the
1122 middle or right after an invisible region. Basically, this
1123 allows insertion and backward-delete right before ellipses.
1124 FIXME: maybe in this case we should not even show?"
1125 :group
'org-edit-structure
1128 (const :tag
"Do not check" nil
)
1129 (const :tag
"Throw error when trying to edit" error
)
1130 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1131 (const :tag
"Show invisible part and do the edit" show
)
1132 (const :tag
"Be smart and do the right thing" smart
)))
1134 (defcustom org-yank-folded-subtrees t
1135 "Non-nil means when yanking subtrees, fold them.
1136 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1137 it starts with a heading and all other headings in it are either children
1138 or siblings, then fold all the subtrees. However, do this only if no
1139 text after the yank would be swallowed into a folded tree by this action."
1140 :group
'org-edit-structure
1143 (defcustom org-yank-adjusted-subtrees nil
1144 "Non-nil means when yanking subtrees, adjust the level.
1145 With this setting, `org-paste-subtree' is used to insert the subtree, see
1146 this function for details."
1147 :group
'org-edit-structure
1150 (defcustom org-M-RET-may-split-line
'((default . t
))
1151 "Non-nil means M-RET will split the line at the cursor position.
1152 When nil, it will go to the end of the line before making a
1154 You may also set this option in a different way for different
1155 contexts. Valid contexts are:
1157 headline when creating a new headline
1158 item when creating a new item
1159 table in a table field
1160 default the value to be used for all contexts not explicitly
1162 :group
'org-structure
1165 (const :tag
"Always" t
)
1166 (const :tag
"Never" nil
)
1167 (repeat :greedy t
:tag
"Individual contexts"
1169 (choice :tag
"Context"
1177 (defcustom org-insert-heading-respect-content nil
1178 "Non-nil means insert new headings after the current subtree.
1179 When nil, the new heading is created directly after the current line.
1180 The commands \\[org-insert-heading-respect-content] and
1181 \\[org-insert-todo-heading-respect-content] turn this variable on
1182 for the duration of the command."
1183 :group
'org-structure
1186 (defcustom org-blank-before-new-entry
'((heading . auto
)
1187 (plain-list-item . auto
))
1188 "Should `org-insert-heading' leave a blank line before new heading/item?
1189 The value is an alist, with `heading' and `plain-list-item' as CAR,
1190 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1191 which case Org will look at the surrounding headings/items and try to
1192 make an intelligent decision whether to insert a blank line or not.
1194 For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
1195 set, the setting here is ignored and no empty line is inserted, to avoid
1196 breaking the list structure."
1197 :group
'org-edit-structure
1199 (cons (const heading
)
1200 (choice (const :tag
"Never" nil
)
1201 (const :tag
"Always" t
)
1202 (const :tag
"Auto" auto
)))
1203 (cons (const plain-list-item
)
1204 (choice (const :tag
"Never" nil
)
1205 (const :tag
"Always" t
)
1206 (const :tag
"Auto" auto
)))))
1208 (defcustom org-insert-heading-hook nil
1209 "Hook being run after inserting a new heading."
1210 :group
'org-edit-structure
1213 (defcustom org-enable-fixed-width-editor t
1214 "Non-nil means lines starting with \":\" are treated as fixed-width.
1215 This currently only means they are never auto-wrapped.
1216 When nil, such lines will be treated like ordinary lines.
1217 See also the QUOTE keyword."
1218 :group
'org-edit-structure
1221 (defcustom org-goto-auto-isearch t
1222 "Non-nil means typing characters in `org-goto' starts incremental search.
1223 When nil, you can use these keybindings to navigate the buffer:
1225 q Quit the org-goto interface
1226 n Go to the next visible heading
1227 p Go to the previous visible heading
1228 f Go one heading forward on same level
1229 b Go one heading backward on same level
1230 u Go one heading up"
1231 :group
'org-edit-structure
1234 (defgroup org-sparse-trees nil
1235 "Options concerning sparse trees in Org-mode."
1236 :tag
"Org Sparse Trees"
1237 :group
'org-structure
)
1239 (defcustom org-highlight-sparse-tree-matches t
1240 "Non-nil means highlight all matches that define a sparse tree.
1241 The highlights will automatically disappear the next time the buffer is
1242 changed by an edit command."
1243 :group
'org-sparse-trees
1246 (defcustom org-remove-highlights-with-change t
1247 "Non-nil means any change to the buffer will remove temporary highlights.
1248 Such highlights are created by `org-occur' and `org-clock-display'.
1249 When nil, `C-c C-c needs to be used to get rid of the highlights.
1250 The highlights created by `org-preview-latex-fragment' always need
1251 `C-c C-c' to be removed."
1252 :group
'org-sparse-trees
1257 (defcustom org-occur-hook
'(org-first-headline-recenter)
1258 "Hook that is run after `org-occur' has constructed a sparse tree.
1259 This can be used to recenter the window to show as much of the structure
1261 :group
'org-sparse-trees
1264 (defgroup org-imenu-and-speedbar nil
1265 "Options concerning imenu and speedbar in Org-mode."
1266 :tag
"Org Imenu and Speedbar"
1267 :group
'org-structure
)
1269 (defcustom org-imenu-depth
2
1270 "The maximum level for Imenu access to Org-mode headlines.
1271 This also applied for speedbar access."
1272 :group
'org-imenu-and-speedbar
1275 (defgroup org-table nil
1276 "Options concerning tables in Org-mode."
1280 (defcustom org-enable-table-editor
'optimized
1281 "Non-nil means lines starting with \"|\" are handled by the table editor.
1282 When nil, such lines will be treated like ordinary lines.
1284 When equal to the symbol `optimized', the table editor will be optimized to
1286 - Automatic overwrite mode in front of whitespace in table fields.
1287 This makes the structure of the table stay in tact as long as the edited
1288 field does not exceed the column width.
1289 - Minimize the number of realigns. Normally, the table is aligned each time
1290 TAB or RET are pressed to move to another field. With optimization this
1291 happens only if changes to a field might have changed the column width.
1292 Optimization requires replacing the functions `self-insert-command',
1293 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1294 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1295 very good at guessing when a re-align will be necessary, but you can always
1296 force one with \\[org-ctrl-c-ctrl-c].
1298 If you would like to use the optimized version in Org-mode, but the
1299 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1301 This variable can be used to turn on and off the table editor during a session,
1302 but in order to toggle optimization, a restart is required.
1304 See also the variable `org-table-auto-blank-field'."
1307 (const :tag
"off" nil
)
1309 (const :tag
"on, optimized" optimized
)))
1311 (defcustom org-self-insert-cluster-for-undo
(or (featurep 'xemacs
)
1312 (version<= emacs-version
"24.1"))
1313 "Non-nil means cluster self-insert commands for undo when possible.
1314 If this is set, then, like in the Emacs command loop, 20 consecutive
1315 characters will be undone together.
1316 This is configurable, because there is some impact on typing performance."
1320 (defcustom org-table-tab-recognizes-table.el t
1321 "Non-nil means TAB will automatically notice a table.el table.
1322 When it sees such a table, it moves point into it and - if necessary -
1323 calls `table-recognize-table'."
1324 :group
'org-table-editing
1327 (defgroup org-link nil
1328 "Options concerning links in Org-mode."
1332 (defvar org-link-abbrev-alist-local nil
1333 "Buffer-local version of `org-link-abbrev-alist', which see.
1334 The value of this is taken from the #+LINK lines.")
1335 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1337 (defcustom org-link-abbrev-alist nil
1338 "Alist of link abbreviations.
1339 The car of each element is a string, to be replaced at the start of a link.
1340 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1341 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1343 [[linkkey:tag][description]]
1345 The 'linkkey' must be a word word, starting with a letter, followed
1346 by letters, numbers, '-' or '_'.
1348 If REPLACE is a string, the tag will simply be appended to create the link.
1349 If the string contains \"%s\", the tag will be inserted there. If the string
1350 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1351 at that point (see the function `url-hexify-string'). If the string contains
1352 the specifier \"%(my-function)\", then the custom function `my-function' will
1353 be invoked: this function takes the tag as its only argument and must return
1356 REPLACE may also be a function that will be called with the tag as the
1357 only argument to create the link, which should be returned as a string.
1359 See the manual for examples."
1363 (string :tag
"Protocol")
1365 (string :tag
"Format")
1368 (defcustom org-descriptive-links t
1369 "Non-nil means Org will display descriptive links.
1370 E.g. [[http://orgmode.org][Org website]] will be displayed as
1371 \"Org Website\", hiding the link itself and just displaying its
1372 description. When set to `nil', Org will display the full links
1375 You can interactively set the value of this variable by calling
1376 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1380 (defcustom org-link-file-path-type
'adaptive
1381 "How the path name in file links should be stored.
1384 relative Relative to the current directory, i.e. the directory of the file
1385 into which the link is being inserted.
1386 absolute Absolute path, if possible with ~ for home directory.
1387 noabbrev Absolute path, no abbreviation of home directory.
1388 adaptive Use relative path for files in the current directory and sub-
1389 directories of it. For other files, use an absolute path."
1397 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1398 "Types of links that should be activated in Org-mode files.
1399 This is a list of symbols, each leading to the activation of a certain link
1400 type. In principle, it does not hurt to turn on most link types - there may
1401 be a small gain when turning off unused link types. The types are:
1403 bracket The recommended [[link][description]] or [[link]] links with hiding.
1404 angle Links in angular brackets that may contain whitespace like
1405 <bbdb:Carsten Dominik>.
1406 plain Plain links in normal text, no whitespace, like http://google.com.
1407 radio Text that is matched by a radio target, see manual for details.
1408 tag Tag settings in a headline (link to tag search).
1409 date Time stamps (link to calendar).
1410 footnote Footnote labels.
1412 Changing this variable requires a restart of Emacs to become effective."
1414 :type
'(set :greedy t
1415 (const :tag
"Double bracket links" bracket
)
1416 (const :tag
"Angular bracket links" angle
)
1417 (const :tag
"Plain text links" plain
)
1418 (const :tag
"Radio target matches" radio
)
1419 (const :tag
"Tags" tag
)
1420 (const :tag
"Timestamps" date
)
1421 (const :tag
"Footnotes" footnote
)))
1423 (defcustom org-make-link-description-function nil
1424 "Function to use for generating link descriptions from links.
1425 When nil, the link location will be used. This function must take
1426 two parameters: the first one is the link, the second one is the
1427 description generated by `org-insert-link'. The function should
1428 return the description to use."
1432 (defgroup org-link-store nil
1433 "Options concerning storing links in Org-mode."
1434 :tag
"Org Store Link"
1437 (defcustom org-url-hexify-p t
1438 "When non-nil, hexify URL when creating a link."
1441 :group
'org-link-store
)
1443 (defcustom org-email-link-description-format
"Email %c: %.30s"
1444 "Format of the description part of a link to an email or usenet message.
1445 The following %-escapes will be replaced by corresponding information:
1447 %F full \"From\" field
1448 %f name, taken from \"From\" field, address if no name
1449 %T full \"To\" field
1450 %t first name in \"To\" field, address if no name
1451 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1452 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1457 You may use normal field width specification between the % and the letter.
1458 This is for example useful to limit the length of the subject.
1460 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1461 :group
'org-link-store
1464 (defcustom org-from-is-user-regexp
1466 (when (and user-mail-address
(not (string= user-mail-address
"")))
1467 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1468 (when (and user-full-name
(not (string= user-full-name
"")))
1469 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1470 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1471 "Regexp matched against the \"From:\" header of an email or usenet message.
1472 It should match if the message is from the user him/herself."
1473 :group
'org-link-store
1476 (defcustom org-context-in-file-links t
1477 "Non-nil means file links from `org-store-link' contain context.
1478 A search string will be added to the file name with :: as separator and
1479 used to find the context when the link is activated by the command
1480 `org-open-at-point'. When this option is t, the entire active region
1481 will be placed in the search string of the file link. If set to a
1482 positive integer, only the first n lines of context will be stored.
1484 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1485 negates this setting for the duration of the command."
1486 :group
'org-link-store
1487 :type
'(choice boolean integer
))
1489 (defcustom org-keep-stored-link-after-insertion nil
1490 "Non-nil means keep link in list for entire session.
1492 The command `org-store-link' adds a link pointing to the current
1493 location to an internal list. These links accumulate during a session.
1494 The command `org-insert-link' can be used to insert links into any
1495 Org-mode file (offering completion for all stored links). When this
1496 option is nil, every link which has been inserted once using \\[org-insert-link]
1497 will be removed from the list, to make completing the unused links
1499 :group
'org-link-store
1502 (defgroup org-link-follow nil
1503 "Options concerning following links in Org-mode."
1504 :tag
"Org Follow Link"
1507 (defcustom org-link-translation-function nil
1508 "Function to translate links with different syntax to Org syntax.
1509 This can be used to translate links created for example by the Planner
1510 or emacs-wiki packages to Org syntax.
1511 The function must accept two parameters, a TYPE containing the link
1512 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1513 which is everything after the link protocol. It should return a cons
1514 with possibly modified values of type and path.
1515 Org contains a function for this, so if you set this variable to
1516 `org-translate-link-from-planner', you should be able follow many
1517 links created by planner."
1518 :group
'org-link-follow
1521 (defcustom org-follow-link-hook nil
1522 "Hook that is run after a link has been followed."
1523 :group
'org-link-follow
1526 (defcustom org-tab-follows-link nil
1527 "Non-nil means on links TAB will follow the link.
1528 Needs to be set before org.el is loaded.
1529 This really should not be used, it does not make sense, and the
1530 implementation is bad."
1531 :group
'org-link-follow
1534 (defcustom org-return-follows-link nil
1535 "Non-nil means on links RET will follow the link."
1536 :group
'org-link-follow
1539 (defcustom org-mouse-1-follows-link
1540 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1541 "Non-nil means mouse-1 on a link will follow the link.
1542 A longer mouse click will still set point. Does not work on XEmacs.
1543 Needs to be set before org.el is loaded."
1544 :group
'org-link-follow
1547 (defcustom org-mark-ring-length
4
1548 "Number of different positions to be recorded in the ring.
1549 Changing this requires a restart of Emacs to work correctly."
1550 :group
'org-link-follow
1553 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1554 "Non-nil means internal links in Org files must exactly match a headline.
1555 When nil, the link search tries to match a phrase with all words
1556 in the search text."
1557 :group
'org-link-follow
1560 (const :tag
"Use fuzzy text search" nil
)
1561 (const :tag
"Match only exact headline" t
)
1562 (const :tag
"Match exact headline or query to create it"
1565 (defcustom org-link-frame-setup
1566 '((vm . vm-visit-folder-other-frame
)
1567 (vm-imap . vm-visit-imap-folder-other-frame
)
1568 (gnus . org-gnus-no-new-news
)
1569 (file . find-file-other-window
)
1570 (wl . wl-other-frame
))
1571 "Setup the frame configuration for following links.
1572 When following a link with Emacs, it may often be useful to display
1573 this link in another window or frame. This variable can be used to
1574 set this up for the different types of links.
1577 `vm-visit-folder-other-window'
1578 `vm-visit-folder-other-frame'
1579 For Gnus, use any of
1582 `org-gnus-no-new-news'
1583 For FILE, use any of
1585 `find-file-other-window'
1586 `find-file-other-frame'
1587 For Wanderlust use any of
1590 For the calendar, use the variable `calendar-setup'.
1591 For BBDB, it is currently only possible to display the matches in
1593 :group
'org-link-follow
1597 (const vm-visit-folder
)
1598 (const vm-visit-folder-other-window
)
1599 (const vm-visit-folder-other-frame
)))
1603 (const gnus-other-frame
)
1604 (const org-gnus-no-new-news
)))
1608 (const find-file-other-window
)
1609 (const find-file-other-frame
)))
1613 (const wl-other-frame
)))))
1615 (defcustom org-display-internal-link-with-indirect-buffer nil
1616 "Non-nil means use indirect buffer to display infile links.
1617 Activating internal links (from one location in a file to another location
1618 in the same file) normally just jumps to the location. When the link is
1619 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1621 another window. When this option is set, the other window actually displays
1622 an indirect buffer clone of the current buffer, to avoid any visibility
1623 changes to the current buffer."
1624 :group
'org-link-follow
1627 (defcustom org-open-non-existing-files nil
1628 "Non-nil means `org-open-file' will open non-existing files.
1629 When nil, an error will be generated.
1630 This variable applies only to external applications because they
1631 might choke on non-existing files. If the link is to a file that
1632 will be opened in Emacs, the variable is ignored."
1633 :group
'org-link-follow
1636 (defcustom org-open-directory-means-index-dot-org nil
1637 "Non-nil means a link to a directory really means to index.org.
1638 When nil, following a directory link will run dired or open a finder/explorer
1639 window on that directory."
1640 :group
'org-link-follow
1643 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1644 "Function and arguments to call for following mailto links.
1645 This is a list with the first element being a Lisp function, and the
1646 remaining elements being arguments to the function. In string arguments,
1647 %a will be replaced by the address, and %s will be replaced by the subject
1648 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1649 :group
'org-link-follow
1651 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1652 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1653 (const :tag
"message-mail" (message-mail "%a" "%s"))
1654 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1656 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1657 "Non-nil means ask for confirmation before executing shell links.
1658 Shell links can be dangerous: just think about a link
1660 [[shell:rm -rf ~/*][Google Search]]
1662 This link would show up in your Org-mode document as \"Google Search\",
1663 but really it would remove your entire home directory.
1664 Therefore we advise against setting this variable to nil.
1665 Just change it to `y-or-n-p' if you want to confirm with a
1666 single keystroke rather than having to type \"yes\"."
1667 :group
'org-link-follow
1669 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1670 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1671 (const :tag
"no confirmation (dangerous)" nil
)))
1672 (put 'org-confirm-shell-link-function
1673 'safe-local-variable
1674 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1676 (defcustom org-confirm-shell-link-not-regexp
""
1677 "A regexp to skip confirmation for shell links."
1678 :group
'org-link-follow
1682 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1683 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1684 Elisp links can be dangerous: just think about a link
1686 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1688 This link would show up in your Org-mode document as \"Google Search\",
1689 but really it would remove your entire home directory.
1690 Therefore we advise against setting this variable to nil.
1691 Just change it to `y-or-n-p' if you want to confirm with a
1692 single keystroke rather than having to type \"yes\"."
1693 :group
'org-link-follow
1695 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1696 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1697 (const :tag
"no confirmation (dangerous)" nil
)))
1698 (put 'org-confirm-shell-link-function
1699 'safe-local-variable
1700 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1702 (defcustom org-confirm-elisp-link-not-regexp
""
1703 "A regexp to skip confirmation for Elisp links."
1704 :group
'org-link-follow
1708 (defconst org-file-apps-defaults-gnu
1712 "Default file applications on a UNIX or GNU/Linux system.
1713 See `org-file-apps'.")
1715 (defconst org-file-apps-defaults-macosx
1718 (system .
"open %s")
1720 ("eps.gz" .
"gv %s")
1722 ("fig" .
"xfig %s"))
1723 "Default file applications on a MacOS X system.
1724 The system \"open\" is known as a default, but we use X11 applications
1725 for some files for which the OS does not have a good default.
1726 See `org-file-apps'.")
1728 (defconst org-file-apps-defaults-windowsnt
1732 (list (if (featurep 'xemacs
)
1733 'mswindows-shell-execute
1737 (list (if (featurep 'xemacs
)
1738 'mswindows-shell-execute
1741 "Default file applications on a Windows NT system.
1742 The system \"open\" is used for most files.
1743 See `org-file-apps'.")
1745 (defcustom org-file-apps
1748 ("\\.mm\\'" . default
)
1749 ("\\.x?html?\\'" . default
)
1750 ("\\.pdf\\'" . default
)
1752 "External applications for opening `file:path' items in a document.
1753 Org-mode uses system defaults for different file types, but
1754 you can use this variable to set the application for a given file
1755 extension. The entries in this list are cons cells where the car identifies
1756 files and the cdr the corresponding command. Possible values for the
1758 \"string\" A string as a file identifier can be interpreted in different
1759 ways, depending on its contents:
1761 - Alphanumeric characters only:
1762 Match links with this file extension.
1763 Example: (\"pdf\" . \"evince %s\")
1764 to open PDFs with evince.
1766 - Regular expression: Match links where the
1767 filename matches the regexp. If you want to
1768 use groups here, use shy groups.
1770 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1771 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1772 to open *.html and *.xhtml with firefox.
1774 - Regular expression which contains (non-shy) groups:
1775 Match links where the whole link, including \"::\", and
1776 anything after that, matches the regexp.
1777 In a custom command string, %1, %2, etc. are replaced with
1778 the parts of the link that were matched by the groups.
1779 For backwards compatibility, if a command string is given
1780 that does not use any of the group matches, this case is
1781 handled identically to the second one (i.e. match against
1783 In a custom lisp form, you can access the group matches with
1784 (match-string n link).
1786 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1787 to open [[file:document.pdf::5]] with evince at page 5.
1789 `directory' Matches a directory
1790 `remote' Matches a remote file, accessible through tramp or efs.
1791 Remote files most likely should be visited through Emacs
1792 because external applications cannot handle such paths.
1793 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1794 so all files Emacs knows how to handle. Using this with
1795 command `emacs' will open most files in Emacs. Beware that this
1796 will also open html files inside Emacs, unless you add
1797 (\"html\" . default) to the list as well.
1798 t Default for files not matched by any of the other options.
1799 `system' The system command to open files, like `open' on Windows
1800 and Mac OS X, and mailcap under GNU/Linux. This is the command
1801 that will be selected if you call `C-c C-o' with a double
1802 \\[universal-argument] \\[universal-argument] prefix.
1804 Possible values for the command are:
1805 `emacs' The file will be visited by the current Emacs process.
1806 `default' Use the default application for this file type, which is the
1807 association for t in the list, most likely in the system-specific
1809 This can be used to overrule an unwanted setting in the
1810 system-specific variable.
1811 `system' Use the system command for opening files, like \"open\".
1812 This command is specified by the entry whose car is `system'.
1813 Most likely, the system-specific version of this variable
1814 does define this command, but you can overrule/replace it
1816 string A command to be executed by a shell; %s will be replaced
1817 by the path to the file.
1818 sexp A Lisp form which will be evaluated. The file path will
1819 be available in the Lisp variable `file'.
1820 For more examples, see the system specific constants
1821 `org-file-apps-defaults-macosx'
1822 `org-file-apps-defaults-windowsnt'
1823 `org-file-apps-defaults-gnu'."
1824 :group
'org-link-follow
1826 (cons (choice :value
""
1827 (string :tag
"Extension")
1828 (const :tag
"System command to open files" system
)
1829 (const :tag
"Default for unrecognized files" t
)
1830 (const :tag
"Remote file" remote
)
1831 (const :tag
"Links to a directory" directory
)
1832 (const :tag
"Any files that have Emacs modes"
1835 (const :tag
"Visit with Emacs" emacs
)
1836 (const :tag
"Use default" default
)
1837 (const :tag
"Use the system command" system
)
1838 (string :tag
"Command")
1839 (sexp :tag
"Lisp form")))))
1841 (defcustom org-doi-server-url
"http://dx.doi.org/"
1842 "The URL of the DOI server."
1845 :group
'org-link-follow
)
1847 (defgroup org-refile nil
1848 "Options concerning refiling entries in Org-mode."
1852 (defcustom org-directory
"~/org"
1853 "Directory with org files.
1854 This is just a default location to look for Org files. There is no need
1855 at all to put your files into this directory. It is only used in the
1856 following situations:
1858 1. When a capture template specifies a target file that is not an
1859 absolute path. The path will then be interpreted relative to
1861 2. When a capture note is filed away in an interactive way (when exiting the
1862 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1863 with `org-directory' as the default path."
1865 :group
'org-remember
1869 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1870 "Default target for storing notes.
1871 Used as a fall back file for org-remember.el and org-capture.el, for
1872 templates that do not specify a target file."
1874 :group
'org-remember
1877 (const :tag
"Default from remember-data-file" nil
)
1880 (defcustom org-goto-interface
'outline
1881 "The default interface to be used for `org-goto'.
1883 outline The interface shows an outline of the relevant file
1884 and the correct heading is found by moving through
1885 the outline or by searching with incremental search.
1886 outline-path-completion Headlines in the current buffer are offered via
1887 completion. This is the interface also used by
1888 the refile command."
1891 (const :tag
"Outline" outline
)
1892 (const :tag
"Outline-path-completion" outline-path-completion
)))
1894 (defcustom org-goto-max-level
5
1895 "Maximum target level when running `org-goto' with refile interface."
1899 (defcustom org-reverse-note-order nil
1900 "Non-nil means store new notes at the beginning of a file or entry.
1901 When nil, new notes will be filed to the end of a file or entry.
1902 This can also be a list with cons cells of regular expressions that
1903 are matched against file names, and values."
1904 :group
'org-remember
1908 (const :tag
"Reverse always" t
)
1909 (const :tag
"Reverse never" nil
)
1910 (repeat :tag
"By file name regexp"
1911 (cons regexp boolean
))))
1913 (defcustom org-log-refile nil
1914 "Information to record when a task is refiled.
1916 Possible values are:
1918 nil Don't add anything
1919 time Add a time stamp to the task
1920 note Prompt for a note and add it with template `org-log-note-headings'
1922 This option can also be set with on a per-file-basis with
1924 #+STARTUP: nologrefile
1925 #+STARTUP: logrefile
1926 #+STARTUP: lognoterefile
1928 You can have local logging settings for a subtree by setting the LOGGING
1929 property to one or more of these keywords.
1931 When bulk-refiling from the agenda, the value `note' is forbidden and
1932 will temporarily be changed to `time'."
1934 :group
'org-progress
1937 (const :tag
"No logging" nil
)
1938 (const :tag
"Record timestamp" time
)
1939 (const :tag
"Record timestamp with note." note
)))
1941 (defcustom org-refile-targets nil
1942 "Targets for refiling entries with \\[org-refile].
1943 This is a list of cons cells. Each cell contains:
1944 - a specification of the files to be considered, either a list of files,
1945 or a symbol whose function or variable value will be used to retrieve
1946 a file name or a list of file names. If you use `org-agenda-files' for
1947 that, all agenda files will be scanned for targets. Nil means consider
1948 headings in the current buffer.
1949 - A specification of how to find candidate refile targets. This may be
1951 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1952 This tag has to be present in all target headlines, inheritance will
1954 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1956 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1957 headlines that are refiling targets.
1958 - a cons cell (:level . N). Any headline of level N is considered a target.
1959 Note that, when `org-odd-levels-only' is set, level corresponds to
1960 order in hierarchy, not to the number of stars.
1961 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1962 Note that, when `org-odd-levels-only' is set, level corresponds to
1963 order in hierarchy, not to the number of stars.
1965 Each element of this list generates a set of possible targets.
1966 The union of these sets is presented (with completion) to
1967 the user by `org-refile'.
1969 You can set the variable `org-refile-target-verify-function' to a function
1970 to verify each headline found by the simple criteria above.
1972 When this variable is nil, all top-level headlines in the current buffer
1973 are used, equivalent to the value `((nil . (:level . 1))'."
1977 (choice :value org-agenda-files
1978 (const :tag
"All agenda files" org-agenda-files
)
1979 (const :tag
"Current buffer" nil
)
1980 (function) (variable) (file))
1981 (choice :tag
"Identify target headline by"
1982 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1983 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1984 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1985 (cons :tag
"Level number" (const :value
:level
) (integer))
1986 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1988 (defcustom org-refile-target-verify-function nil
1989 "Function to verify if the headline at point should be a refile target.
1990 The function will be called without arguments, with point at the
1991 beginning of the headline. It should return t and leave point
1992 where it is if the headline is a valid target for refiling.
1994 If the target should not be selected, the function must return nil.
1995 In addition to this, it may move point to a place from where the search
1996 should be continued. For example, the function may decide that the entire
1997 subtree of the current entry should be excluded and move point to the end
2002 (defcustom org-refile-use-cache nil
2003 "Non-nil means cache refile targets to speed up the process.
2004 The cache for a particular file will be updated automatically when
2005 the buffer has been killed, or when any of the marker used for flagging
2006 refile targets no longer points at a live buffer.
2007 If you have added new entries to a buffer that might themselves be targets,
2008 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
2009 find that easier, `C-u C-u C-u C-c C-w'."
2014 (defcustom org-refile-use-outline-path nil
2015 "Non-nil means provide refile targets as paths.
2016 So a level 3 headline will be available as level1/level2/level3.
2018 When the value is `file', also include the file name (without directory)
2019 into the path. In this case, you can also stop the completion after
2020 the file name, to get entries inserted as top level in the file.
2022 When `full-file-path', include the full file path."
2025 (const :tag
"Not" nil
)
2026 (const :tag
"Yes" t
)
2027 (const :tag
"Start with file name" file
)
2028 (const :tag
"Start with full file path" full-file-path
)))
2030 (defcustom org-outline-path-complete-in-steps t
2031 "Non-nil means complete the outline path in hierarchical steps.
2032 When Org-mode uses the refile interface to select an outline path
2033 \(see variable `org-refile-use-outline-path'), the completion of
2034 the path can be done is a single go, or if can be done in steps down
2035 the headline hierarchy. Going in steps is probably the best if you
2036 do not use a special completion package like `ido' or `icicles'.
2037 However, when using these packages, going in one step can be very
2038 fast, while still showing the whole path to the entry."
2042 (defcustom org-refile-allow-creating-parent-nodes nil
2043 "Non-nil means allow to create new nodes as refile targets.
2044 New nodes are then created by adding \"/new node name\" to the completion
2045 of an existing node. When the value of this variable is `confirm',
2046 new node creation must be confirmed by the user (recommended)
2047 When nil, the completion must match an existing entry.
2049 Note that, if the new heading is not seen by the criteria
2050 listed in `org-refile-targets', multiple instances of the same
2051 heading would be created by trying again to file under the new
2055 (const :tag
"Never" nil
)
2056 (const :tag
"Always" t
)
2057 (const :tag
"Prompt for confirmation" confirm
)))
2059 (defcustom org-refile-active-region-within-subtree nil
2060 "Non-nil means also refile active region within a subtree.
2062 By default `org-refile' doesn't allow refiling regions if they
2063 don't contain a set of subtrees, but it might be convenient to
2064 do so sometimes: in that case, the first line of the region is
2065 converted to a headline before refiling."
2070 (defgroup org-todo nil
2071 "Options concerning TODO items in Org-mode."
2075 (defgroup org-progress nil
2076 "Options concerning Progress logging in Org-mode."
2080 (defvar org-todo-interpretation-widgets
2081 '((:tag
"Sequence (cycling hits every state)" sequence
)
2082 (:tag
"Type (cycling directly to DONE)" type
))
2083 "The available interpretation symbols for customizing `org-todo-keywords'.
2084 Interested libraries should add to this list.")
2086 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2087 "List of TODO entry keyword sequences and their interpretation.
2088 \\<org-mode-map>This is a list of sequences.
2090 Each sequence starts with a symbol, either `sequence' or `type',
2091 indicating if the keywords should be interpreted as a sequence of
2092 action steps, or as different types of TODO items. The first
2093 keywords are states requiring action - these states will select a headline
2094 for inclusion into the global TODO list Org-mode produces. If one of
2095 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2096 signify that no further action is necessary. If \"|\" is not found,
2097 the last keyword is treated as the only DONE state of the sequence.
2099 The command \\[org-todo] cycles an entry through these states, and one
2100 additional state where no keyword is present. For details about this
2101 cycling, see the manual.
2103 TODO keywords and interpretation can also be set on a per-file basis with
2104 the special #+SEQ_TODO and #+TYP_TODO lines.
2106 Each keyword can optionally specify a character for fast state selection
2107 \(in combination with the variable `org-use-fast-todo-selection')
2108 and specifiers for state change logging, using the same syntax that
2109 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2110 the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2111 indicates to record a time stamp each time this state is selected.
2113 Each keyword may also specify if a timestamp or a note should be
2114 recorded when entering or leaving the state, by adding additional
2115 characters in the parenthesis after the keyword. This looks like this:
2116 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2117 record only the time of the state change. With X and Y being either
2118 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2119 Y when leaving the state if and only if the *target* state does not
2120 define X. You may omit any of the fast-selection key or X or /Y,
2121 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2123 For backward compatibility, this variable may also be just a list
2124 of keywords. In this case the interpretation (sequence or type) will be
2125 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2127 :group
'org-keywords
2129 (repeat :tag
"Old syntax, just keywords"
2130 (string :tag
"Keyword"))
2131 (repeat :tag
"New syntax"
2134 :tag
"Interpretation"
2135 ;;Quick and dirty way to see
2136 ;;`org-todo-interpretations'. This takes the
2137 ;;place of item arguments
2145 org-todo-interpretation-widgets
))
2148 (string :tag
"Keyword"))))))
2150 (defvar org-todo-keywords-1 nil
2151 "All TODO and DONE keywords active in a buffer.")
2152 (make-variable-buffer-local 'org-todo-keywords-1
)
2153 (defvar org-todo-keywords-for-agenda nil
)
2154 (defvar org-done-keywords-for-agenda nil
)
2155 (defvar org-drawers-for-agenda nil
)
2156 (defvar org-todo-keyword-alist-for-agenda nil
)
2157 (defvar org-tag-alist-for-agenda nil
)
2158 (defvar org-agenda-contributing-files nil
)
2159 (defvar org-not-done-keywords nil
)
2160 (make-variable-buffer-local 'org-not-done-keywords
)
2161 (defvar org-done-keywords nil
)
2162 (make-variable-buffer-local 'org-done-keywords
)
2163 (defvar org-todo-heads nil
)
2164 (make-variable-buffer-local 'org-todo-heads
)
2165 (defvar org-todo-sets nil
)
2166 (make-variable-buffer-local 'org-todo-sets
)
2167 (defvar org-todo-log-states nil
)
2168 (make-variable-buffer-local 'org-todo-log-states
)
2169 (defvar org-todo-kwd-alist nil
)
2170 (make-variable-buffer-local 'org-todo-kwd-alist
)
2171 (defvar org-todo-key-alist nil
)
2172 (make-variable-buffer-local 'org-todo-key-alist
)
2173 (defvar org-todo-key-trigger nil
)
2174 (make-variable-buffer-local 'org-todo-key-trigger
)
2176 (defcustom org-todo-interpretation
'sequence
2177 "Controls how TODO keywords are interpreted.
2178 This variable is in principle obsolete and is only used for
2179 backward compatibility, if the interpretation of todo keywords is
2180 not given already in `org-todo-keywords'. See that variable for
2183 :group
'org-keywords
2184 :type
'(choice (const sequence
)
2187 (defcustom org-use-fast-todo-selection t
2188 "Non-nil means use the fast todo selection scheme with C-c C-t.
2189 This variable describes if and under what circumstances the cycling
2190 mechanism for TODO keywords will be replaced by a single-key, direct
2193 When nil, fast selection is never used.
2195 When the symbol `prefix', it will be used when `org-todo' is called
2196 with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
2197 `C-u t' in an agenda buffer.
2199 When t, fast selection is used by default. In this case, the prefix
2200 argument forces cycling instead.
2202 In all cases, the special interface is only used if access keys have
2203 actually been assigned by the user, i.e. if keywords in the configuration
2204 are followed by a letter in parenthesis, like TODO(t)."
2207 (const :tag
"Never" nil
)
2208 (const :tag
"By default" t
)
2209 (const :tag
"Only with C-u C-c C-t" prefix
)))
2211 (defcustom org-provide-todo-statistics t
2212 "Non-nil means update todo statistics after insert and toggle.
2213 ALL-HEADLINES means update todo statistics by including headlines
2214 with no TODO keyword as well, counting them as not done.
2215 A list of TODO keywords means the same, but skip keywords that are
2218 When this is set, todo statistics is updated in the parent of the
2219 current entry each time a todo state is changed."
2222 (const :tag
"Yes, only for TODO entries" t
)
2223 (const :tag
"Yes, including all entries" 'all-headlines
)
2224 (repeat :tag
"Yes, for TODOs in this list"
2225 (string :tag
"TODO keyword"))
2226 (other :tag
"No TODO statistics" nil
)))
2228 (defcustom org-hierarchical-todo-statistics t
2229 "Non-nil means TODO statistics covers just direct children.
2230 When nil, all entries in the subtree are considered.
2231 This has only an effect if `org-provide-todo-statistics' is set.
2232 To set this to nil for only a single subtree, use a COOKIE_DATA
2233 property and include the word \"recursive\" into the value."
2237 (defcustom org-after-todo-state-change-hook nil
2238 "Hook which is run after the state of a TODO item was changed.
2239 The new state (a string with a TODO keyword, or nil) is available in the
2240 Lisp variable `org-state'."
2244 (defvar org-blocker-hook nil
2245 "Hook for functions that are allowed to block a state change.
2247 Functions in this hook should not modify the buffer.
2248 Each function gets as its single argument a property list,
2249 see `org-trigger-hook' for more information about this list.
2251 If any of the functions in this hook returns nil, the state change
2254 (defvar org-trigger-hook nil
2255 "Hook for functions that are triggered by a state change.
2257 Each function gets as its single argument a property list with at
2258 least the following elements:
2260 (:type type-of-change :position pos-at-entry-start
2261 :from old-state :to new-state)
2263 Depending on the type, more properties may be present.
2265 This mechanism is currently implemented for:
2269 :type todo-state-change
2270 :from previous state (keyword as a string), or nil, or a symbol
2271 'todo' or 'done', to indicate the general type of state.
2272 :to new state, like in :from")
2274 (defcustom org-enforce-todo-dependencies nil
2275 "Non-nil means undone TODO entries will block switching the parent to DONE.
2276 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2277 be blocked if any prior sibling is not yet done.
2278 Finally, if the parent is blocked because of ordered siblings of its own,
2279 the child will also be blocked."
2280 :set
(lambda (var val
)
2283 (add-hook 'org-blocker-hook
2284 'org-block-todo-from-children-or-siblings-or-parent
)
2285 (remove-hook 'org-blocker-hook
2286 'org-block-todo-from-children-or-siblings-or-parent
)))
2290 (defcustom org-enforce-todo-checkbox-dependencies nil
2291 "Non-nil means unchecked boxes will block switching the parent to DONE.
2292 When this is nil, checkboxes have no influence on switching TODO states.
2293 When non-nil, you first need to check off all check boxes before the TODO
2294 entry can be switched to DONE.
2295 This variable needs to be set before org.el is loaded, and you need to
2296 restart Emacs after a change to make the change effective. The only way
2297 to change is while Emacs is running is through the customize interface."
2298 :set
(lambda (var val
)
2301 (add-hook 'org-blocker-hook
2302 'org-block-todo-from-checkboxes
)
2303 (remove-hook 'org-blocker-hook
2304 'org-block-todo-from-checkboxes
)))
2308 (defcustom org-treat-insert-todo-heading-as-state-change nil
2309 "Non-nil means inserting a TODO heading is treated as state change.
2310 So when the command \\[org-insert-todo-heading] is used, state change
2311 logging will apply if appropriate. When nil, the new TODO item will
2312 be inserted directly, and no logging will take place."
2316 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2317 "Non-nil means switching TODO states with S-cursor counts as state change.
2318 This is the default behavior. However, setting this to nil allows a
2319 convenient way to select a TODO state and bypass any logging associated
2324 (defcustom org-todo-state-tags-triggers nil
2325 "Tag changes that should be triggered by TODO state changes.
2326 This is a list. Each entry is
2328 (state-change (tag . flag) .......)
2330 State-change can be a string with a state, and empty string to indicate the
2331 state that has no TODO keyword, or it can be one of the symbols `todo'
2332 or `done', meaning any not-done or done state, respectively."
2336 (cons (choice :tag
"When changing to"
2337 (const :tag
"Not-done state" todo
)
2338 (const :tag
"Done state" done
)
2339 (string :tag
"State"))
2341 (cons :tag
"Tag action"
2343 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2345 (defcustom org-log-done nil
2346 "Information to record when a task moves to the DONE state.
2348 Possible values are:
2350 nil Don't add anything, just change the keyword
2351 time Add a time stamp to the task
2352 note Prompt for a note and add it with template `org-log-note-headings'
2354 This option can also be set with on a per-file-basis with
2356 #+STARTUP: nologdone
2358 #+STARTUP: lognotedone
2360 You can have local logging settings for a subtree by setting the LOGGING
2361 property to one or more of these keywords."
2363 :group
'org-progress
2365 (const :tag
"No logging" nil
)
2366 (const :tag
"Record CLOSED timestamp" time
)
2367 (const :tag
"Record CLOSED timestamp with note." note
)))
2369 ;; Normalize old uses of org-log-done.
2371 ((eq org-log-done t
) (setq org-log-done
'time
))
2372 ((and (listp org-log-done
) (memq 'done org-log-done
))
2373 (setq org-log-done
'note
)))
2375 (defcustom org-log-reschedule nil
2376 "Information to record when the scheduling date of a tasks is modified.
2378 Possible values are:
2380 nil Don't add anything, just change the date
2381 time Add a time stamp to the task
2382 note Prompt for a note and add it with template `org-log-note-headings'
2384 This option can also be set with on a per-file-basis with
2386 #+STARTUP: nologreschedule
2387 #+STARTUP: logreschedule
2388 #+STARTUP: lognotereschedule"
2390 :group
'org-progress
2392 (const :tag
"No logging" nil
)
2393 (const :tag
"Record timestamp" time
)
2394 (const :tag
"Record timestamp with note." note
)))
2396 (defcustom org-log-redeadline nil
2397 "Information to record when the deadline date of a tasks is modified.
2399 Possible values are:
2401 nil Don't add anything, just change the date
2402 time Add a time stamp to the task
2403 note Prompt for a note and add it with template `org-log-note-headings'
2405 This option can also be set with on a per-file-basis with
2407 #+STARTUP: nologredeadline
2408 #+STARTUP: logredeadline
2409 #+STARTUP: lognoteredeadline
2411 You can have local logging settings for a subtree by setting the LOGGING
2412 property to one or more of these keywords."
2414 :group
'org-progress
2416 (const :tag
"No logging" nil
)
2417 (const :tag
"Record timestamp" time
)
2418 (const :tag
"Record timestamp with note." note
)))
2420 (defcustom org-log-note-clock-out nil
2421 "Non-nil means record a note when clocking out of an item.
2422 This can also be configured on a per-file basis by adding one of
2423 the following lines anywhere in the buffer:
2425 #+STARTUP: lognoteclock-out
2426 #+STARTUP: nolognoteclock-out"
2428 :group
'org-progress
2431 (defcustom org-log-done-with-time t
2432 "Non-nil means the CLOSED time stamp will contain date and time.
2433 When nil, only the date will be recorded."
2434 :group
'org-progress
2437 (defcustom org-log-note-headings
2438 '((done .
"CLOSING NOTE %t")
2439 (state .
"State %-12s from %-12S %t")
2440 (note .
"Note taken on %t")
2441 (reschedule .
"Rescheduled from %S on %t")
2442 (delschedule .
"Not scheduled, was %S on %t")
2443 (redeadline .
"New deadline from %S on %t")
2444 (deldeadline .
"Removed deadline, was %S on %t")
2445 (refile .
"Refiled on %t")
2447 "Headings for notes added to entries.
2448 The value is an alist, with the car being a symbol indicating the note
2449 context, and the cdr is the heading to be used. The heading may also be the
2451 %t in the heading will be replaced by a time stamp.
2452 %T will be an active time stamp instead the default inactive one
2453 %d will be replaced by a short-format time stamp.
2454 %D will be replaced by an active short-format time stamp.
2455 %s will be replaced by the new TODO state, in double quotes.
2456 %S will be replaced by the old TODO state, in double quotes.
2457 %u will be replaced by the user name.
2458 %U will be replaced by the full user name.
2460 In fact, it is not a good idea to change the `state' entry, because
2461 agenda log mode depends on the format of these entries."
2463 :group
'org-progress
2464 :type
'(list :greedy t
2465 (cons (const :tag
"Heading when closing an item" done
) string
)
2467 "Heading when changing todo state (todo sequence only)"
2469 (cons (const :tag
"Heading when just taking a note" note
) string
)
2470 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2471 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2472 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2473 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2474 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2475 (cons (const :tag
"Heading when refiling" refile
) string
)))
2477 (unless (assq 'note org-log-note-headings
)
2478 (push '(note .
"%t") org-log-note-headings
))
2480 (defcustom org-log-into-drawer nil
2481 "Non-nil means insert state change notes and time stamps into a drawer.
2482 When nil, state changes notes will be inserted after the headline and
2483 any scheduling and clock lines, but not inside a drawer.
2485 The value of this variable should be the name of the drawer to use.
2486 LOGBOOK is proposed as the default drawer for this purpose, you can
2487 also set this to a string to define the drawer of your choice.
2489 A value of t is also allowed, representing \"LOGBOOK\".
2491 If this variable is set, `org-log-state-notes-insert-after-drawers'
2494 You can set the property LOG_INTO_DRAWER to overrule this setting for
2497 :group
'org-progress
2499 (const :tag
"Not into a drawer" nil
)
2500 (const :tag
"LOGBOOK" t
)
2501 (string :tag
"Other")))
2503 (if (fboundp 'defvaralias
)
2504 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2506 (defun org-log-into-drawer ()
2507 "Return the value of `org-log-into-drawer', but let properties overrule.
2508 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2509 used instead of the default value."
2510 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit t
)))
2512 ((not p
) org-log-into-drawer
)
2513 ((equal p
"nil") nil
)
2514 ((equal p
"t") "LOGBOOK")
2517 (defcustom org-log-state-notes-insert-after-drawers nil
2518 "Non-nil means insert state change notes after any drawers in entry.
2519 Only the drawers that *immediately* follow the headline and the
2520 deadline/scheduled line are skipped.
2521 When nil, insert notes right after the heading and perhaps the line
2522 with deadline/scheduling if present.
2524 This variable will have no effect if `org-log-into-drawer' is
2527 :group
'org-progress
2530 (defcustom org-log-states-order-reversed t
2531 "Non-nil means the latest state note will be directly after heading.
2532 When nil, the state change notes will be ordered according to time."
2534 :group
'org-progress
2537 (defcustom org-todo-repeat-to-state nil
2538 "The TODO state to which a repeater should return the repeating task.
2539 By default this is the first task in a TODO sequence, or the previous state
2540 in a TODO_TYP set. But you can specify another task here.
2541 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2544 :type
'(choice (const :tag
"Head of sequence" nil
)
2545 (string :tag
"Specific state")))
2547 (defcustom org-log-repeat
'time
2548 "Non-nil means record moving through the DONE state when triggering repeat.
2549 An auto-repeating task is immediately switched back to TODO when
2550 marked DONE. If you are not logging state changes (by adding \"@\"
2551 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2552 record a closing note, there will be no record of the task moving
2553 through DONE. This variable forces taking a note anyway.
2555 nil Don't force a record
2556 time Record a time stamp
2557 note Prompt for a note and add it with template `org-log-note-headings'
2559 This option can also be set with on a per-file-basis with
2561 #+STARTUP: nologrepeat
2562 #+STARTUP: logrepeat
2563 #+STARTUP: lognoterepeat
2565 You can have local logging settings for a subtree by setting the LOGGING
2566 property to one or more of these keywords."
2568 :group
'org-progress
2570 (const :tag
"Don't force a record" nil
)
2571 (const :tag
"Force recording the DONE state" time
)
2572 (const :tag
"Force recording a note with the DONE state" note
)))
2575 (defgroup org-priorities nil
2576 "Priorities in Org-mode."
2577 :tag
"Org Priorities"
2580 (defcustom org-enable-priority-commands t
2581 "Non-nil means priority commands are active.
2582 When nil, these commands will be disabled, so that you never accidentally
2584 :group
'org-priorities
2587 (defcustom org-highest-priority ?A
2588 "The highest priority of TODO items. A character like ?A, ?B etc.
2589 Must have a smaller ASCII number than `org-lowest-priority'."
2590 :group
'org-priorities
2593 (defcustom org-lowest-priority ?C
2594 "The lowest priority of TODO items. A character like ?A, ?B etc.
2595 Must have a larger ASCII number than `org-highest-priority'."
2596 :group
'org-priorities
2599 (defcustom org-default-priority ?B
2600 "The default priority of TODO items.
2601 This is the priority an item gets if no explicit priority is given.
2602 When starting to cycle on an empty priority the first step in the cycle
2603 depends on `org-priority-start-cycle-with-default'. The resulting first
2604 step priority must not exceed the range from `org-highest-priority' to
2605 `org-lowest-priority' which means that `org-default-priority' has to be
2606 in this range exclusive or inclusive the range boundaries. Else the
2607 first step refuses to set the default and the second will fall back
2608 to (depending on the command used) the highest or lowest priority."
2609 :group
'org-priorities
2612 (defcustom org-priority-start-cycle-with-default t
2613 "Non-nil means start with default priority when starting to cycle.
2614 When this is nil, the first step in the cycle will be (depending on the
2615 command used) one higher or lower than the default priority.
2616 See also `org-default-priority'."
2617 :group
'org-priorities
2620 (defcustom org-get-priority-function nil
2621 "Function to extract the priority from a string.
2622 The string is normally the headline. If this is nil Org computes the
2623 priority from the priority cookie like [#A] in the headline. It returns
2624 an integer, increasing by 1000 for each priority level.
2625 The user can set a different function here, which should take a string
2626 as an argument and return the numeric priority."
2627 :group
'org-priorities
2631 (defgroup org-time nil
2632 "Options concerning time stamps and deadlines in Org-mode."
2636 (defcustom org-insert-labeled-timestamps-at-point nil
2637 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2638 When nil, these labeled time stamps are forces into the second line of an
2639 entry, just after the headline. When scheduling from the global TODO list,
2640 the time stamp will always be forced into the second line."
2644 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2645 "Formats for `format-time-string' which are used for time stamps.
2646 It is not recommended to change this constant.")
2648 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2649 "Number of minutes to round time stamps to.
2650 These are two values, the first applies when first creating a time stamp.
2651 The second applies when changing it with the commands `S-up' and `S-down'.
2652 When changing the time stamp, this means that it will change in steps
2653 of N minutes, as given by the second value.
2655 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2656 numbers should be factors of 60, so for example 5, 10, 15.
2658 When this is larger than 1, you can still force an exact time stamp by using
2659 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2660 and by using a prefix arg to `S-up/down' to specify the exact number
2661 of minutes to shift."
2663 :get
#'(lambda (var) ; Make sure both elements are there
2664 (if (integerp (default-value var
))
2665 (list (default-value var
) 5)
2666 (default-value var
)))
2668 (integer :tag
"when inserting times")
2669 (integer :tag
"when modifying times")))
2671 ;; Normalize old customizations of this variable.
2672 (when (integerp org-time-stamp-rounding-minutes
)
2673 (setq org-time-stamp-rounding-minutes
2674 (list org-time-stamp-rounding-minutes
2675 org-time-stamp-rounding-minutes
)))
2677 (defcustom org-display-custom-times nil
2678 "Non-nil means overlay custom formats over all time stamps.
2679 The formats are defined through the variable `org-time-stamp-custom-formats'.
2680 To turn this on on a per-file basis, insert anywhere in the file:
2681 #+STARTUP: customtime"
2685 (make-variable-buffer-local 'org-display-custom-times
)
2687 (defcustom org-time-stamp-custom-formats
2688 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2689 "Custom formats for time stamps. See `format-time-string' for the syntax.
2690 These are overlaid over the default ISO format if the variable
2691 `org-display-custom-times' is set. Time like %H:%M should be at the
2692 end of the second format. The custom formats are also honored by export
2693 commands, if custom time display is turned on at the time of export."
2697 (defun org-time-stamp-format (&optional long inactive
)
2698 "Get the right format for a time string."
2699 (let ((f (if long
(cdr org-time-stamp-formats
)
2700 (car org-time-stamp-formats
))))
2702 (concat "[" (substring f
1 -
1) "]")
2705 (defcustom org-time-clocksum-format
"%d:%02d"
2706 "The format string used when creating CLOCKSUM lines.
2707 This is also used when org-mode generates a time duration."
2711 (defcustom org-time-clocksum-use-fractional nil
2712 "If non-nil, \\[org-clock-display] uses fractional times.
2713 org-mode generates a time duration."
2717 (defcustom org-time-clocksum-fractional-format
"%.2f"
2718 "The format string used when creating CLOCKSUM lines, or when
2719 org-mode generates a time duration."
2723 (defcustom org-deadline-warning-days
14
2724 "No. of days before expiration during which a deadline becomes active.
2725 This variable governs the display in sparse trees and in the agenda.
2726 When 0 or negative, it means use this number (the absolute value of it)
2727 even if a deadline has a different individual lead time specified.
2729 Custom commands can set this variable in the options section."
2731 :group
'org-agenda-daily
/weekly
2734 (defcustom org-read-date-prefer-future t
2735 "Non-nil means assume future for incomplete date input from user.
2736 This affects the following situations:
2737 1. The user gives a month but not a year.
2738 For example, if it is April and you enter \"feb 2\", this will be read
2739 as Feb 2, *next* year. \"May 5\", however, will be this year.
2740 2. The user gives a day, but no month.
2741 For example, if today is the 15th, and you enter \"3\", Org-mode will
2742 read this as the third of *next* month. However, if you enter \"17\",
2743 it will be considered as *this* month.
2745 If you set this variable to the symbol `time', then also the following
2748 3. If the user gives a time.
2749 If the time is before now, it will be interpreted as tomorrow.
2751 Currently none of this works for ISO week specifications.
2753 When this option is nil, the current day, month and year will always be
2756 See also `org-agenda-jump-prefer-future'."
2759 (const :tag
"Never" nil
)
2760 (const :tag
"Check month and day" t
)
2761 (const :tag
"Check month, day, and time" time
)))
2763 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
2764 "Should the agenda jump command prefer the future for incomplete dates?
2765 The default is to do the same as configured in `org-read-date-prefer-future'.
2766 But you can also set a deviating value here.
2767 This may t or nil, or the symbol `org-read-date-prefer-future'."
2772 (const :tag
"Use org-read-date-prefer-future"
2773 org-read-date-prefer-future
)
2774 (const :tag
"Never" nil
)
2775 (const :tag
"Always" t
)))
2777 (defcustom org-read-date-force-compatible-dates t
2778 "Should date/time prompt force dates that are guaranteed to work in Emacs?
2780 Depending on the system Emacs is running on, certain dates cannot
2781 be represented with the type used internally to represent time.
2782 Dates between 1970-1-1 and 2038-1-1 can always be represented
2783 correctly. Some systems allow for earlier dates, some for later,
2784 some for both. One way to find out it to insert any date into an
2785 Org buffer, putting the cursor on the year and hitting S-up and
2786 S-down to test the range.
2788 When this variable is set to t, the date/time prompt will not let
2789 you specify dates outside the 1970-2037 range, so it is certain that
2790 these dates will work in whatever version of Emacs you are
2791 running, and also that you can move a file from one Emacs implementation
2792 to another. WHenever Org is forcing the year for you, it will display
2795 When this variable is nil, Org will check if the date is
2796 representable in the specific Emacs implementation you are using.
2797 If not, it will force a year, usually the current year, and beep
2798 to remind you. Currently this setting is not recommended because
2799 the likelihood that you will open your Org files in an Emacs that
2800 has limited date range is not negligible.
2802 A workaround for this problem is to use diary sexp dates for time
2803 stamps outside of this range."
2808 (defcustom org-read-date-display-live t
2809 "Non-nil means display current interpretation of date prompt live.
2810 This display will be in an overlay, in the minibuffer."
2814 (defcustom org-read-date-popup-calendar t
2815 "Non-nil means pop up a calendar when prompting for a date.
2816 In the calendar, the date can be selected with mouse-1. However, the
2817 minibuffer will also be active, and you can simply enter the date as well.
2818 When nil, only the minibuffer will be available."
2821 (if (fboundp 'defvaralias
)
2822 (defvaralias 'org-popup-calendar-for-date-prompt
2823 'org-read-date-popup-calendar
))
2825 (defcustom org-read-date-minibuffer-setup-hook nil
2826 "Hook to be used to set up keys for the date/time interface.
2827 Add key definitions to `minibuffer-local-map', which will be a temporary
2832 (defcustom org-extend-today-until
0
2833 "The hour when your day really ends. Must be an integer.
2834 This has influence for the following applications:
2835 - When switching the agenda to \"today\". It it is still earlier than
2836 the time given here, the day recognized as TODAY is actually yesterday.
2837 - When a date is read from the user and it is still before the time given
2838 here, the current date and time will be assumed to be yesterday, 23:59.
2839 Also, timestamps inserted in capture templates follow this rule.
2841 IMPORTANT: This is a feature whose implementation is and likely will
2842 remain incomplete. Really, it is only here because past midnight seems to
2843 be the favorite working time of John Wiegley :-)"
2847 (defcustom org-use-effective-time nil
2848 "If non-nil, consider `org-extend-today-until' when creating timestamps.
2849 For example, if `org-extend-today-until' is 8, and it's 4am, then the
2850 \"effective time\" of any timestamps between midnight and 8am will be
2851 23:59 of the previous day."
2856 (defcustom org-edit-timestamp-down-means-later nil
2857 "Non-nil means S-down will increase the time in a time stamp.
2858 When nil, S-up will increase."
2862 (defcustom org-calendar-follow-timestamp-change t
2863 "Non-nil means make the calendar window follow timestamp changes.
2864 When a timestamp is modified and the calendar window is visible, it will be
2865 moved to the new date."
2869 (defgroup org-tags nil
2870 "Options concerning tags in Org-mode."
2874 (defcustom org-tag-alist nil
2875 "List of tags allowed in Org-mode files.
2876 When this list is nil, Org-mode will base TAG input on what is already in the
2878 The value of this variable is an alist, the car of each entry must be a
2879 keyword as a string, the cdr may be a character that is used to select
2880 that tag through the fast-tag-selection interface.
2881 See the manual for details."
2885 (cons (string :tag
"Tag name")
2886 (character :tag
"Access char"))
2887 (list :tag
"Start radio group"
2889 (option (string :tag
"Group description")))
2890 (list :tag
"End radio group"
2892 (option (string :tag
"Group description")))
2893 (const :tag
"New line" (:newline
)))))
2895 (defcustom org-tag-persistent-alist nil
2896 "List of tags that will always appear in all Org-mode files.
2897 This is in addition to any in buffer settings or customizations
2899 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2900 The value of this variable is an alist, the car of each entry must be a
2901 keyword as a string, the cdr may be a character that is used to select
2902 that tag through the fast-tag-selection interface.
2903 See the manual for details.
2904 To disable these tags on a per-file basis, insert anywhere in the file:
2909 (cons (string :tag
"Tag name")
2910 (character :tag
"Access char"))
2911 (const :tag
"Start radio group" (:startgroup
))
2912 (const :tag
"End radio group" (:endgroup
))
2913 (const :tag
"New line" (:newline
)))))
2915 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2916 "If non-nil, always offer completion for all tags of all agenda files.
2917 Instead of customizing this variable directly, you might want to
2918 set it locally for capture buffers, because there no list of
2919 tags in that file can be created dynamically (there are none).
2921 (add-hook 'org-capture-mode-hook
2923 (set (make-local-variable
2924 'org-complete-tags-always-offer-all-agenda-tags)
2930 (defvar org-file-tags nil
2931 "List of tags that can be inherited by all entries in the file.
2932 The tags will be inherited if the variable `org-use-tag-inheritance'
2933 says they should be.
2934 This variable is populated from #+FILETAGS lines.")
2936 (defcustom org-use-fast-tag-selection
'auto
2937 "Non-nil means use fast tag selection scheme.
2938 This is a special interface to select and deselect tags with single keys.
2939 When nil, fast selection is never used.
2940 When the symbol `auto', fast selection is used if and only if selection
2941 characters for tags have been configured, either through the variable
2942 `org-tag-alist' or through a #+TAGS line in the buffer.
2943 When t, fast selection is always used and selection keys are assigned
2944 automatically if necessary."
2947 (const :tag
"Always" t
)
2948 (const :tag
"Never" nil
)
2949 (const :tag
"When selection characters are configured" 'auto
)))
2951 (defcustom org-fast-tag-selection-single-key nil
2952 "Non-nil means fast tag selection exits after first change.
2953 When nil, you have to press RET to exit it.
2954 During fast tag selection, you can toggle this flag with `C-c'.
2955 This variable can also have the value `expert'. In this case, the window
2956 displaying the tags menu is not even shown, until you press C-c again."
2959 (const :tag
"No" nil
)
2960 (const :tag
"Yes" t
)
2961 (const :tag
"Expert" expert
)))
2963 (defvar org-fast-tag-selection-include-todo nil
2964 "Non-nil means fast tags selection interface will also offer TODO states.
2965 This is an undocumented feature, you should not rely on it.")
2967 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2968 "The column to which tags should be indented in a headline.
2969 If this number is positive, it specifies the column. If it is negative,
2970 it means that the tags should be flushright to that column. For example,
2971 -80 works well for a normal 80 character screen.
2972 When 0, place tags directly after headline text, with only one space in
2977 (defcustom org-auto-align-tags t
2978 "Non-nil keeps tags aligned when modifying headlines.
2979 Some operations (i.e. demoting) change the length of a headline and
2980 therefore shift the tags around. With this option turned on, after
2981 each such operation the tags are again aligned to `org-tags-column'."
2985 (defcustom org-use-tag-inheritance t
2986 "Non-nil means tags in levels apply also for sublevels.
2987 When nil, only the tags directly given in a specific line apply there.
2988 This may also be a list of tags that should be inherited, or a regexp that
2989 matches tags that should be inherited. Additional control is possible
2990 with the variable `org-tags-exclude-from-inheritance' which gives an
2991 explicit list of tags to be excluded from inheritance, even if the value of
2992 `org-use-tag-inheritance' would select it for inheritance.
2994 If this option is t, a match early-on in a tree can lead to a large
2995 number of matches in the subtree when constructing the agenda or creating
2996 a sparse tree. If you only want to see the first match in a tree during
2997 a search, check out the variable `org-tags-match-list-sublevels'."
3000 (const :tag
"Not" nil
)
3001 (const :tag
"Always" t
)
3002 (repeat :tag
"Specific tags" (string :tag
"Tag"))
3003 (regexp :tag
"Tags matched by regexp")))
3005 (defcustom org-tags-exclude-from-inheritance nil
3006 "List of tags that should never be inherited.
3007 This is a way to exclude a few tags from inheritance. For way to do
3008 the opposite, to actively allow inheritance for selected tags,
3009 see the variable `org-use-tag-inheritance'."
3011 :type
'(repeat (string :tag
"Tag")))
3013 (defun org-tag-inherit-p (tag)
3014 "Check if TAG is one that should be inherited."
3016 ((member tag org-tags-exclude-from-inheritance
) nil
)
3017 ((eq org-use-tag-inheritance t
) t
)
3018 ((not org-use-tag-inheritance
) nil
)
3019 ((stringp org-use-tag-inheritance
)
3020 (string-match org-use-tag-inheritance tag
))
3021 ((listp org-use-tag-inheritance
)
3022 (member tag org-use-tag-inheritance
))
3023 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3025 (defcustom org-tags-match-list-sublevels t
3026 "Non-nil means list also sublevels of headlines matching a search.
3027 This variable applies to tags/property searches, and also to stuck
3028 projects because this search is based on a tags match as well.
3030 When set to the symbol `indented', sublevels are indented with
3033 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3034 the sublevels of a headline matching a tag search often also match
3035 the same search. Listing all of them can create very long lists.
3036 Setting this variable to nil causes subtrees of a match to be skipped.
3038 This variable is semi-obsolete and probably should always be true. It
3039 is better to limit inheritance to certain tags using the variables
3040 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3043 (const :tag
"No, don't list them" nil
)
3044 (const :tag
"Yes, do list them" t
)
3045 (const :tag
"List them, indented with leading dots" indented
)))
3047 (defcustom org-tags-sort-function nil
3048 "When set, tags are sorted using this function as a comparator."
3051 (const :tag
"No sorting" nil
)
3052 (const :tag
"Alphabetical" string
<)
3053 (const :tag
"Reverse alphabetical" string
>)
3054 (function :tag
"Custom function" nil
)))
3056 (defvar org-tags-history nil
3057 "History of minibuffer reads for tags.")
3058 (defvar org-last-tags-completion-table nil
3059 "The last used completion table for tags.")
3060 (defvar org-after-tags-change-hook nil
3061 "Hook that is run after the tags in a line have changed.")
3063 (defgroup org-properties nil
3064 "Options concerning properties in Org-mode."
3065 :tag
"Org Properties"
3068 (defcustom org-property-format
"%-10s %s"
3069 "How property key/value pairs should be formatted by `indent-line'.
3070 When `indent-line' hits a property definition, it will format the line
3071 according to this format, mainly to make sure that the values are
3072 lined-up with respect to each other."
3073 :group
'org-properties
3076 (defcustom org-properties-postprocess-alist nil
3077 "Alist of properties and functions to adjust inserted values.
3078 Elements of this alist must be of the form
3080 ([string] [function])
3082 where [string] must be a property name and [function] must be a
3083 lambda expression: this lambda expression must take one argument,
3084 the value to adjust, and return the new value as a string.
3086 For example, this element will allow the property \"Remaining\"
3087 to be updated wrt the relation between the \"Effort\" property
3088 and the clock summary:
3090 ((\"Remaining\" (lambda(value)
3091 (let ((clocksum (org-clock-sum-current-item))
3092 (effort (org-duration-string-to-minutes
3093 (org-entry-get (point) \"Effort\"))))
3094 (org-minutes-to-hh:mm-string (- effort clocksum))))))"
3095 :group
'org-properties
3097 :type
'(alist :key-type
(string :tag
"Property")
3098 :value-type
(function :tag
"Function")))
3100 (defcustom org-use-property-inheritance nil
3101 "Non-nil means properties apply also for sublevels.
3103 This setting is chiefly used during property searches. Turning it on can
3104 cause significant overhead when doing a search, which is why it is not
3107 When nil, only the properties directly given in the current entry count.
3108 When t, every property is inherited. The value may also be a list of
3109 properties that should have inheritance, or a regular expression matching
3110 properties that should be inherited.
3112 However, note that some special properties use inheritance under special
3113 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3114 and the properties ending in \"_ALL\" when they are used as descriptor
3115 for valid values of a property.
3117 Note for programmers:
3118 When querying an entry with `org-entry-get', you can control if inheritance
3119 should be used. By default, `org-entry-get' looks only at the local
3120 properties. You can request inheritance by setting the inherit argument
3121 to t (to force inheritance) or to `selective' (to respect the setting
3123 :group
'org-properties
3125 (const :tag
"Not" nil
)
3126 (const :tag
"Always" t
)
3127 (repeat :tag
"Specific properties" (string :tag
"Property"))
3128 (regexp :tag
"Properties matched by regexp")))
3130 (defun org-property-inherit-p (property)
3131 "Check if PROPERTY is one that should be inherited."
3133 ((eq org-use-property-inheritance t
) t
)
3134 ((not org-use-property-inheritance
) nil
)
3135 ((stringp org-use-property-inheritance
)
3136 (string-match org-use-property-inheritance property
))
3137 ((listp org-use-property-inheritance
)
3138 (member property org-use-property-inheritance
))
3139 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3141 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3142 "The default column format, if no other format has been defined.
3143 This variable can be set on the per-file basis by inserting a line
3145 #+COLUMNS: %25ITEM ....."
3146 :group
'org-properties
3149 (defcustom org-columns-ellipses
".."
3150 "The ellipses to be used when a field in column view is truncated.
3151 When this is the empty string, as many characters as possible are shown,
3152 but then there will be no visual indication that the field has been truncated.
3153 When this is a string of length N, the last N characters of a truncated
3154 field are replaced by this string. If the column is narrower than the
3155 ellipses string, only part of the ellipses string will be shown."
3156 :group
'org-properties
3159 (defcustom org-columns-modify-value-for-display-function nil
3160 "Function that modifies values for display in column view.
3161 For example, it can be used to cut out a certain part from a time stamp.
3162 The function must take 2 arguments:
3164 column-title The title of the column (*not* the property name)
3165 value The value that should be modified.
3167 The function should return the value that should be displayed,
3168 or nil if the normal value should be used."
3169 :group
'org-properties
3172 (defcustom org-effort-property
"Effort"
3173 "The property that is being used to keep track of effort estimates.
3174 Effort estimates given in this property need to have the format H:MM."
3175 :group
'org-properties
3176 :group
'org-progress
3177 :type
'(string :tag
"Property"))
3179 (defconst org-global-properties-fixed
3180 '(("VISIBILITY_ALL" .
"folded children content all")
3181 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3182 "List of property/value pairs that can be inherited by any entry.
3184 These are fixed values, for the preset properties. The user variable
3185 that can be used to add to this list is `org-global-properties'.
3187 The entries in this list are cons cells where the car is a property
3188 name and cdr is a string with the value. If the value represents
3189 multiple items like an \"_ALL\" property, separate the items by
3192 (defcustom org-global-properties nil
3193 "List of property/value pairs that can be inherited by any entry.
3195 This list will be combined with the constant `org-global-properties-fixed'.
3197 The entries in this list are cons cells where the car is a property
3198 name and cdr is a string with the value.
3200 You can set buffer-local values for the same purpose in the variable
3201 `org-file-properties' this by adding lines like
3203 #+PROPERTY: NAME VALUE"
3204 :group
'org-properties
3206 (cons (string :tag
"Property")
3207 (string :tag
"Value"))))
3209 (defvar org-file-properties nil
3210 "List of property/value pairs that can be inherited by any entry.
3211 Valid for the current buffer.
3212 This variable is populated from #+PROPERTY lines.")
3213 (make-variable-buffer-local 'org-file-properties
)
3215 (defgroup org-agenda nil
3216 "Options concerning agenda views in Org-mode."
3220 (defvar org-category nil
3221 "Variable used by org files to set a category for agenda display.
3222 Such files should use a file variable to set it, for example
3224 # -*- mode: org; org-category: \"ELisp\"
3226 or contain a special line
3230 If the file does not specify a category, then file's base name
3232 (make-variable-buffer-local 'org-category
)
3233 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3235 (defcustom org-agenda-files nil
3236 "The files to be used for agenda display.
3237 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3238 \\[org-remove-file]. You can also use customize to edit the list.
3240 If an entry is a directory, all files in that directory that are matched by
3241 `org-agenda-file-regexp' will be part of the file list.
3243 If the value of the variable is not a list but a single file name, then
3244 the list of agenda files is actually stored and maintained in that file, one
3245 agenda file per line. In this file paths can be given relative to
3246 `org-directory'. Tilde expansion and environment variable substitution
3250 (repeat :tag
"List of files and directories" file
)
3251 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3253 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3254 "Regular expression to match files for `org-agenda-files'.
3255 If any element in the list in that variable contains a directory instead
3256 of a normal file, all files in that directory that are matched by this
3257 regular expression will be included."
3261 (defcustom org-agenda-text-search-extra-files nil
3262 "List of extra files to be searched by text search commands.
3263 These files will be search in addition to the agenda files by the
3264 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3265 Note that these files will only be searched for text search commands,
3266 not for the other agenda views like todo lists, tag searches or the weekly
3267 agenda. This variable is intended to list notes and possibly archive files
3268 that should also be searched by these two commands.
3269 In fact, if the first element in the list is the symbol `agenda-archives',
3270 than all archive files of all agenda files will be added to the search
3273 :type
'(set :greedy t
3274 (const :tag
"Agenda Archives" agenda-archives
)
3275 (repeat :inline t
(file))))
3277 (if (fboundp 'defvaralias
)
3278 (defvaralias 'org-agenda-multi-occur-extra-files
3279 'org-agenda-text-search-extra-files
))
3281 (defcustom org-agenda-skip-unavailable-files nil
3282 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3283 A nil value means to remove them, after a query, from the list."
3287 (defcustom org-calendar-to-agenda-key
[?c
]
3288 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3289 The command `org-calendar-goto-agenda' will be bound to this key. The
3290 default is the character `c' because then `c' can be used to switch back and
3291 forth between agenda and calendar."
3295 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3296 "The key to be installed in `calendar-mode-map' for adding diary entries.
3297 This option is irrelevant until `org-agenda-diary-file' has been configured
3298 to point to an Org-mode file. When that is the case, the command
3299 `org-agenda-diary-entry' will be bound to the key given here, by default
3300 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3301 if you want to continue doing this, you need to change this to a different
3306 (defcustom org-agenda-diary-file
'diary-file
3307 "File to which to add new entries with the `i' key in agenda and calendar.
3308 When this is the symbol `diary-file', the functionality in the Emacs
3309 calendar will be used to add entries to the `diary-file'. But when this
3310 points to a file, `org-agenda-diary-entry' will be used instead."
3313 (const :tag
"The standard Emacs diary file" diary-file
)
3314 (file :tag
"Special Org file diary entries")))
3316 (eval-after-load "calendar"
3318 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3319 'org-calendar-goto-agenda
)
3320 (add-hook 'calendar-mode-hook
3322 (unless (eq org-agenda-diary-file
'diary-file
)
3323 (define-key calendar-mode-map
3324 org-calendar-insert-diary-entry-key
3325 'org-agenda-diary-entry
))))))
3327 (defgroup org-latex nil
3328 "Options for embedding LaTeX code into Org-mode."
3332 (defcustom org-format-latex-options
3333 '(:foreground default
:background default
:scale
1.0
3334 :html-foreground
"Black" :html-background
"Transparent"
3335 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3336 "Options for creating images from LaTeX fragments.
3337 This is a property list with the following properties:
3338 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3339 `default' means use the foreground of the default face.
3340 :background the background color, or \"Transparent\".
3341 `default' means use the background of the default face.
3342 :scale a scaling factor for the size of the images, to get more pixels
3343 :html-foreground, :html-background, :html-scale
3344 the same numbers for HTML export.
3345 :matchers a list indicating which matchers should be used to
3346 find LaTeX fragments. Valid members of this list are:
3347 \"begin\" find environments
3348 \"$1\" find single characters surrounded by $.$
3349 \"$\" find math expressions surrounded by $...$
3350 \"$$\" find math expressions surrounded by $$....$$
3351 \"\\(\" find math expressions surrounded by \\(...\\)
3352 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3356 (defcustom org-format-latex-signal-error t
3357 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3358 When nil, just push out a message."
3363 (defcustom org-latex-to-mathml-jar-file nil
3364 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3365 Use this to specify additional executable file say a jar file.
3367 When using MathToWeb as the converter, specify the full-path to
3368 your mathtoweb.jar file."
3372 (const :tag
"None" nil
)
3373 (file :tag
"JAR file" :must-match t
)))
3375 (defcustom org-latex-to-mathml-convert-command nil
3376 "Command to convert LaTeX fragments to MathML.
3377 Replace format-specifiers in the command as noted below and use
3378 `shell-command' to convert LaTeX to MathML.
3379 %j: Executable file in fully expanded form as specified by
3380 `org-latex-to-mathml-jar-file'.
3381 %I: Input LaTeX file in fully expanded form
3382 %o: Output MathML file
3383 This command is used by `org-create-math-formula'.
3385 When using MathToWeb as the converter, set this to
3386 \"java -jar %j -unicode -force -df %o %I\"."
3390 (const :tag
"None" nil
)
3391 (string :tag
"\nShell command")))
3393 (defcustom org-latex-create-formula-image-program
'dvipng
3394 "Program to convert LaTeX fragments with.
3396 dvipng Process the LaTeX fragments to dvi file, then convert
3397 dvi files to png files using dvipng.
3398 This will also include processing of non-math environments.
3399 imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
3400 to convert pdf files to png files"
3404 (const :tag
"dvipng" dvipng
)
3405 (const :tag
"imagemagick" imagemagick
)))
3407 (defcustom org-latex-preview-ltxpng-directory
"ltxpng/"
3408 "Path to store latex preview images. A relative path here creates many
3409 directories relative to the processed org files paths. An absolute path
3410 puts all preview images at the same place."
3415 (defun org-format-latex-mathml-available-p ()
3416 "Return t if `org-latex-to-mathml-convert-command' is usable."
3418 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3419 org-latex-to-mathml-convert-command
)
3420 (let ((executable (car (split-string
3421 org-latex-to-mathml-convert-command
))))
3422 (when (executable-find executable
)
3424 "%j" org-latex-to-mathml-convert-command
)
3425 (file-readable-p org-latex-to-mathml-jar-file
)
3428 (defcustom org-format-latex-header
"\\documentclass{article}
3429 \\usepackage[usenames]{color}
3430 \\usepackage{amsmath}
3431 \\usepackage[mathscr]{eucal}
3432 \\pagestyle{empty} % do not remove
3435 % The settings below are copied from fullpage.sty
3436 \\setlength{\\textwidth}{\\paperwidth}
3437 \\addtolength{\\textwidth}{-3cm}
3438 \\setlength{\\oddsidemargin}{1.5cm}
3439 \\addtolength{\\oddsidemargin}{-2.54cm}
3440 \\setlength{\\evensidemargin}{\\oddsidemargin}
3441 \\setlength{\\textheight}{\\paperheight}
3442 \\addtolength{\\textheight}{-\\headheight}
3443 \\addtolength{\\textheight}{-\\headsep}
3444 \\addtolength{\\textheight}{-\\footskip}
3445 \\addtolength{\\textheight}{-3cm}
3446 \\setlength{\\topmargin}{1.5cm}
3447 \\addtolength{\\topmargin}{-2.54cm}"
3448 "The document header used for processing LaTeX fragments.
3449 It is imperative that this header make sure that no page number
3450 appears on the page. The package defined in the variables
3451 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
3452 will either replace the placeholder \"[PACKAGES]\" in this header, or they
3457 (defvar org-format-latex-header-extra nil
)
3459 (defun org-set-packages-alist (var val
)
3460 "Set the packages alist and make sure it has 3 elements per entry."
3461 (set var
(mapcar (lambda (x)
3462 (if (and (consp x
) (= (length x
) 2))
3463 (list (car x
) (nth 1 x
) t
)
3467 (defun org-get-packages-alist (var)
3469 "Get the packages alist and make sure it has 3 elements per entry."
3471 (if (and (consp x
) (= (length x
) 2))
3472 (list (car x
) (nth 1 x
) t
)
3474 (default-value var
)))
3476 ;; The following variables are defined here because is it also used
3477 ;; when formatting latex fragments. Originally it was part of the
3478 ;; LaTeX exporter, which is why the name includes "export".
3479 (defcustom org-export-latex-default-packages-alist
3480 '(("AUTO" "inputenc" t
)
3484 ("" "longtable" nil
)
3496 "Alist of default packages to be inserted in the header.
3497 Change this only if one of the packages here causes an incompatibility
3498 with another package you are using.
3499 The packages in this list are needed by one part or another of Org-mode
3500 to function properly.
3502 - inputenc, fontenc: for basic font and character selection
3503 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
3504 for interpreting the entities in `org-entities'. You can skip some of these
3505 packages if you don't use any of the symbols in it.
3506 - graphicx: for including images
3507 - float, wrapfig: for figure placement
3508 - longtable: for long tables
3509 - hyperref: for cross references
3511 Therefore you should not modify this variable unless you know what you
3512 are doing. The one reason to change it anyway is that you might be loading
3513 some other package that conflicts with one of the default packages.
3514 Each cell is of the format \( \"options\" \"package\" snippet-flag\).
3515 If SNIPPET-FLAG is t, the package also needs to be included when
3516 compiling LaTeX snippets into images for inclusion into HTML."
3517 :group
'org-export-latex
3518 :set
'org-set-packages-alist
3519 :get
'org-get-packages-alist
3523 (list :tag
"options/package pair"
3524 (string :tag
"options")
3525 (string :tag
"package")
3526 (boolean :tag
"Snippet"))
3527 (string :tag
"A line of LaTeX"))))
3529 (defcustom org-export-latex-packages-alist nil
3530 "Alist of packages to be inserted in every LaTeX header.
3531 These will be inserted after `org-export-latex-default-packages-alist'.
3532 Each cell is of the format \( \"options\" \"package\" snippet-flag \).
3533 SNIPPET-FLAG, when t, indicates that this package is also needed when
3534 turning LaTeX snippets into images for inclusion into HTML.
3535 Make sure that you only list packages here which:
3536 - you want in every file
3537 - do not conflict with the default packages in
3538 `org-export-latex-default-packages-alist'
3539 - do not conflict with the setup in `org-format-latex-header'."
3540 :group
'org-export-latex
3541 :set
'org-set-packages-alist
3542 :get
'org-get-packages-alist
3545 (list :tag
"options/package pair"
3546 (string :tag
"options")
3547 (string :tag
"package")
3548 (boolean :tag
"Snippet"))
3549 (string :tag
"A line of LaTeX"))))
3552 (defgroup org-appearance nil
3553 "Settings for Org-mode appearance."
3554 :tag
"Org Appearance"
3557 (defcustom org-level-color-stars-only nil
3558 "Non-nil means fontify only the stars in each headline.
3559 When nil, the entire headline is fontified.
3560 Changing it requires restart of `font-lock-mode' to become effective
3561 also in regions already fontified."
3562 :group
'org-appearance
3565 (defcustom org-hide-leading-stars nil
3566 "Non-nil means hide the first N-1 stars in a headline.
3567 This works by using the face `org-hide' for these stars. This
3568 face is white for a light background, and black for a dark
3569 background. You may have to customize the face `org-hide' to
3571 Changing it requires restart of `font-lock-mode' to become effective
3572 also in regions already fontified.
3573 You may also set this on a per-file basis by adding one of the following
3574 lines to the buffer:
3576 #+STARTUP: hidestars
3577 #+STARTUP: showstars"
3578 :group
'org-appearance
3581 (defcustom org-hidden-keywords nil
3582 "List of symbols corresponding to keywords to be hidden the org buffer.
3583 For example, a value '(title) for this list will make the document's title
3584 appear in the buffer without the initial #+TITLE: keyword."
3585 :group
'org-appearance
3587 :type
'(set (const :tag
"#+AUTHOR" author
)
3588 (const :tag
"#+DATE" date
)
3589 (const :tag
"#+EMAIL" email
)
3590 (const :tag
"#+TITLE" title
)))
3592 (defcustom org-custom-properties nil
3593 "List of properties (as strings) with a special meaning.
3594 The default use of these custom properties is to let the user
3595 hide them with `org-toggle-custom-properties-visibility'."
3596 :group
'org-properties
3597 :group
'org-appearance
3599 :type
'(repeat (string :tag
"Property Name")))
3601 (defcustom org-fontify-done-headline nil
3602 "Non-nil means change the face of a headline if it is marked DONE.
3603 Normally, only the TODO/DONE keyword indicates the state of a headline.
3604 When this is non-nil, the headline after the keyword is set to the
3605 `org-headline-done' as an additional indication."
3606 :group
'org-appearance
3609 (defcustom org-fontify-emphasized-text t
3610 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3611 Changing this variable requires a restart of Emacs to take effect."
3612 :group
'org-appearance
3615 (defcustom org-fontify-whole-heading-line nil
3616 "Non-nil means fontify the whole line for headings.
3617 This is useful when setting a background color for the
3619 :group
'org-appearance
3622 (defcustom org-highlight-latex-fragments-and-specials nil
3623 "Non-nil means fontify what is treated specially by the exporters."
3624 :group
'org-appearance
3627 (defcustom org-hide-emphasis-markers nil
3628 "Non-nil mean font-lock should hide the emphasis marker characters."
3629 :group
'org-appearance
3632 (defcustom org-pretty-entities nil
3633 "Non-nil means show entities as UTF8 characters.
3634 When nil, the \\name form remains in the buffer."
3635 :group
'org-appearance
3639 (defcustom org-pretty-entities-include-sub-superscripts t
3640 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3641 :group
'org-appearance
3645 (defvar org-emph-re nil
3646 "Regular expression for matching emphasis.
3647 After a match, the match groups contain these elements:
3648 0 The match of the full regular expression, including the characters
3649 before and after the proper match
3650 1 The character before the proper match, or empty at beginning of line
3651 2 The proper match, including the leading and trailing markers
3652 3 The leading marker like * or /, indicating the type of highlighting
3653 4 The text between the emphasis markers, not including the markers
3654 5 The character after the match, empty at the end of a line")
3655 (defvar org-verbatim-re nil
3656 "Regular expression for matching verbatim text.")
3657 (defvar org-emphasis-regexp-components
) ; defined just below
3658 (defvar org-emphasis-alist
) ; defined just below
3659 (defun org-set-emph-re (var val
)
3660 "Set variable and compute the emphasis regular expression."
3662 (when (and (boundp 'org-emphasis-alist
)
3663 (boundp 'org-emphasis-regexp-components
)
3664 org-emphasis-alist org-emphasis-regexp-components
)
3665 (let* ((e org-emphasis-regexp-components
)
3671 (body1 (concat body
"*?"))
3672 (markers (mapconcat 'car org-emphasis-alist
""))
3673 (vmarkers (mapconcat
3674 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3675 org-emphasis-alist
"")))
3676 ;; make sure special characters appear at the right position in the class
3677 (if (string-match "\\^" markers
)
3678 (setq markers
(concat (replace-match "" t t markers
) "^")))
3679 (if (string-match "-" markers
)
3680 (setq markers
(concat (replace-match "" t t markers
) "-")))
3681 (if (string-match "\\^" vmarkers
)
3682 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3683 (if (string-match "-" vmarkers
)
3684 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3686 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3687 (int-to-string nl
) "\\}")))
3690 (concat "\\([" pre
"]\\|^\\)"
3692 "\\([" markers
"]\\)"
3700 "\\([" post
"]\\|$\\)"))
3701 (setq org-verbatim-re
3702 (concat "\\([" pre
"]\\|^\\)"
3704 "\\([" vmarkers
"]\\)"
3712 "\\([" post
"]\\|$\\)")))))
3714 (defcustom org-emphasis-regexp-components
3715 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3716 "Components used to build the regular expression for emphasis.
3717 This is a list with five entries. Terminology: In an emphasis string
3718 like \" *strong word* \", we call the initial space PREMATCH, the final
3719 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3720 and \"trong wor\" is the body. The different components in this variable
3721 specify what is allowed/forbidden in each part:
3723 pre Chars allowed as prematch. Beginning of line will be allowed too.
3724 post Chars allowed as postmatch. End of line will be allowed too.
3725 border The chars *forbidden* as border characters.
3726 body-regexp A regexp like \".\" to match a body character. Don't use
3727 non-shy groups here, and don't allow newline here.
3728 newline The maximum number of newlines allowed in an emphasis exp.
3730 Use customize to modify this, or restart Emacs after changing it."
3731 :group
'org-appearance
3732 :set
'org-set-emph-re
3734 (sexp :tag
"Allowed chars in pre ")
3735 (sexp :tag
"Allowed chars in post ")
3736 (sexp :tag
"Forbidden chars in border ")
3737 (sexp :tag
"Regexp for body ")
3738 (integer :tag
"number of newlines allowed")
3739 (option (boolean :tag
"Please ignore this button"))))
3741 (defcustom org-emphasis-alist
3742 `(("*" bold
"<b>" "</b>")
3743 ("/" italic
"<i>" "</i>")
3744 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
3745 ("=" org-code
"<code>" "</code>" verbatim
)
3746 ("~" org-verbatim
"<code>" "</code>" verbatim
)
3747 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
3750 "Special syntax for emphasized text.
3751 Text starting and ending with a special character will be emphasized, for
3752 example *bold*, _underlined_ and /italic/. This variable sets the marker
3753 characters, the face to be used by font-lock for highlighting in Org-mode
3754 Emacs buffers, and the HTML tags to be used for this.
3755 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3756 For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
3757 Use customize to modify this, or restart Emacs after changing it."
3758 :group
'org-appearance
3759 :set
'org-set-emph-re
3762 (string :tag
"Marker character")
3764 (face :tag
"Font-lock-face")
3765 (plist :tag
"Face property list"))
3766 (string :tag
"HTML start tag")
3767 (string :tag
"HTML end tag")
3768 (option (const verbatim
)))))
3770 (defvar org-protecting-blocks
3771 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3772 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3773 This is needed for font-lock setup.")
3775 ;;; Miscellaneous options
3777 (defgroup org-completion nil
3778 "Completion in Org-mode."
3779 :tag
"Org Completion"
3782 (defcustom org-completion-use-ido nil
3783 "Non-nil means use ido completion wherever possible.
3784 Note that `ido-mode' must be active for this variable to be relevant.
3785 If you decide to turn this variable on, you might well want to turn off
3786 `org-outline-path-complete-in-steps'.
3787 See also `org-completion-use-iswitchb'."
3788 :group
'org-completion
3791 (defcustom org-completion-use-iswitchb nil
3792 "Non-nil means use iswitchb completion wherever possible.
3793 Note that `iswitchb-mode' must be active for this variable to be relevant.
3794 If you decide to turn this variable on, you might well want to turn off
3795 `org-outline-path-complete-in-steps'.
3796 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3797 :group
'org-completion
3800 (defcustom org-completion-fallback-command
'hippie-expand
3801 "The expansion command called by \\[pcomplete] in normal context.
3802 Normal means, no org-mode-specific context."
3803 :group
'org-completion
3806 ;;; Functions and variables from their packages
3807 ;; Declared here to avoid compiler warnings
3810 (defvar outline-mode-menu-heading
)
3811 (defvar outline-mode-menu-show
)
3812 (defvar outline-mode-menu-hide
)
3813 (defvar zmacs-regions
) ; XEmacs regions
3816 (defvar mark-active
)
3819 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
3820 (declare-function calendar-forward-day
"cal-move" (arg))
3821 (declare-function calendar-goto-date
"cal-move" (date))
3822 (declare-function calendar-goto-today
"cal-move" ())
3823 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
3824 (defvar calc-embedded-close-formula
)
3825 (defvar calc-embedded-open-formula
)
3826 (declare-function cdlatex-tab
"ext:cdlatex" ())
3827 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
3828 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
3829 (defvar font-lock-unfontify-region-function
)
3830 (declare-function iswitchb-read-buffer
"iswitchb"
3831 (prompt &optional default require-match start matches-set
))
3832 (defvar iswitchb-temp-buflist
)
3833 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
3834 (defvar org-agenda-tags-todo-honor-ignore-options
)
3835 (declare-function org-agenda-skip
"org-agenda" ())
3837 org-agenda-format-item
"org-agenda"
3838 (extra txt
&optional category tags dotime noprefix remove-re habitp
))
3839 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
3840 (declare-function org-agenda-change-all-lines
"org-agenda"
3841 (newhead hdmarker
&optional fixface just-this
))
3842 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
3843 (declare-function org-agenda-maybe-redo
"org-agenda" ())
3844 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
3846 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
3847 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3848 "org-agenda" (&optional end
))
3849 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
3850 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
3851 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
3852 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
3853 (declare-function org-indent-mode
"org-indent" (&optional arg
))
3854 (declare-function parse-time-string
"parse-time" (string))
3855 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
3856 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
3857 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
3858 (defvar remember-data-file
)
3859 (defvar texmathp-why
)
3860 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
3861 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
3863 (defvar w3m-current-url
)
3864 (defvar w3m-current-title
)
3866 (defvar org-latex-regexps
)
3868 ;;; Autoload and prepare some org modules
3870 ;; Some table stuff that needs to be defined here, because it is used
3871 ;; by the functions setting up org-mode or checking for table context.
3873 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
3874 "Detect an org-type or table-type table.")
3875 (defconst org-table-line-regexp
"^[ \t]*|"
3876 "Detect an org-type table line.")
3877 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
3878 "Detect an org-type table line.")
3879 (defconst org-table-hline-regexp
"^[ \t]*|-"
3880 "Detect an org-type table hline.")
3881 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
3882 "Detect a table-type table hline.")
3883 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
3884 "Detect the first line outside a table when searching from within it.
3885 This works for both table types.")
3887 ;; Autoload the functions in org-table.el that are needed by functions here.
3890 (org-autoload "org-table"
3891 '(org-table-begin org-table-blank-field org-table-end
)))
3894 (defun turn-on-orgtbl ()
3895 "Unconditionally turn on `orgtbl-mode'."
3896 (require 'org-table
)
3899 (defun org-at-table-p (&optional table-type
)
3900 "Return t if the cursor is inside an org-type table.
3901 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3902 (if org-enable-table-editor
3904 (beginning-of-line 1)
3905 (looking-at (if table-type org-table-any-line-regexp
3906 org-table-line-regexp
)))
3908 (defsubst org-table-p
() (org-at-table-p))
3910 (defun org-at-table.el-p
()
3911 "Return t if and only if we are at a table.el table."
3912 (and (org-at-table-p 'any
)
3914 (goto-char (org-table-begin 'any
))
3915 (looking-at org-table1-hline-regexp
))))
3916 (defun org-table-recognize-table.el
()
3917 "If there is a table.el table nearby, recognize it and move into it."
3918 (if org-table-tab-recognizes-table.el
3919 (if (org-at-table.el-p
)
3921 (beginning-of-line 1)
3922 (if (looking-at org-table-dataline-regexp
)
3924 (if (looking-at org-table1-hline-regexp
)
3926 (beginning-of-line 2)
3927 (if (looking-at org-table-any-border-regexp
)
3928 (beginning-of-line -
1)))))
3929 (if (re-search-forward "|" (org-table-end t
) t
)
3932 (if (table--at-cell-p (point))
3934 (message "recognizing table.el table...")
3935 (table-recognize-table)
3936 (message "recognizing table.el table...done")))
3937 (error "This should not happen"))
3942 (defun org-at-table-hline-p ()
3943 "Return t if the cursor is inside a hline in a table."
3944 (if org-enable-table-editor
3946 (beginning-of-line 1)
3947 (looking-at org-table-hline-regexp
))
3950 (defvar org-table-clean-did-remove-column nil
)
3952 (defun org-table-map-tables (function &optional quietly
)
3953 "Apply FUNCTION to the start of all tables in the buffer."
3957 (goto-char (point-min))
3958 (while (re-search-forward org-table-any-line-regexp nil t
)
3960 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
3961 (beginning-of-line 1)
3962 (when (and (looking-at org-table-line-regexp
)
3963 ;; Exclude tables in src/example/verbatim/clocktable blocks
3964 (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
3965 (save-excursion (funcall function
))
3966 (or (looking-at org-table-line-regexp
)
3968 (re-search-forward org-table-any-border-regexp nil
1))))
3969 (unless quietly
(message "Mapping tables: done")))
3971 ;; Declare and autoload functions from org-exp.el & Co
3973 (declare-function org-default-export-plist
"org-exp")
3974 (declare-function org-infile-export-plist
"org-exp")
3975 (declare-function org-get-current-options
"org-exp")
3977 ;; Declare and autoload functions from org-agenda.el
3980 (org-autoload "org-agenda"
3981 '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
3983 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock" (beg end
))
3984 (declare-function org-clock-update-mode-line
"org-clock" ())
3985 (declare-function org-resolve-clocks
"org-clock"
3986 (&optional also-non-dangling-p prompt last-valid
))
3987 (defvar org-clock-start-time
)
3988 (defvar org-clock-marker
(make-marker)
3989 "Marker recording the last clock-in.")
3990 (defvar org-clock-hd-marker
(make-marker)
3991 "Marker recording the last clock-in, but the headline position.")
3992 (defvar org-clock-heading
""
3993 "The heading of the current clock entry.")
3994 (defun org-clock-is-active ()
3995 "Return non-nil if clock is currently running.
3996 The return value is actually the clock marker."
3997 (marker-buffer org-clock-marker
))
4000 (org-autoload "org-clock" '(org-clock-remove-overlays
4001 org-clock-update-time-maybe
4002 org-clocktable-shift
)))
4004 (defun org-check-running-clock ()
4005 "Check if the current buffer contains the running clock.
4006 If yes, offer to stop it and to save the buffer with the changes."
4007 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4008 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4011 (when (y-or-n-p "Save changed buffer?")
4014 (defun org-clocktable-try-shift (dir n
)
4015 "Check if this line starts a clock table, if yes, shift the time block."
4016 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4017 (org-clocktable-shift dir n
)))
4020 (defun org-clock-persistence-insinuate ()
4021 "Set up hooks for clock persistence."
4022 (require 'org-clock
)
4023 (add-hook 'org-mode-hook
'org-clock-load
)
4024 (add-hook 'kill-emacs-hook
'org-clock-save
))
4026 ;; Define the variable already here, to make sure we have it.
4027 (defvar org-indent-mode nil
4028 "Non-nil if Org-Indent mode is enabled.
4029 Use the command `org-indent-mode' to change this variable.")
4031 ;; Autoload archiving code
4032 ;; The stuff that is needed for cycling and tags has to be defined here.
4034 (defgroup org-archive nil
4035 "Options concerning archiving in Org-mode."
4037 :group
'org-structure
)
4039 (defcustom org-archive-location
"%s_archive::"
4040 "The location where subtrees should be archived.
4042 The value of this variable is a string, consisting of two parts,
4043 separated by a double-colon. The first part is a filename and
4044 the second part is a headline.
4046 When the filename is omitted, archiving happens in the same file.
4047 %s in the filename will be replaced by the current file
4048 name (without the directory part). Archiving to a different file
4049 is useful to keep archived entries from contributing to the
4052 The archived entries will be filed as subtrees of the specified
4053 headline. When the headline is omitted, the subtrees are simply
4054 filed away at the end of the file, as top-level entries. Also in
4055 the heading you can use %s to represent the file name, this can be
4056 useful when using the same archive for a number of different files.
4058 Here are a few examples:
4060 If the current file is Projects.org, archive in file
4061 Projects.org_archive, as top-level trees. This is the default.
4063 \"::* Archived Tasks\"
4064 Archive in the current file, under the top-level headline
4065 \"* Archived Tasks\".
4067 \"~/org/archive.org::\"
4068 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4070 \"~/org/archive.org::* From %s\"
4071 Archive in file ~/org/archive.org (absolute path), under headlines
4072 \"From FILENAME\" where file name is the current file name.
4074 \"~/org/datetree.org::datetree/* Finished Tasks\"
4075 The \"datetree/\" string is special, signifying to archive
4076 items to the datetree. Items are placed in either the CLOSED
4077 date of the item, or the current date if there is no CLOSED date.
4078 The heading will be a subentry to the current date. There doesn't
4079 need to be a heading, but there always needs to be a slash after
4080 datetree. For example, to store archived items directly in the
4081 datetree, use \"~/org/datetree.org::datetree/\".
4083 \"basement::** Finished Tasks\"
4084 Archive in file ./basement (relative path), as level 3 trees
4085 below the level 2 heading \"** Finished Tasks\".
4087 You may set this option on a per-file basis by adding to the buffer a
4090 #+ARCHIVE: basement::** Finished Tasks
4092 You may also define it locally for a subtree by setting an ARCHIVE property
4093 in the entry. If such a property is found in an entry, or anywhere up
4094 the hierarchy, it will be used."
4098 (defcustom org-archive-tag
"ARCHIVE"
4099 "The tag that marks a subtree as archived.
4100 An archived subtree does not open during visibility cycling, and does
4101 not contribute to the agenda listings.
4102 After changing this, font-lock must be restarted in the relevant buffers to
4103 get the proper fontification."
4105 :group
'org-keywords
4108 (defcustom org-agenda-skip-archived-trees t
4109 "Non-nil means the agenda will skip any items located in archived trees.
4110 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4111 variable is no longer recommended, you should leave it at the value t.
4112 Instead, use the key `v' to cycle the archives-mode in the agenda."
4114 :group
'org-agenda-skip
4117 (defcustom org-columns-skip-archived-trees t
4118 "Non-nil means ignore archived trees when creating column view."
4120 :group
'org-properties
4123 (defcustom org-cycle-open-archived-trees nil
4124 "Non-nil means `org-cycle' will open archived trees.
4125 An archived tree is a tree marked with the tag ARCHIVE.
4126 When nil, archived trees will stay folded. You can still open them with
4127 normal outline commands like `show-all', but not with the cycling commands."
4132 (defcustom org-sparse-tree-open-archived-trees nil
4133 "Non-nil means sparse tree construction shows matches in archived trees.
4134 When nil, matches in these trees are highlighted, but the trees are kept in
4137 :group
'org-sparse-trees
4140 (defcustom org-sparse-tree-default-date-type
'scheduled-or-deadline
4141 "The default date type when building a sparse tree.
4142 When this is nil, a date is a scheduled or a deadline timestamp.
4143 Otherwise, these types are allowed:
4146 active: only active timestamps (<...>)
4147 inactive: only inactive timestamps (<...)
4148 scheduled: only scheduled timestamps
4149 deadline: only deadline timestamps"
4150 :type
'(choice (const :tag
"Scheduled or deadline" 'scheduled-or-deadline
)
4151 (const :tag
"All timestamps" all
)
4152 (const :tag
"Only active timestamps" active
)
4153 (const :tag
"Only inactive timestamps" inactive
)
4154 (const :tag
"Only scheduled timestamps" scheduled
)
4155 (const :tag
"Only deadline timestamps" deadline
))
4157 :group
'org-sparse-trees
)
4159 (defun org-cycle-hide-archived-subtrees (state)
4160 "Re-hide all archived subtrees after a visibility state change."
4161 (when (and (not org-cycle-open-archived-trees
)
4162 (not (memq state
'(overview folded
))))
4164 (let* ((globalp (memq state
'(contents all
)))
4165 (beg (if globalp
(point-min) (point)))
4166 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4167 (org-hide-archived-subtrees beg end
)
4169 (if (looking-at (concat ".*:" org-archive-tag
":"))
4170 (message "%s" (substitute-command-keys
4171 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4173 (defun org-force-cycle-archived ()
4174 "Cycle subtree even if it is archived."
4176 (setq this-command
'org-cycle
)
4177 (let ((org-cycle-open-archived-trees t
))
4178 (call-interactively 'org-cycle
)))
4180 (defun org-hide-archived-subtrees (beg end
)
4181 "Re-hide all archived subtrees after a visibility state change."
4183 (let* ((re (concat ":" org-archive-tag
":")))
4185 (while (re-search-forward re end t
)
4186 (when (org-at-heading-p)
4187 (org-flag-subtree t
)
4188 (org-end-of-subtree t
))))))
4190 (declare-function outline-end-of-heading
"outline" ())
4191 (declare-function outline-flag-region
"outline" (from to flag
))
4192 (defun org-flag-subtree (flag)
4194 (org-back-to-heading t
)
4195 (outline-end-of-heading)
4196 (outline-flag-region (point)
4197 (progn (org-end-of-subtree t
) (point))
4200 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4203 (org-autoload "org-archive"
4204 '(org-add-archive-files)))
4206 ;; Autoload Column View Code
4208 (declare-function org-columns-number-to-string
"org-colview" (n fmt
&optional printf
))
4209 (declare-function org-columns-get-format-and-top-level
"org-colview" ())
4210 (declare-function org-columns-compute
"org-colview" (property))
4212 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4213 '(org-columns-number-to-string
4214 org-columns-get-format-and-top-level
4216 org-columns-remove-overlays
))
4220 (declare-function org-id-store-link
"org-id")
4221 (declare-function org-id-locations-load
"org-id")
4222 (declare-function org-id-locations-save
"org-id")
4223 (defvar org-id-track-globally
)
4224 (org-autoload "org-id"
4227 org-id-get-with-outline-path-completion
4228 org-id-get-with-outline-drilling
))
4230 ;;; Variables for pre-computed regular expressions, all buffer local
4232 (defvar org-drawer-regexp
"^[ \t]*:PROPERTIES:[ \t]*$"
4233 "Matches first line of a hidden block.")
4234 (make-variable-buffer-local 'org-drawer-regexp
)
4235 (defvar org-todo-regexp nil
4236 "Matches any of the TODO state keywords.")
4237 (make-variable-buffer-local 'org-todo-regexp
)
4238 (defvar org-not-done-regexp nil
4239 "Matches any of the TODO state keywords except the last one.")
4240 (make-variable-buffer-local 'org-not-done-regexp
)
4241 (defvar org-not-done-heading-regexp nil
4242 "Matches a TODO headline that is not done.")
4243 (make-variable-buffer-local 'org-not-done-regexp
)
4244 (defvar org-todo-line-regexp nil
4245 "Matches a headline and puts TODO state into group 2 if present.")
4246 (make-variable-buffer-local 'org-todo-line-regexp
)
4247 (defvar org-complex-heading-regexp nil
4248 "Matches a headline and puts everything into groups:
4250 group 2: The todo keyword, maybe
4251 group 3: Priority cookie
4252 group 4: True headline
4254 (make-variable-buffer-local 'org-complex-heading-regexp
)
4255 (defvar org-complex-heading-regexp-format nil
4256 "Printf format to make regexp to match an exact headline.
4257 This regexp will match the headline of any node which has the
4258 exact headline text that is put into the format, but may have any
4259 TODO state, priority and tags.")
4260 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4261 (defvar org-todo-line-tags-regexp nil
4262 "Matches a headline and puts TODO state into group 2 if present.
4263 Also put tags into group 4 if tags are present.")
4264 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4265 (defvar org-ds-keyword-length
12
4266 "Maximum length of the DEADLINE and SCHEDULED keywords.")
4267 (make-variable-buffer-local 'org-ds-keyword-length
)
4268 (defvar org-deadline-regexp nil
4269 "Matches the DEADLINE keyword.")
4270 (make-variable-buffer-local 'org-deadline-regexp
)
4271 (defvar org-deadline-time-regexp nil
4272 "Matches the DEADLINE keyword together with a time stamp.")
4273 (make-variable-buffer-local 'org-deadline-time-regexp
)
4274 (defvar org-deadline-line-regexp nil
4275 "Matches the DEADLINE keyword and the rest of the line.")
4276 (make-variable-buffer-local 'org-deadline-line-regexp
)
4277 (defvar org-scheduled-regexp nil
4278 "Matches the SCHEDULED keyword.")
4279 (make-variable-buffer-local 'org-scheduled-regexp
)
4280 (defvar org-scheduled-time-regexp nil
4281 "Matches the SCHEDULED keyword together with a time stamp.")
4282 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4283 (defvar org-closed-time-regexp nil
4284 "Matches the CLOSED keyword together with a time stamp.")
4285 (make-variable-buffer-local 'org-closed-time-regexp
)
4287 (defvar org-keyword-time-regexp nil
4288 "Matches any of the 4 keywords, together with the time stamp.")
4289 (make-variable-buffer-local 'org-keyword-time-regexp
)
4290 (defvar org-keyword-time-not-clock-regexp nil
4291 "Matches any of the 3 keywords, together with the time stamp.")
4292 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4293 (defvar org-maybe-keyword-time-regexp nil
4294 "Matches a timestamp, possibly preceded by a keyword.")
4295 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4296 (defvar org-all-time-keywords nil
4297 "List of time keywords.")
4298 (make-variable-buffer-local 'org-all-time-keywords
)
4300 (defconst org-plain-time-of-day-regexp
4303 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4306 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4308 "Regular expression to match a plain time or time range.
4309 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4310 groups carry important information:
4312 1 the first time, range or not
4313 8 the second time, if it is a range.")
4315 (defconst org-plain-time-extension-regexp
4318 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4319 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4320 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4321 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4322 groups carry important information:
4325 9 minutes of duration")
4327 (defconst org-stamp-time-of-day-regexp
4329 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4330 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4332 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4333 "Regular expression to match a timestamp time or time range.
4334 After a match, the following groups carry important information:
4336 1 date plus weekday, for back referencing to make sure both times are on the same day
4337 2 the first time, range or not
4338 4 the second time, if it is a range.")
4340 (defconst org-startup-options
4341 '(("fold" org-startup-folded t
)
4342 ("overview" org-startup-folded t
)
4343 ("nofold" org-startup-folded nil
)
4344 ("showall" org-startup-folded nil
)
4345 ("showeverything" org-startup-folded showeverything
)
4346 ("content" org-startup-folded content
)
4347 ("indent" org-startup-indented t
)
4348 ("noindent" org-startup-indented nil
)
4349 ("hidestars" org-hide-leading-stars t
)
4350 ("showstars" org-hide-leading-stars nil
)
4351 ("odd" org-odd-levels-only t
)
4352 ("oddeven" org-odd-levels-only nil
)
4353 ("align" org-startup-align-all-tables t
)
4354 ("noalign" org-startup-align-all-tables nil
)
4355 ("inlineimages" org-startup-with-inline-images t
)
4356 ("noinlineimages" org-startup-with-inline-images nil
)
4357 ("customtime" org-display-custom-times t
)
4358 ("logdone" org-log-done time
)
4359 ("lognotedone" org-log-done note
)
4360 ("nologdone" org-log-done nil
)
4361 ("lognoteclock-out" org-log-note-clock-out t
)
4362 ("nolognoteclock-out" org-log-note-clock-out nil
)
4363 ("logrepeat" org-log-repeat state
)
4364 ("lognoterepeat" org-log-repeat note
)
4365 ("nologrepeat" org-log-repeat nil
)
4366 ("logreschedule" org-log-reschedule time
)
4367 ("lognotereschedule" org-log-reschedule note
)
4368 ("nologreschedule" org-log-reschedule nil
)
4369 ("logredeadline" org-log-redeadline time
)
4370 ("lognoteredeadline" org-log-redeadline note
)
4371 ("nologredeadline" org-log-redeadline nil
)
4372 ("logrefile" org-log-refile time
)
4373 ("lognoterefile" org-log-refile note
)
4374 ("nologrefile" org-log-refile nil
)
4375 ("fninline" org-footnote-define-inline t
)
4376 ("nofninline" org-footnote-define-inline nil
)
4377 ("fnlocal" org-footnote-section nil
)
4378 ("fnauto" org-footnote-auto-label t
)
4379 ("fnprompt" org-footnote-auto-label nil
)
4380 ("fnconfirm" org-footnote-auto-label confirm
)
4381 ("fnplain" org-footnote-auto-label plain
)
4382 ("fnadjust" org-footnote-auto-adjust t
)
4383 ("nofnadjust" org-footnote-auto-adjust nil
)
4384 ("constcgs" constants-unit-system cgs
)
4385 ("constSI" constants-unit-system SI
)
4386 ("noptag" org-tag-persistent-alist nil
)
4387 ("hideblocks" org-hide-block-startup t
)
4388 ("nohideblocks" org-hide-block-startup nil
)
4389 ("beamer" org-startup-with-beamer-mode t
)
4390 ("entitiespretty" org-pretty-entities t
)
4391 ("entitiesplain" org-pretty-entities nil
))
4392 "Variable associated with STARTUP options for org-mode.
4393 Each element is a list of three items: the startup options (as written
4394 in the #+STARTUP line), the corresponding variable, and the value to set
4395 this variable to if the option is found. An optional forth element PUSH
4396 means to push this value onto the list in the variable.")
4398 (defun org-update-property-plist (key val props
)
4399 "Update PROPS with KEY and VAL."
4400 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4401 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4402 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4403 (previous (cdr (assoc key props
))))
4405 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4406 (cons (cons key val
) remainder
))))
4408 (defconst org-block-regexp
4409 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4410 "Regular expression for hiding blocks.")
4411 (defconst org-heading-keyword-regexp-format
4412 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4413 "Printf format for a regexp matching an headline with some keyword.
4414 This regexp will match the headline of any node which has the
4415 exact keyword that is put into the format. The keyword isn't in
4416 any group by default, but the stars and the body are.")
4417 (defconst org-heading-keyword-maybe-regexp-format
4418 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4419 "Printf format for a regexp matching an headline, possibly with some keyword.
4420 This regexp can match any headline with the specified keyword, or
4421 without a keyword. The keyword isn't in any group by default,
4422 but the stars and the body are.")
4424 (defun org-set-regexps-and-options ()
4425 "Precompute regular expressions for current buffer."
4426 (when (derived-mode-p 'org-mode
)
4427 (org-set-local 'org-todo-kwd-alist nil
)
4428 (org-set-local 'org-todo-key-alist nil
)
4429 (org-set-local 'org-todo-key-trigger nil
)
4430 (org-set-local 'org-todo-keywords-1 nil
)
4431 (org-set-local 'org-done-keywords nil
)
4432 (org-set-local 'org-todo-heads nil
)
4433 (org-set-local 'org-todo-sets nil
)
4434 (org-set-local 'org-todo-log-states nil
)
4435 (org-set-local 'org-file-properties nil
)
4436 (org-set-local 'org-file-tags nil
)
4437 (let ((re (org-make-options-regexp
4438 '("CATEGORY" "TODO" "COLUMNS"
4439 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
4440 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
4442 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4444 (scripts org-use-sub-superscripts
)
4445 kwds kws0 kwsa key log value cat arch tags const links hw dws
4446 tail sep kws1 prio props ftags drawers beamer-p
4447 ext-setup-or-nil setup-contents
(start 0))
4451 (goto-char (point-min))
4452 (while (or (and ext-setup-or-nil
4453 (string-match re ext-setup-or-nil start
)
4454 (setq start
(match-end 0)))
4455 (and (setq ext-setup-or-nil nil start
0)
4456 (re-search-forward re nil t
)))
4457 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4458 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4459 (if (stringp value
) (setq value
(org-trim value
)))
4461 ((equal key
"CATEGORY")
4463 ((member key
'("SEQ_TODO" "TODO"))
4464 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4465 ((equal key
"TYP_TODO")
4466 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4467 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4468 ;; general TODO-like setup
4469 (push (cons (intern (downcase (match-string 1 key
)))
4470 (org-split-string value splitre
)) kwds
))
4472 (setq tags
(append tags
(if tags
'("\\n") nil
)
4473 (org-split-string value splitre
))))
4474 ((equal key
"COLUMNS")
4475 (org-set-local 'org-columns-default-format value
))
4477 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4478 (push (cons (match-string 1 value
)
4479 (org-trim (match-string 2 value
)))
4481 ((equal key
"PRIORITIES")
4482 (setq prio
(org-split-string value
" +")))
4483 ((equal key
"PROPERTY")
4484 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4485 (setq props
(org-update-property-plist (match-string 1 value
)
4486 (match-string 2 value
)
4488 ((equal key
"FILETAGS")
4489 (when (string-match "\\S-" value
)
4494 (mapcar (lambda (x) (org-split-string x
":"))
4495 (org-split-string value
)))))))
4496 ((equal key
"DRAWERS")
4497 (setq drawers
(delete-dups (append org-drawers
(org-split-string value splitre
)))))
4498 ((equal key
"CONSTANTS")
4499 (setq const
(append const
(org-split-string value splitre
))))
4500 ((equal key
"STARTUP")
4501 (let ((opts (org-split-string value splitre
))
4503 (while (setq l
(pop opts
))
4504 (when (setq l
(assoc l org-startup-options
))
4505 (setq var
(nth 1 l
) val
(nth 2 l
))
4507 (set (make-local-variable var
) val
)
4508 (if (not (listp (symbol-value var
)))
4509 (set (make-local-variable var
) nil
))
4510 (set (make-local-variable var
) (symbol-value var
))
4511 (add-to-list var val
))))))
4512 ((equal key
"ARCHIVE")
4514 (remove-text-properties 0 (length arch
)
4515 '(face t fontified t
) arch
))
4516 ((equal key
"LATEX_CLASS")
4517 (setq beamer-p
(equal value
"beamer")))
4518 ((equal key
"OPTIONS")
4519 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4520 (setq scripts
(read (match-string 2 value
)))))
4521 ((equal key
"SETUPFILE")
4522 (setq setup-contents
(org-file-contents
4524 (org-remove-double-quotes value
))
4526 (if (not ext-setup-or-nil
)
4527 (setq ext-setup-or-nil setup-contents start
0)
4528 (setq ext-setup-or-nil
4529 (concat (substring ext-setup-or-nil
0 start
)
4530 "\n" setup-contents
"\n"
4531 (substring ext-setup-or-nil start
)))))))
4532 ;; search for property blocks
4533 (goto-char (point-min))
4534 (while (re-search-forward org-block-regexp nil t
)
4535 (when (equal "PROPERTY" (upcase (match-string 1)))
4536 (setq value
(replace-regexp-in-string
4537 "[\n\r]" " " (match-string 4)))
4538 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4539 (setq props
(org-update-property-plist (match-string 1 value
)
4540 (match-string 2 value
)
4542 (org-set-local 'org-use-sub-superscripts scripts
)
4544 (org-set-local 'org-category
(intern cat
))
4545 (push (cons "CATEGORY" cat
) props
))
4547 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4548 (setq prio
(mapcar 'string-to-char prio
))
4549 (org-set-local 'org-highest-priority
(nth 0 prio
))
4550 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4551 (org-set-local 'org-default-priority
(nth 2 prio
)))
4552 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4553 (and ftags
(org-set-local 'org-file-tags
4554 (mapcar 'org-add-prop-inherited ftags
)))
4555 (and drawers
(org-set-local 'org-drawers drawers
))
4556 (and arch
(org-set-local 'org-archive-location arch
))
4557 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4558 ;; Process the TODO keywords
4560 ;; Use the global values as if they had been given locally.
4561 (setq kwds
(default-value 'org-todo-keywords
))
4562 (if (stringp (car kwds
))
4563 (setq kwds
(list (cons org-todo-interpretation
4564 (default-value 'org-todo-keywords
)))))
4565 (setq kwds
(reverse kwds
)))
4566 (setq kwds
(nreverse kwds
))
4568 (while (setq kws
(pop kwds
))
4570 (run-hook-with-args-until-success
4571 'org-todo-setup-filter-hook kws
)
4573 (setq inter
(pop kws
) sep
(member "|" kws
)
4574 kws0
(delete "|" (copy-sequence kws
))
4579 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4581 (setq kw
(match-string 1 x
)
4582 key
(and (match-end 2) (match-string 2 x
))
4583 log
(org-extract-log-state-settings x
))
4584 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4585 (and log
(push log org-todo-log-states
))
4587 (error "Invalid TODO keyword %s" x
)))
4589 kwsa
(if kwsa
(append '((:startgroup
))
4593 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4594 tail
(list inter hw
(car dws
) (org-last dws
))))
4595 (add-to-list 'org-todo-heads hw
'append
)
4596 (push kws1 org-todo-sets
)
4597 (setq org-done-keywords
(append org-done-keywords dws nil
))
4598 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4599 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4600 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4601 (setq org-todo-sets
(nreverse org-todo-sets
)
4602 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4603 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4604 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4605 ;; Process the constants
4608 (while (setq e
(pop const
))
4609 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4610 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4611 (setq org-table-formula-constants-local cst
)))
4613 ;; Process the tags.
4616 (while (setq e
(pop tags
))
4618 ((equal e
"{") (push '(:startgroup
) tgs
))
4619 ((equal e
"}") (push '(:endgroup
) tgs
))
4620 ((equal e
"\\n") (push '(:newline
) tgs
))
4621 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4622 (push (cons (match-string 1 e
)
4623 (string-to-char (match-string 2 e
)))
4625 (t (push (list e
) tgs
))))
4626 (org-set-local 'org-tag-alist nil
)
4627 (while (setq e
(pop tgs
))
4628 (or (and (stringp (car e
))
4629 (assoc (car e
) org-tag-alist
))
4630 (push e org-tag-alist
)))))
4632 ;; Compute the regular expressions and other local variables.
4633 ;; Using `org-outline-regexp-bol' would complicate them much,
4634 ;; because of the fixed white space at the end of that string.
4635 (if (not org-done-keywords
)
4636 (setq org-done-keywords
(and org-todo-keywords-1
4637 (list (org-last org-todo-keywords-1
)))))
4638 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4639 (length org-scheduled-string
)
4640 (length org-clock-string
)
4641 (length org-closed-string
)))
4643 (concat "^[ \t]*:\\("
4644 (mapconcat 'regexp-quote org-drawers
"\\|")
4646 org-not-done-keywords
4647 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4650 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4654 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4656 org-not-done-heading-regexp
4657 (format org-heading-keyword-regexp-format org-not-done-regexp
)
4658 org-todo-line-regexp
4659 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
4660 org-complex-heading-regexp
4661 (concat "^\\(\\*+\\)"
4662 "\\(?: +" org-todo-regexp
"\\)?"
4663 "\\(?: +\\(\\[#.\\]\\)\\)?"
4664 "\\(?: +\\(.*?\\)\\)??"
4665 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
4667 org-complex-heading-regexp-format
4668 (concat "^\\(\\*+\\)"
4669 "\\(?: +" org-todo-regexp
"\\)?"
4670 "\\(?: +\\(\\[#.\\]\\)\\)?"
4672 ;; Stats cookies can be stuck to body.
4673 "\\(?:\\[[0-9%%/]+\\] *\\)?"
4675 "\\(?: *\\[[0-9%%/]+\\]\\)?"
4677 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
4679 org-todo-line-tags-regexp
4680 (concat "^\\(\\*+\\)"
4681 "\\(?: +" org-todo-regexp
"\\)?"
4682 "\\(?: +\\(.*?\\)\\)??"
4683 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
4685 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4686 org-deadline-time-regexp
4687 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4688 org-deadline-line-regexp
4689 (concat "\\<\\(" org-deadline-string
"\\).*")
4690 org-scheduled-regexp
4691 (concat "\\<" org-scheduled-string
)
4692 org-scheduled-time-regexp
4693 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4694 org-closed-time-regexp
4695 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4696 org-keyword-time-regexp
4697 (concat "\\<\\(" org-scheduled-string
4698 "\\|" org-deadline-string
4699 "\\|" org-closed-string
4700 "\\|" org-clock-string
"\\)"
4701 " *[[<]\\([^]>]+\\)[]>]")
4702 org-keyword-time-not-clock-regexp
4703 (concat "\\<\\(" org-scheduled-string
4704 "\\|" org-deadline-string
4705 "\\|" org-closed-string
4707 " *[[<]\\([^]>]+\\)[]>]")
4708 org-maybe-keyword-time-regexp
4709 (concat "\\(\\<\\(" org-scheduled-string
4710 "\\|" org-deadline-string
4711 "\\|" org-closed-string
4712 "\\|" org-clock-string
"\\)\\)?"
4713 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4714 org-all-time-keywords
4715 (mapcar (lambda (w) (substring w
0 -
1))
4716 (list org-scheduled-string org-deadline-string
4717 org-clock-string org-closed-string
))
4719 (org-compute-latex-and-specials-regexp)
4720 (org-set-font-lock-defaults))))
4722 (defun org-file-contents (file &optional noerror
)
4723 "Return the contents of FILE, as a string."
4725 (not (file-readable-p file
)))
4728 (message "Cannot read file \"%s\"" file
)
4731 (error "Cannot read file \"%s\"" file
))
4733 (insert-file-contents file
)
4736 (defun org-extract-log-state-settings (x)
4737 "Extract the log state setting from a TODO keyword string.
4738 This will extract info from a string like \"WAIT(w@/!)\"."
4739 (let (kw key log1 log2
)
4740 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4741 (setq kw
(match-string 1 x
)
4742 key
(and (match-end 2) (match-string 2 x
))
4743 log1
(and (match-end 3) (match-string 3 x
))
4744 log2
(and (match-end 4) (match-string 4 x
)))
4747 (and log1
(if (equal log1
"!") 'time
'note
))
4748 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4750 (defun org-remove-keyword-keys (list)
4751 "Remove a pair of parenthesis at the end of each string in LIST."
4753 (if (string-match "(.*)$" x
)
4754 (substring x
0 (match-beginning 0))
4758 (defun org-assign-fast-keys (alist)
4759 "Assign fast keys to a keyword-key alist.
4760 Respect keys that are already there."
4761 (let (new e
(alt ?
0))
4762 (while (setq e
(pop alist
))
4763 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
4764 (cdr e
)) ;; Key already assigned.
4766 (let ((clist (string-to-list (downcase (car e
))))
4767 (used (append new alist
)))
4768 (when (= (car clist
) ?
@)
4770 (while (and clist
(rassoc (car clist
) used
))
4773 (while (rassoc alt used
)
4775 (push (cons (car e
) (or (car clist
) alt
)) new
))))
4778 ;;; Some variables used in various places
4780 (defvar org-window-configuration nil
4781 "Used in various places to store a window configuration.")
4782 (defvar org-selected-window nil
4783 "Used in various places to store a window configuration.")
4784 (defvar org-finish-function nil
4785 "Function to be called when `C-c C-c' is used.
4786 This is for getting out of special buffers like capture.")
4789 ;; FIXME: Occasionally check by commenting these, to make sure
4790 ;; no other functions uses these, forgetting to let-bind them.
4791 (org-no-warnings (defvar entry
)) ;; unprefixed, from calendar.el
4792 (defvar org-last-state
)
4793 (org-no-warnings (defvar date
)) ;; unprefixed, from calendar.el
4795 ;; Defined somewhere in this file, but used before definition.
4796 (defvar org-entities
) ;; defined in org-entities.el
4797 (defvar org-struct-menu
)
4798 (defvar org-org-menu
)
4799 (defvar org-tbl-menu
)
4801 ;;;; Define the Org-mode
4803 ;; We use a before-change function to check if a table might need
4805 (defvar org-table-may-need-update t
4806 "Indicates that a table might need an update.
4807 This variable is set by `org-before-change-function'.
4808 `org-table-align' sets it back to nil.")
4809 (defun org-before-change-function (beg end
)
4810 "Every change indicates that a table might need an update."
4811 (setq org-table-may-need-update t
))
4812 (defvar org-mode-map
)
4813 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4814 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
4815 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4816 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
4817 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
4818 (defvar org-table-buffer-is-an nil
)
4820 (defvar bidi-paragraph-direction
)
4821 (defvar buffer-face-mode-face
)
4824 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4825 (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"))
4826 (require 'noutline
"noutline" 'noerror
) ;; stock XEmacs does not have it
4828 ;; Other stuff we need.
4829 (require 'time-date
)
4830 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
4835 (require 'org-entities
)
4836 ;; (require 'org-compat) moved higher up in the file before it is first used
4837 (require 'org-faces
)
4839 (require 'org-pcomplete
)
4841 (require 'org-footnote
)
4848 (require 'ob-tangle
)
4849 (require 'ob-comint
)
4853 (define-derived-mode org-mode outline-mode
"Org"
4854 "Outline-based notes management and organizer, alias
4855 \"Carsten's outline-mode for keeping track of everything.\"
4857 Org-mode develops organizational tasks around a NOTES file which
4858 contains information about projects as plain text. Org-mode is
4859 implemented on top of outline-mode, which is ideal to keep the content
4860 of large files well structured. It supports ToDo items, deadlines and
4861 time stamps, which magically appear in the diary listing of the Emacs
4862 calendar. Tables are easily created with a built-in table editor.
4863 Plain text URL-like links connect to websites, emails (VM), Usenet
4864 messages (Gnus), BBDB entries, and any files related to the project.
4865 For printing and sharing of notes, an Org-mode file (or a part of it)
4866 can be exported as a structured ASCII or HTML file.
4868 The following commands are available:
4872 ;; Get rid of Outline menus, they are not needed
4873 ;; Need to do this here because define-derived-mode sets up
4874 ;; the keymap so late. Still, it is a waste to call this each time
4875 ;; we switch another buffer into org-mode.
4876 (if (featurep 'xemacs
)
4877 (when (boundp 'outline-mode-menu-heading
)
4878 ;; Assume this is Greg's port, it uses easymenu
4879 (easy-menu-remove outline-mode-menu-heading
)
4880 (easy-menu-remove outline-mode-menu-show
)
4881 (easy-menu-remove outline-mode-menu-hide
))
4882 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
4883 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
4884 (define-key org-mode-map
[menu-bar show
] 'undefined
))
4886 (org-load-modules-maybe)
4887 (easy-menu-add org-org-menu
)
4888 (easy-menu-add org-tbl-menu
)
4889 (org-install-agenda-files-menu)
4890 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
4891 (add-to-invisibility-spec '(org-cwidth))
4892 (add-to-invisibility-spec '(org-hide-block . t
))
4893 (when (featurep 'xemacs
)
4894 (org-set-local 'line-move-ignore-invisible t
))
4895 (org-set-local 'outline-regexp org-outline-regexp
)
4896 (org-set-local 'outline-level
'org-outline-level
)
4897 (setq bidi-paragraph-direction
'left-to-right
)
4898 (when (and org-ellipsis
4899 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
4900 (fboundp 'make-glyph-code
))
4901 (unless org-display-table
4902 (setq org-display-table
(make-display-table)))
4903 (set-display-table-slot
4906 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
4908 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
4909 (setq buffer-display-table org-display-table
))
4910 (org-set-regexps-and-options)
4911 (when (and org-tag-faces
(not org-tags-special-faces-re
))
4912 ;; tag faces set outside customize.... force initialization.
4913 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
4915 (org-set-local 'calc-embedded-open-mode
"# ")
4916 (modify-syntax-entry ?
@ "w")
4917 (if org-startup-truncated
(setq truncate-lines t
))
4918 (org-set-local 'font-lock-unfontify-region-function
4919 'org-unfontify-region
)
4920 ;; Activate before-change-function
4921 (org-set-local 'org-table-may-need-update t
)
4922 (org-add-hook 'before-change-functions
'org-before-change-function nil
4924 ;; Check for running clock before killing a buffer
4925 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
4927 (org-set-local 'indent-line-function
'org-indent-line
)
4928 (org-set-local 'indent-region-function
'org-indent-region
)
4929 ;; Initialize radio targets.
4930 (org-update-radio-target-regexp)
4931 ;; Filling and auto-filling.
4934 (org-setup-comments-handling)
4935 ;; Beginning/end of defun
4936 (org-set-local 'beginning-of-defun-function
'org-back-to-heading
)
4937 (org-set-local 'end-of-defun-function
(lambda () (interactive) (org-end-of-subtree nil t
)))
4938 ;; Next error for sparse trees
4939 (org-set-local 'next-error-function
'org-occur-next-match
)
4940 ;; Make sure dependence stuff works reliably, even for users who set it
4942 (if org-enforce-todo-dependencies
4943 (add-hook 'org-blocker-hook
4944 'org-block-todo-from-children-or-siblings-or-parent
)
4945 (remove-hook 'org-blocker-hook
4946 'org-block-todo-from-children-or-siblings-or-parent
))
4947 (if org-enforce-todo-checkbox-dependencies
4948 (add-hook 'org-blocker-hook
4949 'org-block-todo-from-checkboxes
)
4950 (remove-hook 'org-blocker-hook
4951 'org-block-todo-from-checkboxes
))
4953 ;; Align options lines
4955 'align-mode-rules-list
4956 '((org-in-buffer-settings
4957 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4958 (modes .
'(org-mode)))))
4961 (org-set-local 'imenu-create-index-function
4962 'org-imenu-get-tree
)
4964 ;; Make isearch reveal context
4965 (if (or (featurep 'xemacs
)
4966 (not (boundp 'outline-isearch-open-invisible-function
)))
4967 ;; Emacs 21 and XEmacs make use of the hook
4968 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
4969 ;; Emacs 22 deals with this through a special variable
4970 (org-set-local 'outline-isearch-open-invisible-function
4971 (lambda (&rest ignore
) (org-show-context 'isearch
))))
4973 ;; Turn on org-beamer-mode?
4974 (and org-startup-with-beamer-mode
(org-beamer-mode))
4976 ;; Setup the pcomplete hooks
4977 (set (make-local-variable 'pcomplete-command-completion-function
)
4978 'org-pcomplete-initial
)
4979 (set (make-local-variable 'pcomplete-command-name-function
)
4980 'org-command-at-point
)
4981 (set (make-local-variable 'pcomplete-default-completion-function
)
4983 (set (make-local-variable 'pcomplete-parse-arguments-function
)
4984 'org-parse-arguments
)
4985 (set (make-local-variable 'pcomplete-termination-string
) "")
4986 (when (>= emacs-major-version
23)
4987 (set (make-local-variable 'buffer-face-mode-face
) 'org-default
))
4989 ;; If empty file that did not turn on org-mode automatically, make it to.
4990 (if (and org-insert-mode-line-in-empty-file
4991 (org-called-interactively-p 'any
)
4992 (= (point-min) (point-max)))
4993 (insert "# -*- mode: org -*-\n\n"))
4994 (unless org-inhibit-startup
4995 (when org-startup-align-all-tables
4996 (let ((bmp (buffer-modified-p)))
4997 (org-table-map-tables 'org-table-align
'quietly
)
4998 (set-buffer-modified-p bmp
)))
4999 (when org-startup-with-inline-images
5000 (org-display-inline-images))
5001 (when org-startup-indented
5002 (require 'org-indent
)
5003 (org-indent-mode 1))
5004 (unless org-inhibit-startup-visibility-stuff
5005 (org-set-startup-visibility)))
5006 ;; Try to set org-hide correctly
5007 (set-face-foreground 'org-hide
(org-find-invisible-foreground)))
5009 (when (fboundp 'abbrev-table-put
)
5010 (abbrev-table-put org-mode-abbrev-table
5011 :parents
(list text-mode-abbrev-table
)))
5013 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5016 (defun org-find-invisible-foreground ()
5017 (let ((candidates (remove
5020 (list (face-background 'default
)
5021 (face-background 'org-default
))
5024 (when (boundp alist
)
5025 (cdr (assoc 'background-color
(symbol-value alist
)))))
5026 '(default-frame-alist initial-frame-alist window-system-default-frame-alist
))
5027 (list (face-foreground 'org-hide
))))))
5028 (car (remove nil candidates
))))
5030 (defun org-current-time ()
5031 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
5032 (if (> (car org-time-stamp-rounding-minutes
) 1)
5033 (let ((r (car org-time-stamp-rounding-minutes
))
5034 (time (decode-time)))
5036 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5041 "Return today date, considering `org-extend-today-until'."
5043 (time-subtract (current-time)
5044 (list 0 (* 3600 org-extend-today-until
) 0))))
5046 ;;;; Font-Lock stuff, including the activators
5048 (defvar org-mouse-map
(make-sparse-keymap))
5049 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5050 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5051 (when org-mouse-1-follows-link
5052 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5053 (when org-tab-follows-link
5054 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5055 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5057 (require 'font-lock
)
5059 (defconst org-non-link-chars
"]\t\n\r<>")
5060 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5061 "shell" "elisp" "doi" "message"))
5062 (defvar org-link-types-re nil
5063 "Matches a link that has a url-like prefix like \"http:\"")
5064 (defvar org-link-re-with-space nil
5065 "Matches a link with spaces, optional angular brackets around it.")
5066 (defvar org-link-re-with-space2 nil
5067 "Matches a link with spaces, optional angular brackets around it.")
5068 (defvar org-link-re-with-space3 nil
5069 "Matches a link with spaces, only for internal part in bracket links.")
5070 (defvar org-angle-link-re nil
5071 "Matches link with angular brackets, spaces are allowed.")
5072 (defvar org-plain-link-re nil
5073 "Matches plain link, without spaces.")
5074 (defvar org-bracket-link-regexp nil
5075 "Matches a link in double brackets.")
5076 (defvar org-bracket-link-analytic-regexp nil
5077 "Regular expression used to analyze links.
5078 Here is what the match groups contain after a match:
5084 (defvar org-bracket-link-analytic-regexp
++ nil
5085 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5086 (defvar org-any-link-re nil
5087 "Regular expression matching any link.")
5089 (defcustom org-match-sexp-depth
3
5090 "Number of stacked braces for sub/superscript matching.
5091 This has to be set before loading org.el to be effective."
5092 :group
'org-export-translation
; ??????????????????????????/
5095 (defun org-create-multibrace-regexp (left right n
)
5096 "Create a regular expression which will match a balanced sexp.
5097 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5098 as single character strings.
5099 The regexp returned will match the entire expression including the
5100 delimiters. It will also define a single group which contains the
5101 match except for the outermost delimiters. The maximum depth of
5102 stacked delimiters is N. Escaping delimiters is not possible."
5103 (let* ((nothing (concat "[^" left right
"]*?"))
5106 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5109 re
(concat re or next
)
5110 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5111 (concat left
"\\(" re
"\\)" right
)))
5113 (defvar org-match-substring-regexp
5115 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5116 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5118 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5120 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5121 "The regular expression matching a sub- or superscript.")
5123 (defvar org-match-substring-with-braces-regexp
5125 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5126 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5128 "The regular expression matching a sub- or superscript, forcing braces.")
5130 (defun org-make-link-regexps ()
5131 "Update the link regular expressions.
5132 This should be called after the variable `org-link-types' has changed."
5133 (setq org-link-types-re
5135 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
5136 org-link-re-with-space
5138 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5139 "\\([^" org-non-link-chars
" ]"
5140 "[^" org-non-link-chars
"]*"
5141 "[^" org-non-link-chars
" ]\\)>?")
5142 org-link-re-with-space2
5144 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5145 "\\([^" org-non-link-chars
" ]"
5147 "[^" org-non-link-chars
" ]\\)>?")
5148 org-link-re-with-space3
5150 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5151 "\\([^" org-non-link-chars
" ]"
5155 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5156 "\\([^" org-non-link-chars
" ]"
5157 "[^" org-non-link-chars
"]*"
5161 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5162 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5163 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5164 org-bracket-link-regexp
5165 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5166 org-bracket-link-analytic-regexp
5169 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
5172 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5174 org-bracket-link-analytic-regexp
++
5177 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
5180 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5183 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5184 org-angle-link-re
"\\)\\|\\("
5185 org-plain-link-re
"\\)")))
5187 (org-make-link-regexps)
5189 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5190 "Regular expression for fast time stamp matching.")
5191 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5192 "Regular expression for fast time stamp matching.")
5193 (defconst org-ts-regexp0
5194 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5195 "Regular expression matching time strings for analysis.
5196 This one does not require the space after the date, so it can be used
5197 on a string that terminates immediately after the date.")
5198 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5199 "Regular expression matching time strings for analysis.")
5200 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5201 "Regular expression matching time stamps, with groups.")
5202 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5203 "Regular expression matching time stamps (also [..]), with groups.")
5204 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5205 "Regular expression matching a time stamp range.")
5206 (defconst org-tr-regexp-both
5207 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5208 "Regular expression matching a time stamp range.")
5209 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5210 org-ts-regexp
"\\)?")
5211 "Regular expression matching a time stamp or time stamp range.")
5212 (defconst org-tsr-regexp-both
(concat org-ts-regexp-both
"\\(--?-?"
5213 org-ts-regexp-both
"\\)?")
5214 "Regular expression matching a time stamp or time stamp range.
5215 The time stamps may be either active or inactive.")
5217 (defvar org-emph-face nil
)
5219 (defun org-do-emphasis-faces (limit)
5220 "Run through the buffer and add overlays to emphasized strings."
5222 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5223 (if (not (= (char-after (match-beginning 3))
5224 (char-after (match-beginning 4))))
5227 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5228 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5232 (org-remove-flyspell-overlays-in
5233 (match-beginning 0) (match-end 0)))
5234 (add-text-properties (match-beginning 2) (match-end 2)
5235 '(font-lock-multiline t org-emphasis t
))
5236 (when org-hide-emphasis-markers
5237 (add-text-properties (match-end 4) (match-beginning 5)
5238 '(invisible org-link
))
5239 (add-text-properties (match-beginning 3) (match-end 3)
5240 '(invisible org-link
)))))
5244 (defun org-emphasize (&optional char
)
5245 "Insert or change an emphasis, i.e. a font like bold or italic.
5246 If there is an active region, change that region to a new emphasis.
5247 If there is no region, just insert the marker characters and position
5248 the cursor between them.
5249 CHAR should be either the marker character, or the first character of the
5250 HTML tag associated with that emphasis. If CHAR is a space, the means
5251 to remove the emphasis of the selected region.
5252 If char is not given (for example in an interactive call) it
5253 will be prompted for."
5255 (let ((eal org-emphasis-alist
) e det
5256 (erc org-emphasis-regexp-components
)
5258 (string "") beg end move tag c s
)
5259 (if (org-region-active-p)
5260 (setq beg
(region-beginning) end
(region-end)
5261 string
(buffer-substring beg end
))
5264 (while (setq e
(pop eal
))
5265 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
5267 (push (cons c
(string-to-char (car e
))) det
)
5268 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
5269 (substring tag
1)))))
5270 (setq det
(nreverse det
))
5272 (message "%s" (concat "Emphasis marker or tag:" prompt
))
5273 (setq char
(read-char-exclusive)))
5274 (setq char
(or (cdr (assoc char det
)) char
))
5275 (if (equal char ?\
)
5276 (setq s
"" move nil
)
5277 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5278 (error "No such emphasis marker: \"%c\"" char
))
5279 (setq s
(char-to-string char
)))
5280 (while (and (> (length string
) 1)
5281 (equal (substring string
0 1) (substring string -
1))
5282 (assoc (substring string
0 1) org-emphasis-alist
))
5283 (setq string
(substring string
1 -
1)))
5284 (setq string
(concat s string s
))
5285 (if beg
(delete-region beg end
))
5287 (string-match (concat "[" (nth 0 erc
) "\n]")
5288 (char-to-string (char-before (point)))))
5291 (string-match (concat "[" (nth 1 erc
) "\n]")
5292 (char-to-string (char-after (point)))))
5293 (insert " ") (backward-char 1))
5295 (and move
(backward-char 1))))
5297 (defconst org-nonsticky-props
5298 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link
))
5300 (defsubst org-rear-nonsticky-at
(pos)
5301 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5303 (defun org-activate-plain-links (limit)
5304 "Run through the buffer and add overlays to links."
5307 (when (and (re-search-forward (concat org-plain-link-re
) limit t
)
5308 (not (org-in-src-block-p)))
5309 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5310 (setq f
(get-text-property (match-beginning 0) 'face
))
5311 (unless (or (org-in-src-block-p)
5313 (and (listp f
) (memq 'org-tag f
)))
5314 (add-text-properties (match-beginning 0) (match-end 0)
5315 (list 'mouse-face
'highlight
5317 'keymap org-mouse-map
))
5318 (org-rear-nonsticky-at (match-end 0)))
5321 (defun org-activate-code (limit)
5322 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5324 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5325 (remove-text-properties (match-beginning 0) (match-end 0)
5326 '(display t invisible t intangible t
))
5329 (defcustom org-src-fontify-natively nil
5330 "When non-nil, fontify code in code blocks."
5333 :group
'org-appearance
5336 (defcustom org-allow-promoting-top-level-subtree nil
5337 "When non-nil, allow promoting a top level subtree.
5338 The leading star of the top level headline will be replaced
5342 :group
'org-appearance
)
5344 (defun org-fontify-meta-lines-and-blocks (limit)
5346 (org-fontify-meta-lines-and-blocks-1 limit
)
5347 (error (message "org-mode fontification error"))))
5349 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5350 "Fontify #+ lines and blocks, in the correct ways."
5351 (let ((case-fold-search t
))
5352 (if (re-search-forward
5353 "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5355 (let ((beg (match-beginning 0))
5356 (block-start (match-end 0))
5358 (lang (match-string 7))
5359 (beg1 (line-beginning-position 2))
5360 (dc1 (downcase (match-string 2)))
5361 (dc3 (downcase (match-string 3)))
5362 end end1 quoting block-type ovl
)
5364 ((member dc1
'("+html:" "+ascii:" "+latex:" "+docbook:"))
5365 ;; a single line of backend-specific content
5366 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5367 (remove-text-properties (match-beginning 0) (match-end 0)
5368 '(display t invisible t intangible t
))
5369 (add-text-properties (match-beginning 1) (match-end 3)
5370 '(font-lock-fontified t face org-meta-line
))
5371 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5372 '(font-lock-fontified t face org-block
))
5373 ; for backend-specific code
5375 ((and (match-end 4) (equal dc3
"+begin"))
5377 (setq block-type
(downcase (match-string 5))
5378 quoting
(member block-type org-protecting-blocks
))
5379 (when (re-search-forward
5380 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5381 nil t
) ;; on purpose, we look further than LIMIT
5382 (setq end
(min (point-max) (match-end 0))
5383 end1
(min (point-max) (1- (match-beginning 0))))
5384 (setq block-end
(match-beginning 0))
5386 (remove-text-properties beg end
5387 '(display t invisible t intangible t
)))
5388 (add-text-properties
5390 '(font-lock-fontified t font-lock-multiline t
))
5391 (add-text-properties beg beg1
'(face org-meta-line
))
5392 (add-text-properties end1
(min (point-max) (1+ end
))
5393 '(face org-meta-line
)) ; for end_src
5395 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5396 (org-src-font-lock-fontify-block lang block-start block-end
)
5397 ;; remove old background overlays
5399 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5400 (delete-overlay ov
)))
5401 (overlays-at (/ (+ beg1 block-end
) 2)))
5402 ;; add a background overlay
5403 (setq ovl
(make-overlay beg1 block-end
))
5404 (overlay-put ovl
'face
'org-block-background
)
5405 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5407 (add-text-properties beg1
(min (point-max) (1+ end1
))
5408 '(face org-block
))) ; end of source block
5409 ((not org-fontify-quote-and-verse-blocks
))
5410 ((string= block-type
"quote")
5411 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-quote
)))
5412 ((string= block-type
"verse")
5413 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-verse
))))
5414 (add-text-properties beg beg1
'(face org-block-begin-line
))
5415 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5416 '(face org-block-end-line
))
5418 ((member dc1
'("+title:" "+author:" "+email:" "+date:"))
5419 (add-text-properties
5421 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5422 '(font-lock-fontified t invisible t
)
5423 '(font-lock-fontified t face org-document-info-keyword
)))
5424 (add-text-properties
5425 (match-beginning 6) (1+ (match-end 6))
5426 (if (string-equal dc1
"+title:")
5427 '(font-lock-fontified t face org-document-title
)
5428 '(font-lock-fontified t face org-document-info
))))
5429 ((or (equal dc1
"+results")
5430 (member dc1
'("+begin:" "+end:" "+caption:" "+label:"
5431 "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
5432 "+call:" "+header:" "+headers:" "+name:"))
5433 (and (match-end 4) (equal dc3
"+attr")))
5434 (add-text-properties
5436 '(font-lock-fontified t face org-meta-line
))
5438 ((member dc3
'(" " ""))
5439 (add-text-properties
5441 '(font-lock-fontified t face font-lock-comment-face
)))
5442 ((not (member (char-after beg
) '(?\ ?
\t)))
5443 ;; just any other in-buffer setting, but not indented
5444 (add-text-properties
5446 '(font-lock-fontified t face org-meta-line
))
5450 (defun org-activate-angle-links (limit)
5451 "Run through the buffer and add overlays to links."
5452 (if (and (re-search-forward org-angle-link-re limit t
)
5453 (not (org-in-src-block-p)))
5455 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5456 (add-text-properties (match-beginning 0) (match-end 0)
5457 (list 'mouse-face
'highlight
5458 'keymap org-mouse-map
))
5459 (org-rear-nonsticky-at (match-end 0))
5462 (defun org-activate-footnote-links (limit)
5463 "Run through the buffer and add overlays to footnotes."
5464 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5466 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5467 (org-remove-flyspell-overlays-in beg end
)
5468 (add-text-properties beg end
5469 (list 'mouse-face
'highlight
5470 'keymap org-mouse-map
5472 (if (= (point-at-bol) beg
)
5473 "Footnote definition"
5474 "Footnote reference")
5475 'font-lock-fontified t
5476 'font-lock-multiline t
5477 'face
'org-footnote
))))))
5479 (defun org-activate-bracket-links (limit)
5480 "Run through the buffer and add overlays to bracketed links."
5481 (if (and (re-search-forward org-bracket-link-regexp limit t
)
5482 (not (org-in-src-block-p)))
5483 (let* ((help (concat "LINK: "
5484 (org-match-string-no-properties 1)))
5485 ;; FIXME: above we should remove the escapes.
5486 ;; but that requires another match, protecting match data,
5487 ;; a lot of overhead for font-lock.
5488 (ip (org-maybe-intangible
5489 (list 'invisible
'org-link
5490 'keymap org-mouse-map
'mouse-face
'highlight
5491 'font-lock-multiline t
'help-echo help
)))
5492 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5493 'font-lock-multiline t
'help-echo help
)))
5494 ;; We need to remove the invisible property here. Table narrowing
5495 ;; may have made some of this invisible.
5496 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5497 (remove-text-properties (match-beginning 0) (match-end 0)
5501 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5502 (org-rear-nonsticky-at (match-beginning 3))
5503 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5504 (org-rear-nonsticky-at (match-end 3))
5505 (add-text-properties (match-end 3) (match-end 0) ip
)
5506 (org-rear-nonsticky-at (match-end 0)))
5507 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5508 (org-rear-nonsticky-at (match-beginning 1))
5509 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5510 (org-rear-nonsticky-at (match-end 1))
5511 (add-text-properties (match-end 1) (match-end 0) ip
)
5512 (org-rear-nonsticky-at (match-end 0)))
5515 (defun org-activate-dates (limit)
5516 "Run through the buffer and add overlays to dates."
5517 (if (re-search-forward org-tsr-regexp-both limit t
)
5519 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5520 (add-text-properties (match-beginning 0) (match-end 0)
5521 (list 'mouse-face
'highlight
5522 'keymap org-mouse-map
))
5523 (org-rear-nonsticky-at (match-end 0))
5524 (when org-display-custom-times
5526 (org-display-custom-time (match-beginning 3) (match-end 3)))
5527 (org-display-custom-time (match-beginning 1) (match-end 1)))
5530 (defvar org-target-link-regexp nil
5531 "Regular expression matching radio targets in plain text.")
5532 (make-variable-buffer-local 'org-target-link-regexp
)
5533 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5534 "Regular expression matching a link target.")
5535 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5536 "Regular expression matching a radio target.")
5537 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5538 "Regular expression matching any target.")
5540 (defun org-activate-target-links (limit)
5541 "Run through the buffer and add overlays to target matches."
5542 (when org-target-link-regexp
5543 (let ((case-fold-search t
))
5544 (if (re-search-forward org-target-link-regexp limit t
)
5546 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5547 (add-text-properties (match-beginning 0) (match-end 0)
5548 (list 'mouse-face
'highlight
5549 'keymap org-mouse-map
5550 'help-echo
"Radio target link"
5551 'org-linked-text t
))
5552 (org-rear-nonsticky-at (match-end 0))
5555 (defun org-update-radio-target-regexp ()
5556 "Find all radio targets in this file and update the regular expression."
5558 (when (memq 'radio org-activate-links
)
5559 (setq org-target-link-regexp
5560 (org-make-target-link-regexp (org-all-targets 'radio
)))
5561 (org-restart-font-lock)))
5563 (defun org-hide-wide-columns (limit)
5565 (setq s
(text-property-any (point) (or limit
(point-max))
5568 (setq e
(next-single-property-change s
'org-cwidth
))
5569 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5573 (defvar org-latex-and-specials-regexp nil
5574 "Regular expression for highlighting export special stuff.")
5575 (defvar org-match-substring-regexp
)
5576 (defvar org-match-substring-with-braces-regexp
)
5578 ;; This should be with the exporter code, but we also use if for font-locking
5579 (defconst org-export-html-special-string-regexps
5580 '(("\\\\-" .
"­")
5581 ("---\\([^-]\\)" .
"—\\1")
5582 ("--\\([^-]\\)" .
"–\\1")
5583 ("\\.\\.\\." .
"…"))
5584 "Regular expressions for special string conversion.")
5587 (defun org-compute-latex-and-specials-regexp ()
5588 "Compute regular expression for stuff treated specially by exporters."
5589 (if (not org-highlight-latex-fragments-and-specials
)
5590 (org-set-local 'org-latex-and-specials-regexp nil
)
5593 ((matchers (plist-get org-format-latex-options
:matchers
))
5594 (latexs (delq nil
(mapcar (lambda (x) (if (member (car x
) matchers
) x
))
5595 org-latex-regexps
)))
5596 (org-export-allow-BIND nil
)
5597 (options (org-combine-plists (org-default-export-plist)
5598 (org-infile-export-plist)))
5599 (org-export-with-sub-superscripts (plist-get options
:sub-superscript
))
5600 (org-export-with-LaTeX-fragments (plist-get options
:LaTeX-fragments
))
5601 (org-export-with-TeX-macros (plist-get options
:TeX-macros
))
5602 (org-export-html-expand (plist-get options
:expand-quoted-html
))
5603 (org-export-with-special-strings (plist-get options
:special-strings
))
5606 ((equal org-export-with-sub-superscripts
'{})
5607 (list org-match-substring-with-braces-regexp
))
5608 (org-export-with-sub-superscripts
5609 (list org-match-substring-regexp
))))
5611 (if org-export-with-LaTeX-fragments
5612 (mapcar (lambda (x) (nth 1 x
)) latexs
)))
5614 (if org-export-with-TeX-macros
5615 (list (concat "\\\\"
5621 (append org-entities-user
5623 (if (boundp 'org-latex-entities
)
5625 (or (car-safe x
) x
))
5630 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5631 (re-special (if org-export-with-special-strings
5632 (mapcar (lambda (x) (car x
))
5633 org-export-html-special-string-regexps
)))
5637 (if org-export-html-expand
"@<[^>\n]+>")
5640 'org-latex-and-specials-regexp
5641 (mapconcat 'identity
(append re-latex re-sub re-macros re-special
5644 (defun org-do-latex-and-special-faces (limit)
5645 "Run through the buffer and add overlays to links."
5646 (when org-latex-and-specials-regexp
5648 (while (and (not rtn
) (re-search-forward org-latex-and-specials-regexp
5650 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5652 '(org-code org-verbatim underline
)))
5655 d
(cond ((member (char-after (1+ (match-beginning 0)))
5658 (font-lock-prepend-text-property
5659 (+ d
(match-beginning 0)) (match-end 0)
5660 'face
'org-latex-and-export-specials
)
5661 (add-text-properties (+ d
(match-beginning 0)) (match-end 0)
5662 '(font-lock-multiline t
)))))
5665 (defun org-restart-font-lock ()
5666 "Restart `font-lock-mode', to force refontification."
5667 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5669 (font-lock-mode 1)))
5671 (defun org-all-targets (&optional radio
)
5672 "Return a list of all targets in this file.
5673 With optional argument RADIO, only find radio targets."
5674 (let ((re (if radio org-radio-target-regexp org-target-regexp
))
5677 (goto-char (point-min))
5678 (while (re-search-forward re nil t
)
5679 (add-to-list 'rtn
(downcase (org-match-string-no-properties 1))))
5682 (defun org-make-target-link-regexp (targets)
5683 "Make regular expression matching all strings in TARGETS.
5684 The regular expression finds the targets also if there is a line break
5691 (setq x
(regexp-quote x
))
5692 (while (string-match " +" x
)
5693 (setq x
(replace-match "\\s-+" t t x
)))
5699 (defun org-activate-tags (limit)
5700 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5702 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5703 (add-text-properties (match-beginning 1) (match-end 1)
5704 (list 'mouse-face
'highlight
5705 'keymap org-mouse-map
))
5706 (org-rear-nonsticky-at (match-end 1))
5709 (defun org-outline-level ()
5710 "Compute the outline level of the heading at point.
5711 This function assumes that the cursor is at the beginning of a line matched
5712 by `outline-regexp'. Otherwise it returns garbage.
5713 If this is called at a normal headline, the level is the number of stars.
5714 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
5716 (looking-at org-outline-regexp
)
5717 (1- (- (match-end 0) (match-beginning 0)))))
5719 (defvar org-font-lock-keywords nil
)
5721 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5722 "Regular expression matching a property line.")
5724 (defvar org-font-lock-hook nil
5725 "Functions to be called for special font lock stuff.")
5727 (defvar org-font-lock-set-keywords-hook nil
5728 "Functions that can manipulate `org-font-lock-extra-keywords'.
5729 This is called after `org-font-lock-extra-keywords' is defined, but before
5730 it is installed to be used by font lock. This can be useful if something
5731 needs to be inserted at a specific position in the font-lock sequence.")
5733 (defun org-font-lock-hook (limit)
5734 "Run `org-font-lock-hook' within LIMIT."
5735 (run-hook-with-args 'org-font-lock-hook limit
))
5737 (defun org-set-font-lock-defaults ()
5738 "Set font lock defaults for the current buffer."
5739 (let* ((em org-fontify-emphasized-text
)
5740 (lk org-activate-links
)
5741 (org-font-lock-extra-keywords
5744 '(org-font-lock-hook)
5746 `(,(if org-fontify-whole-heading-line
5747 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5748 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5749 (1 (org-get-level-face 1))
5750 (2 (org-get-level-face 2))
5751 (3 (org-get-level-face 3)))
5753 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5756 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
5757 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
5758 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
5759 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
5761 (list org-drawer-regexp
'(0 'org-special-keyword t
))
5762 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
5764 (list org-property-re
5765 '(1 'org-special-keyword t
)
5766 '(3 'org-property-value t
))
5768 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
5769 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
5770 (if (memq 'plain lk
) '(org-activate-plain-links))
5771 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
5772 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
5773 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
5774 (if (memq 'footnote lk
) '(org-activate-footnote-links))
5775 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
5776 '(org-hide-wide-columns (0 nil append
))
5778 (list (format org-heading-keyword-regexp-format
5780 '(2 (org-get-todo-face 2) t
))
5782 (if org-fontify-done-headline
5783 (list (format org-heading-keyword-regexp-format
5786 (mapconcat 'regexp-quote org-done-keywords
"\\|")
5788 '(2 'org-headline-done t
))
5791 '(org-font-lock-add-priority-faces)
5793 '(org-font-lock-add-tag-faces)
5795 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
5796 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
5797 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
5798 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
5801 (if (featurep 'xemacs
)
5802 '(org-do-emphasis-faces (0 nil append
))
5803 '(org-do-emphasis-faces)))
5805 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
5806 1 'org-checkbox prepend
)
5807 (if (cdr (assq 'checkbox org-list-automatic-rules
))
5808 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5809 (0 (org-get-checkbox-statistics-face) t
)))
5810 ;; Description list items
5811 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
5812 1 'org-list-dt prepend
)
5813 ;; ARCHIVEd headings
5815 org-outline-regexp-bol
5816 "\\(.*:" org-archive-tag
":.*\\)")
5817 '(1 'org-archived prepend
))
5819 '(org-do-latex-and-special-faces)
5820 '(org-fontify-entities)
5821 '(org-raise-scripts)
5823 '(org-activate-code (1 'org-code t
))
5825 (list (format org-heading-keyword-regexp-format
5827 org-comment-string
"\\|" org-quote-string
5829 '(2 'org-special-keyword t
))
5830 ;; Blocks and meta lines
5831 '(org-fontify-meta-lines-and-blocks)
5833 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
5834 (run-hooks 'org-font-lock-set-keywords-hook
)
5835 ;; Now set the full font-lock-keywords
5836 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
5837 (org-set-local 'font-lock-defaults
5838 '(org-font-lock-keywords t nil nil backward-paragraph
))
5839 (kill-local-variable 'font-lock-keywords
) nil
))
5841 (defun org-toggle-pretty-entities ()
5842 "Toggle the composition display of entities as UTF8 characters."
5844 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
5845 (org-restart-font-lock)
5846 (if org-pretty-entities
5847 (message "Entities are displayed as UTF8 characters")
5850 (org-decompose-region (point-min) (point-max))
5851 (message "Entities are displayed plain"))))
5853 (defvar org-custom-properties-overlays nil
5854 "List of overlays used for custom properties.")
5855 (make-variable-buffer-local 'org-custom-properties-overlays
)
5857 (defun org-toggle-custom-properties-visibility ()
5858 "Display or hide properties in `org-custom-properties'."
5860 (if org-custom-properties-overlays
5861 (progn (mapc 'delete-overlay org-custom-properties-overlays
)
5862 (setq org-custom-properties-overlays nil
))
5863 (unless (not org-custom-properties
)
5867 (goto-char (point-min))
5868 (while (re-search-forward org-property-re nil t
)
5870 (when (equal p
(substring (match-string 1) 1 -
1))
5871 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
5872 (overlay-put o
'invisible t
)
5873 (overlay-put o
'org-custom-property t
)
5874 (push o org-custom-properties-overlays
))))
5875 org-custom-properties
)))))))
5877 (defun org-fontify-entities (limit)
5878 "Find an entity to fontify."
5880 (when org-pretty-entities
5882 (while (re-search-forward
5883 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
5885 (if (and (not (org-in-indented-comment-line))
5886 (setq ee
(org-entity-get (match-string 1)))
5887 (= (length (nth 6 ee
)) 1))
5889 ((end (if (equal (match-string 2) "{}")
5892 (add-text-properties
5893 (match-beginning 0) end
5894 (list 'font-lock-fontified t
))
5895 (compose-region (match-beginning 0) end
5901 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
5902 "Fontify string S like in Org-mode."
5905 (let ((org-odd-levels-only odd-levels
))
5907 (font-lock-fontify-buffer)
5913 (defun org-get-level-face (n)
5914 "Get the right face for match N in font-lock matching of headlines."
5915 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
5916 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
5917 (if org-cycle-level-faces
5918 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
5919 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
5921 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
5923 (t (if org-level-color-stars-only nil org-f
))))
5926 (defun org-get-todo-face (kwd)
5927 "Get the right face for a TODO keyword KWD.
5928 If KWD is a number, get the corresponding match group."
5929 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5930 (or (org-face-from-face-or-color
5931 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
5932 (and (member kwd org-done-keywords
) 'org-done
)
5935 (defun org-face-from-face-or-color (context inherit face-or-color
)
5936 "Create a face list that inherits INHERIT, but sets the foreground color.
5937 When FACE-OR-COLOR is not a string, just return it."
5938 (if (stringp face-or-color
)
5939 (list :inherit inherit
5940 (cdr (assoc context org-faces-easy-properties
))
5944 (defun org-font-lock-add-tag-faces (limit)
5945 "Add the special tag faces."
5946 (when (and org-tag-faces org-tags-special-faces-re
)
5947 (while (re-search-forward org-tags-special-faces-re limit t
)
5948 (add-text-properties (match-beginning 1) (match-end 1)
5949 (list 'face
(org-get-tag-face 1)
5950 'font-lock-fontified t
))
5951 (backward-char 1))))
5953 (defun org-font-lock-add-priority-faces (limit)
5954 "Add the special priority faces."
5955 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
5956 (when (save-match-data (org-at-heading-p))
5957 (add-text-properties
5958 (match-beginning 0) (match-end 0)
5959 (list 'face
(or (org-face-from-face-or-color
5960 'priority
'org-special-keyword
5961 (cdr (assoc (char-after (match-beginning 1))
5962 org-priority-faces
)))
5963 'org-special-keyword
)
5964 'font-lock-fontified t
)))))
5966 (defun org-get-tag-face (kwd)
5967 "Get the right face for a TODO keyword KWD.
5968 If KWD is a number, get the corresponding match group."
5969 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5970 (or (org-face-from-face-or-color
5971 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
5974 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
5975 "Remove fontification and activation overlays from links."
5976 (font-lock-default-unfontify-region beg end
)
5977 (let* ((buffer-undo-list t
)
5978 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
5979 (inhibit-modification-hooks t
)
5980 deactivate-mark buffer-file-name buffer-file-truename
)
5981 (org-decompose-region beg end
)
5982 (remove-text-properties beg end
5983 '(mouse-face t keymap t org-linked-text t
5984 invisible t intangible t
5985 org-no-flyspell t org-emphasis t
))
5986 (org-remove-font-lock-display-properties beg end
)))
5988 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
5989 ((raise 0.3) (height 0.7))
5992 "Display properties for showing superscripts and subscripts.")
5994 (defun org-remove-font-lock-display-properties (beg end
)
5995 "Remove specific display properties that have been added by font lock.
5996 The will remove the raise properties that are used to show superscripts
6000 (setq next
(next-single-property-change beg
'display nil end
)
6001 prop
(get-text-property beg
'display
))
6002 (if (member prop org-script-display
)
6003 (put-text-property beg next
'display nil
))
6006 (defun org-raise-scripts (limit)
6007 "Add raise properties to sub/superscripts."
6008 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
6009 (if (re-search-forward
6010 (if (eq org-use-sub-superscripts t
)
6011 org-match-substring-regexp
6012 org-match-substring-with-braces-regexp
)
6014 (let* ((pos (point)) table-p comment-p
6015 (mpos (match-beginning 3))
6016 (emph-p (get-text-property mpos
'org-emphasis
))
6017 (link-p (get-text-property mpos
'mouse-face
))
6018 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6019 (goto-char (point-at-bol))
6020 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
6021 comment-p
(org-looking-at-p "[ \t]*#"))
6023 ;; FIXME: Should we go back one character here, for a_b^c
6024 ;; (goto-char (1- pos)) ;????????????????????
6025 (if (or comment-p emph-p link-p keyw-p
)
6027 (put-text-property (match-beginning 3) (match-end 0)
6029 (if (equal (char-after (match-beginning 2)) ?^
)
6030 (nth (if table-p
3 1) org-script-display
)
6031 (nth (if table-p
2 0) org-script-display
)))
6032 (add-text-properties (match-beginning 2) (match-end 2)
6034 'org-dwidth t
'org-dwidth-n
1))
6035 (if (and (eq (char-after (match-beginning 3)) ?
{)
6036 (eq (char-before (match-end 3)) ?
}))
6038 (add-text-properties
6039 (match-beginning 3) (1+ (match-beginning 3))
6040 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6041 (add-text-properties
6042 (1- (match-end 3)) (match-end 3)
6043 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6046 ;;;; Visibility cycling, including org-goto and indirect buffer
6050 (defvar org-cycle-global-status nil
)
6051 (make-variable-buffer-local 'org-cycle-global-status
)
6052 (defvar org-cycle-subtree-status nil
)
6053 (make-variable-buffer-local 'org-cycle-subtree-status
)
6055 (defvar org-inlinetask-min-level
)
6058 (defun org-cycle (&optional arg
)
6059 "TAB-action and visibility cycling for Org-mode.
6061 This is the command invoked in Org-mode by the TAB key. Its main purpose
6062 is outline visibility cycling, but it also invokes other actions
6063 in special contexts.
6065 - When this function is called with a prefix argument, rotate the entire
6066 buffer through 3 states (global cycling)
6067 1. OVERVIEW: Show only top-level headlines.
6068 2. CONTENTS: Show all headlines of all levels, but no body text.
6069 3. SHOW ALL: Show everything.
6070 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6071 determined by the variable `org-startup-folded', and by any VISIBILITY
6072 properties in the buffer.
6073 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6074 including any drawers.
6076 - When inside a table, re-align the table and move to the next field.
6078 - When point is at the beginning of a headline, rotate the subtree started
6079 by this line through 3 different states (local cycling)
6080 1. FOLDED: Only the main headline is shown.
6081 2. CHILDREN: The main headline and the direct children are shown.
6082 From this state, you can move to one of the children
6083 and zoom in further.
6084 3. SUBTREE: Show the entire subtree, including body text.
6085 If there is no subtree, switch directly from CHILDREN to FOLDED.
6087 - When point is at the beginning of an empty headline and the variable
6088 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6089 of the headline by demoting and promoting it to likely levels. This
6090 speeds up creation document structure by pressing TAB once or several
6091 times right after creating a new headline.
6093 - When there is a numeric prefix, go up to a heading with level ARG, do
6094 a `show-subtree' and return to the previous cursor position. If ARG
6095 is negative, go up that many levels.
6097 - When point is not at the beginning of a headline, execute the global
6098 binding for TAB, which is re-indenting the line. See the option
6099 `org-cycle-emulate-tab' for details.
6101 - Special case: if point is at the beginning of the buffer and there is
6102 no headline in line 1, this function will act as if called with prefix arg
6103 (C-u TAB, same as S-TAB) also when called without prefix arg.
6104 But only if also the variable `org-cycle-global-at-bob' is t."
6106 (org-load-modules-maybe)
6107 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6108 (and org-cycle-level-after-item
/entry-creation
6109 (or (org-cycle-level)
6110 (org-cycle-item-indentation))))
6112 (or org-cycle-max-level
6113 (and (boundp 'org-inlinetask-min-level
)
6114 org-inlinetask-min-level
6115 (1- org-inlinetask-min-level
))))
6116 (nstars (and limit-level
6117 (if org-odd-levels-only
6118 (and limit-level
(1- (* limit-level
2)))
6121 (if (not (derived-mode-p 'org-mode
))
6123 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6124 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6125 (not (looking-at org-outline-regexp
))))
6128 (delq 'org-optimize-window-after-visibility-change
6129 (copy-sequence org-cycle-hook
))
6133 (if (or bob-special
(equal arg
'(4)))
6134 ;; special case: use global cycling
6140 (setq last-command
'dummy
)
6141 (org-set-startup-visibility)
6142 (message "Startup visibility, plus VISIBILITY properties"))
6146 (message "Entire buffer visible, including drawers"))
6148 ;; Table: enter it or move to the next field.
6149 ((org-at-table-p 'any
)
6150 (if (org-at-table.el-p
)
6151 (message "Use C-c ' to edit table.el tables")
6152 (if arg
(org-table-edit-field t
)
6153 (org-table-justify-field-maybe)
6154 (call-interactively 'org-table-next-field
))))
6156 ((run-hook-with-args-until-success
6157 'org-tab-after-check-for-table-hook
))
6159 ;; Global cycling: delegate to `org-cycle-internal-global'.
6160 ((eq arg t
) (org-cycle-internal-global))
6162 ;; Drawers: delegate to `org-flag-drawer'.
6163 ((and org-drawers org-drawer-regexp
6165 (beginning-of-line 1)
6166 (looking-at org-drawer-regexp
)))
6167 (org-flag-drawer ; toggle block visibility
6168 (not (get-char-property (match-end 0) 'invisible
))))
6170 ;; Show-subtree, ARG levels up from here.
6173 (org-back-to-heading)
6174 (outline-up-heading (if (< arg
0) (- arg
)
6175 (- (funcall outline-level
) arg
)))
6176 (org-show-subtree)))
6178 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6179 ((and (featurep 'org-inlinetask
)
6180 (org-inlinetask-at-task-p)
6181 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6182 (org-inlinetask-toggle-visibility))
6184 ((org-try-cdlatex-tab))
6186 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6187 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6188 (save-excursion (beginning-of-line 1)
6189 (looking-at org-outline-regexp
)))
6190 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6191 (org-cycle-internal-local))
6193 ;; From there: TAB emulation and template completion.
6194 (buffer-read-only (org-back-to-heading))
6196 ((run-hook-with-args-until-success
6197 'org-tab-after-check-for-cycling-hook
))
6199 ((org-try-structure-completion))
6201 ((run-hook-with-args-until-success
6202 'org-tab-before-tab-emulation-hook
))
6204 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6206 (not (looking-at org-outline-regexp
))))
6207 (call-interactively (global-key-binding "\t")))
6209 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6210 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6211 (or (and (eq org-cycle-emulate-tab
'white
)
6212 (= (match-end 0) (point-at-eol)))
6213 (and (eq org-cycle-emulate-tab
'whitestart
)
6214 (>= (match-end 0) pos
))))
6216 (eq org-cycle-emulate-tab t
))
6217 (call-interactively (global-key-binding "\t")))
6220 (org-back-to-heading)
6223 (defun org-cycle-internal-global ()
6224 "Do the global cycling action."
6225 ;; Hack to avoid display of messages for .org attachments in Gnus
6226 (let ((ga (string-match "\\*fontification" (buffer-name))))
6228 ((and (eq last-command this-command
)
6229 (eq org-cycle-global-status
'overview
))
6230 ;; We just created the overview - now do table of contents
6231 ;; This can be slow in very large buffers, so indicate action
6232 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6233 (unless ga
(message "CONTENTS..."))
6235 (unless ga
(message "CONTENTS...done"))
6236 (setq org-cycle-global-status
'contents
)
6237 (run-hook-with-args 'org-cycle-hook
'contents
))
6239 ((and (eq last-command this-command
)
6240 (eq org-cycle-global-status
'contents
))
6241 ;; We just showed the table of contents - now show everything
6242 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6244 (unless ga
(message "SHOW ALL"))
6245 (setq org-cycle-global-status
'all
)
6246 (run-hook-with-args 'org-cycle-hook
'all
))
6249 ;; Default action: go to overview
6250 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6252 (unless ga
(message "OVERVIEW"))
6253 (setq org-cycle-global-status
'overview
)
6254 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6256 (defun org-cycle-internal-local ()
6257 "Do the local cycling action."
6258 (let ((goal-column 0) eoh eol eos has-children children-skipped struct
)
6259 ;; First, determine end of headline (EOH), end of subtree or item
6260 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6265 (setq struct
(org-list-struct))
6266 (setq eoh
(point-at-eol))
6267 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6268 (setq has-children
(org-list-has-child-p (point) struct
)))
6269 (org-back-to-heading)
6270 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6271 (setq eos
(save-excursion (1- (org-end-of-subtree t t
))))
6274 (let ((level (funcall outline-level
)))
6275 (outline-next-heading)
6276 (and (org-at-heading-p t
)
6277 (> (funcall outline-level
) level
))))
6279 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6280 ;; Determine end invisible part of buffer (EOL)
6281 (beginning-of-line 2)
6282 ;; XEmacs doesn't have `next-single-char-property-change'
6283 (if (featurep 'xemacs
)
6284 (while (and (not (eobp)) ;; this is like `next-line'
6285 (get-char-property (1- (point)) 'invisible
))
6286 (beginning-of-line 2))
6287 (while (and (not (eobp)) ;; this is like `next-line'
6288 (get-char-property (1- (point)) 'invisible
))
6289 (goto-char (next-single-char-property-change (point) 'invisible
))
6290 (and (eolp) (beginning-of-line 2))))
6292 ;; Find out what to do next and set `this-command'
6295 ;; Nothing is hidden behind this heading
6296 (unless (org-before-first-heading-p)
6297 (run-hook-with-args 'org-pre-cycle-hook
'empty
))
6298 (message "EMPTY ENTRY")
6299 (setq org-cycle-subtree-status nil
)
6302 (outline-next-heading)
6303 (if (outline-invisible-p) (org-flag-heading nil
))))
6304 ((and (or (>= eol eos
)
6305 (not (string-match "\\S-" (buffer-substring eol eos
))))
6307 (not (setq children-skipped
6308 org-cycle-skip-children-state-if-no-children
))))
6309 ;; Entire subtree is hidden in one line: children view
6310 (unless (org-before-first-heading-p)
6311 (run-hook-with-args 'org-pre-cycle-hook
'children
))
6313 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6315 (org-with-limited-levels (show-children))
6316 ;; FIXME: This slows down the func way too much.
6317 ;; How keep drawers hidden in subtree anyway?
6318 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6319 ;; (org-cycle-hide-drawers 'subtree))
6321 ;; Fold every list in subtree to top-level items.
6322 (when (eq org-cycle-include-plain-lists
'integrate
)
6324 (org-back-to-heading)
6325 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6326 (beginning-of-line 1)
6327 (let* ((struct (org-list-struct))
6328 (prevs (org-list-prevs-alist struct
))
6329 (end (org-list-get-bottom-point struct
)))
6330 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6331 (org-list-get-all-items (point) struct prevs
))
6332 (goto-char end
))))))
6333 (message "CHILDREN")
6336 (outline-next-heading)
6337 (if (outline-invisible-p) (org-flag-heading nil
)))
6338 (setq org-cycle-subtree-status
'children
)
6339 (unless (org-before-first-heading-p)
6340 (run-hook-with-args 'org-cycle-hook
'children
)))
6341 ((or children-skipped
6342 (and (eq last-command this-command
)
6343 (eq org-cycle-subtree-status
'children
)))
6344 ;; We just showed the children, or no children are there,
6345 ;; now show everything.
6346 (unless (org-before-first-heading-p)
6347 (run-hook-with-args 'org-pre-cycle-hook
'subtree
))
6348 (outline-flag-region eoh eos nil
)
6349 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6350 (setq org-cycle-subtree-status
'subtree
)
6351 (unless (org-before-first-heading-p)
6352 (run-hook-with-args 'org-cycle-hook
'subtree
)))
6354 ;; Default action: hide the subtree.
6355 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6356 (outline-flag-region eoh eos t
)
6358 (setq org-cycle-subtree-status
'folded
)
6359 (unless (org-before-first-heading-p)
6360 (run-hook-with-args 'org-cycle-hook
'folded
))))))
6363 (defun org-global-cycle (&optional arg
)
6364 "Cycle the global visibility. For details see `org-cycle'.
6365 With \\[universal-argument] prefix arg, switch to startup visibility.
6366 With a numeric prefix, show all headlines up to that level."
6368 (let ((org-cycle-include-plain-lists
6369 (if (derived-mode-p 'org-mode
) org-cycle-include-plain-lists nil
)))
6373 (hide-sublevels arg
)
6374 (setq org-cycle-global-status
'contents
))
6376 (org-set-startup-visibility)
6377 (message "Startup visibility, plus VISIBILITY properties."))
6379 (org-cycle '(4))))))
6381 (defun org-set-startup-visibility ()
6382 "Set the visibility required by startup options and properties."
6384 ((eq org-startup-folded t
)
6386 ((eq org-startup-folded
'content
)
6387 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6388 (org-cycle '(4)) (org-cycle '(4)))))
6389 (unless (eq org-startup-folded
'showeverything
)
6390 (if org-hide-block-startup
(org-hide-block-all))
6391 (org-set-visibility-according-to-property 'no-cleanup
)
6392 (org-cycle-hide-archived-subtrees 'all
)
6393 (org-cycle-hide-drawers 'all
)
6394 (org-cycle-show-empty-lines t
)))
6396 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6397 "Switch subtree visibilities according to :VISIBILITY: property."
6399 (let (org-show-entry-below state
)
6401 (goto-char (point-min))
6402 (while (re-search-forward
6403 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6405 (setq state
(match-string 1))
6407 (org-back-to-heading t
)
6411 ((equal state
'("fold" "folded"))
6413 ((equal state
"children")
6414 (org-show-hidden-entry)
6416 ((equal state
"content")
6419 (org-narrow-to-subtree)
6421 ((member state
'("all" "showall"))
6424 (org-cycle-hide-archived-subtrees 'all
)
6425 (org-cycle-hide-drawers 'all
)
6426 (org-cycle-show-empty-lines 'all
)))))
6428 ;; This function uses outline-regexp instead of the more fundamental
6429 ;; org-outline-regexp so that org-cycle-global works outside of Org
6430 ;; buffers, where outline-regexp is needed.
6431 (defun org-overview ()
6432 "Switch to overview mode, showing only top-level headlines.
6433 Really, this shows all headlines with level equal or greater than the level
6434 of the first headline in the buffer. This is important, because if the
6435 first headline is not level one, then (hide-sublevels 1) gives confusing
6438 (let ((level (save-excursion
6439 (goto-char (point-min))
6440 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6442 (goto-char (match-beginning 0))
6443 (funcall outline-level
))))))
6444 (and level
(hide-sublevels level
))))
6446 (defun org-content (&optional arg
)
6447 "Show all headlines in the buffer, like a table of contents.
6448 With numerical argument N, show content up to level N."
6451 ;; Visit all headings and show their offspring
6452 (and (integerp arg
) (org-overview))
6453 (goto-char (point-max))
6455 (while (and (progn (condition-case nil
6456 (outline-previous-visible-heading 1)
6457 (error (goto-char (point-min))))
6459 (looking-at org-outline-regexp
))
6461 (show-children (1- arg
))
6463 (if (bobp) (throw 'exit nil
))))))
6466 (defun org-optimize-window-after-visibility-change (state)
6467 "Adjust the window after a change in outline visibility.
6468 This function is the default value of the hook `org-cycle-hook'."
6469 (when (get-buffer-window (current-buffer))
6471 ((eq state
'content
) nil
)
6472 ((eq state
'all
) nil
)
6473 ((eq state
'folded
) nil
)
6474 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6475 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6477 (defun org-remove-empty-overlays-at (pos)
6478 "Remove outline overlays that do not contain non-white stuff."
6481 (and (eq 'outline
(overlay-get o
'invisible
))
6482 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6484 (delete-overlay o
)))
6487 (defun org-clean-visibility-after-subtree-move ()
6488 "Fix visibility issues after moving a subtree."
6489 ;; First, find a reasonable region to look at:
6490 ;; Start two siblings above, end three below
6491 (let* ((beg (save-excursion
6492 (and (org-get-last-sibling)
6493 (org-get-last-sibling))
6495 (end (save-excursion
6496 (and (org-get-next-sibling)
6497 (org-get-next-sibling)
6498 (org-get-next-sibling))
6499 (if (org-at-heading-p)
6502 (level (looking-at "\\*+"))
6503 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6506 (narrow-to-region beg end
)
6508 ;; Properly fold already folded siblings
6509 (goto-char (point-min))
6510 (while (re-search-forward re nil t
)
6511 (if (and (not (outline-invisible-p))
6513 (goto-char (point-at-eol)) (outline-invisible-p)))
6515 (org-cycle-show-empty-lines 'overview
)
6516 (org-cycle-hide-drawers 'overview
)))))
6518 (defun org-cycle-show-empty-lines (state)
6519 "Show empty lines above all visible headlines.
6520 The region to be covered depends on STATE when called through
6521 `org-cycle-hook'. Lisp program can use t for STATE to get the
6522 entire buffer covered. Note that an empty line is only shown if there
6523 are at least `org-cycle-separator-lines' empty lines before the headline."
6524 (when (not (= org-cycle-separator-lines
0))
6526 (let* ((n (abs org-cycle-separator-lines
))
6528 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6529 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6530 (t (let ((ns (number-to-string (- n
2))))
6531 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6532 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6535 ((memq state
'(overview contents t
))
6536 (setq beg
(point-min) end
(point-max)))
6537 ((memq state
'(children folded
))
6538 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6539 (beginning-of-line 2)
6543 (while (re-search-forward re end t
)
6544 (unless (get-char-property (match-end 1) 'invisible
)
6545 (setq e
(match-end 1))
6546 (if (< org-cycle-separator-lines
0)
6547 (setq b
(save-excursion
6548 (goto-char (match-beginning 0))
6549 (org-back-over-empty-lines)
6551 (goto-char (max (point-min) (1- (point))))
6555 (setq b
(match-beginning 1)))
6556 (outline-flag-region b e nil
)))))))
6557 ;; Never hide empty lines at the end of the file.
6559 (goto-char (point-max))
6560 (outline-previous-heading)
6561 (outline-end-of-heading)
6562 (if (and (looking-at "[ \t\n]+")
6563 (= (match-end 0) (point-max)))
6564 (outline-flag-region (point) (match-end 0) nil
))))
6566 (defun org-show-empty-lines-in-parent ()
6567 "Move to the parent and re-show empty lines before visible headlines."
6569 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6570 (org-cycle-show-empty-lines context
))))
6572 (defun org-files-list ()
6573 "Return `org-agenda-files' list, plus all open org-mode files.
6574 This is useful for operations that need to scan all of a user's
6575 open and agenda-wise Org files."
6576 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6577 (dolist (buf (buffer-list))
6578 (with-current-buffer buf
6579 (if (and (derived-mode-p 'org-mode
) (buffer-file-name))
6580 (let ((file (expand-file-name (buffer-file-name))))
6581 (unless (member file files
)
6582 (push file files
))))))
6585 (defsubst org-entry-beginning-position
()
6586 "Return the beginning position of the current entry."
6587 (save-excursion (outline-back-to-heading t
) (point)))
6589 (defsubst org-entry-end-position
()
6590 "Return the end position of the current entry."
6591 (save-excursion (outline-next-heading) (point)))
6593 (defun org-cycle-hide-drawers (state)
6594 "Re-hide all drawers after a visibility state change."
6595 (when (and (derived-mode-p 'org-mode
)
6596 (not (memq state
'(overview folded contents
))))
6598 (let* ((globalp (memq state
'(contents all
)))
6599 (beg (if globalp
(point-min) (point)))
6600 (end (if globalp
(point-max)
6601 (if (eq state
'children
)
6602 (save-excursion (outline-next-heading) (point))
6603 (org-end-of-subtree t
)))))
6605 (while (re-search-forward org-drawer-regexp end t
)
6606 (org-flag-drawer t
))))))
6608 (defun org-flag-drawer (flag)
6609 "When FLAG is non-nil, hide the drawer we are within.
6610 Otherwise make it visible."
6612 (beginning-of-line 1)
6613 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6614 (let ((b (match-end 0)))
6615 (if (re-search-forward
6617 (save-excursion (outline-next-heading) (point)) t
)
6618 (outline-flag-region b
(point-at-eol) flag
)
6619 (error ":END: line missing at position %s" b
))))))
6621 (defun org-subtree-end-visible-p ()
6622 "Is the end of the current subtree visible?"
6623 (pos-visible-in-window-p
6624 (save-excursion (org-end-of-subtree t
) (point))))
6626 (defun org-first-headline-recenter (&optional N
)
6627 "Move cursor to the first headline and recenter the headline.
6628 Optional argument N means put the headline into the Nth line of the window."
6629 (goto-char (point-min))
6630 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
6632 (recenter (prefix-numeric-value N
))))
6634 ;;; Saving and restoring visibility
6636 (defun org-outline-overlay-data (&optional use-markers
)
6637 "Return a list of the locations of all outline overlays.
6638 These are overlays with the `invisible' property value `outline'.
6639 The return value is a list of cons cells, with start and stop
6640 positions for each overlay.
6641 If USE-MARKERS is set, return the positions as markers."
6648 (when (eq (overlay-get o
'invisible
) 'outline
)
6649 (setq beg
(overlay-start o
)
6650 end
(overlay-end o
))
6651 (and beg end
(> end beg
)
6653 (cons (move-marker (make-marker) beg
)
6654 (move-marker (make-marker) end
))
6656 (overlays-in (point-min) (point-max))))))))
6658 (defun org-set-outline-overlay-data (data)
6659 "Create visibility overlays for all positions in DATA.
6660 DATA should have been made by `org-outline-overlay-data'."
6667 (outline-flag-region (car c
) (cdr c
) t
))
6670 ;;; Folding of blocks
6672 (defvar org-hide-block-overlays nil
6673 "Overlays hiding blocks.")
6674 (make-variable-buffer-local 'org-hide-block-overlays
)
6676 (defun org-block-map (function &optional start end
)
6677 "Call FUNCTION at the head of all source blocks in the current buffer.
6678 Optional arguments START and END can be used to limit the range."
6679 (let ((start (or start
(point-min)))
6680 (end (or end
(point-max))))
6683 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6686 (goto-char (match-beginning 0))
6687 (funcall function
)))))))
6689 (defun org-hide-block-toggle-all ()
6690 "Toggle the visibility of all blocks in the current buffer."
6691 (org-block-map #'org-hide-block-toggle
))
6693 (defun org-hide-block-all ()
6694 "Fold all blocks in the current buffer."
6696 (org-show-block-all)
6697 (org-block-map #'org-hide-block-toggle-maybe
))
6699 (defun org-show-block-all ()
6700 "Unfold all blocks in the current buffer."
6702 (mapc 'delete-overlay org-hide-block-overlays
)
6703 (setq org-hide-block-overlays nil
))
6705 (defun org-hide-block-toggle-maybe ()
6706 "Toggle visibility of block at point."
6708 (let ((case-fold-search t
))
6710 (beginning-of-line 1)
6711 (looking-at org-block-regexp
))
6712 (progn (org-hide-block-toggle)
6713 t
) ;; to signal that we took action
6714 nil
))) ;; to signal that we did not
6716 (defun org-hide-block-toggle (&optional force
)
6717 "Toggle the visibility of the current block."
6721 (if (re-search-forward org-block-regexp nil t
)
6722 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
6723 (end (match-end 0)) ;; end of entire body
6725 (if (memq t
(mapcar (lambda (overlay)
6726 (eq (overlay-get overlay
'invisible
)
6728 (overlays-at start
)))
6729 (if (or (not force
) (eq force
'off
))
6731 (when (member ov org-hide-block-overlays
)
6732 (setq org-hide-block-overlays
6733 (delq ov org-hide-block-overlays
)))
6734 (when (eq (overlay-get ov
'invisible
)
6736 (delete-overlay ov
)))
6737 (overlays-at start
)))
6738 (setq ov
(make-overlay start end
))
6739 (overlay-put ov
'invisible
'org-hide-block
)
6740 ;; make the block accessible to isearch
6742 ov
'isearch-open-invisible
6744 (when (member ov org-hide-block-overlays
)
6745 (setq org-hide-block-overlays
6746 (delq ov org-hide-block-overlays
)))
6747 (when (eq (overlay-get ov
'invisible
)
6749 (delete-overlay ov
))))
6750 (push ov org-hide-block-overlays
)))
6751 (error "Not looking at a source block"))))
6753 ;; org-tab-after-check-for-cycling-hook
6754 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
6755 ;; Remove overlays when changing major mode
6756 (add-hook 'org-mode-hook
6757 (lambda () (org-add-hook 'change-major-mode-hook
6758 'org-show-block-all
'append
'local
)))
6762 (defvar org-goto-window-configuration nil
)
6763 (defvar org-goto-marker nil
)
6764 (defvar org-goto-map
)
6765 (defun org-goto-map ()
6766 "Set the keymap `org-goto'."
6768 (let ((map (make-sparse-keymap)))
6769 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
6770 mouse-drag-region universal-argument org-occur
))
6772 (while (setq cmd
(pop cmds
))
6773 (substitute-key-definition cmd cmd map global-map
)))
6774 (suppress-keymap map
)
6775 (org-defkey map
"\C-m" 'org-goto-ret
)
6776 (org-defkey map
[(return)] 'org-goto-ret
)
6777 (org-defkey map
[(left)] 'org-goto-left
)
6778 (org-defkey map
[(right)] 'org-goto-right
)
6779 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
6780 (org-defkey map
"\C-i" 'org-cycle
)
6781 (org-defkey map
[(tab)] 'org-cycle
)
6782 (org-defkey map
[(down)] 'outline-next-visible-heading
)
6783 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
6784 (if org-goto-auto-isearch
6785 (if (fboundp 'define-key-after
)
6786 (define-key-after map
[t] 'org-goto-local-auto-isearch)
6788 (org-defkey map "q" 'org-goto-quit)
6789 (org-defkey map "n" 'outline-next-visible-heading)
6790 (org-defkey map "p" 'outline-previous-visible-heading)
6791 (org-defkey map "f" 'outline-forward-same-level)
6792 (org-defkey map "b" 'outline-backward-same-level)
6793 (org-defkey map "u" 'outline-up-heading))
6794 (org-defkey map "/" 'org-occur)
6795 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6796 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6797 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6798 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6799 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6802 (defconst org-goto-help
6803 "Browse buffer copy, to find location or copy text.%s
6804 RET=jump to location C-g=quit and return to previous location
6805 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6807 (defvar org-goto-start-pos) ; dynamically scoped parameter
6809 ;; FIXME: Docstring does not mention both interfaces
6810 (defun org-goto (&optional alternative-interface)
6811 "Look up a different location in the current file, keeping current visibility.
6813 When you want look-up or go to a different location in a
6814 document, the fastest way is often to fold the entire buffer and
6815 then dive into the tree. This method has the disadvantage, that
6816 the previous location will be folded, which may not be what you
6819 This command works around this by showing a copy of the current
6820 buffer in an indirect buffer, in overview mode. You can dive
6821 into the tree in that copy, use org-occur and incremental search
6822 to find a location. When pressing RET or `Q', the command
6823 returns to the original buffer in which the visibility is still
6824 unchanged. After RET it will also jump to the location selected
6825 in the indirect buffer and expose the headline hierarchy above.
6827 With a prefix argument, use the alternative interface: e.g. if
6828 `org-goto-interface' is 'outline use 'outline-path-completion."
6831 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6832 (org-refile-use-outline-path t)
6833 (org-refile-target-verify-function nil)
6835 (if (not alternative-interface)
6837 (if (eq org-goto-interface 'outline)
6838 'outline-path-completion
6840 (org-goto-start-pos (point))
6842 (if (eq interface 'outline)
6843 (car (org-get-location (current-buffer) org-goto-help))
6844 (let ((pa (org-refile-get-location "Goto" nil nil t)))
6845 (org-refile-check-position pa)
6849 (org-mark-ring-push org-goto-start-pos)
6850 (goto-char selected-point)
6851 (if (or (outline-invisible-p) (org-invisible-p2))
6852 (org-show-context 'org-goto)))
6855 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6856 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6857 (defvar org-goto-local-auto-isearch-map) ; defined below
6859 (defun org-get-location (buf help)
6860 "Let the user select a location in the Org-mode buffer BUF.
6861 This function uses a recursive edit. It returns the selected position
6864 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6865 (isearch-hide-immediately nil)
6866 (isearch-search-fun-function
6867 (lambda () 'org-goto-local-search-headings))
6868 (org-goto-selected-point org-goto-exit-command))
6870 (save-window-excursion
6871 (delete-other-windows)
6872 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6873 (org-pop-to-buffer-same-window
6875 (make-indirect-buffer (current-buffer) "*org-goto*")
6876 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6877 (with-output-to-temp-buffer "*Org Help*"
6878 (princ (format help (if org-goto-auto-isearch
6879 " Just type for auto-isearch."
6880 " n/p/f/b/u to navigate, q to quit."))))
6881 (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
6882 (setq buffer-read-only nil)
6883 (let ((org-startup-truncated t)
6884 (org-startup-folded nil)
6885 (org-startup-align-all-tables nil))
6888 (setq buffer-read-only t)
6889 (if (and (boundp 'org-goto-start-pos)
6890 (integer-or-marker-p org-goto-start-pos))
6891 (let ((org-show-hierarchy-above t)
6892 (org-show-siblings t)
6893 (org-show-following-heading t))
6894 (goto-char org-goto-start-pos)
6895 (and (outline-invisible-p) (org-show-context)))
6896 (goto-char (point-min)))
6897 (let (org-special-ctrl-a/e) (org-beginning-of-line))
6898 (message "Select location and press RET")
6899 (use-local-map org-goto-map)
6901 (kill-buffer "*org-goto*")
6902 (cons org-goto-selected-point org-goto-exit-command))))
6904 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6905 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6906 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6907 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6909 (defun org-goto-local-search-headings (string bound noerror)
6910 "Search and make sure that any matches are in headlines."
6912 (while (if isearch-forward
6913 (search-forward string bound noerror)
6914 (search-backward string bound noerror))
6915 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6916 (and (member :headline context)
6917 (not (member :tags context))))
6918 (throw 'return (point))))))
6920 (defun org-goto-local-auto-isearch ()
6923 (goto-char (point-min))
6924 (let ((keys (this-command-keys)))
6925 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6927 (isearch-process-search-char (string-to-char keys)))))
6929 (defun org-goto-ret (&optional arg)
6930 "Finish `org-goto' by going to the new location."
6932 (setq org-goto-selected-point (point)
6933 org-goto-exit-command 'return)
6936 (defun org-goto-left ()
6937 "Finish `org-goto' by going to the new location."
6939 (if (org-at-heading-p)
6941 (beginning-of-line 1)
6942 (setq org-goto-selected-point (point)
6943 org-goto-exit-command 'left)
6945 (error "Not on a heading")))
6947 (defun org-goto-right ()
6948 "Finish `org-goto' by going to the new location."
6950 (if (org-at-heading-p)
6952 (setq org-goto-selected-point (point)
6953 org-goto-exit-command 'right)
6955 (error "Not on a heading")))
6957 (defun org-goto-quit ()
6958 "Finish `org-goto' without cursor motion."
6960 (setq org-goto-selected-point nil)
6961 (setq org-goto-exit-command 'quit)
6964 ;;; Indirect buffer display of subtrees
6966 (defvar org-indirect-dedicated-frame nil
6967 "This is the frame being used for indirect tree display.")
6968 (defvar org-last-indirect-buffer nil)
6970 (defun org-tree-to-indirect-buffer (&optional arg)
6971 "Create indirect buffer and narrow it to current subtree.
6972 With a numerical prefix ARG, go up to this level and then take that tree.
6973 If ARG is negative, go up that many levels.
6975 If `org-indirect-buffer-display' is not `new-frame', the command removes the
6976 indirect buffer previously made with this command, to avoid proliferation of
6977 indirect buffers. However, when you call the command with a \
6978 \\[universal-argument] prefix, or
6979 when `org-indirect-buffer-display' is `new-frame', the last buffer
6980 is kept so that you can work with several indirect buffers at the same time.
6981 If `org-indirect-buffer-display' is `dedicated-frame', the \
6982 \\[universal-argument] prefix also
6983 requests that a new frame be made for the new buffer, so that the dedicated
6984 frame is not changed."
6986 (let ((cbuf (current-buffer))
6987 (cwin (selected-window))
6989 beg end level heading ibuf)
6991 (org-back-to-heading t)
6993 (setq level (org-outline-level))
6994 (if (< arg 0) (setq arg (+ level arg)))
6995 (while (> (setq level (org-outline-level)) arg)
6996 (org-up-heading-safe)))
6998 heading (org-get-heading))
6999 (org-end-of-subtree t t)
7000 (if (org-at-heading-p) (backward-char 1))
7002 (if (and (buffer-live-p org-last-indirect-buffer)
7003 (not (eq org-indirect-buffer-display 'new-frame))
7005 (kill-buffer org-last-indirect-buffer))
7006 (setq ibuf (org-get-indirect-buffer cbuf)
7007 org-last-indirect-buffer ibuf)
7009 ((or (eq org-indirect-buffer-display 'new-frame)
7010 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7011 (select-frame (make-frame))
7012 (delete-other-windows)
7013 (org-pop-to-buffer-same-window ibuf)
7014 (org-set-frame-title heading))
7015 ((eq org-indirect-buffer-display 'dedicated-frame)
7017 (select-frame (or (and org-indirect-dedicated-frame
7018 (frame-live-p org-indirect-dedicated-frame)
7019 org-indirect-dedicated-frame)
7020 (setq org-indirect-dedicated-frame (make-frame)))))
7021 (delete-other-windows)
7022 (org-pop-to-buffer-same-window ibuf)
7023 (org-set-frame-title (concat "Indirect: " heading)))
7024 ((eq org-indirect-buffer-display 'current-window)
7025 (org-pop-to-buffer-same-window ibuf))
7026 ((eq org-indirect-buffer-display 'other-window)
7027 (pop-to-buffer ibuf))
7028 (t (error "Invalid value")))
7029 (if (featurep 'xemacs)
7030 (save-excursion (org-mode) (turn-on-font-lock)))
7031 (narrow-to-region beg end)
7034 (run-hook-with-args 'org-cycle-hook 'all)
7035 (and (window-live-p cwin) (select-window cwin))))
7037 (defun org-get-indirect-buffer (&optional buffer)
7038 (setq buffer (or buffer (current-buffer)))
7039 (let ((n 1) (base (buffer-name buffer)) bname)
7040 (while (buffer-live-p
7041 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7044 (make-indirect-buffer buffer bname 'clone)
7045 (error (make-indirect-buffer buffer bname)))))
7047 (defun org-set-frame-title (title)
7048 "Set the title of the current frame to the string TITLE."
7049 ;; FIXME: how to name a single frame in XEmacs???
7050 (unless (featurep 'xemacs)
7051 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7053 ;;;; Structure editing
7055 ;;; Inserting headlines
7057 (defun org-previous-line-empty-p ()
7060 (or (beginning-of-line 0) t)
7062 (looking-at "[ \t]*$")))))
7064 (defun org-insert-heading (&optional force-heading invisible-ok)
7065 "Insert a new heading or item with same depth at point.
7066 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
7067 If point is at the beginning of a headline, insert a sibling before the
7068 current headline. If point is not at the beginning, split the line,
7069 create the new headline with the text in the current line after point
7070 \(but see also the variable `org-M-RET-may-split-line').
7072 When INVISIBLE-OK is set, stop at invisible headlines when going back.
7073 This is important for non-interactive uses of the command."
7075 (if (or (= (buffer-size) 0)
7076 (and (not (save-excursion
7077 (and (ignore-errors (org-back-to-heading invisible-ok))
7078 (org-at-heading-p))))
7079 (or force-heading (not (org-in-item-p)))))
7082 (run-hooks 'org-insert-heading-hook))
7083 (when (or force-heading (not (org-insert-item)))
7084 (let* ((empty-line-p nil)
7086 (on-heading (org-at-heading-p))
7087 (head (save-excursion
7090 (org-back-to-heading invisible-ok)
7091 (when (and (not on-heading)
7092 (featurep 'org-inlinetask)
7093 (integerp org-inlinetask-min-level)
7094 (>= (length (match-string 0))
7095 org-inlinetask-min-level))
7096 ;; Find a heading level before the inline task
7097 (while (and (setq level (org-up-heading-safe))
7098 (>= level org-inlinetask-min-level)))
7099 (if (org-at-heading-p)
7100 (org-back-to-heading invisible-ok)
7101 (error "This should not happen")))
7102 (setq empty-line-p (org-previous-line-empty-p))
7105 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7106 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7107 pos hide-previous previous-pos)
7109 ((and (org-at-heading-p) (bolp)
7111 (save-excursion (backward-char 1) (not (outline-invisible-p)))))
7112 ;; insert before the current line
7113 (open-line (if blank 2 1)))
7115 (not org-insert-heading-respect-content)
7118 (backward-char 1) (not (outline-invisible-p)))))
7119 ;; insert right here
7122 ;; somewhere in the line
7124 (setq previous-pos (point-at-bol))
7126 (setq hide-previous (outline-invisible-p)))
7127 (and org-insert-heading-respect-content (org-show-subtree))
7129 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
7132 (goto-char (point-at-bol))
7133 (and (looking-at org-complex-heading-regexp)
7135 (> p (match-beginning 4)))))))
7138 (org-insert-heading-respect-content
7139 (org-end-of-subtree nil t)
7140 (when (featurep 'org-inlinetask)
7141 (while (and (not (eobp))
7142 (looking-at "\\(\\*+\\)[ \t]+")
7143 (>= (length (match-string 1))
7144 org-inlinetask-min-level))
7145 (org-end-of-subtree nil t)))
7146 (or (bolp) (newline))
7147 (or (org-previous-line-empty-p)
7148 (and blank (newline)))
7154 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
7155 (setq tags (and (match-end 2) (match-string 2)))
7157 (delete-region (match-beginning 1) (match-end 1)))
7158 (setq pos (point-at-bol))
7159 (or split (end-of-line 1))
7160 (delete-horizontal-space)
7161 (if (string-match "\\`\\*+\\'"
7162 (buffer-substring (point-at-bol) (point)))
7164 (newline (if blank 2 1))
7170 (org-set-tags nil 'align))))
7172 (or split (end-of-line 1))
7173 (newline (if blank 2 1)))))))
7174 (insert head) (just-one-space)
7177 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
7178 (when (and org-insert-heading-respect-content hide-previous)
7180 (goto-char previous-pos)
7182 (run-hooks 'org-insert-heading-hook)))))
7184 (defun org-get-heading (&optional no-tags no-todo)
7185 "Return the heading of the current entry, without the stars.
7186 When NO-TAGS is non-nil, don't include tags.
7187 When NO-TODO is non-nil, don't include TODO keywords."
7189 (org-back-to-heading t)
7191 ((and no-tags no-todo)
7192 (looking-at org-complex-heading-regexp)
7195 (looking-at (concat org-outline-regexp
7197 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7200 (looking-at org-todo-line-regexp)
7202 (t (looking-at org-heading-regexp)
7203 (match-string 2)))))
7205 (defun org-heading-components ()
7206 "Return the components of the current heading.
7207 This is a list with the following elements:
7208 - the level as an integer
7209 - the reduced level, different if `org-odd-levels-only' is set.
7210 - the TODO keyword, or nil
7211 - the priority character, like ?A, or nil if no priority is given
7212 - the headline text itself, or the tags string if no headline text
7213 - the tags string, or nil."
7215 (org-back-to-heading t)
7216 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
7217 (list (length (match-string 1))
7218 (org-reduced-level (length (match-string 1)))
7219 (org-match-string-no-properties 2)
7220 (and (match-end 3) (aref (match-string 3) 2))
7221 (org-match-string-no-properties 4)
7222 (org-match-string-no-properties 5)))))
7224 (defun org-get-entry ()
7225 "Get the entry text, after heading, entire subtree."
7227 (org-back-to-heading t)
7228 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7230 (defun org-insert-heading-after-current ()
7231 "Insert a new heading with same level as current, after current subtree."
7233 (org-back-to-heading)
7234 (org-insert-heading)
7235 (org-move-subtree-down)
7238 (defun org-insert-heading-respect-content ()
7240 (let ((org-insert-heading-respect-content t))
7241 (org-insert-heading t)))
7243 (defun org-insert-todo-heading-respect-content (&optional force-state)
7245 (let ((org-insert-heading-respect-content t))
7246 (org-insert-todo-heading force-state t)))
7248 (defun org-insert-todo-heading (arg &optional force-heading)
7249 "Insert a new heading with the same level and TODO state as current heading.
7250 If the heading has no TODO state, or if the state is DONE, use the first
7251 state (TODO by default). Also with prefix arg, force first state."
7253 (when (or force-heading (not (org-insert-item 'checkbox)))
7254 (org-insert-heading force-heading)
7256 (org-back-to-heading)
7257 (outline-previous-heading)
7258 (looking-at org-todo-line-regexp))
7262 (not (match-beginning 2))
7263 (member (match-string 2) org-done-keywords))
7264 (car org-todo-keywords-1)
7268 (run-hook-with-args-until-success
7269 'org-todo-get-default-hook new-mark-x nil)
7271 (beginning-of-line 1)
7272 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7273 (if org-treat-insert-todo-heading-as-state-change
7275 (insert new-mark " "))))
7276 (when org-provide-todo-statistics
7277 (org-update-parent-todo-statistics))))
7279 (defun org-insert-subheading (arg)
7280 "Insert a new subheading and demote it.
7281 Works for outline headings and for plain lists alike."
7283 (org-insert-heading arg)
7285 ((org-at-heading-p) (org-do-demote))
7286 ((org-at-item-p) (org-indent-item))))
7288 (defun org-insert-todo-subheading (arg)
7289 "Insert a new subheading with TODO keyword or checkbox and demote it.
7290 Works for outline headings and for plain lists alike."
7292 (org-insert-todo-heading arg)
7294 ((org-at-heading-p) (org-do-demote))
7295 ((org-at-item-p) (org-indent-item))))
7297 ;;; Promotion and Demotion
7299 (defvar org-after-demote-entry-hook nil
7300 "Hook run after an entry has been demoted.
7301 The cursor will be at the beginning of the entry.
7302 When a subtree is being demoted, the hook will be called for each node.")
7304 (defvar org-after-promote-entry-hook nil
7305 "Hook run after an entry has been promoted.
7306 The cursor will be at the beginning of the entry.
7307 When a subtree is being promoted, the hook will be called for each node.")
7309 (defun org-promote-subtree ()
7310 "Promote the entire subtree.
7311 See also `org-promote'."
7314 (org-with-limited-levels (org-map-tree 'org-promote)))
7315 (org-fix-position-after-promote))
7317 (defun org-demote-subtree ()
7318 "Demote the entire subtree. See `org-demote'.
7319 See also `org-promote'."
7322 (org-with-limited-levels (org-map-tree 'org-demote)))
7323 (org-fix-position-after-promote))
7326 (defun org-do-promote ()
7327 "Promote the current heading higher up the tree.
7328 If the region is active in `transient-mark-mode', promote all headings
7332 (if (org-region-active-p)
7333 (org-map-region 'org-promote (region-beginning) (region-end))
7335 (org-fix-position-after-promote))
7337 (defun org-do-demote ()
7338 "Demote the current heading lower down the tree.
7339 If the region is active in `transient-mark-mode', demote all headings
7343 (if (org-region-active-p)
7344 (org-map-region 'org-demote (region-beginning) (region-end))
7346 (org-fix-position-after-promote))
7348 (defun org-fix-position-after-promote ()
7349 "Make sure that after pro/demotion cursor position is right."
7350 (let ((pos (point)))
7351 (when (save-excursion
7352 (beginning-of-line 1)
7353 (looking-at org-todo-line-regexp)
7354 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7355 (cond ((eobp) (insert " "))
7356 ((eolp) (insert " "))
7357 ((equal (char-after) ?\ ) (forward-char 1))))))
7359 (defun org-current-level ()
7360 "Return the level of the current entry, or nil if before the first headline.
7361 The level is the number of stars at the beginning of the headline."
7363 (org-with-limited-levels
7364 (if (ignore-errors (org-back-to-heading t))
7365 (funcall outline-level)))))
7367 (defun org-get-previous-line-level ()
7368 "Return the outline depth of the last headline before the current line.
7369 Returns 0 for the first headline in the buffer, and nil if before the
7371 (let ((current-level (org-current-level))
7372 (prev-level (when (> (line-number-at-pos) 1)
7374 (beginning-of-line 0)
7375 (org-current-level)))))
7376 (cond ((null current-level) nil) ; Before first headline
7377 ((null prev-level) 0) ; At first headline
7380 (defun org-reduced-level (l)
7381 "Compute the effective level of a heading.
7382 This takes into account the setting of `org-odd-levels-only'."
7385 (org-odd-levels-only (1+ (floor (/ l 2))))
7388 (defun org-level-increment ()
7389 "Return the number of stars that will be added or removed at a
7390 time to headlines when structure editing, based on the value of
7391 `org-odd-levels-only'."
7392 (if org-odd-levels-only 2 1))
7394 (defun org-get-valid-level (level &optional change)
7395 "Rectify a level change under the influence of `org-odd-levels-only'
7396 LEVEL is a current level, CHANGE is by how much the level should be
7397 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7398 even level numbers will become the next higher odd number."
7399 (if org-odd-levels-only
7400 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7401 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7402 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7403 (max 1 (+ level (or change 0)))))
7405 (org-define-obsolete-function-alias 'org-get-legal-level 'org-get-valid-level "23.1")
7407 (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
7408 ;; ̀org-with-limited-levels'
7409 (defun org-promote ()
7410 "Promote the current heading higher up the tree.
7411 If the region is active in `transient-mark-mode', promote all headings
7413 (org-back-to-heading t)
7414 (let* ((level (save-match-data (funcall outline-level)))
7415 (after-change-functions (remove 'flyspell-after-change-function
7416 after-change-functions))
7417 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7418 (diff (abs (- level (length up-head) -1))))
7419 (cond ((and (= level 1) org-called-with-limited-levels
7420 org-allow-promoting-top-level-subtree)
7421 (replace-match "# " nil t))
7423 (error "Cannot promote to level 0. UNDO to recover if necessary"))
7424 (t (replace-match up-head nil t)))
7425 ;; Fixup tag positioning
7427 (and org-auto-align-tags (org-set-tags nil t))
7428 (if org-adapt-indentation (org-fixup-indentation (- diff))))
7429 (run-hooks 'org-after-promote-entry-hook)))
7431 (defun org-demote ()
7432 "Demote the current heading lower down the tree.
7433 If the region is active in `transient-mark-mode', demote all headings
7435 (org-back-to-heading t)
7436 (let* ((level (save-match-data (funcall outline-level)))
7437 (after-change-functions (remove 'flyspell-after-change-function
7438 after-change-functions))
7439 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7440 (diff (abs (- level (length down-head) -1))))
7441 (replace-match down-head nil t)
7442 ;; Fixup tag positioning
7443 (and org-auto-align-tags (org-set-tags nil t))
7444 (if org-adapt-indentation (org-fixup-indentation diff))
7445 (run-hooks 'org-after-demote-entry-hook)))
7447 (defun org-cycle-level ()
7448 "Cycle the level of an empty headline through possible states.
7449 This goes first to child, then to parent, level, then up the hierarchy.
7450 After top level, it switches back to sibling level."
7452 (let ((org-adapt-indentation nil))
7453 (when (org-point-at-end-of-empty-headline)
7454 (setq this-command 'org-cycle-level) ; Only needed for caching
7455 (let ((cur-level (org-current-level))
7456 (prev-level (org-get-previous-line-level)))
7458 ;; If first headline in file, promote to top-level.
7460 (loop repeat (/ (- cur-level 1) (org-level-increment))
7461 do (org-do-promote)))
7462 ;; If same level as prev, demote one.
7463 ((= prev-level cur-level)
7465 ;; If parent is top-level, promote to top level if not already.
7467 (loop repeat (/ (- cur-level 1) (org-level-increment))
7468 do (org-do-promote)))
7469 ;; If top-level, return to prev-level.
7471 (loop repeat (/ (- prev-level 1) (org-level-increment))
7472 do (org-do-demote)))
7473 ;; If less than prev-level, promote one.
7474 ((< cur-level prev-level)
7476 ;; If deeper than prev-level, promote until higher than
7478 ((> cur-level prev-level)
7479 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7480 do (org-do-promote))))
7483 (defun org-map-tree (fun)
7484 "Call FUN for every heading underneath the current one."
7485 (org-back-to-heading)
7486 (let ((level (funcall outline-level)))
7490 (outline-next-heading)
7491 (> (funcall outline-level) level))
7495 (defun org-map-region (fun beg end)
7496 "Call FUN for every heading between BEG and END."
7497 (let ((org-ignore-region t))
7499 (setq end (copy-marker end))
7501 (if (and (re-search-forward org-outline-regexp-bol nil t)
7505 (outline-next-heading)
7510 (defvar org-property-end-re) ; silence byte-compiler
7511 (defun org-fixup-indentation (diff)
7512 "Change the indentation in the current entry by DIFF.
7513 However, if any line in the current entry has no indentation, or if it
7514 would end up with no indentation after the change, nothing at all is done."
7516 (let ((end (save-excursion (outline-next-heading)
7518 (prohibit (if (> diff 0)
7520 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7522 (unless (save-excursion (end-of-line 1)
7523 (re-search-forward prohibit end t))
7524 (while (and (< (point) end)
7525 (re-search-forward "^[ \t]+" end t))
7526 (goto-char (match-end 0))
7527 (setq col (current-column))
7528 (if (< diff 0) (replace-match ""))
7529 (org-indent-to-column (+ diff col))))
7530 (move-marker end nil))))
7532 (defun org-convert-to-odd-levels ()
7533 "Convert an org-mode file with all levels allowed to one with odd levels.
7534 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7537 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7538 (let ((outline-level 'org-outline-level)
7539 (org-odd-levels-only nil) n)
7541 (goto-char (point-min))
7542 (while (re-search-forward "^\\*\\*+ " nil t)
7543 (setq n (- (length (match-string 0)) 2))
7544 (while (>= (setq n (1- n)) 0)
7546 (end-of-line 1))))))
7548 (defun org-convert-to-oddeven-levels ()
7549 "Convert an org-mode file with only odd levels to one with odd/even levels.
7550 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7551 file contains a section with an even level, conversion would
7552 destroy the structure of the file. An error is signaled in this
7555 (goto-char (point-min))
7556 ;; First check if there are no even levels
7557 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7558 (org-show-context t)
7559 (error "Not all levels are odd in this file. Conversion not possible"))
7560 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7561 (let ((outline-regexp org-outline-regexp)
7562 (outline-level 'org-outline-level)
7563 (org-odd-levels-only nil) n)
7565 (goto-char (point-min))
7566 (while (re-search-forward "^\\*\\*+ " nil t)
7567 (setq n (/ (1- (length (match-string 0))) 2))
7568 (while (>= (setq n (1- n)) 0)
7570 (end-of-line 1))))))
7572 (defun org-tr-level (n)
7573 "Make N odd if required."
7574 (if org-odd-levels-only (1+ (/ n 2)) n))
7576 ;;; Vertical tree motion, cutting and pasting of subtrees
7578 (defun org-move-subtree-up (&optional arg)
7579 "Move the current subtree up past ARG headlines of the same level."
7581 (org-move-subtree-down (- (prefix-numeric-value arg))))
7583 (defun org-move-subtree-down (&optional arg)
7584 "Move the current subtree down past ARG headlines of the same level."
7586 (setq arg (prefix-numeric-value arg))
7587 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7588 'org-get-last-sibling))
7589 (ins-point (make-marker))
7591 (col (current-column))
7592 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7594 (org-back-to-heading)
7597 (setq ne-beg (org-back-over-empty-lines))
7600 (save-excursion (outline-end-of-heading)
7601 (setq folded (outline-invisible-p)))
7602 (outline-end-of-subtree))
7603 (outline-next-heading)
7604 (setq ne-end (org-back-over-empty-lines))
7607 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7608 ;; include less whitespace
7611 (forward-line (- ne-beg ne-end))
7612 (setq beg (point))))
7613 ;; Find insertion point, with error handling
7615 (or (and (funcall movfunc) (looking-at org-outline-regexp))
7616 (progn (goto-char beg0)
7617 (error "Cannot move past superior level or buffer limit")))
7618 (setq cnt (1- cnt)))
7620 ;; Moving forward - still need to move over subtree
7621 (progn (org-end-of-subtree t t)
7623 (org-back-over-empty-lines)
7624 (or (bolp) (newline)))))
7625 (setq ne-ins (org-back-over-empty-lines))
7626 (move-marker ins-point (point))
7627 (setq txt (buffer-substring beg end))
7628 (org-save-markers-in-region beg end)
7629 (delete-region beg end)
7630 (org-remove-empty-overlays-at beg)
7631 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7632 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7633 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7634 (let ((bbb (point)))
7635 (insert-before-markers txt)
7636 (org-reinstall-markers-in-region bbb)
7637 (move-marker ins-point bbb))
7638 (or (bolp) (insert "\n"))
7639 (setq ins-end (point))
7640 (goto-char ins-point)
7641 (org-skip-whitespace)
7642 (when (and (< arg 0)
7643 (org-first-sibling-p)
7645 ;; Move whitespace back to beginning
7648 (let ((kill-whole-line t))
7649 (kill-line (- ne-ins ne-beg)) (point)))
7650 (insert (make-string (- ne-ins ne-beg) ?\n)))
7651 (move-marker ins-point nil)
7656 (org-cycle-hide-drawers 'children))
7657 (org-clean-visibility-after-subtree-move)
7658 ;; move back to the initial column we were at
7659 (move-to-column col)))
7661 (defvar org-subtree-clip ""
7662 "Clipboard for cut and paste of subtrees.
7663 This is actually only a copy of the kill, because we use the normal kill
7664 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7666 (defvar org-subtree-clip-folded nil
7667 "Was the last copied subtree folded?
7668 This is used to fold the tree back after pasting.")
7670 (defun org-cut-subtree (&optional n)
7671 "Cut the current subtree into the clipboard.
7672 With prefix arg N, cut this many sequential subtrees.
7673 This is a short-hand for marking the subtree and then cutting it."
7675 (org-copy-subtree n 'cut))
7677 (defun org-copy-subtree (&optional n cut force-store-markers)
7678 "Cut the current subtree into the clipboard.
7679 With prefix arg N, cut this many sequential subtrees.
7680 This is a short-hand for marking the subtree and then copying it.
7681 If CUT is non-nil, actually cut the subtree.
7682 If FORCE-STORE-MARKERS is non-nil, store the relative locations
7683 of some markers in the region, even if CUT is non-nil. This is
7684 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
7686 (let (beg end folded (beg0 (point)))
7687 (if (org-called-interactively-p 'any)
7688 (org-back-to-heading nil) ; take what looks like a subtree
7689 (org-back-to-heading t)) ; take what is really there
7691 (skip-chars-forward " \t\r\n")
7693 (save-excursion (outline-end-of-heading)
7694 (setq folded (outline-invisible-p)))
7696 (org-forward-heading-same-level (1- n) t)
7698 (org-end-of-subtree t t))
7702 (setq org-subtree-clip-folded folded)
7703 (when (or cut force-store-markers)
7704 (org-save-markers-in-region beg end))
7705 (if cut (kill-region beg end) (copy-region-as-kill beg end))
7706 (setq org-subtree-clip (current-kill 0))
7707 (message "%s: Subtree(s) with %d characters"
7708 (if cut "Cut" "Copied")
7709 (length org-subtree-clip)))))
7711 (defun org-paste-subtree (&optional level tree for-yank)
7712 "Paste the clipboard as a subtree, with modification of headline level.
7713 The entire subtree is promoted or demoted in order to match a new headline
7716 If the cursor is at the beginning of a headline, the same level as
7717 that headline is used to paste the tree
7719 If not, the new level is derived from the *visible* headings
7720 before and after the insertion point, and taken to be the inferior headline
7721 level of the two. So if the previous visible heading is level 3 and the
7722 next is level 4 (or vice versa), level 4 will be used for insertion.
7723 This makes sure that the subtree remains an independent subtree and does
7724 not swallow low level entries.
7726 You can also force a different level, either by using a numeric prefix
7727 argument, or by inserting the heading marker by hand. For example, if the
7728 cursor is after \"*****\", then the tree will be shifted to level 5.
7730 If optional TREE is given, use this text instead of the kill ring.
7732 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
7733 move back over whitespace before inserting, and move point to the end of
7734 the inserted text when done."
7736 (setq tree (or tree (and kill-ring (current-kill 0))))
7737 (unless (org-kill-is-subtree-p tree)
7739 (substitute-command-keys
7740 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
7741 (org-with-limited-levels
7742 (let* ((visp (not (outline-invisible-p)))
7744 (^re_ "\\(\\*+\\)[ \t]*")
7745 (old-level (if (string-match org-outline-regexp-bol txt)
7746 (- (match-end 0) (match-beginning 0) 1)
7748 (force-level (cond (level (prefix-numeric-value level))
7749 ((and (looking-at "[ \t]*$")
7751 "^\\*+$" (buffer-substring
7752 (point-at-bol) (point))))
7753 (- (match-end 1) (match-beginning 1)))
7755 (looking-at org-outline-regexp))
7756 (- (match-end 0) (point) 1))))
7757 (previous-level (save-excursion
7760 (outline-previous-visible-heading 1)
7761 (if (looking-at ^re_)
7762 (- (match-end 0) (match-beginning 0) 1)
7765 (next-level (save-excursion
7768 (or (looking-at org-outline-regexp)
7769 (outline-next-visible-heading 1))
7770 (if (looking-at ^re_)
7771 (- (match-end 0) (match-beginning 0) 1)
7774 (new-level (or force-level (max previous-level next-level)))
7775 (shift (if (or (= old-level -1)
7777 (= old-level new-level))
7779 (- new-level old-level)))
7780 (delta (if (> shift 0) -1 1))
7781 (func (if (> shift 0) 'org-demote 'org-promote))
7782 (org-odd-levels-only nil)
7784 ;; Remove the forced level indicator
7786 (delete-region (point-at-bol) (point)))
7788 (beginning-of-line (if (bolp) 1 2))
7790 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
7791 (insert-before-markers txt)
7792 (unless (string-match "\n\\'" txt) (insert "\n"))
7793 (setq newend (point))
7794 (org-reinstall-markers-in-region beg)
7797 (skip-chars-forward " \t\n\r")
7799 (if (and (outline-invisible-p) visp)
7800 (save-excursion (outline-show-heading)))
7801 ;; Shift if necessary
7804 (narrow-to-region beg end)
7805 (while (not (= shift 0))
7806 (org-map-region func (point-min) (point-max))
7807 (setq shift (+ delta shift)))
7808 (goto-char (point-min))
7809 (setq newend (point-max))))
7810 (when (or (org-called-interactively-p 'interactive) for-yank)
7811 (message "Clipboard pasted as level %d subtree" new-level))
7812 (if (and (not for-yank) ; in this case, org-yank will decide about folding
7814 (eq org-subtree-clip (current-kill 0))
7815 org-subtree-clip-folded)
7816 ;; The tree was folded before it was killed/copied
7818 (and for-yank (goto-char newend)))))
7820 (defun org-kill-is-subtree-p (&optional txt)
7821 "Check if the current kill is an outline subtree, or a set of trees.
7822 Returns nil if kill does not start with a headline, or if the first
7823 headline level is not the largest headline level in the tree.
7824 So this will actually accept several entries of equal levels as well,
7825 which is OK for `org-paste-subtree'.
7826 If optional TXT is given, check this string instead of the current kill."
7827 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
7828 (re (org-get-limited-outline-regexp))
7829 (^re (concat "^" re))
7830 (start-level (and kill
7832 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
7834 (- (match-end 2) (match-beginning 2) 1)))
7835 (start (1+ (or (match-beginning 2) -1))))
7836 (if (not start-level)
7838 nil) ;; does not even start with a heading
7840 (while (setq start (string-match ^re kill (1+ start)))
7841 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
7845 (defvar org-markers-to-move nil
7846 "Markers that should be moved with a cut-and-paste operation.
7847 Those markers are stored together with their positions relative to
7848 the start of the region.")
7850 (defun org-save-markers-in-region (beg end)
7851 "Check markers in region.
7852 If these markers are between BEG and END, record their position relative
7853 to BEG, so that after moving the block of text, we can put the markers back
7855 This function gets called just before an entry or tree gets cut from the
7856 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
7857 called immediately, to move the markers with the entries."
7858 (setq org-markers-to-move nil)
7859 (when (featurep 'org-clock)
7860 (org-clock-save-markers-for-cut-and-paste beg end))
7861 (when (featurep 'org-agenda)
7862 (org-agenda-save-markers-for-cut-and-paste beg end)))
7864 (defun org-check-and-save-marker (marker beg end)
7865 "Check if MARKER is between BEG and END.
7866 If yes, remember the marker and the distance to BEG."
7867 (when (and (marker-buffer marker)
7868 (equal (marker-buffer marker) (current-buffer)))
7869 (if (and (>= marker beg) (< marker end))
7870 (push (cons marker (- marker beg)) org-markers-to-move))))
7872 (defun org-reinstall-markers-in-region (beg)
7873 "Move all remembered markers to their position relative to BEG."
7875 (move-marker (car x) (+ beg (cdr x))))
7876 org-markers-to-move)
7877 (setq org-markers-to-move nil))
7879 (defun org-narrow-to-subtree ()
7880 "Narrow buffer to the current subtree."
7884 (org-with-limited-levels
7886 (progn (org-back-to-heading t) (point))
7887 (progn (org-end-of-subtree t t)
7888 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
7891 (defun org-narrow-to-block ()
7892 "Narrow buffer to the current block."
7894 (let* ((case-fold-search t)
7895 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
7896 "^[ \t]*#\\+end_.*")))
7898 (narrow-to-region (car blockp) (cdr blockp))
7899 (error "Not in a block"))))
7902 (defvar org-property-drawer-re))
7904 (defvar org-property-start-re) ;; defined below
7905 (defun org-clone-subtree-with-time-shift (n &optional shift)
7906 "Clone the task (subtree) at point N times.
7907 The clones will be inserted as siblings.
7909 In interactive use, the user will be prompted for the number of
7910 clones to be produced, and for a time SHIFT, which may be a
7911 repeater as used in time stamps, for example `+3d'.
7913 When a valid repeater is given and the entry contains any time
7914 stamps, the clones will become a sequence in time, with time
7915 stamps in the subtree shifted for each clone produced. If SHIFT
7916 is nil or the empty string, time stamps will be left alone. The
7917 ID property of the original subtree is removed.
7919 If the original subtree did contain time stamps with a repeater,
7920 the following will happen:
7921 - the repeater will be removed in each clone
7922 - an additional clone will be produced, with the current, unshifted
7923 date(s) in the entry.
7924 - the original entry will be placed *after* all the clones, with
7926 - the start days in the repeater in the original entry will be shifted
7927 to past the last clone.
7928 In this way you can spell out a number of instances of a repeating task,
7929 and still retain the repeater to cover future instances of the task."
7930 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
7931 (let (beg end template task idprop
7932 shift-n shift-what doshift nmin nmax (n-no-remove -1)
7933 (drawer-re org-drawer-regexp))
7934 (if (not (and (integerp n) (> n 0)))
7935 (error "Invalid number of replications %s" n))
7936 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
7937 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
7939 (error "Invalid shift specification %s" shift))
7941 (setq shift-n (string-to-number (match-string 1 shift))
7942 shift-what (cdr (assoc (match-string 2 shift)
7943 '(("d" . day) ("w" . week)
7944 ("m" . month) ("y" . year))))))
7945 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
7946 (setq nmin 1 nmax n)
7947 (org-back-to-heading t)
7949 (setq idprop (org-entry-get nil "ID"))
7950 (org-end-of-subtree t t)
7951 (or (bolp) (insert "\n"))
7953 (setq template (buffer-substring beg end))
7955 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
7956 (delete-region beg end)
7958 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
7960 (loop for n from nmin to nmax do
7965 (goto-char (point-min))
7967 (and idprop (if org-clone-delete-id
7968 (org-entry-delete nil "ID")
7969 (org-id-get-create t)))
7971 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
7973 (goto-char (point-min))
7974 (while (re-search-forward drawer-re nil t)
7976 (org-remove-empty-drawer-at d (point))) org-drawers)))
7977 (goto-char (point-min))
7979 (while (re-search-forward org-ts-regexp-both nil t)
7980 (org-timestamp-change (* n shift-n) shift-what))
7981 (unless (= n n-no-remove)
7982 (goto-char (point-min))
7983 (while (re-search-forward org-ts-regexp nil t)
7985 (goto-char (match-beginning 0))
7986 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
7987 (delete-region (match-beginning 1) (match-end 1)))))))
7988 (setq task (buffer-string)))
7994 (defun org-sort (with-case)
7995 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
7996 Optional argument WITH-CASE means sort case-sensitively."
7999 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
8000 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
8002 (org-call-with-arg 'org-sort-entries with-case))))
8004 (defun org-sort-remove-invisible (s)
8005 (remove-text-properties 0 (length s) org-rm-props s)
8006 (while (string-match org-bracket-link-regexp s)
8007 (setq s (replace-match (if (match-end 2)
8009 (match-string 1 s)) t t s)))
8012 (defvar org-priority-regexp) ; defined later in the file
8014 (defvar org-after-sorting-entries-or-items-hook nil
8015 "Hook that is run after a bunch of entries or items have been sorted.
8016 When children are sorted, the cursor is in the parent line when this
8017 hook gets called. When a region or a plain list is sorted, the cursor
8018 will be in the first entry of the sorted region/list.")
8020 (defun org-sort-entries
8021 (&optional with-case sorting-type getkey-func compare-func property)
8022 "Sort entries on a certain level of an outline tree.
8023 If there is an active region, the entries in the region are sorted.
8024 Else, if the cursor is before the first entry, sort the top-level items.
8025 Else, the children of the entry at point are sorted.
8027 Sorting can be alphabetically, numerically, by date/time as given by
8028 a time stamp, by a property or by priority.
8030 The command prompts for the sorting type unless it has been given to the
8031 function through the SORTING-TYPE argument, which needs to be a character,
8032 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
8033 precise meaning of each character:
8035 n Numerically, by converting the beginning of the entry/item to a number.
8036 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8037 o By order of TODO keywords.
8038 t By date/time, either the first active time stamp in the entry, or, if
8039 none exist, by the first inactive one.
8040 s By the scheduled date/time.
8041 d By deadline date/time.
8042 c By creation time, which is assumed to be the first inactive time stamp
8043 at the beginning of a line.
8044 p By priority according to the cookie.
8045 r By the value of a property.
8047 Capital letters will reverse the sort order.
8049 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8050 called with point at the beginning of the record. It must return either
8051 a string or a number that should serve as the sorting key for that record.
8053 Comparing entries ignores case by default. However, with an optional argument
8054 WITH-CASE, the sorting considers case as well."
8056 (let ((case-func (if with-case 'identity 'downcase))
8058 ;; The clock marker is lost when using `sort-subr', let's
8059 ;; store the clocking string.
8060 (when (equal (marker-buffer org-clock-marker) (current-buffer))
8062 (goto-char org-clock-marker)
8063 (looking-back "^.*") (match-string-no-properties 0))))
8064 start beg end stars re re2
8066 ;; Find beginning and end of region to sort
8068 ((org-region-active-p)
8069 ;; we will sort the region
8070 (setq end (region-end)
8072 (goto-char (region-beginning))
8073 (if (not (org-at-heading-p)) (outline-next-heading))
8074 (setq start (point)))
8075 ((or (org-at-heading-p)
8076 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8077 ;; we will sort the children of the current headline
8078 (org-back-to-heading)
8080 end (progn (org-end-of-subtree t t)
8081 (or (bolp) (insert "\n"))
8082 (org-back-over-empty-lines)
8087 (outline-next-heading))
8089 ;; we will sort the top-level entries in this file
8090 (goto-char (point-min))
8091 (or (org-at-heading-p) (outline-next-heading))
8092 (setq start (point))
8093 (goto-char (point-max))
8094 (beginning-of-line 1)
8095 (when (looking-at ".*?\\S-")
8096 ;; File ends in a non-white line
8099 (setq end (point-max))
8100 (setq what "top-level")
8105 (if (>= beg end) (error "Nothing to sort"))
8107 (looking-at "\\(\\*+\\)")
8108 (setq stars (match-string 1)
8109 re (concat "^" (regexp-quote stars) " +")
8110 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8111 txt (buffer-substring beg end))
8112 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8113 (if (and (not (equal stars "*")) (string-match re2 txt))
8114 (error "Region to sort contains a level above the first entry"))
8116 (unless sorting-type
8118 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8119 [t]ime
[s]cheduled [d]eadline [c]reated
8120 A/N/P/R/O/F/T/S/D/C means reversed:"
8122 (setq sorting-type (read-char-exclusive))
8124 (and (= (downcase sorting-type) ?f)
8126 (org-icompleting-read "Sort using function: "
8127 obarray 'fboundp t nil nil))
8128 (setq getkey-func (intern getkey-func)))
8130 (and (= (downcase sorting-type) ?r)
8132 (org-icompleting-read "Property: "
8133 (mapcar 'list (org-buffer-property-keys t))
8136 (message "Sorting entries...")
8139 (narrow-to-region start end)
8140 (let ((dcst (downcase sorting-type))
8141 (case-fold-search nil)
8142 (now (current-time)))
8144 (/= dcst sorting-type)
8145 ;; This function moves to the beginning character of the "record" to
8148 (if (re-search-forward re nil t)
8149 (goto-char (match-beginning 0))
8150 (goto-char (point-max))))
8151 ;; This function moves to the last character of the "record" being
8156 (outline-forward-same-level 1)
8158 (goto-char (point-max))))))
8159 ;; This function returns the value that gets sorted against.
8163 (if (looking-at org-complex-heading-regexp)
8164 (string-to-number (match-string 4))
8167 (if (looking-at org-complex-heading-regexp)
8168 (funcall case-func (match-string 4))
8171 (let ((end (save-excursion (outline-next-heading) (point))))
8172 (if (or (re-search-forward org-ts-regexp end t)
8173 (re-search-forward org-ts-regexp-both end t))
8174 (org-time-string-to-seconds (match-string 0))
8175 (org-float-time now))))
8177 (let ((end (save-excursion (outline-next-heading) (point))))
8178 (if (re-search-forward
8179 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8181 (org-time-string-to-seconds (match-string 0))
8182 (org-float-time now))))
8184 (let ((end (save-excursion (outline-next-heading) (point))))
8185 (if (re-search-forward org-scheduled-time-regexp end t)
8186 (org-time-string-to-seconds (match-string 1))
8187 (org-float-time now))))
8189 (let ((end (save-excursion (outline-next-heading) (point))))
8190 (if (re-search-forward org-deadline-time-regexp end t)
8191 (org-time-string-to-seconds (match-string 1))
8192 (org-float-time now))))
8194 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8195 (string-to-char (match-string 2))
8196 org-default-priority))
8198 (or (org-entry-get nil property) ""))
8200 (if (looking-at org-complex-heading-regexp)
8201 (- 9999 (length (member (match-string 2)
8202 org-todo-keywords-1)))))
8206 (setq tmp (funcall getkey-func))
8207 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8209 (error "Invalid key function `%s'" getkey-func)))
8210 (t (error "Invalid sorting type `%c'" sorting-type))))
8213 ((= dcst ?a) 'string<)
8214 ((= dcst ?f) compare-func)
8215 ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
8216 (run-hooks 'org-after-sorting-entries-or-items-hook)
8217 ;; Reset the clock marker if needed
8221 (search-forward cmstr nil t)
8222 (move-marker org-clock-marker (point))))
8223 (message "Sorting entries...done")))
8225 (defun org-do-sort (table what &optional with-case sorting-type)
8226 "Sort TABLE of WHAT according to SORTING-TYPE.
8227 The user will be prompted for the SORTING-TYPE if the call to this
8228 function does not specify it. WHAT is only for the prompt, to indicate
8229 what is being sorted. The sorting key will be extracted from
8230 the car of the elements of the table.
8231 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8232 (unless sorting-type
8234 "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
8236 (setq sorting-type (read-char-exclusive)))
8237 (let ((dcst (downcase sorting-type))
8238 extractfun comparefun)
8239 ;; Define the appropriate functions
8242 (setq extractfun 'string-to-number
8243 comparefun (if (= dcst sorting-type) '< '>)))
8245 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8246 (lambda(x) (downcase (org-sort-remove-invisible x))))
8247 comparefun (if (= dcst sorting-type)
8249 (lambda (a b) (and (not (string< a b))
8250 (not (string= a b)))))))
8254 (if (or (string-match org-ts-regexp x)
8255 (string-match org-ts-regexp-both x))
8257 (org-time-string-to-time (match-string 0 x)))
8259 comparefun (if (= dcst sorting-type) '< '>)))
8260 (t (error "Invalid sorting type `%c'" sorting-type)))
8262 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8264 (lambda (a b) (funcall comparefun (car a) (car b))))))
8267 ;;; The orgstruct minor mode
8269 ;; Define a minor mode which can be used in other modes in order to
8270 ;; integrate the org-mode structure editing commands.
8272 ;; This is really a hack, because the org-mode structure commands use
8273 ;; keys which normally belong to the major mode. Here is how it
8274 ;; works: The minor mode defines all the keys necessary to operate the
8275 ;; structure commands, but wraps the commands into a function which
8276 ;; tests if the cursor is currently at a headline or a plain list
8277 ;; item. If that is the case, the structure command is used,
8278 ;; temporarily setting many Org-mode variables like regular
8279 ;; expressions for filling etc. However, when any of those keys is
8280 ;; used at a different location, function uses `key-binding' to look
8281 ;; up if the key has an associated command in another currently active
8282 ;; keymap (minor modes, major mode, global), and executes that
8283 ;; command. There might be problems if any of the keys is otherwise
8284 ;; used as a prefix key.
8286 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
8287 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
8288 ;; addresses this by checking explicitly for both bindings.
8290 (defvar orgstruct-mode-map (make-sparse-keymap)
8291 "Keymap for the minor `orgstruct-mode'.")
8293 (defvar org-local-vars nil
8294 "List of local variables, for use by `orgstruct-mode'.")
8297 (define-minor-mode orgstruct-mode
8298 "Toggle the minor mode `orgstruct-mode'.
8299 This mode is for using Org-mode structure commands in other
8300 modes. The following keys behave as if Org-mode were active, if
8301 the cursor is on a headline, or on a plain list item (both as
8302 defined by Org-mode).
8304 M-up Move entry/item up
8305 M-down Move entry/item down
8308 M-S-up Move entry/item up
8309 M-S-down Move entry/item down
8310 M-S-left Promote subtree
8311 M-S-right Demote subtree
8312 M-q Fill paragraph and items like in Org-mode
8314 C-c - Cycle list bullet
8315 TAB Cycle item visibility
8316 M-RET Insert new heading/item
8317 S-M-RET Insert new TODO heading / Checkbox item
8318 C-c C-c Set tags / toggle checkbox"
8319 nil " OrgStruct" nil
8320 (org-load-modules-maybe)
8321 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
8324 (defun turn-on-orgstruct ()
8325 "Unconditionally turn on `orgstruct-mode'."
8328 (defvar org-fb-vars nil)
8329 (make-variable-buffer-local 'org-fb-vars)
8330 (defun orgstruct++-mode (&optional arg)
8331 "Toggle `orgstruct-mode', the enhanced version of it.
8332 In addition to setting orgstruct-mode, this also exports all
8333 indentation and autofilling variables from org-mode into the
8334 buffer. It will also recognize item context in multiline items."
8336 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8338 (progn (orgstruct-mode -1)
8340 (org-set-local (car v)
8341 (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
8344 (setq org-fb-vars nil)
8349 "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
8350 (symbol-name (car x)))
8351 (setq var (car x) val (nth 1 x))
8352 (push (list var `(quote ,(eval var))) org-fb-vars)
8353 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8355 (org-set-local 'orgstruct-is-++ t))))
8357 (defvar orgstruct-is-++ nil
8358 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8359 (make-variable-buffer-local 'orgstruct-is-++)
8362 (defun turn-on-orgstruct++ ()
8363 "Unconditionally turn on `orgstruct++-mode'."
8364 (orgstruct++-mode 1))
8366 (defun orgstruct-error ()
8367 "Error when there is no default binding for a structure key."
8369 (error "This key has no function outside structure elements"))
8371 (defun orgstruct-setup ()
8372 "Setup orgstruct keymaps."
8376 '([(meta up)] org-metaup)
8377 '([(meta down)] org-metadown)
8378 '([(meta left)] org-metaleft)
8379 '([(meta right)] org-metaright)
8380 '([(meta shift up)] org-shiftmetaup)
8381 '([(meta shift down)] org-shiftmetadown)
8382 '([(meta shift left)] org-shiftmetaleft)
8383 '([(meta shift right)] org-shiftmetaright)
8384 '([?\e (up)] org-metaup)
8385 '([?\e (down)] org-metadown)
8386 '([?\e (left)] org-metaleft)
8387 '([?\e (right)] org-metaright)
8388 '([?\e (shift up)] org-shiftmetaup)
8389 '([?\e (shift down)] org-shiftmetadown)
8390 '([?\e (shift left)] org-shiftmetaleft)
8391 '([?\e (shift right)] org-shiftmetaright)
8392 '([(shift up)] org-shiftup)
8393 '([(shift down)] org-shiftdown)
8394 '([(shift left)] org-shiftleft)
8395 '([(shift right)] org-shiftright)
8396 '("\C-c\C-c" org-ctrl-c-ctrl-c)
8397 '("\M-q" fill-paragraph)
8399 '("\C-c-" org-cycle-list-bullet)))
8401 (while (setq elt (pop bindings))
8402 (setq nfunc (1+ nfunc))
8403 (setq key (org-key (car elt))
8405 cmd (orgstruct-make-binding fun nfunc key))
8406 (org-defkey orgstruct-mode-map key cmd))
8408 ;; Prevent an error for users who forgot to make autoloads
8409 (require 'org-element)
8411 ;; Special treatment needed for TAB and RET
8412 (org-defkey orgstruct-mode-map [(tab)]
8413 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
8414 (org-defkey orgstruct-mode-map "\C-i"
8415 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
8417 (org-defkey orgstruct-mode-map "\M-\C-m"
8418 (orgstruct-make-binding 'org-insert-heading 105
8419 "\M-\C-m" [(meta return)]))
8420 (org-defkey orgstruct-mode-map [(meta return)]
8421 (orgstruct-make-binding 'org-insert-heading 106
8422 [(meta return)] "\M-\C-m"))
8424 (org-defkey orgstruct-mode-map [(shift meta return)]
8425 (orgstruct-make-binding 'org-insert-todo-heading 107
8426 [(meta return)] "\M-\C-m"))
8428 (org-defkey orgstruct-mode-map "\e\C-m"
8429 (orgstruct-make-binding 'org-insert-heading 108
8430 "\e\C-m" [?\e (return)]))
8431 (org-defkey orgstruct-mode-map [?\e (return)]
8432 (orgstruct-make-binding 'org-insert-heading 109
8433 [?\e (return)] "\e\C-m"))
8434 (org-defkey orgstruct-mode-map [?\e (shift return)]
8435 (orgstruct-make-binding 'org-insert-todo-heading 110
8436 [?\e (return)] "\e\C-m"))
8438 (unless org-local-vars
8439 (setq org-local-vars (org-get-local-variables)))
8443 (defun orgstruct-make-binding (fun n &rest keys)
8444 "Create a function for binding in the structure minor mode.
8445 FUN is the command to call inside a table. N is used to create a unique
8446 command name. KEYS are keys that should be checked in for a command
8447 to execute outside of tables."
8450 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
8452 (concat "In Structure, run `" (symbol-name fun) "'.\n"
8453 "Outside of structure, run the binding of `"
8454 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
8458 `(org-context-p 'headline 'item
8459 (and orgstruct-is-++
8460 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
8462 (list 'org-run-like-in-org-mode (list 'quote fun))
8463 (list 'let '(orgstruct-mode)
8464 (list 'call-interactively
8467 (list 'key-binding k))
8469 '('orgstruct-error))))))))
8471 (defun org-contextualize-keys (alist contexts)
8472 "Return valid elements in ALIST depending on CONTEXTS.
8474 `org-agenda-custom-commands' or `org-capture-templates' are the
8475 values used for ALIST, and `org-agenda-custom-commands-contexts'
8476 or `org-capture-templates-contexts' are the associated contexts
8479 ;; normalize contexts
8481 (lambda(c) (cond ((listp (cadr c))
8482 (list (car c) (car c) (cadr c)))
8483 ((string= "" (cadr c))
8484 (list (car c) (car c) (caddr c)))
8487 ;; loop over all commands or templates
8488 (while (setq c (pop a))
8491 ((not (assoc (car c) contexts))
8493 ((and (assoc (car c) contexts)
8494 (setq vrules (org-contextualize-validate-key
8497 (when (not (equal (car vr) (cadr vr)))
8498 (setq repl vr))) vrules)
8499 (if (not repl) (push c r)
8500 (push (cadr repl) s)
8503 (cdr (or (assoc (cadr repl) alist)
8504 (error "Undefined key `%s' as contextual replacement for `%s'"
8505 (cadr repl) (car c)))))
8507 ;; Return limited ALIST, possibly with keys modified, and deduplicated
8512 (let ((tpl (car x)))
8516 (equal y tpl)) s))) x)))
8519 (defun org-contextualize-validate-key (key contexts)
8520 "Check CONTEXTS for agenda or capture KEY."
8522 (while (setq r (pop contexts))
8526 (and (equal key (car r))
8527 (if (functionp rr) (funcall rr)
8528 (or (and (eq (car rr) 'in-file)
8530 (string-match (cdr rr) (buffer-file-name)))
8531 (and (eq (car rr) 'in-mode)
8532 (string-match (cdr rr) (symbol-name major-mode)))
8533 (when (and (eq (car rr) 'not-in-file)
8535 (not (string-match (cdr rr) (buffer-file-name))))
8536 (when (eq (car rr) 'not-in-mode)
8537 (not (string-match (cdr rr) (symbol-name major-mode)))))))
8540 (delete-dups (delq nil res))))
8542 (defun org-context-p (&rest contexts)
8543 "Check if local context is any of CONTEXTS.
8544 Possible values in the list of contexts are `table', `headline', and `item'."
8545 (let ((pos (point)))
8546 (goto-char (point-at-bol))
8547 (prog1 (or (and (memq 'table contexts)
8548 (looking-at "[ \t]*|"))
8549 (and (memq 'headline contexts)
8550 (looking-at org-outline-regexp))
8551 (and (memq 'item contexts)
8552 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
8553 (and (memq 'item-body contexts)
8557 (defun org-get-local-variables ()
8558 "Return a list of all local variables in an Org mode buffer."
8560 (with-current-buffer (get-buffer-create "*Org tmp*")
8563 (setq varlist (buffer-local-variables)))
8564 (kill-buffer "*Org tmp*")
8571 (list (car x) (list 'quote (cdr x)))))
8573 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
8574 (symbol-name (car x)))
8578 (defun org-clone-local-variables (from-buffer &optional regexp)
8579 "Clone local variables from FROM-BUFFER.
8580 Optional argument REGEXP selects variables to clone."
8583 (and (symbolp (car pair))
8585 (string-match regexp (symbol-name (car pair))))
8586 (set (make-local-variable (car pair))
8588 (buffer-local-variables from-buffer)))
8591 (defun org-run-like-in-org-mode (cmd)
8592 "Run a command, pretending that the current buffer is in Org-mode.
8593 This will temporarily bind local variables that are typically bound in
8594 Org-mode to the values they have in Org-mode, and then interactively
8596 (org-load-modules-maybe)
8597 (unless org-local-vars
8598 (setq org-local-vars (org-get-local-variables)))
8599 (eval (list 'let org-local-vars
8600 (list 'call-interactively (list 'quote cmd)))))
8604 (defun org-get-category (&optional pos force-refresh)
8605 "Get the category applying to position POS."
8607 (if force-refresh (org-refresh-category-properties))
8608 (let ((pos (or pos (point))))
8609 (or (get-text-property pos 'org-category)
8610 (progn (org-refresh-category-properties)
8611 (get-text-property pos 'org-category))))))
8613 (defun org-refresh-category-properties ()
8614 "Refresh category text properties in the buffer."
8615 (let ((case-fold-search t)
8616 (inhibit-read-only t)
8618 ((null org-category)
8619 (if buffer-file-name
8620 (file-name-sans-extension
8621 (file-name-nondirectory buffer-file-name))
8623 ((symbolp org-category) (symbol-name org-category))
8625 beg end cat pos optionp)
8630 (goto-char (point-min))
8631 (put-text-property (point) (point-max) 'org-category def-cat)
8632 (while (re-search-forward
8633 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8634 (setq pos (match-end 0)
8635 optionp (equal (char-after (match-beginning 0)) ?#)
8636 cat (org-trim (match-string 2)))
8638 (setq beg (point-at-bol) end (point-max))
8639 (org-back-to-heading t)
8640 (setq beg (point) end (org-end-of-subtree t t)))
8641 (put-text-property beg end 'org-category cat)
8642 (put-text-property beg end 'org-category-position beg)
8643 (goto-char pos)))))))
8645 (defun org-refresh-properties (dprop tprop)
8646 "Refresh buffer text properties.
8647 DPROP is the drawer property and TPROP is the corresponding text
8649 (let ((case-fold-search t)
8650 (inhibit-read-only t) p)
8655 (goto-char (point-min))
8656 (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
8657 (setq p (org-match-string-no-properties 1))
8659 (org-back-to-heading t)
8661 (point-at-bol) (point-at-eol) tprop p))))))))
8666 ;;; Link abbreviations
8668 (defun org-link-expand-abbrev (link)
8669 "Apply replacements as defined in `org-link-abbrev-alist'."
8670 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
8671 (let* ((key (match-string 1 link))
8672 (as (or (assoc key org-link-abbrev-alist-local)
8673 (assoc key org-link-abbrev-alist)))
8674 (tag (and (match-end 2) (match-string 3 link)))
8680 ((symbolp rpl) (funcall rpl tag))
8681 ((string-match "%(\\([^)]+\\))" rpl)
8682 (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
8683 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
8684 ((string-match "%h" rpl)
8685 (replace-match (url-hexify-string (or tag "")) t t rpl))
8686 (t (concat rpl tag)))))
8689 ;;; Storing and inserting links
8691 (defvar org-insert-link-history nil
8692 "Minibuffer history for links inserted with `org-insert-link'.")
8694 (defvar org-stored-links nil
8695 "Contains the links stored with `org-store-link'.")
8697 (defvar org-store-link-plist nil
8698 "Plist with info about the most recently link created with `org-store-link'.")
8700 (defvar org-link-protocols nil
8701 "Link protocols added to Org-mode using `org-add-link-type'.")
8703 (defvar org-store-link-functions nil
8704 "List of functions that are called to create and store a link.
8705 Each function will be called in turn until one returns a non-nil
8706 value. Each function should check if it is responsible for creating
8707 this link (for example by looking at the major mode).
8708 If not, it must exit and return nil.
8709 If yes, it should return a non-nil value after a calling
8710 `org-store-link-props' with a list of properties and values.
8711 Special properties are:
8713 :type The link prefix, like \"http\". This must be given.
8714 :link The link, like \"http://www.astro.uva.nl/~dominik\".
8715 This is obligatory as well.
8716 :description Optional default description for the second pair
8717 of brackets in an Org-mode link. The user can still change
8718 this when inserting this link into an Org-mode buffer.
8720 In addition to these, any additional properties can be specified
8721 and then used in capture templates.")
8723 (defun org-add-link-type (type &optional follow export)
8724 "Add TYPE to the list of `org-link-types'.
8725 Re-compute all regular expressions depending on `org-link-types'
8727 FOLLOW and EXPORT are two functions.
8729 FOLLOW should take the link path as the single argument and do whatever
8730 is necessary to follow the link, for example find a file or display
8733 EXPORT should format the link path for export to one of the export formats.
8734 It should be a function accepting three arguments:
8736 path the path of the link, the text after the prefix (like \"http:\")
8737 desc the description of the link, if any, or a description added by
8738 org-export-normalize-links if there is none
8739 format the export format, a symbol like `html' or `latex' or `ascii'..
8741 The function may use the FORMAT information to return different values
8742 depending on the format. The return value will be put literally into
8743 the exported file. If the return value is nil, this means Org should
8744 do what it normally does with links which do not have EXPORT defined.
8746 Org-mode has a built-in default for exporting links. If you are happy with
8747 this default, there is no need to define an export function for the link
8748 type. For a simple example of an export function, see `org-bbdb.el'."
8749 (add-to-list 'org-link-types type t)
8750 (org-make-link-regexps)
8751 (if (assoc type org-link-protocols)
8752 (setcdr (assoc type org-link-protocols) (list follow export))
8753 (push (list type follow export) org-link-protocols)))
8755 (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
8756 (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
8759 (defun org-store-link (arg)
8760 "\\<org-mode-map>Store an org-link to the current location.
8761 This link is added to `org-stored-links' and can later be inserted
8762 into an org-buffer with \\[org-insert-link].
8764 For some link types, a prefix arg is interpreted:
8765 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
8766 For file links, arg negates `org-context-in-file-links'."
8768 (org-load-modules-maybe)
8769 (setq org-store-link-plist nil) ; reset
8770 (org-with-limited-levels
8771 (let (link cpltxt desc description search txt custom-id agenda-link)
8774 ((run-hook-with-args-until-success 'org-store-link-functions)
8775 (setq link (plist-get org-store-link-plist :link)
8776 desc (or (plist-get org-store-link-plist :description) link)))
8778 ((org-src-edit-buffer-p)
8780 (while (or (not label)
8784 (goto-char (point-min))
8786 (regexp-quote (format org-coderef-label-format label))
8788 (when label (message "Label exists already") (sit-for 2))
8789 (setq label (read-string "Code line label: " label)))
8791 (setq link (format org-coderef-label-format label))
8792 (setq gc (- 79 (length link)))
8793 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
8795 (setq link (concat "(" label ")") desc nil)))
8797 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
8798 ;; We are in the agenda, link to referenced location
8799 (let ((m (or (get-text-property (point) 'org-hd-marker)
8800 (get-text-property (point) 'org-marker))))
8802 (org-with-point-at m
8804 (if (org-called-interactively-p 'any)
8805 (call-interactively 'org-store-link)
8806 (org-store-link nil)))))))
8808 ((eq major-mode 'calendar-mode)
8809 (let ((cd (calendar-cursor-to-date)))
8812 (car org-time-stamp-formats)
8814 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8816 (org-store-link-props :type "calendar" :date cd)))
8818 ((eq major-mode 'help-mode)
8819 (setq link (concat "help:" (save-excursion
8820 (goto-char (point-min))
8821 (looking-at "^[^ ]+")
8823 (org-store-link-props :type "help"))
8825 ((eq major-mode 'w3-mode)
8826 (setq cpltxt (if (and (buffer-name)
8827 (not (string-match "Untitled" (buffer-name))))
8830 link (url-view-url t))
8831 (org-store-link-props :type "w3" :url (url-view-url t)))
8833 ((eq major-mode 'w3m-mode)
8834 (setq cpltxt (or w3m-current-title w3m-current-url)
8835 link w3m-current-url)
8836 (org-store-link-props :type "w3m" :url (url-view-url t)))
8838 ((setq search (run-hook-with-args-until-success
8839 'org-create-file-search-functions))
8840 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8842 (setq cpltxt (or description link)))
8844 ((eq major-mode 'image-mode)
8845 (setq cpltxt (concat "file:"
8846 (abbreviate-file-name buffer-file-name))
8848 (org-store-link-props :type "image" :file buffer-file-name))
8850 ((eq major-mode 'dired-mode)
8851 ;; link to the file in the current line
8852 (let ((file (dired-get-filename nil t)))
8854 (abbreviate-file-name
8855 (expand-file-name (dired-get-filename nil t)))
8856 ;; otherwise, no file so use current directory.
8858 (setq cpltxt (concat "file:" file)
8861 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
8862 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
8864 ((org-in-regexp "<<\\(.*?\\)>>")
8867 (abbreviate-file-name
8868 (buffer-file-name (buffer-base-buffer)))
8869 "::" (match-string 1))
8871 ((and (featurep 'org-id)
8872 (or (eq org-id-link-to-org-use-id t)
8873 (and (org-called-interactively-p 'any)
8874 (or (eq org-id-link-to-org-use-id 'create-if-interactive)
8875 (and (eq org-id-link-to-org-use-id
8876 'create-if-interactive-and-no-custom-id)
8878 (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
8879 ;; We can make a link using the ID.
8880 (setq link (condition-case nil
8881 (prog1 (org-id-store-link)
8882 (setq desc (plist-get org-store-link-plist :description)))
8884 ;; probably before first headline, link to file only
8886 (abbreviate-file-name
8887 (buffer-file-name (buffer-base-buffer))))))))
8889 ;; Just link to current headline
8890 (setq cpltxt (concat "file:"
8891 (abbreviate-file-name
8892 (buffer-file-name (buffer-base-buffer)))))
8893 ;; Add a context search string
8894 (when (org-xor org-context-in-file-links arg)
8896 ((org-at-heading-p) nil)
8897 ((org-region-active-p)
8898 (buffer-substring (region-beginning) (region-end)))))
8899 (when (or (null txt) (string-match "\\S-" txt))
8903 (org-make-org-heading-search-string txt)
8905 desc (or (nth 4 (ignore-errors
8906 (org-heading-components))) "NONE"))))
8907 (if (string-match "::\\'" cpltxt)
8908 (setq cpltxt (substring cpltxt 0 -2)))
8909 (setq link cpltxt))))
8911 ((buffer-file-name (buffer-base-buffer))
8912 ;; Just link to this file here.
8913 (setq cpltxt (concat "file:"
8914 (abbreviate-file-name
8915 (buffer-file-name (buffer-base-buffer)))))
8916 ;; Add a context string
8917 (when (org-xor org-context-in-file-links arg)
8918 (setq txt (if (org-region-active-p)
8919 (buffer-substring (region-beginning) (region-end))
8920 (buffer-substring (point-at-bol) (point-at-eol))))
8921 ;; Only use search option if there is some text.
8922 (when (string-match "\\S-" txt)
8924 (concat cpltxt "::" (org-make-org-heading-search-string txt))
8928 ((org-called-interactively-p 'interactive)
8929 (error "Cannot link to a buffer which is not visiting a file"))
8931 (t (setq link nil)))
8933 (if (consp link) (setq cpltxt (car link) link (cdr link)))
8934 (setq link (or link cpltxt)
8935 desc (or desc cpltxt))
8936 (if (equal desc "NONE") (setq desc nil))
8938 (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
8940 (setq org-stored-links
8941 (cons (list link desc) org-stored-links))
8942 (message "Stored: %s" (or desc link))
8944 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
8946 (setq org-stored-links
8947 (cons (list link desc) org-stored-links))))
8948 (or agenda-link (and link (org-make-link-string link desc)))))))
8950 (defun org-store-link-props (&rest plist)
8951 "Store link properties, extract names and addresses."
8953 (when (setq x (plist-get plist :from))
8954 (setq adr (mail-extract-address-components x))
8955 (setq plist (plist-put plist :fromname (car adr)))
8956 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
8957 (when (setq x (plist-get plist :to))
8958 (setq adr (mail-extract-address-components x))
8959 (setq plist (plist-put plist :toname (car adr)))
8960 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
8961 (let ((from (plist-get plist :from))
8962 (to (plist-get plist :to)))
8963 (when (and from to org-from-is-user-regexp)
8965 (plist-put plist :fromto
8966 (if (string-match org-from-is-user-regexp from)
8968 (concat "from %f"))))))
8969 (setq org-store-link-plist plist))
8971 (defun org-add-link-props (&rest plist)
8972 "Add these properties to the link property list."
8975 (setq key (pop plist) value (pop plist))
8976 (setq org-store-link-plist
8977 (plist-put org-store-link-plist key value)))))
8979 (defun org-email-link-description (&optional fmt)
8980 "Return the description part of an email link.
8981 This takes information from `org-store-link-plist' and formats it
8982 according to FMT (default from `org-email-link-description-format')."
8983 (setq fmt (or fmt org-email-link-description-format))
8984 (let* ((p org-store-link-plist)
8985 (to (plist-get p :toaddress))
8986 (from (plist-get p :fromaddress))
8989 (cons "%c" (plist-get p :fromto))
8990 (cons "%F" (plist-get p :from))
8991 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
8992 (cons "%T" (plist-get p :to))
8993 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
8994 (cons "%s" (plist-get p :subject))
8995 (cons "%d" (plist-get p :date))
8996 (cons "%m" (plist-get p :message-id)))))
8997 (when (string-match "%c" fmt)
8998 ;; Check if the user wrote this message
8999 (if (and org-from-is-user-regexp from to
9000 (save-match-data (string-match org-from-is-user-regexp from)))
9001 (setq fmt (replace-match "to %t" t t fmt))
9002 (setq fmt (replace-match "from %f" t t fmt))))
9003 (org-replace-escapes fmt table)))
9005 (defun org-make-org-heading-search-string (&optional string heading)
9006 "Make search string for STRING or current headline."
9008 (let ((s (or string (org-get-heading)))
9009 (lines org-context-in-file-links))
9010 (unless (and string (not heading))
9011 ;; We are using a headline, clean up garbage in there.
9012 (if (string-match org-todo-regexp s)
9013 (setq s (replace-match "" t t s)))
9014 (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
9015 (setq s (replace-match "" t t s)))
9016 (setq s (org-trim s))
9017 (if (string-match (concat "^\\(" org-quote-string "\\|"
9018 org-comment-string "\\)") s)
9019 (setq s (replace-match "" t t s)))
9020 (while (string-match org-ts-regexp s)
9021 (setq s (replace-match "" t t s))))
9022 (or string (setq s (concat "*" s))) ; Add * for headlines
9023 (when (and string (integerp lines) (> lines 0))
9024 (let ((slines (org-split-string s "\n")))
9025 (when (< lines (length slines))
9028 (reverse (nthcdr (- (length slines) lines)
9029 (reverse slines))) "\n")))))
9030 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
9032 (defun org-make-link-string (link &optional description)
9033 "Make a link with brackets, consisting of LINK and DESCRIPTION."
9034 (unless (string-match "\\S-" link)
9035 (error "Empty link"))
9036 (when (and description
9037 (stringp description)
9038 (not (string-match "\\S-" description)))
9039 (setq description nil))
9040 (when (stringp description)
9041 ;; Remove brackets from the description, they are fatal.
9042 (while (string-match "\\[" description)
9043 (setq description (replace-match "{" t t description)))
9044 (while (string-match "\\]" description)
9045 (setq description (replace-match "}" t t description))))
9046 (when (equal link description)
9047 ;; No description needed, it is identical
9048 (setq description nil))
9049 (when (and (not description)
9050 (not (string-match (org-image-file-name-regexp) link))
9051 (not (equal link (org-link-escape link))))
9052 (setq description (org-extract-attributes link)))
9054 (cond ((string-match (org-image-file-name-regexp) link) link)
9055 ((string-match org-link-types-re link)
9056 (concat (match-string 1 link)
9057 (org-link-escape (substring link (match-end 1)))))
9058 (t (org-link-escape link))))
9059 (concat "[[" link "]"
9060 (if description (concat "[" description "]") "")
9063 (defconst org-link-escape-chars
9064 '(?\ ?\[ ?\] ?\; ?\= ?\+)
9065 "List of characters that should be escaped in link.
9066 This is the list that is used for internal purposes.")
9068 (defconst org-link-escape-chars-browser
9070 "List of escapes for characters that are problematic in links.
9071 This is the list that is used before handing over to the browser.")
9073 (defun org-link-escape (text &optional table merge)
9074 "Return percent escaped representation of TEXT.
9075 TEXT is a string with the text to escape.
9076 Optional argument TABLE is a list with characters that should be
9077 escaped. When nil, `org-link-escape-chars' is used.
9078 If optional argument MERGE is set, merge TABLE into
9079 `org-link-escape-chars'."
9082 (mapc (lambda (defchr)
9083 (unless (member defchr table)
9084 (setq table (cons defchr table)))) org-link-escape-chars))
9086 (setq table org-link-escape-chars)))
9089 (if (or (member char table)
9090 (and (or (< char 32) (= char 37) (> char 126))
9092 (mapconcat (lambda (sequence-element)
9093 (format "%%%.2X" sequence-element))
9094 (or (encode-coding-char char 'utf-8)
9095 (error "Unable to percent escape character: %s"
9096 (char-to-string char))) "")
9097 (char-to-string char))) text ""))
9099 (defun org-link-unescape (str)
9100 "Unhex hexified Unicode strings as returned from the JavaScript function
9101 encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
9102 (unless (and (null str) (string= "" str))
9103 (let ((pos 0) (case-fold-search t) unhexed)
9104 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
9105 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
9106 (setq str (replace-match unhexed t t str))
9107 (setq pos (+ pos (length unhexed))))))
9110 (defun org-link-unescape-compound (hex)
9111 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
9112 Note: this function also decodes single byte encodings like
9113 `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
9115 (let* ((bytes (cdr (split-string hex "%")))
9120 (let* ((val (string-to-number (pop bytes) 16))
9124 ((>= val 252) (cons 6 252))
9125 ((>= val 248) (cons 5 248))
9126 ((>= val 240) (cons 4 240))
9127 ((>= val 224) (cons 3 224))
9128 ((>= val 192) (cons 2 192))
9131 (if (>= val 192) (setq eat (car shift-xor)))
9132 (setq val (logxor val (cdr shift-xor)))
9133 (setq sum (+ (lsh sum (car shift-xor)) val))
9134 (if (> eat 0) (setq eat (- eat 1)))
9136 ((= 0 eat) ;multi byte
9137 (setq ret (concat ret (org-char-to-string sum)))
9139 ((not bytes) ; single byte(s)
9140 (setq ret (org-link-unescape-single-byte-sequence hex))))
9141 )) ;; end (while bytes
9144 (defun org-link-unescape-single-byte-sequence (hex)
9145 "Unhexify hex-encoded single byte character sequences."
9146 (mapconcat (lambda (byte)
9147 (char-to-string (string-to-number byte 16)))
9148 (cdr (split-string hex "%")) ""))
9150 (defun org-xor (a b)
9154 (defun org-fixup-message-id-for-http (s)
9155 "Replace special characters in a message id, so it can be used in an http query."
9156 (when (string-match "%" s)
9157 (setq s (mapconcat (lambda (c)
9160 (char-to-string c)))
9162 (while (string-match "<" s)
9163 (setq s (replace-match "%3C" t t s)))
9164 (while (string-match ">" s)
9165 (setq s (replace-match "%3E" t t s)))
9166 (while (string-match "@" s)
9167 (setq s (replace-match "%40" t t s)))
9170 (defun org-link-prettify (link)
9171 "Return a human-readable representation of LINK.
9172 The car of LINK must be a raw link the cdr of LINK must be either
9173 a link description or nil."
9174 (let ((desc (or (cadr link) "<no description>")))
9175 (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
9176 "<" (car link) ">")))
9179 (defun org-insert-link-global ()
9180 "Insert a link like Org-mode does.
9181 This command can be called in any mode to insert a link in Org-mode syntax."
9183 (org-load-modules-maybe)
9184 (org-run-like-in-org-mode 'org-insert-link))
9186 (defun org-insert-all-links (&optional keep)
9187 "Insert all links in `org-stored-links'."
9189 (let ((links (copy-sequence org-stored-links)) l)
9190 (while (setq l (if keep (pop links) (pop org-stored-links)))
9192 (org-insert-link nil (car l) (cadr l))
9195 (defun org-link-fontify-links-to-this-file ()
9196 "Fontify links to the current file in `org-stored-links'."
9197 (let ((f (buffer-file-name)) a b)
9198 (setq a (mapcar (lambda(l)
9200 (when (and (string-match "^file:\\(.+\\)::" ll)
9201 (equal f (expand-file-name (match-string 1 ll))))
9204 (when (featurep 'org-id)
9205 (setq b (mapcar (lambda(l)
9207 (when (and (string-match "^id:\\(.+\\)$" ll)
9208 (equal f (expand-file-name
9209 (or (org-id-find-id-file
9210 (match-string 1 ll)) ""))))
9214 (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
9215 (delq nil (append a b)))))
9217 (defvar org-link-links-in-this-file nil)
9218 (defun org-insert-link (&optional complete-file link-location default-description)
9219 "Insert a link. At the prompt, enter the link.
9221 Completion can be used to insert any of the link protocol prefixes like
9224 The history can be used to select a link previously stored with
9225 `org-store-link'. When the empty string is entered (i.e. if you just
9226 press RET at the prompt), the link defaults to the most recently
9227 stored link. As SPC triggers completion in the minibuffer, you need to
9228 use M-SPC or C-q SPC to force the insertion of a space character.
9230 You will also be prompted for a description, and if one is given, it will
9231 be displayed in the buffer instead of the link.
9233 If there is already a link at point, this command will allow you to edit link
9234 and description parts.
9236 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9237 be selected using completion. The path to the file will be relative to the
9238 current directory if the file is in the current directory or a subdirectory.
9239 Otherwise, the link will be the absolute path as completed in the minibuffer
9240 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9241 option `org-link-file-path-type'.
9243 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9244 the current directory or below.
9246 With three \\[universal-argument] prefixes, negate the meaning of
9247 `org-keep-stored-link-after-insertion'.
9249 If `org-make-link-description-function' is non-nil, this function will be
9250 called with the link target, and the result will be the default
9253 If the LINK-LOCATION parameter is non-nil, this value will be
9254 used as the link location instead of reading one interactively.
9256 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9257 be used as the default description."
9259 (let* ((wcf (current-window-configuration))
9260 (region (if (org-region-active-p)
9261 (buffer-substring (region-beginning) (region-end))))
9262 (remove (and region (list (region-beginning) (region-end))))
9264 tmphist ; byte-compile incorrectly complains about this
9265 (link link-location)
9266 (abbrevs org-link-abbrev-alist-local)
9267 entry file all-prefixes auto-desc)
9269 (link-location) ; specified by arg, just use it.
9270 ((org-in-regexp org-bracket-link-regexp 1)
9271 ;; We do have a link at point, and we are going to edit it.
9272 (setq remove (list (match-beginning 0) (match-end 0)))
9273 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
9274 (setq link (read-string "Link: "
9276 (org-match-string-no-properties 1)))))
9277 ((or (org-in-regexp org-angle-link-re)
9278 (org-in-regexp org-plain-link-re))
9279 ;; Convert to bracket link
9280 (setq remove (list (match-beginning 0) (match-end 0))
9281 link (read-string "Link: "
9282 (org-remove-angle-brackets (match-string 0)))))
9283 ((member complete-file '((4) (16)))
9284 ;; Completing read for file names.
9285 (setq link (org-file-complete-link complete-file)))
9287 ;; Read link, with completion for stored links.
9288 (org-link-fontify-links-to-this-file)
9289 (org-switch-to-buffer-other-window "*Org Links*")
9290 (with-current-buffer "*Org Links*"
9292 (insert "Insert a link.
9293 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9294 (when org-stored-links
9295 (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9296 (insert (mapconcat 'org-link-prettify
9297 (reverse org-stored-links) "\n")))
9298 (goto-char (point-min)))
9299 (let ((cw (selected-window)))
9300 (select-window (get-buffer-window "*Org Links*" 'visible))
9301 (with-current-buffer "*Org Links*" (setq truncate-lines t))
9302 (unless (pos-visible-in-window-p (point-max))
9303 (org-fit-window-to-buffer))
9304 (and (window-live-p cw) (select-window cw)))
9305 ;; Fake a link history, containing the stored links.
9306 (setq tmphist (append (mapcar 'car org-stored-links)
9307 org-insert-link-history))
9308 (setq all-prefixes (append (mapcar 'car abbrevs)
9309 (mapcar 'car org-link-abbrev-alist)
9314 (let ((org-completion-use-ido nil)
9315 (org-completion-use-iswitchb nil))
9316 (org-completing-read
9319 (mapcar (lambda (x) (list (concat x ":")))
9321 (mapcar 'car org-stored-links)
9322 (mapcar 'cadr org-stored-links))
9325 (caar org-stored-links))))
9326 (if (not (string-match "\\S-" link))
9327 (error "No link selected"))
9329 (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
9331 (if (or (member link all-prefixes)
9332 (and (equal ":" (substring link -1))
9333 (member (substring link 0 -1) all-prefixes)
9334 (setq link (substring link 0 -1))))
9335 (setq link (org-link-try-special-completion link))))
9336 (set-window-configuration wcf)
9337 (kill-buffer "*Org Links*"))
9338 (setq entry (assoc link org-stored-links))
9339 (or entry (push link org-insert-link-history))
9340 (setq desc (or desc (nth 1 entry)))))
9342 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
9343 (not org-keep-stored-link-after-insertion))
9344 (setq org-stored-links (delq (assoc link org-stored-links)
9347 (if (string-match org-plain-link-re link)
9348 ;; URL-like link, normalize the use of angular brackets.
9349 (setq link (org-remove-angle-brackets link)))
9351 ;; Check if we are linking to the current file with a search
9352 ;; option If yes, simplify the link by using only the search
9354 (when (and buffer-file-name
9355 (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
9356 (let* ((path (match-string 1 link))
9357 (case-fold-search nil)
9358 (search (match-string 2 link)))
9360 (if (equal (file-truename buffer-file-name) (file-truename path))
9361 ;; We are linking to this same file, with a search option
9362 (setq link search)))))
9364 ;; Check if we can/should use a relative path. If yes, simplify the link
9365 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
9366 (let* ((type (match-string 1 link))
9367 (path (match-string 2 link))
9369 (case-fold-search nil))
9371 ((or (eq org-link-file-path-type 'absolute)
9372 (equal complete-file '(16)))
9373 (setq path (abbreviate-file-name (expand-file-name path))))
9374 ((eq org-link-file-path-type 'noabbrev)
9375 (setq path (expand-file-name path)))
9376 ((eq org-link-file-path-type 'relative)
9377 (setq path (file-relative-name path)))
9380 (if (string-match (concat "^" (regexp-quote
9382 (file-name-as-directory
9383 default-directory))))
9384 (expand-file-name path))
9385 ;; We are linking a file with relative path name.
9386 (setq path (substring (expand-file-name path)
9388 (setq path (abbreviate-file-name (expand-file-name path)))))))
9389 (setq link (concat type path))
9390 (if (equal desc origpath)
9393 (if org-make-link-description-function
9395 (or (condition-case nil
9396 (funcall org-make-link-description-function link desc)
9397 (error (progn (message "Can't get link description from `%s'"
9398 (symbol-name org-make-link-description-function))
9400 (read-string "Description: " default-description)))
9401 (if default-description (setq desc default-description)
9402 (setq desc (or (and auto-desc desc)
9403 (read-string "Description: " desc)))))
9405 (unless (string-match "\\S-" desc) (setq desc nil))
9406 (if remove (apply 'delete-region remove))
9407 (insert (org-make-link-string link desc))))
9409 (defun org-link-try-special-completion (type)
9410 "If there is completion support for link type TYPE, offer it."
9411 (let ((fun (intern (concat "org-" type "-complete-link"))))
9414 (read-string "Link (no completion support): " (concat type ":")))))
9416 (defun org-file-complete-link (&optional arg)
9417 "Create a file link using completion."
9419 (setq file (read-file-name "File: "))
9420 (let ((pwd (file-name-as-directory (expand-file-name ".")))
9421 (pwd1 (file-name-as-directory (abbreviate-file-name
9422 (expand-file-name ".")))))
9427 (abbreviate-file-name (expand-file-name file)))))
9428 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
9429 (setq link (concat "file:" (match-string 1 file))))
9430 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
9431 (expand-file-name file))
9433 "file:" (match-string 1 (expand-file-name file)))))
9434 (t (setq link (concat "file:" file)))))
9437 (defun org-completing-read (&rest args)
9438 "Completing-read with SPACE being a normal character."
9439 (let ((enable-recursive-minibuffers t)
9440 (minibuffer-local-completion-map
9441 (copy-keymap minibuffer-local-completion-map)))
9442 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
9443 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
9444 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
9445 (apply 'org-icompleting-read args)))
9447 (defun org-completing-read-no-i (&rest args)
9448 (let (org-completion-use-ido org-completion-use-iswitchb)
9449 (apply 'org-completing-read args)))
9451 (defun org-iswitchb-completing-read (prompt choices &rest args)
9452 "Use iswitch as a completing-read replacement to choose from choices.
9453 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
9455 (let* ((iswitchb-use-virtual-buffers nil)
9456 (iswitchb-make-buflist-hook
9458 (setq iswitchb-temp-buflist choices))))
9459 (iswitchb-read-buffer prompt)))
9461 (defun org-icompleting-read (&rest args)
9462 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
9463 (org-without-partial-completion
9464 (if (and org-completion-use-ido
9465 (fboundp 'ido-completing-read)
9466 (boundp 'ido-mode) ido-mode
9467 (listp (second args)))
9468 (let ((ido-enter-matching-directory nil))
9469 (apply 'ido-completing-read (concat (car args))
9470 (if (consp (car (nth 1 args)))
9471 (mapcar 'car (nth 1 args))
9474 (if (and org-completion-use-iswitchb
9475 (boundp 'iswitchb-mode) iswitchb-mode
9476 (listp (second args)))
9477 (apply 'org-iswitchb-completing-read (concat (car args))
9478 (if (consp (car (nth 1 args)))
9479 (mapcar 'car (nth 1 args))
9482 (apply 'completing-read args)))))
9484 (defun org-extract-attributes (s)
9485 "Extract the attributes cookie from a string and set as text property."
9486 (let (a attr (start 0) key value)
9488 (when (string-match "{{\\([^}]+\\)}}$" s)
9489 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
9490 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
9491 (setq key (match-string 1 a) value (match-string 2 a)
9493 attr (plist-put attr (intern key) value))))
9494 (org-add-props s nil 'org-attr attr))
9497 (defun org-extract-attributes-from-string (tag)
9498 (let (key value attr)
9499 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
9500 (setq key (match-string 1 tag) value (match-string 2 tag)
9501 tag (replace-match "" t t tag)
9502 attr (plist-put attr (intern key) value)))
9505 (defun org-attributes-to-string (plist)
9506 "Format a property list into an HTML attribute list."
9507 (let ((s "") key value)
9509 (setq key (pop plist) value (pop plist))
9511 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
9514 ;;; Opening/following a link
9516 (defvar org-link-search-failed nil)
9518 (defvar org-open-link-functions nil
9519 "Hook for functions finding a plain text link.
9520 These functions must take a single argument, the link content.
9521 They will be called for links that look like [[link text][description]]
9522 when LINK TEXT does not have a protocol like \"http:\" and does not look
9523 like a filename (e.g. \"./blue.png\").
9525 These functions will be called *before* Org attempts to resolve the
9526 link by doing text searches in the current buffer - so if you want a
9527 link \"[[target]]\" to still find \"<<target>>\", your function should
9528 handle this as a special case.
9530 When the function does handle the link, it must return a non-nil value.
9531 If it decides that it is not responsible for this link, it must return
9532 nil to indicate that that Org-mode can continue with other options
9533 like exact and fuzzy text search.")
9535 (defun org-next-link ()
9536 "Move forward to the next link.
9537 If the link is in hidden text, expose it."
9539 (when (and org-link-search-failed (eq this-command last-command))
9540 (goto-char (point-min))
9541 (message "Link search wrapped back to beginning of buffer"))
9542 (setq org-link-search-failed nil)
9543 (let* ((pos (point))
9545 (a (assoc :link ct)))
9546 (if a (goto-char (nth 2 a)))
9547 (if (re-search-forward org-any-link-re nil t)
9549 (goto-char (match-beginning 0))
9550 (if (outline-invisible-p) (org-show-context)))
9552 (setq org-link-search-failed t)
9553 (error "No further link found"))))
9555 (defun org-previous-link ()
9556 "Move backward to the previous link.
9557 If the link is in hidden text, expose it."
9559 (when (and org-link-search-failed (eq this-command last-command))
9560 (goto-char (point-max))
9561 (message "Link search wrapped back to end of buffer"))
9562 (setq org-link-search-failed nil)
9563 (let* ((pos (point))
9565 (a (assoc :link ct)))
9566 (if a (goto-char (nth 1 a)))
9567 (if (re-search-backward org-any-link-re nil t)
9569 (goto-char (match-beginning 0))
9570 (if (outline-invisible-p) (org-show-context)))
9572 (setq org-link-search-failed t)
9573 (error "No further link found"))))
9575 (defun org-translate-link (s)
9576 "Translate a link string if a translation function has been defined."
9577 (if (and org-link-translation-function
9578 (fboundp org-link-translation-function)
9579 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
9581 (setq s (funcall org-link-translation-function
9582 (match-string 1 s) (match-string 2 s)))
9583 (concat (car s) ":" (cdr s)))
9586 (defun org-translate-link-from-planner (type path)
9587 "Translate a link from Emacs Planner syntax so that Org can follow it.
9588 This is still an experimental function, your mileage may vary."
9590 ((member type '("http" "https" "news" "ftp"))
9591 ;; standard Internet links are the same.
9593 ((and (equal type "irc") (string-match "^//" path))
9594 ;; Planner has two / at the beginning of an irc link, we have 1.
9595 ;; We should have zero, actually....
9596 (setq path (substring path 1)))
9597 ((and (equal type "lisp") (string-match "^/" path))
9598 ;; Planner has a slash, we do not.
9599 (setq type "elisp" path (substring path 1)))
9600 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
9601 ;; A typical message link. Planner has the id after the final slash,
9602 ;; we separate it with a hash mark
9603 (setq path (concat (match-string 1 path) "#"
9604 (org-remove-angle-brackets (match-string 2 path)))))
9608 (defun org-find-file-at-mouse (ev)
9609 "Open file link or URL at mouse."
9611 (mouse-set-point ev)
9612 (org-open-at-point 'in-emacs))
9614 (defun org-open-at-mouse (ev)
9615 "Open file link or URL at mouse.
9616 See the docstring of `org-open-file' for details."
9618 (mouse-set-point ev)
9619 (if (eq major-mode 'org-agenda-mode)
9620 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
9621 (org-open-at-point))
9623 (defvar org-window-config-before-follow-link nil
9624 "The window configuration before following a link.
9625 This is saved in case the need arises to restore it.")
9627 (defvar org-open-link-marker (make-marker)
9628 "Marker pointing to the location where `org-open-at-point; was called.")
9631 (defun org-open-at-point-global ()
9632 "Follow a link like Org-mode does.
9633 This command can be called in any mode to follow a link that has
9636 (org-run-like-in-org-mode 'org-open-at-point))
9639 (defun org-open-link-from-string (s &optional arg reference-buffer)
9640 "Open a link in the string S, as if it was in Org-mode."
9641 (interactive "sLink: \nP")
9642 (let ((reference-buffer (or reference-buffer (current-buffer))))
9644 (let ((org-inhibit-startup (not reference-buffer)))
9647 (goto-char (point-min))
9648 (when reference-buffer
9649 (setq org-link-abbrev-alist-local
9650 (with-current-buffer reference-buffer
9651 org-link-abbrev-alist-local)))
9652 (org-open-at-point arg reference-buffer)))))
9654 (defvar org-open-at-point-functions nil
9655 "Hook that is run when following a link at point.
9657 Functions in this hook must return t if they identify and follow
9658 a link at point. If they don't find anything interesting at point,
9659 they must return nil.")
9661 (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
9662 (defun org-open-at-point (&optional arg reference-buffer)
9663 "Open link at or after point.
9664 If there is no link at point, this function will search forward up to
9665 the end of the current line.
9666 Normally, files will be opened by an appropriate application. If the
9667 optional prefix argument ARG is non-nil, Emacs will visit the file.
9668 With a double prefix argument, try to open outside of Emacs, in the
9669 application the system uses for this file type."
9671 ;; if in a code block, then open the block's results
9672 (unless (call-interactively #'org-babel-open-src-block-result)
9673 (org-load-modules-maybe)
9674 (move-marker org-open-link-marker (point))
9675 (setq org-window-config-before-follow-link (current-window-configuration))
9676 (org-remove-occur-highlights nil nil t)
9678 ((and (org-at-heading-p)
9679 (not (org-at-timestamp-p t))
9681 (concat org-plain-link-re "\\|"
9682 org-bracket-link-regexp "\\|"
9683 org-angle-link-re "\\|"
9684 "[ \t]:[^ \t\n]+:[ \t]*$")))
9685 (not (get-text-property (point) 'org-linked-text)))
9686 (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
9688 (lkend (cdr lkall)))
9690 (prog1 (search-forward lk nil lkend)
9691 (goto-char (match-beginning 0))
9692 (org-open-at-point))))
9693 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
9694 ((run-hook-with-args-until-success 'org-open-at-point-functions))
9695 ((and (org-at-timestamp-p t)
9696 (not (org-in-regexp org-bracket-link-regexp)))
9697 (org-follow-timestamp-link))
9698 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
9699 (not (org-in-regexp org-any-link-re)))
9700 (org-footnote-action))
9702 (let (type path link line search (pos (point)))
9705 (skip-chars-forward "^]\n\r")
9706 (when (org-in-regexp org-bracket-link-regexp 1)
9707 (setq link (org-extract-attributes
9708 (org-link-unescape (org-match-string-no-properties 1))))
9709 (while (string-match " *\n *" link)
9710 (setq link (replace-match " " t t link)))
9711 (setq link (org-link-expand-abbrev link))
9713 ((or (file-name-absolute-p link)
9714 (string-match "^\\.\\.?/" link))
9715 (setq type "file" path link))
9716 ((string-match org-link-re-with-space3 link)
9717 (setq type (match-string 1 link) path (match-string 2 link)))
9718 ((string-match "^help:+\\(.+\\)" link)
9719 (setq type "help" path (match-string 1 link)))
9720 (t (setq type "thisfile" path link)))
9723 (when (get-text-property (point) 'org-linked-text)
9724 (setq type "thisfile"
9725 pos (if (get-text-property (1+ (point)) 'org-linked-text)
9726 (1+ (point)) (point))
9727 path (buffer-substring
9728 (or (previous-single-property-change pos 'org-linked-text)
9730 (or (next-single-property-change pos 'org-linked-text)
9735 (let ((plinkpos (org-in-regexp org-plain-link-re)))
9736 (when (or (org-in-regexp org-angle-link-re)
9737 (and plinkpos (goto-char (car plinkpos))
9738 (save-match-data (not (looking-back "\\[\\[")))))
9739 (setq type (match-string 1)
9740 path (org-link-unescape (match-string 2)))
9743 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
9745 path (match-string 1))
9746 (while (string-match ":" path)
9747 (setq path (replace-match "+" t t path)))
9749 (when (org-in-regexp "<\\([^><\n]+\\)>")
9750 (setq type "tree-match"
9751 path (match-string 1))
9754 (user-error "No link found"))
9756 ;; switch back to reference buffer
9757 ;; needed when if called in a temporary buffer through
9758 ;; org-open-link-from-string
9759 (with-current-buffer (or reference-buffer (current-buffer))
9761 ;; Remove any trailing spaces in path
9762 (if (string-match " +\\'" path)
9763 (setq path (replace-match "" t t path)))
9764 (if (and org-link-translation-function
9765 (fboundp org-link-translation-function))
9766 ;; Check if we need to translate the link
9767 (let ((tmp (funcall org-link-translation-function type path)))
9768 (setq type (car tmp) path (cdr tmp))))
9772 ((assoc type org-link-protocols)
9773 (funcall (nth 1 (assoc type org-link-protocols)) path))
9775 ((equal type "help")
9776 (let ((f-or-v (intern path)))
9777 (cond ((fboundp f-or-v)
9778 (describe-function f-or-v))
9780 (describe-variable f-or-v))
9781 (t (error "Not a known function or variable")))))
9783 ((equal type "mailto")
9784 (let ((cmd (car org-link-mailto-program))
9785 (args (cdr org-link-mailto-program)) args1
9786 (address path) (subject "") a)
9787 (if (string-match "\\(.*\\)::\\(.*\\)" path)
9788 (setq address (match-string 1 path)
9789 subject (org-link-escape (match-string 2 path))))
9792 ((not (stringp (car args))) (push (pop args) args1))
9793 (t (setq a (pop args))
9794 (if (string-match "%a" a)
9795 (setq a (replace-match address t t a)))
9796 (if (string-match "%s" a)
9797 (setq a (replace-match subject t t a)))
9799 (apply cmd (nreverse args1))))
9801 ((member type '("http" "https" "ftp" "news"))
9802 (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
9804 path org-link-escape-chars-browser)
9807 ((string= type "doi")
9808 (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
9810 path org-link-escape-chars-browser)
9813 ((member type '("message"))
9814 (browse-url (concat type ":" path)))
9816 ((string= type "tags")
9817 (org-tags-view arg path))
9819 ((string= type "tree-match")
9820 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
9822 ((string= type "file")
9823 (if (string-match "::\\([0-9]+\\)\\'" path)
9824 (setq line (string-to-number (match-string 1 path))
9825 path (substring path 0 (match-beginning 0)))
9826 (if (string-match "::\\(.+\\)\\'" path)
9827 (setq search (match-string 1 path)
9828 path (substring path 0 (match-beginning 0)))))
9829 (if (string-match "[*?{]" (file-name-nondirectory path))
9831 (org-open-file path arg line search)))
9833 ((string= type "shell")
9834 (let ((buf (generate-new-buffer "*Org Shell Output"))
9836 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
9837 (string-match org-confirm-shell-link-not-regexp cmd))
9838 (not org-confirm-shell-link-function)
9839 (funcall org-confirm-shell-link-function
9840 (format "Execute \"%s\" in shell? "
9841 (org-add-props cmd nil
9842 'face 'org-warning))))
9844 (message "Executing %s" cmd)
9845 (shell-command cmd buf)
9846 (if (featurep 'midnight)
9847 (setq clean-buffer-list-kill-buffer-names
9848 (cons buf clean-buffer-list-kill-buffer-names))))
9851 ((string= type "elisp")
9853 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
9854 (string-match org-confirm-elisp-link-not-regexp cmd))
9855 (not org-confirm-elisp-link-function)
9856 (funcall org-confirm-elisp-link-function
9857 (format "Execute \"%s\" as elisp? "
9858 (org-add-props cmd nil
9859 'face 'org-warning))))
9860 (message "%s => %s" cmd
9861 (if (equal (string-to-char cmd) ?\()
9863 (call-interactively (read cmd))))
9866 ((and (string= type "thisfile")
9867 (run-hook-with-args-until-success
9868 'org-open-link-functions path)))
9870 ((string= type "thisfile")
9872 (switch-to-buffer-other-window
9873 (org-get-buffer-for-internal-link (current-buffer)))
9874 (org-mark-ring-push))
9875 (let ((cmd `(org-link-search
9877 ,(cond ((equal arg '(4)) ''occur)
9878 ((equal arg '(16)) ''org-occur))
9880 (condition-case nil (let ((org-link-search-inhibit-query t))
9882 (error (progn (widen) (eval cmd))))))
9884 (t (browse-url-at-point)))))))
9885 (move-marker org-open-link-marker nil)
9886 (run-hook-with-args 'org-follow-link-hook)))
9888 (defun org-offer-links-in-entry (buffer marker &optional nth zero)
9889 "Offer links in the current entry and return the selected link.
9890 If there is only one link, return it.
9891 If NTH is an integer, return the NTH link found.
9892 If ZERO is a string, check also this string for a link, and if
9893 there is one, return it."
9894 (with-current-buffer buffer
9899 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
9900 "\\(" org-angle-link-re "\\)\\|"
9901 "\\(" org-plain-link-re "\\)"))
9903 (in-emacs (if (integerp nth) nil nth))
9904 have-zero end links link c)
9905 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
9906 (push (match-string 0 zero) links)
9907 (setq cnt (1- cnt) have-zero t))
9909 (org-back-to-heading t)
9910 (setq end (save-excursion (outline-next-heading) (point)))
9911 (while (re-search-forward re end t)
9912 (push (match-string 0) links))
9913 (setq links (org-uniquify (reverse links))))
9916 (message "No links"))
9917 ((equal (length links) 1)
9918 (setq link (car links)))
9919 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
9920 (setq link (nth (if have-zero nth (1- nth)) links)))
9921 (t ; we have to select a link
9923 (save-window-excursion
9924 (delete-other-windows)
9925 (with-output-to-temp-buffer "*Select Link*"
9927 (if (not (string-match org-bracket-link-regexp l))
9928 (princ (format "[%c] %s\n" (incf cnt)
9929 (org-remove-angle-brackets l)))
9931 (princ (format "[%c] %s (%s)\n" (incf cnt)
9932 (match-string 3 l) (match-string 1 l)))
9933 (princ (format "[%c] %s\n" (incf cnt)
9934 (match-string 1 l))))))
9936 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
9937 (message "Select link to open, RET to open all:")
9938 (setq c (read-char-exclusive))
9939 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
9940 (when (equal c ?q) (error "Abort"))
9944 (if have-zero (setq nth (1+ nth)))
9945 (unless (and (integerp nth) (>= (length links) nth))
9946 (error "Invalid link selection"))
9947 (setq link (nth (1- nth) links)))))
9948 (cons link end))))))
9950 ;; Add special file links that specify the way of opening
9952 (org-add-link-type "file+sys" 'org-open-file-with-system)
9953 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
9954 (defun org-open-file-with-system (path)
9955 "Open file at PATH using the system way of opening it."
9956 (org-open-file path 'system))
9957 (defun org-open-file-with-emacs (path)
9958 "Open file at PATH in Emacs."
9959 (org-open-file path 'emacs))
9960 (defun org-remove-file-link-modifiers ()
9961 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
9962 (goto-char (point-min))
9963 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
9965 (replace-match "file:" t t))))
9966 (eval-after-load "org-exp"
9967 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
9968 'org-remove-file-link-modifiers))
9972 (defvar org-create-file-search-functions nil
9973 "List of functions to construct the right search string for a file link.
9974 These functions are called in turn with point at the location to
9975 which the link should point.
9977 A function in the hook should first test if it would like to
9978 handle this file type, for example by checking the `major-mode'
9979 or the file extension. If it decides not to handle this file, it
9980 should just return nil to give other functions a chance. If it
9981 does handle the file, it must return the search string to be used
9982 when following the link. The search string will be part of the
9983 file link, given after a double colon, and `org-open-at-point'
9984 will automatically search for it. If special measures must be
9985 taken to make the search successful, another function should be
9986 added to the companion hook `org-execute-file-search-functions',
9989 A function in this hook may also use `setq' to set the variable
9990 `description' to provide a suggestion for the descriptive text to
9991 be used for this link when it gets inserted into an Org-mode
9992 buffer with \\[org-insert-link].")
9994 (defvar org-execute-file-search-functions nil
9995 "List of functions to execute a file search triggered by a link.
9997 Functions added to this hook must accept a single argument, the
9998 search string that was part of the file link, the part after the
9999 double colon. The function must first check if it would like to
10000 handle this search, for example by checking the `major-mode' or
10001 the file extension. If it decides not to handle this search, it
10002 should just return nil to give other functions a chance. If it
10003 does handle the search, it must return a non-nil value to keep
10004 other functions from trying.
10006 Each function can access the current prefix argument through the
10007 variable `current-prefix-argument'. Note that a single prefix is
10008 used to force opening a link in Emacs, so it may be good to only
10009 use a numeric or double prefix to guide the search function.
10011 In case this is needed, a function in this hook can also restore
10012 the window configuration before `org-open-at-point' was called using:
10014 (set-window-configuration org-window-config-before-follow-link)")
10016 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
10017 (defun org-link-search (s &optional type avoid-pos stealth)
10018 "Search for a link search option.
10019 If S is surrounded by forward slashes, it is interpreted as a
10020 regular expression. In org-mode files, this will create an `org-occur'
10021 sparse tree. In ordinary files, `occur' will be used to list matches.
10022 If the current buffer is in `dired-mode', grep will be used to search
10023 in all files. If AVOID-POS is given, ignore matches near that position.
10025 When optional argument STEALTH is non-nil, do not modify
10026 visibility around point, thus ignoring
10027 `org-show-hierarchy-above', `org-show-following-heading' and
10028 `org-show-siblings' variables."
10029 (let ((case-fold-search t)
10030 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
10031 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
10032 (append '(("") (" ") ("\t") ("\n"))
10033 org-emphasis-alist)
10036 (pre nil) (post nil)
10037 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
10039 ;; First check if there are any special search functions
10040 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
10041 ;; Now try the builtin stuff
10042 ((and (equal (string-to-char s0) ?#)
10045 (goto-char (point-min))
10048 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
10049 (setq type 'dedicated
10050 pos (match-beginning 0))))
10051 ;; There is an exact target for this
10053 (org-back-to-heading t)))
10055 (goto-char (point-min))
10058 (concat "<<" (regexp-quote s0) ">>") nil t)
10059 (setq type 'dedicated
10060 pos (match-beginning 0))))
10061 ;; There is an exact target for this
10064 (goto-char (point-min))
10067 (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
10068 (setq type 'dedicated pos (match-beginning 0))))
10069 ;; Found an invisible target.
10072 (goto-char (point-min))
10075 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
10076 (setq type 'dedicated pos (match-beginning 0))))
10077 ;; Found an element with a matching #+name affiliated keyword.
10079 ((and (string-match "^(\\(.*\\))$" s0)
10081 (goto-char (point-min))
10084 (concat "[^[]" (regexp-quote
10085 (format org-coderef-label-format
10086 (match-string 1 s0))))
10088 (setq type 'dedicated
10089 pos (1+ (match-beginning 0))))))
10090 ;; There is a coderef target for this
10092 ((string-match "^/\\(.*\\)/$" s)
10093 ;; A regular expression
10095 ((derived-mode-p 'org-mode)
10096 (org-occur (match-string 1 s)))
10097 ;;((eq major-mode 'dired-mode)
10098 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
10099 (t (org-do-occur (match-string 1 s)))))
10100 ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
10101 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
10102 (goto-char (point-min))
10104 ((let (case-fold-search)
10105 (re-search-forward (format org-complex-heading-regexp-format
10108 ;; OK, found a match
10109 (setq type 'dedicated)
10110 (goto-char (match-beginning 0)))
10111 ((and (not org-link-search-inhibit-query)
10112 (eq org-link-search-must-match-exact-headline 'query-to-create)
10113 (y-or-n-p "No match - create this as a new heading? "))
10114 (goto-char (point-max))
10115 (or (bolp) (newline))
10116 (insert "* " s "\n")
10117 (beginning-of-line 0))
10120 (error "No match"))))
10122 ;; A normal search string
10123 (when (equal (string-to-char s) ?*)
10124 ;; Anchor on headlines, post may include tags.
10125 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
10126 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
10127 s (substring s 1)))
10128 (remove-text-properties
10130 '(face nil mouse-face nil keymap nil fontified nil) s)
10131 ;; Make a series of regular expressions to find a match
10132 (setq words (org-split-string s "[ \n\r\t]+")
10134 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
10135 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
10137 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
10138 re2a (concat "[ \t\r\n]" re2a_)
10139 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
10140 re4 (concat "[^a-zA-Z_]" re4_)
10142 re1 (concat pre re2 post)
10143 re3 (concat pre (if pre re4_ re4) post)
10144 re5 (concat pre ".*" re4)
10145 re2 (concat pre re2)
10146 re2a (concat pre (if pre re2a_ re2a))
10147 re4 (concat pre (if pre re4_ re4))
10148 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
10149 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
10153 ((eq type 'org-occur) (org-occur reall))
10154 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
10155 (t (goto-char (point-min))
10157 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
10158 (org-search-not-self 1 re1 nil t)
10159 (org-search-not-self 1 re2 nil t)
10160 (org-search-not-self 1 re2a nil t)
10161 (org-search-not-self 1 re3 nil t)
10162 (org-search-not-self 1 re4 nil t)
10163 (org-search-not-self 1 re5 nil t)
10165 (goto-char (match-beginning 1))
10167 (error "No match"))))))
10168 (and (derived-mode-p 'org-mode)
10170 (org-show-context 'link-search))
10173 (defun org-search-not-self (group &rest args)
10174 "Execute `re-search-forward', but only accept matches that do not
10175 enclose the position of `org-open-link-marker'."
10176 (let ((m org-open-link-marker))
10178 (while (apply 're-search-forward args)
10179 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
10180 (goto-char (match-end group))
10181 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
10182 (> (match-beginning 0) (marker-position m))
10183 (< (match-end 0) (marker-position m)))
10185 (or (not (org-in-regexp
10186 org-bracket-link-analytic-regexp 1))
10187 (not (match-end 4)) ; no description
10188 (and (<= (match-beginning 4) (point))
10189 (>= (match-end 4) (point))))))
10190 (throw 'exit (point))))))))
10192 (defun org-get-buffer-for-internal-link (buffer)
10193 "Return a buffer to be used for displaying the link target of internal links."
10195 ((not org-display-internal-link-with-indirect-buffer)
10197 ((string-match "(Clone)$" (buffer-name buffer))
10198 (message "Buffer is already a clone, not making another one")
10199 ;; we also do not modify visibility in this case
10201 (t ; make a new indirect buffer for displaying the link
10202 (let* ((bn (buffer-name buffer))
10203 (ibn (concat bn "(Clone)"))
10204 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
10205 (with-current-buffer ib (org-overview))
10208 (defun org-do-occur (regexp &optional cleanup)
10209 "Call the Emacs command `occur'.
10210 If CLEANUP is non-nil, remove the printout of the regular expression
10211 in the *Occur* buffer. This is useful if the regex is long and not useful
10215 (let ((cwin (selected-window)) win beg end)
10216 (when (setq win (get-buffer-window "*Occur*"))
10217 (select-window win))
10218 (goto-char (point-min))
10219 (when (re-search-forward "match[a-z]+" nil t)
10220 (setq beg (match-end 0))
10221 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
10222 (setq end (1- (match-beginning 0)))))
10223 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
10224 (goto-char (point-min))
10225 (select-window cwin))))
10227 ;;; The mark ring for links jumps
10229 (defvar org-mark-ring nil
10230 "Mark ring for positions before jumps in Org-mode.")
10231 (defvar org-mark-ring-last-goto nil
10232 "Last position in the mark ring used to go back.")
10233 ;; Fill and close the ring
10234 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
10235 (loop for i from 1 to org-mark-ring-length do
10236 (push (make-marker) org-mark-ring))
10237 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
10240 (defun org-mark-ring-push (&optional pos buffer)
10241 "Put the current position or POS into the mark ring and rotate it."
10243 (setq pos (or pos (point)))
10244 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10245 (move-marker (car org-mark-ring)
10247 (or buffer (current-buffer)))
10249 (substitute-command-keys
10250 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10252 (defun org-mark-ring-goto (&optional n)
10253 "Jump to the previous position in the mark ring.
10254 With prefix arg N, jump back that many stored positions. When
10255 called several times in succession, walk through the entire ring.
10256 Org-mode commands jumping to a different position in the current file,
10257 or to another Org-mode file, automatically push the old position
10261 (if (eq last-command this-command)
10262 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10263 (setq p org-mark-ring))
10264 (setq org-mark-ring-last-goto p)
10266 (org-pop-to-buffer-same-window (marker-buffer m))
10268 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
10270 (defun org-remove-angle-brackets (s)
10271 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
10272 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
10274 (defun org-add-angle-brackets (s)
10275 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
10276 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
10278 (defun org-remove-double-quotes (s)
10279 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
10280 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
10283 ;;; Following specific links
10285 (defun org-follow-timestamp-link ()
10286 "Open an agenda view for the time-stamp date/range at point."
10288 ((org-at-date-range-p t)
10289 (let ((org-agenda-start-on-weekday)
10290 (t1 (match-string 1))
10291 (t2 (match-string 2)) tt1 tt2)
10292 (setq tt1 (time-to-days (org-time-string-to-time t1))
10293 tt2 (time-to-days (org-time-string-to-time t2)))
10294 (let ((org-agenda-buffer-tmp-name
10295 (format "*Org Agenda(a:%s)"
10296 (concat (substring t1 0 10) "--" (substring t2 0 10)))))
10297 (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
10298 ((org-at-timestamp-p t)
10299 (let ((org-agenda-buffer-tmp-name
10300 (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
10301 (org-agenda-list nil (time-to-days (org-time-string-to-time
10302 (substring (match-string 1) 0 10)))
10304 (t (error "This should not happen"))))
10307 ;;; Following file links
10308 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
10309 (declare-function mailcap-extension-to-mime "mailcap" (extn))
10310 (declare-function mailcap-mime-info
10311 "mailcap" (string &optional request no-decode))
10312 (defvar org-wait nil)
10313 (defun org-open-file (path &optional in-emacs line search)
10314 "Open the file at PATH.
10315 First, this expands any special file name abbreviations. Then the
10316 configuration variable `org-file-apps' is checked if it contains an
10317 entry for this file type, and if yes, the corresponding command is launched.
10319 If no application is found, Emacs simply visits the file.
10321 With optional prefix argument IN-EMACS, Emacs will visit the file.
10322 With a double \\[universal-argument] \\[universal-argument] \
10323 prefix arg, Org tries to avoid opening in Emacs
10324 and to use an external application to visit the file.
10326 Optional LINE specifies a line to go to, optional SEARCH a string
10327 to search for. If LINE or SEARCH is given, the file will be
10328 opened in Emacs, unless an entry from org-file-apps that makes
10329 use of groups in a regexp matches.
10331 If you want to change the way frames are used when following a
10332 link, please customize `org-link-frame-setup'.
10334 If the file does not exist, an error is thrown."
10335 (let* ((file (if (equal path "")
10337 (substitute-in-file-name (expand-file-name path))))
10338 (file-apps (append org-file-apps (org-default-apps)))
10339 (apps (org-remove-if
10340 'org-file-apps-entry-match-against-dlink-p file-apps))
10341 (apps-dlink (org-remove-if-not
10342 'org-file-apps-entry-match-against-dlink-p file-apps))
10343 (remp (and (assq 'remote apps) (org-file-remote-p file)))
10344 (dirp (if remp nil (file-directory-p file)))
10345 (file (if (and dirp org-open-directory-means-index-dot-org)
10346 (concat (file-name-as-directory file) "index.org")
10348 (a-m-a-p (assq 'auto-mode apps))
10349 (dfile (downcase file))
10350 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
10351 (link (cond ((and (eq line nil)
10355 (concat file "::" (number-to-string line)))
10357 (concat file "::" search))))
10358 (dlink (downcase link))
10359 (old-buffer (current-buffer))
10361 (old-mode major-mode)
10362 ext cmd link-match-data)
10363 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
10364 (setq ext (match-string 1 dfile))
10365 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
10366 (setq ext (match-string 1 dfile))))
10368 ((member in-emacs '((16) system))
10369 (setq cmd (cdr (assoc 'system apps))))
10370 (in-emacs (setq cmd 'emacs))
10372 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
10373 (and dirp (cdr (assoc 'directory apps)))
10374 ; first, try matching against apps-dlink
10375 ; if we get a match here, store the match data for later
10376 (let ((match (assoc-default dlink apps-dlink
10379 (progn (setq link-match-data (match-data))
10381 (progn (setq in-emacs (or in-emacs line search))
10382 nil))) ; if we have no match in apps-dlink,
10383 ; always open the file in emacs if line or search
10384 ; is given (for backwards compatibility)
10385 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
10387 (cdr (assoc ext apps))
10388 (cdr (assoc t apps))))))
10389 (when (eq cmd 'system)
10390 (setq cmd (cdr (assoc 'system apps))))
10391 (when (eq cmd 'default)
10392 (setq cmd (cdr (assoc t apps))))
10393 (when (eq cmd 'mailcap)
10395 (mailcap-parse-mailcaps)
10396 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
10397 (command (mailcap-mime-info mime-type)))
10398 (if (stringp command)
10400 (setq cmd 'emacs))))
10401 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
10402 (not (file-exists-p file))
10403 (not org-open-non-existing-files))
10404 (error "No such file: %s" file))
10406 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
10407 ;; Remove quotes around the file name - we'll use shell-quote-argument.
10408 (while (string-match "['\"]%s['\"]" cmd)
10409 (setq cmd (replace-match "%s" t t cmd)))
10410 (while (string-match "%s" cmd)
10411 (setq cmd (replace-match
10413 (shell-quote-argument
10414 (convert-standard-filename file)))
10417 ;; Replace "%1", "%2" etc. in command with group matches from regex
10419 (let ((match-index 1)
10420 (number-of-groups (- (/ (length link-match-data) 2) 1)))
10421 (set-match-data link-match-data)
10422 (while (<= match-index number-of-groups)
10423 (let ((regex (concat "%" (number-to-string match-index)))
10424 (replace-with (match-string match-index dlink)))
10425 (while (string-match regex cmd)
10426 (setq cmd (replace-match replace-with t t cmd))))
10427 (setq match-index (+ match-index 1)))))
10429 (save-window-excursion
10430 (start-process-shell-command cmd nil cmd)
10431 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
10435 (funcall (cdr (assq 'file org-link-frame-setup)) file)
10437 (if line (org-goto-line line)
10438 (if search (org-link-search search))))
10440 (let ((file (convert-standard-filename file)))
10442 (set-match-data link-match-data)
10444 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
10445 (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
10446 (or (not (equal old-buffer (current-buffer)))
10447 (not (equal old-pos (point))))
10448 (org-mark-ring-push old-pos old-buffer))))
10450 (defun org-file-apps-entry-match-against-dlink-p (entry)
10451 "This function returns non-nil if `entry' uses a regular
10452 expression which should be matched against the whole link by
10455 It assumes that is the case when the entry uses a regular
10456 expression which has at least one grouping construct and the
10457 action is either a lisp form or a command string containing
10458 '%1', i.e. using at least one subexpression match as a
10460 (let ((selector (car entry))
10461 (action (cdr entry)))
10462 (if (stringp selector)
10463 (and (> (regexp-opt-depth selector) 0)
10464 (or (and (stringp action)
10465 (string-match "%[0-9]" action))
10469 (defun org-default-apps ()
10470 "Return the default applications for this operating system."
10472 ((eq system-type 'darwin)
10473 org-file-apps-defaults-macosx)
10474 ((eq system-type 'windows-nt)
10475 org-file-apps-defaults-windowsnt)
10476 (t org-file-apps-defaults-gnu)))
10478 (defun org-apps-regexp-alist (list &optional add-auto-mode)
10479 "Convert extensions to regular expressions in the cars of LIST.
10480 Also, weed out any non-string entries, because the return value is used
10481 only for regexp matching.
10482 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
10483 point to the symbol `emacs', indicating that the file should
10484 be opened in Emacs."
10487 (mapcar (lambda (x)
10488 (if (not (stringp (car x)))
10490 (if (string-match "\\W" (car x))
10492 (cons (concat "\\." (car x) "\\'") (cdr x)))))
10495 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
10497 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
10498 (defun org-file-remote-p (file)
10499 "Test whether FILE specifies a location on a remote system.
10500 Return non-nil if the location is indeed remote.
10502 For example, the filename \"/user@host:/foo\" specifies a location
10503 on the system \"/user@host:\"."
10504 (cond ((fboundp 'file-remote-p)
10505 (file-remote-p file))
10506 ((fboundp 'tramp-handle-file-remote-p)
10507 (tramp-handle-file-remote-p file))
10508 ((and (boundp 'ange-ftp-name-format)
10509 (string-match (car ange-ftp-name-format) file))
10515 (defun org-get-org-file ()
10516 "Read a filename, with default directory `org-directory'."
10517 (let ((default (or org-default-notes-file remember-data-file)))
10518 (read-file-name (format "File name [%s]: " default)
10519 (file-name-as-directory org-directory)
10522 (defun org-notes-order-reversed-p ()
10523 "Check if the current file should receive notes in reversed order."
10525 ((not org-reverse-note-order) nil)
10526 ((eq t org-reverse-note-order) t)
10527 ((not (listp org-reverse-note-order)) nil)
10529 (let ((all org-reverse-note-order)
10531 (while (setq entry (pop all))
10532 (if (string-match (car entry) buffer-file-name)
10533 (throw 'exit (cdr entry))))
10536 (defvar org-refile-target-table nil
10537 "The list of refile targets, created by `org-refile'.")
10539 (defvar org-agenda-new-buffers nil
10540 "Buffers created to visit agenda files.")
10542 (defvar org-refile-cache nil
10543 "Cache for refile targets.")
10545 (defvar org-refile-markers nil
10546 "All the markers used for caching refile locations.")
10548 (defun org-refile-marker (pos)
10549 "Get a new refile marker, but only if caching is in use."
10550 (if (not org-refile-use-cache)
10552 (let ((m (make-marker)))
10553 (move-marker m pos)
10554 (push m org-refile-markers)
10557 (defun org-refile-cache-clear ()
10558 "Clear the refile cache and disable all the markers."
10559 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
10560 (setq org-refile-markers nil)
10561 (setq org-refile-cache nil)
10562 (message "Refile cache has been cleared"))
10564 (defun org-refile-cache-check-set (set)
10565 "Check if all the markers in the cache still have live buffers."
10568 (while (and set (setq marker (nth 3 (pop set))))
10569 ;; if org-refile-use-outline-path is 'file, marker may be nil
10570 (when (and marker (null (marker-buffer marker)))
10571 (message "not found") (sit-for 3)
10572 (throw 'exit nil)))
10575 (defun org-refile-cache-put (set &rest identifiers)
10576 "Push the refile targets SET into the cache, under IDENTIFIERS."
10577 (let* ((key (sha1 (prin1-to-string identifiers)))
10578 (entry (assoc key org-refile-cache)))
10581 (push (cons key set) org-refile-cache))))
10583 (defun org-refile-cache-get (&rest identifiers)
10584 "Retrieve the cached value for refile targets given by IDENTIFIERS."
10586 ((not org-refile-cache) nil)
10587 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
10589 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
10590 org-refile-cache))))
10591 (and set (org-refile-cache-check-set set) set)))))
10593 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
10594 "Produce a table with refile targets."
10595 (let ((case-fold-search nil)
10596 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
10597 (entries (or org-refile-targets '((nil . (:level . 1)))))
10598 targets tgs txt re files f desc descre fast-path-p level pos0)
10599 (message "Getting targets...")
10600 (with-current-buffer (or default-buffer (current-buffer))
10601 (while (setq entry (pop entries))
10602 (setq files (car entry) desc (cdr entry))
10603 (setq fast-path-p nil)
10605 ((null files) (setq files (list (current-buffer))))
10606 ((eq files 'org-agenda-files)
10607 (setq files (org-agenda-files 'unrestricted)))
10608 ((and (symbolp files) (fboundp files))
10609 (setq files (funcall files)))
10610 ((and (symbolp files) (boundp files))
10611 (setq files (symbol-value files))))
10612 (if (stringp files) (setq files (list files)))
10614 ((eq (car desc) :tag)
10615 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
10616 ((eq (car desc) :todo)
10617 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
10618 ((eq (car desc) :regexp)
10619 (setq descre (cdr desc)))
10620 ((eq (car desc) :level)
10621 (setq descre (concat "^\\*\\{" (number-to-string
10622 (if org-odd-levels-only
10623 (1- (* 2 (cdr desc)))
10626 ((eq (car desc) :maxlevel)
10627 (setq fast-path-p t)
10628 (setq descre (concat "^\\*\\{1," (number-to-string
10629 (if org-odd-levels-only
10630 (1- (* 2 (cdr desc)))
10633 (t (error "Bad refiling target description %s" desc)))
10634 (while (setq f (pop files))
10635 (with-current-buffer
10636 (if (bufferp f) f (org-get-agenda-file-buffer f))
10638 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
10640 (if (bufferp f) (setq f (buffer-file-name
10641 (buffer-base-buffer f))))
10642 (setq f (and f (expand-file-name f)))
10643 (if (eq org-refile-use-outline-path 'file)
10644 (push (list (file-name-nondirectory f) f nil nil) tgs))
10648 (goto-char (point-min))
10649 (while (re-search-forward descre nil t)
10650 (goto-char (setq pos0 (point-at-bol)))
10652 (when org-refile-target-verify-function
10654 (or (funcall org-refile-target-verify-function)
10656 (when (and (looking-at org-complex-heading-regexp)
10657 (not (member (match-string 4) excluded-entries))
10659 (setq level (org-reduced-level
10660 (- (match-end 1) (match-beginning 1)))
10661 txt (org-link-display-format (match-string 4))
10662 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
10663 re (format org-complex-heading-regexp-format
10664 (regexp-quote (match-string 4))))
10665 (when org-refile-use-outline-path
10666 (setq txt (mapconcat
10669 (if (eq org-refile-use-outline-path
10671 (list (file-name-nondirectory
10673 (buffer-base-buffer))))
10674 (if (eq org-refile-use-outline-path
10676 (list (buffer-file-name
10677 (buffer-base-buffer)))))
10678 (org-get-outline-path fast-path-p
10682 (push (list txt f re (org-refile-marker (point)))
10684 (when (= (point) pos0)
10685 ;; verification function has not moved point
10686 (goto-char (point-at-eol))))))))
10687 (when org-refile-use-cache
10688 (org-refile-cache-put tgs (buffer-file-name) descre))
10689 (setq targets (append tgs targets))
10691 (message "Getting targets...done")
10692 (nreverse targets)))
10694 (defun org-protect-slash (s)
10695 (while (string-match "/" s)
10696 (setq s (replace-match "\\" t t s)))
10699 (defvar org-olpa (make-vector 20 nil))
10701 (defun org-get-outline-path (&optional fastp level heading)
10702 "Return the outline path to the current entry, as a list.
10704 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
10705 routine which makes outline path derivations for an entire file,
10706 avoiding backtracing. Refile target collection makes use of that."
10710 (error "Outline path failure, more than 19 levels"))
10711 (loop for i from level upto 19 do
10712 (aset org-olpa i nil))
10714 (delq nil (append org-olpa nil))
10715 (aset org-olpa level heading)))
10716 (let (rtn case-fold-search)
10720 (while (org-up-heading-safe)
10721 (when (looking-at org-complex-heading-regexp)
10722 (push (org-match-string-no-properties 4) rtn)))
10725 (defun org-format-outline-path (path &optional width prefix)
10726 "Format the outline path PATH for display.
10727 Width is the maximum number of characters that is available.
10728 Prefix is a prefix to be included in the returned string,
10729 such as the file name."
10730 (setq width (or width 79))
10731 (if prefix (setq width (- width (length prefix))))
10734 (let* ((nsteps (length path))
10735 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
10736 (maxwidth (if (<= total-width width)
10737 10000 ;; everything fits
10738 ;; we need to shorten the level headings
10739 (/ (- width nsteps) nsteps)))
10740 (org-odd-levels-only nil)
10742 (total (1+ (length prefix))))
10743 (setq maxwidth (max maxwidth 10))
10748 (if (and (= n nsteps) (< maxwidth 10000))
10749 (setq maxwidth (- total-width total)))
10750 (if (< (length h) maxwidth)
10751 (progn (setq total (+ total (length h) 1)) h)
10752 (setq h (substring h 0 (- maxwidth 2))
10753 total (+ total maxwidth 1))
10754 (if (string-match "[ \t]+\\'" h)
10755 (setq h (substring h 0 (match-beginning 0))))
10756 (setq h (concat h "..")))
10757 (org-add-props h nil 'face
10758 (nth (% (1- n) org-n-level-faces)
10763 (defun org-display-outline-path (&optional file current)
10764 "Display the current outline path in the echo area."
10766 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
10767 (case-fold-search nil)
10768 (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
10769 (if current (setq path (append path
10771 (org-back-to-heading t)
10772 (if (looking-at org-complex-heading-regexp)
10773 (list (match-string 4)))))))
10775 (org-format-outline-path
10778 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
10780 (defvar org-refile-history nil
10781 "History for refiling operations.")
10783 (defvar org-after-refile-insert-hook nil
10784 "Hook run after `org-refile' has inserted its stuff at the new location.
10785 Note that this is still *before* the stuff will be removed from
10786 the *old* location.")
10788 (defvar org-capture-last-stored-marker)
10789 (defun org-refile (&optional goto default-buffer rfloc)
10790 "Move the entry or entries at point to another heading.
10791 The list of target headings is compiled using the information in
10792 `org-refile-targets', which see.
10794 At the target location, the entry is filed as a subitem of the target
10795 heading. Depending on `org-reverse-note-order', the new subitem will
10796 either be the first or the last subitem.
10798 If there is an active region, all entries in that region will be moved.
10799 However, the region must fulfill the requirement that the first heading
10800 is the first one sets the top-level of the moved text - at most siblings
10801 below it are allowed.
10803 With prefix arg GOTO, the command will only visit the target location
10804 and not actually move anything.
10806 With a double prefix arg \\[universal-argument] \\[universal-argument], \
10807 go to the location where the last refiling operation has put the subtree.
10808 With a prefix argument of `2', refile to the running clock.
10810 RFLOC can be a refile location obtained in a different way.
10812 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
10814 If you are using target caching (see `org-refile-use-cache'),
10815 you have to clear the target cache in order to find new targets.
10816 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
10817 prefix argument (`C-u C-u C-u C-c C-w')."
10820 (if (member goto '(0 (64)))
10821 (org-refile-cache-clear)
10822 (let* ((cbuf (current-buffer))
10823 (regionp (org-region-active-p))
10824 (region-start (and regionp (region-beginning)))
10825 (region-end (and regionp (region-end)))
10826 (region-length (and regionp (- region-end region-start)))
10827 (filename (buffer-file-name (buffer-base-buffer cbuf)))
10828 pos it nbuf file re level reversed)
10829 (setq last-command nil)
10831 (goto-char region-start)
10832 (or (bolp) (goto-char (point-at-bol)))
10833 (setq region-start (point))
10834 (unless (or (org-kill-is-subtree-p
10835 (buffer-substring region-start region-end))
10836 (prog1 org-refile-active-region-within-subtree
10837 (org-toggle-heading)))
10838 (error "The region is not a (sequence of) subtree(s)")))
10839 (if (equal goto '(16))
10840 (org-refile-goto-last-stored)
10842 (and (equal goto 2)
10843 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
10845 (setq it (list (or org-clock-heading "running clock")
10847 (marker-buffer org-clock-hd-marker))
10849 (marker-position org-clock-hd-marker)))
10852 (let (heading-text)
10855 (org-back-to-heading t)
10857 (nth 4 (org-heading-components))))
10858 (org-refile-get-location
10859 (cond (goto "Goto")
10860 (regionp "Refile region to")
10861 (t (concat "Refile subtree \""
10862 heading-text "\" to")))
10864 (and (not (equal '(4) goto))
10865 org-refile-allow-creating-parent-nodes)
10867 (setq file (nth 1 it)
10870 (if (and (not goto)
10872 (equal (buffer-file-name) file)
10874 (and (>= pos region-start)
10875 (<= pos region-end))
10876 (and (>= pos (point))
10877 (< pos (save-excursion
10878 (org-end-of-subtree t t))))))
10879 (error "Cannot refile to position inside the tree or region"))
10881 (setq nbuf (or (find-buffer-visiting file)
10882 (find-file-noselect file)))
10885 (org-pop-to-buffer-same-window nbuf)
10887 (org-show-context 'org-goto))
10890 (org-kill-new (buffer-substring region-start region-end))
10891 (org-save-markers-in-region region-start region-end))
10892 (org-copy-subtree 1 nil t))
10893 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
10894 (find-file-noselect file)))
10895 (setq reversed (org-notes-order-reversed-p))
10902 (looking-at org-outline-regexp)
10903 (setq level (org-get-valid-level (funcall outline-level) 1))
10906 (or (outline-next-heading) (point-max))
10907 (or (save-excursion (org-get-next-sibling))
10908 (org-end-of-subtree t t)
10912 (goto-char (point-max))
10913 (goto-char (point-min))
10914 (or (outline-next-heading) (goto-char (point-max)))))
10915 (if (not (bolp)) (newline))
10916 (org-paste-subtree level)
10917 (when org-log-refile
10918 (org-add-log-setup 'refile nil nil 'findpos
10920 (unless (eq org-log-refile 'note)
10921 (save-excursion (org-add-log-note))))
10922 (and org-auto-align-tags
10923 (let ((org-loop-over-headlines-in-active-region nil))
10924 (org-set-tags nil t)))
10925 (with-demoted-errors
10926 (bookmark-set "org-refile-last-stored"))
10927 ;; If we are refiling for capture, make sure that the
10928 ;; last-capture pointers point here
10929 (when (org-bound-and-true-p org-refile-for-capture)
10930 (with-demoted-errors
10931 (bookmark-set "org-capture-last-stored-marker"))
10932 (move-marker org-capture-last-stored-marker (point)))
10933 (if (fboundp 'deactivate-mark) (deactivate-mark))
10934 (run-hooks 'org-after-refile-insert-hook))))
10936 (delete-region (point) (+ (point) region-length))
10938 (when (featurep 'org-inlinetask)
10939 (org-inlinetask-remove-END-maybe))
10940 (setq org-markers-to-move nil)
10941 (message "Refiled to \"%s\" in file %s" (car it) file)))))))
10943 (defun org-refile-goto-last-stored ()
10944 "Go to the location where the last refile was stored."
10946 (bookmark-jump "org-refile-last-stored")
10947 (message "This is the location of the last refile"))
10949 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
10951 "Prompt the user for a refile location, using PROMPT.
10952 PROMPT should not be suffixed with a colon and a space, because
10953 this function appends the default value from
10954 `org-refile-history' automatically, if that is not empty.
10955 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
10956 this is used for the GOTO interface."
10957 (let ((org-refile-targets org-refile-targets)
10958 (org-refile-use-outline-path org-refile-use-outline-path)
10960 (when (and (derived-mode-p 'org-mode)
10961 (not org-refile-use-cache)
10965 (setq excluded-entries
10966 (append excluded-entries (list (org-get-heading t t)))))))
10967 (setq org-refile-target-table
10968 (org-refile-get-targets default-buffer excluded-entries)))
10969 (unless org-refile-target-table
10970 (error "No refile targets"))
10971 (let* ((prompt (concat prompt
10972 (and (car org-refile-history)
10973 (concat " (default " (car org-refile-history) ")"))
10975 (cbuf (current-buffer))
10976 (partial-completion-mode nil)
10977 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
10978 (cfunc (if (and org-refile-use-outline-path
10979 org-outline-path-complete-in-steps)
10980 'org-olpath-completing-read
10981 'org-icompleting-read))
10982 (extra (if org-refile-use-outline-path "/" ""))
10983 (filename (and cfn (expand-file-name cfn)))
10986 (if (and (not (member org-refile-use-outline-path
10987 '(file full-file-path)))
10988 (not (equal filename (nth 1 x))))
10989 (cons (concat (car x) extra " ("
10990 (file-name-nondirectory (nth 1 x)) ")")
10992 (cons (concat (car x) extra) (cdr x))))
10993 org-refile-target-table))
10994 (completion-ignore-case t)
10995 pa answ parent-target child parent old-hist)
10996 (setq old-hist org-refile-history)
10997 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
10998 nil 'org-refile-history (car org-refile-history)))
10999 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
11000 (org-refile-check-position pa)
11003 (when (or (not org-refile-history)
11004 (not (eq old-hist org-refile-history))
11005 (not (equal (car pa) (car org-refile-history))))
11006 (setq org-refile-history
11007 (cons (car pa) (if (assoc (car org-refile-history) tbl)
11009 (cdr org-refile-history))))
11010 (if (equal (car org-refile-history) (nth 1 org-refile-history))
11011 (pop org-refile-history)))
11013 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
11015 (setq parent (match-string 1 answ)
11016 child (match-string 2 answ))
11017 (setq parent-target (or (assoc parent tbl)
11018 (assoc (concat parent "/") tbl)))
11019 (when (and parent-target
11020 (or (eq new-nodes t)
11021 (and (eq new-nodes 'confirm)
11022 (y-or-n-p (format "Create new node \"%s\"? "
11024 (org-refile-new-child parent-target child)))
11025 (error "Invalid target location")))))
11027 (declare-function org-string-nw-p "org-macs" (s))
11028 (defun org-refile-check-position (refile-pointer)
11029 "Check if the refile pointer matches the headline to which it points."
11030 (let* ((file (nth 1 refile-pointer))
11031 (re (nth 2 refile-pointer))
11032 (pos (nth 3 refile-pointer))
11034 (if (and (not (markerp pos)) (not file))
11035 (error "Please save the buffer to a file before refiling")
11036 (when (org-string-nw-p re)
11037 (setq buffer (if (markerp pos)
11038 (marker-buffer pos)
11039 (or (find-buffer-visiting file)
11040 (find-file-noselect file))))
11041 (with-current-buffer buffer
11046 (beginning-of-line 1)
11047 (unless (org-looking-at-p re)
11048 (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
11050 (defun org-refile-new-child (parent-target child)
11051 "Use refile target PARENT-TARGET to add new CHILD below it."
11052 (unless parent-target
11053 (error "Cannot find parent for new node"))
11054 (let ((file (nth 1 parent-target))
11055 (pos (nth 3 parent-target))
11057 (with-current-buffer (or (find-buffer-visiting file)
11058 (find-file-noselect file))
11064 (goto-char (point-max))
11065 (if (not (bolp)) (newline)))
11066 (when (looking-at org-outline-regexp)
11067 (setq level (funcall outline-level))
11068 (org-end-of-subtree t t))
11069 (org-back-over-empty-lines)
11070 (insert "\n" (make-string
11071 (if pos (org-get-valid-level level 1) 1) ?*)
11073 (beginning-of-line 0)
11074 (list (concat (car parent-target) "/" child) file "" (point)))))))
11076 (defun org-olpath-completing-read (prompt collection &rest args)
11077 "Read an outline path like a file name."
11078 (let ((thetable collection)
11079 (org-completion-use-ido nil) ; does not work with ido.
11080 (org-completion-use-iswitchb nil)) ; or iswitchb
11082 'org-icompleting-read prompt
11083 (lambda (string predicate &optional flag)
11084 (let (rtn r f (l (length string)))
11088 (try-completion string thetable))
11091 (setq rtn (all-completions string thetable predicate))
11094 (setq r (substring x l))
11095 (if (string-match " ([^)]*)$" x)
11096 (setq f (match-string 0 x))
11098 (if (string-match "/" r)
11099 (concat string (substring r 0 (match-end 0)) f)
11104 (assoc string thetable)))))
11107 ;;;; Dynamic blocks
11109 (defun org-find-dblock (name)
11110 "Find the first dynamic block with name NAME in the buffer.
11111 If not found, stay at current position and return nil."
11112 (let ((case-fold-search t) pos)
11114 (goto-char (point-min))
11115 (setq pos (and (re-search-forward
11116 (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
11117 (match-beginning 0))))
11118 (if pos (goto-char pos))
11121 (defconst org-dblock-start-re
11122 "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
11123 "Matches the start line of a dynamic block, with parameters.")
11125 (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
11126 "Matches the end of a dynamic block.")
11128 (defun org-create-dblock (plist)
11129 "Create a dynamic block section, with parameters taken from PLIST.
11130 PLIST must contain a :name entry which is used as name of the block."
11131 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
11134 (let ((col (current-column))
11135 (name (plist-get plist :name)))
11136 (insert "#+BEGIN: " name)
11138 (if (eq (car plist) :name)
11139 (setq plist (cddr plist))
11140 (insert " " (prin1-to-string (pop plist)))))
11141 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
11142 (beginning-of-line -2)))
11144 (defun org-prepare-dblock ()
11145 "Prepare dynamic block for refresh.
11146 This empties the block, puts the cursor at the insert position and returns
11147 the property list including an extra property :name with the block name."
11148 (unless (looking-at org-dblock-start-re)
11149 (error "Not at a dynamic block"))
11150 (let* ((begdel (1+ (match-end 0)))
11151 (name (org-no-properties (match-string 1)))
11152 (params (append (list :name name)
11153 (read (concat "(" (match-string 3) ")")))))
11155 (beginning-of-line 1)
11156 (skip-chars-forward " \t")
11157 (setq params (plist-put params :indentation-column (current-column))))
11158 (unless (re-search-forward org-dblock-end-re nil t)
11159 (error "Dynamic block not terminated"))
11162 (list :content (buffer-substring
11163 begdel (match-beginning 0)))))
11164 (delete-region begdel (match-beginning 0))
11169 (defun org-map-dblocks (&optional command)
11170 "Apply COMMAND to all dynamic blocks in the current buffer.
11171 If COMMAND is not given, use `org-update-dblock'."
11172 (let ((cmd (or command 'org-update-dblock)))
11174 (goto-char (point-min))
11175 (while (re-search-forward org-dblock-start-re nil t)
11176 (goto-char (match-beginning 0))
11178 (condition-case nil
11180 (error (message "Error during update of dynamic block"))))
11181 (unless (re-search-forward org-dblock-end-re nil t)
11182 (error "Dynamic block not terminated"))))))
11184 (defun org-dblock-update (&optional arg)
11185 "User command for updating dynamic blocks.
11186 Update the dynamic block at point. With prefix ARG, update all dynamic
11187 blocks in the buffer."
11190 (org-update-all-dblocks)
11191 (or (looking-at org-dblock-start-re)
11192 (org-beginning-of-dblock))
11193 (org-update-dblock)))
11195 (defun org-update-dblock ()
11196 "Update the dynamic block at point.
11197 This means to empty the block, parse for parameters and then call
11198 the correct writing function."
11200 (save-window-excursion
11201 (let* ((pos (point))
11202 (line (org-current-line))
11203 (params (org-prepare-dblock))
11204 (name (plist-get params :name))
11205 (indent (plist-get params :indentation-column))
11206 (cmd (intern (concat "org-dblock-write:" name))))
11207 (message "Updating dynamic block `%s' at line %d..." name line)
11208 (funcall cmd params)
11209 (message "Updating dynamic block `%s' at line %d...done" name line)
11211 (when (and indent (> indent 0))
11212 (setq indent (make-string indent ?\ ))
11214 (org-beginning-of-dblock)
11216 (while (not (looking-at org-dblock-end-re))
11218 (beginning-of-line 2))
11219 (when (looking-at org-dblock-end-re)
11220 (and (looking-at "[ \t]+")
11221 (replace-match ""))
11222 (insert indent)))))))
11224 (defun org-beginning-of-dblock ()
11225 "Find the beginning of the dynamic block at point.
11226 Error if there is no such block at point."
11227 (let ((pos (point))
11230 (if (and (re-search-backward org-dblock-start-re nil t)
11231 (setq beg (match-beginning 0))
11232 (re-search-forward org-dblock-end-re nil t)
11233 (> (match-end 0) pos))
11236 (error "Not in a dynamic block"))))
11238 (defun org-update-all-dblocks ()
11239 "Update all dynamic blocks in the buffer.
11240 This function can be used in a hook."
11242 (when (derived-mode-p 'org-mode)
11243 (org-map-dblocks 'org-update-dblock)))
11248 (defconst org-additional-option-like-keywords
11249 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
11250 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
11251 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
11252 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
11254 "ORGTBL" "TBLFM:" "TBLNAME:"
11255 "BEGIN_EXAMPLE" "END_EXAMPLE"
11256 "BEGIN_VERBATIM" "END_VERBATIM"
11257 "BEGIN_QUOTE" "END_QUOTE"
11258 "BEGIN_VERSE" "END_VERSE"
11259 "BEGIN_CENTER" "END_CENTER"
11260 "BEGIN_SRC" "END_SRC"
11261 "BEGIN_RESULT" "END_RESULT"
11262 "BEGIN_lstlisting" "END_lstlisting"
11264 "HEADER:" "HEADERS:"
11265 "COLUMNS:" "PROPERTY:"
11266 "CAPTION:" "LABEL:"
11268 "INCLUDE:" "INDEX:"
11272 (defconst org-options-keywords
11273 '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
11274 "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
11275 "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
11276 "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
11277 "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
11278 "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
11279 "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
11281 (defconst org-additional-option-like-keywords-for-flyspell
11284 (mapconcat (lambda(k)
11285 (replace-regexp-in-string
11287 (concat k " " (downcase k) " " (upcase k))))
11288 (append org-options-keywords org-additional-option-like-keywords)
11292 (defcustom org-structure-template-alist
11293 '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
11294 "<src lang=\"?\">\n\n</src>")
11295 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
11296 "<example>\n?\n</example>")
11297 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
11298 "<quote>\n?\n</quote>")
11299 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
11300 "<verse>\n?\n</verse>")
11301 ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
11302 "<verbatim>\n?\n</verbatim>")
11303 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
11304 "<center>\n?\n</center>")
11305 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
11306 "<literal style=\"latex\">\n?\n</literal>")
11308 "<literal style=\"latex\">?</literal>")
11309 ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
11310 "<literal style=\"html\">\n?\n</literal>")
11312 "<literal style=\"html\">?</literal>")
11313 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
11317 ("I" "#+INCLUDE: %file ?"
11318 "<include file=%file markup=\"?\">"))
11319 "Structure completion elements.
11320 This is a list of abbreviation keys and values. The value gets inserted
11321 if you type `<' followed by the key and then press the completion key,
11322 usually `M-TAB'. %file will be replaced by a file name after prompting
11323 for the file using completion. The cursor will be placed at the position
11324 of the `?` in the template.
11325 There are two templates for each key, the first uses the original Org syntax,
11326 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
11327 the default when the /org-mtags.el/ module has been loaded. See also the
11328 variable `org-mtags-prefer-muse-templates'."
11329 :group 'org-completion
11331 (string :tag "Key")
11332 (string :tag "Template")
11333 (string :tag "Muse Template")))
11335 (defun org-try-structure-completion ()
11336 "Try to complete a structure template before point.
11337 This looks for strings like \"<e\" on an otherwise empty line and
11339 (let ((l (buffer-substring (point-at-bol) (point)))
11341 (when (and (looking-at "[ \t]*$")
11342 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
11343 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
11344 (org-complete-expand-structure-template (+ -1 (point-at-bol)
11345 (match-beginning 1)) a)
11348 (defun org-complete-expand-structure-template (start cell)
11349 "Expand a structure template."
11350 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
11351 (rpl (nth (if musep 2 1) cell))
11353 (delete-region start (point))
11354 (when (string-match "\\`#\\+" rpl)
11357 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
11358 (setq ind (buffer-substring (point-at-bol) (point))))
11360 (setq start (point))
11361 (if (string-match "%file" rpl)
11362 (setq rpl (replace-match
11366 (abbreviate-file-name (read-file-name "Include file: ")))
11369 (setq rpl (mapconcat 'identity (split-string rpl "\n")
11370 (concat "\n" ind)))
11372 (if (re-search-backward "\\?" start t) (delete-char 1))))
11374 ;;;; TODO, DEADLINE, Comments
11376 (defun org-toggle-comment ()
11377 "Change the COMMENT state of an entry."
11380 (org-back-to-heading)
11381 (let (case-fold-search)
11383 ((looking-at (format org-heading-keyword-regexp-format
11384 org-comment-string))
11385 (goto-char (match-end 1))
11386 (looking-at (concat " +" org-comment-string))
11387 (replace-match "" t t)
11388 (when (eolp) (insert " ")))
11389 ((looking-at org-outline-regexp)
11390 (goto-char (match-end 0))
11391 (insert org-comment-string " "))))))
11393 (defvar org-last-todo-state-is-todo nil
11394 "This is non-nil when the last TODO state change led to a TODO state.
11395 If the last change removed the TODO tag or switched to DONE, then
11398 (defvar org-setting-tags nil) ; dynamically skipped
11400 (defvar org-todo-setup-filter-hook nil
11401 "Hook for functions that pre-filter todo specs.
11402 Each function takes a todo spec and returns either nil or the spec
11403 transformed into canonical form." )
11405 (defvar org-todo-get-default-hook nil
11406 "Hook for functions that get a default item for todo.
11407 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
11408 nil or a string to be used for the todo mark." )
11410 (defvar org-agenda-headline-snapshot-before-repeat)
11412 (defun org-current-effective-time ()
11413 "Return current time adjusted for `org-extend-today-until' variable."
11414 (let* ((ct (org-current-time))
11415 (dct (decode-time ct))
11417 (if (and org-use-effective-time
11418 (< (nth 2 dct) org-extend-today-until))
11419 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
11423 (defun org-todo-yesterday (&optional arg)
11424 "Like `org-todo' but the time of change will be 23:59 of yesterday."
11426 (if (eq major-mode 'org-agenda-mode)
11427 (apply 'org-agenda-todo-yesterday arg)
11428 (let* ((hour (third (decode-time
11429 (org-current-time))))
11430 (org-extend-today-until (1+ hour)))
11433 (defun org-todo (&optional arg)
11434 "Change the TODO state of an item.
11435 The state of an item is given by a keyword at the start of the heading,
11437 *** TODO Write paper
11440 The different keywords are specified in the variable `org-todo-keywords'.
11441 By default the available states are \"TODO\" and \"DONE\".
11442 So for this example: when the item starts with TODO, it is changed to DONE.
11443 When it starts with DONE, the DONE is removed. And when neither TODO nor
11444 DONE are present, add TODO at the beginning of the heading.
11446 With \\[universal-argument] prefix arg, use completion to determine the new \
11448 With numeric prefix arg, switch to that state.
11449 With a double \\[universal-argument] prefix, switch to the next set of TODO \
11450 keywords (nextset).
11451 With a triple \\[universal-argument] prefix, circumvent any state blocking.
11452 With a numeric prefix arg of 0, inhibit note taking for the change.
11454 For calling through lisp, arg is also interpreted in the following way:
11455 'none -> empty state
11456 \"\"(empty string) -> switch to empty state
11457 'done -> switch to DONE
11458 'nextset -> switch to the next set of keywords
11459 'previousset -> switch to the previous set of keywords
11460 \"WAITING\" -> switch to the specified keyword, but only if it
11461 really is a member of `org-todo-keywords'."
11463 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
11464 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
11465 'region-start-level 'region))
11466 org-loop-over-headlines-in-active-region)
11469 org-loop-over-headlines-in-active-region
11470 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
11471 (if (equal arg '(16)) (setq arg 'nextset))
11472 (let ((org-blocker-hook org-blocker-hook)
11475 (when (equal arg '(64))
11476 (setq arg nil org-blocker-hook nil))
11477 (when (and org-blocker-hook
11478 (or org-inhibit-blocking
11479 (org-entry-get nil "NOBLOCKING")))
11480 (setq org-blocker-hook nil))
11483 (org-back-to-heading t)
11484 (when (looking-at (concat "^\\*+ " org-comment-string))
11485 (org-toggle-comment)
11487 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
11488 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
11489 (looking-at "\\(?: *\\|[ \t]*$\\)"))
11490 (let* ((match-data (match-data))
11491 (startpos (point-at-bol))
11492 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
11493 (org-log-done org-log-done)
11494 (org-log-repeat org-log-repeat)
11495 (org-todo-log-states org-todo-log-states)
11496 (org-inhibit-logging
11498 (progn (setq arg nil) 'note) org-inhibit-logging))
11499 (this (match-string 1))
11500 (hl-pos (match-beginning 0))
11501 (head (org-get-todo-sequence-head this))
11502 (ass (assoc head org-todo-kwd-alist))
11503 (interpret (nth 1 ass))
11504 (done-word (nth 3 ass))
11505 (final-done-word (nth 4 ass))
11506 (org-last-state (or this ""))
11507 (completion-ignore-case t)
11508 (member (member this org-todo-keywords-1))
11509 (tail (cdr member))
11511 ((and org-todo-key-trigger
11512 (or (and (equal arg '(4))
11513 (eq org-use-fast-todo-selection 'prefix))
11514 (and (not arg) org-use-fast-todo-selection
11515 (not (eq org-use-fast-todo-selection
11517 ;; Use fast selection
11518 (org-fast-todo-selection))
11519 ((and (equal arg '(4))
11520 (or (not org-use-fast-todo-selection)
11521 (not org-todo-key-trigger)))
11522 ;; Read a state with completion
11523 (org-icompleting-read
11524 "State: " (mapcar (lambda(x) (list x))
11525 org-todo-keywords-1)
11529 (if tail (car tail) nil)
11530 (car org-todo-keywords-1)))
11532 (if (equal member org-todo-keywords-1)
11535 (nth (- (length org-todo-keywords-1)
11537 org-todo-keywords-1)
11538 (org-last org-todo-keywords-1))))
11539 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
11540 (setq arg nil))) ; hack to fall back to cycling
11542 ;; user or caller requests a specific state
11544 ((equal arg "") nil)
11545 ((eq arg 'none) nil)
11546 ((eq arg 'done) (or done-word (car org-done-keywords)))
11548 (or (car (cdr (member head org-todo-heads)))
11549 (car org-todo-heads)))
11550 ((eq arg 'previousset)
11551 (let ((org-todo-heads (reverse org-todo-heads)))
11552 (or (car (cdr (member head org-todo-heads)))
11553 (car org-todo-heads))))
11554 ((car (member arg org-todo-keywords-1)))
11556 (error "State `%s' not valid in this file" arg))
11557 ((nth (1- (prefix-numeric-value arg))
11558 org-todo-keywords-1))))
11559 ((null member) (or head (car org-todo-keywords-1)))
11560 ((equal this final-done-word) nil) ;; -> make empty
11561 ((null tail) nil) ;; -> first entry
11562 ((memq interpret '(type priority))
11563 (if (eq this-command last-command)
11565 (if (> (length tail) 0)
11566 (or done-word (car org-done-keywords))
11571 (run-hook-with-args-until-success
11572 'org-todo-get-default-hook org-state org-last-state)
11574 (next (if org-state (concat " " org-state " ") " "))
11575 (change-plist (list :type 'todo-state-change :from this :to org-state
11576 :position startpos))
11578 (when org-blocker-hook
11579 (setq org-last-todo-state-is-todo
11580 (not (member this org-done-keywords)))
11581 (unless (save-excursion
11583 (org-with-wide-buffer
11584 (run-hook-with-args-until-failure
11585 'org-blocker-hook change-plist))))
11586 (if (org-called-interactively-p 'interactive)
11587 (error "TODO state change from %s to %s blocked" this org-state)
11589 (message "TODO state change from %s to %s blocked" this org-state)
11590 (throw 'exit nil))))
11591 (store-match-data match-data)
11592 (replace-match next t t)
11593 (unless (pos-visible-in-window-p hl-pos)
11594 (message "TODO state changed to %s" (org-trim next)))
11596 (setq head (org-get-todo-sequence-head org-state)
11597 ass (assoc head org-todo-kwd-alist)
11598 interpret (nth 1 ass)
11599 done-word (nth 3 ass)
11600 final-done-word (nth 4 ass)))
11601 (when (memq arg '(nextset previousset))
11602 (message "Keyword-Set %d/%d: %s"
11603 (- (length org-todo-sets) -1
11604 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
11605 (length org-todo-sets)
11606 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
11607 (setq org-last-todo-state-is-todo
11608 (not (member org-state org-done-keywords)))
11609 (setq now-done-p (and (member org-state org-done-keywords)
11610 (not (member this org-done-keywords))))
11611 (and logging (org-local-logging logging))
11612 (when (and (or org-todo-log-states org-log-done)
11613 (not (eq org-inhibit-logging t))
11614 (not (memq arg '(nextset previousset))))
11615 ;; we need to look at recording a time and note
11616 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
11617 (nth 2 (assoc this org-todo-log-states))))
11618 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
11619 (setq dolog 'time))
11620 (when (and org-state
11621 (member org-state org-not-done-keywords)
11622 (not (member this org-not-done-keywords)))
11623 ;; This is now a todo state and was not one before
11624 ;; If there was a CLOSED time stamp, get rid of it.
11625 (org-add-planning-info nil nil 'closed))
11626 (when (and now-done-p org-log-done)
11627 ;; It is now done, and it was not done before
11628 (org-add-planning-info 'closed (org-current-effective-time))
11629 (if (and (not dolog) (eq 'note org-log-done))
11630 (org-add-log-setup 'done org-state this 'findpos 'note)))
11631 (when (and org-state dolog)
11632 ;; This is a non-nil state, and we need to log it
11633 (org-add-log-setup 'state org-state this 'findpos dolog)))
11634 ;; Fixup tag positioning
11635 (org-todo-trigger-tag-changes org-state)
11636 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
11637 (when org-provide-todo-statistics
11638 (org-update-parent-todo-statistics))
11639 (run-hooks 'org-after-todo-state-change-hook)
11640 (if (and arg (not (member org-state org-done-keywords)))
11641 (setq head (org-get-todo-sequence-head org-state)))
11642 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
11643 ;; Do we need to trigger a repeat?
11645 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
11646 ;; This is for the agenda, take a snapshot of the headline.
11648 (setq org-agenda-headline-snapshot-before-repeat
11649 (org-get-heading))))
11650 (org-auto-repeat-maybe org-state))
11651 ;; Fixup cursor location if close to the keyword
11652 (if (and (outline-on-heading-p)
11654 (save-excursion (beginning-of-line 1)
11655 (looking-at org-todo-line-regexp))
11656 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
11658 (goto-char (or (match-end 2) (match-end 1)))
11659 (and (looking-at " ") (just-one-space))))
11660 (when org-trigger-hook
11662 (run-hook-with-args 'org-trigger-hook change-plist)))
11663 (when commentp (org-toggle-comment))))))))
11665 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
11666 "Block turning an entry into a TODO, using the hierarchy.
11667 This checks whether the current task should be blocked from state
11668 changes. Such blocking occurs when:
11670 1. The task has children which are not all in a completed state.
11672 2. A task has a parent with the property :ORDERED:, and there
11673 are siblings prior to the current task with incomplete
11676 3. The parent of the task is blocked because it has siblings that should
11677 be done first, or is child of a block grandparent TODO entry."
11679 (if (not org-enforce-todo-dependencies)
11680 t ; if locally turned off don't block
11682 ;; If this is not a todo state change, or if this entry is already DONE,
11684 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11685 (member (plist-get change-plist :from)
11686 (cons 'done org-done-keywords))
11687 (member (plist-get change-plist :to)
11688 (cons 'todo org-not-done-keywords))
11689 (not (plist-get change-plist :to)))
11690 (throw 'dont-block t))
11691 ;; If this task has children, and any are undone, it's blocked
11693 (org-back-to-heading t)
11694 (let ((this-level (funcall outline-level)))
11695 (outline-next-heading)
11696 (let ((child-level (funcall outline-level)))
11697 (while (and (not (eobp))
11698 (> child-level this-level))
11699 ;; this todo has children, check whether they are all
11701 (if (and (not (org-entry-is-done-p))
11702 (org-entry-is-todo-p))
11703 (throw 'dont-block nil))
11704 (outline-next-heading)
11705 (setq child-level (funcall outline-level))))))
11706 ;; Otherwise, if the task's parent has the :ORDERED: property, and
11707 ;; any previous siblings are undone, it's blocked
11709 (org-back-to-heading t)
11710 (let* ((pos (point))
11711 (parent-pos (and (org-up-heading-safe) (point))))
11712 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11713 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11715 (re-search-forward org-not-done-heading-regexp pos t))
11716 (throw 'dont-block nil)) ; block, there is an older sibling not done.
11717 ;; Search further up the hierarchy, to see if an ancestor is blocked
11719 (goto-char parent-pos)
11720 (if (not (looking-at org-not-done-heading-regexp))
11721 (throw 'dont-block t)) ; do not block, parent is not a TODO
11723 (setq parent-pos (and (org-up-heading-safe) (point)))
11724 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11725 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11727 (re-search-forward org-not-done-heading-regexp pos t))
11728 (throw 'dont-block nil)))))))) ; block, older sibling not done.
11730 (defcustom org-track-ordered-property-with-tag nil
11731 "Should the ORDERED property also be shown as a tag?
11732 The ORDERED property decides if an entry should require subtasks to be
11733 completed in sequence. Since a property is not very visible, setting
11734 this option means that toggling the ORDERED property with the command
11735 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
11736 not relevant for the behavior, but it makes things more visible.
11738 Note that toggling the tag with tags commands will not change the property
11739 and therefore not influence behavior!
11741 This can be t, meaning the tag ORDERED should be used, It can also be a
11742 string to select a different tag for this task."
11745 (const :tag "No tracking" nil)
11746 (const :tag "Track with ORDERED tag" t)
11747 (string :tag "Use other tag")))
11749 (defun org-toggle-ordered-property ()
11750 "Toggle the ORDERED property of the current entry.
11751 For better visibility, you can track the value of this property with a tag.
11752 See variable `org-track-ordered-property-with-tag'."
11754 (let* ((t1 org-track-ordered-property-with-tag)
11755 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
11757 (org-back-to-heading)
11758 (if (org-entry-get nil "ORDERED")
11760 (org-delete-property "ORDERED")
11761 (and tag (org-toggle-tag tag 'off))
11762 (message "Subtasks can be completed in arbitrary order"))
11763 (org-entry-put nil "ORDERED" "t")
11764 (and tag (org-toggle-tag tag 'on))
11765 (message "Subtasks must be completed in sequence")))))
11767 (defvar org-blocked-by-checkboxes) ; dynamically scoped
11768 (defun org-block-todo-from-checkboxes (change-plist)
11769 "Block turning an entry into a TODO, using checkboxes.
11770 This checks whether the current task should be blocked from state
11771 changes because there are unchecked boxes in this entry."
11772 (if (not org-enforce-todo-checkbox-dependencies)
11773 t ; if locally turned off don't block
11775 ;; If this is not a todo state change, or if this entry is already DONE,
11777 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11778 (member (plist-get change-plist :from)
11779 (cons 'done org-done-keywords))
11780 (member (plist-get change-plist :to)
11781 (cons 'todo org-not-done-keywords))
11782 (not (plist-get change-plist :to)))
11783 (throw 'dont-block t))
11784 ;; If this task has checkboxes that are not checked, it's blocked
11786 (org-back-to-heading t)
11787 (let ((beg (point)) end)
11788 (outline-next-heading)
11791 (if (org-list-search-forward
11792 (concat (org-item-beginning-re)
11793 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
11797 (if (boundp 'org-blocked-by-checkboxes)
11798 (setq org-blocked-by-checkboxes t))
11799 (throw 'dont-block nil)))))
11800 t))) ; do not block
11802 (defun org-entry-blocked-p ()
11803 "Is the current entry blocked?"
11804 (org-with-buffer-modified-unmodified
11805 (if (org-entry-get nil "NOBLOCKING")
11806 nil ;; Never block this entry
11808 (run-hook-with-args-until-failure
11810 (list :type 'todo-state-change
11815 (defun org-update-statistics-cookies (all)
11816 "Update the statistics cookie, either from TODO or from checkboxes.
11817 This should be called with the cursor in a line with a statistics cookie."
11821 (org-update-checkbox-count 'all)
11822 (org-map-entries 'org-update-parent-todo-statistics))
11823 (if (not (org-at-heading-p))
11824 (org-update-checkbox-count)
11825 (let ((pos (point-marker))
11827 (ignore-errors (org-back-to-heading t))
11828 (if (not (org-at-heading-p))
11829 (org-update-checkbox-count)
11830 (setq l1 (org-outline-level))
11831 (setq end (save-excursion
11832 (outline-next-heading)
11833 (if (org-at-heading-p) (setq l2 (org-outline-level)))
11835 (if (and (save-excursion
11837 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
11838 (not (save-excursion (re-search-forward
11839 ":COOKIE_DATA:.*\\<todo\\>" end t))))
11840 (org-update-checkbox-count)
11841 (if (and l2 (> l2 l1))
11844 (org-update-parent-todo-statistics))
11846 (beginning-of-line 1)
11847 (while (re-search-forward
11848 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
11850 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
11852 (move-marker pos nil)))))
11854 (defvar org-entry-property-inherited-from) ;; defined below
11855 (defun org-update-parent-todo-statistics ()
11856 "Update any statistics cookie in the parent of the current headline.
11857 When `org-hierarchical-todo-statistics' is nil, statistics will cover
11858 the entire subtree and this will travel up the hierarchy and update
11859 statistics everywhere."
11860 (let* ((prop (save-excursion (org-up-heading-safe)
11861 (org-entry-get nil "COOKIE_DATA" 'inherit)))
11862 (recursive (or (not org-hierarchical-todo-statistics)
11863 (and prop (string-match "\\<recursive\\>" prop))))
11864 (lim (or (and prop (marker-position org-entry-property-inherited-from))
11867 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
11868 level ltoggle l1 new ndel
11869 (cnt-all 0) (cnt-done 0) is-percent kwd
11870 checkbox-beg ov ovs ove cookie-present)
11873 (beginning-of-line 1)
11874 (setq ltoggle (funcall outline-level))
11875 ;; Three situations are to consider:
11877 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
11878 ;; to the top-level ancestor on the headline;
11880 ;; 2. If parent has "recursive" property, repeat up to the
11881 ;; headline setting that property, taking inheritance into
11884 ;; 3. Else, move up to direct parent and proceed only once.
11885 (while (and (setq level (org-up-heading-safe))
11886 (or recursive first)
11888 (setq first nil cookie-present nil)
11892 (downcase (or (org-entry-get nil "COOKIE_DATA")
11895 (while (re-search-forward box-re (point-at-eol) t)
11896 (setq cnt-all 0 cnt-done 0 cookie-present t)
11897 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
11899 (unless (outline-next-heading) (throw 'exit nil))
11900 (while (and (looking-at org-complex-heading-regexp)
11901 (> (setq l1 (length (match-string 1))) level))
11902 (setq kwd (and (or recursive (= l1 ltoggle))
11904 (if (or (eq org-provide-todo-statistics 'all-headlines)
11905 (and (listp org-provide-todo-statistics)
11906 (or (member kwd org-provide-todo-statistics)
11907 (member kwd org-done-keywords))))
11908 (setq cnt-all (1+ cnt-all))
11909 (if (eq org-provide-todo-statistics t)
11910 (and kwd (setq cnt-all (1+ cnt-all)))))
11911 (and (member kwd org-done-keywords)
11912 (setq cnt-done (1+ cnt-done)))
11913 (outline-next-heading)))
11916 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
11917 (format "[%d/%d]" cnt-done cnt-all))
11918 ndel (- (match-end 0) checkbox-beg))
11919 ;; handle overlays when updating cookie from column view
11920 (when (setq ov (car (overlays-at checkbox-beg)))
11921 (setq ovs (overlay-start ov) ove (overlay-end ov))
11922 (delete-overlay ov))
11923 (goto-char checkbox-beg)
11925 (delete-region (point) (+ (point) ndel))
11926 (when org-auto-align-tags (org-fix-tags-on-the-fly))
11927 (when ov (move-overlay ov ovs ove)))
11928 (when cookie-present
11929 (run-hook-with-args 'org-after-todo-statistics-hook
11930 cnt-done (- cnt-all cnt-done))))))
11931 (run-hooks 'org-todo-statistics-hook)))
11933 (defvar org-after-todo-statistics-hook nil
11934 "Hook that is called after a TODO statistics cookie has been updated.
11935 Each function is called with two arguments: the number of not-done entries
11936 and the number of done entries.
11938 For example, the following function, when added to this hook, will switch
11939 an entry to DONE when all children are done, and back to TODO when new
11940 entries are set to a TODO status. Note that this hook is only called
11941 when there is a statistics cookie in the headline!
11943 (defun org-summary-todo (n-done n-not-done)
11944 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
11945 (let (org-log-done org-log-states) ; turn off logging
11946 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
11949 (defvar org-todo-statistics-hook nil
11950 "Hook that is run whenever Org thinks TODO statistics should be updated.
11951 This hook runs even if there is no statistics cookie present, in which case
11952 `org-after-todo-statistics-hook' would not run.")
11954 (defun org-todo-trigger-tag-changes (state)
11955 "Apply the changes defined in `org-todo-state-tags-triggers'."
11956 (let ((l org-todo-state-tags-triggers)
11958 (when (or (not state) (equal state ""))
11959 (setq changes (append changes (cdr (assoc "" l)))))
11960 (when (and (stringp state) (> (length state) 0))
11961 (setq changes (append changes (cdr (assoc state l)))))
11962 (when (member state org-not-done-keywords)
11963 (setq changes (append changes (cdr (assoc 'todo l)))))
11964 (when (member state org-done-keywords)
11965 (setq changes (append changes (cdr (assoc 'done l)))))
11966 (dolist (c changes)
11967 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
11969 (defun org-local-logging (value)
11970 "Get logging settings from a property VALUE."
11972 ;; directly set the variables, they are already local.
11973 (setq org-log-done nil
11975 org-todo-log-states nil)
11976 (setq words (org-split-string value))
11977 (while (setq w (pop words))
11979 ((setq a (assoc w org-startup-options))
11980 (and (member (nth 1 a) '(org-log-done org-log-repeat))
11981 (set (nth 1 a) (nth 2 a))))
11982 ((setq a (org-extract-log-state-settings w))
11983 (and (member (car a) org-todo-keywords-1)
11984 (push a org-todo-log-states)))))))
11986 (defun org-get-todo-sequence-head (kwd)
11987 "Return the head of the TODO sequence to which KWD belongs.
11988 If KWD is not set, check if there is a text property remembering the
11993 (or (get-text-property (point-at-bol) 'org-todo-head)
11995 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
11996 nil (point-at-eol)))
11997 (get-text-property p 'org-todo-head))))
11998 ((not (member kwd org-todo-keywords-1))
11999 (car org-todo-keywords-1))
12000 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
12002 (defun org-fast-todo-selection ()
12003 "Fast TODO keyword selection with single keys.
12004 Returns the new TODO keyword, or nil if no state change should occur."
12005 (let* ((fulltable org-todo-key-alist)
12006 (done-keywords org-done-keywords) ;; needed for the faces.
12007 (maxlen (apply 'max (mapcar
12009 (if (stringp (car x)) (string-width (car x)) 0))
12012 (fwidth (+ maxlen 3 1 3))
12013 (ncol (/ (- (window-width) 4) fwidth))
12017 (save-window-excursion
12019 (set-buffer (get-buffer-create " *Org todo*"))
12020 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
12022 (org-set-local 'org-done-keywords done-keywords)
12023 (setq tbl fulltable cnt 0)
12024 (while (setq e (pop tbl))
12026 ((equal e '(:startgroup))
12027 (push '() groups) (setq ingroup t)
12028 (when (not (= cnt 0))
12032 ((equal e '(:endgroup))
12033 (setq ingroup nil cnt 0)
12035 ((equal e '(:newline))
12036 (when (not (= cnt 0))
12040 (while (equal (car tbl) '(:newline))
12042 (setq tbl (cdr tbl)))))
12044 (setq tg (car e) c (cdr e))
12045 (if ingroup (push tg (car groups)))
12046 (setq tg (org-add-props tg nil 'face
12047 (org-get-todo-face tg)))
12048 (if (and (= cnt 0) (not ingroup)) (insert " "))
12049 (insert "[" c "] " tg (make-string
12050 (- fwidth 4 (length tg)) ?\ ))
12051 (when (= (setq cnt (1+ cnt)) ncol)
12053 (if ingroup (insert " "))
12056 (goto-char (point-min))
12057 (if (not expert) (org-fit-window-to-buffer))
12058 (message "[a-z..]:Set [SPC]:clear")
12059 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
12062 (and (= c ?q) (not (rassoc c fulltable))))
12063 (setq quit-flag t))
12065 ((setq e (rassoc c fulltable) tg (car e))
12067 (t (setq quit-flag t)))))))
12069 (defun org-entry-is-todo-p ()
12070 (member (org-get-todo-state) org-not-done-keywords))
12072 (defun org-entry-is-done-p ()
12073 (member (org-get-todo-state) org-done-keywords))
12075 (defun org-get-todo-state ()
12077 (org-back-to-heading t)
12078 (and (looking-at org-todo-line-regexp)
12080 (match-string 2))))
12082 (defun org-at-date-range-p (&optional inactive-ok)
12083 "Is the cursor inside a date range?"
12087 (let ((pos (point)))
12088 (skip-chars-backward "^[<\r\n")
12089 (skip-chars-backward "<[")
12090 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12091 (>= (match-end 0) pos)
12093 (skip-chars-backward "^<[\r\n")
12094 (skip-chars-backward "<[")
12095 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12096 (>= (match-end 0) pos)
12100 (defun org-get-repeat (&optional tagline)
12101 "Check if there is a deadline/schedule with repeater in this entry."
12104 (org-back-to-heading t)
12105 (and (re-search-forward (if tagline
12106 (concat tagline "\\s-*" org-repeat-re)
12108 (org-entry-end-position) t)
12109 (match-string-no-properties 1)))))
12111 (defvar org-last-changed-timestamp)
12112 (defvar org-last-inserted-timestamp)
12113 (defvar org-log-post-message)
12114 (defvar org-log-note-purpose)
12115 (defvar org-log-note-how)
12116 (defvar org-log-note-extra)
12117 (defun org-auto-repeat-maybe (done-word)
12118 "Check if the current headline contains a repeated deadline/schedule.
12119 If yes, set TODO state back to what it was and change the base date
12120 of repeating deadline/scheduled time stamps to new date.
12121 This function is run automatically after each state change to a DONE state."
12122 ;; last-state is dynamically scoped into this function
12123 (let* ((repeat (org-get-repeat))
12124 (aa (assoc org-last-state org-todo-kwd-alist))
12125 (interpret (nth 1 aa))
12127 (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
12128 (msg "Entry repeats: ")
12130 (org-todo-log-states nil)
12131 re type n what ts time to-state)
12133 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
12134 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
12135 org-todo-repeat-to-state))
12136 (unless (and to-state (member to-state org-todo-keywords-1))
12137 (setq to-state (if (eq interpret 'type) org-last-state head)))
12138 (org-todo to-state)
12139 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
12140 (org-entry-put nil "LAST_REPEAT" (format-time-string
12141 (org-time-stamp-format t t))))
12142 (when org-log-repeat
12143 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
12144 (memq 'org-add-log-note post-command-hook))
12145 ;; OK, we are already setup for some record
12146 (if (eq org-log-repeat 'note)
12147 ;; make sure we take a note, not only a time stamp
12148 (setq org-log-note-how 'note))
12149 ;; Set up for taking a record
12150 (org-add-log-setup 'state (or done-word (car org-done-keywords))
12152 'findpos org-log-repeat)))
12153 (org-back-to-heading t)
12154 (org-add-planning-info nil nil 'closed)
12155 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12156 org-deadline-time-regexp "\\)\\|\\("
12157 org-ts-regexp "\\)"))
12158 (while (re-search-forward
12159 re (save-excursion (outline-next-heading) (point)) t)
12160 (setq type (if (match-end 1) org-scheduled-string
12161 (if (match-end 3) org-deadline-string "Plain:"))
12162 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
12163 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
12164 (setq n (string-to-number (match-string 2 ts))
12165 what (match-string 3 ts))
12166 (if (equal what "w") (setq n (* n 7) what "d"))
12167 (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
12168 (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
12169 ;; Preparation, see if we need to modify the start date for the change
12170 (when (match-end 1)
12171 (setq time (save-match-data (org-time-string-to-time ts)))
12173 ((equal (match-string 1 ts) ".")
12174 ;; Shift starting date to today
12175 (org-timestamp-change
12176 (- (org-today) (time-to-days time))
12178 ((equal (match-string 1 ts) "+")
12179 (let ((nshiftmax 10) (nshift 0))
12180 (while (or (= nshift 0)
12181 (<= (time-to-days time)
12182 (time-to-days (current-time))))
12183 (when (= (incf nshift) nshiftmax)
12184 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
12186 (org-timestamp-change n (cdr (assoc what whata)))
12187 (org-at-timestamp-p t)
12188 (setq ts (match-string 1))
12189 (setq time (save-match-data (org-time-string-to-time ts)))))
12190 (org-timestamp-change (- n) (cdr (assoc what whata)))
12191 ;; rematch, so that we have everything in place for the real shift
12192 (org-at-timestamp-p t)
12193 (setq ts (match-string 1))
12194 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
12195 (org-timestamp-change n (cdr (assoc what whata)))
12196 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
12197 (setq org-log-post-message msg)
12198 (message "%s" msg))))
12200 (defun org-show-todo-tree (arg)
12201 "Make a compact tree which shows all headlines marked with TODO.
12202 The tree will show the lines where the regexp matches, and all higher
12203 headlines above the match.
12204 With a \\[universal-argument] prefix, prompt for a regexp to match.
12205 With a numeric prefix N, construct a sparse tree for the Nth element
12206 of `org-todo-keywords-1'."
12208 (let ((case-fold-search nil)
12210 (cond ((null arg) org-not-done-regexp)
12212 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
12213 (mapcar 'list org-todo-keywords-1))))
12215 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12217 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12218 (regexp-quote (nth (1- (prefix-numeric-value arg))
12219 org-todo-keywords-1)))
12220 (t (error "Invalid prefix argument: %s" arg)))))
12221 (message "%d TODO entries found"
12222 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
12224 (defun org-deadline (&optional remove time)
12225 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
12226 With argument REMOVE, remove any deadline from the item.
12227 With argument TIME, set the deadline at the corresponding date. TIME
12228 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12230 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12231 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12232 'region-start-level 'region))
12233 org-loop-over-headlines-in-active-region)
12235 `(org-deadline ',remove ,time)
12236 org-loop-over-headlines-in-active-region
12237 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12238 (let* ((old-date (org-entry-get nil "DEADLINE"))
12239 (repeater (and old-date
12241 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12243 (match-string 1 old-date))))
12246 (when (and old-date org-log-redeadline)
12247 (org-add-log-setup 'deldeadline nil old-date 'findpos
12248 org-log-redeadline))
12249 (org-remove-timestamp-with-keyword org-deadline-string)
12250 (message "Item no longer has a deadline."))
12251 (org-add-planning-info 'deadline time 'closed)
12252 (when (and old-date org-log-redeadline
12253 (not (equal old-date
12254 (substring org-last-inserted-timestamp 1 -1))))
12255 (org-add-log-setup 'redeadline nil old-date 'findpos
12256 org-log-redeadline))
12259 (org-back-to-heading t)
12260 (when (re-search-forward (concat org-deadline-string " "
12261 org-last-inserted-timestamp)
12263 (outline-next-heading) (point)) t)
12264 (goto-char (1- (match-end 0)))
12265 (insert " " repeater)
12266 (setq org-last-inserted-timestamp
12267 (concat (substring org-last-inserted-timestamp 0 -1)
12269 (substring org-last-inserted-timestamp -1))))))
12270 (message "Deadline on %s" org-last-inserted-timestamp)))))
12272 (defun org-schedule (&optional remove time)
12273 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
12274 With argument REMOVE, remove any scheduling date from the item.
12275 With argument TIME, scheduled at the corresponding date. TIME can
12276 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12278 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12279 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12280 'region-start-level 'region))
12281 org-loop-over-headlines-in-active-region)
12283 `(org-schedule ',remove ,time)
12284 org-loop-over-headlines-in-active-region
12285 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12286 (let* ((old-date (org-entry-get nil "SCHEDULED"))
12287 (repeater (and old-date
12289 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12291 (match-string 1 old-date))))
12294 (when (and old-date org-log-reschedule)
12295 (org-add-log-setup 'delschedule nil old-date 'findpos
12296 org-log-reschedule))
12297 (org-remove-timestamp-with-keyword org-scheduled-string)
12298 (message "Item is no longer scheduled."))
12299 (org-add-planning-info 'scheduled time 'closed)
12300 (when (and old-date org-log-reschedule
12301 (not (equal old-date
12302 (substring org-last-inserted-timestamp 1 -1))))
12303 (org-add-log-setup 'reschedule nil old-date 'findpos
12304 org-log-reschedule))
12307 (org-back-to-heading t)
12308 (when (re-search-forward (concat org-scheduled-string " "
12309 org-last-inserted-timestamp)
12311 (outline-next-heading) (point)) t)
12312 (goto-char (1- (match-end 0)))
12313 (insert " " repeater)
12314 (setq org-last-inserted-timestamp
12315 (concat (substring org-last-inserted-timestamp 0 -1)
12317 (substring org-last-inserted-timestamp -1))))))
12318 (message "Scheduled to %s" org-last-inserted-timestamp)))))
12320 (defun org-get-scheduled-time (pom &optional inherit)
12321 "Get the scheduled time as a time tuple, of a format suitable
12322 for calling org-schedule with, or if there is no scheduling,
12324 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
12326 (apply 'encode-time (org-parse-time-string time)))))
12328 (defun org-get-deadline-time (pom &optional inherit)
12329 "Get the deadline as a time tuple, of a format suitable for
12330 calling org-deadline with, or if there is no scheduling, returns
12332 (let ((time (org-entry-get pom "DEADLINE" inherit)))
12334 (apply 'encode-time (org-parse-time-string time)))))
12336 (defun org-remove-timestamp-with-keyword (keyword)
12337 "Remove all time stamps with KEYWORD in the current entry."
12338 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
12341 (org-back-to-heading t)
12343 (outline-next-heading)
12344 (while (re-search-backward re beg t)
12346 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
12347 (equal (char-before) ?\ ))
12348 (backward-delete-char 1)
12349 (if (string-match "^[ \t]*$" (buffer-substring
12350 (point-at-bol) (point-at-eol)))
12351 (delete-region (point-at-bol)
12352 (min (point-max) (1+ (point-at-eol))))))))))
12354 (defun org-add-planning-info (what &optional time &rest remove)
12355 "Insert new timestamp with keyword in the line directly after the headline.
12356 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
12357 If non is given, the user is prompted for a date.
12358 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
12361 (let (org-time-was-given org-end-time-was-given ts
12362 end default-time default-input)
12365 (when (and (memq what '(scheduled deadline))
12367 (and (stringp time)
12368 (string-match "^[-+]+[0-9]" time))))
12369 ;; Try to get a default date/time from existing timestamp
12371 (org-back-to-heading t)
12372 (setq end (save-excursion (outline-next-heading) (point)))
12373 (when (re-search-forward (if (eq what 'scheduled)
12374 org-scheduled-time-regexp
12375 org-deadline-time-regexp)
12377 (setq ts (match-string 1)
12379 (apply 'encode-time (org-parse-time-string ts))
12380 default-input (and ts (org-get-compact-tod ts))))))
12384 ;; This is a string (relative or absolute), set proper date
12385 (apply 'encode-time
12386 (org-read-date-analyze
12387 time default-time (decode-time default-time)))
12388 ;; If necessary, get the time from the user
12389 (or time (org-read-date nil 'to-time nil nil
12390 default-time default-input)))))
12392 (when (and org-insert-labeled-timestamps-at-point
12393 (member what '(scheduled deadline)))
12395 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
12396 (org-insert-time-stamp time org-time-was-given
12397 nil nil nil (list org-end-time-was-given))
12401 (let (col list elt ts buffer-invisibility-spec)
12402 (org-back-to-heading t)
12403 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
12404 (goto-char (match-end 1))
12405 (setq col (current-column))
12406 (goto-char (match-end 0))
12407 (if (eobp) (insert "\n") (forward-char 1))
12408 (when (and (not what)
12411 org-keyword-time-not-clock-regexp))))
12412 ;; Nothing to add, nothing to remove...... :-)
12414 (if (and (not (looking-at org-outline-regexp))
12415 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
12417 (not (equal (match-string 1) org-clock-string)))
12418 (narrow-to-region (match-beginning 0) (match-end 0))
12419 (insert-before-markers "\n")
12421 (narrow-to-region (point) (point))
12422 (and org-adapt-indentation (org-indent-to-column col)))
12423 ;; Check if we have to remove something.
12424 (setq list (cons what remove))
12426 (setq elt (pop list))
12427 (when (or (and (eq elt 'scheduled)
12428 (re-search-forward org-scheduled-time-regexp nil t))
12429 (and (eq elt 'deadline)
12430 (re-search-forward org-deadline-time-regexp nil t))
12431 (and (eq elt 'closed)
12432 (re-search-forward org-closed-time-regexp nil t)))
12434 (if (looking-at "--+<[^>]+>") (replace-match ""))))
12435 (and (looking-at "[ \t]+") (replace-match ""))
12436 (and org-adapt-indentation (bolp) (org-indent-to-column col))
12439 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
12440 (cond ((eq what 'scheduled) org-scheduled-string)
12441 ((eq what 'deadline) org-deadline-string)
12442 ((eq what 'closed) org-closed-string))
12444 (setq ts (org-insert-time-stamp
12446 (or org-time-was-given
12447 (and (eq what 'closed) org-log-done-with-time))
12449 nil nil (list org-end-time-was-given)))
12451 (if (not (or (bolp) (eq (char-before) ?\ )
12452 (memq (char-after) '(32 10))
12455 (goto-char (point-min))
12457 (if (and (looking-at "[ \t]*\n")
12458 (equal (char-before) ?\n))
12459 (delete-region (1- (point)) (point-at-eol)))
12462 (defvar org-log-note-marker (make-marker))
12463 (defvar org-log-note-purpose nil)
12464 (defvar org-log-note-state nil)
12465 (defvar org-log-note-previous-state nil)
12466 (defvar org-log-note-how nil)
12467 (defvar org-log-note-extra nil)
12468 (defvar org-log-note-window-configuration nil)
12469 (defvar org-log-note-return-to (make-marker))
12470 (defvar org-log-note-effective-time nil
12471 "Remembered current time so that dynamically scoped
12472 `org-extend-today-until' affects tha timestamps in state change
12475 (defvar org-log-post-message nil
12476 "Message to be displayed after a log note has been stored.
12477 The auto-repeater uses this.")
12479 (defun org-add-note ()
12480 "Add a note to the current entry.
12481 This is done in the same way as adding a state change note."
12483 (org-add-log-setup 'note nil nil 'findpos nil))
12485 (defvar org-property-end-re)
12486 (defun org-add-log-setup (&optional purpose state prev-state
12488 "Set up the post command hook to take a note.
12489 If this is about to TODO state change, the new state is expected in STATE.
12490 When FINDPOS is non-nil, find the correct position for the note in
12491 the current entry. If not, assume that it can be inserted at point.
12492 HOW is an indicator what kind of note should be created.
12493 EXTRA is additional text that will be inserted into the notes buffer."
12494 (let* ((org-log-into-drawer (org-log-into-drawer))
12495 (drawer (cond ((stringp org-log-into-drawer)
12496 org-log-into-drawer)
12497 (org-log-into-drawer "LOGBOOK"))))
12501 (org-back-to-heading t)
12502 (narrow-to-region (point) (save-excursion
12503 (outline-next-heading) (point)))
12504 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
12505 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
12507 (goto-char (match-end 0))
12510 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
12513 (goto-char (match-end 0))
12514 (or org-log-states-order-reversed
12515 (and (re-search-forward org-property-end-re nil t)
12516 (goto-char (1- (match-beginning 0))))))
12517 (insert "\n:" drawer ":\n:END:")
12518 (beginning-of-line 0)
12520 (beginning-of-line 2)
12523 ((and org-log-state-notes-insert-after-drawers
12525 (forward-line) (looking-at org-drawer-regexp)))
12527 (while (looking-at org-drawer-regexp)
12528 (goto-char (match-end 0))
12529 (re-search-forward org-property-end-re (point-max) t)
12531 (forward-line -1)))
12532 (unless org-log-states-order-reversed
12533 (and (= (char-after) ?\n) (forward-char 1))
12534 (org-skip-over-state-notes)
12535 (skip-chars-backward " \t\n\r")))
12536 (move-marker org-log-note-marker (point))
12537 (setq org-log-note-purpose purpose
12538 org-log-note-state state
12539 org-log-note-previous-state prev-state
12540 org-log-note-how how
12541 org-log-note-extra extra
12542 org-log-note-effective-time (org-current-effective-time))
12543 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
12545 (defun org-skip-over-state-notes ()
12546 "Skip past the list of State notes in an entry."
12547 (if (looking-at "\n[ \t]*- State") (forward-char 1))
12548 (when (ignore-errors (goto-char (org-in-item-p)))
12549 (let* ((struct (org-list-struct))
12550 (prevs (org-list-prevs-alist struct)))
12551 (while (looking-at "[ \t]*- State")
12552 (goto-char (or (org-list-get-next-item (point) struct prevs)
12553 (org-list-get-item-end (point) struct)))))))
12555 (defun org-add-log-note (&optional purpose)
12556 "Pop up a window for taking a note, and add this note later at point."
12557 (remove-hook 'post-command-hook 'org-add-log-note)
12558 (setq org-log-note-window-configuration (current-window-configuration))
12559 (delete-other-windows)
12560 (move-marker org-log-note-return-to (point))
12561 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
12562 (goto-char org-log-note-marker)
12563 (org-switch-to-buffer-other-window "*Org Note*")
12565 (if (memq org-log-note-how '(time state))
12566 (let (current-prefix-arg) (org-store-log-note))
12567 (let ((org-inhibit-startup t)) (org-mode))
12568 (insert (format "# Insert note for %s.
12569 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
12571 ((eq org-log-note-purpose 'clock-out) "stopped clock")
12572 ((eq org-log-note-purpose 'done) "closed todo item")
12573 ((eq org-log-note-purpose 'state)
12574 (format "state change from \"%s\" to \"%s\""
12575 (or org-log-note-previous-state "")
12576 (or org-log-note-state "")))
12577 ((eq org-log-note-purpose 'reschedule)
12579 ((eq org-log-note-purpose 'delschedule)
12580 "no longer scheduled")
12581 ((eq org-log-note-purpose 'redeadline)
12582 "changing deadline")
12583 ((eq org-log-note-purpose 'deldeadline)
12584 "removing deadline")
12585 ((eq org-log-note-purpose 'refile)
12587 ((eq org-log-note-purpose 'note)
12589 (t (error "This should not happen")))))
12590 (if org-log-note-extra (insert org-log-note-extra))
12591 (org-set-local 'org-finish-function 'org-store-log-note)
12592 (run-hooks 'org-log-buffer-setup-hook)))
12594 (defvar org-note-abort nil) ; dynamically scoped
12595 (defun org-store-log-note ()
12596 "Finish taking a log note, and insert it to where it belongs."
12597 (let ((txt (buffer-string))
12598 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
12600 (kill-buffer (current-buffer))
12601 (while (string-match "\\`# .*\n[ \t\n]*" txt)
12602 (setq txt (replace-match "" t t txt)))
12603 (if (string-match "\\s-+\\'" txt)
12604 (setq txt (replace-match "" t t txt)))
12605 (setq lines (org-split-string txt "\n"))
12606 (when (and note (string-match "\\S-" note))
12608 (org-replace-escapes
12610 (list (cons "%u" (user-login-name))
12611 (cons "%U" user-full-name)
12612 (cons "%t" (format-time-string
12613 (org-time-stamp-format 'long 'inactive)
12614 org-log-note-effective-time))
12615 (cons "%T" (format-time-string
12616 (org-time-stamp-format 'long nil)
12617 org-log-note-effective-time))
12618 (cons "%d" (format-time-string
12619 (org-time-stamp-format nil 'inactive)
12620 org-log-note-effective-time))
12621 (cons "%D" (format-time-string
12622 (org-time-stamp-format nil nil)
12623 org-log-note-effective-time))
12624 (cons "%s" (if org-log-note-state
12625 (concat "\"" org-log-note-state "\"")
12627 (cons "%S" (if org-log-note-previous-state
12628 (concat "\"" org-log-note-previous-state "\"")
12630 (if lines (setq note (concat note " \\\\")))
12632 (when (or current-prefix-arg org-note-abort)
12633 (when org-log-into-drawer
12634 (org-remove-empty-drawer-at
12635 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
12636 org-log-note-marker))
12639 (with-current-buffer (marker-buffer org-log-note-marker)
12641 (goto-char org-log-note-marker)
12642 (move-marker org-log-note-marker nil)
12644 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
12645 (setq ind (save-excursion
12646 (if (ignore-errors (goto-char (org-in-item-p)))
12647 (let ((struct (org-list-struct)))
12649 (org-list-get-top-point struct) struct))
12650 (skip-chars-backward " \r\t\n")
12652 ((and (org-at-heading-p)
12653 org-adapt-indentation)
12654 (1+ (org-current-level)))
12655 ((org-at-heading-p) 0)
12656 (t (org-get-indentation))))))
12657 (setq bul (org-list-bullet-string "-"))
12658 (org-indent-line-to ind)
12659 (insert bul (pop lines))
12660 (let ((ind-body (+ (length bul) ind)))
12663 (org-indent-line-to ind-body)
12664 (insert (pop lines))))
12665 (message "Note stored")
12666 (org-back-to-heading t)
12667 (org-cycle-hide-drawers 'children)))))
12668 (set-window-configuration org-log-note-window-configuration)
12669 (with-current-buffer (marker-buffer org-log-note-return-to)
12670 (goto-char org-log-note-return-to))
12671 (move-marker org-log-note-return-to nil)
12672 (and org-log-post-message (message "%s" org-log-post-message)))
12674 (defun org-remove-empty-drawer-at (drawer pos)
12675 "Remove an empty drawer DRAWER at position POS.
12676 POS may also be a marker."
12677 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
12683 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
12684 (replace-match ""))))))
12686 (defvar org-ts-type nil)
12687 (defun org-sparse-tree (&optional arg type)
12688 "Create a sparse tree, prompt for the details.
12689 This command can create sparse trees. You first need to select the type
12690 of match used to create the tree:
12692 t Show all TODO entries.
12693 T Show entries with a specific TODO keyword.
12694 m Show entries selected by a tags/property match.
12695 p Enter a property name and its value (both with completion on existing
12696 names/values) and show entries with that property.
12697 r Show entries matching a regular expression (`/' can be used as well).
12698 b Show deadlines and scheduled items before a date.
12699 a Show deadlines and scheduled items after a date.
12700 d Show deadlines due within `org-deadline-warning-days'.
12701 D Show deadlines and scheduled items between a date range."
12703 (let (ans kwd value ts-type)
12704 (setq type (or type org-sparse-tree-default-date-type))
12705 (setq org-ts-type type)
12706 (message "Sparse tree: [/]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"
12707 (cond ((eq type 'all) "all timestamps")
12708 ((eq type 'scheduled) "only scheduled")
12709 ((eq type 'deadline) "only deadline")
12710 ((eq type 'active) "only active timestamps")
12711 ((eq type 'inactive) "only inactive timestamps")
12712 ((eq type 'scheduled-or-deadline) "scheduled/deadline")
12713 (t "scheduled/deadline")))
12714 (setq ans (read-char-exclusive))
12717 (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
12719 (call-interactively 'org-check-deadlines))
12721 (call-interactively 'org-check-before-date))
12723 (call-interactively 'org-check-after-date))
12725 (call-interactively 'org-check-dates-range))
12727 (call-interactively 'org-show-todo-tree))
12729 (org-show-todo-tree '(4)))
12730 ((member ans '(?T ?m))
12731 (call-interactively 'org-match-sparse-tree))
12732 ((member ans '(?p ?P))
12733 (setq kwd (org-icompleting-read "Property: "
12734 (mapcar 'list (org-buffer-property-keys))))
12735 (setq value (org-icompleting-read "Value: "
12736 (mapcar 'list (org-property-values kwd))))
12737 (unless (string-match "\\`{.*}\\'" value)
12738 (setq value (concat "\"" value "\"")))
12739 (org-match-sparse-tree arg (concat kwd "=" value)))
12740 ((member ans '(?r ?R ?/))
12741 (call-interactively 'org-occur))
12742 (t (error "No such sparse tree command \"%c\"" ans)))))
12744 (defvar org-occur-highlights nil
12745 "List of overlays used for occur matches.")
12746 (make-variable-buffer-local 'org-occur-highlights)
12747 (defvar org-occur-parameters nil
12748 "Parameters of the active org-occur calls.
12749 This is a list, each call to org-occur pushes as cons cell,
12750 containing the regular expression and the callback, onto the list.
12751 The list can contain several entries if `org-occur' has been called
12752 several time with the KEEP-PREVIOUS argument. Otherwise, this list
12753 will only contain one set of parameters. When the highlights are
12754 removed (for example with `C-c C-c', or with the next edit (depending
12755 on `org-remove-highlights-with-change'), this variable is emptied
12757 (make-variable-buffer-local 'org-occur-parameters)
12759 (defun org-occur (regexp &optional keep-previous callback)
12760 "Make a compact tree which shows all matches of REGEXP.
12761 The tree will show the lines where the regexp matches, and all higher
12762 headlines above the match. It will also show the heading after the match,
12763 to make sure editing the matching entry is easy.
12764 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
12765 call to `org-occur' will be kept, to allow stacking of calls to this
12767 If CALLBACK is non-nil, it is a function which is called to confirm
12768 that the match should indeed be shown."
12769 (interactive "sRegexp: \nP")
12770 (when (equal regexp "")
12771 (error "Regexp cannot be empty"))
12772 (unless keep-previous
12773 (org-remove-occur-highlights nil nil t))
12774 (push (cons regexp callback) org-occur-parameters)
12777 (goto-char (point-min))
12778 (if (or (not keep-previous) ; do not want to keep
12779 (not org-occur-highlights)) ; no previous matches
12782 (while (re-search-forward regexp nil t)
12783 (when (or (not callback)
12784 (save-match-data (funcall callback)))
12785 (setq cnt (1+ cnt))
12786 (when org-highlight-sparse-tree-matches
12787 (org-highlight-new-match (match-beginning 0) (match-end 0)))
12788 (org-show-context 'occur-tree))))
12789 (when org-remove-highlights-with-change
12790 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
12792 (unless org-sparse-tree-open-archived-trees
12793 (org-hide-archived-subtrees (point-min) (point-max)))
12794 (run-hooks 'org-occur-hook)
12795 (if (org-called-interactively-p 'interactive)
12796 (message "%d match(es) for regexp %s" cnt regexp))
12799 (defun org-occur-next-match (&optional n reset)
12800 "Function for `next-error-function' to find sparse tree matches.
12801 N is the number of matches to move, when negative move backwards.
12802 RESET is entirely ignored - this function always goes back to the
12803 starting point when no match is found."
12804 (let* ((limit (if (< n 0) (point-min) (point-max)))
12805 (search-func (if (< n 0)
12806 'previous-single-char-property-change
12807 'next-single-char-property-change))
12812 (while (setq p1 (funcall search-func (point) 'org-type))
12813 (when (equal p1 limit)
12815 (error "No more matches"))
12816 (when (equal (get-char-property p1 'org-type) 'org-occur)
12820 (throw 'exit (point))))
12823 (error "No more matches"))))
12825 (defun org-show-context (&optional key)
12826 "Make sure point and context are visible.
12827 How much context is shown depends upon the variables
12828 `org-show-hierarchy-above', `org-show-following-heading',
12829 `org-show-entry-below' and `org-show-siblings'."
12830 (let ((heading-p (org-at-heading-p t))
12831 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
12832 (following-p (org-get-alist-option org-show-following-heading key))
12833 (entry-p (org-get-alist-option org-show-entry-below key))
12834 (siblings-p (org-get-alist-option org-show-siblings key)))
12836 ;; Show heading or entry text
12837 (if (and heading-p (not entry-p))
12838 (org-flag-heading nil) ; only show the heading
12839 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
12840 (org-show-hidden-entry))) ; show entire entry
12842 ;; Show next sibling, or heading below text
12844 (and (if heading-p (org-goto-sibling) (outline-next-heading))
12845 (org-flag-heading nil))))
12846 (when siblings-p (org-show-siblings))
12848 ;; show all higher headings, possibly with siblings
12850 (while (and (condition-case nil
12851 (progn (org-up-heading-all 1) t)
12854 (org-flag-heading nil)
12855 (when siblings-p (org-show-siblings))))))))
12857 (defvar org-reveal-start-hook nil
12858 "Hook run before revealing a location.")
12860 (defun org-reveal (&optional siblings)
12861 "Show current entry, hierarchy above it, and the following headline.
12862 This can be used to show a consistent set of context around locations
12863 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
12864 not t for the search context.
12866 With optional argument SIBLINGS, on each level of the hierarchy all
12867 siblings are shown. This repairs the tree structure to what it would
12868 look like when opened with hierarchical calls to `org-cycle'.
12869 With double optional argument \\[universal-argument] \\[universal-argument], \
12870 go to the parent and show the
12873 (run-hooks 'org-reveal-start-hook)
12874 (let ((org-show-hierarchy-above t)
12875 (org-show-following-heading t)
12876 (org-show-siblings (if siblings t org-show-siblings)))
12877 (org-show-context nil))
12878 (when (equal siblings '(16))
12880 (when (org-up-heading-safe)
12882 (run-hook-with-args 'org-cycle-hook 'subtree)))))
12884 (defun org-highlight-new-match (beg end)
12885 "Highlight from BEG to END and mark the highlight is an occur headline."
12886 (let ((ov (make-overlay beg end)))
12887 (overlay-put ov 'face 'secondary-selection)
12888 (overlay-put ov 'org-type 'org-occur)
12889 (push ov org-occur-highlights)))
12891 (defun org-remove-occur-highlights (&optional beg end noremove)
12892 "Remove the occur highlights from the buffer.
12893 BEG and END are ignored. If NOREMOVE is nil, remove this function
12894 from the `before-change-functions' in the current buffer."
12896 (unless org-inhibit-highlight-removal
12897 (mapc 'delete-overlay org-occur-highlights)
12898 (setq org-occur-highlights nil)
12899 (setq org-occur-parameters nil)
12901 (remove-hook 'before-change-functions
12902 'org-remove-occur-highlights 'local))))
12906 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
12907 "Regular expression matching the priority indicator.")
12909 (defvar org-remove-priority-next-time nil)
12911 (defun org-priority-up ()
12912 "Increase the priority of the current item."
12914 (org-priority 'up))
12916 (defun org-priority-down ()
12917 "Decrease the priority of the current item."
12919 (org-priority 'down))
12921 (defun org-priority (&optional action show)
12922 "Change the priority of an item.
12923 ACTION can be `set', `up', `down', or a character."
12925 (if (equal action '(4))
12926 (org-show-priority)
12927 (unless org-enable-priority-commands
12928 (error "Priority commands are disabled"))
12929 (setq action (or action 'set))
12930 (let (current new news have remove)
12932 (org-back-to-heading t)
12933 (if (looking-at org-priority-regexp)
12934 (setq current (string-to-char (match-string 2))
12937 ((eq action 'remove)
12938 (setq remove t new ?\ ))
12939 ((or (eq action 'set)
12940 (if (featurep 'xemacs) (characterp action) (integerp action)))
12941 (if (not (eq action 'set))
12943 (message "Priority %c-%c, SPC to remove: "
12944 org-highest-priority org-lowest-priority)
12946 (setq new (read-char-exclusive))))
12947 (if (and (= (upcase org-highest-priority) org-highest-priority)
12948 (= (upcase org-lowest-priority) org-lowest-priority))
12949 (setq new (upcase new)))
12950 (cond ((equal new ?\ ) (setq remove t))
12951 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
12952 (error "Priority must be between `%c' and `%c'"
12953 org-highest-priority org-lowest-priority))))
12956 (1- current) ; normal cycling
12957 ;; last priority was empty
12958 (if (eq last-command this-command)
12959 org-lowest-priority ; wrap around empty to lowest
12961 (if org-priority-start-cycle-with-default
12962 org-default-priority
12963 (1- org-default-priority))))))
12966 (1+ current) ; normal cycling
12967 ;; last priority was empty
12968 (if (eq last-command this-command)
12969 org-highest-priority ; wrap around empty to highest
12971 (if org-priority-start-cycle-with-default
12972 org-default-priority
12973 (1+ org-default-priority))))))
12974 (t (error "Invalid action")))
12975 (if (or (< (upcase new) org-highest-priority)
12976 (> (upcase new) org-lowest-priority))
12977 (if (and (memq action '(up down))
12978 (not have) (not (eq last-command this-command)))
12979 ;; `new' is from default priority
12981 "The default can not be set, see `org-default-priority' why")
12982 ;; normal cycling: `new' is beyond highest/lowest priority
12983 ;; and is wrapped around to the empty priority
12985 (setq news (format "%c" new))
12988 (replace-match "" t t nil 1)
12989 (replace-match news t t nil 2))
12991 (error "No priority cookie found in line")
12992 (let ((case-fold-search nil))
12993 (looking-at org-todo-line-regexp))
12996 (goto-char (match-end 2))
12997 (insert " [#" news "]"))
12998 (goto-char (match-beginning 3))
12999 (insert "[#" news "] "))))
13000 (org-preserve-lc (org-set-tags nil 'align)))
13002 (message "Priority removed")
13003 (message "Priority of current item set to %s" news)))))
13005 (defun org-show-priority ()
13006 "Show the priority of the current item.
13007 This priority is composed of the main priority given with the [#A] cookies,
13008 and by additional input from the age of a schedules or deadline entry."
13010 (let ((pri (if (eq major-mode 'org-agenda-mode)
13011 (org-get-at-bol 'priority)
13014 (beginning-of-line)
13015 (and (looking-at org-heading-regexp)
13016 (org-get-priority (match-string 0))))))))
13017 (message "Priority is %d" (if pri pri -1000))))
13019 (defun org-get-priority (s)
13020 "Find priority cookie and return priority."
13022 (if (functionp org-get-priority-function)
13023 (funcall org-get-priority-function)
13024 (if (not (string-match org-priority-regexp s))
13025 (* 1000 (- org-lowest-priority org-default-priority))
13026 (* 1000 (- org-lowest-priority
13027 (string-to-char (match-string 2 s))))))))
13031 (defvar org-agenda-archives-mode)
13032 (defvar org-map-continue-from nil
13033 "Position from where mapping should continue.
13034 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
13036 (defvar org-scanner-tags nil
13037 "The current tag list while the tags scanner is running.")
13038 (defvar org-trust-scanner-tags nil
13039 "Should `org-get-tags-at' use the tags for the scanner.
13040 This is for internal dynamical scoping only.
13041 When this is non-nil, the function `org-get-tags-at' will return the value
13042 of `org-scanner-tags' instead of building the list by itself. This
13043 can lead to large speed-ups when the tags scanner is used in a file with
13044 many entries, and when the list of tags is retrieved, for example to
13045 obtain a list of properties. Building the tags list for each entry in such
13046 a file becomes an N^2 operation - but with this variable set, it scales
13049 (defun org-scan-tags (action matcher todo-only &optional start-level)
13050 "Scan headline tags with inheritance and produce output ACTION.
13052 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
13053 or `agenda' to produce an entry list for an agenda view. It can also be
13054 a Lisp form or a function that should be called at each matched headline, in
13055 this case the return value is a list of all return values from these calls.
13057 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
13058 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
13059 only lines with a not-done TODO keyword are included in the output.
13060 This should be the same variable that was scoped into
13061 and set by `org-make-tags-matcher' when it constructed MATCHER.
13063 START-LEVEL can be a string with asterisks, reducing the scope to
13064 headlines matching this string."
13065 (require 'org-agenda)
13066 (let* ((re (concat "^"
13068 ;; Get the correct level to match
13069 (concat "\\*\\{" (number-to-string start-level) "\\} ")
13070 org-outline-regexp)
13072 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
13073 (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
13074 (props (list 'face 'default
13075 'done-face 'org-agenda-done
13076 'undone-face 'default
13077 'mouse-face 'highlight
13078 'org-not-done-regexp org-not-done-regexp
13079 'org-todo-regexp org-todo-regexp
13080 'org-complex-heading-regexp org-complex-heading-regexp
13082 (format "mouse-2 or RET jump to org file %s"
13083 (abbreviate-file-name
13084 (or (buffer-file-name (buffer-base-buffer))
13085 (buffer-name (buffer-base-buffer)))))))
13086 (case-fold-search nil)
13087 (org-map-continue-from nil)
13088 lspos tags tags-list
13089 (tags-alist (list (cons 0 org-file-tags)))
13090 (llast 0) rtn rtn1 level category i txt
13091 todo marker entry priority)
13092 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
13093 (setq action (list 'lambda nil action)))
13095 (goto-char (point-min))
13096 (when (eq action 'sparse-tree)
13098 (org-remove-occur-highlights))
13099 (while (re-search-forward re nil t)
13100 (setq org-map-continue-from nil)
13102 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
13103 tags (if (match-end 4) (org-match-string-no-properties 4)))
13104 (goto-char (setq lspos (match-beginning 0)))
13105 (setq level (org-reduced-level (funcall outline-level))
13106 category (org-get-category))
13107 (setq i llast llast level)
13108 ;; remove tag lists from same and sublevels
13109 (while (>= i level)
13110 (when (setq entry (assoc i tags-alist))
13111 (setq tags-alist (delete entry tags-alist)))
13113 ;; add the next tags
13115 (setq tags (org-split-string tags ":")
13117 (cons (cons level tags) tags-alist)))
13118 ;; compile tags for current headline
13120 (if org-use-tag-inheritance
13121 (apply 'append (mapcar 'cdr (reverse tags-alist)))
13123 org-scanner-tags tags-list)
13124 (when org-use-tag-inheritance
13125 (setcdr (car tags-alist)
13126 (mapcar (lambda (x)
13127 (setq x (copy-sequence x))
13128 (org-add-prop-inherited x))
13129 (cdar tags-alist))))
13130 (when (and tags org-use-tag-inheritance
13131 (or (not (eq t org-use-tag-inheritance))
13132 org-tags-exclude-from-inheritance))
13133 ;; selective inheritance, remove uninherited ones
13134 (setcdr (car tags-alist)
13135 (org-remove-uninherited-tags (cdar tags-alist))))
13138 ;; eval matcher only when the todo condition is OK
13139 (and (or (not todo-only) (member todo org-not-done-keywords))
13140 (let ((case-fold-search t) (org-trust-scanner-tags t))
13143 ;; Call the skipper, but return t if it does not skip,
13144 ;; so that the `and' form continues evaluating
13146 (unless (eq action 'sparse-tree) (org-agenda-skip))
13149 ;; Check if timestamps are deselecting this entry
13150 (or (not todo-only)
13151 (and (member todo org-not-done-keywords)
13152 (or (not org-agenda-tags-todo-honor-ignore-options)
13153 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
13155 ;; select this headline
13157 ((eq action 'sparse-tree)
13158 (and org-highlight-sparse-tree-matches
13159 (org-get-heading) (match-end 0)
13160 (org-highlight-new-match
13161 (match-beginning 1) (match-end 1)))
13162 (org-show-context 'tags-tree))
13163 ((eq action 'agenda)
13164 (setq txt (org-agenda-format-item
13167 (if (eq org-tags-match-list-sublevels 'indented)
13168 (make-string (1- level) ?.) "")
13172 priority (org-get-priority txt))
13174 (setq marker (org-agenda-new-marker))
13175 (org-add-props txt props
13176 'org-marker marker 'org-hd-marker marker 'org-category category
13178 'priority priority 'type "tagsmatch")
13180 ((functionp action)
13181 (setq org-map-continue-from nil)
13183 (setq rtn1 (funcall action))
13185 (t (error "Invalid action")))
13187 ;; if we are to skip sublevels, jump to end of subtree
13188 (unless org-tags-match-list-sublevels
13189 (org-end-of-subtree t)
13190 (backward-char 1))))
13191 ;; Get the correct position from where to continue
13192 (if org-map-continue-from
13193 (goto-char org-map-continue-from)
13194 (and (= (point) lspos) (end-of-line 1)))))
13195 (when (and (eq action 'sparse-tree)
13196 (not org-sparse-tree-open-archived-trees))
13197 (org-hide-archived-subtrees (point-min) (point-max)))
13200 (defun org-remove-uninherited-tags (tags)
13201 "Remove all tags that are not inherited from the list TAGS."
13203 ((eq org-use-tag-inheritance t)
13204 (if org-tags-exclude-from-inheritance
13205 (org-delete-all org-tags-exclude-from-inheritance tags)
13207 ((not org-use-tag-inheritance) nil)
13208 ((stringp org-use-tag-inheritance)
13211 (if (and (string-match org-use-tag-inheritance x)
13212 (not (member x org-tags-exclude-from-inheritance)))
13215 ((listp org-use-tag-inheritance)
13218 (if (member x org-use-tag-inheritance) x nil))
13221 (defun org-match-sparse-tree (&optional todo-only match)
13222 "Create a sparse tree according to tags string MATCH.
13223 MATCH can contain positive and negative selection of tags, like
13224 \"+WORK+URGENT-WITHBOSS\".
13225 If optional argument TODO-ONLY is non-nil, only select lines that are
13228 (org-agenda-prepare-buffers (list (current-buffer)))
13229 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
13231 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
13233 (defvar org-cached-props nil)
13234 (defun org-cached-entry-get (pom property)
13235 (if (or (eq t org-use-property-inheritance)
13236 (and (stringp org-use-property-inheritance)
13237 (string-match org-use-property-inheritance property))
13238 (and (listp org-use-property-inheritance)
13239 (member property org-use-property-inheritance)))
13240 ;; Caching is not possible, check it directly
13241 (org-entry-get pom property 'inherit)
13242 ;; Get all properties, so that we can do complicated checks easily
13243 (cdr (assoc property (or org-cached-props
13244 (setq org-cached-props
13245 (org-entry-properties pom)))))))
13247 (defun org-global-tags-completion-table (&optional files)
13248 "Return the list of all tags in all agenda buffer/files.
13249 Optional FILES argument is a list of files which can be used
13250 instead of the agenda files."
13257 (set-buffer (find-file-noselect file))
13258 (append (org-get-buffer-tags)
13259 (mapcar (lambda (x) (if (stringp (car-safe x))
13260 (list (car-safe x)) nil))
13262 (if (and files (car files))
13264 (org-agenda-files))))))))
13266 (defun org-make-tags-matcher (match)
13267 "Create the TAGS/TODO matcher form for the selection string MATCH.
13269 The variable `todo-only' is scoped dynamically into this function.
13270 It will be set to t if the matcher restricts matching to TODO entries,
13271 otherwise will not be touched.
13273 Returns a cons of the selection string MATCH and the constructed
13274 lisp form implementing the matcher. The matcher is to be evaluated
13275 at an Org entry, with point on the headline, and returns t if the
13276 entry matches the selection string MATCH. The returned lisp form
13277 references two variables with information about the entry, which
13278 must be bound around the form's evaluation: todo, the TODO keyword
13279 at the entry (or nil of none); and tags-list, the list of all tags
13280 at the entry including inherited ones. Additionally, the category
13281 of the entry (if any) must be specified as the text property
13282 'org-category on the headline.
13284 See also `org-scan-tags'.
13286 (declare (special todo-only))
13287 (unless (boundp 'todo-only)
13288 (error "org-make-tags-matcher expects todo-only to be scoped in"))
13290 ;; Get a new match request, with completion
13291 (let ((org-last-tags-completion-table
13292 (org-global-tags-completion-table)))
13293 (setq match (org-completing-read-no-i
13294 "Match: " 'org-tags-completion-function nil nil nil
13295 'org-tags-history))))
13297 ;; Parse the string and create a lisp form
13298 (let ((match0 match)
13299 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
13301 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
13302 orterms term orlist re-p str-p level-p level-op time-p
13303 prop-p pn pv po gv rest)
13304 (if (string-match "/+" match)
13305 ;; match contains also a todo-matching request
13307 (setq tagsmatch (substring match 0 (match-beginning 0))
13308 todomatch (substring match (match-end 0)))
13309 (if (string-match "^!" todomatch)
13310 (setq todo-only t todomatch (substring todomatch 1)))
13311 (if (string-match "^\\s-*$" todomatch)
13312 (setq todomatch nil)))
13313 ;; only matching tags
13314 (setq tagsmatch match todomatch nil))
13316 ;; Make the tags matcher
13317 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
13318 (setq tagsmatcher t)
13319 (setq orterms (org-split-string tagsmatch "|") orlist nil)
13320 (while (setq term (pop orterms))
13321 (while (and (equal (substring term -1) "\\") orterms)
13322 (setq term (concat term "|" (pop orterms)))) ; repair bad split
13323 (while (string-match re term)
13324 (setq rest (substring term (match-end 0))
13325 minus (and (match-end 1)
13326 (equal (match-string 1 term) "-"))
13327 tag (save-match-data (replace-regexp-in-string
13329 (match-string 2 term)))
13330 re-p (equal (string-to-char tag) ?{)
13331 level-p (match-end 4)
13332 prop-p (match-end 5)
13334 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
13336 (setq level-op (org-op-to-function (match-string 3 term)))
13337 `(,level-op level ,(string-to-number
13338 (match-string 4 term))))
13340 (setq pn (match-string 5 term)
13341 po (match-string 6 term)
13342 pv (match-string 7 term)
13343 re-p (equal (string-to-char pv) ?{)
13344 str-p (equal (string-to-char pv) ?\")
13345 time-p (save-match-data
13346 (string-match "^\"[[<].*[]>]\"$" pv))
13347 pv (if (or re-p str-p) (substring pv 1 -1) pv))
13348 (if time-p (setq pv (org-matcher-time pv)))
13349 (setq po (org-op-to-function po (if time-p 'time str-p)))
13351 ((equal pn "CATEGORY")
13352 (setq gv '(get-text-property (point) 'org-category)))
13356 (setq gv `(org-cached-entry-get nil ,pn))))
13359 `(not (string-match ,pv (or ,gv "")))
13360 `(string-match ,pv (or ,gv "")))
13362 `(,po (or ,gv "") ,pv)
13363 `(,po (string-to-number (or ,gv ""))
13364 ,(string-to-number pv) ))))
13365 (t `(member ,tag tags-list)))
13366 mm (if minus (list 'not mm) mm)
13368 (push mm tagsmatcher))
13369 (push (if (> (length tagsmatcher) 1)
13370 (cons 'and tagsmatcher)
13373 (setq tagsmatcher nil))
13374 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
13376 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
13377 ;; Make the todo matcher
13378 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
13379 (setq todomatcher t)
13380 (setq orterms (org-split-string todomatch "|") orlist nil)
13381 (while (setq term (pop orterms))
13382 (while (string-match re term)
13383 (setq minus (and (match-end 1)
13384 (equal (match-string 1 term) "-"))
13385 kwd (match-string 2 term)
13386 re-p (equal (string-to-char kwd) ?{)
13387 term (substring term (match-end 0))
13389 `(string-match ,(substring kwd 1 -1) todo)
13390 (list 'equal 'todo kwd))
13391 mm (if minus (list 'not mm) mm))
13392 (push mm todomatcher))
13393 (push (if (> (length todomatcher) 1)
13394 (cons 'and todomatcher)
13397 (setq todomatcher nil))
13398 (setq todomatcher (if (> (length orlist) 1)
13399 (cons 'or orlist) (car orlist))))
13401 ;; Return the string and lisp forms of the matcher
13402 (setq matcher (if todomatcher
13403 (list 'and tagsmatcher todomatcher)
13406 (setq matcher (list 'and '(member todo org-not-done-keywords)
13408 (cons match0 matcher)))
13410 (defun org-op-to-function (op &optional stringp)
13411 "Turn an operator into the appropriate function."
13414 ((equal op "<" ) '(< string< org-time<))
13415 ((equal op ">" ) '(> org-string> org-time>))
13416 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
13417 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
13418 ((member op '("=" "==")) '(= string= org-time=))
13419 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
13420 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
13422 (defun org<> (a b) (not (= a b)))
13423 (defun org-string<= (a b) (or (string= a b) (string< a b)))
13424 (defun org-string>= (a b) (not (string< a b)))
13425 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
13426 (defun org-string<> (a b) (not (string= a b)))
13427 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
13428 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
13429 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
13430 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
13431 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
13432 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
13434 "Convert S to a floating point time.
13435 If S is already a number, just return it. If it is a string, parse
13436 it as a time string and apply `float-time' to it. If S is nil, just return 0."
13440 (condition-case nil
13441 (float-time (apply 'encode-time (org-parse-time-string s)))
13445 (defun org-time-today ()
13446 "Time in seconds today at 0:00.
13447 Returns the float number of seconds since the beginning of the
13448 epoch to the beginning of today (00:00)."
13449 (float-time (apply 'encode-time
13450 (append '(0 0 0) (nthcdr 3 (decode-time))))))
13452 (defun org-matcher-time (s)
13453 "Interpret a time comparison value."
13456 ((string= s "<now>") (float-time))
13457 ((string= s "<today>") (org-time-today))
13458 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
13459 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
13460 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
13461 (+ (org-time-today)
13462 (* (string-to-number (match-string 1 s))
13463 (cdr (assoc (match-string 2 s)
13464 '(("d" . 86400.0) ("w" . 604800.0)
13465 ("m" . 2678400.0) ("y" . 31557600.0)))))))
13468 (defun org-match-any-p (re list)
13469 "Does re match any element of list?"
13470 (setq list (mapcar (lambda (x) (string-match re x)) list))
13473 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
13474 (defvar org-tags-overlay (make-overlay 1 1))
13475 (org-detach-overlay org-tags-overlay)
13477 (defun org-get-local-tags-at (&optional pos)
13478 "Get a list of tags defined in the current headline."
13479 (org-get-tags-at pos 'local))
13481 (defun org-get-local-tags ()
13482 "Get a list of tags defined in the current headline."
13483 (org-get-tags-at nil 'local))
13485 (defun org-get-tags-at (&optional pos local)
13486 "Get a list of all headline tags applicable at POS.
13487 POS defaults to point. If tags are inherited, the list contains
13488 the targets in the same sequence as the headlines appear, i.e.
13489 the tags of the current headline come last.
13490 When LOCAL is non-nil, only return tags from the current headline,
13491 ignore inherited ones."
13493 (if (and org-trust-scanner-tags
13494 (or (not pos) (equal pos (point)))
13497 (let (tags ltags lastpos parent)
13501 (goto-char (or pos (point)))
13504 (condition-case nil
13506 (org-back-to-heading t)
13507 (while (not (equal lastpos (point)))
13508 (setq lastpos (point))
13510 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
13511 (setq ltags (org-split-string
13512 (org-match-string-no-properties 1) ":"))
13514 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
13517 (org-remove-uninherited-tags ltags)
13520 (or org-use-tag-inheritance (throw 'done t))
13521 (if local (throw 'done t))
13522 (or (org-up-heading-safe) (error nil))
13527 (append (org-remove-uninherited-tags org-file-tags) tags))))))
13529 (defun org-add-prop-inherited (s)
13530 (add-text-properties 0 (length s) '(inherited t) s)
13533 (defun org-toggle-tag (tag &optional onoff)
13534 "Toggle the tag TAG for the current line.
13535 If ONOFF is `on' or `off', don't toggle but set to this state."
13538 (org-back-to-heading t)
13539 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
13542 (setq current (match-string 1))
13543 (replace-match ""))
13545 (setq current (nreverse (org-split-string current ":")))
13549 (or (member tag current) (push tag current)))
13551 (or (not (member tag current)) (setq current (delete tag current))))
13552 (t (if (member tag current)
13553 (setq current (delete tag current))
13555 (push tag current))))
13559 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
13560 (org-set-tags nil t))
13561 (delete-horizontal-space))
13562 (run-hooks 'org-after-tags-change-hook))
13565 (defun org-align-tags-here (to-col)
13566 ;; Assumes that this is a headline
13567 (let ((pos (point)) (col (current-column)) ncol tags-l p)
13568 (beginning-of-line 1)
13569 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13570 (< pos (match-beginning 2)))
13572 (setq tags-l (- (match-end 2) (match-beginning 2)))
13573 (goto-char (match-beginning 1))
13575 (delete-region (point) (1+ (match-beginning 2)))
13576 (setq ncol (max (current-column)
13580 (- (abs to-col) tags-l))))
13582 (insert (make-string (- ncol (current-column)) ?\ ))
13583 (setq ncol (current-column))
13584 (when indent-tabs-mode (tabify p (point-at-eol)))
13585 (org-move-to-column (min ncol col) t))
13588 (defun org-set-tags-command (&optional arg just-align)
13589 "Call the set-tags command for the current entry."
13591 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
13592 (org-set-tags arg just-align)
13594 (org-back-to-heading t)
13595 (org-set-tags arg just-align))))
13597 (defun org-set-tags-to (data)
13598 "Set the tags of the current entry to DATA, replacing the current tags.
13599 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
13600 If DATA is nil or the empty string, any tags will be removed."
13601 (interactive "sTags: ")
13605 ((equal data "") "")
13607 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
13610 (concat ":" (mapconcat 'identity data ":") ":"))))
13613 (org-back-to-heading t)
13614 (when (looking-at org-complex-heading-regexp)
13617 (goto-char (match-beginning 5))
13619 (delete-region (point) (point-at-eol))
13620 (org-set-tags nil 'align))
13621 (goto-char (point-at-eol))
13623 (org-set-tags nil 'align)))
13624 (beginning-of-line 1)
13625 (if (looking-at ".*?\\([ \t]+\\)$")
13626 (delete-region (match-beginning 1) (match-end 1))))))
13628 (defun org-align-all-tags ()
13629 "Align the tags i all headings."
13632 (or (ignore-errors (org-back-to-heading t))
13633 (outline-next-heading))
13634 (if (org-at-heading-p)
13636 (message "No headings"))))
13638 (defvar org-indent-indentation-per-level)
13639 (defun org-set-tags (&optional arg just-align)
13640 "Set the tags for the current headline.
13641 With prefix ARG, realign all tags in headings in the current buffer."
13643 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13644 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
13645 'region-start-level 'region))
13646 org-loop-over-headlines-in-active-region)
13648 ;; We don't use ARG and JUST-ALIGN here these args are not
13649 ;; useful when looping over headlines
13651 org-loop-over-headlines-in-active-region
13652 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
13653 (let* ((re org-outline-regexp-bol)
13654 (current (unless arg (org-get-tags-string)))
13655 (col (current-column))
13656 (org-setting-tags t)
13657 table current-tags inherited-tags ; computed below when needed
13658 tags p0 c0 c1 rpl di tc level)
13661 (goto-char (point-min))
13662 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
13663 (while (re-search-forward re nil t)
13664 (org-set-tags nil t)
13666 (message "All tags realigned to column %d" org-tags-column))
13668 (setq tags current)
13669 ;; Get a new set of tags from the user
13671 (setq table (append org-tag-persistent-alist
13672 (or org-tag-alist (org-get-buffer-tags))
13674 org-complete-tags-always-offer-all-agenda-tags
13675 (org-global-tags-completion-table
13676 (org-agenda-files))))
13677 org-last-tags-completion-table table
13678 current-tags (org-split-string current ":")
13679 inherited-tags (nreverse
13680 (nthcdr (length current-tags)
13681 (nreverse (org-get-tags-at))))
13683 (if (or (eq t org-use-fast-tag-selection)
13684 (and org-use-fast-tag-selection
13685 (delq nil (mapcar 'cdr table))))
13686 (org-fast-tag-selection
13687 current-tags inherited-tags table
13688 (if org-fast-tag-selection-include-todo
13689 org-todo-key-alist))
13690 (let ((org-add-colon-after-tag-completion (< 1 (length table))))
13692 (org-icompleting-read "Tags: "
13693 'org-tags-completion-function
13694 nil nil current 'org-tags-history))))))
13695 (while (string-match "[-+&]+" tags)
13696 ;; No boolean logic, just a list
13697 (setq tags (replace-match ":" t t tags))))
13699 (setq tags (replace-regexp-in-string "[,]" ":" tags))
13701 (if org-tags-sort-function
13702 (setq tags (mapconcat 'identity
13703 (sort (org-split-string
13704 tags (org-re "[^[:alnum:]_@#%]+"))
13705 org-tags-sort-function) ":")))
13707 (if (string-match "\\`[\t ]*\\'" tags)
13709 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
13710 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
13712 ;; Insert new tags at the correct column
13713 (beginning-of-line 1)
13714 (setq level (or (and (looking-at org-outline-regexp)
13715 (- (match-end 0) (point) 1))
13718 ((and (equal current "") (equal tags "")))
13719 ((re-search-forward
13720 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
13722 (if (equal tags "")
13724 (goto-char (match-beginning 0))
13725 (setq c0 (current-column)
13726 ;; compute offset for the case of org-indent-mode active
13727 di (if org-indent-mode
13728 (* (1- org-indent-indentation-per-level) (1- level))
13730 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
13731 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
13732 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
13733 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
13734 (replace-match rpl t t)
13735 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
13737 (t (error "Tags alignment failed")))
13738 (org-move-to-column col)
13740 (run-hooks 'org-after-tags-change-hook))))))
13742 (defun org-change-tag-in-region (beg end tag off)
13743 "Add or remove TAG for each entry in the region.
13744 This works in the agenda, and also in an org-mode buffer."
13746 (list (region-beginning) (region-end)
13747 (let ((org-last-tags-completion-table
13748 (if (derived-mode-p 'org-mode)
13749 (org-get-buffer-tags)
13750 (org-global-tags-completion-table))))
13751 (org-icompleting-read
13752 "Tag: " 'org-tags-completion-function nil nil nil
13753 'org-tags-history))
13755 (message "[s]et or
[r]emove? ")
13756 (equal (read-char-exclusive) ?r))))
13757 (if (fboundp 'deactivate-mark) (deactivate-mark))
13758 (let ((agendap (equal major-mode 'org-agenda-mode))
13759 l1 l2 m buf pos newhead (cnt 0))
13761 (setq l2 (1- (org-current-line)))
13763 (setq l1 (org-current-line))
13764 (loop for l from l1 to l2 do
13766 (setq m (get-text-property (point) 'org-hd-marker))
13767 (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
13769 (setq buf (if agendap (marker-buffer m) (current-buffer))
13770 pos (if agendap m (point)))
13771 (with-current-buffer buf
13775 (setq cnt (1+ cnt))
13776 (org-toggle-tag tag (if off 'off 'on))
13777 (setq newhead (org-get-heading)))))
13778 (and agendap (org-agenda-change-all-lines newhead m))))
13779 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
13781 (defun org-tags-completion-function (string predicate &optional flag)
13782 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
13783 (confirm (lambda (x) (stringp (car x)))))
13784 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
13785 (setq s1 (match-string 1 string)
13786 s2 (match-string 2 string))
13787 (setq s1 "" s2 string))
13791 (setq rtn (try-completion s2 ctable confirm))
13794 (concat s1 s2 (substring rtn (length s2))
13795 (if (and org-add-colon-after-tag-completion
13796 (assoc rtn ctable))
13801 (all-completions s2 ctable confirm)
13805 (assoc s2 ctable)))
13808 (defun org-fast-tag-insert (kwd tags face &optional end)
13809 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
13810 (insert (format "%-12s" (concat kwd ":"))
13811 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
13814 (defun org-fast-tag-show-exit (flag)
13817 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
13818 (replace-match ""))
13821 (org-move-to-column (- (window-width) 19) t)
13822 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
13824 (defun org-set-current-tags-overlay (current prefix)
13825 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
13826 (if (featurep 'xemacs)
13827 (org-overlay-display org-tags-overlay (concat prefix s)
13828 'secondary-selection)
13829 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
13830 (org-overlay-display org-tags-overlay (concat prefix s)))))
13832 (defvar org-last-tag-selection-key nil)
13833 (defun org-fast-tag-selection (current inherited table &optional todo-table)
13834 "Fast tag selection with single keys.
13835 CURRENT is the current list of tags in the headline, INHERITED is the
13836 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
13837 possibly with grouping information. TODO-TABLE is a similar table with
13838 TODO keywords, should these have keys assigned to them.
13839 If the keys are nil, a-z are automatically assigned.
13840 Returns the new tags string, or nil to not change the current settings."
13841 (let* ((fulltable (append table todo-table))
13842 (maxlen (apply 'max (mapcar
13844 (if (stringp (car x)) (string-width (car x)) 0))
13846 (buf (current-buffer))
13847 (expert (eq org-fast-tag-selection-single-key 'expert))
13849 (fwidth (+ maxlen 3 1 3))
13850 (ncol (/ (- (window-width) 4) fwidth))
13853 tg cnt e c char c1 c2 ntable tbl rtn
13854 ov-start ov-end ov-prefix
13855 (exit-after-next org-fast-tag-selection-single-key)
13856 (done-keywords org-done-keywords)
13859 (beginning-of-line 1)
13861 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13862 (setq ov-start (match-beginning 1)
13863 ov-end (match-end 1)
13865 (setq ov-start (1- (point-at-eol))
13866 ov-end (1+ ov-start))
13867 (skip-chars-forward "^\n\r")
13870 (buffer-substring (1- (point)) (point))
13871 (if (> (current-column) org-tags-column)
13873 (make-string (- org-tags-column (current-column)) ?\ ))))))
13874 (move-overlay org-tags-overlay ov-start ov-end)
13875 (save-window-excursion
13877 (set-buffer (get-buffer-create " *Org tags*"))
13878 (delete-other-windows)
13879 (split-window-vertically)
13880 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
13882 (org-set-local 'org-done-keywords done-keywords)
13883 (org-fast-tag-insert "Inherited" inherited i-face "\n")
13884 (org-fast-tag-insert "Current" current c-face "\n\n")
13885 (org-fast-tag-show-exit exit-after-next)
13886 (org-set-current-tags-overlay current ov-prefix)
13887 (setq tbl fulltable char ?a cnt 0)
13888 (while (setq e (pop tbl))
13890 ((equal (car e) :startgroup)
13891 (push '() groups) (setq ingroup t)
13892 (when (not (= cnt 0))
13895 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
13896 ((equal (car e) :endgroup)
13897 (setq ingroup nil cnt 0)
13898 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
13899 ((equal e '(:newline))
13900 (when (not (= cnt 0))
13904 (while (equal (car tbl) '(:newline))
13906 (setq tbl (cdr tbl)))))
13908 (setq tg (copy-sequence (car e)) c2 nil)
13911 ;; automatically assign a character.
13912 (setq c1 (string-to-char
13913 (downcase (substring
13914 tg (if (= (string-to-char tg) ?@) 1 0)))))
13915 (if (or (rassoc c1 ntable) (rassoc c1 table))
13916 (while (or (rassoc char ntable) (rassoc char table))
13917 (setq char (1+ char)))
13919 (setq c (or c2 char)))
13920 (if ingroup (push tg (car groups)))
13921 (setq tg (org-add-props tg nil 'face
13923 ((not (assoc tg table))
13924 (org-get-todo-face tg))
13925 ((member tg current) c-face)
13926 ((member tg inherited) i-face))))
13927 (if (and (= cnt 0) (not ingroup)) (insert " "))
13928 (insert "[" c "] " tg (make-string
13929 (- fwidth 4 (length tg)) ?\ ))
13930 (push (cons tg c) ntable)
13931 (when (= (setq cnt (1+ cnt)) ncol)
13933 (if ingroup (insert " "))
13935 (setq ntable (nreverse ntable))
13937 (goto-char (point-min))
13938 (if (not expert) (org-fit-window-to-buffer))
13942 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
13943 (if (not groups) "no " "")
13944 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
13945 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13946 (setq org-last-tag-selection-key c)
13948 ((= c ?\r) (throw 'exit t))
13950 (setq groups (not groups))
13951 (goto-char (point-min))
13952 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
13955 (org-fast-tag-show-exit
13956 (setq exit-after-next (not exit-after-next)))
13958 (delete-other-windows)
13959 (set-window-buffer (split-window-vertically) " *Org tags*")
13960 (org-switch-to-buffer-other-window " *Org tags*")
13961 (org-fit-window-to-buffer)))
13963 (and (= c ?q) (not (rassoc c ntable))))
13964 (org-detach-overlay org-tags-overlay)
13965 (setq quit-flag t))
13968 (if exit-after-next (setq exit-after-next 'now)))
13970 (condition-case nil
13971 (setq tg (org-icompleting-read
13974 (with-current-buffer buf
13975 (org-get-buffer-tags)))))
13976 (quit (setq tg "")))
13977 (when (string-match "\\S-" tg)
13978 (add-to-list 'buffer-tags (list tg))
13979 (if (member tg current)
13980 (setq current (delete tg current))
13981 (push tg current)))
13982 (if exit-after-next (setq exit-after-next 'now)))
13983 ((setq e (rassoc c todo-table) tg (car e))
13984 (with-current-buffer buf
13985 (save-excursion (org-todo tg)))
13986 (if exit-after-next (setq exit-after-next 'now)))
13987 ((setq e (rassoc c ntable) tg (car e))
13988 (if (member tg current)
13989 (setq current (delete tg current))
13990 (loop for g in groups do
13993 (setq current (delete x current)))
13996 (if exit-after-next (setq exit-after-next 'now))))
13998 ;; Create a sorted list
14002 (assoc b (cdr (memq (assoc a ntable) ntable))))))
14003 (if (eq exit-after-next 'now) (throw 'exit t))
14004 (goto-char (point-min))
14005 (beginning-of-line 2)
14006 (delete-region (point) (point-at-eol))
14007 (org-fast-tag-insert "Current" current c-face)
14008 (org-set-current-tags-overlay current ov-prefix)
14009 (while (re-search-forward
14010 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
14011 (setq tg (match-string 1))
14012 (add-text-properties
14013 (match-beginning 1) (match-end 1)
14016 ((member tg current) c-face)
14017 ((member tg inherited) i-face)
14018 (t (get-text-property (match-beginning 1) 'face))))))
14019 (goto-char (point-min)))))
14020 (org-detach-overlay org-tags-overlay)
14022 (mapconcat 'identity current ":")
14025 (defun org-get-tags-string ()
14026 "Get the TAGS string in the current headline."
14027 (unless (org-at-heading-p t)
14028 (error "Not on a heading"))
14030 (beginning-of-line 1)
14031 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14032 (org-match-string-no-properties 1)
14035 (defun org-get-tags ()
14036 "Get the list of tags specified in the current headline."
14037 (org-split-string (org-get-tags-string) ":"))
14039 (defun org-get-buffer-tags ()
14040 "Get a table of all tags used in the buffer, for completion."
14043 (goto-char (point-min))
14044 (while (re-search-forward
14045 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
14046 (when (equal (char-after (point-at-bol 0)) ?*)
14047 (mapc (lambda (x) (add-to-list 'tags x))
14048 (org-split-string (org-match-string-no-properties 1) ":")))))
14049 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
14050 (mapcar 'list tags)))
14052 ;;;; The mapping API
14054 (defun org-map-entries (func &optional match scope &rest skip)
14055 "Call FUNC at each headline selected by MATCH in SCOPE.
14057 FUNC is a function or a lisp form. The function will be called without
14058 arguments, with the cursor positioned at the beginning of the headline.
14059 The return values of all calls to the function will be collected and
14060 returned as a list.
14062 The call to FUNC will be wrapped into a save-excursion form, so FUNC
14063 does not need to preserve point. After evaluation, the cursor will be
14064 moved to the end of the line (presumably of the headline of the
14065 processed entry) and search continues from there. Under some
14066 circumstances, this may not produce the wanted results. For example,
14067 if you have removed (e.g. archived) the current (sub)tree it could
14068 mean that the next entry will be skipped entirely. In such cases, you
14069 can specify the position from where search should continue by making
14070 FUNC set the variable `org-map-continue-from' to the desired buffer
14073 MATCH is a tags/property/todo match as it is used in the agenda tags view.
14074 Only headlines that are matched by this query will be considered during
14075 the iteration. When MATCH is nil or t, all headlines will be
14076 visited by the iteration.
14078 SCOPE determines the scope of this command. It can be any of:
14080 nil The current buffer, respecting the restriction if any
14081 tree The subtree started with the entry at point
14082 region The entries within the active region, if any
14084 The entries within the active region, but only those at
14085 the same level than the first one.
14086 file The current buffer, without restriction
14088 The current buffer, and any archives associated with it
14089 agenda All agenda files
14090 agenda-with-archives
14091 All agenda files with any archive files associated with them
14093 If this is a list, all files in the list will be scanned
14095 The remaining args are treated as settings for the skipping facilities of
14096 the scanner. The following items can be given here:
14098 archive skip trees with the archive tag.
14099 comment skip trees with the COMMENT keyword
14100 function or Emacs Lisp form:
14101 will be used as value for `org-agenda-skip-function', so whenever
14102 the function returns t, FUNC will not be called for that
14103 entry and search will continue from the point where the
14104 function leaves it.
14106 If your function needs to retrieve the tags including inherited tags
14107 at the *current* entry, you can use the value of the variable
14108 `org-scanner-tags' which will be much faster than getting the value
14109 with `org-get-tags-at'. If your function gets properties with
14110 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
14111 to t around the call to `org-entry-properties' to get the same speedup.
14112 Note that if your function moves around to retrieve tags and properties at
14113 a *different* entry, you cannot use these techniques."
14114 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
14115 (not (org-region-active-p)))
14116 (let* ((org-agenda-archives-mode nil) ; just to make sure
14117 (org-agenda-skip-archived-trees (memq 'archive skip))
14118 (org-agenda-skip-comment-trees (memq 'comment skip))
14119 (org-agenda-skip-function
14120 (car (org-delete-all '(comment archive) skip)))
14121 (org-tags-match-list-sublevels t)
14122 (start-level (eq scope 'region-start-level))
14124 org-todo-keywords-for-agenda
14125 org-done-keywords-for-agenda
14126 org-todo-keyword-alist-for-agenda
14127 org-drawers-for-agenda
14128 org-tag-alist-for-agenda
14132 ((eq match t) (setq matcher t))
14133 ((eq match nil) (setq matcher t))
14134 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
14138 (cond ((eq scope 'tree)
14139 (org-back-to-heading t)
14140 (org-narrow-to-subtree)
14142 ((and (or (eq scope 'region) (eq scope 'region-start-level))
14143 (org-region-active-p))
14144 ;; If needed, set start-level to a string like "2"
14147 (goto-char (region-beginning))
14148 (unless (org-at-heading-p) (outline-next-heading))
14149 (setq start-level (org-current-level))))
14150 (narrow-to-region (region-beginning)
14152 (goto-char (region-end))
14153 (unless (and (bolp) (org-at-heading-p))
14154 (outline-next-heading))
14160 (org-agenda-prepare-buffers
14161 (list (buffer-file-name (current-buffer))))
14162 (setq res (org-scan-tags func matcher todo-only start-level)))
14163 ;; Get the right scope
14165 ((and scope (listp scope) (symbolp (car scope)))
14166 (setq scope (eval scope)))
14167 ((eq scope 'agenda)
14168 (setq scope (org-agenda-files t)))
14169 ((eq scope 'agenda-with-archives)
14170 (setq scope (org-agenda-files t))
14171 (setq scope (org-add-archive-files scope)))
14173 (setq scope (list (buffer-file-name))))
14174 ((eq scope 'file-with-archives)
14175 (setq scope (org-add-archive-files (list (buffer-file-name))))))
14176 (org-agenda-prepare-buffers scope)
14177 (while (setq file (pop scope))
14178 (with-current-buffer (org-find-base-buffer-visiting file)
14182 (goto-char (point-min))
14183 (setq res (append res (org-scan-tags func matcher todo-only))))))))))
14188 ;;; Setting and retrieving properties
14190 (defconst org-special-properties
14191 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
14192 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
14193 "The special properties valid in Org-mode.
14195 These are properties that are not defined in the property drawer,
14196 but in some other way.")
14198 (defconst org-default-properties
14199 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
14200 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
14201 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
14202 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
14203 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
14204 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
14205 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
14206 "Some properties that are used by Org-mode for various purposes.
14207 Being in this list makes sure that they are offered for completion.")
14209 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
14210 "Regular expression matching the first line of a property drawer.")
14212 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
14213 "Regular expression matching the last line of a property drawer.")
14215 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
14216 "Regular expression matching the first line of a property drawer.")
14218 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
14219 "Regular expression matching the first line of a property drawer.")
14221 (defconst org-property-drawer-re
14222 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
14223 org-property-end-re "\\)\n?")
14224 "Matches an entire property drawer.")
14226 (defconst org-clock-drawer-re
14227 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
14228 org-property-end-re "\\)\n?")
14229 "Matches an entire clock drawer.")
14231 (defsubst org-re-property (property)
14232 "Return a regexp matching a PROPERTY line.
14233 Match group 1 will be set to the value."
14234 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
14236 (defsubst org-re-property-keyword (property)
14237 "Return a regexp matching a PROPERTY line, possibly with no
14238 value for the property."
14239 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
14241 (defun org-property-action ()
14242 "Do an action on properties."
14245 (org-at-property-p)
14246 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
14247 (setq c (read-char-exclusive))
14250 (call-interactively 'org-set-property))
14252 (call-interactively 'org-delete-property))
14254 (call-interactively 'org-delete-property-globally))
14256 (call-interactively 'org-compute-property-at-point))
14257 (t (error "No such property action %c" c)))))
14259 (defun org-inc-effort ()
14260 "Increment the value of the effort property in the current entry."
14262 (org-set-effort nil t))
14264 (defun org-set-effort (&optional value increment)
14265 "Set the effort property of the current entry.
14266 With numerical prefix arg, use the nth allowed value, 0 stands for the
14267 10th allowed value.
14269 When INCREMENT is non-nil, set the property to the next allowed value."
14271 (if (equal value 0) (setq value 10))
14272 (let* ((completion-ignore-case t)
14273 (prop org-effort-property)
14274 (cur (org-entry-get nil prop))
14275 (allowed (org-property-get-allowed-values nil prop 'table))
14276 (existing (mapcar 'list (org-property-values prop)))
14279 ((stringp value) value)
14280 ((and allowed (integerp value))
14281 (or (car (nth (1- value) allowed))
14282 (car (org-last allowed))))
14283 ((and allowed increment)
14284 (or (caadr (member (list cur) allowed))
14285 (error "Allowed effort values are not set")))
14287 (message "Select 1-9,0, [RET%s]: %s"
14288 (if cur (concat "=" cur) "")
14289 (mapconcat 'car allowed " "))
14290 (setq rpl (read-char-exclusive))
14291 (if (equal rpl ?\r)
14293 (setq rpl (- rpl ?0))
14294 (if (equal rpl 0) (setq rpl 10))
14295 (if (and (> rpl 0) (<= rpl (length allowed)))
14296 (car (nth (1- rpl) allowed))
14297 (org-completing-read "Effort: " allowed nil))))
14299 (let (org-completion-use-ido org-completion-use-iswitchb)
14300 (org-completing-read
14301 (concat "Effort " (if (and cur (string-match "\\S-" cur))
14302 (concat "[" cur "]") "")
14304 existing nil nil "" nil cur))))))
14305 (unless (equal (org-entry-get nil prop) val)
14306 (org-entry-put nil prop val))
14308 (org-back-to-heading t)
14309 (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
14310 (message "%s is now %s" prop val)))
14312 (defun org-at-property-p ()
14313 "Is cursor inside a property drawer?"
14315 (beginning-of-line 1)
14316 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
14317 (save-match-data ;; Used by calling procedures
14319 (range (unless (org-before-first-heading-p)
14320 (org-get-property-block))))
14321 (and range (<= (car range) p) (< p (cdr range))))))))
14323 (defun org-get-property-block (&optional beg end force)
14324 "Return the (beg . end) range of the body of the property drawer.
14325 BEG and END are the beginning and end of the current subtree, or of
14326 the part before the first headline. If they are not given, they will
14327 be found. If the drawer does not exist and FORCE is non-nil, create
14331 (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
14332 (progn (org-back-to-heading t) (point))))
14333 (end (or end (and (not (outline-next-heading)) (point-max))
14336 (if (re-search-forward org-property-start-re end t)
14337 (setq beg (1+ (match-end 0)))
14340 (org-insert-property-drawer)
14341 (setq end (progn (outline-next-heading) (point))))
14344 (if (re-search-forward org-property-start-re end t)
14345 (setq beg (1+ (match-end 0)))))
14346 (if (re-search-forward org-property-end-re end t)
14347 (setq end (match-beginning 0))
14348 (or force (throw 'exit nil))
14352 (insert ":END:\n"))
14355 (defun org-entry-properties (&optional pom which specific)
14356 "Get all properties of the entry at point-or-marker POM.
14357 This includes the TODO keyword, the tags, time strings for deadline,
14358 scheduled, and clocking, and any additional properties defined in the
14359 entry. The return value is an alist, keys may occur multiple times
14360 if the property key was used several times.
14361 POM may also be nil, in which case the current entry is used.
14362 If WHICH is nil or `all', get all properties. If WHICH is
14363 `special' or `standard', only get that subclass. If WHICH
14364 is a string only get exactly this property. SPECIFIC can be a string, the
14365 specific property we are interested in. Specifying it can speed
14366 things up because then unnecessary parsing is avoided."
14367 (setq which (or which 'all))
14368 (org-with-point-at pom
14369 (let ((clockstr (substring org-clock-string 0 -1))
14370 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
14371 (case-fold-search nil)
14372 beg end range props sum-props key key1 value string clocksum clocksumt)
14374 (when (condition-case nil
14375 (and (derived-mode-p 'org-mode) (org-back-to-heading t))
14378 (setq sum-props (get-text-property (point) 'org-summaries))
14379 (setq clocksum (get-text-property (point) :org-clock-minutes)
14380 clocksumt (get-text-property (point) :org-clock-minutes-today))
14381 (outline-next-heading)
14383 (when (memq which '(all special))
14384 ;; Get the special properties, like TODO and tags
14386 (when (and (or (not specific) (string= specific "TODO"))
14387 (looking-at org-todo-line-regexp) (match-end 2))
14388 (push (cons "TODO" (org-match-string-no-properties 2)) props))
14389 (when (and (or (not specific) (string= specific "PRIORITY"))
14390 (looking-at org-priority-regexp))
14391 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
14392 (when (or (not specific) (string= specific "FILE"))
14393 (push (cons "FILE" buffer-file-name) props))
14394 (when (and (or (not specific) (string= specific "TAGS"))
14395 (setq value (org-get-tags-string))
14396 (string-match "\\S-" value))
14397 (push (cons "TAGS" value) props))
14398 (when (and (or (not specific) (string= specific "ALLTAGS"))
14399 (setq value (org-get-tags-at)))
14400 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
14403 (when (or (not specific) (string= specific "BLOCKED"))
14404 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
14405 (when (or (not specific)
14407 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
14408 "TIMESTAMP" "TIMESTAMP_IA")))
14410 (while (re-search-forward org-maybe-keyword-time-regexp end t)
14411 (setq key (if (match-end 1)
14412 (substring (org-match-string-no-properties 1)
14414 string (if (equal key clockstr)
14416 (buffer-substring-no-properties
14417 (match-beginning 3) (goto-char
14419 (substring (org-match-string-no-properties 3)
14421 ;; Get the correct property name from the key. This is
14422 ;; necessary if the user has configured time keywords.
14423 (setq key1 (concat key ":"))
14427 (if (= (char-after (match-beginning 3)) ?\[)
14428 "TIMESTAMP_IA" "TIMESTAMP")))
14429 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
14430 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
14431 ((equal key1 org-closed-string) (setq key "CLOSED"))
14432 ((equal key1 org-clock-string) (setq key "CLOCK")))
14433 (if (and specific (equal key specific) (not (equal key "CLOCK")))
14435 (push (cons key string) props)
14436 ;; no need to search further if match is found
14438 (when (or (equal key "CLOCK") (not (assoc key props)))
14439 (push (cons key string) props)))))))
14441 (when (memq which '(all standard))
14442 ;; Get the standard properties, like :PROP: ...
14443 (setq range (org-get-property-block beg end))
14445 (goto-char (car range))
14446 (while (re-search-forward
14447 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
14449 (setq key (org-match-string-no-properties 1)
14450 value (org-trim (or (org-match-string-no-properties 2) "")))
14451 (unless (member key excluded)
14452 (push (cons key (or value "")) props)))))
14454 (push (cons "CLOCKSUM"
14455 (org-columns-number-to-string (/ (float clocksum) 60.)
14459 (push (cons "CLOCKSUM_T"
14460 (org-columns-number-to-string (/ (float clocksumt) 60.)
14463 (unless (assoc "CATEGORY" props)
14464 (push (cons "CATEGORY" (org-get-category)) props))
14465 (append sum-props (nreverse props)))))))
14467 (defun org-entry-get (pom property &optional inherit literal-nil)
14468 "Get value of PROPERTY for entry or content at point-or-marker POM.
14469 If INHERIT is non-nil and the entry does not have the property,
14470 then also check higher levels of the hierarchy.
14471 If INHERIT is the symbol `selective', use inheritance only if the setting
14472 in `org-use-property-inheritance' selects PROPERTY for inheritance.
14473 If the property is present but empty, the return value is the empty string.
14474 If the property is not present at all, nil is returned.
14476 If LITERAL-NIL is set, return the string value \"nil\" as a string,
14477 do not interpret it as the list atom nil. This is used for inheritance
14478 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
14479 (org-with-point-at pom
14480 (if (and inherit (if (eq inherit 'selective)
14481 (org-property-inherit-p property)
14483 (org-entry-get-with-inheritance property literal-nil)
14484 (if (member property org-special-properties)
14485 ;; We need a special property. Use `org-entry-properties' to
14486 ;; retrieve it, but specify the wanted property
14487 (cdr (assoc property (org-entry-properties nil 'special property)))
14488 (let ((range (org-get-property-block)))
14489 (when (and range (not (eq (car range) (cdr range))))
14490 (let* ((props (list (or (assoc property org-file-properties)
14491 (assoc property org-global-properties)
14492 (assoc property org-global-properties-fixed))))
14494 (when (re-search-forward
14495 (org-re-property key) (cdr range) t)
14497 (org-update-property-plist
14500 (org-match-string-no-properties 1) "")
14503 (goto-char (car range))
14504 (funcall ap property)
14505 (goto-char (car range))
14506 (while (funcall ap (concat property "+")))
14507 (setq val (cdr (assoc property props)))
14508 (when val (if literal-nil val (org-not-nil val))))))))))
14510 (defun org-property-or-variable-value (var &optional inherit)
14511 "Check if there is a property fixing the value of VAR.
14512 If yes, return this value. If not, return the current value of the variable."
14513 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14514 (if (and prop (stringp prop) (string-match "\\S-" prop))
14516 (symbol-value var))))
14518 (defun org-entry-delete (pom property)
14519 "Delete the property PROPERTY from entry at point-or-marker POM."
14520 (org-with-point-at pom
14521 (if (member property org-special-properties)
14522 nil ; cannot delete these properties.
14523 (let ((range (org-get-property-block)))
14525 (goto-char (car range))
14527 (org-re-property property)
14530 (delete-region (match-beginning 0) (1+ (point-at-eol)))
14534 ;; Multi-values properties are properties that contain multiple values
14535 ;; These values are assumed to be single words, separated by whitespace.
14536 (defun org-entry-add-to-multivalued-property (pom property value)
14537 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
14538 (let* ((old (org-entry-get pom property))
14539 (values (and old (org-split-string old "[ \t]"))))
14540 (setq value (org-entry-protect-space value))
14541 (unless (member value values)
14542 (setq values (cons value values))
14543 (org-entry-put pom property
14544 (mapconcat 'identity values " ")))))
14546 (defun org-entry-remove-from-multivalued-property (pom property value)
14547 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
14548 (let* ((old (org-entry-get pom property))
14549 (values (and old (org-split-string old "[ \t]"))))
14550 (setq value (org-entry-protect-space value))
14551 (when (member value values)
14552 (setq values (delete value values))
14553 (org-entry-put pom property
14554 (mapconcat 'identity values " ")))))
14556 (defun org-entry-member-in-multivalued-property (pom property value)
14557 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
14558 (let* ((old (org-entry-get pom property))
14559 (values (and old (org-split-string old "[ \t]"))))
14560 (setq value (org-entry-protect-space value))
14561 (member value values)))
14563 (defun org-entry-get-multivalued-property (pom property)
14564 "Return a list of values in a multivalued property."
14565 (let* ((value (org-entry-get pom property))
14566 (values (and value (org-split-string value "[ \t]"))))
14567 (mapcar 'org-entry-restore-space values)))
14569 (defun org-entry-put-multivalued-property (pom property &rest values)
14570 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
14571 VALUES should be a list of strings. Spaces will be protected."
14572 (org-entry-put pom property
14573 (mapconcat 'org-entry-protect-space values " "))
14574 (let* ((value (org-entry-get pom property))
14575 (values (and value (org-split-string value "[ \t]"))))
14576 (mapcar 'org-entry-restore-space values)))
14578 (defun org-entry-protect-space (s)
14579 "Protect spaces and newline in string S."
14580 (while (string-match " " s)
14581 (setq s (replace-match "%20" t t s)))
14582 (while (string-match "\n" s)
14583 (setq s (replace-match "%0A" t t s)))
14586 (defun org-entry-restore-space (s)
14587 "Restore spaces and newline in string S."
14588 (while (string-match "%20" s)
14589 (setq s (replace-match " " t t s)))
14590 (while (string-match "%0A" s)
14591 (setq s (replace-match "\n" t t s)))
14594 (defvar org-entry-property-inherited-from (make-marker)
14595 "Marker pointing to the entry from where a property was inherited.
14596 Each call to `org-entry-get-with-inheritance' will set this marker to the
14597 location of the entry where the inheritance search matched. If there was
14598 no match, the marker will point nowhere.
14599 Note that also `org-entry-get' calls this function, if the INHERIT flag
14602 (defun org-entry-get-with-inheritance (property &optional literal-nil)
14603 "Get PROPERTY of entry or content at point, search higher levels if needed.
14604 The search will stop at the first ancestor which has the property defined.
14605 If the value found is \"nil\", return nil to show that the property
14606 should be considered as undefined (this is the meaning of nil here).
14607 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
14608 (move-marker org-entry-property-inherited-from nil)
14615 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
14616 (or (ignore-errors (org-back-to-heading t))
14617 (goto-char (point-min)))
14618 (move-marker org-entry-property-inherited-from (point))
14620 (or (ignore-errors (org-up-heading-safe))
14621 (throw 'ex nil))))))
14623 (cdr (assoc property org-file-properties))
14624 (cdr (assoc property org-global-properties))
14625 (cdr (assoc property org-global-properties-fixed))))
14626 (if literal-nil tmp (org-not-nil tmp))))
14628 (defvar org-property-changed-functions nil
14629 "Hook called when the value of a property has changed.
14630 Each hook function should accept two arguments, the name of the property
14631 and the new value.")
14633 (defun org-entry-put (pom property value)
14634 "Set PROPERTY to VALUE for entry at point-or-marker POM."
14635 (org-with-point-at pom
14636 (org-back-to-heading t)
14637 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
14640 ((equal property "TODO")
14641 (when (and (stringp value) (string-match "\\S-" value)
14642 (not (member value org-todo-keywords-1)))
14643 (error "\"%s\" is not a valid TODO state" value))
14644 (if (or (not value)
14645 (not (string-match "\\S-" value)))
14646 (setq value 'none))
14648 (org-set-tags nil 'align))
14649 ((equal property "PRIORITY")
14650 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
14651 (string-to-char value) ?\ ))
14652 (org-set-tags nil 'align))
14653 ((equal property "SCHEDULED")
14654 (if (re-search-forward org-scheduled-time-regexp end t)
14656 ((eq value 'earlier) (org-timestamp-change -1 'day))
14657 ((eq value 'later) (org-timestamp-change 1 'day))
14658 (t (call-interactively 'org-schedule)))
14659 (call-interactively 'org-schedule)))
14660 ((equal property "DEADLINE")
14661 (if (re-search-forward org-deadline-time-regexp end t)
14663 ((eq value 'earlier) (org-timestamp-change -1 'day))
14664 ((eq value 'later) (org-timestamp-change 1 'day))
14665 (t (call-interactively 'org-deadline)))
14666 (call-interactively 'org-deadline)))
14667 ((member property org-special-properties)
14668 (error "The %s property can not yet be set with `org-entry-put'"
14670 (t ; a non-special property
14671 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
14672 (setq range (org-get-property-block beg end 'force))
14673 (goto-char (car range))
14674 (if (re-search-forward
14675 (org-re-property-keyword property) (cdr range) t)
14677 (delete-region (match-beginning 0) (match-end 0))
14678 (goto-char (match-beginning 0)))
14679 (goto-char (cdr range))
14683 (insert ":" property ":")
14684 (and value (insert " " value))
14685 (org-indent-line)))))
14686 (run-hook-with-args 'org-property-changed-functions property value)))
14688 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
14689 "Get all property keys in the current buffer.
14690 With INCLUDE-SPECIALS, also list the special properties that reflect things
14691 like tags and TODO state.
14692 With INCLUDE-DEFAULTS, also include properties that has special meaning
14693 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
14695 With INCLUDE-COLUMNS, also include property names given in COLUMN
14696 formats in the current buffer."
14697 (let (rtn range cfmt s p)
14701 (goto-char (point-min))
14702 (while (re-search-forward org-property-start-re nil t)
14703 (setq range (org-get-property-block))
14704 (goto-char (car range))
14705 (while (re-search-forward
14706 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
14708 (add-to-list 'rtn (org-match-string-no-properties 1)))
14709 (outline-next-heading))))
14711 (when include-specials
14712 (setq rtn (append org-special-properties rtn)))
14714 (when include-defaults
14715 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
14716 (add-to-list 'rtn org-effort-property))
14718 (when include-columns
14722 (goto-char (point-min))
14723 (while (re-search-forward
14724 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
14726 (setq cfmt (match-string 2) s 0)
14727 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
14729 (setq s (match-end 0)
14730 p (match-string 1 cfmt))
14731 (unless (or (equal p "ITEM")
14732 (member p org-special-properties))
14733 (add-to-list 'rtn (match-string 1 cfmt))))))))
14735 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
14737 (defun org-property-values (key)
14738 "Return a list of all values of property KEY in the current buffer."
14742 (goto-char (point-min))
14743 (let ((re (org-re-property key))
14745 (while (re-search-forward re nil t)
14746 (add-to-list 'values (org-trim (match-string 1))))
14747 (delete "" values)))))
14749 (defun org-insert-property-drawer ()
14750 "Insert a property drawer into the current entry."
14751 (org-back-to-heading t)
14752 (looking-at org-outline-regexp)
14753 (let ((indent (if org-adapt-indentation
14754 (- (match-end 0) (match-beginning 0))
14757 (re (concat "^[ \t]*" org-keyword-time-regexp))
14759 (outline-next-heading)
14762 (while (re-search-forward re end t))
14763 (setq hiddenp (outline-invisible-p))
14765 (and (equal (char-after) ?\n) (forward-char 1))
14766 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
14767 (if (member (match-string 1) '("CLOCK:" ":END:"))
14768 ;; just skip this line
14769 (beginning-of-line 2)
14770 ;; Drawer start, find the end
14771 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
14772 (beginning-of-line 1)))
14773 (org-skip-over-state-notes)
14774 (skip-chars-backward " \t\n\r")
14775 (if (eq (char-before) ?*) (forward-char 1))
14776 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
14777 (beginning-of-line 0)
14778 (org-indent-to-column indent)
14779 (beginning-of-line 2)
14780 (org-indent-to-column indent)
14781 (beginning-of-line 0)
14784 (org-back-to-heading t)
14786 (org-flag-drawer t))))
14788 (defun org-insert-drawer (&optional arg drawer)
14789 "Insert a drawer at point.
14791 Optional argument DRAWER, when non-nil, is a string representing
14792 drawer's name. Otherwise, the user is prompted for a name.
14794 If a region is active, insert the drawer around that region
14797 Point is left between drawer's boundaries."
14799 (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
14801 ;; SYSTEM-DRAWERS is a list of drawer names that are used
14802 ;; internally by Org. They are meant to be inserted
14804 (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
14805 ;; Remove system drawers from list. Note: For some reason,
14806 ;; `org-completing-read' ignores the predicate while
14807 ;; `completing-read' handles it fine.
14808 (drawer (if arg "PROPERTIES"
14811 "Drawer: " org-drawers
14812 (lambda (d) (not (member d system-drawers))))))))
14814 ;; With C-u, fall back on `org-insert-property-drawer'
14815 (arg (org-insert-property-drawer))
14816 ;; With an active region, insert a drawer at point.
14817 ((not (org-region-active-p))
14819 (unless (bolp) (insert "\n"))
14820 (insert (format ":%s:\n\n:END:\n" drawer))
14821 (forward-line -2)))
14822 ;; Otherwise, insert the drawer at point
14824 (let ((rbeg (region-beginning))
14825 (rend (copy-marker (region-end))))
14829 (beginning-of-line)
14830 (when (save-excursion
14831 (re-search-forward org-outline-regexp-bol rend t))
14832 (error "Drawers cannot contain headlines"))
14833 ;; Position point at the beginning of the first
14834 ;; non-blank line in region. Insert drawer's opening
14835 ;; there, then indent it.
14836 (org-skip-whitespace)
14837 (beginning-of-line)
14838 (insert ":" drawer ":\n")
14840 (indent-for-tab-command)
14841 ;; Move point to the beginning of the first blank line
14842 ;; after the last non-blank line in region. Insert
14843 ;; drawer's closing, then indent it.
14845 (skip-chars-backward " \r\t\n")
14847 (deactivate-mark t)
14848 (indent-for-tab-command)
14849 (unless (eolp) (insert "\n")))
14850 ;; Clear marker, whatever the outcome of insertion is.
14851 (set-marker rend nil)))))))
14853 (defvar org-property-set-functions-alist nil
14854 "Property set function alist.
14855 Each entry should have the following format:
14857 (PROPERTY . READ-FUNCTION)
14859 The read function will be called with the same argument as
14860 `org-completing-read'.")
14862 (defun org-set-property-function (property)
14863 "Get the function that should be used to set PROPERTY.
14864 This is computed according to `org-property-set-functions-alist'."
14865 (or (cdr (assoc property org-property-set-functions-alist))
14866 'org-completing-read))
14868 (defun org-read-property-value (property)
14869 "Read PROPERTY value from user."
14870 (let* ((completion-ignore-case t)
14871 (allowed (org-property-get-allowed-values nil property 'table))
14872 (cur (org-entry-get nil property))
14873 (prompt (concat property " value"
14874 (if (and cur (string-match "\\S-" cur))
14875 (concat " [" cur "]") "") ": "))
14876 (set-function (org-set-property-function property))
14878 (funcall set-function prompt allowed nil
14879 (not (get-text-property 0 'org-unrestricted
14881 (let (org-completion-use-ido org-completion-use-iswitchb)
14882 (funcall set-function prompt
14883 (mapcar 'list (org-property-values property))
14884 nil nil "" nil cur)))))
14889 (defvar org-last-set-property nil)
14890 (defun org-read-property-name ()
14891 "Read a property name."
14892 (let* ((completion-ignore-case t)
14893 (keys (org-buffer-property-keys nil t t))
14894 (default-prop (or (save-excursion
14896 (beginning-of-line)
14897 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
14898 (null (string= (match-string 1) "END"))
14899 (match-string 1))))
14900 org-last-set-property))
14901 (property (org-icompleting-read
14903 (if default-prop (concat " [" default-prop "]") "")
14905 (mapcar 'list keys)
14909 (if (member property keys)
14911 (or (cdr (assoc (downcase property)
14912 (mapcar (lambda (x) (cons (downcase x) x))
14916 (defun org-set-property (property value)
14917 "In the current entry, set PROPERTY to VALUE.
14918 When called interactively, this will prompt for a property name, offering
14919 completion on existing and default properties. And then it will prompt
14920 for a value, offering completion either on allowed values (via an inherited
14921 xxx_ALL property) or on existing values in other instances of this property
14922 in the current file."
14923 (interactive (list nil nil))
14924 (let* ((property (or property (org-read-property-name)))
14925 (value (or value (org-read-property-value property)))
14926 (fn (cdr (assoc property org-properties-postprocess-alist))))
14927 (setq org-last-set-property property)
14928 ;; Possibly postprocess the inserted value:
14929 (when fn (setq value (funcall fn value)))
14930 (unless (equal (org-entry-get nil property) value)
14931 (org-entry-put nil property value))))
14933 (defun org-delete-property (property)
14934 "In the current entry, delete PROPERTY."
14936 (let* ((completion-ignore-case t)
14937 (prop (org-icompleting-read "Property: "
14938 (org-entry-properties nil 'standard))))
14940 (message "Property %s %s" property
14941 (if (org-entry-delete nil property)
14943 "was not present in the entry")))
14945 (defun org-delete-property-globally (property)
14946 "Remove PROPERTY globally, from all entries."
14948 (let* ((completion-ignore-case t)
14949 (prop (org-icompleting-read
14950 "Globally remove property: "
14951 (mapcar 'list (org-buffer-property-keys)))))
14956 (goto-char (point-min))
14958 (while (re-search-forward
14959 (org-re-property property)
14961 (setq cnt (1+ cnt))
14962 (delete-region (match-beginning 0) (1+ (point-at-eol))))
14963 (message "Property \"%s\" removed from %d entries" property cnt)))))
14965 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
14967 (defun org-compute-property-at-point ()
14968 "Compute the property at point.
14969 This looks for an enclosing column format, extracts the operator and
14970 then applies it to the property in the column format's scope."
14972 (unless (org-at-property-p)
14973 (error "Not at a property"))
14974 (let ((prop (org-match-string-no-properties 2)))
14975 (org-columns-get-format-and-top-level)
14976 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
14977 (error "No operator defined for property %s" prop))
14978 (org-columns-compute prop)))
14980 (defvar org-property-allowed-value-functions nil
14981 "Hook for functions supplying allowed values for a specific property.
14982 The functions must take a single argument, the name of the property, and
14983 return a flat list of allowed values. If \":ETC\" is one of
14984 the values, this means that these values are intended as defaults for
14985 completion, but that other values should be allowed too.
14986 The functions must return nil if they are not responsible for this
14989 (defun org-property-get-allowed-values (pom property &optional table)
14990 "Get allowed values for the property PROPERTY.
14991 When TABLE is non-nil, return an alist that can directly be used for
14995 ((equal property "TODO")
14996 (setq vals (org-with-point-at pom
14997 (append org-todo-keywords-1 '("")))))
14998 ((equal property "PRIORITY")
14999 (let ((n org-lowest-priority))
15000 (while (>= n org-highest-priority)
15001 (push (char-to-string n) vals)
15003 ((member property org-special-properties))
15004 ((setq vals (run-hook-with-args-until-success
15005 'org-property-allowed-value-functions property)))
15007 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
15008 (when (and vals (string-match "\\S-" vals))
15009 (setq vals (car (read-from-string (concat "(" vals ")"))))
15010 (setq vals (mapcar (lambda (x)
15011 (cond ((stringp x) x)
15012 ((numberp x) (number-to-string x))
15013 ((symbolp x) (symbol-name x))
15016 (when (member ":ETC" vals)
15017 (setq vals (remove ":ETC" vals))
15018 (org-add-props (car vals) '(org-unrestricted t)))
15019 (if table (mapcar 'list vals) vals)))
15021 (defun org-property-previous-allowed-value (&optional previous)
15022 "Switch to the next allowed value for this property."
15024 (org-property-next-allowed-value t))
15026 (defun org-property-next-allowed-value (&optional previous)
15027 "Switch to the next allowed value for this property."
15029 (unless (org-at-property-p)
15030 (error "Not at a property"))
15031 (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
15032 (key (match-string 2))
15033 (value (match-string 3))
15034 (allowed (or (org-property-get-allowed-values (point) key)
15035 (and (member value '("[ ]" "[-]" "[X]"))
15039 (error "Allowed values for this property have not been defined"))
15040 (if previous (setq allowed (reverse allowed)))
15041 (if (member value allowed)
15042 (setq nval (car (cdr (member value allowed)))))
15043 (setq nval (or nval (car allowed)))
15044 (if (equal nval value)
15045 (error "Only one allowed value for this property"))
15046 (org-at-property-p)
15047 (replace-match (concat " :" key ": " nval) t t)
15049 (beginning-of-line 1)
15050 (skip-chars-forward " \t")
15051 (when (equal prop org-effort-property)
15053 (org-back-to-heading t)
15054 (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
15055 (run-hook-with-args 'org-property-changed-functions key nval)))
15057 (defun org-find-olp (path &optional this-buffer)
15058 "Return a marker pointing to the entry at outline path OLP.
15059 If anything goes wrong, throw an error.
15060 You can wrap this call to catch the error like this:
15062 (condition-case msg
15063 (org-mobile-locate-entry (match-string 4))
15064 (error (nth 1 msg)))
15066 The return value will then be either a string with the error message,
15067 or a marker if everything is OK.
15069 If THIS-BUFFER is set, the outline path does not contain a file,
15071 (let* ((file (if this-buffer buffer-file-name (pop path)))
15072 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
15076 limit re end found pos heading cnt flevel)
15077 (unless buffer (error "File not found :%s" file))
15078 (with-current-buffer buffer
15082 (setq limit (point-max))
15083 (goto-char (point-min))
15084 (while (setq heading (pop path))
15085 (setq re (format org-complex-heading-regexp-format
15086 (regexp-quote heading)))
15087 (setq cnt 0 pos (point))
15088 (while (re-search-forward re end t)
15089 (setq level (- (match-end 1) (match-beginning 1)))
15090 (if (and (>= level lmin) (<= level lmax))
15091 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
15092 (when (= cnt 0) (error "Heading not found on level %d: %s"
15094 (when (> cnt 1) (error "Heading not unique on level %d: %s"
15097 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
15098 (setq end (save-excursion (org-end-of-subtree t t))))
15099 (when (org-at-heading-p)
15100 (point-marker)))))))
15102 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
15103 "Find node HEADING in BUFFER.
15104 Return a marker to the heading if it was found, or nil if not.
15105 If POS-ONLY is set, return just the position instead of a marker.
15107 The heading text must match exact, but it may have a TODO keyword,
15108 a priority cookie and tags in the standard locations."
15109 (with-current-buffer (or buffer (current-buffer))
15113 (goto-char (point-min))
15114 (let (case-fold-search)
15115 (if (re-search-forward
15116 (format org-complex-heading-regexp-format
15117 (regexp-quote heading)) nil t)
15119 (match-beginning 0)
15120 (move-marker (make-marker) (match-beginning 0)))))))))
15122 (defun org-find-exact-heading-in-directory (heading &optional dir)
15123 "Find Org node headline HEADING in all .org files in directory DIR.
15124 When the target headline is found, return a marker to this location."
15125 (let ((files (directory-files (or dir default-directory)
15126 nil "\\`[^.#].*\\.org\\'"))
15127 file visiting m buffer)
15129 (while (setq file (pop files))
15130 (message "trying %s" file)
15131 (setq visiting (org-find-base-buffer-visiting file))
15132 (setq buffer (or visiting (find-file-noselect file)))
15133 (setq m (org-find-exact-headline-in-buffer
15135 (when (and (not m) (not visiting)) (kill-buffer buffer))
15136 (and m (throw 'found m))))))
15138 (defun org-find-entry-with-id (ident)
15139 "Locate the entry that contains the ID property with exact value IDENT.
15140 IDENT can be a string, a symbol or a number, this function will search for
15141 the string representation of it.
15142 Return the position where this entry starts, or nil if there is no such entry."
15143 (interactive "sID: ")
15145 ((stringp ident) ident)
15146 ((symbol-name ident) (symbol-name ident))
15147 ((numberp ident) (number-to-string ident))
15148 (t (error "IDENT %s must be a string, symbol or number" ident))))
15149 (case-fold-search nil))
15153 (goto-char (point-min))
15154 (when (re-search-forward
15155 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
15157 (org-back-to-heading t)
15162 (defvar org-last-changed-timestamp nil)
15163 (defvar org-last-inserted-timestamp nil
15164 "The last time stamp inserted with `org-insert-time-stamp'.")
15165 (defvar org-time-was-given) ; dynamically scoped parameter
15166 (defvar org-end-time-was-given) ; dynamically scoped parameter
15167 (defvar org-ts-what) ; dynamically scoped parameter
15169 (defun org-time-stamp (arg &optional inactive)
15170 "Prompt for a date/time and insert a time stamp.
15171 If the user specifies a time like HH:MM or if this command is
15172 called with at least one prefix argument, the time stamp contains
15173 the date and the time. Otherwise, only the date is be included.
15175 All parts of a date not specified by the user is filled in from
15176 the current date/time. So if you just press return without
15177 typing anything, the time stamp will represent the current
15180 If there is already a timestamp at the cursor, it will be
15183 With two universal prefix arguments, insert an active timestamp
15184 with the current time without prompting the user."
15188 ;; Default time is either today, or, when entering a range,
15189 ;; the range start.
15190 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
15192 (re-search-backward
15193 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
15194 (- (point) 20) t)))
15195 (apply 'encode-time (org-parse-time-string (match-string 1)))
15197 (default-input (and ts (org-get-compact-tod ts)))
15198 (repeater (save-excursion
15200 (beginning-of-line)
15201 (when (re-search-forward
15202 "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
15203 (save-excursion (progn (end-of-line) (point))) t)
15204 (match-string 0)))))
15205 org-time-was-given org-end-time-was-given time)
15207 ((and (org-at-timestamp-p t)
15208 (memq last-command '(org-time-stamp org-time-stamp-inactive))
15209 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
15211 (setq time (let ((this-command this-command))
15212 (org-read-date arg 'totime nil nil
15213 default-time default-input inactive)))
15214 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
15215 ((org-at-timestamp-p t)
15216 (setq time (let ((this-command this-command))
15217 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15218 (when (org-at-timestamp-p t) ; just to get the match data
15219 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
15221 (setq org-last-changed-timestamp
15222 (org-insert-time-stamp
15223 time (or org-time-was-given arg)
15224 inactive nil nil (list org-end-time-was-given)))
15225 (when repeater (goto-char (1- (point))) (insert " " repeater)
15226 (setq org-last-changed-timestamp
15227 (concat (substring org-last-inserted-timestamp 0 -1)
15228 " " repeater ">"))))
15229 (message "Timestamp updated"))
15231 (org-insert-time-stamp (current-time) t))
15233 (setq time (let ((this-command this-command))
15234 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15235 (org-insert-time-stamp time (or org-time-was-given arg) inactive
15236 nil nil (list org-end-time-was-given))))))
15238 ;; FIXME: can we use this for something else, like computing time differences?
15239 (defun org-get-compact-tod (s)
15240 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
15241 (let* ((t1 (match-string 1 s))
15242 (h1 (string-to-number (match-string 2 s)))
15243 (m1 (string-to-number (match-string 3 s)))
15244 (t2 (and (match-end 4) (match-string 5 s)))
15245 (h2 (and t2 (string-to-number (match-string 6 s))))
15246 (m2 (and t2 (string-to-number (match-string 7 s))))
15250 (setq dh (- h2 h1) dm (- m2 m1))
15251 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
15252 (concat t1 "+" (number-to-string dh)
15253 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
15255 (defun org-time-stamp-inactive (&optional arg)
15256 "Insert an inactive time stamp.
15257 An inactive time stamp is enclosed in square brackets instead of angle
15258 brackets. It is inactive in the sense that it does not trigger agenda entries,
15259 does not link to the calendar and cannot be changed with the S-cursor keys.
15260 So these are more for recording a certain time/date."
15262 (org-time-stamp arg 'inactive))
15264 (defvar org-date-ovl (make-overlay 1 1))
15265 (overlay-put org-date-ovl 'face 'org-date-selected)
15266 (org-detach-overlay org-date-ovl)
15268 (defvar org-ans1) ; dynamically scoped parameter
15269 (defvar org-ans2) ; dynamically scoped parameter
15271 (defvar org-plain-time-of-day-regexp) ; defined below
15273 (defvar org-overriding-default-time nil) ; dynamically scoped
15274 (defvar org-read-date-overlay nil)
15275 (defvar org-dcst nil) ; dynamically scoped
15276 (defvar org-read-date-history nil)
15277 (defvar org-read-date-final-answer nil)
15278 (defvar org-read-date-analyze-futurep nil)
15279 (defvar org-read-date-analyze-forced-year nil)
15280 (defvar org-read-date-inactive)
15282 (defun org-read-date (&optional org-with-time to-time from-string prompt
15283 default-time default-input inactive)
15284 "Read a date, possibly a time, and make things smooth for the user.
15285 The prompt will suggest to enter an ISO date, but you can also enter anything
15286 which will at least partially be understood by `parse-time-string'.
15287 Unrecognized parts of the date will default to the current day, month, year,
15288 hour and minute. If this command is called to replace a timestamp at point,
15289 or to enter the second timestamp of a range, the default time is taken
15290 from the existing stamp. Furthermore, the command prefers the future,
15291 so if you are giving a date where the year is not given, and the day-month
15292 combination is already past in the current year, it will assume you
15293 mean next year. For details, see the manual. A few examples:
15295 3-2-5 --> 2003-02-05
15296 feb 15 --> currentyear-02-15
15297 2/15 --> currentyear-02-15
15298 sep 12 9 --> 2009-09-12
15299 12:45 --> today 12:45
15300 22 sept 0:34 --> currentyear-09-22 0:34
15301 12 --> currentyear-currentmonth-12
15302 Fri --> nearest Friday (today or later)
15305 Furthermore you can specify a relative date by giving, as the *first* thing
15306 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
15307 change in days weeks, months, years.
15308 With a single plus or minus, the date is relative to today. With a double
15309 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
15310 +4d --> four days from today
15311 +4 --> same as above
15312 +2w --> two weeks from today
15313 ++5 --> five days from default date
15315 The function understands only English month and weekday abbreviations.
15317 While prompting, a calendar is popped up - you can also select the
15318 date with the mouse (button 1). The calendar shows a period of three
15319 months. To scroll it to other months, use the keys `>' and `<'.
15320 If you don't like the calendar, turn it off with
15321 \(setq org-read-date-popup-calendar nil)
15323 With optional argument TO-TIME, the date will immediately be converted
15324 to an internal time.
15325 With an optional argument ORG-WITH-TIME, the prompt will suggest to
15326 also insert a time. Note that when ORG-WITH-TIME is not set, you can
15327 still enter a time, and this function will inform the calling routine
15328 about this change. The calling routine may then choose to change the
15329 format used to insert the time stamp into the buffer to include the time.
15330 With optional argument FROM-STRING, read from this string instead from
15331 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
15332 the time/date that is used for everything that is not specified by the
15334 (require 'parse-time)
15335 (let* ((org-time-stamp-rounding-minutes
15336 (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
15337 (org-dcst org-display-custom-times)
15338 (ct (org-current-time))
15339 (org-def (or org-overriding-default-time default-time ct))
15340 (org-defdecode (decode-time org-def))
15342 (when (< (nth 2 org-defdecode) org-extend-today-until)
15343 (setcar (nthcdr 2 org-defdecode) -1)
15344 (setcar (nthcdr 1 org-defdecode) 59)
15345 (setq org-def (apply 'encode-time org-defdecode)
15346 org-defdecode (decode-time org-def)))))
15347 (mouse-autoselect-window nil) ; Don't let the mouse jump
15348 (calendar-frame-setup nil)
15349 (calendar-setup nil)
15350 (calendar-move-hook nil)
15351 (calendar-view-diary-initially-flag nil)
15352 (calendar-view-holidays-initially-flag nil)
15353 (timestr (format-time-string
15354 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
15355 (prompt (concat (if prompt (concat prompt " ") "")
15356 (format "Date+time [%s]: " timestr)))
15357 ans (org-ans0 "") org-ans1 org-ans2 final)
15360 (from-string (setq ans from-string))
15361 (org-read-date-popup-calendar
15363 (save-window-excursion
15365 (org-eval-in-calendar '(setq cursor-type nil) t)
15368 (calendar-forward-day (- (time-to-days org-def)
15369 (calendar-absolute-from-gregorian
15370 (calendar-current-date))))
15371 (org-eval-in-calendar nil t)
15372 (let* ((old-map (current-local-map))
15373 (map (copy-keymap calendar-mode-map))
15374 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
15375 (org-defkey map (kbd "RET") 'org-calendar-select)
15376 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
15377 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
15378 (org-defkey minibuffer-local-map [(meta shift left)]
15379 (lambda () (interactive)
15380 (org-eval-in-calendar '(calendar-backward-month 1))))
15381 (org-defkey minibuffer-local-map [(meta shift right)]
15382 (lambda () (interactive)
15383 (org-eval-in-calendar '(calendar-forward-month 1))))
15384 (org-defkey minibuffer-local-map [(meta shift up)]
15385 (lambda () (interactive)
15386 (org-eval-in-calendar '(calendar-backward-year 1))))
15387 (org-defkey minibuffer-local-map [(meta shift down)]
15388 (lambda () (interactive)
15389 (org-eval-in-calendar '(calendar-forward-year 1))))
15390 (org-defkey minibuffer-local-map [?\e (shift left)]
15391 (lambda () (interactive)
15392 (org-eval-in-calendar '(calendar-backward-month 1))))
15393 (org-defkey minibuffer-local-map [?\e (shift right)]
15394 (lambda () (interactive)
15395 (org-eval-in-calendar '(calendar-forward-month 1))))
15396 (org-defkey minibuffer-local-map [?\e (shift up)]
15397 (lambda () (interactive)
15398 (org-eval-in-calendar '(calendar-backward-year 1))))
15399 (org-defkey minibuffer-local-map [?\e (shift down)]
15400 (lambda () (interactive)
15401 (org-eval-in-calendar '(calendar-forward-year 1))))
15402 (org-defkey minibuffer-local-map [(shift up)]
15403 (lambda () (interactive)
15404 (org-eval-in-calendar '(calendar-backward-week 1))))
15405 (org-defkey minibuffer-local-map [(shift down)]
15406 (lambda () (interactive)
15407 (org-eval-in-calendar '(calendar-forward-week 1))))
15408 (org-defkey minibuffer-local-map [(shift left)]
15409 (lambda () (interactive)
15410 (org-eval-in-calendar '(calendar-backward-day 1))))
15411 (org-defkey minibuffer-local-map [(shift right)]
15412 (lambda () (interactive)
15413 (org-eval-in-calendar '(calendar-forward-day 1))))
15414 (org-defkey minibuffer-local-map ">"
15415 (lambda () (interactive)
15416 (org-eval-in-calendar '(scroll-calendar-left 1))))
15417 (org-defkey minibuffer-local-map "<"
15418 (lambda () (interactive)
15419 (org-eval-in-calendar '(scroll-calendar-right 1))))
15420 (org-defkey minibuffer-local-map "\C-v"
15421 (lambda () (interactive)
15422 (org-eval-in-calendar
15423 '(calendar-scroll-left-three-months 1))))
15424 (org-defkey minibuffer-local-map "\M-v"
15425 (lambda () (interactive)
15426 (org-eval-in-calendar
15427 '(calendar-scroll-right-three-months 1))))
15428 (run-hooks 'org-read-date-minibuffer-setup-hook)
15431 (use-local-map map)
15432 (setq org-read-date-inactive inactive)
15433 (add-hook 'post-command-hook 'org-read-date-display)
15434 (setq org-ans0 (read-string prompt default-input
15435 'org-read-date-history nil))
15436 ;; org-ans0: from prompt
15437 ;; org-ans1: from mouse click
15438 ;; org-ans2: from calendar motion
15439 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
15440 (remove-hook 'post-command-hook 'org-read-date-display)
15441 (use-local-map old-map)
15442 (when org-read-date-overlay
15443 (delete-overlay org-read-date-overlay)
15444 (setq org-read-date-overlay nil)))))
15445 (bury-buffer "*Calendar*")))))
15447 (t ; Naked prompt only
15449 (setq ans (read-string prompt default-input
15450 'org-read-date-history timestr))
15451 (when org-read-date-overlay
15452 (delete-overlay org-read-date-overlay)
15453 (setq org-read-date-overlay nil)))))
15455 (setq final (org-read-date-analyze ans org-def org-defdecode))
15457 (when org-read-date-analyze-forced-year
15458 (message "Year was forced into %s"
15459 (if org-read-date-force-compatible-dates
15460 "compatible range (1970-2037)"
15461 "range representable on this machine"))
15464 ;; One round trip to get rid of 34th of August and stuff like that....
15465 (setq final (decode-time (apply 'encode-time final)))
15467 (setq org-read-date-final-answer ans)
15470 (apply 'encode-time final)
15471 (if (and (boundp 'org-time-was-given) org-time-was-given)
15472 (format "%04d-%02d-%02d %02d:%02d"
15473 (nth 5 final) (nth 4 final) (nth 3 final)
15474 (nth 2 final) (nth 1 final))
15475 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
15478 (defvar org-defdecode)
15479 (defvar org-with-time)
15480 (defun org-read-date-display ()
15481 "Display the current date prompt interpretation in the minibuffer."
15482 (when org-read-date-display-live
15483 (when org-read-date-overlay
15484 (delete-overlay org-read-date-overlay))
15485 (when (minibufferp (current-buffer))
15488 (while (not (equal (buffer-substring
15489 (max (point-min) (- (point) 4)) (point))
15492 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
15493 " " (or org-ans1 org-ans2)))
15494 (org-end-time-was-given nil)
15495 (f (org-read-date-analyze ans org-def org-defdecode))
15497 org-time-stamp-custom-formats
15498 org-time-stamp-formats))
15499 (fmt (if (or org-with-time
15500 (and (boundp 'org-time-was-given) org-time-was-given))
15503 (txt (format-time-string fmt (apply 'encode-time f)))
15504 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
15505 (txt (concat "=> " txt)))
15506 (when (and org-end-time-was-given
15507 (string-match org-plain-time-of-day-regexp txt))
15508 (setq txt (concat (substring txt 0 (match-end 0)) "-"
15509 org-end-time-was-given
15510 (substring txt (match-end 0)))))
15511 (when org-read-date-analyze-futurep
15512 (setq txt (concat txt " (=>F)")))
15513 (setq org-read-date-overlay
15514 (make-overlay (1- (point-at-eol)) (point-at-eol)))
15515 (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
15517 (defun org-read-date-analyze (ans org-def org-defdecode)
15518 "Analyze the combined answer of the date prompt."
15519 ;; FIXME: cleanup and comment
15520 (let ((nowdecode (decode-time (current-time)))
15521 delta deltan deltaw deltadef year month day
15522 hour minute second wday pm h2 m2 tl wday1
15523 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
15524 (setq org-read-date-analyze-futurep nil
15525 org-read-date-analyze-forced-year nil)
15526 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
15529 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
15530 (setq ans (replace-match "" t t ans)
15532 deltaw (nth 1 delta)
15533 deltadef (nth 2 delta)))
15535 ;; Check if there is an iso week date in there. If yes, store the
15536 ;; info and postpone interpreting it until the rest of the parsing
15538 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
15539 (setq iso-year (if (match-end 1)
15540 (org-small-year-to-year
15541 (string-to-number (match-string 1 ans))))
15542 iso-weekday (if (match-end 3)
15543 (string-to-number (match-string 3 ans)))
15544 iso-week (string-to-number (match-string 2 ans)))
15545 (setq ans (replace-match "" t t ans)))
15547 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
15548 (when (string-match
15549 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
15550 (setq year (if (match-end 2)
15551 (string-to-number (match-string 2 ans))
15552 (progn (setq kill-year t)
15553 (string-to-number (format-time-string "%Y"))))
15554 month (string-to-number (match-string 3 ans))
15555 day (string-to-number (match-string 4 ans)))
15556 (if (< year 100) (setq year (+ 2000 year)))
15557 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15560 ;; Help matching dotted european dates
15561 (when (string-match
15562 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
15563 (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
15565 (string-to-number (format-time-string "%Y")))
15566 day (string-to-number (match-string 1 ans))
15567 month (string-to-number (match-string 2 ans))
15568 ans (replace-match (format "%04d-%02d-%02d" year month day)
15571 ;; Help matching american dates, like 5/30 or 5/30/7
15572 (when (string-match
15573 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
15574 (setq year (if (match-end 4)
15575 (string-to-number (match-string 4 ans))
15576 (progn (setq kill-year t)
15577 (string-to-number (format-time-string "%Y"))))
15578 month (string-to-number (match-string 1 ans))
15579 day (string-to-number (match-string 2 ans)))
15580 (if (< year 100) (setq year (+ 2000 year)))
15581 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15583 ;; Help matching am/pm times, because `parse-time-string' does not do that.
15584 ;; If there is a time with am/pm, and *no* time without it, we convert
15585 ;; so that matching will be successful.
15586 (loop for i from 1 to 2 do ; twice, for end time as well
15587 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
15588 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
15589 (setq hour (string-to-number (match-string 1 ans))
15590 minute (if (match-end 3)
15591 (string-to-number (match-string 3 ans))
15594 (string-to-char (downcase (match-string 4 ans)))))
15595 (if (and (= hour 12) (not pm))
15597 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
15598 (setq ans (replace-match (format "%02d:%02d" hour minute)
15601 ;; Check if a time range is given as a duration
15602 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
15603 (setq hour (string-to-number (match-string 1 ans))
15604 h2 (+ hour (string-to-number (match-string 3 ans)))
15605 minute (string-to-number (match-string 2 ans))
15606 m2 (+ minute (if (match-end 5) (string-to-number
15607 (match-string 5 ans))0)))
15608 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
15609 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
15612 ;; Check if there is a time range
15613 (when (boundp 'org-end-time-was-given)
15614 (setq org-time-was-given nil)
15615 (when (and (string-match org-plain-time-of-day-regexp ans)
15617 (setq org-end-time-was-given (match-string 8 ans))
15618 (setq ans (concat (substring ans 0 (match-beginning 7))
15619 (substring ans (match-end 7))))))
15621 (setq tl (parse-time-string ans)
15622 day (or (nth 3 tl) (nth 3 org-defdecode))
15623 month (or (nth 4 tl)
15624 (if (and org-read-date-prefer-future
15625 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
15626 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
15627 (nth 4 org-defdecode)))
15628 year (or (and (not kill-year) (nth 5 tl))
15629 (if (and org-read-date-prefer-future
15630 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
15631 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
15632 (nth 5 org-defdecode)))
15633 hour (or (nth 2 tl) (nth 2 org-defdecode))
15634 minute (or (nth 1 tl) (nth 1 org-defdecode))
15635 second (or (nth 0 tl) 0)
15638 (when (and (eq org-read-date-prefer-future 'time)
15639 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
15640 (equal day (nth 3 nowdecode))
15641 (equal month (nth 4 nowdecode))
15642 (equal year (nth 5 nowdecode))
15644 (or (< (nth 2 tl) (nth 2 nowdecode))
15645 (and (= (nth 2 tl) (nth 2 nowdecode))
15647 (< (nth 1 tl) (nth 1 nowdecode)))))
15651 ;; Special date definitions below
15654 ;; There was an iso week
15657 (setq year (or iso-year year)
15658 day (or iso-weekday wday 1)
15659 wday nil ; to make sure that the trigger below does not match
15660 iso-date (calendar-gregorian-from-absolute
15661 (calendar-absolute-from-iso
15662 (list iso-week day year))))
15663 ; FIXME: Should we also push ISO weeks into the future?
15664 ; (when (and org-read-date-prefer-future
15666 ; (< (calendar-absolute-from-gregorian iso-date)
15667 ; (time-to-days (current-time))))
15668 ; (setq year (1+ year)
15669 ; iso-date (calendar-gregorian-from-absolute
15670 ; (calendar-absolute-from-iso
15671 ; (list iso-week day year)))))
15672 (setq month (car iso-date)
15673 year (nth 2 iso-date)
15674 day (nth 1 iso-date)))
15678 (let ((now (decode-time (current-time))))
15679 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
15680 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
15681 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
15682 ((equal deltaw "m") (setq month (+ month deltan)))
15683 ((equal deltaw "y") (setq year (+ year deltan)))))
15684 ((and wday (not (nth 3 tl)))
15685 ;; Weekday was given, but no day, so pick that day in the week
15686 ;; on or after the derived date.
15687 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
15688 (unless (equal wday wday1)
15689 (setq day (+ day (% (- wday wday1 -7) 7))))))
15690 (if (and (boundp 'org-time-was-given)
15692 (setq org-time-was-given t))
15693 (if (< year 100) (setq year (+ 2000 year)))
15694 ;; Check of the date is representable
15695 (if org-read-date-force-compatible-dates
15698 (setq year 1970 org-read-date-analyze-forced-year t))
15700 (setq year 2037 org-read-date-analyze-forced-year t)))
15701 (condition-case nil
15702 (ignore (encode-time second minute hour day month year))
15704 (setq year (nth 5 org-defdecode))
15705 (setq org-read-date-analyze-forced-year t))))
15706 (setq org-read-date-analyze-futurep futurep)
15707 (list second minute hour day month year)))
15709 (defvar parse-time-weekdays)
15710 (defun org-read-date-get-relative (s today default)
15711 "Check string S for special relative date string.
15712 TODAY and DEFAULT are internal times, for today and for a default.
15713 Return shift list (N what def-flag)
15714 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
15715 N is the number of WHATs to shift.
15716 DEF-FLAG is t when a double ++ or -- indicates shift relative to
15717 the DEFAULT date rather than TODAY."
15718 (require 'parse-time)
15722 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
15724 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
15725 "\\([ \t]\\|$\\)") s)
15726 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
15727 (let* ((dir (if (> (match-end 1) (match-beginning 1))
15728 (string-to-char (substring (match-string 1 s) -1))
15730 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
15731 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
15732 (what (if (match-end 3) (match-string 3 s) "d"))
15733 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
15734 (date (if rel default today))
15735 (wday (nth 6 (decode-time date)))
15739 (setq delta (mod (+ 7 (- wday1 wday)) 7))
15740 (if (= dir ?-) (setq delta (- delta 7)))
15741 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
15742 (list delta "d" rel))
15743 (list (* n (if (= dir ?-) -1 1)) what rel)))))
15745 (defun org-order-calendar-date-args (arg1 arg2 arg3)
15746 "Turn a user-specified date into the internal representation.
15747 The internal representation needed by the calendar is (month day year).
15748 This is a wrapper to handle the brain-dead convention in calendar that
15749 user function argument order change dependent on argument order."
15750 (if (boundp 'calendar-date-style)
15752 ((eq calendar-date-style 'american)
15753 (list arg1 arg2 arg3))
15754 ((eq calendar-date-style 'european)
15755 (list arg2 arg1 arg3))
15756 ((eq calendar-date-style 'iso)
15757 (list arg2 arg3 arg1)))
15758 (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
15759 (if (org-bound-and-true-p european-calendar-style)
15760 (list arg2 arg1 arg3)
15761 (list arg1 arg2 arg3)))))
15763 (defun org-eval-in-calendar (form &optional keepdate)
15764 "Eval FORM in the calendar window and return to current window.
15765 When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
15766 otherwise stick to the current value of `org-ans2'."
15767 (let ((sf (selected-frame))
15768 (sw (selected-window)))
15769 (select-window (get-buffer-window "*Calendar*" t))
15771 (when (and (not keepdate) (calendar-cursor-to-date))
15772 (let* ((date (calendar-cursor-to-date))
15773 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15774 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
15775 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
15777 (org-select-frame-set-input-focus sf)))
15779 (defun org-calendar-select ()
15780 "Return to `org-read-date' with the date currently selected.
15781 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15783 (when (calendar-cursor-to-date)
15784 (let* ((date (calendar-cursor-to-date))
15785 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15786 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15787 (if (active-minibuffer-window) (exit-minibuffer))))
15789 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
15790 "Insert a date stamp for the date given by the internal TIME.
15791 WITH-HM means use the stamp format that includes the time of the day.
15792 INACTIVE means use square brackets instead of angular ones, so that the
15793 stamp will not contribute to the agenda.
15794 PRE and POST are optional strings to be inserted before and after the
15796 The command returns the inserted time stamp."
15797 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
15799 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
15800 (insert-before-markers (or pre ""))
15801 (when (listp extra)
15802 (setq extra (car extra))
15803 (if (and (stringp extra)
15804 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
15805 (setq extra (format "-%02d:%02d"
15806 (string-to-number (match-string 1 extra))
15807 (string-to-number (match-string 2 extra))))
15810 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
15811 (insert-before-markers (setq stamp (format-time-string fmt time)))
15812 (insert-before-markers (or post ""))
15813 (setq org-last-inserted-timestamp stamp)))
15815 (defun org-toggle-time-stamp-overlays ()
15816 "Toggle the use of custom time stamp formats."
15818 (setq org-display-custom-times (not org-display-custom-times))
15819 (unless org-display-custom-times
15820 (let ((p (point-min)) (bmp (buffer-modified-p)))
15821 (while (setq p (next-single-property-change p 'display))
15822 (if (and (get-text-property p 'display)
15823 (eq (get-text-property p 'face) 'org-date))
15824 (remove-text-properties
15825 p (setq p (next-single-property-change p 'display))
15827 (set-buffer-modified-p bmp)))
15828 (if (featurep 'xemacs)
15829 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
15830 (org-restart-font-lock)
15831 (setq org-table-may-need-update t)
15832 (if org-display-custom-times
15833 (message "Time stamps are overlaid with custom format")
15834 (message "Time stamp overlays removed")))
15836 (defun org-display-custom-time (beg end)
15837 "Overlay modified time stamp format over timestamp between BEG and END."
15838 (let* ((ts (buffer-substring beg end))
15839 t1 w1 with-hm tf time str w2 (off 0))
15841 (setq t1 (org-parse-time-string ts t))
15842 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
15843 (setq off (- (match-end 0) (match-beginning 0)))))
15844 (setq end (- end off))
15845 (setq w1 (- end beg)
15846 with-hm (and (nth 1 t1) (nth 2 t1))
15847 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
15848 time (org-fix-decoded-time t1)
15850 (format-time-string
15851 (substring tf 1 -1) (apply 'encode-time time))
15852 nil 'mouse-face 'highlight)
15854 (if (not (= w2 w1))
15855 (add-text-properties (1+ beg) (+ 2 beg)
15856 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
15857 (if (featurep 'xemacs)
15859 (put-text-property beg end 'invisible t)
15860 (put-text-property beg end 'end-glyph (make-glyph str)))
15861 (put-text-property beg end 'display str))))
15863 (defun org-translate-time (string)
15864 "Translate all timestamps in STRING to custom format.
15865 But do this only if the variable `org-display-custom-times' is set."
15866 (when org-display-custom-times
15869 (re org-ts-regexp-both)
15870 t1 with-hm inactive tf time str beg end)
15871 (while (setq start (string-match re string start))
15872 (setq beg (match-beginning 0)
15874 t1 (save-match-data
15875 (org-parse-time-string (substring string beg end) t))
15876 with-hm (and (nth 1 t1) (nth 2 t1))
15877 inactive (equal (substring string beg (1+ beg)) "[")
15878 tf (funcall (if with-hm 'cdr 'car)
15879 org-time-stamp-custom-formats)
15880 time (org-fix-decoded-time t1)
15881 str (format-time-string
15883 (if inactive "[" "<") (substring tf 1 -1)
15884 (if inactive "]" ">"))
15885 (apply 'encode-time time))
15886 string (replace-match str t t string)
15887 start (+ start (length str)))))))
15890 (defun org-fix-decoded-time (time)
15891 "Set 0 instead of nil for the first 6 elements of time.
15892 Don't touch the rest."
15894 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
15896 (defun org-days-to-time (timestamp-string)
15897 "Difference between TIMESTAMP-STRING and now in days."
15898 (- (time-to-days (org-time-string-to-time timestamp-string))
15899 (time-to-days (current-time))))
15901 (defun org-deadline-close (timestamp-string &optional ndays)
15902 "Is the time in TIMESTAMP-STRING close to the current date?"
15903 (setq ndays (or ndays (org-get-wdays timestamp-string)))
15904 (and (< (org-days-to-time timestamp-string) ndays)
15905 (not (org-entry-is-done-p))))
15907 (defun org-get-wdays (ts)
15908 "Get the deadline lead time appropriate for timestring TS."
15910 ((<= org-deadline-warning-days 0)
15911 ;; 0 or negative, enforce this value no matter what
15912 (- org-deadline-warning-days))
15913 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
15914 ;; lead time is specified.
15915 (floor (* (string-to-number (match-string 1 ts))
15916 (cdr (assoc (match-string 2 ts)
15917 '(("d" . 1) ("w" . 7)
15918 ("m" . 30.4) ("y" . 365.25)))))))
15919 ;; go for the default.
15920 (t org-deadline-warning-days)))
15922 (defun org-calendar-select-mouse (ev)
15923 "Return to `org-read-date' with the date currently selected.
15924 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15926 (mouse-set-point ev)
15927 (when (calendar-cursor-to-date)
15928 (let* ((date (calendar-cursor-to-date))
15929 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15930 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15931 (if (active-minibuffer-window) (exit-minibuffer))))
15933 (defun org-check-deadlines (ndays)
15934 "Check if there are any deadlines due or past due.
15935 A deadline is considered due if it happens within `org-deadline-warning-days'
15936 days from today's date. If the deadline appears in an entry marked DONE,
15937 it is not shown. The prefix arg NDAYS can be used to test that many
15938 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
15940 (let* ((org-warn-days
15942 ((equal ndays '(4)) 100000)
15943 (ndays (prefix-numeric-value ndays))
15944 (t (abs org-deadline-warning-days))))
15945 (case-fold-search nil)
15946 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15948 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
15950 (message "%d deadlines past-due or due within %d days"
15951 (org-occur regexp nil callback)
15954 (defsubst org-re-timestamp (type)
15955 "Return a regexp for timestamp TYPE.
15956 Allowed values for TYPE are:
15958 all: all timestamps
15959 active: only active timestamps (<...>)
15960 inactive: only inactive timestamps ([...])
15961 scheduled: only scheduled timestamps
15962 deadline: only deadline timestamps
15964 When TYPE is nil, fall back on returning a regexp that matches
15965 both scheduled and deadline timestamps."
15966 (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9>
\n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
15967 ((eq type 'active) org-ts-regexp)
15968 ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^
\n>]*?\\)\\]")
15969 ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
15970 ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15971 ((eq type 'scheduled-or-deadline)
15972 (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
15974 (defun org-check-before-date (date)
15975 "Check if there are deadlines or scheduled entries before DATE."
15976 (interactive (list (org-read-date)))
15977 (let ((case-fold-search nil)
15978 (regexp (org-re-timestamp org-ts-type))
15980 (lambda () (time-less-p
15981 (org-time-string-to-time (match-string 1))
15982 (org-time-string-to-time date)))))
15983 (message "%d entries before %s"
15984 (org-occur regexp nil callback) date)))
15986 (defun org-check-after-date (date)
15987 "Check if there are deadlines or scheduled entries after DATE."
15988 (interactive (list (org-read-date)))
15989 (let ((case-fold-search nil)
15990 (regexp (org-re-timestamp org-ts-type))
15994 (org-time-string-to-time (match-string 1))
15995 (org-time-string-to-time date))))))
15996 (message "%d entries after %s"
15997 (org-occur regexp nil callback) date)))
15999 (defun org-check-dates-range (start-date end-date)
16000 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
16001 (interactive (list (org-read-date nil nil nil "Range starts")
16002 (org-read-date nil nil nil "Range end")))
16003 (let ((case-fold-search nil)
16004 (regexp (org-re-timestamp org-ts-type))
16007 (let ((match (match-string 1)))
16010 (org-time-string-to-time match)
16011 (org-time-string-to-time start-date)))
16013 (org-time-string-to-time match)
16014 (org-time-string-to-time end-date)))))))
16015 (message "%d entries between %s and %s"
16016 (org-occur regexp nil callback) start-date end-date)))
16018 (defun org-evaluate-time-range (&optional to-buffer)
16019 "Evaluate a time range by computing the difference between start and end.
16020 Normally the result is just printed in the echo area, but with prefix arg
16021 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
16022 If the time range is actually in a table, the result is inserted into the
16024 For time difference computation, a year is assumed to be exactly 365
16025 days in order to avoid rounding problems."
16028 (org-clock-update-time-maybe)
16030 (unless (org-at-date-range-p t)
16031 (goto-char (point-at-bol))
16032 (re-search-forward org-tr-regexp-both (point-at-eol) t))
16033 (if (not (org-at-date-range-p t))
16034 (error "Not at a time-stamp range, and none found in current line")))
16035 (let* ((ts1 (match-string 1))
16036 (ts2 (match-string 2))
16037 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
16038 (match-end (match-end 0))
16039 (time1 (org-time-string-to-time ts1))
16040 (time2 (org-time-string-to-time ts2))
16041 (t1 (org-float-time time1))
16042 (t2 (org-float-time time2))
16043 (diff (abs (- t2 t1)))
16044 (negative (< (- t2 t1) 0))
16045 ;; (ys (floor (* 365 24 60 60)))
16048 (fy "%dy %dd %02d:%02d")
16050 (fd "%dd %02d:%02d")
16055 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16057 d (floor (/ diff ds)) diff (mod diff ds)
16058 h (floor (/ diff hs)) diff (mod diff hs)
16059 m (floor (/ diff 60)))
16060 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16062 d (floor (+ (/ diff ds) 0.5))
16064 (if (not to-buffer)
16065 (message "%s" (org-make-tdiff-string y d h m))
16066 (if (org-at-table-p)
16068 (goto-char match-end)
16070 (and (looking-at " *|") (goto-char (match-end 0))))
16071 (goto-char match-end))
16073 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
16074 (replace-match ""))
16075 (if negative (insert " -"))
16076 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
16077 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
16078 (insert " " (format fh h m))))
16079 (if align (org-table-align))
16080 (message "Time difference inserted")))))
16082 (defun org-make-tdiff-string (y d h m)
16085 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
16087 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
16089 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
16091 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
16093 (apply 'format fmt (nreverse l))))
16095 (defun org-time-string-to-time (s &optional buffer pos)
16096 "Convert a timestamp string into internal time."
16097 (condition-case errdata
16098 (apply 'encode-time (org-parse-time-string s))
16099 (error (error "Bad timestamp `%s'%s\nError was: %s"
16100 s (if (not (and buffer pos))
16102 (format " at %d in buffer `%s'" pos buffer))
16105 (defun org-time-string-to-seconds (s)
16106 "Convert a timestamp string to a number of seconds."
16107 (org-float-time (org-time-string-to-time s)))
16109 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
16110 "Convert a time stamp to an absolute day number.
16111 If there is a specifier for a cyclic time stamp, get the closest date to
16113 PREFER and SHOW-ALL are passed through to `org-closest-date'.
16114 The variable date is bound by the calendar when this is called."
16116 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
16117 (if (org-diary-sexp-entry (match-string 1 s) "" date)
16120 ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
16121 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
16122 (time-to-days (current-time))) (match-string 0 s)
16125 (condition-case errdata
16126 (apply 'encode-time (org-parse-time-string s))
16127 (error (error "Bad timestamp `%s'%s\nError was: %s"
16128 s (if (not (and buffer pos))
16130 (format " at %d in buffer `%s'" pos buffer))
16131 (cdr errdata))))))))
16133 (defun org-days-to-iso-week (days)
16134 "Return the iso week number."
16136 (car (calendar-iso-from-absolute days)))
16138 (defun org-small-year-to-year (year)
16139 "Convert 2-digit years into 4-digit years.
16140 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
16141 The year 2000 cannot be abbreviated. Any year larger than 99
16142 is returned unchanged."
16144 (setq year (+ 2000 year))
16146 (setq year (+ 1900 year))))
16149 (defun org-time-from-absolute (d)
16150 "Return the time corresponding to date D.
16151 D may be an absolute day number, or a calendar-type list (month day year)."
16152 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
16153 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
16155 (defun org-calendar-holiday ()
16156 "List of holidays, for Diary display in Org-mode."
16157 (require 'holidays)
16159 (if (fboundp 'calendar-check-holidays)
16160 'calendar-check-holidays 'check-calendar-holidays) date)))
16161 (if hl (mapconcat 'identity hl "; "))))
16163 (defun org-diary-sexp-entry (sexp entry date)
16164 "Process a SEXP diary ENTRY for DATE."
16165 (require 'diary-lib)
16166 (let ((result (if calendar-debug-sexp
16167 (let ((stack-trace-on-error t))
16168 (eval (car (read-from-string sexp))))
16169 (condition-case nil
16170 (eval (car (read-from-string sexp)))
16173 (message "Bad sexp at line %d in %s: %s"
16175 (buffer-file-name) sexp)
16177 (cond ((stringp result) (split-string result "; "))
16178 ((and (consp result)
16179 (not (consp (cdr result)))
16180 (stringp (cdr result))) (cdr result))
16181 ((and (consp result)
16182 (stringp (car result))) result)
16185 (defun org-diary-to-ical-string (frombuf)
16186 "Get iCalendar entries from diary entries in buffer FROMBUF.
16187 This uses the icalendar.el library."
16188 (let* ((tmpdir (if (featurep 'xemacs)
16190 temporary-file-directory))
16191 (tmpfile (make-temp-name
16192 (expand-file-name "orgics" tmpdir)))
16194 (with-current-buffer frombuf
16195 (icalendar-export-region (point-min) (point-max) tmpfile)
16196 (setq buf (find-buffer-visiting tmpfile))
16198 (goto-char (point-min))
16199 (if (re-search-forward "^BEGIN:VEVENT" nil t)
16200 (setq b (match-beginning 0)))
16201 (goto-char (point-max))
16202 (if (re-search-backward "^END:VEVENT" nil t)
16203 (setq e (match-end 0)))
16204 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
16206 (delete-file tmpfile)
16209 (defun org-closest-date (start current change prefer show-all)
16210 "Find the date closest to CURRENT that is consistent with START and CHANGE.
16211 When PREFER is `past', return a date that is either CURRENT or past.
16212 When PREFER is `future', return a date that is either CURRENT or future.
16213 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
16214 ;; Make the proper lists from the dates
16216 (let ((a1 '(("h" . hour)
16221 (shour (nth 2 (org-parse-time-string start)))
16222 dn dw sday cday n1 n2 n0
16223 d m y y1 y2 date1 date2 nmonths nm ny m2)
16225 (setq start (org-date-to-gregorian start)
16226 current (org-date-to-gregorian
16229 (time-to-days (current-time))))
16230 sday (calendar-absolute-from-gregorian start)
16231 cday (calendar-absolute-from-gregorian current))
16233 (if (<= cday sday) (throw 'exit sday))
16235 (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
16236 (setq dn (string-to-number (match-string 1 change))
16237 dw (cdr (assoc (match-string 2 change) a1)))
16238 (error "Invalid change specifier: %s" change))
16239 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
16242 (let ((missing-hours
16243 (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
16245 (setq n1 (if (zerop missing-hours) cday
16246 (- cday (1+ (floor (/ missing-hours 24)))))
16247 n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
16249 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
16252 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
16253 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
16254 (setq date1 (list m d y1)
16255 n1 (calendar-absolute-from-gregorian date1)
16256 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
16257 n2 (calendar-absolute-from-gregorian date2)))
16259 ;; approx number of month between the two dates
16260 (setq nmonths (floor (/ (- cday sday) 30.436875)))
16261 ;; How often does dn fit in there?
16262 (setq d (nth 1 start) m (car start) y (nth 2 start)
16263 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
16265 ny (floor (/ m 12))
16268 (while (> m 12) (setq m (- m 12) y (1+ y)))
16269 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
16270 (setq m2 (+ m dn) y2 y)
16271 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16272 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
16273 (while (<= n2 cday)
16274 (setq n1 n2 m m2 y y2)
16275 (setq m2 (+ m dn) y2 y)
16276 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16277 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
16278 ;; Make sure n1 is the earlier date
16279 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
16282 ((eq prefer 'past) (if (= cday n2) n2 n1))
16283 ((eq prefer 'future) (if (= cday n1) n1 n2))
16284 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
16286 ((eq prefer 'past) (if (= cday n2) n2 n1))
16287 ((eq prefer 'future) (if (= cday n1) n1 n2))
16288 (t (if (= cday n1) n1 n2)))))))
16290 (defun org-date-to-gregorian (date)
16291 "Turn any specification of DATE into a Gregorian date for the calendar."
16292 (cond ((integerp date) (calendar-gregorian-from-absolute date))
16293 ((and (listp date) (= (length date) 3)) date)
16295 (setq date (org-parse-time-string date))
16296 (list (nth 4 date) (nth 3 date) (nth 5 date)))
16298 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
16300 (defun org-parse-time-string (s &optional nodefault)
16301 "Parse the standard Org-mode time string.
16302 This should be a lot faster than the normal `parse-time-string'.
16303 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
16304 hour and minute fields will be nil if not given."
16305 (if (string-match org-ts-regexp0 s)
16307 (if (or (match-beginning 8) (not nodefault))
16308 (string-to-number (or (match-string 8 s) "0")))
16309 (if (or (match-beginning 7) (not nodefault))
16310 (string-to-number (or (match-string 7 s) "0")))
16311 (string-to-number (match-string 4 s))
16312 (string-to-number (match-string 3 s))
16313 (string-to-number (match-string 2 s))
16315 (error "Not a standard Org-mode time string: %s" s)))
16317 (defun org-timestamp-up (&optional arg)
16318 "Increase the date item at the cursor by one.
16319 If the cursor is on the year, change the year. If it is on the month,
16320 the day or the time, change that.
16321 With prefix ARG, change by that many units."
16323 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
16325 (defun org-timestamp-down (&optional arg)
16326 "Decrease the date item at the cursor by one.
16327 If the cursor is on the year, change the year. If it is on the month,
16328 the day or the time, change that.
16329 With prefix ARG, change by that many units."
16331 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
16333 (defun org-timestamp-up-day (&optional arg)
16334 "Increase the date in the time stamp by one day.
16335 With prefix ARG, change that many days."
16337 (if (and (not (org-at-timestamp-p t))
16338 (org-at-heading-p))
16340 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
16342 (defun org-timestamp-down-day (&optional arg)
16343 "Decrease the date in the time stamp by one day.
16344 With prefix ARG, change that many days."
16346 (if (and (not (org-at-timestamp-p t))
16347 (org-at-heading-p))
16349 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
16351 (defun org-at-timestamp-p (&optional inactive-ok)
16352 "Determine if the cursor is in or at a timestamp."
16354 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
16356 (ans (or (looking-at tsr)
16358 (skip-chars-backward "^[<\n\r\t")
16359 (if (> (point) (point-min)) (backward-char 1))
16360 (and (looking-at tsr)
16361 (> (- (match-end 0) pos) -1))))))
16363 (boundp 'org-ts-what)
16366 ((= pos (match-beginning 0)) 'bracket)
16367 ;; Point is considered to be "on the bracket" whether
16368 ;; it's really on it or right after it.
16369 ((= pos (1- (match-end 0))) 'bracket)
16370 ((= pos (match-end 0)) 'after)
16371 ((org-pos-in-match-range pos 2) 'year)
16372 ((org-pos-in-match-range pos 3) 'month)
16373 ((org-pos-in-match-range pos 7) 'hour)
16374 ((org-pos-in-match-range pos 8) 'minute)
16375 ((or (org-pos-in-match-range pos 4)
16376 (org-pos-in-match-range pos 5)) 'day)
16377 ((and (> pos (or (match-end 8) (match-end 5)))
16378 (< pos (match-end 0)))
16379 (- pos (or (match-end 8) (match-end 5))))
16383 (defun org-toggle-timestamp-type ()
16384 "Toggle the type (<active> or [inactive]) of a time stamp."
16386 (when (org-at-timestamp-p t)
16387 (let ((beg (match-beginning 0)) (end (match-end 0))
16388 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
16391 (while (re-search-forward "[][<>]" end t)
16392 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
16394 (message "Timestamp is now %sactive"
16395 (if (equal (char-after beg) ?<) "" "in")))))
16397 (defvar org-clock-history) ; defined in org-clock.el
16398 (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
16399 (defun org-timestamp-change (n &optional what updown)
16400 "Change the date in the time stamp at point.
16401 The date will be changed by N times WHAT. WHAT can be `day', `month',
16402 `year', `minute', `second'. If WHAT is not given, the cursor position
16403 in the timestamp determines what will be changed."
16404 (let ((origin (point)) origin-cat
16406 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
16409 ts time time0 fixnext clrgx)
16410 (if (not (org-at-timestamp-p t))
16411 (error "Not at a timestamp"))
16412 (if (and (not what) (eq org-ts-what 'bracket))
16413 (org-toggle-timestamp-type)
16414 ;; Point isn't on brackets. Remember the part of the time-stamp
16415 ;; the point was in. Indeed, size of time-stamps may change,
16416 ;; but point must be kept in the same category nonetheless.
16417 (setq origin-cat org-ts-what)
16418 (if (and (not what) (not (eq org-ts-what 'day))
16419 org-display-custom-times
16420 (get-text-property (point) 'display)
16421 (not (get-text-property (1- (point)) 'display)))
16422 (setq org-ts-what 'day))
16423 (setq org-ts-what (or what org-ts-what)
16424 inactive (= (char-after (match-beginning 0)) ?\[)
16425 ts (match-string 0))
16428 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
16430 (setq extra (match-string 1 ts)))
16431 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
16433 (setq time0 (org-parse-time-string ts))
16435 (eq org-ts-what 'minute)
16436 (not current-prefix-arg))
16437 ;; This looks like s-up and s-down. Change by one rounding step.
16438 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
16439 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
16440 (setcar (cdr time0) (+ (nth 1 time0)
16441 (if (> n 0) (- rem) (- dm rem))))))
16443 (encode-time (or (car time0) 0)
16444 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
16445 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
16446 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
16447 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
16448 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
16450 (when (and (member org-ts-what '(hour minute))
16452 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
16453 (setq extra (org-modify-ts-extra
16455 (if (eq org-ts-what 'hour) 2 5)
16457 (when (integerp org-ts-what)
16458 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
16459 (if (eq what 'calendar)
16460 (let ((cal-date (org-get-date-from-calendar)))
16461 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
16462 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
16463 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
16464 (setcar time0 (or (car time0) 0))
16465 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
16466 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
16467 (setq time (apply 'encode-time time0))))
16468 ;; Insert the new time-stamp, and ensure point stays in the same
16469 ;; category as before (i.e. not after the last position in that
16471 (let ((pos (point)))
16472 ;; Stay before inserted string. `save-excursion' is of no use.
16473 (setq org-last-changed-timestamp
16474 (org-insert-time-stamp time with-hm inactive nil nil extra))
16477 (looking-at org-ts-regexp3)
16479 ;; `day' category ends before `hour' if any, or at
16480 ;; the end of the day name.
16481 ((eq origin-cat 'day)
16482 (min (or (match-beginning 7) (1- (match-end 5))) origin))
16483 ((eq origin-cat 'hour) (min (match-end 7) origin))
16484 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
16485 ((integerp origin-cat) (min (1- (match-end 0)) origin))
16486 ;; `year' and `month' have both fixed size: point
16487 ;; couldn't have moved into another part.
16489 ;; Update clock if on a CLOCK line.
16490 (org-clock-update-time-maybe)
16491 ;; Maybe adjust the closest clock in `org-clock-history'
16492 (when org-clock-adjust-closest
16493 (if (not (and (org-at-clock-log-p)
16494 (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
16495 org-clock-history))))))
16496 (message "No clock to adjust")
16497 (cond ((save-excursion ; fix previous clock?
16498 (re-search-backward org-ts-regexp0 nil t)
16499 (org-looking-back (concat org-clock-string " \\[")))
16500 (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
16501 ((save-excursion ; fix next clock?
16502 (re-search-backward org-ts-regexp0 nil t)
16503 (looking-at (concat org-ts-regexp0 "\\] =>")))
16504 (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
16505 (save-window-excursion
16506 ;; Find closest clock to point, adjust the previous/next one in history
16507 (let* ((p (save-excursion (org-back-to-heading t)))
16508 (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
16510 (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
16511 (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
16513 (message "No clock to adjust")
16515 (org-goto-marker-or-bmk clfixpos)
16517 (when (re-search-forward clrgx nil t)
16518 (goto-char (match-beginning 1))
16519 (let (org-clock-adjust-closest)
16520 (org-timestamp-change n org-ts-what updown))
16521 (message "Clock adjusted in %s for heading: %s"
16522 (file-name-nondirectory (buffer-file-name))
16523 (org-get-heading t t)))))))))
16524 ;; Try to recenter the calendar window, if any.
16525 (if (and org-calendar-follow-timestamp-change
16526 (get-buffer-window "*Calendar*" t)
16527 (memq org-ts-what '(day month year)))
16528 (org-recenter-calendar (time-to-days time))))))
16530 (defun org-modify-ts-extra (s pos n dm)
16531 "Change the different parts of the lead-time and repeat fields in timestamp."
16532 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
16534 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
16536 ((or (org-pos-in-match-range pos 2)
16537 (org-pos-in-match-range pos 3))
16538 (setq m (string-to-number (match-string 3 s))
16539 h (string-to-number (match-string 2 s)))
16540 (if (org-pos-in-match-range pos 2)
16542 (setq n (* dm (org-no-warnings (signum n))))
16543 (when (not (= 0 (setq rem (% m dm))))
16544 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
16546 (if (< m 0) (setq m (+ m 60) h (1- h)))
16547 (if (> m 59) (setq m (- m 60) h (1+ h)))
16548 (setq h (min 24 (max 0 h)))
16549 (setq ng 1 new (format "-%02d:%02d" h m)))
16550 ((org-pos-in-match-range pos 6)
16551 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
16552 ((org-pos-in-match-range pos 5)
16553 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
16555 ((org-pos-in-match-range pos 9)
16556 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
16557 ((org-pos-in-match-range pos 8)
16558 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
16562 (substring s 0 (match-beginning ng))
16564 (substring s (match-end ng))))))
16567 (defun org-recenter-calendar (date)
16568 "If the calendar is visible, recenter it to DATE."
16569 (let ((cwin (get-buffer-window "*Calendar*" t)))
16571 (let ((calendar-move-hook nil))
16572 (with-selected-window cwin
16573 (calendar-goto-date (if (listp date) date
16574 (calendar-gregorian-from-absolute date))))))))
16576 (defun org-goto-calendar (&optional arg)
16577 "Go to the Emacs calendar at the current date.
16578 If there is a time stamp in the current line, go to that date.
16579 A prefix ARG can be used to force the current date."
16581 (let ((tsr org-ts-regexp) diff
16582 (calendar-move-hook nil)
16583 (calendar-view-holidays-initially-flag nil)
16584 (calendar-view-diary-initially-flag nil))
16585 (if (or (org-at-timestamp-p)
16587 (beginning-of-line 1)
16588 (looking-at (concat ".*" tsr))))
16589 (let ((d1 (time-to-days (current-time)))
16591 (org-time-string-to-time (match-string 1)))))
16592 (setq diff (- d2 d1))))
16594 (calendar-goto-today)
16595 (if (and diff (not arg)) (calendar-forward-day diff))))
16597 (defun org-get-date-from-calendar ()
16598 "Return a list (month day year) of date at point in calendar."
16599 (with-current-buffer "*Calendar*"
16601 (calendar-cursor-to-date))))
16603 (defun org-date-from-calendar ()
16604 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
16605 If there is already a time stamp at the cursor position, update it."
16607 (if (org-at-timestamp-p t)
16608 (org-timestamp-change 0 'calendar)
16609 (let ((cal-date (org-get-date-from-calendar)))
16610 (org-insert-time-stamp
16611 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
16613 (defun org-minutes-to-hh:mm-string (m)
16614 "Compute H:MM from a number of minutes."
16615 (let ((h (/ m 60)))
16616 (setq m (- m (* 60 h)))
16617 (format org-time-clocksum-format h m)))
16619 (defun org-hh:mm-string-to-minutes (s)
16620 "Convert a string H:MM to a number of minutes.
16621 If the string is just a number, interpret it as minutes.
16622 In fact, the first hh:mm or number in the string will be taken,
16623 there can be extra stuff in the string.
16624 If no number is found, the return value is 0."
16627 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
16628 (+ (* (string-to-number (match-string 1 s)) 60)
16629 (string-to-number (match-string 2 s))))
16630 ((string-match "\\([0-9]+\\)" s)
16631 (string-to-number (match-string 1 s)))
16634 (defcustom org-effort-durations
16637 ("w" . ,(* 60 8 5))
16638 ("m" . ,(* 60 8 5 4))
16639 ("y" . ,(* 60 8 5 40)))
16640 "Conversion factor to minutes for an effort modifier.
16642 Each entry has the form (MODIFIER . MINUTES).
16644 In an effort string, a number followed by MODIFIER is multiplied
16645 by the specified number of MINUTES to obtain an effort in
16648 For example, if the value of this variable is ((\"hours\" . 60)), then an
16649 effort string \"2hours\" is equivalent to 120 minutes."
16652 :type '(alist :key-type (string :tag "Modifier")
16653 :value-type (number :tag "Minutes")))
16655 (defun org-duration-string-to-minutes (s &optional output-to-string)
16656 "Convert a duration string S to minutes.
16658 A bare number is interpreted as minutes, modifiers can be set by
16659 customizing `org-effort-durations' (which see).
16661 Entries containing a colon are interpreted as H:MM by
16662 `org-hh:mm-string-to-minutes'."
16664 (re (concat "\\([0-9.]+\\) *\\("
16665 (regexp-opt (mapcar 'car org-effort-durations))
16667 (while (string-match re s)
16668 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
16669 (string-to-number (match-string 1 s))))
16670 (setq s (replace-match "" nil t s)))
16671 (setq result (floor result))
16672 (incf result (org-hh:mm-string-to-minutes s))
16673 (if output-to-string (number-to-string result) result)))
16677 (defun org-save-all-org-buffers ()
16678 "Save all Org-mode buffers without user confirmation."
16680 (message "Saving all Org-mode buffers...")
16681 (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
16682 (when (featurep 'org-id) (org-id-locations-save))
16683 (message "Saving all Org-mode buffers... done"))
16685 (defun org-revert-all-org-buffers ()
16686 "Revert all Org-mode buffers.
16687 Prompt for confirmation when there are unsaved changes.
16688 Be sure you know what you are doing before letting this function
16689 overwrite your changes.
16691 This function is useful in a setup where one tracks org files
16692 with a version control system, to revert on one machine after pulling
16693 changes from another. I believe the procedure must be like this:
16695 1. M-x org-save-all-org-buffers
16696 2. Pull changes from the other machine, resolve conflicts
16697 3. M-x org-revert-all-org-buffers"
16699 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
16702 (save-window-excursion
16705 (when (and (with-current-buffer b (derived-mode-p 'org-mode))
16706 (with-current-buffer b buffer-file-name))
16707 (org-pop-to-buffer-same-window b)
16708 (revert-buffer t 'no-confirm)))
16710 (when (and (featurep 'org-id) org-id-track-globally)
16711 (org-id-locations-load)))))
16716 (defun org-switchb (&optional arg)
16717 "Switch between Org buffers.
16718 With one prefix argument, restrict available buffers to files.
16719 With two prefix arguments, restrict available buffers to agenda files.
16721 Defaults to `iswitchb' for buffer name completion.
16722 Set `org-completion-use-ido' to make it use ido instead."
16724 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
16725 ((equal arg '(16)) (org-buffer-list 'agenda))
16726 (t (org-buffer-list))))
16727 (org-completion-use-iswitchb org-completion-use-iswitchb)
16728 (org-completion-use-ido org-completion-use-ido))
16729 (unless (or org-completion-use-ido org-completion-use-iswitchb)
16730 (setq org-completion-use-iswitchb t))
16731 (org-pop-to-buffer-same-window
16732 (org-icompleting-read "Org buffer: "
16733 (mapcar 'list (mapcar 'buffer-name blist))
16736 ;;; Define some older names previously used for this functionality
16738 (defalias 'org-ido-switchb 'org-switchb)
16740 (defalias 'org-iswitchb 'org-switchb)
16742 (defun org-buffer-list (&optional predicate exclude-tmp)
16743 "Return a list of Org buffers.
16744 PREDICATE can be `export', `files' or `agenda'.
16746 export restrict the list to Export buffers.
16747 files restrict the list to buffers visiting Org files.
16748 agenda restrict the list to buffers visiting agenda files.
16750 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
16752 (agenda-files (and (eq predicate 'agenda)
16753 (mapcar 'file-truename (org-agenda-files t))))
16756 ((eq predicate 'files)
16757 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
16758 ((eq predicate 'export)
16759 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
16760 ((eq predicate 'agenda)
16762 (with-current-buffer b
16763 (and (derived-mode-p 'org-mode)
16764 (setq bfn (buffer-file-name b))
16765 (member (file-truename bfn) agenda-files)))))
16766 (t (lambda (b) (with-current-buffer b
16767 (or (derived-mode-p 'org-mode)
16768 (string-match "\*Org .*Export"
16769 (buffer-name b)))))))))
16773 (if (and (funcall filter b)
16774 (or (not exclude-tmp)
16775 (not (string-match "tmp" (buffer-name b)))))
16780 (defun org-agenda-files (&optional unrestricted archives)
16781 "Get the list of agenda files.
16782 Optional UNRESTRICTED means return the full list even if a restriction
16783 is currently in place.
16784 When ARCHIVES is t, include all archive files that are really being
16785 used by the agenda files. If ARCHIVE is `ifmode', do this only if
16786 `org-agenda-archives-mode' is t."
16789 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
16790 ((stringp org-agenda-files) (org-read-agenda-file-list))
16791 ((listp org-agenda-files) org-agenda-files)
16792 (t (error "Invalid value of `org-agenda-files'")))))
16793 (setq files (apply 'append
16794 (mapcar (lambda (f)
16795 (if (file-directory-p f)
16797 f t org-agenda-file-regexp)
16800 (when org-agenda-skip-unavailable-files
16801 (setq files (delq nil
16804 (and (file-readable-p file) file)))
16806 (when (or (eq archives t)
16807 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
16808 (setq files (org-add-archive-files files)))
16811 (defun org-agenda-file-p (&optional file)
16812 "Return non-nil, if FILE is an agenda file.
16813 If FILE is omitted, use the file associated with the current
16815 (member (or file (buffer-file-name))
16816 (org-agenda-files t)))
16818 (defun org-edit-agenda-file-list ()
16819 "Edit the list of agenda files.
16820 Depending on setup, this either uses customize to edit the variable
16821 `org-agenda-files', or it visits the file that is holding the list. In the
16822 latter case, the buffer is set up in a way that saving it automatically kills
16823 the buffer and restores the previous window configuration."
16825 (if (stringp org-agenda-files)
16826 (let ((cw (current-window-configuration)))
16827 (find-file org-agenda-files)
16828 (org-set-local 'org-window-configuration cw)
16829 (org-add-hook 'after-save-hook
16831 (set-window-configuration
16832 (prog1 org-window-configuration
16833 (kill-buffer (current-buffer))))
16834 (org-install-agenda-files-menu)
16835 (message "New agenda file list installed"))
16837 (message "%s" (substitute-command-keys
16838 "Edit list and finish with \\[save-buffer]")))
16839 (customize-variable 'org-agenda-files)))
16841 (defun org-store-new-agenda-file-list (list)
16842 "Set new value for the agenda file list and save it correctly."
16843 (if (stringp org-agenda-files)
16844 (let ((fe (org-read-agenda-file-list t)) b u)
16845 (while (setq b (find-buffer-visiting org-agenda-files))
16847 (with-temp-file org-agenda-files
16850 (lambda (f) ;; Keep un-expanded entries.
16851 (if (setq u (assoc f fe))
16856 (let ((org-mode-hook nil) (org-inhibit-startup t)
16857 (org-insert-mode-line-in-empty-file nil))
16858 (setq org-agenda-files list)
16859 (customize-save-variable 'org-agenda-files org-agenda-files))))
16861 (defun org-read-agenda-file-list (&optional pair-with-expansion)
16862 "Read the list of agenda files from a file.
16863 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
16864 filenames, used by `org-store-new-agenda-file-list' to write back
16865 un-expanded file names."
16866 (when (file-directory-p org-agenda-files)
16867 (error "`org-agenda-files' cannot be a single directory"))
16868 (when (stringp org-agenda-files)
16870 (insert-file-contents org-agenda-files)
16873 (let ((e (expand-file-name (substitute-in-file-name f)
16875 (if pair-with-expansion
16878 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
16881 (defun org-cycle-agenda-files ()
16882 "Cycle through the files in `org-agenda-files'.
16883 If the current buffer visits an agenda file, find the next one in the list.
16884 If the current buffer does not, find the first agenda file."
16886 (let* ((fs (org-agenda-files t))
16887 (files (append fs (list (car fs))))
16888 (tcf (if buffer-file-name (file-truename buffer-file-name)))
16890 (unless files (error "No agenda files"))
16892 (while (setq file (pop files))
16893 (if (equal (file-truename file) tcf)
16895 (find-file (car files))
16897 (find-file (car fs)))
16898 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
16900 (defun org-agenda-file-to-front (&optional to-end)
16901 "Move/add the current file to the top of the agenda file list.
16902 If the file is not present in the list, it is added to the front. If it is
16903 present, it is moved there. With optional argument TO-END, add/move to the
16906 (let ((org-agenda-skip-unavailable-files nil)
16907 (file-alist (mapcar (lambda (x)
16908 (cons (file-truename x) x))
16909 (org-agenda-files t)))
16910 (ctf (file-truename
16911 (or buffer-file-name
16912 (error "Please save the current buffer to a file"))))
16914 (setq x (assoc ctf file-alist) had x)
16916 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
16918 (setq file-alist (append (delq x file-alist) (list x)))
16919 (setq file-alist (cons x (delq x file-alist))))
16920 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
16921 (org-install-agenda-files-menu)
16922 (message "File %s to %s of agenda file list"
16923 (if had "moved" "added") (if to-end "end" "front"))))
16925 (defun org-remove-file (&optional file)
16926 "Remove current file from the list of files in variable `org-agenda-files'.
16927 These are the files which are being checked for agenda entries.
16928 Optional argument FILE means use this file instead of the current."
16930 (let* ((org-agenda-skip-unavailable-files nil)
16931 (file (or file buffer-file-name
16932 (error "Current buffer does not visit a file")))
16933 (true-file (file-truename file))
16934 (afile (abbreviate-file-name file))
16935 (files (delq nil (mapcar
16937 (if (equal true-file
16940 (org-agenda-files t)))))
16941 (if (not (= (length files) (length (org-agenda-files t))))
16943 (org-store-new-agenda-file-list files)
16944 (org-install-agenda-files-menu)
16945 (message "Removed file: %s" afile))
16946 (message "File was not in list: %s (not removed)" afile))))
16948 (defun org-file-menu-entry (file)
16949 (vector file (list 'find-file file) t))
16951 (defun org-check-agenda-file (file)
16952 "Make sure FILE exists. If not, ask user what to do."
16953 (when (not (file-exists-p file))
16954 (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
16955 (abbreviate-file-name file))
16956 (let ((r (downcase (read-char-exclusive))))
16959 (org-remove-file file)
16960 (throw 'nextfile t))
16961 (t (error "Abort"))))))
16963 (defun org-get-agenda-file-buffer (file)
16964 "Get a buffer visiting FILE. If the buffer needs to be created, add
16965 it to the list of buffers which might be released later."
16966 (let ((buf (org-find-base-buffer-visiting file)))
16968 buf ; just return it
16969 ;; Make a new buffer and remember it
16970 (setq buf (find-file-noselect file))
16971 (if buf (push buf org-agenda-new-buffers))
16974 (defun org-release-buffers (blist)
16975 "Release all buffers in list, asking the user for confirmation when needed.
16976 When a buffer is unmodified, it is just killed. When modified, it is saved
16977 \(if the user agrees) and then killed."
16979 (while (setq buf (pop blist))
16980 (setq file (buffer-file-name buf))
16981 (when (and (buffer-modified-p buf)
16983 (y-or-n-p (format "Save file %s? " file)))
16984 (with-current-buffer buf (save-buffer)))
16985 (kill-buffer buf))))
16987 (defun org-agenda-prepare-buffers (files)
16988 "Create buffers for all agenda files, protect archived trees and comments."
16990 (let ((pa '(:org-archived t))
16991 (pc '(:org-comment t))
16992 (pall '(:org-archived t :org-comment t))
16993 (inhibit-read-only t)
16994 (rea (concat ":" org-archive-tag ":"))
16998 (while (setq file (pop files))
17002 (org-check-agenda-file file)
17003 (set-buffer (org-get-agenda-file-buffer file)))
17005 (setq bmp (buffer-modified-p))
17006 (org-refresh-category-properties)
17007 (org-refresh-properties org-effort-property 'org-effort)
17008 (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
17009 (setq org-todo-keywords-for-agenda
17010 (append org-todo-keywords-for-agenda org-todo-keywords-1))
17011 (setq org-done-keywords-for-agenda
17012 (append org-done-keywords-for-agenda org-done-keywords))
17013 (setq org-todo-keyword-alist-for-agenda
17014 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
17015 (setq org-drawers-for-agenda
17016 (append org-drawers-for-agenda org-drawers))
17017 (setq org-tag-alist-for-agenda
17018 (append org-tag-alist-for-agenda org-tag-alist))
17021 (remove-text-properties (point-min) (point-max) pall)
17022 (when org-agenda-skip-archived-trees
17023 (goto-char (point-min))
17024 (while (re-search-forward rea nil t)
17025 (if (org-at-heading-p t)
17026 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
17027 (goto-char (point-min))
17028 (setq re (format org-heading-keyword-regexp-format
17029 org-comment-string))
17030 (while (re-search-forward re nil t)
17031 (add-text-properties
17032 (match-beginning 0) (org-end-of-subtree t) pc)))
17033 (set-buffer-modified-p bmp)))))
17034 (setq org-todo-keywords-for-agenda
17035 (org-uniquify org-todo-keywords-for-agenda))
17036 (setq org-todo-keyword-alist-for-agenda
17037 (org-uniquify org-todo-keyword-alist-for-agenda)
17038 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
17040 ;;;; Embedded LaTeX
17042 (defvar org-cdlatex-mode-map (make-sparse-keymap)
17043 "Keymap for the minor `org-cdlatex-mode'.")
17045 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
17046 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
17047 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
17048 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
17049 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
17051 (defvar org-cdlatex-texmathp-advice-is-done nil
17052 "Flag remembering if we have applied the advice to texmathp already.")
17054 (define-minor-mode org-cdlatex-mode
17055 "Toggle the minor `org-cdlatex-mode'.
17056 This mode supports entering LaTeX environment and math in LaTeX fragments
17058 \\{org-cdlatex-mode-map}"
17060 (when org-cdlatex-mode
17062 (run-hooks 'cdlatex-mode-hook)
17063 (cdlatex-compute-tables))
17064 (unless org-cdlatex-texmathp-advice-is-done
17065 (setq org-cdlatex-texmathp-advice-is-done t)
17066 (defadvice texmathp (around org-math-always-on activate)
17067 "Always return t in org-mode buffers.
17068 This is because we want to insert math symbols without dollars even outside
17069 the LaTeX math segments. If Orgmode thinks that point is actually inside
17070 an embedded LaTeX fragment, let texmathp do its job.
17071 \\[org-cdlatex-mode-map]"
17075 ((not (derived-mode-p 'org-mode)) ad-do-it)
17076 ((eq this-command 'cdlatex-math-symbol)
17077 (setq ad-return-value t
17078 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
17080 (let ((p (org-inside-LaTeX-fragment-p)))
17081 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
17082 (setq ad-return-value t
17083 texmathp-why '("Org-mode embedded math" . 0))
17084 (if p ad-do-it)))))))))
17086 (defun turn-on-org-cdlatex ()
17087 "Unconditionally turn on `org-cdlatex-mode'."
17088 (org-cdlatex-mode 1))
17090 (defun org-inside-LaTeX-fragment-p ()
17091 "Test if point is inside a LaTeX fragment.
17092 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
17093 sequence appearing also before point.
17094 Even though the matchers for math are configurable, this function assumes
17095 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
17096 delimiters are skipped when they have been removed by customization.
17097 The return value is nil, or a cons cell with the delimiter and the
17098 position of this delimiter.
17100 This function does a reasonably good job, but can locally be fooled by
17101 for example currency specifications. For example it will assume being in
17102 inline math after \"$22.34\". The LaTeX fragment formatter will only format
17103 fragments that are properly closed, but during editing, we have to live
17104 with the uncertainty caused by missing closing delimiters. This function
17105 looks only before point, not after."
17107 (let ((pos (point))
17108 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
17110 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
17112 dd-on str (start 0) m re)
17115 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
17116 re (nth 1 (assoc "$" org-latex-regexps)))
17117 (while (string-match re str start)
17119 ((= (match-end 0) (length str))
17120 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
17121 ((= (match-end 0) (- (length str) 5))
17123 (t (setq start (match-end 0))))))
17124 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
17126 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
17127 (and (match-beginning 2) (throw 'exit nil))
17129 (while (re-search-backward "\\$\\$" lim t)
17130 (setq dd-on (not dd-on)))
17132 (if dd-on (cons "$$" m))))))
17134 (defun org-inside-latex-macro-p ()
17135 "Is point inside a LaTeX macro or its arguments?"
17138 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
17140 (defun org-try-cdlatex-tab ()
17141 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
17142 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
17143 - inside a LaTeX fragment, or
17144 - after the first word in a line, where an abbreviation expansion could
17145 insert a LaTeX environment."
17146 (when org-cdlatex-mode
17148 ;; Before any word on the line: No expansion possible.
17149 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
17150 ;; Just after first word on the line: Expand it. Make sure it
17151 ;; cannot happen on headlines, though.
17153 (skip-chars-backward "a-zA-Z0-9*")
17154 (skip-chars-backward " \t")
17155 (and (bolp) (not (org-at-heading-p))))
17157 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
17159 (defun org-cdlatex-underscore-caret (&optional arg)
17160 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
17161 Revert to the normal definition outside of these fragments."
17163 (if (org-inside-LaTeX-fragment-p)
17164 (call-interactively 'cdlatex-sub-superscript)
17165 (let (org-cdlatex-mode)
17166 (call-interactively (key-binding (vector last-input-event))))))
17168 (defun org-cdlatex-math-modify (&optional arg)
17169 "Execute `cdlatex-math-modify' in LaTeX fragments.
17170 Revert to the normal definition outside of these fragments."
17172 (if (org-inside-LaTeX-fragment-p)
17173 (call-interactively 'cdlatex-math-modify)
17174 (let (org-cdlatex-mode)
17175 (call-interactively (key-binding (vector last-input-event))))))
17177 (defvar org-latex-fragment-image-overlays nil
17178 "List of overlays carrying the images of latex fragments.")
17179 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
17181 (defun org-remove-latex-fragment-image-overlays ()
17182 "Remove all overlays with LaTeX fragment images in current buffer."
17183 (mapc 'delete-overlay org-latex-fragment-image-overlays)
17184 (setq org-latex-fragment-image-overlays nil))
17186 (defun org-preview-latex-fragment (&optional subtree)
17187 "Preview the LaTeX fragment at point, or all locally or globally.
17188 If the cursor is in a LaTeX fragment, create the image and overlay
17189 it over the source code. If there is no fragment at point, display
17190 all fragments in the current text, from one headline to the next. With
17191 prefix SUBTREE, display all fragments in the current subtree. With a
17192 double prefix arg \\[universal-argument] \\[universal-argument], or when \
17193 the cursor is before the first headline,
17194 display all fragments in the buffer.
17195 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
17197 (unless buffer-file-name
17198 (error "Can't preview LaTeX fragment in a non-file buffer"))
17199 (org-remove-latex-fragment-image-overlays)
17202 (let (beg end at msg)
17204 ((or (equal subtree '(16))
17205 (not (save-excursion
17206 (re-search-backward org-outline-regexp-bol nil t))))
17207 (setq beg (point-min) end (point-max)
17208 msg "Creating images for buffer...%s"))
17209 ((equal subtree '(4))
17210 (org-back-to-heading)
17211 (setq beg (point) end (org-end-of-subtree t)
17212 msg "Creating images for subtree...%s"))
17214 (if (setq at (org-inside-LaTeX-fragment-p))
17215 (goto-char (max (point-min) (- (cdr at) 2)))
17216 (org-back-to-heading))
17217 (setq beg (point) end (progn (outline-next-heading) (point))
17218 msg (if at "Creating image...%s"
17219 "Creating images for entry...%s"))))
17221 (narrow-to-region beg end)
17224 (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
17225 (file-name-nondirectory
17226 buffer-file-name)))
17227 default-directory 'overlays msg at 'forbuffer
17228 org-latex-create-formula-image-program)
17229 (message msg "done. Use `C-c C-c' to remove images.")))))
17231 (defvar org-latex-regexps
17232 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
17233 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
17234 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
17235 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17236 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17237 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
17238 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
17239 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
17240 "Regular expressions for matching embedded LaTeX.")
17242 (defvar org-export-have-math nil) ;; dynamic scoping
17243 (defun org-format-latex (prefix &optional dir overlays msg at
17244 forbuffer processing-type)
17245 "Replace LaTeX fragments with links to an image, and produce images.
17246 Some of the options can be changed using the variable
17247 `org-format-latex-options'."
17248 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
17249 (let* ((prefixnodir (file-name-nondirectory prefix))
17250 (absprefix (expand-file-name prefix dir))
17251 (todir (file-name-directory absprefix))
17252 (opt org-format-latex-options)
17253 (matchers (plist-get opt :matchers))
17254 (re-list org-latex-regexps)
17255 (org-format-latex-header-extra
17256 (plist-get (org-infile-export-plist) :latex-header-extra))
17257 (cnt 0) txt hash link beg end re e checkdir
17258 executables-checked string
17259 m n block-type block linkfile movefile ov)
17260 ;; Check the different regular expressions
17261 (while (setq e (pop re-list))
17262 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
17263 block (if block-type "\n\n" ""))
17264 (when (member m matchers)
17265 (goto-char (point-min))
17266 (while (re-search-forward re nil t)
17267 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
17268 (not (get-text-property (match-beginning n)
17271 (not (eq (get-char-property (match-beginning n)
17273 'org-latex-overlay))))
17274 (setq org-export-have-math t)
17276 ((eq processing-type 'verbatim)
17277 ;; Leave the text verbatim, just protect it
17278 (add-text-properties (match-beginning n) (match-end n)
17279 '(org-protected t)))
17280 ((eq processing-type 'mathjax)
17281 ;; Prepare for MathJax processing
17282 (setq string (match-string n))
17283 (if (member m '("$" "$1"))
17285 (delete-region (match-beginning n) (match-end n))
17286 (goto-char (match-beginning n))
17287 (insert (org-add-props (concat "\\(" (substring string 1 -1)
17289 '(org-protected t))))
17290 (add-text-properties (match-beginning n) (match-end n)
17291 '(org-protected t))))
17292 ((or (eq processing-type 'dvipng)
17293 (eq processing-type 'imagemagick))
17294 ;; Process to an image
17295 (setq txt (match-string n)
17296 beg (match-beginning n) end (match-end n)
17298 (let (print-length print-level) ; make sure full list is printed
17299 (setq hash (sha1 (prin1-to-string
17300 (list org-format-latex-header
17301 org-format-latex-header-extra
17302 org-export-latex-default-packages-alist
17303 org-export-latex-packages-alist
17304 org-format-latex-options
17306 linkfile (format "%s_%s.png" prefix hash)
17307 movefile (format "%s_%s.png" absprefix hash)))
17308 (setq link (concat block "[[file:" linkfile "]]" block))
17309 (if msg (message msg cnt))
17311 (unless checkdir ; make sure the directory exists
17313 (or (file-directory-p todir) (make-directory todir t)))
17315 ((eq processing-type 'dvipng)
17316 (unless executables-checked
17317 (org-check-external-command
17318 "latex" "needed to convert LaTeX fragments to images")
17319 (org-check-external-command
17320 "dvipng" "needed to convert LaTeX fragments to images")
17321 (setq executables-checked t))
17322 (unless (file-exists-p movefile)
17323 (org-create-formula-image-with-dvipng
17324 txt movefile opt forbuffer)))
17325 ((eq processing-type 'imagemagick)
17326 (unless executables-checked
17327 (org-check-external-command
17328 "convert" "you need to install imagemagick")
17329 (setq executables-checked t))
17330 (unless (file-exists-p movefile)
17331 (org-create-formula-image-with-imagemagick
17332 txt movefile opt forbuffer))))
17336 (if (eq (overlay-get o 'org-overlay-type)
17337 'org-latex-overlay)
17338 (delete-overlay o)))
17339 (overlays-in beg end))
17340 (setq ov (make-overlay beg end))
17341 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
17342 (if (featurep 'xemacs)
17344 (overlay-put ov 'invisible t)
17347 (make-glyph (vector 'png :file movefile))))
17350 (list 'image :type 'png :file movefile :ascent 'center)))
17351 (push ov org-latex-fragment-image-overlays)
17353 (delete-region beg end)
17354 (insert (org-add-props link
17355 (list 'org-latex-src
17356 (replace-regexp-in-string
17358 'org-latex-src-embed-type
17359 (if block-type 'paragraph 'character))))))
17360 ((eq processing-type 'mathml)
17361 ;; Process to MathML
17362 (unless executables-checked
17363 (unless (save-match-data (org-format-latex-mathml-available-p))
17364 (error "LaTeX to MathML converter not configured"))
17365 (setq executables-checked t))
17366 (setq txt (match-string n)
17367 beg (match-beginning n) end (match-end n)
17369 (if msg (message msg cnt))
17371 (delete-region beg end)
17372 (insert (org-format-latex-as-mathml
17373 txt block-type prefix dir)))
17375 (error "Unknown conversion type %s for latex fragments"
17376 processing-type)))))))))
17378 (defun org-create-math-formula (latex-frag &optional mathml-file)
17379 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
17380 Use `org-latex-to-mathml-convert-command'. If the conversion is
17381 sucessful, return the portion between \"<math...> </math>\"
17382 elements otherwise return nil. When MATHML-FILE is specified,
17383 write the results in to that file. When invoked as an
17384 interactive command, prompt for LATEX-FRAG, with initial value
17385 set to the current active region and echo the results for user
17387 (interactive (list (let ((frag (when (org-region-active-p)
17388 (buffer-substring-no-properties
17389 (region-beginning) (region-end)))))
17390 (read-string "LaTeX Fragment: " frag nil frag))))
17391 (unless latex-frag (error "Invalid latex-frag"))
17392 (let* ((tmp-in-file (file-relative-name
17393 (make-temp-name (expand-file-name "ltxmathml-in"))))
17394 (ignore (write-region latex-frag nil tmp-in-file))
17395 (tmp-out-file (file-relative-name
17396 (make-temp-name (expand-file-name "ltxmathml-out"))))
17398 org-latex-to-mathml-convert-command
17399 `((?j . ,(shell-quote-argument
17400 (expand-file-name org-latex-to-mathml-jar-file)))
17401 (?I . ,(shell-quote-argument tmp-in-file))
17402 (?o . ,(shell-quote-argument tmp-out-file)))))
17403 mathml shell-command-output)
17404 (when (org-called-interactively-p 'any)
17405 (unless (org-format-latex-mathml-available-p)
17406 (error "LaTeX to MathML converter not configured")))
17407 (message "Running %s" cmd)
17408 (setq shell-command-output (shell-command-to-string cmd))
17410 (when (file-readable-p tmp-out-file)
17411 (with-current-buffer (find-file-noselect tmp-out-file t)
17412 (goto-char (point-min))
17413 (when (re-search-forward
17416 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
17418 (regexp-quote "</math>")) nil t)
17419 (prog1 (match-string 0) (kill-buffer))))))
17423 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
17425 (write-region mathml nil mathml-file))
17426 (when (org-called-interactively-p 'any)
17428 ((message "LaTeX to MathML conversion failed")
17429 (message shell-command-output)))
17430 (delete-file tmp-in-file)
17431 (when (file-exists-p tmp-out-file)
17432 (delete-file tmp-out-file))
17435 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
17436 prefix &optional dir)
17437 "Use `org-create-math-formula' but check local cache first."
17438 (let* ((absprefix (expand-file-name prefix dir))
17439 (print-length nil) (print-level nil)
17440 (formula-id (concat
17445 org-latex-to-mathml-convert-command)))))
17446 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
17447 (formula-cache-dir (file-name-directory formula-cache)))
17449 (unless (file-directory-p formula-cache-dir)
17450 (make-directory formula-cache-dir t))
17452 (unless (file-exists-p formula-cache)
17453 (org-create-math-formula latex-frag formula-cache))
17455 (if (file-exists-p formula-cache)
17456 ;; Successful conversion. Return the link to MathML file.
17458 (format "[[file:%s]]" (file-relative-name formula-cache dir))
17459 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
17460 'org-latex-src-embed-type (if latex-frag-type
17461 'paragraph 'character)))
17462 ;; Failed conversion. Return the LaTeX fragment verbatim
17463 (add-text-properties
17464 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
17467 ;; This function borrows from Ganesh Swami's latex2png.el
17468 (defun org-create-formula-image-with-dvipng (string tofile options buffer)
17469 "This calls dvipng."
17470 (require 'org-latex)
17471 (let* ((tmpdir (if (featurep 'xemacs)
17473 temporary-file-directory))
17474 (texfilebase (make-temp-name
17475 (expand-file-name "orgtex" tmpdir)))
17476 (texfile (concat texfilebase ".tex"))
17477 (dvifile (concat texfilebase ".dvi"))
17478 (pngfile (concat texfilebase ".png"))
17479 (fnh (if (featurep 'xemacs)
17480 (font-height (face-font 'default))
17481 (face-attribute 'default :height nil)))
17482 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17483 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17484 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17486 (bg (or (plist-get options (if buffer :background :html-background))
17488 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
17489 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
17490 (with-temp-file texfile
17491 (insert (org-splice-latex-header
17492 org-format-latex-header
17493 org-export-latex-default-packages-alist
17494 org-export-latex-packages-alist t
17495 org-format-latex-header-extra))
17496 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
17497 (require 'org-latex)
17498 (org-export-latex-fix-inputenc))
17499 (let ((dir default-directory))
17500 (condition-case nil
17503 (call-process "latex" nil nil nil texfile))
17506 (if (not (file-exists-p dvifile))
17507 (progn (message "Failed to create dvi file from %s" texfile) nil)
17508 (condition-case nil
17509 (if (featurep 'xemacs)
17510 (call-process "dvipng" nil nil nil
17515 (call-process "dvipng" nil nil nil
17518 ;;"-x" scale "-y" scale
17523 (if (not (file-exists-p pngfile))
17524 (if org-format-latex-signal-error
17525 (error "Failed to create png file from %s" texfile)
17526 (message "Failed to create png file from %s" texfile)
17528 ;; Use the requested file name and clean up
17529 (copy-file pngfile tofile 'replace)
17530 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
17531 (if (file-exists-p (concat texfilebase e))
17532 (delete-file (concat texfilebase e))))
17535 (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
17536 (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
17537 "This calls convert, which is included into imagemagick."
17538 (require 'org-latex)
17539 (let* ((tmpdir (if (featurep 'xemacs)
17541 temporary-file-directory))
17542 (texfilebase (make-temp-name
17543 (expand-file-name "orgtex" tmpdir)))
17544 (texfile (concat texfilebase ".tex"))
17545 (pdffile (concat texfilebase ".pdf"))
17546 (pngfile (concat texfilebase ".png"))
17547 (fnh (if (featurep 'xemacs)
17548 (font-height (face-font 'default))
17549 (face-attribute 'default :height nil)))
17550 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17551 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17552 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17554 (bg (or (plist-get options (if buffer :background :html-background))
17556 (if (eq fg 'default) (setq fg (org-latex-color :foreground))
17557 (setq fg (org-latex-color-format fg)))
17558 (if (eq bg 'default) (setq bg (org-latex-color :background))
17559 (setq bg (org-latex-color-format
17560 (if (string= bg "Transparent")(setq bg "white")))))
17561 (with-temp-file texfile
17562 (insert (org-splice-latex-header
17563 org-format-latex-header
17564 org-export-latex-default-packages-alist
17565 org-export-latex-packages-alist t
17566 org-format-latex-header-extra))
17567 (insert "\n\\begin{document}\n"
17568 "\\definecolor{fg}{rgb}{" fg "}\n"
17569 "\\definecolor{bg}{rgb}{" bg "}\n"
17570 "\n\\pagecolor{bg}\n"
17574 "\n\\end{document}\n" )
17575 (require 'org-latex)
17576 (org-export-latex-fix-inputenc))
17577 (let ((dir default-directory) cmd cmds latex-frags-cmds)
17578 (condition-case nil
17581 (setq cmds org-latex-to-pdf-process)
17583 (setq latex-frags-cmds (pop cmds))
17584 (if (listp latex-frags-cmds)
17586 (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
17587 (while latex-frags-cmds
17588 (setq cmd (pop latex-frags-cmds))
17589 (while (string-match "%b" cmd)
17590 (setq cmd (replace-match
17592 (shell-quote-argument texfile))
17594 (while (string-match "%f" cmd)
17595 (setq cmd (replace-match
17597 (shell-quote-argument (file-name-nondirectory texfile)))
17599 (while (string-match "%o" cmd)
17600 (setq cmd (replace-match
17602 (shell-quote-argument (file-name-directory texfile)))
17604 (setq cmd (split-string cmd))
17605 (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
17608 (if (not (file-exists-p pdffile))
17609 (progn (message "Failed to create pdf file from %s" texfile) nil)
17610 (condition-case nil
17611 (if (featurep 'xemacs)
17612 (call-process "convert" nil nil nil
17618 ;; "-sharpen" "0x1.0"
17620 (call-process "convert" nil nil nil
17626 ; "-sharpen" "0x1.0"
17629 (if (not (file-exists-p pngfile))
17630 (if org-format-latex-signal-error
17631 (error "Failed to create png file from %s" texfile)
17632 (message "Failed to create png file from %s" texfile)
17634 ;; Use the requested file name and clean up
17635 (copy-file pngfile tofile 'replace)
17636 (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
17637 (if (file-exists-p (concat texfilebase e))
17638 (delete-file (concat texfilebase e))))
17641 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
17642 "Fill a LaTeX header template TPL.
17643 In the template, the following place holders will be recognized:
17645 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
17646 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
17647 [PACKAGES] \\usepackage statements for PKG
17648 [NO-PACKAGES] do not include PKG
17649 [EXTRA] the string EXTRA
17650 [NO-EXTRA] do not include EXTRA
17652 For backward compatibility, if both the positive and the negative place
17653 holder is missing, the positive one (without the \"NO-\") will be
17654 assumed to be present at the end of the template.
17655 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
17657 SNIPPETS-P indicates if this is run to create snippet images for HTML."
17658 (let (rpl (end ""))
17659 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
17660 (setq rpl (if (or (match-end 1) (not def-pkg))
17661 "" (org-latex-packages-to-string def-pkg snippets-p t))
17662 tpl (replace-match rpl t t tpl))
17663 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
17665 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
17666 (setq rpl (if (or (match-end 1) (not pkg))
17667 "" (org-latex-packages-to-string pkg snippets-p t))
17668 tpl (replace-match rpl t t tpl))
17671 (org-latex-packages-to-string pkg snippets-p)))))
17673 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
17674 (setq rpl (if (or (match-end 1) (not extra))
17675 "" (concat extra "\n"))
17676 tpl (replace-match rpl t t tpl))
17677 (if (and extra (string-match "\\S-" extra))
17678 (setq end (concat end "\n" extra))))
17680 (if (string-match "\\S-" end)
17681 (concat tpl "\n" end)
17684 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
17685 "Turn an alist of packages into a string with the \\usepackage macros."
17686 (setq pkg (mapconcat (lambda(p)
17689 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
17690 (format "%% Package %s omitted" (cadr p)))
17691 ((equal "" (car p))
17692 (format "\\usepackage{%s}" (cadr p)))
17694 (format "\\usepackage[%s]{%s}"
17695 (car p) (cadr p)))))
17698 (if newline (concat pkg "\n") pkg))
17700 (defun org-dvipng-color (attr)
17701 "Return a RGB color specification for dvipng."
17702 (apply 'format "rgb %s %s %s"
17703 (mapcar 'org-normalize-color
17704 (if (featurep 'xemacs)
17705 (color-rgb-components
17706 (face-property 'default
17707 (cond ((eq attr :foreground) 'foreground)
17708 ((eq attr :background) 'background))))
17709 (color-values (face-attribute 'default attr nil))))))
17711 (defun org-latex-color (attr)
17712 "Return a RGB color for the LaTeX color package."
17713 (apply 'format "%s,%s,%s"
17714 (mapcar 'org-normalize-color
17715 (if (featurep 'xemacs)
17716 (color-rgb-components
17717 (face-property 'default
17718 (cond ((eq attr :foreground) 'foreground)
17719 ((eq attr :background) 'background))))
17720 (color-values (face-attribute 'default attr nil))))))
17722 (defun org-latex-color-format (color-name)
17723 "Convert COLOR-NAME to a RGB color value."
17724 (apply 'format "%s,%s,%s"
17725 (mapcar 'org-normalize-color
17726 (color-values color-name))))
17728 (defun org-normalize-color (value)
17729 "Return string to be used as color value for an RGB component."
17730 (format "%g" (/ value 65535.0)))
17735 (defvar org-inline-image-overlays nil)
17736 (make-variable-buffer-local 'org-inline-image-overlays)
17738 (defun org-toggle-inline-images (&optional include-linked)
17739 "Toggle the display of inline images.
17740 INCLUDE-LINKED is passed to `org-display-inline-images'."
17742 (if org-inline-image-overlays
17744 (org-remove-inline-images)
17745 (message "Inline image display turned off"))
17746 (org-display-inline-images include-linked)
17747 (if org-inline-image-overlays
17748 (message "%d images displayed inline"
17749 (length org-inline-image-overlays))
17750 (message "No images to display inline"))))
17752 (defun org-redisplay-inline-images ()
17753 "Refresh the display of inline images."
17755 (if (not org-inline-image-overlays)
17756 (org-toggle-inline-images)
17757 (org-toggle-inline-images)
17758 (org-toggle-inline-images)))
17760 (defun org-display-inline-images (&optional include-linked refresh beg end)
17761 "Display inline images.
17762 Normally only links without a description part are inlined, because this
17763 is how it will work for export. When INCLUDE-LINKED is set, also links
17764 with a description part will be inlined. This can be nice for a quick
17765 look at those images, but it does not reflect what exported files will look
17767 When REFRESH is set, refresh existing images between BEG and END.
17768 This will create new image displays only if necessary.
17769 BEG and END default to the buffer boundaries."
17772 (org-remove-inline-images)
17773 (if (fboundp 'clear-image-cache) (clear-image-cache)))
17777 (setq beg (or beg (point-min)) end (or end (point-max)))
17779 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
17780 (substring (org-image-file-name-regexp) 0 -2)
17781 "\\)\\]" (if include-linked "" "\\]")))
17783 (while (re-search-forward re end t)
17784 (setq old (get-char-property-and-overlay (match-beginning 1)
17785 'org-image-overlay))
17786 (setq file (expand-file-name
17787 (concat (or (match-string 3) "") (match-string 4))))
17788 (when (file-exists-p file)
17789 (if (and (car-safe old) refresh)
17790 (image-refresh (overlay-get (cdr old) 'display))
17791 (setq img (save-match-data (create-image file)))
17793 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
17794 (overlay-put ov 'display img)
17795 (overlay-put ov 'face 'default)
17796 (overlay-put ov 'org-image-overlay t)
17797 (overlay-put ov 'modification-hooks
17798 (list 'org-display-inline-remove-overlay))
17799 (push ov org-inline-image-overlays)))))))))
17801 (org-define-obsolete-function-alias
17802 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
17804 (defun org-display-inline-remove-overlay (ov after beg end &optional len)
17805 "Remove inline-display overlay if a corresponding region is modified."
17806 (let ((inhibit-modification-hooks t))
17807 (when (and ov after)
17808 (delete ov org-inline-image-overlays)
17809 (delete-overlay ov))))
17811 (defun org-remove-inline-images ()
17812 "Remove inline display of images."
17814 (mapc 'delete-overlay org-inline-image-overlays)
17815 (setq org-inline-image-overlays nil))
17819 ;; Outline functions from `outline-mode-prefix-map'
17820 ;; that can be remapped in Org:
17821 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
17822 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
17823 (define-key org-mode-map [remap outline-forward-same-level]
17824 'org-forward-heading-same-level)
17825 (define-key org-mode-map [remap outline-backward-same-level]
17826 'org-backward-heading-same-level)
17827 (define-key org-mode-map [remap show-branches]
17828 'org-kill-note-or-show-branches)
17829 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
17830 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
17831 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
17833 ;; Outline functions from `outline-mode-prefix-map' that can not
17834 ;; be remapped in Org:
17836 ;; - the column "key binding" shows whether the Outline function is still
17837 ;; available in Org mode on the same key that it has been bound to in
17839 ;; - "overridden": key used for a different functionality in Org mode
17840 ;; - else: key still bound to the same Outline function in Org mode
17842 ;; | Outline function | key binding | Org replacement |
17843 ;; |------------------------------------+-------------+-----------------------|
17844 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
17845 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
17846 ;; | `outline-up-heading' | `C-c C-u' | still same function |
17847 ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
17848 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
17849 ;; | `show-entry' | overridden | no replacement |
17850 ;; | `show-children' | `C-c C-i' | visibility cycling |
17851 ;; | `show-branches' | `C-c C-k' | still same function |
17852 ;; | `show-subtree' | overridden | visibility cycling |
17853 ;; | `show-all' | overridden | no replacement |
17854 ;; | `hide-subtree' | overridden | visibility cycling |
17855 ;; | `hide-body' | overridden | no replacement |
17856 ;; | `hide-entry' | overridden | visibility cycling |
17857 ;; | `hide-leaves' | overridden | no replacement |
17858 ;; | `hide-sublevels' | overridden | no replacement |
17859 ;; | `hide-other' | overridden | no replacement |
17861 ;; Make `C-c C-x' a prefix key
17862 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
17864 ;; TAB key with modifiers
17865 (org-defkey org-mode-map "\C-i" 'org-cycle)
17866 (org-defkey org-mode-map [(tab)] 'org-cycle)
17867 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
17868 (org-defkey org-mode-map "\M-\t" 'pcomplete)
17869 ;; The following line is necessary under Suse GNU/Linux
17870 (unless (featurep 'xemacs)
17871 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
17872 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
17873 (define-key org-mode-map [backtab] 'org-shifttab)
17875 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
17876 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
17877 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
17879 ;; Cursor keys with modifiers
17880 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
17881 (org-defkey org-mode-map [(meta right)] 'org-metaright)
17882 (org-defkey org-mode-map [(meta up)] 'org-metaup)
17883 (org-defkey org-mode-map [(meta down)] 'org-metadown)
17885 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
17886 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
17887 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
17888 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
17890 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
17891 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
17892 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
17893 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
17895 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
17896 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
17897 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
17898 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
17901 (define-key org-mode-map org-babel-key-prefix org-babel-map)
17902 (mapc (lambda (pair)
17903 (define-key org-babel-map (car pair) (cdr pair)))
17904 org-babel-key-bindings)
17906 ;;; Extra keys for tty access.
17907 ;; We only set them when really needed because otherwise the
17908 ;; menus don't show the simple keys
17910 (when (or org-use-extra-keys
17911 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
17912 (not window-system))
17913 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
17914 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
17915 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
17916 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
17917 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
17918 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
17919 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
17920 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
17921 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
17922 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
17923 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
17924 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
17925 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
17926 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
17927 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
17928 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
17929 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
17930 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
17931 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
17932 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
17933 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
17934 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
17935 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
17936 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
17937 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
17938 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
17939 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
17940 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
17942 ;; All the other keys
17944 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
17945 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
17946 (if (boundp 'narrow-map)
17947 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
17948 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
17949 (if (boundp 'narrow-map)
17950 (org-defkey narrow-map "b" 'org-narrow-to-block)
17951 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
17952 (if (boundp 'narrow-map)
17953 (org-defkey narrow-map "e" 'org-narrow-to-element)
17954 (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
17955 (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
17956 (org-defkey org-mode-map "\M-}" 'org-forward-element)
17957 (org-defkey org-mode-map "\M-{" 'org-backward-element)
17958 (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
17959 (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
17960 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
17961 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
17962 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
17963 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
17964 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
17965 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
17966 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
17967 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
17968 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
17969 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
17970 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
17971 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
17972 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
17973 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
17974 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
17975 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
17976 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
17977 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
17978 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
17979 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
17980 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
17981 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
17982 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
17983 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
17984 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
17985 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
17986 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
17987 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
17988 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
17989 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
17990 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
17991 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
17992 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
17993 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
17994 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
17995 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
17996 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
17997 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
17998 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
17999 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
18000 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
18001 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
18002 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
18003 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
18004 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
18005 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
18006 (org-defkey org-mode-map "\C-c^" 'org-sort)
18007 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
18008 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
18009 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
18010 (org-defkey org-mode-map "\C-m" 'org-return)
18011 (org-defkey org-mode-map "\C-j" 'org-return-indent)
18012 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
18013 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
18014 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
18015 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
18016 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
18017 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
18018 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
18019 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
18020 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
18021 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
18022 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
18023 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
18024 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
18025 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
18026 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
18027 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
18028 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
18029 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
18030 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
18031 (org-defkey org-mode-map "\M-h" 'org-mark-element)
18032 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
18033 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
18035 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
18036 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
18037 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
18039 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
18040 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
18041 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
18042 (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
18043 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
18044 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
18045 (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
18046 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
18047 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
18048 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
18049 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
18050 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
18051 (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
18052 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
18053 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
18054 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
18055 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
18056 (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
18057 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
18058 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
18059 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
18060 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
18062 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
18063 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
18064 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
18065 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
18066 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
18068 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
18070 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
18072 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
18073 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
18075 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
18078 (when (featurep 'xemacs)
18079 (org-defkey org-mode-map 'button3 'popup-mode-menu))
18082 (defconst org-speed-commands-default
18084 ("Outline Navigation")
18085 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
18086 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
18087 ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
18088 ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
18089 ("u" . (org-speed-move-safe 'outline-up-heading))
18091 ("g" . (org-refile t))
18092 ("Outline Visibility")
18094 ("C" . org-shifttab)
18095 (" " . org-display-outline-path)
18096 ("=" . org-columns)
18097 ("Outline Structure Editing")
18098 ("U" . org-shiftmetaup)
18099 ("D" . org-shiftmetadown)
18100 ("r" . org-metaright)
18101 ("l" . org-metaleft)
18102 ("R" . org-shiftmetaright)
18103 ("L" . org-shiftmetaleft)
18104 ("i" . (progn (forward-char 1) (call-interactively
18105 'org-insert-heading-respect-content)))
18108 ("a" . org-archive-subtree-default-with-confirmation)
18109 ("." . org-mark-subtree)
18110 ("#" . org-toggle-comment)
18112 ("I" . org-clock-in)
18113 ("O" . org-clock-out)
18114 ("Meta Data Editing")
18116 ("," . (org-priority))
18117 ("0" . (org-priority ?\ ))
18118 ("1" . (org-priority ?A))
18119 ("2" . (org-priority ?B))
18120 ("3" . (org-priority ?C))
18121 (":" . org-set-tags-command)
18122 ("e" . org-set-effort)
18123 ("E" . org-inc-effort)
18124 ("W" . (lambda(m) (interactive "sMinutes before warning: ")
18125 (org-entry-put (point) "APPT_WARNTIME" m)))
18126 ("Agenda Views etc")
18128 ("/" . org-sparse-tree)
18130 ("o" . org-open-at-point)
18131 ("?" . org-speed-command-help)
18132 ("<" . (org-agenda-set-restriction-lock 'subtree))
18133 (">" . (org-agenda-remove-restriction-lock))
18135 "The default speed commands.")
18137 (defun org-print-speed-command (e)
18138 (if (> (length (car e)) 1)
18143 (princ (make-string (length (car e)) ?-))
18147 (if (symbolp (cdr e))
18148 (princ (symbol-name (cdr e)))
18152 (defun org-speed-command-help ()
18153 "Show the available speed commands."
18155 (if (not org-use-speed-commands)
18156 (error "Speed commands are not activated, customize `org-use-speed-commands'")
18157 (with-output-to-temp-buffer "*Help*"
18158 (princ "User-defined Speed commands\n===========================\n")
18159 (mapc 'org-print-speed-command org-speed-commands-user)
18161 (princ "Built-in Speed commands\n=======================\n")
18162 (mapc 'org-print-speed-command org-speed-commands-default))
18163 (with-current-buffer "*Help*"
18164 (setq truncate-lines t))))
18166 (defun org-speed-move-safe (cmd)
18167 "Execute CMD, but make sure that the cursor always ends up in a headline.
18168 If not, return to the original position and throw an error."
18170 (let ((pos (point)))
18171 (call-interactively cmd)
18172 (unless (and (bolp) (org-at-heading-p))
18174 (error "Boundary reached while executing %s" cmd))))
18176 (defvar org-self-insert-command-undo-counter 0)
18178 (defvar org-table-auto-blank-field) ; defined in org-table.el
18179 (defvar org-speed-command nil)
18181 (org-define-obsolete-function-alias
18182 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
18184 (defun org-speed-command-activate (keys)
18185 "Hook for activating single-letter speed commands.
18186 `org-speed-commands-default' specifies a minimal command set.
18187 Use `org-speed-commands-user' for further customization."
18188 (when (or (and (bolp) (looking-at org-outline-regexp))
18189 (and (functionp org-use-speed-commands)
18190 (funcall org-use-speed-commands)))
18191 (cdr (assoc keys (append org-speed-commands-user
18192 org-speed-commands-default)))))
18194 (org-define-obsolete-function-alias
18195 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
18197 (defun org-babel-speed-command-activate (keys)
18198 "Hook for activating single-letter code block commands."
18199 (when (and (bolp) (looking-at org-babel-src-block-regexp))
18200 (cdr (assoc keys org-babel-key-bindings))))
18202 (defcustom org-speed-command-hook
18203 '(org-speed-command-default-hook org-babel-speed-command-hook)
18204 "Hook for activating speed commands at strategic locations.
18205 Hook functions are called in sequence until a valid handler is
18208 Each hook takes a single argument, a user-pressed command key
18209 which is also a `self-insert-command' from the global map.
18211 Within the hook, examine the cursor position and the command key
18212 and return nil or a valid handler as appropriate. Handler could
18213 be one of an interactive command, a function, or a form.
18215 Set `org-use-speed-commands' to non-nil value to enable this
18216 hook. The default setting is `org-speed-command-activate'."
18217 :group 'org-structure
18221 (defun org-self-insert-command (N)
18222 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
18223 If the cursor is in a table looking at whitespace, the whitespace is
18224 overwritten, and the table is not marked as requiring realignment."
18226 (org-check-before-invisible-edit 'insert)
18228 ((and org-use-speed-commands
18229 (setq org-speed-command
18230 (run-hook-with-args-until-success
18231 'org-speed-command-hook (this-command-keys))))
18233 ((commandp org-speed-command)
18234 (setq this-command org-speed-command)
18235 (call-interactively org-speed-command))
18236 ((functionp org-speed-command)
18237 (funcall org-speed-command))
18238 ((and org-speed-command (listp org-speed-command))
18239 (eval org-speed-command))
18240 (t (let (org-use-speed-commands)
18241 (call-interactively 'org-self-insert-command)))))
18245 ;; check if we blank the field, and if that triggers align
18246 (and (featurep 'org-table) org-table-auto-blank-field
18247 (member last-command
18248 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
18249 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
18250 ;; got extra space, this field does not determine column width
18251 (let (org-table-may-need-update) (org-table-blank-field))
18252 ;; no extra space, this field may determine column width
18253 (org-table-blank-field)))
18256 (looking-at "[^|\n]* |"))
18257 (let (org-table-may-need-update)
18258 (goto-char (1- (match-end 0)))
18259 (backward-delete-char 1)
18260 (goto-char (match-beginning 0))
18261 (self-insert-command N)))
18263 (setq org-table-may-need-update t)
18264 (self-insert-command N)
18265 (org-fix-tags-on-the-fly)
18266 (if org-self-insert-cluster-for-undo
18267 (if (not (eq last-command 'org-self-insert-command))
18268 (setq org-self-insert-command-undo-counter 1)
18269 (if (>= org-self-insert-command-undo-counter 20)
18270 (setq org-self-insert-command-undo-counter 1)
18271 (and (> org-self-insert-command-undo-counter 0)
18272 buffer-undo-list (listp buffer-undo-list)
18273 (not (cadr buffer-undo-list)) ; remove nil entry
18274 (setcdr buffer-undo-list (cddr buffer-undo-list)))
18275 (setq org-self-insert-command-undo-counter
18276 (1+ org-self-insert-command-undo-counter))))))))
18278 (defun org-check-before-invisible-edit (kind)
18279 "Check is editing if kind KIND would be dangerous with invisible text around.
18280 The detailed reaction depends on the user option `org-catch-invisible-edits'."
18281 ;; First, try to get out of here as quickly as possible, to reduce overhead
18282 (if (and org-catch-invisible-edits
18283 (or (not (boundp 'visible-mode)) (not visible-mode))
18284 (or (get-char-property (point) 'invisible)
18285 (get-char-property (max (point-min) (1- (point))) 'invisible)))
18286 ;; OK, we need to take a closer look
18287 (let* ((invisible-at-point (get-char-property (point) 'invisible))
18288 (invisible-before-point (if (bobp) nil (get-char-property
18289 (1- (point)) 'invisible)))
18290 (border-and-ok-direction
18292 ;; Check if we are acting predictably before invisible text
18293 (and invisible-at-point (not invisible-before-point)
18294 (memq kind '(insert delete-backward)))
18295 ;; Check if we are acting predictably after invisible text
18296 ;; This works not well, and I have turned it off. It seems
18297 ;; better to always show and stop after invisible text.
18298 ;; (and (not invisible-at-point) invisible-before-point
18299 ;; (memq kind '(insert delete)))
18301 (when (or (memq invisible-at-point '(outline org-hide-block t))
18302 (memq invisible-before-point '(outline org-hide-block t)))
18303 (if (eq org-catch-invisible-edits 'error)
18304 (error "Editing in invisible areas is prohibited - make visible first"))
18305 (if (and org-custom-properties-overlays
18306 (y-or-n-p "Display invisible properties in this buffer? "))
18307 (org-toggle-custom-properties-visibility)
18308 ;; Make the area visible
18310 (if invisible-before-point
18311 (goto-char (previous-single-char-property-change
18312 (point) 'invisible)))
18315 ((eq org-catch-invisible-edits 'show)
18316 ;; That's it, we do the edit after showing
18318 "Unfolding invisible region around point before editing")
18320 ((and (eq org-catch-invisible-edits 'smart)
18321 border-and-ok-direction)
18322 (message "Unfolding invisible region around point before editing"))
18324 ;; Don't do the edit, make the user repeat it in full visibility
18325 (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
18327 (defun org-fix-tags-on-the-fly ()
18328 (when (and (equal (char-after (point-at-bol)) ?*)
18329 (org-at-heading-p))
18330 (org-align-tags-here org-tags-column)))
18332 (defun org-delete-backward-char (N)
18333 "Like `delete-backward-char', insert whitespace at field end in tables.
18334 When deleting backwards, in tables this function will insert whitespace in
18335 front of the next \"|\" separator, to keep the table aligned. The table will
18336 still be marked for re-alignment if the field did fill the entire column,
18337 because, in this case the deletion might narrow the column."
18340 (org-check-before-invisible-edit 'delete-backward)
18341 (if (and (org-table-p)
18343 (string-match "|" (buffer-substring (point-at-bol) (point)))
18344 (looking-at ".*?|"))
18345 (let ((pos (point))
18346 (noalign (looking-at "[^|\n\r]* |"))
18347 (c org-table-may-need-update))
18348 (backward-delete-char N)
18349 (if (not overwrite-mode)
18351 (skip-chars-forward "^|")
18353 (goto-char (1- pos))))
18354 ;; noalign: if there were two spaces at the end, this field
18355 ;; does not determine the width of the column.
18356 (if noalign (setq org-table-may-need-update c)))
18357 (backward-delete-char N)
18358 (org-fix-tags-on-the-fly))))
18360 (defun org-delete-char (N)
18361 "Like `delete-char', but insert whitespace at field end in tables.
18362 When deleting characters, in tables this function will insert whitespace in
18363 front of the next \"|\" separator, to keep the table aligned. The table will
18364 still be marked for re-alignment if the field did fill the entire column,
18365 because, in this case the deletion might narrow the column."
18368 (org-check-before-invisible-edit 'delete)
18369 (if (and (org-table-p)
18371 (not (= (char-after) ?|))
18373 (if (looking-at ".*?|")
18374 (let ((pos (point))
18375 (noalign (looking-at "[^|\n\r]* |"))
18376 (c org-table-may-need-update))
18377 (replace-match (concat
18378 (substring (match-string 0) 1 -1)
18381 ;; noalign: if there were two spaces at the end, this field
18382 ;; does not determine the width of the column.
18383 (if noalign (setq org-table-may-need-update c)))
18386 (org-fix-tags-on-the-fly))))
18388 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
18389 (put 'org-self-insert-command 'delete-selection t)
18390 (put 'orgtbl-self-insert-command 'delete-selection t)
18391 (put 'org-delete-char 'delete-selection 'supersede)
18392 (put 'org-delete-backward-char 'delete-selection 'supersede)
18393 (put 'org-yank 'delete-selection 'yank)
18395 ;; Make `flyspell-mode' delay after some commands
18396 (put 'org-self-insert-command 'flyspell-delayed t)
18397 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
18398 (put 'org-delete-char 'flyspell-delayed t)
18399 (put 'org-delete-backward-char 'flyspell-delayed t)
18401 ;; Make pabbrev-mode expand after org-mode commands
18402 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
18403 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
18405 ;; How to do this: Measure non-white length of current string
18406 ;; If equal to column width, we should realign.
18408 (defun org-remap (map &rest commands)
18409 "In MAP, remap the functions given in COMMANDS.
18410 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
18413 (setq old (pop commands) new (pop commands))
18414 (if (fboundp 'command-remapping)
18415 (org-defkey map (vector 'remap old) new)
18416 (substitute-key-definition old new map global-map)))))
18418 (when (eq org-enable-table-editor 'optimized)
18419 ;; If the user wants maximum table support, we need to hijack
18420 ;; some standard editing functions
18421 (org-remap org-mode-map
18422 'self-insert-command 'org-self-insert-command
18423 'delete-char 'org-delete-char
18424 'delete-backward-char 'org-delete-backward-char)
18425 (org-defkey org-mode-map "|" 'org-force-self-insert))
18427 (defvar org-ctrl-c-ctrl-c-hook nil
18428 "Hook for functions attaching themselves to `C-c C-c'.
18430 This can be used to add additional functionality to the C-c C-c
18431 key which executes context-dependent commands. This hook is run
18432 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
18433 run after the last test.
18435 Each function will be called with no arguments. The function
18436 must check if the context is appropriate for it to act. If yes,
18437 it should do its thing and then return a non-nil value. If the
18438 context is wrong, just do nothing and return nil.")
18440 (defvar org-ctrl-c-ctrl-c-final-hook nil
18441 "Hook for functions attaching themselves to `C-c C-c'.
18443 This can be used to add additional functionality to the C-c C-c
18444 key which executes context-dependent commands. This hook is run
18445 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
18446 before the first test.
18448 Each function will be called with no arguments. The function
18449 must check if the context is appropriate for it to act. If yes,
18450 it should do its thing and then return a non-nil value. If the
18451 context is wrong, just do nothing and return nil.")
18453 (defvar org-tab-first-hook nil
18454 "Hook for functions to attach themselves to TAB.
18455 See `org-ctrl-c-ctrl-c-hook' for more information.
18456 This hook runs as the first action when TAB is pressed, even before
18457 `org-cycle' messes around with the `outline-regexp' to cater for
18458 inline tasks and plain list item folding.
18459 If any function in this hook returns t, any other actions that
18460 would have been caused by TAB (such as table field motion or visibility
18461 cycling) will not occur.")
18463 (defvar org-tab-after-check-for-table-hook nil
18464 "Hook for functions to attach themselves to TAB.
18465 See `org-ctrl-c-ctrl-c-hook' for more information.
18466 This hook runs after it has been established that the cursor is not in a
18467 table, but before checking if the cursor is in a headline or if global cycling
18469 If any function in this hook returns t, not other actions like visibility
18470 cycling will be done.")
18472 (defvar org-tab-after-check-for-cycling-hook nil
18473 "Hook for functions to attach themselves to TAB.
18474 See `org-ctrl-c-ctrl-c-hook' for more information.
18475 This hook runs after it has been established that not table field motion and
18476 not visibility should be done because of current context. This is probably
18477 the place where a package like yasnippets can hook in.")
18479 (defvar org-tab-before-tab-emulation-hook nil
18480 "Hook for functions to attach themselves to TAB.
18481 See `org-ctrl-c-ctrl-c-hook' for more information.
18482 This hook runs after every other options for TAB have been exhausted, but
18483 before indentation and \t insertion takes place.")
18485 (defvar org-metaleft-hook nil
18486 "Hook for functions attaching themselves to `M-left'.
18487 See `org-ctrl-c-ctrl-c-hook' for more information.")
18488 (defvar org-metaright-hook nil
18489 "Hook for functions attaching themselves to `M-right'.
18490 See `org-ctrl-c-ctrl-c-hook' for more information.")
18491 (defvar org-metaup-hook nil
18492 "Hook for functions attaching themselves to `M-up'.
18493 See `org-ctrl-c-ctrl-c-hook' for more information.")
18494 (defvar org-metadown-hook nil
18495 "Hook for functions attaching themselves to `M-down'.
18496 See `org-ctrl-c-ctrl-c-hook' for more information.")
18497 (defvar org-shiftmetaleft-hook nil
18498 "Hook for functions attaching themselves to `M-S-left'.
18499 See `org-ctrl-c-ctrl-c-hook' for more information.")
18500 (defvar org-shiftmetaright-hook nil
18501 "Hook for functions attaching themselves to `M-S-right'.
18502 See `org-ctrl-c-ctrl-c-hook' for more information.")
18503 (defvar org-shiftmetaup-hook nil
18504 "Hook for functions attaching themselves to `M-S-up'.
18505 See `org-ctrl-c-ctrl-c-hook' for more information.")
18506 (defvar org-shiftmetadown-hook nil
18507 "Hook for functions attaching themselves to `M-S-down'.
18508 See `org-ctrl-c-ctrl-c-hook' for more information.")
18509 (defvar org-metareturn-hook nil
18510 "Hook for functions attaching themselves to `M-RET'.
18511 See `org-ctrl-c-ctrl-c-hook' for more information.")
18512 (defvar org-shiftup-hook nil
18513 "Hook for functions attaching themselves to `S-up'.
18514 See `org-ctrl-c-ctrl-c-hook' for more information.")
18515 (defvar org-shiftup-final-hook nil
18516 "Hook for functions attaching themselves to `S-up'.
18517 This one runs after all other options except shift-select have been excluded.
18518 See `org-ctrl-c-ctrl-c-hook' for more information.")
18519 (defvar org-shiftdown-hook nil
18520 "Hook for functions attaching themselves to `S-down'.
18521 See `org-ctrl-c-ctrl-c-hook' for more information.")
18522 (defvar org-shiftdown-final-hook nil
18523 "Hook for functions attaching themselves to `S-down'.
18524 This one runs after all other options except shift-select have been excluded.
18525 See `org-ctrl-c-ctrl-c-hook' for more information.")
18526 (defvar org-shiftleft-hook nil
18527 "Hook for functions attaching themselves to `S-left'.
18528 See `org-ctrl-c-ctrl-c-hook' for more information.")
18529 (defvar org-shiftleft-final-hook nil
18530 "Hook for functions attaching themselves to `S-left'.
18531 This one runs after all other options except shift-select have been excluded.
18532 See `org-ctrl-c-ctrl-c-hook' for more information.")
18533 (defvar org-shiftright-hook nil
18534 "Hook for functions attaching themselves to `S-right'.
18535 See `org-ctrl-c-ctrl-c-hook' for more information.")
18536 (defvar org-shiftright-final-hook nil
18537 "Hook for functions attaching themselves to `S-right'.
18538 This one runs after all other options except shift-select have been excluded.
18539 See `org-ctrl-c-ctrl-c-hook' for more information.")
18541 (defun org-modifier-cursor-error ()
18542 "Throw an error, a modified cursor command was applied in wrong context."
18543 (error "This command is active in special context like tables, headlines or items"))
18545 (defun org-shiftselect-error ()
18546 "Throw an error because Shift-Cursor command was applied in wrong context."
18547 (if (and (boundp 'shift-select-mode) shift-select-mode)
18548 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
18549 (error "This command works only in special context like headlines or timestamps")))
18551 (defun org-call-for-shift-select (cmd)
18552 (let ((this-command-keys-shift-translated t))
18553 (call-interactively cmd)))
18555 (defun org-shifttab (&optional arg)
18556 "Global visibility cycling or move to previous table field.
18557 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
18559 See the individual commands for more information."
18562 ((org-at-table-p) (call-interactively 'org-table-previous-field))
18564 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
18565 (message "Content view to level: %d" arg)
18566 (org-content (prefix-numeric-value arg2))
18567 (setq org-cycle-global-status 'overview)))
18568 (t (call-interactively 'org-global-cycle))))
18570 (defun org-shiftmetaleft ()
18571 "Promote subtree or delete table column.
18572 Calls `org-promote-subtree', `org-outdent-item-tree', or
18573 `org-table-delete-column', depending on context. See the
18574 individual commands for more information."
18577 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
18578 ((org-at-table-p) (call-interactively 'org-table-delete-column))
18579 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
18580 ((if (not (org-region-active-p)) (org-at-item-p)
18581 (save-excursion (goto-char (region-beginning))
18583 (call-interactively 'org-outdent-item-tree))
18584 (t (org-modifier-cursor-error))))
18586 (defun org-shiftmetaright ()
18587 "Demote subtree or insert table column.
18588 Calls `org-demote-subtree', `org-indent-item-tree', or
18589 `org-table-insert-column', depending on context. See the
18590 individual commands for more information."
18593 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
18594 ((org-at-table-p) (call-interactively 'org-table-insert-column))
18595 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
18596 ((if (not (org-region-active-p)) (org-at-item-p)
18597 (save-excursion (goto-char (region-beginning))
18599 (call-interactively 'org-indent-item-tree))
18600 (t (org-modifier-cursor-error))))
18602 (defun org-shiftmetaup (&optional arg)
18603 "Move subtree up or kill table row.
18604 Calls `org-move-subtree-up' or `org-table-kill-row' or
18605 `org-move-item-up' or `org-timestamp-up', depending on context.
18606 See the individual commands for more information."
18609 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
18610 ((org-at-table-p) (call-interactively 'org-table-kill-row))
18611 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18612 ((org-at-item-p) (call-interactively 'org-move-item-up))
18613 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18614 (call-interactively 'org-timestamp-up)))
18615 (t (org-modifier-cursor-error))))
18617 (defun org-shiftmetadown (&optional arg)
18618 "Move subtree down or insert table row.
18619 Calls `org-move-subtree-down' or `org-table-insert-row' or
18620 `org-move-item-down' or `org-timestamp-up', depending on context.
18621 See the individual commands for more information."
18624 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
18625 ((org-at-table-p) (call-interactively 'org-table-insert-row))
18626 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18627 ((org-at-item-p) (call-interactively 'org-move-item-down))
18628 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18629 (call-interactively 'org-timestamp-down)))
18630 (t (org-modifier-cursor-error))))
18632 (defsubst org-hidden-tree-error ()
18634 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
18636 (defun org-metaleft (&optional arg)
18637 "Promote heading or move table column to left.
18638 Calls `org-do-promote' or `org-table-move-column', depending on context.
18639 With no specific context, calls the Emacs default `backward-word'.
18640 See the individual commands for more information."
18643 ((run-hook-with-args-until-success 'org-metaleft-hook))
18644 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
18645 ((org-with-limited-levels
18646 (or (org-at-heading-p)
18647 (and (org-region-active-p)
18649 (goto-char (region-beginning))
18650 (org-at-heading-p)))))
18651 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18652 (call-interactively 'org-do-promote))
18653 ;; At an inline task.
18654 ((org-at-heading-p)
18655 (call-interactively 'org-inlinetask-promote))
18656 ((or (org-at-item-p)
18657 (and (org-region-active-p)
18659 (goto-char (region-beginning))
18661 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18662 (call-interactively 'org-outdent-item))
18663 (t (call-interactively 'backward-word))))
18665 (defun org-metaright (&optional arg)
18666 "Demote a subtree, a list item or move table column to right.
18667 In front of a drawer or a block keyword, indent it correctly.
18668 With no specific context, calls the Emacs default `forward-word'.
18669 See the individual commands for more information."
18672 ((run-hook-with-args-until-success 'org-metaright-hook))
18673 ((org-at-table-p) (call-interactively 'org-table-move-column))
18674 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
18675 ((org-at-block-p) (call-interactively 'org-indent-block))
18676 ((org-with-limited-levels
18677 (or (org-at-heading-p)
18678 (and (org-region-active-p)
18680 (goto-char (region-beginning))
18681 (org-at-heading-p)))))
18682 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18683 (call-interactively 'org-do-demote))
18684 ;; At an inline task.
18685 ((org-at-heading-p)
18686 (call-interactively 'org-inlinetask-demote))
18687 ((or (org-at-item-p)
18688 (and (org-region-active-p)
18690 (goto-char (region-beginning))
18692 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18693 (call-interactively 'org-indent-item))
18694 (t (call-interactively 'forward-word))))
18696 (defun org-check-for-hidden (what)
18697 "Check if there are hidden headlines/items in the current visual line.
18698 WHAT can be either `headlines' or `items'. If the current line is
18699 an outline or item heading and it has a folded subtree below it,
18700 this function returns t, nil otherwise."
18702 ((eq what 'headlines) org-outline-regexp-bol)
18703 ((eq what 'items) (org-item-beginning-re))
18704 (t (error "This should not happen"))))
18708 (unless (org-region-active-p)
18709 (setq beg (point-at-bol))
18710 (beginning-of-line 2)
18711 (while (and (not (eobp)) ;; this is like `next-line'
18712 (get-char-property (1- (point)) 'invisible))
18713 (beginning-of-line 2))
18716 (goto-char (point-at-eol))
18717 (setq end (max end (point)))
18718 (while (re-search-forward re end t)
18719 (if (get-char-property (match-beginning 0) 'invisible)
18723 (autoload 'org-element-at-point "org-element")
18724 (autoload 'org-element-type "org-element")
18726 (declare-function org-element-at-point "org-element" (&optional keep-trail))
18727 (declare-function org-element-type "org-element" (element))
18728 (declare-function org-element-contents "org-element" (element))
18729 (declare-function org-element-property "org-element" (property element))
18730 (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
18731 (declare-function org-element-nested-p "org-element" (elem-a elem-b))
18732 (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
18733 (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
18734 (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
18736 (defun org-metaup (&optional arg)
18737 "Move subtree up or move table row up.
18738 Calls `org-move-subtree-up' or `org-table-move-row' or
18739 `org-move-item-up', depending on context. See the individual commands
18740 for more information."
18743 ((run-hook-with-args-until-success 'org-metaup-hook))
18744 ((org-region-active-p)
18745 (let* ((a (min (region-beginning) (region-end)))
18746 (b (1- (max (region-beginning) (region-end))))
18747 (c (save-excursion (goto-char a)
18748 (move-beginning-of-line 0)))
18749 (d (save-excursion (goto-char a)
18750 (move-end-of-line 0) (point))))
18751 (transpose-regions a b c d)
18753 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
18754 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18755 ((org-at-item-p) (call-interactively 'org-move-item-up))
18756 (t (org-drag-element-backward))))
18758 (defun org-metadown (&optional arg)
18759 "Move subtree down or move table row down.
18760 Calls `org-move-subtree-down' or `org-table-move-row' or
18761 `org-move-item-down', depending on context. See the individual
18762 commands for more information."
18765 ((run-hook-with-args-until-success 'org-metadown-hook))
18766 ((org-region-active-p)
18767 (let* ((a (min (region-beginning) (region-end)))
18768 (b (max (region-beginning) (region-end)))
18769 (c (save-excursion (goto-char b)
18770 (move-beginning-of-line 1)))
18771 (d (save-excursion (goto-char b)
18772 (move-end-of-line 1) (1+ (point)))))
18773 (transpose-regions a b c d)
18775 ((org-at-table-p) (call-interactively 'org-table-move-row))
18776 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18777 ((org-at-item-p) (call-interactively 'org-move-item-down))
18778 (t (org-drag-element-forward))))
18780 (defun org-shiftup (&optional arg)
18781 "Increase item in timestamp or increase priority of current headline.
18782 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
18783 depending on context. See the individual commands for more information."
18786 ((run-hook-with-args-until-success 'org-shiftup-hook))
18787 ((and org-support-shift-select (org-region-active-p))
18788 (org-call-for-shift-select 'previous-line))
18789 ((org-at-timestamp-p t)
18790 (call-interactively (if org-edit-timestamp-down-means-later
18791 'org-timestamp-down 'org-timestamp-up)))
18792 ((and (not (eq org-support-shift-select 'always))
18793 org-enable-priority-commands
18794 (org-at-heading-p))
18795 (call-interactively 'org-priority-up))
18796 ((and (not org-support-shift-select) (org-at-item-p))
18797 (call-interactively 'org-previous-item))
18798 ((org-clocktable-try-shift 'up arg))
18799 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
18800 (org-support-shift-select
18801 (org-call-for-shift-select 'previous-line))
18802 (t (org-shiftselect-error))))
18804 (defun org-shiftdown (&optional arg)
18805 "Decrease item in timestamp or decrease priority of current headline.
18806 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
18807 depending on context. See the individual commands for more information."
18810 ((run-hook-with-args-until-success 'org-shiftdown-hook))
18811 ((and org-support-shift-select (org-region-active-p))
18812 (org-call-for-shift-select 'next-line))
18813 ((org-at-timestamp-p t)
18814 (call-interactively (if org-edit-timestamp-down-means-later
18815 'org-timestamp-up 'org-timestamp-down)))
18816 ((and (not (eq org-support-shift-select 'always))
18817 org-enable-priority-commands
18818 (org-at-heading-p))
18819 (call-interactively 'org-priority-down))
18820 ((and (not org-support-shift-select) (org-at-item-p))
18821 (call-interactively 'org-next-item))
18822 ((org-clocktable-try-shift 'down arg))
18823 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
18824 (org-support-shift-select
18825 (org-call-for-shift-select 'next-line))
18826 (t (org-shiftselect-error))))
18828 (defun org-shiftright (&optional arg)
18829 "Cycle the thing at point or in the current line, depending on context.
18830 Depending on context, this does one of the following:
18832 - switch a timestamp at point one day into the future
18833 - on a headline, switch to the next TODO keyword.
18834 - on an item, switch entire list to the next bullet type
18835 - on a property line, switch to the next allowed value
18836 - on a clocktable definition line, move time block into the future"
18839 ((run-hook-with-args-until-success 'org-shiftright-hook))
18840 ((and org-support-shift-select (org-region-active-p))
18841 (org-call-for-shift-select 'forward-char))
18842 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
18843 ((and (not (eq org-support-shift-select 'always))
18844 (org-at-heading-p))
18845 (let ((org-inhibit-logging
18846 (not org-treat-S-cursor-todo-selection-as-state-change))
18847 (org-inhibit-blocking
18848 (not org-treat-S-cursor-todo-selection-as-state-change)))
18849 (org-call-with-arg 'org-todo 'right)))
18850 ((or (and org-support-shift-select
18851 (not (eq org-support-shift-select 'always))
18852 (org-at-item-bullet-p))
18853 (and (not org-support-shift-select) (org-at-item-p)))
18854 (org-call-with-arg 'org-cycle-list-bullet nil))
18855 ((and (not (eq org-support-shift-select 'always))
18856 (org-at-property-p))
18857 (call-interactively 'org-property-next-allowed-value))
18858 ((org-clocktable-try-shift 'right arg))
18859 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
18860 (org-support-shift-select
18861 (org-call-for-shift-select 'forward-char))
18862 (t (org-shiftselect-error))))
18864 (defun org-shiftleft (&optional arg)
18865 "Cycle the thing at point or in the current line, depending on context.
18866 Depending on context, this does one of the following:
18868 - switch a timestamp at point one day into the past
18869 - on a headline, switch to the previous TODO keyword.
18870 - on an item, switch entire list to the previous bullet type
18871 - on a property line, switch to the previous allowed value
18872 - on a clocktable definition line, move time block into the past"
18875 ((run-hook-with-args-until-success 'org-shiftleft-hook))
18876 ((and org-support-shift-select (org-region-active-p))
18877 (org-call-for-shift-select 'backward-char))
18878 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
18879 ((and (not (eq org-support-shift-select 'always))
18880 (org-at-heading-p))
18881 (let ((org-inhibit-logging
18882 (not org-treat-S-cursor-todo-selection-as-state-change))
18883 (org-inhibit-blocking
18884 (not org-treat-S-cursor-todo-selection-as-state-change)))
18885 (org-call-with-arg 'org-todo 'left)))
18886 ((or (and org-support-shift-select
18887 (not (eq org-support-shift-select 'always))
18888 (org-at-item-bullet-p))
18889 (and (not org-support-shift-select) (org-at-item-p)))
18890 (org-call-with-arg 'org-cycle-list-bullet 'previous))
18891 ((and (not (eq org-support-shift-select 'always))
18892 (org-at-property-p))
18893 (call-interactively 'org-property-previous-allowed-value))
18894 ((org-clocktable-try-shift 'left arg))
18895 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
18896 (org-support-shift-select
18897 (org-call-for-shift-select 'backward-char))
18898 (t (org-shiftselect-error))))
18900 (defun org-shiftcontrolright ()
18901 "Switch to next TODO set."
18904 ((and org-support-shift-select (org-region-active-p))
18905 (org-call-for-shift-select 'forward-word))
18906 ((and (not (eq org-support-shift-select 'always))
18907 (org-at-heading-p))
18908 (org-call-with-arg 'org-todo 'nextset))
18909 (org-support-shift-select
18910 (org-call-for-shift-select 'forward-word))
18911 (t (org-shiftselect-error))))
18913 (defun org-shiftcontrolleft ()
18914 "Switch to previous TODO set."
18917 ((and org-support-shift-select (org-region-active-p))
18918 (org-call-for-shift-select 'backward-word))
18919 ((and (not (eq org-support-shift-select 'always))
18920 (org-at-heading-p))
18921 (org-call-with-arg 'org-todo 'previousset))
18922 (org-support-shift-select
18923 (org-call-for-shift-select 'backward-word))
18924 (t (org-shiftselect-error))))
18926 (defun org-shiftcontrolup ()
18927 "Change timestamps synchronously up in CLOCK log lines."
18929 (cond ((and (not org-support-shift-select)
18930 (org-at-clock-log-p)
18931 (org-at-timestamp-p t))
18932 (org-clock-timestamps-up))
18933 (t (org-shiftselect-error))))
18935 (defun org-shiftcontroldown ()
18936 "Change timestamps synchronously down in CLOCK log lines."
18938 (cond ((and (not org-support-shift-select)
18939 (org-at-clock-log-p)
18940 (org-at-timestamp-p t))
18941 (org-clock-timestamps-down))
18942 (t (org-shiftselect-error))))
18944 (defun org-ctrl-c-ret ()
18945 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
18948 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
18949 (t (call-interactively 'org-insert-heading))))
18951 (defun org-find-visible ()
18953 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18954 (get-char-property s 'invisible)))
18956 (defun org-find-invisible ()
18958 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18959 (not (get-char-property s 'invisible))))
18962 (defun org-copy-visible (beg end)
18963 "Copy the visible parts of the region."
18968 (narrow-to-region beg end)
18969 (setq s (goto-char (point-min)))
18970 (while (not (= (point) (point-max)))
18971 (goto-char (org-find-invisible))
18972 (push (buffer-substring s (point)) snippets)
18973 (setq s (goto-char (org-find-visible))))))
18974 (kill-new (apply 'concat (nreverse snippets)))))
18976 (defun org-copy-special ()
18977 "Copy region in table or copy current subtree.
18978 Calls `org-table-copy' or `org-copy-subtree', depending on context.
18979 See the individual commands for more information."
18981 (call-interactively
18982 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
18984 (defun org-cut-special ()
18985 "Cut region in table or cut current subtree.
18986 Calls `org-table-copy' or `org-cut-subtree', depending on context.
18987 See the individual commands for more information."
18989 (call-interactively
18990 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
18992 (defun org-paste-special (arg)
18993 "Paste rectangular region into table, or past subtree relative to level.
18994 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
18995 See the individual commands for more information."
18997 (if (org-at-table-p)
18998 (org-table-paste-rectangle)
18999 (org-paste-subtree arg)))
19001 (defsubst org-in-fixed-width-region-p ()
19002 "Is point in a fixed-width region?"
19004 (eq 'fixed-width (org-element-type (org-element-at-point)))))
19006 (defun org-edit-special (&optional arg)
19007 "Call a special editor for the stuff at point.
19008 When at a table, call the formula editor with `org-table-edit-formulas'.
19009 When in a source code block, call `org-edit-src-code'.
19010 When in a fixed-width region, call `org-edit-fixed-width-region'.
19011 When in an #+include line, visit the included file.
19012 On a link, call `ffap' to visit the link at point.
19013 Otherwise, return a user error."
19015 ;; possibly prep session before editing source
19016 (when (and (org-in-src-block-p) arg)
19017 (let* ((info (org-babel-get-src-block-info))
19018 (lang (nth 0 info))
19019 (params (nth 2 info))
19020 (session (cdr (assoc :session params))))
19021 (when (and info session) ;; we are in a source-code block with a session
19023 (intern (concat "org-babel-prep-session:" lang)) session params))))
19024 (cond ;; proceed with `org-edit-special'
19026 (beginning-of-line 1)
19027 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
19028 (find-file (org-trim (match-string 1))))
19029 ((or (org-at-table-p)
19031 (beginning-of-line 1)
19032 (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
19033 (call-interactively 'org-table-edit-formulas))
19034 ((or (org-in-block-p '("src" "example" "latex" "html"))
19035 (org-at-table.el-p))
19036 (org-edit-src-code))
19037 ((org-in-fixed-width-region-p) (org-edit-fixed-width-region))
19038 ((org-at-regexp-p org-any-link-re) (call-interactively 'ffap))
19039 (t (user-error "No special environment to edit here"))))
19041 (defvar org-table-coordinate-overlays) ; defined in org-table.el
19042 (defun org-ctrl-c-ctrl-c (&optional arg)
19043 "Set tags in headline, or update according to changed information at point.
19045 This command does many different things, depending on context:
19047 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
19048 this is what we do.
19050 - If the cursor is on a statistics cookie, update it.
19052 - If the cursor is in a headline, prompt for tags and insert them
19053 into the current line, aligned to `org-tags-column'. When called
19054 with prefix arg, realign all tags in the current buffer.
19056 - If the cursor is in one of the special #+KEYWORD lines, this
19057 triggers scanning the buffer for these lines and updating the
19060 - If the cursor is inside a table, realign the table. This command
19061 works even if the automatic table editor has been turned off.
19063 - If the cursor is on a #+TBLFM line, re-apply the formulas to
19066 - If the cursor is at a footnote reference or definition, jump to
19067 the corresponding definition or references, respectively.
19069 - If the cursor is a the beginning of a dynamic block, update it.
19071 - If the current buffer is a capture buffer, close note and file it.
19073 - If the cursor is on a <<<target>>>, update radio targets and
19074 corresponding links in this buffer.
19076 - If the cursor is on a numbered item in a plain list, renumber the
19079 - If the cursor is on a checkbox, toggle it.
19081 - If the cursor is on a code block, evaluate it. The variable
19082 `org-confirm-babel-evaluate' can be used to control prompting
19083 before code block evaluation, by default every code block
19084 evaluation requires confirmation. Code block evaluation can be
19085 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
19087 (let ((org-enable-table-editor t))
19089 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
19090 org-occur-highlights
19091 org-latex-fragment-image-overlays)
19092 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
19093 (org-remove-occur-highlights)
19094 (org-remove-latex-fragment-image-overlays)
19095 (message "Temporary highlights/overlays removed from current buffer"))
19096 ((and (local-variable-p 'org-finish-function (current-buffer))
19097 (fboundp org-finish-function))
19098 (funcall org-finish-function))
19099 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
19100 ((org-in-regexp org-ts-regexp-both)
19101 (org-timestamp-change 0 'day))
19102 ((or (looking-at org-property-start-re)
19103 (org-at-property-p))
19104 (call-interactively 'org-property-action))
19105 ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
19106 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
19107 (or (org-at-heading-p) (org-at-item-p)))
19108 (call-interactively 'org-update-statistics-cookies))
19109 ((org-at-heading-p) (call-interactively 'org-set-tags))
19110 ((org-at-table.el-p)
19111 (message "Use C-c ' to edit table.el tables"))
19113 (org-table-maybe-eval-formula)
19115 (call-interactively 'org-table-recalculate)
19116 (org-table-maybe-recalculate-line))
19117 (call-interactively 'org-table-align)
19118 (orgtbl-send-table 'maybe))
19119 ((or (org-footnote-at-reference-p)
19120 (org-footnote-at-definition-p))
19121 (call-interactively 'org-footnote-action))
19122 ((org-at-item-checkbox-p)
19123 ;; Cursor at a checkbox: repair list and update checkboxes. Send
19124 ;; list only if at top item.
19125 (let* ((cbox (match-string 1))
19126 (struct (org-list-struct))
19127 (old-struct (copy-tree struct))
19128 (parents (org-list-parents-alist struct))
19129 (orderedp (org-entry-get nil "ORDERED"))
19130 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19132 ;; Use a light version of `org-toggle-checkbox' to avoid
19133 ;; computing list structure twice.
19134 (let ((new-box (cond
19135 ((equal arg '(16)) "[-]")
19136 ((equal arg '(4)) nil)
19137 ((equal "[X]" cbox) "[ ]")
19139 (if (and firstp arg)
19140 ;; If at first item of sub-list, remove check-box from
19141 ;; every item at the same level.
19143 (lambda (pos) (org-list-set-checkbox pos struct new-box))
19144 (org-list-get-all-items
19145 (point-at-bol) struct (org-list-prevs-alist struct)))
19146 (org-list-set-checkbox (point-at-bol) struct new-box)))
19147 ;; Replicate `org-list-write-struct', while grabbing a return
19148 ;; value from `org-list-struct-fix-box'.
19149 (org-list-struct-fix-ind struct parents 2)
19150 (org-list-struct-fix-item-end struct)
19151 (let ((prevs (org-list-prevs-alist struct)))
19152 (org-list-struct-fix-bul struct prevs)
19153 (org-list-struct-fix-ind struct parents)
19155 (org-list-struct-fix-box struct parents prevs orderedp)))
19156 (if (equal struct old-struct)
19157 (user-error "Cannot toggle this checkbox (unchecked subitems?)")
19158 (org-list-struct-apply-struct struct old-struct)
19159 (org-update-checkbox-count-maybe))
19162 "Checkboxes were removed due to unchecked box at line %d"
19163 (org-current-line block-item)))
19164 (when firstp (org-list-send-list 'maybe))))
19166 ;; Cursor at an item: repair list. Do checkbox related actions
19167 ;; only if function was called with an argument. Send list only
19169 (let* ((struct (org-list-struct))
19170 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19173 (setq old-struct (copy-tree struct))
19175 ;; If at first item of sub-list, add check-box to every
19176 ;; item at the same level.
19179 (unless (org-list-get-checkbox pos struct)
19180 (org-list-set-checkbox pos struct "[ ]")))
19181 (org-list-get-all-items
19182 (point-at-bol) struct (org-list-prevs-alist struct)))
19183 (org-list-set-checkbox (point-at-bol) struct "[ ]")))
19184 (org-list-write-struct
19185 struct (org-list-parents-alist struct) old-struct)
19186 (when arg (org-update-checkbox-count-maybe))
19187 (when firstp (org-list-send-list 'maybe))))
19188 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
19190 (beginning-of-line 1)
19191 (save-excursion (org-update-dblock)))
19193 (let ((case-fold-search t))
19194 (beginning-of-line 1)
19195 (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
19197 ((or (equal (match-string 1) "TBLFM")
19198 (equal (match-string 1) "tblfm"))
19199 ;; Recalculate the table before this line
19201 (beginning-of-line 1)
19202 (skip-chars-backward " \r\n\t")
19203 (if (org-at-table-p)
19204 (org-call-with-arg 'org-table-recalculate (or arg t)))))
19206 (let ((org-inhibit-startup-visibility-stuff t)
19207 (org-startup-align-all-tables nil))
19208 (when (boundp 'org-table-coordinate-overlays)
19209 (mapc 'delete-overlay org-table-coordinate-overlays)
19210 (setq org-table-coordinate-overlays nil))
19211 (org-save-outline-visibility 'use-markers (org-mode-restart)))
19212 (message "Local setup has been refreshed"))))
19213 ((org-clock-update-time-maybe))
19215 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
19216 (error "C-c C-c can do nothing useful at this location"))))))
19218 (defun org-mode-restart ()
19219 "Restart Org-mode, to scan again for special lines.
19220 Also updates the keyword regular expressions."
19223 (message "Org-mode restarted"))
19225 (defun org-kill-note-or-show-branches ()
19226 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
19228 (if (not org-finish-function)
19231 (call-interactively 'show-branches))
19232 (let ((org-note-abort t))
19233 (funcall org-finish-function))))
19235 (defun org-return (&optional indent)
19236 "Goto next table row or insert a newline.
19237 Calls `org-table-next-row' or `newline', depending on context.
19238 See the individual commands for more information."
19242 ((or (bobp) (org-in-src-block-p))
19243 (if indent (newline-and-indent) (newline)))
19245 (org-table-justify-field-maybe)
19246 (call-interactively 'org-table-next-row))
19247 ;; when `newline-and-indent' is called within a list, make sure
19248 ;; text moved stays inside the item.
19249 ((and (org-in-item-p) indent)
19250 (if (and (org-at-item-p) (>= (point) (match-end 0)))
19252 (save-match-data (newline))
19253 (org-indent-line-to (length (match-string 0))))
19254 (let ((ind (org-get-indentation)))
19256 (if (org-looking-back org-list-end-re)
19258 (org-indent-line-to ind)))))
19259 ((and org-return-follows-link
19260 (org-at-timestamp-p t)
19261 (not (eq org-ts-what 'after)))
19262 (org-follow-timestamp-link))
19263 ((and org-return-follows-link
19264 (let ((tprop (get-text-property (point) 'face)))
19265 (or (eq tprop 'org-link)
19266 (and (listp tprop) (memq 'org-link tprop)))))
19267 (call-interactively 'org-open-at-point))
19268 ((and (org-at-heading-p)
19270 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
19274 (t (if indent (newline-and-indent) (newline))))))
19276 (defun org-return-indent ()
19277 "Goto next table row or insert a newline and indent.
19278 Calls `org-table-next-row' or `newline-and-indent', depending on
19279 context. See the individual commands for more information."
19283 (defun org-ctrl-c-star ()
19284 "Compute table, or change heading status of lines.
19285 Calls `org-table-recalculate' or `org-toggle-heading',
19286 depending on context."
19290 (call-interactively 'org-table-recalculate))
19292 ;; Convert all lines in region to list items
19293 (call-interactively 'org-toggle-heading))))
19295 (defun org-ctrl-c-minus ()
19296 "Insert separator line in table or modify bullet status of line.
19297 Also turns a plain line or a region of lines into list items.
19298 Calls `org-table-insert-hline', `org-toggle-item', or
19299 `org-cycle-list-bullet', depending on context."
19303 (call-interactively 'org-table-insert-hline))
19304 ((org-region-active-p)
19305 (call-interactively 'org-toggle-item))
19307 (call-interactively 'org-cycle-list-bullet))
19309 (call-interactively 'org-toggle-item))))
19311 (defun org-toggle-item (arg)
19312 "Convert headings or normal lines to items, items to normal lines.
19313 If there is no active region, only the current line is considered.
19315 If the first non blank line in the region is an headline, convert
19316 all headlines to items, shifting text accordingly.
19318 If it is an item, convert all items to normal lines.
19320 If it is normal text, change region into an item. With a prefix
19321 argument ARG, change each line in region into an item."
19325 ;; Shift text in current section to IND, from point to END.
19326 ;; The function leaves point to END line.
19328 (let ((min-i 1000) (end (copy-marker end)))
19329 ;; First determine the minimum indentation (MIN-I) of
19333 (while (< (point) end)
19334 (let ((i (org-get-indentation)))
19336 ;; Skip blank lines and inline tasks.
19337 ((looking-at "^[ \t]*$"))
19338 ((looking-at org-outline-regexp-bol))
19339 ;; We can't find less than 0 indentation.
19340 ((zerop i) (throw 'exit (setq min-i 0)))
19341 ((< i min-i) (setq min-i i))))
19343 ;; Then indent each line so that a line indented to
19344 ;; MIN-I becomes indented to IND. Ignore blank lines
19345 ;; and inline tasks in the process.
19346 (let ((delta (- ind min-i)))
19347 (while (< (point) end)
19348 (unless (or (looking-at "^[ \t]*$")
19349 (looking-at org-outline-regexp-bol))
19350 (org-indent-line-to (+ (org-get-indentation) delta)))
19351 (forward-line)))))))
19354 ;; Return beginning of first non-blank line, starting from
19359 (skip-chars-forward " \r\t\n")
19362 ;; Determine boundaries of changes.
19363 (if (org-region-active-p)
19364 (setq beg (funcall skip-blanks (region-beginning))
19365 end (copy-marker (region-end)))
19366 (setq beg (funcall skip-blanks (point-at-bol))
19367 end (copy-marker (point-at-eol))))
19368 ;; Depending on the starting line, choose an action on the text
19369 ;; between BEG and END.
19370 (org-with-limited-levels
19374 ;; Case 1. Start at an item: de-itemize. Note that it only
19375 ;; happens when a region is active: `org-ctrl-c-minus'
19376 ;; would call `org-cycle-list-bullet' otherwise.
19378 (while (< (point) end)
19379 (when (org-at-item-p)
19380 (skip-chars-forward " \t")
19381 (delete-region (point) (match-end 0)))
19383 ;; Case 2. Start at an heading: convert to items.
19384 ((org-at-heading-p)
19385 (let* ((bul (org-list-bullet-string "-"))
19386 (bul-len (length bul))
19387 ;; Indentation of the first heading. It should be
19388 ;; relative to the indentation of its parent, if any.
19389 (start-ind (save-excursion
19391 ((not org-adapt-indentation) 0)
19392 ((not (outline-previous-heading)) 0)
19393 (t (length (match-string 0))))))
19394 ;; Level of first heading. Further headings will be
19395 ;; compared to it to determine hierarchy in the list.
19396 (ref-level (org-reduced-level (org-outline-level))))
19397 (while (< (point) end)
19398 (let* ((level (org-reduced-level (org-outline-level)))
19399 (delta (max 0 (- level ref-level))))
19400 ;; If current headline is less indented than the first
19401 ;; one, set it as reference, in order to preserve
19403 (when (< level ref-level) (setq ref-level level))
19404 (replace-match bul t t)
19405 (org-indent-line-to (+ start-ind (* delta bul-len)))
19406 ;; Ensure all text down to END (or SECTION-END) belongs
19407 ;; to the newly created item.
19408 (let ((section-end (save-excursion
19409 (or (outline-next-heading) (point)))))
19411 (funcall shift-text
19412 (+ start-ind (* (1+ delta) bul-len))
19413 (min end section-end)))))))
19414 ;; Case 3. Normal line with ARG: turn each non-item line into
19417 (while (< (point) end)
19418 (unless (or (org-at-heading-p) (org-at-item-p))
19419 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
19421 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
19423 ;; Case 4. Normal line without ARG: make the first line of
19424 ;; region an item, and shift indentation of others
19425 ;; lines to set them as item's body.
19426 (t (let* ((bul (org-list-bullet-string "-"))
19427 (bul-len (length bul))
19428 (ref-ind (org-get-indentation)))
19429 (skip-chars-forward " \t")
19432 (while (< (point) end)
19433 ;; Ensure that lines less indented than first one
19434 ;; still get included in item body.
19435 (funcall shift-text
19436 (+ ref-ind bul-len)
19437 (min end (save-excursion (or (outline-next-heading)
19439 (forward-line)))))))))
19441 (defun org-toggle-heading (&optional nstars)
19442 "Convert headings to normal text, or items or text to headings.
19443 If there is no active region, only the current line is considered.
19445 With a \\[universal-argument] prefix, convert the whole list at
19446 point into heading.
19450 - If the first non blank line is an headline, remove the stars
19451 from all headlines in the region.
19453 - If it is a normal line turn each and every normal line (i.e. not an
19454 heading or an item) in the region into a heading.
19456 - If it is a plain list item, turn all plain list items into headings.
19458 When converting a line into a heading, the number of stars is chosen
19459 such that the lines become children of the current entry. However,
19460 when a prefix argument is given, its value determines the number of
19465 ;; Return beginning of first non-blank line, starting from
19470 (while (org-at-comment-p) (forward-line))
19471 (skip-chars-forward " \r\t\n")
19474 ;; Determine boundaries of changes. If a universal prefix has
19475 ;; been given, put the list in a region. If region ends at a bol,
19476 ;; do not consider the last line to be in the region.
19478 (when (and current-prefix-arg (org-at-item-p))
19479 (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
19480 (org-mark-element))
19482 (if (org-region-active-p)
19483 (setq beg (funcall skip-blanks (region-beginning))
19484 end (copy-marker (save-excursion
19485 (goto-char (region-end))
19486 (if (bolp) (point) (point-at-eol)))))
19487 (setq beg (funcall skip-blanks (point-at-bol))
19488 end (copy-marker (point-at-eol))))
19489 ;; Ensure inline tasks don't count as headings.
19490 (org-with-limited-levels
19494 ;; Case 1. Started at an heading: de-star headings.
19495 ((org-at-heading-p)
19496 (while (< (point) end)
19497 (when (org-at-heading-p t)
19498 (looking-at org-outline-regexp) (replace-match "")
19501 ;; Case 2. Started at an item: change items into headlines.
19502 ;; One star will be added by `org-list-to-subtree'.
19504 (let* ((stars (make-string
19506 ;; subtract the star that will be added again by
19507 ;; `org-list-to-subtree'
19508 (1- (prefix-numeric-value current-prefix-arg))
19509 (or (org-current-level) 0))
19512 (cond (nstars "") ; stars from prefix only
19513 ((equal stars "") "") ; before first heading
19514 (org-odd-levels-only "*") ; inside heading, odd
19515 (t "")))) ; inside heading, oddeven
19516 (while (< (point) end)
19517 (when (org-at-item-p)
19518 ;; Pay attention to cases when region ends before list.
19519 (let* ((struct (org-list-struct))
19520 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
19522 (narrow-to-region (point) list-end)
19524 (org-list-to-subtree
19525 (org-list-parse-list t)
19526 '(:istart (concat stars add-stars (funcall get-stars depth))
19527 :icount (concat stars add-stars (funcall get-stars depth)))))))
19530 ;; Case 3. Started at normal text: make every line an heading,
19531 ;; skipping headlines and items.
19532 (t (let* ((stars (make-string
19534 (prefix-numeric-value current-prefix-arg)
19535 (or (org-current-level) 0))
19538 (cond (nstars "") ; stars from prefix only
19539 ((equal stars "") "*") ; before first heading
19540 (org-odd-levels-only "**") ; inside heading, odd
19541 (t "*"))) ; inside heading, oddeven
19542 (rpl (concat stars add-stars " ")))
19543 (while (< (point) end)
19544 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
19545 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
19546 (replace-match (concat rpl (match-string 2))) (setq toggled t))
19547 (forward-line)))))))
19548 (unless toggled (message "Cannot toggle heading from here"))))
19550 (defun org-meta-return (&optional arg)
19551 "Insert a new heading or wrap a region in a table.
19552 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
19553 See the individual commands for more information."
19556 ((run-hook-with-args-until-success 'org-metareturn-hook))
19557 ((or (org-at-drawer-p) (org-at-property-p))
19558 (newline-and-indent))
19560 (call-interactively 'org-table-wrap-region))
19561 (t (call-interactively 'org-insert-heading))))
19565 (defsubst org-in-subtree-not-table-p ()
19566 "Are we in a subtree and not in a table?"
19567 (and (not (org-before-first-heading-p))
19568 (not (org-at-table-p))))
19570 ;; Define the Org-mode menus
19571 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
19573 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
19574 ["Next Field" org-cycle (org-at-table-p)]
19575 ["Previous Field" org-shifttab (org-at-table-p)]
19576 ["Next Row" org-return (org-at-table-p)]
19578 ["Blank Field" org-table-blank-field (org-at-table-p)]
19579 ["Edit Field" org-table-edit-field (org-at-table-p)]
19580 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
19583 ["Move Column Left" org-metaleft (org-at-table-p)]
19584 ["Move Column Right" org-metaright (org-at-table-p)]
19585 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
19586 ["Insert Column" org-shiftmetaright (org-at-table-p)])
19588 ["Move Row Up" org-metaup (org-at-table-p)]
19589 ["Move Row Down" org-metadown (org-at-table-p)]
19590 ["Delete Row" org-shiftmetaup (org-at-table-p)]
19591 ["Insert Row" org-shiftmetadown (org-at-table-p)]
19592 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
19594 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
19596 ["Copy Rectangle" org-copy-special (org-at-table-p)]
19597 ["Cut Rectangle" org-cut-special (org-at-table-p)]
19598 ["Paste Rectangle" org-paste-special (org-at-table-p)]
19599 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
19602 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
19603 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
19604 ["Edit Formulas" org-edit-special (org-at-table-p)]
19606 ["Recalculate line" org-table-recalculate (org-at-table-p)]
19607 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
19608 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
19610 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
19612 ["Sum Column/Rectangle" org-table-sum
19613 (or (org-at-table-p) (org-region-active-p))]
19614 ["Which Column?" org-table-current-column (org-at-table-p)])
19616 org-table-toggle-formula-debugger
19617 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
19618 ["Show Col/Row Numbers"
19619 org-table-toggle-coordinate-overlays
19621 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
19623 ["Create" org-table-create (and (not (org-at-table-p))
19624 org-enable-table-editor)]
19625 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
19626 ["Import from File" org-table-import (not (org-at-table-p))]
19627 ["Export to File" org-table-export (org-at-table-p)]
19629 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
19631 (easy-menu-define org-org-menu org-mode-map "Org menu"
19634 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
19635 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
19636 ["Sparse Tree..." org-sparse-tree t]
19637 ["Reveal Context" org-reveal t]
19638 ["Show All" show-all t]
19640 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
19642 ["New Heading" org-insert-heading t]
19643 ("Navigate Headings"
19644 ["Up" outline-up-heading t]
19645 ["Next" outline-next-visible-heading t]
19646 ["Previous" outline-previous-visible-heading t]
19647 ["Next Same Level" outline-forward-same-level t]
19648 ["Previous Same Level" outline-backward-same-level t]
19650 ["Jump" org-goto t])
19652 ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
19654 ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
19655 ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
19657 ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
19658 ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
19659 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
19661 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
19663 ["Copy visible text" org-copy-visible t]
19665 ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
19666 ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
19667 ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
19668 ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
19670 ["Sort Region/Children" org-sort t]
19672 ["Convert to odd levels" org-convert-to-odd-levels t]
19673 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
19675 ["Emphasis..." org-emphasize t]
19676 ["Edit Source Example" org-edit-special t]
19678 ["Footnote new/jump" org-footnote-action t]
19679 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
19681 ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
19683 ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
19684 ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
19685 ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
19689 ["Store Link (Global)" org-store-link t]
19690 ["Find existing link to here" org-occur-link-in-agenda-files t]
19691 ["Insert Link" org-insert-link t]
19692 ["Follow Link" org-open-at-point t]
19694 ["Next link" org-next-link t]
19695 ["Previous link" org-previous-link t]
19697 ["Descriptive Links"
19698 org-toggle-link-display
19700 :selected org-descriptive-links
19703 org-toggle-link-display
19705 :selected (not org-descriptive-links)])
19708 ["TODO/DONE/-" org-todo t]
19710 ["Next keyword" org-shiftright (org-at-heading-p)]
19711 ["Previous keyword" org-shiftleft (org-at-heading-p)]
19712 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
19713 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
19714 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
19715 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
19716 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
19718 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
19719 :selected org-enforce-todo-dependencies :style toggle :active t]
19720 "Settings for tree at point"
19721 ["Do Children sequentially" org-toggle-ordered-property :style radio
19722 :selected (org-entry-get nil "ORDERED")
19723 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19724 ["Do Children parallel" org-toggle-ordered-property :style radio
19725 :selected (not (org-entry-get nil "ORDERED"))
19726 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19728 ["Set Priority" org-priority t]
19729 ["Priority Up" org-shiftup t]
19730 ["Priority Down" org-shiftdown t]
19732 ["Get news from all feeds" org-feed-update-all t]
19733 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
19734 ["Customize feeds" (customize-variable 'org-feed-alist) t])
19735 ("TAGS and Properties"
19736 ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
19737 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
19739 ["Set property" org-set-property (not (org-before-first-heading-p))]
19740 ["Column view of properties" org-columns t]
19741 ["Insert Column View DBlock" org-insert-columns-dblock t])
19742 ("Dates and Scheduling"
19743 ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
19744 ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
19746 ["1 Day Later" org-shiftright (org-at-timestamp-p)]
19747 ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
19748 ["1 ... Later" org-shiftup (org-at-timestamp-p)]
19749 ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
19750 ["Compute Time Range" org-evaluate-time-range t]
19751 ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
19752 ["Deadline" org-deadline (not (org-before-first-heading-p))]
19754 ["Custom time format" org-toggle-time-stamp-overlays
19755 :style radio :selected org-display-custom-times]
19757 ["Goto Calendar" org-goto-calendar t]
19758 ["Date from Calendar" org-date-from-calendar t]
19760 ["Start/Restart Timer" org-timer-start t]
19761 ["Pause/Continue Timer" org-timer-pause-or-continue t]
19762 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
19763 ["Insert Timer String" org-timer t]
19764 ["Insert Timer Item" org-timer-item t])
19766 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
19767 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
19768 ["Clock out" org-clock-out t]
19769 ["Clock cancel" org-clock-cancel t]
19771 ["Mark as default task" org-clock-mark-default-task t]
19772 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
19773 ["Goto running clock" org-clock-goto t]
19775 ["Display times" org-clock-display t]
19776 ["Create clock table" org-clock-report t]
19778 ["Record DONE time"
19779 (progn (setq org-log-done (not org-log-done))
19780 (message "Switching to %s will %s record a timestamp"
19781 (car org-done-keywords)
19782 (if org-log-done "automatically" "not")))
19783 :style toggle :selected org-log-done])
19785 ["Agenda Command..." org-agenda t]
19786 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
19787 ("File List for Agenda")
19788 ("Special views current file"
19789 ["TODO Tree" org-show-todo-tree t]
19790 ["Check Deadlines" org-check-deadlines t]
19791 ["Timeline" org-timeline t]
19792 ["Tags/Property tree" org-match-sparse-tree t])
19794 ["Export/Publish..." org-export t]
19796 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
19797 :selected org-cdlatex-mode]
19798 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
19799 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
19800 ["Modify math symbol" org-cdlatex-math-modify
19801 (org-inside-LaTeX-fragment-p)]
19802 ["Insert citation" org-reftex-citation t]
19804 ["Template for BEAMER" (progn (require 'org-beamer)
19805 (org-insert-beamer-options-template)) t])
19808 ["Push Files and Views" org-mobile-push t]
19809 ["Get Captured and Flagged" org-mobile-pull t]
19810 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
19812 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
19815 ["Show Version" org-version t]
19816 ["Info Documentation" org-info t])
19818 ["Browse Org Group" org-customize t]
19820 ["Expand This Menu" org-create-customize-menu
19821 (fboundp 'customize-menu-create)])
19822 ["Send bug report" org-submit-bug-report t]
19825 ["Refresh setup current buffer" org-mode-restart t]
19826 ["Reload Org (after update)" org-reload t]
19827 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
19830 (defun org-info (&optional node)
19831 "Read documentation for Org-mode in the info system.
19832 With optional NODE, go directly to that node."
19834 (info (format "(org)%s" (or node ""))))
19837 (defun org-submit-bug-report ()
19838 "Submit a bug report on Org-mode via mail.
19840 Don't hesitate to report any problems or inaccurate documentation.
19842 If you don't have setup sending mail from (X)Emacs, please copy the
19843 output buffer into your mail program, as it gives us important
19844 information about your Org-mode version and configuration."
19846 (require 'reporter)
19847 (org-load-modules-maybe)
19848 (org-require-autoloaded-modules)
19849 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
19850 (reporter-submit-bug-report
19851 "emacs-orgmode@gnu.org"
19852 (org-version nil 'full)
19854 (save-window-excursion
19855 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
19856 (delete-other-windows)
19858 (insert "You are about to submit a bug report to the Org-mode mailing list.
19860 We would like to add your full Org-mode and Outline configuration to the
19861 bug report. This greatly simplifies the work of the maintainer and
19862 other experts on the mailing list.
19864 HOWEVER, some variables you have customized may contain private
19865 information. The names of customers, colleagues, or friends, might
19866 appear in the form of file names, tags, todo states, or search strings.
19867 If you answer yes to the prompt, you might want to check and remove
19868 such private information before sending the email.")
19869 (add-text-properties (point-min) (point-max) '(face org-warning))
19870 (when (yes-or-no-p "Include your Org-mode configuration ")
19874 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
19875 (or (and (symbol-value v)
19876 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
19878 (get v 'custom-type) (get v 'standard-value)
19879 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
19881 (kill-buffer (get-buffer "*Warn about privacy*"))
19884 "Remember to cover the basics, that is, what you expected to happen and
19885 what in fact did happen. You don't know how to make a good report? See
19887 http://orgmode.org/manual/Feedback.html#Feedback
19889 Your bug report will be posted to the Org-mode mailing list.
19890 ------------------------------------------------------------------------")
19892 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
19893 (replace-match "\\1Bug: \\3 [\\2]")))))
19896 (defun org-install-agenda-files-menu ()
19897 (let ((bl (buffer-list)))
19900 (set-buffer (pop bl))
19901 (if (derived-mode-p 'org-mode) (setq bl nil)))
19902 (when (derived-mode-p 'org-mode)
19904 '("Org") "File List for Agenda"
19907 ["Edit File List" (org-edit-agenda-file-list) t]
19908 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
19909 ["Remove Current File from List" org-remove-file t]
19910 ["Cycle through agenda files" org-cycle-agenda-files t]
19911 ["Occur in all agenda files" org-occur-in-agenda-files t]
19913 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
19917 (defun org-require-autoloaded-modules ()
19920 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
19921 org-docbook org-exp org-html org-icalendar
19923 org-publish org-remember org-table
19924 org-timer org-xoxo)))
19927 (defun org-reload (&optional uncompiled)
19928 "Reload all org lisp files.
19929 With prefix arg UNCOMPILED, load the uncompiled versions."
19931 (require 'find-func)
19932 (let* ((file-re "^org\\(-.*\\)?\\.el")
19933 (dir-org (file-name-directory (org-find-library-dir "org")))
19934 (dir-org-contrib (ignore-errors
19935 (file-name-directory
19936 (org-find-library-dir "org-contribdir"))))
19938 (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
19939 (append (list nil "comint" "eval" "exp" "keys"
19940 "lob" "ref" "table" "tangle")
19944 (when (cdr lang) (symbol-name (car lang))))
19945 org-babel-load-languages)))))
19947 (append babel-files
19948 (and dir-org-contrib
19949 (directory-files dir-org-contrib t file-re))
19950 (directory-files dir-org t file-re)))
19951 (remove-re (concat (if (featurep 'xemacs)
19952 "org-colview" "org-colview-xemacs")
19954 (setq files (mapcar 'file-name-sans-extension files))
19955 (setq files (mapcar
19956 (lambda (x) (if (string-match remove-re x) nil x))
19958 (setq files (delq nil files))
19961 (when (featurep (intern (file-name-nondirectory f)))
19962 (if (and (not uncompiled)
19963 (file-exists-p (concat f ".elc")))
19964 (load (concat f ".elc") nil nil 'nosuffix)
19965 (load (concat f ".el") nil nil 'nosuffix))))
19967 (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
19968 (org-version nil 'full 'message))
19971 (defun org-customize ()
19972 "Call the customize function with org as argument."
19974 (org-load-modules-maybe)
19975 (org-require-autoloaded-modules)
19976 (customize-browse 'org))
19978 (defun org-create-customize-menu ()
19979 "Create a full customization menu for Org-mode, insert it into the menu."
19981 (org-load-modules-maybe)
19982 (org-require-autoloaded-modules)
19983 (if (fboundp 'customize-menu-create)
19986 '("Org") "Customize"
19987 `(["Browse Org group" org-customize t]
19989 ,(customize-menu-create 'org)
19990 ["Set" Custom-set t]
19991 ["Save" Custom-save t]
19992 ["Reset to Current" Custom-reset-current t]
19993 ["Reset to Saved" Custom-reset-saved t]
19994 ["Reset to Standard Settings" Custom-reset-standard t]))
19995 (message "\"Org\"-menu now contains full customization menu"))
19996 (error "Cannot expand menu (outdated version of cus-edit.el)")))
19998 ;;;; Miscellaneous stuff
20000 ;;; Generally useful functions
20002 (defun org-get-at-bol (property)
20003 "Get text property PROPERTY at beginning of line."
20004 (get-text-property (point-at-bol) property))
20006 (defun org-find-text-property-in-string (prop s)
20007 "Return the first non-nil value of property PROP in string S."
20008 (or (get-text-property 0 prop s)
20009 (get-text-property (or (next-single-property-change 0 prop s) 0)
20012 (defun org-display-warning (message) ;; Copied from Emacs-Muse
20013 "Display the given MESSAGE as a warning."
20014 (if (fboundp 'display-warning)
20015 (display-warning 'org message
20016 (if (featurep 'xemacs) 'warning :warning))
20017 (let ((buf (get-buffer-create "*Org warnings*")))
20018 (with-current-buffer buf
20019 (goto-char (point-max))
20020 (insert "Warning (Org): " message)
20023 (display-buffer buf)
20026 (defun org-eval (form)
20027 "Eval FORM and return result."
20028 (condition-case error
20030 (error (format "%%![Error: %s]" error))))
20032 (defun org-in-clocktable-p ()
20033 "Check if the cursor is in a clocktable."
20034 (let ((pos (point)) start)
20037 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
20038 (setq start (match-beginning 0))
20039 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
20040 (>= (match-end 0) pos)
20043 (defun org-in-commented-line ()
20044 "Is point in a line starting with `#'?"
20045 (equal (char-after (point-at-bol)) ?#))
20047 (defun org-in-indented-comment-line ()
20048 "Is point in a line starting with `#' after some white space?"
20051 (goto-char (point-at-bol))
20052 (looking-at "[ \t]*#"))))
20054 (defun org-in-verbatim-emphasis ()
20056 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
20058 (defun org-goto-marker-or-bmk (marker &optional bookmark)
20059 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
20060 (if (and marker (marker-buffer marker)
20061 (buffer-live-p (marker-buffer marker)))
20063 (org-pop-to-buffer-same-window (marker-buffer marker))
20064 (if (or (> marker (point-max)) (< marker (point-min)))
20067 (org-show-context 'org-goto))
20069 (bookmark-jump bookmark)
20070 (error "Cannot find location"))))
20072 (defun org-quote-csv-field (s)
20073 "Quote field for inclusion in CSV material."
20074 (if (string-match "[\",]" s)
20075 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
20078 (defun org-force-self-insert (N)
20079 "Needed to enforce self-insert under remapping."
20081 (self-insert-command N))
20083 (defun org-string-width (s)
20084 "Compute width of string, ignoring invisible characters.
20085 This ignores character with invisibility property `org-link', and also
20086 characters with property `org-cwidth', because these will become invisible
20087 upon the next fontification round."
20089 (when (or (eq t buffer-invisibility-spec)
20090 (assq 'org-link buffer-invisibility-spec))
20091 (while (setq b (text-property-any 0 (length s)
20092 'invisible 'org-link s))
20093 (setq s (concat (substring s 0 b)
20094 (substring s (or (next-single-property-change
20095 b 'invisible s) (length s)))))))
20096 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
20097 (setq s (concat (substring s 0 b)
20098 (substring s (or (next-single-property-change
20099 b 'org-cwidth s) (length s))))))
20100 (setq l (string-width s) b -1)
20101 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
20102 (setq l (- l (get-text-property b 'org-dwidth-n s))))
20105 (defun org-shorten-string (s maxlength)
20106 "Shorten string S so tht it is no longer than MAXLENGTH characters.
20107 If the string is shorter or has length MAXLENGTH, just return the
20108 original string. If it is longer, the functions finds a space in the
20109 string, breaks this string off at that locations and adds three dots
20110 as ellipsis. Including the ellipsis, the string will not be longer
20111 than MAXLENGTH. If finding a good breaking point in the string does
20112 not work, the string is just chopped off in the middle of a word
20114 (if (<= (length s) maxlength)
20116 (let* ((n (max (- maxlength 4) 1))
20117 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
20118 (if (string-match re s)
20119 (concat (match-string 1 s) "...")
20120 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
20122 (defun org-get-indentation (&optional line)
20123 "Get the indentation of the current line, interpreting tabs.
20124 When LINE is given, assume it represents a line and compute its indentation."
20126 (if (string-match "^ *" (org-remove-tabs line))
20129 (beginning-of-line 1)
20130 (skip-chars-forward " \t")
20131 (current-column))))
20133 (defun org-get-string-indentation (s)
20134 "What indentation has S due to SPACE and TAB at the beginning of the string?"
20135 (let ((n -1) (i 0) (w tab-width) c)
20137 (while (< (setq n (1+ n)) (length s))
20138 (setq c (aref s n))
20139 (cond ((= c ?\ ) (setq i (1+ i)))
20140 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
20141 (t (throw 'exit t)))))
20144 (defun org-remove-tabs (s &optional width)
20145 "Replace tabulators in S with spaces.
20146 Assumes that s is a single line, starting in column 0."
20147 (setq width (or width tab-width))
20148 (while (string-match "\t" s)
20149 (setq s (replace-match
20151 (- (* width (/ (+ (match-beginning 0) width) width))
20152 (match-beginning 0)) ?\ )
20156 (defun org-fix-indentation (line ind)
20157 "Fix indentation in LINE.
20158 IND is a cons cell with target and minimum indentation.
20159 If the current indentation in LINE is smaller than the minimum,
20160 leave it alone. If it is larger than ind, set it to the target."
20161 (let* ((l (org-remove-tabs line))
20162 (i (org-get-indentation l))
20163 (i1 (car ind)) (i2 (cdr ind)))
20164 (if (>= i i2) (setq l (substring line i2)))
20166 (concat (make-string i1 ?\ ) l)
20169 (defun org-remove-indentation (code &optional n)
20170 "Remove the maximum common indentation from the lines in CODE.
20171 N may optionally be the number of spaces to remove."
20174 (org-do-remove-indentation n)
20177 (defun org-do-remove-indentation (&optional n)
20178 "Remove the maximum common indentation from the buffer."
20179 (untabify (point-min) (point-max))
20180 (let ((min 10000) re)
20183 (goto-char (point-min))
20184 (while (re-search-forward "^ *[^ \n]" nil t)
20185 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
20186 (unless (or (= min 0) (= min 10000))
20187 (setq re (format "^ \\{%d\\}" min))
20188 (goto-char (point-min))
20189 (while (re-search-forward re nil t)
20194 (defun org-fill-template (template alist)
20195 "Find each %key of ALIST in TEMPLATE and replace it."
20196 (let ((case-fold-search nil)
20198 (setq alist (sort (copy-sequence alist)
20199 (lambda (a b) (< (length (car a)) (length (car b))))))
20200 (while (setq entry (pop alist))
20202 (replace-regexp-in-string
20203 (concat "%" (regexp-quote (car entry)))
20204 (or (cdr entry) "") template t t)))
20207 (defun org-base-buffer (buffer)
20208 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
20211 (or (buffer-base-buffer buffer)
20214 (defun org-trim (s)
20215 "Remove whitespace at beginning and end of string."
20216 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
20217 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
20220 (defun org-wrap (string &optional width lines)
20221 "Wrap string to either a number of lines, or a width in characters.
20222 If WIDTH is non-nil, the string is wrapped to that width, however many lines
20223 that costs. If there is a word longer than WIDTH, the text is actually
20224 wrapped to the length of that word.
20225 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
20226 many lines, whatever width that takes.
20227 The return value is a list of lines, without newlines at the end."
20228 (let* ((words (org-split-string string "[ \t\n]+"))
20229 (maxword (apply 'max (mapcar 'org-string-width words)))
20232 (org-do-wrap words (max maxword width)))
20235 (setq ll (org-do-wrap words maxword))
20236 (if (<= (length ll) lines)
20239 (while (> (length ll) lines)
20241 (setq ll (org-do-wrap words w)))
20243 (t (error "Cannot wrap this")))))
20245 (defun org-do-wrap (words width)
20246 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
20249 (setq line (pop words))
20250 (while (and words (< (+ (length line) (length (car words))) width))
20251 (setq line (concat line " " (pop words))))
20252 (setq lines (push line lines)))
20255 (defun org-split-string (string &optional separators)
20256 "Splits STRING into substrings at SEPARATORS.
20257 No empty strings are returned if there are matches at the beginning
20258 and end of string."
20259 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
20263 (while (and (string-match rexp string
20265 (= start (match-beginning 0))
20266 (< start (length string)))
20268 (< (match-beginning 0) (length string)))
20270 (or (eq (match-beginning 0) 0)
20271 (and (eq (match-beginning 0) (match-end 0))
20272 (eq (match-beginning 0) start))
20274 (cons (substring string start (match-beginning 0))
20276 (setq start (match-end 0)))
20277 (or (eq start (length string))
20279 (cons (substring string start)
20283 (defun org-quote-vert (s)
20284 "Replace \"|\" with \"\\vert\"."
20285 (while (string-match "|" s)
20286 (setq s (replace-match "\\vert" t t s)))
20289 (defun org-uuidgen-p (s)
20290 "Is S an ID created by UUIDGEN?"
20291 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
20293 (defun org-in-src-block-p (&optional inside)
20294 "Whether point is in a code source block.
20295 When INSIDE is non-nil, don't consider we are within a src block
20296 when point is at #+BEGIN_SRC or #+END_SRC."
20297 (let ((case-fold-search t) ov)
20298 (or (and (setq ov (overlays-at (point)))
20299 (memq 'org-block-background
20300 (overlay-properties (car ov))))
20304 (beginning-of-line)
20305 (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
20307 (defun org-context ()
20308 "Return a list of contexts of the current cursor position.
20309 If several contexts apply, all are returned.
20310 Each context entry is a list with a symbol naming the context, and
20311 two positions indicating start and end of the context. Possible
20314 :headline anywhere in a headline
20315 :headline-stars on the leading stars in a headline
20316 :todo-keyword on a TODO keyword (including DONE) in a headline
20317 :tags on the TAGS in a headline
20318 :priority on the priority cookie in a headline
20319 :item on the first line of a plain list item
20320 :item-bullet on the bullet/number of a plain list item
20321 :checkbox on the checkbox in a plain list item
20322 :table in an org-mode table
20323 :table-special on a special filed in a table
20324 :table-table in a table.el table
20325 :clocktable in a clocktable
20326 :src-block in a source block
20327 :link on a hyperlink
20328 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
20329 :target on a <<target>>
20330 :radio-target on a <<<radio-target>>>
20331 :latex-fragment on a LaTeX fragment
20332 :latex-preview on a LaTeX fragment with overlaid preview image
20334 This function expects the position to be visible because it uses font-lock
20335 faces as a help to recognize the following contexts: :table-special, :link,
20337 (let* ((f (get-text-property (point) 'face))
20338 (faces (if (listp f) f (list f)))
20339 (case-fold-search t)
20340 (p (point)) clist o)
20341 ;; First the large context
20343 ((org-at-heading-p t)
20344 (push (list :headline (point-at-bol) (point-at-eol)) clist)
20346 (beginning-of-line 1)
20347 (looking-at org-todo-line-tags-regexp))
20348 (push (org-point-in-group p 1 :headline-stars) clist)
20349 (push (org-point-in-group p 2 :todo-keyword) clist)
20350 (push (org-point-in-group p 4 :tags) clist))
20352 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
20353 (if (looking-at "\\[#[A-Z0-9]\\]")
20354 (push (org-point-in-group p 0 :priority) clist)))
20357 (push (org-point-in-group p 2 :item-bullet) clist)
20358 (push (list :item (point-at-bol)
20359 (save-excursion (org-end-of-item) (point)))
20361 (and (org-at-item-checkbox-p)
20362 (push (org-point-in-group p 0 :checkbox) clist)))
20365 (push (list :table (org-table-begin) (org-table-end)) clist)
20366 (if (memq 'org-formula faces)
20367 (push (list :table-special
20368 (previous-single-property-change p 'face)
20369 (next-single-property-change p 'face)) clist)))
20370 ((org-at-table-p 'any)
20371 (push (list :table-table) clist)))
20374 (let ((case-fold-search t))
20375 ;; New the "medium" contexts: clocktables, source blocks
20376 (cond ((org-in-clocktable-p)
20377 (push (list :clocktable
20378 (and (or (looking-at "#\\+BEGIN: clocktable")
20379 (search-backward "#+BEGIN: clocktable" nil t))
20380 (match-beginning 0))
20381 (and (re-search-forward "#\\+END:?" nil t)
20382 (match-end 0))) clist))
20383 ((org-in-src-block-p)
20384 (push (list :src-block
20385 (and (or (looking-at "#\\+BEGIN_SRC")
20386 (search-backward "#+BEGIN_SRC" nil t))
20387 (match-beginning 0))
20388 (and (search-forward "#+END_SRC" nil t)
20389 (match-beginning 0))) clist))))
20392 ;; Now the small context
20394 ((org-at-timestamp-p)
20395 (push (org-point-in-group p 0 :timestamp) clist))
20396 ((memq 'org-link faces)
20398 (previous-single-property-change p 'face)
20399 (next-single-property-change p 'face)) clist))
20400 ((memq 'org-special-keyword faces)
20401 (push (list :keyword
20402 (previous-single-property-change p 'face)
20403 (next-single-property-change p 'face)) clist))
20405 (push (org-point-in-group p 0 :target) clist)
20406 (goto-char (1- (match-beginning 0)))
20407 (if (looking-at org-radio-target-regexp)
20408 (push (org-point-in-group p 0 :radio-target) clist))
20410 ((setq o (car (delq nil
20413 (if (memq x org-latex-fragment-image-overlays) x))
20414 (overlays-at (point))))))
20415 (push (list :latex-fragment
20416 (overlay-start o) (overlay-end o)) clist)
20417 (push (list :latex-preview
20418 (overlay-start o) (overlay-end o)) clist))
20419 ((org-inside-LaTeX-fragment-p)
20420 ;; FIXME: positions wrong.
20421 (push (list :latex-fragment (point) (point)) clist)))
20423 (setq clist (nreverse (delq nil clist)))
20426 ;; FIXME: Compare with at-regexp-p Do we need both?
20427 (defun org-in-regexp (re &optional nlines visually)
20428 "Check if point is inside a match of regexp.
20429 Normally only the current line is checked, but you can include NLINES extra
20430 lines both before and after point into the search.
20431 If VISUALLY is set, require that the cursor is not after the match but
20432 really on, so that the block visually is on the match."
20434 (let ((pos (point))
20435 (eol (point-at-eol (+ 1 (or nlines 0))))
20436 (inc (if visually 1 0)))
20438 (beginning-of-line (- 1 (or nlines 0)))
20439 (while (re-search-forward re eol t)
20440 (if (and (<= (match-beginning 0) pos)
20441 (>= (+ inc (match-end 0)) pos))
20442 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
20444 (defun org-at-regexp-p (regexp)
20445 "Is point inside a match of REGEXP in the current line?"
20448 (let ((pos (point)) (end (point-at-eol)))
20449 (beginning-of-line 1)
20450 (while (re-search-forward regexp end t)
20451 (if (and (<= (match-beginning 0) pos)
20452 (>= (match-end 0) pos))
20456 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
20457 "Non-nil when point is between matches of START-RE and END-RE.
20459 Also return a non-nil value when point is on one of the matches.
20461 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
20462 buffer positions. Default values are the positions of headlines
20463 surrounding the point.
20465 The functions returns a cons cell whose car (resp. cdr) is the
20466 position before START-RE (resp. after END-RE)."
20468 (let ((pos (point))
20469 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
20470 (limit-down (or lim-down (save-excursion (outline-next-heading))))
20473 ;; Point is on a block when on START-RE or if START-RE can be
20474 ;; found before it...
20475 (and (or (org-at-regexp-p start-re)
20476 (re-search-backward start-re limit-up t))
20477 (setq beg (match-beginning 0))
20478 ;; ... and END-RE after it...
20479 (goto-char (match-end 0))
20480 (re-search-forward end-re limit-down t)
20481 (> (setq end (match-end 0)) pos)
20482 ;; ... without another START-RE in-between.
20483 (goto-char (match-beginning 0))
20484 (not (re-search-backward start-re (1+ beg) t))
20486 (cons beg end))))))
20488 (defun org-in-block-p (names)
20489 "Non-nil when point belongs to a block whose name belongs to NAMES.
20491 NAMES is a list of strings containing names of blocks.
20493 Return first block name matched, or nil. Beware that in case of
20494 nested blocks, the returned name may not belong to the closest
20498 (let ((case-fold-search t)
20499 (lim-up (save-excursion (outline-previous-heading)))
20500 (lim-down (save-excursion (outline-next-heading))))
20501 (mapc (lambda (name)
20502 (let ((n (regexp-quote name)))
20503 (when (org-between-regexps-p
20504 (concat "^[ \t]*#\\+begin_" n)
20505 (concat "^[ \t]*#\\+end_" n)
20511 (defun org-occur-in-agenda-files (regexp &optional nlines)
20512 "Call `multi-occur' with buffers for all agenda files."
20513 (interactive "sOrg-files matching: \np")
20514 (let* ((files (org-agenda-files))
20515 (tnames (mapcar 'file-truename files))
20516 (extra org-agenda-text-search-extra-files)
20518 (when (eq (car extra) 'agenda-archives)
20519 (setq extra (cdr extra))
20520 (setq files (org-add-archive-files files)))
20521 (while (setq f (pop extra))
20522 (unless (member (file-truename f) tnames)
20523 (add-to-list 'files f 'append)
20524 (add-to-list 'tnames (file-truename f) 'append)))
20526 (mapcar (lambda (x)
20527 (with-current-buffer
20528 (or (get-file-buffer x) (find-file-noselect x))
20534 (if (boundp 'occur-mode-find-occurrence-hook)
20536 (add-hook 'occur-mode-find-occurrence-hook
20538 (when (derived-mode-p 'org-mode)
20541 (defadvice occur-mode-goto-occurrence
20542 (after org-occur-reveal activate)
20543 (and (derived-mode-p 'org-mode) (org-reveal)))
20544 (defadvice occur-mode-goto-occurrence-other-window
20545 (after org-occur-reveal activate)
20546 (and (derived-mode-p 'org-mode) (org-reveal)))
20547 (defadvice occur-mode-display-occurrence
20548 (after org-occur-reveal activate)
20549 (when (derived-mode-p 'org-mode)
20550 (let ((pos (occur-mode-find-occurrence)))
20551 (with-current-buffer (marker-buffer pos)
20556 (defun org-occur-link-in-agenda-files ()
20557 "Create a link and search for it in the agendas.
20558 The link is not stored in `org-stored-links', it is just created
20559 for the search purpose."
20561 (let ((link (condition-case nil
20562 (org-store-link nil)
20563 (error "Unable to create a link to here"))))
20564 (org-occur-in-agenda-files (regexp-quote link))))
20566 (defun org-uniquify (list)
20567 "Remove duplicate elements from LIST."
20569 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
20572 (defun org-delete-all (elts list)
20573 "Remove all elements in ELTS from LIST."
20575 (setq list (delete (pop elts) list)))
20578 (defun org-count (cl-item cl-seq)
20579 "Count the number of occurrences of ITEM in SEQ.
20580 Taken from `count' in cl-seq.el with all keyword arguments removed."
20581 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
20582 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
20583 (while (< cl-start cl-end)
20584 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
20585 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
20586 (setq cl-start (1+ cl-start)))
20589 (defun org-remove-if (predicate seq)
20590 "Remove everything from SEQ that fulfills PREDICATE."
20594 (if (not (funcall predicate e)) (push e res)))
20597 (defun org-remove-if-not (predicate seq)
20598 "Remove everything from SEQ that does not fulfill PREDICATE."
20602 (if (funcall predicate e) (push e res)))
20605 (defun org-reduce (cl-func cl-seq &rest cl-keys)
20606 "Reduce two-argument FUNCTION across SEQ.
20607 Taken from `reduce' in cl-seq.el with all keyword arguments but
20608 \":initial-value\" removed."
20609 (let ((cl-accum (cond ((memq :initial-value cl-keys)
20610 (cadr (memq :initial-value cl-keys)))
20611 (cl-seq (pop cl-seq))
20612 (t (funcall cl-func)))))
20614 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
20617 (defun org-back-over-empty-lines ()
20618 "Move backwards over whitespace, to the beginning of the first empty line.
20619 Returns the number of empty lines passed."
20620 (let ((pos (point)))
20621 (if (cdr (assoc 'heading org-blank-before-new-entry))
20622 (skip-chars-backward " \t\n\r")
20624 (forward-line -1)))
20625 (beginning-of-line 2)
20626 (goto-char (min (point) pos))
20627 (count-lines (point) pos)))
20629 (defun org-skip-whitespace ()
20630 (skip-chars-forward " \t\n\r"))
20632 (defun org-point-in-group (point group &optional context)
20633 "Check if POINT is in match-group GROUP.
20634 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
20635 match. If the match group does not exist or point is not inside it,
20637 (and (match-beginning group)
20638 (>= point (match-beginning group))
20639 (<= point (match-end group))
20641 (list context (match-beginning group) (match-end group))
20644 (defun org-switch-to-buffer-other-window (&rest args)
20645 "Switch to buffer in a second window on the current frame.
20646 In particular, do not allow pop-up frames.
20647 Returns the newly created buffer."
20649 (apply 'switch-to-buffer-other-window args)))
20651 (defun org-combine-plists (&rest plists)
20652 "Create a single property list from all plists in PLISTS.
20653 The process starts by copying the first list, and then setting properties
20654 from the other lists. Settings in the last list are the most significant
20655 ones and overrule settings in the other lists."
20656 (let ((rtn (copy-sequence (pop plists)))
20659 (setq ls (pop plists))
20661 (setq p (pop ls) v (pop ls))
20662 (setq rtn (plist-put rtn p v))))
20665 (defun org-replace-escapes (string table)
20666 "Replace %-escapes in STRING with values in TABLE.
20667 TABLE is an association list with keys like \"%a\" and string values.
20668 The sequences in STRING may contain normal field width and padding information,
20669 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
20670 so values can contain further %-escapes if they are define later in TABLE."
20671 (let ((tbl (copy-alist table))
20672 (case-fold-search nil)
20675 (while (setq e (pop tbl))
20676 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
20677 (when (and (cdr e) (string-match re (cdr e)))
20678 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
20680 (add-text-properties 0 3 (list 'sref sref) safe)
20681 (setcdr e (replace-match safe t t (cdr e)))))
20682 (while (string-match re string)
20683 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
20685 (setq string (replace-match rpl t t string))))
20686 (while (setq pchg (next-property-change pchg string))
20687 (let ((sref (get-text-property pchg 'sref string)))
20688 (when (and sref (string-match "SREF" string pchg))
20689 (setq string (replace-match sref t t string)))))
20692 (defun org-sublist (list start end)
20693 "Return a section of LIST, from START to END.
20694 Counting starts at 1."
20695 (let (rtn (c start))
20696 (setq list (nthcdr (1- start) list))
20697 (while (and list (<= c end))
20698 (push (pop list) rtn)
20702 (defun org-find-base-buffer-visiting (file)
20703 "Like `find-buffer-visiting' but always return the base buffer and
20704 not an indirect buffer."
20705 (let ((buf (or (get-file-buffer file)
20706 (find-buffer-visiting file))))
20708 (or (buffer-base-buffer buf) buf)
20711 (defun org-image-file-name-regexp (&optional extensions)
20712 "Return regexp matching the file names of images.
20713 If EXTENSIONS is given, only match these."
20714 (if (and (not extensions) (fboundp 'image-file-name-regexp))
20715 (image-file-name-regexp)
20716 (let ((image-file-name-extensions
20718 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
20719 "xbm" "xpm" "pbm" "pgm" "ppm"))))
20721 (regexp-opt (nconc (mapcar 'upcase
20722 image-file-name-extensions)
20723 image-file-name-extensions)
20727 (defun org-file-image-p (file &optional extensions)
20728 "Return non-nil if FILE is an image."
20730 (string-match (org-image-file-name-regexp extensions) file)))
20732 (defun org-get-cursor-date ()
20733 "Return the date at cursor in as a time.
20734 This works in the calendar and in the agenda, anywhere else it just
20735 returns the current time."
20736 (let (date day defd)
20738 ((eq major-mode 'calendar-mode)
20739 (setq date (calendar-cursor-to-date)
20740 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
20741 ((eq major-mode 'org-agenda-mode)
20742 (setq day (get-text-property (point) 'day))
20744 (setq date (calendar-gregorian-from-absolute day)
20745 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
20747 (or defd (current-time))))
20749 (defun org-mark-subtree (&optional up)
20750 "Mark the current subtree.
20751 This puts point at the start of the current subtree, and mark at
20752 the end. If a numeric prefix UP is given, move up into the
20753 hierarchy of headlines by UP levels before marking the subtree."
20755 (org-with-limited-levels
20756 (cond ((org-at-heading-p) (beginning-of-line))
20757 ((org-before-first-heading-p) (error "Not in a subtree"))
20758 (t (outline-previous-visible-heading 1))))
20759 (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
20760 (if (org-called-interactively-p 'any)
20761 (call-interactively 'org-mark-element)
20762 (org-mark-element)))
20766 (defun org-indent-line ()
20767 "Indent line depending on context."
20769 (let* ((pos (point))
20770 (itemp (org-at-item-p))
20771 (case-fold-search t)
20772 (org-drawer-regexp (or org-drawer-regexp "\000"))
20773 (inline-task-p (and (featurep 'org-inlinetask)
20774 (org-inlinetask-in-task-p)))
20775 (inline-re (and inline-task-p
20776 (org-inlinetask-outline-regexp)))
20778 (if (and orgstruct-is-++ (eq pos (point)))
20779 (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
20780 (indent-according-to-mode))
20781 (beginning-of-line 1)
20784 ((looking-at org-outline-regexp) (setq column 0))
20786 ((looking-at "#\\+include:") (setq column 0))
20787 ;; Footnote definition
20788 ((looking-at org-footnote-definition-re) (setq column 0))
20789 ;; Literal examples
20790 ((looking-at "[ \t]*:\\( \\|$\\)")
20791 (setq column (org-get-indentation))) ; do nothing
20793 ((ignore-errors (goto-char (org-in-item-p)))
20794 (setq column (if itemp
20795 (org-get-indentation)
20796 (org-list-item-body-column (point))))
20799 ((and (looking-at "[ \t]*:END:")
20800 (save-excursion (re-search-backward org-drawer-regexp nil t)))
20802 (goto-char (1- (match-beginning 1)))
20803 (setq column (current-column))))
20805 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
20807 (re-search-backward
20808 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
20809 (setq column (org-get-indentation (match-string 0))))
20810 ((and (not (looking-at "[ \t]*#\\+begin_"))
20811 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
20813 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
20815 (cond ((equal (downcase (match-string 1)) "src")
20816 ;; src blocks: let `org-edit-src-exit' handle them
20817 (org-get-indentation))
20818 ((equal (downcase (match-string 1)) "example")
20819 (max (org-get-indentation)
20820 (org-get-indentation (match-string 0))))
20822 (org-get-indentation (match-string 0))))))
20823 ;; This line has nothing special, look at the previous relevant
20824 ;; line to compute indentation
20826 (beginning-of-line 0)
20827 (while (and (not (bobp))
20828 (not (looking-at org-table-line-regexp))
20829 (not (looking-at org-drawer-regexp))
20830 ;; When point started in an inline task, do not move
20831 ;; above task starting line.
20832 (not (and inline-task-p (looking-at inline-re)))
20833 ;; Skip drawers, blocks, empty lines, verbatim,
20834 ;; comments, tables, footnotes definitions, lists,
20836 (or (and (looking-at "[ \t]*:END:")
20837 (re-search-backward org-drawer-regexp nil t))
20838 (and (looking-at "[ \t]*#\\+end_")
20839 (re-search-backward "[ \t]*#\\+begin_"nil t))
20840 (looking-at "[ \t]*[\n:#|]")
20841 (looking-at org-footnote-definition-re)
20842 (and (ignore-errors (goto-char (org-in-item-p)))
20844 (org-list-get-top-point (org-list-struct))))
20845 (and (not inline-task-p)
20846 (featurep 'org-inlinetask)
20847 (org-inlinetask-in-task-p)
20848 (or (org-inlinetask-goto-beginning) t))))
20849 (beginning-of-line 0))
20851 ;; There was an heading above.
20852 ((looking-at "\\*+[ \t]+")
20853 (if (not org-adapt-indentation)
20855 (goto-char (match-end 0))
20856 (setq column (current-column))))
20857 ;; A drawer had started and is unfinished
20858 ((looking-at org-drawer-regexp)
20859 (goto-char (1- (match-beginning 1)))
20860 (setq column (current-column)))
20861 ;; Else, nothing noticeable found: get indentation and go on.
20862 (t (setq column (org-get-indentation))))))
20863 ;; Now apply indentation and move cursor accordingly
20865 (if (<= (current-column) (current-indentation))
20866 (org-indent-line-to column)
20867 (save-excursion (org-indent-line-to column)))
20868 ;; Special polishing for properties, see `org-property-format'
20869 (setq column (current-column))
20870 (beginning-of-line 1)
20872 "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
20873 (replace-match (concat (match-string 1)
20874 (format org-property-format
20875 (match-string 2) (match-string 3)))
20877 (org-move-to-column column))))
20879 (defun org-indent-drawer ()
20880 "Indent the drawer at point."
20883 (e (and (save-excursion (re-search-forward ":END:" nil t))
20888 (when (overlays-at (point))
20889 (member 'invisible (overlay-properties
20890 (car (overlays-at (point)))))))))
20891 (when folded (org-cycle))
20892 (indent-for-tab-command)
20893 (while (and (move-beginning-of-line 2) (< (point) e))
20894 (indent-for-tab-command))
20896 (when folded (org-cycle)))
20897 (message "Drawer at point indented"))
20899 (defun org-indent-block ()
20900 "Indent the block at point."
20903 (case-fold-search t)
20904 (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
20909 (when (overlays-at (point))
20910 (member 'invisible (overlay-properties
20911 (car (overlays-at (point)))))))))
20912 (when folded (org-cycle))
20913 (indent-for-tab-command)
20914 (while (and (move-beginning-of-line 2) (< (point) e))
20915 (indent-for-tab-command))
20917 (when folded (org-cycle)))
20918 (message "Block at point indented"))
20920 (defun org-indent-region (start end)
20924 (let ((line-end (org-current-line end)))
20926 (while (< (org-current-line) line-end)
20927 (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
20928 (t (call-interactively 'org-indent-line)))
20929 (move-beginning-of-line 2)))))
20934 ;; We use our own fill-paragraph and auto-fill functions.
20936 ;; `org-fill-paragraph' relies on adaptive filling and context
20937 ;; checking. Appropriate `fill-prefix' is computed with
20938 ;; `org-adaptive-fill-function'.
20940 ;; `org-auto-fill-function' takes care of auto-filling. It calls
20941 ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
20942 ;; `org-adaptive-fill-function' value. Internally,
20943 ;; `org-comment-line-break-function' breaks the line.
20945 ;; `org-setup-filling' installs filling and auto-filling related
20946 ;; variables during `org-mode' initialization.
20948 (defun org-setup-filling ()
20950 ;; Prevent auto-fill from inserting unwanted new items.
20951 (when (boundp 'fill-nobreak-predicate)
20953 'fill-nobreak-predicate
20955 (append fill-nobreak-predicate
20956 '(org-fill-paragraph-separate-nobreak-p
20957 org-fill-line-break-nobreak-p)))))
20958 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
20959 (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
20960 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
20961 (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
20963 (defvar org-element-paragraph-separate) ; org-element.el
20964 (defun org-fill-paragraph-separate-nobreak-p ()
20965 "Non-nil when a line break at point would insert a new item."
20966 (looking-at (substring org-element-paragraph-separate 1)))
20968 (defun org-fill-line-break-nobreak-p ()
20969 "Non-nil when a line break at point would create an Org line break."
20971 (skip-chars-backward "[ \t]")
20972 (skip-chars-backward "\\\\")
20973 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
20975 (declare-function message-in-body-p "message" ())
20976 (defvar org-element--affiliated-re) ; From org-element.el
20977 (defun org-adaptive-fill-function ()
20978 "Compute a fill prefix for the current line.
20979 Return fill prefix, as a string, or nil if current line isn't
20980 meant to be filled."
20982 (when (and (derived-mode-p 'message-mode) (message-in-body-p))
20984 (beginning-of-line)
20985 (cond ((looking-at message-cite-prefix-regexp)
20986 (setq prefix (match-string-no-properties 0)))
20987 ((looking-at org-outline-regexp)
20988 (setq prefix "")))))
20990 (org-with-wide-buffer
20991 (let* ((p (line-beginning-position))
20992 (element (save-excursion (beginning-of-line) (org-element-at-point)))
20993 (type (org-element-type element))
20996 (goto-char (org-element-property :begin element))
20997 (while (looking-at org-element--affiliated-re) (forward-line))
20999 (unless (< p post-affiliated)
21001 (comment (looking-at "[ \t]*# ?") (match-string 0))
21002 (footnote-definition "")
21004 (make-string (org-list-item-body-column post-affiliated) ? ))
21006 ;; Fill prefix is usually the same as the current line,
21007 ;; except if the paragraph is at the beginning of an item.
21008 (let ((parent (org-element-property :parent element)))
21009 (cond ((eq (org-element-type parent) 'item)
21010 (make-string (org-list-item-body-column
21011 (org-element-property :begin parent))
21013 ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
21017 ;; Only fill contents if P is within block boundaries.
21018 (let* ((cbeg (save-excursion (goto-char post-affiliated)
21021 (cend (save-excursion
21022 (goto-char (org-element-property :end element))
21023 (skip-chars-backward " \r\t\n")
21024 (line-beginning-position))))
21025 (when (and (>= p cbeg) (< p cend))
21026 (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
21030 (declare-function message-goto-body "message" ())
21031 (defvar message-cite-prefix-regexp) ; From message.el
21032 (defvar org-element-all-objects) ; From org-element.el
21033 (defun org-fill-paragraph (&optional justify)
21034 "Fill element at point, when applicable.
21036 This function only applies to comment blocks, comments, example
21037 blocks and paragraphs. Also, as a special case, re-align table
21038 when point is at one.
21040 If JUSTIFY is non-nil (interactively, with prefix argument),
21041 justify as well. If `sentence-end-double-space' is non-nil, then
21042 period followed by one space does not end a sentence, so don't
21043 break a line there. The variable `fill-column' controls the
21046 For convenience, when point is at a plain list, an item or
21047 a footnote definition, try to fill the first paragraph within."
21049 (if (and (derived-mode-p 'message-mode)
21050 (or (not (message-in-body-p))
21051 (save-excursion (move-beginning-of-line 1)
21052 (looking-at message-cite-prefix-regexp))))
21053 ;; First ensure filling is correct in message-mode.
21054 (let ((fill-paragraph-function
21055 (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
21056 (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
21057 (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
21058 (paragraph-separate
21059 (cadadr (assoc 'paragraph-separate org-fb-vars))))
21060 (fill-paragraph nil))
21062 ;; Move to end of line in order to get the first paragraph
21063 ;; within a plain list or a footnote definition.
21065 (let ((element (org-element-at-point)))
21066 ;; First check if point is in a blank line at the beginning of
21067 ;; the buffer. In that case, ignore filling.
21068 (if (< (point) (org-element-property :begin element)) t
21069 (case (org-element-type element)
21070 ;; Use major mode filling function is src blocks.
21071 (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
21072 ;; Align Org tables, leave table.el tables as-is.
21073 (table-row (org-table-align) t)
21075 (when (eq (org-element-property :type element) 'org)
21079 ;; Paragraphs may contain `line-break' type objects.
21080 (let ((beg (max (point-min)
21081 (org-element-property :contents-begin element)))
21082 (end (min (point-max)
21083 (org-element-property :contents-end element))))
21084 ;; Do nothing if point is at an affiliated keyword.
21085 (if (< (point) beg) t
21086 (when (derived-mode-p 'message-mode)
21087 ;; In `message-mode', do not fill following
21088 ;; citation in current paragraph nor text before
21090 (let ((body-start (save-excursion (message-goto-body))))
21091 (when body-start (setq beg (max body-start beg))))
21092 (when (save-excursion
21094 (concat "^" message-cite-prefix-regexp) end t))
21095 (setq end (match-beginning 0))))
21096 ;; Fill paragraph, taking line breaks into
21097 ;; consideration. For that, slice the paragraph
21098 ;; using line breaks as separators, and fill the
21099 ;; parts in reverse order to avoid messing with
21105 (fill-region-as-paragraph pos (point) justify)
21107 ;; Find the list of ending positions for line
21108 ;; breaks in the current paragraph. Add paragraph
21109 ;; beginning to include first slice.
21114 (org-element--parse-objects
21115 beg end nil org-element-all-objects)
21117 (lambda (lb) (org-element-property :end lb)))))))
21119 ;; Contents of `comment-block' type elements should be
21120 ;; filled as plain text, but only if point is within block
21123 (let* ((case-fold-search t)
21124 (beg (save-excursion
21125 (goto-char (org-element-property :begin element))
21126 (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
21129 (end (save-excursion
21130 (goto-char (org-element-property :end element))
21131 (re-search-backward "^[ \t]*#\\+end_comment" nil t)
21132 (line-beginning-position))))
21133 (when (and (>= (point) beg) (< (point) end))
21134 (fill-region-as-paragraph
21137 (re-search-backward "^[ \t]*$" beg 'move)
21138 (line-beginning-position))
21140 (beginning-of-line)
21141 (re-search-forward "^[ \t]*$" end 'move)
21142 (line-beginning-position))
21146 (comment (fill-comment-paragraph justify))
21147 ;; Ignore every other element.
21148 (otherwise t)))))))
21150 (defun org-auto-fill-function ()
21151 "Auto-fill function."
21152 ;; Check if auto-filling is meaningful.
21153 (let ((fc (current-fill-column)))
21154 (when (and fc (> (current-column) fc))
21155 (let* ((fill-prefix (org-adaptive-fill-function))
21156 ;; Enforce empty fill prefix, if required. Otherwise, it
21157 ;; will be computed again.
21158 (adaptive-fill-mode (not (equal fill-prefix ""))))
21159 (when fill-prefix (do-auto-fill))))))
21161 (defun org-comment-line-break-function (&optional soft)
21162 "Break line at point and indent, continuing comment if within one.
21163 The inserted newline is marked hard if variable
21164 `use-hard-newlines' is true, unless optional argument SOFT is
21166 (if soft (insert-and-inherit ?\n) (newline 1))
21167 (save-excursion (forward-char -1) (delete-horizontal-space))
21168 (delete-horizontal-space)
21169 (indent-to-left-margin)
21170 (insert-before-markers-and-inherit fill-prefix))
21175 ;; Org comments syntax is quite complex. It requires the entire line
21176 ;; to be just a comment. Also, even with the right syntax at the
21177 ;; beginning of line, some some elements (i.e. verse-block or
21178 ;; example-block) don't accept comments. Usual Emacs comment commands
21179 ;; cannot cope with those requirements. Therefore, Org replaces them.
21181 ;; Org still relies on `comment-dwim', but cannot trust
21182 ;; `comment-only-p'. So, `comment-region-function' and
21183 ;; `uncomment-region-function' both point
21184 ;; to`org-comment-or-uncomment-region'. Eventually,
21185 ;; `org-insert-comment' takes care of insertion of comments at the
21186 ;; beginning of line.
21188 ;; `org-setup-comments-handling' install comments related variables
21189 ;; during `org-mode' initialization.
21191 (defun org-setup-comments-handling ()
21193 (org-set-local 'comment-use-syntax nil)
21194 (org-set-local 'comment-start "# ")
21195 (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
21196 (org-set-local 'comment-insert-comment-function 'org-insert-comment)
21197 (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
21198 (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
21200 (defun org-insert-comment ()
21201 "Insert an empty comment above current line.
21202 If the line is empty, insert comment at its beginning."
21203 (beginning-of-line)
21204 (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
21208 (defvar comment-empty-lines) ; From newcomment.el.
21209 (defun org-comment-or-uncomment-region (beg end &rest ignore)
21210 "Comment or uncomment each non-blank line in the region.
21211 Uncomment each non-blank line between BEG and END if it only
21212 contains commented lines. Otherwise, comment them."
21215 (narrow-to-region (save-excursion (goto-char beg)
21216 (skip-chars-forward " \r\t\n" end)
21217 (line-beginning-position))
21218 (save-excursion (goto-char end)
21219 (skip-chars-backward " \r\t\n" beg)
21220 (line-end-position)))
21222 ;; UNCOMMENTP is non-nil when every non blank line between
21223 ;; BEG and END is a comment.
21225 (goto-char (point-min))
21226 (while (and (not (eobp))
21227 (let ((element (org-element-at-point)))
21228 (and (eq (org-element-type element) 'comment)
21229 (goto-char (min (point-max)
21230 (org-element-property
21231 :end element)))))))
21234 ;; Only blank lines and comments in region: uncomment it.
21236 (goto-char (point-min))
21237 (while (not (eobp))
21238 (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
21239 (replace-match "" nil nil nil 1))
21241 ;; Comment each line in region.
21242 (let ((min-indent (point-max)))
21243 ;; First find the minimum indentation across all lines.
21245 (goto-char (point-min))
21246 (while (and (not (eobp)) (not (zerop min-indent)))
21247 (unless (looking-at "[ \t]*$")
21248 (setq min-indent (min min-indent (current-indentation))))
21250 ;; Then loop over all lines.
21252 (goto-char (point-min))
21253 (while (not (eobp))
21254 (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
21255 (org-move-to-column min-indent t)
21256 (insert comment-start))
21257 (forward-line))))))))
21262 (defun org-toggle-fixed-width-section (arg)
21263 "Toggle the fixed-width export.
21264 If there is no active region, the QUOTE keyword at the current headline is
21265 inserted or removed. When present, it causes the text between this headline
21266 and the next to be exported as fixed-width text, and unmodified.
21267 If there is an active region, this command adds or removes a colon as the
21268 first character of this line. If the first character of a line is a colon,
21269 this line is also exported in fixed-width font."
21272 (regionp (org-region-active-p))
21273 (beg (if regionp (region-beginning) (point)))
21274 (end (if regionp (region-end)))
21275 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
21276 (case-fold-search nil)
21277 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
21282 (setq cc (current-column))
21283 (beginning-of-line 1)
21284 (setq off (looking-at re))
21285 (while (> nlines 0)
21286 (setq nlines (1- nlines))
21287 (beginning-of-line 1)
21290 (org-move-to-column cc t)
21293 ((and off (looking-at re))
21294 (replace-match "" t t nil 1))
21295 ((not off) (org-move-to-column cc t) (insert ": ")))
21298 (org-back-to-heading)
21300 ((looking-at (format org-heading-keyword-regexp-format
21302 (goto-char (match-end 1))
21303 (looking-at (concat " +" org-quote-string))
21304 (replace-match "" t t)
21305 (when (eolp) (insert " ")))
21306 ((looking-at org-outline-regexp)
21307 (goto-char (match-end 0))
21308 (insert org-quote-string " ")))))))
21310 (defun org-reftex-citation ()
21311 "Use reftex-citation to insert a citation into the buffer.
21312 This looks for a line like
21314 #+BIBLIOGRAPHY: foo plain option:-d
21316 and derives from it that foo.bib is the bibliography file relevant
21317 for this document. It then installs the necessary environment for RefTeX
21318 to work in this buffer and calls `reftex-citation' to insert a citation
21321 Export of such citations to both LaTeX and HTML is handled by the contributed
21322 package org-exp-bibtex by Taru Karttunen."
21324 (let ((reftex-docstruct-symbol 'rds)
21325 (reftex-cite-format "\\cite{%l}")
21330 (let ((case-fold-search t)
21331 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
21332 (if (not (save-excursion
21333 (or (re-search-forward re nil t)
21334 (re-search-backward re nil t))))
21335 (error "No bibliography defined in file")
21336 (setq bib (concat (match-string 1) ".bib")
21337 rds (list (list 'bib bib)))))))
21338 (call-interactively 'reftex-citation)))
21340 ;;;; Functions extending outline functionality
21342 (defun org-beginning-of-line (&optional arg)
21343 "Go to the beginning of the current line. If that is invisible, continue
21344 to a visible line beginning. This makes the function of C-a more intuitive.
21345 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
21346 first attempt, and only move to after the tags when the cursor is already
21347 beyond the end of the headline."
21349 (let ((pos (point))
21350 (special (if (consp org-special-ctrl-a/e)
21351 (car org-special-ctrl-a/e)
21352 org-special-ctrl-a/e))
21354 (if (org-bound-and-true-p visual-line-mode)
21355 (beginning-of-visual-line 1)
21356 (beginning-of-line 1))
21357 (if (and arg (fboundp 'move-beginning-of-line))
21358 (call-interactively 'move-beginning-of-line)
21362 (if (org-truely-invisible-p)
21363 (while (and (not (bobp)) (org-truely-invisible-p))
21365 (beginning-of-line 1))
21366 (forward-char 1))))
21369 ((and (looking-at org-complex-heading-regexp)
21370 (= (char-after (match-end 1)) ?\ ))
21371 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
21375 (cond ((> pos refpos) refpos)
21376 ((= pos (point)) refpos)
21378 (cond ((> pos (point)) (point))
21379 ((not (eq last-command this-command)) (point))
21382 ;; Being at an item and not looking at an the item means point
21383 ;; was previously moved to beginning of a visual line, which
21384 ;; doesn't contain the item. Therefore, do nothing special,
21386 (when (looking-at org-list-full-item-re)
21387 ;; Set special position at first white space character after
21388 ;; bullet, and check-box, if any.
21389 (let ((after-bullet
21390 (let ((box (match-end 3)))
21391 (if (not box) (match-end 1)
21392 (let ((after (char-after box)))
21393 (if (and after (= after ? )) (1+ box) box))))))
21394 ;; Special case: Move point to special position when
21395 ;; currently after it or at beginning of line.
21397 (when (or (> pos after-bullet) (= (point) pos))
21398 (goto-char after-bullet))
21399 ;; Reversed case: Move point to special position when
21400 ;; point was already at beginning of line and command is
21402 (when (and (= (point) pos) (eq last-command this-command))
21403 (goto-char after-bullet))))))))
21405 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21407 (defun org-end-of-line (&optional arg)
21408 "Go to the end of the line.
21409 If this is a headline, and `org-special-ctrl-a/e' is set, ignore
21410 tags on the first attempt, and only move to after the tags when
21411 the cursor is already beyond the end of the headline."
21413 (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
21414 org-special-ctrl-a/e))
21415 (move-fun (cond ((org-bound-and-true-p visual-line-mode)
21416 'end-of-visual-line)
21417 ((fboundp 'move-end-of-line) 'move-end-of-line)
21418 (t 'end-of-line))))
21419 (if (or (not special) arg) (call-interactively move-fun)
21420 (let* ((element (save-excursion (beginning-of-line)
21421 (org-element-at-point)))
21422 (type (org-element-type element)))
21424 ((memq type '(headline inlinetask))
21425 (let ((pos (point)))
21426 (beginning-of-line 1)
21427 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
21429 (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
21430 (goto-char (match-beginning 1))
21431 (goto-char (match-end 0)))
21432 (if (or (< pos (match-end 0))
21433 (not (eq this-command last-command)))
21434 (goto-char (match-end 0))
21435 (goto-char (match-beginning 1))))
21436 (call-interactively move-fun))))
21437 ((org-element-property :hiddenp element)
21438 ;; If element is hidden, `move-end-of-line' would put point
21439 ;; after it. Use `end-of-line' to stay on current line.
21440 (call-interactively 'end-of-line))
21441 (t (call-interactively move-fun)))))
21442 (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21444 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
21445 (define-key org-mode-map "\C-e" 'org-end-of-line)
21447 (defun org-backward-sentence (&optional arg)
21448 "Go to beginning of sentence, or beginning of table field.
21449 This will call `backward-sentence' or `org-table-beginning-of-field',
21450 depending on context."
21453 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
21454 (t (call-interactively 'backward-sentence))))
21456 (defun org-forward-sentence (&optional arg)
21457 "Go to end of sentence, or end of table field.
21458 This will call `forward-sentence' or `org-table-end-of-field',
21459 depending on context."
21462 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
21463 (t (call-interactively 'forward-sentence))))
21465 (define-key org-mode-map "\M-a" 'org-backward-sentence)
21466 (define-key org-mode-map "\M-e" 'org-forward-sentence)
21468 (defun org-kill-line (&optional arg)
21469 "Kill line, to tags or end of line."
21472 ((or (not org-special-ctrl-k)
21474 (not (org-at-heading-p)))
21475 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
21476 org-ctrl-k-protect-subtree)
21477 (if (or (eq org-ctrl-k-protect-subtree 'error)
21478 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
21479 (error "C-k aborted - would kill hidden subtree")))
21480 (call-interactively
21481 (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
21482 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
21483 (kill-region (point) (match-beginning 1))
21484 (org-set-tags nil t))
21485 (t (kill-region (point) (point-at-eol)))))
21487 (define-key org-mode-map "\C-k" 'org-kill-line)
21489 (defun org-yank (&optional arg)
21490 "Yank. If the kill is a subtree, treat it specially.
21491 This command will look at the current kill and check if is a single
21492 subtree, or a series of subtrees[1]. If it passes the test, and if the
21493 cursor is at the beginning of a line or after the stars of a currently
21494 empty headline, then the yank is handled specially. How exactly depends
21495 on the value of the following variables, both set by default.
21497 org-yank-folded-subtrees
21498 When set, the subtree(s) will be folded after insertion, but only
21499 if doing so would now swallow text after the yanked text.
21501 org-yank-adjusted-subtrees
21502 When set, the subtree will be promoted or demoted in order to
21503 fit into the local outline tree structure, which means that the level
21504 will be adjusted so that it becomes the smaller one of the two
21505 *visible* surrounding headings.
21507 Any prefix to this command will cause `yank' to be called directly with
21508 no special treatment. In particular, a simple \\[universal-argument] prefix \
21510 plainly yank the text as it is.
21512 \[1] The test checks if the first non-white line is a heading
21513 and if there are no other headings with fewer stars."
21515 (org-yank-generic 'yank arg))
21517 (defun org-yank-generic (command arg)
21518 "Perform some yank-like command.
21520 This function implements the behavior described in the `org-yank'
21521 documentation. However, it has been generalized to work for any
21522 interactive command with similar behavior."
21524 ;; pretend to be command COMMAND
21525 (setq this-command command)
21528 (call-interactively command)
21530 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
21531 (and (org-kill-is-subtree-p)
21533 (and (looking-at "[ \t]*$")
21536 (buffer-substring (point-at-bol) (point)))))))
21539 ((and subtreep org-yank-folded-subtrees)
21540 (let ((beg (point))
21542 (if (and subtreep org-yank-adjusted-subtrees)
21543 (org-paste-subtree nil nil 'for-yank)
21544 (call-interactively command))
21548 (when (and (bolp) subtreep
21549 (not (setq swallowp
21550 (org-yank-folding-would-swallow-text beg end))))
21551 (org-with-limited-levels
21552 (or (looking-at org-outline-regexp)
21553 (re-search-forward org-outline-regexp-bol end t))
21554 (while (and (< (point) end) (looking-at org-outline-regexp))
21556 (org-cycle-show-empty-lines 'folded)
21557 (condition-case nil
21558 (outline-forward-same-level 1)
21559 (error (goto-char end))))))
21562 "Inserted text not folded because that would swallow text"))
21565 (skip-chars-forward " \t\n\r")
21566 (beginning-of-line 1)
21567 (push-mark beg 'nomsg)))
21568 ((and subtreep org-yank-adjusted-subtrees)
21569 (let ((beg (point-at-bol)))
21570 (org-paste-subtree nil nil 'for-yank)
21571 (push-mark beg 'nomsg)))
21573 (call-interactively command))))))
21575 (defun org-yank-folding-would-swallow-text (beg end)
21576 "Would hide-subtree at BEG swallow any text after END?"
21578 (org-with-limited-levels
21581 (when (or (looking-at org-outline-regexp)
21582 (re-search-forward org-outline-regexp-bol end t))
21583 (setq level (org-outline-level)))
21585 (skip-chars-forward " \t\r\n\v\f")
21587 (and (bolp) (looking-at org-outline-regexp)
21588 (<= (org-outline-level) level)))
21589 nil ; Nothing would be swallowed
21590 t))))) ; something would swallow
21592 (define-key org-mode-map "\C-y" 'org-yank)
21594 (defun org-truely-invisible-p ()
21595 "Check if point is at a character currently not visible.
21596 This version does not only check the character property, but also
21598 ;; Early versions of noutline don't have `outline-invisible-p'.
21599 (if (org-bound-and-true-p visible-mode)
21601 (outline-invisible-p)))
21603 (defun org-invisible-p2 ()
21604 "Check if point is at a character currently not visible."
21606 (if (and (eolp) (not (bobp))) (backward-char 1))
21607 ;; Early versions of noutline don't have `outline-invisible-p'.
21608 (outline-invisible-p)))
21610 (defun org-back-to-heading (&optional invisible-ok)
21611 "Call `outline-back-to-heading', but provide a better error message."
21612 (condition-case nil
21613 (outline-back-to-heading invisible-ok)
21614 (error (error "Before first headline at position %d in buffer %s"
21615 (point) (current-buffer)))))
21617 (defun org-before-first-heading-p ()
21618 "Before first heading?"
21621 (null (re-search-backward org-outline-regexp-bol nil t))))
21623 (defun org-at-heading-p (&optional ignored)
21624 (outline-on-heading-p t))
21625 ;; Compatibility alias with Org versions < 7.8.03
21626 (defalias 'org-on-heading-p 'org-at-heading-p)
21628 (defun org-at-comment-p nil
21629 "Is cursor in a line starting with a # character?"
21631 (beginning-of-line)
21632 (looking-at "^#")))
21634 (defun org-at-drawer-p nil
21635 "Is cursor at a drawer keyword?"
21637 (move-beginning-of-line 1)
21638 (looking-at org-drawer-regexp)))
21640 (defun org-at-block-p nil
21641 "Is cursor at a block keyword?"
21643 (move-beginning-of-line 1)
21644 (looking-at org-block-regexp)))
21646 (defun org-point-at-end-of-empty-headline ()
21647 "If point is at the end of an empty headline, return t, else nil.
21648 If the heading only contains a TODO keyword, it is still still considered
21650 (and (looking-at "[ \t]*$")
21651 (when org-todo-line-regexp
21653 (beginning-of-line 1)
21654 (let ((case-fold-search nil))
21655 (looking-at org-todo-line-regexp)
21656 (string= (match-string 3) ""))))))
21658 (defun org-at-heading-or-item-p ()
21659 (or (org-at-heading-p) (org-at-item-p)))
21661 (defun org-at-target-p ()
21662 (or (org-in-regexp org-radio-target-regexp)
21663 (org-in-regexp org-target-regexp)))
21664 ;; Compatibility alias with Org versions < 7.8.03
21665 (defalias 'org-on-target-p 'org-at-target-p)
21667 (defun org-up-heading-all (arg)
21668 "Move to the heading line of which the present line is a subheading.
21669 This function considers both visible and invisible heading lines.
21670 With argument, move up ARG levels."
21671 (if (fboundp 'outline-up-heading-all)
21672 (outline-up-heading-all arg) ; emacs 21 version of outline.el
21673 (outline-up-heading arg t))) ; emacs 22 version of outline.el
21675 (defun org-up-heading-safe ()
21676 "Move to the heading line of which the present line is a subheading.
21677 This version will not throw an error. It will return the level of the
21678 headline found, or nil if no higher level is found.
21680 Also, this function will be a lot faster than `outline-up-heading',
21681 because it relies on stars being the outline starters. This can really
21682 make a significant difference in outlines with very many siblings."
21683 (let (start-level re)
21684 (org-back-to-heading t)
21685 (setq start-level (funcall outline-level))
21686 (if (equal start-level 1)
21688 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
21689 (if (re-search-backward re nil t)
21690 (funcall outline-level)))))
21692 (defun org-first-sibling-p ()
21693 "Is this heading the first child of its parents?"
21695 (let ((re org-outline-regexp-bol)
21697 (unless (org-at-heading-p t)
21698 (error "Not at a heading"))
21699 (setq level (funcall outline-level))
21701 (if (not (re-search-backward re nil t))
21703 (setq l (funcall outline-level))
21706 (defun org-goto-sibling (&optional previous)
21707 "Goto the next sibling, even if it is invisible.
21708 When PREVIOUS is set, go to the previous sibling instead. Returns t
21709 when a sibling was found. When none is found, return nil and don't
21711 (let ((fun (if previous 're-search-backward 're-search-forward))
21713 (re org-outline-regexp-bol)
21715 (when (condition-case nil (org-back-to-heading t) (error nil))
21716 (setq level (funcall outline-level))
21718 (or previous (forward-char 1))
21719 (while (funcall fun re nil t)
21720 (setq l (funcall outline-level))
21721 (when (< l level) (goto-char pos) (throw 'exit nil))
21722 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
21726 (defun org-show-siblings ()
21727 "Show all siblings of the current headline."
21729 (while (org-goto-sibling) (org-flag-heading nil)))
21731 (while (org-goto-sibling 'previous)
21732 (org-flag-heading nil))))
21734 (defun org-goto-first-child ()
21735 "Goto the first child, even if it is invisible.
21736 Return t when a child was found. Otherwise don't move point and
21738 (let (level (pos (point)) (re org-outline-regexp-bol))
21739 (when (condition-case nil (org-back-to-heading t) (error nil))
21740 (setq level (outline-level))
21742 (if (and (re-search-forward re nil t) (> (outline-level) level))
21743 (progn (goto-char (match-beginning 0)) t)
21744 (goto-char pos) nil))))
21746 (defun org-show-hidden-entry ()
21747 "Show an entry where even the heading is hidden."
21751 (defun org-flag-heading (flag &optional entry)
21752 "Flag the current heading. FLAG non-nil means make invisible.
21753 When ENTRY is non-nil, show the entire entry."
21755 (org-back-to-heading t)
21756 ;; Check if we should show the entire entry
21761 (and (outline-next-heading)
21762 (org-flag-heading nil))))
21763 (outline-flag-region (max (point-min) (1- (point)))
21764 (save-excursion (outline-end-of-heading) (point))
21767 (defun org-get-next-sibling ()
21768 "Move to next heading of the same level, and return point.
21769 If there is no such heading, return nil.
21770 This is like outline-next-sibling, but invisible headings are ok."
21771 (let ((level (funcall outline-level)))
21772 (outline-next-heading)
21773 (while (and (not (eobp)) (> (funcall outline-level) level))
21774 (outline-next-heading))
21775 (if (or (eobp) (< (funcall outline-level) level))
21779 (defun org-get-last-sibling ()
21780 "Move to previous heading of the same level, and return point.
21781 If there is no such heading, return nil."
21782 (let ((opoint (point))
21783 (level (funcall outline-level)))
21784 (outline-previous-heading)
21785 (when (and (/= (point) opoint) (outline-on-heading-p t))
21786 (while (and (> (funcall outline-level) level)
21788 (outline-previous-heading))
21789 (if (< (funcall outline-level) level)
21793 (defun org-end-of-subtree (&optional invisible-ok to-heading)
21794 "Goto to the end of a subtree."
21795 ;; This contains an exact copy of the original function, but it uses
21796 ;; `org-back-to-heading', to make it work also in invisible
21797 ;; trees. And is uses an invisible-ok argument.
21798 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
21799 ;; Furthermore, when used inside Org, finding the end of a large subtree
21800 ;; with many children and grandchildren etc, this can be much faster
21801 ;; than the outline version.
21802 (org-back-to-heading invisible-ok)
21804 (level (funcall outline-level)))
21805 (if (and (derived-mode-p 'org-mode) (< level 1000))
21806 ;; A true heading (not a plain list item), in Org-mode
21807 ;; This means we can easily find the end by looking
21808 ;; only for the right number of stars. Using a regexp to do
21809 ;; this is so much faster than using a Lisp loop.
21810 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
21812 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
21813 ;; something else, do it the slow way
21814 (while (and (not (eobp))
21815 (or first (> (funcall outline-level) level)))
21817 (outline-next-heading)))
21819 (if (memq (preceding-char) '(?\n ?\^M))
21821 ;; Go to end of line before heading
21823 (if (memq (preceding-char) '(?\n ?\^M))
21824 ;; leave blank line before heading
21825 (forward-char -1))))))
21828 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
21829 "Use Org version in org-mode, for dramatic speed-up."
21830 (if (derived-mode-p 'org-mode)
21832 (org-end-of-subtree nil t)
21833 (unless (eobp) (backward-char 1)))
21836 (defun org-end-of-meta-data-and-drawers ()
21837 "Jump to the first text after meta data and drawers in the current entry.
21838 This will move over empty lines, lines with planning time stamps,
21839 clocking lines, and drawers."
21840 (org-back-to-heading t)
21841 (let ((end (save-excursion (outline-next-heading) (point)))
21842 (re (concat "\\(" org-drawer-regexp "\\)"
21843 "\\|" "[ \t]*" org-keyword-time-regexp)))
21845 (while (re-search-forward re end t)
21846 (if (not (match-end 1))
21847 ;; empty or planning line
21849 ;; a drawer, find the end
21850 (re-search-forward "^[ \t]*:END:" end 'move)
21852 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
21855 (defun org-forward-heading-same-level (arg &optional invisible-ok)
21856 "Move forward to the arg'th subheading at same level as this one.
21857 Stop at the first and last subheadings of a superior heading.
21858 Normally this only looks at visible headings, but when INVISIBLE-OK is
21859 non-nil it will also look at invisible ones."
21861 (org-back-to-heading invisible-ok)
21863 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21864 (re (format "^\\*\\{1,%d\\} " level))
21868 (while (and (re-search-forward re nil 'move)
21869 (setq l (- (match-end 0) (match-beginning 0) 1))
21872 (progn (backward-char 1) (outline-invisible-p)))
21873 (if (< l level) (setq arg 1)))
21874 (setq arg (1- arg)))
21875 (beginning-of-line 1)))
21877 (defun org-backward-heading-same-level (arg &optional invisible-ok)
21878 "Move backward to the arg'th subheading at same level as this one.
21879 Stop at the first and last subheadings of a superior heading."
21881 (org-back-to-heading)
21883 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21884 (re (format "^\\*\\{1,%d\\} " level))
21887 (while (and (re-search-backward re nil 'move)
21888 (setq l (- (match-end 0) (match-beginning 0) 1))
21891 (outline-invisible-p))
21892 (if (< l level) (setq arg 1)))
21893 (setq arg (1- arg)))))
21895 (defun org-forward-element ()
21896 "Move forward by one element.
21897 Move to the next element at the same level, when possible."
21899 (cond ((eobp) (error "Cannot move further down"))
21900 ((org-with-limited-levels (org-at-heading-p))
21901 (let ((origin (point)))
21902 (org-forward-heading-same-level 1)
21903 (unless (org-with-limited-levels (org-at-heading-p))
21905 (error "Cannot move further down"))))
21907 (let* ((elem (org-element-at-point))
21908 (end (org-element-property :end elem))
21909 (parent (org-element-property :parent elem)))
21910 (if (and parent (= (org-element-property :contents-end parent) end))
21911 (goto-char (org-element-property :end parent))
21912 (goto-char end))))))
21914 (defun org-backward-element ()
21915 "Move backward by one element.
21916 Move to the previous element at the same level, when possible."
21918 (cond ((bobp) (error "Cannot move further up"))
21919 ((org-with-limited-levels (org-at-heading-p))
21920 ;; At an headline, move to the previous one, if any, or stay
21922 (let ((origin (point)))
21923 (org-backward-heading-same-level 1)
21924 (unless (org-with-limited-levels (org-at-heading-p))
21926 (error "Cannot move further up"))))
21928 (let* ((trail (org-element-at-point 'keep-trail))
21930 (prev-elem (nth 1 trail))
21931 (beg (org-element-property :begin elem)))
21933 ;; Move to beginning of current element if point isn't
21935 ((/= (point) beg) (goto-char beg))
21936 (prev-elem (goto-char (org-element-property :begin prev-elem)))
21937 ((org-before-first-heading-p) (goto-char (point-min)))
21938 (t (org-back-to-heading)))))))
21940 (defun org-up-element ()
21941 "Move to upper element."
21943 (if (org-with-limited-levels (org-at-heading-p))
21944 (unless (org-up-heading-safe) (error "No surrounding element"))
21945 (let* ((elem (org-element-at-point))
21946 (parent (org-element-property :parent elem)))
21947 (if parent (goto-char (org-element-property :begin parent))
21948 (if (org-with-limited-levels (org-before-first-heading-p))
21949 (error "No surrounding element")
21950 (org-with-limited-levels (org-back-to-heading)))))))
21952 (defvar org-element-greater-elements)
21953 (defun org-down-element ()
21954 "Move to inner element."
21956 (let ((element (org-element-at-point)))
21958 ((memq (org-element-type element) '(plain-list table))
21959 (goto-char (org-element-property :contents-begin element))
21961 ((memq (org-element-type element) org-element-greater-elements)
21962 ;; If contents are hidden, first disclose them.
21963 (when (org-element-property :hiddenp element) (org-cycle))
21964 (goto-char (or (org-element-property :contents-begin element)
21965 (error "No content for this element"))))
21966 (t (error "No inner element")))))
21968 (defun org-drag-element-backward ()
21969 "Move backward element at point."
21971 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
21972 (let* ((trail (org-element-at-point 'keep-trail))
21974 (prev-elem (nth 1 trail)))
21975 ;; Error out if no previous element or previous element is
21976 ;; a parent of the current one.
21977 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
21978 (error "Cannot drag element backward")
21979 (let ((pos (point)))
21980 (org-element-swap-A-B prev-elem elem)
21981 (goto-char (+ (org-element-property :begin prev-elem)
21982 (- pos (org-element-property :begin elem)))))))))
21984 (defun org-drag-element-forward ()
21985 "Move forward element at point."
21987 (let* ((pos (point))
21988 (elem (org-element-at-point)))
21989 (when (= (point-max) (org-element-property :end elem))
21990 (error "Cannot drag element forward"))
21991 (goto-char (org-element-property :end elem))
21992 (let ((next-elem (org-element-at-point)))
21993 (when (or (org-element-nested-p elem next-elem)
21994 (and (eq (org-element-type next-elem) 'headline)
21995 (not (eq (org-element-type elem) 'headline))))
21997 (error "Cannot drag element forward"))
21998 ;; Compute new position of point: it's shifted by NEXT-ELEM
21999 ;; body's length (without final blanks) and by the length of
22000 ;; blanks between ELEM and NEXT-ELEM.
22001 (let ((size-next (- (save-excursion
22002 (goto-char (org-element-property :end next-elem))
22003 (skip-chars-backward " \r\t\n")
22005 ;; Small correction if buffer doesn't end
22006 ;; with a newline character.
22007 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
22008 (org-element-property :begin next-elem)))
22009 (size-blank (- (org-element-property :end elem)
22011 (goto-char (org-element-property :end elem))
22012 (skip-chars-backward " \r\t\n")
22015 (org-element-swap-A-B elem next-elem)
22016 (goto-char (+ pos size-next size-blank))))))
22018 (defun org-mark-element ()
22019 "Put point at beginning of this element, mark at end.
22021 Interactively, if this command is repeated or (in Transient Mark
22022 mode) if the mark is active, it marks the next element after the
22023 ones already marked."
22025 (let (deactivate-mark)
22026 (if (and (org-called-interactively-p 'any)
22027 (or (and (eq last-command this-command) (mark t))
22028 (and transient-mark-mode mark-active)))
22032 (goto-char (org-element-property :end (org-element-at-point)))))
22033 (let ((element (org-element-at-point)))
22035 (push-mark (org-element-property :end element) t t)
22036 (goto-char (org-element-property :begin element))))))
22038 (defun org-narrow-to-element ()
22039 "Narrow buffer to current element."
22041 (let ((elem (org-element-at-point)))
22043 ((eq (car elem) 'headline)
22045 (org-element-property :begin elem)
22046 (org-element-property :end elem)))
22047 ((memq (car elem) org-element-greater-elements)
22049 (org-element-property :contents-begin elem)
22050 (org-element-property :contents-end elem)))
22053 (org-element-property :begin elem)
22054 (org-element-property :end elem))))))
22056 (defun org-transpose-element ()
22057 "Transpose current and previous elements, keeping blank lines between.
22058 Point is moved after both elements."
22060 (org-skip-whitespace)
22061 (let ((end (org-element-property :end (org-element-at-point))))
22062 (org-drag-element-backward)
22065 (defun org-unindent-buffer ()
22066 "Un-indent the visible part of the buffer.
22067 Relative indentation (between items, inside blocks, etc.) isn't
22070 (unless (eq major-mode 'org-mode)
22071 (error "Cannot un-indent a buffer not in Org mode"))
22072 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
22073 unindent-tree ; For byte-compiler.
22079 (if (memq (org-element-type element) '(headline section))
22080 (funcall unindent-tree (org-element-contents element))
22084 (org-element-property :begin element)
22085 (org-element-property :end element))
22086 (org-do-remove-indentation)))))
22087 (reverse contents))))))
22088 (funcall unindent-tree (org-element-contents parse-tree))))
22090 (defun org-show-subtree ()
22091 "Show everything after this heading at deeper levels."
22093 (outline-flag-region
22096 (org-end-of-subtree t t))
22099 (defun org-show-entry ()
22100 "Show the body directly following this heading.
22101 Show the heading too, if it is currently invisible."
22104 (condition-case nil
22106 (org-back-to-heading t)
22107 (outline-flag-region
22108 (max (point-min) (1- (point)))
22110 (if (re-search-forward
22111 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
22112 (match-beginning 1)
22115 (org-cycle-hide-drawers 'children))
22118 (defun org-make-options-regexp (kwds &optional extra)
22119 "Make a regular expression for keyword lines."
22122 (mapconcat 'regexp-quote kwds "\\|")
22123 (if extra (concat "\\|" extra))
22124 "\\):[ \t]*\\(.*\\)"))
22126 ;; Make isearch reveal the necessary context
22127 (defun org-isearch-end ()
22128 "Reveal context after isearch exits."
22129 (when isearch-success ; only if search was successful
22130 (if (featurep 'xemacs)
22131 ;; Under XEmacs, the hook is run in the correct place,
22132 ;; we directly show the context.
22133 (org-show-context 'isearch)
22134 ;; In Emacs the hook runs *before* restoring the overlays.
22135 ;; So we have to use a one-time post-command-hook to do this.
22136 ;; (Emacs 22 has a special variable, see function `org-mode')
22137 (unless (and (boundp 'isearch-mode-end-hook-quit)
22138 isearch-mode-end-hook-quit)
22139 ;; Only when the isearch was not quitted.
22140 (org-add-hook 'post-command-hook 'org-isearch-post-command
22141 'append 'local)))))
22143 (defun org-isearch-post-command ()
22144 "Remove self from hook, and show context."
22145 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
22146 (org-show-context 'isearch))
22149 ;;;; Integration with and fixes for other packages
22153 (defvar org-imenu-markers nil
22154 "All markers currently used by Imenu.")
22155 (make-variable-buffer-local 'org-imenu-markers)
22157 (defun org-imenu-new-marker (&optional pos)
22158 "Return a new marker for use by Imenu, and remember the marker."
22159 (let ((m (make-marker)))
22160 (move-marker m (or pos (point)))
22161 (push m org-imenu-markers)
22164 (defun org-imenu-get-tree ()
22165 "Produce the index for Imenu."
22166 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
22167 (setq org-imenu-markers nil)
22168 (let* ((n org-imenu-depth)
22169 (re (concat "^" (org-get-limited-outline-regexp)))
22170 (subs (make-vector (1+ n) nil))
22176 (goto-char (point-max))
22177 (while (re-search-backward re nil t)
22178 (setq level (org-reduced-level (funcall outline-level)))
22179 (when (and (<= level n)
22180 (looking-at org-complex-heading-regexp))
22181 (setq head (org-link-display-format
22182 (org-match-string-no-properties 4))
22183 m (org-imenu-new-marker))
22184 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
22185 (if (>= level last-level)
22186 (push (cons head m) (aref subs level))
22187 (push (cons head (aref subs (1+ level))) (aref subs level))
22188 (loop for i from (1+ level) to n do (aset subs i nil)))
22189 (setq last-level level)))))
22192 (eval-after-load "imenu"
22194 (add-hook 'imenu-after-jump-hook
22196 (if (derived-mode-p 'org-mode)
22197 (org-show-context 'org-goto))))))
22199 (defun org-link-display-format (link)
22200 "Replace a link with either the description, or the link target
22201 if no description is present"
22203 (if (string-match org-bracket-link-analytic-regexp link)
22204 (replace-match (if (match-end 5)
22205 (match-string 5 link)
22206 (concat (match-string 1 link)
22207 (match-string 3 link)))
22211 (defun org-toggle-link-display ()
22212 "Toggle the literal or descriptive display of links."
22214 (if org-descriptive-links
22215 (progn (org-remove-from-invisibility-spec '(org-link))
22216 (org-restart-font-lock)
22217 (setq org-descriptive-links nil))
22218 (progn (add-to-invisibility-spec '(org-link))
22219 (org-restart-font-lock)
22220 (setq org-descriptive-links t))))
22222 ;; Speedbar support
22224 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
22225 "Overlay marking the agenda restriction line in speedbar.")
22226 (overlay-put org-speedbar-restriction-lock-overlay
22227 'face 'org-agenda-restriction-lock)
22228 (overlay-put org-speedbar-restriction-lock-overlay
22229 'help-echo "Agendas are currently limited to this item.")
22230 (org-detach-overlay org-speedbar-restriction-lock-overlay)
22232 (defun org-speedbar-set-agenda-restriction ()
22233 "Restrict future agenda commands to the location at point in speedbar.
22234 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
22236 (require 'org-agenda)
22237 (let (p m tp np dir txt)
22239 ((setq p (text-property-any (point-at-bol) (point-at-eol)
22241 (setq m (get-text-property p 'org-imenu-marker))
22242 (with-current-buffer (marker-buffer m)
22244 (org-agenda-set-restriction-lock 'subtree)))
22245 ((setq p (text-property-any (point-at-bol) (point-at-eol)
22246 'speedbar-function 'speedbar-find-file))
22247 (setq tp (previous-single-property-change
22248 (1+ p) 'speedbar-function)
22249 np (next-single-property-change
22250 tp 'speedbar-function)
22251 dir (speedbar-line-directory)
22252 txt (buffer-substring-no-properties (or tp (point-min))
22253 (or np (point-max))))
22254 (with-current-buffer (find-file-noselect
22255 (let ((default-directory dir))
22256 (expand-file-name txt)))
22257 (unless (derived-mode-p 'org-mode)
22258 (error "Cannot restrict to non-Org-mode file"))
22259 (org-agenda-set-restriction-lock 'file)))
22260 (t (error "Don't know how to restrict Org-mode's agenda")))
22261 (move-overlay org-speedbar-restriction-lock-overlay
22262 (point-at-bol) (point-at-eol))
22263 (setq current-prefix-arg nil)
22264 (org-agenda-maybe-redo)))
22266 (eval-after-load "speedbar"
22268 (speedbar-add-supported-extension ".org")
22269 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
22270 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
22271 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
22272 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
22273 (add-hook 'speedbar-visiting-tag-hook
22274 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
22276 ;;; Fixes and Hacks for problems with other packages
22278 ;; Make flyspell not check words in links, to not mess up our keymap
22279 (defun org-mode-flyspell-verify ()
22280 "Don't let flyspell put overlays at active buttons, or on
22281 {todo,all-time,additional-option-like}-keywords."
22282 (let ((pos (max (1- (point)) (point-min)))
22283 (word (thing-at-point 'word)))
22284 (and (not (get-text-property pos 'keymap))
22285 (not (get-text-property pos 'org-no-flyspell))
22286 (not (member word org-todo-keywords-1))
22287 (not (member word org-all-time-keywords))
22288 (not (member word org-options-keywords))
22289 (not (member word (mapcar 'car org-startup-options)))
22290 (not (member word org-additional-option-like-keywords-for-flyspell)))))
22292 (defun org-remove-flyspell-overlays-in (beg end)
22293 "Remove flyspell overlays in region."
22294 (and (org-bound-and-true-p flyspell-mode)
22295 (fboundp 'flyspell-delete-region-overlays)
22296 (flyspell-delete-region-overlays beg end))
22297 (add-text-properties beg end '(org-no-flyspell t)))
22299 ;; Make `bookmark-jump' shows the jump location if it was hidden.
22300 (eval-after-load "bookmark"
22301 '(if (boundp 'bookmark-after-jump-hook)
22302 ;; We can use the hook
22303 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
22304 ;; Hook not available, use advice
22305 (defadvice bookmark-jump (after org-make-visible activate)
22306 "Make the position visible."
22307 (org-bookmark-jump-unhide))))
22309 ;; Make sure saveplace shows the location if it was hidden
22310 (eval-after-load "saveplace"
22311 '(defadvice save-place-find-file-hook (after org-make-visible activate)
22312 "Make the position visible."
22313 (org-bookmark-jump-unhide)))
22315 ;; Make sure ecb shows the location if it was hidden
22316 (eval-after-load "ecb"
22317 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
22318 "Make hierarchy visible when jumping into location from ECB tree buffer."
22319 (if (derived-mode-p 'org-mode)
22320 (org-show-context))))
22322 (defun org-bookmark-jump-unhide ()
22323 "Unhide the current position, to show the bookmark location."
22324 (and (derived-mode-p 'org-mode)
22325 (or (outline-invisible-p)
22326 (save-excursion (goto-char (max (point-min) (1- (point))))
22327 (outline-invisible-p)))
22328 (org-show-context 'bookmark-jump)))
22330 ;; Make session.el ignore our circular variable
22331 (eval-after-load "session"
22332 '(add-to-list 'session-globals-exclude 'org-mark-ring))
22334 ;;;; Experimental code
22336 (defun org-closed-in-range ()
22337 "Sparse tree of items closed in a certain time range.
22338 Still experimental, may disappear in the future."
22340 ;; Get the time interval from the user.
22341 (let* ((time1 (org-float-time
22342 (org-read-date nil 'to-time nil "Starting date: ")))
22343 (time2 (org-float-time
22344 (org-read-date nil 'to-time nil "End date:")))
22345 ;; callback function
22346 (callback (lambda ()
22349 (apply 'encode-time
22350 (org-parse-time-string
22351 (match-string 1))))))
22352 ;; check if time in interval
22353 (and (>= time time1) (<= time time2))))))
22354 ;; make tree, check each match with the callback
22355 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
22361 (run-hooks 'org-load-hook)
22363 ;;; org.el ends here