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 (let ((load-suffixes (list ".el")))
82 (load "org-loaddefs" 'noerror nil nil
'mustsuffix
))
84 ;; `org-outline-regexp' ought to be a defconst but is let-binding in
85 ;; some places -- e.g. see the macro org-with-limited-levels.
87 ;; In Org buffers, the value of `outline-regexp' is that of
88 ;; `org-outline-regexp'. The only function still directly relying on
89 ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
90 ;; job when `orgstruct-mode' is active.
91 (defvar org-outline-regexp
"\\*+ "
92 "Regexp to match Org headlines.")
94 (defvar org-outline-regexp-bol
"^\\*+ "
95 "Regexp to match Org headlines.
96 This is similar to `org-outline-regexp' but additionally makes
97 sure that we are at the beginning of the line.")
99 (defvar org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
100 "Matches an headline, putting stars and text into groups.
101 Stars are put in group 1 and the trimmed body in group 2.")
103 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
104 (when (fboundp 'defvaralias
)
105 (unless (boundp 'calendar-view-holidays-initially-flag
)
106 (defvaralias 'calendar-view-holidays-initially-flag
107 'view-calendar-holidays-initially
))
108 (unless (boundp 'calendar-view-diary-initially-flag
)
109 (defvaralias 'calendar-view-diary-initially-flag
110 'view-diary-entries-initially
))
111 (unless (boundp 'diary-fancy-buffer
)
112 (defvaralias 'diary-fancy-buffer
'fancy-diary-buffer
)))
114 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
115 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
116 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
117 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
118 (declare-function org-at-clock-log-p
"org-clock" ())
119 (declare-function org-clock-get-last-clock-out-time
"org-clock" ())
120 (declare-function org-clock-timestamps-up
"org-clock" (&optional n
))
121 (declare-function org-clock-timestamps-down
"org-clock" (&optional n
))
122 (declare-function org-clock-sum-current-item
"org-clock" (&optional tstart
))
124 (declare-function orgtbl-mode
"org-table" (&optional arg
))
125 (declare-function org-clock-out
"org-clock" (&optional switch-to-state fail-quietly at-time
))
126 (declare-function org-beamer-mode
"org-beamer" ())
127 (declare-function org-table-edit-field
"org-table" (arg))
128 (declare-function org-table-justify-field-maybe
"org-table" (&optional new
))
129 (declare-function org-id-get-create
"org-id" (&optional force
))
130 (declare-function org-id-find-id-file
"org-id" (id))
131 (declare-function org-tags-view
"org-agenda" (&optional todo-only match
))
132 (declare-function org-agenda-list
"org-agenda" (&optional arg start-day span
))
133 (declare-function org-table-align
"org-table" ())
134 (declare-function org-table-paste-rectangle
"org-table" ())
135 (declare-function org-table-maybe-eval-formula
"org-table" ())
136 (declare-function org-table-maybe-recalculate-line
"org-table" ())
138 (autoload 'org-element-at-point
"org-element")
139 (autoload 'org-element-type
"org-element")
141 (declare-function org-element-at-point
"org-element" (&optional keep-trail
))
142 (declare-function org-element-type
"org-element" (element))
143 (declare-function org-element-context
"org-element" ())
144 (declare-function org-element-contents
"org-element" (element))
145 (declare-function org-element-property
"org-element" (property element
))
146 (declare-function org-element-map
"org-element" (data types fun
&optional info first-match no-recursion
))
147 (declare-function org-element-nested-p
"org-element" (elem-a elem-b
))
148 (declare-function org-element-swap-A-B
"org-element" (elem-a elem-b
))
149 (declare-function org-element--parse-objects
"org-element" (beg end acc restriction
))
150 (declare-function org-element-parse-buffer
"org-element" (&optional granularity visible-only
))
152 ;; load languages based on value of `org-babel-load-languages'
153 (defvar org-babel-load-languages
)
156 (defun org-babel-do-load-languages (sym value
)
157 "Load the languages defined in `org-babel-load-languages'."
158 (set-default sym value
)
160 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
163 (require (intern (concat "ob-" lang
))))
165 (funcall 'fmakunbound
166 (intern (concat "org-babel-execute:" lang
)))
167 (funcall 'fmakunbound
168 (intern (concat "org-babel-expand-body:" lang
)))))))
169 org-babel-load-languages
))
171 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
172 "Languages which can be evaluated in Org-mode buffers.
173 This list can be used to load support for any of the languages
174 below, note that each language will depend on a different set of
175 system executables and/or Emacs modes. When a language is
176 \"loaded\", then code blocks in that language can be evaluated
177 with `org-babel-execute-src-block' bound by default to C-c
178 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
179 be set to remove code block evaluation from the C-c C-c
180 keybinding. By default only Emacs Lisp (which has no
181 requirements) is loaded."
183 :set
'org-babel-do-load-languages
185 :type
'(alist :tag
"Babel Languages"
188 (const :tag
"Awk" awk
)
191 (const :tag
"Asymptote" asymptote
)
192 (const :tag
"Calc" calc
)
193 (const :tag
"Clojure" clojure
)
194 (const :tag
"CSS" css
)
195 (const :tag
"Ditaa" ditaa
)
196 (const :tag
"Dot" dot
)
197 (const :tag
"Emacs Lisp" emacs-lisp
)
198 (const :tag
"Fortran" fortran
)
199 (const :tag
"Gnuplot" gnuplot
)
200 (const :tag
"Haskell" haskell
)
202 (const :tag
"Java" java
)
203 (const :tag
"Javascript" js
)
204 (const :tag
"LaTeX" latex
)
205 (const :tag
"Ledger" ledger
)
206 (const :tag
"Lilypond" lilypond
)
207 (const :tag
"Lisp" lisp
)
208 (const :tag
"Makefile" makefile
)
209 (const :tag
"Maxima" maxima
)
210 (const :tag
"Matlab" matlab
)
211 (const :tag
"Mscgen" mscgen
)
212 (const :tag
"Ocaml" ocaml
)
213 (const :tag
"Octave" octave
)
214 (const :tag
"Org" org
)
215 (const :tag
"Perl" perl
)
216 (const :tag
"Pico Lisp" picolisp
)
217 (const :tag
"PlantUML" plantuml
)
218 (const :tag
"Python" python
)
219 (const :tag
"Ruby" ruby
)
220 (const :tag
"Sass" sass
)
221 (const :tag
"Scala" scala
)
222 (const :tag
"Scheme" scheme
)
223 (const :tag
"Screen" screen
)
224 (const :tag
"Shell Script" sh
)
225 (const :tag
"Shen" shen
)
226 (const :tag
"Sql" sql
)
227 (const :tag
"Sqlite" sqlite
))
228 :value-type
(boolean :tag
"Activate" :value t
)))
230 ;;;; Customization variables
231 (defcustom org-clone-delete-id nil
232 "Remove ID property of clones of a subtree.
233 When non-nil, clones of a subtree don't inherit the ID property.
234 Otherwise they inherit the ID property with a new unique
241 (require 'org-compat
)
245 (defun org-version (&optional here full message
)
246 "Show the org-mode version in the echo area.
247 With prefix argument HERE, insert it at point.
248 When FULL is non-nil, use a verbose version string.
249 When MESSAGE is non-nil, display a message with the version."
251 (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
252 (save-load-suffixes load-suffixes
)
253 (load-suffixes (list ".el"))
254 (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
256 (and (fboundp 'org-release
) (fboundp 'org-git-version
))
257 (load (concat org-dir
"org-version")
258 'noerror
'nomessage nil
'mustsuffix
)))
259 (load-suffixes save-load-suffixes
)
260 (org-version (org-release))
261 (git-version (org-git-version))
262 (version (format "Org-mode version %s (%s @ %s)"
266 (if (string= org-dir org-install-dir
)
268 (concat "mixed installation! " org-install-dir
" and " org-dir
))
269 "org-loaddefs.el can not be found!")))
270 (_version (if full version org-version
)))
271 (if (org-called-interactively-p 'interactive
)
275 (if message
(message _version
))
278 (defconst org-version
(org-version))
280 ;;; Compatibility constants
282 ;;; The custom variables
285 "Outline-based notes management and organizer."
290 (defcustom org-mode-hook nil
291 "Mode hook for Org-mode, run after the mode was turned on."
295 (defcustom org-load-hook nil
296 "Hook that is run after org.el has been loaded."
300 (defcustom org-log-buffer-setup-hook nil
301 "Hook that is run after an Org log buffer is created."
306 (defvar org-modules
) ; defined below
307 (defvar org-modules-loaded nil
308 "Have the modules been loaded already?")
310 (defun org-load-modules-maybe (&optional force
)
311 "Load all extensions listed in `org-modules'."
312 (when (or force
(not org-modules-loaded
))
314 (condition-case nil
(require ext
)
315 (error (message "Problems while trying to load feature `%s'" ext
))))
317 (setq org-modules-loaded t
)))
319 (defun org-set-modules (var value
)
320 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
322 (when (featurep 'org
)
323 (org-load-modules-maybe 'force
)))
325 (when (org-bound-and-true-p org-modules
)
326 (let ((a (member 'org-infojs org-modules
)))
327 (and a
(setcar a
'org-jsinfo
))))
329 (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
)
330 "Modules that should always be loaded together with org.el.
331 If a description starts with <C>, the file is not part of Emacs
332 and loading it will require that you have downloaded and properly installed
333 the org-mode distribution.
335 You can also use this system to load external packages (i.e. neither Org
336 core modules, nor modules from the CONTRIB directory). Just add symbols
337 to the end of the list. If the package is called org-xyz.el, then you need
338 to add the symbol `xyz', and the package must have a call to
342 :set
'org-set-modules
345 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
346 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
347 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
348 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
349 (const :tag
" docview: Links to doc-view buffers" org-docview
)
350 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
351 (const :tag
" id: Global IDs for identifying entries" org-id
)
352 (const :tag
" info: Links to Info nodes" org-info
)
353 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
354 (const :tag
" habit: Track your consistency with habits" org-habit
)
355 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
356 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
357 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
358 (const :tag
" mew Links to Mew folders/messages" org-mew
)
359 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
360 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
361 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
362 (const :tag
" special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks
)
363 (const :tag
" vm: Links to VM folders/messages" org-vm
)
364 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
365 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
366 (const :tag
" mouse: Additional mouse support" org-mouse
)
367 (const :tag
" TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler
)
369 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
370 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
371 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
372 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
373 (const :tag
"C collector: Collect properties into tables" org-collector
)
374 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
375 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
376 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
377 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
378 (const :tag
"C eval: Include command output as text" org-eval
)
379 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
380 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
381 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
382 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
383 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
385 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
387 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
388 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
389 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
390 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
391 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
392 (const :tag
"C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber
)
393 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
394 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
395 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
396 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
397 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
398 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
399 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
400 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
401 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
402 (const :tag
"C track: Keep up with Org-mode development" org-track
)
403 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
404 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
405 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
407 (defcustom org-support-shift-select nil
408 "Non-nil means make shift-cursor commands select text when possible.
410 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
411 start selecting a region, or enlarge regions started in this way.
412 In Org-mode, in special contexts, these same keys are used for
413 other purposes, important enough to compete with shift selection.
414 Org tries to balance these needs by supporting `shift-select-mode'
415 outside these special contexts, under control of this variable.
417 The default of this variable is nil, to avoid confusing behavior. Shifted
418 cursor keys will then execute Org commands in the following contexts:
419 - on a headline, changing TODO state (left/right) and priority (up/down)
420 - on a time stamp, changing the time
421 - in a plain list item, changing the bullet type
422 - in a property definition line, switching between allowed values
423 - in the BEGIN line of a clock table (changing the time block).
424 Outside these contexts, the commands will throw an error.
426 When this variable is t and the cursor is not in a special
427 context, Org-mode will support shift-selection for making and
428 enlarging regions. To make this more effective, the bullet
429 cycling will no longer happen anywhere in an item line, but only
430 if the cursor is exactly on the bullet.
432 If you set this variable to the symbol `always', then the keys
433 will not be special in headlines, property lines, and item lines,
434 to make shift selection work there as well. If this is what you
435 want, you can use the following alternative commands: `C-c C-t'
436 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
437 can be used to switch TODO sets, `C-c -' to cycle item bullet
438 types, and properties can be edited by hand or in column view.
440 However, when the cursor is on a timestamp, shift-cursor commands
441 will still edit the time stamp - this is just too good to give up.
443 XEmacs user should have this variable set to nil, because
444 `shift-select-mode' is in Emacs 23 or later only."
447 (const :tag
"Never" nil
)
448 (const :tag
"When outside special context" t
)
449 (const :tag
"Everywhere except timestamps" always
)))
451 (defcustom org-loop-over-headlines-in-active-region nil
452 "Shall some commands act upon headlines in the active region?
454 When set to `t', some commands will be performed in all headlines
455 within the active region.
457 When set to `start-level', some commands will be performed in all
458 headlines within the active region, provided that these headlines
459 are of the same level than the first one.
461 When set to a string, those commands will be performed on the
462 matching headlines within the active region. Such string must be
463 a tags/property/todo match as it is used in the agenda tags view.
465 The list of commands is: `org-schedule', `org-deadline',
466 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
467 `org-archive-to-archive-sibling'. The archiving commands skip
468 already archived entries."
469 :type
'(choice (const :tag
"Don't loop" nil
)
470 (const :tag
"All headlines in active region" t
)
471 (const :tag
"In active region, headlines at the same level than the first one" 'start-level
)
472 (string :tag
"Tags/Property/Todo matcher"))
477 (defgroup org-startup nil
478 "Options concerning startup of Org-mode."
482 (defcustom org-startup-folded t
483 "Non-nil means entering Org-mode will switch to OVERVIEW.
484 This can also be configured on a per-file basis by adding one of
485 the following lines anywhere in the buffer:
487 #+STARTUP: fold (or `overview', this is equivalent)
488 #+STARTUP: nofold (or `showall', this is equivalent)
490 #+STARTUP: showeverything"
493 (const :tag
"nofold: show all" nil
)
494 (const :tag
"fold: overview" t
)
495 (const :tag
"content: all headlines" content
)
496 (const :tag
"show everything, even drawers" showeverything
)))
498 (defcustom org-startup-truncated t
499 "Non-nil means entering Org-mode will set `truncate-lines'.
500 This is useful since some lines containing links can be very long and
501 uninteresting. Also tables look terrible when wrapped."
505 (defcustom org-startup-indented nil
506 "Non-nil means turn on `org-indent-mode' on startup.
507 This can also be configured on a per-file basis by adding one of
508 the following lines anywhere in the buffer:
512 :group
'org-structure
514 (const :tag
"Not" nil
)
515 (const :tag
"Globally (slow on startup in large files)" t
)))
517 (defcustom org-use-sub-superscripts t
518 "Non-nil means interpret \"_\" and \"^\" for export.
519 When this option is turned on, you can use TeX-like syntax for sub- and
520 superscripts. Several characters after \"_\" or \"^\" will be
521 considered as a single item - so grouping with {} is normally not
522 needed. For example, the following things will be parsed as single
523 sub- or superscripts.
525 10^24 or 10^tau several digits will be considered 1 item.
526 10^-12 or 10^-tau a leading sign with digits or a word
527 x^2-y^3 will be read as x^2 - y^3, because items are
528 terminated by almost any nonword/nondigit char.
529 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
531 Still, ambiguity is possible - so when in doubt use {} to enclose the
532 sub/superscript. If you set this variable to the symbol `{}',
533 the braces are *required* in order to trigger interpretations as
534 sub/superscript. This can be helpful in documents that need \"_\"
535 frequently in plain text.
537 Not all export backends support this, but HTML does.
539 This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
541 :group
'org-export-translation
544 (const :tag
"Always interpret" t
)
545 (const :tag
"Only with braces" {})
546 (const :tag
"Never interpret" nil
)))
548 (if (fboundp 'defvaralias
)
549 (defvaralias 'org-export-with-sub-superscripts
'org-use-sub-superscripts
))
552 (defcustom org-startup-with-beamer-mode nil
553 "Non-nil means turn on `org-beamer-mode' on startup.
554 This can also be configured on a per-file basis by adding one of
555 the following lines anywhere in the buffer:
562 (defcustom org-startup-align-all-tables nil
563 "Non-nil means align all tables when visiting a file.
564 This is useful when the column width in tables is forced with <N> cookies
565 in table fields. Such tables will look correct only after the first re-align.
566 This can also be configured on a per-file basis by adding one of
567 the following lines anywhere in the buffer:
573 (defcustom org-startup-with-inline-images nil
574 "Non-nil means show inline images when loading a new Org file.
575 This can also be configured on a per-file basis by adding one of
576 the following lines anywhere in the buffer:
577 #+STARTUP: inlineimages
578 #+STARTUP: noinlineimages"
583 (defcustom org-insert-mode-line-in-empty-file nil
584 "Non-nil means insert the first line setting Org-mode in empty files.
585 When the function `org-mode' is called interactively in an empty file, this
586 normally means that the file name does not automatically trigger Org-mode.
587 To ensure that the file will always be in Org-mode in the future, a
588 line enforcing Org-mode will be inserted into the buffer, if this option
593 (defcustom org-replace-disputed-keys nil
594 "Non-nil means use alternative key bindings for some keys.
595 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
596 These keys are also used by other packages like shift-selection-mode'
597 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
598 If you want to use Org-mode together with one of these other modes,
599 or more generally if you would like to move some Org-mode commands to
600 other keys, set this variable and configure the keys with the variable
603 This option is only relevant at load-time of Org-mode, and must be set
604 *before* org.el is loaded. Changing it requires a restart of Emacs to
609 (defcustom org-use-extra-keys nil
610 "Non-nil means use extra key sequence definitions for certain commands.
611 This happens automatically if you run XEmacs or if `window-system'
612 is nil. This variable lets you do the same manually. You must
613 set it before loading org.
615 Example: on Carbon Emacs 22 running graphically, with an external
616 keyboard on a Powerbook, the default way of setting M-left might
617 not work for either Alt or ESC. Setting this variable will make
622 (if (fboundp 'defvaralias
)
623 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
625 (defcustom org-disputed-keys
626 '(([(shift up
)] .
[(meta p
)])
627 ([(shift down
)] .
[(meta n
)])
628 ([(shift left
)] .
[(meta -
)])
629 ([(shift right
)] .
[(meta +)])
630 ([(control shift right
)] .
[(meta shift
+)])
631 ([(control shift left
)] .
[(meta shift -
)]))
632 "Keys for which Org-mode and other modes compete.
633 This is an alist, cars are the default keys, second element specifies
634 the alternative to use when `org-replace-disputed-keys' is t.
636 Keys can be specified in any syntax supported by `define-key'.
637 The value of this option takes effect only at Org-mode's startup,
638 therefore you'll have to restart Emacs to apply it after changing."
643 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
644 Or return the original if not disputed.
645 Also apply the translations defined in `org-xemacs-key-equivalents'."
646 (when org-replace-disputed-keys
647 (let* ((nkey (key-description key
))
648 (x (org-find-if (lambda (x)
649 (equal (key-description (car x
)) nkey
))
651 (setq key
(if x
(cdr x
) key
))))
652 (when (featurep 'xemacs
)
653 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
656 (defun org-find-if (predicate seq
)
659 (if (funcall predicate
(car seq
))
660 (throw 'exit
(car seq
))
663 (defun org-defkey (keymap key def
)
664 "Define a key, possibly translated, as returned by `org-key'."
665 (define-key keymap
(org-key key
) def
))
667 (defcustom org-ellipsis nil
668 "The ellipsis to use in the Org-mode outline.
669 When nil, just use the standard three dots. When a string, use that instead,
670 When a face, use the standard 3 dots, but with the specified face.
671 The change affects only Org-mode (which will then use its own display table).
672 Changing this requires executing `M-x org-mode' in a buffer to become
675 :type
'(choice (const :tag
"Default" nil
)
676 (face :tag
"Face" :value org-warning
)
677 (string :tag
"String" :value
"...#")))
679 (defvar org-display-table nil
680 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
682 (defgroup org-keywords nil
683 "Keywords in Org-mode."
687 (defcustom org-deadline-string
"DEADLINE:"
688 "String to mark deadline entries.
689 A deadline is this string, followed by a time stamp. Should be a word,
690 terminated by a colon. You can insert a schedule keyword and
691 a timestamp with \\[org-deadline].
692 Changes become only effective after restarting Emacs."
696 (defcustom org-scheduled-string
"SCHEDULED:"
697 "String to mark scheduled TODO entries.
698 A schedule is this string, followed by a time stamp. Should be a word,
699 terminated by a colon. You can insert a schedule keyword and
700 a timestamp with \\[org-schedule].
701 Changes become only effective after restarting Emacs."
705 (defcustom org-closed-string
"CLOSED:"
706 "String used as the prefix for timestamps logging closing a TODO entry."
710 (defcustom org-clock-string
"CLOCK:"
711 "String used as prefix for timestamps clocking work hours on an item."
715 (defconst org-planning-or-clock-line-re
(concat "^[ \t]*\\("
716 org-scheduled-string
"\\|"
717 org-deadline-string
"\\|"
718 org-closed-string
"\\|"
719 org-clock-string
"\\)")
720 "Matches a line with planning or clock info.")
722 (defcustom org-comment-string
"COMMENT"
723 "Entries starting with this keyword will never be exported.
724 An entry can be toggled between COMMENT and normal with
725 \\[org-toggle-comment].
726 Changes become only effective after restarting Emacs."
730 (defcustom org-quote-string
"QUOTE"
731 "Entries starting with this keyword will be exported in fixed-width font.
732 Quoting applies only to the text in the entry following the headline, and does
733 not extend beyond the next headline, even if that is lower level.
734 An entry can be toggled between QUOTE and normal with
735 \\[org-toggle-fixed-width-section]."
739 (defconst org-repeat-re
740 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
741 "Regular expression for specifying repeated events.
742 After a match, group 1 contains the repeat expression.")
744 (defgroup org-structure nil
745 "Options concerning the general structure of Org-mode files."
749 (defgroup org-reveal-location nil
750 "Options about how to make context of a location visible."
751 :tag
"Org Reveal Location"
752 :group
'org-structure
)
754 (defconst org-context-choice
756 (const :tag
"Always" t
)
757 (const :tag
"Never" nil
)
758 (repeat :greedy t
:tag
"Individual contexts"
760 (choice :tag
"Context"
767 (const bookmark-jump
)
771 "Contexts for the reveal options.")
773 (defcustom org-show-hierarchy-above
'((default . t
))
774 "Non-nil means show full hierarchy 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 hierarchy of headings
777 above the exposed location is shown.
778 Turning this off for example for sparse trees makes them very compact.
779 Instead of t, this can also be an alist specifying this option for different
780 contexts. Valid contexts are
781 agenda when exposing an entry from the agenda
782 org-goto when using the command `org-goto' on key C-c C-j
783 occur-tree when using the command `org-occur' on key C-c /
784 tags-tree when constructing a sparse tree based on tags matches
785 link-search when exposing search matches associated with a link
786 mark-goto when exposing the jump goal of a mark
787 bookmark-jump when exposing a bookmark location
788 isearch when exiting from an incremental search
789 default default for all contexts not set explicitly"
790 :group
'org-reveal-location
791 :type org-context-choice
)
793 (defcustom org-show-following-heading
'((default . nil
))
794 "Non-nil means show following heading when revealing a location.
795 Org-mode often shows locations in an org-mode file which might have
796 been invisible before. When this is set, the heading following the
798 Turning this off for example for sparse trees makes them very compact,
799 but makes it harder to edit the location of the match. In such a case,
800 use the command \\[org-reveal] to show more context.
801 Instead of t, this can also be an alist specifying this option for different
802 contexts. See `org-show-hierarchy-above' for valid contexts."
803 :group
'org-reveal-location
804 :type org-context-choice
)
806 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
807 "Non-nil means show all sibling heading when revealing a location.
808 Org-mode often shows locations in an org-mode file which might have
809 been invisible before. When this is set, the sibling of the current entry
810 heading are all made visible. If `org-show-hierarchy-above' is t,
811 the same happens on each level of the hierarchy above the current entry.
813 By default this is on for the isearch context, off for all other contexts.
814 Turning this off for example for sparse trees makes them very compact,
815 but makes it harder to edit the location of the match. In such a case,
816 use the command \\[org-reveal] to show more context.
817 Instead of t, this can also be an alist specifying this option for different
818 contexts. See `org-show-hierarchy-above' for valid contexts."
819 :group
'org-reveal-location
820 :type org-context-choice
)
822 (defcustom org-show-entry-below
'((default . nil
))
823 "Non-nil means show the entry below a headline when revealing a location.
824 Org-mode often shows locations in an org-mode file which might have
825 been invisible before. When this is set, the text below the headline that is
826 exposed is also shown.
828 By default this is off for all contexts.
829 Instead of t, this can also be an alist specifying this option for different
830 contexts. See `org-show-hierarchy-above' for valid contexts."
831 :group
'org-reveal-location
832 :type org-context-choice
)
834 (defcustom org-indirect-buffer-display
'other-window
835 "How should indirect tree buffers be displayed?
836 This applies to indirect buffers created with the commands
837 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
839 current-window Display in the current window
840 other-window Just display in another window.
841 dedicated-frame Create one new frame, and re-use it each time.
842 new-frame Make a new frame each time. Note that in this case
843 previously-made indirect buffers are kept, and you need to
844 kill these buffers yourself."
845 :group
'org-structure
846 :group
'org-agenda-windows
848 (const :tag
"In current window" current-window
)
849 (const :tag
"In current frame, other window" other-window
)
850 (const :tag
"Each time a new frame" new-frame
)
851 (const :tag
"One dedicated frame" dedicated-frame
)))
853 (defcustom org-use-speed-commands nil
854 "Non-nil means activate single letter commands at beginning of a headline.
855 This may also be a function to test for appropriate locations where speed
856 commands should be active."
857 :group
'org-structure
859 (const :tag
"Never" nil
)
860 (const :tag
"At beginning of headline stars" t
)
863 (defcustom org-speed-commands-user nil
864 "Alist of additional speed commands.
865 This list will be checked before `org-speed-commands-default'
866 when the variable `org-use-speed-commands' is non-nil
867 and when the cursor is at the beginning of a headline.
868 The car if each entry is a string with a single letter, which must
869 be assigned to `self-insert-command' in the global map.
870 The cdr is either a command to be called interactively, a function
871 to be called, or a form to be evaluated.
872 An entry that is just a list with a single string will be interpreted
873 as a descriptive headline that will be added when listing the speed
874 commands in the Help buffer using the `?' speed command."
875 :group
'org-structure
876 :type
'(repeat :value
("k" . ignore
)
877 (choice :value
("k" . ignore
)
878 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
879 (cons :tag
"Letter and Command"
880 (string :tag
"Command letter")
885 (defgroup org-cycle nil
886 "Options concerning visibility cycling in Org-mode."
888 :group
'org-structure
)
890 (defcustom org-cycle-skip-children-state-if-no-children t
891 "Non-nil means skip CHILDREN state in entries that don't have any."
895 (defcustom org-cycle-max-level nil
896 "Maximum level which should still be subject to visibility cycling.
897 Levels higher than this will, for cycling, be treated as text, not a headline.
898 When `org-odd-levels-only' is set, a value of N in this variable actually
899 means 2N-1 stars as the limiting headline.
900 When nil, cycle all levels.
901 Note that the limiting level of cycling is also influenced by
902 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
903 `org-inlinetask-min-level' is, cycling will be limited to levels one less
907 (const :tag
"No limit" nil
)
908 (integer :tag
"Maximum level")))
910 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
911 "Names of drawers. Drawers are not opened by cycling on the headline above.
912 Drawers only open with a TAB on the drawer line itself. A drawer looks like
917 The drawer \"PROPERTIES\" is special for capturing properties through
920 Drawers can be defined on the per-file basis with a line like:
922 #+DRAWERS: HIDDEN STATE PROPERTIES"
923 :group
'org-structure
925 :type
'(repeat (string :tag
"Drawer Name")))
927 (defcustom org-hide-block-startup nil
928 "Non-nil means entering Org-mode will fold all blocks.
929 This can also be set in on a per-file basis with
931 #+STARTUP: hideblocks
932 #+STARTUP: showblocks"
937 (defcustom org-cycle-global-at-bob nil
938 "Cycle globally if cursor is at beginning of buffer and not at a headline.
939 This makes it possible to do global cycling without having to use S-TAB or
940 \\[universal-argument] TAB. For this special case to work, the first line
941 of the buffer must not be a headline -- it may be empty or some other text.
942 When used in this way, `org-cycle-hook' is disabled temporarily to make
943 sure the cursor stays at the beginning of the buffer. When this option is
944 nil, don't do anything special at the beginning of the buffer."
948 (defcustom org-cycle-level-after-item
/entry-creation t
949 "Non-nil means cycle entry level or item indentation in new empty entries.
951 When the cursor is at the end of an empty headline, i.e with only stars
952 and maybe a TODO keyword, TAB will then switch the entry to become a child,
953 and then all possible ancestor states, before returning to the original state.
954 This makes data entry extremely fast: M-RET to create a new headline,
955 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
957 When the cursor is at the end of an empty plain list item, one TAB will
958 make it a subitem, two or more tabs will back up to make this an item
959 higher up in the item hierarchy."
963 (defcustom org-cycle-emulate-tab t
964 "Where should `org-cycle' emulate TAB.
966 white Only in completely white lines
967 whitestart Only at the beginning of lines, before the first non-white char
968 t Everywhere except in headlines
969 exc-hl-bol Everywhere except at the start of a headline
970 If TAB is used in a place where it does not emulate TAB, the current subtree
971 visibility is cycled."
973 :type
'(choice (const :tag
"Never" nil
)
974 (const :tag
"Only in completely white lines" white
)
975 (const :tag
"Before first char in a line" whitestart
)
976 (const :tag
"Everywhere except in headlines" t
)
977 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
980 (defcustom org-cycle-separator-lines
2
981 "Number of empty lines needed to keep an empty line between collapsed trees.
982 If you leave an empty line between the end of a subtree and the following
983 headline, this empty line is hidden when the subtree is folded.
984 Org-mode will leave (exactly) one empty line visible if the number of
985 empty lines is equal or larger to the number given in this variable.
986 So the default 2 means at least 2 empty lines after the end of a subtree
987 are needed to produce free space between a collapsed subtree and the
990 If the number is negative, and the number of empty lines is at least -N,
991 all empty lines are shown.
993 Special case: when 0, never leave empty lines in collapsed view."
996 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
998 (defcustom org-pre-cycle-hook nil
999 "Hook that is run before visibility cycling is happening.
1000 The function(s) in this hook must accept a single argument which indicates
1001 the new state that will be set right after running this hook. The
1002 argument is a symbol. Before a global state change, it can have the values
1003 `overview', `content', or `all'. Before a local state change, it can have
1004 the values `folded', `children', or `subtree'."
1008 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
1009 org-cycle-hide-drawers
1010 org-cycle-hide-inline-tasks
1011 org-cycle-show-empty-lines
1012 org-optimize-window-after-visibility-change
)
1013 "Hook that is run after `org-cycle' has changed the buffer visibility.
1014 The function(s) in this hook must accept a single argument which indicates
1015 the new state that was set by the most recent `org-cycle' command. The
1016 argument is a symbol. After a global state change, it can have the values
1017 `overview', `contents', or `all'. After a local state change, it can have
1018 the values `folded', `children', or `subtree'."
1022 (defgroup org-edit-structure nil
1023 "Options concerning structure editing in Org-mode."
1024 :tag
"Org Edit Structure"
1025 :group
'org-structure
)
1027 (defcustom org-odd-levels-only nil
1028 "Non-nil means skip even levels and only use odd levels for the outline.
1029 This has the effect that two stars are being added/taken away in
1030 promotion/demotion commands. It also influences how levels are
1031 handled by the exporters.
1032 Changing it requires restart of `font-lock-mode' to become effective
1033 for fontification also in regions already fontified.
1034 You may also set this on a per-file basis by adding one of the following
1035 lines to the buffer:
1039 :group
'org-edit-structure
1040 :group
'org-appearance
1043 (defcustom org-adapt-indentation t
1044 "Non-nil means adapt indentation to outline node level.
1046 When this variable is set, Org assumes that you write outlines by
1047 indenting text in each node to align with the headline (after the stars).
1048 The following issues are influenced by this variable:
1050 - When this is set and the *entire* text in an entry is indented, the
1051 indentation is increased by one space in a demotion command, and
1052 decreased by one in a promotion command. If any line in the entry
1053 body starts with text at column 0, indentation is not changed at all.
1055 - Property drawers and planning information is inserted indented when
1056 this variable s set. When nil, they will not be indented.
1058 - TAB indents a line relative to context. The lines below a headline
1059 will be indented when this variable is set.
1061 Note that this is all about true indentation, by adding and removing
1062 space characters. See also `org-indent.el' which does level-dependent
1063 indentation in a virtual way, i.e. at display time in Emacs."
1064 :group
'org-edit-structure
1067 (defcustom org-special-ctrl-a
/e nil
1068 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1070 When t, `C-a' will bring back the cursor to the beginning of the
1071 headline text, i.e. after the stars and after a possible TODO
1072 keyword. In an item, this will be the position after bullet and
1073 check-box, if any. When the cursor is already at that position,
1074 another `C-a' will bring it to the beginning of the line.
1076 `C-e' will jump to the end of the headline, ignoring the presence
1077 of tags in the headline. A second `C-e' will then jump to the
1078 true end of the line, after any tags. This also means that, when
1079 this variable is non-nil, `C-e' also will never jump beyond the
1080 end of the heading of a folded section, i.e. not after the
1083 When set to the symbol `reversed', the first `C-a' or `C-e' works
1084 normally, going to the true line boundary first. Only a directly
1085 following, identical keypress will bring the cursor to the
1088 This may also be a cons cell where the behavior for `C-a' and
1089 `C-e' is set separately."
1090 :group
'org-edit-structure
1092 (const :tag
"off" nil
)
1093 (const :tag
"on: after stars/bullet and before tags first" t
)
1094 (const :tag
"reversed: true line boundary first" reversed
)
1095 (cons :tag
"Set C-a and C-e separately"
1096 (choice :tag
"Special C-a"
1097 (const :tag
"off" nil
)
1098 (const :tag
"on: after stars/bullet first" t
)
1099 (const :tag
"reversed: before stars/bullet first" reversed
))
1100 (choice :tag
"Special C-e"
1101 (const :tag
"off" nil
)
1102 (const :tag
"on: before tags first" t
)
1103 (const :tag
"reversed: after tags first" reversed
)))))
1104 (if (fboundp 'defvaralias
)
1105 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
1107 (defcustom org-special-ctrl-k nil
1108 "Non-nil means `C-k' will behave specially in headlines.
1109 When nil, `C-k' will call the default `kill-line' command.
1110 When t, the following will happen while the cursor is in the headline:
1112 - When the cursor is at the beginning of a headline, kill the entire
1113 line and possible the folded subtree below the line.
1114 - When in the middle of the headline text, kill the headline up to the tags.
1115 - When after the headline text, kill the tags."
1116 :group
'org-edit-structure
1119 (defcustom org-ctrl-k-protect-subtree nil
1120 "Non-nil means, do not delete a hidden subtree with C-k.
1121 When set to the symbol `error', simply throw an error when C-k is
1122 used to kill (part-of) a headline that has hidden text behind it.
1123 Any other non-nil value will result in a query to the user, if it is
1124 OK to kill that hidden subtree. When nil, kill without remorse."
1125 :group
'org-edit-structure
1128 (const :tag
"Do not protect hidden subtrees" nil
)
1129 (const :tag
"Protect hidden subtrees with a security query" t
)
1130 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1132 (defcustom org-catch-invisible-edits nil
1133 "Check if in invisible region before inserting or deleting a character.
1136 nil Do not check, so just do invisible edits.
1137 error Throw an error and do nothing.
1138 show Make point visible, and do the requested edit.
1139 show-and-error Make point visible, then throw an error and abort the edit.
1140 smart Make point visible, and do insertion/deletion if it is
1141 adjacent to visible text and the change feels predictable.
1142 Never delete a previously invisible character or add in the
1143 middle or right after an invisible region. Basically, this
1144 allows insertion and backward-delete right before ellipses.
1145 FIXME: maybe in this case we should not even show?"
1146 :group
'org-edit-structure
1149 (const :tag
"Do not check" nil
)
1150 (const :tag
"Throw error when trying to edit" error
)
1151 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1152 (const :tag
"Show invisible part and do the edit" show
)
1153 (const :tag
"Be smart and do the right thing" smart
)))
1155 (defcustom org-yank-folded-subtrees t
1156 "Non-nil means when yanking subtrees, fold them.
1157 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1158 it starts with a heading and all other headings in it are either children
1159 or siblings, then fold all the subtrees. However, do this only if no
1160 text after the yank would be swallowed into a folded tree by this action."
1161 :group
'org-edit-structure
1164 (defcustom org-yank-adjusted-subtrees nil
1165 "Non-nil means when yanking subtrees, adjust the level.
1166 With this setting, `org-paste-subtree' is used to insert the subtree, see
1167 this function for details."
1168 :group
'org-edit-structure
1171 (defcustom org-M-RET-may-split-line
'((default . t
))
1172 "Non-nil means M-RET will split the line at the cursor position.
1173 When nil, it will go to the end of the line before making a
1175 You may also set this option in a different way for different
1176 contexts. Valid contexts are:
1178 headline when creating a new headline
1179 item when creating a new item
1180 table in a table field
1181 default the value to be used for all contexts not explicitly
1183 :group
'org-structure
1186 (const :tag
"Always" t
)
1187 (const :tag
"Never" nil
)
1188 (repeat :greedy t
:tag
"Individual contexts"
1190 (choice :tag
"Context"
1198 (defcustom org-insert-heading-respect-content nil
1199 "Non-nil means insert new headings after the current subtree.
1200 When nil, the new heading is created directly after the current line.
1201 The commands \\[org-insert-heading-respect-content] and
1202 \\[org-insert-todo-heading-respect-content] turn this variable on
1203 for the duration of the command."
1204 :group
'org-structure
1207 (defcustom org-blank-before-new-entry
'((heading . auto
)
1208 (plain-list-item . auto
))
1209 "Should `org-insert-heading' leave a blank line before new heading/item?
1210 The value is an alist, with `heading' and `plain-list-item' as CAR,
1211 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1212 which case Org will look at the surrounding headings/items and try to
1213 make an intelligent decision whether to insert a blank line or not.
1215 For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
1216 set, the setting here is ignored and no empty line is inserted, to avoid
1217 breaking the list structure."
1218 :group
'org-edit-structure
1220 (cons (const heading
)
1221 (choice (const :tag
"Never" nil
)
1222 (const :tag
"Always" t
)
1223 (const :tag
"Auto" auto
)))
1224 (cons (const plain-list-item
)
1225 (choice (const :tag
"Never" nil
)
1226 (const :tag
"Always" t
)
1227 (const :tag
"Auto" auto
)))))
1229 (defcustom org-insert-heading-hook nil
1230 "Hook being run after inserting a new heading."
1231 :group
'org-edit-structure
1234 (defcustom org-enable-fixed-width-editor t
1235 "Non-nil means lines starting with \":\" are treated as fixed-width.
1236 This currently only means they are never auto-wrapped.
1237 When nil, such lines will be treated like ordinary lines.
1238 See also the QUOTE keyword."
1239 :group
'org-edit-structure
1242 (defcustom org-goto-auto-isearch t
1243 "Non-nil means typing characters in `org-goto' starts incremental search."
1244 :group
'org-edit-structure
1247 (defgroup org-sparse-trees nil
1248 "Options concerning sparse trees in Org-mode."
1249 :tag
"Org Sparse Trees"
1250 :group
'org-structure
)
1252 (defcustom org-highlight-sparse-tree-matches t
1253 "Non-nil means highlight all matches that define a sparse tree.
1254 The highlights will automatically disappear the next time the buffer is
1255 changed by an edit command."
1256 :group
'org-sparse-trees
1259 (defcustom org-remove-highlights-with-change t
1260 "Non-nil means any change to the buffer will remove temporary highlights.
1261 Such highlights are created by `org-occur' and `org-clock-display'.
1262 When nil, `C-c C-c needs to be used to get rid of the highlights.
1263 The highlights created by `org-preview-latex-fragment' always need
1264 `C-c C-c' to be removed."
1265 :group
'org-sparse-trees
1270 (defcustom org-occur-hook
'(org-first-headline-recenter)
1271 "Hook that is run after `org-occur' has constructed a sparse tree.
1272 This can be used to recenter the window to show as much of the structure
1274 :group
'org-sparse-trees
1277 (defgroup org-imenu-and-speedbar nil
1278 "Options concerning imenu and speedbar in Org-mode."
1279 :tag
"Org Imenu and Speedbar"
1280 :group
'org-structure
)
1282 (defcustom org-imenu-depth
2
1283 "The maximum level for Imenu access to Org-mode headlines.
1284 This also applied for speedbar access."
1285 :group
'org-imenu-and-speedbar
1288 (defgroup org-table nil
1289 "Options concerning tables in Org-mode."
1293 (defcustom org-enable-table-editor
'optimized
1294 "Non-nil means lines starting with \"|\" are handled by the table editor.
1295 When nil, such lines will be treated like ordinary lines.
1297 When equal to the symbol `optimized', the table editor will be optimized to
1299 - Automatic overwrite mode in front of whitespace in table fields.
1300 This makes the structure of the table stay in tact as long as the edited
1301 field does not exceed the column width.
1302 - Minimize the number of realigns. Normally, the table is aligned each time
1303 TAB or RET are pressed to move to another field. With optimization this
1304 happens only if changes to a field might have changed the column width.
1305 Optimization requires replacing the functions `self-insert-command',
1306 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1307 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1308 very good at guessing when a re-align will be necessary, but you can always
1309 force one with \\[org-ctrl-c-ctrl-c].
1311 If you would like to use the optimized version in Org-mode, but the
1312 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1314 This variable can be used to turn on and off the table editor during a session,
1315 but in order to toggle optimization, a restart is required.
1317 See also the variable `org-table-auto-blank-field'."
1320 (const :tag
"off" nil
)
1322 (const :tag
"on, optimized" optimized
)))
1324 (defcustom org-self-insert-cluster-for-undo
(or (featurep 'xemacs
)
1325 (version<= emacs-version
"24.1"))
1326 "Non-nil means cluster self-insert commands for undo when possible.
1327 If this is set, then, like in the Emacs command loop, 20 consecutive
1328 characters will be undone together.
1329 This is configurable, because there is some impact on typing performance."
1333 (defcustom org-table-tab-recognizes-table.el t
1334 "Non-nil means TAB will automatically notice a table.el table.
1335 When it sees such a table, it moves point into it and - if necessary -
1336 calls `table-recognize-table'."
1337 :group
'org-table-editing
1340 (defgroup org-link nil
1341 "Options concerning links in Org-mode."
1345 (defvar org-link-abbrev-alist-local nil
1346 "Buffer-local version of `org-link-abbrev-alist', which see.
1347 The value of this is taken from the #+LINK lines.")
1348 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1350 (defcustom org-link-abbrev-alist nil
1351 "Alist of link abbreviations.
1352 The car of each element is a string, to be replaced at the start of a link.
1353 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1354 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1356 [[linkkey:tag][description]]
1358 The 'linkkey' must be a word word, starting with a letter, followed
1359 by letters, numbers, '-' or '_'.
1361 If REPLACE is a string, the tag will simply be appended to create the link.
1362 If the string contains \"%s\", the tag will be inserted there. If the string
1363 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1364 at that point (see the function `url-hexify-string'). If the string contains
1365 the specifier \"%(my-function)\", then the custom function `my-function' will
1366 be invoked: this function takes the tag as its only argument and must return
1369 REPLACE may also be a function that will be called with the tag as the
1370 only argument to create the link, which should be returned as a string.
1372 See the manual for examples."
1376 (string :tag
"Protocol")
1378 (string :tag
"Format")
1381 (defcustom org-descriptive-links t
1382 "Non-nil means Org will display descriptive links.
1383 E.g. [[http://orgmode.org][Org website]] will be displayed as
1384 \"Org Website\", hiding the link itself and just displaying its
1385 description. When set to `nil', Org will display the full links
1388 You can interactively set the value of this variable by calling
1389 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1393 (defcustom org-link-file-path-type
'adaptive
1394 "How the path name in file links should be stored.
1397 relative Relative to the current directory, i.e. the directory of the file
1398 into which the link is being inserted.
1399 absolute Absolute path, if possible with ~ for home directory.
1400 noabbrev Absolute path, no abbreviation of home directory.
1401 adaptive Use relative path for files in the current directory and sub-
1402 directories of it. For other files, use an absolute path."
1410 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1411 "Types of links that should be activated in Org-mode files.
1412 This is a list of symbols, each leading to the activation of a certain link
1413 type. In principle, it does not hurt to turn on most link types - there may
1414 be a small gain when turning off unused link types. The types are:
1416 bracket The recommended [[link][description]] or [[link]] links with hiding.
1417 angle Links in angular brackets that may contain whitespace like
1418 <bbdb:Carsten Dominik>.
1419 plain Plain links in normal text, no whitespace, like http://google.com.
1420 radio Text that is matched by a radio target, see manual for details.
1421 tag Tag settings in a headline (link to tag search).
1422 date Time stamps (link to calendar).
1423 footnote Footnote labels.
1425 Changing this variable requires a restart of Emacs to become effective."
1427 :type
'(set :greedy t
1428 (const :tag
"Double bracket links" bracket
)
1429 (const :tag
"Angular bracket links" angle
)
1430 (const :tag
"Plain text links" plain
)
1431 (const :tag
"Radio target matches" radio
)
1432 (const :tag
"Tags" tag
)
1433 (const :tag
"Timestamps" date
)
1434 (const :tag
"Footnotes" footnote
)))
1436 (defcustom org-make-link-description-function nil
1437 "Function to use for generating link descriptions from links.
1438 When nil, the link location will be used. This function must take
1439 two parameters: the first one is the link, the second one is the
1440 description generated by `org-insert-link'. The function should
1441 return the description to use."
1445 (defgroup org-link-store nil
1446 "Options concerning storing links in Org-mode."
1447 :tag
"Org Store Link"
1450 (defcustom org-url-hexify-p t
1451 "When non-nil, hexify URL when creating a link."
1454 :group
'org-link-store
)
1456 (defcustom org-email-link-description-format
"Email %c: %.30s"
1457 "Format of the description part of a link to an email or usenet message.
1458 The following %-escapes will be replaced by corresponding information:
1460 %F full \"From\" field
1461 %f name, taken from \"From\" field, address if no name
1462 %T full \"To\" field
1463 %t first name in \"To\" field, address if no name
1464 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1465 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1470 You may use normal field width specification between the % and the letter.
1471 This is for example useful to limit the length of the subject.
1473 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1474 :group
'org-link-store
1477 (defcustom org-from-is-user-regexp
1479 (when (and user-mail-address
(not (string= user-mail-address
"")))
1480 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1481 (when (and user-full-name
(not (string= user-full-name
"")))
1482 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1483 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1484 "Regexp matched against the \"From:\" header of an email or usenet message.
1485 It should match if the message is from the user him/herself."
1486 :group
'org-link-store
1489 (defcustom org-context-in-file-links t
1490 "Non-nil means file links from `org-store-link' contain context.
1491 A search string will be added to the file name with :: as separator and
1492 used to find the context when the link is activated by the command
1493 `org-open-at-point'. When this option is t, the entire active region
1494 will be placed in the search string of the file link. If set to a
1495 positive integer, only the first n lines of context will be stored.
1497 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1498 negates this setting for the duration of the command."
1499 :group
'org-link-store
1500 :type
'(choice boolean integer
))
1502 (defcustom org-keep-stored-link-after-insertion nil
1503 "Non-nil means keep link in list for entire session.
1505 The command `org-store-link' adds a link pointing to the current
1506 location to an internal list. These links accumulate during a session.
1507 The command `org-insert-link' can be used to insert links into any
1508 Org-mode file (offering completion for all stored links). When this
1509 option is nil, every link which has been inserted once using \\[org-insert-link]
1510 will be removed from the list, to make completing the unused links
1512 :group
'org-link-store
1515 (defgroup org-link-follow nil
1516 "Options concerning following links in Org-mode."
1517 :tag
"Org Follow Link"
1520 (defcustom org-link-translation-function nil
1521 "Function to translate links with different syntax to Org syntax.
1522 This can be used to translate links created for example by the Planner
1523 or emacs-wiki packages to Org syntax.
1524 The function must accept two parameters, a TYPE containing the link
1525 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1526 which is everything after the link protocol. It should return a cons
1527 with possibly modified values of type and path.
1528 Org contains a function for this, so if you set this variable to
1529 `org-translate-link-from-planner', you should be able follow many
1530 links created by planner."
1531 :group
'org-link-follow
1534 (defcustom org-follow-link-hook nil
1535 "Hook that is run after a link has been followed."
1536 :group
'org-link-follow
1539 (defcustom org-tab-follows-link nil
1540 "Non-nil means on links TAB will follow the link.
1541 Needs to be set before org.el is loaded.
1542 This really should not be used, it does not make sense, and the
1543 implementation is bad."
1544 :group
'org-link-follow
1547 (defcustom org-return-follows-link nil
1548 "Non-nil means on links RET will follow the link."
1549 :group
'org-link-follow
1552 (defcustom org-mouse-1-follows-link
1553 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1554 "Non-nil means mouse-1 on a link will follow the link.
1555 A longer mouse click will still set point. Does not work on XEmacs.
1556 Needs to be set before org.el is loaded."
1557 :group
'org-link-follow
1560 (defcustom org-mark-ring-length
4
1561 "Number of different positions to be recorded in the ring.
1562 Changing this requires a restart of Emacs to work correctly."
1563 :group
'org-link-follow
1566 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1567 "Non-nil means internal links in Org files must exactly match a headline.
1568 When nil, the link search tries to match a phrase with all words
1569 in the search text."
1570 :group
'org-link-follow
1573 (const :tag
"Use fuzzy text search" nil
)
1574 (const :tag
"Match only exact headline" t
)
1575 (const :tag
"Match exact headline or query to create it"
1578 (defcustom org-link-frame-setup
1579 '((vm . vm-visit-folder-other-frame
)
1580 (vm-imap . vm-visit-imap-folder-other-frame
)
1581 (gnus . org-gnus-no-new-news
)
1582 (file . find-file-other-window
)
1583 (wl . wl-other-frame
))
1584 "Setup the frame configuration for following links.
1585 When following a link with Emacs, it may often be useful to display
1586 this link in another window or frame. This variable can be used to
1587 set this up for the different types of links.
1590 `vm-visit-folder-other-window'
1591 `vm-visit-folder-other-frame'
1592 For Gnus, use any of
1595 `org-gnus-no-new-news'
1596 For FILE, use any of
1598 `find-file-other-window'
1599 `find-file-other-frame'
1600 For Wanderlust use any of
1603 For the calendar, use the variable `calendar-setup'.
1604 For BBDB, it is currently only possible to display the matches in
1606 :group
'org-link-follow
1610 (const vm-visit-folder
)
1611 (const vm-visit-folder-other-window
)
1612 (const vm-visit-folder-other-frame
)))
1616 (const gnus-other-frame
)
1617 (const org-gnus-no-new-news
)))
1621 (const find-file-other-window
)
1622 (const find-file-other-frame
)))
1626 (const wl-other-frame
)))))
1628 (defcustom org-display-internal-link-with-indirect-buffer nil
1629 "Non-nil means use indirect buffer to display infile links.
1630 Activating internal links (from one location in a file to another location
1631 in the same file) normally just jumps to the location. When the link is
1632 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1634 another window. When this option is set, the other window actually displays
1635 an indirect buffer clone of the current buffer, to avoid any visibility
1636 changes to the current buffer."
1637 :group
'org-link-follow
1640 (defcustom org-open-non-existing-files nil
1641 "Non-nil means `org-open-file' will open non-existing files.
1642 When nil, an error will be generated.
1643 This variable applies only to external applications because they
1644 might choke on non-existing files. If the link is to a file that
1645 will be opened in Emacs, the variable is ignored."
1646 :group
'org-link-follow
1649 (defcustom org-open-directory-means-index-dot-org nil
1650 "Non-nil means a link to a directory really means to index.org.
1651 When nil, following a directory link will run dired or open a finder/explorer
1652 window on that directory."
1653 :group
'org-link-follow
1656 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1657 "Function and arguments to call for following mailto links.
1658 This is a list with the first element being a Lisp function, and the
1659 remaining elements being arguments to the function. In string arguments,
1660 %a will be replaced by the address, and %s will be replaced by the subject
1661 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1662 :group
'org-link-follow
1664 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1665 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1666 (const :tag
"message-mail" (message-mail "%a" "%s"))
1667 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1669 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1670 "Non-nil means ask for confirmation before executing shell links.
1671 Shell links can be dangerous: just think about a link
1673 [[shell:rm -rf ~/*][Google Search]]
1675 This link would show up in your Org-mode document as \"Google Search\",
1676 but really it would remove your entire home directory.
1677 Therefore we advise against setting this variable to nil.
1678 Just change it to `y-or-n-p' if you want to confirm with a
1679 single keystroke rather than having to type \"yes\"."
1680 :group
'org-link-follow
1682 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1683 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1684 (const :tag
"no confirmation (dangerous)" nil
)))
1685 (put 'org-confirm-shell-link-function
1686 'safe-local-variable
1687 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1689 (defcustom org-confirm-shell-link-not-regexp
""
1690 "A regexp to skip confirmation for shell links."
1691 :group
'org-link-follow
1695 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1696 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1697 Elisp links can be dangerous: just think about a link
1699 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1701 This link would show up in your Org-mode document as \"Google Search\",
1702 but really it would remove your entire home directory.
1703 Therefore we advise against setting this variable to nil.
1704 Just change it to `y-or-n-p' if you want to confirm with a
1705 single keystroke rather than having to type \"yes\"."
1706 :group
'org-link-follow
1708 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1709 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1710 (const :tag
"no confirmation (dangerous)" nil
)))
1711 (put 'org-confirm-shell-link-function
1712 'safe-local-variable
1713 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1715 (defcustom org-confirm-elisp-link-not-regexp
""
1716 "A regexp to skip confirmation for Elisp links."
1717 :group
'org-link-follow
1721 (defconst org-file-apps-defaults-gnu
1725 "Default file applications on a UNIX or GNU/Linux system.
1726 See `org-file-apps'.")
1728 (defconst org-file-apps-defaults-macosx
1731 (system .
"open %s")
1733 ("eps.gz" .
"gv %s")
1735 ("fig" .
"xfig %s"))
1736 "Default file applications on a MacOS X system.
1737 The system \"open\" is known as a default, but we use X11 applications
1738 for some files for which the OS does not have a good default.
1739 See `org-file-apps'.")
1741 (defconst org-file-apps-defaults-windowsnt
1745 (list (if (featurep 'xemacs
)
1746 'mswindows-shell-execute
1750 (list (if (featurep 'xemacs
)
1751 'mswindows-shell-execute
1754 "Default file applications on a Windows NT system.
1755 The system \"open\" is used for most files.
1756 See `org-file-apps'.")
1758 (defcustom org-file-apps
1761 ("\\.mm\\'" . default
)
1762 ("\\.x?html?\\'" . default
)
1763 ("\\.pdf\\'" . default
)
1765 "External applications for opening `file:path' items in a document.
1766 Org-mode uses system defaults for different file types, but
1767 you can use this variable to set the application for a given file
1768 extension. The entries in this list are cons cells where the car identifies
1769 files and the cdr the corresponding command. Possible values for the
1771 \"string\" A string as a file identifier can be interpreted in different
1772 ways, depending on its contents:
1774 - Alphanumeric characters only:
1775 Match links with this file extension.
1776 Example: (\"pdf\" . \"evince %s\")
1777 to open PDFs with evince.
1779 - Regular expression: Match links where the
1780 filename matches the regexp. If you want to
1781 use groups here, use shy groups.
1783 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1784 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1785 to open *.html and *.xhtml with firefox.
1787 - Regular expression which contains (non-shy) groups:
1788 Match links where the whole link, including \"::\", and
1789 anything after that, matches the regexp.
1790 In a custom command string, %1, %2, etc. are replaced with
1791 the parts of the link that were matched by the groups.
1792 For backwards compatibility, if a command string is given
1793 that does not use any of the group matches, this case is
1794 handled identically to the second one (i.e. match against
1796 In a custom lisp form, you can access the group matches with
1797 (match-string n link).
1799 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1800 to open [[file:document.pdf::5]] with evince at page 5.
1802 `directory' Matches a directory
1803 `remote' Matches a remote file, accessible through tramp or efs.
1804 Remote files most likely should be visited through Emacs
1805 because external applications cannot handle such paths.
1806 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1807 so all files Emacs knows how to handle. Using this with
1808 command `emacs' will open most files in Emacs. Beware that this
1809 will also open html files inside Emacs, unless you add
1810 (\"html\" . default) to the list as well.
1811 t Default for files not matched by any of the other options.
1812 `system' The system command to open files, like `open' on Windows
1813 and Mac OS X, and mailcap under GNU/Linux. This is the command
1814 that will be selected if you call `C-c C-o' with a double
1815 \\[universal-argument] \\[universal-argument] prefix.
1817 Possible values for the command are:
1818 `emacs' The file will be visited by the current Emacs process.
1819 `default' Use the default application for this file type, which is the
1820 association for t in the list, most likely in the system-specific
1822 This can be used to overrule an unwanted setting in the
1823 system-specific variable.
1824 `system' Use the system command for opening files, like \"open\".
1825 This command is specified by the entry whose car is `system'.
1826 Most likely, the system-specific version of this variable
1827 does define this command, but you can overrule/replace it
1829 string A command to be executed by a shell; %s will be replaced
1830 by the path to the file.
1831 sexp A Lisp form which will be evaluated. The file path will
1832 be available in the Lisp variable `file'.
1833 For more examples, see the system specific constants
1834 `org-file-apps-defaults-macosx'
1835 `org-file-apps-defaults-windowsnt'
1836 `org-file-apps-defaults-gnu'."
1837 :group
'org-link-follow
1839 (cons (choice :value
""
1840 (string :tag
"Extension")
1841 (const :tag
"System command to open files" system
)
1842 (const :tag
"Default for unrecognized files" t
)
1843 (const :tag
"Remote file" remote
)
1844 (const :tag
"Links to a directory" directory
)
1845 (const :tag
"Any files that have Emacs modes"
1848 (const :tag
"Visit with Emacs" emacs
)
1849 (const :tag
"Use default" default
)
1850 (const :tag
"Use the system command" system
)
1851 (string :tag
"Command")
1852 (sexp :tag
"Lisp form")))))
1854 (defcustom org-doi-server-url
"http://dx.doi.org/"
1855 "The URL of the DOI server."
1858 :group
'org-link-follow
)
1860 (defgroup org-refile nil
1861 "Options concerning refiling entries in Org-mode."
1865 (defcustom org-directory
"~/org"
1866 "Directory with org files.
1867 This is just a default location to look for Org files. There is no need
1868 at all to put your files into this directory. It is only used in the
1869 following situations:
1871 1. When a capture template specifies a target file that is not an
1872 absolute path. The path will then be interpreted relative to
1874 2. When a capture note is filed away in an interactive way (when exiting the
1875 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1876 with `org-directory' as the default path."
1878 :group
'org-remember
1882 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1883 "Default target for storing notes.
1884 Used as a fall back file for org-remember.el and org-capture.el, for
1885 templates that do not specify a target file."
1887 :group
'org-remember
1890 (const :tag
"Default from remember-data-file" nil
)
1893 (defcustom org-goto-interface
'outline
1894 "The default interface to be used for `org-goto'.
1896 outline The interface shows an outline of the relevant file
1897 and the correct heading is found by moving through
1898 the outline or by searching with incremental search.
1899 outline-path-completion Headlines in the current buffer are offered via
1900 completion. This is the interface also used by
1901 the refile command."
1904 (const :tag
"Outline" outline
)
1905 (const :tag
"Outline-path-completion" outline-path-completion
)))
1907 (defcustom org-goto-max-level
5
1908 "Maximum target level when running `org-goto' with refile interface."
1912 (defcustom org-reverse-note-order nil
1913 "Non-nil means store new notes at the beginning of a file or entry.
1914 When nil, new notes will be filed to the end of a file or entry.
1915 This can also be a list with cons cells of regular expressions that
1916 are matched against file names, and values."
1917 :group
'org-remember
1921 (const :tag
"Reverse always" t
)
1922 (const :tag
"Reverse never" nil
)
1923 (repeat :tag
"By file name regexp"
1924 (cons regexp boolean
))))
1926 (defcustom org-log-refile nil
1927 "Information to record when a task is refiled.
1929 Possible values are:
1931 nil Don't add anything
1932 time Add a time stamp to the task
1933 note Prompt for a note and add it with template `org-log-note-headings'
1935 This option can also be set with on a per-file-basis with
1937 #+STARTUP: nologrefile
1938 #+STARTUP: logrefile
1939 #+STARTUP: lognoterefile
1941 You can have local logging settings for a subtree by setting the LOGGING
1942 property to one or more of these keywords.
1944 When bulk-refiling from the agenda, the value `note' is forbidden and
1945 will temporarily be changed to `time'."
1947 :group
'org-progress
1950 (const :tag
"No logging" nil
)
1951 (const :tag
"Record timestamp" time
)
1952 (const :tag
"Record timestamp with note." note
)))
1954 (defcustom org-refile-targets nil
1955 "Targets for refiling entries with \\[org-refile].
1956 This is a list of cons cells. Each cell contains:
1957 - a specification of the files to be considered, either a list of files,
1958 or a symbol whose function or variable value will be used to retrieve
1959 a file name or a list of file names. If you use `org-agenda-files' for
1960 that, all agenda files will be scanned for targets. Nil means consider
1961 headings in the current buffer.
1962 - A specification of how to find candidate refile targets. This may be
1964 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1965 This tag has to be present in all target headlines, inheritance will
1967 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1969 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1970 headlines that are refiling targets.
1971 - a cons cell (:level . N). Any headline of level N is considered a target.
1972 Note that, when `org-odd-levels-only' is set, level corresponds to
1973 order in hierarchy, not to the number of stars.
1974 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1975 Note that, when `org-odd-levels-only' is set, level corresponds to
1976 order in hierarchy, not to the number of stars.
1978 Each element of this list generates a set of possible targets.
1979 The union of these sets is presented (with completion) to
1980 the user by `org-refile'.
1982 You can set the variable `org-refile-target-verify-function' to a function
1983 to verify each headline found by the simple criteria above.
1985 When this variable is nil, all top-level headlines in the current buffer
1986 are used, equivalent to the value `((nil . (:level . 1))'."
1990 (choice :value org-agenda-files
1991 (const :tag
"All agenda files" org-agenda-files
)
1992 (const :tag
"Current buffer" nil
)
1993 (function) (variable) (file))
1994 (choice :tag
"Identify target headline by"
1995 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1996 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1997 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1998 (cons :tag
"Level number" (const :value
:level
) (integer))
1999 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
2001 (defcustom org-refile-target-verify-function nil
2002 "Function to verify if the headline at point should be a refile target.
2003 The function will be called without arguments, with point at the
2004 beginning of the headline. It should return t and leave point
2005 where it is if the headline is a valid target for refiling.
2007 If the target should not be selected, the function must return nil.
2008 In addition to this, it may move point to a place from where the search
2009 should be continued. For example, the function may decide that the entire
2010 subtree of the current entry should be excluded and move point to the end
2015 (defcustom org-refile-use-cache nil
2016 "Non-nil means cache refile targets to speed up the process.
2017 The cache for a particular file will be updated automatically when
2018 the buffer has been killed, or when any of the marker used for flagging
2019 refile targets no longer points at a live buffer.
2020 If you have added new entries to a buffer that might themselves be targets,
2021 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
2022 find that easier, `C-u C-u C-u C-c C-w'."
2027 (defcustom org-refile-use-outline-path nil
2028 "Non-nil means provide refile targets as paths.
2029 So a level 3 headline will be available as level1/level2/level3.
2031 When the value is `file', also include the file name (without directory)
2032 into the path. In this case, you can also stop the completion after
2033 the file name, to get entries inserted as top level in the file.
2035 When `full-file-path', include the full file path."
2038 (const :tag
"Not" nil
)
2039 (const :tag
"Yes" t
)
2040 (const :tag
"Start with file name" file
)
2041 (const :tag
"Start with full file path" full-file-path
)))
2043 (defcustom org-outline-path-complete-in-steps t
2044 "Non-nil means complete the outline path in hierarchical steps.
2045 When Org-mode uses the refile interface to select an outline path
2046 \(see variable `org-refile-use-outline-path'), the completion of
2047 the path can be done is a single go, or if can be done in steps down
2048 the headline hierarchy. Going in steps is probably the best if you
2049 do not use a special completion package like `ido' or `icicles'.
2050 However, when using these packages, going in one step can be very
2051 fast, while still showing the whole path to the entry."
2055 (defcustom org-refile-allow-creating-parent-nodes nil
2056 "Non-nil means allow to create new nodes as refile targets.
2057 New nodes are then created by adding \"/new node name\" to the completion
2058 of an existing node. When the value of this variable is `confirm',
2059 new node creation must be confirmed by the user (recommended)
2060 When nil, the completion must match an existing entry.
2062 Note that, if the new heading is not seen by the criteria
2063 listed in `org-refile-targets', multiple instances of the same
2064 heading would be created by trying again to file under the new
2068 (const :tag
"Never" nil
)
2069 (const :tag
"Always" t
)
2070 (const :tag
"Prompt for confirmation" confirm
)))
2072 (defcustom org-refile-active-region-within-subtree nil
2073 "Non-nil means also refile active region within a subtree.
2075 By default `org-refile' doesn't allow refiling regions if they
2076 don't contain a set of subtrees, but it might be convenient to
2077 do so sometimes: in that case, the first line of the region is
2078 converted to a headline before refiling."
2083 (defgroup org-todo nil
2084 "Options concerning TODO items in Org-mode."
2088 (defgroup org-progress nil
2089 "Options concerning Progress logging in Org-mode."
2093 (defvar org-todo-interpretation-widgets
2094 '((:tag
"Sequence (cycling hits every state)" sequence
)
2095 (:tag
"Type (cycling directly to DONE)" type
))
2096 "The available interpretation symbols for customizing `org-todo-keywords'.
2097 Interested libraries should add to this list.")
2099 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2100 "List of TODO entry keyword sequences and their interpretation.
2101 \\<org-mode-map>This is a list of sequences.
2103 Each sequence starts with a symbol, either `sequence' or `type',
2104 indicating if the keywords should be interpreted as a sequence of
2105 action steps, or as different types of TODO items. The first
2106 keywords are states requiring action - these states will select a headline
2107 for inclusion into the global TODO list Org-mode produces. If one of
2108 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2109 signify that no further action is necessary. If \"|\" is not found,
2110 the last keyword is treated as the only DONE state of the sequence.
2112 The command \\[org-todo] cycles an entry through these states, and one
2113 additional state where no keyword is present. For details about this
2114 cycling, see the manual.
2116 TODO keywords and interpretation can also be set on a per-file basis with
2117 the special #+SEQ_TODO and #+TYP_TODO lines.
2119 Each keyword can optionally specify a character for fast state selection
2120 \(in combination with the variable `org-use-fast-todo-selection')
2121 and specifiers for state change logging, using the same syntax that
2122 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2123 the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2124 indicates to record a time stamp each time this state is selected.
2126 Each keyword may also specify if a timestamp or a note should be
2127 recorded when entering or leaving the state, by adding additional
2128 characters in the parenthesis after the keyword. This looks like this:
2129 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2130 record only the time of the state change. With X and Y being either
2131 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2132 Y when leaving the state if and only if the *target* state does not
2133 define X. You may omit any of the fast-selection key or X or /Y,
2134 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2136 For backward compatibility, this variable may also be just a list
2137 of keywords. In this case the interpretation (sequence or type) will be
2138 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2140 :group
'org-keywords
2142 (repeat :tag
"Old syntax, just keywords"
2143 (string :tag
"Keyword"))
2144 (repeat :tag
"New syntax"
2147 :tag
"Interpretation"
2148 ;;Quick and dirty way to see
2149 ;;`org-todo-interpretations'. This takes the
2150 ;;place of item arguments
2158 org-todo-interpretation-widgets
))
2161 (string :tag
"Keyword"))))))
2163 (defvar org-todo-keywords-1 nil
2164 "All TODO and DONE keywords active in a buffer.")
2165 (make-variable-buffer-local 'org-todo-keywords-1
)
2166 (defvar org-todo-keywords-for-agenda nil
)
2167 (defvar org-done-keywords-for-agenda nil
)
2168 (defvar org-drawers-for-agenda nil
)
2169 (defvar org-todo-keyword-alist-for-agenda nil
)
2170 (defvar org-tag-alist-for-agenda nil
)
2171 (defvar org-agenda-contributing-files nil
)
2172 (defvar org-not-done-keywords nil
)
2173 (make-variable-buffer-local 'org-not-done-keywords
)
2174 (defvar org-done-keywords nil
)
2175 (make-variable-buffer-local 'org-done-keywords
)
2176 (defvar org-todo-heads nil
)
2177 (make-variable-buffer-local 'org-todo-heads
)
2178 (defvar org-todo-sets nil
)
2179 (make-variable-buffer-local 'org-todo-sets
)
2180 (defvar org-todo-log-states nil
)
2181 (make-variable-buffer-local 'org-todo-log-states
)
2182 (defvar org-todo-kwd-alist nil
)
2183 (make-variable-buffer-local 'org-todo-kwd-alist
)
2184 (defvar org-todo-key-alist nil
)
2185 (make-variable-buffer-local 'org-todo-key-alist
)
2186 (defvar org-todo-key-trigger nil
)
2187 (make-variable-buffer-local 'org-todo-key-trigger
)
2189 (defcustom org-todo-interpretation
'sequence
2190 "Controls how TODO keywords are interpreted.
2191 This variable is in principle obsolete and is only used for
2192 backward compatibility, if the interpretation of todo keywords is
2193 not given already in `org-todo-keywords'. See that variable for
2196 :group
'org-keywords
2197 :type
'(choice (const sequence
)
2200 (defcustom org-use-fast-todo-selection t
2201 "Non-nil means use the fast todo selection scheme with C-c C-t.
2202 This variable describes if and under what circumstances the cycling
2203 mechanism for TODO keywords will be replaced by a single-key, direct
2206 When nil, fast selection is never used.
2208 When the symbol `prefix', it will be used when `org-todo' is called
2209 with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
2210 `C-u t' in an agenda buffer.
2212 When t, fast selection is used by default. In this case, the prefix
2213 argument forces cycling instead.
2215 In all cases, the special interface is only used if access keys have
2216 actually been assigned by the user, i.e. if keywords in the configuration
2217 are followed by a letter in parenthesis, like TODO(t)."
2220 (const :tag
"Never" nil
)
2221 (const :tag
"By default" t
)
2222 (const :tag
"Only with C-u C-c C-t" prefix
)))
2224 (defcustom org-provide-todo-statistics t
2225 "Non-nil means update todo statistics after insert and toggle.
2226 ALL-HEADLINES means update todo statistics by including headlines
2227 with no TODO keyword as well, counting them as not done.
2228 A list of TODO keywords means the same, but skip keywords that are
2231 When this is set, todo statistics is updated in the parent of the
2232 current entry each time a todo state is changed."
2235 (const :tag
"Yes, only for TODO entries" t
)
2236 (const :tag
"Yes, including all entries" 'all-headlines
)
2237 (repeat :tag
"Yes, for TODOs in this list"
2238 (string :tag
"TODO keyword"))
2239 (other :tag
"No TODO statistics" nil
)))
2241 (defcustom org-hierarchical-todo-statistics t
2242 "Non-nil means TODO statistics covers just direct children.
2243 When nil, all entries in the subtree are considered.
2244 This has only an effect if `org-provide-todo-statistics' is set.
2245 To set this to nil for only a single subtree, use a COOKIE_DATA
2246 property and include the word \"recursive\" into the value."
2250 (defcustom org-after-todo-state-change-hook nil
2251 "Hook which is run after the state of a TODO item was changed.
2252 The new state (a string with a TODO keyword, or nil) is available in the
2253 Lisp variable `org-state'."
2257 (defvar org-blocker-hook nil
2258 "Hook for functions that are allowed to block a state change.
2260 Functions in this hook should not modify the buffer.
2261 Each function gets as its single argument a property list,
2262 see `org-trigger-hook' for more information about this list.
2264 If any of the functions in this hook returns nil, the state change
2267 (defvar org-trigger-hook nil
2268 "Hook for functions that are triggered by a state change.
2270 Each function gets as its single argument a property list with at
2271 least the following elements:
2273 (:type type-of-change :position pos-at-entry-start
2274 :from old-state :to new-state)
2276 Depending on the type, more properties may be present.
2278 This mechanism is currently implemented for:
2282 :type todo-state-change
2283 :from previous state (keyword as a string), or nil, or a symbol
2284 'todo' or 'done', to indicate the general type of state.
2285 :to new state, like in :from")
2287 (defcustom org-enforce-todo-dependencies nil
2288 "Non-nil means undone TODO entries will block switching the parent to DONE.
2289 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2290 be blocked if any prior sibling is not yet done.
2291 Finally, if the parent is blocked because of ordered siblings of its own,
2292 the child will also be blocked."
2293 :set
(lambda (var val
)
2296 (add-hook 'org-blocker-hook
2297 'org-block-todo-from-children-or-siblings-or-parent
)
2298 (remove-hook 'org-blocker-hook
2299 'org-block-todo-from-children-or-siblings-or-parent
)))
2303 (defcustom org-enforce-todo-checkbox-dependencies nil
2304 "Non-nil means unchecked boxes will block switching the parent to DONE.
2305 When this is nil, checkboxes have no influence on switching TODO states.
2306 When non-nil, you first need to check off all check boxes before the TODO
2307 entry can be switched to DONE.
2308 This variable needs to be set before org.el is loaded, and you need to
2309 restart Emacs after a change to make the change effective. The only way
2310 to change is while Emacs is running is through the customize interface."
2311 :set
(lambda (var val
)
2314 (add-hook 'org-blocker-hook
2315 'org-block-todo-from-checkboxes
)
2316 (remove-hook 'org-blocker-hook
2317 'org-block-todo-from-checkboxes
)))
2321 (defcustom org-treat-insert-todo-heading-as-state-change nil
2322 "Non-nil means inserting a TODO heading is treated as state change.
2323 So when the command \\[org-insert-todo-heading] is used, state change
2324 logging will apply if appropriate. When nil, the new TODO item will
2325 be inserted directly, and no logging will take place."
2329 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2330 "Non-nil means switching TODO states with S-cursor counts as state change.
2331 This is the default behavior. However, setting this to nil allows a
2332 convenient way to select a TODO state and bypass any logging associated
2337 (defcustom org-todo-state-tags-triggers nil
2338 "Tag changes that should be triggered by TODO state changes.
2339 This is a list. Each entry is
2341 (state-change (tag . flag) .......)
2343 State-change can be a string with a state, and empty string to indicate the
2344 state that has no TODO keyword, or it can be one of the symbols `todo'
2345 or `done', meaning any not-done or done state, respectively."
2349 (cons (choice :tag
"When changing to"
2350 (const :tag
"Not-done state" todo
)
2351 (const :tag
"Done state" done
)
2352 (string :tag
"State"))
2354 (cons :tag
"Tag action"
2356 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2358 (defcustom org-log-done nil
2359 "Information to record when a task moves to the DONE state.
2361 Possible values are:
2363 nil Don't add anything, just change the keyword
2364 time Add a time stamp to the task
2365 note Prompt for a note and add it with template `org-log-note-headings'
2367 This option can also be set with on a per-file-basis with
2369 #+STARTUP: nologdone
2371 #+STARTUP: lognotedone
2373 You can have local logging settings for a subtree by setting the LOGGING
2374 property to one or more of these keywords."
2376 :group
'org-progress
2378 (const :tag
"No logging" nil
)
2379 (const :tag
"Record CLOSED timestamp" time
)
2380 (const :tag
"Record CLOSED timestamp with note." note
)))
2382 ;; Normalize old uses of org-log-done.
2384 ((eq org-log-done t
) (setq org-log-done
'time
))
2385 ((and (listp org-log-done
) (memq 'done org-log-done
))
2386 (setq org-log-done
'note
)))
2388 (defcustom org-log-reschedule nil
2389 "Information to record when the scheduling date of a tasks is modified.
2391 Possible values are:
2393 nil Don't add anything, just change the date
2394 time Add a time stamp to the task
2395 note Prompt for a note and add it with template `org-log-note-headings'
2397 This option can also be set with on a per-file-basis with
2399 #+STARTUP: nologreschedule
2400 #+STARTUP: logreschedule
2401 #+STARTUP: lognotereschedule"
2403 :group
'org-progress
2405 (const :tag
"No logging" nil
)
2406 (const :tag
"Record timestamp" time
)
2407 (const :tag
"Record timestamp with note." note
)))
2409 (defcustom org-log-redeadline nil
2410 "Information to record when the deadline date of a tasks is modified.
2412 Possible values are:
2414 nil Don't add anything, just change the date
2415 time Add a time stamp to the task
2416 note Prompt for a note and add it with template `org-log-note-headings'
2418 This option can also be set with on a per-file-basis with
2420 #+STARTUP: nologredeadline
2421 #+STARTUP: logredeadline
2422 #+STARTUP: lognoteredeadline
2424 You can have local logging settings for a subtree by setting the LOGGING
2425 property to one or more of these keywords."
2427 :group
'org-progress
2429 (const :tag
"No logging" nil
)
2430 (const :tag
"Record timestamp" time
)
2431 (const :tag
"Record timestamp with note." note
)))
2433 (defcustom org-log-note-clock-out nil
2434 "Non-nil means record a note when clocking out of an item.
2435 This can also be configured on a per-file basis by adding one of
2436 the following lines anywhere in the buffer:
2438 #+STARTUP: lognoteclock-out
2439 #+STARTUP: nolognoteclock-out"
2441 :group
'org-progress
2444 (defcustom org-log-done-with-time t
2445 "Non-nil means the CLOSED time stamp will contain date and time.
2446 When nil, only the date will be recorded."
2447 :group
'org-progress
2450 (defcustom org-log-note-headings
2451 '((done .
"CLOSING NOTE %t")
2452 (state .
"State %-12s from %-12S %t")
2453 (note .
"Note taken on %t")
2454 (reschedule .
"Rescheduled from %S on %t")
2455 (delschedule .
"Not scheduled, was %S on %t")
2456 (redeadline .
"New deadline from %S on %t")
2457 (deldeadline .
"Removed deadline, was %S on %t")
2458 (refile .
"Refiled on %t")
2460 "Headings for notes added to entries.
2461 The value is an alist, with the car being a symbol indicating the note
2462 context, and the cdr is the heading to be used. The heading may also be the
2464 %t in the heading will be replaced by a time stamp.
2465 %T will be an active time stamp instead the default inactive one
2466 %d will be replaced by a short-format time stamp.
2467 %D will be replaced by an active short-format time stamp.
2468 %s will be replaced by the new TODO state, in double quotes.
2469 %S will be replaced by the old TODO state, in double quotes.
2470 %u will be replaced by the user name.
2471 %U will be replaced by the full user name.
2473 In fact, it is not a good idea to change the `state' entry, because
2474 agenda log mode depends on the format of these entries."
2476 :group
'org-progress
2477 :type
'(list :greedy t
2478 (cons (const :tag
"Heading when closing an item" done
) string
)
2480 "Heading when changing todo state (todo sequence only)"
2482 (cons (const :tag
"Heading when just taking a note" note
) string
)
2483 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2484 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2485 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2486 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2487 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2488 (cons (const :tag
"Heading when refiling" refile
) string
)))
2490 (unless (assq 'note org-log-note-headings
)
2491 (push '(note .
"%t") org-log-note-headings
))
2493 (defcustom org-log-into-drawer nil
2494 "Non-nil means insert state change notes and time stamps into a drawer.
2495 When nil, state changes notes will be inserted after the headline and
2496 any scheduling and clock lines, but not inside a drawer.
2498 The value of this variable should be the name of the drawer to use.
2499 LOGBOOK is proposed as the default drawer for this purpose, you can
2500 also set this to a string to define the drawer of your choice.
2502 A value of t is also allowed, representing \"LOGBOOK\".
2504 A value of t or nil can also be set with on a per-file-basis with
2506 #+STARTUP: logdrawer
2507 #+STARTUP: nologdrawer
2509 If this variable is set, `org-log-state-notes-insert-after-drawers'
2512 You can set the property LOG_INTO_DRAWER to overrule this setting for
2515 :group
'org-progress
2517 (const :tag
"Not into a drawer" nil
)
2518 (const :tag
"LOGBOOK" t
)
2519 (string :tag
"Other")))
2521 (if (fboundp 'defvaralias
)
2522 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2524 (defun org-log-into-drawer ()
2525 "Return the value of `org-log-into-drawer', but let properties overrule.
2526 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2527 used instead of the default value."
2528 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit t
)))
2530 ((not p
) org-log-into-drawer
)
2531 ((equal p
"nil") nil
)
2532 ((equal p
"t") "LOGBOOK")
2535 (defcustom org-log-state-notes-insert-after-drawers nil
2536 "Non-nil means insert state change notes after any drawers in entry.
2537 Only the drawers that *immediately* follow the headline and the
2538 deadline/scheduled line are skipped.
2539 When nil, insert notes right after the heading and perhaps the line
2540 with deadline/scheduling if present.
2542 This variable will have no effect if `org-log-into-drawer' is
2545 :group
'org-progress
2548 (defcustom org-log-states-order-reversed t
2549 "Non-nil means the latest state note will be directly after heading.
2550 When nil, the state change notes will be ordered according to time.
2552 This option can also be set with on a per-file-basis with
2554 #+STARTUP: logstatesreversed
2555 #+STARTUP: nologstatesreversed"
2557 :group
'org-progress
2560 (defcustom org-todo-repeat-to-state nil
2561 "The TODO state to which a repeater should return the repeating task.
2562 By default this is the first task in a TODO sequence, or the previous state
2563 in a TODO_TYP set. But you can specify another task here.
2564 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2567 :type
'(choice (const :tag
"Head of sequence" nil
)
2568 (string :tag
"Specific state")))
2570 (defcustom org-log-repeat
'time
2571 "Non-nil means record moving through the DONE state when triggering repeat.
2572 An auto-repeating task is immediately switched back to TODO when
2573 marked DONE. If you are not logging state changes (by adding \"@\"
2574 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2575 record a closing note, there will be no record of the task moving
2576 through DONE. This variable forces taking a note anyway.
2578 nil Don't force a record
2579 time Record a time stamp
2580 note Prompt for a note and add it with template `org-log-note-headings'
2582 This option can also be set with on a per-file-basis with
2584 #+STARTUP: nologrepeat
2585 #+STARTUP: logrepeat
2586 #+STARTUP: lognoterepeat
2588 You can have local logging settings for a subtree by setting the LOGGING
2589 property to one or more of these keywords."
2591 :group
'org-progress
2593 (const :tag
"Don't force a record" nil
)
2594 (const :tag
"Force recording the DONE state" time
)
2595 (const :tag
"Force recording a note with the DONE state" note
)))
2598 (defgroup org-priorities nil
2599 "Priorities in Org-mode."
2600 :tag
"Org Priorities"
2603 (defcustom org-enable-priority-commands t
2604 "Non-nil means priority commands are active.
2605 When nil, these commands will be disabled, so that you never accidentally
2607 :group
'org-priorities
2610 (defcustom org-highest-priority ?A
2611 "The highest priority of TODO items. A character like ?A, ?B etc.
2612 Must have a smaller ASCII number than `org-lowest-priority'."
2613 :group
'org-priorities
2616 (defcustom org-lowest-priority ?C
2617 "The lowest priority of TODO items. A character like ?A, ?B etc.
2618 Must have a larger ASCII number than `org-highest-priority'."
2619 :group
'org-priorities
2622 (defcustom org-default-priority ?B
2623 "The default priority of TODO items.
2624 This is the priority an item gets if no explicit priority is given.
2625 When starting to cycle on an empty priority the first step in the cycle
2626 depends on `org-priority-start-cycle-with-default'. The resulting first
2627 step priority must not exceed the range from `org-highest-priority' to
2628 `org-lowest-priority' which means that `org-default-priority' has to be
2629 in this range exclusive or inclusive the range boundaries. Else the
2630 first step refuses to set the default and the second will fall back
2631 to (depending on the command used) the highest or lowest priority."
2632 :group
'org-priorities
2635 (defcustom org-priority-start-cycle-with-default t
2636 "Non-nil means start with default priority when starting to cycle.
2637 When this is nil, the first step in the cycle will be (depending on the
2638 command used) one higher or lower than the default priority.
2639 See also `org-default-priority'."
2640 :group
'org-priorities
2643 (defcustom org-get-priority-function nil
2644 "Function to extract the priority from a string.
2645 The string is normally the headline. If this is nil Org computes the
2646 priority from the priority cookie like [#A] in the headline. It returns
2647 an integer, increasing by 1000 for each priority level.
2648 The user can set a different function here, which should take a string
2649 as an argument and return the numeric priority."
2650 :group
'org-priorities
2654 (defgroup org-time nil
2655 "Options concerning time stamps and deadlines in Org-mode."
2659 (defcustom org-insert-labeled-timestamps-at-point nil
2660 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2661 When nil, these labeled time stamps are forces into the second line of an
2662 entry, just after the headline. When scheduling from the global TODO list,
2663 the time stamp will always be forced into the second line."
2667 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2668 "Formats for `format-time-string' which are used for time stamps.
2669 It is not recommended to change this constant.")
2671 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2672 "Number of minutes to round time stamps to.
2673 These are two values, the first applies when first creating a time stamp.
2674 The second applies when changing it with the commands `S-up' and `S-down'.
2675 When changing the time stamp, this means that it will change in steps
2676 of N minutes, as given by the second value.
2678 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2679 numbers should be factors of 60, so for example 5, 10, 15.
2681 When this is larger than 1, you can still force an exact time stamp by using
2682 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2683 and by using a prefix arg to `S-up/down' to specify the exact number
2684 of minutes to shift."
2686 :get
#'(lambda (var) ; Make sure both elements are there
2687 (if (integerp (default-value var
))
2688 (list (default-value var
) 5)
2689 (default-value var
)))
2691 (integer :tag
"when inserting times")
2692 (integer :tag
"when modifying times")))
2694 ;; Normalize old customizations of this variable.
2695 (when (integerp org-time-stamp-rounding-minutes
)
2696 (setq org-time-stamp-rounding-minutes
2697 (list org-time-stamp-rounding-minutes
2698 org-time-stamp-rounding-minutes
)))
2700 (defcustom org-display-custom-times nil
2701 "Non-nil means overlay custom formats over all time stamps.
2702 The formats are defined through the variable `org-time-stamp-custom-formats'.
2703 To turn this on on a per-file basis, insert anywhere in the file:
2704 #+STARTUP: customtime"
2708 (make-variable-buffer-local 'org-display-custom-times
)
2710 (defcustom org-time-stamp-custom-formats
2711 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2712 "Custom formats for time stamps. See `format-time-string' for the syntax.
2713 These are overlaid over the default ISO format if the variable
2714 `org-display-custom-times' is set. Time like %H:%M should be at the
2715 end of the second format. The custom formats are also honored by export
2716 commands, if custom time display is turned on at the time of export."
2720 (defun org-time-stamp-format (&optional long inactive
)
2721 "Get the right format for a time string."
2722 (let ((f (if long
(cdr org-time-stamp-formats
)
2723 (car org-time-stamp-formats
))))
2725 (concat "[" (substring f
1 -
1) "]")
2728 (defcustom org-time-clocksum-format
2729 '(:days
"%dd " :hours
"%d" :require-hours t
:minutes
":%02d" :require-minutes t
)
2730 "The format string used when creating CLOCKSUM lines.
2731 This is also used when Org mode generates a time duration.
2733 The value can be a single format string containing two
2734 %-sequences, which will be filled with the number of hours and
2735 minutes in that order.
2737 Alternatively, the value can be a plist associating any of the
2738 keys :years, :months, :weeks, :days, :hours or :minutes with
2739 format strings. The time duration is formatted using only the
2740 time components that are needed and concatenating the results.
2741 If a time unit in absent, it falls back to the next smallest
2744 The keys :require-years, :require-months, :require-days,
2745 :require-weeks, :require-hours, :require-minutes are also
2746 meaningful. A non-nil value for these keys indicates that the
2747 corresponding time component should always be included, even if
2753 \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
2756 means durations longer than a day will be expressed in days,
2757 hours and minutes, and durations less than a day will always be
2758 expressed in hours and minutes (even for durations less than an
2763 \(:days \"%dd\" :minutes \"%dm\")
2765 means durations longer than a day will be expressed in days and
2766 minutes, and durations less than a day will be expressed entirely
2767 in minutes (even for durations longer than an hour)."
2769 :type
'(choice (string :tag
"Format string")
2771 (group :inline t
(const :tag
"Years" :years
)
2772 (string :tag
"Format string"))
2774 (const :tag
"Always show years" :require-years
)
2776 (group :inline t
(const :tag
"Months" :months
)
2777 (string :tag
"Format string"))
2779 (const :tag
"Always show months" :require-months
)
2781 (group :inline t
(const :tag
"Weeks" :weeks
)
2782 (string :tag
"Format string"))
2784 (const :tag
"Always show weeks" :require-weeks
)
2786 (group :inline t
(const :tag
"Days" :days
)
2787 (string :tag
"Format string"))
2789 (const :tag
"Always show days" :require-days
)
2791 (group :inline t
(const :tag
"Hours" :hours
)
2792 (string :tag
"Format string"))
2794 (const :tag
"Always show hours" :require-hours
)
2796 (group :inline t
(const :tag
"Minutes" :minutes
)
2797 (string :tag
"Format string"))
2799 (const :tag
"Always show minutes" :require-minutes
)
2802 (defcustom org-time-clocksum-use-fractional nil
2803 "If non-nil, \\[org-clock-display] uses fractional times.
2804 org-mode generates a time duration."
2808 (defcustom org-time-clocksum-fractional-format
"%.2f"
2809 "The format string used when creating CLOCKSUM lines,
2810 or when Org mode generates a time duration, if
2811 `org-time-clocksum-use-fractional' is enabled.
2813 The value can be a single format string containing one
2814 %-sequence, which will be filled with the number of hours as
2817 Alternatively, the value can be a plist associating any of the
2818 keys :years, :months, :weeks, :days, :hours or :minutes with
2819 a format string. The time duration is formatted using the
2820 largest time unit which gives a non-zero integer part. If all
2821 specified formats have zero integer part, the smallest time unit
2824 :type
'(choice (string :tag
"Format string")
2825 (set (group :inline t
(const :tag
"Years" :years
)
2826 (string :tag
"Format string"))
2827 (group :inline t
(const :tag
"Months" :months
)
2828 (string :tag
"Format string"))
2829 (group :inline t
(const :tag
"Weeks" :weeks
)
2830 (string :tag
"Format string"))
2831 (group :inline t
(const :tag
"Days" :days
)
2832 (string :tag
"Format string"))
2833 (group :inline t
(const :tag
"Hours" :hours
)
2834 (string :tag
"Format string"))
2835 (group :inline t
(const :tag
"Minutes" :minutes
)
2836 (string :tag
"Format string")))))
2838 (defcustom org-deadline-warning-days
14
2839 "No. of days before expiration during which a deadline becomes active.
2840 This variable governs the display in sparse trees and in the agenda.
2841 When 0 or negative, it means use this number (the absolute value of it)
2842 even if a deadline has a different individual lead time specified.
2844 Custom commands can set this variable in the options section."
2846 :group
'org-agenda-daily
/weekly
2849 (defcustom org-read-date-prefer-future t
2850 "Non-nil means assume future for incomplete date input from user.
2851 This affects the following situations:
2852 1. The user gives a month but not a year.
2853 For example, if it is April and you enter \"feb 2\", this will be read
2854 as Feb 2, *next* year. \"May 5\", however, will be this year.
2855 2. The user gives a day, but no month.
2856 For example, if today is the 15th, and you enter \"3\", Org-mode will
2857 read this as the third of *next* month. However, if you enter \"17\",
2858 it will be considered as *this* month.
2860 If you set this variable to the symbol `time', then also the following
2863 3. If the user gives a time.
2864 If the time is before now, it will be interpreted as tomorrow.
2866 Currently none of this works for ISO week specifications.
2868 When this option is nil, the current day, month and year will always be
2871 See also `org-agenda-jump-prefer-future'."
2874 (const :tag
"Never" nil
)
2875 (const :tag
"Check month and day" t
)
2876 (const :tag
"Check month, day, and time" time
)))
2878 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
2879 "Should the agenda jump command prefer the future for incomplete dates?
2880 The default is to do the same as configured in `org-read-date-prefer-future'.
2881 But you can also set a deviating value here.
2882 This may t or nil, or the symbol `org-read-date-prefer-future'."
2887 (const :tag
"Use org-read-date-prefer-future"
2888 org-read-date-prefer-future
)
2889 (const :tag
"Never" nil
)
2890 (const :tag
"Always" t
)))
2892 (defcustom org-read-date-force-compatible-dates t
2893 "Should date/time prompt force dates that are guaranteed to work in Emacs?
2895 Depending on the system Emacs is running on, certain dates cannot
2896 be represented with the type used internally to represent time.
2897 Dates between 1970-1-1 and 2038-1-1 can always be represented
2898 correctly. Some systems allow for earlier dates, some for later,
2899 some for both. One way to find out it to insert any date into an
2900 Org buffer, putting the cursor on the year and hitting S-up and
2901 S-down to test the range.
2903 When this variable is set to t, the date/time prompt will not let
2904 you specify dates outside the 1970-2037 range, so it is certain that
2905 these dates will work in whatever version of Emacs you are
2906 running, and also that you can move a file from one Emacs implementation
2907 to another. WHenever Org is forcing the year for you, it will display
2910 When this variable is nil, Org will check if the date is
2911 representable in the specific Emacs implementation you are using.
2912 If not, it will force a year, usually the current year, and beep
2913 to remind you. Currently this setting is not recommended because
2914 the likelihood that you will open your Org files in an Emacs that
2915 has limited date range is not negligible.
2917 A workaround for this problem is to use diary sexp dates for time
2918 stamps outside of this range."
2923 (defcustom org-read-date-display-live t
2924 "Non-nil means display current interpretation of date prompt live.
2925 This display will be in an overlay, in the minibuffer."
2929 (defcustom org-read-date-popup-calendar t
2930 "Non-nil means pop up a calendar when prompting for a date.
2931 In the calendar, the date can be selected with mouse-1. However, the
2932 minibuffer will also be active, and you can simply enter the date as well.
2933 When nil, only the minibuffer will be available."
2936 (if (fboundp 'defvaralias
)
2937 (defvaralias 'org-popup-calendar-for-date-prompt
2938 'org-read-date-popup-calendar
))
2940 (defcustom org-read-date-minibuffer-setup-hook nil
2941 "Hook to be used to set up keys for the date/time interface.
2942 Add key definitions to `minibuffer-local-map', which will be a temporary
2947 (defcustom org-extend-today-until
0
2948 "The hour when your day really ends. Must be an integer.
2949 This has influence for the following applications:
2950 - When switching the agenda to \"today\". It it is still earlier than
2951 the time given here, the day recognized as TODAY is actually yesterday.
2952 - When a date is read from the user and it is still before the time given
2953 here, the current date and time will be assumed to be yesterday, 23:59.
2954 Also, timestamps inserted in capture templates follow this rule.
2956 IMPORTANT: This is a feature whose implementation is and likely will
2957 remain incomplete. Really, it is only here because past midnight seems to
2958 be the favorite working time of John Wiegley :-)"
2962 (defcustom org-use-effective-time nil
2963 "If non-nil, consider `org-extend-today-until' when creating timestamps.
2964 For example, if `org-extend-today-until' is 8, and it's 4am, then the
2965 \"effective time\" of any timestamps between midnight and 8am will be
2966 23:59 of the previous day."
2971 (defcustom org-use-last-clock-out-time-as-effective-time nil
2972 "When non-nil, use the last clock out time for `org-todo'.
2973 Note that this option has precedence over the combined use of
2974 `org-use-effective-time' and `org-extend-today-until'."
2979 (defcustom org-edit-timestamp-down-means-later nil
2980 "Non-nil means S-down will increase the time in a time stamp.
2981 When nil, S-up will increase."
2985 (defcustom org-calendar-follow-timestamp-change t
2986 "Non-nil means make the calendar window follow timestamp changes.
2987 When a timestamp is modified and the calendar window is visible, it will be
2988 moved to the new date."
2992 (defgroup org-tags nil
2993 "Options concerning tags in Org-mode."
2997 (defcustom org-tag-alist nil
2998 "List of tags allowed in Org-mode files.
2999 When this list is nil, Org-mode will base TAG input on what is already in the
3001 The value of this variable is an alist, the car of each entry must be a
3002 keyword as a string, the cdr may be a character that is used to select
3003 that tag through the fast-tag-selection interface.
3004 See the manual for details."
3008 (cons (string :tag
"Tag name")
3009 (character :tag
"Access char"))
3010 (list :tag
"Start radio group"
3012 (option (string :tag
"Group description")))
3013 (list :tag
"End radio group"
3015 (option (string :tag
"Group description")))
3016 (const :tag
"New line" (:newline
)))))
3018 (defcustom org-tag-persistent-alist nil
3019 "List of tags that will always appear in all Org-mode files.
3020 This is in addition to any in buffer settings or customizations
3022 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
3023 The value of this variable is an alist, the car of each entry must be a
3024 keyword as a string, the cdr may be a character that is used to select
3025 that tag through the fast-tag-selection interface.
3026 See the manual for details.
3027 To disable these tags on a per-file basis, insert anywhere in the file:
3032 (cons (string :tag
"Tag name")
3033 (character :tag
"Access char"))
3034 (const :tag
"Start radio group" (:startgroup
))
3035 (const :tag
"End radio group" (:endgroup
))
3036 (const :tag
"New line" (:newline
)))))
3038 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
3039 "If non-nil, always offer completion for all tags of all agenda files.
3040 Instead of customizing this variable directly, you might want to
3041 set it locally for capture buffers, because there no list of
3042 tags in that file can be created dynamically (there are none).
3044 (add-hook 'org-capture-mode-hook
3046 (set (make-local-variable
3047 'org-complete-tags-always-offer-all-agenda-tags)
3053 (defvar org-file-tags nil
3054 "List of tags that can be inherited by all entries in the file.
3055 The tags will be inherited if the variable `org-use-tag-inheritance'
3056 says they should be.
3057 This variable is populated from #+FILETAGS lines.")
3059 (defcustom org-use-fast-tag-selection
'auto
3060 "Non-nil means use fast tag selection scheme.
3061 This is a special interface to select and deselect tags with single keys.
3062 When nil, fast selection is never used.
3063 When the symbol `auto', fast selection is used if and only if selection
3064 characters for tags have been configured, either through the variable
3065 `org-tag-alist' or through a #+TAGS line in the buffer.
3066 When t, fast selection is always used and selection keys are assigned
3067 automatically if necessary."
3070 (const :tag
"Always" t
)
3071 (const :tag
"Never" nil
)
3072 (const :tag
"When selection characters are configured" 'auto
)))
3074 (defcustom org-fast-tag-selection-single-key nil
3075 "Non-nil means fast tag selection exits after first change.
3076 When nil, you have to press RET to exit it.
3077 During fast tag selection, you can toggle this flag with `C-c'.
3078 This variable can also have the value `expert'. In this case, the window
3079 displaying the tags menu is not even shown, until you press C-c again."
3082 (const :tag
"No" nil
)
3083 (const :tag
"Yes" t
)
3084 (const :tag
"Expert" expert
)))
3086 (defvar org-fast-tag-selection-include-todo nil
3087 "Non-nil means fast tags selection interface will also offer TODO states.
3088 This is an undocumented feature, you should not rely on it.")
3090 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
3091 "The column to which tags should be indented in a headline.
3092 If this number is positive, it specifies the column. If it is negative,
3093 it means that the tags should be flushright to that column. For example,
3094 -80 works well for a normal 80 character screen.
3095 When 0, place tags directly after headline text, with only one space in
3100 (defcustom org-auto-align-tags t
3101 "Non-nil keeps tags aligned when modifying headlines.
3102 Some operations (i.e. demoting) change the length of a headline and
3103 therefore shift the tags around. With this option turned on, after
3104 each such operation the tags are again aligned to `org-tags-column'."
3108 (defcustom org-use-tag-inheritance t
3109 "Non-nil means tags in levels apply also for sublevels.
3110 When nil, only the tags directly given in a specific line apply there.
3111 This may also be a list of tags that should be inherited, or a regexp that
3112 matches tags that should be inherited. Additional control is possible
3113 with the variable `org-tags-exclude-from-inheritance' which gives an
3114 explicit list of tags to be excluded from inheritance., even if the value of
3115 `org-use-tag-inheritance' would select it for inheritance.
3117 If this option is t, a match early-on in a tree can lead to a large
3118 number of matches in the subtree when constructing the agenda or creating
3119 a sparse tree. If you only want to see the first match in a tree during
3120 a search, check out the variable `org-tags-match-list-sublevels'."
3123 (const :tag
"Not" nil
)
3124 (const :tag
"Always" t
)
3125 (repeat :tag
"Specific tags" (string :tag
"Tag"))
3126 (regexp :tag
"Tags matched by regexp")))
3128 (defcustom org-tags-exclude-from-inheritance nil
3129 "List of tags that should never be inherited.
3130 This is a way to exclude a few tags from inheritance. For way to do
3131 the opposite, to actively allow inheritance for selected tags,
3132 see the variable `org-use-tag-inheritance'."
3134 :type
'(repeat (string :tag
"Tag")))
3136 (defun org-tag-inherit-p (tag)
3137 "Check if TAG is one that should be inherited."
3139 ((member tag org-tags-exclude-from-inheritance
) nil
)
3140 ((eq org-use-tag-inheritance t
) t
)
3141 ((not org-use-tag-inheritance
) nil
)
3142 ((stringp org-use-tag-inheritance
)
3143 (string-match org-use-tag-inheritance tag
))
3144 ((listp org-use-tag-inheritance
)
3145 (member tag org-use-tag-inheritance
))
3146 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3148 (defcustom org-tags-match-list-sublevels t
3149 "Non-nil means list also sublevels of headlines matching a search.
3150 This variable applies to tags/property searches, and also to stuck
3151 projects because this search is based on a tags match as well.
3153 When set to the symbol `indented', sublevels are indented with
3156 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3157 the sublevels of a headline matching a tag search often also match
3158 the same search. Listing all of them can create very long lists.
3159 Setting this variable to nil causes subtrees of a match to be skipped.
3161 This variable is semi-obsolete and probably should always be true. It
3162 is better to limit inheritance to certain tags using the variables
3163 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3166 (const :tag
"No, don't list them" nil
)
3167 (const :tag
"Yes, do list them" t
)
3168 (const :tag
"List them, indented with leading dots" indented
)))
3170 (defcustom org-tags-sort-function nil
3171 "When set, tags are sorted using this function as a comparator."
3174 (const :tag
"No sorting" nil
)
3175 (const :tag
"Alphabetical" string
<)
3176 (const :tag
"Reverse alphabetical" string
>)
3177 (function :tag
"Custom function" nil
)))
3179 (defvar org-tags-history nil
3180 "History of minibuffer reads for tags.")
3181 (defvar org-last-tags-completion-table nil
3182 "The last used completion table for tags.")
3183 (defvar org-after-tags-change-hook nil
3184 "Hook that is run after the tags in a line have changed.")
3186 (defgroup org-properties nil
3187 "Options concerning properties in Org-mode."
3188 :tag
"Org Properties"
3191 (defcustom org-property-format
"%-10s %s"
3192 "How property key/value pairs should be formatted by `indent-line'.
3193 When `indent-line' hits a property definition, it will format the line
3194 according to this format, mainly to make sure that the values are
3195 lined-up with respect to each other."
3196 :group
'org-properties
3199 (defcustom org-properties-postprocess-alist nil
3200 "Alist of properties and functions to adjust inserted values.
3201 Elements of this alist must be of the form
3203 ([string] [function])
3205 where [string] must be a property name and [function] must be a
3206 lambda expression: this lambda expression must take one argument,
3207 the value to adjust, and return the new value as a string.
3209 For example, this element will allow the property \"Remaining\"
3210 to be updated wrt the relation between the \"Effort\" property
3211 and the clock summary:
3213 ((\"Remaining\" (lambda(value)
3214 (let ((clocksum (org-clock-sum-current-item))
3215 (effort (org-duration-string-to-minutes
3216 (org-entry-get (point) \"Effort\"))))
3217 (org-minutes-to-clocksum-string (- effort clocksum))))))"
3218 :group
'org-properties
3220 :type
'(alist :key-type
(string :tag
"Property")
3221 :value-type
(function :tag
"Function")))
3223 (defcustom org-use-property-inheritance nil
3224 "Non-nil means properties apply also for sublevels.
3226 This setting is chiefly used during property searches. Turning it on can
3227 cause significant overhead when doing a search, which is why it is not
3230 When nil, only the properties directly given in the current entry count.
3231 When t, every property is inherited. The value may also be a list of
3232 properties that should have inheritance, or a regular expression matching
3233 properties that should be inherited.
3235 However, note that some special properties use inheritance under special
3236 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3237 and the properties ending in \"_ALL\" when they are used as descriptor
3238 for valid values of a property.
3240 Note for programmers:
3241 When querying an entry with `org-entry-get', you can control if inheritance
3242 should be used. By default, `org-entry-get' looks only at the local
3243 properties. You can request inheritance by setting the inherit argument
3244 to t (to force inheritance) or to `selective' (to respect the setting
3246 :group
'org-properties
3248 (const :tag
"Not" nil
)
3249 (const :tag
"Always" t
)
3250 (repeat :tag
"Specific properties" (string :tag
"Property"))
3251 (regexp :tag
"Properties matched by regexp")))
3253 (defun org-property-inherit-p (property)
3254 "Check if PROPERTY is one that should be inherited."
3256 ((eq org-use-property-inheritance t
) t
)
3257 ((not org-use-property-inheritance
) nil
)
3258 ((stringp org-use-property-inheritance
)
3259 (string-match org-use-property-inheritance property
))
3260 ((listp org-use-property-inheritance
)
3261 (member property org-use-property-inheritance
))
3262 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3264 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3265 "The default column format, if no other format has been defined.
3266 This variable can be set on the per-file basis by inserting a line
3268 #+COLUMNS: %25ITEM ....."
3269 :group
'org-properties
3272 (defcustom org-columns-ellipses
".."
3273 "The ellipses to be used when a field in column view is truncated.
3274 When this is the empty string, as many characters as possible are shown,
3275 but then there will be no visual indication that the field has been truncated.
3276 When this is a string of length N, the last N characters of a truncated
3277 field are replaced by this string. If the column is narrower than the
3278 ellipses string, only part of the ellipses string will be shown."
3279 :group
'org-properties
3282 (defcustom org-columns-modify-value-for-display-function nil
3283 "Function that modifies values for display in column view.
3284 For example, it can be used to cut out a certain part from a time stamp.
3285 The function must take 2 arguments:
3287 column-title The title of the column (*not* the property name)
3288 value The value that should be modified.
3290 The function should return the value that should be displayed,
3291 or nil if the normal value should be used."
3292 :group
'org-properties
3295 (defcustom org-effort-property
"Effort"
3296 "The property that is being used to keep track of effort estimates.
3297 Effort estimates given in this property need to have the format H:MM."
3298 :group
'org-properties
3299 :group
'org-progress
3300 :type
'(string :tag
"Property"))
3302 (defconst org-global-properties-fixed
3303 '(("VISIBILITY_ALL" .
"folded children content all")
3304 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3305 "List of property/value pairs that can be inherited by any entry.
3307 These are fixed values, for the preset properties. The user variable
3308 that can be used to add to this list is `org-global-properties'.
3310 The entries in this list are cons cells where the car is a property
3311 name and cdr is a string with the value. If the value represents
3312 multiple items like an \"_ALL\" property, separate the items by
3315 (defcustom org-global-properties nil
3316 "List of property/value pairs that can be inherited by any entry.
3318 This list will be combined with the constant `org-global-properties-fixed'.
3320 The entries in this list are cons cells where the car is a property
3321 name and cdr is a string with the value.
3323 You can set buffer-local values for the same purpose in the variable
3324 `org-file-properties' this by adding lines like
3326 #+PROPERTY: NAME VALUE"
3327 :group
'org-properties
3329 (cons (string :tag
"Property")
3330 (string :tag
"Value"))))
3332 (defvar org-file-properties nil
3333 "List of property/value pairs that can be inherited by any entry.
3334 Valid for the current buffer.
3335 This variable is populated from #+PROPERTY lines.")
3336 (make-variable-buffer-local 'org-file-properties
)
3338 (defgroup org-agenda nil
3339 "Options concerning agenda views in Org-mode."
3343 (defvar org-category nil
3344 "Variable used by org files to set a category for agenda display.
3345 Such files should use a file variable to set it, for example
3347 # -*- mode: org; org-category: \"ELisp\"
3349 or contain a special line
3353 If the file does not specify a category, then file's base name
3355 (make-variable-buffer-local 'org-category
)
3356 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3358 (defcustom org-agenda-files nil
3359 "The files to be used for agenda display.
3360 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3361 \\[org-remove-file]. You can also use customize to edit the list.
3363 If an entry is a directory, all files in that directory that are matched by
3364 `org-agenda-file-regexp' will be part of the file list.
3366 If the value of the variable is not a list but a single file name, then
3367 the list of agenda files is actually stored and maintained in that file, one
3368 agenda file per line. In this file paths can be given relative to
3369 `org-directory'. Tilde expansion and environment variable substitution
3373 (repeat :tag
"List of files and directories" file
)
3374 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3376 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3377 "Regular expression to match files for `org-agenda-files'.
3378 If any element in the list in that variable contains a directory instead
3379 of a normal file, all files in that directory that are matched by this
3380 regular expression will be included."
3384 (defcustom org-agenda-text-search-extra-files nil
3385 "List of extra files to be searched by text search commands.
3386 These files will be search in addition to the agenda files by the
3387 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3388 Note that these files will only be searched for text search commands,
3389 not for the other agenda views like todo lists, tag searches or the weekly
3390 agenda. This variable is intended to list notes and possibly archive files
3391 that should also be searched by these two commands.
3392 In fact, if the first element in the list is the symbol `agenda-archives',
3393 than all archive files of all agenda files will be added to the search
3396 :type
'(set :greedy t
3397 (const :tag
"Agenda Archives" agenda-archives
)
3398 (repeat :inline t
(file))))
3400 (if (fboundp 'defvaralias
)
3401 (defvaralias 'org-agenda-multi-occur-extra-files
3402 'org-agenda-text-search-extra-files
))
3404 (defcustom org-agenda-skip-unavailable-files nil
3405 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3406 A nil value means to remove them, after a query, from the list."
3410 (defcustom org-calendar-to-agenda-key
[?c
]
3411 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3412 The command `org-calendar-goto-agenda' will be bound to this key. The
3413 default is the character `c' because then `c' can be used to switch back and
3414 forth between agenda and calendar."
3418 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3419 "The key to be installed in `calendar-mode-map' for adding diary entries.
3420 This option is irrelevant until `org-agenda-diary-file' has been configured
3421 to point to an Org-mode file. When that is the case, the command
3422 `org-agenda-diary-entry' will be bound to the key given here, by default
3423 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3424 if you want to continue doing this, you need to change this to a different
3429 (defcustom org-agenda-diary-file
'diary-file
3430 "File to which to add new entries with the `i' key in agenda and calendar.
3431 When this is the symbol `diary-file', the functionality in the Emacs
3432 calendar will be used to add entries to the `diary-file'. But when this
3433 points to a file, `org-agenda-diary-entry' will be used instead."
3436 (const :tag
"The standard Emacs diary file" diary-file
)
3437 (file :tag
"Special Org file diary entries")))
3439 (eval-after-load "calendar"
3441 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3442 'org-calendar-goto-agenda
)
3443 (add-hook 'calendar-mode-hook
3445 (unless (eq org-agenda-diary-file
'diary-file
)
3446 (define-key calendar-mode-map
3447 org-calendar-insert-diary-entry-key
3448 'org-agenda-diary-entry
))))))
3450 (defgroup org-latex nil
3451 "Options for embedding LaTeX code into Org-mode."
3455 (defcustom org-format-latex-options
3456 '(:foreground default
:background default
:scale
1.0
3457 :html-foreground
"Black" :html-background
"Transparent"
3458 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3459 "Options for creating images from LaTeX fragments.
3460 This is a property list with the following properties:
3461 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3462 `default' means use the foreground of the default face.
3463 `auto' means use the foreground from the text face.
3464 :background the background color, or \"Transparent\".
3465 `default' means use the background of the default face.
3466 `auto' means use the background from the text face.
3467 :scale a scaling factor for the size of the images, to get more pixels
3468 :html-foreground, :html-background, :html-scale
3469 the same numbers for HTML export.
3470 :matchers a list indicating which matchers should be used to
3471 find LaTeX fragments. Valid members of this list are:
3472 \"begin\" find environments
3473 \"$1\" find single characters surrounded by $.$
3474 \"$\" find math expressions surrounded by $...$
3475 \"$$\" find math expressions surrounded by $$....$$
3476 \"\\(\" find math expressions surrounded by \\(...\\)
3477 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3481 (defcustom org-format-latex-signal-error t
3482 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3483 When nil, just push out a message."
3488 (defcustom org-latex-to-mathml-jar-file nil
3489 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3490 Use this to specify additional executable file say a jar file.
3492 When using MathToWeb as the converter, specify the full-path to
3493 your mathtoweb.jar file."
3497 (const :tag
"None" nil
)
3498 (file :tag
"JAR file" :must-match t
)))
3500 (defcustom org-latex-to-mathml-convert-command nil
3501 "Command to convert LaTeX fragments to MathML.
3502 Replace format-specifiers in the command as noted below and use
3503 `shell-command' to convert LaTeX to MathML.
3504 %j: Executable file in fully expanded form as specified by
3505 `org-latex-to-mathml-jar-file'.
3506 %I: Input LaTeX file in fully expanded form
3507 %o: Output MathML file
3508 This command is used by `org-create-math-formula'.
3510 When using MathToWeb as the converter, set this to
3511 \"java -jar %j -unicode -force -df %o %I\"."
3515 (const :tag
"None" nil
)
3516 (string :tag
"\nShell command")))
3518 (defcustom org-latex-create-formula-image-program
'dvipng
3519 "Program to convert LaTeX fragments with.
3521 dvipng Process the LaTeX fragments to dvi file, then convert
3522 dvi files to png files using dvipng.
3523 This will also include processing of non-math environments.
3524 imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
3525 to convert pdf files to png files"
3529 (const :tag
"dvipng" dvipng
)
3530 (const :tag
"imagemagick" imagemagick
)))
3532 (defcustom org-latex-preview-ltxpng-directory
"ltxpng/"
3533 "Path to store latex preview images.
3534 A relative path here creates many directories relative to the
3535 processed org files paths. An absolute path puts all preview
3536 images at the same place."
3541 (defun org-format-latex-mathml-available-p ()
3542 "Return t if `org-latex-to-mathml-convert-command' is usable."
3544 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3545 org-latex-to-mathml-convert-command
)
3546 (let ((executable (car (split-string
3547 org-latex-to-mathml-convert-command
))))
3548 (when (executable-find executable
)
3550 "%j" org-latex-to-mathml-convert-command
)
3551 (file-readable-p org-latex-to-mathml-jar-file
)
3554 (defcustom org-format-latex-header
"\\documentclass{article}
3555 \\usepackage[usenames]{color}
3556 \\usepackage{amsmath}
3557 \\usepackage[mathscr]{eucal}
3558 \\pagestyle{empty} % do not remove
3561 % The settings below are copied from fullpage.sty
3562 \\setlength{\\textwidth}{\\paperwidth}
3563 \\addtolength{\\textwidth}{-3cm}
3564 \\setlength{\\oddsidemargin}{1.5cm}
3565 \\addtolength{\\oddsidemargin}{-2.54cm}
3566 \\setlength{\\evensidemargin}{\\oddsidemargin}
3567 \\setlength{\\textheight}{\\paperheight}
3568 \\addtolength{\\textheight}{-\\headheight}
3569 \\addtolength{\\textheight}{-\\headsep}
3570 \\addtolength{\\textheight}{-\\footskip}
3571 \\addtolength{\\textheight}{-3cm}
3572 \\setlength{\\topmargin}{1.5cm}
3573 \\addtolength{\\topmargin}{-2.54cm}"
3574 "The document header used for processing LaTeX fragments.
3575 It is imperative that this header make sure that no page number
3576 appears on the page. The package defined in the variables
3577 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
3578 will either replace the placeholder \"[PACKAGES]\" in this header, or they
3583 (defvar org-format-latex-header-extra nil
)
3585 (defun org-set-packages-alist (var val
)
3586 "Set the packages alist and make sure it has 3 elements per entry."
3587 (set var
(mapcar (lambda (x)
3588 (if (and (consp x
) (= (length x
) 2))
3589 (list (car x
) (nth 1 x
) t
)
3593 (defun org-get-packages-alist (var)
3595 "Get the packages alist and make sure it has 3 elements per entry."
3597 (if (and (consp x
) (= (length x
) 2))
3598 (list (car x
) (nth 1 x
) t
)
3600 (default-value var
)))
3602 ;; The following variables are defined here because is it also used
3603 ;; when formatting latex fragments. Originally it was part of the
3604 ;; LaTeX exporter, which is why the name includes "export".
3605 (defcustom org-export-latex-default-packages-alist
3606 '(("AUTO" "inputenc" t
)
3610 ("" "longtable" nil
)
3622 "Alist of default packages to be inserted in the header.
3623 Change this only if one of the packages here causes an incompatibility
3624 with another package you are using.
3625 The packages in this list are needed by one part or another of Org-mode
3626 to function properly.
3628 - inputenc, fontenc: for basic font and character selection
3629 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
3630 for interpreting the entities in `org-entities'. You can skip some of these
3631 packages if you don't use any of the symbols in it.
3632 - graphicx: for including images
3633 - float, wrapfig: for figure placement
3634 - longtable: for long tables
3635 - hyperref: for cross references
3637 Therefore you should not modify this variable unless you know what you
3638 are doing. The one reason to change it anyway is that you might be loading
3639 some other package that conflicts with one of the default packages.
3640 Each cell is of the format \( \"options\" \"package\" snippet-flag\).
3641 If SNIPPET-FLAG is t, the package also needs to be included when
3642 compiling LaTeX snippets into images for inclusion into HTML."
3643 :group
'org-export-latex
3644 :set
'org-set-packages-alist
3645 :get
'org-get-packages-alist
3649 (list :tag
"options/package pair"
3650 (string :tag
"options")
3651 (string :tag
"package")
3652 (boolean :tag
"Snippet"))
3653 (string :tag
"A line of LaTeX"))))
3655 (defcustom org-export-latex-packages-alist nil
3656 "Alist of packages to be inserted in every LaTeX header.
3657 These will be inserted after `org-export-latex-default-packages-alist'.
3658 Each cell is of the format \( \"options\" \"package\" snippet-flag \).
3659 SNIPPET-FLAG, when t, indicates that this package is also needed when
3660 turning LaTeX snippets into images for inclusion into HTML.
3661 Make sure that you only list packages here which:
3662 - you want in every file
3663 - do not conflict with the default packages in
3664 `org-export-latex-default-packages-alist'
3665 - do not conflict with the setup in `org-format-latex-header'."
3666 :group
'org-export-latex
3667 :set
'org-set-packages-alist
3668 :get
'org-get-packages-alist
3671 (list :tag
"options/package pair"
3672 (string :tag
"options")
3673 (string :tag
"package")
3674 (boolean :tag
"Snippet"))
3675 (string :tag
"A line of LaTeX"))))
3678 (defgroup org-appearance nil
3679 "Settings for Org-mode appearance."
3680 :tag
"Org Appearance"
3683 (defcustom org-level-color-stars-only nil
3684 "Non-nil means fontify only the stars in each headline.
3685 When nil, the entire headline is fontified.
3686 Changing it requires restart of `font-lock-mode' to become effective
3687 also in regions already fontified."
3688 :group
'org-appearance
3691 (defcustom org-hide-leading-stars nil
3692 "Non-nil means hide the first N-1 stars in a headline.
3693 This works by using the face `org-hide' for these stars. This
3694 face is white for a light background, and black for a dark
3695 background. You may have to customize the face `org-hide' to
3697 Changing it requires restart of `font-lock-mode' to become effective
3698 also in regions already fontified.
3699 You may also set this on a per-file basis by adding one of the following
3700 lines to the buffer:
3702 #+STARTUP: hidestars
3703 #+STARTUP: showstars"
3704 :group
'org-appearance
3707 (defcustom org-hidden-keywords nil
3708 "List of symbols corresponding to keywords to be hidden the org buffer.
3709 For example, a value '(title) for this list will make the document's title
3710 appear in the buffer without the initial #+TITLE: keyword."
3711 :group
'org-appearance
3713 :type
'(set (const :tag
"#+AUTHOR" author
)
3714 (const :tag
"#+DATE" date
)
3715 (const :tag
"#+EMAIL" email
)
3716 (const :tag
"#+TITLE" title
)))
3718 (defcustom org-custom-properties nil
3719 "List of properties (as strings) with a special meaning.
3720 The default use of these custom properties is to let the user
3721 hide them with `org-toggle-custom-properties-visibility'."
3722 :group
'org-properties
3723 :group
'org-appearance
3725 :type
'(repeat (string :tag
"Property Name")))
3727 (defcustom org-fontify-done-headline nil
3728 "Non-nil means change the face of a headline if it is marked DONE.
3729 Normally, only the TODO/DONE keyword indicates the state of a headline.
3730 When this is non-nil, the headline after the keyword is set to the
3731 `org-headline-done' as an additional indication."
3732 :group
'org-appearance
3735 (defcustom org-fontify-emphasized-text t
3736 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3737 Changing this variable requires a restart of Emacs to take effect."
3738 :group
'org-appearance
3741 (defcustom org-fontify-whole-heading-line nil
3742 "Non-nil means fontify the whole line for headings.
3743 This is useful when setting a background color for the
3745 :group
'org-appearance
3748 (defcustom org-highlight-latex-fragments-and-specials nil
3749 "Non-nil means fontify what is treated specially by the exporters."
3750 :group
'org-appearance
3753 (defcustom org-hide-emphasis-markers nil
3754 "Non-nil mean font-lock should hide the emphasis marker characters."
3755 :group
'org-appearance
3758 (defcustom org-pretty-entities nil
3759 "Non-nil means show entities as UTF8 characters.
3760 When nil, the \\name form remains in the buffer."
3761 :group
'org-appearance
3765 (defcustom org-pretty-entities-include-sub-superscripts t
3766 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3767 :group
'org-appearance
3771 (defvar org-emph-re nil
3772 "Regular expression for matching emphasis.
3773 After a match, the match groups contain these elements:
3774 0 The match of the full regular expression, including the characters
3775 before and after the proper match
3776 1 The character before the proper match, or empty at beginning of line
3777 2 The proper match, including the leading and trailing markers
3778 3 The leading marker like * or /, indicating the type of highlighting
3779 4 The text between the emphasis markers, not including the markers
3780 5 The character after the match, empty at the end of a line")
3781 (defvar org-verbatim-re nil
3782 "Regular expression for matching verbatim text.")
3783 (defvar org-emphasis-regexp-components
) ; defined just below
3784 (defvar org-emphasis-alist
) ; defined just below
3785 (defun org-set-emph-re (var val
)
3786 "Set variable and compute the emphasis regular expression."
3788 (when (and (boundp 'org-emphasis-alist
)
3789 (boundp 'org-emphasis-regexp-components
)
3790 org-emphasis-alist org-emphasis-regexp-components
)
3791 (let* ((e org-emphasis-regexp-components
)
3797 (body1 (concat body
"*?"))
3798 (markers (mapconcat 'car org-emphasis-alist
""))
3799 (vmarkers (mapconcat
3800 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3801 org-emphasis-alist
"")))
3802 ;; make sure special characters appear at the right position in the class
3803 (if (string-match "\\^" markers
)
3804 (setq markers
(concat (replace-match "" t t markers
) "^")))
3805 (if (string-match "-" markers
)
3806 (setq markers
(concat (replace-match "" t t markers
) "-")))
3807 (if (string-match "\\^" vmarkers
)
3808 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3809 (if (string-match "-" vmarkers
)
3810 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3812 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3813 (int-to-string nl
) "\\}")))
3816 (concat "\\([" pre
"]\\|^\\)"
3818 "\\([" markers
"]\\)"
3826 "\\([" post
"]\\|$\\)"))
3827 (setq org-verbatim-re
3828 (concat "\\([" pre
"]\\|^\\)"
3830 "\\([" vmarkers
"]\\)"
3838 "\\([" post
"]\\|$\\)")))))
3840 (defcustom org-emphasis-regexp-components
3841 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3842 "Components used to build the regular expression for emphasis.
3843 This is a list with five entries. Terminology: In an emphasis string
3844 like \" *strong word* \", we call the initial space PREMATCH, the final
3845 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3846 and \"trong wor\" is the body. The different components in this variable
3847 specify what is allowed/forbidden in each part:
3849 pre Chars allowed as prematch. Beginning of line will be allowed too.
3850 post Chars allowed as postmatch. End of line will be allowed too.
3851 border The chars *forbidden* as border characters.
3852 body-regexp A regexp like \".\" to match a body character. Don't use
3853 non-shy groups here, and don't allow newline here.
3854 newline The maximum number of newlines allowed in an emphasis exp.
3856 Use customize to modify this, or restart Emacs after changing it."
3857 :group
'org-appearance
3858 :set
'org-set-emph-re
3860 (sexp :tag
"Allowed chars in pre ")
3861 (sexp :tag
"Allowed chars in post ")
3862 (sexp :tag
"Forbidden chars in border ")
3863 (sexp :tag
"Regexp for body ")
3864 (integer :tag
"number of newlines allowed")
3865 (option (boolean :tag
"Please ignore this button"))))
3867 (defcustom org-emphasis-alist
3868 `(("*" bold
"<b>" "</b>")
3869 ("/" italic
"<i>" "</i>")
3870 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
3871 ("=" org-code
"<code>" "</code>" verbatim
)
3872 ("~" org-verbatim
"<code>" "</code>" verbatim
)
3873 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
3876 "Special syntax for emphasized text.
3877 Text starting and ending with a special character will be emphasized, for
3878 example *bold*, _underlined_ and /italic/. This variable sets the marker
3879 characters, the face to be used by font-lock for highlighting in Org-mode
3880 Emacs buffers, and the HTML tags to be used for this.
3881 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3882 For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
3883 Use customize to modify this, or restart Emacs after changing it."
3884 :group
'org-appearance
3885 :set
'org-set-emph-re
3888 (string :tag
"Marker character")
3890 (face :tag
"Font-lock-face")
3891 (plist :tag
"Face property list"))
3892 (string :tag
"HTML start tag")
3893 (string :tag
"HTML end tag")
3894 (option (const verbatim
)))))
3896 (defvar org-syntax-table
3897 (let ((st (make-syntax-table)))
3898 (mapc (lambda(c) (modify-syntax-entry
3899 (string-to-char (car c
)) "w p" st
))
3903 (defvar org-protecting-blocks
3904 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3905 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3906 This is needed for font-lock setup.")
3908 ;;; Miscellaneous options
3910 (defgroup org-completion nil
3911 "Completion in Org-mode."
3912 :tag
"Org Completion"
3915 (defcustom org-completion-use-ido nil
3916 "Non-nil means use ido completion wherever possible.
3917 Note that `ido-mode' must be active for this variable to be relevant.
3918 If you decide to turn this variable on, you might well want to turn off
3919 `org-outline-path-complete-in-steps'.
3920 See also `org-completion-use-iswitchb'."
3921 :group
'org-completion
3924 (defcustom org-completion-use-iswitchb nil
3925 "Non-nil means use iswitchb completion wherever possible.
3926 Note that `iswitchb-mode' must be active for this variable to be relevant.
3927 If you decide to turn this variable on, you might well want to turn off
3928 `org-outline-path-complete-in-steps'.
3929 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3930 :group
'org-completion
3933 (defcustom org-completion-fallback-command
'hippie-expand
3934 "The expansion command called by \\[pcomplete] in normal context.
3935 Normal means, no org-mode-specific context."
3936 :group
'org-completion
3939 ;;; Functions and variables from their packages
3940 ;; Declared here to avoid compiler warnings
3943 (defvar outline-mode-menu-heading
)
3944 (defvar outline-mode-menu-show
)
3945 (defvar outline-mode-menu-hide
)
3946 (defvar zmacs-regions
) ; XEmacs regions
3949 (defvar mark-active
)
3952 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
3953 (declare-function calendar-forward-day
"cal-move" (arg))
3954 (declare-function calendar-goto-date
"cal-move" (date))
3955 (declare-function calendar-goto-today
"cal-move" ())
3956 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
3957 (defvar calc-embedded-close-formula
)
3958 (defvar calc-embedded-open-formula
)
3959 (declare-function cdlatex-tab
"ext:cdlatex" ())
3960 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
3961 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
3962 (defvar font-lock-unfontify-region-function
)
3963 (declare-function iswitchb-read-buffer
"iswitchb"
3964 (prompt &optional default require-match start matches-set
))
3965 (defvar iswitchb-temp-buflist
)
3966 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
3967 (defvar org-agenda-tags-todo-honor-ignore-options
)
3968 (declare-function org-agenda-skip
"org-agenda" ())
3970 org-agenda-format-item
"org-agenda"
3971 (extra txt
&optional level category tags dotime noprefix remove-re habitp
))
3972 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
3973 (declare-function org-agenda-change-all-lines
"org-agenda"
3974 (newhead hdmarker
&optional fixface just-this
))
3975 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
3976 (declare-function org-agenda-maybe-redo
"org-agenda" ())
3977 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
3979 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
3980 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3981 "org-agenda" (&optional end
))
3982 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
3983 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
3984 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
3985 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
3986 (declare-function org-indent-mode
"org-indent" (&optional arg
))
3987 (declare-function parse-time-string
"parse-time" (string))
3988 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
3989 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
3990 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
3991 (defvar remember-data-file
)
3992 (defvar texmathp-why
)
3993 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
3994 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
3996 (defvar org-latex-regexps
)
3998 ;;; Autoload and prepare some org modules
4000 ;; Some table stuff that needs to be defined here, because it is used
4001 ;; by the functions setting up org-mode or checking for table context.
4003 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
4004 "Detect an org-type or table-type table.")
4005 (defconst org-table-line-regexp
"^[ \t]*|"
4006 "Detect an org-type table line.")
4007 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
4008 "Detect an org-type table line.")
4009 (defconst org-table-hline-regexp
"^[ \t]*|-"
4010 "Detect an org-type table hline.")
4011 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
4012 "Detect a table-type table hline.")
4013 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
4014 "Detect the first line outside a table when searching from within it.
4015 This works for both table types.")
4017 ;; Autoload the functions in org-table.el that are needed by functions here.
4020 (org-autoload "org-table"
4021 '(org-table-begin org-table-blank-field org-table-end
)))
4024 (defun turn-on-orgtbl ()
4025 "Unconditionally turn on `orgtbl-mode'."
4026 (require 'org-table
)
4029 (defun org-at-table-p (&optional table-type
)
4030 "Return t if the cursor is inside an org-type table.
4031 If TABLE-TYPE is non-nil, also check for table.el-type tables."
4032 (if org-enable-table-editor
4034 (beginning-of-line 1)
4035 (looking-at (if table-type org-table-any-line-regexp
4036 org-table-line-regexp
)))
4038 (defsubst org-table-p
() (org-at-table-p))
4040 (defun org-at-table.el-p
()
4041 "Return t if and only if we are at a table.el table."
4042 (and (org-at-table-p 'any
)
4044 (goto-char (org-table-begin 'any
))
4045 (looking-at org-table1-hline-regexp
))))
4046 (defun org-table-recognize-table.el
()
4047 "If there is a table.el table nearby, recognize it and move into it."
4048 (if org-table-tab-recognizes-table.el
4049 (if (org-at-table.el-p
)
4051 (beginning-of-line 1)
4052 (if (looking-at org-table-dataline-regexp
)
4054 (if (looking-at org-table1-hline-regexp
)
4056 (beginning-of-line 2)
4057 (if (looking-at org-table-any-border-regexp
)
4058 (beginning-of-line -
1)))))
4059 (if (re-search-forward "|" (org-table-end t
) t
)
4062 (if (table--at-cell-p (point))
4064 (message "recognizing table.el table...")
4065 (table-recognize-table)
4066 (message "recognizing table.el table...done")))
4067 (error "This should not happen"))
4072 (defun org-at-table-hline-p ()
4073 "Return t if the cursor is inside a hline in a table."
4074 (if org-enable-table-editor
4076 (beginning-of-line 1)
4077 (looking-at org-table-hline-regexp
))
4080 (defvar org-table-clean-did-remove-column nil
)
4082 (defun org-table-map-tables (function &optional quietly
)
4083 "Apply FUNCTION to the start of all tables in the buffer."
4087 (goto-char (point-min))
4088 (while (re-search-forward org-table-any-line-regexp nil t
)
4090 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
4091 (beginning-of-line 1)
4092 (when (and (looking-at org-table-line-regexp
)
4093 ;; Exclude tables in src/example/verbatim/clocktable blocks
4094 (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
4095 (save-excursion (funcall function
))
4096 (or (looking-at org-table-line-regexp
)
4098 (re-search-forward org-table-any-border-regexp nil
1))))
4099 (unless quietly
(message "Mapping tables: done")))
4101 ;; Declare and autoload functions from org-exp.el & Co
4103 (declare-function org-default-export-plist
"org-exp")
4104 (declare-function org-infile-export-plist
"org-exp")
4105 (declare-function org-get-current-options
"org-exp")
4107 ;; Declare and autoload functions from org-agenda.el
4110 (org-autoload "org-agenda"
4111 '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
4113 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock" (beg end
))
4114 (declare-function org-clock-update-mode-line
"org-clock" ())
4115 (declare-function org-resolve-clocks
"org-clock"
4116 (&optional also-non-dangling-p prompt last-valid
))
4117 (defvar org-clock-start-time
)
4118 (defvar org-clock-marker
(make-marker)
4119 "Marker recording the last clock-in.")
4120 (defvar org-clock-hd-marker
(make-marker)
4121 "Marker recording the last clock-in, but the headline position.")
4122 (defvar org-clock-heading
""
4123 "The heading of the current clock entry.")
4124 (defun org-clock-is-active ()
4125 "Return non-nil if clock is currently running.
4126 The return value is actually the clock marker."
4127 (marker-buffer org-clock-marker
))
4130 (org-autoload "org-clock" '(org-clock-remove-overlays
4131 org-clock-update-time-maybe
4132 org-clocktable-shift
)))
4134 (defun org-check-running-clock ()
4135 "Check if the current buffer contains the running clock.
4136 If yes, offer to stop it and to save the buffer with the changes."
4137 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4138 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4141 (when (y-or-n-p "Save changed buffer?")
4144 (defun org-clocktable-try-shift (dir n
)
4145 "Check if this line starts a clock table, if yes, shift the time block."
4146 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4147 (org-clocktable-shift dir n
)))
4150 (defun org-clock-persistence-insinuate ()
4151 "Set up hooks for clock persistence."
4152 (require 'org-clock
)
4153 (add-hook 'org-mode-hook
'org-clock-load
)
4154 (add-hook 'kill-emacs-hook
'org-clock-save
))
4156 ;; Define the variable already here, to make sure we have it.
4157 (defvar org-indent-mode nil
4158 "Non-nil if Org-Indent mode is enabled.
4159 Use the command `org-indent-mode' to change this variable.")
4161 ;; Autoload archiving code
4162 ;; The stuff that is needed for cycling and tags has to be defined here.
4164 (defgroup org-archive nil
4165 "Options concerning archiving in Org-mode."
4167 :group
'org-structure
)
4169 (defcustom org-archive-location
"%s_archive::"
4170 "The location where subtrees should be archived.
4172 The value of this variable is a string, consisting of two parts,
4173 separated by a double-colon. The first part is a filename and
4174 the second part is a headline.
4176 When the filename is omitted, archiving happens in the same file.
4177 %s in the filename will be replaced by the current file
4178 name (without the directory part). Archiving to a different file
4179 is useful to keep archived entries from contributing to the
4182 The archived entries will be filed as subtrees of the specified
4183 headline. When the headline is omitted, the subtrees are simply
4184 filed away at the end of the file, as top-level entries. Also in
4185 the heading you can use %s to represent the file name, this can be
4186 useful when using the same archive for a number of different files.
4188 Here are a few examples:
4190 If the current file is Projects.org, archive in file
4191 Projects.org_archive, as top-level trees. This is the default.
4193 \"::* Archived Tasks\"
4194 Archive in the current file, under the top-level headline
4195 \"* Archived Tasks\".
4197 \"~/org/archive.org::\"
4198 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4200 \"~/org/archive.org::* From %s\"
4201 Archive in file ~/org/archive.org (absolute path), under headlines
4202 \"From FILENAME\" where file name is the current file name.
4204 \"~/org/datetree.org::datetree/* Finished Tasks\"
4205 The \"datetree/\" string is special, signifying to archive
4206 items to the datetree. Items are placed in either the CLOSED
4207 date of the item, or the current date if there is no CLOSED date.
4208 The heading will be a subentry to the current date. There doesn't
4209 need to be a heading, but there always needs to be a slash after
4210 datetree. For example, to store archived items directly in the
4211 datetree, use \"~/org/datetree.org::datetree/\".
4213 \"basement::** Finished Tasks\"
4214 Archive in file ./basement (relative path), as level 3 trees
4215 below the level 2 heading \"** Finished Tasks\".
4217 You may set this option on a per-file basis by adding to the buffer a
4220 #+ARCHIVE: basement::** Finished Tasks
4222 You may also define it locally for a subtree by setting an ARCHIVE property
4223 in the entry. If such a property is found in an entry, or anywhere up
4224 the hierarchy, it will be used."
4228 (defcustom org-archive-tag
"ARCHIVE"
4229 "The tag that marks a subtree as archived.
4230 An archived subtree does not open during visibility cycling, and does
4231 not contribute to the agenda listings.
4232 After changing this, font-lock must be restarted in the relevant buffers to
4233 get the proper fontification."
4235 :group
'org-keywords
4238 (defcustom org-agenda-skip-archived-trees t
4239 "Non-nil means the agenda will skip any items located in archived trees.
4240 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4241 variable is no longer recommended, you should leave it at the value t.
4242 Instead, use the key `v' to cycle the archives-mode in the agenda."
4244 :group
'org-agenda-skip
4247 (defcustom org-columns-skip-archived-trees t
4248 "Non-nil means ignore archived trees when creating column view."
4250 :group
'org-properties
4253 (defcustom org-cycle-open-archived-trees nil
4254 "Non-nil means `org-cycle' will open archived trees.
4255 An archived tree is a tree marked with the tag ARCHIVE.
4256 When nil, archived trees will stay folded. You can still open them with
4257 normal outline commands like `show-all', but not with the cycling commands."
4262 (defcustom org-sparse-tree-open-archived-trees nil
4263 "Non-nil means sparse tree construction shows matches in archived trees.
4264 When nil, matches in these trees are highlighted, but the trees are kept in
4267 :group
'org-sparse-trees
4270 (defcustom org-sparse-tree-default-date-type
'scheduled-or-deadline
4271 "The default date type when building a sparse tree.
4272 When this is nil, a date is a scheduled or a deadline timestamp.
4273 Otherwise, these types are allowed:
4276 active: only active timestamps (<...>)
4277 inactive: only inactive timestamps (<...)
4278 scheduled: only scheduled timestamps
4279 deadline: only deadline timestamps"
4280 :type
'(choice (const :tag
"Scheduled or deadline" 'scheduled-or-deadline
)
4281 (const :tag
"All timestamps" all
)
4282 (const :tag
"Only active timestamps" active
)
4283 (const :tag
"Only inactive timestamps" inactive
)
4284 (const :tag
"Only scheduled timestamps" scheduled
)
4285 (const :tag
"Only deadline timestamps" deadline
))
4287 :group
'org-sparse-trees
)
4289 (defun org-cycle-hide-archived-subtrees (state)
4290 "Re-hide all archived subtrees after a visibility state change."
4291 (when (and (not org-cycle-open-archived-trees
)
4292 (not (memq state
'(overview folded
))))
4294 (let* ((globalp (memq state
'(contents all
)))
4295 (beg (if globalp
(point-min) (point)))
4296 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4297 (org-hide-archived-subtrees beg end
)
4299 (if (looking-at (concat ".*:" org-archive-tag
":"))
4300 (message "%s" (substitute-command-keys
4301 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4303 (defun org-force-cycle-archived ()
4304 "Cycle subtree even if it is archived."
4306 (setq this-command
'org-cycle
)
4307 (let ((org-cycle-open-archived-trees t
))
4308 (call-interactively 'org-cycle
)))
4310 (defun org-hide-archived-subtrees (beg end
)
4311 "Re-hide all archived subtrees after a visibility state change."
4313 (let* ((re (concat ":" org-archive-tag
":")))
4315 (while (re-search-forward re end t
)
4316 (when (org-at-heading-p)
4317 (org-flag-subtree t
)
4318 (org-end-of-subtree t
))))))
4320 (declare-function outline-end-of-heading
"outline" ())
4321 (declare-function outline-flag-region
"outline" (from to flag
))
4322 (defun org-flag-subtree (flag)
4324 (org-back-to-heading t
)
4325 (outline-end-of-heading)
4326 (outline-flag-region (point)
4327 (progn (org-end-of-subtree t
) (point))
4330 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4333 (org-autoload "org-archive"
4334 '(org-add-archive-files)))
4336 ;; Autoload Column View Code
4338 (declare-function org-columns-number-to-string
"org-colview" (n fmt
&optional printf
))
4339 (declare-function org-columns-get-format-and-top-level
"org-colview" ())
4340 (declare-function org-columns-compute
"org-colview" (property))
4342 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4343 '(org-columns-number-to-string
4344 org-columns-get-format-and-top-level
4346 org-columns-remove-overlays
))
4350 (declare-function org-id-store-link
"org-id")
4351 (declare-function org-id-locations-load
"org-id")
4352 (declare-function org-id-locations-save
"org-id")
4353 (defvar org-id-track-globally
)
4354 (org-autoload "org-id"
4357 org-id-get-with-outline-path-completion
4358 org-id-get-with-outline-drilling
))
4360 ;;; Variables for pre-computed regular expressions, all buffer local
4362 (defvar org-drawer-regexp
"^[ \t]*:PROPERTIES:[ \t]*$"
4363 "Matches first line of a hidden block.")
4364 (make-variable-buffer-local 'org-drawer-regexp
)
4365 (defvar org-todo-regexp nil
4366 "Matches any of the TODO state keywords.")
4367 (make-variable-buffer-local 'org-todo-regexp
)
4368 (defvar org-not-done-regexp nil
4369 "Matches any of the TODO state keywords except the last one.")
4370 (make-variable-buffer-local 'org-not-done-regexp
)
4371 (defvar org-not-done-heading-regexp nil
4372 "Matches a TODO headline that is not done.")
4373 (make-variable-buffer-local 'org-not-done-regexp
)
4374 (defvar org-todo-line-regexp nil
4375 "Matches a headline and puts TODO state into group 2 if present.")
4376 (make-variable-buffer-local 'org-todo-line-regexp
)
4377 (defvar org-complex-heading-regexp nil
4378 "Matches a headline and puts everything into groups:
4380 group 2: The todo keyword, maybe
4381 group 3: Priority cookie
4382 group 4: True headline
4384 (make-variable-buffer-local 'org-complex-heading-regexp
)
4385 (defvar org-complex-heading-regexp-format nil
4386 "Printf format to make regexp to match an exact headline.
4387 This regexp will match the headline of any node which has the
4388 exact headline text that is put into the format, but may have any
4389 TODO state, priority and tags.")
4390 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4391 (defvar org-todo-line-tags-regexp nil
4392 "Matches a headline and puts TODO state into group 2 if present.
4393 Also put tags into group 4 if tags are present.")
4394 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4395 (defvar org-ds-keyword-length
12
4396 "Maximum length of the DEADLINE and SCHEDULED keywords.")
4397 (make-variable-buffer-local 'org-ds-keyword-length
)
4398 (defvar org-deadline-regexp nil
4399 "Matches the DEADLINE keyword.")
4400 (make-variable-buffer-local 'org-deadline-regexp
)
4401 (defvar org-deadline-time-regexp nil
4402 "Matches the DEADLINE keyword together with a time stamp.")
4403 (make-variable-buffer-local 'org-deadline-time-regexp
)
4404 (defvar org-deadline-line-regexp nil
4405 "Matches the DEADLINE keyword and the rest of the line.")
4406 (make-variable-buffer-local 'org-deadline-line-regexp
)
4407 (defvar org-scheduled-regexp nil
4408 "Matches the SCHEDULED keyword.")
4409 (make-variable-buffer-local 'org-scheduled-regexp
)
4410 (defvar org-scheduled-time-regexp nil
4411 "Matches the SCHEDULED keyword together with a time stamp.")
4412 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4413 (defvar org-closed-time-regexp nil
4414 "Matches the CLOSED keyword together with a time stamp.")
4415 (make-variable-buffer-local 'org-closed-time-regexp
)
4417 (defvar org-keyword-time-regexp nil
4418 "Matches any of the 4 keywords, together with the time stamp.")
4419 (make-variable-buffer-local 'org-keyword-time-regexp
)
4420 (defvar org-keyword-time-not-clock-regexp nil
4421 "Matches any of the 3 keywords, together with the time stamp.")
4422 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4423 (defvar org-maybe-keyword-time-regexp nil
4424 "Matches a timestamp, possibly preceded by a keyword.")
4425 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4426 (defvar org-all-time-keywords nil
4427 "List of time keywords.")
4428 (make-variable-buffer-local 'org-all-time-keywords
)
4430 (defconst org-plain-time-of-day-regexp
4433 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4436 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4438 "Regular expression to match a plain time or time range.
4439 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4440 groups carry important information:
4442 1 the first time, range or not
4443 8 the second time, if it is a range.")
4445 (defconst org-plain-time-extension-regexp
4448 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4449 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4450 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4451 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4452 groups carry important information:
4455 9 minutes of duration")
4457 (defconst org-stamp-time-of-day-regexp
4459 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4460 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4462 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4463 "Regular expression to match a timestamp time or time range.
4464 After a match, the following groups carry important information:
4466 1 date plus weekday, for back referencing to make sure both times are on the same day
4467 2 the first time, range or not
4468 4 the second time, if it is a range.")
4470 (defconst org-startup-options
4471 '(("fold" org-startup-folded t
)
4472 ("overview" org-startup-folded t
)
4473 ("nofold" org-startup-folded nil
)
4474 ("showall" org-startup-folded nil
)
4475 ("showeverything" org-startup-folded showeverything
)
4476 ("content" org-startup-folded content
)
4477 ("indent" org-startup-indented t
)
4478 ("noindent" org-startup-indented nil
)
4479 ("hidestars" org-hide-leading-stars t
)
4480 ("showstars" org-hide-leading-stars nil
)
4481 ("odd" org-odd-levels-only t
)
4482 ("oddeven" org-odd-levels-only nil
)
4483 ("align" org-startup-align-all-tables t
)
4484 ("noalign" org-startup-align-all-tables nil
)
4485 ("inlineimages" org-startup-with-inline-images t
)
4486 ("noinlineimages" org-startup-with-inline-images nil
)
4487 ("customtime" org-display-custom-times t
)
4488 ("logdone" org-log-done time
)
4489 ("lognotedone" org-log-done note
)
4490 ("nologdone" org-log-done nil
)
4491 ("lognoteclock-out" org-log-note-clock-out t
)
4492 ("nolognoteclock-out" org-log-note-clock-out nil
)
4493 ("logrepeat" org-log-repeat state
)
4494 ("lognoterepeat" org-log-repeat note
)
4495 ("logdrawer" org-log-into-drawer t
)
4496 ("nologdrawer" org-log-into-drawer nil
)
4497 ("logstatesreversed" org-log-states-order-reversed t
)
4498 ("nologstatesreversed" org-log-states-order-reversed nil
)
4499 ("nologrepeat" org-log-repeat nil
)
4500 ("logreschedule" org-log-reschedule time
)
4501 ("lognotereschedule" org-log-reschedule note
)
4502 ("nologreschedule" org-log-reschedule nil
)
4503 ("logredeadline" org-log-redeadline time
)
4504 ("lognoteredeadline" org-log-redeadline note
)
4505 ("nologredeadline" org-log-redeadline nil
)
4506 ("logrefile" org-log-refile time
)
4507 ("lognoterefile" org-log-refile note
)
4508 ("nologrefile" org-log-refile nil
)
4509 ("fninline" org-footnote-define-inline t
)
4510 ("nofninline" org-footnote-define-inline nil
)
4511 ("fnlocal" org-footnote-section nil
)
4512 ("fnauto" org-footnote-auto-label t
)
4513 ("fnprompt" org-footnote-auto-label nil
)
4514 ("fnconfirm" org-footnote-auto-label confirm
)
4515 ("fnplain" org-footnote-auto-label plain
)
4516 ("fnadjust" org-footnote-auto-adjust t
)
4517 ("nofnadjust" org-footnote-auto-adjust nil
)
4518 ("constcgs" constants-unit-system cgs
)
4519 ("constSI" constants-unit-system SI
)
4520 ("noptag" org-tag-persistent-alist nil
)
4521 ("hideblocks" org-hide-block-startup t
)
4522 ("nohideblocks" org-hide-block-startup nil
)
4523 ("beamer" org-startup-with-beamer-mode t
)
4524 ("entitiespretty" org-pretty-entities t
)
4525 ("entitiesplain" org-pretty-entities nil
))
4526 "Variable associated with STARTUP options for org-mode.
4527 Each element is a list of three items: the startup options (as written
4528 in the #+STARTUP line), the corresponding variable, and the value to set
4529 this variable to if the option is found. An optional forth element PUSH
4530 means to push this value onto the list in the variable.")
4532 (defun org-update-property-plist (key val props
)
4533 "Update PROPS with KEY and VAL."
4534 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4535 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4536 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4537 (previous (cdr (assoc key props
))))
4539 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4540 (cons (cons key val
) remainder
))))
4542 (defconst org-block-regexp
4543 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4544 "Regular expression for hiding blocks.")
4545 (defconst org-heading-keyword-regexp-format
4546 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4547 "Printf format for a regexp matching an headline with some keyword.
4548 This regexp will match the headline of any node which has the
4549 exact keyword that is put into the format. The keyword isn't in
4550 any group by default, but the stars and the body are.")
4551 (defconst org-heading-keyword-maybe-regexp-format
4552 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4553 "Printf format for a regexp matching an headline, possibly with some keyword.
4554 This regexp can match any headline with the specified keyword, or
4555 without a keyword. The keyword isn't in any group by default,
4556 but the stars and the body are.")
4558 (defun org-set-regexps-and-options ()
4559 "Precompute regular expressions for current buffer."
4560 (when (derived-mode-p 'org-mode
)
4561 (org-set-local 'org-todo-kwd-alist nil
)
4562 (org-set-local 'org-todo-key-alist nil
)
4563 (org-set-local 'org-todo-key-trigger nil
)
4564 (org-set-local 'org-todo-keywords-1 nil
)
4565 (org-set-local 'org-done-keywords nil
)
4566 (org-set-local 'org-todo-heads nil
)
4567 (org-set-local 'org-todo-sets nil
)
4568 (org-set-local 'org-todo-log-states nil
)
4569 (org-set-local 'org-file-properties nil
)
4570 (org-set-local 'org-file-tags nil
)
4571 (let ((re (org-make-options-regexp
4572 '("CATEGORY" "TODO" "COLUMNS"
4573 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
4574 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
4576 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4578 (scripts org-use-sub-superscripts
)
4579 kwds kws0 kwsa key log value cat arch tags const links hw dws
4580 tail sep kws1 prio props ftags drawers beamer-p
4581 ext-setup-or-nil setup-contents
(start 0))
4585 (goto-char (point-min))
4586 (while (or (and ext-setup-or-nil
4587 (string-match re ext-setup-or-nil start
)
4588 (setq start
(match-end 0)))
4589 (and (setq ext-setup-or-nil nil start
0)
4590 (re-search-forward re nil t
)))
4591 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4592 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4593 (if (stringp value
) (setq value
(org-trim value
)))
4595 ((equal key
"CATEGORY")
4597 ((member key
'("SEQ_TODO" "TODO"))
4598 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4599 ((equal key
"TYP_TODO")
4600 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4601 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4602 ;; general TODO-like setup
4603 (push (cons (intern (downcase (match-string 1 key
)))
4604 (org-split-string value splitre
)) kwds
))
4606 (setq tags
(append tags
(if tags
'("\\n") nil
)
4607 (org-split-string value splitre
))))
4608 ((equal key
"COLUMNS")
4609 (org-set-local 'org-columns-default-format value
))
4611 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4612 (push (cons (match-string 1 value
)
4613 (org-trim (match-string 2 value
)))
4615 ((equal key
"PRIORITIES")
4616 (setq prio
(org-split-string value
" +")))
4617 ((equal key
"PROPERTY")
4618 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4619 (setq props
(org-update-property-plist (match-string 1 value
)
4620 (match-string 2 value
)
4622 ((equal key
"FILETAGS")
4623 (when (string-match "\\S-" value
)
4628 (mapcar (lambda (x) (org-split-string x
":"))
4629 (org-split-string value
)))))))
4630 ((equal key
"DRAWERS")
4631 (setq drawers
(delete-dups (append org-drawers
(org-split-string value splitre
)))))
4632 ((equal key
"CONSTANTS")
4633 (setq const
(append const
(org-split-string value splitre
))))
4634 ((equal key
"STARTUP")
4635 (let ((opts (org-split-string value splitre
))
4637 (while (setq l
(pop opts
))
4638 (when (setq l
(assoc l org-startup-options
))
4639 (setq var
(nth 1 l
) val
(nth 2 l
))
4641 (set (make-local-variable var
) val
)
4642 (if (not (listp (symbol-value var
)))
4643 (set (make-local-variable var
) nil
))
4644 (set (make-local-variable var
) (symbol-value var
))
4645 (add-to-list var val
))))))
4646 ((equal key
"ARCHIVE")
4648 (remove-text-properties 0 (length arch
)
4649 '(face t fontified t
) arch
))
4650 ((equal key
"LATEX_CLASS")
4651 (setq beamer-p
(equal value
"beamer")))
4652 ((equal key
"OPTIONS")
4653 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4654 (setq scripts
(read (match-string 2 value
)))))
4655 ((equal key
"SETUPFILE")
4656 (setq setup-contents
(org-file-contents
4658 (org-remove-double-quotes value
))
4660 (if (not ext-setup-or-nil
)
4661 (setq ext-setup-or-nil setup-contents start
0)
4662 (setq ext-setup-or-nil
4663 (concat (substring ext-setup-or-nil
0 start
)
4664 "\n" setup-contents
"\n"
4665 (substring ext-setup-or-nil start
)))))))
4666 ;; search for property blocks
4667 (goto-char (point-min))
4668 (while (re-search-forward org-block-regexp nil t
)
4669 (when (equal "PROPERTY" (upcase (match-string 1)))
4670 (setq value
(replace-regexp-in-string
4671 "[\n\r]" " " (match-string 4)))
4672 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4673 (setq props
(org-update-property-plist (match-string 1 value
)
4674 (match-string 2 value
)
4676 (org-set-local 'org-use-sub-superscripts scripts
)
4678 (org-set-local 'org-category
(intern cat
))
4679 (push (cons "CATEGORY" cat
) props
))
4681 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4682 (setq prio
(mapcar 'string-to-char prio
))
4683 (org-set-local 'org-highest-priority
(nth 0 prio
))
4684 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4685 (org-set-local 'org-default-priority
(nth 2 prio
)))
4686 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4687 (and ftags
(org-set-local 'org-file-tags
4688 (mapcar 'org-add-prop-inherited ftags
)))
4689 (and drawers
(org-set-local 'org-drawers drawers
))
4690 (and arch
(org-set-local 'org-archive-location arch
))
4691 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4692 ;; Process the TODO keywords
4694 ;; Use the global values as if they had been given locally.
4695 (setq kwds
(default-value 'org-todo-keywords
))
4696 (if (stringp (car kwds
))
4697 (setq kwds
(list (cons org-todo-interpretation
4698 (default-value 'org-todo-keywords
)))))
4699 (setq kwds
(reverse kwds
)))
4700 (setq kwds
(nreverse kwds
))
4702 (while (setq kws
(pop kwds
))
4704 (run-hook-with-args-until-success
4705 'org-todo-setup-filter-hook kws
)
4707 (setq inter
(pop kws
) sep
(member "|" kws
)
4708 kws0
(delete "|" (copy-sequence kws
))
4713 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4715 (setq kw
(match-string 1 x
)
4716 key
(and (match-end 2) (match-string 2 x
))
4717 log
(org-extract-log-state-settings x
))
4718 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4719 (and log
(push log org-todo-log-states
))
4721 (error "Invalid TODO keyword %s" x
)))
4723 kwsa
(if kwsa
(append '((:startgroup
))
4727 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4728 tail
(list inter hw
(car dws
) (org-last dws
))))
4729 (add-to-list 'org-todo-heads hw
'append
)
4730 (push kws1 org-todo-sets
)
4731 (setq org-done-keywords
(append org-done-keywords dws nil
))
4732 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4733 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4734 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4735 (setq org-todo-sets
(nreverse org-todo-sets
)
4736 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4737 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4738 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4739 ;; Process the constants
4742 (while (setq e
(pop const
))
4743 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4744 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4745 (setq org-table-formula-constants-local cst
)))
4747 ;; Process the tags.
4750 (while (setq e
(pop tags
))
4752 ((equal e
"{") (push '(:startgroup
) tgs
))
4753 ((equal e
"}") (push '(:endgroup
) tgs
))
4754 ((equal e
"\\n") (push '(:newline
) tgs
))
4755 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4756 (push (cons (match-string 1 e
)
4757 (string-to-char (match-string 2 e
)))
4759 (t (push (list e
) tgs
))))
4760 (org-set-local 'org-tag-alist nil
)
4761 (while (setq e
(pop tgs
))
4762 (or (and (stringp (car e
))
4763 (assoc (car e
) org-tag-alist
))
4764 (push e org-tag-alist
)))))
4766 ;; Compute the regular expressions and other local variables.
4767 ;; Using `org-outline-regexp-bol' would complicate them much,
4768 ;; because of the fixed white space at the end of that string.
4769 (if (not org-done-keywords
)
4770 (setq org-done-keywords
(and org-todo-keywords-1
4771 (list (org-last org-todo-keywords-1
)))))
4772 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4773 (length org-scheduled-string
)
4774 (length org-clock-string
)
4775 (length org-closed-string
)))
4777 (concat "^[ \t]*:\\("
4778 (mapconcat 'regexp-quote org-drawers
"\\|")
4780 org-not-done-keywords
4781 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4784 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4788 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4790 org-not-done-heading-regexp
4791 (format org-heading-keyword-regexp-format org-not-done-regexp
)
4792 org-todo-line-regexp
4793 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
4794 org-complex-heading-regexp
4795 (concat "^\\(\\*+\\)"
4796 "\\(?: +" org-todo-regexp
"\\)?"
4797 "\\(?: +\\(\\[#.\\]\\)\\)?"
4798 "\\(?: +\\(.*?\\)\\)??"
4799 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
4801 org-complex-heading-regexp-format
4802 (concat "^\\(\\*+\\)"
4803 "\\(?: +" org-todo-regexp
"\\)?"
4804 "\\(?: +\\(\\[#.\\]\\)\\)?"
4806 ;; Stats cookies can be stuck to body.
4807 "\\(?:\\[[0-9%%/]+\\] *\\)?"
4809 "\\(?: *\\[[0-9%%/]+\\]\\)?"
4811 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
4813 org-todo-line-tags-regexp
4814 (concat "^\\(\\*+\\)"
4815 "\\(?: +" org-todo-regexp
"\\)?"
4816 "\\(?: +\\(.*?\\)\\)??"
4817 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
4819 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4820 org-deadline-time-regexp
4821 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4822 org-deadline-line-regexp
4823 (concat "\\<\\(" org-deadline-string
"\\).*")
4824 org-scheduled-regexp
4825 (concat "\\<" org-scheduled-string
)
4826 org-scheduled-time-regexp
4827 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4828 org-closed-time-regexp
4829 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4830 org-keyword-time-regexp
4831 (concat "\\<\\(" org-scheduled-string
4832 "\\|" org-deadline-string
4833 "\\|" org-closed-string
4834 "\\|" org-clock-string
"\\)"
4835 " *[[<]\\([^]>]+\\)[]>]")
4836 org-keyword-time-not-clock-regexp
4837 (concat "\\<\\(" org-scheduled-string
4838 "\\|" org-deadline-string
4839 "\\|" org-closed-string
4841 " *[[<]\\([^]>]+\\)[]>]")
4842 org-maybe-keyword-time-regexp
4843 (concat "\\(\\<\\(" org-scheduled-string
4844 "\\|" org-deadline-string
4845 "\\|" org-closed-string
4846 "\\|" org-clock-string
"\\)\\)?"
4847 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4848 org-all-time-keywords
4849 (mapcar (lambda (w) (substring w
0 -
1))
4850 (list org-scheduled-string org-deadline-string
4851 org-clock-string org-closed-string
))
4853 (org-compute-latex-and-specials-regexp)
4854 (org-set-font-lock-defaults))))
4856 (defun org-file-contents (file &optional noerror
)
4857 "Return the contents of FILE, as a string."
4859 (not (file-readable-p file
)))
4862 (message "Cannot read file \"%s\"" file
)
4865 (error "Cannot read file \"%s\"" file
))
4867 (insert-file-contents file
)
4870 (defun org-extract-log-state-settings (x)
4871 "Extract the log state setting from a TODO keyword string.
4872 This will extract info from a string like \"WAIT(w@/!)\"."
4873 (let (kw key log1 log2
)
4874 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4875 (setq kw
(match-string 1 x
)
4876 key
(and (match-end 2) (match-string 2 x
))
4877 log1
(and (match-end 3) (match-string 3 x
))
4878 log2
(and (match-end 4) (match-string 4 x
)))
4881 (and log1
(if (equal log1
"!") 'time
'note
))
4882 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4884 (defun org-remove-keyword-keys (list)
4885 "Remove a pair of parenthesis at the end of each string in LIST."
4887 (if (string-match "(.*)$" x
)
4888 (substring x
0 (match-beginning 0))
4892 (defun org-assign-fast-keys (alist)
4893 "Assign fast keys to a keyword-key alist.
4894 Respect keys that are already there."
4895 (let (new e
(alt ?
0))
4896 (while (setq e
(pop alist
))
4897 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
4898 (cdr e
)) ;; Key already assigned.
4900 (let ((clist (string-to-list (downcase (car e
))))
4901 (used (append new alist
)))
4902 (when (= (car clist
) ?
@)
4904 (while (and clist
(rassoc (car clist
) used
))
4907 (while (rassoc alt used
)
4909 (push (cons (car e
) (or (car clist
) alt
)) new
))))
4912 ;;; Some variables used in various places
4914 (defvar org-window-configuration nil
4915 "Used in various places to store a window configuration.")
4916 (defvar org-selected-window nil
4917 "Used in various places to store a window configuration.")
4918 (defvar org-finish-function nil
4919 "Function to be called when `C-c C-c' is used.
4920 This is for getting out of special buffers like capture.")
4923 ;; FIXME: Occasionally check by commenting these, to make sure
4924 ;; no other functions uses these, forgetting to let-bind them.
4925 (org-no-warnings (defvar entry
)) ;; unprefixed, from calendar.el
4926 (defvar org-last-state
)
4927 (org-no-warnings (defvar date
)) ;; unprefixed, from calendar.el
4929 ;; Defined somewhere in this file, but used before definition.
4930 (defvar org-entities
) ;; defined in org-entities.el
4931 (defvar org-struct-menu
)
4932 (defvar org-org-menu
)
4933 (defvar org-tbl-menu
)
4935 ;;;; Define the Org-mode
4937 ;; We use a before-change function to check if a table might need
4939 (defvar org-table-may-need-update t
4940 "Indicates that a table might need an update.
4941 This variable is set by `org-before-change-function'.
4942 `org-table-align' sets it back to nil.")
4943 (defun org-before-change-function (beg end
)
4944 "Every change indicates that a table might need an update."
4945 (setq org-table-may-need-update t
))
4946 (defvar org-mode-map
)
4947 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4948 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
4949 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4950 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
4951 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
4952 (defvar org-table-buffer-is-an nil
)
4954 (defvar bidi-paragraph-direction
)
4955 (defvar buffer-face-mode-face
)
4958 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4959 (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"))
4960 (require 'noutline
"noutline" 'noerror
) ;; stock XEmacs does not have it
4962 ;; Other stuff we need.
4963 (require 'time-date
)
4964 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
4969 (require 'org-entities
)
4970 ;; (require 'org-compat) moved higher up in the file before it is first used
4971 (require 'org-faces
)
4973 (require 'org-pcomplete
)
4975 (require 'org-footnote
)
4981 (define-derived-mode org-mode outline-mode
"Org"
4982 "Outline-based notes management and organizer, alias
4983 \"Carsten's outline-mode for keeping track of everything.\"
4985 Org-mode develops organizational tasks around a NOTES file which
4986 contains information about projects as plain text. Org-mode is
4987 implemented on top of outline-mode, which is ideal to keep the content
4988 of large files well structured. It supports ToDo items, deadlines and
4989 time stamps, which magically appear in the diary listing of the Emacs
4990 calendar. Tables are easily created with a built-in table editor.
4991 Plain text URL-like links connect to websites, emails (VM), Usenet
4992 messages (Gnus), BBDB entries, and any files related to the project.
4993 For printing and sharing of notes, an Org-mode file (or a part of it)
4994 can be exported as a structured ASCII or HTML file.
4996 The following commands are available:
5000 ;; Get rid of Outline menus, they are not needed
5001 ;; Need to do this here because define-derived-mode sets up
5002 ;; the keymap so late. Still, it is a waste to call this each time
5003 ;; we switch another buffer into org-mode.
5004 (if (featurep 'xemacs
)
5005 (when (boundp 'outline-mode-menu-heading
)
5006 ;; Assume this is Greg's port, it uses easymenu
5007 (easy-menu-remove outline-mode-menu-heading
)
5008 (easy-menu-remove outline-mode-menu-show
)
5009 (easy-menu-remove outline-mode-menu-hide
))
5010 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
5011 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
5012 (define-key org-mode-map
[menu-bar show
] 'undefined
))
5014 (org-load-modules-maybe)
5015 (easy-menu-add org-org-menu
)
5016 (easy-menu-add org-tbl-menu
)
5017 (org-install-agenda-files-menu)
5018 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
5019 (add-to-invisibility-spec '(org-cwidth))
5020 (add-to-invisibility-spec '(org-hide-block . t
))
5021 (when (featurep 'xemacs
)
5022 (org-set-local 'line-move-ignore-invisible t
))
5023 (org-set-local 'outline-regexp org-outline-regexp
)
5024 (org-set-local 'outline-level
'org-outline-level
)
5025 (setq bidi-paragraph-direction
'left-to-right
)
5026 (when (and org-ellipsis
5027 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
5028 (fboundp 'make-glyph-code
))
5029 (unless org-display-table
5030 (setq org-display-table
(make-display-table)))
5031 (set-display-table-slot
5034 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
5036 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
5037 (setq buffer-display-table org-display-table
))
5038 (org-set-regexps-and-options)
5039 (when (and org-tag-faces
(not org-tags-special-faces-re
))
5040 ;; tag faces set outside customize.... force initialization.
5041 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
5043 (org-set-local 'calc-embedded-open-mode
"# ")
5044 (modify-syntax-entry ?
@ "w")
5045 (modify-syntax-entry ?
\" "\"")
5046 (if org-startup-truncated
(setq truncate-lines t
))
5047 (org-set-local 'font-lock-unfontify-region-function
5048 'org-unfontify-region
)
5049 ;; Activate before-change-function
5050 (org-set-local 'org-table-may-need-update t
)
5051 (org-add-hook 'before-change-functions
'org-before-change-function nil
5053 ;; Check for running clock before killing a buffer
5054 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
5055 ;; Initialize macros templates.
5056 (org-macro-initialize-templates)
5057 ;; Initialize radio targets.
5058 (org-update-radio-target-regexp)
5060 (org-set-local 'indent-line-function
'org-indent-line
)
5061 (org-set-local 'indent-region-function
'org-indent-region
)
5062 ;; Filling and auto-filling.
5065 (org-setup-comments-handling)
5066 ;; Beginning/end of defun
5067 (org-set-local 'beginning-of-defun-function
'org-back-to-heading
)
5068 (org-set-local 'end-of-defun-function
(lambda () (interactive) (org-end-of-subtree nil t
)))
5069 ;; Next error for sparse trees
5070 (org-set-local 'next-error-function
'org-occur-next-match
)
5071 ;; Make sure dependence stuff works reliably, even for users who set it
5073 (if org-enforce-todo-dependencies
5074 (add-hook 'org-blocker-hook
5075 'org-block-todo-from-children-or-siblings-or-parent
)
5076 (remove-hook 'org-blocker-hook
5077 'org-block-todo-from-children-or-siblings-or-parent
))
5078 (if org-enforce-todo-checkbox-dependencies
5079 (add-hook 'org-blocker-hook
5080 'org-block-todo-from-checkboxes
)
5081 (remove-hook 'org-blocker-hook
5082 'org-block-todo-from-checkboxes
))
5084 ;; Align options lines
5086 'align-mode-rules-list
5087 '((org-in-buffer-settings
5088 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
5089 (modes .
'(org-mode)))))
5092 (org-set-local 'imenu-create-index-function
5093 'org-imenu-get-tree
)
5095 ;; Make isearch reveal context
5096 (if (or (featurep 'xemacs
)
5097 (not (boundp 'outline-isearch-open-invisible-function
)))
5098 ;; Emacs 21 and XEmacs make use of the hook
5099 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
5100 ;; Emacs 22 deals with this through a special variable
5101 (org-set-local 'outline-isearch-open-invisible-function
5102 (lambda (&rest ignore
) (org-show-context 'isearch
))))
5104 ;; Turn on org-beamer-mode?
5105 (and org-startup-with-beamer-mode
(org-beamer-mode))
5107 ;; Setup the pcomplete hooks
5108 (set (make-local-variable 'pcomplete-command-completion-function
)
5109 'org-pcomplete-initial
)
5110 (set (make-local-variable 'pcomplete-command-name-function
)
5111 'org-command-at-point
)
5112 (set (make-local-variable 'pcomplete-default-completion-function
)
5114 (set (make-local-variable 'pcomplete-parse-arguments-function
)
5115 'org-parse-arguments
)
5116 (set (make-local-variable 'pcomplete-termination-string
) "")
5117 (when (>= emacs-major-version
23)
5118 (set (make-local-variable 'buffer-face-mode-face
) 'org-default
))
5120 ;; If empty file that did not turn on org-mode automatically, make it to.
5121 (if (and org-insert-mode-line-in-empty-file
5122 (org-called-interactively-p 'any
)
5123 (= (point-min) (point-max)))
5124 (insert "# -*- mode: org -*-\n\n"))
5125 (unless org-inhibit-startup
5126 (when org-startup-align-all-tables
5127 (let ((bmp (buffer-modified-p)))
5128 (org-table-map-tables 'org-table-align
'quietly
)
5129 (set-buffer-modified-p bmp
)))
5130 (when org-startup-with-inline-images
5131 (org-display-inline-images))
5132 (when org-startup-indented
5133 (require 'org-indent
)
5134 (org-indent-mode 1))
5135 (unless org-inhibit-startup-visibility-stuff
5136 (org-set-startup-visibility)))
5137 ;; Try to set org-hide correctly
5138 (set-face-foreground 'org-hide
(org-find-invisible-foreground)))
5140 (when (fboundp 'abbrev-table-put
)
5141 (abbrev-table-put org-mode-abbrev-table
5142 :parents
(list text-mode-abbrev-table
)))
5144 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5147 (defun org-find-invisible-foreground ()
5148 (let ((candidates (remove
5151 (list (face-background 'default
)
5152 (face-background 'org-default
))
5155 (when (boundp alist
)
5156 (cdr (assoc 'background-color
(symbol-value alist
)))))
5157 '(default-frame-alist initial-frame-alist window-system-default-frame-alist
))
5158 (list (face-foreground 'org-hide
))))))
5159 (car (remove nil candidates
))))
5161 (defun org-current-time (&optional rounding-minutes
)
5162 "Current time, possibly rounded to ROUNDING-MINUTES.
5163 When ROUNDING-MINUTES is not an integer, fall back on the car of
5164 `org-time-stamp-rounding-minutes'."
5165 (let ((r (or (and (integerp rounding-minutes
) rounding-minutes
)
5166 (car org-time-stamp-rounding-minutes
)))
5167 (time (decode-time)))
5170 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5175 "Return today date, considering `org-extend-today-until'."
5177 (time-subtract (current-time)
5178 (list 0 (* 3600 org-extend-today-until
) 0))))
5180 ;;;; Font-Lock stuff, including the activators
5182 (defvar org-mouse-map
(make-sparse-keymap))
5183 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5184 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5185 (when org-mouse-1-follows-link
5186 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5187 (when org-tab-follows-link
5188 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5189 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5191 (require 'font-lock
)
5193 (defconst org-non-link-chars
"]\t\n\r<>")
5194 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5195 "shell" "elisp" "doi" "message"))
5196 (defvar org-link-types-re nil
5197 "Matches a link that has a url-like prefix like \"http:\"")
5198 (defvar org-link-re-with-space nil
5199 "Matches a link with spaces, optional angular brackets around it.")
5200 (defvar org-link-re-with-space2 nil
5201 "Matches a link with spaces, optional angular brackets around it.")
5202 (defvar org-link-re-with-space3 nil
5203 "Matches a link with spaces, only for internal part in bracket links.")
5204 (defvar org-angle-link-re nil
5205 "Matches link with angular brackets, spaces are allowed.")
5206 (defvar org-plain-link-re nil
5207 "Matches plain link, without spaces.")
5208 (defvar org-bracket-link-regexp nil
5209 "Matches a link in double brackets.")
5210 (defvar org-bracket-link-analytic-regexp nil
5211 "Regular expression used to analyze links.
5212 Here is what the match groups contain after a match:
5218 (defvar org-bracket-link-analytic-regexp
++ nil
5219 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5220 (defvar org-any-link-re nil
5221 "Regular expression matching any link.")
5223 (defcustom org-match-sexp-depth
3
5224 "Number of stacked braces for sub/superscript matching.
5225 This has to be set before loading org.el to be effective."
5226 :group
'org-export-translation
; ??????????????????????????/
5229 (defun org-create-multibrace-regexp (left right n
)
5230 "Create a regular expression which will match a balanced sexp.
5231 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5232 as single character strings.
5233 The regexp returned will match the entire expression including the
5234 delimiters. It will also define a single group which contains the
5235 match except for the outermost delimiters. The maximum depth of
5236 stacked delimiters is N. Escaping delimiters is not possible."
5237 (let* ((nothing (concat "[^" left right
"]*?"))
5240 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5243 re
(concat re or next
)
5244 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5245 (concat left
"\\(" re
"\\)" right
)))
5247 (defvar org-match-substring-regexp
5249 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5250 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5252 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5254 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5255 "The regular expression matching a sub- or superscript.")
5257 (defvar org-match-substring-with-braces-regexp
5259 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5260 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5262 "The regular expression matching a sub- or superscript, forcing braces.")
5264 (defun org-make-link-regexps ()
5265 "Update the link regular expressions.
5266 This should be called after the variable `org-link-types' has changed."
5267 (setq org-link-types-re
5269 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
5270 org-link-re-with-space
5272 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5273 "\\([^" org-non-link-chars
" ]"
5274 "[^" org-non-link-chars
"]*"
5275 "[^" org-non-link-chars
" ]\\)>?")
5276 org-link-re-with-space2
5278 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5279 "\\([^" org-non-link-chars
" ]"
5281 "[^" org-non-link-chars
" ]\\)>?")
5282 org-link-re-with-space3
5284 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5285 "\\([^" org-non-link-chars
" ]"
5289 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5290 "\\([^" org-non-link-chars
" ]"
5291 "[^" org-non-link-chars
"]*"
5295 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5296 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5297 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5298 org-bracket-link-regexp
5299 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5300 org-bracket-link-analytic-regexp
5303 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
5306 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5308 org-bracket-link-analytic-regexp
++
5311 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
5314 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5317 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5318 org-angle-link-re
"\\)\\|\\("
5319 org-plain-link-re
"\\)")))
5321 (org-make-link-regexps)
5323 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5324 "Regular expression for fast time stamp matching.")
5325 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5326 "Regular expression for fast time stamp matching.")
5327 (defconst org-ts-regexp0
5328 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5329 "Regular expression matching time strings for analysis.
5330 This one does not require the space after the date, so it can be used
5331 on a string that terminates immediately after the date.")
5332 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5333 "Regular expression matching time strings for analysis.")
5334 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5335 "Regular expression matching time stamps, with groups.")
5336 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5337 "Regular expression matching time stamps (also [..]), with groups.")
5338 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5339 "Regular expression matching a time stamp range.")
5340 (defconst org-tr-regexp-both
5341 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5342 "Regular expression matching a time stamp range.")
5343 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5344 org-ts-regexp
"\\)?")
5345 "Regular expression matching a time stamp or time stamp range.")
5346 (defconst org-tsr-regexp-both
(concat org-ts-regexp-both
"\\(--?-?"
5347 org-ts-regexp-both
"\\)?")
5348 "Regular expression matching a time stamp or time stamp range.
5349 The time stamps may be either active or inactive.")
5351 (defvar org-emph-face nil
)
5353 (defun org-do-emphasis-faces (limit)
5354 "Run through the buffer and add overlays to emphasized strings."
5356 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5357 (if (not (= (char-after (match-beginning 3))
5358 (char-after (match-beginning 4))))
5361 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5362 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5366 (org-remove-flyspell-overlays-in
5367 (match-beginning 0) (match-end 0)))
5368 (add-text-properties (match-beginning 2) (match-end 2)
5369 '(font-lock-multiline t org-emphasis t
))
5370 (when org-hide-emphasis-markers
5371 (add-text-properties (match-end 4) (match-beginning 5)
5372 '(invisible org-link
))
5373 (add-text-properties (match-beginning 3) (match-end 3)
5374 '(invisible org-link
)))))
5378 (defun org-emphasize (&optional char
)
5379 "Insert or change an emphasis, i.e. a font like bold or italic.
5380 If there is an active region, change that region to a new emphasis.
5381 If there is no region, just insert the marker characters and position
5382 the cursor between them.
5383 CHAR should be either the marker character, or the first character of the
5384 HTML tag associated with that emphasis. If CHAR is a space, the means
5385 to remove the emphasis of the selected region.
5386 If char is not given (for example in an interactive call) it
5387 will be prompted for."
5389 (let ((eal org-emphasis-alist
) e det
5390 (erc org-emphasis-regexp-components
)
5392 (string "") beg end move tag c s
)
5393 (if (org-region-active-p)
5394 (setq beg
(region-beginning) end
(region-end)
5395 string
(buffer-substring beg end
))
5398 (while (setq e
(pop eal
))
5399 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
5401 (push (cons c
(string-to-char (car e
))) det
)
5402 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
5403 (substring tag
1)))))
5404 (setq det
(nreverse det
))
5406 (message "%s" (concat "Emphasis marker or tag:" prompt
))
5407 (setq char
(read-char-exclusive)))
5408 (setq char
(or (cdr (assoc char det
)) char
))
5409 (if (equal char ?\
)
5410 (setq s
"" move nil
)
5411 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5412 (error "No such emphasis marker: \"%c\"" char
))
5413 (setq s
(char-to-string char
)))
5414 (while (and (> (length string
) 1)
5415 (equal (substring string
0 1) (substring string -
1))
5416 (assoc (substring string
0 1) org-emphasis-alist
))
5417 (setq string
(substring string
1 -
1)))
5418 (setq string
(concat s string s
))
5419 (if beg
(delete-region beg end
))
5421 (string-match (concat "[" (nth 0 erc
) "\n]")
5422 (char-to-string (char-before (point)))))
5425 (string-match (concat "[" (nth 1 erc
) "\n]")
5426 (char-to-string (char-after (point)))))
5427 (insert " ") (backward-char 1))
5429 (and move
(backward-char 1))))
5431 (defconst org-nonsticky-props
5432 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link
))
5434 (defsubst org-rear-nonsticky-at
(pos)
5435 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5437 (defun org-activate-plain-links (limit)
5438 "Run through the buffer and add overlays to links."
5441 (when (and (re-search-forward (concat org-plain-link-re
) limit t
)
5442 (not (org-in-src-block-p)))
5443 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5444 (setq f
(get-text-property (match-beginning 0) 'face
))
5445 (setq hl
(org-match-string-no-properties 0))
5446 (if (or (eq f
'org-tag
)
5447 (and (listp f
) (memq 'org-tag f
)))
5449 (add-text-properties (match-beginning 0) (match-end 0)
5450 (list 'mouse-face
'highlight
5452 'htmlize-link
`(:uri
,hl
)
5453 'keymap org-mouse-map
))
5454 (org-rear-nonsticky-at (match-end 0)))
5457 (defun org-activate-code (limit)
5458 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5460 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5461 (remove-text-properties (match-beginning 0) (match-end 0)
5462 '(display t invisible t intangible t
))
5465 (defcustom org-src-fontify-natively nil
5466 "When non-nil, fontify code in code blocks."
5469 :group
'org-appearance
5472 (defcustom org-allow-promoting-top-level-subtree nil
5473 "When non-nil, allow promoting a top level subtree.
5474 The leading star of the top level headline will be replaced
5478 :group
'org-appearance
)
5480 (defun org-fontify-meta-lines-and-blocks (limit)
5482 (org-fontify-meta-lines-and-blocks-1 limit
)
5483 (error (message "org-mode fontification error"))))
5485 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5486 "Fontify #+ lines and blocks, in the correct ways."
5487 (let ((case-fold-search t
))
5488 (if (re-search-forward
5489 "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5491 (let ((beg (match-beginning 0))
5492 (block-start (match-end 0))
5494 (lang (match-string 7))
5495 (beg1 (line-beginning-position 2))
5496 (dc1 (downcase (match-string 2)))
5497 (dc3 (downcase (match-string 3)))
5498 end end1 quoting block-type ovl
)
5500 ((member dc1
'("+html:" "+ascii:" "+latex:" "+docbook:"))
5501 ;; a single line of backend-specific content
5502 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5503 (remove-text-properties (match-beginning 0) (match-end 0)
5504 '(display t invisible t intangible t
))
5505 (add-text-properties (match-beginning 1) (match-end 3)
5506 '(font-lock-fontified t face org-meta-line
))
5507 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5508 '(font-lock-fontified t face org-block
))
5509 ; for backend-specific code
5511 ((and (match-end 4) (equal dc3
"+begin"))
5513 (setq block-type
(downcase (match-string 5))
5514 quoting
(member block-type org-protecting-blocks
))
5515 (when (re-search-forward
5516 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5517 nil t
) ;; on purpose, we look further than LIMIT
5518 (setq end
(min (point-max) (match-end 0))
5519 end1
(min (point-max) (1- (match-beginning 0))))
5520 (setq block-end
(match-beginning 0))
5522 (remove-text-properties beg end
5523 '(display t invisible t intangible t
)))
5524 (add-text-properties
5526 '(font-lock-fontified t font-lock-multiline t
))
5527 (add-text-properties beg beg1
'(face org-meta-line
))
5528 (add-text-properties end1
(min (point-max) (1+ end
))
5529 '(face org-meta-line
)) ; for end_src
5531 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5532 (org-src-font-lock-fontify-block lang block-start block-end
)
5533 ;; remove old background overlays
5535 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5536 (delete-overlay ov
)))
5537 (overlays-at (/ (+ beg1 block-end
) 2)))
5538 ;; add a background overlay
5539 (setq ovl
(make-overlay beg1 block-end
))
5540 (overlay-put ovl
'face
'org-block-background
)
5541 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5543 (add-text-properties beg1
(min (point-max) (1+ end1
))
5544 '(face org-block
))) ; end of source block
5545 ((not org-fontify-quote-and-verse-blocks
))
5546 ((string= block-type
"quote")
5547 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-quote
)))
5548 ((string= block-type
"verse")
5549 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-verse
))))
5550 (add-text-properties beg beg1
'(face org-block-begin-line
))
5551 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5552 '(face org-block-end-line
))
5554 ((member dc1
'("+title:" "+author:" "+email:" "+date:"))
5555 (add-text-properties
5557 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5558 '(font-lock-fontified t invisible t
)
5559 '(font-lock-fontified t face org-document-info-keyword
)))
5560 (add-text-properties
5561 (match-beginning 6) (match-end 6)
5562 (if (string-equal dc1
"+title:")
5563 '(font-lock-fontified t face org-document-title
)
5564 '(font-lock-fontified t face org-document-info
))))
5565 ((or (equal dc1
"+results")
5566 (member dc1
'("+begin:" "+end:" "+caption:" "+label:"
5567 "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
5568 "+call:" "+header:" "+headers:" "+name:"))
5569 (and (match-end 4) (equal dc3
"+attr")))
5570 (add-text-properties
5572 '(font-lock-fontified t face org-meta-line
))
5574 ((member dc3
'(" " ""))
5575 (add-text-properties
5577 '(font-lock-fontified t face font-lock-comment-face
)))
5578 ((not (member (char-after beg
) '(?\ ?
\t)))
5579 ;; just any other in-buffer setting, but not indented
5580 (add-text-properties
5582 '(font-lock-fontified t face org-meta-line
))
5586 (defun org-activate-angle-links (limit)
5587 "Run through the buffer and add overlays to links."
5588 (if (and (re-search-forward org-angle-link-re limit t
)
5589 (not (org-in-src-block-p)))
5591 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5592 (add-text-properties (match-beginning 0) (match-end 0)
5593 (list 'mouse-face
'highlight
5594 'keymap org-mouse-map
))
5595 (org-rear-nonsticky-at (match-end 0))
5598 (defun org-activate-footnote-links (limit)
5599 "Run through the buffer and add overlays to footnotes."
5600 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5602 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5603 (org-remove-flyspell-overlays-in beg end
)
5604 (add-text-properties beg end
5605 (list 'mouse-face
'highlight
5606 'keymap org-mouse-map
5608 (if (= (point-at-bol) beg
)
5609 "Footnote definition"
5610 "Footnote reference")
5611 'font-lock-fontified t
5612 'font-lock-multiline t
5613 'face
'org-footnote
))))))
5615 (defun org-activate-bracket-links (limit)
5616 "Run through the buffer and add overlays to bracketed links."
5617 (if (and (re-search-forward org-bracket-link-regexp limit t
)
5618 (not (org-in-src-block-p)))
5619 (let* ((hl (org-match-string-no-properties 1))
5620 (help (concat "LINK: " hl
))
5621 ;; FIXME: Above we should remove the escapes. But that
5622 ;; requires another match, protecting match data, a lot
5623 ;; of overhead for font-lock.
5624 (ip (org-maybe-intangible
5625 (list 'invisible
'org-link
5626 'keymap org-mouse-map
'mouse-face
'highlight
5627 'font-lock-multiline t
'help-echo help
5628 'htmlize-link
`(:uri
,hl
))))
5629 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5630 'font-lock-multiline t
'help-echo help
5631 'htmlize-link
`(:uri
,hl
))))
5632 ;; We need to remove the invisible property here. Table narrowing
5633 ;; may have made some of this invisible.
5634 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5635 (remove-text-properties (match-beginning 0) (match-end 0)
5639 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5640 (org-rear-nonsticky-at (match-beginning 3))
5641 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5642 (org-rear-nonsticky-at (match-end 3))
5643 (add-text-properties (match-end 3) (match-end 0) ip
)
5644 (org-rear-nonsticky-at (match-end 0)))
5645 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5646 (org-rear-nonsticky-at (match-beginning 1))
5647 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5648 (org-rear-nonsticky-at (match-end 1))
5649 (add-text-properties (match-end 1) (match-end 0) ip
)
5650 (org-rear-nonsticky-at (match-end 0)))
5653 (defun org-activate-dates (limit)
5654 "Run through the buffer and add overlays to dates."
5655 (if (re-search-forward org-tsr-regexp-both limit t
)
5657 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5658 (add-text-properties (match-beginning 0) (match-end 0)
5659 (list 'mouse-face
'highlight
5660 'keymap org-mouse-map
))
5661 (org-rear-nonsticky-at (match-end 0))
5662 (when org-display-custom-times
5664 (org-display-custom-time (match-beginning 3) (match-end 3)))
5665 (org-display-custom-time (match-beginning 1) (match-end 1)))
5668 (defvar org-target-link-regexp nil
5669 "Regular expression matching radio targets in plain text.")
5670 (make-variable-buffer-local 'org-target-link-regexp
)
5671 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5672 "Regular expression matching a link target.")
5673 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5674 "Regular expression matching a radio target.")
5675 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5676 "Regular expression matching any target.")
5678 (defun org-activate-target-links (limit)
5679 "Run through the buffer and add overlays to target matches."
5680 (when org-target-link-regexp
5681 (let ((case-fold-search t
))
5682 (if (re-search-forward org-target-link-regexp limit t
)
5684 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5685 (add-text-properties (match-beginning 0) (match-end 0)
5686 (list 'mouse-face
'highlight
5687 'keymap org-mouse-map
5688 'help-echo
"Radio target link"
5689 'org-linked-text t
))
5690 (org-rear-nonsticky-at (match-end 0))
5693 (defun org-update-radio-target-regexp ()
5694 "Find all radio targets in this file and update the regular expression."
5696 (when (memq 'radio org-activate-links
)
5697 (setq org-target-link-regexp
5698 (org-make-target-link-regexp (org-all-targets 'radio
)))
5699 (org-restart-font-lock)))
5701 (defun org-hide-wide-columns (limit)
5703 (setq s
(text-property-any (point) (or limit
(point-max))
5706 (setq e
(next-single-property-change s
'org-cwidth
))
5707 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5711 (defvar org-latex-and-specials-regexp nil
5712 "Regular expression for highlighting export special stuff.")
5713 (defvar org-match-substring-regexp
)
5714 (defvar org-match-substring-with-braces-regexp
)
5716 ;; This should be with the exporter code, but we also use if for font-locking
5717 (defconst org-export-html-special-string-regexps
5718 '(("\\\\-" .
"­")
5719 ("---\\([^-]\\)" .
"—\\1")
5720 ("--\\([^-]\\)" .
"–\\1")
5721 ("\\.\\.\\." .
"…"))
5722 "Regular expressions for special string conversion.")
5725 (defun org-compute-latex-and-specials-regexp ()
5726 "Compute regular expression for stuff treated specially by exporters."
5727 (if (not org-highlight-latex-fragments-and-specials
)
5728 (org-set-local 'org-latex-and-specials-regexp nil
)
5731 ((matchers (plist-get org-format-latex-options
:matchers
))
5732 (latexs (delq nil
(mapcar (lambda (x) (if (member (car x
) matchers
) x
))
5733 org-latex-regexps
)))
5734 (org-export-allow-BIND nil
)
5735 (options (org-combine-plists (org-default-export-plist)
5736 (org-infile-export-plist)))
5737 (org-export-with-sub-superscripts (plist-get options
:sub-superscript
))
5738 (org-export-with-LaTeX-fragments (plist-get options
:LaTeX-fragments
))
5739 (org-export-with-TeX-macros (plist-get options
:TeX-macros
))
5740 (org-export-html-expand (plist-get options
:expand-quoted-html
))
5741 (org-export-with-special-strings (plist-get options
:special-strings
))
5744 ((equal org-export-with-sub-superscripts
'{})
5745 (list org-match-substring-with-braces-regexp
))
5746 (org-export-with-sub-superscripts
5747 (list org-match-substring-regexp
))))
5749 (if org-export-with-LaTeX-fragments
5750 (mapcar (lambda (x) (nth 1 x
)) latexs
)))
5752 (if org-export-with-TeX-macros
5753 (list (concat "\\\\"
5759 (append org-entities-user
5761 (if (boundp 'org-latex-entities
)
5763 (or (car-safe x
) x
))
5768 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5769 (re-special (if org-export-with-special-strings
5770 (mapcar (lambda (x) (car x
))
5771 org-export-html-special-string-regexps
)))
5775 (if org-export-html-expand
"@<[^>\n]+>")
5778 'org-latex-and-specials-regexp
5779 (mapconcat 'identity
(append re-latex re-sub re-macros re-special
5782 (defun org-do-latex-and-special-faces (limit)
5783 "Run through the buffer and add overlays to links."
5784 (when org-latex-and-specials-regexp
5786 (while (and (not rtn
) (re-search-forward org-latex-and-specials-regexp
5788 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5790 '(org-code org-verbatim underline
)))
5793 d
(cond ((member (char-after (1+ (match-beginning 0)))
5796 (font-lock-prepend-text-property
5797 (+ d
(match-beginning 0)) (match-end 0)
5798 'face
'org-latex-and-export-specials
)
5799 (add-text-properties (+ d
(match-beginning 0)) (match-end 0)
5800 '(font-lock-multiline t
)))))
5803 (defun org-restart-font-lock ()
5804 "Restart `font-lock-mode', to force refontification."
5805 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5807 (font-lock-mode 1)))
5809 (defun org-all-targets (&optional radio
)
5810 "Return a list of all targets in this file.
5811 When optional argument RADIO is non-nil, only find radio
5813 (let ((re (if radio org-radio-target-regexp org-target-regexp
)) rtn
)
5815 (goto-char (point-min))
5816 (while (re-search-forward re nil t
)
5817 ;; Make sure point is really within the object.
5819 (let ((obj (org-element-context)))
5820 (when (memq (org-element-type obj
) '(radio-target target
))
5821 (add-to-list 'rtn
(downcase (org-element-property :value obj
))))))
5824 (defun org-make-target-link-regexp (targets)
5825 "Make regular expression matching all strings in TARGETS.
5826 The regular expression finds the targets also if there is a line break
5833 (setq x
(regexp-quote x
))
5834 (while (string-match " +" x
)
5835 (setq x
(replace-match "\\s-+" t t x
)))
5841 (defun org-activate-tags (limit)
5842 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5844 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5845 (add-text-properties (match-beginning 1) (match-end 1)
5846 (list 'mouse-face
'highlight
5847 'keymap org-mouse-map
))
5848 (org-rear-nonsticky-at (match-end 1))
5851 (defun org-outline-level ()
5852 "Compute the outline level of the heading at point.
5853 If this is called at a normal headline, the level is the number of stars.
5854 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
5856 (if (not (condition-case nil
5857 (org-back-to-heading t
)
5860 (looking-at org-outline-regexp
)
5861 (1- (- (match-end 0) (match-beginning 0))))))
5863 (defvar org-font-lock-keywords nil
)
5865 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5866 "Regular expression matching a property line.")
5868 (defvar org-font-lock-hook nil
5869 "Functions to be called for special font lock stuff.")
5871 (defvar org-font-lock-set-keywords-hook nil
5872 "Functions that can manipulate `org-font-lock-extra-keywords'.
5873 This is called after `org-font-lock-extra-keywords' is defined, but before
5874 it is installed to be used by font lock. This can be useful if something
5875 needs to be inserted at a specific position in the font-lock sequence.")
5877 (defun org-font-lock-hook (limit)
5878 "Run `org-font-lock-hook' within LIMIT."
5879 (run-hook-with-args 'org-font-lock-hook limit
))
5881 (defun org-set-font-lock-defaults ()
5882 "Set font lock defaults for the current buffer."
5883 (let* ((em org-fontify-emphasized-text
)
5884 (lk org-activate-links
)
5885 (org-font-lock-extra-keywords
5888 '(org-font-lock-hook)
5890 `(,(if org-fontify-whole-heading-line
5891 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5892 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5893 (1 (org-get-level-face 1))
5894 (2 (org-get-level-face 2))
5895 (3 (org-get-level-face 3)))
5897 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5900 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
5901 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
5902 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
5903 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
5905 (list org-drawer-regexp
'(0 'org-special-keyword t
))
5906 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
5908 (list org-property-re
5909 '(1 'org-special-keyword t
)
5910 '(3 'org-property-value t
))
5912 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
5913 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
5914 (if (memq 'plain lk
) '(org-activate-plain-links))
5915 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
5916 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
5917 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
5918 (if (memq 'footnote lk
) '(org-activate-footnote-links))
5919 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
5920 '(org-hide-wide-columns (0 nil append
))
5922 (list (format org-heading-keyword-regexp-format
5924 '(2 (org-get-todo-face 2) t
))
5926 (if org-fontify-done-headline
5927 (list (format org-heading-keyword-regexp-format
5930 (mapconcat 'regexp-quote org-done-keywords
"\\|")
5932 '(2 'org-headline-done t
))
5935 '(org-font-lock-add-priority-faces)
5937 '(org-font-lock-add-tag-faces)
5939 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
5940 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
5941 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
5942 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
5945 (if (featurep 'xemacs
)
5946 '(org-do-emphasis-faces (0 nil append
))
5947 '(org-do-emphasis-faces)))
5949 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
5950 1 'org-checkbox prepend
)
5951 (if (cdr (assq 'checkbox org-list-automatic-rules
))
5952 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5953 (0 (org-get-checkbox-statistics-face) t
)))
5954 ;; Description list items
5955 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
5956 1 'org-list-dt prepend
)
5957 ;; ARCHIVEd headings
5959 org-outline-regexp-bol
5960 "\\(.*:" org-archive-tag
":.*\\)")
5961 '(1 'org-archived prepend
))
5963 '(org-do-latex-and-special-faces)
5964 '(org-fontify-entities)
5965 '(org-raise-scripts)
5967 '(org-activate-code (1 'org-code t
))
5969 (list (format org-heading-keyword-regexp-format
5971 org-comment-string
"\\|" org-quote-string
5973 '(2 'org-special-keyword t
))
5974 ;; Blocks and meta lines
5975 '(org-fontify-meta-lines-and-blocks)
5977 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
5978 (run-hooks 'org-font-lock-set-keywords-hook
)
5979 ;; Now set the full font-lock-keywords
5980 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
5981 (org-set-local 'font-lock-defaults
5982 '(org-font-lock-keywords t nil nil backward-paragraph
))
5983 (kill-local-variable 'font-lock-keywords
) nil
))
5985 (defun org-toggle-pretty-entities ()
5986 "Toggle the composition display of entities as UTF8 characters."
5988 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
5989 (org-restart-font-lock)
5990 (if org-pretty-entities
5991 (message "Entities are displayed as UTF8 characters")
5994 (org-decompose-region (point-min) (point-max))
5995 (message "Entities are displayed plain"))))
5997 (defvar org-custom-properties-overlays nil
5998 "List of overlays used for custom properties.")
5999 (make-variable-buffer-local 'org-custom-properties-overlays
)
6001 (defun org-toggle-custom-properties-visibility ()
6002 "Display or hide properties in `org-custom-properties'."
6004 (if org-custom-properties-overlays
6005 (progn (mapc 'delete-overlay org-custom-properties-overlays
)
6006 (setq org-custom-properties-overlays nil
))
6007 (unless (not org-custom-properties
)
6011 (goto-char (point-min))
6012 (while (re-search-forward org-property-re nil t
)
6014 (when (equal p
(substring (match-string 1) 1 -
1))
6015 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
6016 (overlay-put o
'invisible t
)
6017 (overlay-put o
'org-custom-property t
)
6018 (push o org-custom-properties-overlays
))))
6019 org-custom-properties
)))))))
6021 (defun org-fontify-entities (limit)
6022 "Find an entity to fontify."
6024 (when org-pretty-entities
6026 (while (re-search-forward
6027 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
6029 (if (and (not (org-in-indented-comment-line))
6030 (setq ee
(org-entity-get (match-string 1)))
6031 (= (length (nth 6 ee
)) 1))
6033 ((end (if (equal (match-string 2) "{}")
6036 (add-text-properties
6037 (match-beginning 0) end
6038 (list 'font-lock-fontified t
))
6039 (compose-region (match-beginning 0) end
6045 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
6046 "Fontify string S like in Org-mode."
6049 (let ((org-odd-levels-only odd-levels
))
6051 (font-lock-fontify-buffer)
6057 (defun org-get-level-face (n)
6058 "Get the right face for match N in font-lock matching of headlines."
6059 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
6060 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
6061 (if org-cycle-level-faces
6062 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
6063 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
6065 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
6067 (t (if org-level-color-stars-only nil org-f
))))
6070 (defun org-get-todo-face (kwd)
6071 "Get the right face for a TODO keyword KWD.
6072 If KWD is a number, get the corresponding match group."
6073 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6074 (or (org-face-from-face-or-color
6075 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
6076 (and (member kwd org-done-keywords
) 'org-done
)
6079 (defun org-face-from-face-or-color (context inherit face-or-color
)
6080 "Create a face list that inherits INHERIT, but sets the foreground color.
6081 When FACE-OR-COLOR is not a string, just return it."
6082 (if (stringp face-or-color
)
6083 (list :inherit inherit
6084 (cdr (assoc context org-faces-easy-properties
))
6088 (defun org-font-lock-add-tag-faces (limit)
6089 "Add the special tag faces."
6090 (when (and org-tag-faces org-tags-special-faces-re
)
6091 (while (re-search-forward org-tags-special-faces-re limit t
)
6092 (add-text-properties (match-beginning 1) (match-end 1)
6093 (list 'face
(org-get-tag-face 1)
6094 'font-lock-fontified t
))
6095 (backward-char 1))))
6097 (defun org-font-lock-add-priority-faces (limit)
6098 "Add the special priority faces."
6099 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
6100 (when (save-match-data (org-at-heading-p))
6101 (add-text-properties
6102 (match-beginning 0) (match-end 0)
6103 (list 'face
(or (org-face-from-face-or-color
6104 'priority
'org-special-keyword
6105 (cdr (assoc (char-after (match-beginning 1))
6106 org-priority-faces
)))
6107 'org-special-keyword
)
6108 'font-lock-fontified t
)))))
6110 (defun org-get-tag-face (kwd)
6111 "Get the right face for a TODO keyword KWD.
6112 If KWD is a number, get the corresponding match group."
6113 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6114 (or (org-face-from-face-or-color
6115 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
6118 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
6119 "Remove fontification and activation overlays from links."
6120 (font-lock-default-unfontify-region beg end
)
6121 (let* ((buffer-undo-list t
)
6122 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
6123 (inhibit-modification-hooks t
)
6124 deactivate-mark buffer-file-name buffer-file-truename
)
6125 (org-decompose-region beg end
)
6126 (remove-text-properties beg end
6127 '(mouse-face t keymap t org-linked-text t
6128 invisible t intangible t
6129 org-no-flyspell t org-emphasis t
))
6130 (org-remove-font-lock-display-properties beg end
)))
6132 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
6133 ((raise 0.3) (height 0.7))
6136 "Display properties for showing superscripts and subscripts.")
6138 (defun org-remove-font-lock-display-properties (beg end
)
6139 "Remove specific display properties that have been added by font lock.
6140 The will remove the raise properties that are used to show superscripts
6144 (setq next
(next-single-property-change beg
'display nil end
)
6145 prop
(get-text-property beg
'display
))
6146 (if (member prop org-script-display
)
6147 (put-text-property beg next
'display nil
))
6150 (defun org-raise-scripts (limit)
6151 "Add raise properties to sub/superscripts."
6152 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
6153 (if (re-search-forward
6154 (if (eq org-use-sub-superscripts t
)
6155 org-match-substring-regexp
6156 org-match-substring-with-braces-regexp
)
6158 (let* ((pos (point)) table-p comment-p
6159 (mpos (match-beginning 3))
6160 (emph-p (get-text-property mpos
'org-emphasis
))
6161 (link-p (get-text-property mpos
'mouse-face
))
6162 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6163 (goto-char (point-at-bol))
6164 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
6165 comment-p
(org-looking-at-p "[ \t]*#"))
6167 ;; FIXME: Should we go back one character here, for a_b^c
6168 ;; (goto-char (1- pos)) ;????????????????????
6169 (if (or comment-p emph-p link-p keyw-p
)
6171 (put-text-property (match-beginning 3) (match-end 0)
6173 (if (equal (char-after (match-beginning 2)) ?^
)
6174 (nth (if table-p
3 1) org-script-display
)
6175 (nth (if table-p
2 0) org-script-display
)))
6176 (add-text-properties (match-beginning 2) (match-end 2)
6178 'org-dwidth t
'org-dwidth-n
1))
6179 (if (and (eq (char-after (match-beginning 3)) ?
{)
6180 (eq (char-before (match-end 3)) ?
}))
6182 (add-text-properties
6183 (match-beginning 3) (1+ (match-beginning 3))
6184 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6185 (add-text-properties
6186 (1- (match-end 3)) (match-end 3)
6187 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6190 ;;;; Visibility cycling, including org-goto and indirect buffer
6194 (defvar org-cycle-global-status nil
)
6195 (make-variable-buffer-local 'org-cycle-global-status
)
6196 (defvar org-cycle-subtree-status nil
)
6197 (make-variable-buffer-local 'org-cycle-subtree-status
)
6199 (defvar org-inlinetask-min-level
)
6202 (defun org-cycle (&optional arg
)
6203 "TAB-action and visibility cycling for Org-mode.
6205 This is the command invoked in Org-mode by the TAB key. Its main purpose
6206 is outline visibility cycling, but it also invokes other actions
6207 in special contexts.
6209 - When this function is called with a prefix argument, rotate the entire
6210 buffer through 3 states (global cycling)
6211 1. OVERVIEW: Show only top-level headlines.
6212 2. CONTENTS: Show all headlines of all levels, but no body text.
6213 3. SHOW ALL: Show everything.
6214 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6215 determined by the variable `org-startup-folded', and by any VISIBILITY
6216 properties in the buffer.
6217 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6218 including any drawers.
6220 - When inside a table, re-align the table and move to the next field.
6222 - When point is at the beginning of a headline, rotate the subtree started
6223 by this line through 3 different states (local cycling)
6224 1. FOLDED: Only the main headline is shown.
6225 2. CHILDREN: The main headline and the direct children are shown.
6226 From this state, you can move to one of the children
6227 and zoom in further.
6228 3. SUBTREE: Show the entire subtree, including body text.
6229 If there is no subtree, switch directly from CHILDREN to FOLDED.
6231 - When point is at the beginning of an empty headline and the variable
6232 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6233 of the headline by demoting and promoting it to likely levels. This
6234 speeds up creation document structure by pressing TAB once or several
6235 times right after creating a new headline.
6237 - When there is a numeric prefix, go up to a heading with level ARG, do
6238 a `show-subtree' and return to the previous cursor position. If ARG
6239 is negative, go up that many levels.
6241 - When point is not at the beginning of a headline, execute the global
6242 binding for TAB, which is re-indenting the line. See the option
6243 `org-cycle-emulate-tab' for details.
6245 - Special case: if point is at the beginning of the buffer and there is
6246 no headline in line 1, this function will act as if called with prefix arg
6247 (C-u TAB, same as S-TAB) also when called without prefix arg.
6248 But only if also the variable `org-cycle-global-at-bob' is t."
6250 (org-load-modules-maybe)
6251 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6252 (and org-cycle-level-after-item
/entry-creation
6253 (or (org-cycle-level)
6254 (org-cycle-item-indentation))))
6255 (let* (message-log-max ; Don't populate the *Messages* buffer
6257 (or org-cycle-max-level
6258 (and (boundp 'org-inlinetask-min-level
)
6259 org-inlinetask-min-level
6260 (1- org-inlinetask-min-level
))))
6261 (nstars (and limit-level
6262 (if org-odd-levels-only
6263 (and limit-level
(1- (* limit-level
2)))
6266 (if (not (derived-mode-p 'org-mode
))
6268 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6269 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6270 (not (looking-at org-outline-regexp
))))
6273 (delq 'org-optimize-window-after-visibility-change
6274 (copy-sequence org-cycle-hook
))
6278 (if (or bob-special
(equal arg
'(4)))
6279 ;; special case: use global cycling
6285 (setq last-command
'dummy
)
6286 (org-set-startup-visibility)
6287 (message "Startup visibility, plus VISIBILITY properties"))
6291 (message "Entire buffer visible, including drawers"))
6293 ;; Table: enter it or move to the next field.
6294 ((org-at-table-p 'any
)
6295 (if (org-at-table.el-p
)
6296 (message "Use C-c ' to edit table.el tables")
6297 (if arg
(org-table-edit-field t
)
6298 (org-table-justify-field-maybe)
6299 (call-interactively 'org-table-next-field
))))
6301 ((run-hook-with-args-until-success
6302 'org-tab-after-check-for-table-hook
))
6304 ;; Global cycling: delegate to `org-cycle-internal-global'.
6305 ((eq arg t
) (org-cycle-internal-global))
6307 ;; Drawers: delegate to `org-flag-drawer'.
6308 ((and org-drawers org-drawer-regexp
6310 (beginning-of-line 1)
6311 (looking-at org-drawer-regexp
)))
6312 (org-flag-drawer ; toggle block visibility
6313 (not (get-char-property (match-end 0) 'invisible
))))
6315 ;; Show-subtree, ARG levels up from here.
6318 (org-back-to-heading)
6319 (outline-up-heading (if (< arg
0) (- arg
)
6320 (- (funcall outline-level
) arg
)))
6321 (org-show-subtree)))
6323 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6324 ((and (featurep 'org-inlinetask
)
6325 (org-inlinetask-at-task-p)
6326 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6327 (org-inlinetask-toggle-visibility))
6329 ((org-try-cdlatex-tab))
6331 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6332 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6333 (save-excursion (beginning-of-line 1)
6334 (looking-at org-outline-regexp
)))
6335 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6336 (org-cycle-internal-local))
6338 ;; From there: TAB emulation and template completion.
6339 (buffer-read-only (org-back-to-heading))
6341 ((run-hook-with-args-until-success
6342 'org-tab-after-check-for-cycling-hook
))
6344 ((org-try-structure-completion))
6346 ((run-hook-with-args-until-success
6347 'org-tab-before-tab-emulation-hook
))
6349 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6351 (not (looking-at org-outline-regexp
))))
6352 (call-interactively (global-key-binding "\t")))
6354 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6355 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6356 (or (and (eq org-cycle-emulate-tab
'white
)
6357 (= (match-end 0) (point-at-eol)))
6358 (and (eq org-cycle-emulate-tab
'whitestart
)
6359 (>= (match-end 0) pos
))))
6361 (eq org-cycle-emulate-tab t
))
6362 (call-interactively (global-key-binding "\t")))
6365 (org-back-to-heading)
6368 (defun org-cycle-internal-global ()
6369 "Do the global cycling action."
6370 ;; Hack to avoid display of messages for .org attachments in Gnus
6371 (let (message-log-max ; Don't populate the *Messages* buffer
6372 (ga (string-match "\\*fontification" (buffer-name))))
6374 ((and (eq last-command this-command
)
6375 (eq org-cycle-global-status
'overview
))
6376 ;; We just created the overview - now do table of contents
6377 ;; This can be slow in very large buffers, so indicate action
6378 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6379 (unless ga
(message "CONTENTS..."))
6381 (unless ga
(message "CONTENTS...done"))
6382 (setq org-cycle-global-status
'contents
)
6383 (run-hook-with-args 'org-cycle-hook
'contents
))
6385 ((and (eq last-command this-command
)
6386 (eq org-cycle-global-status
'contents
))
6387 ;; We just showed the table of contents - now show everything
6388 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6390 (unless ga
(message "SHOW ALL"))
6391 (setq org-cycle-global-status
'all
)
6392 (run-hook-with-args 'org-cycle-hook
'all
))
6395 ;; Default action: go to overview
6396 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6398 (unless ga
(message "OVERVIEW"))
6399 (setq org-cycle-global-status
'overview
)
6400 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6402 (defun org-cycle-internal-local ()
6403 "Do the local cycling action."
6404 (let (message-log-max ; Don't populate the *Messages* buffer
6405 (goal-column 0) eoh eol eos has-children children-skipped struct
)
6406 ;; First, determine end of headline (EOH), end of subtree or item
6407 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6412 (setq struct
(org-list-struct))
6413 (setq eoh
(point-at-eol))
6414 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6415 (setq has-children
(org-list-has-child-p (point) struct
)))
6416 (org-back-to-heading)
6417 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6418 (setq eos
(save-excursion (1- (org-end-of-subtree t t
))))
6421 (let ((level (funcall outline-level
)))
6422 (outline-next-heading)
6423 (and (org-at-heading-p t
)
6424 (> (funcall outline-level
) level
))))
6426 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6427 ;; Determine end invisible part of buffer (EOL)
6428 (beginning-of-line 2)
6429 ;; XEmacs doesn't have `next-single-char-property-change'
6430 (if (featurep 'xemacs
)
6431 (while (and (not (eobp)) ;; this is like `next-line'
6432 (get-char-property (1- (point)) 'invisible
))
6433 (beginning-of-line 2))
6434 (while (and (not (eobp)) ;; this is like `next-line'
6435 (get-char-property (1- (point)) 'invisible
))
6436 (goto-char (next-single-char-property-change (point) 'invisible
))
6437 (and (eolp) (beginning-of-line 2))))
6439 ;; Find out what to do next and set `this-command'
6442 ;; Nothing is hidden behind this heading
6443 (unless (org-before-first-heading-p)
6444 (run-hook-with-args 'org-pre-cycle-hook
'empty
))
6445 (message "EMPTY ENTRY")
6446 (setq org-cycle-subtree-status nil
)
6449 (outline-next-heading)
6450 (if (outline-invisible-p) (org-flag-heading nil
))))
6451 ((and (or (>= eol eos
)
6452 (not (string-match "\\S-" (buffer-substring eol eos
))))
6454 (not (setq children-skipped
6455 org-cycle-skip-children-state-if-no-children
))))
6456 ;; Entire subtree is hidden in one line: children view
6457 (unless (org-before-first-heading-p)
6458 (run-hook-with-args 'org-pre-cycle-hook
'children
))
6460 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6462 (org-with-limited-levels (show-children))
6463 ;; FIXME: This slows down the func way too much.
6464 ;; How keep drawers hidden in subtree anyway?
6465 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6466 ;; (org-cycle-hide-drawers 'subtree))
6468 ;; Fold every list in subtree to top-level items.
6469 (when (eq org-cycle-include-plain-lists
'integrate
)
6471 (org-back-to-heading)
6472 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6473 (beginning-of-line 1)
6474 (let* ((struct (org-list-struct))
6475 (prevs (org-list-prevs-alist struct
))
6476 (end (org-list-get-bottom-point struct
)))
6477 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6478 (org-list-get-all-items (point) struct prevs
))
6479 (goto-char end
))))))
6480 (message "CHILDREN")
6483 (outline-next-heading)
6484 (if (outline-invisible-p) (org-flag-heading nil
)))
6485 (setq org-cycle-subtree-status
'children
)
6486 (unless (org-before-first-heading-p)
6487 (run-hook-with-args 'org-cycle-hook
'children
)))
6488 ((or children-skipped
6489 (and (eq last-command this-command
)
6490 (eq org-cycle-subtree-status
'children
)))
6491 ;; We just showed the children, or no children are there,
6492 ;; now show everything.
6493 (unless (org-before-first-heading-p)
6494 (run-hook-with-args 'org-pre-cycle-hook
'subtree
))
6495 (outline-flag-region eoh eos nil
)
6496 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6497 (setq org-cycle-subtree-status
'subtree
)
6498 (unless (org-before-first-heading-p)
6499 (run-hook-with-args 'org-cycle-hook
'subtree
)))
6501 ;; Default action: hide the subtree.
6502 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6503 (outline-flag-region eoh eos t
)
6505 (setq org-cycle-subtree-status
'folded
)
6506 (unless (org-before-first-heading-p)
6507 (run-hook-with-args 'org-cycle-hook
'folded
))))))
6510 (defun org-global-cycle (&optional arg
)
6511 "Cycle the global visibility. For details see `org-cycle'.
6512 With \\[universal-argument] prefix arg, switch to startup visibility.
6513 With a numeric prefix, show all headlines up to that level."
6515 (let ((org-cycle-include-plain-lists
6516 (if (derived-mode-p 'org-mode
) org-cycle-include-plain-lists nil
)))
6520 (hide-sublevels arg
)
6521 (setq org-cycle-global-status
'contents
))
6523 (org-set-startup-visibility)
6524 (message "Startup visibility, plus VISIBILITY properties."))
6526 (org-cycle '(4))))))
6528 (defun org-set-startup-visibility ()
6529 "Set the visibility required by startup options and properties."
6531 ((eq org-startup-folded t
)
6533 ((eq org-startup-folded
'content
)
6534 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6535 (org-cycle '(4)) (org-cycle '(4)))))
6536 (unless (eq org-startup-folded
'showeverything
)
6537 (if org-hide-block-startup
(org-hide-block-all))
6538 (org-set-visibility-according-to-property 'no-cleanup
)
6539 (org-cycle-hide-archived-subtrees 'all
)
6540 (org-cycle-hide-drawers 'all
)
6541 (org-cycle-show-empty-lines t
)))
6543 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6544 "Switch subtree visibilities according to :VISIBILITY: property."
6546 (let (org-show-entry-below state
)
6548 (goto-char (point-min))
6549 (while (re-search-forward
6550 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6552 (setq state
(match-string 1))
6554 (org-back-to-heading t
)
6558 ((equal state
'("fold" "folded"))
6560 ((equal state
"children")
6561 (org-show-hidden-entry)
6563 ((equal state
"content")
6566 (org-narrow-to-subtree)
6568 ((member state
'("all" "showall"))
6571 (org-cycle-hide-archived-subtrees 'all
)
6572 (org-cycle-hide-drawers 'all
)
6573 (org-cycle-show-empty-lines 'all
)))))
6575 ;; This function uses outline-regexp instead of the more fundamental
6576 ;; org-outline-regexp so that org-cycle-global works outside of Org
6577 ;; buffers, where outline-regexp is needed.
6578 (defun org-overview ()
6579 "Switch to overview mode, showing only top-level headlines.
6580 Really, this shows all headlines with level equal or greater than the level
6581 of the first headline in the buffer. This is important, because if the
6582 first headline is not level one, then (hide-sublevels 1) gives confusing
6585 (let ((level (save-excursion
6586 (goto-char (point-min))
6587 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6589 (goto-char (match-beginning 0))
6590 (funcall outline-level
))))))
6591 (and level
(hide-sublevels level
))))
6593 (defun org-content (&optional arg
)
6594 "Show all headlines in the buffer, like a table of contents.
6595 With numerical argument N, show content up to level N."
6598 ;; Visit all headings and show their offspring
6599 (and (integerp arg
) (org-overview))
6600 (goto-char (point-max))
6602 (while (and (progn (condition-case nil
6603 (outline-previous-visible-heading 1)
6604 (error (goto-char (point-min))))
6606 (looking-at org-outline-regexp
))
6608 (show-children (1- arg
))
6610 (if (bobp) (throw 'exit nil
))))))
6613 (defun org-optimize-window-after-visibility-change (state)
6614 "Adjust the window after a change in outline visibility.
6615 This function is the default value of the hook `org-cycle-hook'."
6616 (when (get-buffer-window (current-buffer))
6618 ((eq state
'content
) nil
)
6619 ((eq state
'all
) nil
)
6620 ((eq state
'folded
) nil
)
6621 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6622 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6624 (defun org-remove-empty-overlays-at (pos)
6625 "Remove outline overlays that do not contain non-white stuff."
6628 (and (eq 'outline
(overlay-get o
'invisible
))
6629 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6631 (delete-overlay o
)))
6634 (defun org-clean-visibility-after-subtree-move ()
6635 "Fix visibility issues after moving a subtree."
6636 ;; First, find a reasonable region to look at:
6637 ;; Start two siblings above, end three below
6638 (let* ((beg (save-excursion
6639 (and (org-get-last-sibling)
6640 (org-get-last-sibling))
6642 (end (save-excursion
6643 (and (org-get-next-sibling)
6644 (org-get-next-sibling)
6645 (org-get-next-sibling))
6646 (if (org-at-heading-p)
6649 (level (looking-at "\\*+"))
6650 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6653 (narrow-to-region beg end
)
6655 ;; Properly fold already folded siblings
6656 (goto-char (point-min))
6657 (while (re-search-forward re nil t
)
6658 (if (and (not (outline-invisible-p))
6660 (goto-char (point-at-eol)) (outline-invisible-p)))
6662 (org-cycle-show-empty-lines 'overview
)
6663 (org-cycle-hide-drawers 'overview
)))))
6665 (defun org-cycle-show-empty-lines (state)
6666 "Show empty lines above all visible headlines.
6667 The region to be covered depends on STATE when called through
6668 `org-cycle-hook'. Lisp program can use t for STATE to get the
6669 entire buffer covered. Note that an empty line is only shown if there
6670 are at least `org-cycle-separator-lines' empty lines before the headline."
6671 (when (not (= org-cycle-separator-lines
0))
6673 (let* ((n (abs org-cycle-separator-lines
))
6675 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6676 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6677 (t (let ((ns (number-to-string (- n
2))))
6678 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6679 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6682 ((memq state
'(overview contents t
))
6683 (setq beg
(point-min) end
(point-max)))
6684 ((memq state
'(children folded
))
6685 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6686 (beginning-of-line 2)
6690 (while (re-search-forward re end t
)
6691 (unless (get-char-property (match-end 1) 'invisible
)
6692 (setq e
(match-end 1))
6693 (if (< org-cycle-separator-lines
0)
6694 (setq b
(save-excursion
6695 (goto-char (match-beginning 0))
6696 (org-back-over-empty-lines)
6698 (goto-char (max (point-min) (1- (point))))
6702 (setq b
(match-beginning 1)))
6703 (outline-flag-region b e nil
)))))))
6704 ;; Never hide empty lines at the end of the file.
6706 (goto-char (point-max))
6707 (outline-previous-heading)
6708 (outline-end-of-heading)
6709 (if (and (looking-at "[ \t\n]+")
6710 (= (match-end 0) (point-max)))
6711 (outline-flag-region (point) (match-end 0) nil
))))
6713 (defun org-show-empty-lines-in-parent ()
6714 "Move to the parent and re-show empty lines before visible headlines."
6716 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6717 (org-cycle-show-empty-lines context
))))
6719 (defun org-files-list ()
6720 "Return `org-agenda-files' list, plus all open org-mode files.
6721 This is useful for operations that need to scan all of a user's
6722 open and agenda-wise Org files."
6723 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6724 (dolist (buf (buffer-list))
6725 (with-current-buffer buf
6726 (if (and (derived-mode-p 'org-mode
) (buffer-file-name))
6727 (let ((file (expand-file-name (buffer-file-name))))
6728 (unless (member file files
)
6729 (push file files
))))))
6732 (defsubst org-entry-beginning-position
()
6733 "Return the beginning position of the current entry."
6734 (save-excursion (outline-back-to-heading t
) (point)))
6736 (defsubst org-entry-end-position
()
6737 "Return the end position of the current entry."
6738 (save-excursion (outline-next-heading) (point)))
6740 (defun org-cycle-hide-drawers (state)
6741 "Re-hide all drawers after a visibility state change."
6742 (when (and (derived-mode-p 'org-mode
)
6743 (not (memq state
'(overview folded contents
))))
6745 (let* ((globalp (memq state
'(contents all
)))
6746 (beg (if globalp
(point-min) (point)))
6747 (end (if globalp
(point-max)
6748 (if (eq state
'children
)
6749 (save-excursion (outline-next-heading) (point))
6750 (org-end-of-subtree t
)))))
6752 (while (re-search-forward org-drawer-regexp end t
)
6753 (org-flag-drawer t
))))))
6755 (defun org-cycle-hide-inline-tasks (state)
6756 "Re-hide inline task when switching to 'contents visibility state."
6757 (when (and (eq state
'contents
)
6758 (boundp 'org-inlinetask-min-level
)
6759 org-inlinetask-min-level
)
6760 (hide-sublevels (1- org-inlinetask-min-level
))))
6762 (defun org-flag-drawer (flag)
6763 "When FLAG is non-nil, hide the drawer we are within.
6764 Otherwise make it visible."
6766 (beginning-of-line 1)
6767 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6768 (let ((b (match-end 0)))
6769 (if (re-search-forward
6771 (save-excursion (outline-next-heading) (point)) t
)
6772 (outline-flag-region b
(point-at-eol) flag
)
6773 (error ":END: line missing at position %s" b
))))))
6775 (defun org-subtree-end-visible-p ()
6776 "Is the end of the current subtree visible?"
6777 (pos-visible-in-window-p
6778 (save-excursion (org-end-of-subtree t
) (point))))
6780 (defun org-first-headline-recenter (&optional N
)
6781 "Move cursor to the first headline and recenter the headline.
6782 Optional argument N means put the headline into the Nth line of the window."
6783 (goto-char (point-min))
6784 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
6786 (recenter (prefix-numeric-value N
))))
6788 ;;; Saving and restoring visibility
6790 (defun org-outline-overlay-data (&optional use-markers
)
6791 "Return a list of the locations of all outline overlays.
6792 These are overlays with the `invisible' property value `outline'.
6793 The return value is a list of cons cells, with start and stop
6794 positions for each overlay.
6795 If USE-MARKERS is set, return the positions as markers."
6802 (when (eq (overlay-get o
'invisible
) 'outline
)
6803 (setq beg
(overlay-start o
)
6804 end
(overlay-end o
))
6805 (and beg end
(> end beg
)
6807 (cons (move-marker (make-marker) beg
)
6808 (move-marker (make-marker) end
))
6810 (overlays-in (point-min) (point-max))))))))
6812 (defun org-set-outline-overlay-data (data)
6813 "Create visibility overlays for all positions in DATA.
6814 DATA should have been made by `org-outline-overlay-data'."
6821 (outline-flag-region (car c
) (cdr c
) t
))
6824 ;;; Folding of blocks
6826 (defvar org-hide-block-overlays nil
6827 "Overlays hiding blocks.")
6828 (make-variable-buffer-local 'org-hide-block-overlays
)
6830 (defun org-block-map (function &optional start end
)
6831 "Call FUNCTION at the head of all source blocks in the current buffer.
6832 Optional arguments START and END can be used to limit the range."
6833 (let ((start (or start
(point-min)))
6834 (end (or end
(point-max))))
6837 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6840 (goto-char (match-beginning 0))
6841 (funcall function
)))))))
6843 (defun org-hide-block-toggle-all ()
6844 "Toggle the visibility of all blocks in the current buffer."
6845 (org-block-map #'org-hide-block-toggle
))
6847 (defun org-hide-block-all ()
6848 "Fold all blocks in the current buffer."
6850 (org-show-block-all)
6851 (org-block-map #'org-hide-block-toggle-maybe
))
6853 (defun org-show-block-all ()
6854 "Unfold all blocks in the current buffer."
6856 (mapc 'delete-overlay org-hide-block-overlays
)
6857 (setq org-hide-block-overlays nil
))
6859 (defun org-hide-block-toggle-maybe ()
6860 "Toggle visibility of block at point."
6862 (let ((case-fold-search t
))
6864 (beginning-of-line 1)
6865 (looking-at org-block-regexp
))
6866 (progn (org-hide-block-toggle)
6867 t
) ;; to signal that we took action
6868 nil
))) ;; to signal that we did not
6870 (defun org-hide-block-toggle (&optional force
)
6871 "Toggle the visibility of the current block."
6875 (if (re-search-forward org-block-regexp nil t
)
6876 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
6877 (end (match-end 0)) ;; end of entire body
6879 (if (memq t
(mapcar (lambda (overlay)
6880 (eq (overlay-get overlay
'invisible
)
6882 (overlays-at start
)))
6883 (if (or (not force
) (eq force
'off
))
6885 (when (member ov org-hide-block-overlays
)
6886 (setq org-hide-block-overlays
6887 (delq ov org-hide-block-overlays
)))
6888 (when (eq (overlay-get ov
'invisible
)
6890 (delete-overlay ov
)))
6891 (overlays-at start
)))
6892 (setq ov
(make-overlay start end
))
6893 (overlay-put ov
'invisible
'org-hide-block
)
6894 ;; make the block accessible to isearch
6896 ov
'isearch-open-invisible
6898 (when (member ov org-hide-block-overlays
)
6899 (setq org-hide-block-overlays
6900 (delq ov org-hide-block-overlays
)))
6901 (when (eq (overlay-get ov
'invisible
)
6903 (delete-overlay ov
))))
6904 (push ov org-hide-block-overlays
)))
6905 (error "Not looking at a source block"))))
6907 ;; org-tab-after-check-for-cycling-hook
6908 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
6909 ;; Remove overlays when changing major mode
6910 (add-hook 'org-mode-hook
6911 (lambda () (org-add-hook 'change-major-mode-hook
6912 'org-show-block-all
'append
'local
)))
6916 (defvar org-goto-window-configuration nil
)
6917 (defvar org-goto-marker nil
)
6918 (defvar org-goto-map
6919 (let ((map (make-sparse-keymap)))
6920 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur
)) cmd
)
6921 (while (setq cmd
(pop cmds
))
6922 (substitute-key-definition cmd cmd map global-map
)))
6923 (suppress-keymap map
)
6924 (org-defkey map
"\C-m" 'org-goto-ret
)
6925 (org-defkey map
[(return)] 'org-goto-ret
)
6926 (org-defkey map
[(left)] 'org-goto-left
)
6927 (org-defkey map
[(right)] 'org-goto-right
)
6928 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
6929 (org-defkey map
"\C-i" 'org-cycle
)
6930 (org-defkey map
[(tab)] 'org-cycle
)
6931 (org-defkey map
[(down)] 'outline-next-visible-heading
)
6932 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
6933 (if org-goto-auto-isearch
6934 (if (fboundp 'define-key-after
)
6935 (define-key-after map
[t] 'org-goto-local-auto-isearch)
6937 (org-defkey map "q" 'org-goto-quit)
6938 (org-defkey map "n" 'outline-next-visible-heading)
6939 (org-defkey map "p" 'outline-previous-visible-heading)
6940 (org-defkey map "f" 'outline-forward-same-level)
6941 (org-defkey map "b" 'outline-backward-same-level)
6942 (org-defkey map "u" 'outline-up-heading))
6943 (org-defkey map "/" 'org-occur)
6944 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6945 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6946 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6947 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6948 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6951 (defconst org-goto-help
6952 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6953 RET=jump to location [Q]uit and return to previous location
6954 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6956 (defvar org-goto-start-pos) ; dynamically scoped parameter
6958 ;; FIXME: Docstring does not mention both interfaces
6959 (defun org-goto (&optional alternative-interface)
6960 "Look up a different location in the current file, keeping current visibility.
6962 When you want look-up or go to a different location in a
6963 document, the fastest way is often to fold the entire buffer and
6964 then dive into the tree. This method has the disadvantage, that
6965 the previous location will be folded, which may not be what you
6968 This command works around this by showing a copy of the current
6969 buffer in an indirect buffer, in overview mode. You can dive
6970 into the tree in that copy, use org-occur and incremental search
6971 to find a location. When pressing RET or `Q', the command
6972 returns to the original buffer in which the visibility is still
6973 unchanged. After RET it will also jump to the location selected
6974 in the indirect buffer and expose the headline hierarchy above.
6976 With a prefix argument, use the alternative interface: e.g. if
6977 `org-goto-interface' is 'outline use 'outline-path-completion."
6979 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6980 (org-refile-use-outline-path t)
6981 (org-refile-target-verify-function nil)
6983 (if (not alternative-interface)
6985 (if (eq org-goto-interface 'outline)
6986 'outline-path-completion
6988 (org-goto-start-pos (point))
6990 (if (eq interface 'outline)
6991 (car (org-get-location (current-buffer) org-goto-help))
6992 (let ((pa (org-refile-get-location "Goto" nil nil t)))
6993 (org-refile-check-position pa)
6997 (org-mark-ring-push org-goto-start-pos)
6998 (goto-char selected-point)
6999 (if (or (outline-invisible-p) (org-invisible-p2))
7000 (org-show-context 'org-goto)))
7003 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
7004 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
7005 (defvar org-goto-local-auto-isearch-map) ; defined below
7007 (defun org-get-location (buf help)
7008 "Let the user select a location in the Org-mode buffer BUF.
7009 This function uses a recursive edit. It returns the selected position
7012 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
7013 (isearch-hide-immediately nil)
7014 (isearch-search-fun-function
7015 (lambda () 'org-goto-local-search-headings))
7016 (org-goto-selected-point org-goto-exit-command))
7018 (save-window-excursion
7019 (delete-other-windows)
7020 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
7021 (org-pop-to-buffer-same-window
7023 (make-indirect-buffer (current-buffer) "*org-goto*")
7024 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
7025 (with-output-to-temp-buffer "*Help*"
7027 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
7028 (setq buffer-read-only nil)
7029 (let ((org-startup-truncated t)
7030 (org-startup-folded nil)
7031 (org-startup-align-all-tables nil))
7034 (setq buffer-read-only t)
7035 (if (and (boundp 'org-goto-start-pos)
7036 (integer-or-marker-p org-goto-start-pos))
7037 (let ((org-show-hierarchy-above t)
7038 (org-show-siblings t)
7039 (org-show-following-heading t))
7040 (goto-char org-goto-start-pos)
7041 (and (outline-invisible-p) (org-show-context)))
7042 (goto-char (point-min)))
7043 (let (org-special-ctrl-a/e) (org-beginning-of-line))
7044 (message "Select location and press RET")
7045 (use-local-map org-goto-map)
7048 (kill-buffer "*org-goto*")
7049 (cons org-goto-selected-point org-goto-exit-command))))
7051 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
7052 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
7053 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
7054 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
7056 (defun org-goto-local-search-headings (string bound noerror)
7057 "Search and make sure that any matches are in headlines."
7059 (while (if isearch-forward
7060 (search-forward string bound noerror)
7061 (search-backward string bound noerror))
7062 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
7063 (and (member :headline context)
7064 (not (member :tags context))))
7065 (throw 'return (point))))))
7067 (defun org-goto-local-auto-isearch ()
7070 (goto-char (point-min))
7071 (let ((keys (this-command-keys)))
7072 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
7074 (isearch-process-search-char (string-to-char keys)))))
7076 (defun org-goto-ret (&optional arg)
7077 "Finish `org-goto' by going to the new location."
7079 (setq org-goto-selected-point (point)
7080 org-goto-exit-command 'return)
7083 (defun org-goto-left ()
7084 "Finish `org-goto' by going to the new location."
7086 (if (org-at-heading-p)
7088 (beginning-of-line 1)
7089 (setq org-goto-selected-point (point)
7090 org-goto-exit-command 'left)
7092 (error "Not on a heading")))
7094 (defun org-goto-right ()
7095 "Finish `org-goto' by going to the new location."
7097 (if (org-at-heading-p)
7099 (setq org-goto-selected-point (point)
7100 org-goto-exit-command 'right)
7102 (error "Not on a heading")))
7104 (defun org-goto-quit ()
7105 "Finish `org-goto' without cursor motion."
7107 (setq org-goto-selected-point nil)
7108 (setq org-goto-exit-command 'quit)
7111 ;;; Indirect buffer display of subtrees
7113 (defvar org-indirect-dedicated-frame nil
7114 "This is the frame being used for indirect tree display.")
7115 (defvar org-last-indirect-buffer nil)
7117 (defun org-tree-to-indirect-buffer (&optional arg)
7118 "Create indirect buffer and narrow it to current subtree.
7119 With a numerical prefix ARG, go up to this level and then take that tree.
7120 If ARG is negative, go up that many levels.
7122 If `org-indirect-buffer-display' is not `new-frame', the command removes the
7123 indirect buffer previously made with this command, to avoid proliferation of
7124 indirect buffers. However, when you call the command with a \
7125 \\[universal-argument] prefix, or
7126 when `org-indirect-buffer-display' is `new-frame', the last buffer
7127 is kept so that you can work with several indirect buffers at the same time.
7128 If `org-indirect-buffer-display' is `dedicated-frame', the \
7129 \\[universal-argument] prefix also
7130 requests that a new frame be made for the new buffer, so that the dedicated
7131 frame is not changed."
7133 (let ((cbuf (current-buffer))
7134 (cwin (selected-window))
7136 beg end level heading ibuf)
7138 (org-back-to-heading t)
7140 (setq level (org-outline-level))
7141 (if (< arg 0) (setq arg (+ level arg)))
7142 (while (> (setq level (org-outline-level)) arg)
7143 (org-up-heading-safe)))
7145 heading (org-get-heading))
7146 (org-end-of-subtree t t)
7147 (if (org-at-heading-p) (backward-char 1))
7149 (if (and (buffer-live-p org-last-indirect-buffer)
7150 (not (eq org-indirect-buffer-display 'new-frame))
7152 (kill-buffer org-last-indirect-buffer))
7153 (setq ibuf (org-get-indirect-buffer cbuf)
7154 org-last-indirect-buffer ibuf)
7156 ((or (eq org-indirect-buffer-display 'new-frame)
7157 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7158 (select-frame (make-frame))
7159 (delete-other-windows)
7160 (org-pop-to-buffer-same-window ibuf)
7161 (org-set-frame-title heading))
7162 ((eq org-indirect-buffer-display 'dedicated-frame)
7164 (select-frame (or (and org-indirect-dedicated-frame
7165 (frame-live-p org-indirect-dedicated-frame)
7166 org-indirect-dedicated-frame)
7167 (setq org-indirect-dedicated-frame (make-frame)))))
7168 (delete-other-windows)
7169 (org-pop-to-buffer-same-window ibuf)
7170 (org-set-frame-title (concat "Indirect: " heading)))
7171 ((eq org-indirect-buffer-display 'current-window)
7172 (org-pop-to-buffer-same-window ibuf))
7173 ((eq org-indirect-buffer-display 'other-window)
7174 (pop-to-buffer ibuf))
7175 (t (error "Invalid value")))
7176 (if (featurep 'xemacs)
7177 (save-excursion (org-mode) (turn-on-font-lock)))
7178 (narrow-to-region beg end)
7181 (run-hook-with-args 'org-cycle-hook 'all)
7182 (and (window-live-p cwin) (select-window cwin))))
7184 (defun org-get-indirect-buffer (&optional buffer)
7185 (setq buffer (or buffer (current-buffer)))
7186 (let ((n 1) (base (buffer-name buffer)) bname)
7187 (while (buffer-live-p
7188 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7191 (make-indirect-buffer buffer bname 'clone)
7192 (error (make-indirect-buffer buffer bname)))))
7194 (defun org-set-frame-title (title)
7195 "Set the title of the current frame to the string TITLE."
7196 ;; FIXME: how to name a single frame in XEmacs???
7197 (unless (featurep 'xemacs)
7198 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7200 ;;;; Structure editing
7202 ;;; Inserting headlines
7204 (defun org-previous-line-empty-p ()
7207 (or (beginning-of-line 0) t)
7209 (looking-at "[ \t]*$")))))
7211 (defun org-insert-heading (&optional force-heading invisible-ok)
7212 "Insert a new heading or item with same depth at point.
7213 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
7214 If point is at the beginning of a headline, insert a sibling before the
7215 current headline. If point is not at the beginning, split the line,
7216 create the new headline with the text in the current line after point
7217 \(but see also the variable `org-M-RET-may-split-line').
7219 With a double prefix arg, force the heading to be inserted at the
7220 end of the parent subtree.
7222 When INVISIBLE-OK is set, stop at invisible headlines when going back.
7223 This is important for non-interactive uses of the command."
7225 (if (or (= (buffer-size) 0)
7226 (and (not (save-excursion
7227 (and (ignore-errors (org-back-to-heading invisible-ok))
7228 (org-at-heading-p))))
7229 (or force-heading (not (org-in-item-p)))))
7232 (run-hooks 'org-insert-heading-hook))
7233 (when (or force-heading (not (org-insert-item)))
7234 (let* ((empty-line-p nil)
7236 (on-heading (org-at-heading-p))
7237 (head (save-excursion
7240 (org-back-to-heading invisible-ok)
7241 (when (and (not on-heading)
7242 (featurep 'org-inlinetask)
7243 (integerp org-inlinetask-min-level)
7244 (>= (length (match-string 0))
7245 org-inlinetask-min-level))
7246 ;; Find a heading level before the inline task
7247 (while (and (setq level (org-up-heading-safe))
7248 (>= level org-inlinetask-min-level)))
7249 (if (org-at-heading-p)
7250 (org-back-to-heading invisible-ok)
7251 (error "This should not happen")))
7252 (setq empty-line-p (org-previous-line-empty-p))
7255 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7256 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7257 pos hide-previous previous-pos)
7259 ((and (org-at-heading-p) (bolp)
7261 (save-excursion (backward-char 1) (not (outline-invisible-p)))))
7262 ;; insert before the current line
7263 (open-line (if blank 2 1)))
7265 (not org-insert-heading-respect-content)
7268 (backward-char 1) (not (outline-invisible-p)))))
7269 ;; insert right here
7272 ;; somewhere in the line
7274 (setq previous-pos (point-at-bol))
7276 (setq hide-previous (outline-invisible-p)))
7277 (and org-insert-heading-respect-content (org-show-subtree))
7279 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
7282 (goto-char (point-at-bol))
7283 (and (looking-at org-complex-heading-regexp)
7285 (> p (match-beginning 4)))))))
7288 (org-insert-heading-respect-content
7289 (if (not (equal force-heading '(16)))
7290 (org-end-of-subtree nil t)
7291 (org-up-heading-safe)
7292 (org-end-of-subtree nil t))
7293 (when (featurep 'org-inlinetask)
7294 (while (and (not (eobp))
7295 (looking-at "\\(\\*+\\)[ \t]+")
7296 (>= (length (match-string 1))
7297 org-inlinetask-min-level))
7298 (org-end-of-subtree nil t)))
7299 (or (bolp) (newline))
7300 (or (org-previous-line-empty-p)
7301 (and blank (newline)))
7307 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
7308 (setq tags (and (match-end 2) (match-string 2)))
7310 (delete-region (match-beginning 1) (match-end 1)))
7311 (setq pos (point-at-bol))
7312 (or split (end-of-line 1))
7313 (delete-horizontal-space)
7314 (if (string-match "\\`\\*+\\'"
7315 (buffer-substring (point-at-bol) (point)))
7317 (newline (if blank 2 1))
7323 (org-set-tags nil 'align))))
7325 (or split (end-of-line 1))
7326 (newline (if blank 2 1)))))))
7327 (insert head) (just-one-space)
7330 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
7331 (when (and org-insert-heading-respect-content hide-previous)
7333 (goto-char previous-pos)
7335 (run-hooks 'org-insert-heading-hook)))))
7337 (defun org-get-heading (&optional no-tags no-todo)
7338 "Return the heading of the current entry, without the stars.
7339 When NO-TAGS is non-nil, don't include tags.
7340 When NO-TODO is non-nil, don't include TODO keywords."
7342 (org-back-to-heading t)
7344 ((and no-tags no-todo)
7345 (looking-at org-complex-heading-regexp)
7348 (looking-at (concat org-outline-regexp
7350 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7353 (looking-at org-todo-line-regexp)
7355 (t (looking-at org-heading-regexp)
7356 (match-string 2)))))
7358 (defun org-heading-components ()
7359 "Return the components of the current heading.
7360 This is a list with the following elements:
7361 - the level as an integer
7362 - the reduced level, different if `org-odd-levels-only' is set.
7363 - the TODO keyword, or nil
7364 - the priority character, like ?A, or nil if no priority is given
7365 - the headline text itself, or the tags string if no headline text
7366 - the tags string, or nil."
7368 (org-back-to-heading t)
7369 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
7370 (list (length (match-string 1))
7371 (org-reduced-level (length (match-string 1)))
7372 (org-match-string-no-properties 2)
7373 (and (match-end 3) (aref (match-string 3) 2))
7374 (org-match-string-no-properties 4)
7375 (org-match-string-no-properties 5)))))
7377 (defun org-get-entry ()
7378 "Get the entry text, after heading, entire subtree."
7380 (org-back-to-heading t)
7381 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7383 (defun org-insert-heading-after-current ()
7384 "Insert a new heading with same level as current, after current subtree."
7386 (org-back-to-heading)
7387 (org-insert-heading)
7388 (org-move-subtree-down)
7391 (defun org-insert-heading-respect-content ()
7393 (let ((org-insert-heading-respect-content t))
7394 (org-insert-heading t)))
7396 (defun org-insert-todo-heading-respect-content (&optional force-state)
7398 (let ((org-insert-heading-respect-content t))
7399 (org-insert-todo-heading force-state t)))
7401 (defun org-insert-todo-heading (arg &optional force-heading)
7402 "Insert a new heading with the same level and TODO state as current heading.
7403 If the heading has no TODO state, or if the state is DONE, use the first
7404 state (TODO by default). Also one prefix arg, force first state. With two
7405 prefix args, force inserting at the end of the parent subtree."
7407 (when (or force-heading (not (org-insert-item 'checkbox)))
7408 (org-insert-heading (or (and (equal arg '(16)) '(16))
7411 (org-back-to-heading)
7412 (outline-previous-heading)
7413 (looking-at org-todo-line-regexp))
7417 (not (match-beginning 2))
7418 (member (match-string 2) org-done-keywords))
7419 (car org-todo-keywords-1)
7423 (run-hook-with-args-until-success
7424 'org-todo-get-default-hook new-mark-x nil)
7426 (beginning-of-line 1)
7427 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7428 (if org-treat-insert-todo-heading-as-state-change
7430 (insert new-mark " "))))
7431 (when org-provide-todo-statistics
7432 (org-update-parent-todo-statistics))))
7434 (defun org-insert-subheading (arg)
7435 "Insert a new subheading and demote it.
7436 Works for outline headings and for plain lists alike."
7438 (org-insert-heading arg)
7440 ((org-at-heading-p) (org-do-demote))
7441 ((org-at-item-p) (org-indent-item))))
7443 (defun org-insert-todo-subheading (arg)
7444 "Insert a new subheading with TODO keyword or checkbox and demote it.
7445 Works for outline headings and for plain lists alike."
7447 (org-insert-todo-heading arg)
7449 ((org-at-heading-p) (org-do-demote))
7450 ((org-at-item-p) (org-indent-item))))
7452 ;;; Promotion and Demotion
7454 (defvar org-after-demote-entry-hook nil
7455 "Hook run after an entry has been demoted.
7456 The cursor will be at the beginning of the entry.
7457 When a subtree is being demoted, the hook will be called for each node.")
7459 (defvar org-after-promote-entry-hook nil
7460 "Hook run after an entry has been promoted.
7461 The cursor will be at the beginning of the entry.
7462 When a subtree is being promoted, the hook will be called for each node.")
7464 (defun org-promote-subtree ()
7465 "Promote the entire subtree.
7466 See also `org-promote'."
7469 (org-with-limited-levels (org-map-tree 'org-promote)))
7470 (org-fix-position-after-promote))
7472 (defun org-demote-subtree ()
7473 "Demote the entire subtree. See `org-demote'.
7474 See also `org-promote'."
7477 (org-with-limited-levels (org-map-tree 'org-demote)))
7478 (org-fix-position-after-promote))
7481 (defun org-do-promote ()
7482 "Promote the current heading higher up the tree.
7483 If the region is active in `transient-mark-mode', promote all headings
7487 (if (org-region-active-p)
7488 (org-map-region 'org-promote (region-beginning) (region-end))
7490 (org-fix-position-after-promote))
7492 (defun org-do-demote ()
7493 "Demote the current heading lower down the tree.
7494 If the region is active in `transient-mark-mode', demote all headings
7498 (if (org-region-active-p)
7499 (org-map-region 'org-demote (region-beginning) (region-end))
7501 (org-fix-position-after-promote))
7503 (defun org-fix-position-after-promote ()
7504 "Make sure that after pro/demotion cursor position is right."
7505 (let ((pos (point)))
7506 (when (save-excursion
7507 (beginning-of-line 1)
7508 (looking-at org-todo-line-regexp)
7509 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7510 (cond ((eobp) (insert " "))
7511 ((eolp) (insert " "))
7512 ((equal (char-after) ?\ ) (forward-char 1))))))
7514 (defun org-current-level ()
7515 "Return the level of the current entry, or nil if before the first headline.
7516 The level is the number of stars at the beginning of the headline."
7518 (org-with-limited-levels
7519 (if (ignore-errors (org-back-to-heading t))
7520 (funcall outline-level)))))
7522 (defun org-get-previous-line-level ()
7523 "Return the outline depth of the last headline before the current line.
7524 Returns 0 for the first headline in the buffer, and nil if before the
7526 (let ((current-level (org-current-level))
7527 (prev-level (when (> (line-number-at-pos) 1)
7529 (beginning-of-line 0)
7530 (org-current-level)))))
7531 (cond ((null current-level) nil) ; Before first headline
7532 ((null prev-level) 0) ; At first headline
7535 (defun org-reduced-level (l)
7536 "Compute the effective level of a heading.
7537 This takes into account the setting of `org-odd-levels-only'."
7540 (org-odd-levels-only (1+ (floor (/ l 2))))
7543 (defun org-level-increment ()
7544 "Return the number of stars that will be added or removed at a
7545 time to headlines when structure editing, based on the value of
7546 `org-odd-levels-only'."
7547 (if org-odd-levels-only 2 1))
7549 (defun org-get-valid-level (level &optional change)
7550 "Rectify a level change under the influence of `org-odd-levels-only'
7551 LEVEL is a current level, CHANGE is by how much the level should be
7552 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7553 even level numbers will become the next higher odd number."
7554 (if org-odd-levels-only
7555 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7556 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7557 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7558 (max 1 (+ level (or change 0)))))
7560 (if (boundp 'define-obsolete-function-alias)
7561 (if (or (featurep 'xemacs) (< emacs-major-version 23))
7562 (define-obsolete-function-alias 'org-get-legal-level
7563 'org-get-valid-level)
7564 (define-obsolete-function-alias 'org-get-legal-level
7565 'org-get-valid-level "23.1")))
7567 (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
7568 ;; ̀org-with-limited-levels'
7569 (defun org-promote ()
7570 "Promote the current heading higher up the tree.
7571 If the region is active in `transient-mark-mode', promote all headings
7573 (org-back-to-heading t)
7574 (let* ((level (save-match-data (funcall outline-level)))
7575 (after-change-functions (remove 'flyspell-after-change-function
7576 after-change-functions))
7577 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7578 (diff (abs (- level (length up-head) -1))))
7579 (cond ((and (= level 1) org-called-with-limited-levels
7580 org-allow-promoting-top-level-subtree)
7581 (replace-match "# " nil t))
7583 (error "Cannot promote to level 0. UNDO to recover if necessary"))
7584 (t (replace-match up-head nil t)))
7585 ;; Fixup tag positioning
7587 (and org-auto-align-tags (org-set-tags nil t))
7588 (if org-adapt-indentation (org-fixup-indentation (- diff))))
7589 (run-hooks 'org-after-promote-entry-hook)))
7591 (defun org-demote ()
7592 "Demote the current heading lower down the tree.
7593 If the region is active in `transient-mark-mode', demote all headings
7595 (org-back-to-heading t)
7596 (let* ((level (save-match-data (funcall outline-level)))
7597 (after-change-functions (remove 'flyspell-after-change-function
7598 after-change-functions))
7599 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7600 (diff (abs (- level (length down-head) -1))))
7601 (replace-match down-head nil t)
7602 ;; Fixup tag positioning
7603 (and org-auto-align-tags (org-set-tags nil t))
7604 (if org-adapt-indentation (org-fixup-indentation diff))
7605 (run-hooks 'org-after-demote-entry-hook)))
7607 (defun org-cycle-level ()
7608 "Cycle the level of an empty headline through possible states.
7609 This goes first to child, then to parent, level, then up the hierarchy.
7610 After top level, it switches back to sibling level."
7612 (let ((org-adapt-indentation nil))
7613 (when (org-point-at-end-of-empty-headline)
7614 (setq this-command 'org-cycle-level) ; Only needed for caching
7615 (let ((cur-level (org-current-level))
7616 (prev-level (org-get-previous-line-level)))
7618 ;; If first headline in file, promote to top-level.
7620 (loop repeat (/ (- cur-level 1) (org-level-increment))
7621 do (org-do-promote)))
7622 ;; If same level as prev, demote one.
7623 ((= prev-level cur-level)
7625 ;; If parent is top-level, promote to top level if not already.
7627 (loop repeat (/ (- cur-level 1) (org-level-increment))
7628 do (org-do-promote)))
7629 ;; If top-level, return to prev-level.
7631 (loop repeat (/ (- prev-level 1) (org-level-increment))
7632 do (org-do-demote)))
7633 ;; If less than prev-level, promote one.
7634 ((< cur-level prev-level)
7636 ;; If deeper than prev-level, promote until higher than
7638 ((> cur-level prev-level)
7639 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7640 do (org-do-promote))))
7643 (defun org-map-tree (fun)
7644 "Call FUN for every heading underneath the current one."
7645 (org-back-to-heading)
7646 (let ((level (funcall outline-level)))
7650 (outline-next-heading)
7651 (> (funcall outline-level) level))
7655 (defun org-map-region (fun beg end)
7656 "Call FUN for every heading between BEG and END."
7657 (let ((org-ignore-region t))
7659 (setq end (copy-marker end))
7661 (if (and (re-search-forward org-outline-regexp-bol nil t)
7665 (outline-next-heading)
7670 (defvar org-property-end-re) ; silence byte-compiler
7671 (defun org-fixup-indentation (diff)
7672 "Change the indentation in the current entry by DIFF.
7673 However, if any line in the current entry has no indentation, or if it
7674 would end up with no indentation after the change, nothing at all is done."
7676 (let ((end (save-excursion (outline-next-heading)
7678 (prohibit (if (> diff 0)
7680 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7682 (unless (save-excursion (end-of-line 1)
7683 (re-search-forward prohibit end t))
7684 (while (and (< (point) end)
7685 (re-search-forward "^[ \t]+" end t))
7686 (goto-char (match-end 0))
7687 (setq col (current-column))
7688 (if (< diff 0) (replace-match ""))
7689 (org-indent-to-column (+ diff col))))
7690 (move-marker end nil))))
7692 (defun org-convert-to-odd-levels ()
7693 "Convert an org-mode file with all levels allowed to one with odd levels.
7694 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7697 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7698 (let ((outline-level 'org-outline-level)
7699 (org-odd-levels-only nil) n)
7701 (goto-char (point-min))
7702 (while (re-search-forward "^\\*\\*+ " nil t)
7703 (setq n (- (length (match-string 0)) 2))
7704 (while (>= (setq n (1- n)) 0)
7706 (end-of-line 1))))))
7708 (defun org-convert-to-oddeven-levels ()
7709 "Convert an org-mode file with only odd levels to one with odd/even levels.
7710 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7711 file contains a section with an even level, conversion would
7712 destroy the structure of the file. An error is signaled in this
7715 (goto-char (point-min))
7716 ;; First check if there are no even levels
7717 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7718 (org-show-context t)
7719 (error "Not all levels are odd in this file. Conversion not possible"))
7720 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7721 (let ((outline-regexp org-outline-regexp)
7722 (outline-level 'org-outline-level)
7723 (org-odd-levels-only nil) n)
7725 (goto-char (point-min))
7726 (while (re-search-forward "^\\*\\*+ " nil t)
7727 (setq n (/ (1- (length (match-string 0))) 2))
7728 (while (>= (setq n (1- n)) 0)
7730 (end-of-line 1))))))
7732 (defun org-tr-level (n)
7733 "Make N odd if required."
7734 (if org-odd-levels-only (1+ (/ n 2)) n))
7736 ;;; Vertical tree motion, cutting and pasting of subtrees
7738 (defun org-move-subtree-up (&optional arg)
7739 "Move the current subtree up past ARG headlines of the same level."
7741 (org-move-subtree-down (- (prefix-numeric-value arg))))
7743 (defun org-move-subtree-down (&optional arg)
7744 "Move the current subtree down past ARG headlines of the same level."
7746 (setq arg (prefix-numeric-value arg))
7747 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7748 'org-get-last-sibling))
7749 (ins-point (make-marker))
7751 (col (current-column))
7752 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7754 (org-back-to-heading)
7757 (setq ne-beg (org-back-over-empty-lines))
7760 (save-excursion (outline-end-of-heading)
7761 (setq folded (outline-invisible-p)))
7762 (outline-end-of-subtree))
7763 (outline-next-heading)
7764 (setq ne-end (org-back-over-empty-lines))
7767 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7768 ;; include less whitespace
7771 (forward-line (- ne-beg ne-end))
7772 (setq beg (point))))
7773 ;; Find insertion point, with error handling
7775 (or (and (funcall movfunc) (looking-at org-outline-regexp))
7776 (progn (goto-char beg0)
7777 (error "Cannot move past superior level or buffer limit")))
7778 (setq cnt (1- cnt)))
7780 ;; Moving forward - still need to move over subtree
7781 (progn (org-end-of-subtree t t)
7783 (org-back-over-empty-lines)
7784 (or (bolp) (newline)))))
7785 (setq ne-ins (org-back-over-empty-lines))
7786 (move-marker ins-point (point))
7787 (setq txt (buffer-substring beg end))
7788 (org-save-markers-in-region beg end)
7789 (delete-region beg end)
7790 (org-remove-empty-overlays-at beg)
7791 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7792 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7793 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7794 (let ((bbb (point)))
7795 (insert-before-markers txt)
7796 (org-reinstall-markers-in-region bbb)
7797 (move-marker ins-point bbb))
7798 (or (bolp) (insert "\n"))
7799 (setq ins-end (point))
7800 (goto-char ins-point)
7801 (org-skip-whitespace)
7802 (when (and (< arg 0)
7803 (org-first-sibling-p)
7805 ;; Move whitespace back to beginning
7808 (let ((kill-whole-line t))
7809 (kill-line (- ne-ins ne-beg)) (point)))
7810 (insert (make-string (- ne-ins ne-beg) ?\n)))
7811 (move-marker ins-point nil)
7816 (org-cycle-hide-drawers 'children))
7817 (org-clean-visibility-after-subtree-move)
7818 ;; move back to the initial column we were at
7819 (move-to-column col)))
7821 (defvar org-subtree-clip ""
7822 "Clipboard for cut and paste of subtrees.
7823 This is actually only a copy of the kill, because we use the normal kill
7824 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7826 (defvar org-subtree-clip-folded nil
7827 "Was the last copied subtree folded?
7828 This is used to fold the tree back after pasting.")
7830 (defun org-cut-subtree (&optional n)
7831 "Cut the current subtree into the clipboard.
7832 With prefix arg N, cut this many sequential subtrees.
7833 This is a short-hand for marking the subtree and then cutting it."
7835 (org-copy-subtree n 'cut))
7837 (defun org-copy-subtree (&optional n cut force-store-markers)
7838 "Cut the current subtree into the clipboard.
7839 With prefix arg N, cut this many sequential subtrees.
7840 This is a short-hand for marking the subtree and then copying it.
7841 If CUT is non-nil, actually cut the subtree.
7842 If FORCE-STORE-MARKERS is non-nil, store the relative locations
7843 of some markers in the region, even if CUT is non-nil. This is
7844 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
7846 (let (beg end folded (beg0 (point)))
7847 (if (org-called-interactively-p 'any)
7848 (org-back-to-heading nil) ; take what looks like a subtree
7849 (org-back-to-heading t)) ; take what is really there
7851 (skip-chars-forward " \t\r\n")
7853 (save-excursion (outline-end-of-heading)
7854 (setq folded (outline-invisible-p)))
7856 (org-forward-heading-same-level (1- n) t)
7858 (org-end-of-subtree t t))
7862 (setq org-subtree-clip-folded folded)
7863 (when (or cut force-store-markers)
7864 (org-save-markers-in-region beg end))
7865 (if cut (kill-region beg end) (copy-region-as-kill beg end))
7866 (setq org-subtree-clip (current-kill 0))
7867 (message "%s: Subtree(s) with %d characters"
7868 (if cut "Cut" "Copied")
7869 (length org-subtree-clip)))))
7871 (defun org-paste-subtree (&optional level tree for-yank)
7872 "Paste the clipboard as a subtree, with modification of headline level.
7873 The entire subtree is promoted or demoted in order to match a new headline
7876 If the cursor is at the beginning of a headline, the same level as
7877 that headline is used to paste the tree
7879 If not, the new level is derived from the *visible* headings
7880 before and after the insertion point, and taken to be the inferior headline
7881 level of the two. So if the previous visible heading is level 3 and the
7882 next is level 4 (or vice versa), level 4 will be used for insertion.
7883 This makes sure that the subtree remains an independent subtree and does
7884 not swallow low level entries.
7886 You can also force a different level, either by using a numeric prefix
7887 argument, or by inserting the heading marker by hand. For example, if the
7888 cursor is after \"*****\", then the tree will be shifted to level 5.
7890 If optional TREE is given, use this text instead of the kill ring.
7892 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
7893 move back over whitespace before inserting, and move point to the end of
7894 the inserted text when done."
7896 (setq tree (or tree (and kill-ring (current-kill 0))))
7897 (unless (org-kill-is-subtree-p tree)
7899 (substitute-command-keys
7900 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
7901 (org-with-limited-levels
7902 (let* ((visp (not (outline-invisible-p)))
7904 (^re_ "\\(\\*+\\)[ \t]*")
7905 (old-level (if (string-match org-outline-regexp-bol txt)
7906 (- (match-end 0) (match-beginning 0) 1)
7908 (force-level (cond (level (prefix-numeric-value level))
7909 ((and (looking-at "[ \t]*$")
7911 "^\\*+$" (buffer-substring
7912 (point-at-bol) (point))))
7913 (- (match-end 1) (match-beginning 1)))
7915 (looking-at org-outline-regexp))
7916 (- (match-end 0) (point) 1))))
7917 (previous-level (save-excursion
7920 (outline-previous-visible-heading 1)
7921 (if (looking-at ^re_)
7922 (- (match-end 0) (match-beginning 0) 1)
7925 (next-level (save-excursion
7928 (or (looking-at org-outline-regexp)
7929 (outline-next-visible-heading 1))
7930 (if (looking-at ^re_)
7931 (- (match-end 0) (match-beginning 0) 1)
7934 (new-level (or force-level (max previous-level next-level)))
7935 (shift (if (or (= old-level -1)
7937 (= old-level new-level))
7939 (- new-level old-level)))
7940 (delta (if (> shift 0) -1 1))
7941 (func (if (> shift 0) 'org-demote 'org-promote))
7942 (org-odd-levels-only nil)
7944 ;; Remove the forced level indicator
7946 (delete-region (point-at-bol) (point)))
7948 (beginning-of-line (if (bolp) 1 2))
7950 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
7951 (insert-before-markers txt)
7952 (unless (string-match "\n\\'" txt) (insert "\n"))
7953 (setq newend (point))
7954 (org-reinstall-markers-in-region beg)
7957 (skip-chars-forward " \t\n\r")
7959 (if (and (outline-invisible-p) visp)
7960 (save-excursion (outline-show-heading)))
7961 ;; Shift if necessary
7964 (narrow-to-region beg end)
7965 (while (not (= shift 0))
7966 (org-map-region func (point-min) (point-max))
7967 (setq shift (+ delta shift)))
7968 (goto-char (point-min))
7969 (setq newend (point-max))))
7970 (when (or (org-called-interactively-p 'interactive) for-yank)
7971 (message "Clipboard pasted as level %d subtree" new-level))
7972 (if (and (not for-yank) ; in this case, org-yank will decide about folding
7974 (eq org-subtree-clip (current-kill 0))
7975 org-subtree-clip-folded)
7976 ;; The tree was folded before it was killed/copied
7978 (and for-yank (goto-char newend)))))
7980 (defun org-kill-is-subtree-p (&optional txt)
7981 "Check if the current kill is an outline subtree, or a set of trees.
7982 Returns nil if kill does not start with a headline, or if the first
7983 headline level is not the largest headline level in the tree.
7984 So this will actually accept several entries of equal levels as well,
7985 which is OK for `org-paste-subtree'.
7986 If optional TXT is given, check this string instead of the current kill."
7987 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
7988 (re (org-get-limited-outline-regexp))
7989 (^re (concat "^" re))
7990 (start-level (and kill
7992 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
7994 (- (match-end 2) (match-beginning 2) 1)))
7995 (start (1+ (or (match-beginning 2) -1))))
7996 (if (not start-level)
7998 nil) ;; does not even start with a heading
8000 (while (setq start (string-match ^re kill (1+ start)))
8001 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
8005 (defvar org-markers-to-move nil
8006 "Markers that should be moved with a cut-and-paste operation.
8007 Those markers are stored together with their positions relative to
8008 the start of the region.")
8010 (defun org-save-markers-in-region (beg end)
8011 "Check markers in region.
8012 If these markers are between BEG and END, record their position relative
8013 to BEG, so that after moving the block of text, we can put the markers back
8015 This function gets called just before an entry or tree gets cut from the
8016 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
8017 called immediately, to move the markers with the entries."
8018 (setq org-markers-to-move nil)
8019 (when (featurep 'org-clock)
8020 (org-clock-save-markers-for-cut-and-paste beg end))
8021 (when (featurep 'org-agenda)
8022 (org-agenda-save-markers-for-cut-and-paste beg end)))
8024 (defun org-check-and-save-marker (marker beg end)
8025 "Check if MARKER is between BEG and END.
8026 If yes, remember the marker and the distance to BEG."
8027 (when (and (marker-buffer marker)
8028 (equal (marker-buffer marker) (current-buffer)))
8029 (if (and (>= marker beg) (< marker end))
8030 (push (cons marker (- marker beg)) org-markers-to-move))))
8032 (defun org-reinstall-markers-in-region (beg)
8033 "Move all remembered markers to their position relative to BEG."
8035 (move-marker (car x) (+ beg (cdr x))))
8036 org-markers-to-move)
8037 (setq org-markers-to-move nil))
8039 (defun org-narrow-to-subtree ()
8040 "Narrow buffer to the current subtree."
8044 (org-with-limited-levels
8046 (progn (org-back-to-heading t) (point))
8047 (progn (org-end-of-subtree t t)
8048 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
8051 (defun org-narrow-to-block ()
8052 "Narrow buffer to the current block."
8054 (let* ((case-fold-search t)
8055 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
8056 "^[ \t]*#\\+end_.*")))
8058 (narrow-to-region (car blockp) (cdr blockp))
8059 (error "Not in a block"))))
8062 (defvar org-property-drawer-re))
8064 (defvar org-property-start-re) ;; defined below
8065 (defun org-clone-subtree-with-time-shift (n &optional shift)
8066 "Clone the task (subtree) at point N times.
8067 The clones will be inserted as siblings.
8069 In interactive use, the user will be prompted for the number of
8070 clones to be produced, and for a time SHIFT, which may be a
8071 repeater as used in time stamps, for example `+3d'.
8073 When a valid repeater is given and the entry contains any time
8074 stamps, the clones will become a sequence in time, with time
8075 stamps in the subtree shifted for each clone produced. If SHIFT
8076 is nil or the empty string, time stamps will be left alone. The
8077 ID property of the original subtree is removed.
8079 If the original subtree did contain time stamps with a repeater,
8080 the following will happen:
8081 - the repeater will be removed in each clone
8082 - an additional clone will be produced, with the current, unshifted
8083 date(s) in the entry.
8084 - the original entry will be placed *after* all the clones, with
8086 - the start days in the repeater in the original entry will be shifted
8087 to past the last clone.
8088 In this way you can spell out a number of instances of a repeating task,
8089 and still retain the repeater to cover future instances of the task."
8090 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
8091 (let (beg end template task idprop
8092 shift-n shift-what doshift nmin nmax (n-no-remove -1)
8093 (drawer-re org-drawer-regexp))
8094 (if (not (and (integerp n) (> n 0)))
8095 (error "Invalid number of replications %s" n))
8096 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
8097 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
8099 (error "Invalid shift specification %s" shift))
8101 (setq shift-n (string-to-number (match-string 1 shift))
8102 shift-what (cdr (assoc (match-string 2 shift)
8103 '(("d" . day) ("w" . week)
8104 ("m" . month) ("y" . year))))))
8105 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
8106 (setq nmin 1 nmax n)
8107 (org-back-to-heading t)
8109 (setq idprop (org-entry-get nil "ID"))
8110 (org-end-of-subtree t t)
8111 (or (bolp) (insert "\n"))
8113 (setq template (buffer-substring beg end))
8115 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
8116 (delete-region beg end)
8118 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
8120 (loop for n from nmin to nmax do
8125 (goto-char (point-min))
8127 (and idprop (if org-clone-delete-id
8128 (org-entry-delete nil "ID")
8129 (org-id-get-create t)))
8131 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
8133 (goto-char (point-min))
8134 (while (re-search-forward drawer-re nil t)
8136 (org-remove-empty-drawer-at d (point))) org-drawers)))
8137 (goto-char (point-min))
8139 (while (re-search-forward org-ts-regexp-both nil t)
8140 (org-timestamp-change (* n shift-n) shift-what))
8141 (unless (= n n-no-remove)
8142 (goto-char (point-min))
8143 (while (re-search-forward org-ts-regexp nil t)
8145 (goto-char (match-beginning 0))
8146 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
8147 (delete-region (match-beginning 1) (match-end 1)))))))
8148 (setq task (buffer-string)))
8154 (defun org-sort (with-case)
8155 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
8156 Optional argument WITH-CASE means sort case-sensitively."
8159 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
8160 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
8162 (org-call-with-arg 'org-sort-entries with-case))))
8164 (defun org-sort-remove-invisible (s)
8165 (remove-text-properties 0 (length s) org-rm-props s)
8166 (while (string-match org-bracket-link-regexp s)
8167 (setq s (replace-match (if (match-end 2)
8169 (match-string 1 s)) t t s)))
8172 (defvar org-priority-regexp) ; defined later in the file
8174 (defvar org-after-sorting-entries-or-items-hook nil
8175 "Hook that is run after a bunch of entries or items have been sorted.
8176 When children are sorted, the cursor is in the parent line when this
8177 hook gets called. When a region or a plain list is sorted, the cursor
8178 will be in the first entry of the sorted region/list.")
8180 (defun org-sort-entries
8181 (&optional with-case sorting-type getkey-func compare-func property)
8182 "Sort entries on a certain level of an outline tree.
8183 If there is an active region, the entries in the region are sorted.
8184 Else, if the cursor is before the first entry, sort the top-level items.
8185 Else, the children of the entry at point are sorted.
8187 Sorting can be alphabetically, numerically, by date/time as given by
8188 a time stamp, by a property or by priority.
8190 The command prompts for the sorting type unless it has been given to the
8191 function through the SORTING-TYPE argument, which needs to be a character,
8192 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
8193 precise meaning of each character:
8195 n Numerically, by converting the beginning of the entry/item to a number.
8196 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8197 o By order of TODO keywords.
8198 t By date/time, either the first active time stamp in the entry, or, if
8199 none exist, by the first inactive one.
8200 s By the scheduled date/time.
8201 d By deadline date/time.
8202 c By creation time, which is assumed to be the first inactive time stamp
8203 at the beginning of a line.
8204 p By priority according to the cookie.
8205 r By the value of a property.
8207 Capital letters will reverse the sort order.
8209 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8210 called with point at the beginning of the record. It must return either
8211 a string or a number that should serve as the sorting key for that record.
8213 Comparing entries ignores case by default. However, with an optional argument
8214 WITH-CASE, the sorting considers case as well."
8216 (let ((case-func (if with-case 'identity 'downcase))
8218 ;; The clock marker is lost when using `sort-subr', let's
8219 ;; store the clocking string.
8220 (when (equal (marker-buffer org-clock-marker) (current-buffer))
8222 (goto-char org-clock-marker)
8223 (looking-back "^.*") (match-string-no-properties 0))))
8224 start beg end stars re re2
8226 ;; Find beginning and end of region to sort
8228 ((org-region-active-p)
8229 ;; we will sort the region
8230 (setq end (region-end)
8232 (goto-char (region-beginning))
8233 (if (not (org-at-heading-p)) (outline-next-heading))
8234 (setq start (point)))
8235 ((or (org-at-heading-p)
8236 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8237 ;; we will sort the children of the current headline
8238 (org-back-to-heading)
8240 end (progn (org-end-of-subtree t t)
8241 (or (bolp) (insert "\n"))
8242 (org-back-over-empty-lines)
8247 (outline-next-heading))
8249 ;; we will sort the top-level entries in this file
8250 (goto-char (point-min))
8251 (or (org-at-heading-p) (outline-next-heading))
8252 (setq start (point))
8253 (goto-char (point-max))
8254 (beginning-of-line 1)
8255 (when (looking-at ".*?\\S-")
8256 ;; File ends in a non-white line
8259 (setq end (point-max))
8260 (setq what "top-level")
8265 (if (>= beg end) (error "Nothing to sort"))
8267 (looking-at "\\(\\*+\\)")
8268 (setq stars (match-string 1)
8269 re (concat "^" (regexp-quote stars) " +")
8270 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8271 txt (buffer-substring beg end))
8272 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8273 (if (and (not (equal stars "*")) (string-match re2 txt))
8274 (error "Region to sort contains a level above the first entry"))
8276 (unless sorting-type
8278 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8279 [t]ime
[s]cheduled [d]eadline [c]reated
8280 A/N/P/R/O/F/T/S/D/C means reversed:"
8282 (setq sorting-type (read-char-exclusive))
8285 (and (= (downcase sorting-type) ?f)
8287 (org-icompleting-read "Sort using function: "
8288 obarray 'fboundp t nil nil))
8289 (setq getkey-func (intern getkey-func))))
8291 (and (= (downcase sorting-type) ?r)
8294 (org-icompleting-read "Property: "
8295 (mapcar 'list (org-buffer-property-keys t))
8298 (message "Sorting entries...")
8301 (narrow-to-region start end)
8302 (let ((dcst (downcase sorting-type))
8303 (case-fold-search nil)
8304 (now (current-time)))
8306 (/= dcst sorting-type)
8307 ;; This function moves to the beginning character of the "record" to
8310 (if (re-search-forward re nil t)
8311 (goto-char (match-beginning 0))
8312 (goto-char (point-max))))
8313 ;; This function moves to the last character of the "record" being
8318 (outline-forward-same-level 1)
8320 (goto-char (point-max))))))
8321 ;; This function returns the value that gets sorted against.
8325 (if (looking-at org-complex-heading-regexp)
8326 (string-to-number (match-string 4))
8329 (if (looking-at org-complex-heading-regexp)
8330 (funcall case-func (match-string 4))
8333 (let ((end (save-excursion (outline-next-heading) (point))))
8334 (if (or (re-search-forward org-ts-regexp end t)
8335 (re-search-forward org-ts-regexp-both end t))
8336 (org-time-string-to-seconds (match-string 0))
8337 (org-float-time now))))
8339 (let ((end (save-excursion (outline-next-heading) (point))))
8340 (if (re-search-forward
8341 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8343 (org-time-string-to-seconds (match-string 0))
8344 (org-float-time now))))
8346 (let ((end (save-excursion (outline-next-heading) (point))))
8347 (if (re-search-forward org-scheduled-time-regexp end t)
8348 (org-time-string-to-seconds (match-string 1))
8349 (org-float-time now))))
8351 (let ((end (save-excursion (outline-next-heading) (point))))
8352 (if (re-search-forward org-deadline-time-regexp end t)
8353 (org-time-string-to-seconds (match-string 1))
8354 (org-float-time now))))
8356 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8357 (string-to-char (match-string 2))
8358 org-default-priority))
8360 (or (org-entry-get nil property) ""))
8362 (if (looking-at org-complex-heading-regexp)
8363 (- 9999 (length (member (match-string 2)
8364 org-todo-keywords-1)))))
8368 (setq tmp (funcall getkey-func))
8369 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8371 (error "Invalid key function `%s'" getkey-func)))
8372 (t (error "Invalid sorting type `%c'" sorting-type))))
8375 ((= dcst ?a) 'string<)
8376 ((= dcst ?f) compare-func)
8377 ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
8378 (run-hooks 'org-after-sorting-entries-or-items-hook)
8379 ;; Reset the clock marker if needed
8383 (search-forward cmstr nil t)
8384 (move-marker org-clock-marker (point))))
8385 (message "Sorting entries...done")))
8387 (defun org-do-sort (table what &optional with-case sorting-type)
8388 "Sort TABLE of WHAT according to SORTING-TYPE.
8389 The user will be prompted for the SORTING-TYPE if the call to this
8390 function does not specify it. WHAT is only for the prompt, to indicate
8391 what is being sorted. The sorting key will be extracted from
8392 the car of the elements of the table.
8393 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8394 (unless sorting-type
8396 "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
8398 (setq sorting-type (read-char-exclusive)))
8399 (let ((dcst (downcase sorting-type))
8400 extractfun comparefun)
8401 ;; Define the appropriate functions
8404 (setq extractfun 'string-to-number
8405 comparefun (if (= dcst sorting-type) '< '>)))
8407 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8408 (lambda(x) (downcase (org-sort-remove-invisible x))))
8409 comparefun (if (= dcst sorting-type)
8411 (lambda (a b) (and (not (string< a b))
8412 (not (string= a b)))))))
8416 (if (or (string-match org-ts-regexp x)
8417 (string-match org-ts-regexp-both x))
8419 (org-time-string-to-time (match-string 0 x)))
8421 comparefun (if (= dcst sorting-type) '< '>)))
8422 (t (error "Invalid sorting type `%c'" sorting-type)))
8424 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8426 (lambda (a b) (funcall comparefun (car a) (car b))))))
8429 ;;; The orgstruct minor mode
8431 ;; Define a minor mode which can be used in other modes in order to
8432 ;; integrate the org-mode structure editing commands.
8434 ;; This is really a hack, because the org-mode structure commands use
8435 ;; keys which normally belong to the major mode. Here is how it
8436 ;; works: The minor mode defines all the keys necessary to operate the
8437 ;; structure commands, but wraps the commands into a function which
8438 ;; tests if the cursor is currently at a headline or a plain list
8439 ;; item. If that is the case, the structure command is used,
8440 ;; temporarily setting many Org-mode variables like regular
8441 ;; expressions for filling etc. However, when any of those keys is
8442 ;; used at a different location, function uses `key-binding' to look
8443 ;; up if the key has an associated command in another currently active
8444 ;; keymap (minor modes, major mode, global), and executes that
8445 ;; command. There might be problems if any of the keys is otherwise
8446 ;; used as a prefix key.
8448 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
8449 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
8450 ;; addresses this by checking explicitly for both bindings.
8452 (defvar orgstruct-mode-map (make-sparse-keymap)
8453 "Keymap for the minor `orgstruct-mode'.")
8455 (defvar org-local-vars nil
8456 "List of local variables, for use by `orgstruct-mode'.")
8459 (define-minor-mode orgstruct-mode
8460 "Toggle the minor mode `orgstruct-mode'.
8461 This mode is for using Org-mode structure commands in other
8462 modes. The following keys behave as if Org-mode were active, if
8463 the cursor is on a headline, or on a plain list item (both as
8464 defined by Org-mode).
8466 M-up Move entry/item up
8467 M-down Move entry/item down
8470 M-S-up Move entry/item up
8471 M-S-down Move entry/item down
8472 M-S-left Promote subtree
8473 M-S-right Demote subtree
8474 M-q Fill paragraph and items like in Org-mode
8476 C-c - Cycle list bullet
8477 TAB Cycle item visibility
8478 M-RET Insert new heading/item
8479 S-M-RET Insert new TODO heading / Checkbox item
8480 C-c C-c Set tags / toggle checkbox"
8481 nil " OrgStruct" nil
8482 (org-load-modules-maybe)
8483 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
8486 (defun turn-on-orgstruct ()
8487 "Unconditionally turn on `orgstruct-mode'."
8490 (defvar org-fb-vars nil)
8491 (make-variable-buffer-local 'org-fb-vars)
8492 (defun orgstruct++-mode (&optional arg)
8493 "Toggle `orgstruct-mode', the enhanced version of it.
8494 In addition to setting orgstruct-mode, this also exports all
8495 indentation and autofilling variables from org-mode into the
8496 buffer. It will also recognize item context in multiline items."
8498 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8500 (progn (orgstruct-mode -1)
8502 (org-set-local (car v)
8503 (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
8506 (setq org-fb-vars nil)
8511 "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
8512 (symbol-name (car x)))
8513 (setq var (car x) val (nth 1 x))
8514 (push (list var `(quote ,(eval var))) org-fb-vars)
8515 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8517 (org-set-local 'orgstruct-is-++ t))))
8519 (defvar orgstruct-is-++ nil
8520 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8521 (make-variable-buffer-local 'orgstruct-is-++)
8524 (defun turn-on-orgstruct++ ()
8525 "Unconditionally turn on `orgstruct++-mode'."
8526 (orgstruct++-mode 1))
8528 (defun orgstruct-error ()
8529 "Error when there is no default binding for a structure key."
8531 (error "This key has no function outside structure elements"))
8533 (defun orgstruct-setup ()
8534 "Setup orgstruct keymaps."
8538 '([(meta up)] org-metaup)
8539 '([(meta down)] org-metadown)
8540 '([(meta left)] org-metaleft)
8541 '([(meta right)] org-metaright)
8542 '([(meta shift up)] org-shiftmetaup)
8543 '([(meta shift down)] org-shiftmetadown)
8544 '([(meta shift left)] org-shiftmetaleft)
8545 '([(meta shift right)] org-shiftmetaright)
8546 '([?\e (up)] org-metaup)
8547 '([?\e (down)] org-metadown)
8548 '([?\e (left)] org-metaleft)
8549 '([?\e (right)] org-metaright)
8550 '([?\e (shift up)] org-shiftmetaup)
8551 '([?\e (shift down)] org-shiftmetadown)
8552 '([?\e (shift left)] org-shiftmetaleft)
8553 '([?\e (shift right)] org-shiftmetaright)
8554 '([(shift up)] org-shiftup)
8555 '([(shift down)] org-shiftdown)
8556 '([(shift left)] org-shiftleft)
8557 '([(shift right)] org-shiftright)
8558 '("\C-c\C-c" org-ctrl-c-ctrl-c)
8559 '("\M-q" fill-paragraph)
8561 '("\C-c-" org-cycle-list-bullet)))
8563 (while (setq elt (pop bindings))
8564 (setq nfunc (1+ nfunc))
8565 (setq key (org-key (car elt))
8567 cmd (orgstruct-make-binding fun nfunc key))
8568 (org-defkey orgstruct-mode-map key cmd))
8570 ;; Prevent an error for users who forgot to make autoloads
8571 (require 'org-element)
8573 ;; Special treatment needed for TAB and RET
8574 (org-defkey orgstruct-mode-map [(tab)]
8575 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
8576 (org-defkey orgstruct-mode-map "\C-i"
8577 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
8579 (org-defkey orgstruct-mode-map "\M-\C-m"
8580 (orgstruct-make-binding 'org-insert-heading 105
8581 "\M-\C-m" [(meta return)]))
8582 (org-defkey orgstruct-mode-map [(meta return)]
8583 (orgstruct-make-binding 'org-insert-heading 106
8584 [(meta return)] "\M-\C-m"))
8586 (org-defkey orgstruct-mode-map [(shift meta return)]
8587 (orgstruct-make-binding 'org-insert-todo-heading 107
8588 [(meta return)] "\M-\C-m"))
8590 (org-defkey orgstruct-mode-map "\e\C-m"
8591 (orgstruct-make-binding 'org-insert-heading 108
8592 "\e\C-m" [?\e (return)]))
8593 (org-defkey orgstruct-mode-map [?\e (return)]
8594 (orgstruct-make-binding 'org-insert-heading 109
8595 [?\e (return)] "\e\C-m"))
8596 (org-defkey orgstruct-mode-map [?\e (shift return)]
8597 (orgstruct-make-binding 'org-insert-todo-heading 110
8598 [?\e (return)] "\e\C-m"))
8600 (unless org-local-vars
8601 (setq org-local-vars (org-get-local-variables)))
8605 (defun orgstruct-make-binding (fun n &rest keys)
8606 "Create a function for binding in the structure minor mode.
8607 FUN is the command to call inside a table. N is used to create a unique
8608 command name. KEYS are keys that should be checked in for a command
8609 to execute outside of tables."
8612 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
8614 (concat "In Structure, run `" (symbol-name fun) "'.\n"
8615 "Outside of structure, run the binding of `"
8616 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
8620 `(org-context-p 'headline 'item
8621 (and orgstruct-is-++
8622 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
8624 (list 'org-run-like-in-org-mode (list 'quote fun))
8625 (list 'let '(orgstruct-mode)
8626 (list 'call-interactively
8629 (list 'key-binding k))
8631 '('orgstruct-error))))))))
8633 (defun org-contextualize-keys (alist contexts)
8634 "Return valid elements in ALIST depending on CONTEXTS.
8636 `org-agenda-custom-commands' or `org-capture-templates' are the
8637 values used for ALIST, and `org-agenda-custom-commands-contexts'
8638 or `org-capture-templates-contexts' are the associated contexts
8641 ;; normalize contexts
8643 (lambda(c) (cond ((listp (cadr c))
8644 (list (car c) (car c) (cadr c)))
8645 ((string= "" (cadr c))
8646 (list (car c) (car c) (caddr c)))
8649 ;; loop over all commands or templates
8650 (while (setq c (pop a))
8653 ((not (assoc (car c) contexts))
8655 ((and (assoc (car c) contexts)
8656 (setq vrules (org-contextualize-validate-key
8659 (when (not (equal (car vr) (cadr vr)))
8660 (setq repl vr))) vrules)
8661 (if (not repl) (push c r)
8662 (push (cadr repl) s)
8665 (cdr (or (assoc (cadr repl) alist)
8666 (error "Undefined key `%s' as contextual replacement for `%s'"
8667 (cadr repl) (car c)))))
8669 ;; Return limited ALIST, possibly with keys modified, and deduplicated
8674 (let ((tpl (car x)))
8678 (equal y tpl)) s))) x)))
8681 (defun org-contextualize-validate-key (key contexts)
8682 "Check CONTEXTS for agenda or capture KEY."
8684 (while (setq r (pop contexts))
8688 (and (equal key (car r))
8689 (if (functionp rr) (funcall rr)
8690 (or (and (eq (car rr) 'in-file)
8692 (string-match (cdr rr) (buffer-file-name)))
8693 (and (eq (car rr) 'in-mode)
8694 (string-match (cdr rr) (symbol-name major-mode)))
8695 (when (and (eq (car rr) 'not-in-file)
8697 (not (string-match (cdr rr) (buffer-file-name))))
8698 (when (eq (car rr) 'not-in-mode)
8699 (not (string-match (cdr rr) (symbol-name major-mode)))))))
8702 (delete-dups (delq nil res))))
8704 (defun org-context-p (&rest contexts)
8705 "Check if local context is any of CONTEXTS.
8706 Possible values in the list of contexts are `table', `headline', and `item'."
8707 (let ((pos (point)))
8708 (goto-char (point-at-bol))
8709 (prog1 (or (and (memq 'table contexts)
8710 (looking-at "[ \t]*|"))
8711 (and (memq 'headline contexts)
8712 (looking-at org-outline-regexp))
8713 (and (memq 'item contexts)
8714 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
8715 (and (memq 'item-body contexts)
8719 (defun org-get-local-variables ()
8720 "Return a list of all local variables in an Org mode buffer."
8722 (with-current-buffer (get-buffer-create "*Org tmp*")
8725 (setq varlist (buffer-local-variables)))
8726 (kill-buffer "*Org tmp*")
8733 (list (car x) (list 'quote (cdr x)))))
8735 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
8736 (symbol-name (car x)))
8740 (defun org-clone-local-variables (from-buffer &optional regexp)
8741 "Clone local variables from FROM-BUFFER.
8742 Optional argument REGEXP selects variables to clone."
8745 (and (symbolp (car pair))
8747 (string-match regexp (symbol-name (car pair))))
8748 (set (make-local-variable (car pair))
8750 (buffer-local-variables from-buffer)))
8753 (defun org-run-like-in-org-mode (cmd)
8754 "Run a command, pretending that the current buffer is in Org-mode.
8755 This will temporarily bind local variables that are typically bound in
8756 Org-mode to the values they have in Org-mode, and then interactively
8758 (org-load-modules-maybe)
8759 (unless org-local-vars
8760 (setq org-local-vars (org-get-local-variables)))
8761 (eval (list 'let org-local-vars
8762 (list 'call-interactively (list 'quote cmd)))))
8766 (defun org-get-category (&optional pos force-refresh)
8767 "Get the category applying to position POS."
8769 (if force-refresh (org-refresh-category-properties))
8770 (let ((pos (or pos (point))))
8771 (or (get-text-property pos 'org-category)
8772 (progn (org-refresh-category-properties)
8773 (get-text-property pos 'org-category))))))
8775 (defun org-refresh-category-properties ()
8776 "Refresh category text properties in the buffer."
8777 (let ((case-fold-search t)
8778 (inhibit-read-only t)
8780 ((null org-category)
8781 (if buffer-file-name
8782 (file-name-sans-extension
8783 (file-name-nondirectory buffer-file-name))
8785 ((symbolp org-category) (symbol-name org-category))
8787 beg end cat pos optionp)
8792 (goto-char (point-min))
8793 (put-text-property (point) (point-max) 'org-category def-cat)
8794 (while (re-search-forward
8795 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8796 (setq pos (match-end 0)
8797 optionp (equal (char-after (match-beginning 0)) ?#)
8798 cat (org-trim (match-string 2)))
8800 (setq beg (point-at-bol) end (point-max))
8801 (org-back-to-heading t)
8802 (setq beg (point) end (org-end-of-subtree t t)))
8803 (put-text-property beg end 'org-category cat)
8804 (put-text-property beg end 'org-category-position beg)
8805 (goto-char pos)))))))
8810 ;;; Link abbreviations
8812 (defun org-link-expand-abbrev (link)
8813 "Apply replacements as defined in `org-link-abbrev-alist'."
8814 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
8815 (let* ((key (match-string 1 link))
8816 (as (or (assoc key org-link-abbrev-alist-local)
8817 (assoc key org-link-abbrev-alist)))
8818 (tag (and (match-end 2) (match-string 3 link)))
8824 ((symbolp rpl) (funcall rpl tag))
8825 ((string-match "%(\\([^)]+\\))" rpl)
8826 (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
8827 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
8828 ((string-match "%h" rpl)
8829 (replace-match (url-hexify-string (or tag "")) t t rpl))
8830 (t (concat rpl tag)))))
8833 ;;; Storing and inserting links
8835 (defvar org-insert-link-history nil
8836 "Minibuffer history for links inserted with `org-insert-link'.")
8838 (defvar org-stored-links nil
8839 "Contains the links stored with `org-store-link'.")
8841 (defvar org-store-link-plist nil
8842 "Plist with info about the most recently link created with `org-store-link'.")
8844 (defvar org-link-protocols nil
8845 "Link protocols added to Org-mode using `org-add-link-type'.")
8847 (defvar org-store-link-functions nil
8848 "List of functions that are called to create and store a link.
8849 Each function will be called in turn until one returns a non-nil
8850 value. Each function should check if it is responsible for creating
8851 this link (for example by looking at the major mode).
8852 If not, it must exit and return nil.
8853 If yes, it should return a non-nil value after a calling
8854 `org-store-link-props' with a list of properties and values.
8855 Special properties are:
8857 :type The link prefix, like \"http\". This must be given.
8858 :link The link, like \"http://www.astro.uva.nl/~dominik\".
8859 This is obligatory as well.
8860 :description Optional default description for the second pair
8861 of brackets in an Org-mode link. The user can still change
8862 this when inserting this link into an Org-mode buffer.
8864 In addition to these, any additional properties can be specified
8865 and then used in capture templates.")
8867 (defun org-add-link-type (type &optional follow export)
8868 "Add TYPE to the list of `org-link-types'.
8869 Re-compute all regular expressions depending on `org-link-types'
8871 FOLLOW and EXPORT are two functions.
8873 FOLLOW should take the link path as the single argument and do whatever
8874 is necessary to follow the link, for example find a file or display
8877 EXPORT should format the link path for export to one of the export formats.
8878 It should be a function accepting three arguments:
8880 path the path of the link, the text after the prefix (like \"http:\")
8881 desc the description of the link, if any, or a description added by
8882 org-export-normalize-links if there is none
8883 format the export format, a symbol like `html' or `latex' or `ascii'..
8885 The function may use the FORMAT information to return different values
8886 depending on the format. The return value will be put literally into
8887 the exported file. If the return value is nil, this means Org should
8888 do what it normally does with links which do not have EXPORT defined.
8890 Org-mode has a built-in default for exporting links. If you are happy with
8891 this default, there is no need to define an export function for the link
8892 type. For a simple example of an export function, see `org-bbdb.el'."
8893 (add-to-list 'org-link-types type t)
8894 (org-make-link-regexps)
8895 (if (assoc type org-link-protocols)
8896 (setcdr (assoc type org-link-protocols) (list follow export))
8897 (push (list type follow export) org-link-protocols)))
8899 (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
8900 (defvar org-link-to-org-use-id) ; Defined in org-id.el
8903 (defun org-store-link (arg)
8904 "\\<org-mode-map>Store an org-link to the current location.
8905 This link is added to `org-stored-links' and can later be inserted
8906 into an org-buffer with \\[org-insert-link].
8908 For some link types, a prefix arg is interpreted.
8909 For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
8910 For file links, arg negates `org-context-in-file-links'.
8912 A double prefix arg force skipping storing functions that are not
8913 part of Org's core."
8915 (org-load-modules-maybe)
8916 (setq org-store-link-plist nil) ; reset
8917 (org-with-limited-levels
8918 (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
8920 ((and (not (equal arg '(16)))
8923 nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
8924 org-store-link-functions))
8925 sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
8926 (or (and (cdr sfuns)
8928 (completing-read "Which function for creating the link? "
8929 sfunsn t (car sfunsn)))))
8930 (funcall (caar sfuns)))
8931 (setq link (plist-get org-store-link-plist :link)
8932 desc (or (plist-get org-store-link-plist :description) link))))
8933 ((org-src-edit-buffer-p)
8935 (while (or (not label)
8939 (goto-char (point-min))
8941 (regexp-quote (format org-coderef-label-format label))
8943 (when label (message "Label exists already") (sit-for 2))
8944 (setq label (read-string "Code line label: " label)))
8946 (setq link (format org-coderef-label-format label))
8947 (setq gc (- 79 (length link)))
8948 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
8950 (setq link (concat "(" label ")") desc nil)))
8952 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
8953 ;; We are in the agenda, link to referenced location
8954 (let ((m (or (get-text-property (point) 'org-hd-marker)
8955 (get-text-property (point) 'org-marker))))
8957 (org-with-point-at m
8959 (if (org-called-interactively-p 'any)
8960 (call-interactively 'org-store-link)
8961 (org-store-link nil)))))))
8963 ((eq major-mode 'calendar-mode)
8964 (let ((cd (calendar-cursor-to-date)))
8967 (car org-time-stamp-formats)
8969 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8971 (org-store-link-props :type "calendar" :date cd)))
8973 ((eq major-mode 'help-mode)
8974 (setq link (concat "help:" (save-excursion
8975 (goto-char (point-min))
8976 (looking-at "^[^ ]+")
8978 (org-store-link-props :type "help"))
8980 ((eq major-mode 'w3-mode)
8981 (setq cpltxt (if (and (buffer-name)
8982 (not (string-match "Untitled" (buffer-name))))
8985 link (url-view-url t))
8986 (org-store-link-props :type "w3" :url (url-view-url t)))
8988 ((setq search (run-hook-with-args-until-success
8989 'org-create-file-search-functions))
8990 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8992 (setq cpltxt (or description link)))
8994 ((eq major-mode 'image-mode)
8995 (setq cpltxt (concat "file:"
8996 (abbreviate-file-name buffer-file-name))
8998 (org-store-link-props :type "image" :file buffer-file-name))
9000 ((eq major-mode 'dired-mode)
9001 ;; link to the file in the current line
9002 (let ((file (dired-get-filename nil t)))
9004 (abbreviate-file-name
9005 (expand-file-name (dired-get-filename nil t)))
9006 ;; otherwise, no file so use current directory.
9008 (setq cpltxt (concat "file:" file)
9011 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
9012 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
9014 ((org-in-regexp "<<\\(.*?\\)>>")
9017 (abbreviate-file-name
9018 (buffer-file-name (buffer-base-buffer)))
9019 "::" (match-string 1))
9021 ((and (featurep 'org-id)
9022 (or (eq org-link-to-org-use-id t)
9023 (and (org-called-interactively-p 'any)
9024 (or (eq org-link-to-org-use-id 'create-if-interactive)
9025 (and (eq org-link-to-org-use-id
9026 'create-if-interactive-and-no-custom-id)
9028 (and org-link-to-org-use-id (org-entry-get nil "ID"))))
9029 ;; We can make a link using the ID.
9030 (setq link (condition-case nil
9031 (prog1 (org-id-store-link)
9032 (setq desc (plist-get org-store-link-plist :description)))
9034 ;; probably before first headline, link to file only
9036 (abbreviate-file-name
9037 (buffer-file-name (buffer-base-buffer))))))))
9039 ;; Just link to current headline
9040 (setq cpltxt (concat "file:"
9041 (abbreviate-file-name
9042 (buffer-file-name (buffer-base-buffer)))))
9043 ;; Add a context search string
9044 (when (org-xor org-context-in-file-links arg)
9045 (let* ((ee (org-element-at-point))
9046 (et (org-element-type ee))
9047 (ev (plist-get (cadr ee) :value)))
9049 ((org-at-heading-p) nil)
9050 ((eq et 'keyword) ev)
9051 ((org-region-active-p)
9052 (buffer-substring (region-beginning) (region-end)))))
9053 (when (or (null txt) (string-match "\\S-" txt))
9057 (org-make-org-heading-search-string txt)
9059 desc (or (and (eq et 'keyword) ev)
9060 (nth 4 (ignore-errors (org-heading-components)))
9062 (if (string-match "::\\'" cpltxt)
9063 (setq cpltxt (substring cpltxt 0 -2)))
9064 (setq link cpltxt))))
9066 ((buffer-file-name (buffer-base-buffer))
9067 ;; Just link to this file here.
9068 (setq cpltxt (concat "file:"
9069 (abbreviate-file-name
9070 (buffer-file-name (buffer-base-buffer)))))
9071 ;; Add a context string
9072 (when (org-xor org-context-in-file-links arg)
9073 (setq txt (if (org-region-active-p)
9074 (buffer-substring (region-beginning) (region-end))
9075 (buffer-substring (point-at-bol) (point-at-eol))))
9076 ;; Only use search option if there is some text.
9077 (when (string-match "\\S-" txt)
9079 (concat cpltxt "::" (org-make-org-heading-search-string txt))
9083 ((org-called-interactively-p 'interactive)
9084 (user-error "No method for storing a link from this buffer"))
9086 (t (setq link nil)))
9088 (if (consp link) (setq cpltxt (car link) link (cdr link)))
9089 (setq link (or link cpltxt)
9090 desc (or desc cpltxt))
9091 (cond ((equal desc "NONE") (setq desc nil))
9092 ((string-match org-bracket-link-regexp desc)
9093 (setq desc (replace-regexp-in-string
9094 org-bracket-link-regexp
9095 (concat "\\3" (if (equal (length (match-string 0 desc))
9096 (length desc)) "*" "")) desc))))
9098 (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
9100 (setq org-stored-links
9101 (cons (list link desc) org-stored-links))
9102 (message "Stored: %s" (or desc link))
9104 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
9106 (setq org-stored-links
9107 (cons (list link desc) org-stored-links))))
9108 (or agenda-link (and link (org-make-link-string link desc)))))))
9110 (defun org-store-link-props (&rest plist)
9111 "Store link properties, extract names and addresses."
9113 (when (setq x (plist-get plist :from))
9114 (setq adr (mail-extract-address-components x))
9115 (setq plist (plist-put plist :fromname (car adr)))
9116 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
9117 (when (setq x (plist-get plist :to))
9118 (setq adr (mail-extract-address-components x))
9119 (setq plist (plist-put plist :toname (car adr)))
9120 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
9121 (let ((from (plist-get plist :from))
9122 (to (plist-get plist :to)))
9123 (when (and from to org-from-is-user-regexp)
9125 (plist-put plist :fromto
9126 (if (string-match org-from-is-user-regexp from)
9128 (concat "from %f"))))))
9129 (setq org-store-link-plist plist))
9131 (defun org-add-link-props (&rest plist)
9132 "Add these properties to the link property list."
9135 (setq key (pop plist) value (pop plist))
9136 (setq org-store-link-plist
9137 (plist-put org-store-link-plist key value)))))
9139 (defun org-email-link-description (&optional fmt)
9140 "Return the description part of an email link.
9141 This takes information from `org-store-link-plist' and formats it
9142 according to FMT (default from `org-email-link-description-format')."
9143 (setq fmt (or fmt org-email-link-description-format))
9144 (let* ((p org-store-link-plist)
9145 (to (plist-get p :toaddress))
9146 (from (plist-get p :fromaddress))
9149 (cons "%c" (plist-get p :fromto))
9150 (cons "%F" (plist-get p :from))
9151 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
9152 (cons "%T" (plist-get p :to))
9153 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
9154 (cons "%s" (plist-get p :subject))
9155 (cons "%d" (plist-get p :date))
9156 (cons "%m" (plist-get p :message-id)))))
9157 (when (string-match "%c" fmt)
9158 ;; Check if the user wrote this message
9159 (if (and org-from-is-user-regexp from to
9160 (save-match-data (string-match org-from-is-user-regexp from)))
9161 (setq fmt (replace-match "to %t" t t fmt))
9162 (setq fmt (replace-match "from %f" t t fmt))))
9163 (org-replace-escapes fmt table)))
9165 (defun org-make-org-heading-search-string (&optional string)
9166 "Make search string for the current headline or STRING."
9168 (and (derived-mode-p 'org-mode)
9170 (org-back-to-heading t)
9171 (plist-get (cadr (org-element-at-point))
9173 (lines org-context-in-file-links))
9174 (or string (setq s (concat "*" s))) ; Add * for headlines
9175 (when (and string (integerp lines) (> lines 0))
9176 (let ((slines (org-split-string s "\n")))
9177 (when (< lines (length slines))
9180 (reverse (nthcdr (- (length slines) lines)
9181 (reverse slines))) "\n")))))
9182 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
9184 (defun org-make-link-string (link &optional description)
9185 "Make a link with brackets, consisting of LINK and DESCRIPTION."
9186 (unless (string-match "\\S-" link)
9187 (error "Empty link"))
9188 (when (and description
9189 (stringp description)
9190 (not (string-match "\\S-" description)))
9191 (setq description nil))
9192 (when (stringp description)
9193 ;; Remove brackets from the description, they are fatal.
9194 (while (string-match "\\[" description)
9195 (setq description (replace-match "{" t t description)))
9196 (while (string-match "\\]" description)
9197 (setq description (replace-match "}" t t description))))
9198 (when (equal link description)
9199 ;; No description needed, it is identical
9200 (setq description nil))
9201 (when (and (not description)
9202 (not (string-match (org-image-file-name-regexp) link))
9203 (not (equal link (org-link-escape link))))
9204 (setq description (org-extract-attributes link)))
9206 (cond ((string-match (org-image-file-name-regexp) link) link)
9207 ((string-match org-link-types-re link)
9208 (concat (match-string 1 link)
9209 (org-link-escape (substring link (match-end 1)))))
9210 (t (org-link-escape link))))
9211 (concat "[[" link "]"
9212 (if description (concat "[" description "]") "")
9215 (defconst org-link-escape-chars
9216 '(?\ ?\[ ?\] ?\; ?\= ?\+)
9217 "List of characters that should be escaped in link.
9218 This is the list that is used for internal purposes.")
9220 (defconst org-link-escape-chars-browser
9222 "List of escapes for characters that are problematic in links.
9223 This is the list that is used before handing over to the browser.")
9225 (defun org-link-escape (text &optional table merge)
9226 "Return percent escaped representation of TEXT.
9227 TEXT is a string with the text to escape.
9228 Optional argument TABLE is a list with characters that should be
9229 escaped. When nil, `org-link-escape-chars' is used.
9230 If optional argument MERGE is set, merge TABLE into
9231 `org-link-escape-chars'."
9234 (mapc (lambda (defchr)
9235 (unless (member defchr table)
9236 (setq table (cons defchr table)))) org-link-escape-chars))
9238 (setq table org-link-escape-chars)))
9241 (if (or (member char table)
9242 (and (or (< char 32) (= char 37) (> char 126))
9244 (mapconcat (lambda (sequence-element)
9245 (format "%%%.2X" sequence-element))
9246 (or (encode-coding-char char 'utf-8)
9247 (error "Unable to percent escape character: %s"
9248 (char-to-string char))) "")
9249 (char-to-string char))) text ""))
9251 (defun org-link-unescape (str)
9252 "Unhex hexified Unicode strings as returned from the JavaScript function
9253 encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
9254 (unless (and (null str) (string= "" str))
9255 (let ((pos 0) (case-fold-search t) unhexed)
9256 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
9257 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
9258 (setq str (replace-match unhexed t t str))
9259 (setq pos (+ pos (length unhexed))))))
9262 (defun org-link-unescape-compound (hex)
9263 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
9264 Note: this function also decodes single byte encodings like
9265 `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
9267 (let* ((bytes (cdr (split-string hex "%")))
9272 (let* ((val (string-to-number (pop bytes) 16))
9276 ((>= val 252) (cons 6 252))
9277 ((>= val 248) (cons 5 248))
9278 ((>= val 240) (cons 4 240))
9279 ((>= val 224) (cons 3 224))
9280 ((>= val 192) (cons 2 192))
9283 (if (>= val 192) (setq eat (car shift-xor)))
9284 (setq val (logxor val (cdr shift-xor)))
9285 (setq sum (+ (lsh sum (car shift-xor)) val))
9286 (if (> eat 0) (setq eat (- eat 1)))
9288 ((= 0 eat) ;multi byte
9289 (setq ret (concat ret (org-char-to-string sum)))
9291 ((not bytes) ; single byte(s)
9292 (setq ret (org-link-unescape-single-byte-sequence hex))))
9293 )) ;; end (while bytes
9296 (defun org-link-unescape-single-byte-sequence (hex)
9297 "Unhexify hex-encoded single byte character sequences."
9298 (mapconcat (lambda (byte)
9299 (char-to-string (string-to-number byte 16)))
9300 (cdr (split-string hex "%")) ""))
9302 (defun org-xor (a b)
9306 (defun org-fixup-message-id-for-http (s)
9307 "Replace special characters in a message id, so it can be used in an http query."
9308 (when (string-match "%" s)
9309 (setq s (mapconcat (lambda (c)
9312 (char-to-string c)))
9314 (while (string-match "<" s)
9315 (setq s (replace-match "%3C" t t s)))
9316 (while (string-match ">" s)
9317 (setq s (replace-match "%3E" t t s)))
9318 (while (string-match "@" s)
9319 (setq s (replace-match "%40" t t s)))
9322 (defun org-link-prettify (link)
9323 "Return a human-readable representation of LINK.
9324 The car of LINK must be a raw link the cdr of LINK must be either
9325 a link description or nil."
9326 (let ((desc (or (cadr link) "<no description>")))
9327 (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
9328 "<" (car link) ">")))
9331 (defun org-insert-link-global ()
9332 "Insert a link like Org-mode does.
9333 This command can be called in any mode to insert a link in Org-mode syntax."
9335 (org-load-modules-maybe)
9336 (org-run-like-in-org-mode 'org-insert-link))
9338 (defun org-insert-all-links (&optional keep)
9339 "Insert all links in `org-stored-links'."
9341 (let ((links (copy-sequence org-stored-links)) l)
9342 (while (setq l (if keep (pop links) (pop org-stored-links)))
9344 (org-insert-link nil (car l) (cadr l))
9347 (defun org-link-fontify-links-to-this-file ()
9348 "Fontify links to the current file in `org-stored-links'."
9349 (let ((f (buffer-file-name)) a b)
9350 (setq a (mapcar (lambda(l)
9352 (when (and (string-match "^file:\\(.+\\)::" ll)
9353 (equal f (expand-file-name (match-string 1 ll))))
9356 (when (featurep 'org-id)
9357 (setq b (mapcar (lambda(l)
9359 (when (and (string-match "^id:\\(.+\\)$" ll)
9360 (equal f (expand-file-name
9361 (or (org-id-find-id-file
9362 (match-string 1 ll)) ""))))
9366 (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
9367 (delq nil (append a b)))))
9369 (defvar org-link-links-in-this-file nil)
9370 (defun org-insert-link (&optional complete-file link-location default-description)
9371 "Insert a link. At the prompt, enter the link.
9373 Completion can be used to insert any of the link protocol prefixes like
9376 The history can be used to select a link previously stored with
9377 `org-store-link'. When the empty string is entered (i.e. if you just
9378 press RET at the prompt), the link defaults to the most recently
9379 stored link. As SPC triggers completion in the minibuffer, you need to
9380 use M-SPC or C-q SPC to force the insertion of a space character.
9382 You will also be prompted for a description, and if one is given, it will
9383 be displayed in the buffer instead of the link.
9385 If there is already a link at point, this command will allow you to edit link
9386 and description parts.
9388 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9389 be selected using completion. The path to the file will be relative to the
9390 current directory if the file is in the current directory or a subdirectory.
9391 Otherwise, the link will be the absolute path as completed in the minibuffer
9392 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9393 option `org-link-file-path-type'.
9395 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9396 the current directory or below.
9398 With three \\[universal-argument] prefixes, negate the meaning of
9399 `org-keep-stored-link-after-insertion'.
9401 If `org-make-link-description-function' is non-nil, this function will be
9402 called with the link target, and the result will be the default
9405 If the LINK-LOCATION parameter is non-nil, this value will be
9406 used as the link location instead of reading one interactively.
9408 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9409 be used as the default description."
9411 (let* ((wcf (current-window-configuration))
9412 (origbuf (current-buffer))
9413 (region (if (org-region-active-p)
9414 (buffer-substring (region-beginning) (region-end))))
9415 (remove (and region (list (region-beginning) (region-end))))
9417 tmphist ; byte-compile incorrectly complains about this
9418 (link link-location)
9419 (abbrevs org-link-abbrev-alist-local)
9420 entry file all-prefixes auto-desc)
9422 (link-location) ; specified by arg, just use it.
9423 ((org-in-regexp org-bracket-link-regexp 1)
9424 ;; We do have a link at point, and we are going to edit it.
9425 (setq remove (list (match-beginning 0) (match-end 0)))
9426 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
9427 (setq link (read-string "Link: "
9429 (org-match-string-no-properties 1)))))
9430 ((or (org-in-regexp org-angle-link-re)
9431 (org-in-regexp org-plain-link-re))
9432 ;; Convert to bracket link
9433 (setq remove (list (match-beginning 0) (match-end 0))
9434 link (read-string "Link: "
9435 (org-remove-angle-brackets (match-string 0)))))
9436 ((member complete-file '((4) (16)))
9437 ;; Completing read for file names.
9438 (setq link (org-file-complete-link complete-file)))
9440 ;; Read link, with completion for stored links.
9441 (org-link-fontify-links-to-this-file)
9442 (org-switch-to-buffer-other-window "*Org Links*")
9443 (with-current-buffer "*Org Links*"
9445 (insert "Insert a link.
9446 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9447 (when org-stored-links
9448 (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9449 (insert (mapconcat 'org-link-prettify
9450 (reverse org-stored-links) "\n")))
9451 (goto-char (point-min)))
9452 (let ((cw (selected-window)))
9453 (select-window (get-buffer-window "*Org Links*" 'visible))
9454 (with-current-buffer "*Org Links*" (setq truncate-lines t))
9455 (unless (pos-visible-in-window-p (point-max))
9456 (org-fit-window-to-buffer))
9457 (and (window-live-p cw) (select-window cw)))
9458 ;; Fake a link history, containing the stored links.
9459 (setq tmphist (append (mapcar 'car org-stored-links)
9460 org-insert-link-history))
9461 (setq all-prefixes (append (mapcar 'car abbrevs)
9462 (mapcar 'car org-link-abbrev-alist)
9467 (org-completing-read
9470 (mapcar (lambda (x) (concat x ":"))
9472 (mapcar 'car org-stored-links))
9475 (caar org-stored-links)))
9476 (if (not (string-match "\\S-" link))
9477 (error "No link selected"))
9479 (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
9481 (if (or (member link all-prefixes)
9482 (and (equal ":" (substring link -1))
9483 (member (substring link 0 -1) all-prefixes)
9484 (setq link (substring link 0 -1))))
9485 (setq link (with-current-buffer origbuf
9486 (org-link-try-special-completion link)))))
9487 (set-window-configuration wcf)
9488 (kill-buffer "*Org Links*"))
9489 (setq entry (assoc link org-stored-links))
9490 (or entry (push link org-insert-link-history))
9491 (setq desc (or desc (nth 1 entry)))))
9493 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
9494 (not org-keep-stored-link-after-insertion))
9495 (setq org-stored-links (delq (assoc link org-stored-links)
9498 (if (string-match org-plain-link-re link)
9499 ;; URL-like link, normalize the use of angular brackets.
9500 (setq link (org-remove-angle-brackets link)))
9502 ;; Check if we are linking to the current file with a search
9503 ;; option If yes, simplify the link by using only the search
9505 (when (and buffer-file-name
9506 (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
9507 (let* ((path (match-string 1 link))
9508 (case-fold-search nil)
9509 (search (match-string 2 link)))
9511 (if (equal (file-truename buffer-file-name) (file-truename path))
9512 ;; We are linking to this same file, with a search option
9513 (setq link search)))))
9515 ;; Check if we can/should use a relative path. If yes, simplify the link
9516 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
9517 (let* ((type (match-string 1 link))
9518 (path (match-string 2 link))
9520 (case-fold-search nil))
9522 ((or (eq org-link-file-path-type 'absolute)
9523 (equal complete-file '(16)))
9524 (setq path (abbreviate-file-name (expand-file-name path))))
9525 ((eq org-link-file-path-type 'noabbrev)
9526 (setq path (expand-file-name path)))
9527 ((eq org-link-file-path-type 'relative)
9528 (setq path (file-relative-name path)))
9531 (if (string-match (concat "^" (regexp-quote
9533 (file-name-as-directory
9534 default-directory))))
9535 (expand-file-name path))
9536 ;; We are linking a file with relative path name.
9537 (setq path (substring (expand-file-name path)
9539 (setq path (abbreviate-file-name (expand-file-name path)))))))
9540 (setq link (concat type path))
9541 (if (equal desc origpath)
9544 (if org-make-link-description-function
9546 (or (condition-case nil
9547 (funcall org-make-link-description-function link desc)
9548 (error (progn (message "Can't get link description from `%s'"
9549 (symbol-name org-make-link-description-function))
9551 (read-string "Description: " default-description)))
9552 (if default-description (setq desc default-description)
9553 (setq desc (or (and auto-desc desc)
9554 (read-string "Description: " desc)))))
9556 (unless (string-match "\\S-" desc) (setq desc nil))
9557 (if remove (apply 'delete-region remove))
9558 (insert (org-make-link-string link desc))))
9560 (defun org-link-try-special-completion (type)
9561 "If there is completion support for link type TYPE, offer it."
9562 (let ((fun (intern (concat "org-" type "-complete-link"))))
9565 (read-string "Link (no completion support): " (concat type ":")))))
9567 (defun org-file-complete-link (&optional arg)
9568 "Create a file link using completion."
9570 (setq file (org-iread-file-name "File: "))
9571 (let ((pwd (file-name-as-directory (expand-file-name ".")))
9572 (pwd1 (file-name-as-directory (abbreviate-file-name
9573 (expand-file-name ".")))))
9578 (abbreviate-file-name (expand-file-name file)))))
9579 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
9580 (setq link (concat "file:" (match-string 1 file))))
9581 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
9582 (expand-file-name file))
9584 "file:" (match-string 1 (expand-file-name file)))))
9585 (t (setq link (concat "file:" file)))))
9588 (defun org-iread-file-name (&rest args)
9589 "Read-file-name using `ido-mode' speedup if available.
9590 ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
9591 See `read-file-name' for a description of parameters."
9592 (org-without-partial-completion
9593 (if (and org-completion-use-ido
9594 (fboundp 'ido-read-file-name)
9595 (boundp 'ido-mode) ido-mode
9596 (listp (second args)))
9597 (let ((ido-enter-matching-directory nil))
9598 (apply 'ido-read-file-name args))
9599 (apply 'read-file-name args))))
9601 (defun org-completing-read (&rest args)
9602 "Completing-read with SPACE being a normal character."
9603 (let ((enable-recursive-minibuffers t)
9604 (minibuffer-local-completion-map
9605 (copy-keymap minibuffer-local-completion-map)))
9606 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
9607 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
9608 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
9609 (apply 'org-icompleting-read args)))
9611 (defun org-completing-read-no-i (&rest args)
9612 (let (org-completion-use-ido org-completion-use-iswitchb)
9613 (apply 'org-completing-read args)))
9615 (defun org-iswitchb-completing-read (prompt choices &rest args)
9616 "Use iswitch as a completing-read replacement to choose from choices.
9617 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
9619 (let* ((iswitchb-use-virtual-buffers nil)
9620 (iswitchb-make-buflist-hook
9622 (setq iswitchb-temp-buflist choices))))
9623 (iswitchb-read-buffer prompt)))
9625 (defun org-icompleting-read (&rest args)
9626 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
9627 (org-without-partial-completion
9628 (if (and org-completion-use-ido
9629 (fboundp 'ido-completing-read)
9630 (boundp 'ido-mode) ido-mode
9631 (listp (second args)))
9632 (let ((ido-enter-matching-directory nil))
9633 (apply 'ido-completing-read (concat (car args))
9634 (if (consp (car (nth 1 args)))
9635 (mapcar 'car (nth 1 args))
9638 (if (and org-completion-use-iswitchb
9639 (boundp 'iswitchb-mode) iswitchb-mode
9640 (listp (second args)))
9641 (apply 'org-iswitchb-completing-read (concat (car args))
9642 (if (consp (car (nth 1 args)))
9643 (mapcar 'car (nth 1 args))
9646 (apply 'completing-read args)))))
9648 (defun org-extract-attributes (s)
9649 "Extract the attributes cookie from a string and set as text property."
9650 (let (a attr (start 0) key value)
9652 (when (string-match "{{\\([^}]+\\)}}$" s)
9653 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
9654 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
9655 (setq key (match-string 1 a) value (match-string 2 a)
9657 attr (plist-put attr (intern key) value))))
9658 (org-add-props s nil 'org-attr attr))
9661 (defun org-extract-attributes-from-string (tag)
9662 (let (key value attr)
9663 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
9664 (setq key (match-string 1 tag) value (match-string 2 tag)
9665 tag (replace-match "" t t tag)
9666 attr (plist-put attr (intern key) value)))
9669 (defun org-attributes-to-string (plist)
9670 "Format a property list into an HTML attribute list."
9671 (let ((s "") key value)
9673 (setq key (pop plist) value (pop plist))
9675 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
9678 ;;; Opening/following a link
9680 (defvar org-link-search-failed nil)
9682 (defvar org-open-link-functions nil
9683 "Hook for functions finding a plain text link.
9684 These functions must take a single argument, the link content.
9685 They will be called for links that look like [[link text][description]]
9686 when LINK TEXT does not have a protocol like \"http:\" and does not look
9687 like a filename (e.g. \"./blue.png\").
9689 These functions will be called *before* Org attempts to resolve the
9690 link by doing text searches in the current buffer - so if you want a
9691 link \"[[target]]\" to still find \"<<target>>\", your function should
9692 handle this as a special case.
9694 When the function does handle the link, it must return a non-nil value.
9695 If it decides that it is not responsible for this link, it must return
9696 nil to indicate that that Org-mode can continue with other options
9697 like exact and fuzzy text search.")
9699 (defun org-next-link ()
9700 "Move forward to the next link.
9701 If the link is in hidden text, expose it."
9703 (when (and org-link-search-failed (eq this-command last-command))
9704 (goto-char (point-min))
9705 (message "Link search wrapped back to beginning of buffer"))
9706 (setq org-link-search-failed nil)
9707 (let* ((pos (point))
9709 (a (assoc :link ct)))
9710 (if a (goto-char (nth 2 a)))
9711 (if (re-search-forward org-any-link-re nil t)
9713 (goto-char (match-beginning 0))
9714 (if (outline-invisible-p) (org-show-context)))
9716 (setq org-link-search-failed t)
9717 (error "No further link found"))))
9719 (defun org-previous-link ()
9720 "Move backward to the previous link.
9721 If the link is in hidden text, expose it."
9723 (when (and org-link-search-failed (eq this-command last-command))
9724 (goto-char (point-max))
9725 (message "Link search wrapped back to end of buffer"))
9726 (setq org-link-search-failed nil)
9727 (let* ((pos (point))
9729 (a (assoc :link ct)))
9730 (if a (goto-char (nth 1 a)))
9731 (if (re-search-backward org-any-link-re nil t)
9733 (goto-char (match-beginning 0))
9734 (if (outline-invisible-p) (org-show-context)))
9736 (setq org-link-search-failed t)
9737 (error "No further link found"))))
9739 (defun org-translate-link (s)
9740 "Translate a link string if a translation function has been defined."
9741 (if (and org-link-translation-function
9742 (fboundp org-link-translation-function)
9743 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
9745 (setq s (funcall org-link-translation-function
9746 (match-string 1 s) (match-string 2 s)))
9747 (concat (car s) ":" (cdr s)))
9750 (defun org-translate-link-from-planner (type path)
9751 "Translate a link from Emacs Planner syntax so that Org can follow it.
9752 This is still an experimental function, your mileage may vary."
9754 ((member type '("http" "https" "news" "ftp"))
9755 ;; standard Internet links are the same.
9757 ((and (equal type "irc") (string-match "^//" path))
9758 ;; Planner has two / at the beginning of an irc link, we have 1.
9759 ;; We should have zero, actually....
9760 (setq path (substring path 1)))
9761 ((and (equal type "lisp") (string-match "^/" path))
9762 ;; Planner has a slash, we do not.
9763 (setq type "elisp" path (substring path 1)))
9764 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
9765 ;; A typical message link. Planner has the id after the final slash,
9766 ;; we separate it with a hash mark
9767 (setq path (concat (match-string 1 path) "#"
9768 (org-remove-angle-brackets (match-string 2 path)))))
9772 (defun org-find-file-at-mouse (ev)
9773 "Open file link or URL at mouse."
9775 (mouse-set-point ev)
9776 (org-open-at-point 'in-emacs))
9778 (defun org-open-at-mouse (ev)
9779 "Open file link or URL at mouse.
9780 See the docstring of `org-open-file' for details."
9782 (mouse-set-point ev)
9783 (if (eq major-mode 'org-agenda-mode)
9784 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
9785 (org-open-at-point))
9787 (defvar org-window-config-before-follow-link nil
9788 "The window configuration before following a link.
9789 This is saved in case the need arises to restore it.")
9791 (defvar org-open-link-marker (make-marker)
9792 "Marker pointing to the location where `org-open-at-point; was called.")
9795 (defun org-open-at-point-global ()
9796 "Follow a link like Org-mode does.
9797 This command can be called in any mode to follow a link that has
9800 (org-run-like-in-org-mode 'org-open-at-point))
9803 (defun org-open-link-from-string (s &optional arg reference-buffer)
9804 "Open a link in the string S, as if it was in Org-mode."
9805 (interactive "sLink: \nP")
9806 (let ((reference-buffer (or reference-buffer (current-buffer))))
9808 (let ((org-inhibit-startup (not reference-buffer)))
9811 (goto-char (point-min))
9812 (when reference-buffer
9813 (setq org-link-abbrev-alist-local
9814 (with-current-buffer reference-buffer
9815 org-link-abbrev-alist-local)))
9816 (org-open-at-point arg reference-buffer)))))
9818 (defvar org-open-at-point-functions nil
9819 "Hook that is run when following a link at point.
9821 Functions in this hook must return t if they identify and follow
9822 a link at point. If they don't find anything interesting at point,
9823 they must return nil.")
9825 (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
9826 (defun org-open-at-point (&optional arg reference-buffer)
9827 "Open link at or after point.
9828 If there is no link at point, this function will search forward up to
9829 the end of the current line.
9830 Normally, files will be opened by an appropriate application. If the
9831 optional prefix argument ARG is non-nil, Emacs will visit the file.
9832 With a double prefix argument, try to open outside of Emacs, in the
9833 application the system uses for this file type."
9835 ;; if in a code block, then open the block's results
9836 (unless (call-interactively #'org-babel-open-src-block-result)
9837 (org-load-modules-maybe)
9838 (move-marker org-open-link-marker (point))
9839 (setq org-window-config-before-follow-link (current-window-configuration))
9840 (org-remove-occur-highlights nil nil t)
9842 ((and (org-at-heading-p)
9843 (not (org-at-timestamp-p t))
9845 (concat org-plain-link-re "\\|"
9846 org-bracket-link-regexp "\\|"
9847 org-angle-link-re "\\|"
9848 "[ \t]:[^ \t\n]+:[ \t]*$")))
9849 (not (get-text-property (point) 'org-linked-text)))
9850 (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
9852 (lkend (cdr lkall)))
9854 (prog1 (search-forward lk nil lkend)
9855 (goto-char (match-beginning 0))
9856 (org-open-at-point))))
9857 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
9858 ((run-hook-with-args-until-success 'org-open-at-point-functions))
9859 ((and (org-at-timestamp-p t)
9860 (not (org-in-regexp org-bracket-link-regexp)))
9861 (org-follow-timestamp-link))
9862 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
9863 (not (org-in-regexp org-any-link-re)))
9864 (org-footnote-action))
9866 (let (type path link line search (pos (point)))
9869 (skip-chars-forward "^]\n\r")
9870 (when (org-in-regexp org-bracket-link-regexp 1)
9871 (setq link (org-extract-attributes
9872 (org-link-unescape (org-match-string-no-properties 1))))
9873 (while (string-match " *\n *" link)
9874 (setq link (replace-match " " t t link)))
9875 (setq link (org-link-expand-abbrev link))
9877 ((or (file-name-absolute-p link)
9878 (string-match "^\\.\\.?/" link))
9879 (setq type "file" path link))
9880 ((string-match org-link-re-with-space3 link)
9881 (setq type (match-string 1 link) path (match-string 2 link)))
9882 ((string-match "^help:+\\(.+\\)" link)
9883 (setq type "help" path (match-string 1 link)))
9884 (t (setq type "thisfile" path link)))
9887 (when (get-text-property (point) 'org-linked-text)
9888 (setq type "thisfile"
9889 pos (if (get-text-property (1+ (point)) 'org-linked-text)
9890 (1+ (point)) (point))
9891 path (buffer-substring
9892 (or (previous-single-property-change pos 'org-linked-text)
9894 (or (next-single-property-change pos 'org-linked-text)
9899 (let ((plinkpos (org-in-regexp org-plain-link-re)))
9900 (when (or (org-in-regexp org-angle-link-re)
9901 (and plinkpos (goto-char (car plinkpos))
9902 (save-match-data (not (looking-back "\\[\\[")))))
9903 (setq type (match-string 1)
9904 path (org-link-unescape (match-string 2)))
9907 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
9909 path (match-string 1))
9910 (while (string-match ":" path)
9911 (setq path (replace-match "+" t t path)))
9913 (when (org-in-regexp "<\\([^><\n]+\\)>")
9914 (setq type "tree-match"
9915 path (match-string 1))
9918 (user-error "No link found"))
9920 ;; switch back to reference buffer
9921 ;; needed when if called in a temporary buffer through
9922 ;; org-open-link-from-string
9923 (with-current-buffer (or reference-buffer (current-buffer))
9925 ;; Remove any trailing spaces in path
9926 (if (string-match " +\\'" path)
9927 (setq path (replace-match "" t t path)))
9928 (if (and org-link-translation-function
9929 (fboundp org-link-translation-function))
9930 ;; Check if we need to translate the link
9931 (let ((tmp (funcall org-link-translation-function type path)))
9932 (setq type (car tmp) path (cdr tmp))))
9936 ((assoc type org-link-protocols)
9937 (funcall (nth 1 (assoc type org-link-protocols)) path))
9939 ((equal type "help")
9940 (let ((f-or-v (intern path)))
9941 (cond ((fboundp f-or-v)
9942 (describe-function f-or-v))
9944 (describe-variable f-or-v))
9945 (t (error "Not a known function or variable")))))
9947 ((equal type "mailto")
9948 (let ((cmd (car org-link-mailto-program))
9949 (args (cdr org-link-mailto-program)) args1
9950 (address path) (subject "") a)
9951 (if (string-match "\\(.*\\)::\\(.*\\)" path)
9952 (setq address (match-string 1 path)
9953 subject (org-link-escape (match-string 2 path))))
9956 ((not (stringp (car args))) (push (pop args) args1))
9957 (t (setq a (pop args))
9958 (if (string-match "%a" a)
9959 (setq a (replace-match address t t a)))
9960 (if (string-match "%s" a)
9961 (setq a (replace-match subject t t a)))
9963 (apply cmd (nreverse args1))))
9965 ((member type '("http" "https" "ftp" "news"))
9966 (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
9968 path org-link-escape-chars-browser)
9971 ((string= type "doi")
9972 (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
9974 path org-link-escape-chars-browser)
9977 ((member type '("message"))
9978 (browse-url (concat type ":" path)))
9980 ((string= type "tags")
9981 (org-tags-view arg path))
9983 ((string= type "tree-match")
9984 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
9986 ((string= type "file")
9987 (if (string-match "::\\([0-9]+\\)\\'" path)
9988 (setq line (string-to-number (match-string 1 path))
9989 path (substring path 0 (match-beginning 0)))
9990 (if (string-match "::\\(.+\\)\\'" path)
9991 (setq search (match-string 1 path)
9992 path (substring path 0 (match-beginning 0)))))
9993 (if (string-match "[*?{]" (file-name-nondirectory path))
9995 (org-open-file path arg line search)))
9997 ((string= type "shell")
9998 (let ((buf (generate-new-buffer "*Org Shell Output"))
10000 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
10001 (string-match org-confirm-shell-link-not-regexp cmd))
10002 (not org-confirm-shell-link-function)
10003 (funcall org-confirm-shell-link-function
10004 (format "Execute \"%s\" in shell? "
10005 (org-add-props cmd nil
10006 'face 'org-warning))))
10008 (message "Executing %s" cmd)
10009 (shell-command cmd buf)
10010 (if (featurep 'midnight)
10011 (setq clean-buffer-list-kill-buffer-names
10012 (cons buf clean-buffer-list-kill-buffer-names))))
10015 ((string= type "elisp")
10017 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
10018 (string-match org-confirm-elisp-link-not-regexp cmd))
10019 (not org-confirm-elisp-link-function)
10020 (funcall org-confirm-elisp-link-function
10021 (format "Execute \"%s\" as elisp? "
10022 (org-add-props cmd nil
10023 'face 'org-warning))))
10024 (message "%s => %s" cmd
10025 (if (equal (string-to-char cmd) ?\()
10027 (call-interactively (read cmd))))
10030 ((and (string= type "thisfile")
10031 (run-hook-with-args-until-success
10032 'org-open-link-functions path)))
10034 ((string= type "thisfile")
10036 (switch-to-buffer-other-window
10037 (org-get-buffer-for-internal-link (current-buffer)))
10038 (org-mark-ring-push))
10039 (let ((cmd `(org-link-search
10041 ,(cond ((equal arg '(4)) ''occur)
10042 ((equal arg '(16)) ''org-occur))
10044 (condition-case nil (let ((org-link-search-inhibit-query t))
10046 (error (progn (widen) (eval cmd))))))
10048 (t (browse-url-at-point)))))))
10049 (move-marker org-open-link-marker nil)
10050 (run-hook-with-args 'org-follow-link-hook)))
10052 (defun org-offer-links-in-entry (buffer marker &optional nth zero)
10053 "Offer links in the current entry and return the selected link.
10054 If there is only one link, return it.
10055 If NTH is an integer, return the NTH link found.
10056 If ZERO is a string, check also this string for a link, and if
10057 there is one, return it."
10058 (with-current-buffer buffer
10063 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
10064 "\\(" org-angle-link-re "\\)\\|"
10065 "\\(" org-plain-link-re "\\)"))
10067 (in-emacs (if (integerp nth) nil nth))
10068 have-zero end links link c)
10069 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
10070 (push (match-string 0 zero) links)
10071 (setq cnt (1- cnt) have-zero t))
10073 (org-back-to-heading t)
10074 (setq end (save-excursion (outline-next-heading) (point)))
10075 (while (re-search-forward re end t)
10076 (push (match-string 0) links))
10077 (setq links (org-uniquify (reverse links))))
10080 (message "No links"))
10081 ((equal (length links) 1)
10082 (setq link (car links)))
10083 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
10084 (setq link (nth (if have-zero nth (1- nth)) links)))
10085 (t ; we have to select a link
10087 (save-window-excursion
10088 (delete-other-windows)
10089 (with-output-to-temp-buffer "*Select Link*"
10091 (if (not (string-match org-bracket-link-regexp l))
10092 (princ (format "[%c] %s\n" (incf cnt)
10093 (org-remove-angle-brackets l)))
10095 (princ (format "[%c] %s (%s)\n" (incf cnt)
10096 (match-string 3 l) (match-string 1 l)))
10097 (princ (format "[%c] %s\n" (incf cnt)
10098 (match-string 1 l))))))
10100 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
10101 (message "Select link to open, RET to open all:")
10102 (setq c (read-char-exclusive))
10103 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
10104 (when (equal c ?q) (error "Abort"))
10105 (if (equal c ?\C-m)
10107 (setq nth (- c ?0))
10108 (if have-zero (setq nth (1+ nth)))
10109 (unless (and (integerp nth) (>= (length links) nth))
10110 (error "Invalid link selection"))
10111 (setq link (nth (1- nth) links)))))
10112 (cons link end))))))
10114 ;; Add special file links that specify the way of opening
10116 (org-add-link-type "file+sys" 'org-open-file-with-system)
10117 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
10118 (defun org-open-file-with-system (path)
10119 "Open file at PATH using the system way of opening it."
10120 (org-open-file path 'system))
10121 (defun org-open-file-with-emacs (path)
10122 "Open file at PATH in Emacs."
10123 (org-open-file path 'emacs))
10124 (defun org-remove-file-link-modifiers ()
10125 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
10126 (goto-char (point-min))
10127 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
10128 (org-if-unprotected
10129 (replace-match "file:" t t))))
10130 (eval-after-load "org-exp"
10131 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
10132 'org-remove-file-link-modifiers))
10134 ;;;; Time estimates
10136 (defun org-get-effort (&optional pom)
10137 "Get the effort estimate for the current entry."
10138 (org-entry-get pom org-effort-property))
10142 (defvar org-create-file-search-functions nil
10143 "List of functions to construct the right search string for a file link.
10144 These functions are called in turn with point at the location to
10145 which the link should point.
10147 A function in the hook should first test if it would like to
10148 handle this file type, for example by checking the `major-mode'
10149 or the file extension. If it decides not to handle this file, it
10150 should just return nil to give other functions a chance. If it
10151 does handle the file, it must return the search string to be used
10152 when following the link. The search string will be part of the
10153 file link, given after a double colon, and `org-open-at-point'
10154 will automatically search for it. If special measures must be
10155 taken to make the search successful, another function should be
10156 added to the companion hook `org-execute-file-search-functions',
10159 A function in this hook may also use `setq' to set the variable
10160 `description' to provide a suggestion for the descriptive text to
10161 be used for this link when it gets inserted into an Org-mode
10162 buffer with \\[org-insert-link].")
10164 (defvar org-execute-file-search-functions nil
10165 "List of functions to execute a file search triggered by a link.
10167 Functions added to this hook must accept a single argument, the
10168 search string that was part of the file link, the part after the
10169 double colon. The function must first check if it would like to
10170 handle this search, for example by checking the `major-mode' or
10171 the file extension. If it decides not to handle this search, it
10172 should just return nil to give other functions a chance. If it
10173 does handle the search, it must return a non-nil value to keep
10174 other functions from trying.
10176 Each function can access the current prefix argument through the
10177 variable `current-prefix-argument'. Note that a single prefix is
10178 used to force opening a link in Emacs, so it may be good to only
10179 use a numeric or double prefix to guide the search function.
10181 In case this is needed, a function in this hook can also restore
10182 the window configuration before `org-open-at-point' was called using:
10184 (set-window-configuration org-window-config-before-follow-link)")
10186 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
10187 (defun org-link-search (s &optional type avoid-pos stealth)
10188 "Search for a link search option.
10189 If S is surrounded by forward slashes, it is interpreted as a
10190 regular expression. In org-mode files, this will create an `org-occur'
10191 sparse tree. In ordinary files, `occur' will be used to list matches.
10192 If the current buffer is in `dired-mode', grep will be used to search
10193 in all files. If AVOID-POS is given, ignore matches near that position.
10195 When optional argument STEALTH is non-nil, do not modify
10196 visibility around point, thus ignoring
10197 `org-show-hierarchy-above', `org-show-following-heading' and
10198 `org-show-siblings' variables."
10199 (let ((case-fold-search t)
10200 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
10201 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
10202 (append '(("") (" ") ("\t") ("\n"))
10203 org-emphasis-alist)
10206 (pre nil) (post nil)
10207 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
10209 ;; First check if there are any special search functions
10210 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
10211 ;; Now try the builtin stuff
10212 ((and (equal (string-to-char s0) ?#)
10215 (goto-char (point-min))
10218 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
10219 (setq type 'dedicated
10220 pos (match-beginning 0))))
10221 ;; There is an exact target for this
10223 (org-back-to-heading t)))
10225 (goto-char (point-min))
10228 (concat "<<" (regexp-quote s0) ">>") nil t)
10229 (setq type 'dedicated
10230 pos (match-beginning 0))))
10231 ;; There is an exact target for this
10234 (goto-char (point-min))
10237 (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
10238 (setq type 'dedicated pos (match-beginning 0))))
10239 ;; Found an invisible target.
10242 (goto-char (point-min))
10245 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
10246 (setq type 'dedicated pos (match-beginning 0))))
10247 ;; Found an element with a matching #+name affiliated keyword.
10249 ((and (string-match "^(\\(.*\\))$" s0)
10251 (goto-char (point-min))
10254 (concat "[^[]" (regexp-quote
10255 (format org-coderef-label-format
10256 (match-string 1 s0))))
10258 (setq type 'dedicated
10259 pos (1+ (match-beginning 0))))))
10260 ;; There is a coderef target for this
10262 ((string-match "^/\\(.*\\)/$" s)
10263 ;; A regular expression
10265 ((derived-mode-p 'org-mode)
10266 (org-occur (match-string 1 s)))
10267 ;;((eq major-mode 'dired-mode)
10268 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
10269 (t (org-do-occur (match-string 1 s)))))
10270 ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
10271 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
10272 (goto-char (point-min))
10274 ((let (case-fold-search)
10275 (re-search-forward (format org-complex-heading-regexp-format
10278 ;; OK, found a match
10279 (setq type 'dedicated)
10280 (goto-char (match-beginning 0)))
10281 ((and (not org-link-search-inhibit-query)
10282 (eq org-link-search-must-match-exact-headline 'query-to-create)
10283 (y-or-n-p "No match - create this as a new heading? "))
10284 (goto-char (point-max))
10285 (or (bolp) (newline))
10286 (insert "* " s "\n")
10287 (beginning-of-line 0))
10290 (error "No match"))))
10292 ;; A normal search string
10293 (when (equal (string-to-char s) ?*)
10294 ;; Anchor on headlines, post may include tags.
10295 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
10296 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
10297 s (substring s 1)))
10298 (remove-text-properties
10300 '(face nil mouse-face nil keymap nil fontified nil) s)
10301 ;; Make a series of regular expressions to find a match
10302 (setq words (org-split-string s "[ \n\r\t]+")
10304 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
10305 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
10307 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
10308 re2a (concat "[ \t\r\n]" re2a_)
10309 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
10310 re4 (concat "[^a-zA-Z_]" re4_)
10312 re1 (concat pre re2 post)
10313 re3 (concat pre (if pre re4_ re4) post)
10314 re5 (concat pre ".*" re4)
10315 re2 (concat pre re2)
10316 re2a (concat pre (if pre re2a_ re2a))
10317 re4 (concat pre (if pre re4_ re4))
10318 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
10319 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
10323 ((eq type 'org-occur) (org-occur reall))
10324 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
10325 (t (goto-char (point-min))
10327 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
10328 (org-search-not-self 1 re1 nil t)
10329 (org-search-not-self 1 re2 nil t)
10330 (org-search-not-self 1 re2a nil t)
10331 (org-search-not-self 1 re3 nil t)
10332 (org-search-not-self 1 re4 nil t)
10333 (org-search-not-self 1 re5 nil t)
10335 (goto-char (match-beginning 1))
10337 (error "No match"))))))
10338 (and (derived-mode-p 'org-mode)
10340 (org-show-context 'link-search))
10343 (defun org-search-not-self (group &rest args)
10344 "Execute `re-search-forward', but only accept matches that do not
10345 enclose the position of `org-open-link-marker'."
10346 (let ((m org-open-link-marker))
10348 (while (apply 're-search-forward args)
10349 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
10350 (goto-char (match-end group))
10351 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
10352 (> (match-beginning 0) (marker-position m))
10353 (< (match-end 0) (marker-position m)))
10355 (or (not (org-in-regexp
10356 org-bracket-link-analytic-regexp 1))
10357 (not (match-end 4)) ; no description
10358 (and (<= (match-beginning 4) (point))
10359 (>= (match-end 4) (point))))))
10360 (throw 'exit (point))))))))
10362 (defun org-get-buffer-for-internal-link (buffer)
10363 "Return a buffer to be used for displaying the link target of internal links."
10365 ((not org-display-internal-link-with-indirect-buffer)
10367 ((string-match "(Clone)$" (buffer-name buffer))
10368 (message "Buffer is already a clone, not making another one")
10369 ;; we also do not modify visibility in this case
10371 (t ; make a new indirect buffer for displaying the link
10372 (let* ((bn (buffer-name buffer))
10373 (ibn (concat bn "(Clone)"))
10374 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
10375 (with-current-buffer ib (org-overview))
10378 (defun org-do-occur (regexp &optional cleanup)
10379 "Call the Emacs command `occur'.
10380 If CLEANUP is non-nil, remove the printout of the regular expression
10381 in the *Occur* buffer. This is useful if the regex is long and not useful
10385 (let ((cwin (selected-window)) win beg end)
10386 (when (setq win (get-buffer-window "*Occur*"))
10387 (select-window win))
10388 (goto-char (point-min))
10389 (when (re-search-forward "match[a-z]+" nil t)
10390 (setq beg (match-end 0))
10391 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
10392 (setq end (1- (match-beginning 0)))))
10393 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
10394 (goto-char (point-min))
10395 (select-window cwin))))
10397 ;;; The mark ring for links jumps
10399 (defvar org-mark-ring nil
10400 "Mark ring for positions before jumps in Org-mode.")
10401 (defvar org-mark-ring-last-goto nil
10402 "Last position in the mark ring used to go back.")
10403 ;; Fill and close the ring
10404 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
10405 (loop for i from 1 to org-mark-ring-length do
10406 (push (make-marker) org-mark-ring))
10407 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
10410 (defun org-mark-ring-push (&optional pos buffer)
10411 "Put the current position or POS into the mark ring and rotate it."
10413 (setq pos (or pos (point)))
10414 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10415 (move-marker (car org-mark-ring)
10417 (or buffer (current-buffer)))
10419 (substitute-command-keys
10420 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10422 (defun org-mark-ring-goto (&optional n)
10423 "Jump to the previous position in the mark ring.
10424 With prefix arg N, jump back that many stored positions. When
10425 called several times in succession, walk through the entire ring.
10426 Org-mode commands jumping to a different position in the current file,
10427 or to another Org-mode file, automatically push the old position
10431 (if (eq last-command this-command)
10432 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10433 (setq p org-mark-ring))
10434 (setq org-mark-ring-last-goto p)
10436 (org-pop-to-buffer-same-window (marker-buffer m))
10438 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
10440 (defun org-remove-angle-brackets (s)
10441 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
10442 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
10444 (defun org-add-angle-brackets (s)
10445 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
10446 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
10448 (defun org-remove-double-quotes (s)
10449 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
10450 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
10453 ;;; Following specific links
10455 (defun org-follow-timestamp-link ()
10456 "Open an agenda view for the time-stamp date/range at point."
10458 ((org-at-date-range-p t)
10459 (let ((org-agenda-start-on-weekday)
10460 (t1 (match-string 1))
10461 (t2 (match-string 2)) tt1 tt2)
10462 (setq tt1 (time-to-days (org-time-string-to-time t1))
10463 tt2 (time-to-days (org-time-string-to-time t2)))
10464 (let ((org-agenda-buffer-tmp-name
10465 (format "*Org Agenda(a:%s)"
10466 (concat (substring t1 0 10) "--" (substring t2 0 10)))))
10467 (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
10468 ((org-at-timestamp-p t)
10469 (let ((org-agenda-buffer-tmp-name
10470 (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
10471 (org-agenda-list nil (time-to-days (org-time-string-to-time
10472 (substring (match-string 1) 0 10)))
10474 (t (error "This should not happen"))))
10477 ;;; Following file links
10478 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
10479 (declare-function mailcap-extension-to-mime "mailcap" (extn))
10480 (declare-function mailcap-mime-info
10481 "mailcap" (string &optional request no-decode))
10482 (defvar org-wait nil)
10483 (defun org-open-file (path &optional in-emacs line search)
10484 "Open the file at PATH.
10485 First, this expands any special file name abbreviations. Then the
10486 configuration variable `org-file-apps' is checked if it contains an
10487 entry for this file type, and if yes, the corresponding command is launched.
10489 If no application is found, Emacs simply visits the file.
10491 With optional prefix argument IN-EMACS, Emacs will visit the file.
10492 With a double \\[universal-argument] \\[universal-argument] \
10493 prefix arg, Org tries to avoid opening in Emacs
10494 and to use an external application to visit the file.
10496 Optional LINE specifies a line to go to, optional SEARCH a string
10497 to search for. If LINE or SEARCH is given, the file will be
10498 opened in Emacs, unless an entry from org-file-apps that makes
10499 use of groups in a regexp matches.
10501 If you want to change the way frames are used when following a
10502 link, please customize `org-link-frame-setup'.
10504 If the file does not exist, an error is thrown."
10505 (let* ((file (if (equal path "")
10507 (substitute-in-file-name (expand-file-name path))))
10508 (file-apps (append org-file-apps (org-default-apps)))
10509 (apps (org-remove-if
10510 'org-file-apps-entry-match-against-dlink-p file-apps))
10511 (apps-dlink (org-remove-if-not
10512 'org-file-apps-entry-match-against-dlink-p file-apps))
10513 (remp (and (assq 'remote apps) (org-file-remote-p file)))
10514 (dirp (if remp nil (file-directory-p file)))
10515 (file (if (and dirp org-open-directory-means-index-dot-org)
10516 (concat (file-name-as-directory file) "index.org")
10518 (a-m-a-p (assq 'auto-mode apps))
10519 (dfile (downcase file))
10520 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
10521 (link (cond ((and (eq line nil)
10525 (concat file "::" (number-to-string line)))
10527 (concat file "::" search))))
10528 (dlink (downcase link))
10529 (old-buffer (current-buffer))
10531 (old-mode major-mode)
10532 ext cmd link-match-data)
10533 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
10534 (setq ext (match-string 1 dfile))
10535 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
10536 (setq ext (match-string 1 dfile))))
10538 ((member in-emacs '((16) system))
10539 (setq cmd (cdr (assoc 'system apps))))
10540 (in-emacs (setq cmd 'emacs))
10542 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
10543 (and dirp (cdr (assoc 'directory apps)))
10544 ; first, try matching against apps-dlink
10545 ; if we get a match here, store the match data for later
10546 (let ((match (assoc-default dlink apps-dlink
10549 (progn (setq link-match-data (match-data))
10551 (progn (setq in-emacs (or in-emacs line search))
10552 nil))) ; if we have no match in apps-dlink,
10553 ; always open the file in emacs if line or search
10554 ; is given (for backwards compatibility)
10555 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
10557 (cdr (assoc ext apps))
10558 (cdr (assoc t apps))))))
10559 (when (eq cmd 'system)
10560 (setq cmd (cdr (assoc 'system apps))))
10561 (when (eq cmd 'default)
10562 (setq cmd (cdr (assoc t apps))))
10563 (when (eq cmd 'mailcap)
10565 (mailcap-parse-mailcaps)
10566 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
10567 (command (mailcap-mime-info mime-type)))
10568 (if (stringp command)
10570 (setq cmd 'emacs))))
10571 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
10572 (not (file-exists-p file))
10573 (not org-open-non-existing-files))
10574 (error "No such file: %s" file))
10576 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
10577 ;; Remove quotes around the file name - we'll use shell-quote-argument.
10578 (while (string-match "['\"]%s['\"]" cmd)
10579 (setq cmd (replace-match "%s" t t cmd)))
10580 (while (string-match "%s" cmd)
10581 (setq cmd (replace-match
10583 (shell-quote-argument
10584 (convert-standard-filename file)))
10587 ;; Replace "%1", "%2" etc. in command with group matches from regex
10589 (let ((match-index 1)
10590 (number-of-groups (- (/ (length link-match-data) 2) 1)))
10591 (set-match-data link-match-data)
10592 (while (<= match-index number-of-groups)
10593 (let ((regex (concat "%" (number-to-string match-index)))
10594 (replace-with (match-string match-index dlink)))
10595 (while (string-match regex cmd)
10596 (setq cmd (replace-match replace-with t t cmd))))
10597 (setq match-index (+ match-index 1)))))
10599 (save-window-excursion
10600 (message "Running %s...done" cmd)
10601 (start-process-shell-command cmd nil cmd)
10602 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
10605 (funcall (cdr (assq 'file org-link-frame-setup)) file)
10607 (if line (org-goto-line line)
10608 (if search (org-link-search search))))
10610 (let ((file (convert-standard-filename file)))
10612 (set-match-data link-match-data)
10614 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
10615 (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
10616 (or (not (equal old-buffer (current-buffer)))
10617 (not (equal old-pos (point))))
10618 (org-mark-ring-push old-pos old-buffer))))
10620 (defun org-file-apps-entry-match-against-dlink-p (entry)
10621 "This function returns non-nil if `entry' uses a regular
10622 expression which should be matched against the whole link by
10625 It assumes that is the case when the entry uses a regular
10626 expression which has at least one grouping construct and the
10627 action is either a lisp form or a command string containing
10628 '%1', i.e. using at least one subexpression match as a
10630 (let ((selector (car entry))
10631 (action (cdr entry)))
10632 (if (stringp selector)
10633 (and (> (regexp-opt-depth selector) 0)
10634 (or (and (stringp action)
10635 (string-match "%[0-9]" action))
10639 (defun org-default-apps ()
10640 "Return the default applications for this operating system."
10642 ((eq system-type 'darwin)
10643 org-file-apps-defaults-macosx)
10644 ((eq system-type 'windows-nt)
10645 org-file-apps-defaults-windowsnt)
10646 (t org-file-apps-defaults-gnu)))
10648 (defun org-apps-regexp-alist (list &optional add-auto-mode)
10649 "Convert extensions to regular expressions in the cars of LIST.
10650 Also, weed out any non-string entries, because the return value is used
10651 only for regexp matching.
10652 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
10653 point to the symbol `emacs', indicating that the file should
10654 be opened in Emacs."
10657 (mapcar (lambda (x)
10658 (if (not (stringp (car x)))
10660 (if (string-match "\\W" (car x))
10662 (cons (concat "\\." (car x) "\\'") (cdr x)))))
10665 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
10667 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
10668 (defun org-file-remote-p (file)
10669 "Test whether FILE specifies a location on a remote system.
10670 Return non-nil if the location is indeed remote.
10672 For example, the filename \"/user@host:/foo\" specifies a location
10673 on the system \"/user@host:\"."
10674 (cond ((fboundp 'file-remote-p)
10675 (file-remote-p file))
10676 ((fboundp 'tramp-handle-file-remote-p)
10677 (tramp-handle-file-remote-p file))
10678 ((and (boundp 'ange-ftp-name-format)
10679 (string-match (car ange-ftp-name-format) file))
10685 (defun org-get-org-file ()
10686 "Read a filename, with default directory `org-directory'."
10687 (let ((default (or org-default-notes-file remember-data-file)))
10688 (read-file-name (format "File name [%s]: " default)
10689 (file-name-as-directory org-directory)
10692 (defun org-notes-order-reversed-p ()
10693 "Check if the current file should receive notes in reversed order."
10695 ((not org-reverse-note-order) nil)
10696 ((eq t org-reverse-note-order) t)
10697 ((not (listp org-reverse-note-order)) nil)
10699 (let ((all org-reverse-note-order)
10701 (while (setq entry (pop all))
10702 (if (string-match (car entry) buffer-file-name)
10703 (throw 'exit (cdr entry))))
10706 (defvar org-refile-target-table nil
10707 "The list of refile targets, created by `org-refile'.")
10709 (defvar org-agenda-new-buffers nil
10710 "Buffers created to visit agenda files.")
10712 (defvar org-refile-cache nil
10713 "Cache for refile targets.")
10715 (defvar org-refile-markers nil
10716 "All the markers used for caching refile locations.")
10718 (defun org-refile-marker (pos)
10719 "Get a new refile marker, but only if caching is in use."
10720 (if (not org-refile-use-cache)
10722 (let ((m (make-marker)))
10723 (move-marker m pos)
10724 (push m org-refile-markers)
10727 (defun org-refile-cache-clear ()
10728 "Clear the refile cache and disable all the markers."
10729 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
10730 (setq org-refile-markers nil)
10731 (setq org-refile-cache nil)
10732 (message "Refile cache has been cleared"))
10734 (defun org-refile-cache-check-set (set)
10735 "Check if all the markers in the cache still have live buffers."
10738 (while (and set (setq marker (nth 3 (pop set))))
10739 ;; if org-refile-use-outline-path is 'file, marker may be nil
10740 (when (and marker (null (marker-buffer marker)))
10741 (message "not found") (sit-for 3)
10742 (throw 'exit nil)))
10745 (defun org-refile-cache-put (set &rest identifiers)
10746 "Push the refile targets SET into the cache, under IDENTIFIERS."
10747 (let* ((key (sha1 (prin1-to-string identifiers)))
10748 (entry (assoc key org-refile-cache)))
10751 (push (cons key set) org-refile-cache))))
10753 (defun org-refile-cache-get (&rest identifiers)
10754 "Retrieve the cached value for refile targets given by IDENTIFIERS."
10756 ((not org-refile-cache) nil)
10757 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
10759 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
10760 org-refile-cache))))
10761 (and set (org-refile-cache-check-set set) set)))))
10763 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
10764 "Produce a table with refile targets."
10765 (let ((case-fold-search nil)
10766 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
10767 (entries (or org-refile-targets '((nil . (:level . 1)))))
10768 targets tgs txt re files f desc descre fast-path-p level pos0)
10769 (message "Getting targets...")
10770 (with-current-buffer (or default-buffer (current-buffer))
10771 (while (setq entry (pop entries))
10772 (setq files (car entry) desc (cdr entry))
10773 (setq fast-path-p nil)
10775 ((null files) (setq files (list (current-buffer))))
10776 ((eq files 'org-agenda-files)
10777 (setq files (org-agenda-files 'unrestricted)))
10778 ((and (symbolp files) (fboundp files))
10779 (setq files (funcall files)))
10780 ((and (symbolp files) (boundp files))
10781 (setq files (symbol-value files))))
10782 (if (stringp files) (setq files (list files)))
10784 ((eq (car desc) :tag)
10785 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
10786 ((eq (car desc) :todo)
10787 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
10788 ((eq (car desc) :regexp)
10789 (setq descre (cdr desc)))
10790 ((eq (car desc) :level)
10791 (setq descre (concat "^\\*\\{" (number-to-string
10792 (if org-odd-levels-only
10793 (1- (* 2 (cdr desc)))
10796 ((eq (car desc) :maxlevel)
10797 (setq fast-path-p t)
10798 (setq descre (concat "^\\*\\{1," (number-to-string
10799 (if org-odd-levels-only
10800 (1- (* 2 (cdr desc)))
10803 (t (error "Bad refiling target description %s" desc)))
10804 (while (setq f (pop files))
10805 (with-current-buffer
10806 (if (bufferp f) f (org-get-agenda-file-buffer f))
10808 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
10810 (if (bufferp f) (setq f (buffer-file-name
10811 (buffer-base-buffer f))))
10812 (setq f (and f (expand-file-name f)))
10813 (if (eq org-refile-use-outline-path 'file)
10814 (push (list (file-name-nondirectory f) f nil nil) tgs))
10818 (goto-char (point-min))
10819 (while (re-search-forward descre nil t)
10820 (goto-char (setq pos0 (point-at-bol)))
10822 (when org-refile-target-verify-function
10824 (or (funcall org-refile-target-verify-function)
10826 (when (and (looking-at org-complex-heading-regexp)
10827 (not (member (match-string 4) excluded-entries))
10829 (setq level (org-reduced-level
10830 (- (match-end 1) (match-beginning 1)))
10831 txt (org-link-display-format (match-string 4))
10832 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
10833 re (format org-complex-heading-regexp-format
10834 (regexp-quote (match-string 4))))
10835 (when org-refile-use-outline-path
10836 (setq txt (mapconcat
10839 (if (eq org-refile-use-outline-path
10841 (list (file-name-nondirectory
10843 (buffer-base-buffer))))
10844 (if (eq org-refile-use-outline-path
10846 (list (buffer-file-name
10847 (buffer-base-buffer)))))
10848 (org-get-outline-path fast-path-p
10852 (push (list txt f re (org-refile-marker (point)))
10854 (when (= (point) pos0)
10855 ;; verification function has not moved point
10856 (goto-char (point-at-eol))))))))
10857 (when org-refile-use-cache
10858 (org-refile-cache-put tgs (buffer-file-name) descre))
10859 (setq targets (append tgs targets))
10861 (message "Getting targets...done")
10862 (nreverse targets)))
10864 (defun org-protect-slash (s)
10865 (while (string-match "/" s)
10866 (setq s (replace-match "\\" t t s)))
10869 (defvar org-olpa (make-vector 20 nil))
10871 (defun org-get-outline-path (&optional fastp level heading)
10872 "Return the outline path to the current entry, as a list.
10874 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
10875 routine which makes outline path derivations for an entire file,
10876 avoiding backtracing. Refile target collection makes use of that."
10880 (error "Outline path failure, more than 19 levels"))
10881 (loop for i from level upto 19 do
10882 (aset org-olpa i nil))
10884 (delq nil (append org-olpa nil))
10885 (aset org-olpa level heading)))
10886 (let (rtn case-fold-search)
10890 (while (org-up-heading-safe)
10891 (when (looking-at org-complex-heading-regexp)
10892 (push (org-match-string-no-properties 4) rtn)))
10895 (defun org-format-outline-path (path &optional width prefix separator)
10896 "Format the outline path PATH for display.
10897 WIDTH is the maximum number of characters that is available.
10898 PREFIX is a prefix to be included in the returned string,
10899 such as the file name.
10900 SEPARATOR is inserted between the different parts of the path,
10901 the default is \"/\"."
10902 (setq width (or width 79))
10903 (if prefix (setq width (- width (length prefix))))
10906 (let* ((nsteps (length path))
10907 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
10908 (maxwidth (if (<= total-width width)
10909 10000 ;; everything fits
10910 ;; we need to shorten the level headings
10911 (/ (- width nsteps) nsteps)))
10912 (org-odd-levels-only nil)
10914 (total (1+ (length prefix))))
10915 (setq maxwidth (max maxwidth 10))
10917 (if prefix (or separator "/"))
10921 (if (and (= n nsteps) (< maxwidth 10000))
10922 (setq maxwidth (- total-width total)))
10923 (if (< (length h) maxwidth)
10924 (progn (setq total (+ total (length h) 1)) h)
10925 (setq h (substring h 0 (- maxwidth 2))
10926 total (+ total maxwidth 1))
10927 (if (string-match "[ \t]+\\'" h)
10928 (setq h (substring h 0 (match-beginning 0))))
10929 (setq h (concat h "..")))
10930 (org-add-props h nil 'face
10931 (nth (% (1- n) org-n-level-faces)
10934 path (or separator "/"))))))
10936 (defun org-display-outline-path (&optional file current separator just-return-string)
10937 "Display the current outline path in the echo area.
10939 If FILE is non-nil, prepend the output with the file name.
10940 If CURRENT is non-nil, append the current heading to the output.
10941 SEPARATOR is passed through to `org-format-outline-path'. It separates
10942 the different parts of the path and defaults to \"/\".
10943 If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
10945 (let* (case-fold-search
10946 message-log-max ; Don't populate the *Messages* buffer
10947 (bfn (buffer-file-name (buffer-base-buffer)))
10948 (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
10950 (if current (setq path (append path
10952 (org-back-to-heading t)
10953 (if (looking-at org-complex-heading-regexp)
10954 (list (match-string 4)))))))
10956 (org-format-outline-path
10959 (and file bfn (concat (file-name-nondirectory bfn) separator))
10961 (if just-return-string
10962 (org-no-properties res)
10963 (message "%s" res))))
10965 (defvar org-refile-history nil
10966 "History for refiling operations.")
10968 (defvar org-after-refile-insert-hook nil
10969 "Hook run after `org-refile' has inserted its stuff at the new location.
10970 Note that this is still *before* the stuff will be removed from
10971 the *old* location.")
10973 (defvar org-capture-last-stored-marker)
10974 (defvar org-refile-keep nil
10975 "Non-nil means `org-refile' will copy instead of refile.")
10978 "Like `org-refile', but copy."
10980 (let ((org-refile-keep t))
10981 (funcall 'org-refile nil nil nil "Copy")))
10983 (defun org-refile (&optional goto default-buffer rfloc msg)
10984 "Move the entry or entries at point to another heading.
10985 The list of target headings is compiled using the information in
10986 `org-refile-targets', which see.
10988 At the target location, the entry is filed as a subitem of the target
10989 heading. Depending on `org-reverse-note-order', the new subitem will
10990 either be the first or the last subitem.
10992 If there is an active region, all entries in that region will be moved.
10993 However, the region must fulfill the requirement that the first heading
10994 is the first one sets the top-level of the moved text - at most siblings
10995 below it are allowed.
10997 With prefix arg GOTO, the command will only visit the target location
10998 and not actually move anything.
11000 With a double prefix arg \\[universal-argument] \\[universal-argument], \
11001 go to the location where the last refiling operation has put the subtree.
11002 With a prefix argument of `2', refile to the running clock.
11004 RFLOC can be a refile location obtained in a different way.
11006 MSG is a string to replace \"Refile\" in the default prompt with
11007 another verb. E.g. `org-copy' sets this parameter to \"Copy\".
11009 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
11011 If you are using target caching (see `org-refile-use-cache'),
11012 you have to clear the target cache in order to find new targets.
11013 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
11014 prefix argument (`C-u C-u C-u C-c C-w')."
11017 (if (member goto '(0 (64)))
11018 (org-refile-cache-clear)
11019 (let* ((actionmsg (or msg "Refile"))
11020 (cbuf (current-buffer))
11021 (regionp (org-region-active-p))
11022 (region-start (and regionp (region-beginning)))
11023 (region-end (and regionp (region-end)))
11024 (region-length (and regionp (- region-end region-start)))
11025 (filename (buffer-file-name (buffer-base-buffer cbuf)))
11026 pos it nbuf file re level reversed)
11027 (setq last-command nil)
11029 (goto-char region-start)
11030 (or (bolp) (goto-char (point-at-bol)))
11031 (setq region-start (point))
11032 (unless (or (org-kill-is-subtree-p
11033 (buffer-substring region-start region-end))
11034 (prog1 org-refile-active-region-within-subtree
11035 (org-toggle-heading)))
11036 (error "The region is not a (sequence of) subtree(s)")))
11037 (if (equal goto '(16))
11038 (org-refile-goto-last-stored)
11040 (and (equal goto 2)
11041 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
11043 (setq it (list (or org-clock-heading "running clock")
11045 (marker-buffer org-clock-hd-marker))
11047 (marker-position org-clock-hd-marker)))
11050 (let (heading-text)
11053 (org-back-to-heading t)
11055 (nth 4 (org-heading-components))))
11057 (org-refile-get-location
11058 (cond (goto "Goto")
11059 (regionp (concat actionmsg " region to"))
11060 (t (concat actionmsg " subtree \""
11061 heading-text "\" to")))
11063 (and (not (equal '(4) goto))
11064 org-refile-allow-creating-parent-nodes)
11066 (setq file (nth 1 it)
11069 (if (and (not goto)
11071 (equal (buffer-file-name) file)
11073 (and (>= pos region-start)
11074 (<= pos region-end))
11075 (and (>= pos (point))
11076 (< pos (save-excursion
11077 (org-end-of-subtree t t))))))
11078 (error "Cannot refile to position inside the tree or region"))
11080 (setq nbuf (or (find-buffer-visiting file)
11081 (find-file-noselect file)))
11084 (org-pop-to-buffer-same-window nbuf)
11086 (org-show-context 'org-goto))
11089 (org-kill-new (buffer-substring region-start region-end))
11090 (org-save-markers-in-region region-start region-end))
11091 (org-copy-subtree 1 nil t))
11092 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
11093 (find-file-noselect file)))
11094 (setq reversed (org-notes-order-reversed-p))
11101 (looking-at org-outline-regexp)
11102 (setq level (org-get-valid-level (funcall outline-level) 1))
11105 (or (outline-next-heading) (point-max))
11106 (or (save-excursion (org-get-next-sibling))
11107 (org-end-of-subtree t t)
11111 (goto-char (point-max))
11112 (goto-char (point-min))
11113 (or (outline-next-heading) (goto-char (point-max)))))
11114 (if (not (bolp)) (newline))
11115 (org-paste-subtree level)
11116 (when org-log-refile
11117 (org-add-log-setup 'refile nil nil 'findpos
11119 (unless (eq org-log-refile 'note)
11120 (save-excursion (org-add-log-note))))
11121 (and org-auto-align-tags
11122 (let ((org-loop-over-headlines-in-active-region nil))
11123 (org-set-tags nil t)))
11124 (with-demoted-errors
11125 (bookmark-set "org-refile-last-stored"))
11126 ;; If we are refiling for capture, make sure that the
11127 ;; last-capture pointers point here
11128 (when (org-bound-and-true-p org-refile-for-capture)
11129 (with-demoted-errors
11130 (bookmark-set "org-capture-last-stored-marker"))
11131 (move-marker org-capture-last-stored-marker (point)))
11132 (if (fboundp 'deactivate-mark) (deactivate-mark))
11133 (run-hooks 'org-after-refile-insert-hook))))
11134 (unless org-refile-keep
11136 (delete-region (point) (+ (point) region-length))
11137 (org-cut-subtree)))
11138 (when (featurep 'org-inlinetask)
11139 (org-inlinetask-remove-END-maybe))
11140 (setq org-markers-to-move nil)
11141 (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
11143 (defun org-refile-goto-last-stored ()
11144 "Go to the location where the last refile was stored."
11146 (bookmark-jump "org-refile-last-stored")
11147 (message "This is the location of the last refile"))
11149 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
11151 "Prompt the user for a refile location, using PROMPT.
11152 PROMPT should not be suffixed with a colon and a space, because
11153 this function appends the default value from
11154 `org-refile-history' automatically, if that is not empty.
11155 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
11156 this is used for the GOTO interface."
11157 (let ((org-refile-targets org-refile-targets)
11158 (org-refile-use-outline-path org-refile-use-outline-path)
11160 (when (and (derived-mode-p 'org-mode)
11161 (not org-refile-use-cache)
11165 (setq excluded-entries
11166 (append excluded-entries (list (org-get-heading t t)))))))
11167 (setq org-refile-target-table
11168 (org-refile-get-targets default-buffer excluded-entries)))
11169 (unless org-refile-target-table
11170 (error "No refile targets"))
11171 (let* ((cbuf (current-buffer))
11172 (partial-completion-mode nil)
11173 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
11174 (cfunc (if (and org-refile-use-outline-path
11175 org-outline-path-complete-in-steps)
11176 'org-olpath-completing-read
11177 'org-icompleting-read))
11178 (extra (if org-refile-use-outline-path "/" ""))
11179 (cbnex (concat (buffer-name) extra))
11180 (filename (and cfn (expand-file-name cfn)))
11183 (if (and (not (member org-refile-use-outline-path
11184 '(file full-file-path)))
11185 (not (equal filename (nth 1 x))))
11186 (cons (concat (car x) extra " ("
11187 (file-name-nondirectory (nth 1 x)) ")")
11189 (cons (concat (car x) extra) (cdr x))))
11190 org-refile-target-table))
11191 (completion-ignore-case t)
11193 (prompt (concat prompt
11194 (or (and (car org-refile-history)
11195 (concat " (default " (car org-refile-history) ")"))
11196 (and (assoc cbnex tbl) (setq cdef cbnex)
11197 (concat " (default " cbnex ")"))) ": "))
11198 pa answ parent-target child parent old-hist)
11199 (setq old-hist org-refile-history)
11200 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
11201 nil 'org-refile-history (or cdef (car org-refile-history))))
11202 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
11203 (org-refile-check-position pa)
11206 (when (or (not org-refile-history)
11207 (not (eq old-hist org-refile-history))
11208 (not (equal (car pa) (car org-refile-history))))
11209 (setq org-refile-history
11210 (cons (car pa) (if (assoc (car org-refile-history) tbl)
11212 (cdr org-refile-history))))
11213 (if (equal (car org-refile-history) (nth 1 org-refile-history))
11214 (pop org-refile-history)))
11216 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
11218 (setq parent (match-string 1 answ)
11219 child (match-string 2 answ))
11220 (setq parent-target (or (assoc parent tbl)
11221 (assoc (concat parent "/") tbl)))
11222 (when (and parent-target
11223 (or (eq new-nodes t)
11224 (and (eq new-nodes 'confirm)
11225 (y-or-n-p (format "Create new node \"%s\"? "
11227 (org-refile-new-child parent-target child)))
11228 (error "Invalid target location")))))
11230 (declare-function org-string-nw-p "org-macs" (s))
11231 (defun org-refile-check-position (refile-pointer)
11232 "Check if the refile pointer matches the headline to which it points."
11233 (let* ((file (nth 1 refile-pointer))
11234 (re (nth 2 refile-pointer))
11235 (pos (nth 3 refile-pointer))
11237 (if (and (not (markerp pos)) (not file))
11238 (error "Please save the buffer to a file before refiling")
11239 (when (org-string-nw-p re)
11240 (setq buffer (if (markerp pos)
11241 (marker-buffer pos)
11242 (or (find-buffer-visiting file)
11243 (find-file-noselect file))))
11244 (with-current-buffer buffer
11249 (beginning-of-line 1)
11250 (unless (org-looking-at-p re)
11251 (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
11253 (defun org-refile-new-child (parent-target child)
11254 "Use refile target PARENT-TARGET to add new CHILD below it."
11255 (unless parent-target
11256 (error "Cannot find parent for new node"))
11257 (let ((file (nth 1 parent-target))
11258 (pos (nth 3 parent-target))
11260 (with-current-buffer (or (find-buffer-visiting file)
11261 (find-file-noselect file))
11267 (goto-char (point-max))
11268 (if (not (bolp)) (newline)))
11269 (when (looking-at org-outline-regexp)
11270 (setq level (funcall outline-level))
11271 (org-end-of-subtree t t))
11272 (org-back-over-empty-lines)
11273 (insert "\n" (make-string
11274 (if pos (org-get-valid-level level 1) 1) ?*)
11276 (beginning-of-line 0)
11277 (list (concat (car parent-target) "/" child) file "" (point)))))))
11279 (defun org-olpath-completing-read (prompt collection &rest args)
11280 "Read an outline path like a file name."
11281 (let ((thetable collection)
11282 (org-completion-use-ido nil) ; does not work with ido.
11283 (org-completion-use-iswitchb nil)) ; or iswitchb
11285 'org-icompleting-read prompt
11286 (lambda (string predicate &optional flag)
11287 (let (rtn r f (l (length string)))
11291 (try-completion string thetable))
11294 (setq rtn (all-completions string thetable predicate))
11297 (setq r (substring x l))
11298 (if (string-match " ([^)]*)$" x)
11299 (setq f (match-string 0 x))
11301 (if (string-match "/" r)
11302 (concat string (substring r 0 (match-end 0)) f)
11307 (assoc string thetable)))))
11310 ;;;; Dynamic blocks
11312 (defun org-find-dblock (name)
11313 "Find the first dynamic block with name NAME in the buffer.
11314 If not found, stay at current position and return nil."
11315 (let ((case-fold-search t) pos)
11317 (goto-char (point-min))
11318 (setq pos (and (re-search-forward
11319 (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
11320 (match-beginning 0))))
11321 (if pos (goto-char pos))
11324 (defconst org-dblock-start-re
11325 "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
11326 "Matches the start line of a dynamic block, with parameters.")
11328 (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
11329 "Matches the end of a dynamic block.")
11331 (defun org-create-dblock (plist)
11332 "Create a dynamic block section, with parameters taken from PLIST.
11333 PLIST must contain a :name entry which is used as name of the block."
11334 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
11337 (let ((col (current-column))
11338 (name (plist-get plist :name)))
11339 (insert "#+BEGIN: " name)
11341 (if (eq (car plist) :name)
11342 (setq plist (cddr plist))
11343 (insert " " (prin1-to-string (pop plist)))))
11344 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
11345 (beginning-of-line -2)))
11347 (defun org-prepare-dblock ()
11348 "Prepare dynamic block for refresh.
11349 This empties the block, puts the cursor at the insert position and returns
11350 the property list including an extra property :name with the block name."
11351 (unless (looking-at org-dblock-start-re)
11352 (error "Not at a dynamic block"))
11353 (let* ((begdel (1+ (match-end 0)))
11354 (name (org-no-properties (match-string 1)))
11355 (params (append (list :name name)
11356 (read (concat "(" (match-string 3) ")")))))
11358 (beginning-of-line 1)
11359 (skip-chars-forward " \t")
11360 (setq params (plist-put params :indentation-column (current-column))))
11361 (unless (re-search-forward org-dblock-end-re nil t)
11362 (error "Dynamic block not terminated"))
11365 (list :content (buffer-substring
11366 begdel (match-beginning 0)))))
11367 (delete-region begdel (match-beginning 0))
11372 (defun org-map-dblocks (&optional command)
11373 "Apply COMMAND to all dynamic blocks in the current buffer.
11374 If COMMAND is not given, use `org-update-dblock'."
11375 (let ((cmd (or command 'org-update-dblock)))
11377 (goto-char (point-min))
11378 (while (re-search-forward org-dblock-start-re nil t)
11379 (goto-char (match-beginning 0))
11381 (condition-case nil
11383 (error (message "Error during update of dynamic block"))))
11384 (unless (re-search-forward org-dblock-end-re nil t)
11385 (error "Dynamic block not terminated"))))))
11387 (defun org-dblock-update (&optional arg)
11388 "User command for updating dynamic blocks.
11389 Update the dynamic block at point. With prefix ARG, update all dynamic
11390 blocks in the buffer."
11393 (org-update-all-dblocks)
11394 (or (looking-at org-dblock-start-re)
11395 (org-beginning-of-dblock))
11396 (org-update-dblock)))
11398 (defun org-update-dblock ()
11399 "Update the dynamic block at point.
11400 This means to empty the block, parse for parameters and then call
11401 the correct writing function."
11403 (save-window-excursion
11404 (let* ((pos (point))
11405 (line (org-current-line))
11406 (params (org-prepare-dblock))
11407 (name (plist-get params :name))
11408 (indent (plist-get params :indentation-column))
11409 (cmd (intern (concat "org-dblock-write:" name))))
11410 (message "Updating dynamic block `%s' at line %d..." name line)
11411 (funcall cmd params)
11412 (message "Updating dynamic block `%s' at line %d...done" name line)
11414 (when (and indent (> indent 0))
11415 (setq indent (make-string indent ?\ ))
11417 (org-beginning-of-dblock)
11419 (while (not (looking-at org-dblock-end-re))
11421 (beginning-of-line 2))
11422 (when (looking-at org-dblock-end-re)
11423 (and (looking-at "[ \t]+")
11424 (replace-match ""))
11425 (insert indent)))))))
11427 (defun org-beginning-of-dblock ()
11428 "Find the beginning of the dynamic block at point.
11429 Error if there is no such block at point."
11430 (let ((pos (point))
11433 (if (and (re-search-backward org-dblock-start-re nil t)
11434 (setq beg (match-beginning 0))
11435 (re-search-forward org-dblock-end-re nil t)
11436 (> (match-end 0) pos))
11439 (error "Not in a dynamic block"))))
11441 (defun org-update-all-dblocks ()
11442 "Update all dynamic blocks in the buffer.
11443 This function can be used in a hook."
11445 (when (derived-mode-p 'org-mode)
11446 (org-map-dblocks 'org-update-dblock)))
11451 (defconst org-additional-option-like-keywords
11452 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
11453 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
11454 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
11455 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
11457 "ORGTBL" "TBLFM:" "TBLNAME:"
11458 "BEGIN_EXAMPLE" "END_EXAMPLE"
11459 "BEGIN_VERBATIM" "END_VERBATIM"
11460 "BEGIN_QUOTE" "END_QUOTE"
11461 "BEGIN_VERSE" "END_VERSE"
11462 "BEGIN_CENTER" "END_CENTER"
11463 "BEGIN_SRC" "END_SRC"
11464 "BEGIN_RESULT" "END_RESULT"
11465 "BEGIN_lstlisting" "END_lstlisting"
11467 "HEADER:" "HEADERS:"
11468 "COLUMNS:" "PROPERTY:"
11469 "CAPTION:" "LABEL:"
11471 "INCLUDE:" "INDEX:"
11475 (defconst org-options-keywords
11476 '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
11477 "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
11478 "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
11479 "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
11480 "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
11481 "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
11482 "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
11484 (defconst org-additional-option-like-keywords-for-flyspell
11487 (mapconcat (lambda(k)
11488 (replace-regexp-in-string
11490 (concat k " " (downcase k) " " (upcase k))))
11491 (append org-options-keywords org-additional-option-like-keywords)
11495 (defcustom org-structure-template-alist
11496 '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
11497 "<src lang=\"?\">\n\n</src>")
11498 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
11499 "<example>\n?\n</example>")
11500 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
11501 "<quote>\n?\n</quote>")
11502 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
11503 "<verse>\n?\n</verse>")
11504 ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
11505 "<verbatim>\n?\n</verbatim>")
11506 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
11507 "<center>\n?\n</center>")
11508 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
11509 "<literal style=\"latex\">\n?\n</literal>")
11511 "<literal style=\"latex\">?</literal>")
11512 ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
11513 "<literal style=\"html\">\n?\n</literal>")
11515 "<literal style=\"html\">?</literal>")
11516 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
11520 ("I" "#+INCLUDE: %file ?"
11521 "<include file=%file markup=\"?\">"))
11522 "Structure completion elements.
11523 This is a list of abbreviation keys and values. The value gets inserted
11524 if you type `<' followed by the key and then press the completion key,
11525 usually `M-TAB'. %file will be replaced by a file name after prompting
11526 for the file using completion. The cursor will be placed at the position
11527 of the `?` in the template.
11528 There are two templates for each key, the first uses the original Org syntax,
11529 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
11530 the default when the /org-mtags.el/ module has been loaded. See also the
11531 variable `org-mtags-prefer-muse-templates'."
11532 :group 'org-completion
11534 (string :tag "Key")
11535 (string :tag "Template")
11536 (string :tag "Muse Template")))
11538 (defun org-try-structure-completion ()
11539 "Try to complete a structure template before point.
11540 This looks for strings like \"<e\" on an otherwise empty line and
11542 (let ((l (buffer-substring (point-at-bol) (point)))
11544 (when (and (looking-at "[ \t]*$")
11545 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
11546 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
11547 (org-complete-expand-structure-template (+ -1 (point-at-bol)
11548 (match-beginning 1)) a)
11551 (defun org-complete-expand-structure-template (start cell)
11552 "Expand a structure template."
11553 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
11554 (rpl (nth (if musep 2 1) cell))
11556 (delete-region start (point))
11557 (when (string-match "\\`#\\+" rpl)
11560 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
11561 (setq ind (buffer-substring (point-at-bol) (point))))
11563 (setq start (point))
11564 (if (string-match "%file" rpl)
11565 (setq rpl (replace-match
11569 (abbreviate-file-name (read-file-name "Include file: ")))
11572 (setq rpl (mapconcat 'identity (split-string rpl "\n")
11573 (concat "\n" ind)))
11575 (if (re-search-backward "\\?" start t) (delete-char 1))))
11577 ;;;; TODO, DEADLINE, Comments
11579 (defun org-toggle-comment ()
11580 "Change the COMMENT state of an entry."
11583 (org-back-to-heading)
11584 (let (case-fold-search)
11586 ((looking-at (format org-heading-keyword-regexp-format
11587 org-comment-string))
11588 (goto-char (match-end 1))
11589 (looking-at (concat " +" org-comment-string))
11590 (replace-match "" t t)
11591 (when (eolp) (insert " ")))
11592 ((looking-at org-outline-regexp)
11593 (goto-char (match-end 0))
11594 (insert org-comment-string " "))))))
11596 (defvar org-last-todo-state-is-todo nil
11597 "This is non-nil when the last TODO state change led to a TODO state.
11598 If the last change removed the TODO tag or switched to DONE, then
11601 (defvar org-setting-tags nil) ; dynamically skipped
11603 (defvar org-todo-setup-filter-hook nil
11604 "Hook for functions that pre-filter todo specs.
11605 Each function takes a todo spec and returns either nil or the spec
11606 transformed into canonical form." )
11608 (defvar org-todo-get-default-hook nil
11609 "Hook for functions that get a default item for todo.
11610 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
11611 nil or a string to be used for the todo mark." )
11613 (defvar org-agenda-headline-snapshot-before-repeat)
11615 (defun org-current-effective-time ()
11616 "Return current time adjusted for `org-extend-today-until' variable."
11617 (let* ((ct (org-current-time))
11618 (dct (decode-time ct))
11621 (org-use-last-clock-out-time-as-effective-time
11622 (or (org-clock-get-last-clock-out-time) ct))
11623 ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
11624 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
11628 (defun org-todo-yesterday (&optional arg)
11629 "Like `org-todo' but the time of change will be 23:59 of yesterday."
11631 (if (eq major-mode 'org-agenda-mode)
11632 (apply 'org-agenda-todo-yesterday arg)
11633 (let* ((hour (third (decode-time
11634 (org-current-time))))
11635 (org-extend-today-until (1+ hour)))
11638 (defvar org-block-entry-blocking ""
11639 "First entry preventing the TODO state change.")
11641 (defun org-todo (&optional arg)
11642 "Change the TODO state of an item.
11643 The state of an item is given by a keyword at the start of the heading,
11645 *** TODO Write paper
11648 The different keywords are specified in the variable `org-todo-keywords'.
11649 By default the available states are \"TODO\" and \"DONE\".
11650 So for this example: when the item starts with TODO, it is changed to DONE.
11651 When it starts with DONE, the DONE is removed. And when neither TODO nor
11652 DONE are present, add TODO at the beginning of the heading.
11654 With \\[universal-argument] prefix arg, use completion to determine the new \
11656 With numeric prefix arg, switch to that state.
11657 With a double \\[universal-argument] prefix, switch to the next set of TODO \
11658 keywords (nextset).
11659 With a triple \\[universal-argument] prefix, circumvent any state blocking.
11660 With a numeric prefix arg of 0, inhibit note taking for the change.
11662 For calling through lisp, arg is also interpreted in the following way:
11663 'none -> empty state
11664 \"\"(empty string) -> switch to empty state
11665 'done -> switch to DONE
11666 'nextset -> switch to the next set of keywords
11667 'previousset -> switch to the previous set of keywords
11668 \"WAITING\" -> switch to the specified keyword, but only if it
11669 really is a member of `org-todo-keywords'."
11671 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
11672 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
11673 'region-start-level 'region))
11674 org-loop-over-headlines-in-active-region)
11677 org-loop-over-headlines-in-active-region
11678 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
11679 (if (equal arg '(16)) (setq arg 'nextset))
11680 (let ((org-blocker-hook org-blocker-hook)
11683 (when (equal arg '(64))
11684 (setq arg nil org-blocker-hook nil))
11685 (when (and org-blocker-hook
11686 (or org-inhibit-blocking
11687 (org-entry-get nil "NOBLOCKING")))
11688 (setq org-blocker-hook nil))
11691 (org-back-to-heading t)
11692 (when (looking-at (concat "^\\*+ " org-comment-string))
11693 (org-toggle-comment)
11695 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
11696 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
11697 (looking-at "\\(?: *\\|[ \t]*$\\)"))
11698 (let* ((match-data (match-data))
11699 (startpos (point-at-bol))
11700 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
11701 (org-log-done org-log-done)
11702 (org-log-repeat org-log-repeat)
11703 (org-todo-log-states org-todo-log-states)
11704 (org-inhibit-logging
11706 (progn (setq arg nil) 'note) org-inhibit-logging))
11707 (this (match-string 1))
11708 (hl-pos (match-beginning 0))
11709 (head (org-get-todo-sequence-head this))
11710 (ass (assoc head org-todo-kwd-alist))
11711 (interpret (nth 1 ass))
11712 (done-word (nth 3 ass))
11713 (final-done-word (nth 4 ass))
11714 (org-last-state (or this ""))
11715 (completion-ignore-case t)
11716 (member (member this org-todo-keywords-1))
11717 (tail (cdr member))
11719 ((and org-todo-key-trigger
11720 (or (and (equal arg '(4))
11721 (eq org-use-fast-todo-selection 'prefix))
11722 (and (not arg) org-use-fast-todo-selection
11723 (not (eq org-use-fast-todo-selection
11725 ;; Use fast selection
11726 (org-fast-todo-selection))
11727 ((and (equal arg '(4))
11728 (or (not org-use-fast-todo-selection)
11729 (not org-todo-key-trigger)))
11730 ;; Read a state with completion
11731 (org-icompleting-read
11732 "State: " (mapcar (lambda(x) (list x))
11733 org-todo-keywords-1)
11737 (if tail (car tail) nil)
11738 (car org-todo-keywords-1)))
11740 (if (equal member org-todo-keywords-1)
11743 (nth (- (length org-todo-keywords-1)
11745 org-todo-keywords-1)
11746 (org-last org-todo-keywords-1))))
11747 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
11748 (setq arg nil))) ; hack to fall back to cycling
11750 ;; user or caller requests a specific state
11752 ((equal arg "") nil)
11753 ((eq arg 'none) nil)
11754 ((eq arg 'done) (or done-word (car org-done-keywords)))
11756 (or (car (cdr (member head org-todo-heads)))
11757 (car org-todo-heads)))
11758 ((eq arg 'previousset)
11759 (let ((org-todo-heads (reverse org-todo-heads)))
11760 (or (car (cdr (member head org-todo-heads)))
11761 (car org-todo-heads))))
11762 ((car (member arg org-todo-keywords-1)))
11764 (error "State `%s' not valid in this file" arg))
11765 ((nth (1- (prefix-numeric-value arg))
11766 org-todo-keywords-1))))
11767 ((null member) (or head (car org-todo-keywords-1)))
11768 ((equal this final-done-word) nil) ;; -> make empty
11769 ((null tail) nil) ;; -> first entry
11770 ((memq interpret '(type priority))
11771 (if (eq this-command last-command)
11773 (if (> (length tail) 0)
11774 (or done-word (car org-done-keywords))
11779 (run-hook-with-args-until-success
11780 'org-todo-get-default-hook org-state org-last-state)
11782 (next (if org-state (concat " " org-state " ") " "))
11783 (change-plist (list :type 'todo-state-change :from this :to org-state
11784 :position startpos))
11786 (when org-blocker-hook
11787 (setq org-last-todo-state-is-todo
11788 (not (member this org-done-keywords)))
11789 (unless (save-excursion
11791 (org-with-wide-buffer
11792 (run-hook-with-args-until-failure
11793 'org-blocker-hook change-plist))))
11794 (if (org-called-interactively-p 'interactive)
11795 (user-error "TODO state change from %s to %s blocked (by \"%s\")"
11796 this org-state org-block-entry-blocking)
11798 (message "TODO state change from %s to %s blocked (by \"%s\")"
11799 this org-state org-block-entry-blocking)
11800 (throw 'exit nil))))
11801 (store-match-data match-data)
11802 (replace-match next t t)
11803 (unless (pos-visible-in-window-p hl-pos)
11804 (message "TODO state changed to %s" (org-trim next)))
11806 (setq head (org-get-todo-sequence-head org-state)
11807 ass (assoc head org-todo-kwd-alist)
11808 interpret (nth 1 ass)
11809 done-word (nth 3 ass)
11810 final-done-word (nth 4 ass)))
11811 (when (memq arg '(nextset previousset))
11812 (message "Keyword-Set %d/%d: %s"
11813 (- (length org-todo-sets) -1
11814 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
11815 (length org-todo-sets)
11816 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
11817 (setq org-last-todo-state-is-todo
11818 (not (member org-state org-done-keywords)))
11819 (setq now-done-p (and (member org-state org-done-keywords)
11820 (not (member this org-done-keywords))))
11821 (and logging (org-local-logging logging))
11822 (when (and (or org-todo-log-states org-log-done)
11823 (not (eq org-inhibit-logging t))
11824 (not (memq arg '(nextset previousset))))
11825 ;; we need to look at recording a time and note
11826 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
11827 (nth 2 (assoc this org-todo-log-states))))
11828 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
11829 (setq dolog 'time))
11830 (when (and org-state
11831 (member org-state org-not-done-keywords)
11832 (not (member this org-not-done-keywords)))
11833 ;; This is now a todo state and was not one before
11834 ;; If there was a CLOSED time stamp, get rid of it.
11835 (org-add-planning-info nil nil 'closed))
11836 (when (and now-done-p org-log-done)
11837 ;; It is now done, and it was not done before
11838 (org-add-planning-info 'closed (org-current-effective-time))
11839 (if (and (not dolog) (eq 'note org-log-done))
11840 (org-add-log-setup 'done org-state this 'findpos 'note)))
11841 (when (and org-state dolog)
11842 ;; This is a non-nil state, and we need to log it
11843 (org-add-log-setup 'state org-state this 'findpos dolog)))
11844 ;; Fixup tag positioning
11845 (org-todo-trigger-tag-changes org-state)
11846 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
11847 (when org-provide-todo-statistics
11848 (org-update-parent-todo-statistics))
11849 (run-hooks 'org-after-todo-state-change-hook)
11850 (if (and arg (not (member org-state org-done-keywords)))
11851 (setq head (org-get-todo-sequence-head org-state)))
11852 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
11853 ;; Do we need to trigger a repeat?
11855 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
11856 ;; This is for the agenda, take a snapshot of the headline.
11858 (setq org-agenda-headline-snapshot-before-repeat
11859 (org-get-heading))))
11860 (org-auto-repeat-maybe org-state))
11861 ;; Fixup cursor location if close to the keyword
11862 (if (and (outline-on-heading-p)
11864 (save-excursion (beginning-of-line 1)
11865 (looking-at org-todo-line-regexp))
11866 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
11868 (goto-char (or (match-end 2) (match-end 1)))
11869 (and (looking-at " ") (just-one-space))))
11870 (when org-trigger-hook
11872 (run-hook-with-args 'org-trigger-hook change-plist)))
11873 (when commentp (org-toggle-comment))))))))
11875 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
11876 "Block turning an entry into a TODO, using the hierarchy.
11877 This checks whether the current task should be blocked from state
11878 changes. Such blocking occurs when:
11880 1. The task has children which are not all in a completed state.
11882 2. A task has a parent with the property :ORDERED:, and there
11883 are siblings prior to the current task with incomplete
11886 3. The parent of the task is blocked because it has siblings that should
11887 be done first, or is child of a block grandparent TODO entry."
11889 (if (not org-enforce-todo-dependencies)
11890 t ; if locally turned off don't block
11892 ;; If this is not a todo state change, or if this entry is already DONE,
11894 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11895 (member (plist-get change-plist :from)
11896 (cons 'done org-done-keywords))
11897 (member (plist-get change-plist :to)
11898 (cons 'todo org-not-done-keywords))
11899 (not (plist-get change-plist :to)))
11900 (throw 'dont-block t))
11901 ;; If this task has children, and any are undone, it's blocked
11903 (org-back-to-heading t)
11904 (let ((this-level (funcall outline-level)))
11905 (outline-next-heading)
11906 (let ((child-level (funcall outline-level)))
11907 (while (and (not (eobp))
11908 (> child-level this-level))
11909 ;; this todo has children, check whether they are all
11911 (if (and (not (org-entry-is-done-p))
11912 (org-entry-is-todo-p))
11913 (progn (setq org-block-entry-blocking (org-get-heading))
11914 (throw 'dont-block nil)))
11915 (outline-next-heading)
11916 (setq child-level (funcall outline-level))))))
11917 ;; Otherwise, if the task's parent has the :ORDERED: property, and
11918 ;; any previous siblings are undone, it's blocked
11920 (org-back-to-heading t)
11921 (let* ((pos (point))
11922 (parent-pos (and (org-up-heading-safe) (point))))
11923 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11924 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11926 (re-search-forward org-not-done-heading-regexp pos t))
11927 (setq org-block-entry-blocking (match-string 0))
11928 (throw 'dont-block nil)) ; block, there is an older sibling not done.
11929 ;; Search further up the hierarchy, to see if an ancestor is blocked
11931 (goto-char parent-pos)
11932 (if (not (looking-at org-not-done-heading-regexp))
11933 (throw 'dont-block t)) ; do not block, parent is not a TODO
11935 (setq parent-pos (and (org-up-heading-safe) (point)))
11936 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11937 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11939 (re-search-forward org-not-done-heading-regexp pos t)
11940 (setq org-block-entry-blocking (org-get-heading)))
11941 (throw 'dont-block nil)))))))) ; block, older sibling not done.
11943 (defcustom org-track-ordered-property-with-tag nil
11944 "Should the ORDERED property also be shown as a tag?
11945 The ORDERED property decides if an entry should require subtasks to be
11946 completed in sequence. Since a property is not very visible, setting
11947 this option means that toggling the ORDERED property with the command
11948 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
11949 not relevant for the behavior, but it makes things more visible.
11951 Note that toggling the tag with tags commands will not change the property
11952 and therefore not influence behavior!
11954 This can be t, meaning the tag ORDERED should be used, It can also be a
11955 string to select a different tag for this task."
11958 (const :tag "No tracking" nil)
11959 (const :tag "Track with ORDERED tag" t)
11960 (string :tag "Use other tag")))
11962 (defun org-toggle-ordered-property ()
11963 "Toggle the ORDERED property of the current entry.
11964 For better visibility, you can track the value of this property with a tag.
11965 See variable `org-track-ordered-property-with-tag'."
11967 (let* ((t1 org-track-ordered-property-with-tag)
11968 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
11970 (org-back-to-heading)
11971 (if (org-entry-get nil "ORDERED")
11973 (org-delete-property "ORDERED")
11974 (and tag (org-toggle-tag tag 'off))
11975 (message "Subtasks can be completed in arbitrary order"))
11976 (org-entry-put nil "ORDERED" "t")
11977 (and tag (org-toggle-tag tag 'on))
11978 (message "Subtasks must be completed in sequence")))))
11980 (defvar org-blocked-by-checkboxes) ; dynamically scoped
11981 (defun org-block-todo-from-checkboxes (change-plist)
11982 "Block turning an entry into a TODO, using checkboxes.
11983 This checks whether the current task should be blocked from state
11984 changes because there are unchecked boxes in this entry."
11985 (if (not org-enforce-todo-checkbox-dependencies)
11986 t ; if locally turned off don't block
11988 ;; If this is not a todo state change, or if this entry is already DONE,
11990 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11991 (member (plist-get change-plist :from)
11992 (cons 'done org-done-keywords))
11993 (member (plist-get change-plist :to)
11994 (cons 'todo org-not-done-keywords))
11995 (not (plist-get change-plist :to)))
11996 (throw 'dont-block t))
11997 ;; If this task has checkboxes that are not checked, it's blocked
11999 (org-back-to-heading t)
12000 (let ((beg (point)) end)
12001 (outline-next-heading)
12004 (if (org-list-search-forward
12005 (concat (org-item-beginning-re)
12006 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
12010 (if (boundp 'org-blocked-by-checkboxes)
12011 (setq org-blocked-by-checkboxes t))
12012 (throw 'dont-block nil)))))
12013 t))) ; do not block
12015 (defun org-entry-blocked-p ()
12016 "Is the current entry blocked?"
12017 (org-with-buffer-modified-unmodified
12018 (if (org-entry-get nil "NOBLOCKING")
12019 nil ;; Never block this entry
12021 (run-hook-with-args-until-failure
12023 (list :type 'todo-state-change
12028 (defun org-update-statistics-cookies (all)
12029 "Update the statistics cookie, either from TODO or from checkboxes.
12030 This should be called with the cursor in a line with a statistics cookie."
12034 (org-update-checkbox-count 'all)
12035 (org-map-entries 'org-update-parent-todo-statistics))
12036 (if (not (org-at-heading-p))
12037 (org-update-checkbox-count)
12038 (let ((pos (point-marker))
12040 (ignore-errors (org-back-to-heading t))
12041 (if (not (org-at-heading-p))
12042 (org-update-checkbox-count)
12043 (setq l1 (org-outline-level))
12044 (setq end (save-excursion
12045 (outline-next-heading)
12046 (if (org-at-heading-p) (setq l2 (org-outline-level)))
12048 (if (and (save-excursion
12050 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
12051 (not (save-excursion (re-search-forward
12052 ":COOKIE_DATA:.*\\<todo\\>" end t))))
12053 (org-update-checkbox-count)
12054 (if (and l2 (> l2 l1))
12057 (org-update-parent-todo-statistics))
12059 (beginning-of-line 1)
12060 (while (re-search-forward
12061 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
12063 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
12065 (move-marker pos nil)))))
12067 (defvar org-entry-property-inherited-from) ;; defined below
12068 (defun org-update-parent-todo-statistics ()
12069 "Update any statistics cookie in the parent of the current headline.
12070 When `org-hierarchical-todo-statistics' is nil, statistics will cover
12071 the entire subtree and this will travel up the hierarchy and update
12072 statistics everywhere."
12073 (let* ((prop (save-excursion (org-up-heading-safe)
12074 (org-entry-get nil "COOKIE_DATA" 'inherit)))
12075 (recursive (or (not org-hierarchical-todo-statistics)
12076 (and prop (string-match "\\<recursive\\>" prop))))
12077 (lim (or (and prop (marker-position org-entry-property-inherited-from))
12080 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
12081 level ltoggle l1 new ndel
12082 (cnt-all 0) (cnt-done 0) is-percent kwd
12083 checkbox-beg ov ovs ove cookie-present)
12086 (beginning-of-line 1)
12087 (setq ltoggle (funcall outline-level))
12088 ;; Three situations are to consider:
12090 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
12091 ;; to the top-level ancestor on the headline;
12093 ;; 2. If parent has "recursive" property, repeat up to the
12094 ;; headline setting that property, taking inheritance into
12097 ;; 3. Else, move up to direct parent and proceed only once.
12098 (while (and (setq level (org-up-heading-safe))
12099 (or recursive first)
12101 (setq first nil cookie-present nil)
12105 (downcase (or (org-entry-get nil "COOKIE_DATA")
12108 (while (re-search-forward box-re (point-at-eol) t)
12109 (setq cnt-all 0 cnt-done 0 cookie-present t)
12110 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
12112 (unless (outline-next-heading) (throw 'exit nil))
12113 (while (and (looking-at org-complex-heading-regexp)
12114 (> (setq l1 (length (match-string 1))) level))
12115 (setq kwd (and (or recursive (= l1 ltoggle))
12117 (if (or (eq org-provide-todo-statistics 'all-headlines)
12118 (and (listp org-provide-todo-statistics)
12119 (or (member kwd org-provide-todo-statistics)
12120 (member kwd org-done-keywords))))
12121 (setq cnt-all (1+ cnt-all))
12122 (if (eq org-provide-todo-statistics t)
12123 (and kwd (setq cnt-all (1+ cnt-all)))))
12124 (and (member kwd org-done-keywords)
12125 (setq cnt-done (1+ cnt-done)))
12126 (outline-next-heading)))
12129 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
12130 (format "[%d/%d]" cnt-done cnt-all))
12131 ndel (- (match-end 0) checkbox-beg))
12132 ;; handle overlays when updating cookie from column view
12133 (when (setq ov (car (overlays-at checkbox-beg)))
12134 (setq ovs (overlay-start ov) ove (overlay-end ov))
12135 (delete-overlay ov))
12136 (goto-char checkbox-beg)
12138 (delete-region (point) (+ (point) ndel))
12139 (when org-auto-align-tags (org-fix-tags-on-the-fly))
12140 (when ov (move-overlay ov ovs ove)))
12141 (when cookie-present
12142 (run-hook-with-args 'org-after-todo-statistics-hook
12143 cnt-done (- cnt-all cnt-done))))))
12144 (run-hooks 'org-todo-statistics-hook)))
12146 (defvar org-after-todo-statistics-hook nil
12147 "Hook that is called after a TODO statistics cookie has been updated.
12148 Each function is called with two arguments: the number of not-done entries
12149 and the number of done entries.
12151 For example, the following function, when added to this hook, will switch
12152 an entry to DONE when all children are done, and back to TODO when new
12153 entries are set to a TODO status. Note that this hook is only called
12154 when there is a statistics cookie in the headline!
12156 (defun org-summary-todo (n-done n-not-done)
12157 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
12158 (let (org-log-done org-log-states) ; turn off logging
12159 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
12162 (defvar org-todo-statistics-hook nil
12163 "Hook that is run whenever Org thinks TODO statistics should be updated.
12164 This hook runs even if there is no statistics cookie present, in which case
12165 `org-after-todo-statistics-hook' would not run.")
12167 (defun org-todo-trigger-tag-changes (state)
12168 "Apply the changes defined in `org-todo-state-tags-triggers'."
12169 (let ((l org-todo-state-tags-triggers)
12171 (when (or (not state) (equal state ""))
12172 (setq changes (append changes (cdr (assoc "" l)))))
12173 (when (and (stringp state) (> (length state) 0))
12174 (setq changes (append changes (cdr (assoc state l)))))
12175 (when (member state org-not-done-keywords)
12176 (setq changes (append changes (cdr (assoc 'todo l)))))
12177 (when (member state org-done-keywords)
12178 (setq changes (append changes (cdr (assoc 'done l)))))
12179 (dolist (c changes)
12180 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
12182 (defun org-local-logging (value)
12183 "Get logging settings from a property VALUE."
12185 ;; directly set the variables, they are already local.
12186 (setq org-log-done nil
12188 org-todo-log-states nil)
12189 (setq words (org-split-string value))
12190 (while (setq w (pop words))
12192 ((setq a (assoc w org-startup-options))
12193 (and (member (nth 1 a) '(org-log-done org-log-repeat))
12194 (set (nth 1 a) (nth 2 a))))
12195 ((setq a (org-extract-log-state-settings w))
12196 (and (member (car a) org-todo-keywords-1)
12197 (push a org-todo-log-states)))))))
12199 (defun org-get-todo-sequence-head (kwd)
12200 "Return the head of the TODO sequence to which KWD belongs.
12201 If KWD is not set, check if there is a text property remembering the
12206 (or (get-text-property (point-at-bol) 'org-todo-head)
12208 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
12209 nil (point-at-eol)))
12210 (get-text-property p 'org-todo-head))))
12211 ((not (member kwd org-todo-keywords-1))
12212 (car org-todo-keywords-1))
12213 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
12215 (defun org-fast-todo-selection ()
12216 "Fast TODO keyword selection with single keys.
12217 Returns the new TODO keyword, or nil if no state change should occur."
12218 (let* ((fulltable org-todo-key-alist)
12219 (done-keywords org-done-keywords) ;; needed for the faces.
12220 (maxlen (apply 'max (mapcar
12222 (if (stringp (car x)) (string-width (car x)) 0))
12225 (fwidth (+ maxlen 3 1 3))
12226 (ncol (/ (- (window-width) 4) fwidth))
12230 (save-window-excursion
12232 (set-buffer (get-buffer-create " *Org todo*"))
12233 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
12235 (org-set-local 'org-done-keywords done-keywords)
12236 (setq tbl fulltable cnt 0)
12237 (while (setq e (pop tbl))
12239 ((equal e '(:startgroup))
12240 (push '() groups) (setq ingroup t)
12241 (when (not (= cnt 0))
12245 ((equal e '(:endgroup))
12246 (setq ingroup nil cnt 0)
12248 ((equal e '(:newline))
12249 (when (not (= cnt 0))
12253 (while (equal (car tbl) '(:newline))
12255 (setq tbl (cdr tbl)))))
12257 (setq tg (car e) c (cdr e))
12258 (if ingroup (push tg (car groups)))
12259 (setq tg (org-add-props tg nil 'face
12260 (org-get-todo-face tg)))
12261 (if (and (= cnt 0) (not ingroup)) (insert " "))
12262 (insert "[" c "] " tg (make-string
12263 (- fwidth 4 (length tg)) ?\ ))
12264 (when (= (setq cnt (1+ cnt)) ncol)
12266 (if ingroup (insert " "))
12269 (goto-char (point-min))
12270 (if (not expert) (org-fit-window-to-buffer))
12271 (message "[a-z..]:Set [SPC]:clear")
12272 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
12275 (and (= c ?q) (not (rassoc c fulltable))))
12276 (setq quit-flag t))
12278 ((setq e (rassoc c fulltable) tg (car e))
12280 (t (setq quit-flag t)))))))
12282 (defun org-entry-is-todo-p ()
12283 (member (org-get-todo-state) org-not-done-keywords))
12285 (defun org-entry-is-done-p ()
12286 (member (org-get-todo-state) org-done-keywords))
12288 (defun org-get-todo-state ()
12290 (org-back-to-heading t)
12291 (and (looking-at org-todo-line-regexp)
12293 (match-string 2))))
12295 (defun org-at-date-range-p (&optional inactive-ok)
12296 "Is the cursor inside a date range?"
12300 (let ((pos (point)))
12301 (skip-chars-backward "^[<\r\n")
12302 (skip-chars-backward "<[")
12303 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12304 (>= (match-end 0) pos)
12306 (skip-chars-backward "^<[\r\n")
12307 (skip-chars-backward "<[")
12308 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12309 (>= (match-end 0) pos)
12313 (defun org-get-repeat (&optional tagline)
12314 "Check if there is a deadline/schedule with repeater in this entry."
12317 (org-back-to-heading t)
12318 (and (re-search-forward (if tagline
12319 (concat tagline "\\s-*" org-repeat-re)
12321 (org-entry-end-position) t)
12322 (match-string-no-properties 1)))))
12324 (defvar org-last-changed-timestamp)
12325 (defvar org-last-inserted-timestamp)
12326 (defvar org-log-post-message)
12327 (defvar org-log-note-purpose)
12328 (defvar org-log-note-how)
12329 (defvar org-log-note-extra)
12330 (defun org-auto-repeat-maybe (done-word)
12331 "Check if the current headline contains a repeated deadline/schedule.
12332 If yes, set TODO state back to what it was and change the base date
12333 of repeating deadline/scheduled time stamps to new date.
12334 This function is run automatically after each state change to a DONE state."
12335 ;; last-state is dynamically scoped into this function
12336 (let* ((repeat (org-get-repeat))
12337 (aa (assoc org-last-state org-todo-kwd-alist))
12338 (interpret (nth 1 aa))
12340 (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
12341 (msg "Entry repeats: ")
12343 (org-todo-log-states nil)
12344 re type n what ts time to-state)
12346 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
12347 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
12348 org-todo-repeat-to-state))
12349 (unless (and to-state (member to-state org-todo-keywords-1))
12350 (setq to-state (if (eq interpret 'type) org-last-state head)))
12351 (org-todo to-state)
12352 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
12353 (org-entry-put nil "LAST_REPEAT" (format-time-string
12354 (org-time-stamp-format t t))))
12355 (when org-log-repeat
12356 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
12357 (memq 'org-add-log-note post-command-hook))
12358 ;; OK, we are already setup for some record
12359 (if (eq org-log-repeat 'note)
12360 ;; make sure we take a note, not only a time stamp
12361 (setq org-log-note-how 'note))
12362 ;; Set up for taking a record
12363 (org-add-log-setup 'state (or done-word (car org-done-keywords))
12365 'findpos org-log-repeat)))
12366 (org-back-to-heading t)
12367 (org-add-planning-info nil nil 'closed)
12368 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12369 org-deadline-time-regexp "\\)\\|\\("
12370 org-ts-regexp "\\)"))
12371 (while (re-search-forward
12372 re (save-excursion (outline-next-heading) (point)) t)
12373 (setq type (if (match-end 1) org-scheduled-string
12374 (if (match-end 3) org-deadline-string "Plain:"))
12375 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
12376 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
12377 (setq n (string-to-number (match-string 2 ts))
12378 what (match-string 3 ts))
12379 (if (equal what "w") (setq n (* n 7) what "d"))
12380 (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
12381 (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
12382 ;; Preparation, see if we need to modify the start date for the change
12383 (when (match-end 1)
12384 (setq time (save-match-data (org-time-string-to-time ts)))
12386 ((equal (match-string 1 ts) ".")
12387 ;; Shift starting date to today
12388 (org-timestamp-change
12389 (- (org-today) (time-to-days time))
12391 ((equal (match-string 1 ts) "+")
12392 (let ((nshiftmax 10) (nshift 0))
12393 (while (or (= nshift 0)
12394 (<= (time-to-days time)
12395 (time-to-days (current-time))))
12396 (when (= (incf nshift) nshiftmax)
12397 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
12399 (org-timestamp-change n (cdr (assoc what whata)))
12400 (org-at-timestamp-p t)
12401 (setq ts (match-string 1))
12402 (setq time (save-match-data (org-time-string-to-time ts)))))
12403 (org-timestamp-change (- n) (cdr (assoc what whata)))
12404 ;; rematch, so that we have everything in place for the real shift
12405 (org-at-timestamp-p t)
12406 (setq ts (match-string 1))
12407 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
12408 (org-timestamp-change n (cdr (assoc what whata)))
12409 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
12410 (setq org-log-post-message msg)
12411 (message "%s" msg))))
12413 (defun org-show-todo-tree (arg)
12414 "Make a compact tree which shows all headlines marked with TODO.
12415 The tree will show the lines where the regexp matches, and all higher
12416 headlines above the match.
12417 With a \\[universal-argument] prefix, prompt for a regexp to match.
12418 With a numeric prefix N, construct a sparse tree for the Nth element
12419 of `org-todo-keywords-1'."
12421 (let ((case-fold-search nil)
12423 (cond ((null arg) org-not-done-regexp)
12425 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
12426 (mapcar 'list org-todo-keywords-1))))
12428 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12430 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12431 (regexp-quote (nth (1- (prefix-numeric-value arg))
12432 org-todo-keywords-1)))
12433 (t (error "Invalid prefix argument: %s" arg)))))
12434 (message "%d TODO entries found"
12435 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
12437 (defun org-deadline (&optional remove time)
12438 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
12439 With argument REMOVE, remove any deadline from the item.
12440 With argument TIME, set the deadline at the corresponding date. TIME
12441 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12443 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12444 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12445 'region-start-level 'region))
12446 org-loop-over-headlines-in-active-region)
12448 `(org-deadline ',remove ,time)
12449 org-loop-over-headlines-in-active-region
12450 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12451 (let* ((old-date (org-entry-get nil "DEADLINE"))
12452 (repeater (and old-date
12454 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12456 (match-string 1 old-date))))
12459 (when (and old-date org-log-redeadline)
12460 (org-add-log-setup 'deldeadline nil old-date 'findpos
12461 org-log-redeadline))
12462 (org-remove-timestamp-with-keyword org-deadline-string)
12463 (message "Item no longer has a deadline."))
12464 (org-add-planning-info 'deadline time 'closed)
12465 (when (and old-date org-log-redeadline
12466 (not (equal old-date
12467 (substring org-last-inserted-timestamp 1 -1))))
12468 (org-add-log-setup 'redeadline nil old-date 'findpos
12469 org-log-redeadline))
12472 (org-back-to-heading t)
12473 (when (re-search-forward (concat org-deadline-string " "
12474 org-last-inserted-timestamp)
12476 (outline-next-heading) (point)) t)
12477 (goto-char (1- (match-end 0)))
12478 (insert " " repeater)
12479 (setq org-last-inserted-timestamp
12480 (concat (substring org-last-inserted-timestamp 0 -1)
12482 (substring org-last-inserted-timestamp -1))))))
12483 (message "Deadline on %s" org-last-inserted-timestamp)))))
12485 (defun org-schedule (&optional remove time)
12486 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
12487 With argument REMOVE, remove any scheduling date from the item.
12488 With argument TIME, scheduled at the corresponding date. TIME can
12489 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12491 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12492 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12493 'region-start-level 'region))
12494 org-loop-over-headlines-in-active-region)
12496 `(org-schedule ',remove ,time)
12497 org-loop-over-headlines-in-active-region
12498 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12499 (let* ((old-date (org-entry-get nil "SCHEDULED"))
12500 (repeater (and old-date
12502 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12504 (match-string 1 old-date))))
12507 (when (and old-date org-log-reschedule)
12508 (org-add-log-setup 'delschedule nil old-date 'findpos
12509 org-log-reschedule))
12510 (org-remove-timestamp-with-keyword org-scheduled-string)
12511 (message "Item is no longer scheduled."))
12512 (org-add-planning-info 'scheduled time 'closed)
12513 (when (and old-date org-log-reschedule
12514 (not (equal old-date
12515 (substring org-last-inserted-timestamp 1 -1))))
12516 (org-add-log-setup 'reschedule nil old-date 'findpos
12517 org-log-reschedule))
12520 (org-back-to-heading t)
12521 (when (re-search-forward (concat org-scheduled-string " "
12522 org-last-inserted-timestamp)
12524 (outline-next-heading) (point)) t)
12525 (goto-char (1- (match-end 0)))
12526 (insert " " repeater)
12527 (setq org-last-inserted-timestamp
12528 (concat (substring org-last-inserted-timestamp 0 -1)
12530 (substring org-last-inserted-timestamp -1))))))
12531 (message "Scheduled to %s" org-last-inserted-timestamp)))))
12533 (defun org-get-scheduled-time (pom &optional inherit)
12534 "Get the scheduled time as a time tuple, of a format suitable
12535 for calling org-schedule with, or if there is no scheduling,
12537 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
12539 (apply 'encode-time (org-parse-time-string time)))))
12541 (defun org-get-deadline-time (pom &optional inherit)
12542 "Get the deadline as a time tuple, of a format suitable for
12543 calling org-deadline with, or if there is no scheduling, returns
12545 (let ((time (org-entry-get pom "DEADLINE" inherit)))
12547 (apply 'encode-time (org-parse-time-string time)))))
12549 (defun org-remove-timestamp-with-keyword (keyword)
12550 "Remove all time stamps with KEYWORD in the current entry."
12551 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
12554 (org-back-to-heading t)
12556 (outline-next-heading)
12557 (while (re-search-backward re beg t)
12559 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
12560 (equal (char-before) ?\ ))
12561 (backward-delete-char 1)
12562 (if (string-match "^[ \t]*$" (buffer-substring
12563 (point-at-bol) (point-at-eol)))
12564 (delete-region (point-at-bol)
12565 (min (point-max) (1+ (point-at-eol))))))))))
12567 (defun org-add-planning-info (what &optional time &rest remove)
12568 "Insert new timestamp with keyword in the line directly after the headline.
12569 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
12570 If non is given, the user is prompted for a date.
12571 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
12574 (let (org-time-was-given org-end-time-was-given ts
12575 end default-time default-input)
12578 (when (and (memq what '(scheduled deadline))
12580 (and (stringp time)
12581 (string-match "^[-+]+[0-9]" time))))
12582 ;; Try to get a default date/time from existing timestamp
12584 (org-back-to-heading t)
12585 (setq end (save-excursion (outline-next-heading) (point)))
12586 (when (re-search-forward (if (eq what 'scheduled)
12587 org-scheduled-time-regexp
12588 org-deadline-time-regexp)
12590 (setq ts (match-string 1)
12592 (apply 'encode-time (org-parse-time-string ts))
12593 default-input (and ts (org-get-compact-tod ts))))))
12597 ;; This is a string (relative or absolute), set proper date
12598 (apply 'encode-time
12599 (org-read-date-analyze
12600 time default-time (decode-time default-time)))
12601 ;; If necessary, get the time from the user
12602 (or time (org-read-date nil 'to-time nil nil
12603 default-time default-input)))))
12605 (when (and org-insert-labeled-timestamps-at-point
12606 (member what '(scheduled deadline)))
12608 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
12609 (org-insert-time-stamp time org-time-was-given
12610 nil nil nil (list org-end-time-was-given))
12614 (let (col list elt ts buffer-invisibility-spec)
12615 (org-back-to-heading t)
12616 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
12617 (goto-char (match-end 1))
12618 (setq col (current-column))
12619 (goto-char (match-end 0))
12620 (if (eobp) (insert "\n") (forward-char 1))
12621 (when (and (not what)
12624 org-keyword-time-not-clock-regexp))))
12625 ;; Nothing to add, nothing to remove...... :-)
12627 (if (and (not (looking-at org-outline-regexp))
12628 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
12630 (not (equal (match-string 1) org-clock-string)))
12631 (narrow-to-region (match-beginning 0) (match-end 0))
12632 (insert-before-markers "\n")
12634 (narrow-to-region (point) (point))
12635 (and org-adapt-indentation (org-indent-to-column col)))
12636 ;; Check if we have to remove something.
12637 (setq list (cons what remove))
12639 (setq elt (pop list))
12640 (when (or (and (eq elt 'scheduled)
12641 (re-search-forward org-scheduled-time-regexp nil t))
12642 (and (eq elt 'deadline)
12643 (re-search-forward org-deadline-time-regexp nil t))
12644 (and (eq elt 'closed)
12645 (re-search-forward org-closed-time-regexp nil t)))
12647 (if (looking-at "--+<[^>]+>") (replace-match ""))))
12648 (and (looking-at "[ \t]+") (replace-match ""))
12649 (and org-adapt-indentation (bolp) (org-indent-to-column col))
12652 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
12653 (cond ((eq what 'scheduled) org-scheduled-string)
12654 ((eq what 'deadline) org-deadline-string)
12655 ((eq what 'closed) org-closed-string))
12657 (setq ts (org-insert-time-stamp
12659 (or org-time-was-given
12660 (and (eq what 'closed) org-log-done-with-time))
12662 nil nil (list org-end-time-was-given)))
12664 (if (not (or (bolp) (eq (char-before) ?\ )
12665 (memq (char-after) '(32 10))
12668 (goto-char (point-min))
12670 (if (and (looking-at "[ \t]*\n")
12671 (equal (char-before) ?\n))
12672 (delete-region (1- (point)) (point-at-eol)))
12675 (defvar org-log-note-marker (make-marker))
12676 (defvar org-log-note-purpose nil)
12677 (defvar org-log-note-state nil)
12678 (defvar org-log-note-previous-state nil)
12679 (defvar org-log-note-how nil)
12680 (defvar org-log-note-extra nil)
12681 (defvar org-log-note-window-configuration nil)
12682 (defvar org-log-note-return-to (make-marker))
12683 (defvar org-log-note-effective-time nil
12684 "Remembered current time so that dynamically scoped
12685 `org-extend-today-until' affects tha timestamps in state change
12688 (defvar org-log-post-message nil
12689 "Message to be displayed after a log note has been stored.
12690 The auto-repeater uses this.")
12692 (defun org-add-note ()
12693 "Add a note to the current entry.
12694 This is done in the same way as adding a state change note."
12696 (org-add-log-setup 'note nil nil 'findpos nil))
12698 (defvar org-property-end-re)
12699 (defun org-add-log-setup (&optional purpose state prev-state
12701 "Set up the post command hook to take a note.
12702 If this is about to TODO state change, the new state is expected in STATE.
12703 When FINDPOS is non-nil, find the correct position for the note in
12704 the current entry. If not, assume that it can be inserted at point.
12705 HOW is an indicator what kind of note should be created.
12706 EXTRA is additional text that will be inserted into the notes buffer."
12707 (let* ((org-log-into-drawer (org-log-into-drawer))
12708 (drawer (cond ((stringp org-log-into-drawer)
12709 org-log-into-drawer)
12710 (org-log-into-drawer "LOGBOOK"))))
12714 (org-back-to-heading t)
12715 (narrow-to-region (point) (save-excursion
12716 (outline-next-heading) (point)))
12717 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
12718 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
12720 (goto-char (match-end 0))
12723 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
12726 (goto-char (match-end 0))
12727 (or org-log-states-order-reversed
12728 (and (re-search-forward org-property-end-re nil t)
12729 (goto-char (1- (match-beginning 0))))))
12730 (insert "\n:" drawer ":\n:END:")
12731 (beginning-of-line 0)
12733 (beginning-of-line 2)
12736 ((and org-log-state-notes-insert-after-drawers
12738 (forward-line) (looking-at org-drawer-regexp)))
12740 (while (looking-at org-drawer-regexp)
12741 (goto-char (match-end 0))
12742 (re-search-forward org-property-end-re (point-max) t)
12744 (forward-line -1)))
12745 (unless org-log-states-order-reversed
12746 (and (= (char-after) ?\n) (forward-char 1))
12747 (org-skip-over-state-notes)
12748 (skip-chars-backward " \t\n\r")))
12749 (move-marker org-log-note-marker (point))
12750 (setq org-log-note-purpose purpose
12751 org-log-note-state state
12752 org-log-note-previous-state prev-state
12753 org-log-note-how how
12754 org-log-note-extra extra
12755 org-log-note-effective-time (org-current-effective-time))
12756 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
12758 (defun org-skip-over-state-notes ()
12759 "Skip past the list of State notes in an entry."
12760 (if (looking-at "\n[ \t]*- State") (forward-char 1))
12761 (when (ignore-errors (goto-char (org-in-item-p)))
12762 (let* ((struct (org-list-struct))
12763 (prevs (org-list-prevs-alist struct)))
12764 (while (looking-at "[ \t]*- State")
12765 (goto-char (or (org-list-get-next-item (point) struct prevs)
12766 (org-list-get-item-end (point) struct)))))))
12768 (defun org-add-log-note (&optional purpose)
12769 "Pop up a window for taking a note, and add this note later at point."
12770 (remove-hook 'post-command-hook 'org-add-log-note)
12771 (setq org-log-note-window-configuration (current-window-configuration))
12772 (delete-other-windows)
12773 (move-marker org-log-note-return-to (point))
12774 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
12775 (goto-char org-log-note-marker)
12776 (org-switch-to-buffer-other-window "*Org Note*")
12778 (if (memq org-log-note-how '(time state))
12779 (let (current-prefix-arg) (org-store-log-note))
12780 (let ((org-inhibit-startup t)) (org-mode))
12781 (insert (format "# Insert note for %s.
12782 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
12784 ((eq org-log-note-purpose 'clock-out) "stopped clock")
12785 ((eq org-log-note-purpose 'done) "closed todo item")
12786 ((eq org-log-note-purpose 'state)
12787 (format "state change from \"%s\" to \"%s\""
12788 (or org-log-note-previous-state "")
12789 (or org-log-note-state "")))
12790 ((eq org-log-note-purpose 'reschedule)
12792 ((eq org-log-note-purpose 'delschedule)
12793 "no longer scheduled")
12794 ((eq org-log-note-purpose 'redeadline)
12795 "changing deadline")
12796 ((eq org-log-note-purpose 'deldeadline)
12797 "removing deadline")
12798 ((eq org-log-note-purpose 'refile)
12800 ((eq org-log-note-purpose 'note)
12802 (t (error "This should not happen")))))
12803 (if org-log-note-extra (insert org-log-note-extra))
12804 (org-set-local 'org-finish-function 'org-store-log-note)
12805 (run-hooks 'org-log-buffer-setup-hook)))
12807 (defvar org-note-abort nil) ; dynamically scoped
12808 (defun org-store-log-note ()
12809 "Finish taking a log note, and insert it to where it belongs."
12810 (let ((txt (buffer-string)))
12811 (kill-buffer (current-buffer))
12812 (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
12814 (while (string-match "\\`# .*\n[ \t\n]*" txt)
12815 (setq txt (replace-match "" t t txt)))
12816 (if (string-match "\\s-+\\'" txt)
12817 (setq txt (replace-match "" t t txt)))
12818 (setq lines (org-split-string txt "\n"))
12819 (when (and note (string-match "\\S-" note))
12821 (org-replace-escapes
12823 (list (cons "%u" (user-login-name))
12824 (cons "%U" user-full-name)
12825 (cons "%t" (format-time-string
12826 (org-time-stamp-format 'long 'inactive)
12827 org-log-note-effective-time))
12828 (cons "%T" (format-time-string
12829 (org-time-stamp-format 'long nil)
12830 org-log-note-effective-time))
12831 (cons "%d" (format-time-string
12832 (org-time-stamp-format nil 'inactive)
12833 org-log-note-effective-time))
12834 (cons "%D" (format-time-string
12835 (org-time-stamp-format nil nil)
12836 org-log-note-effective-time))
12837 (cons "%s" (if org-log-note-state
12838 (concat "\"" org-log-note-state "\"")
12840 (cons "%S" (if org-log-note-previous-state
12841 (concat "\"" org-log-note-previous-state "\"")
12843 (if lines (setq note (concat note " \\\\")))
12845 (when (or current-prefix-arg org-note-abort)
12846 (when org-log-into-drawer
12847 (org-remove-empty-drawer-at
12848 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
12849 org-log-note-marker))
12852 (with-current-buffer (marker-buffer org-log-note-marker)
12854 (goto-char org-log-note-marker)
12855 (move-marker org-log-note-marker nil)
12857 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
12858 (setq ind (save-excursion
12859 (if (ignore-errors (goto-char (org-in-item-p)))
12860 (let ((struct (org-list-struct)))
12862 (org-list-get-top-point struct) struct))
12863 (skip-chars-backward " \r\t\n")
12865 ((and (org-at-heading-p)
12866 org-adapt-indentation)
12867 (1+ (org-current-level)))
12868 ((org-at-heading-p) 0)
12869 (t (org-get-indentation))))))
12870 (setq bul (org-list-bullet-string "-"))
12871 (org-indent-line-to ind)
12872 (insert bul (pop lines))
12873 (let ((ind-body (+ (length bul) ind)))
12876 (org-indent-line-to ind-body)
12877 (insert (pop lines))))
12878 (message "Note stored")
12879 (org-back-to-heading t)
12880 (org-cycle-hide-drawers 'children))))))
12881 (set-window-configuration org-log-note-window-configuration)
12882 (with-current-buffer (marker-buffer org-log-note-return-to)
12883 (goto-char org-log-note-return-to))
12884 (move-marker org-log-note-return-to nil)
12885 (and org-log-post-message (message "%s" org-log-post-message)))
12887 (defun org-remove-empty-drawer-at (drawer pos)
12888 "Remove an empty drawer DRAWER at position POS.
12889 POS may also be a marker."
12890 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
12896 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
12897 (replace-match ""))))))
12899 (defvar org-ts-type nil)
12900 (defun org-sparse-tree (&optional arg type)
12901 "Create a sparse tree, prompt for the details.
12902 This command can create sparse trees. You first need to select the type
12903 of match used to create the tree:
12905 t Show all TODO entries.
12906 T Show entries with a specific TODO keyword.
12907 m Show entries selected by a tags/property match.
12908 p Enter a property name and its value (both with completion on existing
12909 names/values) and show entries with that property.
12910 r Show entries matching a regular expression (`/' can be used as well).
12911 b Show deadlines and scheduled items before a date.
12912 a Show deadlines and scheduled items after a date.
12913 d Show deadlines due within `org-deadline-warning-days'.
12914 D Show deadlines and scheduled items between a date range."
12916 (let (ans kwd value ts-type)
12917 (setq type (or type org-sparse-tree-default-date-type))
12918 (setq org-ts-type type)
12919 (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"
12920 (cond ((eq type 'all) "all timestamps")
12921 ((eq type 'scheduled) "only scheduled")
12922 ((eq type 'deadline) "only deadline")
12923 ((eq type 'active) "only active timestamps")
12924 ((eq type 'inactive) "only inactive timestamps")
12925 ((eq type 'scheduled-or-deadline) "scheduled/deadline")
12926 (t "scheduled/deadline")))
12927 (setq ans (read-char-exclusive))
12930 (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
12932 (call-interactively 'org-check-deadlines))
12934 (call-interactively 'org-check-before-date))
12936 (call-interactively 'org-check-after-date))
12938 (call-interactively 'org-check-dates-range))
12940 (call-interactively 'org-show-todo-tree))
12942 (org-show-todo-tree '(4)))
12943 ((member ans '(?T ?m))
12944 (call-interactively 'org-match-sparse-tree))
12945 ((member ans '(?p ?P))
12946 (setq kwd (org-icompleting-read "Property: "
12947 (mapcar 'list (org-buffer-property-keys))))
12948 (setq value (org-icompleting-read "Value: "
12949 (mapcar 'list (org-property-values kwd))))
12950 (unless (string-match "\\`{.*}\\'" value)
12951 (setq value (concat "\"" value "\"")))
12952 (org-match-sparse-tree arg (concat kwd "=" value)))
12953 ((member ans '(?r ?R ?/))
12954 (call-interactively 'org-occur))
12955 (t (error "No such sparse tree command \"%c\"" ans)))))
12957 (defvar org-occur-highlights nil
12958 "List of overlays used for occur matches.")
12959 (make-variable-buffer-local 'org-occur-highlights)
12960 (defvar org-occur-parameters nil
12961 "Parameters of the active org-occur calls.
12962 This is a list, each call to org-occur pushes as cons cell,
12963 containing the regular expression and the callback, onto the list.
12964 The list can contain several entries if `org-occur' has been called
12965 several time with the KEEP-PREVIOUS argument. Otherwise, this list
12966 will only contain one set of parameters. When the highlights are
12967 removed (for example with `C-c C-c', or with the next edit (depending
12968 on `org-remove-highlights-with-change'), this variable is emptied
12970 (make-variable-buffer-local 'org-occur-parameters)
12972 (defun org-occur (regexp &optional keep-previous callback)
12973 "Make a compact tree which shows all matches of REGEXP.
12974 The tree will show the lines where the regexp matches, and all higher
12975 headlines above the match. It will also show the heading after the match,
12976 to make sure editing the matching entry is easy.
12977 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
12978 call to `org-occur' will be kept, to allow stacking of calls to this
12980 If CALLBACK is non-nil, it is a function which is called to confirm
12981 that the match should indeed be shown."
12982 (interactive "sRegexp: \nP")
12983 (when (equal regexp "")
12984 (error "Regexp cannot be empty"))
12985 (unless keep-previous
12986 (org-remove-occur-highlights nil nil t))
12987 (push (cons regexp callback) org-occur-parameters)
12990 (goto-char (point-min))
12991 (if (or (not keep-previous) ; do not want to keep
12992 (not org-occur-highlights)) ; no previous matches
12995 (while (re-search-forward regexp nil t)
12996 (when (or (not callback)
12997 (save-match-data (funcall callback)))
12998 (setq cnt (1+ cnt))
12999 (when org-highlight-sparse-tree-matches
13000 (org-highlight-new-match (match-beginning 0) (match-end 0)))
13001 (org-show-context 'occur-tree))))
13002 (when org-remove-highlights-with-change
13003 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
13005 (unless org-sparse-tree-open-archived-trees
13006 (org-hide-archived-subtrees (point-min) (point-max)))
13007 (run-hooks 'org-occur-hook)
13008 (if (org-called-interactively-p 'interactive)
13009 (message "%d match(es) for regexp %s" cnt regexp))
13012 (defun org-occur-next-match (&optional n reset)
13013 "Function for `next-error-function' to find sparse tree matches.
13014 N is the number of matches to move, when negative move backwards.
13015 RESET is entirely ignored - this function always goes back to the
13016 starting point when no match is found."
13017 (let* ((limit (if (< n 0) (point-min) (point-max)))
13018 (search-func (if (< n 0)
13019 'previous-single-char-property-change
13020 'next-single-char-property-change))
13025 (while (setq p1 (funcall search-func (point) 'org-type))
13026 (when (equal p1 limit)
13028 (error "No more matches"))
13029 (when (equal (get-char-property p1 'org-type) 'org-occur)
13033 (throw 'exit (point))))
13036 (error "No more matches"))))
13038 (defun org-show-context (&optional key)
13039 "Make sure point and context are visible.
13040 How much context is shown depends upon the variables
13041 `org-show-hierarchy-above', `org-show-following-heading',
13042 `org-show-entry-below' and `org-show-siblings'."
13043 (let ((heading-p (org-at-heading-p t))
13044 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
13045 (following-p (org-get-alist-option org-show-following-heading key))
13046 (entry-p (org-get-alist-option org-show-entry-below key))
13047 (siblings-p (org-get-alist-option org-show-siblings key)))
13049 ;; Show heading or entry text
13050 (if (and heading-p (not entry-p))
13051 (org-flag-heading nil) ; only show the heading
13052 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
13053 (org-show-hidden-entry))) ; show entire entry
13055 ;; Show next sibling, or heading below text
13057 (and (if heading-p (org-goto-sibling) (outline-next-heading))
13058 (org-flag-heading nil))))
13059 (when siblings-p (org-show-siblings))
13061 ;; show all higher headings, possibly with siblings
13063 (while (and (condition-case nil
13064 (progn (org-up-heading-all 1) t)
13067 (org-flag-heading nil)
13068 (when siblings-p (org-show-siblings))))))))
13070 (defvar org-reveal-start-hook nil
13071 "Hook run before revealing a location.")
13073 (defun org-reveal (&optional siblings)
13074 "Show current entry, hierarchy above it, and the following headline.
13075 This can be used to show a consistent set of context around locations
13076 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
13077 not t for the search context.
13079 With optional argument SIBLINGS, on each level of the hierarchy all
13080 siblings are shown. This repairs the tree structure to what it would
13081 look like when opened with hierarchical calls to `org-cycle'.
13082 With double optional argument \\[universal-argument] \\[universal-argument], \
13083 go to the parent and show the
13086 (run-hooks 'org-reveal-start-hook)
13087 (let ((org-show-hierarchy-above t)
13088 (org-show-following-heading t)
13089 (org-show-siblings (if siblings t org-show-siblings)))
13090 (org-show-context nil))
13091 (when (equal siblings '(16))
13093 (when (org-up-heading-safe)
13095 (run-hook-with-args 'org-cycle-hook 'subtree)))))
13097 (defun org-highlight-new-match (beg end)
13098 "Highlight from BEG to END and mark the highlight is an occur headline."
13099 (let ((ov (make-overlay beg end)))
13100 (overlay-put ov 'face 'secondary-selection)
13101 (overlay-put ov 'org-type 'org-occur)
13102 (push ov org-occur-highlights)))
13104 (defun org-remove-occur-highlights (&optional beg end noremove)
13105 "Remove the occur highlights from the buffer.
13106 BEG and END are ignored. If NOREMOVE is nil, remove this function
13107 from the `before-change-functions' in the current buffer."
13109 (unless org-inhibit-highlight-removal
13110 (mapc 'delete-overlay org-occur-highlights)
13111 (setq org-occur-highlights nil)
13112 (setq org-occur-parameters nil)
13114 (remove-hook 'before-change-functions
13115 'org-remove-occur-highlights 'local))))
13119 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
13120 "Regular expression matching the priority indicator.")
13122 (defvar org-remove-priority-next-time nil)
13124 (defun org-priority-up ()
13125 "Increase the priority of the current item."
13127 (org-priority 'up))
13129 (defun org-priority-down ()
13130 "Decrease the priority of the current item."
13132 (org-priority 'down))
13134 (defun org-priority (&optional action show)
13135 "Change the priority of an item.
13136 ACTION can be `set', `up', `down', or a character."
13138 (if (equal action '(4))
13139 (org-show-priority)
13140 (unless org-enable-priority-commands
13141 (error "Priority commands are disabled"))
13142 (setq action (or action 'set))
13143 (let (current new news have remove)
13145 (org-back-to-heading t)
13146 (if (looking-at org-priority-regexp)
13147 (setq current (string-to-char (match-string 2))
13150 ((eq action 'remove)
13151 (setq remove t new ?\ ))
13152 ((or (eq action 'set)
13153 (if (featurep 'xemacs) (characterp action) (integerp action)))
13154 (if (not (eq action 'set))
13156 (message "Priority %c-%c, SPC to remove: "
13157 org-highest-priority org-lowest-priority)
13159 (setq new (read-char-exclusive))))
13160 (if (and (= (upcase org-highest-priority) org-highest-priority)
13161 (= (upcase org-lowest-priority) org-lowest-priority))
13162 (setq new (upcase new)))
13163 (cond ((equal new ?\ ) (setq remove t))
13164 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
13165 (error "Priority must be between `%c' and `%c'"
13166 org-highest-priority org-lowest-priority))))
13169 (1- current) ; normal cycling
13170 ;; last priority was empty
13171 (if (eq last-command this-command)
13172 org-lowest-priority ; wrap around empty to lowest
13174 (if org-priority-start-cycle-with-default
13175 org-default-priority
13176 (1- org-default-priority))))))
13179 (1+ current) ; normal cycling
13180 ;; last priority was empty
13181 (if (eq last-command this-command)
13182 org-highest-priority ; wrap around empty to highest
13184 (if org-priority-start-cycle-with-default
13185 org-default-priority
13186 (1+ org-default-priority))))))
13187 (t (error "Invalid action")))
13188 (if (or (< (upcase new) org-highest-priority)
13189 (> (upcase new) org-lowest-priority))
13190 (if (and (memq action '(up down))
13191 (not have) (not (eq last-command this-command)))
13192 ;; `new' is from default priority
13194 "The default can not be set, see `org-default-priority' why")
13195 ;; normal cycling: `new' is beyond highest/lowest priority
13196 ;; and is wrapped around to the empty priority
13198 (setq news (format "%c" new))
13201 (replace-match "" t t nil 1)
13202 (replace-match news t t nil 2))
13204 (error "No priority cookie found in line")
13205 (let ((case-fold-search nil))
13206 (looking-at org-todo-line-regexp))
13209 (goto-char (match-end 2))
13210 (insert " [#" news "]"))
13211 (goto-char (match-beginning 3))
13212 (insert "[#" news "] "))))
13213 (org-preserve-lc (org-set-tags nil 'align)))
13215 (message "Priority removed")
13216 (message "Priority of current item set to %s" news)))))
13218 (defun org-show-priority ()
13219 "Show the priority of the current item.
13220 This priority is composed of the main priority given with the [#A] cookies,
13221 and by additional input from the age of a schedules or deadline entry."
13223 (let ((pri (if (eq major-mode 'org-agenda-mode)
13224 (org-get-at-bol 'priority)
13227 (beginning-of-line)
13228 (and (looking-at org-heading-regexp)
13229 (org-get-priority (match-string 0))))))))
13230 (message "Priority is %d" (if pri pri -1000))))
13232 (defun org-get-priority (s)
13233 "Find priority cookie and return priority."
13234 (if (functionp org-get-priority-function)
13235 (funcall org-get-priority-function)
13237 (if (not (string-match org-priority-regexp s))
13238 (* 1000 (- org-lowest-priority org-default-priority))
13239 (* 1000 (- org-lowest-priority
13240 (string-to-char (match-string 2 s))))))))
13244 (defvar org-agenda-archives-mode)
13245 (defvar org-map-continue-from nil
13246 "Position from where mapping should continue.
13247 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
13249 (defvar org-scanner-tags nil
13250 "The current tag list while the tags scanner is running.")
13251 (defvar org-trust-scanner-tags nil
13252 "Should `org-get-tags-at' use the tags for the scanner.
13253 This is for internal dynamical scoping only.
13254 When this is non-nil, the function `org-get-tags-at' will return the value
13255 of `org-scanner-tags' instead of building the list by itself. This
13256 can lead to large speed-ups when the tags scanner is used in a file with
13257 many entries, and when the list of tags is retrieved, for example to
13258 obtain a list of properties. Building the tags list for each entry in such
13259 a file becomes an N^2 operation - but with this variable set, it scales
13262 (defun org-scan-tags (action matcher todo-only &optional start-level)
13263 "Scan headline tags with inheritance and produce output ACTION.
13265 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
13266 or `agenda' to produce an entry list for an agenda view. It can also be
13267 a Lisp form or a function that should be called at each matched headline, in
13268 this case the return value is a list of all return values from these calls.
13270 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
13271 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
13272 only lines with a not-done TODO keyword are included in the output.
13273 This should be the same variable that was scoped into
13274 and set by `org-make-tags-matcher' when it constructed MATCHER.
13276 START-LEVEL can be a string with asterisks, reducing the scope to
13277 headlines matching this string."
13278 (require 'org-agenda)
13279 (let* ((re (concat "^"
13281 ;; Get the correct level to match
13282 (concat "\\*\\{" (number-to-string start-level) "\\} ")
13283 org-outline-regexp)
13285 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
13286 (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
13287 (props (list 'face 'default
13288 'done-face 'org-agenda-done
13289 'undone-face 'default
13290 'mouse-face 'highlight
13291 'org-not-done-regexp org-not-done-regexp
13292 'org-todo-regexp org-todo-regexp
13293 'org-complex-heading-regexp org-complex-heading-regexp
13295 (format "mouse-2 or RET jump to org file %s"
13296 (abbreviate-file-name
13297 (or (buffer-file-name (buffer-base-buffer))
13298 (buffer-name (buffer-base-buffer)))))))
13299 (case-fold-search nil)
13300 (org-map-continue-from nil)
13301 lspos tags tags-list
13302 (tags-alist (list (cons 0 org-file-tags)))
13303 (llast 0) rtn rtn1 level category i txt
13304 todo marker entry priority)
13305 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
13306 (setq action (list 'lambda nil action)))
13308 (goto-char (point-min))
13309 (when (eq action 'sparse-tree)
13311 (org-remove-occur-highlights))
13312 (while (re-search-forward re nil t)
13313 (setq org-map-continue-from nil)
13315 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
13316 tags (if (match-end 4) (org-match-string-no-properties 4)))
13317 (goto-char (setq lspos (match-beginning 0)))
13318 (setq level (org-reduced-level (org-outline-level))
13319 category (org-get-category))
13320 (setq i llast llast level)
13321 ;; remove tag lists from same and sublevels
13322 (while (>= i level)
13323 (when (setq entry (assoc i tags-alist))
13324 (setq tags-alist (delete entry tags-alist)))
13326 ;; add the next tags
13328 (setq tags (org-split-string tags ":")
13330 (cons (cons level tags) tags-alist)))
13331 ;; compile tags for current headline
13333 (if org-use-tag-inheritance
13334 (apply 'append (mapcar 'cdr (reverse tags-alist)))
13336 org-scanner-tags tags-list)
13337 (when org-use-tag-inheritance
13338 (setcdr (car tags-alist)
13339 (mapcar (lambda (x)
13340 (setq x (copy-sequence x))
13341 (org-add-prop-inherited x))
13342 (cdar tags-alist))))
13343 (when (and tags org-use-tag-inheritance
13344 (or (not (eq t org-use-tag-inheritance))
13345 org-tags-exclude-from-inheritance))
13346 ;; selective inheritance, remove uninherited ones
13347 (setcdr (car tags-alist)
13348 (org-remove-uninherited-tags (cdar tags-alist))))
13351 ;; eval matcher only when the todo condition is OK
13352 (and (or (not todo-only) (member todo org-not-done-keywords))
13353 (let ((case-fold-search t) (org-trust-scanner-tags t))
13356 ;; Call the skipper, but return t if it does not skip,
13357 ;; so that the `and' form continues evaluating
13359 (unless (eq action 'sparse-tree) (org-agenda-skip))
13362 ;; Check if timestamps are deselecting this entry
13363 (or (not todo-only)
13364 (and (member todo org-not-done-keywords)
13365 (or (not org-agenda-tags-todo-honor-ignore-options)
13366 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
13368 ;; Extra check for the archive tag
13369 ;; FIXME: Does the skipper already do this????
13371 (not (member org-archive-tag tags-list))
13372 ;; we have an archive tag, should we use this anyway?
13373 (or (not org-agenda-skip-archived-trees)
13374 (and (eq action 'agenda) org-agenda-archives-mode))))
13376 ;; select this headline
13378 ((eq action 'sparse-tree)
13379 (and org-highlight-sparse-tree-matches
13380 (org-get-heading) (match-end 0)
13381 (org-highlight-new-match
13382 (match-beginning 1) (match-end 1)))
13383 (org-show-context 'tags-tree))
13384 ((eq action 'agenda)
13385 (setq txt (org-agenda-format-item
13388 (if (eq org-tags-match-list-sublevels 'indented)
13389 (make-string (1- level) ?.) "")
13393 priority (org-get-priority txt))
13395 (setq marker (org-agenda-new-marker))
13396 (org-add-props txt props
13397 'org-marker marker 'org-hd-marker marker 'org-category category
13399 'priority priority 'type "tagsmatch")
13401 ((functionp action)
13402 (setq org-map-continue-from nil)
13404 (setq rtn1 (funcall action))
13406 (t (error "Invalid action")))
13408 ;; if we are to skip sublevels, jump to end of subtree
13409 (unless org-tags-match-list-sublevels
13410 (org-end-of-subtree t)
13411 (backward-char 1))))
13412 ;; Get the correct position from where to continue
13413 (if org-map-continue-from
13414 (goto-char org-map-continue-from)
13415 (and (= (point) lspos) (end-of-line 1)))))
13416 (when (and (eq action 'sparse-tree)
13417 (not org-sparse-tree-open-archived-trees))
13418 (org-hide-archived-subtrees (point-min) (point-max)))
13421 (defun org-remove-uninherited-tags (tags)
13422 "Remove all tags that are not inherited from the list TAGS."
13424 ((eq org-use-tag-inheritance t)
13425 (if org-tags-exclude-from-inheritance
13426 (org-delete-all org-tags-exclude-from-inheritance tags)
13428 ((not org-use-tag-inheritance) nil)
13429 ((stringp org-use-tag-inheritance)
13432 (if (and (string-match org-use-tag-inheritance x)
13433 (not (member x org-tags-exclude-from-inheritance)))
13436 ((listp org-use-tag-inheritance)
13439 (if (member x org-use-tag-inheritance) x nil))
13442 (defun org-match-sparse-tree (&optional todo-only match)
13443 "Create a sparse tree according to tags string MATCH.
13444 MATCH can contain positive and negative selection of tags, like
13445 \"+WORK+URGENT-WITHBOSS\".
13446 If optional argument TODO-ONLY is non-nil, only select lines that are
13449 (org-agenda-prepare-buffers (list (current-buffer)))
13450 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
13452 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
13454 (defvar org-cached-props nil)
13455 (defun org-cached-entry-get (pom property)
13456 (if (or (eq t org-use-property-inheritance)
13457 (and (stringp org-use-property-inheritance)
13458 (string-match org-use-property-inheritance property))
13459 (and (listp org-use-property-inheritance)
13460 (member property org-use-property-inheritance)))
13461 ;; Caching is not possible, check it directly
13462 (org-entry-get pom property 'inherit)
13463 ;; Get all properties, so that we can do complicated checks easily
13464 (cdr (assoc property (or org-cached-props
13465 (setq org-cached-props
13466 (org-entry-properties pom)))))))
13468 (defun org-global-tags-completion-table (&optional files)
13469 "Return the list of all tags in all agenda buffer/files.
13470 Optional FILES argument is a list of files which can be used
13471 instead of the agenda files."
13478 (set-buffer (find-file-noselect file))
13479 (append (org-get-buffer-tags)
13480 (mapcar (lambda (x) (if (stringp (car-safe x))
13481 (list (car-safe x)) nil))
13483 (if (and files (car files))
13485 (org-agenda-files))))))))
13487 (defun org-make-tags-matcher (match)
13488 "Create the TAGS/TODO matcher form for the selection string MATCH.
13490 The variable `todo-only' is scoped dynamically into this function.
13491 It will be set to t if the matcher restricts matching to TODO entries,
13492 otherwise will not be touched.
13494 Returns a cons of the selection string MATCH and the constructed
13495 lisp form implementing the matcher. The matcher is to be evaluated
13496 at an Org entry, with point on the headline, and returns t if the
13497 entry matches the selection string MATCH. The returned lisp form
13498 references two variables with information about the entry, which
13499 must be bound around the form's evaluation: todo, the TODO keyword
13500 at the entry (or nil of none); and tags-list, the list of all tags
13501 at the entry including inherited ones. Additionally, the category
13502 of the entry (if any) must be specified as the text property
13503 'org-category on the headline.
13505 See also `org-scan-tags'.
13507 (declare (special todo-only))
13508 (unless (boundp 'todo-only)
13509 (error "org-make-tags-matcher expects todo-only to be scoped in"))
13511 ;; Get a new match request, with completion
13512 (let ((org-last-tags-completion-table
13513 (org-global-tags-completion-table)))
13514 (setq match (org-completing-read-no-i
13515 "Match: " 'org-tags-completion-function nil nil nil
13516 'org-tags-history))))
13518 ;; Parse the string and create a lisp form
13519 (let ((match0 match)
13520 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
13522 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
13523 orterms term orlist re-p str-p level-p level-op time-p
13524 prop-p pn pv po gv rest)
13525 (if (string-match "/+" match)
13526 ;; match contains also a todo-matching request
13528 (setq tagsmatch (substring match 0 (match-beginning 0))
13529 todomatch (substring match (match-end 0)))
13530 (if (string-match "^!" todomatch)
13531 (setq todo-only t todomatch (substring todomatch 1)))
13532 (if (string-match "^\\s-*$" todomatch)
13533 (setq todomatch nil)))
13534 ;; only matching tags
13535 (setq tagsmatch match todomatch nil))
13537 ;; Make the tags matcher
13538 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
13539 (setq tagsmatcher t)
13540 (setq orterms (org-split-string tagsmatch "|") orlist nil)
13541 (while (setq term (pop orterms))
13542 (while (and (equal (substring term -1) "\\") orterms)
13543 (setq term (concat term "|" (pop orterms)))) ; repair bad split
13544 (while (string-match re term)
13545 (setq rest (substring term (match-end 0))
13546 minus (and (match-end 1)
13547 (equal (match-string 1 term) "-"))
13548 tag (save-match-data (replace-regexp-in-string
13550 (match-string 2 term)))
13551 re-p (equal (string-to-char tag) ?{)
13552 level-p (match-end 4)
13553 prop-p (match-end 5)
13555 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
13557 (setq level-op (org-op-to-function (match-string 3 term)))
13558 `(,level-op level ,(string-to-number
13559 (match-string 4 term))))
13561 (setq pn (match-string 5 term)
13562 po (match-string 6 term)
13563 pv (match-string 7 term)
13564 re-p (equal (string-to-char pv) ?{)
13565 str-p (equal (string-to-char pv) ?\")
13566 time-p (save-match-data
13567 (string-match "^\"[[<].*[]>]\"$" pv))
13568 pv (if (or re-p str-p) (substring pv 1 -1) pv))
13569 (if time-p (setq pv (org-matcher-time pv)))
13570 (setq po (org-op-to-function po (if time-p 'time str-p)))
13572 ((equal pn "CATEGORY")
13573 (setq gv '(get-text-property (point) 'org-category)))
13577 (setq gv `(org-cached-entry-get nil ,pn))))
13580 `(not (string-match ,pv (or ,gv "")))
13581 `(string-match ,pv (or ,gv "")))
13583 `(,po (or ,gv "") ,pv)
13584 `(,po (string-to-number (or ,gv ""))
13585 ,(string-to-number pv) ))))
13586 (t `(member ,tag tags-list)))
13587 mm (if minus (list 'not mm) mm)
13589 (push mm tagsmatcher))
13590 (push (if (> (length tagsmatcher) 1)
13591 (cons 'and tagsmatcher)
13594 (setq tagsmatcher nil))
13595 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
13597 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
13598 ;; Make the todo matcher
13599 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
13600 (setq todomatcher t)
13601 (setq orterms (org-split-string todomatch "|") orlist nil)
13602 (while (setq term (pop orterms))
13603 (while (string-match re term)
13604 (setq minus (and (match-end 1)
13605 (equal (match-string 1 term) "-"))
13606 kwd (match-string 2 term)
13607 re-p (equal (string-to-char kwd) ?{)
13608 term (substring term (match-end 0))
13610 `(string-match ,(substring kwd 1 -1) todo)
13611 (list 'equal 'todo kwd))
13612 mm (if minus (list 'not mm) mm))
13613 (push mm todomatcher))
13614 (push (if (> (length todomatcher) 1)
13615 (cons 'and todomatcher)
13618 (setq todomatcher nil))
13619 (setq todomatcher (if (> (length orlist) 1)
13620 (cons 'or orlist) (car orlist))))
13622 ;; Return the string and lisp forms of the matcher
13623 (setq matcher (if todomatcher
13624 (list 'and tagsmatcher todomatcher)
13627 (setq matcher (list 'and '(member todo org-not-done-keywords)
13629 (cons match0 matcher)))
13631 (defun org-op-to-function (op &optional stringp)
13632 "Turn an operator into the appropriate function."
13635 ((equal op "<" ) '(< string< org-time<))
13636 ((equal op ">" ) '(> org-string> org-time>))
13637 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
13638 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
13639 ((member op '("=" "==")) '(= string= org-time=))
13640 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
13641 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
13643 (defun org<> (a b) (not (= a b)))
13644 (defun org-string<= (a b) (or (string= a b) (string< a b)))
13645 (defun org-string>= (a b) (not (string< a b)))
13646 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
13647 (defun org-string<> (a b) (not (string= a b)))
13648 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
13649 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
13650 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
13651 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
13652 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
13653 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
13655 "Convert S to a floating point time.
13656 If S is already a number, just return it. If it is a string, parse
13657 it as a time string and apply `float-time' to it. If S is nil, just return 0."
13661 (condition-case nil
13662 (float-time (apply 'encode-time (org-parse-time-string s)))
13666 (defun org-time-today ()
13667 "Time in seconds today at 0:00.
13668 Returns the float number of seconds since the beginning of the
13669 epoch to the beginning of today (00:00)."
13670 (float-time (apply 'encode-time
13671 (append '(0 0 0) (nthcdr 3 (decode-time))))))
13673 (defun org-matcher-time (s)
13674 "Interpret a time comparison value."
13677 ((string= s "<now>") (float-time))
13678 ((string= s "<today>") (org-time-today))
13679 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
13680 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
13681 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
13682 (+ (org-time-today)
13683 (* (string-to-number (match-string 1 s))
13684 (cdr (assoc (match-string 2 s)
13685 '(("d" . 86400.0) ("w" . 604800.0)
13686 ("m" . 2678400.0) ("y" . 31557600.0)))))))
13689 (defun org-match-any-p (re list)
13690 "Does re match any element of list?"
13691 (setq list (mapcar (lambda (x) (string-match re x)) list))
13694 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
13695 (defvar org-tags-overlay (make-overlay 1 1))
13696 (org-detach-overlay org-tags-overlay)
13698 (defun org-get-local-tags-at (&optional pos)
13699 "Get a list of tags defined in the current headline."
13700 (org-get-tags-at pos 'local))
13702 (defun org-get-local-tags ()
13703 "Get a list of tags defined in the current headline."
13704 (org-get-tags-at nil 'local))
13706 (defun org-get-tags-at (&optional pos local)
13707 "Get a list of all headline tags applicable at POS.
13708 POS defaults to point. If tags are inherited, the list contains
13709 the targets in the same sequence as the headlines appear, i.e.
13710 the tags of the current headline come last.
13711 When LOCAL is non-nil, only return tags from the current headline,
13712 ignore inherited ones."
13714 (if (and org-trust-scanner-tags
13715 (or (not pos) (equal pos (point)))
13718 (let (tags ltags lastpos parent)
13722 (goto-char (or pos (point)))
13725 (condition-case nil
13727 (org-back-to-heading t)
13728 (while (not (equal lastpos (point)))
13729 (setq lastpos (point))
13731 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
13732 (setq ltags (org-split-string
13733 (org-match-string-no-properties 1) ":"))
13735 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
13738 (org-remove-uninherited-tags ltags)
13741 (or org-use-tag-inheritance (throw 'done t))
13742 (if local (throw 'done t))
13743 (or (org-up-heading-safe) (error nil))
13748 (append (org-remove-uninherited-tags org-file-tags) tags))))))
13750 (defun org-add-prop-inherited (s)
13751 (add-text-properties 0 (length s) '(inherited t) s)
13754 (defun org-toggle-tag (tag &optional onoff)
13755 "Toggle the tag TAG for the current line.
13756 If ONOFF is `on' or `off', don't toggle but set to this state."
13759 (org-back-to-heading t)
13760 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
13763 (setq current (match-string 1))
13764 (replace-match ""))
13766 (setq current (nreverse (org-split-string current ":")))
13770 (or (member tag current) (push tag current)))
13772 (or (not (member tag current)) (setq current (delete tag current))))
13773 (t (if (member tag current)
13774 (setq current (delete tag current))
13776 (push tag current))))
13780 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
13781 (org-set-tags nil t))
13782 (delete-horizontal-space))
13783 (run-hooks 'org-after-tags-change-hook))
13786 (defun org-align-tags-here (to-col)
13787 ;; Assumes that this is a headline
13788 (let ((pos (point)) (col (current-column)) ncol tags-l p)
13789 (beginning-of-line 1)
13790 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13791 (< pos (match-beginning 2)))
13793 (setq tags-l (- (match-end 2) (match-beginning 2)))
13794 (goto-char (match-beginning 1))
13796 (delete-region (point) (1+ (match-beginning 2)))
13797 (setq ncol (max (current-column)
13801 (- (abs to-col) tags-l))))
13803 (insert (make-string (- ncol (current-column)) ?\ ))
13804 (setq ncol (current-column))
13805 (when indent-tabs-mode (tabify p (point-at-eol)))
13806 (org-move-to-column (min ncol col) t))
13809 (defun org-set-tags-command (&optional arg just-align)
13810 "Call the set-tags command for the current entry."
13812 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
13813 (org-set-tags arg just-align)
13815 (org-back-to-heading t)
13816 (org-set-tags arg just-align))))
13818 (defun org-set-tags-to (data)
13819 "Set the tags of the current entry to DATA, replacing the current tags.
13820 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
13821 If DATA is nil or the empty string, any tags will be removed."
13822 (interactive "sTags: ")
13826 ((equal data "") "")
13828 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
13831 (concat ":" (mapconcat 'identity data ":") ":"))))
13834 (org-back-to-heading t)
13835 (when (looking-at org-complex-heading-regexp)
13838 (goto-char (match-beginning 5))
13840 (delete-region (point) (point-at-eol))
13841 (org-set-tags nil 'align))
13842 (goto-char (point-at-eol))
13844 (org-set-tags nil 'align)))
13845 (beginning-of-line 1)
13846 (if (looking-at ".*?\\([ \t]+\\)$")
13847 (delete-region (match-beginning 1) (match-end 1))))))
13849 (defun org-align-all-tags ()
13850 "Align the tags i all headings."
13853 (or (ignore-errors (org-back-to-heading t))
13854 (outline-next-heading))
13855 (if (org-at-heading-p)
13857 (message "No headings"))))
13859 (defvar org-indent-indentation-per-level)
13860 (defun org-set-tags (&optional arg just-align)
13861 "Set the tags for the current headline.
13862 With prefix ARG, realign all tags in headings in the current buffer."
13864 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13865 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
13866 'region-start-level 'region))
13867 org-loop-over-headlines-in-active-region)
13869 ;; We don't use ARG and JUST-ALIGN here these args are not
13870 ;; useful when looping over headlines
13872 org-loop-over-headlines-in-active-region
13873 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
13874 (let* ((re org-outline-regexp-bol)
13875 (current (unless arg (org-get-tags-string)))
13876 (col (current-column))
13877 (org-setting-tags t)
13878 table current-tags inherited-tags ; computed below when needed
13879 tags p0 c0 c1 rpl di tc level)
13882 (goto-char (point-min))
13883 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
13884 (while (re-search-forward re nil t)
13885 (org-set-tags nil t)
13887 (message "All tags realigned to column %d" org-tags-column))
13889 (setq tags current)
13890 ;; Get a new set of tags from the user
13892 (setq table (append org-tag-persistent-alist
13893 (or org-tag-alist (org-get-buffer-tags))
13895 org-complete-tags-always-offer-all-agenda-tags
13896 (org-global-tags-completion-table
13897 (org-agenda-files))))
13898 org-last-tags-completion-table table
13899 current-tags (org-split-string current ":")
13900 inherited-tags (nreverse
13901 (nthcdr (length current-tags)
13902 (nreverse (org-get-tags-at))))
13904 (if (or (eq t org-use-fast-tag-selection)
13905 (and org-use-fast-tag-selection
13906 (delq nil (mapcar 'cdr table))))
13907 (org-fast-tag-selection
13908 current-tags inherited-tags table
13909 (if org-fast-tag-selection-include-todo
13910 org-todo-key-alist))
13911 (let ((org-add-colon-after-tag-completion (< 1 (length table))))
13913 (org-icompleting-read "Tags: "
13914 'org-tags-completion-function
13915 nil nil current 'org-tags-history))))))
13916 (while (string-match "[-+&]+" tags)
13917 ;; No boolean logic, just a list
13918 (setq tags (replace-match ":" t t tags))))
13920 (setq tags (replace-regexp-in-string "[,]" ":" tags))
13922 (if org-tags-sort-function
13923 (setq tags (mapconcat 'identity
13924 (sort (org-split-string
13925 tags (org-re "[^[:alnum:]_@#%]+"))
13926 org-tags-sort-function) ":")))
13928 (if (string-match "\\`[\t ]*\\'" tags)
13930 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
13931 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
13933 ;; Insert new tags at the correct column
13934 (beginning-of-line 1)
13935 (setq level (or (and (looking-at org-outline-regexp)
13936 (- (match-end 0) (point) 1))
13939 ((and (equal current "") (equal tags "")))
13940 ((re-search-forward
13941 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
13943 (if (equal tags "")
13945 (goto-char (match-beginning 0))
13946 (setq c0 (current-column)
13947 ;; compute offset for the case of org-indent-mode active
13948 di (if org-indent-mode
13949 (* (1- org-indent-indentation-per-level) (1- level))
13951 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
13952 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
13953 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
13954 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
13955 (replace-match rpl t t)
13956 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
13958 (t (error "Tags alignment failed")))
13959 (org-move-to-column col)
13961 (run-hooks 'org-after-tags-change-hook))))))
13963 (defun org-change-tag-in-region (beg end tag off)
13964 "Add or remove TAG for each entry in the region.
13965 This works in the agenda, and also in an org-mode buffer."
13967 (list (region-beginning) (region-end)
13968 (let ((org-last-tags-completion-table
13969 (if (derived-mode-p 'org-mode)
13970 (org-get-buffer-tags)
13971 (org-global-tags-completion-table))))
13972 (org-icompleting-read
13973 "Tag: " 'org-tags-completion-function nil nil nil
13974 'org-tags-history))
13976 (message "[s]et or
[r]emove? ")
13977 (equal (read-char-exclusive) ?r))))
13978 (if (fboundp 'deactivate-mark) (deactivate-mark))
13979 (let ((agendap (equal major-mode 'org-agenda-mode))
13980 l1 l2 m buf pos newhead (cnt 0))
13982 (setq l2 (1- (org-current-line)))
13984 (setq l1 (org-current-line))
13985 (loop for l from l1 to l2 do
13987 (setq m (get-text-property (point) 'org-hd-marker))
13988 (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
13990 (setq buf (if agendap (marker-buffer m) (current-buffer))
13991 pos (if agendap m (point)))
13992 (with-current-buffer buf
13996 (setq cnt (1+ cnt))
13997 (org-toggle-tag tag (if off 'off 'on))
13998 (setq newhead (org-get-heading)))))
13999 (and agendap (org-agenda-change-all-lines newhead m))))
14000 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
14002 (defun org-tags-completion-function (string predicate &optional flag)
14003 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
14004 (confirm (lambda (x) (stringp (car x)))))
14005 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
14006 (setq s1 (match-string 1 string)
14007 s2 (match-string 2 string))
14008 (setq s1 "" s2 string))
14012 (setq rtn (try-completion s2 ctable confirm))
14015 (concat s1 s2 (substring rtn (length s2))
14016 (if (and org-add-colon-after-tag-completion
14017 (assoc rtn ctable))
14022 (all-completions s2 ctable confirm)
14026 (assoc s2 ctable)))
14029 (defun org-fast-tag-insert (kwd tags face &optional end)
14030 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
14031 (insert (format "%-12s" (concat kwd ":"))
14032 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
14035 (defun org-fast-tag-show-exit (flag)
14038 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
14039 (replace-match ""))
14042 (org-move-to-column (- (window-width) 19) t)
14043 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
14045 (defun org-set-current-tags-overlay (current prefix)
14046 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
14047 (if (featurep 'xemacs)
14048 (org-overlay-display org-tags-overlay (concat prefix s)
14049 'secondary-selection)
14050 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
14051 (org-overlay-display org-tags-overlay (concat prefix s)))))
14053 (defvar org-last-tag-selection-key nil)
14054 (defun org-fast-tag-selection (current inherited table &optional todo-table)
14055 "Fast tag selection with single keys.
14056 CURRENT is the current list of tags in the headline, INHERITED is the
14057 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
14058 possibly with grouping information. TODO-TABLE is a similar table with
14059 TODO keywords, should these have keys assigned to them.
14060 If the keys are nil, a-z are automatically assigned.
14061 Returns the new tags string, or nil to not change the current settings."
14062 (let* ((fulltable (append table todo-table))
14063 (maxlen (apply 'max (mapcar
14065 (if (stringp (car x)) (string-width (car x)) 0))
14067 (buf (current-buffer))
14068 (expert (eq org-fast-tag-selection-single-key 'expert))
14070 (fwidth (+ maxlen 3 1 3))
14071 (ncol (/ (- (window-width) 4) fwidth))
14074 tg cnt e c char c1 c2 ntable tbl rtn
14075 ov-start ov-end ov-prefix
14076 (exit-after-next org-fast-tag-selection-single-key)
14077 (done-keywords org-done-keywords)
14080 (beginning-of-line 1)
14082 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14083 (setq ov-start (match-beginning 1)
14084 ov-end (match-end 1)
14086 (setq ov-start (1- (point-at-eol))
14087 ov-end (1+ ov-start))
14088 (skip-chars-forward "^\n\r")
14091 (buffer-substring (1- (point)) (point))
14092 (if (> (current-column) org-tags-column)
14094 (make-string (- org-tags-column (current-column)) ?\ ))))))
14095 (move-overlay org-tags-overlay ov-start ov-end)
14096 (save-window-excursion
14098 (set-buffer (get-buffer-create " *Org tags*"))
14099 (delete-other-windows)
14100 (split-window-vertically)
14101 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
14103 (org-set-local 'org-done-keywords done-keywords)
14104 (org-fast-tag-insert "Inherited" inherited i-face "\n")
14105 (org-fast-tag-insert "Current" current c-face "\n\n")
14106 (org-fast-tag-show-exit exit-after-next)
14107 (org-set-current-tags-overlay current ov-prefix)
14108 (setq tbl fulltable char ?a cnt 0)
14109 (while (setq e (pop tbl))
14111 ((equal (car e) :startgroup)
14112 (push '() groups) (setq ingroup t)
14113 (when (not (= cnt 0))
14116 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
14117 ((equal (car e) :endgroup)
14118 (setq ingroup nil cnt 0)
14119 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
14120 ((equal e '(:newline))
14121 (when (not (= cnt 0))
14125 (while (equal (car tbl) '(:newline))
14127 (setq tbl (cdr tbl)))))
14129 (setq tg (copy-sequence (car e)) c2 nil)
14132 ;; automatically assign a character.
14133 (setq c1 (string-to-char
14134 (downcase (substring
14135 tg (if (= (string-to-char tg) ?@) 1 0)))))
14136 (if (or (rassoc c1 ntable) (rassoc c1 table))
14137 (while (or (rassoc char ntable) (rassoc char table))
14138 (setq char (1+ char)))
14140 (setq c (or c2 char)))
14141 (if ingroup (push tg (car groups)))
14142 (setq tg (org-add-props tg nil 'face
14144 ((not (assoc tg table))
14145 (org-get-todo-face tg))
14146 ((member tg current) c-face)
14147 ((member tg inherited) i-face))))
14148 (if (and (= cnt 0) (not ingroup)) (insert " "))
14149 (insert "[" c "] " tg (make-string
14150 (- fwidth 4 (length tg)) ?\ ))
14151 (push (cons tg c) ntable)
14152 (when (= (setq cnt (1+ cnt)) ncol)
14154 (if ingroup (insert " "))
14156 (setq ntable (nreverse ntable))
14158 (goto-char (point-min))
14159 (if (not expert) (org-fit-window-to-buffer))
14163 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
14164 (if (not groups) "no " "")
14165 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
14166 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
14167 (setq org-last-tag-selection-key c)
14169 ((= c ?\r) (throw 'exit t))
14171 (setq groups (not groups))
14172 (goto-char (point-min))
14173 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
14176 (org-fast-tag-show-exit
14177 (setq exit-after-next (not exit-after-next)))
14179 (delete-other-windows)
14180 (set-window-buffer (split-window-vertically) " *Org tags*")
14181 (org-switch-to-buffer-other-window " *Org tags*")
14182 (org-fit-window-to-buffer)))
14184 (and (= c ?q) (not (rassoc c ntable))))
14185 (org-detach-overlay org-tags-overlay)
14186 (setq quit-flag t))
14189 (if exit-after-next (setq exit-after-next 'now)))
14191 (condition-case nil
14192 (setq tg (org-icompleting-read
14195 (with-current-buffer buf
14196 (org-get-buffer-tags)))))
14197 (quit (setq tg "")))
14198 (when (string-match "\\S-" tg)
14199 (add-to-list 'buffer-tags (list tg))
14200 (if (member tg current)
14201 (setq current (delete tg current))
14202 (push tg current)))
14203 (if exit-after-next (setq exit-after-next 'now)))
14204 ((setq e (rassoc c todo-table) tg (car e))
14205 (with-current-buffer buf
14206 (save-excursion (org-todo tg)))
14207 (if exit-after-next (setq exit-after-next 'now)))
14208 ((setq e (rassoc c ntable) tg (car e))
14209 (if (member tg current)
14210 (setq current (delete tg current))
14211 (loop for g in groups do
14214 (setq current (delete x current)))
14217 (if exit-after-next (setq exit-after-next 'now))))
14219 ;; Create a sorted list
14223 (assoc b (cdr (memq (assoc a ntable) ntable))))))
14224 (if (eq exit-after-next 'now) (throw 'exit t))
14225 (goto-char (point-min))
14226 (beginning-of-line 2)
14227 (delete-region (point) (point-at-eol))
14228 (org-fast-tag-insert "Current" current c-face)
14229 (org-set-current-tags-overlay current ov-prefix)
14230 (while (re-search-forward
14231 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
14232 (setq tg (match-string 1))
14233 (add-text-properties
14234 (match-beginning 1) (match-end 1)
14237 ((member tg current) c-face)
14238 ((member tg inherited) i-face)
14239 (t (get-text-property (match-beginning 1) 'face))))))
14240 (goto-char (point-min)))))
14241 (org-detach-overlay org-tags-overlay)
14243 (mapconcat 'identity current ":")
14246 (defun org-get-tags-string ()
14247 "Get the TAGS string in the current headline."
14248 (unless (org-at-heading-p t)
14249 (error "Not on a heading"))
14251 (beginning-of-line 1)
14252 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14253 (org-match-string-no-properties 1)
14256 (defun org-get-tags ()
14257 "Get the list of tags specified in the current headline."
14258 (org-split-string (org-get-tags-string) ":"))
14260 (defun org-get-buffer-tags ()
14261 "Get a table of all tags used in the buffer, for completion."
14264 (goto-char (point-min))
14265 (while (re-search-forward
14266 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
14267 (when (equal (char-after (point-at-bol 0)) ?*)
14268 (mapc (lambda (x) (add-to-list 'tags x))
14269 (org-split-string (org-match-string-no-properties 1) ":")))))
14270 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
14271 (mapcar 'list tags)))
14273 ;;;; The mapping API
14275 (defun org-map-entries (func &optional match scope &rest skip)
14276 "Call FUNC at each headline selected by MATCH in SCOPE.
14278 FUNC is a function or a lisp form. The function will be called without
14279 arguments, with the cursor positioned at the beginning of the headline.
14280 The return values of all calls to the function will be collected and
14281 returned as a list.
14283 The call to FUNC will be wrapped into a save-excursion form, so FUNC
14284 does not need to preserve point. After evaluation, the cursor will be
14285 moved to the end of the line (presumably of the headline of the
14286 processed entry) and search continues from there. Under some
14287 circumstances, this may not produce the wanted results. For example,
14288 if you have removed (e.g. archived) the current (sub)tree it could
14289 mean that the next entry will be skipped entirely. In such cases, you
14290 can specify the position from where search should continue by making
14291 FUNC set the variable `org-map-continue-from' to the desired buffer
14294 MATCH is a tags/property/todo match as it is used in the agenda tags view.
14295 Only headlines that are matched by this query will be considered during
14296 the iteration. When MATCH is nil or t, all headlines will be
14297 visited by the iteration.
14299 SCOPE determines the scope of this command. It can be any of:
14301 nil The current buffer, respecting the restriction if any
14302 tree The subtree started with the entry at point
14303 region The entries within the active region, if any
14305 The entries within the active region, but only those at
14306 the same level than the first one.
14307 file The current buffer, without restriction
14309 The current buffer, and any archives associated with it
14310 agenda All agenda files
14311 agenda-with-archives
14312 All agenda files with any archive files associated with them
14314 If this is a list, all files in the list will be scanned
14316 The remaining args are treated as settings for the skipping facilities of
14317 the scanner. The following items can be given here:
14319 archive skip trees with the archive tag.
14320 comment skip trees with the COMMENT keyword
14321 function or Emacs Lisp form:
14322 will be used as value for `org-agenda-skip-function', so whenever
14323 the function returns t, FUNC will not be called for that
14324 entry and search will continue from the point where the
14325 function leaves it.
14327 If your function needs to retrieve the tags including inherited tags
14328 at the *current* entry, you can use the value of the variable
14329 `org-scanner-tags' which will be much faster than getting the value
14330 with `org-get-tags-at'. If your function gets properties with
14331 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
14332 to t around the call to `org-entry-properties' to get the same speedup.
14333 Note that if your function moves around to retrieve tags and properties at
14334 a *different* entry, you cannot use these techniques."
14335 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
14336 (not (org-region-active-p)))
14337 (let* ((org-agenda-archives-mode nil) ; just to make sure
14338 (org-agenda-skip-archived-trees (memq 'archive skip))
14339 (org-agenda-skip-comment-trees (memq 'comment skip))
14340 (org-agenda-skip-function
14341 (car (org-delete-all '(comment archive) skip)))
14342 (org-tags-match-list-sublevels t)
14343 (start-level (eq scope 'region-start-level))
14345 org-todo-keywords-for-agenda
14346 org-done-keywords-for-agenda
14347 org-todo-keyword-alist-for-agenda
14348 org-drawers-for-agenda
14349 org-tag-alist-for-agenda
14353 ((eq match t) (setq matcher t))
14354 ((eq match nil) (setq matcher t))
14355 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
14359 (cond ((eq scope 'tree)
14360 (org-back-to-heading t)
14361 (org-narrow-to-subtree)
14363 ((and (or (eq scope 'region) (eq scope 'region-start-level))
14364 (org-region-active-p))
14365 ;; If needed, set start-level to a string like "2"
14368 (goto-char (region-beginning))
14369 (unless (org-at-heading-p) (outline-next-heading))
14370 (setq start-level (org-current-level))))
14371 (narrow-to-region (region-beginning)
14373 (goto-char (region-end))
14374 (unless (and (bolp) (org-at-heading-p))
14375 (outline-next-heading))
14381 (org-agenda-prepare-buffers
14382 (list (buffer-file-name (current-buffer))))
14383 (setq res (org-scan-tags func matcher todo-only start-level)))
14384 ;; Get the right scope
14386 ((and scope (listp scope) (symbolp (car scope)))
14387 (setq scope (eval scope)))
14388 ((eq scope 'agenda)
14389 (setq scope (org-agenda-files t)))
14390 ((eq scope 'agenda-with-archives)
14391 (setq scope (org-agenda-files t))
14392 (setq scope (org-add-archive-files scope)))
14394 (setq scope (list (buffer-file-name))))
14395 ((eq scope 'file-with-archives)
14396 (setq scope (org-add-archive-files (list (buffer-file-name))))))
14397 (org-agenda-prepare-buffers scope)
14398 (while (setq file (pop scope))
14399 (with-current-buffer (org-find-base-buffer-visiting file)
14403 (goto-char (point-min))
14404 (setq res (append res (org-scan-tags func matcher todo-only))))))))))
14409 ;;; Setting and retrieving properties
14411 (defconst org-special-properties
14412 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
14413 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
14414 "The special properties valid in Org-mode.
14416 These are properties that are not defined in the property drawer,
14417 but in some other way.")
14419 (defconst org-default-properties
14420 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
14421 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
14422 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
14423 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
14424 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
14425 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
14426 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
14427 "Some properties that are used by Org-mode for various purposes.
14428 Being in this list makes sure that they are offered for completion.")
14430 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
14431 "Regular expression matching the first line of a property drawer.")
14433 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
14434 "Regular expression matching the last line of a property drawer.")
14436 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
14437 "Regular expression matching the first line of a property drawer.")
14439 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
14440 "Regular expression matching the first line of a property drawer.")
14442 (defconst org-property-drawer-re
14443 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
14444 org-property-end-re "\\)\n?")
14445 "Matches an entire property drawer.")
14447 (defconst org-clock-drawer-re
14448 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
14449 org-property-end-re "\\)\n?")
14450 "Matches an entire clock drawer.")
14452 (defsubst org-re-property (property)
14453 "Return a regexp matching a PROPERTY line.
14454 Match group 1 will be set to the value."
14455 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
14457 (defsubst org-re-property-keyword (property)
14458 "Return a regexp matching a PROPERTY line, possibly with no
14459 value for the property."
14460 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
14462 (defun org-property-action ()
14463 "Do an action on properties."
14466 (org-at-property-p)
14467 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
14468 (setq c (read-char-exclusive))
14471 (call-interactively 'org-set-property))
14473 (call-interactively 'org-delete-property))
14475 (call-interactively 'org-delete-property-globally))
14477 (call-interactively 'org-compute-property-at-point))
14478 (t (error "No such property action %c" c)))))
14480 (defun org-inc-effort ()
14481 "Increment the value of the effort property in the current entry."
14483 (org-set-effort nil t))
14485 (defun org-set-effort (&optional value increment)
14486 "Set the effort property of the current entry.
14487 With numerical prefix arg, use the nth allowed value, 0 stands for the
14488 10th allowed value.
14490 When INCREMENT is non-nil, set the property to the next allowed value."
14492 (if (equal value 0) (setq value 10))
14493 (let* ((completion-ignore-case t)
14494 (prop org-effort-property)
14495 (cur (org-entry-get nil prop))
14496 (allowed (org-property-get-allowed-values nil prop 'table))
14497 (existing (mapcar 'list (org-property-values prop)))
14500 ((stringp value) value)
14501 ((and allowed (integerp value))
14502 (or (car (nth (1- value) allowed))
14503 (car (org-last allowed))))
14504 ((and allowed increment)
14505 (or (caadr (member (list cur) allowed))
14506 (error "Allowed effort values are not set")))
14508 (message "Select 1-9,0, [RET%s]: %s"
14509 (if cur (concat "=" cur) "")
14510 (mapconcat 'car allowed " "))
14511 (setq rpl (read-char-exclusive))
14512 (if (equal rpl ?\r)
14514 (setq rpl (- rpl ?0))
14515 (if (equal rpl 0) (setq rpl 10))
14516 (if (and (> rpl 0) (<= rpl (length allowed)))
14517 (car (nth (1- rpl) allowed))
14518 (org-completing-read "Effort: " allowed nil))))
14520 (let (org-completion-use-ido org-completion-use-iswitchb)
14521 (org-completing-read
14522 (concat "Effort " (if (and cur (string-match "\\S-" cur))
14523 (concat "[" cur "]") "")
14525 existing nil nil "" nil cur))))))
14526 (unless (equal (org-entry-get nil prop) val)
14527 (org-entry-put nil prop val))
14528 (message "%s is now %s" prop val)))
14530 (defun org-at-property-p ()
14531 "Is cursor inside a property drawer?"
14533 (beginning-of-line 1)
14534 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
14535 (save-match-data ;; Used by calling procedures
14537 (range (unless (org-before-first-heading-p)
14538 (org-get-property-block))))
14539 (and range (<= (car range) p) (< p (cdr range))))))))
14541 (defun org-get-property-block (&optional beg end force)
14542 "Return the (beg . end) range of the body of the property drawer.
14543 BEG and END are the beginning and end of the current subtree, or of
14544 the part before the first headline. If they are not given, they will
14545 be found. If the drawer does not exist and FORCE is non-nil, create
14549 (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
14550 (progn (org-back-to-heading t) (point))))
14551 (end (or end (and (not (outline-next-heading)) (point-max))
14554 (if (re-search-forward org-property-start-re end t)
14555 (setq beg (1+ (match-end 0)))
14558 (org-insert-property-drawer)
14559 (setq end (progn (outline-next-heading) (point))))
14562 (if (re-search-forward org-property-start-re end t)
14563 (setq beg (1+ (match-end 0)))))
14564 (if (re-search-forward org-property-end-re end t)
14565 (setq end (match-beginning 0))
14566 (or force (throw 'exit nil))
14570 (insert ":END:\n"))
14573 (defun org-entry-properties (&optional pom which specific)
14574 "Get all properties of the entry at point-or-marker POM.
14575 This includes the TODO keyword, the tags, time strings for deadline,
14576 scheduled, and clocking, and any additional properties defined in the
14577 entry. The return value is an alist, keys may occur multiple times
14578 if the property key was used several times.
14579 POM may also be nil, in which case the current entry is used.
14580 If WHICH is nil or `all', get all properties. If WHICH is
14581 `special' or `standard', only get that subclass. If WHICH
14582 is a string only get exactly this property. SPECIFIC can be a string, the
14583 specific property we are interested in. Specifying it can speed
14584 things up because then unnecessary parsing is avoided."
14585 (setq which (or which 'all))
14586 (org-with-point-at pom
14587 (let ((clockstr (substring org-clock-string 0 -1))
14588 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
14589 (case-fold-search nil)
14590 beg end range props sum-props key key1 value string clocksum clocksumt)
14592 (when (condition-case nil
14593 (and (derived-mode-p 'org-mode) (org-back-to-heading t))
14596 (setq sum-props (get-text-property (point) 'org-summaries))
14597 (setq clocksum (get-text-property (point) :org-clock-minutes)
14598 clocksumt (get-text-property (point) :org-clock-minutes-today))
14599 (outline-next-heading)
14601 (when (memq which '(all special))
14602 ;; Get the special properties, like TODO and tags
14604 (when (and (or (not specific) (string= specific "TODO"))
14605 (looking-at org-todo-line-regexp) (match-end 2))
14606 (push (cons "TODO" (org-match-string-no-properties 2)) props))
14607 (when (and (or (not specific) (string= specific "PRIORITY"))
14608 (looking-at org-priority-regexp))
14609 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
14610 (when (or (not specific) (string= specific "FILE"))
14611 (push (cons "FILE" buffer-file-name) props))
14612 (when (and (or (not specific) (string= specific "TAGS"))
14613 (setq value (org-get-tags-string))
14614 (string-match "\\S-" value))
14615 (push (cons "TAGS" value) props))
14616 (when (and (or (not specific) (string= specific "ALLTAGS"))
14617 (setq value (org-get-tags-at)))
14618 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
14621 (when (or (not specific) (string= specific "BLOCKED"))
14622 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
14623 (when (or (not specific)
14625 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
14626 "TIMESTAMP" "TIMESTAMP_IA")))
14628 (while (re-search-forward org-maybe-keyword-time-regexp end t)
14629 (setq key (if (match-end 1)
14630 (substring (org-match-string-no-properties 1)
14632 string (if (equal key clockstr)
14634 (buffer-substring-no-properties
14635 (match-beginning 3) (goto-char
14637 (substring (org-match-string-no-properties 3)
14639 ;; Get the correct property name from the key. This is
14640 ;; necessary if the user has configured time keywords.
14641 (setq key1 (concat key ":"))
14645 (if (= (char-after (match-beginning 3)) ?\[)
14646 "TIMESTAMP_IA" "TIMESTAMP")))
14647 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
14648 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
14649 ((equal key1 org-closed-string) (setq key "CLOSED"))
14650 ((equal key1 org-clock-string) (setq key "CLOCK")))
14651 (if (and specific (equal key specific) (not (equal key "CLOCK")))
14653 (push (cons key string) props)
14654 ;; no need to search further if match is found
14656 (when (or (equal key "CLOCK") (not (assoc key props)))
14657 (push (cons key string) props)))))))
14659 (when (memq which '(all standard))
14660 ;; Get the standard properties, like :PROP: ...
14661 (setq range (org-get-property-block beg end))
14663 (goto-char (car range))
14664 (while (re-search-forward
14665 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
14667 (setq key (org-match-string-no-properties 1)
14668 value (org-trim (or (org-match-string-no-properties 2) "")))
14669 (unless (member key excluded)
14670 (push (cons key (or value "")) props)))))
14672 (push (cons "CLOCKSUM"
14673 (org-columns-number-to-string (/ (float clocksum) 60.)
14677 (push (cons "CLOCKSUM_T"
14678 (org-columns-number-to-string (/ (float clocksumt) 60.)
14681 (unless (assoc "CATEGORY" props)
14682 (push (cons "CATEGORY" (org-get-category)) props))
14683 (append sum-props (nreverse props)))))))
14685 (defun org-entry-get (pom property &optional inherit literal-nil)
14686 "Get value of PROPERTY for entry or content at point-or-marker POM.
14687 If INHERIT is non-nil and the entry does not have the property,
14688 then also check higher levels of the hierarchy.
14689 If INHERIT is the symbol `selective', use inheritance only if the setting
14690 in `org-use-property-inheritance' selects PROPERTY for inheritance.
14691 If the property is present but empty, the return value is the empty string.
14692 If the property is not present at all, nil is returned.
14694 If LITERAL-NIL is set, return the string value \"nil\" as a string,
14695 do not interpret it as the list atom nil. This is used for inheritance
14696 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
14697 (org-with-point-at pom
14698 (if (and inherit (if (eq inherit 'selective)
14699 (org-property-inherit-p property)
14701 (org-entry-get-with-inheritance property literal-nil)
14702 (if (member property org-special-properties)
14703 ;; We need a special property. Use `org-entry-properties' to
14704 ;; retrieve it, but specify the wanted property
14705 (cdr (assoc property (org-entry-properties nil 'special property)))
14706 (let* ((range (org-get-property-block))
14707 (props (list (or (assoc property org-file-properties)
14708 (assoc property org-global-properties)
14709 (assoc property org-global-properties-fixed))))
14711 (when (re-search-forward
14712 (org-re-property key) (cdr range) t)
14714 (org-update-property-plist
14717 (org-match-string-no-properties 1) "")
14720 (when (and range (goto-char (car range)))
14721 (funcall ap property)
14722 (goto-char (car range))
14723 (while (funcall ap (concat property "+")))
14724 (setq val (cdr (assoc property props)))
14725 (when val (if literal-nil val (org-not-nil val)))))))))
14727 (defun org-property-or-variable-value (var &optional inherit)
14728 "Check if there is a property fixing the value of VAR.
14729 If yes, return this value. If not, return the current value of the variable."
14730 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14731 (if (and prop (stringp prop) (string-match "\\S-" prop))
14733 (symbol-value var))))
14735 (defun org-entry-delete (pom property)
14736 "Delete the property PROPERTY from entry at point-or-marker POM."
14737 (org-with-point-at pom
14738 (if (member property org-special-properties)
14739 nil ; cannot delete these properties.
14740 (let ((range (org-get-property-block)))
14742 (goto-char (car range))
14744 (org-re-property property)
14747 (delete-region (match-beginning 0) (1+ (point-at-eol)))
14751 ;; Multi-values properties are properties that contain multiple values
14752 ;; These values are assumed to be single words, separated by whitespace.
14753 (defun org-entry-add-to-multivalued-property (pom property value)
14754 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
14755 (let* ((old (org-entry-get pom property))
14756 (values (and old (org-split-string old "[ \t]"))))
14757 (setq value (org-entry-protect-space value))
14758 (unless (member value values)
14759 (setq values (cons value values))
14760 (org-entry-put pom property
14761 (mapconcat 'identity values " ")))))
14763 (defun org-entry-remove-from-multivalued-property (pom property value)
14764 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
14765 (let* ((old (org-entry-get pom property))
14766 (values (and old (org-split-string old "[ \t]"))))
14767 (setq value (org-entry-protect-space value))
14768 (when (member value values)
14769 (setq values (delete value values))
14770 (org-entry-put pom property
14771 (mapconcat 'identity values " ")))))
14773 (defun org-entry-member-in-multivalued-property (pom property value)
14774 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
14775 (let* ((old (org-entry-get pom property))
14776 (values (and old (org-split-string old "[ \t]"))))
14777 (setq value (org-entry-protect-space value))
14778 (member value values)))
14780 (defun org-entry-get-multivalued-property (pom property)
14781 "Return a list of values in a multivalued property."
14782 (let* ((value (org-entry-get pom property))
14783 (values (and value (org-split-string value "[ \t]"))))
14784 (mapcar 'org-entry-restore-space values)))
14786 (defun org-entry-put-multivalued-property (pom property &rest values)
14787 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
14788 VALUES should be a list of strings. Spaces will be protected."
14789 (org-entry-put pom property
14790 (mapconcat 'org-entry-protect-space values " "))
14791 (let* ((value (org-entry-get pom property))
14792 (values (and value (org-split-string value "[ \t]"))))
14793 (mapcar 'org-entry-restore-space values)))
14795 (defun org-entry-protect-space (s)
14796 "Protect spaces and newline in string S."
14797 (while (string-match " " s)
14798 (setq s (replace-match "%20" t t s)))
14799 (while (string-match "\n" s)
14800 (setq s (replace-match "%0A" t t s)))
14803 (defun org-entry-restore-space (s)
14804 "Restore spaces and newline in string S."
14805 (while (string-match "%20" s)
14806 (setq s (replace-match " " t t s)))
14807 (while (string-match "%0A" s)
14808 (setq s (replace-match "\n" t t s)))
14811 (defvar org-entry-property-inherited-from (make-marker)
14812 "Marker pointing to the entry from where a property was inherited.
14813 Each call to `org-entry-get-with-inheritance' will set this marker to the
14814 location of the entry where the inheritance search matched. If there was
14815 no match, the marker will point nowhere.
14816 Note that also `org-entry-get' calls this function, if the INHERIT flag
14819 (defun org-entry-get-with-inheritance (property &optional literal-nil)
14820 "Get PROPERTY of entry or content at point, search higher levels if needed.
14821 The search will stop at the first ancestor which has the property defined.
14822 If the value found is \"nil\", return nil to show that the property
14823 should be considered as undefined (this is the meaning of nil here).
14824 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
14825 (move-marker org-entry-property-inherited-from nil)
14832 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
14833 (or (ignore-errors (org-back-to-heading t))
14834 (goto-char (point-min)))
14835 (move-marker org-entry-property-inherited-from (point))
14837 (or (ignore-errors (org-up-heading-safe))
14838 (throw 'ex nil))))))
14840 (cdr (assoc property org-file-properties))
14841 (cdr (assoc property org-global-properties))
14842 (cdr (assoc property org-global-properties-fixed))))
14843 (if literal-nil tmp (org-not-nil tmp))))
14845 (defvar org-property-changed-functions nil
14846 "Hook called when the value of a property has changed.
14847 Each hook function should accept two arguments, the name of the property
14848 and the new value.")
14850 (defun org-entry-put (pom property value)
14851 "Set PROPERTY to VALUE for entry at point-or-marker POM."
14852 (org-with-point-at pom
14853 (org-back-to-heading t)
14854 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
14857 ((equal property "TODO")
14858 (when (and (stringp value) (string-match "\\S-" value)
14859 (not (member value org-todo-keywords-1)))
14860 (error "\"%s\" is not a valid TODO state" value))
14861 (if (or (not value)
14862 (not (string-match "\\S-" value)))
14863 (setq value 'none))
14865 (org-set-tags nil 'align))
14866 ((equal property "PRIORITY")
14867 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
14868 (string-to-char value) ?\ ))
14869 (org-set-tags nil 'align))
14870 ((equal property "CLOCKSUM")
14871 (if (not (re-search-forward
14872 (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
14873 (error "Cannot find a clock log")
14874 (goto-char (- (match-end 1) 2))
14876 ((eq value 'earlier) (org-timestamp-down))
14877 ((eq value 'later) (org-timestamp-up)))
14878 (org-clock-sum-current-item)))
14879 ((equal property "SCHEDULED")
14880 (if (re-search-forward org-scheduled-time-regexp end t)
14882 ((eq value 'earlier) (org-timestamp-change -1 'day))
14883 ((eq value 'later) (org-timestamp-change 1 'day))
14884 (t (call-interactively 'org-schedule)))
14885 (call-interactively 'org-schedule)))
14886 ((equal property "DEADLINE")
14887 (if (re-search-forward org-deadline-time-regexp end t)
14889 ((eq value 'earlier) (org-timestamp-change -1 'day))
14890 ((eq value 'later) (org-timestamp-change 1 'day))
14891 (t (call-interactively 'org-deadline)))
14892 (call-interactively 'org-deadline)))
14893 ((member property org-special-properties)
14894 (error "The %s property can not yet be set with `org-entry-put'"
14896 (t ; a non-special property
14897 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
14898 (setq range (org-get-property-block beg end 'force))
14899 (goto-char (car range))
14900 (if (re-search-forward
14901 (org-re-property-keyword property) (cdr range) t)
14903 (delete-region (match-beginning 0) (match-end 0))
14904 (goto-char (match-beginning 0)))
14905 (goto-char (cdr range))
14909 (insert ":" property ":")
14910 (and value (insert " " value))
14911 (org-indent-line)))))
14912 (run-hook-with-args 'org-property-changed-functions property value)))
14914 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
14915 "Get all property keys in the current buffer.
14916 With INCLUDE-SPECIALS, also list the special properties that reflect things
14917 like tags and TODO state.
14918 With INCLUDE-DEFAULTS, also include properties that has special meaning
14919 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
14921 With INCLUDE-COLUMNS, also include property names given in COLUMN
14922 formats in the current buffer."
14923 (let (rtn range cfmt s p)
14927 (goto-char (point-min))
14928 (while (re-search-forward org-property-start-re nil t)
14929 (setq range (org-get-property-block))
14930 (goto-char (car range))
14931 (while (re-search-forward
14932 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
14934 (add-to-list 'rtn (org-match-string-no-properties 1)))
14935 (outline-next-heading))))
14937 (when include-specials
14938 (setq rtn (append org-special-properties rtn)))
14940 (when include-defaults
14941 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
14942 (add-to-list 'rtn org-effort-property))
14944 (when include-columns
14948 (goto-char (point-min))
14949 (while (re-search-forward
14950 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
14952 (setq cfmt (match-string 2) s 0)
14953 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
14955 (setq s (match-end 0)
14956 p (match-string 1 cfmt))
14957 (unless (or (equal p "ITEM")
14958 (member p org-special-properties))
14959 (add-to-list 'rtn (match-string 1 cfmt))))))))
14961 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
14963 (defun org-property-values (key)
14964 "Return a list of all values of property KEY in the current buffer."
14968 (goto-char (point-min))
14969 (let ((re (org-re-property key))
14971 (while (re-search-forward re nil t)
14972 (add-to-list 'values (org-trim (match-string 1))))
14973 (delete "" values)))))
14975 (defun org-insert-property-drawer ()
14976 "Insert a property drawer into the current entry."
14977 (org-back-to-heading t)
14978 (looking-at org-outline-regexp)
14979 (let ((indent (if org-adapt-indentation
14980 (- (match-end 0) (match-beginning 0))
14983 (re (concat "^[ \t]*" org-keyword-time-regexp))
14985 (outline-next-heading)
14988 (while (re-search-forward re end t))
14989 (setq hiddenp (outline-invisible-p))
14991 (and (equal (char-after) ?\n) (forward-char 1))
14992 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
14993 (if (member (match-string 1) '("CLOCK:" ":END:"))
14994 ;; just skip this line
14995 (beginning-of-line 2)
14996 ;; Drawer start, find the end
14997 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
14998 (beginning-of-line 1)))
14999 (org-skip-over-state-notes)
15000 (skip-chars-backward " \t\n\r")
15001 (if (eq (char-before) ?*) (forward-char 1))
15002 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
15003 (beginning-of-line 0)
15004 (org-indent-to-column indent)
15005 (beginning-of-line 2)
15006 (org-indent-to-column indent)
15007 (beginning-of-line 0)
15010 (org-back-to-heading t)
15012 (org-flag-drawer t))))
15014 (defun org-insert-drawer (&optional arg drawer)
15015 "Insert a drawer at point.
15017 Optional argument DRAWER, when non-nil, is a string representing
15018 drawer's name. Otherwise, the user is prompted for a name.
15020 If a region is active, insert the drawer around that region
15023 Point is left between drawer's boundaries."
15025 (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
15027 ;; SYSTEM-DRAWERS is a list of drawer names that are used
15028 ;; internally by Org. They are meant to be inserted
15030 (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
15031 ;; Remove system drawers from list. Note: For some reason,
15032 ;; `org-completing-read' ignores the predicate while
15033 ;; `completing-read' handles it fine.
15034 (drawer (if arg "PROPERTIES"
15037 "Drawer: " org-drawers
15038 (lambda (d) (not (member d system-drawers))))))))
15040 ;; With C-u, fall back on `org-insert-property-drawer'
15041 (arg (org-insert-property-drawer))
15042 ;; With an active region, insert a drawer at point.
15043 ((not (org-region-active-p))
15045 (unless (bolp) (insert "\n"))
15046 (insert (format ":%s:\n\n:END:\n" drawer))
15047 (forward-line -2)))
15048 ;; Otherwise, insert the drawer at point
15050 (let ((rbeg (region-beginning))
15051 (rend (copy-marker (region-end))))
15055 (beginning-of-line)
15056 (when (save-excursion
15057 (re-search-forward org-outline-regexp-bol rend t))
15058 (error "Drawers cannot contain headlines"))
15059 ;; Position point at the beginning of the first
15060 ;; non-blank line in region. Insert drawer's opening
15061 ;; there, then indent it.
15062 (org-skip-whitespace)
15063 (beginning-of-line)
15064 (insert ":" drawer ":\n")
15066 (indent-for-tab-command)
15067 ;; Move point to the beginning of the first blank line
15068 ;; after the last non-blank line in region. Insert
15069 ;; drawer's closing, then indent it.
15071 (skip-chars-backward " \r\t\n")
15073 (deactivate-mark t)
15074 (indent-for-tab-command)
15075 (unless (eolp) (insert "\n")))
15076 ;; Clear marker, whatever the outcome of insertion is.
15077 (set-marker rend nil)))))))
15079 (defvar org-property-set-functions-alist nil
15080 "Property set function alist.
15081 Each entry should have the following format:
15083 (PROPERTY . READ-FUNCTION)
15085 The read function will be called with the same argument as
15086 `org-completing-read'.")
15088 (defun org-set-property-function (property)
15089 "Get the function that should be used to set PROPERTY.
15090 This is computed according to `org-property-set-functions-alist'."
15091 (or (cdr (assoc property org-property-set-functions-alist))
15092 'org-completing-read))
15094 (defun org-read-property-value (property)
15095 "Read PROPERTY value from user."
15096 (let* ((completion-ignore-case t)
15097 (allowed (org-property-get-allowed-values nil property 'table))
15098 (cur (org-entry-get nil property))
15099 (prompt (concat property " value"
15100 (if (and cur (string-match "\\S-" cur))
15101 (concat " [" cur "]") "") ": "))
15102 (set-function (org-set-property-function property))
15104 (funcall set-function prompt allowed nil
15105 (not (get-text-property 0 'org-unrestricted
15107 (let (org-completion-use-ido org-completion-use-iswitchb)
15108 (funcall set-function prompt
15109 (mapcar 'list (org-property-values property))
15110 nil nil "" nil cur)))))
15115 (defvar org-last-set-property nil)
15116 (defvar org-last-set-property-value nil)
15117 (defun org-read-property-name ()
15118 "Read a property name."
15119 (let* ((completion-ignore-case t)
15120 (keys (org-buffer-property-keys nil t t))
15121 (default-prop (or (save-excursion
15123 (beginning-of-line)
15124 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
15125 (null (string= (match-string 1) "END"))
15126 (match-string 1))))
15127 org-last-set-property))
15128 (property (org-icompleting-read
15130 (if default-prop (concat " [" default-prop "]") "")
15132 (mapcar 'list keys)
15135 (if (member property keys)
15137 (or (cdr (assoc (downcase property)
15138 (mapcar (lambda (x) (cons (downcase x) x))
15142 (defun org-set-property-and-value (use-last)
15143 "Allow to set [PROPERTY]: [value] direction from prompt.
15144 When use-default, don't even ask, just use the last
15145 \"[PROPERTY]: [value]\" string from the history."
15147 (let* ((completion-ignore-case t)
15148 (pv (or (and use-last org-last-set-property-value)
15149 (org-completing-read
15150 "Enter a \"[Property]: [value]\" pair: "
15151 nil nil nil nil nil
15152 org-last-set-property-value)))
15154 (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
15155 (setq prop (match-string 1 pv)
15156 val (match-string 2 pv))
15157 (org-set-property prop val))))
15159 (defun org-set-property (property value)
15160 "In the current entry, set PROPERTY to VALUE.
15161 When called interactively, this will prompt for a property name, offering
15162 completion on existing and default properties. And then it will prompt
15163 for a value, offering completion either on allowed values (via an inherited
15164 xxx_ALL property) or on existing values in other instances of this property
15165 in the current file."
15166 (interactive (list nil nil))
15167 (let* ((property (or property (org-read-property-name)))
15168 (value (or value (org-read-property-value property)))
15169 (fn (cdr (assoc property org-properties-postprocess-alist))))
15170 (setq org-last-set-property property)
15171 (setq org-last-set-property-value (concat property ": " value))
15172 ;; Possibly postprocess the inserted value:
15173 (when fn (setq value (funcall fn value)))
15174 (unless (equal (org-entry-get nil property) value)
15175 (org-entry-put nil property value))))
15177 (defun org-delete-property (property)
15178 "In the current entry, delete PROPERTY."
15180 (let* ((completion-ignore-case t)
15181 (prop (org-icompleting-read "Property: "
15182 (org-entry-properties nil 'standard))))
15184 (message "Property %s %s" property
15185 (if (org-entry-delete nil property)
15187 "was not present in the entry")))
15189 (defun org-delete-property-globally (property)
15190 "Remove PROPERTY globally, from all entries."
15192 (let* ((completion-ignore-case t)
15193 (prop (org-icompleting-read
15194 "Globally remove property: "
15195 (mapcar 'list (org-buffer-property-keys)))))
15200 (goto-char (point-min))
15202 (while (re-search-forward
15203 (org-re-property property)
15205 (setq cnt (1+ cnt))
15206 (delete-region (match-beginning 0) (1+ (point-at-eol))))
15207 (message "Property \"%s\" removed from %d entries" property cnt)))))
15209 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
15211 (defun org-compute-property-at-point ()
15212 "Compute the property at point.
15213 This looks for an enclosing column format, extracts the operator and
15214 then applies it to the property in the column format's scope."
15216 (unless (org-at-property-p)
15217 (error "Not at a property"))
15218 (let ((prop (org-match-string-no-properties 2)))
15219 (org-columns-get-format-and-top-level)
15220 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
15221 (error "No operator defined for property %s" prop))
15222 (org-columns-compute prop)))
15224 (defvar org-property-allowed-value-functions nil
15225 "Hook for functions supplying allowed values for a specific property.
15226 The functions must take a single argument, the name of the property, and
15227 return a flat list of allowed values. If \":ETC\" is one of
15228 the values, this means that these values are intended as defaults for
15229 completion, but that other values should be allowed too.
15230 The functions must return nil if they are not responsible for this
15233 (defun org-property-get-allowed-values (pom property &optional table)
15234 "Get allowed values for the property PROPERTY.
15235 When TABLE is non-nil, return an alist that can directly be used for
15239 ((equal property "TODO")
15240 (setq vals (org-with-point-at pom
15241 (append org-todo-keywords-1 '("")))))
15242 ((equal property "PRIORITY")
15243 (let ((n org-lowest-priority))
15244 (while (>= n org-highest-priority)
15245 (push (char-to-string n) vals)
15247 ((member property org-special-properties))
15248 ((setq vals (run-hook-with-args-until-success
15249 'org-property-allowed-value-functions property)))
15251 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
15252 (when (and vals (string-match "\\S-" vals))
15253 (setq vals (car (read-from-string (concat "(" vals ")"))))
15254 (setq vals (mapcar (lambda (x)
15255 (cond ((stringp x) x)
15256 ((numberp x) (number-to-string x))
15257 ((symbolp x) (symbol-name x))
15260 (when (member ":ETC" vals)
15261 (setq vals (remove ":ETC" vals))
15262 (org-add-props (car vals) '(org-unrestricted t)))
15263 (if table (mapcar 'list vals) vals)))
15265 (defun org-property-previous-allowed-value (&optional previous)
15266 "Switch to the next allowed value for this property."
15268 (org-property-next-allowed-value t))
15270 (defun org-property-next-allowed-value (&optional previous)
15271 "Switch to the next allowed value for this property."
15273 (unless (org-at-property-p)
15274 (error "Not at a property"))
15275 (let* ((key (match-string 2))
15276 (value (match-string 3))
15277 (allowed (or (org-property-get-allowed-values (point) key)
15278 (and (member value '("[ ]" "[-]" "[X]"))
15282 (error "Allowed values for this property have not been defined"))
15283 (if previous (setq allowed (reverse allowed)))
15284 (if (member value allowed)
15285 (setq nval (car (cdr (member value allowed)))))
15286 (setq nval (or nval (car allowed)))
15287 (if (equal nval value)
15288 (error "Only one allowed value for this property"))
15289 (org-at-property-p)
15290 (replace-match (concat " :" key ": " nval) t t)
15292 (beginning-of-line 1)
15293 (skip-chars-forward " \t")
15294 (run-hook-with-args 'org-property-changed-functions key nval)))
15296 (defun org-find-olp (path &optional this-buffer)
15297 "Return a marker pointing to the entry at outline path OLP.
15298 If anything goes wrong, throw an error.
15299 You can wrap this call to catch the error like this:
15301 (condition-case msg
15302 (org-mobile-locate-entry (match-string 4))
15303 (error (nth 1 msg)))
15305 The return value will then be either a string with the error message,
15306 or a marker if everything is OK.
15308 If THIS-BUFFER is set, the outline path does not contain a file,
15310 (let* ((file (if this-buffer buffer-file-name (pop path)))
15311 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
15315 limit re end found pos heading cnt flevel)
15316 (unless buffer (error "File not found :%s" file))
15317 (with-current-buffer buffer
15321 (setq limit (point-max))
15322 (goto-char (point-min))
15323 (while (setq heading (pop path))
15324 (setq re (format org-complex-heading-regexp-format
15325 (regexp-quote heading)))
15326 (setq cnt 0 pos (point))
15327 (while (re-search-forward re end t)
15328 (setq level (- (match-end 1) (match-beginning 1)))
15329 (if (and (>= level lmin) (<= level lmax))
15330 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
15331 (when (= cnt 0) (error "Heading not found on level %d: %s"
15333 (when (> cnt 1) (error "Heading not unique on level %d: %s"
15336 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
15337 (setq end (save-excursion (org-end-of-subtree t t))))
15338 (when (org-at-heading-p)
15339 (point-marker)))))))
15341 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
15342 "Find node HEADING in BUFFER.
15343 Return a marker to the heading if it was found, or nil if not.
15344 If POS-ONLY is set, return just the position instead of a marker.
15346 The heading text must match exact, but it may have a TODO keyword,
15347 a priority cookie and tags in the standard locations."
15348 (with-current-buffer (or buffer (current-buffer))
15352 (goto-char (point-min))
15353 (let (case-fold-search)
15354 (if (re-search-forward
15355 (format org-complex-heading-regexp-format
15356 (regexp-quote heading)) nil t)
15358 (match-beginning 0)
15359 (move-marker (make-marker) (match-beginning 0)))))))))
15361 (defun org-find-exact-heading-in-directory (heading &optional dir)
15362 "Find Org node headline HEADING in all .org files in directory DIR.
15363 When the target headline is found, return a marker to this location."
15364 (let ((files (directory-files (or dir default-directory)
15365 nil "\\`[^.#].*\\.org\\'"))
15366 file visiting m buffer)
15368 (while (setq file (pop files))
15369 (message "trying %s" file)
15370 (setq visiting (org-find-base-buffer-visiting file))
15371 (setq buffer (or visiting (find-file-noselect file)))
15372 (setq m (org-find-exact-headline-in-buffer
15374 (when (and (not m) (not visiting)) (kill-buffer buffer))
15375 (and m (throw 'found m))))))
15377 (defun org-find-entry-with-id (ident)
15378 "Locate the entry that contains the ID property with exact value IDENT.
15379 IDENT can be a string, a symbol or a number, this function will search for
15380 the string representation of it.
15381 Return the position where this entry starts, or nil if there is no such entry."
15382 (interactive "sID: ")
15384 ((stringp ident) ident)
15385 ((symbol-name ident) (symbol-name ident))
15386 ((numberp ident) (number-to-string ident))
15387 (t (error "IDENT %s must be a string, symbol or number" ident))))
15388 (case-fold-search nil))
15392 (goto-char (point-min))
15393 (when (re-search-forward
15394 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
15396 (org-back-to-heading t)
15401 (defvar org-last-changed-timestamp nil)
15402 (defvar org-last-inserted-timestamp nil
15403 "The last time stamp inserted with `org-insert-time-stamp'.")
15404 (defvar org-time-was-given) ; dynamically scoped parameter
15405 (defvar org-end-time-was-given) ; dynamically scoped parameter
15406 (defvar org-ts-what) ; dynamically scoped parameter
15408 (defun org-time-stamp (arg &optional inactive)
15409 "Prompt for a date/time and insert a time stamp.
15410 If the user specifies a time like HH:MM or if this command is
15411 called with at least one prefix argument, the time stamp contains
15412 the date and the time. Otherwise, only the date is be included.
15414 All parts of a date not specified by the user is filled in from
15415 the current date/time. So if you just press return without
15416 typing anything, the time stamp will represent the current
15419 If there is already a timestamp at the cursor, it will be
15422 With two universal prefix arguments, insert an active timestamp
15423 with the current time without prompting the user."
15427 ;; Default time is either today, or, when entering a range,
15428 ;; the range start.
15429 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
15431 (re-search-backward
15432 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
15433 (- (point) 20) t)))
15434 (apply 'encode-time (org-parse-time-string (match-string 1)))
15436 (default-input (and ts (org-get-compact-tod ts)))
15437 (repeater (save-excursion
15439 (beginning-of-line)
15440 (when (re-search-forward
15441 "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
15442 (save-excursion (progn (end-of-line) (point))) t)
15443 (match-string 0)))))
15444 org-time-was-given org-end-time-was-given time)
15446 ((and (org-at-timestamp-p t)
15447 (memq last-command '(org-time-stamp org-time-stamp-inactive))
15448 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
15450 (setq time (let ((this-command this-command))
15451 (org-read-date arg 'totime nil nil
15452 default-time default-input inactive)))
15453 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
15454 ((org-at-timestamp-p t)
15455 (setq time (let ((this-command this-command))
15456 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15457 (when (org-at-timestamp-p t) ; just to get the match data
15458 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
15460 (setq org-last-changed-timestamp
15461 (org-insert-time-stamp
15462 time (or org-time-was-given arg)
15463 inactive nil nil (list org-end-time-was-given)))
15464 (when repeater (goto-char (1- (point))) (insert " " repeater)
15465 (setq org-last-changed-timestamp
15466 (concat (substring org-last-inserted-timestamp 0 -1)
15467 " " repeater ">"))))
15468 (message "Timestamp updated"))
15470 (org-insert-time-stamp (current-time) t))
15472 (setq time (let ((this-command this-command))
15473 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15474 (org-insert-time-stamp time (or org-time-was-given arg) inactive
15475 nil nil (list org-end-time-was-given))))))
15477 ;; FIXME: can we use this for something else, like computing time differences?
15478 (defun org-get-compact-tod (s)
15479 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
15480 (let* ((t1 (match-string 1 s))
15481 (h1 (string-to-number (match-string 2 s)))
15482 (m1 (string-to-number (match-string 3 s)))
15483 (t2 (and (match-end 4) (match-string 5 s)))
15484 (h2 (and t2 (string-to-number (match-string 6 s))))
15485 (m2 (and t2 (string-to-number (match-string 7 s))))
15489 (setq dh (- h2 h1) dm (- m2 m1))
15490 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
15491 (concat t1 "+" (number-to-string dh)
15492 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
15494 (defun org-time-stamp-inactive (&optional arg)
15495 "Insert an inactive time stamp.
15496 An inactive time stamp is enclosed in square brackets instead of angle
15497 brackets. It is inactive in the sense that it does not trigger agenda entries,
15498 does not link to the calendar and cannot be changed with the S-cursor keys.
15499 So these are more for recording a certain time/date."
15501 (org-time-stamp arg 'inactive))
15503 (defvar org-date-ovl (make-overlay 1 1))
15504 (overlay-put org-date-ovl 'face 'org-date-selected)
15505 (org-detach-overlay org-date-ovl)
15507 (defvar org-ans1) ; dynamically scoped parameter
15508 (defvar org-ans2) ; dynamically scoped parameter
15510 (defvar org-plain-time-of-day-regexp) ; defined below
15512 (defvar org-overriding-default-time nil) ; dynamically scoped
15513 (defvar org-read-date-overlay nil)
15514 (defvar org-dcst nil) ; dynamically scoped
15515 (defvar org-read-date-history nil)
15516 (defvar org-read-date-final-answer nil)
15517 (defvar org-read-date-analyze-futurep nil)
15518 (defvar org-read-date-analyze-forced-year nil)
15519 (defvar org-read-date-inactive)
15521 (defun org-read-date (&optional org-with-time to-time from-string prompt
15522 default-time default-input inactive)
15523 "Read a date, possibly a time, and make things smooth for the user.
15524 The prompt will suggest to enter an ISO date, but you can also enter anything
15525 which will at least partially be understood by `parse-time-string'.
15526 Unrecognized parts of the date will default to the current day, month, year,
15527 hour and minute. If this command is called to replace a timestamp at point,
15528 or to enter the second timestamp of a range, the default time is taken
15529 from the existing stamp. Furthermore, the command prefers the future,
15530 so if you are giving a date where the year is not given, and the day-month
15531 combination is already past in the current year, it will assume you
15532 mean next year. For details, see the manual. A few examples:
15534 3-2-5 --> 2003-02-05
15535 feb 15 --> currentyear-02-15
15536 2/15 --> currentyear-02-15
15537 sep 12 9 --> 2009-09-12
15538 12:45 --> today 12:45
15539 22 sept 0:34 --> currentyear-09-22 0:34
15540 12 --> currentyear-currentmonth-12
15541 Fri --> nearest Friday (today or later)
15544 Furthermore you can specify a relative date by giving, as the *first* thing
15545 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
15546 change in days weeks, months, years.
15547 With a single plus or minus, the date is relative to today. With a double
15548 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
15549 +4d --> four days from today
15550 +4 --> same as above
15551 +2w --> two weeks from today
15552 ++5 --> five days from default date
15554 The function understands only English month and weekday abbreviations.
15556 While prompting, a calendar is popped up - you can also select the
15557 date with the mouse (button 1). The calendar shows a period of three
15558 months. To scroll it to other months, use the keys `>' and `<'.
15559 If you don't like the calendar, turn it off with
15560 \(setq org-read-date-popup-calendar nil)
15562 With optional argument TO-TIME, the date will immediately be converted
15563 to an internal time.
15564 With an optional argument ORG-WITH-TIME, the prompt will suggest to
15565 also insert a time. Note that when ORG-WITH-TIME is not set, you can
15566 still enter a time, and this function will inform the calling routine
15567 about this change. The calling routine may then choose to change the
15568 format used to insert the time stamp into the buffer to include the time.
15569 With optional argument FROM-STRING, read from this string instead from
15570 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
15571 the time/date that is used for everything that is not specified by the
15573 (require 'parse-time)
15574 (let* ((org-time-stamp-rounding-minutes
15575 (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
15576 (org-dcst org-display-custom-times)
15577 (ct (org-current-time))
15578 (org-def (or org-overriding-default-time default-time ct))
15579 (org-defdecode (decode-time org-def))
15581 (when (< (nth 2 org-defdecode) org-extend-today-until)
15582 (setcar (nthcdr 2 org-defdecode) -1)
15583 (setcar (nthcdr 1 org-defdecode) 59)
15584 (setq org-def (apply 'encode-time org-defdecode)
15585 org-defdecode (decode-time org-def)))))
15586 (calendar-frame-setup nil)
15587 (calendar-setup nil)
15588 (calendar-move-hook nil)
15589 (calendar-view-diary-initially-flag nil)
15590 (calendar-view-holidays-initially-flag nil)
15591 (timestr (format-time-string
15592 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
15593 (prompt (concat (if prompt (concat prompt " ") "")
15594 (format "Date+time [%s]: " timestr)))
15595 ans (org-ans0 "") org-ans1 org-ans2 final)
15598 (from-string (setq ans from-string))
15599 (org-read-date-popup-calendar
15601 (save-window-excursion
15603 (org-eval-in-calendar '(setq cursor-type nil) t)
15606 (calendar-forward-day (- (time-to-days org-def)
15607 (calendar-absolute-from-gregorian
15608 (calendar-current-date))))
15609 (org-eval-in-calendar nil t)
15610 (let* ((old-map (current-local-map))
15611 (map (copy-keymap calendar-mode-map))
15612 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
15613 (org-defkey map (kbd "RET") 'org-calendar-select)
15614 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
15615 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
15616 (org-defkey minibuffer-local-map [(meta shift left)]
15617 (lambda () (interactive)
15618 (org-eval-in-calendar '(calendar-backward-month 1))))
15619 (org-defkey minibuffer-local-map [(meta shift right)]
15620 (lambda () (interactive)
15621 (org-eval-in-calendar '(calendar-forward-month 1))))
15622 (org-defkey minibuffer-local-map [(meta shift up)]
15623 (lambda () (interactive)
15624 (org-eval-in-calendar '(calendar-backward-year 1))))
15625 (org-defkey minibuffer-local-map [(meta shift down)]
15626 (lambda () (interactive)
15627 (org-eval-in-calendar '(calendar-forward-year 1))))
15628 (org-defkey minibuffer-local-map [?\e (shift left)]
15629 (lambda () (interactive)
15630 (org-eval-in-calendar '(calendar-backward-month 1))))
15631 (org-defkey minibuffer-local-map [?\e (shift right)]
15632 (lambda () (interactive)
15633 (org-eval-in-calendar '(calendar-forward-month 1))))
15634 (org-defkey minibuffer-local-map [?\e (shift up)]
15635 (lambda () (interactive)
15636 (org-eval-in-calendar '(calendar-backward-year 1))))
15637 (org-defkey minibuffer-local-map [?\e (shift down)]
15638 (lambda () (interactive)
15639 (org-eval-in-calendar '(calendar-forward-year 1))))
15640 (org-defkey minibuffer-local-map [(shift up)]
15641 (lambda () (interactive)
15642 (org-eval-in-calendar '(calendar-backward-week 1))))
15643 (org-defkey minibuffer-local-map [(shift down)]
15644 (lambda () (interactive)
15645 (org-eval-in-calendar '(calendar-forward-week 1))))
15646 (org-defkey minibuffer-local-map [(shift left)]
15647 (lambda () (interactive)
15648 (org-eval-in-calendar '(calendar-backward-day 1))))
15649 (org-defkey minibuffer-local-map [(shift right)]
15650 (lambda () (interactive)
15651 (org-eval-in-calendar '(calendar-forward-day 1))))
15652 (org-defkey minibuffer-local-map ">"
15653 (lambda () (interactive)
15654 (org-eval-in-calendar '(scroll-calendar-left 1))))
15655 (org-defkey minibuffer-local-map "<"
15656 (lambda () (interactive)
15657 (org-eval-in-calendar '(scroll-calendar-right 1))))
15658 (org-defkey minibuffer-local-map "\C-v"
15659 (lambda () (interactive)
15660 (org-eval-in-calendar
15661 '(calendar-scroll-left-three-months 1))))
15662 (org-defkey minibuffer-local-map "\M-v"
15663 (lambda () (interactive)
15664 (org-eval-in-calendar
15665 '(calendar-scroll-right-three-months 1))))
15666 (run-hooks 'org-read-date-minibuffer-setup-hook)
15669 (use-local-map map)
15670 (setq org-read-date-inactive inactive)
15671 (add-hook 'post-command-hook 'org-read-date-display)
15672 (setq org-ans0 (read-string prompt default-input
15673 'org-read-date-history nil))
15674 ;; org-ans0: from prompt
15675 ;; org-ans1: from mouse click
15676 ;; org-ans2: from calendar motion
15677 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
15678 (remove-hook 'post-command-hook 'org-read-date-display)
15679 (use-local-map old-map)
15680 (when org-read-date-overlay
15681 (delete-overlay org-read-date-overlay)
15682 (setq org-read-date-overlay nil)))))
15683 (bury-buffer "*Calendar*")))))
15685 (t ; Naked prompt only
15687 (setq ans (read-string prompt default-input
15688 'org-read-date-history timestr))
15689 (when org-read-date-overlay
15690 (delete-overlay org-read-date-overlay)
15691 (setq org-read-date-overlay nil)))))
15693 (setq final (org-read-date-analyze ans org-def org-defdecode))
15695 (when org-read-date-analyze-forced-year
15696 (message "Year was forced into %s"
15697 (if org-read-date-force-compatible-dates
15698 "compatible range (1970-2037)"
15699 "range representable on this machine"))
15702 ;; One round trip to get rid of 34th of August and stuff like that....
15703 (setq final (decode-time (apply 'encode-time final)))
15705 (setq org-read-date-final-answer ans)
15708 (apply 'encode-time final)
15709 (if (and (boundp 'org-time-was-given) org-time-was-given)
15710 (format "%04d-%02d-%02d %02d:%02d"
15711 (nth 5 final) (nth 4 final) (nth 3 final)
15712 (nth 2 final) (nth 1 final))
15713 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
15716 (defvar org-defdecode)
15717 (defvar org-with-time)
15718 (defun org-read-date-display ()
15719 "Display the current date prompt interpretation in the minibuffer."
15720 (when org-read-date-display-live
15721 (when org-read-date-overlay
15722 (delete-overlay org-read-date-overlay))
15723 (when (minibufferp (current-buffer))
15726 (while (not (equal (buffer-substring
15727 (max (point-min) (- (point) 4)) (point))
15730 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
15731 " " (or org-ans1 org-ans2)))
15732 (org-end-time-was-given nil)
15733 (f (org-read-date-analyze ans org-def org-defdecode))
15735 org-time-stamp-custom-formats
15736 org-time-stamp-formats))
15737 (fmt (if (or org-with-time
15738 (and (boundp 'org-time-was-given) org-time-was-given))
15741 (txt (format-time-string fmt (apply 'encode-time f)))
15742 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
15743 (txt (concat "=> " txt)))
15744 (when (and org-end-time-was-given
15745 (string-match org-plain-time-of-day-regexp txt))
15746 (setq txt (concat (substring txt 0 (match-end 0)) "-"
15747 org-end-time-was-given
15748 (substring txt (match-end 0)))))
15749 (when org-read-date-analyze-futurep
15750 (setq txt (concat txt " (=>F)")))
15751 (setq org-read-date-overlay
15752 (make-overlay (1- (point-at-eol)) (point-at-eol)))
15753 (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
15755 (defun org-read-date-analyze (ans org-def org-defdecode)
15756 "Analyze the combined answer of the date prompt."
15757 ;; FIXME: cleanup and comment
15758 (let ((nowdecode (decode-time (current-time)))
15759 delta deltan deltaw deltadef year month day
15760 hour minute second wday pm h2 m2 tl wday1
15761 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
15762 (setq org-read-date-analyze-futurep nil
15763 org-read-date-analyze-forced-year nil)
15764 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
15767 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
15768 (setq ans (replace-match "" t t ans)
15770 deltaw (nth 1 delta)
15771 deltadef (nth 2 delta)))
15773 ;; Check if there is an iso week date in there. If yes, store the
15774 ;; info and postpone interpreting it until the rest of the parsing
15776 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
15777 (setq iso-year (if (match-end 1)
15778 (org-small-year-to-year
15779 (string-to-number (match-string 1 ans))))
15780 iso-weekday (if (match-end 3)
15781 (string-to-number (match-string 3 ans)))
15782 iso-week (string-to-number (match-string 2 ans)))
15783 (setq ans (replace-match "" t t ans)))
15785 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
15786 (when (string-match
15787 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
15788 (setq year (if (match-end 2)
15789 (string-to-number (match-string 2 ans))
15790 (progn (setq kill-year t)
15791 (string-to-number (format-time-string "%Y"))))
15792 month (string-to-number (match-string 3 ans))
15793 day (string-to-number (match-string 4 ans)))
15794 (if (< year 100) (setq year (+ 2000 year)))
15795 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15798 ;; Help matching dotted european dates
15799 (when (string-match
15800 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
15801 (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
15803 (string-to-number (format-time-string "%Y")))
15804 day (string-to-number (match-string 1 ans))
15805 month (string-to-number (match-string 2 ans))
15806 ans (replace-match (format "%04d-%02d-%02d" year month day)
15809 ;; Help matching american dates, like 5/30 or 5/30/7
15810 (when (string-match
15811 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
15812 (setq year (if (match-end 4)
15813 (string-to-number (match-string 4 ans))
15814 (progn (setq kill-year t)
15815 (string-to-number (format-time-string "%Y"))))
15816 month (string-to-number (match-string 1 ans))
15817 day (string-to-number (match-string 2 ans)))
15818 (if (< year 100) (setq year (+ 2000 year)))
15819 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15821 ;; Help matching am/pm times, because `parse-time-string' does not do that.
15822 ;; If there is a time with am/pm, and *no* time without it, we convert
15823 ;; so that matching will be successful.
15824 (loop for i from 1 to 2 do ; twice, for end time as well
15825 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
15826 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
15827 (setq hour (string-to-number (match-string 1 ans))
15828 minute (if (match-end 3)
15829 (string-to-number (match-string 3 ans))
15832 (string-to-char (downcase (match-string 4 ans)))))
15833 (if (and (= hour 12) (not pm))
15835 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
15836 (setq ans (replace-match (format "%02d:%02d" hour minute)
15839 ;; Check if a time range is given as a duration
15840 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
15841 (setq hour (string-to-number (match-string 1 ans))
15842 h2 (+ hour (string-to-number (match-string 3 ans)))
15843 minute (string-to-number (match-string 2 ans))
15844 m2 (+ minute (if (match-end 5) (string-to-number
15845 (match-string 5 ans))0)))
15846 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
15847 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
15850 ;; Check if there is a time range
15851 (when (boundp 'org-end-time-was-given)
15852 (setq org-time-was-given nil)
15853 (when (and (string-match org-plain-time-of-day-regexp ans)
15855 (setq org-end-time-was-given (match-string 8 ans))
15856 (setq ans (concat (substring ans 0 (match-beginning 7))
15857 (substring ans (match-end 7))))))
15859 (setq tl (parse-time-string ans)
15860 day (or (nth 3 tl) (nth 3 org-defdecode))
15861 month (or (nth 4 tl)
15862 (if (and org-read-date-prefer-future
15863 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
15864 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
15865 (nth 4 org-defdecode)))
15866 year (or (and (not kill-year) (nth 5 tl))
15867 (if (and org-read-date-prefer-future
15868 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
15869 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
15870 (nth 5 org-defdecode)))
15871 hour (or (nth 2 tl) (nth 2 org-defdecode))
15872 minute (or (nth 1 tl) (nth 1 org-defdecode))
15873 second (or (nth 0 tl) 0)
15876 (when (and (eq org-read-date-prefer-future 'time)
15877 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
15878 (equal day (nth 3 nowdecode))
15879 (equal month (nth 4 nowdecode))
15880 (equal year (nth 5 nowdecode))
15882 (or (< (nth 2 tl) (nth 2 nowdecode))
15883 (and (= (nth 2 tl) (nth 2 nowdecode))
15885 (< (nth 1 tl) (nth 1 nowdecode)))))
15889 ;; Special date definitions below
15892 ;; There was an iso week
15895 (setq year (or iso-year year)
15896 day (or iso-weekday wday 1)
15897 wday nil ; to make sure that the trigger below does not match
15898 iso-date (calendar-gregorian-from-absolute
15899 (calendar-absolute-from-iso
15900 (list iso-week day year))))
15901 ; FIXME: Should we also push ISO weeks into the future?
15902 ; (when (and org-read-date-prefer-future
15904 ; (< (calendar-absolute-from-gregorian iso-date)
15905 ; (time-to-days (current-time))))
15906 ; (setq year (1+ year)
15907 ; iso-date (calendar-gregorian-from-absolute
15908 ; (calendar-absolute-from-iso
15909 ; (list iso-week day year)))))
15910 (setq month (car iso-date)
15911 year (nth 2 iso-date)
15912 day (nth 1 iso-date)))
15916 (let ((now (decode-time (current-time))))
15917 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
15918 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
15919 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
15920 ((equal deltaw "m") (setq month (+ month deltan)))
15921 ((equal deltaw "y") (setq year (+ year deltan)))))
15922 ((and wday (not (nth 3 tl)))
15923 ;; Weekday was given, but no day, so pick that day in the week
15924 ;; on or after the derived date.
15925 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
15926 (unless (equal wday wday1)
15927 (setq day (+ day (% (- wday wday1 -7) 7))))))
15928 (if (and (boundp 'org-time-was-given)
15930 (setq org-time-was-given t))
15931 (if (< year 100) (setq year (+ 2000 year)))
15932 ;; Check of the date is representable
15933 (if org-read-date-force-compatible-dates
15936 (setq year 1970 org-read-date-analyze-forced-year t))
15938 (setq year 2037 org-read-date-analyze-forced-year t)))
15939 (condition-case nil
15940 (ignore (encode-time second minute hour day month year))
15942 (setq year (nth 5 org-defdecode))
15943 (setq org-read-date-analyze-forced-year t))))
15944 (setq org-read-date-analyze-futurep futurep)
15945 (list second minute hour day month year)))
15947 (defvar parse-time-weekdays)
15948 (defun org-read-date-get-relative (s today default)
15949 "Check string S for special relative date string.
15950 TODAY and DEFAULT are internal times, for today and for a default.
15951 Return shift list (N what def-flag)
15952 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
15953 N is the number of WHATs to shift.
15954 DEF-FLAG is t when a double ++ or -- indicates shift relative to
15955 the DEFAULT date rather than TODAY."
15956 (require 'parse-time)
15960 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
15962 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
15963 "\\([ \t]\\|$\\)") s)
15964 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
15965 (let* ((dir (if (> (match-end 1) (match-beginning 1))
15966 (string-to-char (substring (match-string 1 s) -1))
15968 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
15969 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
15970 (what (if (match-end 3) (match-string 3 s) "d"))
15971 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
15972 (date (if rel default today))
15973 (wday (nth 6 (decode-time date)))
15977 (setq delta (mod (+ 7 (- wday1 wday)) 7))
15978 (if (= dir ?-) (setq delta (- delta 7)))
15979 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
15980 (list delta "d" rel))
15981 (list (* n (if (= dir ?-) -1 1)) what rel)))))
15983 (defun org-order-calendar-date-args (arg1 arg2 arg3)
15984 "Turn a user-specified date into the internal representation.
15985 The internal representation needed by the calendar is (month day year).
15986 This is a wrapper to handle the brain-dead convention in calendar that
15987 user function argument order change dependent on argument order."
15988 (if (boundp 'calendar-date-style)
15990 ((eq calendar-date-style 'american)
15991 (list arg1 arg2 arg3))
15992 ((eq calendar-date-style 'european)
15993 (list arg2 arg1 arg3))
15994 ((eq calendar-date-style 'iso)
15995 (list arg2 arg3 arg1)))
15996 (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
15997 (if (org-bound-and-true-p european-calendar-style)
15998 (list arg2 arg1 arg3)
15999 (list arg1 arg2 arg3)))))
16001 (defun org-eval-in-calendar (form &optional keepdate)
16002 "Eval FORM in the calendar window and return to current window.
16003 When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
16004 otherwise stick to the current value of `org-ans2'."
16005 (let ((sf (selected-frame))
16006 (sw (selected-window)))
16007 (select-window (get-buffer-window "*Calendar*" t))
16009 (when (and (not keepdate) (calendar-cursor-to-date))
16010 (let* ((date (calendar-cursor-to-date))
16011 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16012 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
16013 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
16015 (org-select-frame-set-input-focus sf)))
16017 (defun org-calendar-select ()
16018 "Return to `org-read-date' with the date currently selected.
16019 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
16021 (when (calendar-cursor-to-date)
16022 (let* ((date (calendar-cursor-to-date))
16023 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16024 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
16025 (if (active-minibuffer-window) (exit-minibuffer))))
16027 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
16028 "Insert a date stamp for the date given by the internal TIME.
16029 WITH-HM means use the stamp format that includes the time of the day.
16030 INACTIVE means use square brackets instead of angular ones, so that the
16031 stamp will not contribute to the agenda.
16032 PRE and POST are optional strings to be inserted before and after the
16034 The command returns the inserted time stamp."
16035 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
16037 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
16038 (insert-before-markers (or pre ""))
16039 (when (listp extra)
16040 (setq extra (car extra))
16041 (if (and (stringp extra)
16042 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
16043 (setq extra (format "-%02d:%02d"
16044 (string-to-number (match-string 1 extra))
16045 (string-to-number (match-string 2 extra))))
16048 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
16049 (insert-before-markers (setq stamp (format-time-string fmt time)))
16050 (insert-before-markers (or post ""))
16051 (setq org-last-inserted-timestamp stamp)))
16053 (defun org-toggle-time-stamp-overlays ()
16054 "Toggle the use of custom time stamp formats."
16056 (setq org-display-custom-times (not org-display-custom-times))
16057 (unless org-display-custom-times
16058 (let ((p (point-min)) (bmp (buffer-modified-p)))
16059 (while (setq p (next-single-property-change p 'display))
16060 (if (and (get-text-property p 'display)
16061 (eq (get-text-property p 'face) 'org-date))
16062 (remove-text-properties
16063 p (setq p (next-single-property-change p 'display))
16065 (set-buffer-modified-p bmp)))
16066 (if (featurep 'xemacs)
16067 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
16068 (org-restart-font-lock)
16069 (setq org-table-may-need-update t)
16070 (if org-display-custom-times
16071 (message "Time stamps are overlaid with custom format")
16072 (message "Time stamp overlays removed")))
16074 (defun org-display-custom-time (beg end)
16075 "Overlay modified time stamp format over timestamp between BEG and END."
16076 (let* ((ts (buffer-substring beg end))
16077 t1 w1 with-hm tf time str w2 (off 0))
16079 (setq t1 (org-parse-time-string ts t))
16080 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
16081 (setq off (- (match-end 0) (match-beginning 0)))))
16082 (setq end (- end off))
16083 (setq w1 (- end beg)
16084 with-hm (and (nth 1 t1) (nth 2 t1))
16085 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
16086 time (org-fix-decoded-time t1)
16088 (format-time-string
16089 (substring tf 1 -1) (apply 'encode-time time))
16090 nil 'mouse-face 'highlight)
16092 (if (not (= w2 w1))
16093 (add-text-properties (1+ beg) (+ 2 beg)
16094 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
16095 (if (featurep 'xemacs)
16097 (put-text-property beg end 'invisible t)
16098 (put-text-property beg end 'end-glyph (make-glyph str)))
16099 (put-text-property beg end 'display str))))
16101 (defun org-translate-time (string)
16102 "Translate all timestamps in STRING to custom format.
16103 But do this only if the variable `org-display-custom-times' is set."
16104 (when org-display-custom-times
16107 (re org-ts-regexp-both)
16108 t1 with-hm inactive tf time str beg end)
16109 (while (setq start (string-match re string start))
16110 (setq beg (match-beginning 0)
16112 t1 (save-match-data
16113 (org-parse-time-string (substring string beg end) t))
16114 with-hm (and (nth 1 t1) (nth 2 t1))
16115 inactive (equal (substring string beg (1+ beg)) "[")
16116 tf (funcall (if with-hm 'cdr 'car)
16117 org-time-stamp-custom-formats)
16118 time (org-fix-decoded-time t1)
16119 str (format-time-string
16121 (if inactive "[" "<") (substring tf 1 -1)
16122 (if inactive "]" ">"))
16123 (apply 'encode-time time))
16124 string (replace-match str t t string)
16125 start (+ start (length str)))))))
16128 (defun org-fix-decoded-time (time)
16129 "Set 0 instead of nil for the first 6 elements of time.
16130 Don't touch the rest."
16132 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
16134 (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
16136 (defun org-time-stamp-to-now (timestamp-string &optional seconds)
16137 "Difference between TIMESTAMP-STRING and now in days.
16138 If SECONDS is non-nil, return the difference in seconds."
16139 (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
16140 (- (funcall fdiff (org-time-string-to-time timestamp-string))
16141 (funcall fdiff (current-time)))))
16143 (defun org-deadline-close (timestamp-string &optional ndays)
16144 "Is the time in TIMESTAMP-STRING close to the current date?"
16145 (setq ndays (or ndays (org-get-wdays timestamp-string)))
16146 (and (< (org-time-stamp-to-now timestamp-string) ndays)
16147 (not (org-entry-is-done-p))))
16149 (defun org-get-wdays (ts)
16150 "Get the deadline lead time appropriate for timestring TS."
16152 ((<= org-deadline-warning-days 0)
16153 ;; 0 or negative, enforce this value no matter what
16154 (- org-deadline-warning-days))
16155 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
16156 ;; lead time is specified.
16157 (floor (* (string-to-number (match-string 1 ts))
16158 (cdr (assoc (match-string 2 ts)
16159 '(("d" . 1) ("w" . 7)
16160 ("m" . 30.4) ("y" . 365.25)))))))
16161 ;; go for the default.
16162 (t org-deadline-warning-days)))
16164 (defun org-calendar-select-mouse (ev)
16165 "Return to `org-read-date' with the date currently selected.
16166 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
16168 (mouse-set-point ev)
16169 (when (calendar-cursor-to-date)
16170 (let* ((date (calendar-cursor-to-date))
16171 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16172 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
16173 (if (active-minibuffer-window) (exit-minibuffer))))
16175 (defun org-check-deadlines (ndays)
16176 "Check if there are any deadlines due or past due.
16177 A deadline is considered due if it happens within `org-deadline-warning-days'
16178 days from today's date. If the deadline appears in an entry marked DONE,
16179 it is not shown. The prefix arg NDAYS can be used to test that many
16180 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
16182 (let* ((org-warn-days
16184 ((equal ndays '(4)) 100000)
16185 (ndays (prefix-numeric-value ndays))
16186 (t (abs org-deadline-warning-days))))
16187 (case-fold-search nil)
16188 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
16190 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
16192 (message "%d deadlines past-due or due within %d days"
16193 (org-occur regexp nil callback)
16196 (defsubst org-re-timestamp (type)
16197 "Return a regexp for timestamp TYPE.
16198 Allowed values for TYPE are:
16200 all: all timestamps
16201 active: only active timestamps (<...>)
16202 inactive: only inactive timestamps ([...])
16203 scheduled: only scheduled timestamps
16204 deadline: only deadline timestamps
16206 When TYPE is nil, fall back on returning a regexp that matches
16207 both scheduled and deadline timestamps."
16208 (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9>
\n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
16209 ((eq type 'active) org-ts-regexp)
16210 ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^
\n>]*?\\)\\]")
16211 ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
16212 ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
16213 ((eq type 'scheduled-or-deadline)
16214 (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
16216 (defun org-check-before-date (date)
16217 "Check if there are deadlines or scheduled entries before DATE."
16218 (interactive (list (org-read-date)))
16219 (let ((case-fold-search nil)
16220 (regexp (org-re-timestamp org-ts-type))
16222 (lambda () (time-less-p
16223 (org-time-string-to-time (match-string 1))
16224 (org-time-string-to-time date)))))
16225 (message "%d entries before %s"
16226 (org-occur regexp nil callback) date)))
16228 (defun org-check-after-date (date)
16229 "Check if there are deadlines or scheduled entries after DATE."
16230 (interactive (list (org-read-date)))
16231 (let ((case-fold-search nil)
16232 (regexp (org-re-timestamp org-ts-type))
16236 (org-time-string-to-time (match-string 1))
16237 (org-time-string-to-time date))))))
16238 (message "%d entries after %s"
16239 (org-occur regexp nil callback) date)))
16241 (defun org-check-dates-range (start-date end-date)
16242 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
16243 (interactive (list (org-read-date nil nil nil "Range starts")
16244 (org-read-date nil nil nil "Range end")))
16245 (let ((case-fold-search nil)
16246 (regexp (org-re-timestamp org-ts-type))
16249 (let ((match (match-string 1)))
16252 (org-time-string-to-time match)
16253 (org-time-string-to-time start-date)))
16255 (org-time-string-to-time match)
16256 (org-time-string-to-time end-date)))))))
16257 (message "%d entries between %s and %s"
16258 (org-occur regexp nil callback) start-date end-date)))
16260 (defun org-evaluate-time-range (&optional to-buffer)
16261 "Evaluate a time range by computing the difference between start and end.
16262 Normally the result is just printed in the echo area, but with prefix arg
16263 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
16264 If the time range is actually in a table, the result is inserted into the
16266 For time difference computation, a year is assumed to be exactly 365
16267 days in order to avoid rounding problems."
16270 (org-clock-update-time-maybe)
16272 (unless (org-at-date-range-p t)
16273 (goto-char (point-at-bol))
16274 (re-search-forward org-tr-regexp-both (point-at-eol) t))
16275 (if (not (org-at-date-range-p t))
16276 (error "Not at a time-stamp range, and none found in current line")))
16277 (let* ((ts1 (match-string 1))
16278 (ts2 (match-string 2))
16279 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
16280 (match-end (match-end 0))
16281 (time1 (org-time-string-to-time ts1))
16282 (time2 (org-time-string-to-time ts2))
16283 (t1 (org-float-time time1))
16284 (t2 (org-float-time time2))
16285 (diff (abs (- t2 t1)))
16286 (negative (< (- t2 t1) 0))
16287 ;; (ys (floor (* 365 24 60 60)))
16290 (fy "%dy %dd %02d:%02d")
16292 (fd "%dd %02d:%02d")
16297 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16299 d (floor (/ diff ds)) diff (mod diff ds)
16300 h (floor (/ diff hs)) diff (mod diff hs)
16301 m (floor (/ diff 60)))
16302 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16304 d (floor (+ (/ diff ds) 0.5))
16306 (if (not to-buffer)
16307 (message "%s" (org-make-tdiff-string y d h m))
16308 (if (org-at-table-p)
16310 (goto-char match-end)
16312 (and (looking-at " *|") (goto-char (match-end 0))))
16313 (goto-char match-end))
16315 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
16316 (replace-match ""))
16317 (if negative (insert " -"))
16318 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
16319 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
16320 (insert " " (format fh h m))))
16321 (if align (org-table-align))
16322 (message "Time difference inserted")))))
16324 (defun org-make-tdiff-string (y d h m)
16327 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
16329 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
16331 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
16333 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
16335 (apply 'format fmt (nreverse l))))
16337 (defun org-time-string-to-time (s &optional buffer pos)
16338 "Convert a timestamp string into internal time."
16339 (condition-case errdata
16340 (apply 'encode-time (org-parse-time-string s))
16341 (error (error "Bad timestamp `%s'%s\nError was: %s"
16342 s (if (not (and buffer pos))
16344 (format " at %d in buffer `%s'" pos buffer))
16347 (defun org-time-string-to-seconds (s)
16348 "Convert a timestamp string to a number of seconds."
16349 (org-float-time (org-time-string-to-time s)))
16351 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
16352 "Convert a time stamp to an absolute day number.
16353 If there is a specifier for a cyclic time stamp, get the closest date to
16355 PREFER and SHOW-ALL are passed through to `org-closest-date'.
16356 The variable date is bound by the calendar when this is called."
16358 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
16359 (if (org-diary-sexp-entry (match-string 1 s) "" date)
16362 ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
16363 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
16364 (time-to-days (current-time))) (match-string 0 s)
16367 (condition-case errdata
16368 (apply 'encode-time (org-parse-time-string s))
16369 (error (error "Bad timestamp `%s'%s\nError was: %s"
16370 s (if (not (and buffer pos))
16372 (format " at %d in buffer `%s'" pos buffer))
16373 (cdr errdata))))))))
16375 (defun org-days-to-iso-week (days)
16376 "Return the iso week number."
16378 (car (calendar-iso-from-absolute days)))
16380 (defun org-small-year-to-year (year)
16381 "Convert 2-digit years into 4-digit years.
16382 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
16383 The year 2000 cannot be abbreviated. Any year larger than 99
16384 is returned unchanged."
16386 (setq year (+ 2000 year))
16388 (setq year (+ 1900 year))))
16391 (defun org-time-from-absolute (d)
16392 "Return the time corresponding to date D.
16393 D may be an absolute day number, or a calendar-type list (month day year)."
16394 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
16395 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
16397 (defun org-calendar-holiday ()
16398 "List of holidays, for Diary display in Org-mode."
16399 (require 'holidays)
16401 (if (fboundp 'calendar-check-holidays)
16402 'calendar-check-holidays 'check-calendar-holidays) date)))
16403 (if hl (mapconcat 'identity hl "; "))))
16405 (defun org-diary-sexp-entry (sexp entry date)
16406 "Process a SEXP diary ENTRY for DATE."
16407 (require 'diary-lib)
16408 (let ((result (if calendar-debug-sexp
16409 (let ((stack-trace-on-error t))
16410 (eval (car (read-from-string sexp))))
16411 (condition-case nil
16412 (eval (car (read-from-string sexp)))
16415 (message "Bad sexp at line %d in %s: %s"
16417 (buffer-file-name) sexp)
16419 (cond ((stringp result) (split-string result "; "))
16420 ((and (consp result)
16421 (not (consp (cdr result)))
16422 (stringp (cdr result))) (cdr result))
16423 ((and (consp result)
16424 (stringp (car result))) result)
16427 (defun org-diary-to-ical-string (frombuf)
16428 "Get iCalendar entries from diary entries in buffer FROMBUF.
16429 This uses the icalendar.el library."
16430 (let* ((tmpdir (if (featurep 'xemacs)
16432 temporary-file-directory))
16433 (tmpfile (make-temp-name
16434 (expand-file-name "orgics" tmpdir)))
16436 (with-current-buffer frombuf
16437 (icalendar-export-region (point-min) (point-max) tmpfile)
16438 (setq buf (find-buffer-visiting tmpfile))
16440 (goto-char (point-min))
16441 (if (re-search-forward "^BEGIN:VEVENT" nil t)
16442 (setq b (match-beginning 0)))
16443 (goto-char (point-max))
16444 (if (re-search-backward "^END:VEVENT" nil t)
16445 (setq e (match-end 0)))
16446 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
16448 (delete-file tmpfile)
16451 (defun org-closest-date (start current change prefer show-all)
16452 "Find the date closest to CURRENT that is consistent with START and CHANGE.
16453 When PREFER is `past', return a date that is either CURRENT or past.
16454 When PREFER is `future', return a date that is either CURRENT or future.
16455 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
16456 ;; Make the proper lists from the dates
16458 (let ((a1 '(("h" . hour)
16463 (shour (nth 2 (org-parse-time-string start)))
16464 dn dw sday cday n1 n2 n0
16465 d m y y1 y2 date1 date2 nmonths nm ny m2)
16467 (setq start (org-date-to-gregorian start)
16468 current (org-date-to-gregorian
16471 (time-to-days (current-time))))
16472 sday (calendar-absolute-from-gregorian start)
16473 cday (calendar-absolute-from-gregorian current))
16475 (if (<= cday sday) (throw 'exit sday))
16477 (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
16478 (setq dn (string-to-number (match-string 1 change))
16479 dw (cdr (assoc (match-string 2 change) a1)))
16480 (error "Invalid change specifier: %s" change))
16481 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
16484 (let ((missing-hours
16485 (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
16487 (setq n1 (if (zerop missing-hours) cday
16488 (- cday (1+ (floor (/ missing-hours 24)))))
16489 n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
16491 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
16494 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
16495 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
16496 (setq date1 (list m d y1)
16497 n1 (calendar-absolute-from-gregorian date1)
16498 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
16499 n2 (calendar-absolute-from-gregorian date2)))
16501 ;; approx number of month between the two dates
16502 (setq nmonths (floor (/ (- cday sday) 30.436875)))
16503 ;; How often does dn fit in there?
16504 (setq d (nth 1 start) m (car start) y (nth 2 start)
16505 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
16507 ny (floor (/ m 12))
16510 (while (> m 12) (setq m (- m 12) y (1+ y)))
16511 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
16512 (setq m2 (+ m dn) y2 y)
16513 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16514 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
16515 (while (<= n2 cday)
16516 (setq n1 n2 m m2 y y2)
16517 (setq m2 (+ m dn) y2 y)
16518 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16519 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
16520 ;; Make sure n1 is the earlier date
16521 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
16524 ((eq prefer 'past) (if (= cday n2) n2 n1))
16525 ((eq prefer 'future) (if (= cday n1) n1 n2))
16526 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
16528 ((eq prefer 'past) (if (= cday n2) n2 n1))
16529 ((eq prefer 'future) (if (= cday n1) n1 n2))
16530 (t (if (= cday n1) n1 n2)))))))
16532 (defun org-date-to-gregorian (date)
16533 "Turn any specification of DATE into a Gregorian date for the calendar."
16534 (cond ((integerp date) (calendar-gregorian-from-absolute date))
16535 ((and (listp date) (= (length date) 3)) date)
16537 (setq date (org-parse-time-string date))
16538 (list (nth 4 date) (nth 3 date) (nth 5 date)))
16540 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
16542 (defun org-parse-time-string (s &optional nodefault)
16543 "Parse the standard Org-mode time string.
16544 This should be a lot faster than the normal `parse-time-string'.
16545 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
16546 hour and minute fields will be nil if not given."
16547 (cond ((string-match org-ts-regexp0 s)
16549 (if (or (match-beginning 8) (not nodefault))
16550 (string-to-number (or (match-string 8 s) "0")))
16551 (if (or (match-beginning 7) (not nodefault))
16552 (string-to-number (or (match-string 7 s) "0")))
16553 (string-to-number (match-string 4 s))
16554 (string-to-number (match-string 3 s))
16555 (string-to-number (match-string 2 s))
16557 ((string-match "^<[^>]+>$" s)
16558 (decode-time (seconds-to-time (org-matcher-time s))))
16559 (t (error "Not a standard Org-mode time string: %s" s))))
16561 (defun org-timestamp-up (&optional arg)
16562 "Increase the date item at the cursor by one.
16563 If the cursor is on the year, change the year. If it is on the month,
16564 the day or the time, change that.
16565 With prefix ARG, change by that many units."
16567 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
16569 (defun org-timestamp-down (&optional arg)
16570 "Decrease the date item at the cursor by one.
16571 If the cursor is on the year, change the year. If it is on the month,
16572 the day or the time, change that.
16573 With prefix ARG, change by that many units."
16575 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
16577 (defun org-timestamp-up-day (&optional arg)
16578 "Increase the date in the time stamp by one day.
16579 With prefix ARG, change that many days."
16581 (if (and (not (org-at-timestamp-p t))
16582 (org-at-heading-p))
16584 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
16586 (defun org-timestamp-down-day (&optional arg)
16587 "Decrease the date in the time stamp by one day.
16588 With prefix ARG, change that many days."
16590 (if (and (not (org-at-timestamp-p t))
16591 (org-at-heading-p))
16593 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
16595 (defun org-at-timestamp-p (&optional inactive-ok)
16596 "Determine if the cursor is in or at a timestamp."
16598 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
16600 (ans (or (looking-at tsr)
16602 (skip-chars-backward "^[<\n\r\t")
16603 (if (> (point) (point-min)) (backward-char 1))
16604 (and (looking-at tsr)
16605 (> (- (match-end 0) pos) -1))))))
16607 (boundp 'org-ts-what)
16610 ((= pos (match-beginning 0)) 'bracket)
16611 ;; Point is considered to be "on the bracket" whether
16612 ;; it's really on it or right after it.
16613 ((= pos (1- (match-end 0))) 'bracket)
16614 ((= pos (match-end 0)) 'after)
16615 ((org-pos-in-match-range pos 2) 'year)
16616 ((org-pos-in-match-range pos 3) 'month)
16617 ((org-pos-in-match-range pos 7) 'hour)
16618 ((org-pos-in-match-range pos 8) 'minute)
16619 ((or (org-pos-in-match-range pos 4)
16620 (org-pos-in-match-range pos 5)) 'day)
16621 ((and (> pos (or (match-end 8) (match-end 5)))
16622 (< pos (match-end 0)))
16623 (- pos (or (match-end 8) (match-end 5))))
16627 (defun org-toggle-timestamp-type ()
16628 "Toggle the type (<active> or [inactive]) of a time stamp."
16630 (when (org-at-timestamp-p t)
16631 (let ((beg (match-beginning 0)) (end (match-end 0))
16632 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
16635 (while (re-search-forward "[][<>]" end t)
16636 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
16638 (message "Timestamp is now %sactive"
16639 (if (equal (char-after beg) ?<) "" "in")))))
16641 (defvar org-clock-history) ; defined in org-clock.el
16642 (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
16643 (defun org-timestamp-change (n &optional what updown)
16644 "Change the date in the time stamp at point.
16645 The date will be changed by N times WHAT. WHAT can be `day', `month',
16646 `year', `minute', `second'. If WHAT is not given, the cursor position
16647 in the timestamp determines what will be changed."
16648 (let ((origin (point)) origin-cat
16650 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
16653 ts time time0 fixnext clrgx)
16654 (if (not (org-at-timestamp-p t))
16655 (error "Not at a timestamp"))
16656 (if (and (not what) (eq org-ts-what 'bracket))
16657 (org-toggle-timestamp-type)
16658 ;; Point isn't on brackets. Remember the part of the time-stamp
16659 ;; the point was in. Indeed, size of time-stamps may change,
16660 ;; but point must be kept in the same category nonetheless.
16661 (setq origin-cat org-ts-what)
16662 (if (and (not what) (not (eq org-ts-what 'day))
16663 org-display-custom-times
16664 (get-text-property (point) 'display)
16665 (not (get-text-property (1- (point)) 'display)))
16666 (setq org-ts-what 'day))
16667 (setq org-ts-what (or what org-ts-what)
16668 inactive (= (char-after (match-beginning 0)) ?\[)
16669 ts (match-string 0))
16672 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
16674 (setq extra (match-string 1 ts)))
16675 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
16677 (setq time0 (org-parse-time-string ts))
16679 (eq org-ts-what 'minute)
16680 (not current-prefix-arg))
16681 ;; This looks like s-up and s-down. Change by one rounding step.
16682 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
16683 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
16684 (setcar (cdr time0) (+ (nth 1 time0)
16685 (if (> n 0) (- rem) (- dm rem))))))
16687 (encode-time (or (car time0) 0)
16688 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
16689 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
16690 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
16691 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
16692 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
16694 (when (and (member org-ts-what '(hour minute))
16696 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
16697 (setq extra (org-modify-ts-extra
16699 (if (eq org-ts-what 'hour) 2 5)
16701 (when (integerp org-ts-what)
16702 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
16703 (if (eq what 'calendar)
16704 (let ((cal-date (org-get-date-from-calendar)))
16705 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
16706 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
16707 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
16708 (setcar time0 (or (car time0) 0))
16709 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
16710 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
16711 (setq time (apply 'encode-time time0))))
16712 ;; Insert the new time-stamp, and ensure point stays in the same
16713 ;; category as before (i.e. not after the last position in that
16715 (let ((pos (point)))
16716 ;; Stay before inserted string. `save-excursion' is of no use.
16717 (setq org-last-changed-timestamp
16718 (org-insert-time-stamp time with-hm inactive nil nil extra))
16721 (looking-at org-ts-regexp3)
16723 ;; `day' category ends before `hour' if any, or at
16724 ;; the end of the day name.
16725 ((eq origin-cat 'day)
16726 (min (or (match-beginning 7) (1- (match-end 5))) origin))
16727 ((eq origin-cat 'hour) (min (match-end 7) origin))
16728 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
16729 ((integerp origin-cat) (min (1- (match-end 0)) origin))
16730 ;; `year' and `month' have both fixed size: point
16731 ;; couldn't have moved into another part.
16733 ;; Update clock if on a CLOCK line.
16734 (org-clock-update-time-maybe)
16735 ;; Maybe adjust the closest clock in `org-clock-history'
16736 (when org-clock-adjust-closest
16737 (if (not (and (org-at-clock-log-p)
16738 (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
16739 org-clock-history))))))
16740 (message "No clock to adjust")
16741 (cond ((save-excursion ; fix previous clock?
16742 (re-search-backward org-ts-regexp0 nil t)
16743 (org-looking-back (concat org-clock-string " \\[")))
16744 (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
16745 ((save-excursion ; fix next clock?
16746 (re-search-backward org-ts-regexp0 nil t)
16747 (looking-at (concat org-ts-regexp0 "\\] =>")))
16748 (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
16749 (save-window-excursion
16750 ;; Find closest clock to point, adjust the previous/next one in history
16751 (let* ((p (save-excursion (org-back-to-heading t)))
16752 (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
16754 (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
16755 (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
16757 (message "No clock to adjust")
16759 (org-goto-marker-or-bmk clfixpos)
16761 (when (re-search-forward clrgx nil t)
16762 (goto-char (match-beginning 1))
16763 (let (org-clock-adjust-closest)
16764 (org-timestamp-change n org-ts-what updown))
16765 (message "Clock adjusted in %s for heading: %s"
16766 (file-name-nondirectory (buffer-file-name))
16767 (org-get-heading t t)))))))))
16768 ;; Try to recenter the calendar window, if any.
16769 (if (and org-calendar-follow-timestamp-change
16770 (get-buffer-window "*Calendar*" t)
16771 (memq org-ts-what '(day month year)))
16772 (org-recenter-calendar (time-to-days time))))))
16774 (defun org-modify-ts-extra (s pos n dm)
16775 "Change the different parts of the lead-time and repeat fields in timestamp."
16776 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
16778 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
16780 ((or (org-pos-in-match-range pos 2)
16781 (org-pos-in-match-range pos 3))
16782 (setq m (string-to-number (match-string 3 s))
16783 h (string-to-number (match-string 2 s)))
16784 (if (org-pos-in-match-range pos 2)
16786 (setq n (* dm (org-no-warnings (signum n))))
16787 (when (not (= 0 (setq rem (% m dm))))
16788 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
16790 (if (< m 0) (setq m (+ m 60) h (1- h)))
16791 (if (> m 59) (setq m (- m 60) h (1+ h)))
16792 (setq h (min 24 (max 0 h)))
16793 (setq ng 1 new (format "-%02d:%02d" h m)))
16794 ((org-pos-in-match-range pos 6)
16795 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
16796 ((org-pos-in-match-range pos 5)
16797 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
16799 ((org-pos-in-match-range pos 9)
16800 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
16801 ((org-pos-in-match-range pos 8)
16802 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
16806 (substring s 0 (match-beginning ng))
16808 (substring s (match-end ng))))))
16811 (defun org-recenter-calendar (date)
16812 "If the calendar is visible, recenter it to DATE."
16813 (let ((cwin (get-buffer-window "*Calendar*" t)))
16815 (let ((calendar-move-hook nil))
16816 (with-selected-window cwin
16817 (calendar-goto-date (if (listp date) date
16818 (calendar-gregorian-from-absolute date))))))))
16820 (defun org-goto-calendar (&optional arg)
16821 "Go to the Emacs calendar at the current date.
16822 If there is a time stamp in the current line, go to that date.
16823 A prefix ARG can be used to force the current date."
16825 (let ((tsr org-ts-regexp) diff
16826 (calendar-move-hook nil)
16827 (calendar-view-holidays-initially-flag nil)
16828 (calendar-view-diary-initially-flag nil))
16829 (if (or (org-at-timestamp-p)
16831 (beginning-of-line 1)
16832 (looking-at (concat ".*" tsr))))
16833 (let ((d1 (time-to-days (current-time)))
16835 (org-time-string-to-time (match-string 1)))))
16836 (setq diff (- d2 d1))))
16838 (calendar-goto-today)
16839 (if (and diff (not arg)) (calendar-forward-day diff))))
16841 (defun org-get-date-from-calendar ()
16842 "Return a list (month day year) of date at point in calendar."
16843 (with-current-buffer "*Calendar*"
16845 (calendar-cursor-to-date))))
16847 (defun org-date-from-calendar ()
16848 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
16849 If there is already a time stamp at the cursor position, update it."
16851 (if (org-at-timestamp-p t)
16852 (org-timestamp-change 0 'calendar)
16853 (let ((cal-date (org-get-date-from-calendar)))
16854 (org-insert-time-stamp
16855 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
16857 (defun org-minutes-to-clocksum-string (m)
16858 "Format number of minutes as a clocksum string.
16859 The format is determined by `org-time-clocksum-format',
16860 `org-time-clocksum-use-fractional' and
16861 `org-time-clocksum-fractional-format'."
16862 (let ((clocksum "") fmt n)
16863 ;; fractional format
16864 (if org-time-clocksum-use-fractional
16866 ;; single format string
16867 ((stringp org-time-clocksum-fractional-format)
16868 (format org-time-clocksum-fractional-format (/ m 60.0)))
16869 ;; choice of fractional formats for different time units
16870 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
16871 (> (/ (truncate m) (* 365 24 60)) 0))
16872 (format fmt (/ m (* 365 24 60.0))))
16873 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
16874 (> (/ (truncate m) (* 30 24 60)) 0))
16875 (format fmt (/ m (* 30 24 60.0))))
16876 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
16877 (> (/ (truncate m) (* 7 24 60)) 0))
16878 (format fmt (/ m (* 7 24 60.0))))
16879 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
16880 (> (/ (truncate m) (* 24 60)) 0))
16881 (format fmt (/ m (* 24 60.0))))
16882 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
16883 (> (/ (truncate m) 60) 0))
16884 (format fmt (/ m 60.0)))
16885 ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
16887 ;; fall back to smallest time unit with a format
16888 ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
16889 (format fmt (/ m 60.0)))
16890 ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
16891 (format fmt (/ m (* 24 60.0))))
16892 ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
16893 (format fmt (/ m (* 7 24 60.0))))
16894 ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
16895 (format fmt (/ m (* 30 24 60.0))))
16896 ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
16897 (format fmt (/ m (* 365 24 60.0)))))
16898 ;; standard (non-fractional) format, with single format string
16899 (if (stringp org-time-clocksum-format)
16900 (format org-time-clocksum-format (setq n (/ m 60)) (- m (* 60 n)))
16901 ;; separate formats components
16902 (and (setq fmt (plist-get org-time-clocksum-format :years))
16903 (or (> (setq n (/ (truncate m) (* 365 24 60))) 0)
16904 (plist-get org-time-clocksum-format :require-years))
16905 (setq clocksum (concat clocksum (format fmt n))
16906 m (- m (* n 365 24 60))))
16907 (and (setq fmt (plist-get org-time-clocksum-format :months))
16908 (or (> (setq n (/ (truncate m) (* 30 24 60))) 0)
16909 (plist-get org-time-clocksum-format :require-months))
16910 (setq clocksum (concat clocksum (format fmt n))
16911 m (- m (* n 30 24 60))))
16912 (and (setq fmt (plist-get org-time-clocksum-format :weeks))
16913 (or (> (setq n (/ (truncate m) (* 7 24 60))) 0)
16914 (plist-get org-time-clocksum-format :require-weeks))
16915 (setq clocksum (concat clocksum (format fmt n))
16916 m (- m (* n 7 24 60))))
16917 (and (setq fmt (plist-get org-time-clocksum-format :days))
16918 (or (> (setq n (/ (truncate m) (* 24 60))) 0)
16919 (plist-get org-time-clocksum-format :require-days))
16920 (setq clocksum (concat clocksum (format fmt n))
16921 m (- m (* n 24 60))))
16922 (and (setq fmt (plist-get org-time-clocksum-format :hours))
16923 (or (> (setq n (/ (truncate m) 60)) 0)
16924 (plist-get org-time-clocksum-format :require-hours))
16925 (setq clocksum (concat clocksum (format fmt n))
16927 (and (setq fmt (plist-get org-time-clocksum-format :minutes))
16928 (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
16929 (setq clocksum (concat clocksum (format fmt m))))
16930 ;; return formatted time duration
16933 (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
16934 (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
16935 "Org mode version 8.0")
16937 (defun org-hours-to-clocksum-string (n)
16938 (org-minutes-to-clocksum-string (* n 60)))
16940 (defun org-hh:mm-string-to-minutes (s)
16941 "Convert a string H:MM to a number of minutes.
16942 If the string is just a number, interpret it as minutes.
16943 In fact, the first hh:mm or number in the string will be taken,
16944 there can be extra stuff in the string.
16945 If no number is found, the return value is 0."
16948 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
16949 (+ (* (string-to-number (match-string 1 s)) 60)
16950 (string-to-number (match-string 2 s))))
16951 ((string-match "\\([0-9]+\\)" s)
16952 (string-to-number (match-string 1 s)))
16955 (defcustom org-effort-durations
16958 ("w" . ,(* 60 8 5))
16959 ("m" . ,(* 60 8 5 4))
16960 ("y" . ,(* 60 8 5 40)))
16961 "Conversion factor to minutes for an effort modifier.
16963 Each entry has the form (MODIFIER . MINUTES).
16965 In an effort string, a number followed by MODIFIER is multiplied
16966 by the specified number of MINUTES to obtain an effort in
16969 For example, if the value of this variable is ((\"hours\" . 60)), then an
16970 effort string \"2hours\" is equivalent to 120 minutes."
16973 :type '(alist :key-type (string :tag "Modifier")
16974 :value-type (number :tag "Minutes")))
16976 (defcustom org-image-actual-width t
16977 "Should we use the actual width of images when inlining them?
16979 When set to `t', always use the image width.
16981 When set to a number, use imagemagick (when available) to set
16982 the image's width to this value.
16984 When set to a number in a list, try to get the width from the
16985 #+ATTR.* keyword if it matches a width specification like
16986 width=\"[0-9]+\", and fall back on that number if none is found.
16988 When set to nil, try to get the width from an #+ATTR.* keyword
16989 and fall back on the original width if none is found.
16991 This requires Emacs >= 24.1, build with imagemagick support."
16992 :group 'org-appearance
16995 (const :tag "Use the image width" t)
16996 (integer :tag "Use a number of pixels")
16997 (list :tag "Use #+ATTR* or a number of pixels" (integer))
16998 (const :tag "Use #+ATTR* or don't resize" nil)))
17000 (defun org-duration-string-to-minutes (s &optional output-to-string)
17001 "Convert a duration string S to minutes.
17003 A bare number is interpreted as minutes, modifiers can be set by
17004 customizing `org-effort-durations' (which see).
17006 Entries containing a colon are interpreted as H:MM by
17007 `org-hh:mm-string-to-minutes'."
17009 (re (concat "\\([0-9.]+\\) *\\("
17010 (regexp-opt (mapcar 'car org-effort-durations))
17012 (while (string-match re s)
17013 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
17014 (string-to-number (match-string 1 s))))
17015 (setq s (replace-match "" nil t s)))
17016 (setq result (floor result))
17017 (incf result (org-hh:mm-string-to-minutes s))
17018 (if output-to-string (number-to-string result) result)))
17022 (defun org-save-all-org-buffers ()
17023 "Save all Org-mode buffers without user confirmation."
17025 (message "Saving all Org-mode buffers...")
17026 (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
17027 (when (featurep 'org-id) (org-id-locations-save))
17028 (message "Saving all Org-mode buffers... done"))
17030 (defun org-revert-all-org-buffers ()
17031 "Revert all Org-mode buffers.
17032 Prompt for confirmation when there are unsaved changes.
17033 Be sure you know what you are doing before letting this function
17034 overwrite your changes.
17036 This function is useful in a setup where one tracks org files
17037 with a version control system, to revert on one machine after pulling
17038 changes from another. I believe the procedure must be like this:
17040 1. M-x org-save-all-org-buffers
17041 2. Pull changes from the other machine, resolve conflicts
17042 3. M-x org-revert-all-org-buffers"
17044 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
17047 (save-window-excursion
17050 (when (and (with-current-buffer b (derived-mode-p 'org-mode))
17051 (with-current-buffer b buffer-file-name))
17052 (org-pop-to-buffer-same-window b)
17053 (revert-buffer t 'no-confirm)))
17055 (when (and (featurep 'org-id) org-id-track-globally)
17056 (org-id-locations-load)))))
17061 (defun org-switchb (&optional arg)
17062 "Switch between Org buffers.
17063 With one prefix argument, restrict available buffers to files.
17064 With two prefix arguments, restrict available buffers to agenda files.
17066 Defaults to `iswitchb' for buffer name completion.
17067 Set `org-completion-use-ido' to make it use ido instead."
17069 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
17070 ((equal arg '(16)) (org-buffer-list 'agenda))
17071 (t (org-buffer-list))))
17072 (org-completion-use-iswitchb org-completion-use-iswitchb)
17073 (org-completion-use-ido org-completion-use-ido))
17074 (unless (or org-completion-use-ido org-completion-use-iswitchb)
17075 (setq org-completion-use-iswitchb t))
17076 (org-pop-to-buffer-same-window
17077 (org-icompleting-read "Org buffer: "
17078 (mapcar 'list (mapcar 'buffer-name blist))
17081 ;;; Define some older names previously used for this functionality
17083 (defalias 'org-ido-switchb 'org-switchb)
17085 (defalias 'org-iswitchb 'org-switchb)
17087 (defun org-buffer-list (&optional predicate exclude-tmp)
17088 "Return a list of Org buffers.
17089 PREDICATE can be `export', `files' or `agenda'.
17091 export restrict the list to Export buffers.
17092 files restrict the list to buffers visiting Org files.
17093 agenda restrict the list to buffers visiting agenda files.
17095 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
17097 (agenda-files (and (eq predicate 'agenda)
17098 (mapcar 'file-truename (org-agenda-files t))))
17101 ((eq predicate 'files)
17102 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
17103 ((eq predicate 'export)
17104 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
17105 ((eq predicate 'agenda)
17107 (with-current-buffer b
17108 (and (derived-mode-p 'org-mode)
17109 (setq bfn (buffer-file-name b))
17110 (member (file-truename bfn) agenda-files)))))
17111 (t (lambda (b) (with-current-buffer b
17112 (or (derived-mode-p 'org-mode)
17113 (string-match "\*Org .*Export"
17114 (buffer-name b)))))))))
17118 (if (and (funcall filter b)
17119 (or (not exclude-tmp)
17120 (not (string-match "tmp" (buffer-name b)))))
17125 (defun org-agenda-files (&optional unrestricted archives)
17126 "Get the list of agenda files.
17127 Optional UNRESTRICTED means return the full list even if a restriction
17128 is currently in place.
17129 When ARCHIVES is t, include all archive files that are really being
17130 used by the agenda files. If ARCHIVE is `ifmode', do this only if
17131 `org-agenda-archives-mode' is t."
17134 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
17135 ((stringp org-agenda-files) (org-read-agenda-file-list))
17136 ((listp org-agenda-files) org-agenda-files)
17137 (t (error "Invalid value of `org-agenda-files'")))))
17138 (setq files (apply 'append
17139 (mapcar (lambda (f)
17140 (if (file-directory-p f)
17142 f t org-agenda-file-regexp)
17145 (when org-agenda-skip-unavailable-files
17146 (setq files (delq nil
17149 (and (file-readable-p file) file)))
17151 (when (or (eq archives t)
17152 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
17153 (setq files (org-add-archive-files files)))
17156 (defun org-agenda-file-p (&optional file)
17157 "Return non-nil, if FILE is an agenda file.
17158 If FILE is omitted, use the file associated with the current
17160 (member (or file (buffer-file-name))
17161 (org-agenda-files t)))
17163 (defun org-edit-agenda-file-list ()
17164 "Edit the list of agenda files.
17165 Depending on setup, this either uses customize to edit the variable
17166 `org-agenda-files', or it visits the file that is holding the list. In the
17167 latter case, the buffer is set up in a way that saving it automatically kills
17168 the buffer and restores the previous window configuration."
17170 (if (stringp org-agenda-files)
17171 (let ((cw (current-window-configuration)))
17172 (find-file org-agenda-files)
17173 (org-set-local 'org-window-configuration cw)
17174 (org-add-hook 'after-save-hook
17176 (set-window-configuration
17177 (prog1 org-window-configuration
17178 (kill-buffer (current-buffer))))
17179 (org-install-agenda-files-menu)
17180 (message "New agenda file list installed"))
17182 (message "%s" (substitute-command-keys
17183 "Edit list and finish with \\[save-buffer]")))
17184 (customize-variable 'org-agenda-files)))
17186 (defun org-store-new-agenda-file-list (list)
17187 "Set new value for the agenda file list and save it correctly."
17188 (if (stringp org-agenda-files)
17189 (let ((fe (org-read-agenda-file-list t)) b u)
17190 (while (setq b (find-buffer-visiting org-agenda-files))
17192 (with-temp-file org-agenda-files
17195 (lambda (f) ;; Keep un-expanded entries.
17196 (if (setq u (assoc f fe))
17201 (let ((org-mode-hook nil) (org-inhibit-startup t)
17202 (org-insert-mode-line-in-empty-file nil))
17203 (setq org-agenda-files list)
17204 (customize-save-variable 'org-agenda-files org-agenda-files))))
17206 (defun org-read-agenda-file-list (&optional pair-with-expansion)
17207 "Read the list of agenda files from a file.
17208 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
17209 filenames, used by `org-store-new-agenda-file-list' to write back
17210 un-expanded file names."
17211 (when (file-directory-p org-agenda-files)
17212 (error "`org-agenda-files' cannot be a single directory"))
17213 (when (stringp org-agenda-files)
17215 (insert-file-contents org-agenda-files)
17218 (let ((e (expand-file-name (substitute-in-file-name f)
17220 (if pair-with-expansion
17223 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
17226 (defun org-cycle-agenda-files ()
17227 "Cycle through the files in `org-agenda-files'.
17228 If the current buffer visits an agenda file, find the next one in the list.
17229 If the current buffer does not, find the first agenda file."
17231 (let* ((fs (org-agenda-files t))
17232 (files (append fs (list (car fs))))
17233 (tcf (if buffer-file-name (file-truename buffer-file-name)))
17235 (unless files (error "No agenda files"))
17237 (while (setq file (pop files))
17238 (if (equal (file-truename file) tcf)
17240 (find-file (car files))
17242 (find-file (car fs)))
17243 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
17245 (defun org-agenda-file-to-front (&optional to-end)
17246 "Move/add the current file to the top of the agenda file list.
17247 If the file is not present in the list, it is added to the front. If it is
17248 present, it is moved there. With optional argument TO-END, add/move to the
17251 (let ((org-agenda-skip-unavailable-files nil)
17252 (file-alist (mapcar (lambda (x)
17253 (cons (file-truename x) x))
17254 (org-agenda-files t)))
17255 (ctf (file-truename
17256 (or buffer-file-name
17257 (error "Please save the current buffer to a file"))))
17259 (setq x (assoc ctf file-alist) had x)
17261 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
17263 (setq file-alist (append (delq x file-alist) (list x)))
17264 (setq file-alist (cons x (delq x file-alist))))
17265 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
17266 (org-install-agenda-files-menu)
17267 (message "File %s to %s of agenda file list"
17268 (if had "moved" "added") (if to-end "end" "front"))))
17270 (defun org-remove-file (&optional file)
17271 "Remove current file from the list of files in variable `org-agenda-files'.
17272 These are the files which are being checked for agenda entries.
17273 Optional argument FILE means use this file instead of the current."
17275 (let* ((org-agenda-skip-unavailable-files nil)
17276 (file (or file buffer-file-name
17277 (error "Current buffer does not visit a file")))
17278 (true-file (file-truename file))
17279 (afile (abbreviate-file-name file))
17280 (files (delq nil (mapcar
17282 (if (equal true-file
17285 (org-agenda-files t)))))
17286 (if (not (= (length files) (length (org-agenda-files t))))
17288 (org-store-new-agenda-file-list files)
17289 (org-install-agenda-files-menu)
17290 (message "Removed file: %s" afile))
17291 (message "File was not in list: %s (not removed)" afile))))
17293 (defun org-file-menu-entry (file)
17294 (vector file (list 'find-file file) t))
17296 (defun org-check-agenda-file (file)
17297 "Make sure FILE exists. If not, ask user what to do."
17298 (when (not (file-exists-p file))
17299 (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
17300 (abbreviate-file-name file))
17301 (let ((r (downcase (read-char-exclusive))))
17304 (org-remove-file file)
17305 (throw 'nextfile t))
17306 (t (error "Abort"))))))
17308 (defun org-get-agenda-file-buffer (file)
17309 "Get a buffer visiting FILE. If the buffer needs to be created, add
17310 it to the list of buffers which might be released later."
17311 (let ((buf (org-find-base-buffer-visiting file)))
17313 buf ; just return it
17314 ;; Make a new buffer and remember it
17315 (setq buf (find-file-noselect file))
17316 (if buf (push buf org-agenda-new-buffers))
17319 (defun org-release-buffers (blist)
17320 "Release all buffers in list, asking the user for confirmation when needed.
17321 When a buffer is unmodified, it is just killed. When modified, it is saved
17322 \(if the user agrees) and then killed."
17324 (while (setq buf (pop blist))
17325 (setq file (buffer-file-name buf))
17326 (when (and (buffer-modified-p buf)
17328 (y-or-n-p (format "Save file %s? " file)))
17329 (with-current-buffer buf (save-buffer)))
17330 (kill-buffer buf))))
17332 (defun org-agenda-prepare-buffers (files)
17333 "Create buffers for all agenda files, protect archived trees and comments."
17335 (let ((pa '(:org-archived t))
17336 (pc '(:org-comment t))
17337 (pall '(:org-archived t :org-comment t))
17338 (inhibit-read-only t)
17339 (rea (concat ":" org-archive-tag ":"))
17343 (while (setq file (pop files))
17347 (org-check-agenda-file file)
17348 (set-buffer (org-get-agenda-file-buffer file)))
17350 (setq bmp (buffer-modified-p))
17351 (org-refresh-category-properties)
17352 (setq org-todo-keywords-for-agenda
17353 (append org-todo-keywords-for-agenda org-todo-keywords-1))
17354 (setq org-done-keywords-for-agenda
17355 (append org-done-keywords-for-agenda org-done-keywords))
17356 (setq org-todo-keyword-alist-for-agenda
17357 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
17358 (setq org-drawers-for-agenda
17359 (append org-drawers-for-agenda org-drawers))
17360 (setq org-tag-alist-for-agenda
17361 (append org-tag-alist-for-agenda org-tag-alist))
17364 (remove-text-properties (point-min) (point-max) pall)
17365 (when org-agenda-skip-archived-trees
17366 (goto-char (point-min))
17367 (while (re-search-forward rea nil t)
17368 (if (org-at-heading-p t)
17369 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
17370 (goto-char (point-min))
17371 (setq re (format org-heading-keyword-regexp-format
17372 org-comment-string))
17373 (while (re-search-forward re nil t)
17374 (add-text-properties
17375 (match-beginning 0) (org-end-of-subtree t) pc)))
17376 (set-buffer-modified-p bmp)))))
17377 (setq org-todo-keywords-for-agenda
17378 (org-uniquify org-todo-keywords-for-agenda))
17379 (setq org-todo-keyword-alist-for-agenda
17380 (org-uniquify org-todo-keyword-alist-for-agenda)
17381 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
17383 ;;;; Embedded LaTeX
17385 (defvar org-cdlatex-mode-map (make-sparse-keymap)
17386 "Keymap for the minor `org-cdlatex-mode'.")
17388 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
17389 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
17390 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
17391 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
17392 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
17394 (defvar org-cdlatex-texmathp-advice-is-done nil
17395 "Flag remembering if we have applied the advice to texmathp already.")
17397 (define-minor-mode org-cdlatex-mode
17398 "Toggle the minor `org-cdlatex-mode'.
17399 This mode supports entering LaTeX environment and math in LaTeX fragments
17401 \\{org-cdlatex-mode-map}"
17403 (when org-cdlatex-mode
17405 (run-hooks 'cdlatex-mode-hook)
17406 (cdlatex-compute-tables))
17407 (unless org-cdlatex-texmathp-advice-is-done
17408 (setq org-cdlatex-texmathp-advice-is-done t)
17409 (defadvice texmathp (around org-math-always-on activate)
17410 "Always return t in org-mode buffers.
17411 This is because we want to insert math symbols without dollars even outside
17412 the LaTeX math segments. If Orgmode thinks that point is actually inside
17413 an embedded LaTeX fragment, let texmathp do its job.
17414 \\[org-cdlatex-mode-map]"
17418 ((not (derived-mode-p 'org-mode)) ad-do-it)
17419 ((eq this-command 'cdlatex-math-symbol)
17420 (setq ad-return-value t
17421 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
17423 (let ((p (org-inside-LaTeX-fragment-p)))
17424 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
17425 (setq ad-return-value t
17426 texmathp-why '("Org-mode embedded math" . 0))
17427 (if p ad-do-it)))))))))
17429 (defun turn-on-org-cdlatex ()
17430 "Unconditionally turn on `org-cdlatex-mode'."
17431 (org-cdlatex-mode 1))
17433 (defun org-inside-LaTeX-fragment-p ()
17434 "Test if point is inside a LaTeX fragment.
17435 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
17436 sequence appearing also before point.
17437 Even though the matchers for math are configurable, this function assumes
17438 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
17439 delimiters are skipped when they have been removed by customization.
17440 The return value is nil, or a cons cell with the delimiter and the
17441 position of this delimiter.
17443 This function does a reasonably good job, but can locally be fooled by
17444 for example currency specifications. For example it will assume being in
17445 inline math after \"$22.34\". The LaTeX fragment formatter will only format
17446 fragments that are properly closed, but during editing, we have to live
17447 with the uncertainty caused by missing closing delimiters. This function
17448 looks only before point, not after."
17450 (let ((pos (point))
17451 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
17453 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
17455 dd-on str (start 0) m re)
17458 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
17459 re (nth 1 (assoc "$" org-latex-regexps)))
17460 (while (string-match re str start)
17462 ((= (match-end 0) (length str))
17463 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
17464 ((= (match-end 0) (- (length str) 5))
17466 (t (setq start (match-end 0))))))
17467 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
17469 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
17470 (and (match-beginning 2) (throw 'exit nil))
17472 (while (re-search-backward "\\$\\$" lim t)
17473 (setq dd-on (not dd-on)))
17475 (if dd-on (cons "$$" m))))))
17477 (defun org-inside-latex-macro-p ()
17478 "Is point inside a LaTeX macro or its arguments?"
17481 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
17483 (defun org-try-cdlatex-tab ()
17484 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
17485 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
17486 - inside a LaTeX fragment, or
17487 - after the first word in a line, where an abbreviation expansion could
17488 insert a LaTeX environment."
17489 (when org-cdlatex-mode
17491 ;; Before any word on the line: No expansion possible.
17492 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
17493 ;; Just after first word on the line: Expand it. Make sure it
17494 ;; cannot happen on headlines, though.
17496 (skip-chars-backward "a-zA-Z0-9*")
17497 (skip-chars-backward " \t")
17498 (and (bolp) (not (org-at-heading-p))))
17500 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
17502 (defun org-cdlatex-underscore-caret (&optional arg)
17503 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
17504 Revert to the normal definition outside of these fragments."
17506 (if (org-inside-LaTeX-fragment-p)
17507 (call-interactively 'cdlatex-sub-superscript)
17508 (let (org-cdlatex-mode)
17509 (call-interactively (key-binding (vector last-input-event))))))
17511 (defun org-cdlatex-math-modify (&optional arg)
17512 "Execute `cdlatex-math-modify' in LaTeX fragments.
17513 Revert to the normal definition outside of these fragments."
17515 (if (org-inside-LaTeX-fragment-p)
17516 (call-interactively 'cdlatex-math-modify)
17517 (let (org-cdlatex-mode)
17518 (call-interactively (key-binding (vector last-input-event))))))
17520 (defvar org-latex-fragment-image-overlays nil
17521 "List of overlays carrying the images of latex fragments.")
17522 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
17524 (defun org-remove-latex-fragment-image-overlays ()
17525 "Remove all overlays with LaTeX fragment images in current buffer."
17526 (mapc 'delete-overlay org-latex-fragment-image-overlays)
17527 (setq org-latex-fragment-image-overlays nil))
17529 (defun org-preview-latex-fragment (&optional subtree)
17530 "Preview the LaTeX fragment at point, or all locally or globally.
17531 If the cursor is in a LaTeX fragment, create the image and overlay
17532 it over the source code. If there is no fragment at point, display
17533 all fragments in the current text, from one headline to the next. With
17534 prefix SUBTREE, display all fragments in the current subtree. With a
17535 double prefix arg \\[universal-argument] \\[universal-argument], or when \
17536 the cursor is before the first headline,
17537 display all fragments in the buffer.
17538 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
17540 (unless buffer-file-name
17541 (error "Can't preview LaTeX fragment in a non-file buffer"))
17542 (org-remove-latex-fragment-image-overlays)
17545 (let (beg end at msg)
17547 ((or (equal subtree '(16))
17548 (not (save-excursion
17549 (re-search-backward org-outline-regexp-bol nil t))))
17550 (setq beg (point-min) end (point-max)
17551 msg "Creating images for buffer...%s"))
17552 ((equal subtree '(4))
17553 (org-back-to-heading)
17554 (setq beg (point) end (org-end-of-subtree t)
17555 msg "Creating images for subtree...%s"))
17557 (if (setq at (org-inside-LaTeX-fragment-p))
17558 (goto-char (max (point-min) (- (cdr at) 2)))
17559 (org-back-to-heading))
17560 (setq beg (point) end (progn (outline-next-heading) (point))
17561 msg (if at "Creating image...%s"
17562 "Creating images for entry...%s"))))
17564 (narrow-to-region beg end)
17567 (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
17568 (file-name-nondirectory
17569 buffer-file-name)))
17570 default-directory 'overlays msg at 'forbuffer
17571 org-latex-create-formula-image-program)
17572 (message msg "done. Use `C-c C-c' to remove images.")))))
17574 (defvar org-latex-regexps
17575 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
17576 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
17577 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
17578 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17579 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17580 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
17581 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
17582 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
17583 "Regular expressions for matching embedded LaTeX.")
17585 (defvar org-export-have-math nil) ;; dynamic scoping
17586 (defun org-format-latex (prefix &optional dir overlays msg at
17587 forbuffer processing-type)
17588 "Replace LaTeX fragments with links to an image, and produce images.
17589 Some of the options can be changed using the variable
17590 `org-format-latex-options'."
17591 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
17592 (let* ((prefixnodir (file-name-nondirectory prefix))
17593 (absprefix (expand-file-name prefix dir))
17594 (todir (file-name-directory absprefix))
17595 (opt org-format-latex-options)
17596 (optnew org-format-latex-options)
17597 (matchers (plist-get opt :matchers))
17598 (re-list org-latex-regexps)
17599 (org-format-latex-header-extra
17600 (plist-get (org-infile-export-plist) :latex-header-extra))
17601 (cnt 0) txt hash link beg end re e checkdir
17603 m n block-type block linkfile movefile ov)
17604 ;; Check the different regular expressions
17605 (while (setq e (pop re-list))
17606 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
17607 block (if block-type "\n\n" ""))
17608 (when (member m matchers)
17609 (goto-char (point-min))
17610 (while (re-search-forward re nil t)
17611 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
17612 (not (get-text-property (match-beginning n)
17615 (not (eq (get-char-property (match-beginning n)
17617 'org-latex-overlay))))
17618 (setq org-export-have-math t)
17620 ((eq processing-type 'verbatim)
17621 ;; Leave the text verbatim, just protect it
17622 (add-text-properties (match-beginning n) (match-end n)
17623 '(org-protected t)))
17624 ((eq processing-type 'mathjax)
17625 ;; Prepare for MathJax processing
17626 (setq string (match-string n))
17627 (if (member m '("$" "$1"))
17629 (delete-region (match-beginning n) (match-end n))
17630 (goto-char (match-beginning n))
17631 (insert (org-add-props (concat "\\(" (substring string 1 -1)
17633 '(org-protected t))))
17634 (add-text-properties (match-beginning n) (match-end n)
17635 '(org-protected t))))
17636 ((or (eq processing-type 'dvipng)
17637 (eq processing-type 'imagemagick))
17638 ;; Process to an image
17639 (setq txt (match-string n)
17640 beg (match-beginning n) end (match-end n)
17642 (let ((face (face-at-point))
17643 (fg (plist-get opt :foreground))
17644 (bg (plist-get opt :background))
17645 print-length print-level) ; make sure full list is printed
17647 ; Get the colors from the face at point
17649 (when (eq fg 'auto)
17650 (setq fg (face-attribute face :foreground nil 'default)))
17651 (when (eq bg 'auto)
17652 (setq bg (face-attribute face :background nil 'default)))
17653 (setq optnew (copy-sequence opt))
17654 (plist-put optnew :foreground fg)
17655 (plist-put optnew :background bg))
17656 (setq hash (sha1 (prin1-to-string
17657 (list org-format-latex-header
17658 org-format-latex-header-extra
17659 org-export-latex-default-packages-alist
17660 org-export-latex-packages-alist
17661 org-format-latex-options
17662 forbuffer txt fg bg)))
17663 linkfile (format "%s_%s.png" prefix hash)
17664 movefile (format "%s_%s.png" absprefix hash)))
17665 (setq link (concat block "[[file:" linkfile "]]" block))
17666 (if msg (message msg cnt))
17668 (unless checkdir ; make sure the directory exists
17670 (or (file-directory-p todir) (make-directory todir t)))
17671 (unless (file-exists-p movefile)
17672 (org-create-formula-image
17673 txt movefile optnew forbuffer processing-type))
17677 (if (eq (overlay-get o 'org-overlay-type)
17678 'org-latex-overlay)
17679 (delete-overlay o)))
17680 (overlays-in beg end))
17681 (setq ov (make-overlay beg end))
17682 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
17683 (if (featurep 'xemacs)
17685 (overlay-put ov 'invisible t)
17688 (make-glyph (vector 'png :file movefile))))
17691 (list 'image :type 'png :file movefile :ascent 'center)))
17692 (push ov org-latex-fragment-image-overlays)
17694 (delete-region beg end)
17695 (insert (org-add-props link
17696 (list 'org-latex-src
17697 (replace-regexp-in-string
17699 'org-latex-src-embed-type
17700 (if block-type 'paragraph 'character))))))
17701 ((eq processing-type 'mathml)
17702 ;; Process to MathML
17703 (unless (save-match-data (org-format-latex-mathml-available-p))
17704 (error "LaTeX to MathML converter not configured"))
17705 (setq txt (match-string n)
17706 beg (match-beginning n) end (match-end n)
17708 (if msg (message msg cnt))
17710 (delete-region beg end)
17711 (insert (org-format-latex-as-mathml
17712 txt block-type prefix dir)))
17714 (error "Unknown conversion type %s for latex fragments"
17715 processing-type)))))))))
17717 (defun org-create-math-formula (latex-frag &optional mathml-file)
17718 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
17719 Use `org-latex-to-mathml-convert-command'. If the conversion is
17720 sucessful, return the portion between \"<math...> </math>\"
17721 elements otherwise return nil. When MATHML-FILE is specified,
17722 write the results in to that file. When invoked as an
17723 interactive command, prompt for LATEX-FRAG, with initial value
17724 set to the current active region and echo the results for user
17726 (interactive (list (let ((frag (when (org-region-active-p)
17727 (buffer-substring-no-properties
17728 (region-beginning) (region-end)))))
17729 (read-string "LaTeX Fragment: " frag nil frag))))
17730 (unless latex-frag (error "Invalid latex-frag"))
17731 (let* ((tmp-in-file (file-relative-name
17732 (make-temp-name (expand-file-name "ltxmathml-in"))))
17733 (ignore (write-region latex-frag nil tmp-in-file))
17734 (tmp-out-file (file-relative-name
17735 (make-temp-name (expand-file-name "ltxmathml-out"))))
17737 org-latex-to-mathml-convert-command
17738 `((?j . ,(shell-quote-argument
17739 (expand-file-name org-latex-to-mathml-jar-file)))
17740 (?I . ,(shell-quote-argument tmp-in-file))
17741 (?o . ,(shell-quote-argument tmp-out-file)))))
17742 mathml shell-command-output)
17743 (when (org-called-interactively-p 'any)
17744 (unless (org-format-latex-mathml-available-p)
17745 (error "LaTeX to MathML converter not configured")))
17746 (message "Running %s" cmd)
17747 (setq shell-command-output (shell-command-to-string cmd))
17749 (when (file-readable-p tmp-out-file)
17750 (with-current-buffer (find-file-noselect tmp-out-file t)
17751 (goto-char (point-min))
17752 (when (re-search-forward
17755 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
17757 (regexp-quote "</math>")) nil t)
17758 (prog1 (match-string 0) (kill-buffer))))))
17762 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
17764 (write-region mathml nil mathml-file))
17765 (when (org-called-interactively-p 'any)
17767 ((message "LaTeX to MathML conversion failed")
17768 (message shell-command-output)))
17769 (delete-file tmp-in-file)
17770 (when (file-exists-p tmp-out-file)
17771 (delete-file tmp-out-file))
17774 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
17775 prefix &optional dir)
17776 "Use `org-create-math-formula' but check local cache first."
17777 (let* ((absprefix (expand-file-name prefix dir))
17778 (print-length nil) (print-level nil)
17779 (formula-id (concat
17784 org-latex-to-mathml-convert-command)))))
17785 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
17786 (formula-cache-dir (file-name-directory formula-cache)))
17788 (unless (file-directory-p formula-cache-dir)
17789 (make-directory formula-cache-dir t))
17791 (unless (file-exists-p formula-cache)
17792 (org-create-math-formula latex-frag formula-cache))
17794 (if (file-exists-p formula-cache)
17795 ;; Successful conversion. Return the link to MathML file.
17797 (format "[[file:%s]]" (file-relative-name formula-cache dir))
17798 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
17799 'org-latex-src-embed-type (if latex-frag-type
17800 'paragraph 'character)))
17801 ;; Failed conversion. Return the LaTeX fragment verbatim
17802 (add-text-properties
17803 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
17806 (defun org-create-formula-image (string tofile options buffer &optional type)
17807 "Create an image from LaTeX source using dvipng or convert.
17808 This function calls either `org-create-formula-image-with-dvipng'
17809 or `org-create-formula-image-with-imagemagick' depending on the
17810 value of `org-latex-create-formula-image-program' or on the value
17811 of the optional TYPE variable.
17813 Note: ultimately these two function should be combined as they
17814 share a good deal of logic."
17815 (org-check-external-command
17816 "latex" "needed to convert LaTeX fragments to images")
17818 (case (or type org-latex-create-formula-image-program)
17820 (org-check-external-command
17821 "dvipng" "needed to convert LaTeX fragments to images")
17822 #'org-create-formula-image-with-dvipng)
17824 (org-check-external-command
17825 "convert" "you need to install imagemagick")
17826 #'org-create-formula-image-with-imagemagick)
17828 "invalid value of `org-latex-create-formula-image-program'")))
17829 string tofile options buffer))
17831 ;; This function borrows from Ganesh Swami's latex2png.el
17832 (defun org-create-formula-image-with-dvipng (string tofile options buffer)
17833 "This calls dvipng."
17834 (require 'org-latex)
17835 (let* ((tmpdir (if (featurep 'xemacs)
17837 temporary-file-directory))
17838 (texfilebase (make-temp-name
17839 (expand-file-name "orgtex" tmpdir)))
17840 (texfile (concat texfilebase ".tex"))
17841 (dvifile (concat texfilebase ".dvi"))
17842 (pngfile (concat texfilebase ".png"))
17843 (fnh (if (featurep 'xemacs)
17844 (font-height (face-font 'default))
17845 (face-attribute 'default :height nil)))
17846 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17847 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17848 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17850 (bg (or (plist-get options (if buffer :background :html-background))
17852 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
17853 (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
17854 (if (eq bg 'default) (setq bg (org-dvipng-color :background))
17855 (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
17856 (with-temp-file texfile
17857 (insert (org-splice-latex-header
17858 org-format-latex-header
17859 org-export-latex-default-packages-alist
17860 org-export-latex-packages-alist t
17861 org-format-latex-header-extra))
17862 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
17863 (require 'org-latex)
17864 (org-export-latex-fix-inputenc))
17865 (let ((dir default-directory))
17866 (condition-case nil
17869 (call-process "latex" nil nil nil texfile))
17872 (if (not (file-exists-p dvifile))
17873 (progn (message "Failed to create dvi file from %s" texfile) nil)
17874 (condition-case nil
17875 (if (featurep 'xemacs)
17876 (call-process "dvipng" nil nil nil
17881 (call-process "dvipng" nil nil nil
17884 ;;"-x" scale "-y" scale
17889 (if (not (file-exists-p pngfile))
17890 (if org-format-latex-signal-error
17891 (error "Failed to create png file from %s" texfile)
17892 (message "Failed to create png file from %s" texfile)
17894 ;; Use the requested file name and clean up
17895 (copy-file pngfile tofile 'replace)
17896 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
17897 (if (file-exists-p (concat texfilebase e))
17898 (delete-file (concat texfilebase e))))
17901 (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
17902 (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
17903 "This calls convert, which is included into imagemagick."
17904 (require 'org-latex)
17905 (let* ((tmpdir (if (featurep 'xemacs)
17907 temporary-file-directory))
17908 (texfilebase (make-temp-name
17909 (expand-file-name "orgtex" tmpdir)))
17910 (texfile (concat texfilebase ".tex"))
17911 (pdffile (concat texfilebase ".pdf"))
17912 (pngfile (concat texfilebase ".png"))
17913 (fnh (if (featurep 'xemacs)
17914 (font-height (face-font 'default))
17915 (face-attribute 'default :height nil)))
17916 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17917 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17918 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17920 (bg (or (plist-get options (if buffer :background :html-background))
17922 (if (eq fg 'default) (setq fg (org-latex-color :foreground))
17923 (setq fg (org-latex-color-format fg)))
17924 (if (eq bg 'default) (setq bg (org-latex-color :background))
17925 (setq bg (org-latex-color-format
17926 (if (string= bg "Transparent") "white" bg))))
17927 (with-temp-file texfile
17928 (insert (org-splice-latex-header
17929 org-format-latex-header
17930 org-export-latex-default-packages-alist
17931 org-export-latex-packages-alist t
17932 org-format-latex-header-extra))
17933 (insert "\n\\begin{document}\n"
17934 "\\definecolor{fg}{rgb}{" fg "}\n"
17935 "\\definecolor{bg}{rgb}{" bg "}\n"
17936 "\n\\pagecolor{bg}\n"
17940 "\n\\end{document}\n" )
17941 (require 'org-latex)
17942 (org-export-latex-fix-inputenc))
17943 (let ((dir default-directory) cmd cmds latex-frags-cmds)
17944 (condition-case nil
17947 (setq cmds org-latex-to-pdf-process)
17949 (setq latex-frags-cmds (pop cmds))
17950 (if (listp latex-frags-cmds)
17952 (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
17953 (while latex-frags-cmds
17954 (setq cmd (pop latex-frags-cmds))
17955 (while (string-match "%b" cmd)
17956 (setq cmd (replace-match
17958 (shell-quote-argument texfile))
17960 (while (string-match "%f" cmd)
17961 (setq cmd (replace-match
17963 (shell-quote-argument (file-name-nondirectory texfile)))
17965 (while (string-match "%o" cmd)
17966 (setq cmd (replace-match
17968 (shell-quote-argument (file-name-directory texfile)))
17970 (setq cmd (split-string cmd))
17971 (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
17974 (if (not (file-exists-p pdffile))
17975 (progn (message "Failed to create pdf file from %s" texfile) nil)
17976 (condition-case nil
17977 (if (featurep 'xemacs)
17978 (call-process "convert" nil nil nil
17984 ;; "-sharpen" "0x1.0"
17986 (call-process "convert" nil nil nil
17992 ; "-sharpen" "0x1.0"
17995 (if (not (file-exists-p pngfile))
17996 (if org-format-latex-signal-error
17997 (error "Failed to create png file from %s" texfile)
17998 (message "Failed to create png file from %s" texfile)
18000 ;; Use the requested file name and clean up
18001 (copy-file pngfile tofile 'replace)
18002 (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
18003 (if (file-exists-p (concat texfilebase e))
18004 (delete-file (concat texfilebase e))))
18007 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
18008 "Fill a LaTeX header template TPL.
18009 In the template, the following place holders will be recognized:
18011 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
18012 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
18013 [PACKAGES] \\usepackage statements for PKG
18014 [NO-PACKAGES] do not include PKG
18015 [EXTRA] the string EXTRA
18016 [NO-EXTRA] do not include EXTRA
18018 For backward compatibility, if both the positive and the negative place
18019 holder is missing, the positive one (without the \"NO-\") will be
18020 assumed to be present at the end of the template.
18021 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
18023 SNIPPETS-P indicates if this is run to create snippet images for HTML."
18024 (let (rpl (end ""))
18025 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
18026 (setq rpl (if (or (match-end 1) (not def-pkg))
18027 "" (org-latex-packages-to-string def-pkg snippets-p t))
18028 tpl (replace-match rpl t t tpl))
18029 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
18031 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
18032 (setq rpl (if (or (match-end 1) (not pkg))
18033 "" (org-latex-packages-to-string pkg snippets-p t))
18034 tpl (replace-match rpl t t tpl))
18037 (org-latex-packages-to-string pkg snippets-p)))))
18039 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
18040 (setq rpl (if (or (match-end 1) (not extra))
18041 "" (concat extra "\n"))
18042 tpl (replace-match rpl t t tpl))
18043 (if (and extra (string-match "\\S-" extra))
18044 (setq end (concat end "\n" extra))))
18046 (if (string-match "\\S-" end)
18047 (concat tpl "\n" end)
18050 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
18051 "Turn an alist of packages into a string with the \\usepackage macros."
18052 (setq pkg (mapconcat (lambda(p)
18055 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
18056 (format "%% Package %s omitted" (cadr p)))
18057 ((equal "" (car p))
18058 (format "\\usepackage{%s}" (cadr p)))
18060 (format "\\usepackage[%s]{%s}"
18061 (car p) (cadr p)))))
18064 (if newline (concat pkg "\n") pkg))
18066 (defun org-dvipng-color (attr)
18067 "Return a RGB color specification for dvipng."
18068 (apply 'format "rgb %s %s %s"
18069 (mapcar 'org-normalize-color
18070 (if (featurep 'xemacs)
18071 (color-rgb-components
18072 (face-property 'default
18073 (cond ((eq attr :foreground) 'foreground)
18074 ((eq attr :background) 'background))))
18075 (color-values (face-attribute 'default attr nil))))))
18077 (defun org-dvipng-color-format (color-name)
18078 "Convert COLOR-NAME to a RGB color value for dvipng."
18079 (apply 'format "rgb %s %s %s"
18080 (mapcar 'org-normalize-color
18081 (color-values color-name))))
18083 (defun org-latex-color (attr)
18084 "Return a RGB color for the LaTeX color package."
18085 (apply 'format "%s,%s,%s"
18086 (mapcar 'org-normalize-color
18087 (if (featurep 'xemacs)
18088 (color-rgb-components
18089 (face-property 'default
18090 (cond ((eq attr :foreground) 'foreground)
18091 ((eq attr :background) 'background))))
18092 (color-values (face-attribute 'default attr nil))))))
18094 (defun org-latex-color-format (color-name)
18095 "Convert COLOR-NAME to a RGB color value."
18096 (apply 'format "%s,%s,%s"
18097 (mapcar 'org-normalize-color
18098 (color-values color-name))))
18100 (defun org-normalize-color (value)
18101 "Return string to be used as color value for an RGB component."
18102 (format "%g" (/ value 65535.0)))
18107 (defvar org-inline-image-overlays nil)
18108 (make-variable-buffer-local 'org-inline-image-overlays)
18110 (defun org-toggle-inline-images (&optional include-linked)
18111 "Toggle the display of inline images.
18112 INCLUDE-LINKED is passed to `org-display-inline-images'."
18114 (if org-inline-image-overlays
18116 (org-remove-inline-images)
18117 (message "Inline image display turned off"))
18118 (org-display-inline-images include-linked)
18119 (if (and (org-called-interactively-p)
18120 org-inline-image-overlays)
18121 (message "%d images displayed inline"
18122 (length org-inline-image-overlays))
18123 (message "No images to display inline"))))
18125 (defun org-redisplay-inline-images ()
18126 "Refresh the display of inline images."
18128 (if (not org-inline-image-overlays)
18129 (org-toggle-inline-images)
18130 (org-toggle-inline-images)
18131 (org-toggle-inline-images)))
18133 (defun org-display-inline-images (&optional include-linked refresh beg end)
18134 "Display inline images.
18135 Normally only links without a description part are inlined, because this
18136 is how it will work for export. When INCLUDE-LINKED is set, also links
18137 with a description part will be inlined. This can be nice for a quick
18138 look at those images, but it does not reflect what exported files will look
18140 When REFRESH is set, refresh existing images between BEG and END.
18141 This will create new image displays only if necessary.
18142 BEG and END default to the buffer boundaries."
18145 (org-remove-inline-images)
18146 (if (fboundp 'clear-image-cache) (clear-image-cache)))
18150 (setq beg (or beg (point-min)) end (or end (point-max)))
18152 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
18153 (substring (org-image-file-name-regexp) 0 -2)
18154 "\\)\\]" (if include-linked "" "\\]")))
18155 old file ov img type attrwidth width)
18156 (while (re-search-forward re end t)
18157 (setq old (get-char-property-and-overlay (match-beginning 1)
18158 'org-image-overlay)
18159 file (expand-file-name
18160 (concat (or (match-string 3) "") (match-string 4))))
18161 (when (image-type-available-p 'imagemagick)
18162 (setq attrwidth (if (or (listp org-image-actual-width)
18163 (null org-image-actual-width))
18166 (when (re-search-backward
18167 "#\\+ATTR.*width=\"\\([^\"]+\\)\""
18169 (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
18170 (string-to-number (match-string 1))))))
18171 width (cond ((eq org-image-actual-width t) nil)
18172 ((null org-image-actual-width) attrwidth)
18173 ((numberp org-image-actual-width)
18174 org-image-actual-width)
18175 ((listp org-image-actual-width)
18176 (or attrwidth (car org-image-actual-width))))
18177 type (if width 'imagemagick)))
18178 (when (file-exists-p file)
18179 (if (and (car-safe old) refresh)
18180 (image-refresh (overlay-get (cdr old) 'display))
18181 (setq img (save-match-data (create-image file type nil :width width)))
18183 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
18184 (overlay-put ov 'display img)
18185 (overlay-put ov 'face 'default)
18186 (overlay-put ov 'org-image-overlay t)
18187 (overlay-put ov 'modification-hooks
18188 (list 'org-display-inline-remove-overlay))
18189 (push ov org-inline-image-overlays)))))))))
18191 (define-obsolete-function-alias
18192 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
18194 (defun org-display-inline-remove-overlay (ov after beg end &optional len)
18195 "Remove inline-display overlay if a corresponding region is modified."
18196 (let ((inhibit-modification-hooks t))
18197 (when (and ov after)
18198 (delete ov org-inline-image-overlays)
18199 (delete-overlay ov))))
18201 (defun org-remove-inline-images ()
18202 "Remove inline display of images."
18204 (mapc 'delete-overlay org-inline-image-overlays)
18205 (setq org-inline-image-overlays nil))
18209 ;; Outline functions from `outline-mode-prefix-map'
18210 ;; that can be remapped in Org:
18211 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
18212 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
18213 (define-key org-mode-map [remap outline-forward-same-level]
18214 'org-forward-heading-same-level)
18215 (define-key org-mode-map [remap outline-backward-same-level]
18216 'org-backward-heading-same-level)
18217 (define-key org-mode-map [remap show-branches]
18218 'org-kill-note-or-show-branches)
18219 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
18220 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
18221 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
18223 ;; Outline functions from `outline-mode-prefix-map' that can not
18224 ;; be remapped in Org:
18226 ;; - the column "key binding" shows whether the Outline function is still
18227 ;; available in Org mode on the same key that it has been bound to in
18229 ;; - "overridden": key used for a different functionality in Org mode
18230 ;; - else: key still bound to the same Outline function in Org mode
18232 ;; | Outline function | key binding | Org replacement |
18233 ;; |------------------------------------+-------------+-----------------------|
18234 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
18235 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
18236 ;; | `outline-up-heading' | `C-c C-u' | still same function |
18237 ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
18238 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
18239 ;; | `show-entry' | overridden | no replacement |
18240 ;; | `show-children' | `C-c C-i' | visibility cycling |
18241 ;; | `show-branches' | `C-c C-k' | still same function |
18242 ;; | `show-subtree' | overridden | visibility cycling |
18243 ;; | `show-all' | overridden | no replacement |
18244 ;; | `hide-subtree' | overridden | visibility cycling |
18245 ;; | `hide-body' | overridden | no replacement |
18246 ;; | `hide-entry' | overridden | visibility cycling |
18247 ;; | `hide-leaves' | overridden | no replacement |
18248 ;; | `hide-sublevels' | overridden | no replacement |
18249 ;; | `hide-other' | overridden | no replacement |
18251 ;; Make `C-c C-x' a prefix key
18252 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
18254 ;; TAB key with modifiers
18255 (org-defkey org-mode-map "\C-i" 'org-cycle)
18256 (org-defkey org-mode-map [(tab)] 'org-cycle)
18257 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
18258 (org-defkey org-mode-map "\M-\t" 'pcomplete)
18259 ;; The following line is necessary under Suse GNU/Linux
18260 (unless (featurep 'xemacs)
18261 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
18262 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
18263 (define-key org-mode-map [backtab] 'org-shifttab)
18265 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
18266 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
18267 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
18269 ;; Cursor keys with modifiers
18270 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
18271 (org-defkey org-mode-map [(meta right)] 'org-metaright)
18272 (org-defkey org-mode-map [(meta up)] 'org-metaup)
18273 (org-defkey org-mode-map [(meta down)] 'org-metadown)
18275 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
18276 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
18277 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
18278 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
18280 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
18281 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
18282 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
18283 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
18285 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
18286 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
18287 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
18288 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
18291 (define-key org-mode-map org-babel-key-prefix org-babel-map)
18292 (mapc (lambda (pair)
18293 (define-key org-babel-map (car pair) (cdr pair)))
18294 org-babel-key-bindings)
18296 ;;; Extra keys for tty access.
18297 ;; We only set them when really needed because otherwise the
18298 ;; menus don't show the simple keys
18300 (when (or org-use-extra-keys
18301 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
18302 (not window-system))
18303 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
18304 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
18305 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
18306 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
18307 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
18308 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
18309 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
18310 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
18311 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
18312 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
18313 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
18314 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
18315 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
18316 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
18317 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
18318 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
18319 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
18320 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
18321 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
18322 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
18323 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
18324 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
18325 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
18326 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
18327 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
18328 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
18329 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
18330 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
18332 ;; All the other keys
18334 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
18335 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
18336 (if (boundp 'narrow-map)
18337 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
18338 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
18339 (if (boundp 'narrow-map)
18340 (org-defkey narrow-map "b" 'org-narrow-to-block)
18341 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
18342 (if (boundp 'narrow-map)
18343 (org-defkey narrow-map "e" 'org-narrow-to-element)
18344 (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
18345 (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
18346 (org-defkey org-mode-map "\M-}" 'org-forward-element)
18347 (org-defkey org-mode-map "\M-{" 'org-backward-element)
18348 (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
18349 (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
18350 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
18351 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
18352 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
18353 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
18354 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
18355 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
18356 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
18357 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
18358 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
18359 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
18360 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
18361 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
18362 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
18363 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
18364 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
18365 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
18366 (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
18367 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
18368 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
18369 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
18370 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
18371 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
18372 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
18373 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
18374 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
18375 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
18376 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
18377 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
18378 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
18379 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
18380 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
18381 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
18382 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
18383 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
18384 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
18385 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
18386 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
18387 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
18388 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
18389 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
18390 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
18391 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
18392 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
18393 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
18394 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
18395 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
18396 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
18397 (org-defkey org-mode-map "\C-c^" 'org-sort)
18398 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
18399 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
18400 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
18401 (org-defkey org-mode-map "\C-m" 'org-return)
18402 (org-defkey org-mode-map "\C-j" 'org-return-indent)
18403 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
18404 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
18405 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
18406 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
18407 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
18408 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
18409 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
18410 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
18411 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
18412 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
18413 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
18414 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
18415 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
18416 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
18417 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
18418 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
18419 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
18420 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
18421 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
18422 (org-defkey org-mode-map "\M-h" 'org-mark-element)
18423 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
18424 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
18426 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
18427 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
18428 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
18430 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
18431 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
18432 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
18433 (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
18434 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
18435 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
18436 (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
18437 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
18438 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
18439 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
18440 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
18441 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
18442 (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
18443 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
18444 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
18445 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
18446 (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
18447 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
18448 (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
18449 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
18450 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
18451 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
18452 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
18454 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
18455 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
18456 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
18457 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
18458 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
18460 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
18462 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
18464 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
18465 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
18467 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
18470 (when (featurep 'xemacs)
18471 (org-defkey org-mode-map 'button3 'popup-mode-menu))
18474 (defconst org-speed-commands-default
18476 ("Outline Navigation")
18477 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
18478 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
18479 ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
18480 ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
18481 ("u" . (org-speed-move-safe 'outline-up-heading))
18483 ("g" . (org-refile t))
18484 ("Outline Visibility")
18486 ("C" . org-shifttab)
18487 (" " . org-display-outline-path)
18488 (":" . org-columns)
18489 ("Outline Structure Editing")
18490 ("U" . org-shiftmetaup)
18491 ("D" . org-shiftmetadown)
18492 ("r" . org-metaright)
18493 ("l" . org-metaleft)
18494 ("R" . org-shiftmetaright)
18495 ("L" . org-shiftmetaleft)
18496 ("i" . (progn (forward-char 1) (call-interactively
18497 'org-insert-heading-respect-content)))
18500 ("a" . org-archive-subtree-default-with-confirmation)
18501 ("." . org-mark-subtree)
18502 ("#" . org-toggle-comment)
18504 ("I" . org-clock-in)
18505 ("O" . org-clock-out)
18506 ("Meta Data Editing")
18508 ("," . (org-priority))
18509 ("0" . (org-priority ?\ ))
18510 ("1" . (org-priority ?A))
18511 ("2" . (org-priority ?B))
18512 ("3" . (org-priority ?C))
18513 (";" . org-set-tags-command)
18514 ("e" . org-set-effort)
18515 ("E" . org-inc-effort)
18516 ("W" . (lambda(m) (interactive "sMinutes before warning: ")
18517 (org-entry-put (point) "APPT_WARNTIME" m)))
18518 ("Agenda Views etc")
18520 ("/" . org-sparse-tree)
18522 ("o" . org-open-at-point)
18523 ("?" . org-speed-command-help)
18524 ("<" . (org-agenda-set-restriction-lock 'subtree))
18525 (">" . (org-agenda-remove-restriction-lock))
18527 "The default speed commands.")
18529 (defun org-print-speed-command (e)
18530 (if (> (length (car e)) 1)
18535 (princ (make-string (length (car e)) ?-))
18539 (if (symbolp (cdr e))
18540 (princ (symbol-name (cdr e)))
18544 (defun org-speed-command-help ()
18545 "Show the available speed commands."
18547 (if (not org-use-speed-commands)
18548 (error "Speed commands are not activated, customize `org-use-speed-commands'")
18549 (with-output-to-temp-buffer "*Help*"
18550 (princ "User-defined Speed commands\n===========================\n")
18551 (mapc 'org-print-speed-command org-speed-commands-user)
18553 (princ "Built-in Speed commands\n=======================\n")
18554 (mapc 'org-print-speed-command org-speed-commands-default))
18555 (with-current-buffer "*Help*"
18556 (setq truncate-lines t))))
18558 (defun org-speed-move-safe (cmd)
18559 "Execute CMD, but make sure that the cursor always ends up in a headline.
18560 If not, return to the original position and throw an error."
18562 (let ((pos (point)))
18563 (call-interactively cmd)
18564 (unless (and (bolp) (org-at-heading-p))
18566 (error "Boundary reached while executing %s" cmd))))
18568 (defvar org-self-insert-command-undo-counter 0)
18570 (defvar org-table-auto-blank-field) ; defined in org-table.el
18571 (defvar org-speed-command nil)
18573 (define-obsolete-function-alias
18574 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
18576 (defun org-speed-command-activate (keys)
18577 "Hook for activating single-letter speed commands.
18578 `org-speed-commands-default' specifies a minimal command set.
18579 Use `org-speed-commands-user' for further customization."
18580 (when (or (and (bolp) (looking-at org-outline-regexp))
18581 (and (functionp org-use-speed-commands)
18582 (funcall org-use-speed-commands)))
18583 (cdr (assoc keys (append org-speed-commands-user
18584 org-speed-commands-default)))))
18586 (define-obsolete-function-alias
18587 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
18589 (defun org-babel-speed-command-activate (keys)
18590 "Hook for activating single-letter code block commands."
18591 (when (and (bolp) (looking-at org-babel-src-block-regexp))
18592 (cdr (assoc keys org-babel-key-bindings))))
18594 (defcustom org-speed-command-hook
18595 '(org-speed-command-default-hook org-babel-speed-command-hook)
18596 "Hook for activating speed commands at strategic locations.
18597 Hook functions are called in sequence until a valid handler is
18600 Each hook takes a single argument, a user-pressed command key
18601 which is also a `self-insert-command' from the global map.
18603 Within the hook, examine the cursor position and the command key
18604 and return nil or a valid handler as appropriate. Handler could
18605 be one of an interactive command, a function, or a form.
18607 Set `org-use-speed-commands' to non-nil value to enable this
18608 hook. The default setting is `org-speed-command-activate'."
18609 :group 'org-structure
18613 (defun org-self-insert-command (N)
18614 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
18615 If the cursor is in a table looking at whitespace, the whitespace is
18616 overwritten, and the table is not marked as requiring realignment."
18618 (org-check-before-invisible-edit 'insert)
18620 ((and org-use-speed-commands
18621 (setq org-speed-command
18622 (run-hook-with-args-until-success
18623 'org-speed-command-hook (this-command-keys))))
18625 ((commandp org-speed-command)
18626 (setq this-command org-speed-command)
18627 (call-interactively org-speed-command))
18628 ((functionp org-speed-command)
18629 (funcall org-speed-command))
18630 ((and org-speed-command (listp org-speed-command))
18631 (eval org-speed-command))
18632 (t (let (org-use-speed-commands)
18633 (call-interactively 'org-self-insert-command)))))
18637 ;; check if we blank the field, and if that triggers align
18638 (and (featurep 'org-table) org-table-auto-blank-field
18639 (member last-command
18640 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
18641 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
18642 ;; got extra space, this field does not determine column width
18643 (let (org-table-may-need-update) (org-table-blank-field))
18644 ;; no extra space, this field may determine column width
18645 (org-table-blank-field)))
18648 (looking-at "[^|\n]* |"))
18649 (let (org-table-may-need-update)
18650 (goto-char (1- (match-end 0)))
18651 (backward-delete-char 1)
18652 (goto-char (match-beginning 0))
18653 (self-insert-command N)))
18655 (setq org-table-may-need-update t)
18656 (self-insert-command N)
18657 (org-fix-tags-on-the-fly)
18658 (if org-self-insert-cluster-for-undo
18659 (if (not (eq last-command 'org-self-insert-command))
18660 (setq org-self-insert-command-undo-counter 1)
18661 (if (>= org-self-insert-command-undo-counter 20)
18662 (setq org-self-insert-command-undo-counter 1)
18663 (and (> org-self-insert-command-undo-counter 0)
18664 buffer-undo-list (listp buffer-undo-list)
18665 (not (cadr buffer-undo-list)) ; remove nil entry
18666 (setcdr buffer-undo-list (cddr buffer-undo-list)))
18667 (setq org-self-insert-command-undo-counter
18668 (1+ org-self-insert-command-undo-counter))))))))
18670 (defun org-check-before-invisible-edit (kind)
18671 "Check is editing if kind KIND would be dangerous with invisible text around.
18672 The detailed reaction depends on the user option `org-catch-invisible-edits'."
18673 ;; First, try to get out of here as quickly as possible, to reduce overhead
18674 (if (and org-catch-invisible-edits
18675 (or (not (boundp 'visible-mode)) (not visible-mode))
18676 (or (get-char-property (point) 'invisible)
18677 (get-char-property (max (point-min) (1- (point))) 'invisible)))
18678 ;; OK, we need to take a closer look
18679 (let* ((invisible-at-point (get-char-property (point) 'invisible))
18680 (invisible-before-point (if (bobp) nil (get-char-property
18681 (1- (point)) 'invisible)))
18682 (border-and-ok-direction
18684 ;; Check if we are acting predictably before invisible text
18685 (and invisible-at-point (not invisible-before-point)
18686 (memq kind '(insert delete-backward)))
18687 ;; Check if we are acting predictably after invisible text
18688 ;; This works not well, and I have turned it off. It seems
18689 ;; better to always show and stop after invisible text.
18690 ;; (and (not invisible-at-point) invisible-before-point
18691 ;; (memq kind '(insert delete)))
18693 (when (or (memq invisible-at-point '(outline org-hide-block t))
18694 (memq invisible-before-point '(outline org-hide-block t)))
18695 (if (eq org-catch-invisible-edits 'error)
18696 (error "Editing in invisible areas is prohibited - make visible first"))
18697 (if (and org-custom-properties-overlays
18698 (y-or-n-p "Display invisible properties in this buffer? "))
18699 (org-toggle-custom-properties-visibility)
18700 ;; Make the area visible
18702 (if invisible-before-point
18703 (goto-char (previous-single-char-property-change
18704 (point) 'invisible)))
18707 ((eq org-catch-invisible-edits 'show)
18708 ;; That's it, we do the edit after showing
18710 "Unfolding invisible region around point before editing")
18712 ((and (eq org-catch-invisible-edits 'smart)
18713 border-and-ok-direction)
18714 (message "Unfolding invisible region around point before editing"))
18716 ;; Don't do the edit, make the user repeat it in full visibility
18717 (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
18719 (defun org-fix-tags-on-the-fly ()
18720 (when (and (equal (char-after (point-at-bol)) ?*)
18721 (org-at-heading-p))
18722 (org-align-tags-here org-tags-column)))
18724 (defun org-delete-backward-char (N)
18725 "Like `delete-backward-char', insert whitespace at field end in tables.
18726 When deleting backwards, in tables this function will insert whitespace in
18727 front of the next \"|\" separator, to keep the table aligned. The table will
18728 still be marked for re-alignment if the field did fill the entire column,
18729 because, in this case the deletion might narrow the column."
18732 (org-check-before-invisible-edit 'delete-backward)
18733 (if (and (org-table-p)
18735 (string-match "|" (buffer-substring (point-at-bol) (point)))
18736 (looking-at ".*?|"))
18737 (let ((pos (point))
18738 (noalign (looking-at "[^|\n\r]* |"))
18739 (c org-table-may-need-update))
18740 (backward-delete-char N)
18741 (if (not overwrite-mode)
18743 (skip-chars-forward "^|")
18745 (goto-char (1- pos))))
18746 ;; noalign: if there were two spaces at the end, this field
18747 ;; does not determine the width of the column.
18748 (if noalign (setq org-table-may-need-update c)))
18749 (backward-delete-char N)
18750 (org-fix-tags-on-the-fly))))
18752 (defun org-delete-char (N)
18753 "Like `delete-char', but insert whitespace at field end in tables.
18754 When deleting characters, in tables this function will insert whitespace in
18755 front of the next \"|\" separator, to keep the table aligned. The table will
18756 still be marked for re-alignment if the field did fill the entire column,
18757 because, in this case the deletion might narrow the column."
18760 (org-check-before-invisible-edit 'delete)
18761 (if (and (org-table-p)
18763 (not (= (char-after) ?|))
18765 (if (looking-at ".*?|")
18766 (let ((pos (point))
18767 (noalign (looking-at "[^|\n\r]* |"))
18768 (c org-table-may-need-update))
18769 (replace-match (concat
18770 (substring (match-string 0) 1 -1)
18773 ;; noalign: if there were two spaces at the end, this field
18774 ;; does not determine the width of the column.
18775 (if noalign (setq org-table-may-need-update c)))
18778 (org-fix-tags-on-the-fly))))
18780 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
18781 (put 'org-self-insert-command 'delete-selection t)
18782 (put 'orgtbl-self-insert-command 'delete-selection t)
18783 (put 'org-delete-char 'delete-selection 'supersede)
18784 (put 'org-delete-backward-char 'delete-selection 'supersede)
18785 (put 'org-yank 'delete-selection 'yank)
18787 ;; Make `flyspell-mode' delay after some commands
18788 (put 'org-self-insert-command 'flyspell-delayed t)
18789 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
18790 (put 'org-delete-char 'flyspell-delayed t)
18791 (put 'org-delete-backward-char 'flyspell-delayed t)
18793 ;; Make pabbrev-mode expand after org-mode commands
18794 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
18795 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
18797 ;; How to do this: Measure non-white length of current string
18798 ;; If equal to column width, we should realign.
18800 (defun org-remap (map &rest commands)
18801 "In MAP, remap the functions given in COMMANDS.
18802 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
18805 (setq old (pop commands) new (pop commands))
18806 (if (fboundp 'command-remapping)
18807 (org-defkey map (vector 'remap old) new)
18808 (substitute-key-definition old new map global-map)))))
18810 (when (eq org-enable-table-editor 'optimized)
18811 ;; If the user wants maximum table support, we need to hijack
18812 ;; some standard editing functions
18813 (org-remap org-mode-map
18814 'self-insert-command 'org-self-insert-command
18815 'delete-char 'org-delete-char
18816 'delete-backward-char 'org-delete-backward-char)
18817 (org-defkey org-mode-map "|" 'org-force-self-insert))
18819 (defvar org-ctrl-c-ctrl-c-hook nil
18820 "Hook for functions attaching themselves to `C-c C-c'.
18822 This can be used to add additional functionality to the C-c C-c
18823 key which executes context-dependent commands. This hook is run
18824 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
18825 run after the last test.
18827 Each function will be called with no arguments. The function
18828 must check if the context is appropriate for it to act. If yes,
18829 it should do its thing and then return a non-nil value. If the
18830 context is wrong, just do nothing and return nil.")
18832 (defvar org-ctrl-c-ctrl-c-final-hook nil
18833 "Hook for functions attaching themselves to `C-c C-c'.
18835 This can be used to add additional functionality to the C-c C-c
18836 key which executes context-dependent commands. This hook is run
18837 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
18838 before the first test.
18840 Each function will be called with no arguments. The function
18841 must check if the context is appropriate for it to act. If yes,
18842 it should do its thing and then return a non-nil value. If the
18843 context is wrong, just do nothing and return nil.")
18845 (defvar org-tab-first-hook nil
18846 "Hook for functions to attach themselves to TAB.
18847 See `org-ctrl-c-ctrl-c-hook' for more information.
18848 This hook runs as the first action when TAB is pressed, even before
18849 `org-cycle' messes around with the `outline-regexp' to cater for
18850 inline tasks and plain list item folding.
18851 If any function in this hook returns t, any other actions that
18852 would have been caused by TAB (such as table field motion or visibility
18853 cycling) will not occur.")
18855 (defvar org-tab-after-check-for-table-hook nil
18856 "Hook for functions to attach themselves to TAB.
18857 See `org-ctrl-c-ctrl-c-hook' for more information.
18858 This hook runs after it has been established that the cursor is not in a
18859 table, but before checking if the cursor is in a headline or if global cycling
18861 If any function in this hook returns t, not other actions like visibility
18862 cycling will be done.")
18864 (defvar org-tab-after-check-for-cycling-hook nil
18865 "Hook for functions to attach themselves to TAB.
18866 See `org-ctrl-c-ctrl-c-hook' for more information.
18867 This hook runs after it has been established that not table field motion and
18868 not visibility should be done because of current context. This is probably
18869 the place where a package like yasnippets can hook in.")
18871 (defvar org-tab-before-tab-emulation-hook nil
18872 "Hook for functions to attach themselves to TAB.
18873 See `org-ctrl-c-ctrl-c-hook' for more information.
18874 This hook runs after every other options for TAB have been exhausted, but
18875 before indentation and \t insertion takes place.")
18877 (defvar org-metaleft-hook nil
18878 "Hook for functions attaching themselves to `M-left'.
18879 See `org-ctrl-c-ctrl-c-hook' for more information.")
18880 (defvar org-metaright-hook nil
18881 "Hook for functions attaching themselves to `M-right'.
18882 See `org-ctrl-c-ctrl-c-hook' for more information.")
18883 (defvar org-metaup-hook nil
18884 "Hook for functions attaching themselves to `M-up'.
18885 See `org-ctrl-c-ctrl-c-hook' for more information.")
18886 (defvar org-metadown-hook nil
18887 "Hook for functions attaching themselves to `M-down'.
18888 See `org-ctrl-c-ctrl-c-hook' for more information.")
18889 (defvar org-shiftmetaleft-hook nil
18890 "Hook for functions attaching themselves to `M-S-left'.
18891 See `org-ctrl-c-ctrl-c-hook' for more information.")
18892 (defvar org-shiftmetaright-hook nil
18893 "Hook for functions attaching themselves to `M-S-right'.
18894 See `org-ctrl-c-ctrl-c-hook' for more information.")
18895 (defvar org-shiftmetaup-hook nil
18896 "Hook for functions attaching themselves to `M-S-up'.
18897 See `org-ctrl-c-ctrl-c-hook' for more information.")
18898 (defvar org-shiftmetadown-hook nil
18899 "Hook for functions attaching themselves to `M-S-down'.
18900 See `org-ctrl-c-ctrl-c-hook' for more information.")
18901 (defvar org-metareturn-hook nil
18902 "Hook for functions attaching themselves to `M-RET'.
18903 See `org-ctrl-c-ctrl-c-hook' for more information.")
18904 (defvar org-shiftup-hook nil
18905 "Hook for functions attaching themselves to `S-up'.
18906 See `org-ctrl-c-ctrl-c-hook' for more information.")
18907 (defvar org-shiftup-final-hook nil
18908 "Hook for functions attaching themselves to `S-up'.
18909 This one runs after all other options except shift-select have been excluded.
18910 See `org-ctrl-c-ctrl-c-hook' for more information.")
18911 (defvar org-shiftdown-hook nil
18912 "Hook for functions attaching themselves to `S-down'.
18913 See `org-ctrl-c-ctrl-c-hook' for more information.")
18914 (defvar org-shiftdown-final-hook nil
18915 "Hook for functions attaching themselves to `S-down'.
18916 This one runs after all other options except shift-select have been excluded.
18917 See `org-ctrl-c-ctrl-c-hook' for more information.")
18918 (defvar org-shiftleft-hook nil
18919 "Hook for functions attaching themselves to `S-left'.
18920 See `org-ctrl-c-ctrl-c-hook' for more information.")
18921 (defvar org-shiftleft-final-hook nil
18922 "Hook for functions attaching themselves to `S-left'.
18923 This one runs after all other options except shift-select have been excluded.
18924 See `org-ctrl-c-ctrl-c-hook' for more information.")
18925 (defvar org-shiftright-hook nil
18926 "Hook for functions attaching themselves to `S-right'.
18927 See `org-ctrl-c-ctrl-c-hook' for more information.")
18928 (defvar org-shiftright-final-hook nil
18929 "Hook for functions attaching themselves to `S-right'.
18930 This one runs after all other options except shift-select have been excluded.
18931 See `org-ctrl-c-ctrl-c-hook' for more information.")
18933 (defun org-modifier-cursor-error ()
18934 "Throw an error, a modified cursor command was applied in wrong context."
18935 (error "This command is active in special context like tables, headlines or items"))
18937 (defun org-shiftselect-error ()
18938 "Throw an error because Shift-Cursor command was applied in wrong context."
18939 (if (and (boundp 'shift-select-mode) shift-select-mode)
18940 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
18941 (error "This command works only in special context like headlines or timestamps")))
18943 (defun org-call-for-shift-select (cmd)
18944 (let ((this-command-keys-shift-translated t))
18945 (call-interactively cmd)))
18947 (defun org-shifttab (&optional arg)
18948 "Global visibility cycling or move to previous table field.
18949 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
18951 See the individual commands for more information."
18954 ((org-at-table-p) (call-interactively 'org-table-previous-field))
18956 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
18957 (message "Content view to level: %d" arg)
18958 (org-content (prefix-numeric-value arg2))
18959 (setq org-cycle-global-status 'overview)))
18960 (t (call-interactively 'org-global-cycle))))
18962 (defun org-shiftmetaleft ()
18963 "Promote subtree or delete table column.
18964 Calls `org-promote-subtree', `org-outdent-item-tree', or
18965 `org-table-delete-column', depending on context. See the
18966 individual commands for more information."
18969 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
18970 ((org-at-table-p) (call-interactively 'org-table-delete-column))
18971 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
18972 ((if (not (org-region-active-p)) (org-at-item-p)
18973 (save-excursion (goto-char (region-beginning))
18975 (call-interactively 'org-outdent-item-tree))
18976 (t (org-modifier-cursor-error))))
18978 (defun org-shiftmetaright ()
18979 "Demote subtree or insert table column.
18980 Calls `org-demote-subtree', `org-indent-item-tree', or
18981 `org-table-insert-column', depending on context. See the
18982 individual commands for more information."
18985 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
18986 ((org-at-table-p) (call-interactively 'org-table-insert-column))
18987 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
18988 ((if (not (org-region-active-p)) (org-at-item-p)
18989 (save-excursion (goto-char (region-beginning))
18991 (call-interactively 'org-indent-item-tree))
18992 (t (org-modifier-cursor-error))))
18994 (defun org-shiftmetaup (&optional arg)
18995 "Move subtree up or kill table row.
18996 Calls `org-move-subtree-up' or `org-table-kill-row' or
18997 `org-move-item-up' or `org-timestamp-up', depending on context.
18998 See the individual commands for more information."
19001 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
19002 ((org-at-table-p) (call-interactively 'org-table-kill-row))
19003 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
19004 ((org-at-item-p) (call-interactively 'org-move-item-up))
19005 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
19006 (call-interactively 'org-timestamp-up)))
19007 (t (org-modifier-cursor-error))))
19009 (defun org-shiftmetadown (&optional arg)
19010 "Move subtree down or insert table row.
19011 Calls `org-move-subtree-down' or `org-table-insert-row' or
19012 `org-move-item-down' or `org-timestamp-up', depending on context.
19013 See the individual commands for more information."
19016 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
19017 ((org-at-table-p) (call-interactively 'org-table-insert-row))
19018 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
19019 ((org-at-item-p) (call-interactively 'org-move-item-down))
19020 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
19021 (call-interactively 'org-timestamp-down)))
19022 (t (org-modifier-cursor-error))))
19024 (defsubst org-hidden-tree-error ()
19026 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
19028 (defun org-metaleft (&optional arg)
19029 "Promote heading or move table column to left.
19030 Calls `org-do-promote' or `org-table-move-column', depending on context.
19031 With no specific context, calls the Emacs default `backward-word'.
19032 See the individual commands for more information."
19035 ((run-hook-with-args-until-success 'org-metaleft-hook))
19036 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
19037 ((org-with-limited-levels
19038 (or (org-at-heading-p)
19039 (and (org-region-active-p)
19041 (goto-char (region-beginning))
19042 (org-at-heading-p)))))
19043 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
19044 (call-interactively 'org-do-promote))
19045 ;; At an inline task.
19046 ((org-at-heading-p)
19047 (call-interactively 'org-inlinetask-promote))
19048 ((or (org-at-item-p)
19049 (and (org-region-active-p)
19051 (goto-char (region-beginning))
19053 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
19054 (call-interactively 'org-outdent-item))
19055 (t (call-interactively 'backward-word))))
19057 (defun org-metaright (&optional arg)
19058 "Demote a subtree, a list item or move table column to right.
19059 In front of a drawer or a block keyword, indent it correctly.
19060 With no specific context, calls the Emacs default `forward-word'.
19061 See the individual commands for more information."
19064 ((run-hook-with-args-until-success 'org-metaright-hook))
19065 ((org-at-table-p) (call-interactively 'org-table-move-column))
19066 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
19067 ((org-at-block-p) (call-interactively 'org-indent-block))
19068 ((org-with-limited-levels
19069 (or (org-at-heading-p)
19070 (and (org-region-active-p)
19072 (goto-char (region-beginning))
19073 (org-at-heading-p)))))
19074 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
19075 (call-interactively 'org-do-demote))
19076 ;; At an inline task.
19077 ((org-at-heading-p)
19078 (call-interactively 'org-inlinetask-demote))
19079 ((or (org-at-item-p)
19080 (and (org-region-active-p)
19082 (goto-char (region-beginning))
19084 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
19085 (call-interactively 'org-indent-item))
19086 (t (call-interactively 'forward-word))))
19088 (defun org-check-for-hidden (what)
19089 "Check if there are hidden headlines/items in the current visual line.
19090 WHAT can be either `headlines' or `items'. If the current line is
19091 an outline or item heading and it has a folded subtree below it,
19092 this function returns t, nil otherwise."
19094 ((eq what 'headlines) org-outline-regexp-bol)
19095 ((eq what 'items) (org-item-beginning-re))
19096 (t (error "This should not happen"))))
19100 (unless (org-region-active-p)
19101 (setq beg (point-at-bol))
19102 (beginning-of-line 2)
19103 (while (and (not (eobp)) ;; this is like `next-line'
19104 (get-char-property (1- (point)) 'invisible))
19105 (beginning-of-line 2))
19108 (goto-char (point-at-eol))
19109 (setq end (max end (point)))
19110 (while (re-search-forward re end t)
19111 (if (get-char-property (match-beginning 0) 'invisible)
19115 (defun org-metaup (&optional arg)
19116 "Move subtree up or move table row up.
19117 Calls `org-move-subtree-up' or `org-table-move-row' or
19118 `org-move-item-up', depending on context. See the individual commands
19119 for more information."
19122 ((run-hook-with-args-until-success 'org-metaup-hook))
19123 ((org-region-active-p)
19124 (let* ((a (min (region-beginning) (region-end)))
19125 (b (1- (max (region-beginning) (region-end))))
19126 (c (save-excursion (goto-char a)
19127 (move-beginning-of-line 0)))
19128 (d (save-excursion (goto-char a)
19129 (move-end-of-line 0) (point))))
19130 (transpose-regions a b c d)
19132 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
19133 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
19134 ((org-at-item-p) (call-interactively 'org-move-item-up))
19135 (t (org-drag-element-backward))))
19137 (defun org-metadown (&optional arg)
19138 "Move subtree down or move table row down.
19139 Calls `org-move-subtree-down' or `org-table-move-row' or
19140 `org-move-item-down', depending on context. See the individual
19141 commands for more information."
19144 ((run-hook-with-args-until-success 'org-metadown-hook))
19145 ((org-region-active-p)
19146 (let* ((a (min (region-beginning) (region-end)))
19147 (b (max (region-beginning) (region-end)))
19148 (c (save-excursion (goto-char b)
19149 (move-beginning-of-line 1)))
19150 (d (save-excursion (goto-char b)
19151 (move-end-of-line 1) (1+ (point)))))
19152 (transpose-regions a b c d)
19154 ((org-at-table-p) (call-interactively 'org-table-move-row))
19155 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
19156 ((org-at-item-p) (call-interactively 'org-move-item-down))
19157 (t (org-drag-element-forward))))
19159 (defun org-shiftup (&optional arg)
19160 "Increase item in timestamp or increase priority of current headline.
19161 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
19162 depending on context. See the individual commands for more information."
19165 ((run-hook-with-args-until-success 'org-shiftup-hook))
19166 ((and org-support-shift-select (org-region-active-p))
19167 (org-call-for-shift-select 'previous-line))
19168 ((org-at-timestamp-p t)
19169 (call-interactively (if org-edit-timestamp-down-means-later
19170 'org-timestamp-down 'org-timestamp-up)))
19171 ((and (not (eq org-support-shift-select 'always))
19172 org-enable-priority-commands
19173 (org-at-heading-p))
19174 (call-interactively 'org-priority-up))
19175 ((and (not org-support-shift-select) (org-at-item-p))
19176 (call-interactively 'org-previous-item))
19177 ((org-clocktable-try-shift 'up arg))
19178 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
19179 (org-support-shift-select
19180 (org-call-for-shift-select 'previous-line))
19181 (t (org-shiftselect-error))))
19183 (defun org-shiftdown (&optional arg)
19184 "Decrease item in timestamp or decrease priority of current headline.
19185 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
19186 depending on context. See the individual commands for more information."
19189 ((run-hook-with-args-until-success 'org-shiftdown-hook))
19190 ((and org-support-shift-select (org-region-active-p))
19191 (org-call-for-shift-select 'next-line))
19192 ((org-at-timestamp-p t)
19193 (call-interactively (if org-edit-timestamp-down-means-later
19194 'org-timestamp-up 'org-timestamp-down)))
19195 ((and (not (eq org-support-shift-select 'always))
19196 org-enable-priority-commands
19197 (org-at-heading-p))
19198 (call-interactively 'org-priority-down))
19199 ((and (not org-support-shift-select) (org-at-item-p))
19200 (call-interactively 'org-next-item))
19201 ((org-clocktable-try-shift 'down arg))
19202 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
19203 (org-support-shift-select
19204 (org-call-for-shift-select 'next-line))
19205 (t (org-shiftselect-error))))
19207 (defun org-shiftright (&optional arg)
19208 "Cycle the thing at point or in the current line, depending on context.
19209 Depending on context, this does one of the following:
19211 - switch a timestamp at point one day into the future
19212 - on a headline, switch to the next TODO keyword.
19213 - on an item, switch entire list to the next bullet type
19214 - on a property line, switch to the next allowed value
19215 - on a clocktable definition line, move time block into the future"
19218 ((run-hook-with-args-until-success 'org-shiftright-hook))
19219 ((and org-support-shift-select (org-region-active-p))
19220 (org-call-for-shift-select 'forward-char))
19221 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
19222 ((and (not (eq org-support-shift-select 'always))
19223 (org-at-heading-p))
19224 (let ((org-inhibit-logging
19225 (not org-treat-S-cursor-todo-selection-as-state-change))
19226 (org-inhibit-blocking
19227 (not org-treat-S-cursor-todo-selection-as-state-change)))
19228 (org-call-with-arg 'org-todo 'right)))
19229 ((or (and org-support-shift-select
19230 (not (eq org-support-shift-select 'always))
19231 (org-at-item-bullet-p))
19232 (and (not org-support-shift-select) (org-at-item-p)))
19233 (org-call-with-arg 'org-cycle-list-bullet nil))
19234 ((and (not (eq org-support-shift-select 'always))
19235 (org-at-property-p))
19236 (call-interactively 'org-property-next-allowed-value))
19237 ((org-clocktable-try-shift 'right arg))
19238 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
19239 (org-support-shift-select
19240 (org-call-for-shift-select 'forward-char))
19241 (t (org-shiftselect-error))))
19243 (defun org-shiftleft (&optional arg)
19244 "Cycle the thing at point or in the current line, depending on context.
19245 Depending on context, this does one of the following:
19247 - switch a timestamp at point one day into the past
19248 - on a headline, switch to the previous TODO keyword.
19249 - on an item, switch entire list to the previous bullet type
19250 - on a property line, switch to the previous allowed value
19251 - on a clocktable definition line, move time block into the past"
19254 ((run-hook-with-args-until-success 'org-shiftleft-hook))
19255 ((and org-support-shift-select (org-region-active-p))
19256 (org-call-for-shift-select 'backward-char))
19257 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
19258 ((and (not (eq org-support-shift-select 'always))
19259 (org-at-heading-p))
19260 (let ((org-inhibit-logging
19261 (not org-treat-S-cursor-todo-selection-as-state-change))
19262 (org-inhibit-blocking
19263 (not org-treat-S-cursor-todo-selection-as-state-change)))
19264 (org-call-with-arg 'org-todo 'left)))
19265 ((or (and org-support-shift-select
19266 (not (eq org-support-shift-select 'always))
19267 (org-at-item-bullet-p))
19268 (and (not org-support-shift-select) (org-at-item-p)))
19269 (org-call-with-arg 'org-cycle-list-bullet 'previous))
19270 ((and (not (eq org-support-shift-select 'always))
19271 (org-at-property-p))
19272 (call-interactively 'org-property-previous-allowed-value))
19273 ((org-clocktable-try-shift 'left arg))
19274 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
19275 (org-support-shift-select
19276 (org-call-for-shift-select 'backward-char))
19277 (t (org-shiftselect-error))))
19279 (defun org-shiftcontrolright ()
19280 "Switch to next TODO set."
19283 ((and org-support-shift-select (org-region-active-p))
19284 (org-call-for-shift-select 'forward-word))
19285 ((and (not (eq org-support-shift-select 'always))
19286 (org-at-heading-p))
19287 (org-call-with-arg 'org-todo 'nextset))
19288 (org-support-shift-select
19289 (org-call-for-shift-select 'forward-word))
19290 (t (org-shiftselect-error))))
19292 (defun org-shiftcontrolleft ()
19293 "Switch to previous TODO set."
19296 ((and org-support-shift-select (org-region-active-p))
19297 (org-call-for-shift-select 'backward-word))
19298 ((and (not (eq org-support-shift-select 'always))
19299 (org-at-heading-p))
19300 (org-call-with-arg 'org-todo 'previousset))
19301 (org-support-shift-select
19302 (org-call-for-shift-select 'backward-word))
19303 (t (org-shiftselect-error))))
19305 (defun org-shiftcontrolup (&optional n)
19306 "Change timestamps synchronously up in CLOCK log lines.
19307 Optional argument N tells to change by that many units."
19309 (cond ((and (not org-support-shift-select)
19310 (org-at-clock-log-p)
19311 (org-at-timestamp-p t))
19312 (org-clock-timestamps-up n))
19313 (t (org-shiftselect-error))))
19315 (defun org-shiftcontroldown (&optional n)
19316 "Change timestamps synchronously down in CLOCK log lines.
19317 Optional argument N tells to change by that many units."
19319 (cond ((and (not org-support-shift-select)
19320 (org-at-clock-log-p)
19321 (org-at-timestamp-p t))
19322 (org-clock-timestamps-down n))
19323 (t (org-shiftselect-error))))
19325 (defun org-ctrl-c-ret ()
19326 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
19329 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
19330 (t (call-interactively 'org-insert-heading))))
19332 (defun org-find-visible ()
19334 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
19335 (get-char-property s 'invisible)))
19337 (defun org-find-invisible ()
19339 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
19340 (not (get-char-property s 'invisible))))
19343 (defun org-copy-visible (beg end)
19344 "Copy the visible parts of the region."
19349 (narrow-to-region beg end)
19350 (setq s (goto-char (point-min)))
19351 (while (not (= (point) (point-max)))
19352 (goto-char (org-find-invisible))
19353 (push (buffer-substring s (point)) snippets)
19354 (setq s (goto-char (org-find-visible))))))
19355 (kill-new (apply 'concat (nreverse snippets)))))
19357 (defun org-copy-special ()
19358 "Copy region in table or copy current subtree.
19359 Calls `org-table-copy' or `org-copy-subtree', depending on context.
19360 See the individual commands for more information."
19362 (call-interactively
19363 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
19365 (defun org-cut-special ()
19366 "Cut region in table or cut current subtree.
19367 Calls `org-table-copy' or `org-cut-subtree', depending on context.
19368 See the individual commands for more information."
19370 (call-interactively
19371 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
19373 (defun org-paste-special (arg)
19374 "Paste rectangular region into table, or past subtree relative to level.
19375 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
19376 See the individual commands for more information."
19378 (if (org-at-table-p)
19379 (org-table-paste-rectangle)
19380 (org-paste-subtree arg)))
19382 (defsubst org-in-fixed-width-region-p ()
19383 "Is point in a fixed-width region?"
19385 (eq 'fixed-width (org-element-type (org-element-at-point)))))
19387 (defun org-edit-special (&optional arg)
19388 "Call a special editor for the stuff at point.
19389 When at a table, call the formula editor with `org-table-edit-formulas'.
19390 When in a source code block, call `org-edit-src-code'.
19391 When in a fixed-width region, call `org-edit-fixed-width-region'.
19392 When in an #+include line, visit the included file.
19393 On a link, call `ffap' to visit the link at point.
19394 Otherwise, return a user error."
19396 ;; possibly prep session before editing source
19397 (when (and (org-in-src-block-p) arg)
19398 (let* ((info (org-babel-get-src-block-info))
19399 (lang (nth 0 info))
19400 (params (nth 2 info))
19401 (session (cdr (assoc :session params))))
19402 (when (and info session) ;; we are in a source-code block with a session
19404 (intern (concat "org-babel-prep-session:" lang)) session params))))
19405 (cond ;; proceed with `org-edit-special'
19407 (beginning-of-line 1)
19408 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
19409 (find-file (org-trim (match-string 1))))
19410 ((or (org-at-table-p)
19412 (beginning-of-line 1)
19413 (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
19414 (call-interactively 'org-table-edit-formulas))
19415 ((or (org-in-block-p '("src" "example" "latex" "html"))
19416 (org-at-table.el-p))
19417 (org-edit-src-code))
19418 ((org-in-fixed-width-region-p) (org-edit-fixed-width-region))
19419 ((org-at-regexp-p org-any-link-re) (call-interactively 'ffap))
19420 (t (user-error "No special environment to edit here"))))
19422 (defvar org-table-coordinate-overlays) ; defined in org-table.el
19423 (defun org-ctrl-c-ctrl-c (&optional arg)
19424 "Set tags in headline, or update according to changed information at point.
19426 This command does many different things, depending on context:
19428 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
19429 this is what we do.
19431 - If the cursor is on a statistics cookie, update it.
19433 - If the cursor is in a headline, prompt for tags and insert them
19434 into the current line, aligned to `org-tags-column'. When called
19435 with prefix arg, realign all tags in the current buffer.
19437 - If the cursor is in one of the special #+KEYWORD lines, this
19438 triggers scanning the buffer for these lines and updating the
19441 - If the cursor is inside a table, realign the table. This command
19442 works even if the automatic table editor has been turned off.
19444 - If the cursor is on a #+TBLFM line, re-apply the formulas to
19447 - If the cursor is at a footnote reference or definition, jump to
19448 the corresponding definition or references, respectively.
19450 - If the cursor is a the beginning of a dynamic block, update it.
19452 - If the current buffer is a capture buffer, close note and file it.
19454 - If the cursor is on a <<<target>>>, update radio targets and
19455 corresponding links in this buffer.
19457 - If the cursor is on a numbered item in a plain list, renumber the
19460 - If the cursor is on a checkbox, toggle it.
19462 - If the cursor is on a code block, evaluate it. The variable
19463 `org-confirm-babel-evaluate' can be used to control prompting
19464 before code block evaluation, by default every code block
19465 evaluation requires confirmation. Code block evaluation can be
19466 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
19468 (let ((org-enable-table-editor t))
19470 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
19471 org-occur-highlights
19472 org-latex-fragment-image-overlays)
19473 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
19474 (org-remove-occur-highlights)
19475 (org-remove-latex-fragment-image-overlays)
19476 (message "Temporary highlights/overlays removed from current buffer"))
19477 ((and (local-variable-p 'org-finish-function (current-buffer))
19478 (fboundp org-finish-function))
19479 (funcall org-finish-function))
19480 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
19481 ((org-in-regexp org-ts-regexp-both)
19482 (org-timestamp-change 0 'day))
19483 ((or (looking-at org-property-start-re)
19484 (org-at-property-p))
19485 (call-interactively 'org-property-action))
19486 ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
19487 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
19488 (or (org-at-heading-p) (org-at-item-p)))
19489 (call-interactively 'org-update-statistics-cookies))
19490 ((org-at-heading-p) (call-interactively 'org-set-tags))
19491 ((org-at-table.el-p)
19492 (message "Use C-c ' to edit table.el tables"))
19494 (org-table-maybe-eval-formula)
19496 (call-interactively 'org-table-recalculate)
19497 (org-table-maybe-recalculate-line))
19498 (call-interactively 'org-table-align)
19499 (orgtbl-send-table 'maybe))
19500 ((or (org-footnote-at-reference-p)
19501 (org-footnote-at-definition-p))
19502 (call-interactively 'org-footnote-action))
19503 ((org-at-item-checkbox-p)
19504 ;; Cursor at a checkbox: repair list and update checkboxes. Send
19505 ;; list only if at top item.
19506 (let* ((cbox (match-string 1))
19507 (struct (org-list-struct))
19508 (old-struct (copy-tree struct))
19509 (parents (org-list-parents-alist struct))
19510 (orderedp (org-entry-get nil "ORDERED"))
19511 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19513 ;; Use a light version of `org-toggle-checkbox' to avoid
19514 ;; computing list structure twice.
19515 (let ((new-box (cond
19516 ((equal arg '(16)) "[-]")
19517 ((equal arg '(4)) nil)
19518 ((equal "[X]" cbox) "[ ]")
19520 (if (and firstp arg)
19521 ;; If at first item of sub-list, remove check-box from
19522 ;; every item at the same level.
19524 (lambda (pos) (org-list-set-checkbox pos struct new-box))
19525 (org-list-get-all-items
19526 (point-at-bol) struct (org-list-prevs-alist struct)))
19527 (org-list-set-checkbox (point-at-bol) struct new-box)))
19528 ;; Replicate `org-list-write-struct', while grabbing a return
19529 ;; value from `org-list-struct-fix-box'.
19530 (org-list-struct-fix-ind struct parents 2)
19531 (org-list-struct-fix-item-end struct)
19532 (let ((prevs (org-list-prevs-alist struct)))
19533 (org-list-struct-fix-bul struct prevs)
19534 (org-list-struct-fix-ind struct parents)
19536 (org-list-struct-fix-box struct parents prevs orderedp)))
19537 (if (equal struct old-struct)
19538 (user-error "Cannot toggle this checkbox (unchecked subitems?)")
19539 (org-list-struct-apply-struct struct old-struct)
19540 (org-update-checkbox-count-maybe))
19543 "Checkboxes were removed due to unchecked box at line %d"
19544 (org-current-line block-item)))
19545 (when firstp (org-list-send-list 'maybe))))
19547 ;; Cursor at an item: repair list. Do checkbox related actions
19548 ;; only if function was called with an argument. Send list only
19550 (let* ((struct (org-list-struct))
19551 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19554 (setq old-struct (copy-tree struct))
19556 ;; If at first item of sub-list, add check-box to every
19557 ;; item at the same level.
19560 (unless (org-list-get-checkbox pos struct)
19561 (org-list-set-checkbox pos struct "[ ]")))
19562 (org-list-get-all-items
19563 (point-at-bol) struct (org-list-prevs-alist struct)))
19564 (org-list-set-checkbox (point-at-bol) struct "[ ]")))
19565 (org-list-write-struct
19566 struct (org-list-parents-alist struct) old-struct)
19567 (when arg (org-update-checkbox-count-maybe))
19568 (when firstp (org-list-send-list 'maybe))))
19569 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
19571 (beginning-of-line 1)
19572 (save-excursion (org-update-dblock)))
19574 (let ((case-fold-search t))
19575 (beginning-of-line 1)
19576 (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
19578 ((or (equal (match-string 1) "TBLFM")
19579 (equal (match-string 1) "tblfm"))
19580 ;; Recalculate the table before this line
19582 (beginning-of-line 1)
19583 (skip-chars-backward " \r\n\t")
19584 (if (org-at-table-p)
19585 (org-call-with-arg 'org-table-recalculate (or arg t)))))
19587 (let ((org-inhibit-startup-visibility-stuff t)
19588 (org-startup-align-all-tables nil))
19589 (when (boundp 'org-table-coordinate-overlays)
19590 (mapc 'delete-overlay org-table-coordinate-overlays)
19591 (setq org-table-coordinate-overlays nil))
19592 (org-save-outline-visibility 'use-markers (org-mode-restart)))
19593 (message "Local setup has been refreshed"))))
19594 ((org-clock-update-time-maybe))
19596 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
19597 (error "C-c C-c can do nothing useful at this location"))))))
19599 (defun org-mode-restart ()
19600 "Restart Org-mode, to scan again for special lines.
19601 Also updates the keyword regular expressions."
19604 (message "Org-mode restarted"))
19606 (defun org-kill-note-or-show-branches ()
19607 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
19609 (if (not org-finish-function)
19612 (call-interactively 'show-branches))
19613 (let ((org-note-abort t))
19614 (funcall org-finish-function))))
19616 (defun org-return (&optional indent)
19617 "Goto next table row or insert a newline.
19618 Calls `org-table-next-row' or `newline', depending on context.
19619 See the individual commands for more information."
19623 ((or (bobp) (org-in-src-block-p))
19624 (if indent (newline-and-indent) (newline)))
19626 (org-table-justify-field-maybe)
19627 (call-interactively 'org-table-next-row))
19628 ;; when `newline-and-indent' is called within a list, make sure
19629 ;; text moved stays inside the item.
19630 ((and (org-in-item-p) indent)
19631 (if (and (org-at-item-p) (>= (point) (match-end 0)))
19633 (save-match-data (newline))
19634 (org-indent-line-to (length (match-string 0))))
19635 (let ((ind (org-get-indentation)))
19637 (if (org-looking-back org-list-end-re)
19639 (org-indent-line-to ind)))))
19640 ((and org-return-follows-link
19641 (org-at-timestamp-p t)
19642 (not (eq org-ts-what 'after)))
19643 (org-follow-timestamp-link))
19644 ((and org-return-follows-link
19645 (let ((tprop (get-text-property (point) 'face)))
19646 (or (eq tprop 'org-link)
19647 (and (listp tprop) (memq 'org-link tprop)))))
19648 (call-interactively 'org-open-at-point))
19649 ((and (org-at-heading-p)
19651 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
19655 (t (if indent (newline-and-indent) (newline))))))
19657 (defun org-return-indent ()
19658 "Goto next table row or insert a newline and indent.
19659 Calls `org-table-next-row' or `newline-and-indent', depending on
19660 context. See the individual commands for more information."
19664 (defun org-ctrl-c-star ()
19665 "Compute table, or change heading status of lines.
19666 Calls `org-table-recalculate' or `org-toggle-heading',
19667 depending on context."
19671 (call-interactively 'org-table-recalculate))
19673 ;; Convert all lines in region to list items
19674 (call-interactively 'org-toggle-heading))))
19676 (defun org-ctrl-c-minus ()
19677 "Insert separator line in table or modify bullet status of line.
19678 Also turns a plain line or a region of lines into list items.
19679 Calls `org-table-insert-hline', `org-toggle-item', or
19680 `org-cycle-list-bullet', depending on context."
19684 (call-interactively 'org-table-insert-hline))
19685 ((org-region-active-p)
19686 (call-interactively 'org-toggle-item))
19688 (call-interactively 'org-cycle-list-bullet))
19690 (call-interactively 'org-toggle-item))))
19692 (defun org-toggle-item (arg)
19693 "Convert headings or normal lines to items, items to normal lines.
19694 If there is no active region, only the current line is considered.
19696 If the first non blank line in the region is an headline, convert
19697 all headlines to items, shifting text accordingly.
19699 If it is an item, convert all items to normal lines.
19701 If it is normal text, change region into an item. With a prefix
19702 argument ARG, change each line in region into an item."
19706 ;; Shift text in current section to IND, from point to END.
19707 ;; The function leaves point to END line.
19709 (let ((min-i 1000) (end (copy-marker end)))
19710 ;; First determine the minimum indentation (MIN-I) of
19714 (while (< (point) end)
19715 (let ((i (org-get-indentation)))
19717 ;; Skip blank lines and inline tasks.
19718 ((looking-at "^[ \t]*$"))
19719 ((looking-at org-outline-regexp-bol))
19720 ;; We can't find less than 0 indentation.
19721 ((zerop i) (throw 'exit (setq min-i 0)))
19722 ((< i min-i) (setq min-i i))))
19724 ;; Then indent each line so that a line indented to
19725 ;; MIN-I becomes indented to IND. Ignore blank lines
19726 ;; and inline tasks in the process.
19727 (let ((delta (- ind min-i)))
19728 (while (< (point) end)
19729 (unless (or (looking-at "^[ \t]*$")
19730 (looking-at org-outline-regexp-bol))
19731 (org-indent-line-to (+ (org-get-indentation) delta)))
19732 (forward-line)))))))
19735 ;; Return beginning of first non-blank line, starting from
19740 (skip-chars-forward " \r\t\n")
19743 ;; Determine boundaries of changes.
19744 (if (org-region-active-p)
19745 (setq beg (funcall skip-blanks (region-beginning))
19746 end (copy-marker (region-end)))
19747 (setq beg (funcall skip-blanks (point-at-bol))
19748 end (copy-marker (point-at-eol))))
19749 ;; Depending on the starting line, choose an action on the text
19750 ;; between BEG and END.
19751 (org-with-limited-levels
19755 ;; Case 1. Start at an item: de-itemize. Note that it only
19756 ;; happens when a region is active: `org-ctrl-c-minus'
19757 ;; would call `org-cycle-list-bullet' otherwise.
19759 (while (< (point) end)
19760 (when (org-at-item-p)
19761 (skip-chars-forward " \t")
19762 (delete-region (point) (match-end 0)))
19764 ;; Case 2. Start at an heading: convert to items.
19765 ((org-at-heading-p)
19766 (let* ((bul (org-list-bullet-string "-"))
19767 (bul-len (length bul))
19768 ;; Indentation of the first heading. It should be
19769 ;; relative to the indentation of its parent, if any.
19770 (start-ind (save-excursion
19772 ((not org-adapt-indentation) 0)
19773 ((not (outline-previous-heading)) 0)
19774 (t (length (match-string 0))))))
19775 ;; Level of first heading. Further headings will be
19776 ;; compared to it to determine hierarchy in the list.
19777 (ref-level (org-reduced-level (org-outline-level))))
19778 (while (< (point) end)
19779 (let* ((level (org-reduced-level (org-outline-level)))
19780 (delta (max 0 (- level ref-level))))
19781 ;; If current headline is less indented than the first
19782 ;; one, set it as reference, in order to preserve
19784 (when (< level ref-level) (setq ref-level level))
19785 (replace-match bul t t)
19786 (org-indent-line-to (+ start-ind (* delta bul-len)))
19787 ;; Ensure all text down to END (or SECTION-END) belongs
19788 ;; to the newly created item.
19789 (let ((section-end (save-excursion
19790 (or (outline-next-heading) (point)))))
19792 (funcall shift-text
19793 (+ start-ind (* (1+ delta) bul-len))
19794 (min end section-end)))))))
19795 ;; Case 3. Normal line with ARG: turn each non-item line into
19798 (while (< (point) end)
19799 (unless (or (org-at-heading-p) (org-at-item-p))
19800 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
19802 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
19804 ;; Case 4. Normal line without ARG: make the first line of
19805 ;; region an item, and shift indentation of others
19806 ;; lines to set them as item's body.
19807 (t (let* ((bul (org-list-bullet-string "-"))
19808 (bul-len (length bul))
19809 (ref-ind (org-get-indentation)))
19810 (skip-chars-forward " \t")
19813 (while (< (point) end)
19814 ;; Ensure that lines less indented than first one
19815 ;; still get included in item body.
19816 (funcall shift-text
19817 (+ ref-ind bul-len)
19818 (min end (save-excursion (or (outline-next-heading)
19820 (forward-line)))))))))
19822 (defun org-toggle-heading (&optional nstars)
19823 "Convert headings to normal text, or items or text to headings.
19824 If there is no active region, only the current line is considered.
19826 With a \\[universal-argument] prefix, convert the whole list at
19827 point into heading.
19831 - If the first non blank line is an headline, remove the stars
19832 from all headlines in the region.
19834 - If it is a normal line turn each and every normal line (i.e. not an
19835 heading or an item) in the region into a heading.
19837 - If it is a plain list item, turn all plain list items into headings.
19839 When converting a line into a heading, the number of stars is chosen
19840 such that the lines become children of the current entry. However,
19841 when a prefix argument is given, its value determines the number of
19846 ;; Return beginning of first non-blank line, starting from
19851 (while (org-at-comment-p) (forward-line))
19852 (skip-chars-forward " \r\t\n")
19855 ;; Determine boundaries of changes. If a universal prefix has
19856 ;; been given, put the list in a region. If region ends at a bol,
19857 ;; do not consider the last line to be in the region.
19859 (when (and current-prefix-arg (org-at-item-p))
19860 (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
19861 (org-mark-element))
19863 (if (org-region-active-p)
19864 (setq beg (funcall skip-blanks (region-beginning))
19865 end (copy-marker (save-excursion
19866 (goto-char (region-end))
19867 (if (bolp) (point) (point-at-eol)))))
19868 (setq beg (funcall skip-blanks (point-at-bol))
19869 end (copy-marker (point-at-eol))))
19870 ;; Ensure inline tasks don't count as headings.
19871 (org-with-limited-levels
19875 ;; Case 1. Started at an heading: de-star headings.
19876 ((org-at-heading-p)
19877 (while (< (point) end)
19878 (when (org-at-heading-p t)
19879 (looking-at org-outline-regexp) (replace-match "")
19882 ;; Case 2. Started at an item: change items into headlines.
19883 ;; One star will be added by `org-list-to-subtree'.
19885 (let* ((stars (make-string
19887 ;; subtract the star that will be added again by
19888 ;; `org-list-to-subtree'
19889 (1- (prefix-numeric-value current-prefix-arg))
19890 (or (org-current-level) 0))
19893 (cond (nstars "") ; stars from prefix only
19894 ((equal stars "") "") ; before first heading
19895 (org-odd-levels-only "*") ; inside heading, odd
19896 (t "")))) ; inside heading, oddeven
19897 (while (< (point) end)
19898 (when (org-at-item-p)
19899 ;; Pay attention to cases when region ends before list.
19900 (let* ((struct (org-list-struct))
19901 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
19903 (narrow-to-region (point) list-end)
19905 (org-list-to-subtree
19906 (org-list-parse-list t)
19907 '(:istart (concat stars add-stars (funcall get-stars depth))
19908 :icount (concat stars add-stars (funcall get-stars depth)))))))
19911 ;; Case 3. Started at normal text: make every line an heading,
19912 ;; skipping headlines and items.
19913 (t (let* ((stars (make-string
19915 (prefix-numeric-value current-prefix-arg)
19916 (or (org-current-level) 0))
19919 (cond (nstars "") ; stars from prefix only
19920 ((equal stars "") "*") ; before first heading
19921 (org-odd-levels-only "**") ; inside heading, odd
19922 (t "*"))) ; inside heading, oddeven
19923 (rpl (concat stars add-stars " ")))
19924 (while (< (point) end)
19925 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
19926 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
19927 (replace-match (concat rpl (match-string 2))) (setq toggled t))
19928 (forward-line)))))))
19929 (unless toggled (message "Cannot toggle heading from here"))))
19931 (defun org-meta-return (&optional arg)
19932 "Insert a new heading or wrap a region in a table.
19933 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
19934 See the individual commands for more information."
19937 ((run-hook-with-args-until-success 'org-metareturn-hook))
19938 ((or (org-at-drawer-p) (org-at-property-p))
19939 (newline-and-indent))
19941 (call-interactively 'org-table-wrap-region))
19942 (t (call-interactively 'org-insert-heading))))
19946 (defsubst org-in-subtree-not-table-p ()
19947 "Are we in a subtree and not in a table?"
19948 (and (not (org-before-first-heading-p))
19949 (not (org-at-table-p))))
19951 ;; Define the Org-mode menus
19952 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
19954 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
19955 ["Next Field" org-cycle (org-at-table-p)]
19956 ["Previous Field" org-shifttab (org-at-table-p)]
19957 ["Next Row" org-return (org-at-table-p)]
19959 ["Blank Field" org-table-blank-field (org-at-table-p)]
19960 ["Edit Field" org-table-edit-field (org-at-table-p)]
19961 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
19964 ["Move Column Left" org-metaleft (org-at-table-p)]
19965 ["Move Column Right" org-metaright (org-at-table-p)]
19966 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
19967 ["Insert Column" org-shiftmetaright (org-at-table-p)])
19969 ["Move Row Up" org-metaup (org-at-table-p)]
19970 ["Move Row Down" org-metadown (org-at-table-p)]
19971 ["Delete Row" org-shiftmetaup (org-at-table-p)]
19972 ["Insert Row" org-shiftmetadown (org-at-table-p)]
19973 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
19975 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
19977 ["Copy Rectangle" org-copy-special (org-at-table-p)]
19978 ["Cut Rectangle" org-cut-special (org-at-table-p)]
19979 ["Paste Rectangle" org-paste-special (org-at-table-p)]
19980 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
19983 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
19984 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
19985 ["Edit Formulas" org-edit-special (org-at-table-p)]
19987 ["Recalculate line" org-table-recalculate (org-at-table-p)]
19988 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
19989 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
19991 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
19993 ["Sum Column/Rectangle" org-table-sum
19994 (or (org-at-table-p) (org-region-active-p))]
19995 ["Which Column?" org-table-current-column (org-at-table-p)])
19997 org-table-toggle-formula-debugger
19998 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
19999 ["Show Col/Row Numbers"
20000 org-table-toggle-coordinate-overlays
20002 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
20004 ["Create" org-table-create (and (not (org-at-table-p))
20005 org-enable-table-editor)]
20006 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
20007 ["Import from File" org-table-import (not (org-at-table-p))]
20008 ["Export to File" org-table-export (org-at-table-p)]
20010 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
20012 (easy-menu-define org-org-menu org-mode-map "Org menu"
20015 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
20016 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
20017 ["Sparse Tree..." org-sparse-tree t]
20018 ["Reveal Context" org-reveal t]
20019 ["Show All" show-all t]
20021 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
20023 ["New Heading" org-insert-heading t]
20024 ("Navigate Headings"
20025 ["Up" outline-up-heading t]
20026 ["Next" outline-next-visible-heading t]
20027 ["Previous" outline-previous-visible-heading t]
20028 ["Next Same Level" outline-forward-same-level t]
20029 ["Previous Same Level" outline-backward-same-level t]
20031 ["Jump" org-goto t])
20033 ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
20035 ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
20036 ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
20038 ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
20039 ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
20040 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
20042 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
20044 ["Copy visible text" org-copy-visible t]
20046 ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
20047 ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
20048 ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
20049 ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
20051 ["Sort Region/Children" org-sort t]
20053 ["Convert to odd levels" org-convert-to-odd-levels t]
20054 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
20056 ["Emphasis..." org-emphasize t]
20057 ["Edit Source Example" org-edit-special t]
20059 ["Footnote new/jump" org-footnote-action t]
20060 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
20062 ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
20064 ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
20065 ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
20066 ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
20070 ["Store Link (Global)" org-store-link t]
20071 ["Find existing link to here" org-occur-link-in-agenda-files t]
20072 ["Insert Link" org-insert-link t]
20073 ["Follow Link" org-open-at-point t]
20075 ["Next link" org-next-link t]
20076 ["Previous link" org-previous-link t]
20078 ["Descriptive Links"
20079 org-toggle-link-display
20081 :selected org-descriptive-links
20084 org-toggle-link-display
20086 :selected (not org-descriptive-links)])
20089 ["TODO/DONE/-" org-todo t]
20091 ["Next keyword" org-shiftright (org-at-heading-p)]
20092 ["Previous keyword" org-shiftleft (org-at-heading-p)]
20093 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
20094 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
20095 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
20096 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
20097 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
20099 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
20100 :selected org-enforce-todo-dependencies :style toggle :active t]
20101 "Settings for tree at point"
20102 ["Do Children sequentially" org-toggle-ordered-property :style radio
20103 :selected (org-entry-get nil "ORDERED")
20104 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
20105 ["Do Children parallel" org-toggle-ordered-property :style radio
20106 :selected (not (org-entry-get nil "ORDERED"))
20107 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
20109 ["Set Priority" org-priority t]
20110 ["Priority Up" org-shiftup t]
20111 ["Priority Down" org-shiftdown t]
20113 ["Get news from all feeds" org-feed-update-all t]
20114 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
20115 ["Customize feeds" (customize-variable 'org-feed-alist) t])
20116 ("TAGS and Properties"
20117 ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
20118 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
20120 ["Set property" org-set-property (not (org-before-first-heading-p))]
20121 ["Column view of properties" org-columns t]
20122 ["Insert Column View DBlock" org-insert-columns-dblock t])
20123 ("Dates and Scheduling"
20124 ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
20125 ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
20127 ["1 Day Later" org-shiftright (org-at-timestamp-p)]
20128 ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
20129 ["1 ... Later" org-shiftup (org-at-timestamp-p)]
20130 ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
20131 ["Compute Time Range" org-evaluate-time-range t]
20132 ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
20133 ["Deadline" org-deadline (not (org-before-first-heading-p))]
20135 ["Custom time format" org-toggle-time-stamp-overlays
20136 :style radio :selected org-display-custom-times]
20138 ["Goto Calendar" org-goto-calendar t]
20139 ["Date from Calendar" org-date-from-calendar t]
20141 ["Start/Restart Timer" org-timer-start t]
20142 ["Pause/Continue Timer" org-timer-pause-or-continue t]
20143 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
20144 ["Insert Timer String" org-timer t]
20145 ["Insert Timer Item" org-timer-item t])
20147 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
20148 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
20149 ["Clock out" org-clock-out t]
20150 ["Clock cancel" org-clock-cancel t]
20152 ["Mark as default task" org-clock-mark-default-task t]
20153 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
20154 ["Goto running clock" org-clock-goto t]
20156 ["Display times" org-clock-display t]
20157 ["Create clock table" org-clock-report t]
20159 ["Record DONE time"
20160 (progn (setq org-log-done (not org-log-done))
20161 (message "Switching to %s will %s record a timestamp"
20162 (car org-done-keywords)
20163 (if org-log-done "automatically" "not")))
20164 :style toggle :selected org-log-done])
20166 ["Agenda Command..." org-agenda t]
20167 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
20168 ("File List for Agenda")
20169 ("Special views current file"
20170 ["TODO Tree" org-show-todo-tree t]
20171 ["Check Deadlines" org-check-deadlines t]
20172 ["Timeline" org-timeline t]
20173 ["Tags/Property tree" org-match-sparse-tree t])
20175 ["Export/Publish..." org-export t]
20177 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
20178 :selected org-cdlatex-mode]
20179 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
20180 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
20181 ["Modify math symbol" org-cdlatex-math-modify
20182 (org-inside-LaTeX-fragment-p)]
20183 ["Insert citation" org-reftex-citation t]
20185 ["Template for BEAMER" (progn (require 'org-beamer)
20186 (org-insert-beamer-options-template)) t])
20189 ["Push Files and Views" org-mobile-push t]
20190 ["Get Captured and Flagged" org-mobile-pull t]
20191 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
20193 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
20196 ["Show Version" org-version t]
20197 ["Info Documentation" org-info t])
20199 ["Browse Org Group" org-customize t]
20201 ["Expand This Menu" org-create-customize-menu
20202 (fboundp 'customize-menu-create)])
20203 ["Send bug report" org-submit-bug-report t]
20206 ["Refresh setup current buffer" org-mode-restart t]
20207 ["Reload Org (after update)" org-reload t]
20208 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
20211 (defun org-info (&optional node)
20212 "Read documentation for Org-mode in the info system.
20213 With optional NODE, go directly to that node."
20215 (info (format "(org)%s" (or node ""))))
20218 (defun org-submit-bug-report ()
20219 "Submit a bug report on Org-mode via mail.
20221 Don't hesitate to report any problems or inaccurate documentation.
20223 If you don't have setup sending mail from (X)Emacs, please copy the
20224 output buffer into your mail program, as it gives us important
20225 information about your Org-mode version and configuration."
20227 (require 'reporter)
20228 (org-load-modules-maybe)
20229 (org-require-autoloaded-modules)
20230 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
20231 (reporter-submit-bug-report
20232 "emacs-orgmode@gnu.org"
20233 (org-version nil 'full)
20235 (save-window-excursion
20236 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
20237 (delete-other-windows)
20239 (insert "You are about to submit a bug report to the Org-mode mailing list.
20241 We would like to add your full Org-mode and Outline configuration to the
20242 bug report. This greatly simplifies the work of the maintainer and
20243 other experts on the mailing list.
20245 HOWEVER, some variables you have customized may contain private
20246 information. The names of customers, colleagues, or friends, might
20247 appear in the form of file names, tags, todo states, or search strings.
20248 If you answer yes to the prompt, you might want to check and remove
20249 such private information before sending the email.")
20250 (add-text-properties (point-min) (point-max) '(face org-warning))
20251 (when (yes-or-no-p "Include your Org-mode configuration ")
20255 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
20256 (or (and (symbol-value v)
20257 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
20259 (get v 'custom-type) (get v 'standard-value)
20260 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
20262 (kill-buffer (get-buffer "*Warn about privacy*"))
20265 "Remember to cover the basics, that is, what you expected to happen and
20266 what in fact did happen. You don't know how to make a good report? See
20268 http://orgmode.org/manual/Feedback.html#Feedback
20270 Your bug report will be posted to the Org-mode mailing list.
20271 ------------------------------------------------------------------------")
20273 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
20274 (replace-match "\\1Bug: \\3 [\\2]")))))
20277 (defun org-install-agenda-files-menu ()
20278 (let ((bl (buffer-list)))
20281 (set-buffer (pop bl))
20282 (if (derived-mode-p 'org-mode) (setq bl nil)))
20283 (when (derived-mode-p 'org-mode)
20285 '("Org") "File List for Agenda"
20288 ["Edit File List" (org-edit-agenda-file-list) t]
20289 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
20290 ["Remove Current File from List" org-remove-file t]
20291 ["Cycle through agenda files" org-cycle-agenda-files t]
20292 ["Occur in all agenda files" org-occur-in-agenda-files t]
20294 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
20298 (defun org-require-autoloaded-modules ()
20301 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
20302 org-docbook org-exp org-html org-icalendar
20304 org-publish org-remember org-table
20305 org-timer org-xoxo)))
20308 (defun org-reload (&optional uncompiled)
20309 "Reload all org lisp files.
20310 With prefix arg UNCOMPILED, load the uncompiled versions."
20312 (require 'loadhist)
20313 (let* ((org-dir (org-find-library-dir "org"))
20314 (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
20315 (feature-re "^\\(org\\|ob\\)\\(-.*\\)?")
20316 (remove-re (mapconcat 'identity
20317 (mapcar (lambda (f) (concat "^" f "$"))
20318 (list (if (featurep 'xemacs)
20320 "org-colview-xemacs")
20321 "org" "org-loaddefs" "org-version"))
20323 (feats (delete-dups
20324 (mapcar 'file-name-sans-extension
20325 (mapcar 'file-name-nondirectory
20327 (mapcar 'feature-file
20334 (if (and (string-match feature-re f)
20335 (not (string-match remove-re f)))
20339 (list "org-version" "org")))
20340 (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
20341 load-uncore load-misses)
20344 (mapcar (lambda (f)
20345 (or (load (concat org-dir f) 'noerror nil nil 'mustsuffix)
20346 (and (string= org-dir contrib-dir)
20347 (load (concat contrib-dir f) 'noerror nil nil 'mustsuffix))
20348 (and (load (concat (org-find-library-dir f) f) 'noerror nil nil 'mustsuffix)
20349 (add-to-list 'load-uncore f 'append)
20354 (message "The following feature%s found in load-path, please check if that's correct:\n%s"
20355 (if (> (length load-uncore) 1) "s were" " was") load-uncore))
20357 (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
20358 (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
20359 (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
20362 (defun org-customize ()
20363 "Call the customize function with org as argument."
20365 (org-load-modules-maybe)
20366 (org-require-autoloaded-modules)
20367 (customize-browse 'org))
20369 (defun org-create-customize-menu ()
20370 "Create a full customization menu for Org-mode, insert it into the menu."
20372 (org-load-modules-maybe)
20373 (org-require-autoloaded-modules)
20374 (if (fboundp 'customize-menu-create)
20377 '("Org") "Customize"
20378 `(["Browse Org group" org-customize t]
20380 ,(customize-menu-create 'org)
20381 ["Set" Custom-set t]
20382 ["Save" Custom-save t]
20383 ["Reset to Current" Custom-reset-current t]
20384 ["Reset to Saved" Custom-reset-saved t]
20385 ["Reset to Standard Settings" Custom-reset-standard t]))
20386 (message "\"Org\"-menu now contains full customization menu"))
20387 (error "Cannot expand menu (outdated version of cus-edit.el)")))
20389 ;;;; Miscellaneous stuff
20391 ;;; Generally useful functions
20393 (defun org-get-at-bol (property)
20394 "Get text property PROPERTY at beginning of line."
20395 (get-text-property (point-at-bol) property))
20397 (defun org-find-text-property-in-string (prop s)
20398 "Return the first non-nil value of property PROP in string S."
20399 (or (get-text-property 0 prop s)
20400 (get-text-property (or (next-single-property-change 0 prop s) 0)
20403 (defun org-display-warning (message) ;; Copied from Emacs-Muse
20404 "Display the given MESSAGE as a warning."
20405 (if (fboundp 'display-warning)
20406 (display-warning 'org message
20407 (if (featurep 'xemacs) 'warning :warning))
20408 (let ((buf (get-buffer-create "*Org warnings*")))
20409 (with-current-buffer buf
20410 (goto-char (point-max))
20411 (insert "Warning (Org): " message)
20414 (display-buffer buf)
20417 (defun org-eval (form)
20418 "Eval FORM and return result."
20419 (condition-case error
20421 (error (format "%%![Error: %s]" error))))
20423 (defun org-in-clocktable-p ()
20424 "Check if the cursor is in a clocktable."
20425 (let ((pos (point)) start)
20428 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
20429 (setq start (match-beginning 0))
20430 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
20431 (>= (match-end 0) pos)
20434 (defun org-in-commented-line ()
20435 "Is point in a line starting with `#'?"
20436 (equal (char-after (point-at-bol)) ?#))
20438 (defun org-in-indented-comment-line ()
20439 "Is point in a line starting with `#' after some white space?"
20442 (goto-char (point-at-bol))
20443 (looking-at "[ \t]*#"))))
20445 (defun org-in-verbatim-emphasis ()
20447 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
20449 (defun org-goto-marker-or-bmk (marker &optional bookmark)
20450 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
20451 (if (and marker (marker-buffer marker)
20452 (buffer-live-p (marker-buffer marker)))
20454 (org-pop-to-buffer-same-window (marker-buffer marker))
20455 (if (or (> marker (point-max)) (< marker (point-min)))
20458 (org-show-context 'org-goto))
20460 (bookmark-jump bookmark)
20461 (error "Cannot find location"))))
20463 (defun org-quote-csv-field (s)
20464 "Quote field for inclusion in CSV material."
20465 (if (string-match "[\",]" s)
20466 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
20469 (defun org-force-self-insert (N)
20470 "Needed to enforce self-insert under remapping."
20472 (self-insert-command N))
20474 (defun org-string-width (s)
20475 "Compute width of string, ignoring invisible characters.
20476 This ignores character with invisibility property `org-link', and also
20477 characters with property `org-cwidth', because these will become invisible
20478 upon the next fontification round."
20480 (when (or (eq t buffer-invisibility-spec)
20481 (assq 'org-link buffer-invisibility-spec))
20482 (while (setq b (text-property-any 0 (length s)
20483 'invisible 'org-link s))
20484 (setq s (concat (substring s 0 b)
20485 (substring s (or (next-single-property-change
20486 b 'invisible s) (length s)))))))
20487 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
20488 (setq s (concat (substring s 0 b)
20489 (substring s (or (next-single-property-change
20490 b 'org-cwidth s) (length s))))))
20491 (setq l (string-width s) b -1)
20492 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
20493 (setq l (- l (get-text-property b 'org-dwidth-n s))))
20496 (defun org-shorten-string (s maxlength)
20497 "Shorten string S so tht it is no longer than MAXLENGTH characters.
20498 If the string is shorter or has length MAXLENGTH, just return the
20499 original string. If it is longer, the functions finds a space in the
20500 string, breaks this string off at that locations and adds three dots
20501 as ellipsis. Including the ellipsis, the string will not be longer
20502 than MAXLENGTH. If finding a good breaking point in the string does
20503 not work, the string is just chopped off in the middle of a word
20505 (if (<= (length s) maxlength)
20507 (let* ((n (max (- maxlength 4) 1))
20508 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
20509 (if (string-match re s)
20510 (concat (match-string 1 s) "...")
20511 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
20513 (defun org-get-indentation (&optional line)
20514 "Get the indentation of the current line, interpreting tabs.
20515 When LINE is given, assume it represents a line and compute its indentation."
20517 (if (string-match "^ *" (org-remove-tabs line))
20520 (beginning-of-line 1)
20521 (skip-chars-forward " \t")
20522 (current-column))))
20524 (defun org-get-string-indentation (s)
20525 "What indentation has S due to SPACE and TAB at the beginning of the string?"
20526 (let ((n -1) (i 0) (w tab-width) c)
20528 (while (< (setq n (1+ n)) (length s))
20529 (setq c (aref s n))
20530 (cond ((= c ?\ ) (setq i (1+ i)))
20531 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
20532 (t (throw 'exit t)))))
20535 (defun org-remove-tabs (s &optional width)
20536 "Replace tabulators in S with spaces.
20537 Assumes that s is a single line, starting in column 0."
20538 (setq width (or width tab-width))
20539 (while (string-match "\t" s)
20540 (setq s (replace-match
20542 (- (* width (/ (+ (match-beginning 0) width) width))
20543 (match-beginning 0)) ?\ )
20547 (defun org-fix-indentation (line ind)
20548 "Fix indentation in LINE.
20549 IND is a cons cell with target and minimum indentation.
20550 If the current indentation in LINE is smaller than the minimum,
20551 leave it alone. If it is larger than ind, set it to the target."
20552 (let* ((l (org-remove-tabs line))
20553 (i (org-get-indentation l))
20554 (i1 (car ind)) (i2 (cdr ind)))
20555 (if (>= i i2) (setq l (substring line i2)))
20557 (concat (make-string i1 ?\ ) l)
20560 (defun org-remove-indentation (code &optional n)
20561 "Remove the maximum common indentation from the lines in CODE.
20562 N may optionally be the number of spaces to remove."
20565 (org-do-remove-indentation n)
20568 (defun org-do-remove-indentation (&optional n)
20569 "Remove the maximum common indentation from the buffer."
20570 (untabify (point-min) (point-max))
20571 (let ((min 10000) re)
20574 (goto-char (point-min))
20575 (while (re-search-forward "^ *[^ \n]" nil t)
20576 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
20577 (unless (or (= min 0) (= min 10000))
20578 (setq re (format "^ \\{%d\\}" min))
20579 (goto-char (point-min))
20580 (while (re-search-forward re nil t)
20585 (defun org-fill-template (template alist)
20586 "Find each %key of ALIST in TEMPLATE and replace it."
20587 (let ((case-fold-search nil)
20589 (setq alist (sort (copy-sequence alist)
20590 (lambda (a b) (< (length (car a)) (length (car b))))))
20591 (while (setq entry (pop alist))
20593 (replace-regexp-in-string
20594 (concat "%" (regexp-quote (car entry)))
20595 (or (cdr entry) "") template t t)))
20598 (defun org-base-buffer (buffer)
20599 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
20602 (or (buffer-base-buffer buffer)
20605 (defun org-trim (s)
20606 "Remove whitespace at beginning and end of string."
20607 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
20608 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
20611 (defun org-wrap (string &optional width lines)
20612 "Wrap string to either a number of lines, or a width in characters.
20613 If WIDTH is non-nil, the string is wrapped to that width, however many lines
20614 that costs. If there is a word longer than WIDTH, the text is actually
20615 wrapped to the length of that word.
20616 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
20617 many lines, whatever width that takes.
20618 The return value is a list of lines, without newlines at the end."
20619 (let* ((words (org-split-string string "[ \t\n]+"))
20620 (maxword (apply 'max (mapcar 'org-string-width words)))
20623 (org-do-wrap words (max maxword width)))
20626 (setq ll (org-do-wrap words maxword))
20627 (if (<= (length ll) lines)
20630 (while (> (length ll) lines)
20632 (setq ll (org-do-wrap words w)))
20634 (t (error "Cannot wrap this")))))
20636 (defun org-do-wrap (words width)
20637 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
20640 (setq line (pop words))
20641 (while (and words (< (+ (length line) (length (car words))) width))
20642 (setq line (concat line " " (pop words))))
20643 (setq lines (push line lines)))
20646 (defun org-split-string (string &optional separators)
20647 "Splits STRING into substrings at SEPARATORS.
20648 No empty strings are returned if there are matches at the beginning
20649 and end of string."
20650 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
20654 (while (and (string-match rexp string
20656 (= start (match-beginning 0))
20657 (< start (length string)))
20659 (< (match-beginning 0) (length string)))
20661 (or (eq (match-beginning 0) 0)
20662 (and (eq (match-beginning 0) (match-end 0))
20663 (eq (match-beginning 0) start))
20665 (cons (substring string start (match-beginning 0))
20667 (setq start (match-end 0)))
20668 (or (eq start (length string))
20670 (cons (substring string start)
20674 (defun org-quote-vert (s)
20675 "Replace \"|\" with \"\\vert\"."
20676 (while (string-match "|" s)
20677 (setq s (replace-match "\\vert" t t s)))
20680 (defun org-uuidgen-p (s)
20681 "Is S an ID created by UUIDGEN?"
20682 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
20684 (defun org-in-src-block-p (&optional inside)
20685 "Whether point is in a code source block.
20686 When INSIDE is non-nil, don't consider we are within a src block
20687 when point is at #+BEGIN_SRC or #+END_SRC."
20688 (let ((case-fold-search t) ov)
20689 (or (and (setq ov (overlays-at (point)))
20690 (memq 'org-block-background
20691 (overlay-properties (car ov))))
20695 (beginning-of-line)
20696 (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
20698 (defun org-context ()
20699 "Return a list of contexts of the current cursor position.
20700 If several contexts apply, all are returned.
20701 Each context entry is a list with a symbol naming the context, and
20702 two positions indicating start and end of the context. Possible
20705 :headline anywhere in a headline
20706 :headline-stars on the leading stars in a headline
20707 :todo-keyword on a TODO keyword (including DONE) in a headline
20708 :tags on the TAGS in a headline
20709 :priority on the priority cookie in a headline
20710 :item on the first line of a plain list item
20711 :item-bullet on the bullet/number of a plain list item
20712 :checkbox on the checkbox in a plain list item
20713 :table in an org-mode table
20714 :table-special on a special filed in a table
20715 :table-table in a table.el table
20716 :clocktable in a clocktable
20717 :src-block in a source block
20718 :link on a hyperlink
20719 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
20720 :target on a <<target>>
20721 :radio-target on a <<<radio-target>>>
20722 :latex-fragment on a LaTeX fragment
20723 :latex-preview on a LaTeX fragment with overlaid preview image
20725 This function expects the position to be visible because it uses font-lock
20726 faces as a help to recognize the following contexts: :table-special, :link,
20728 (let* ((f (get-text-property (point) 'face))
20729 (faces (if (listp f) f (list f)))
20730 (case-fold-search t)
20731 (p (point)) clist o)
20732 ;; First the large context
20734 ((org-at-heading-p t)
20735 (push (list :headline (point-at-bol) (point-at-eol)) clist)
20737 (beginning-of-line 1)
20738 (looking-at org-todo-line-tags-regexp))
20739 (push (org-point-in-group p 1 :headline-stars) clist)
20740 (push (org-point-in-group p 2 :todo-keyword) clist)
20741 (push (org-point-in-group p 4 :tags) clist))
20743 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
20744 (if (looking-at "\\[#[A-Z0-9]\\]")
20745 (push (org-point-in-group p 0 :priority) clist)))
20748 (push (org-point-in-group p 2 :item-bullet) clist)
20749 (push (list :item (point-at-bol)
20750 (save-excursion (org-end-of-item) (point)))
20752 (and (org-at-item-checkbox-p)
20753 (push (org-point-in-group p 0 :checkbox) clist)))
20756 (push (list :table (org-table-begin) (org-table-end)) clist)
20757 (if (memq 'org-formula faces)
20758 (push (list :table-special
20759 (previous-single-property-change p 'face)
20760 (next-single-property-change p 'face)) clist)))
20761 ((org-at-table-p 'any)
20762 (push (list :table-table) clist)))
20765 (let ((case-fold-search t))
20766 ;; New the "medium" contexts: clocktables, source blocks
20767 (cond ((org-in-clocktable-p)
20768 (push (list :clocktable
20769 (and (or (looking-at "#\\+BEGIN: clocktable")
20770 (search-backward "#+BEGIN: clocktable" nil t))
20771 (match-beginning 0))
20772 (and (re-search-forward "#\\+END:?" nil t)
20773 (match-end 0))) clist))
20774 ((org-in-src-block-p)
20775 (push (list :src-block
20776 (and (or (looking-at "#\\+BEGIN_SRC")
20777 (search-backward "#+BEGIN_SRC" nil t))
20778 (match-beginning 0))
20779 (and (search-forward "#+END_SRC" nil t)
20780 (match-beginning 0))) clist))))
20783 ;; Now the small context
20785 ((org-at-timestamp-p)
20786 (push (org-point-in-group p 0 :timestamp) clist))
20787 ((memq 'org-link faces)
20789 (previous-single-property-change p 'face)
20790 (next-single-property-change p 'face)) clist))
20791 ((memq 'org-special-keyword faces)
20792 (push (list :keyword
20793 (previous-single-property-change p 'face)
20794 (next-single-property-change p 'face)) clist))
20796 (push (org-point-in-group p 0 :target) clist)
20797 (goto-char (1- (match-beginning 0)))
20798 (if (looking-at org-radio-target-regexp)
20799 (push (org-point-in-group p 0 :radio-target) clist))
20801 ((setq o (car (delq nil
20804 (if (memq x org-latex-fragment-image-overlays) x))
20805 (overlays-at (point))))))
20806 (push (list :latex-fragment
20807 (overlay-start o) (overlay-end o)) clist)
20808 (push (list :latex-preview
20809 (overlay-start o) (overlay-end o)) clist))
20810 ((org-inside-LaTeX-fragment-p)
20811 ;; FIXME: positions wrong.
20812 (push (list :latex-fragment (point) (point)) clist)))
20814 (setq clist (nreverse (delq nil clist)))
20817 ;; FIXME: Compare with at-regexp-p Do we need both?
20818 (defun org-in-regexp (re &optional nlines visually)
20819 "Check if point is inside a match of regexp.
20820 Normally only the current line is checked, but you can include NLINES extra
20821 lines both before and after point into the search.
20822 If VISUALLY is set, require that the cursor is not after the match but
20823 really on, so that the block visually is on the match."
20825 (let ((pos (point))
20826 (eol (point-at-eol (+ 1 (or nlines 0))))
20827 (inc (if visually 1 0)))
20829 (beginning-of-line (- 1 (or nlines 0)))
20830 (while (re-search-forward re eol t)
20831 (if (and (<= (match-beginning 0) pos)
20832 (>= (+ inc (match-end 0)) pos))
20833 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
20835 (defun org-at-regexp-p (regexp)
20836 "Is point inside a match of REGEXP in the current line?"
20839 (let ((pos (point)) (end (point-at-eol)))
20840 (beginning-of-line 1)
20841 (while (re-search-forward regexp end t)
20842 (if (and (<= (match-beginning 0) pos)
20843 (>= (match-end 0) pos))
20847 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
20848 "Non-nil when point is between matches of START-RE and END-RE.
20850 Also return a non-nil value when point is on one of the matches.
20852 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
20853 buffer positions. Default values are the positions of headlines
20854 surrounding the point.
20856 The functions returns a cons cell whose car (resp. cdr) is the
20857 position before START-RE (resp. after END-RE)."
20859 (let ((pos (point))
20860 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
20861 (limit-down (or lim-down (save-excursion (outline-next-heading))))
20864 ;; Point is on a block when on START-RE or if START-RE can be
20865 ;; found before it...
20866 (and (or (org-at-regexp-p start-re)
20867 (re-search-backward start-re limit-up t))
20868 (setq beg (match-beginning 0))
20869 ;; ... and END-RE after it...
20870 (goto-char (match-end 0))
20871 (re-search-forward end-re limit-down t)
20872 (> (setq end (match-end 0)) pos)
20873 ;; ... without another START-RE in-between.
20874 (goto-char (match-beginning 0))
20875 (not (re-search-backward start-re (1+ beg) t))
20877 (cons beg end))))))
20879 (defun org-in-block-p (names)
20880 "Non-nil when point belongs to a block whose name belongs to NAMES.
20882 NAMES is a list of strings containing names of blocks.
20884 Return first block name matched, or nil. Beware that in case of
20885 nested blocks, the returned name may not belong to the closest
20889 (let ((case-fold-search t)
20890 (lim-up (save-excursion (outline-previous-heading)))
20891 (lim-down (save-excursion (outline-next-heading))))
20892 (mapc (lambda (name)
20893 (let ((n (regexp-quote name)))
20894 (when (org-between-regexps-p
20895 (concat "^[ \t]*#\\+begin_" n)
20896 (concat "^[ \t]*#\\+end_" n)
20902 (defun org-occur-in-agenda-files (regexp &optional nlines)
20903 "Call `multi-occur' with buffers for all agenda files."
20904 (interactive "sOrg-files matching: \np")
20905 (let* ((files (org-agenda-files))
20906 (tnames (mapcar 'file-truename files))
20907 (extra org-agenda-text-search-extra-files)
20909 (when (eq (car extra) 'agenda-archives)
20910 (setq extra (cdr extra))
20911 (setq files (org-add-archive-files files)))
20912 (while (setq f (pop extra))
20913 (unless (member (file-truename f) tnames)
20914 (add-to-list 'files f 'append)
20915 (add-to-list 'tnames (file-truename f) 'append)))
20917 (mapcar (lambda (x)
20918 (with-current-buffer
20919 (or (get-file-buffer x) (find-file-noselect x))
20925 (if (boundp 'occur-mode-find-occurrence-hook)
20927 (add-hook 'occur-mode-find-occurrence-hook
20929 (when (derived-mode-p 'org-mode)
20932 (defadvice occur-mode-goto-occurrence
20933 (after org-occur-reveal activate)
20934 (and (derived-mode-p 'org-mode) (org-reveal)))
20935 (defadvice occur-mode-goto-occurrence-other-window
20936 (after org-occur-reveal activate)
20937 (and (derived-mode-p 'org-mode) (org-reveal)))
20938 (defadvice occur-mode-display-occurrence
20939 (after org-occur-reveal activate)
20940 (when (derived-mode-p 'org-mode)
20941 (let ((pos (occur-mode-find-occurrence)))
20942 (with-current-buffer (marker-buffer pos)
20947 (defun org-occur-link-in-agenda-files ()
20948 "Create a link and search for it in the agendas.
20949 The link is not stored in `org-stored-links', it is just created
20950 for the search purpose."
20952 (let ((link (condition-case nil
20953 (org-store-link nil)
20954 (error "Unable to create a link to here"))))
20955 (org-occur-in-agenda-files (regexp-quote link))))
20957 (defun org-uniquify (list)
20958 "Remove duplicate elements from LIST."
20960 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
20963 (defun org-delete-all (elts list)
20964 "Remove all elements in ELTS from LIST."
20966 (setq list (delete (pop elts) list)))
20969 (defun org-count (cl-item cl-seq)
20970 "Count the number of occurrences of ITEM in SEQ.
20971 Taken from `count' in cl-seq.el with all keyword arguments removed."
20972 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
20973 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
20974 (while (< cl-start cl-end)
20975 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
20976 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
20977 (setq cl-start (1+ cl-start)))
20980 (defun org-remove-if (predicate seq)
20981 "Remove everything from SEQ that fulfills PREDICATE."
20985 (if (not (funcall predicate e)) (push e res)))
20988 (defun org-remove-if-not (predicate seq)
20989 "Remove everything from SEQ that does not fulfill PREDICATE."
20993 (if (funcall predicate e) (push e res)))
20996 (defun org-reduce (cl-func cl-seq &rest cl-keys)
20997 "Reduce two-argument FUNCTION across SEQ.
20998 Taken from `reduce' in cl-seq.el with all keyword arguments but
20999 \":initial-value\" removed."
21000 (let ((cl-accum (cond ((memq :initial-value cl-keys)
21001 (cadr (memq :initial-value cl-keys)))
21002 (cl-seq (pop cl-seq))
21003 (t (funcall cl-func)))))
21005 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
21008 (defun org-back-over-empty-lines ()
21009 "Move backwards over whitespace, to the beginning of the first empty line.
21010 Returns the number of empty lines passed."
21011 (let ((pos (point)))
21012 (if (cdr (assoc 'heading org-blank-before-new-entry))
21013 (skip-chars-backward " \t\n\r")
21015 (forward-line -1)))
21016 (beginning-of-line 2)
21017 (goto-char (min (point) pos))
21018 (count-lines (point) pos)))
21020 (defun org-skip-whitespace ()
21021 (skip-chars-forward " \t\n\r"))
21023 (defun org-point-in-group (point group &optional context)
21024 "Check if POINT is in match-group GROUP.
21025 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
21026 match. If the match group does not exist or point is not inside it,
21028 (and (match-beginning group)
21029 (>= point (match-beginning group))
21030 (<= point (match-end group))
21032 (list context (match-beginning group) (match-end group))
21035 (defun org-switch-to-buffer-other-window (&rest args)
21036 "Switch to buffer in a second window on the current frame.
21037 In particular, do not allow pop-up frames.
21038 Returns the newly created buffer."
21040 (apply 'switch-to-buffer-other-window args)))
21042 (defun org-combine-plists (&rest plists)
21043 "Create a single property list from all plists in PLISTS.
21044 The process starts by copying the first list, and then setting properties
21045 from the other lists. Settings in the last list are the most significant
21046 ones and overrule settings in the other lists."
21047 (let ((rtn (copy-sequence (pop plists)))
21050 (setq ls (pop plists))
21052 (setq p (pop ls) v (pop ls))
21053 (setq rtn (plist-put rtn p v))))
21056 (defun org-replace-escapes (string table)
21057 "Replace %-escapes in STRING with values in TABLE.
21058 TABLE is an association list with keys like \"%a\" and string values.
21059 The sequences in STRING may contain normal field width and padding information,
21060 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
21061 so values can contain further %-escapes if they are define later in TABLE."
21062 (let ((tbl (copy-alist table))
21063 (case-fold-search nil)
21066 (while (setq e (pop tbl))
21067 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
21068 (when (and (cdr e) (string-match re (cdr e)))
21069 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
21071 (add-text-properties 0 3 (list 'sref sref) safe)
21072 (setcdr e (replace-match safe t t (cdr e)))))
21073 (while (string-match re string)
21074 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
21076 (setq string (replace-match rpl t t string))))
21077 (while (setq pchg (next-property-change pchg string))
21078 (let ((sref (get-text-property pchg 'sref string)))
21079 (when (and sref (string-match "SREF" string pchg))
21080 (setq string (replace-match sref t t string)))))
21083 (defun org-sublist (list start end)
21084 "Return a section of LIST, from START to END.
21085 Counting starts at 1."
21086 (let (rtn (c start))
21087 (setq list (nthcdr (1- start) list))
21088 (while (and list (<= c end))
21089 (push (pop list) rtn)
21093 (defun org-find-base-buffer-visiting (file)
21094 "Like `find-buffer-visiting' but always return the base buffer and
21095 not an indirect buffer."
21096 (let ((buf (or (get-file-buffer file)
21097 (find-buffer-visiting file))))
21099 (or (buffer-base-buffer buf) buf)
21102 (defun org-image-file-name-regexp (&optional extensions)
21103 "Return regexp matching the file names of images.
21104 If EXTENSIONS is given, only match these."
21105 (if (and (not extensions) (fboundp 'image-file-name-regexp))
21106 (image-file-name-regexp)
21107 (let ((image-file-name-extensions
21109 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
21110 "xbm" "xpm" "pbm" "pgm" "ppm"))))
21112 (regexp-opt (nconc (mapcar 'upcase
21113 image-file-name-extensions)
21114 image-file-name-extensions)
21118 (defun org-file-image-p (file &optional extensions)
21119 "Return non-nil if FILE is an image."
21121 (string-match (org-image-file-name-regexp extensions) file)))
21123 (defun org-get-cursor-date (&optional with-time)
21124 "Return the date at cursor in as a time.
21125 This works in the calendar and in the agenda, anywhere else it just
21126 returns the current time.
21127 If WITH-TIME is non-nil, returns the time of the event at point (in
21128 the agenda) or the current time of the day."
21129 (let (date day defd tp tm hod mod)
21131 (setq tp (get-text-property (point) 'time))
21132 (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
21133 (setq hod (string-to-number (match-string 1 tp))
21134 mod (string-to-number (match-string 2 tp))))
21135 (or tp (setq hod (nth 2 (decode-time (current-time)))
21136 mod (nth 1 (decode-time (current-time))))))
21138 ((eq major-mode 'calendar-mode)
21139 (setq date (calendar-cursor-to-date)
21140 defd (encode-time 0 (or mod 0) (or hod 0)
21141 (nth 1 date) (nth 0 date) (nth 2 date))))
21142 ((eq major-mode 'org-agenda-mode)
21143 (setq day (get-text-property (point) 'day))
21145 (setq date (calendar-gregorian-from-absolute day)
21146 defd (encode-time 0 (or mod 0) (or hod 0)
21147 (nth 1 date) (nth 0 date) (nth 2 date))))))
21148 (or defd (current-time))))
21150 (defun org-mark-subtree (&optional up)
21151 "Mark the current subtree.
21152 This puts point at the start of the current subtree, and mark at
21153 the end. If a numeric prefix UP is given, move up into the
21154 hierarchy of headlines by UP levels before marking the subtree."
21156 (org-with-limited-levels
21157 (cond ((org-at-heading-p) (beginning-of-line))
21158 ((org-before-first-heading-p) (error "Not in a subtree"))
21159 (t (outline-previous-visible-heading 1))))
21160 (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
21161 (if (org-called-interactively-p 'any)
21162 (call-interactively 'org-mark-element)
21163 (org-mark-element)))
21168 ;; Macros are expanded with `org-macro-replace-all', which relies
21169 ;; internally on `org-macro-expand'.
21171 ;; Default templates for expansion are stored in the buffer-local
21172 ;; variable `org-macro-templates'. This variable is updated by
21173 ;; `org-macro-initialize-templates'.
21175 ;; Along with macros defined through #+MACRO: keyword, default
21176 ;; templates include the following hard-coded macros:
21177 ;; {{{time(format-string)}}}, {{{property(node-property)}}},
21178 ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
21180 ;; During export, {{{author}}}, {{{date}}}, {{{email}}} and
21181 ;; {{{title}}} will also be provided.
21184 (defvar org-macro-templates nil
21185 "Alist containing all macro templates in current buffer.
21186 Associations are in the shape of (NAME . TEMPLATE) where NAME
21187 stands for macro's name and template for its replacement value,
21188 both as strings. This is an internal variable. Do not set it
21189 directly, use instead:
21191 #+MACRO: name template")
21192 (make-variable-buffer-local 'org-macro-templates)
21194 (defun org-macro-expand (macro templates)
21195 "Return expanded MACRO, as a string.
21196 MACRO is an object, obtained, for example, with
21197 `org-element-context'. TEMPLATES is an alist of templates used
21198 for expansion. See `org-macro-templates' for a buffer-local
21199 default value. Return nil if no template was found."
21201 ;; Macro names are case-insensitive.
21202 (cdr (assoc-string (org-element-property :key macro) templates t))))
21204 (let ((value (replace-regexp-in-string
21207 (or (nth (1- (string-to-number (substring arg 1)))
21208 (org-element-property :args macro))
21209 ;; No argument provided: remove
21213 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
21214 (when (string-match "\\`(eval\\>" value)
21215 (setq value (eval (read value))))
21217 (format "%s" (or value ""))))))
21219 (defun org-macro-replace-all (templates)
21220 "Replace all macros in current buffer by their expansion.
21221 TEMPLATES is an alist of templates used for expansion. See
21222 `org-macro-templates' for a buffer-local default value."
21224 (goto-char (point-min))
21226 (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
21227 (let ((object (org-element-context)))
21228 (when (eq (org-element-type object) 'macro)
21229 (let* ((value (org-macro-expand object templates))
21230 (begin (org-element-property :begin object))
21231 (signature (list begin
21233 (org-element-property :args object))))
21234 ;; Avoid circular dependencies by checking if the same
21235 ;; macro with the same arguments is expanded at the same
21237 (if (member signature record)
21238 (error "Circular macro expansion: %s"
21239 (org-element-property :key object))
21241 (push signature record)
21244 ;; Preserve white spaces after the macro.
21245 (progn (goto-char (org-element-property :end object))
21246 (skip-chars-backward " \t")
21248 ;; Leave point before replacement in case of recursive
21250 (save-excursion (insert value)))))))))))
21252 (defun org-macro-initialize-templates ()
21253 "Collect macro templates defined in current buffer.
21254 Templates are stored in buffer-local variable
21255 `org-macro-templates'. In addition to buffer-defined macros, the
21256 function installs the following ones: \"property\",
21257 \"time\". and, if the buffer is associated to a file,
21258 \"input-file\" and \"modification-time\"."
21259 (let ((case-fold-search t)
21262 ;; Add CELL to `org-macro-templates' if there's no
21263 ;; association matching its name already. Otherwise,
21264 ;; replace old association with the new one in that
21266 (let ((old-template (assoc (car cell) org-macro-templates)))
21267 (if old-template (setcdr old-template (cdr cell))
21268 (push cell org-macro-templates))))))
21269 ;; Install buffer-local macros.
21270 (org-with-wide-buffer
21271 (goto-char (point-min))
21272 (while (re-search-forward "^[ \t]*#\\+MACRO:" nil t)
21273 (let ((element (org-element-at-point)))
21274 (when (eq (org-element-type element) 'keyword)
21275 (let ((value (org-element-property :value element)))
21276 (when (string-match "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" value)
21277 (funcall set-template
21278 (cons (match-string 1 value)
21279 (or (match-string 2 value) "")))))))))
21280 ;; Install hard-coded macros.
21281 (mapc (lambda (cell) (funcall set-template cell))
21283 (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
21284 (cons "time" "(eval (format-time-string \"$1\"))")))
21285 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
21286 (when (and visited-file (file-exists-p visited-file))
21287 (mapc (lambda (cell) (funcall set-template cell))
21289 (cons "input-file" (file-name-nondirectory visited-file))
21290 (cons "modification-time"
21291 (format "(eval (format-time-string \"$1\" '%s))"
21293 (nth 5 (file-attributes visited-file)))))))))))
21298 (defun org-indent-line ()
21299 "Indent line depending on context."
21301 (let* ((pos (point))
21302 (itemp (org-at-item-p))
21303 (case-fold-search t)
21304 (org-drawer-regexp (or org-drawer-regexp "\000"))
21305 (inline-task-p (and (featurep 'org-inlinetask)
21306 (org-inlinetask-in-task-p)))
21307 (inline-re (and inline-task-p
21308 (org-inlinetask-outline-regexp)))
21310 (if (and orgstruct-is-++ (eq pos (point)))
21311 (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
21312 (indent-according-to-mode))
21313 (beginning-of-line 1)
21316 ((looking-at org-outline-regexp) (setq column 0))
21318 ((looking-at "#\\+include:") (setq column 0))
21319 ;; Footnote definition
21320 ((looking-at org-footnote-definition-re) (setq column 0))
21321 ;; Literal examples
21322 ((looking-at "[ \t]*:\\( \\|$\\)")
21323 (setq column (org-get-indentation))) ; do nothing
21325 ((ignore-errors (goto-char (org-in-item-p)))
21326 (setq column (if itemp
21327 (org-get-indentation)
21328 (org-list-item-body-column (point))))
21331 ((and (looking-at "[ \t]*:END:")
21332 (save-excursion (re-search-backward org-drawer-regexp nil t)))
21334 (goto-char (1- (match-beginning 1)))
21335 (setq column (current-column))))
21337 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
21339 (re-search-backward
21340 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
21341 (setq column (org-get-indentation (match-string 0))))
21342 ((and (not (looking-at "[ \t]*#\\+begin_"))
21343 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
21345 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
21347 (cond ((equal (downcase (match-string 1)) "src")
21348 ;; src blocks: let `org-edit-src-exit' handle them
21349 (org-get-indentation))
21350 ((equal (downcase (match-string 1)) "example")
21351 (max (org-get-indentation)
21352 (org-get-indentation (match-string 0))))
21354 (org-get-indentation (match-string 0))))))
21355 ;; This line has nothing special, look at the previous relevant
21356 ;; line to compute indentation
21358 (beginning-of-line 0)
21359 (while (and (not (bobp))
21360 (not (looking-at org-table-line-regexp))
21361 (not (looking-at org-drawer-regexp))
21362 ;; When point started in an inline task, do not move
21363 ;; above task starting line.
21364 (not (and inline-task-p (looking-at inline-re)))
21365 ;; Skip drawers, blocks, empty lines, verbatim,
21366 ;; comments, tables, footnotes definitions, lists,
21368 (or (and (looking-at "[ \t]*:END:")
21369 (re-search-backward org-drawer-regexp nil t))
21370 (and (looking-at "[ \t]*#\\+end_")
21371 (re-search-backward "[ \t]*#\\+begin_"nil t))
21372 (looking-at "[ \t]*[\n:#|]")
21373 (looking-at org-footnote-definition-re)
21374 (and (ignore-errors (goto-char (org-in-item-p)))
21376 (org-list-get-top-point (org-list-struct))))
21377 (and (not inline-task-p)
21378 (featurep 'org-inlinetask)
21379 (org-inlinetask-in-task-p)
21380 (or (org-inlinetask-goto-beginning) t))))
21381 (beginning-of-line 0))
21383 ;; There was an heading above.
21384 ((looking-at "\\*+[ \t]+")
21385 (if (not org-adapt-indentation)
21387 (goto-char (match-end 0))
21388 (setq column (current-column))))
21389 ;; A drawer had started and is unfinished
21390 ((looking-at org-drawer-regexp)
21391 (goto-char (1- (match-beginning 1)))
21392 (setq column (current-column)))
21393 ;; Else, nothing noticeable found: get indentation and go on.
21394 (t (setq column (org-get-indentation))))))
21395 ;; Now apply indentation and move cursor accordingly
21397 (if (<= (current-column) (current-indentation))
21398 (org-indent-line-to column)
21399 (save-excursion (org-indent-line-to column)))
21400 ;; Special polishing for properties, see `org-property-format'
21401 (setq column (current-column))
21402 (beginning-of-line 1)
21404 "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
21405 (replace-match (concat (match-string 1)
21406 (format org-property-format
21407 (match-string 2) (match-string 3)))
21409 (org-move-to-column column))))
21411 (defun org-indent-drawer ()
21412 "Indent the drawer at point."
21415 (e (and (save-excursion (re-search-forward ":END:" nil t))
21420 (when (overlays-at (point))
21421 (member 'invisible (overlay-properties
21422 (car (overlays-at (point)))))))))
21423 (when folded (org-cycle))
21424 (indent-for-tab-command)
21425 (while (and (move-beginning-of-line 2) (< (point) e))
21426 (indent-for-tab-command))
21428 (when folded (org-cycle)))
21429 (message "Drawer at point indented"))
21431 (defun org-indent-block ()
21432 "Indent the block at point."
21435 (case-fold-search t)
21436 (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
21441 (when (overlays-at (point))
21442 (member 'invisible (overlay-properties
21443 (car (overlays-at (point)))))))))
21444 (when folded (org-cycle))
21445 (indent-for-tab-command)
21446 (while (and (move-beginning-of-line 2) (< (point) e))
21447 (indent-for-tab-command))
21449 (when folded (org-cycle)))
21450 (message "Block at point indented"))
21452 (defun org-indent-region (start end)
21456 (let ((line-end (org-current-line end)))
21458 (while (< (org-current-line) line-end)
21459 (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
21460 (t (call-interactively 'org-indent-line)))
21461 (move-beginning-of-line 2)))))
21466 ;; We use our own fill-paragraph and auto-fill functions.
21468 ;; `org-fill-paragraph' relies on adaptive filling and context
21469 ;; checking. Appropriate `fill-prefix' is computed with
21470 ;; `org-adaptive-fill-function'.
21472 ;; `org-auto-fill-function' takes care of auto-filling. It calls
21473 ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
21474 ;; `org-adaptive-fill-function' value. Internally,
21475 ;; `org-comment-line-break-function' breaks the line.
21477 ;; `org-setup-filling' installs filling and auto-filling related
21478 ;; variables during `org-mode' initialization.
21480 (defun org-setup-filling ()
21482 ;; Prevent auto-fill from inserting unwanted new items.
21483 (when (boundp 'fill-nobreak-predicate)
21485 'fill-nobreak-predicate
21487 (append fill-nobreak-predicate
21488 '(org-fill-paragraph-separate-nobreak-p
21489 org-fill-line-break-nobreak-p)))))
21490 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
21491 (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
21492 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
21493 (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
21495 (defvar org-element-paragraph-separate) ; org-element.el
21496 (defun org-fill-paragraph-separate-nobreak-p ()
21497 "Non-nil when a line break at point would insert a new item."
21498 (looking-at (substring org-element-paragraph-separate 1)))
21500 (defun org-fill-line-break-nobreak-p ()
21501 "Non-nil when a line break at point would create an Org line break."
21503 (skip-chars-backward "[ \t]")
21504 (skip-chars-backward "\\\\")
21505 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
21507 (declare-function message-in-body-p "message" ())
21508 (defun org-adaptive-fill-function ()
21509 "Compute a fill prefix for the current line.
21510 Return fill prefix, as a string, or nil if current line isn't
21511 meant to be filled."
21512 (org-with-wide-buffer
21513 (unless (and (derived-mode-p 'message-mode) (not (message-in-body-p)))
21514 ;; FIXME: This is really the job of orgstruct++-mode
21515 (let* ((p (line-beginning-position))
21516 (element (save-excursion (beginning-of-line)
21517 (org-element-at-point)))
21518 (type (org-element-type element))
21519 (post-affiliated (org-element-property :post-affiliated element)))
21520 (unless (and post-affiliated (< p post-affiliated))
21522 (comment (looking-at "[ \t]*# ?") (match-string 0))
21523 (footnote-definition "")
21525 (make-string (org-list-item-body-column
21526 (or post-affiliated
21527 (org-element-property :begin element)))
21530 ;; Fill prefix is usually the same as the current line,
21531 ;; except if the paragraph is at the beginning of an item.
21532 (let ((parent (org-element-property :parent element)))
21533 (cond ((eq (org-element-type parent) 'item)
21534 (make-string (org-list-item-body-column
21535 (org-element-property :begin parent))
21537 ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
21541 ;; Only fill contents if P is within block boundaries.
21542 (let* ((cbeg (save-excursion (goto-char post-affiliated)
21545 (cend (save-excursion
21546 (goto-char (org-element-property :end element))
21547 (skip-chars-backward " \r\t\n")
21548 (line-beginning-position))))
21549 (when (and (>= p cbeg) (< p cend))
21550 (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
21554 (declare-function message-goto-body "message" ())
21555 (defvar message-cite-prefix-regexp) ; From message.el
21556 (defvar org-element-all-objects) ; From org-element.el
21557 (defun org-fill-paragraph (&optional justify)
21558 "Fill element at point, when applicable.
21560 This function only applies to comment blocks, comments, example
21561 blocks and paragraphs. Also, as a special case, re-align table
21562 when point is at one.
21564 If JUSTIFY is non-nil (interactively, with prefix argument),
21565 justify as well. If `sentence-end-double-space' is non-nil, then
21566 period followed by one space does not end a sentence, so don't
21567 break a line there. The variable `fill-column' controls the
21570 For convenience, when point is at a plain list, an item or
21571 a footnote definition, try to fill the first paragraph within."
21573 (if (and (derived-mode-p 'message-mode)
21574 (or (not (message-in-body-p))
21575 (save-excursion (move-beginning-of-line 1)
21576 (looking-at message-cite-prefix-regexp))))
21577 ;; First ensure filling is correct in message-mode.
21578 (let ((fill-paragraph-function
21579 (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
21580 (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
21581 (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
21582 (paragraph-separate
21583 (cadadr (assoc 'paragraph-separate org-fb-vars))))
21584 (fill-paragraph nil))
21586 ;; Move to end of line in order to get the first paragraph
21587 ;; within a plain list or a footnote definition.
21589 (let ((element (org-element-at-point)))
21590 ;; First check if point is in a blank line at the beginning of
21591 ;; the buffer. In that case, ignore filling.
21592 (if (< (point) (org-element-property :begin element)) t
21593 (case (org-element-type element)
21594 ;; Use major mode filling function is src blocks.
21595 (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
21596 ;; Align Org tables, leave table.el tables as-is.
21597 (table-row (org-table-align) t)
21599 (when (eq (org-element-property :type element) 'org)
21603 ;; Paragraphs may contain `line-break' type objects.
21604 (let ((beg (max (point-min)
21605 (org-element-property :contents-begin element)))
21606 (end (min (point-max)
21607 (org-element-property :contents-end element))))
21608 ;; Do nothing if point is at an affiliated keyword.
21609 (if (< (point) beg) t
21610 (when (derived-mode-p 'message-mode)
21611 ;; In `message-mode', do not fill following
21612 ;; citation in current paragraph nor text before
21614 (let ((body-start (save-excursion (message-goto-body))))
21615 (when body-start (setq beg (max body-start beg))))
21616 (when (save-excursion
21618 (concat "^" message-cite-prefix-regexp) end t))
21619 (setq end (match-beginning 0))))
21620 ;; Fill paragraph, taking line breaks into
21621 ;; consideration. For that, slice the paragraph
21622 ;; using line breaks as separators, and fill the
21623 ;; parts in reverse order to avoid messing with
21629 (fill-region-as-paragraph pos (point) justify)
21631 ;; Find the list of ending positions for line
21632 ;; breaks in the current paragraph. Add paragraph
21633 ;; beginning to include first slice.
21638 (org-element--parse-objects
21639 beg end nil org-element-all-objects)
21641 (lambda (lb) (org-element-property :end lb)))))))
21643 ;; Contents of `comment-block' type elements should be
21644 ;; filled as plain text, but only if point is within block
21647 (let* ((case-fold-search t)
21648 (beg (save-excursion
21649 (goto-char (org-element-property :begin element))
21650 (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
21653 (end (save-excursion
21654 (goto-char (org-element-property :end element))
21655 (re-search-backward "^[ \t]*#\\+end_comment" nil t)
21656 (line-beginning-position))))
21657 (when (and (>= (point) beg) (< (point) end))
21658 (fill-region-as-paragraph
21661 (re-search-backward "^[ \t]*$" beg 'move)
21662 (line-beginning-position))
21664 (beginning-of-line)
21665 (re-search-forward "^[ \t]*$" end 'move)
21666 (line-beginning-position))
21670 (comment (fill-comment-paragraph justify))
21671 ;; Ignore every other element.
21672 (otherwise t)))))))
21674 (defun org-auto-fill-function ()
21675 "Auto-fill function."
21676 ;; Check if auto-filling is meaningful.
21677 (let ((fc (current-fill-column)))
21678 (when (and fc (> (current-column) fc))
21679 (let* ((fill-prefix (org-adaptive-fill-function))
21680 ;; Enforce empty fill prefix, if required. Otherwise, it
21681 ;; will be computed again.
21682 (adaptive-fill-mode (not (equal fill-prefix ""))))
21683 (when fill-prefix (do-auto-fill))))))
21685 (defun org-comment-line-break-function (&optional soft)
21686 "Break line at point and indent, continuing comment if within one.
21687 The inserted newline is marked hard if variable
21688 `use-hard-newlines' is true, unless optional argument SOFT is
21690 (if soft (insert-and-inherit ?\n) (newline 1))
21691 (save-excursion (forward-char -1) (delete-horizontal-space))
21692 (delete-horizontal-space)
21693 (indent-to-left-margin)
21694 (insert-before-markers-and-inherit fill-prefix))
21699 ;; Org comments syntax is quite complex. It requires the entire line
21700 ;; to be just a comment. Also, even with the right syntax at the
21701 ;; beginning of line, some some elements (i.e. verse-block or
21702 ;; example-block) don't accept comments. Usual Emacs comment commands
21703 ;; cannot cope with those requirements. Therefore, Org replaces them.
21705 ;; Org still relies on `comment-dwim', but cannot trust
21706 ;; `comment-only-p'. So, `comment-region-function' and
21707 ;; `uncomment-region-function' both point
21708 ;; to`org-comment-or-uncomment-region'. Eventually,
21709 ;; `org-insert-comment' takes care of insertion of comments at the
21710 ;; beginning of line.
21712 ;; `org-setup-comments-handling' install comments related variables
21713 ;; during `org-mode' initialization.
21715 (defun org-setup-comments-handling ()
21717 (org-set-local 'comment-use-syntax nil)
21718 (org-set-local 'comment-start "# ")
21719 (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
21720 (org-set-local 'comment-insert-comment-function 'org-insert-comment)
21721 (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
21722 (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
21724 (defun org-insert-comment ()
21725 "Insert an empty comment above current line.
21726 If the line is empty, insert comment at its beginning."
21727 (beginning-of-line)
21728 (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
21732 (defvar comment-empty-lines) ; From newcomment.el.
21733 (defun org-comment-or-uncomment-region (beg end &rest ignore)
21734 "Comment or uncomment each non-blank line in the region.
21735 Uncomment each non-blank line between BEG and END if it only
21736 contains commented lines. Otherwise, comment them."
21739 (narrow-to-region (save-excursion (goto-char beg)
21740 (skip-chars-forward " \r\t\n" end)
21741 (line-beginning-position))
21742 (save-excursion (goto-char end)
21743 (skip-chars-backward " \r\t\n" beg)
21744 (line-end-position)))
21746 ;; UNCOMMENTP is non-nil when every non blank line between
21747 ;; BEG and END is a comment.
21749 (goto-char (point-min))
21750 (while (and (not (eobp))
21751 (let ((element (org-element-at-point)))
21752 (and (eq (org-element-type element) 'comment)
21753 (goto-char (min (point-max)
21754 (org-element-property
21755 :end element)))))))
21758 ;; Only blank lines and comments in region: uncomment it.
21760 (goto-char (point-min))
21761 (while (not (eobp))
21762 (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
21763 (replace-match "" nil nil nil 1))
21765 ;; Comment each line in region.
21766 (let ((min-indent (point-max)))
21767 ;; First find the minimum indentation across all lines.
21769 (goto-char (point-min))
21770 (while (and (not (eobp)) (not (zerop min-indent)))
21771 (unless (looking-at "[ \t]*$")
21772 (setq min-indent (min min-indent (current-indentation))))
21774 ;; Then loop over all lines.
21776 (goto-char (point-min))
21777 (while (not (eobp))
21778 (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
21779 (org-move-to-column min-indent t)
21780 (insert comment-start))
21781 (forward-line))))))))
21786 (defun org-toggle-fixed-width-section (arg)
21787 "Toggle the fixed-width export.
21788 If there is no active region, the QUOTE keyword at the current headline is
21789 inserted or removed. When present, it causes the text between this headline
21790 and the next to be exported as fixed-width text, and unmodified.
21791 If there is an active region, this command adds or removes a colon as the
21792 first character of this line. If the first character of a line is a colon,
21793 this line is also exported in fixed-width font."
21796 (regionp (org-region-active-p))
21797 (beg (if regionp (region-beginning) (point)))
21798 (end (if regionp (region-end)))
21799 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
21800 (case-fold-search nil)
21801 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
21806 (setq cc (current-column))
21807 (beginning-of-line 1)
21808 (setq off (looking-at re))
21809 (while (> nlines 0)
21810 (setq nlines (1- nlines))
21811 (beginning-of-line 1)
21814 (org-move-to-column cc t)
21817 ((and off (looking-at re))
21818 (replace-match "" t t nil 1))
21819 ((not off) (org-move-to-column cc t) (insert ": ")))
21822 (org-back-to-heading)
21824 ((looking-at (format org-heading-keyword-regexp-format
21826 (goto-char (match-end 1))
21827 (looking-at (concat " +" org-quote-string))
21828 (replace-match "" t t)
21829 (when (eolp) (insert " ")))
21830 ((looking-at org-outline-regexp)
21831 (goto-char (match-end 0))
21832 (insert org-quote-string " ")))))))
21834 (defun org-reftex-citation ()
21835 "Use reftex-citation to insert a citation into the buffer.
21836 This looks for a line like
21838 #+BIBLIOGRAPHY: foo plain option:-d
21840 and derives from it that foo.bib is the bibliography file relevant
21841 for this document. It then installs the necessary environment for RefTeX
21842 to work in this buffer and calls `reftex-citation' to insert a citation
21845 Export of such citations to both LaTeX and HTML is handled by the contributed
21846 package org-exp-bibtex by Taru Karttunen."
21848 (let ((reftex-docstruct-symbol 'rds)
21849 (reftex-cite-format "\\cite{%l}")
21854 (let ((case-fold-search t)
21855 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
21856 (if (not (save-excursion
21857 (or (re-search-forward re nil t)
21858 (re-search-backward re nil t))))
21859 (error "No bibliography defined in file")
21860 (setq bib (concat (match-string 1) ".bib")
21861 rds (list (list 'bib bib)))))))
21862 (call-interactively 'reftex-citation)))
21864 ;;;; Functions extending outline functionality
21866 (defun org-beginning-of-line (&optional arg)
21867 "Go to the beginning of the current line. If that is invisible, continue
21868 to a visible line beginning. This makes the function of C-a more intuitive.
21869 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
21870 first attempt, and only move to after the tags when the cursor is already
21871 beyond the end of the headline."
21873 (let ((pos (point))
21874 (special (if (consp org-special-ctrl-a/e)
21875 (car org-special-ctrl-a/e)
21876 org-special-ctrl-a/e))
21878 (if (org-bound-and-true-p visual-line-mode)
21879 (beginning-of-visual-line 1)
21880 (beginning-of-line 1))
21881 (if (and arg (fboundp 'move-beginning-of-line))
21882 (call-interactively 'move-beginning-of-line)
21886 (if (org-truely-invisible-p)
21887 (while (and (not (bobp)) (org-truely-invisible-p))
21889 (beginning-of-line 1))
21890 (forward-char 1))))
21893 ((and (looking-at org-complex-heading-regexp)
21894 (= (char-after (match-end 1)) ?\ ))
21895 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
21899 (cond ((> pos refpos) refpos)
21900 ((= pos (point)) refpos)
21902 (cond ((> pos (point)) (point))
21903 ((not (eq last-command this-command)) (point))
21906 ;; Being at an item and not looking at an the item means point
21907 ;; was previously moved to beginning of a visual line, which
21908 ;; doesn't contain the item. Therefore, do nothing special,
21910 (when (looking-at org-list-full-item-re)
21911 ;; Set special position at first white space character after
21912 ;; bullet, and check-box, if any.
21913 (let ((after-bullet
21914 (let ((box (match-end 3)))
21915 (if (not box) (match-end 1)
21916 (let ((after (char-after box)))
21917 (if (and after (= after ? )) (1+ box) box))))))
21918 ;; Special case: Move point to special position when
21919 ;; currently after it or at beginning of line.
21921 (when (or (> pos after-bullet) (= (point) pos))
21922 (goto-char after-bullet))
21923 ;; Reversed case: Move point to special position when
21924 ;; point was already at beginning of line and command is
21926 (when (and (= (point) pos) (eq last-command this-command))
21927 (goto-char after-bullet))))))))
21929 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21931 (defun org-end-of-line (&optional arg)
21932 "Go to the end of the line.
21933 If this is a headline, and `org-special-ctrl-a/e' is set, ignore
21934 tags on the first attempt, and only move to after the tags when
21935 the cursor is already beyond the end of the headline."
21937 (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
21938 org-special-ctrl-a/e))
21939 (move-fun (cond ((org-bound-and-true-p visual-line-mode)
21940 'end-of-visual-line)
21941 ((fboundp 'move-end-of-line) 'move-end-of-line)
21942 (t 'end-of-line))))
21943 (if (or (not special) arg) (call-interactively move-fun)
21944 (let* ((element (save-excursion (beginning-of-line)
21945 (org-element-at-point)))
21946 (type (org-element-type element)))
21948 ((memq type '(headline inlinetask))
21949 (let ((pos (point)))
21950 (beginning-of-line 1)
21951 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
21953 (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
21954 (goto-char (match-beginning 1))
21955 (goto-char (match-end 0)))
21956 (if (or (< pos (match-end 0))
21957 (not (eq this-command last-command)))
21958 (goto-char (match-end 0))
21959 (goto-char (match-beginning 1))))
21960 (call-interactively move-fun))))
21961 ((org-element-property :hiddenp element)
21962 ;; If element is hidden, `move-end-of-line' would put point
21963 ;; after it. Use `end-of-line' to stay on current line.
21964 (call-interactively 'end-of-line))
21965 (t (call-interactively move-fun)))))
21966 (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21968 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
21969 (define-key org-mode-map "\C-e" 'org-end-of-line)
21971 (defun org-backward-sentence (&optional arg)
21972 "Go to beginning of sentence, or beginning of table field.
21973 This will call `backward-sentence' or `org-table-beginning-of-field',
21974 depending on context."
21977 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
21978 (t (call-interactively 'backward-sentence))))
21980 (defun org-forward-sentence (&optional arg)
21981 "Go to end of sentence, or end of table field.
21982 This will call `forward-sentence' or `org-table-end-of-field',
21983 depending on context."
21986 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
21987 (t (call-interactively 'forward-sentence))))
21989 (define-key org-mode-map "\M-a" 'org-backward-sentence)
21990 (define-key org-mode-map "\M-e" 'org-forward-sentence)
21992 (defun org-kill-line (&optional arg)
21993 "Kill line, to tags or end of line."
21996 ((or (not org-special-ctrl-k)
21998 (not (org-at-heading-p)))
21999 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
22000 org-ctrl-k-protect-subtree)
22001 (if (or (eq org-ctrl-k-protect-subtree 'error)
22002 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
22003 (error "C-k aborted - would kill hidden subtree")))
22004 (call-interactively
22005 (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
22006 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
22007 (kill-region (point) (match-beginning 1))
22008 (org-set-tags nil t))
22009 (t (kill-region (point) (point-at-eol)))))
22011 (define-key org-mode-map "\C-k" 'org-kill-line)
22013 (defun org-yank (&optional arg)
22014 "Yank. If the kill is a subtree, treat it specially.
22015 This command will look at the current kill and check if is a single
22016 subtree, or a series of subtrees[1]. If it passes the test, and if the
22017 cursor is at the beginning of a line or after the stars of a currently
22018 empty headline, then the yank is handled specially. How exactly depends
22019 on the value of the following variables, both set by default.
22021 org-yank-folded-subtrees
22022 When set, the subtree(s) will be folded after insertion, but only
22023 if doing so would now swallow text after the yanked text.
22025 org-yank-adjusted-subtrees
22026 When set, the subtree will be promoted or demoted in order to
22027 fit into the local outline tree structure, which means that the level
22028 will be adjusted so that it becomes the smaller one of the two
22029 *visible* surrounding headings.
22031 Any prefix to this command will cause `yank' to be called directly with
22032 no special treatment. In particular, a simple \\[universal-argument] prefix \
22034 plainly yank the text as it is.
22036 \[1] The test checks if the first non-white line is a heading
22037 and if there are no other headings with fewer stars."
22039 (org-yank-generic 'yank arg))
22041 (defun org-yank-generic (command arg)
22042 "Perform some yank-like command.
22044 This function implements the behavior described in the `org-yank'
22045 documentation. However, it has been generalized to work for any
22046 interactive command with similar behavior."
22048 ;; pretend to be command COMMAND
22049 (setq this-command command)
22052 (call-interactively command)
22054 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
22055 (and (org-kill-is-subtree-p)
22057 (and (looking-at "[ \t]*$")
22060 (buffer-substring (point-at-bol) (point)))))))
22063 ((and subtreep org-yank-folded-subtrees)
22064 (let ((beg (point))
22066 (if (and subtreep org-yank-adjusted-subtrees)
22067 (org-paste-subtree nil nil 'for-yank)
22068 (call-interactively command))
22072 (when (and (bolp) subtreep
22073 (not (setq swallowp
22074 (org-yank-folding-would-swallow-text beg end))))
22075 (org-with-limited-levels
22076 (or (looking-at org-outline-regexp)
22077 (re-search-forward org-outline-regexp-bol end t))
22078 (while (and (< (point) end) (looking-at org-outline-regexp))
22080 (org-cycle-show-empty-lines 'folded)
22081 (condition-case nil
22082 (outline-forward-same-level 1)
22083 (error (goto-char end))))))
22086 "Inserted text not folded because that would swallow text"))
22089 (skip-chars-forward " \t\n\r")
22090 (beginning-of-line 1)
22091 (push-mark beg 'nomsg)))
22092 ((and subtreep org-yank-adjusted-subtrees)
22093 (let ((beg (point-at-bol)))
22094 (org-paste-subtree nil nil 'for-yank)
22095 (push-mark beg 'nomsg)))
22097 (call-interactively command))))))
22099 (defun org-yank-folding-would-swallow-text (beg end)
22100 "Would hide-subtree at BEG swallow any text after END?"
22102 (org-with-limited-levels
22105 (when (or (looking-at org-outline-regexp)
22106 (re-search-forward org-outline-regexp-bol end t))
22107 (setq level (org-outline-level)))
22109 (skip-chars-forward " \t\r\n\v\f")
22111 (and (bolp) (looking-at org-outline-regexp)
22112 (<= (org-outline-level) level)))
22113 nil ; Nothing would be swallowed
22114 t))))) ; something would swallow
22116 (define-key org-mode-map "\C-y" 'org-yank)
22118 (defun org-truely-invisible-p ()
22119 "Check if point is at a character currently not visible.
22120 This version does not only check the character property, but also
22122 ;; Early versions of noutline don't have `outline-invisible-p'.
22123 (if (org-bound-and-true-p visible-mode)
22125 (outline-invisible-p)))
22127 (defun org-invisible-p2 ()
22128 "Check if point is at a character currently not visible."
22130 (if (and (eolp) (not (bobp))) (backward-char 1))
22131 ;; Early versions of noutline don't have `outline-invisible-p'.
22132 (outline-invisible-p)))
22134 (defun org-back-to-heading (&optional invisible-ok)
22135 "Call `outline-back-to-heading', but provide a better error message."
22136 (condition-case nil
22137 (outline-back-to-heading invisible-ok)
22138 (error (error "Before first headline at position %d in buffer %s"
22139 (point) (current-buffer)))))
22141 (defun org-before-first-heading-p ()
22142 "Before first heading?"
22145 (null (re-search-backward org-outline-regexp-bol nil t))))
22147 (defun org-at-heading-p (&optional ignored)
22148 (outline-on-heading-p t))
22149 ;; Compatibility alias with Org versions < 7.8.03
22150 (defalias 'org-on-heading-p 'org-at-heading-p)
22152 (defun org-at-comment-p nil
22153 "Is cursor in a line starting with a # character?"
22155 (beginning-of-line)
22156 (looking-at "^#")))
22158 (defun org-at-drawer-p nil
22159 "Is cursor at a drawer keyword?"
22161 (move-beginning-of-line 1)
22162 (looking-at org-drawer-regexp)))
22164 (defun org-at-block-p nil
22165 "Is cursor at a block keyword?"
22167 (move-beginning-of-line 1)
22168 (looking-at org-block-regexp)))
22170 (defun org-point-at-end-of-empty-headline ()
22171 "If point is at the end of an empty headline, return t, else nil.
22172 If the heading only contains a TODO keyword, it is still still considered
22174 (and (looking-at "[ \t]*$")
22175 (when org-todo-line-regexp
22177 (beginning-of-line 1)
22178 (let ((case-fold-search nil))
22179 (looking-at org-todo-line-regexp)
22180 (string= (match-string 3) ""))))))
22182 (defun org-at-heading-or-item-p ()
22183 (or (org-at-heading-p) (org-at-item-p)))
22185 (defun org-at-target-p ()
22186 (or (org-in-regexp org-radio-target-regexp)
22187 (org-in-regexp org-target-regexp)))
22188 ;; Compatibility alias with Org versions < 7.8.03
22189 (defalias 'org-on-target-p 'org-at-target-p)
22191 (defun org-up-heading-all (arg)
22192 "Move to the heading line of which the present line is a subheading.
22193 This function considers both visible and invisible heading lines.
22194 With argument, move up ARG levels."
22195 (if (fboundp 'outline-up-heading-all)
22196 (outline-up-heading-all arg) ; emacs 21 version of outline.el
22197 (outline-up-heading arg t))) ; emacs 22 version of outline.el
22199 (defun org-up-heading-safe ()
22200 "Move to the heading line of which the present line is a subheading.
22201 This version will not throw an error. It will return the level of the
22202 headline found, or nil if no higher level is found.
22204 Also, this function will be a lot faster than `outline-up-heading',
22205 because it relies on stars being the outline starters. This can really
22206 make a significant difference in outlines with very many siblings."
22207 (let (start-level re)
22208 (org-back-to-heading t)
22209 (setq start-level (funcall outline-level))
22210 (if (equal start-level 1)
22212 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
22213 (if (re-search-backward re nil t)
22214 (funcall outline-level)))))
22216 (defun org-first-sibling-p ()
22217 "Is this heading the first child of its parents?"
22219 (let ((re org-outline-regexp-bol)
22221 (unless (org-at-heading-p t)
22222 (error "Not at a heading"))
22223 (setq level (funcall outline-level))
22225 (if (not (re-search-backward re nil t))
22227 (setq l (funcall outline-level))
22230 (defun org-goto-sibling (&optional previous)
22231 "Goto the next sibling, even if it is invisible.
22232 When PREVIOUS is set, go to the previous sibling instead. Returns t
22233 when a sibling was found. When none is found, return nil and don't
22235 (let ((fun (if previous 're-search-backward 're-search-forward))
22237 (re org-outline-regexp-bol)
22239 (when (condition-case nil (org-back-to-heading t) (error nil))
22240 (setq level (funcall outline-level))
22242 (or previous (forward-char 1))
22243 (while (funcall fun re nil t)
22244 (setq l (funcall outline-level))
22245 (when (< l level) (goto-char pos) (throw 'exit nil))
22246 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
22250 (defun org-show-siblings ()
22251 "Show all siblings of the current headline."
22253 (while (org-goto-sibling) (org-flag-heading nil)))
22255 (while (org-goto-sibling 'previous)
22256 (org-flag-heading nil))))
22258 (defun org-goto-first-child ()
22259 "Goto the first child, even if it is invisible.
22260 Return t when a child was found. Otherwise don't move point and
22262 (let (level (pos (point)) (re org-outline-regexp-bol))
22263 (when (condition-case nil (org-back-to-heading t) (error nil))
22264 (setq level (outline-level))
22266 (if (and (re-search-forward re nil t) (> (outline-level) level))
22267 (progn (goto-char (match-beginning 0)) t)
22268 (goto-char pos) nil))))
22270 (defun org-show-hidden-entry ()
22271 "Show an entry where even the heading is hidden."
22275 (defun org-flag-heading (flag &optional entry)
22276 "Flag the current heading. FLAG non-nil means make invisible.
22277 When ENTRY is non-nil, show the entire entry."
22279 (org-back-to-heading t)
22280 ;; Check if we should show the entire entry
22285 (and (outline-next-heading)
22286 (org-flag-heading nil))))
22287 (outline-flag-region (max (point-min) (1- (point)))
22288 (save-excursion (outline-end-of-heading) (point))
22291 (defun org-get-next-sibling ()
22292 "Move to next heading of the same level, and return point.
22293 If there is no such heading, return nil.
22294 This is like outline-next-sibling, but invisible headings are ok."
22295 (let ((level (funcall outline-level)))
22296 (outline-next-heading)
22297 (while (and (not (eobp)) (> (funcall outline-level) level))
22298 (outline-next-heading))
22299 (if (or (eobp) (< (funcall outline-level) level))
22303 (defun org-get-last-sibling ()
22304 "Move to previous heading of the same level, and return point.
22305 If there is no such heading, return nil."
22306 (let ((opoint (point))
22307 (level (funcall outline-level)))
22308 (outline-previous-heading)
22309 (when (and (/= (point) opoint) (outline-on-heading-p t))
22310 (while (and (> (funcall outline-level) level)
22312 (outline-previous-heading))
22313 (if (< (funcall outline-level) level)
22317 (defun org-end-of-subtree (&optional invisible-ok to-heading)
22318 "Goto to the end of a subtree."
22319 ;; This contains an exact copy of the original function, but it uses
22320 ;; `org-back-to-heading', to make it work also in invisible
22321 ;; trees. And is uses an invisible-ok argument.
22322 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
22323 ;; Furthermore, when used inside Org, finding the end of a large subtree
22324 ;; with many children and grandchildren etc, this can be much faster
22325 ;; than the outline version.
22326 (org-back-to-heading invisible-ok)
22328 (level (funcall outline-level)))
22329 (if (and (derived-mode-p 'org-mode) (< level 1000))
22330 ;; A true heading (not a plain list item), in Org-mode
22331 ;; This means we can easily find the end by looking
22332 ;; only for the right number of stars. Using a regexp to do
22333 ;; this is so much faster than using a Lisp loop.
22334 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
22336 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
22337 ;; something else, do it the slow way
22338 (while (and (not (eobp))
22339 (or first (> (funcall outline-level) level)))
22341 (outline-next-heading)))
22343 (if (memq (preceding-char) '(?\n ?\^M))
22345 ;; Go to end of line before heading
22347 (if (memq (preceding-char) '(?\n ?\^M))
22348 ;; leave blank line before heading
22349 (forward-char -1))))))
22352 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
22353 "Use Org version in org-mode, for dramatic speed-up."
22354 (if (derived-mode-p 'org-mode)
22356 (org-end-of-subtree nil t)
22357 (unless (eobp) (backward-char 1)))
22360 (defun org-end-of-meta-data-and-drawers ()
22361 "Jump to the first text after meta data and drawers in the current entry.
22362 This will move over empty lines, lines with planning time stamps,
22363 clocking lines, and drawers."
22364 (org-back-to-heading t)
22365 (let ((end (save-excursion (outline-next-heading) (point)))
22366 (re (concat "\\(" org-drawer-regexp "\\)"
22367 "\\|" "[ \t]*" org-keyword-time-regexp)))
22369 (while (re-search-forward re end t)
22370 (if (not (match-end 1))
22371 ;; empty or planning line
22373 ;; a drawer, find the end
22374 (re-search-forward "^[ \t]*:END:" end 'move)
22376 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
22379 (defun org-forward-heading-same-level (arg &optional invisible-ok)
22380 "Move forward to the arg'th subheading at same level as this one.
22381 Stop at the first and last subheadings of a superior heading.
22382 Normally this only looks at visible headings, but when INVISIBLE-OK is
22383 non-nil it will also look at invisible ones."
22385 (if (not (ignore-errors (org-back-to-heading invisible-ok)))
22386 (outline-next-heading)
22388 (let* ((level (- (match-end 0) (match-beginning 0) 1))
22389 (re (format "^\\*\\{1,%d\\} " level))
22393 (while (and (re-search-forward re nil 'move)
22394 (setq l (- (match-end 0) (match-beginning 0) 1))
22397 (progn (backward-char 1) (outline-invisible-p)))
22398 (if (< l level) (setq arg 1)))
22399 (setq arg (1- arg)))
22400 (beginning-of-line 1))))
22402 (defun org-backward-heading-same-level (arg &optional invisible-ok)
22403 "Move backward to the arg'th subheading at same level as this one.
22404 Stop at the first and last subheadings of a superior heading."
22406 (if (not (ignore-errors (org-back-to-heading)))
22407 (goto-char (point-min))
22409 (let* ((level (- (match-end 0) (match-beginning 0) 1))
22410 (re (format "^\\*\\{1,%d\\} " level))
22413 (while (and (re-search-backward re nil 'move)
22414 (setq l (- (match-end 0) (match-beginning 0) 1))
22417 (outline-invisible-p))
22418 (if (< l level) (setq arg 1)))
22419 (setq arg (1- arg))))))
22421 (defun org-forward-element ()
22422 "Move forward by one element.
22423 Move to the next element at the same level, when possible."
22425 (cond ((eobp) (error "Cannot move further down"))
22426 ((org-with-limited-levels (org-at-heading-p))
22427 (let ((origin (point)))
22428 (org-forward-heading-same-level 1)
22429 (unless (org-with-limited-levels (org-at-heading-p))
22431 (error "Cannot move further down"))))
22433 (let* ((elem (org-element-at-point))
22434 (end (org-element-property :end elem))
22435 (parent (org-element-property :parent elem)))
22436 (if (and parent (= (org-element-property :contents-end parent) end))
22437 (goto-char (org-element-property :end parent))
22438 (goto-char end))))))
22440 (defun org-backward-element ()
22441 "Move backward by one element.
22442 Move to the previous element at the same level, when possible."
22444 (cond ((bobp) (error "Cannot move further up"))
22445 ((org-with-limited-levels (org-at-heading-p))
22446 ;; At an headline, move to the previous one, if any, or stay
22448 (let ((origin (point)))
22449 (org-backward-heading-same-level 1)
22450 (unless (org-with-limited-levels (org-at-heading-p))
22452 (error "Cannot move further up"))))
22454 (let* ((trail (org-element-at-point 'keep-trail))
22456 (prev-elem (nth 1 trail))
22457 (beg (org-element-property :begin elem)))
22459 ;; Move to beginning of current element if point isn't
22461 ((/= (point) beg) (goto-char beg))
22462 (prev-elem (goto-char (org-element-property :begin prev-elem)))
22463 ((org-before-first-heading-p) (goto-char (point-min)))
22464 (t (org-back-to-heading)))))))
22466 (defun org-up-element ()
22467 "Move to upper element."
22469 (if (org-with-limited-levels (org-at-heading-p))
22470 (unless (org-up-heading-safe) (error "No surrounding element"))
22471 (let* ((elem (org-element-at-point))
22472 (parent (org-element-property :parent elem)))
22473 (if parent (goto-char (org-element-property :begin parent))
22474 (if (org-with-limited-levels (org-before-first-heading-p))
22475 (error "No surrounding element")
22476 (org-with-limited-levels (org-back-to-heading)))))))
22478 (defvar org-element-greater-elements)
22479 (defun org-down-element ()
22480 "Move to inner element."
22482 (let ((element (org-element-at-point)))
22484 ((memq (org-element-type element) '(plain-list table))
22485 (goto-char (org-element-property :contents-begin element))
22487 ((memq (org-element-type element) org-element-greater-elements)
22488 ;; If contents are hidden, first disclose them.
22489 (when (org-element-property :hiddenp element) (org-cycle))
22490 (goto-char (or (org-element-property :contents-begin element)
22491 (error "No content for this element"))))
22492 (t (error "No inner element")))))
22494 (defun org-drag-element-backward ()
22495 "Move backward element at point."
22497 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
22498 (let* ((trail (org-element-at-point 'keep-trail))
22500 (prev-elem (nth 1 trail)))
22501 ;; Error out if no previous element or previous element is
22502 ;; a parent of the current one.
22503 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
22504 (error "Cannot drag element backward")
22505 (let ((pos (point)))
22506 (org-element-swap-A-B prev-elem elem)
22507 (goto-char (+ (org-element-property :begin prev-elem)
22508 (- pos (org-element-property :begin elem)))))))))
22510 (defun org-drag-element-forward ()
22511 "Move forward element at point."
22513 (let* ((pos (point))
22514 (elem (org-element-at-point)))
22515 (when (= (point-max) (org-element-property :end elem))
22516 (error "Cannot drag element forward"))
22517 (goto-char (org-element-property :end elem))
22518 (let ((next-elem (org-element-at-point)))
22519 (when (or (org-element-nested-p elem next-elem)
22520 (and (eq (org-element-type next-elem) 'headline)
22521 (not (eq (org-element-type elem) 'headline))))
22523 (error "Cannot drag element forward"))
22524 ;; Compute new position of point: it's shifted by NEXT-ELEM
22525 ;; body's length (without final blanks) and by the length of
22526 ;; blanks between ELEM and NEXT-ELEM.
22527 (let ((size-next (- (save-excursion
22528 (goto-char (org-element-property :end next-elem))
22529 (skip-chars-backward " \r\t\n")
22531 ;; Small correction if buffer doesn't end
22532 ;; with a newline character.
22533 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
22534 (org-element-property :begin next-elem)))
22535 (size-blank (- (org-element-property :end elem)
22537 (goto-char (org-element-property :end elem))
22538 (skip-chars-backward " \r\t\n")
22541 (org-element-swap-A-B elem next-elem)
22542 (goto-char (+ pos size-next size-blank))))))
22544 (defun org-mark-element ()
22545 "Put point at beginning of this element, mark at end.
22547 Interactively, if this command is repeated or (in Transient Mark
22548 mode) if the mark is active, it marks the next element after the
22549 ones already marked."
22551 (let (deactivate-mark)
22552 (if (and (org-called-interactively-p 'any)
22553 (or (and (eq last-command this-command) (mark t))
22554 (and transient-mark-mode mark-active)))
22558 (goto-char (org-element-property :end (org-element-at-point)))))
22559 (let ((element (org-element-at-point)))
22561 (push-mark (org-element-property :end element) t t)
22562 (goto-char (org-element-property :begin element))))))
22564 (defun org-narrow-to-element ()
22565 "Narrow buffer to current element."
22567 (let ((elem (org-element-at-point)))
22569 ((eq (car elem) 'headline)
22571 (org-element-property :begin elem)
22572 (org-element-property :end elem)))
22573 ((memq (car elem) org-element-greater-elements)
22575 (org-element-property :contents-begin elem)
22576 (org-element-property :contents-end elem)))
22579 (org-element-property :begin elem)
22580 (org-element-property :end elem))))))
22582 (defun org-transpose-words ()
22583 "Transpose words, using `org-mode' syntax table."
22585 (with-syntax-table org-syntax-table
22586 (call-interactively 'transpose-words)))
22587 (org-remap org-mode-map 'transpose-words 'org-transpose-words)
22589 (defun org-transpose-element ()
22590 "Transpose current and previous elements, keeping blank lines between.
22591 Point is moved after both elements."
22593 (org-skip-whitespace)
22594 (let ((end (org-element-property :end (org-element-at-point))))
22595 (org-drag-element-backward)
22598 (defun org-unindent-buffer ()
22599 "Un-indent the visible part of the buffer.
22600 Relative indentation (between items, inside blocks, etc.) isn't
22603 (unless (eq major-mode 'org-mode)
22604 (error "Cannot un-indent a buffer not in Org mode"))
22605 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
22606 unindent-tree ; For byte-compiler.
22612 (if (memq (org-element-type element) '(headline section))
22613 (funcall unindent-tree (org-element-contents element))
22617 (org-element-property :begin element)
22618 (org-element-property :end element))
22619 (org-do-remove-indentation)))))
22620 (reverse contents))))))
22621 (funcall unindent-tree (org-element-contents parse-tree))))
22623 (defun org-show-subtree ()
22624 "Show everything after this heading at deeper levels."
22626 (outline-flag-region
22629 (org-end-of-subtree t t))
22632 (defun org-show-entry ()
22633 "Show the body directly following this heading.
22634 Show the heading too, if it is currently invisible."
22637 (condition-case nil
22639 (org-back-to-heading t)
22640 (outline-flag-region
22641 (max (point-min) (1- (point)))
22643 (if (re-search-forward
22644 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
22645 (match-beginning 1)
22648 (org-cycle-hide-drawers 'children))
22651 (defun org-make-options-regexp (kwds &optional extra)
22652 "Make a regular expression for keyword lines."
22655 (mapconcat 'regexp-quote kwds "\\|")
22656 (if extra (concat "\\|" extra))
22657 "\\):[ \t]*\\(.*\\)"))
22659 ;; Make isearch reveal the necessary context
22660 (defun org-isearch-end ()
22661 "Reveal context after isearch exits."
22662 (when isearch-success ; only if search was successful
22663 (if (featurep 'xemacs)
22664 ;; Under XEmacs, the hook is run in the correct place,
22665 ;; we directly show the context.
22666 (org-show-context 'isearch)
22667 ;; In Emacs the hook runs *before* restoring the overlays.
22668 ;; So we have to use a one-time post-command-hook to do this.
22669 ;; (Emacs 22 has a special variable, see function `org-mode')
22670 (unless (and (boundp 'isearch-mode-end-hook-quit)
22671 isearch-mode-end-hook-quit)
22672 ;; Only when the isearch was not quitted.
22673 (org-add-hook 'post-command-hook 'org-isearch-post-command
22674 'append 'local)))))
22676 (defun org-isearch-post-command ()
22677 "Remove self from hook, and show context."
22678 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
22679 (org-show-context 'isearch))
22682 ;;;; Integration with and fixes for other packages
22686 (defvar org-imenu-markers nil
22687 "All markers currently used by Imenu.")
22688 (make-variable-buffer-local 'org-imenu-markers)
22690 (defun org-imenu-new-marker (&optional pos)
22691 "Return a new marker for use by Imenu, and remember the marker."
22692 (let ((m (make-marker)))
22693 (move-marker m (or pos (point)))
22694 (push m org-imenu-markers)
22697 (defun org-imenu-get-tree ()
22698 "Produce the index for Imenu."
22699 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
22700 (setq org-imenu-markers nil)
22701 (let* ((n org-imenu-depth)
22702 (re (concat "^" (org-get-limited-outline-regexp)))
22703 (subs (make-vector (1+ n) nil))
22709 (goto-char (point-max))
22710 (while (re-search-backward re nil t)
22711 (setq level (org-reduced-level (funcall outline-level)))
22712 (when (and (<= level n)
22713 (looking-at org-complex-heading-regexp))
22714 (setq head (org-link-display-format
22715 (org-match-string-no-properties 4))
22716 m (org-imenu-new-marker))
22717 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
22718 (if (>= level last-level)
22719 (push (cons head m) (aref subs level))
22720 (push (cons head (aref subs (1+ level))) (aref subs level))
22721 (loop for i from (1+ level) to n do (aset subs i nil)))
22722 (setq last-level level)))))
22725 (eval-after-load "imenu"
22727 (add-hook 'imenu-after-jump-hook
22729 (if (derived-mode-p 'org-mode)
22730 (org-show-context 'org-goto))))))
22732 (defun org-link-display-format (link)
22733 "Replace a link with either the description, or the link target
22734 if no description is present"
22736 (if (string-match org-bracket-link-analytic-regexp link)
22737 (replace-match (if (match-end 5)
22738 (match-string 5 link)
22739 (concat (match-string 1 link)
22740 (match-string 3 link)))
22744 (defun org-toggle-link-display ()
22745 "Toggle the literal or descriptive display of links."
22747 (if org-descriptive-links
22748 (progn (org-remove-from-invisibility-spec '(org-link))
22749 (org-restart-font-lock)
22750 (setq org-descriptive-links nil))
22751 (progn (add-to-invisibility-spec '(org-link))
22752 (org-restart-font-lock)
22753 (setq org-descriptive-links t))))
22755 ;; Speedbar support
22757 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
22758 "Overlay marking the agenda restriction line in speedbar.")
22759 (overlay-put org-speedbar-restriction-lock-overlay
22760 'face 'org-agenda-restriction-lock)
22761 (overlay-put org-speedbar-restriction-lock-overlay
22762 'help-echo "Agendas are currently limited to this item.")
22763 (org-detach-overlay org-speedbar-restriction-lock-overlay)
22765 (defun org-speedbar-set-agenda-restriction ()
22766 "Restrict future agenda commands to the location at point in speedbar.
22767 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
22769 (require 'org-agenda)
22770 (let (p m tp np dir txt)
22772 ((setq p (text-property-any (point-at-bol) (point-at-eol)
22774 (setq m (get-text-property p 'org-imenu-marker))
22775 (with-current-buffer (marker-buffer m)
22777 (org-agenda-set-restriction-lock 'subtree)))
22778 ((setq p (text-property-any (point-at-bol) (point-at-eol)
22779 'speedbar-function 'speedbar-find-file))
22780 (setq tp (previous-single-property-change
22781 (1+ p) 'speedbar-function)
22782 np (next-single-property-change
22783 tp 'speedbar-function)
22784 dir (speedbar-line-directory)
22785 txt (buffer-substring-no-properties (or tp (point-min))
22786 (or np (point-max))))
22787 (with-current-buffer (find-file-noselect
22788 (let ((default-directory dir))
22789 (expand-file-name txt)))
22790 (unless (derived-mode-p 'org-mode)
22791 (error "Cannot restrict to non-Org-mode file"))
22792 (org-agenda-set-restriction-lock 'file)))
22793 (t (error "Don't know how to restrict Org-mode's agenda")))
22794 (move-overlay org-speedbar-restriction-lock-overlay
22795 (point-at-bol) (point-at-eol))
22796 (setq current-prefix-arg nil)
22797 (org-agenda-maybe-redo)))
22799 (eval-after-load "speedbar"
22801 (speedbar-add-supported-extension ".org")
22802 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
22803 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
22804 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
22805 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
22806 (add-hook 'speedbar-visiting-tag-hook
22807 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
22809 ;;; Fixes and Hacks for problems with other packages
22811 ;; Make flyspell not check words in links, to not mess up our keymap
22812 (defun org-mode-flyspell-verify ()
22813 "Don't let flyspell put overlays at active buttons, or on
22814 {todo,all-time,additional-option-like}-keywords."
22815 (let ((pos (max (1- (point)) (point-min)))
22816 (word (thing-at-point 'word)))
22817 (and (not (get-text-property pos 'keymap))
22818 (not (get-text-property pos 'org-no-flyspell))
22819 (not (member word org-todo-keywords-1))
22820 (not (member word org-all-time-keywords))
22821 (not (member word org-options-keywords))
22822 (not (member word (mapcar 'car org-startup-options)))
22823 (not (member word org-additional-option-like-keywords-for-flyspell)))))
22825 (defun org-remove-flyspell-overlays-in (beg end)
22826 "Remove flyspell overlays in region."
22827 (and (org-bound-and-true-p flyspell-mode)
22828 (fboundp 'flyspell-delete-region-overlays)
22829 (flyspell-delete-region-overlays beg end))
22830 (add-text-properties beg end '(org-no-flyspell t)))
22832 ;; Make `bookmark-jump' shows the jump location if it was hidden.
22833 (eval-after-load "bookmark"
22834 '(if (boundp 'bookmark-after-jump-hook)
22835 ;; We can use the hook
22836 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
22837 ;; Hook not available, use advice
22838 (defadvice bookmark-jump (after org-make-visible activate)
22839 "Make the position visible."
22840 (org-bookmark-jump-unhide))))
22842 ;; Make sure saveplace shows the location if it was hidden
22843 (eval-after-load "saveplace"
22844 '(defadvice save-place-find-file-hook (after org-make-visible activate)
22845 "Make the position visible."
22846 (org-bookmark-jump-unhide)))
22848 ;; Make sure ecb shows the location if it was hidden
22849 (eval-after-load "ecb"
22850 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
22851 "Make hierarchy visible when jumping into location from ECB tree buffer."
22852 (if (derived-mode-p 'org-mode)
22853 (org-show-context))))
22855 (defun org-bookmark-jump-unhide ()
22856 "Unhide the current position, to show the bookmark location."
22857 (and (derived-mode-p 'org-mode)
22858 (or (outline-invisible-p)
22859 (save-excursion (goto-char (max (point-min) (1- (point))))
22860 (outline-invisible-p)))
22861 (org-show-context 'bookmark-jump)))
22863 ;; Make session.el ignore our circular variable
22864 (eval-after-load "session"
22865 '(add-to-list 'session-globals-exclude 'org-mark-ring))
22867 ;;;; Experimental code
22869 (defun org-closed-in-range ()
22870 "Sparse tree of items closed in a certain time range.
22871 Still experimental, may disappear in the future."
22873 ;; Get the time interval from the user.
22874 (let* ((time1 (org-float-time
22875 (org-read-date nil 'to-time nil "Starting date: ")))
22876 (time2 (org-float-time
22877 (org-read-date nil 'to-time nil "End date:")))
22878 ;; callback function
22879 (callback (lambda ()
22882 (apply 'encode-time
22883 (org-parse-time-string
22884 (match-string 1))))))
22885 ;; check if time in interval
22886 (and (>= time time1) (<= time time2))))))
22887 ;; make tree, check each match with the callback
22888 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
22894 (run-hooks 'org-load-hook)
22896 ;;; org.el ends here