1 ;;; org.el --- Outline-based notes management and organizer
3 ;; Carstens outline-mode for keeping track of everything.
4 ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
8 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
30 ;; project planning with a fast and effective plain-text system.
32 ;; Org-mode develops organizational tasks around NOTES files that contain
33 ;; information about projects as plain text. Org-mode is implemented on
34 ;; top of outline-mode, which makes it possible to keep the content of
35 ;; large files well structured. Visibility cycling and structure editing
36 ;; help to work with the tree. Tables are easily created with a built-in
37 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
38 ;; and scheduling. It dynamically compiles entries into an agenda that
39 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
40 ;; Plain text URL-like links connect to websites, emails, Usenet
41 ;; messages, BBDB entries, and any files related to the projects. For
42 ;; printing and sharing of notes, an Org-mode file can be exported as a
43 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
44 ;; iCalendar file. It can also serve as a publishing tool for a set of
47 ;; Installation and Activation
48 ;; ---------------------------
49 ;; See the corresponding sections in the manual at
51 ;; http://orgmode.org/org.html#Installation
55 ;; The documentation of Org-mode can be found in the TeXInfo file. The
56 ;; distribution also contains a PDF version of it. At the homepage of
57 ;; Org-mode, you can read the same text online as HTML. There is also an
58 ;; excellent reference card made by Philip Rooke. This card can be found
59 ;; in the etc/ directory of Emacs 22.
61 ;; A list of recent changes can be found at
62 ;; http://orgmode.org/Changes.html
66 (defvar org-inhibit-highlight-removal nil
) ; dynamically scoped param
67 (defvar org-table-formula-constants-local nil
68 "Local version of `org-table-formula-constants'.")
69 (make-variable-buffer-local 'org-table-formula-constants-local
)
71 ;;;; Require other packages
79 (require 'format-spec
)
81 (load "org-loaddefs.el" t t
)
83 ;; `org-outline-regexp' ought to be a defconst but is let-binding in
84 ;; some places -- e.g. see the macro org-with-limited-levels.
86 ;; In Org buffers, the value of `outline-regexp' is that of
87 ;; `org-outline-regexp'. The only function still directly relying on
88 ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
89 ;; job when `orgstruct-mode' is active.
90 (defvar org-outline-regexp
"\\*+ "
91 "Regexp to match Org headlines.")
93 (defvar org-outline-regexp-bol
"^\\*+ "
94 "Regexp to match Org headlines.
95 This is similar to `org-outline-regexp' but additionally makes
96 sure that we are at the beginning of the line.")
98 (defvar org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
99 "Matches an headline, putting stars and text into groups.
100 Stars are put in group 1 and the trimmed body in group 2.")
102 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
103 (when (fboundp 'defvaralias
)
104 (unless (boundp 'calendar-view-holidays-initially-flag
)
105 (defvaralias 'calendar-view-holidays-initially-flag
106 'view-calendar-holidays-initially
))
107 (unless (boundp 'calendar-view-diary-initially-flag
)
108 (defvaralias 'calendar-view-diary-initially-flag
109 'view-diary-entries-initially
))
110 (unless (boundp 'diary-fancy-buffer
)
111 (defvaralias 'diary-fancy-buffer
'fancy-diary-buffer
)))
113 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
114 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
115 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
116 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
117 (declare-function org-at-clock-log-p
"org-clock" ())
118 (declare-function org-clock-timestamps-up
"org-clock" ())
119 (declare-function org-clock-timestamps-down
"org-clock" ())
120 (declare-function org-clock-sum-current-item
"org-clock" (&optional tstart
))
122 (declare-function orgtbl-mode
"org-table" (&optional arg
))
123 (declare-function org-clock-out
"org-clock" (&optional switch-to-state fail-quietly at-time
))
124 (declare-function org-beamer-mode
"org-beamer" ())
125 (declare-function org-table-edit-field
"org-table" (arg))
126 (declare-function org-table-justify-field-maybe
"org-table" (&optional new
))
127 (declare-function org-id-get-create
"org-id" (&optional force
))
128 (declare-function org-id-find-id-file
"org-id" (id))
129 (declare-function org-tags-view
"org-agenda" (&optional todo-only match
))
130 (declare-function org-agenda-list
"org-agenda" (&optional arg start-day span
))
131 (declare-function org-table-align
"org-table" ())
132 (declare-function org-table-paste-rectangle
"org-table" ())
133 (declare-function org-table-maybe-eval-formula
"org-table" ())
134 (declare-function org-table-maybe-recalculate-line
"org-table" ())
136 ;; load languages based on value of `org-babel-load-languages'
137 (defvar org-babel-load-languages
)
140 (defun org-babel-do-load-languages (sym value
)
141 "Load the languages defined in `org-babel-load-languages'."
142 (set-default sym value
)
144 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
147 (require (intern (concat "ob-" lang
))))
149 (funcall 'fmakunbound
150 (intern (concat "org-babel-execute:" lang
)))
151 (funcall 'fmakunbound
152 (intern (concat "org-babel-expand-body:" lang
)))))))
153 org-babel-load-languages
))
155 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
156 "Languages which can be evaluated in Org-mode buffers.
157 This list can be used to load support for any of the languages
158 below, note that each language will depend on a different set of
159 system executables and/or Emacs modes. When a language is
160 \"loaded\", then code blocks in that language can be evaluated
161 with `org-babel-execute-src-block' bound by default to C-c
162 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
163 be set to remove code block evaluation from the C-c C-c
164 keybinding. By default only Emacs Lisp (which has no
165 requirements) is loaded."
167 :set
'org-babel-do-load-languages
169 :type
'(alist :tag
"Babel Languages"
172 (const :tag
"Awk" awk
)
175 (const :tag
"Asymptote" asymptote
)
176 (const :tag
"Calc" calc
)
177 (const :tag
"Clojure" clojure
)
178 (const :tag
"CSS" css
)
179 (const :tag
"Ditaa" ditaa
)
180 (const :tag
"Dot" dot
)
181 (const :tag
"Emacs Lisp" emacs-lisp
)
182 (const :tag
"Fortran" fortran
)
183 (const :tag
"Gnuplot" gnuplot
)
184 (const :tag
"Haskell" haskell
)
186 (const :tag
"Java" java
)
187 (const :tag
"Javascript" js
)
188 (const :tag
"LaTeX" latex
)
189 (const :tag
"Ledger" ledger
)
190 (const :tag
"Lilypond" lilypond
)
191 (const :tag
"Lisp" lisp
)
192 (const :tag
"Maxima" maxima
)
193 (const :tag
"Matlab" matlab
)
194 (const :tag
"Mscgen" mscgen
)
195 (const :tag
"Ocaml" ocaml
)
196 (const :tag
"Octave" octave
)
197 (const :tag
"Org" org
)
198 (const :tag
"Perl" perl
)
199 (const :tag
"Pico Lisp" picolisp
)
200 (const :tag
"PlantUML" plantuml
)
201 (const :tag
"Python" python
)
202 (const :tag
"Ruby" ruby
)
203 (const :tag
"Sass" sass
)
204 (const :tag
"Scala" scala
)
205 (const :tag
"Scheme" scheme
)
206 (const :tag
"Screen" screen
)
207 (const :tag
"Shell Script" sh
)
208 (const :tag
"Shen" shen
)
209 (const :tag
"Sql" sql
)
210 (const :tag
"Sqlite" sqlite
))
211 :value-type
(boolean :tag
"Activate" :value t
)))
213 ;;;; Customization variables
214 (defcustom org-clone-delete-id nil
215 "Remove ID property of clones of a subtree.
216 When non-nil, clones of a subtree don't inherit the ID property.
217 Otherwise they inherit the ID property with a new unique
224 (require 'org-compat
)
228 (defun org-version (&optional here full message
)
229 "Show the org-mode version in the echo area.
230 With prefix argument HERE, insert it at point.
231 When FULL is non-nil, use a verbose version string.
232 When MESSAGE is non-nil, display a message with the version."
234 (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
235 (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs.el")))
237 (and (fboundp 'org-release
) (fboundp 'org-git-version
))
238 (load (concat org-dir
"org-version.el")
239 'noerror
'nomessage
'nosuffix
)))
240 (org-version (org-release))
241 (git-version (org-git-version))
242 (version (format "Org-mode version %s (%s @ %s)"
246 (if (string= org-dir org-install-dir
)
248 (concat "mixed installation! " org-install-dir
" and " org-dir
))
249 "org-loaddefs.el can not be found!")))
250 (_version (if full version org-version
)))
251 (if (org-called-interactively-p 'interactive
)
255 (if message
(message _version
))
258 (defconst org-version
(org-version))
260 ;;; Compatibility constants
262 ;;; The custom variables
265 "Outline-based notes management and organizer."
270 (defcustom org-mode-hook nil
271 "Mode hook for Org-mode, run after the mode was turned on."
275 (defcustom org-load-hook nil
276 "Hook that is run after org.el has been loaded."
280 (defcustom org-log-buffer-setup-hook nil
281 "Hook that is run after an Org log buffer is created."
286 (defvar org-modules
) ; defined below
287 (defvar org-modules-loaded nil
288 "Have the modules been loaded already?")
290 (defun org-load-modules-maybe (&optional force
)
291 "Load all extensions listed in `org-modules'."
292 (when (or force
(not org-modules-loaded
))
294 (condition-case nil
(require ext
)
295 (error (message "Problems while trying to load feature `%s'" ext
))))
297 (setq org-modules-loaded t
)))
299 (defun org-set-modules (var value
)
300 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
302 (when (featurep 'org
)
303 (org-load-modules-maybe 'force
)))
305 (when (org-bound-and-true-p org-modules
)
306 (let ((a (member 'org-infojs org-modules
)))
307 (and a
(setcar a
'org-jsinfo
))))
309 (defcustom org-modules
'(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl
)
310 "Modules that should always be loaded together with org.el.
311 If a description starts with <C>, the file is not part of Emacs
312 and loading it will require that you have downloaded and properly installed
313 the org-mode distribution.
315 You can also use this system to load external packages (i.e. neither Org
316 core modules, nor modules from the CONTRIB directory). Just add symbols
317 to the end of the list. If the package is called org-xyz.el, then you need
318 to add the symbol `xyz', and the package must have a call to
322 :set
'org-set-modules
325 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
326 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
327 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
328 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
329 (const :tag
" docview: Links to doc-view buffers" org-docview
)
330 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
331 (const :tag
" id: Global IDs for identifying entries" org-id
)
332 (const :tag
" info: Links to Info nodes" org-info
)
333 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
334 (const :tag
" habit: Track your consistency with habits" org-habit
)
335 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
336 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
337 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
338 (const :tag
" mew Links to Mew folders/messages" org-mew
)
339 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
340 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
341 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
342 (const :tag
" special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks
)
343 (const :tag
" vm: Links to VM folders/messages" org-vm
)
344 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
345 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
346 (const :tag
" mouse: Additional mouse support" org-mouse
)
347 (const :tag
" TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler
)
349 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
350 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
351 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
352 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
353 (const :tag
"C collector: Collect properties into tables" org-collector
)
354 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
355 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
356 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
357 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
358 (const :tag
"C eval: Include command output as text" org-eval
)
359 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
360 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
361 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
362 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
363 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
365 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
367 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
368 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
369 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
370 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
371 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
372 (const :tag
"C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber
)
373 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
374 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
375 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
376 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
377 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
378 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
379 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
380 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
381 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
382 (const :tag
"C track: Keep up with Org-mode development" org-track
)
383 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
384 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
385 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
387 (defcustom org-support-shift-select nil
388 "Non-nil means make shift-cursor commands select text when possible.
390 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
391 start selecting a region, or enlarge regions started in this way.
392 In Org-mode, in special contexts, these same keys are used for
393 other purposes, important enough to compete with shift selection.
394 Org tries to balance these needs by supporting `shift-select-mode'
395 outside these special contexts, under control of this variable.
397 The default of this variable is nil, to avoid confusing behavior. Shifted
398 cursor keys will then execute Org commands in the following contexts:
399 - on a headline, changing TODO state (left/right) and priority (up/down)
400 - on a time stamp, changing the time
401 - in a plain list item, changing the bullet type
402 - in a property definition line, switching between allowed values
403 - in the BEGIN line of a clock table (changing the time block).
404 Outside these contexts, the commands will throw an error.
406 When this variable is t and the cursor is not in a special
407 context, Org-mode will support shift-selection for making and
408 enlarging regions. To make this more effective, the bullet
409 cycling will no longer happen anywhere in an item line, but only
410 if the cursor is exactly on the bullet.
412 If you set this variable to the symbol `always', then the keys
413 will not be special in headlines, property lines, and item lines,
414 to make shift selection work there as well. If this is what you
415 want, you can use the following alternative commands: `C-c C-t'
416 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
417 can be used to switch TODO sets, `C-c -' to cycle item bullet
418 types, and properties can be edited by hand or in column view.
420 However, when the cursor is on a timestamp, shift-cursor commands
421 will still edit the time stamp - this is just too good to give up.
423 XEmacs user should have this variable set to nil, because
424 `shift-select-mode' is in Emacs 23 or later only."
427 (const :tag
"Never" nil
)
428 (const :tag
"When outside special context" t
)
429 (const :tag
"Everywhere except timestamps" always
)))
431 (defcustom org-loop-over-headlines-in-active-region nil
432 "Shall some commands act upon headlines in the active region?
434 When set to `t', some commands will be performed in all headlines
435 within the active region.
437 When set to `start-level', some commands will be performed in all
438 headlines within the active region, provided that these headlines
439 are of the same level than the first one.
441 When set to a string, those commands will be performed on the
442 matching headlines within the active region. Such string must be
443 a tags/property/todo match as it is used in the agenda tags view.
445 The list of commands is: `org-schedule', `org-deadline',
446 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
447 `org-archive-to-archive-sibling'. The archiving commands skip
448 already archived entries."
449 :type
'(choice (const :tag
"Don't loop" nil
)
450 (const :tag
"All headlines in active region" t
)
451 (const :tag
"In active region, headlines at the same level than the first one" 'start-level
)
452 (string :tag
"Tags/Property/Todo matcher"))
457 (defgroup org-startup nil
458 "Options concerning startup of Org-mode."
462 (defcustom org-startup-folded t
463 "Non-nil means entering Org-mode will switch to OVERVIEW.
464 This can also be configured on a per-file basis by adding one of
465 the following lines anywhere in the buffer:
467 #+STARTUP: fold (or `overview', this is equivalent)
468 #+STARTUP: nofold (or `showall', this is equivalent)
470 #+STARTUP: showeverything"
473 (const :tag
"nofold: show all" nil
)
474 (const :tag
"fold: overview" t
)
475 (const :tag
"content: all headlines" content
)
476 (const :tag
"show everything, even drawers" showeverything
)))
478 (defcustom org-startup-truncated t
479 "Non-nil means entering Org-mode will set `truncate-lines'.
480 This is useful since some lines containing links can be very long and
481 uninteresting. Also tables look terrible when wrapped."
485 (defcustom org-startup-indented nil
486 "Non-nil means turn on `org-indent-mode' on startup.
487 This can also be configured on a per-file basis by adding one of
488 the following lines anywhere in the buffer:
492 :group
'org-structure
494 (const :tag
"Not" nil
)
495 (const :tag
"Globally (slow on startup in large files)" t
)))
497 (defcustom org-use-sub-superscripts t
498 "Non-nil means interpret \"_\" and \"^\" for export.
499 When this option is turned on, you can use TeX-like syntax for sub- and
500 superscripts. Several characters after \"_\" or \"^\" will be
501 considered as a single item - so grouping with {} is normally not
502 needed. For example, the following things will be parsed as single
503 sub- or superscripts.
505 10^24 or 10^tau several digits will be considered 1 item.
506 10^-12 or 10^-tau a leading sign with digits or a word
507 x^2-y^3 will be read as x^2 - y^3, because items are
508 terminated by almost any nonword/nondigit char.
509 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
511 Still, ambiguity is possible - so when in doubt use {} to enclose the
512 sub/superscript. If you set this variable to the symbol `{}',
513 the braces are *required* in order to trigger interpretations as
514 sub/superscript. This can be helpful in documents that need \"_\"
515 frequently in plain text.
517 Not all export backends support this, but HTML does.
519 This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
521 :group
'org-export-translation
524 (const :tag
"Always interpret" t
)
525 (const :tag
"Only with braces" {})
526 (const :tag
"Never interpret" nil
)))
528 (if (fboundp 'defvaralias
)
529 (defvaralias 'org-export-with-sub-superscripts
'org-use-sub-superscripts
))
532 (defcustom org-startup-with-beamer-mode nil
533 "Non-nil means turn on `org-beamer-mode' on startup.
534 This can also be configured on a per-file basis by adding one of
535 the following lines anywhere in the buffer:
542 (defcustom org-startup-align-all-tables nil
543 "Non-nil means align all tables when visiting a file.
544 This is useful when the column width in tables is forced with <N> cookies
545 in table fields. Such tables will look correct only after the first re-align.
546 This can also be configured on a per-file basis by adding one of
547 the following lines anywhere in the buffer:
553 (defcustom org-startup-with-inline-images nil
554 "Non-nil means show inline images when loading a new Org file.
555 This can also be configured on a per-file basis by adding one of
556 the following lines anywhere in the buffer:
557 #+STARTUP: inlineimages
558 #+STARTUP: noinlineimages"
563 (defcustom org-insert-mode-line-in-empty-file nil
564 "Non-nil means insert the first line setting Org-mode in empty files.
565 When the function `org-mode' is called interactively in an empty file, this
566 normally means that the file name does not automatically trigger Org-mode.
567 To ensure that the file will always be in Org-mode in the future, a
568 line enforcing Org-mode will be inserted into the buffer, if this option
573 (defcustom org-replace-disputed-keys nil
574 "Non-nil means use alternative key bindings for some keys.
575 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
576 These keys are also used by other packages like shift-selection-mode'
577 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
578 If you want to use Org-mode together with one of these other modes,
579 or more generally if you would like to move some Org-mode commands to
580 other keys, set this variable and configure the keys with the variable
583 This option is only relevant at load-time of Org-mode, and must be set
584 *before* org.el is loaded. Changing it requires a restart of Emacs to
589 (defcustom org-use-extra-keys nil
590 "Non-nil means use extra key sequence definitions for certain commands.
591 This happens automatically if you run XEmacs or if `window-system'
592 is nil. This variable lets you do the same manually. You must
593 set it before loading org.
595 Example: on Carbon Emacs 22 running graphically, with an external
596 keyboard on a Powerbook, the default way of setting M-left might
597 not work for either Alt or ESC. Setting this variable will make
602 (if (fboundp 'defvaralias
)
603 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
605 (defcustom org-disputed-keys
606 '(([(shift up
)] .
[(meta p
)])
607 ([(shift down
)] .
[(meta n
)])
608 ([(shift left
)] .
[(meta -
)])
609 ([(shift right
)] .
[(meta +)])
610 ([(control shift right
)] .
[(meta shift
+)])
611 ([(control shift left
)] .
[(meta shift -
)]))
612 "Keys for which Org-mode and other modes compete.
613 This is an alist, cars are the default keys, second element specifies
614 the alternative to use when `org-replace-disputed-keys' is t.
616 Keys can be specified in any syntax supported by `define-key'.
617 The value of this option takes effect only at Org-mode's startup,
618 therefore you'll have to restart Emacs to apply it after changing."
623 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
624 Or return the original if not disputed.
625 Also apply the translations defined in `org-xemacs-key-equivalents'."
626 (when org-replace-disputed-keys
627 (let* ((nkey (key-description key
))
628 (x (org-find-if (lambda (x)
629 (equal (key-description (car x
)) nkey
))
631 (setq key
(if x
(cdr x
) key
))))
632 (when (featurep 'xemacs
)
633 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
636 (defun org-find-if (predicate seq
)
639 (if (funcall predicate
(car seq
))
640 (throw 'exit
(car seq
))
643 (defun org-defkey (keymap key def
)
644 "Define a key, possibly translated, as returned by `org-key'."
645 (define-key keymap
(org-key key
) def
))
647 (defcustom org-ellipsis nil
648 "The ellipsis to use in the Org-mode outline.
649 When nil, just use the standard three dots. When a string, use that instead,
650 When a face, use the standard 3 dots, but with the specified face.
651 The change affects only Org-mode (which will then use its own display table).
652 Changing this requires executing `M-x org-mode' in a buffer to become
655 :type
'(choice (const :tag
"Default" nil
)
656 (face :tag
"Face" :value org-warning
)
657 (string :tag
"String" :value
"...#")))
659 (defvar org-display-table nil
660 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
662 (defgroup org-keywords nil
663 "Keywords in Org-mode."
667 (defcustom org-deadline-string
"DEADLINE:"
668 "String to mark deadline entries.
669 A deadline is this string, followed by a time stamp. Should be a word,
670 terminated by a colon. You can insert a schedule keyword and
671 a timestamp with \\[org-deadline].
672 Changes become only effective after restarting Emacs."
676 (defcustom org-scheduled-string
"SCHEDULED:"
677 "String to mark scheduled TODO entries.
678 A schedule is this string, followed by a time stamp. Should be a word,
679 terminated by a colon. You can insert a schedule keyword and
680 a timestamp with \\[org-schedule].
681 Changes become only effective after restarting Emacs."
685 (defcustom org-closed-string
"CLOSED:"
686 "String used as the prefix for timestamps logging closing a TODO entry."
690 (defcustom org-clock-string
"CLOCK:"
691 "String used as prefix for timestamps clocking work hours on an item."
695 (defconst org-planning-or-clock-line-re
(concat "^[ \t]*\\("
696 org-scheduled-string
"\\|"
697 org-deadline-string
"\\|"
698 org-closed-string
"\\|"
699 org-clock-string
"\\)")
700 "Matches a line with planning or clock info.")
702 (defcustom org-comment-string
"COMMENT"
703 "Entries starting with this keyword will never be exported.
704 An entry can be toggled between COMMENT and normal with
705 \\[org-toggle-comment].
706 Changes become only effective after restarting Emacs."
710 (defcustom org-quote-string
"QUOTE"
711 "Entries starting with this keyword will be exported in fixed-width font.
712 Quoting applies only to the text in the entry following the headline, and does
713 not extend beyond the next headline, even if that is lower level.
714 An entry can be toggled between QUOTE and normal with
715 \\[org-toggle-fixed-width-section]."
719 (defconst org-repeat-re
720 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
721 "Regular expression for specifying repeated events.
722 After a match, group 1 contains the repeat expression.")
724 (defgroup org-structure nil
725 "Options concerning the general structure of Org-mode files."
729 (defgroup org-reveal-location nil
730 "Options about how to make context of a location visible."
731 :tag
"Org Reveal Location"
732 :group
'org-structure
)
734 (defconst org-context-choice
736 (const :tag
"Always" t
)
737 (const :tag
"Never" nil
)
738 (repeat :greedy t
:tag
"Individual contexts"
740 (choice :tag
"Context"
747 (const bookmark-jump
)
751 "Contexts for the reveal options.")
753 (defcustom org-show-hierarchy-above
'((default . t
))
754 "Non-nil means show full hierarchy when revealing a location.
755 Org-mode often shows locations in an org-mode file which might have
756 been invisible before. When this is set, the hierarchy of headings
757 above the exposed location is shown.
758 Turning this off for example for sparse trees makes them very compact.
759 Instead of t, this can also be an alist specifying this option for different
760 contexts. Valid contexts are
761 agenda when exposing an entry from the agenda
762 org-goto when using the command `org-goto' on key C-c C-j
763 occur-tree when using the command `org-occur' on key C-c /
764 tags-tree when constructing a sparse tree based on tags matches
765 link-search when exposing search matches associated with a link
766 mark-goto when exposing the jump goal of a mark
767 bookmark-jump when exposing a bookmark location
768 isearch when exiting from an incremental search
769 default default for all contexts not set explicitly"
770 :group
'org-reveal-location
771 :type org-context-choice
)
773 (defcustom org-show-following-heading
'((default . nil
))
774 "Non-nil means show following heading when revealing a location.
775 Org-mode often shows locations in an org-mode file which might have
776 been invisible before. When this is set, the heading following the
778 Turning this off for example for sparse trees makes them very compact,
779 but makes it harder to edit the location of the match. In such a case,
780 use the command \\[org-reveal] to show more context.
781 Instead of t, this can also be an alist specifying this option for different
782 contexts. See `org-show-hierarchy-above' for valid contexts."
783 :group
'org-reveal-location
784 :type org-context-choice
)
786 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
787 "Non-nil means show all sibling heading when revealing a location.
788 Org-mode often shows locations in an org-mode file which might have
789 been invisible before. When this is set, the sibling of the current entry
790 heading are all made visible. If `org-show-hierarchy-above' is t,
791 the same happens on each level of the hierarchy above the current entry.
793 By default this is on for the isearch context, off for all other contexts.
794 Turning this off for example for sparse trees makes them very compact,
795 but makes it harder to edit the location of the match. In such a case,
796 use the command \\[org-reveal] to show more context.
797 Instead of t, this can also be an alist specifying this option for different
798 contexts. See `org-show-hierarchy-above' for valid contexts."
799 :group
'org-reveal-location
800 :type org-context-choice
)
802 (defcustom org-show-entry-below
'((default . nil
))
803 "Non-nil means show the entry below a headline when revealing a location.
804 Org-mode often shows locations in an org-mode file which might have
805 been invisible before. When this is set, the text below the headline that is
806 exposed is also shown.
808 By default this is off for all contexts.
809 Instead of t, this can also be an alist specifying this option for different
810 contexts. See `org-show-hierarchy-above' for valid contexts."
811 :group
'org-reveal-location
812 :type org-context-choice
)
814 (defcustom org-indirect-buffer-display
'other-window
815 "How should indirect tree buffers be displayed?
816 This applies to indirect buffers created with the commands
817 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
819 current-window Display in the current window
820 other-window Just display in another window.
821 dedicated-frame Create one new frame, and re-use it each time.
822 new-frame Make a new frame each time. Note that in this case
823 previously-made indirect buffers are kept, and you need to
824 kill these buffers yourself."
825 :group
'org-structure
826 :group
'org-agenda-windows
828 (const :tag
"In current window" current-window
)
829 (const :tag
"In current frame, other window" other-window
)
830 (const :tag
"Each time a new frame" new-frame
)
831 (const :tag
"One dedicated frame" dedicated-frame
)))
833 (defcustom org-use-speed-commands nil
834 "Non-nil means activate single letter commands at beginning of a headline.
835 This may also be a function to test for appropriate locations where speed
836 commands should be active."
837 :group
'org-structure
839 (const :tag
"Never" nil
)
840 (const :tag
"At beginning of headline stars" t
)
843 (defcustom org-speed-commands-user nil
844 "Alist of additional speed commands.
845 This list will be checked before `org-speed-commands-default'
846 when the variable `org-use-speed-commands' is non-nil
847 and when the cursor is at the beginning of a headline.
848 The car if each entry is a string with a single letter, which must
849 be assigned to `self-insert-command' in the global map.
850 The cdr is either a command to be called interactively, a function
851 to be called, or a form to be evaluated.
852 An entry that is just a list with a single string will be interpreted
853 as a descriptive headline that will be added when listing the speed
854 commands in the Help buffer using the `?' speed command."
855 :group
'org-structure
856 :type
'(repeat :value
("k" . ignore
)
857 (choice :value
("k" . ignore
)
858 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
859 (cons :tag
"Letter and Command"
860 (string :tag
"Command letter")
865 (defgroup org-cycle nil
866 "Options concerning visibility cycling in Org-mode."
868 :group
'org-structure
)
870 (defcustom org-cycle-skip-children-state-if-no-children t
871 "Non-nil means skip CHILDREN state in entries that don't have any."
875 (defcustom org-cycle-max-level nil
876 "Maximum level which should still be subject to visibility cycling.
877 Levels higher than this will, for cycling, be treated as text, not a headline.
878 When `org-odd-levels-only' is set, a value of N in this variable actually
879 means 2N-1 stars as the limiting headline.
880 When nil, cycle all levels.
881 Note that the limiting level of cycling is also influenced by
882 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
883 `org-inlinetask-min-level' is, cycling will be limited to levels one less
887 (const :tag
"No limit" nil
)
888 (integer :tag
"Maximum level")))
890 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
891 "Names of drawers. Drawers are not opened by cycling on the headline above.
892 Drawers only open with a TAB on the drawer line itself. A drawer looks like
897 The drawer \"PROPERTIES\" is special for capturing properties through
900 Drawers can be defined on the per-file basis with a line like:
902 #+DRAWERS: HIDDEN STATE PROPERTIES"
903 :group
'org-structure
905 :type
'(repeat (string :tag
"Drawer Name")))
907 (defcustom org-hide-block-startup nil
908 "Non-nil means entering Org-mode will fold all blocks.
909 This can also be set in on a per-file basis with
911 #+STARTUP: hideblocks
912 #+STARTUP: showblocks"
917 (defcustom org-cycle-global-at-bob nil
918 "Cycle globally if cursor is at beginning of buffer and not at a headline.
919 This makes it possible to do global cycling without having to use S-TAB or
920 \\[universal-argument] TAB. For this special case to work, the first line
921 of the buffer must not be a headline -- it may be empty or some other text.
922 When used in this way, `org-cycle-hook' is disabled temporarily to make
923 sure the cursor stays at the beginning of the buffer. When this option is
924 nil, don't do anything special at the beginning of the buffer."
928 (defcustom org-cycle-level-after-item
/entry-creation t
929 "Non-nil means cycle entry level or item indentation in new empty entries.
931 When the cursor is at the end of an empty headline, i.e with only stars
932 and maybe a TODO keyword, TAB will then switch the entry to become a child,
933 and then all possible ancestor states, before returning to the original state.
934 This makes data entry extremely fast: M-RET to create a new headline,
935 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
937 When the cursor is at the end of an empty plain list item, one TAB will
938 make it a subitem, two or more tabs will back up to make this an item
939 higher up in the item hierarchy."
943 (defcustom org-cycle-emulate-tab t
944 "Where should `org-cycle' emulate TAB.
946 white Only in completely white lines
947 whitestart Only at the beginning of lines, before the first non-white char
948 t Everywhere except in headlines
949 exc-hl-bol Everywhere except at the start of a headline
950 If TAB is used in a place where it does not emulate TAB, the current subtree
951 visibility is cycled."
953 :type
'(choice (const :tag
"Never" nil
)
954 (const :tag
"Only in completely white lines" white
)
955 (const :tag
"Before first char in a line" whitestart
)
956 (const :tag
"Everywhere except in headlines" t
)
957 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
960 (defcustom org-cycle-separator-lines
2
961 "Number of empty lines needed to keep an empty line between collapsed trees.
962 If you leave an empty line between the end of a subtree and the following
963 headline, this empty line is hidden when the subtree is folded.
964 Org-mode will leave (exactly) one empty line visible if the number of
965 empty lines is equal or larger to the number given in this variable.
966 So the default 2 means at least 2 empty lines after the end of a subtree
967 are needed to produce free space between a collapsed subtree and the
970 If the number is negative, and the number of empty lines is at least -N,
971 all empty lines are shown.
973 Special case: when 0, never leave empty lines in collapsed view."
976 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
978 (defcustom org-pre-cycle-hook nil
979 "Hook that is run before visibility cycling is happening.
980 The function(s) in this hook must accept a single argument which indicates
981 the new state that will be set right after running this hook. The
982 argument is a symbol. Before a global state change, it can have the values
983 `overview', `content', or `all'. Before a local state change, it can have
984 the values `folded', `children', or `subtree'."
988 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
989 org-cycle-hide-drawers
990 org-cycle-show-empty-lines
991 org-optimize-window-after-visibility-change
)
992 "Hook that is run after `org-cycle' has changed the buffer visibility.
993 The function(s) in this hook must accept a single argument which indicates
994 the new state that was set by the most recent `org-cycle' command. The
995 argument is a symbol. After a global state change, it can have the values
996 `overview', `contents', or `all'. After a local state change, it can have
997 the values `folded', `children', or `subtree'."
1001 (defgroup org-edit-structure nil
1002 "Options concerning structure editing in Org-mode."
1003 :tag
"Org Edit Structure"
1004 :group
'org-structure
)
1006 (defcustom org-odd-levels-only nil
1007 "Non-nil means skip even levels and only use odd levels for the outline.
1008 This has the effect that two stars are being added/taken away in
1009 promotion/demotion commands. It also influences how levels are
1010 handled by the exporters.
1011 Changing it requires restart of `font-lock-mode' to become effective
1012 for fontification also in regions already fontified.
1013 You may also set this on a per-file basis by adding one of the following
1014 lines to the buffer:
1018 :group
'org-edit-structure
1019 :group
'org-appearance
1022 (defcustom org-adapt-indentation t
1023 "Non-nil means adapt indentation to outline node level.
1025 When this variable is set, Org assumes that you write outlines by
1026 indenting text in each node to align with the headline (after the stars).
1027 The following issues are influenced by this variable:
1029 - When this is set and the *entire* text in an entry is indented, the
1030 indentation is increased by one space in a demotion command, and
1031 decreased by one in a promotion command. If any line in the entry
1032 body starts with text at column 0, indentation is not changed at all.
1034 - Property drawers and planning information is inserted indented when
1035 this variable s set. When nil, they will not be indented.
1037 - TAB indents a line relative to context. The lines below a headline
1038 will be indented when this variable is set.
1040 Note that this is all about true indentation, by adding and removing
1041 space characters. See also `org-indent.el' which does level-dependent
1042 indentation in a virtual way, i.e. at display time in Emacs."
1043 :group
'org-edit-structure
1046 (defcustom org-special-ctrl-a
/e nil
1047 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1049 When t, `C-a' will bring back the cursor to the beginning of the
1050 headline text, i.e. after the stars and after a possible TODO
1051 keyword. In an item, this will be the position after bullet and
1052 check-box, if any. When the cursor is already at that position,
1053 another `C-a' will bring it to the beginning of the line.
1055 `C-e' will jump to the end of the headline, ignoring the presence
1056 of tags in the headline. A second `C-e' will then jump to the
1057 true end of the line, after any tags. This also means that, when
1058 this variable is non-nil, `C-e' also will never jump beyond the
1059 end of the heading of a folded section, i.e. not after the
1062 When set to the symbol `reversed', the first `C-a' or `C-e' works
1063 normally, going to the true line boundary first. Only a directly
1064 following, identical keypress will bring the cursor to the
1067 This may also be a cons cell where the behavior for `C-a' and
1068 `C-e' is set separately."
1069 :group
'org-edit-structure
1071 (const :tag
"off" nil
)
1072 (const :tag
"on: after stars/bullet and before tags first" t
)
1073 (const :tag
"reversed: true line boundary first" reversed
)
1074 (cons :tag
"Set C-a and C-e separately"
1075 (choice :tag
"Special C-a"
1076 (const :tag
"off" nil
)
1077 (const :tag
"on: after stars/bullet first" t
)
1078 (const :tag
"reversed: before stars/bullet first" reversed
))
1079 (choice :tag
"Special C-e"
1080 (const :tag
"off" nil
)
1081 (const :tag
"on: before tags first" t
)
1082 (const :tag
"reversed: after tags first" reversed
)))))
1083 (if (fboundp 'defvaralias
)
1084 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
1086 (defcustom org-special-ctrl-k nil
1087 "Non-nil means `C-k' will behave specially in headlines.
1088 When nil, `C-k' will call the default `kill-line' command.
1089 When t, the following will happen while the cursor is in the headline:
1091 - When the cursor is at the beginning of a headline, kill the entire
1092 line and possible the folded subtree below the line.
1093 - When in the middle of the headline text, kill the headline up to the tags.
1094 - When after the headline text, kill the tags."
1095 :group
'org-edit-structure
1098 (defcustom org-ctrl-k-protect-subtree nil
1099 "Non-nil means, do not delete a hidden subtree with C-k.
1100 When set to the symbol `error', simply throw an error when C-k is
1101 used to kill (part-of) a headline that has hidden text behind it.
1102 Any other non-nil value will result in a query to the user, if it is
1103 OK to kill that hidden subtree. When nil, kill without remorse."
1104 :group
'org-edit-structure
1107 (const :tag
"Do not protect hidden subtrees" nil
)
1108 (const :tag
"Protect hidden subtrees with a security query" t
)
1109 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1111 (defcustom org-catch-invisible-edits nil
1112 "Check if in invisible region before inserting or deleting a character.
1115 nil Do not check, so just do invisible edits.
1116 error Throw an error and do nothing.
1117 show Make point visible, and do the requested edit.
1118 show-and-error Make point visible, then throw an error and abort the edit.
1119 smart Make point visible, and do insertion/deletion if it is
1120 adjacent to visible text and the change feels predictable.
1121 Never delete a previously invisible character or add in the
1122 middle or right after an invisible region. Basically, this
1123 allows insertion and backward-delete right before ellipses.
1124 FIXME: maybe in this case we should not even show?"
1125 :group
'org-edit-structure
1128 (const :tag
"Do not check" nil
)
1129 (const :tag
"Throw error when trying to edit" error
)
1130 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1131 (const :tag
"Show invisible part and do the edit" show
)
1132 (const :tag
"Be smart and do the right thing" smart
)))
1134 (defcustom org-yank-folded-subtrees t
1135 "Non-nil means when yanking subtrees, fold them.
1136 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1137 it starts with a heading and all other headings in it are either children
1138 or siblings, then fold all the subtrees. However, do this only if no
1139 text after the yank would be swallowed into a folded tree by this action."
1140 :group
'org-edit-structure
1143 (defcustom org-yank-adjusted-subtrees nil
1144 "Non-nil means when yanking subtrees, adjust the level.
1145 With this setting, `org-paste-subtree' is used to insert the subtree, see
1146 this function for details."
1147 :group
'org-edit-structure
1150 (defcustom org-M-RET-may-split-line
'((default . t
))
1151 "Non-nil means M-RET will split the line at the cursor position.
1152 When nil, it will go to the end of the line before making a
1154 You may also set this option in a different way for different
1155 contexts. Valid contexts are:
1157 headline when creating a new headline
1158 item when creating a new item
1159 table in a table field
1160 default the value to be used for all contexts not explicitly
1162 :group
'org-structure
1165 (const :tag
"Always" t
)
1166 (const :tag
"Never" nil
)
1167 (repeat :greedy t
:tag
"Individual contexts"
1169 (choice :tag
"Context"
1177 (defcustom org-insert-heading-respect-content nil
1178 "Non-nil means insert new headings after the current subtree.
1179 When nil, the new heading is created directly after the current line.
1180 The commands \\[org-insert-heading-respect-content] and
1181 \\[org-insert-todo-heading-respect-content] turn this variable on
1182 for the duration of the command."
1183 :group
'org-structure
1186 (defcustom org-blank-before-new-entry
'((heading . auto
)
1187 (plain-list-item . auto
))
1188 "Should `org-insert-heading' leave a blank line before new heading/item?
1189 The value is an alist, with `heading' and `plain-list-item' as CAR,
1190 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1191 which case Org will look at the surrounding headings/items and try to
1192 make an intelligent decision whether to insert a blank line or not.
1194 For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
1195 set, the setting here is ignored and no empty line is inserted, to avoid
1196 breaking the list structure."
1197 :group
'org-edit-structure
1199 (cons (const heading
)
1200 (choice (const :tag
"Never" nil
)
1201 (const :tag
"Always" t
)
1202 (const :tag
"Auto" auto
)))
1203 (cons (const plain-list-item
)
1204 (choice (const :tag
"Never" nil
)
1205 (const :tag
"Always" t
)
1206 (const :tag
"Auto" auto
)))))
1208 (defcustom org-insert-heading-hook nil
1209 "Hook being run after inserting a new heading."
1210 :group
'org-edit-structure
1213 (defcustom org-enable-fixed-width-editor t
1214 "Non-nil means lines starting with \":\" are treated as fixed-width.
1215 This currently only means they are never auto-wrapped.
1216 When nil, such lines will be treated like ordinary lines.
1217 See also the QUOTE keyword."
1218 :group
'org-edit-structure
1221 (defcustom org-goto-auto-isearch t
1222 "Non-nil means typing characters in `org-goto' starts incremental search.
1223 When nil, you can use these keybindings to navigate the buffer:
1225 q Quit the org-goto interface
1226 n Go to the next visible heading
1227 p Go to the previous visible heading
1228 f Go one heading forward on same level
1229 b Go one heading backward on same level
1230 u Go one heading up"
1231 :group
'org-edit-structure
1234 (defgroup org-sparse-trees nil
1235 "Options concerning sparse trees in Org-mode."
1236 :tag
"Org Sparse Trees"
1237 :group
'org-structure
)
1239 (defcustom org-highlight-sparse-tree-matches t
1240 "Non-nil means highlight all matches that define a sparse tree.
1241 The highlights will automatically disappear the next time the buffer is
1242 changed by an edit command."
1243 :group
'org-sparse-trees
1246 (defcustom org-remove-highlights-with-change t
1247 "Non-nil means any change to the buffer will remove temporary highlights.
1248 Such highlights are created by `org-occur' and `org-clock-display'.
1249 When nil, `C-c C-c needs to be used to get rid of the highlights.
1250 The highlights created by `org-preview-latex-fragment' always need
1251 `C-c C-c' to be removed."
1252 :group
'org-sparse-trees
1257 (defcustom org-occur-hook
'(org-first-headline-recenter)
1258 "Hook that is run after `org-occur' has constructed a sparse tree.
1259 This can be used to recenter the window to show as much of the structure
1261 :group
'org-sparse-trees
1264 (defgroup org-imenu-and-speedbar nil
1265 "Options concerning imenu and speedbar in Org-mode."
1266 :tag
"Org Imenu and Speedbar"
1267 :group
'org-structure
)
1269 (defcustom org-imenu-depth
2
1270 "The maximum level for Imenu access to Org-mode headlines.
1271 This also applied for speedbar access."
1272 :group
'org-imenu-and-speedbar
1275 (defgroup org-table nil
1276 "Options concerning tables in Org-mode."
1280 (defcustom org-enable-table-editor
'optimized
1281 "Non-nil means lines starting with \"|\" are handled by the table editor.
1282 When nil, such lines will be treated like ordinary lines.
1284 When equal to the symbol `optimized', the table editor will be optimized to
1286 - Automatic overwrite mode in front of whitespace in table fields.
1287 This makes the structure of the table stay in tact as long as the edited
1288 field does not exceed the column width.
1289 - Minimize the number of realigns. Normally, the table is aligned each time
1290 TAB or RET are pressed to move to another field. With optimization this
1291 happens only if changes to a field might have changed the column width.
1292 Optimization requires replacing the functions `self-insert-command',
1293 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1294 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1295 very good at guessing when a re-align will be necessary, but you can always
1296 force one with \\[org-ctrl-c-ctrl-c].
1298 If you would like to use the optimized version in Org-mode, but the
1299 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1301 This variable can be used to turn on and off the table editor during a session,
1302 but in order to toggle optimization, a restart is required.
1304 See also the variable `org-table-auto-blank-field'."
1307 (const :tag
"off" nil
)
1309 (const :tag
"on, optimized" optimized
)))
1311 (defcustom org-self-insert-cluster-for-undo
(or (featurep 'xemacs
)
1312 (version<= emacs-version
"24.1"))
1313 "Non-nil means cluster self-insert commands for undo when possible.
1314 If this is set, then, like in the Emacs command loop, 20 consecutive
1315 characters will be undone together.
1316 This is configurable, because there is some impact on typing performance."
1320 (defcustom org-table-tab-recognizes-table.el t
1321 "Non-nil means TAB will automatically notice a table.el table.
1322 When it sees such a table, it moves point into it and - if necessary -
1323 calls `table-recognize-table'."
1324 :group
'org-table-editing
1327 (defgroup org-link nil
1328 "Options concerning links in Org-mode."
1332 (defvar org-link-abbrev-alist-local nil
1333 "Buffer-local version of `org-link-abbrev-alist', which see.
1334 The value of this is taken from the #+LINK lines.")
1335 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1337 (defcustom org-link-abbrev-alist nil
1338 "Alist of link abbreviations.
1339 The car of each element is a string, to be replaced at the start of a link.
1340 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1341 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1343 [[linkkey:tag][description]]
1345 The 'linkkey' must be a word word, starting with a letter, followed
1346 by letters, numbers, '-' or '_'.
1348 If REPLACE is a string, the tag will simply be appended to create the link.
1349 If the string contains \"%s\", the tag will be inserted there. If the string
1350 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1351 at that point (see the function `url-hexify-string'). If the string contains
1352 the specifier \"%(my-function)\", then the custom function `my-function' will
1353 be invoked: this function takes the tag as its only argument and must return
1356 REPLACE may also be a function that will be called with the tag as the
1357 only argument to create the link, which should be returned as a string.
1359 See the manual for examples."
1363 (string :tag
"Protocol")
1365 (string :tag
"Format")
1368 (defcustom org-descriptive-links t
1369 "Non-nil means Org will display descriptive links.
1370 E.g. [[http://orgmode.org][Org website]] will be displayed as
1371 \"Org Website\", hiding the link itself and just displaying its
1372 description. When set to `nil', Org will display the full links
1375 You can interactively set the value of this variable by calling
1376 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1380 (defcustom org-link-file-path-type
'adaptive
1381 "How the path name in file links should be stored.
1384 relative Relative to the current directory, i.e. the directory of the file
1385 into which the link is being inserted.
1386 absolute Absolute path, if possible with ~ for home directory.
1387 noabbrev Absolute path, no abbreviation of home directory.
1388 adaptive Use relative path for files in the current directory and sub-
1389 directories of it. For other files, use an absolute path."
1397 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1398 "Types of links that should be activated in Org-mode files.
1399 This is a list of symbols, each leading to the activation of a certain link
1400 type. In principle, it does not hurt to turn on most link types - there may
1401 be a small gain when turning off unused link types. The types are:
1403 bracket The recommended [[link][description]] or [[link]] links with hiding.
1404 angle Links in angular brackets that may contain whitespace like
1405 <bbdb:Carsten Dominik>.
1406 plain Plain links in normal text, no whitespace, like http://google.com.
1407 radio Text that is matched by a radio target, see manual for details.
1408 tag Tag settings in a headline (link to tag search).
1409 date Time stamps (link to calendar).
1410 footnote Footnote labels.
1412 Changing this variable requires a restart of Emacs to become effective."
1414 :type
'(set :greedy t
1415 (const :tag
"Double bracket links" bracket
)
1416 (const :tag
"Angular bracket links" angle
)
1417 (const :tag
"Plain text links" plain
)
1418 (const :tag
"Radio target matches" radio
)
1419 (const :tag
"Tags" tag
)
1420 (const :tag
"Timestamps" date
)
1421 (const :tag
"Footnotes" footnote
)))
1423 (defcustom org-make-link-description-function nil
1424 "Function to use for generating link descriptions from links.
1425 When nil, the link location will be used. This function must take
1426 two parameters: the first one is the link, the second one is the
1427 description generated by `org-insert-link'. The function should
1428 return the description to use."
1432 (defgroup org-link-store nil
1433 "Options concerning storing links in Org-mode."
1434 :tag
"Org Store Link"
1437 (defcustom org-url-hexify-p t
1438 "When non-nil, hexify URL when creating a link."
1441 :group
'org-link-store
)
1443 (defcustom org-email-link-description-format
"Email %c: %.30s"
1444 "Format of the description part of a link to an email or usenet message.
1445 The following %-escapes will be replaced by corresponding information:
1447 %F full \"From\" field
1448 %f name, taken from \"From\" field, address if no name
1449 %T full \"To\" field
1450 %t first name in \"To\" field, address if no name
1451 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1452 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1457 You may use normal field width specification between the % and the letter.
1458 This is for example useful to limit the length of the subject.
1460 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1461 :group
'org-link-store
1464 (defcustom org-from-is-user-regexp
1466 (when (and user-mail-address
(not (string= user-mail-address
"")))
1467 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1468 (when (and user-full-name
(not (string= user-full-name
"")))
1469 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1470 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1471 "Regexp matched against the \"From:\" header of an email or usenet message.
1472 It should match if the message is from the user him/herself."
1473 :group
'org-link-store
1476 (defcustom org-context-in-file-links t
1477 "Non-nil means file links from `org-store-link' contain context.
1478 A search string will be added to the file name with :: as separator and
1479 used to find the context when the link is activated by the command
1480 `org-open-at-point'. When this option is t, the entire active region
1481 will be placed in the search string of the file link. If set to a
1482 positive integer, only the first n lines of context will be stored.
1484 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1485 negates this setting for the duration of the command."
1486 :group
'org-link-store
1487 :type
'(choice boolean integer
))
1489 (defcustom org-keep-stored-link-after-insertion nil
1490 "Non-nil means keep link in list for entire session.
1492 The command `org-store-link' adds a link pointing to the current
1493 location to an internal list. These links accumulate during a session.
1494 The command `org-insert-link' can be used to insert links into any
1495 Org-mode file (offering completion for all stored links). When this
1496 option is nil, every link which has been inserted once using \\[org-insert-link]
1497 will be removed from the list, to make completing the unused links
1499 :group
'org-link-store
1502 (defgroup org-link-follow nil
1503 "Options concerning following links in Org-mode."
1504 :tag
"Org Follow Link"
1507 (defcustom org-link-translation-function nil
1508 "Function to translate links with different syntax to Org syntax.
1509 This can be used to translate links created for example by the Planner
1510 or emacs-wiki packages to Org syntax.
1511 The function must accept two parameters, a TYPE containing the link
1512 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1513 which is everything after the link protocol. It should return a cons
1514 with possibly modified values of type and path.
1515 Org contains a function for this, so if you set this variable to
1516 `org-translate-link-from-planner', you should be able follow many
1517 links created by planner."
1518 :group
'org-link-follow
1521 (defcustom org-follow-link-hook nil
1522 "Hook that is run after a link has been followed."
1523 :group
'org-link-follow
1526 (defcustom org-tab-follows-link nil
1527 "Non-nil means on links TAB will follow the link.
1528 Needs to be set before org.el is loaded.
1529 This really should not be used, it does not make sense, and the
1530 implementation is bad."
1531 :group
'org-link-follow
1534 (defcustom org-return-follows-link nil
1535 "Non-nil means on links RET will follow the link."
1536 :group
'org-link-follow
1539 (defcustom org-mouse-1-follows-link
1540 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1541 "Non-nil means mouse-1 on a link will follow the link.
1542 A longer mouse click will still set point. Does not work on XEmacs.
1543 Needs to be set before org.el is loaded."
1544 :group
'org-link-follow
1547 (defcustom org-mark-ring-length
4
1548 "Number of different positions to be recorded in the ring.
1549 Changing this requires a restart of Emacs to work correctly."
1550 :group
'org-link-follow
1553 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1554 "Non-nil means internal links in Org files must exactly match a headline.
1555 When nil, the link search tries to match a phrase with all words
1556 in the search text."
1557 :group
'org-link-follow
1560 (const :tag
"Use fuzzy text search" nil
)
1561 (const :tag
"Match only exact headline" t
)
1562 (const :tag
"Match exact headline or query to create it"
1565 (defcustom org-link-frame-setup
1566 '((vm . vm-visit-folder-other-frame
)
1567 (vm-imap . vm-visit-imap-folder-other-frame
)
1568 (gnus . org-gnus-no-new-news
)
1569 (file . find-file-other-window
)
1570 (wl . wl-other-frame
))
1571 "Setup the frame configuration for following links.
1572 When following a link with Emacs, it may often be useful to display
1573 this link in another window or frame. This variable can be used to
1574 set this up for the different types of links.
1577 `vm-visit-folder-other-window'
1578 `vm-visit-folder-other-frame'
1579 For Gnus, use any of
1582 `org-gnus-no-new-news'
1583 For FILE, use any of
1585 `find-file-other-window'
1586 `find-file-other-frame'
1587 For Wanderlust use any of
1590 For the calendar, use the variable `calendar-setup'.
1591 For BBDB, it is currently only possible to display the matches in
1593 :group
'org-link-follow
1597 (const vm-visit-folder
)
1598 (const vm-visit-folder-other-window
)
1599 (const vm-visit-folder-other-frame
)))
1603 (const gnus-other-frame
)
1604 (const org-gnus-no-new-news
)))
1608 (const find-file-other-window
)
1609 (const find-file-other-frame
)))
1613 (const wl-other-frame
)))))
1615 (defcustom org-display-internal-link-with-indirect-buffer nil
1616 "Non-nil means use indirect buffer to display infile links.
1617 Activating internal links (from one location in a file to another location
1618 in the same file) normally just jumps to the location. When the link is
1619 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1621 another window. When this option is set, the other window actually displays
1622 an indirect buffer clone of the current buffer, to avoid any visibility
1623 changes to the current buffer."
1624 :group
'org-link-follow
1627 (defcustom org-open-non-existing-files nil
1628 "Non-nil means `org-open-file' will open non-existing files.
1629 When nil, an error will be generated.
1630 This variable applies only to external applications because they
1631 might choke on non-existing files. If the link is to a file that
1632 will be opened in Emacs, the variable is ignored."
1633 :group
'org-link-follow
1636 (defcustom org-open-directory-means-index-dot-org nil
1637 "Non-nil means a link to a directory really means to index.org.
1638 When nil, following a directory link will run dired or open a finder/explorer
1639 window on that directory."
1640 :group
'org-link-follow
1643 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1644 "Function and arguments to call for following mailto links.
1645 This is a list with the first element being a Lisp function, and the
1646 remaining elements being arguments to the function. In string arguments,
1647 %a will be replaced by the address, and %s will be replaced by the subject
1648 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1649 :group
'org-link-follow
1651 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1652 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1653 (const :tag
"message-mail" (message-mail "%a" "%s"))
1654 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1656 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1657 "Non-nil means ask for confirmation before executing shell links.
1658 Shell links can be dangerous: just think about a link
1660 [[shell:rm -rf ~/*][Google Search]]
1662 This link would show up in your Org-mode document as \"Google Search\",
1663 but really it would remove your entire home directory.
1664 Therefore we advise against setting this variable to nil.
1665 Just change it to `y-or-n-p' if you want to confirm with a
1666 single keystroke rather than having to type \"yes\"."
1667 :group
'org-link-follow
1669 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1670 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1671 (const :tag
"no confirmation (dangerous)" nil
)))
1672 (put 'org-confirm-shell-link-function
1673 'safe-local-variable
1674 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1676 (defcustom org-confirm-shell-link-not-regexp
""
1677 "A regexp to skip confirmation for shell links."
1678 :group
'org-link-follow
1682 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1683 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1684 Elisp links can be dangerous: just think about a link
1686 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1688 This link would show up in your Org-mode document as \"Google Search\",
1689 but really it would remove your entire home directory.
1690 Therefore we advise against setting this variable to nil.
1691 Just change it to `y-or-n-p' if you want to confirm with a
1692 single keystroke rather than having to type \"yes\"."
1693 :group
'org-link-follow
1695 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1696 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1697 (const :tag
"no confirmation (dangerous)" nil
)))
1698 (put 'org-confirm-shell-link-function
1699 'safe-local-variable
1700 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1702 (defcustom org-confirm-elisp-link-not-regexp
""
1703 "A regexp to skip confirmation for Elisp links."
1704 :group
'org-link-follow
1708 (defconst org-file-apps-defaults-gnu
1712 "Default file applications on a UNIX or GNU/Linux system.
1713 See `org-file-apps'.")
1715 (defconst org-file-apps-defaults-macosx
1718 (system .
"open %s")
1720 ("eps.gz" .
"gv %s")
1722 ("fig" .
"xfig %s"))
1723 "Default file applications on a MacOS X system.
1724 The system \"open\" is known as a default, but we use X11 applications
1725 for some files for which the OS does not have a good default.
1726 See `org-file-apps'.")
1728 (defconst org-file-apps-defaults-windowsnt
1732 (list (if (featurep 'xemacs
)
1733 'mswindows-shell-execute
1737 (list (if (featurep 'xemacs
)
1738 'mswindows-shell-execute
1741 "Default file applications on a Windows NT system.
1742 The system \"open\" is used for most files.
1743 See `org-file-apps'.")
1745 (defcustom org-file-apps
1748 ("\\.mm\\'" . default
)
1749 ("\\.x?html?\\'" . default
)
1750 ("\\.pdf\\'" . default
)
1752 "External applications for opening `file:path' items in a document.
1753 Org-mode uses system defaults for different file types, but
1754 you can use this variable to set the application for a given file
1755 extension. The entries in this list are cons cells where the car identifies
1756 files and the cdr the corresponding command. Possible values for the
1758 \"string\" A string as a file identifier can be interpreted in different
1759 ways, depending on its contents:
1761 - Alphanumeric characters only:
1762 Match links with this file extension.
1763 Example: (\"pdf\" . \"evince %s\")
1764 to open PDFs with evince.
1766 - Regular expression: Match links where the
1767 filename matches the regexp. If you want to
1768 use groups here, use shy groups.
1770 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1771 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1772 to open *.html and *.xhtml with firefox.
1774 - Regular expression which contains (non-shy) groups:
1775 Match links where the whole link, including \"::\", and
1776 anything after that, matches the regexp.
1777 In a custom command string, %1, %2, etc. are replaced with
1778 the parts of the link that were matched by the groups.
1779 For backwards compatibility, if a command string is given
1780 that does not use any of the group matches, this case is
1781 handled identically to the second one (i.e. match against
1783 In a custom lisp form, you can access the group matches with
1784 (match-string n link).
1786 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1787 to open [[file:document.pdf::5]] with evince at page 5.
1789 `directory' Matches a directory
1790 `remote' Matches a remote file, accessible through tramp or efs.
1791 Remote files most likely should be visited through Emacs
1792 because external applications cannot handle such paths.
1793 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1794 so all files Emacs knows how to handle. Using this with
1795 command `emacs' will open most files in Emacs. Beware that this
1796 will also open html files inside Emacs, unless you add
1797 (\"html\" . default) to the list as well.
1798 t Default for files not matched by any of the other options.
1799 `system' The system command to open files, like `open' on Windows
1800 and Mac OS X, and mailcap under GNU/Linux. This is the command
1801 that will be selected if you call `C-c C-o' with a double
1802 \\[universal-argument] \\[universal-argument] prefix.
1804 Possible values for the command are:
1805 `emacs' The file will be visited by the current Emacs process.
1806 `default' Use the default application for this file type, which is the
1807 association for t in the list, most likely in the system-specific
1809 This can be used to overrule an unwanted setting in the
1810 system-specific variable.
1811 `system' Use the system command for opening files, like \"open\".
1812 This command is specified by the entry whose car is `system'.
1813 Most likely, the system-specific version of this variable
1814 does define this command, but you can overrule/replace it
1816 string A command to be executed by a shell; %s will be replaced
1817 by the path to the file.
1818 sexp A Lisp form which will be evaluated. The file path will
1819 be available in the Lisp variable `file'.
1820 For more examples, see the system specific constants
1821 `org-file-apps-defaults-macosx'
1822 `org-file-apps-defaults-windowsnt'
1823 `org-file-apps-defaults-gnu'."
1824 :group
'org-link-follow
1826 (cons (choice :value
""
1827 (string :tag
"Extension")
1828 (const :tag
"System command to open files" system
)
1829 (const :tag
"Default for unrecognized files" t
)
1830 (const :tag
"Remote file" remote
)
1831 (const :tag
"Links to a directory" directory
)
1832 (const :tag
"Any files that have Emacs modes"
1835 (const :tag
"Visit with Emacs" emacs
)
1836 (const :tag
"Use default" default
)
1837 (const :tag
"Use the system command" system
)
1838 (string :tag
"Command")
1839 (sexp :tag
"Lisp form")))))
1841 (defcustom org-doi-server-url
"http://dx.doi.org/"
1842 "The URL of the DOI server."
1845 :group
'org-link-follow
)
1847 (defgroup org-refile nil
1848 "Options concerning refiling entries in Org-mode."
1852 (defcustom org-directory
"~/org"
1853 "Directory with org files.
1854 This is just a default location to look for Org files. There is no need
1855 at all to put your files into this directory. It is only used in the
1856 following situations:
1858 1. When a capture template specifies a target file that is not an
1859 absolute path. The path will then be interpreted relative to
1861 2. When a capture note is filed away in an interactive way (when exiting the
1862 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1863 with `org-directory' as the default path."
1865 :group
'org-remember
1869 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1870 "Default target for storing notes.
1871 Used as a fall back file for org-remember.el and org-capture.el, for
1872 templates that do not specify a target file."
1874 :group
'org-remember
1877 (const :tag
"Default from remember-data-file" nil
)
1880 (defcustom org-goto-interface
'outline
1881 "The default interface to be used for `org-goto'.
1883 outline The interface shows an outline of the relevant file
1884 and the correct heading is found by moving through
1885 the outline or by searching with incremental search.
1886 outline-path-completion Headlines in the current buffer are offered via
1887 completion. This is the interface also used by
1888 the refile command."
1891 (const :tag
"Outline" outline
)
1892 (const :tag
"Outline-path-completion" outline-path-completion
)))
1894 (defcustom org-goto-max-level
5
1895 "Maximum target level when running `org-goto' with refile interface."
1899 (defcustom org-reverse-note-order nil
1900 "Non-nil means store new notes at the beginning of a file or entry.
1901 When nil, new notes will be filed to the end of a file or entry.
1902 This can also be a list with cons cells of regular expressions that
1903 are matched against file names, and values."
1904 :group
'org-remember
1908 (const :tag
"Reverse always" t
)
1909 (const :tag
"Reverse never" nil
)
1910 (repeat :tag
"By file name regexp"
1911 (cons regexp boolean
))))
1913 (defcustom org-log-refile nil
1914 "Information to record when a task is refiled.
1916 Possible values are:
1918 nil Don't add anything
1919 time Add a time stamp to the task
1920 note Prompt for a note and add it with template `org-log-note-headings'
1922 This option can also be set with on a per-file-basis with
1924 #+STARTUP: nologrefile
1925 #+STARTUP: logrefile
1926 #+STARTUP: lognoterefile
1928 You can have local logging settings for a subtree by setting the LOGGING
1929 property to one or more of these keywords.
1931 When bulk-refiling from the agenda, the value `note' is forbidden and
1932 will temporarily be changed to `time'."
1934 :group
'org-progress
1937 (const :tag
"No logging" nil
)
1938 (const :tag
"Record timestamp" time
)
1939 (const :tag
"Record timestamp with note." note
)))
1941 (defcustom org-refile-targets nil
1942 "Targets for refiling entries with \\[org-refile].
1943 This is a list of cons cells. Each cell contains:
1944 - a specification of the files to be considered, either a list of files,
1945 or a symbol whose function or variable value will be used to retrieve
1946 a file name or a list of file names. If you use `org-agenda-files' for
1947 that, all agenda files will be scanned for targets. Nil means consider
1948 headings in the current buffer.
1949 - A specification of how to find candidate refile targets. This may be
1951 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1952 This tag has to be present in all target headlines, inheritance will
1954 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1956 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1957 headlines that are refiling targets.
1958 - a cons cell (:level . N). Any headline of level N is considered a target.
1959 Note that, when `org-odd-levels-only' is set, level corresponds to
1960 order in hierarchy, not to the number of stars.
1961 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1962 Note that, when `org-odd-levels-only' is set, level corresponds to
1963 order in hierarchy, not to the number of stars.
1965 Each element of this list generates a set of possible targets.
1966 The union of these sets is presented (with completion) to
1967 the user by `org-refile'.
1969 You can set the variable `org-refile-target-verify-function' to a function
1970 to verify each headline found by the simple criteria above.
1972 When this variable is nil, all top-level headlines in the current buffer
1973 are used, equivalent to the value `((nil . (:level . 1))'."
1977 (choice :value org-agenda-files
1978 (const :tag
"All agenda files" org-agenda-files
)
1979 (const :tag
"Current buffer" nil
)
1980 (function) (variable) (file))
1981 (choice :tag
"Identify target headline by"
1982 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1983 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1984 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1985 (cons :tag
"Level number" (const :value
:level
) (integer))
1986 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1988 (defcustom org-refile-target-verify-function nil
1989 "Function to verify if the headline at point should be a refile target.
1990 The function will be called without arguments, with point at the
1991 beginning of the headline. It should return t and leave point
1992 where it is if the headline is a valid target for refiling.
1994 If the target should not be selected, the function must return nil.
1995 In addition to this, it may move point to a place from where the search
1996 should be continued. For example, the function may decide that the entire
1997 subtree of the current entry should be excluded and move point to the end
2002 (defcustom org-refile-use-cache nil
2003 "Non-nil means cache refile targets to speed up the process.
2004 The cache for a particular file will be updated automatically when
2005 the buffer has been killed, or when any of the marker used for flagging
2006 refile targets no longer points at a live buffer.
2007 If you have added new entries to a buffer that might themselves be targets,
2008 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
2009 find that easier, `C-u C-u C-u C-c C-w'."
2014 (defcustom org-refile-use-outline-path nil
2015 "Non-nil means provide refile targets as paths.
2016 So a level 3 headline will be available as level1/level2/level3.
2018 When the value is `file', also include the file name (without directory)
2019 into the path. In this case, you can also stop the completion after
2020 the file name, to get entries inserted as top level in the file.
2022 When `full-file-path', include the full file path."
2025 (const :tag
"Not" nil
)
2026 (const :tag
"Yes" t
)
2027 (const :tag
"Start with file name" file
)
2028 (const :tag
"Start with full file path" full-file-path
)))
2030 (defcustom org-outline-path-complete-in-steps t
2031 "Non-nil means complete the outline path in hierarchical steps.
2032 When Org-mode uses the refile interface to select an outline path
2033 \(see variable `org-refile-use-outline-path'), the completion of
2034 the path can be done is a single go, or if can be done in steps down
2035 the headline hierarchy. Going in steps is probably the best if you
2036 do not use a special completion package like `ido' or `icicles'.
2037 However, when using these packages, going in one step can be very
2038 fast, while still showing the whole path to the entry."
2042 (defcustom org-refile-allow-creating-parent-nodes nil
2043 "Non-nil means allow to create new nodes as refile targets.
2044 New nodes are then created by adding \"/new node name\" to the completion
2045 of an existing node. When the value of this variable is `confirm',
2046 new node creation must be confirmed by the user (recommended)
2047 When nil, the completion must match an existing entry.
2049 Note that, if the new heading is not seen by the criteria
2050 listed in `org-refile-targets', multiple instances of the same
2051 heading would be created by trying again to file under the new
2055 (const :tag
"Never" nil
)
2056 (const :tag
"Always" t
)
2057 (const :tag
"Prompt for confirmation" confirm
)))
2059 (defcustom org-refile-active-region-within-subtree nil
2060 "Non-nil means also refile active region within a subtree.
2062 By default `org-refile' doesn't allow refiling regions if they
2063 don't contain a set of subtrees, but it might be convenient to
2064 do so sometimes: in that case, the first line of the region is
2065 converted to a headline before refiling."
2070 (defgroup org-todo nil
2071 "Options concerning TODO items in Org-mode."
2075 (defgroup org-progress nil
2076 "Options concerning Progress logging in Org-mode."
2080 (defvar org-todo-interpretation-widgets
2081 '((:tag
"Sequence (cycling hits every state)" sequence
)
2082 (:tag
"Type (cycling directly to DONE)" type
))
2083 "The available interpretation symbols for customizing `org-todo-keywords'.
2084 Interested libraries should add to this list.")
2086 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2087 "List of TODO entry keyword sequences and their interpretation.
2088 \\<org-mode-map>This is a list of sequences.
2090 Each sequence starts with a symbol, either `sequence' or `type',
2091 indicating if the keywords should be interpreted as a sequence of
2092 action steps, or as different types of TODO items. The first
2093 keywords are states requiring action - these states will select a headline
2094 for inclusion into the global TODO list Org-mode produces. If one of
2095 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2096 signify that no further action is necessary. If \"|\" is not found,
2097 the last keyword is treated as the only DONE state of the sequence.
2099 The command \\[org-todo] cycles an entry through these states, and one
2100 additional state where no keyword is present. For details about this
2101 cycling, see the manual.
2103 TODO keywords and interpretation can also be set on a per-file basis with
2104 the special #+SEQ_TODO and #+TYP_TODO lines.
2106 Each keyword can optionally specify a character for fast state selection
2107 \(in combination with the variable `org-use-fast-todo-selection')
2108 and specifiers for state change logging, using the same syntax that
2109 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2110 the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2111 indicates to record a time stamp each time this state is selected.
2113 Each keyword may also specify if a timestamp or a note should be
2114 recorded when entering or leaving the state, by adding additional
2115 characters in the parenthesis after the keyword. This looks like this:
2116 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2117 record only the time of the state change. With X and Y being either
2118 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2119 Y when leaving the state if and only if the *target* state does not
2120 define X. You may omit any of the fast-selection key or X or /Y,
2121 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2123 For backward compatibility, this variable may also be just a list
2124 of keywords. In this case the interpretation (sequence or type) will be
2125 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2127 :group
'org-keywords
2129 (repeat :tag
"Old syntax, just keywords"
2130 (string :tag
"Keyword"))
2131 (repeat :tag
"New syntax"
2134 :tag
"Interpretation"
2135 ;;Quick and dirty way to see
2136 ;;`org-todo-interpretations'. This takes the
2137 ;;place of item arguments
2145 org-todo-interpretation-widgets
))
2148 (string :tag
"Keyword"))))))
2150 (defvar org-todo-keywords-1 nil
2151 "All TODO and DONE keywords active in a buffer.")
2152 (make-variable-buffer-local 'org-todo-keywords-1
)
2153 (defvar org-todo-keywords-for-agenda nil
)
2154 (defvar org-done-keywords-for-agenda nil
)
2155 (defvar org-drawers-for-agenda nil
)
2156 (defvar org-todo-keyword-alist-for-agenda nil
)
2157 (defvar org-tag-alist-for-agenda nil
)
2158 (defvar org-agenda-contributing-files nil
)
2159 (defvar org-not-done-keywords nil
)
2160 (make-variable-buffer-local 'org-not-done-keywords
)
2161 (defvar org-done-keywords nil
)
2162 (make-variable-buffer-local 'org-done-keywords
)
2163 (defvar org-todo-heads nil
)
2164 (make-variable-buffer-local 'org-todo-heads
)
2165 (defvar org-todo-sets nil
)
2166 (make-variable-buffer-local 'org-todo-sets
)
2167 (defvar org-todo-log-states nil
)
2168 (make-variable-buffer-local 'org-todo-log-states
)
2169 (defvar org-todo-kwd-alist nil
)
2170 (make-variable-buffer-local 'org-todo-kwd-alist
)
2171 (defvar org-todo-key-alist nil
)
2172 (make-variable-buffer-local 'org-todo-key-alist
)
2173 (defvar org-todo-key-trigger nil
)
2174 (make-variable-buffer-local 'org-todo-key-trigger
)
2176 (defcustom org-todo-interpretation
'sequence
2177 "Controls how TODO keywords are interpreted.
2178 This variable is in principle obsolete and is only used for
2179 backward compatibility, if the interpretation of todo keywords is
2180 not given already in `org-todo-keywords'. See that variable for
2183 :group
'org-keywords
2184 :type
'(choice (const sequence
)
2187 (defcustom org-use-fast-todo-selection t
2188 "Non-nil means use the fast todo selection scheme with C-c C-t.
2189 This variable describes if and under what circumstances the cycling
2190 mechanism for TODO keywords will be replaced by a single-key, direct
2193 When nil, fast selection is never used.
2195 When the symbol `prefix', it will be used when `org-todo' is called
2196 with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
2197 `C-u t' in an agenda buffer.
2199 When t, fast selection is used by default. In this case, the prefix
2200 argument forces cycling instead.
2202 In all cases, the special interface is only used if access keys have
2203 actually been assigned by the user, i.e. if keywords in the configuration
2204 are followed by a letter in parenthesis, like TODO(t)."
2207 (const :tag
"Never" nil
)
2208 (const :tag
"By default" t
)
2209 (const :tag
"Only with C-u C-c C-t" prefix
)))
2211 (defcustom org-provide-todo-statistics t
2212 "Non-nil means update todo statistics after insert and toggle.
2213 ALL-HEADLINES means update todo statistics by including headlines
2214 with no TODO keyword as well, counting them as not done.
2215 A list of TODO keywords means the same, but skip keywords that are
2218 When this is set, todo statistics is updated in the parent of the
2219 current entry each time a todo state is changed."
2222 (const :tag
"Yes, only for TODO entries" t
)
2223 (const :tag
"Yes, including all entries" 'all-headlines
)
2224 (repeat :tag
"Yes, for TODOs in this list"
2225 (string :tag
"TODO keyword"))
2226 (other :tag
"No TODO statistics" nil
)))
2228 (defcustom org-hierarchical-todo-statistics t
2229 "Non-nil means TODO statistics covers just direct children.
2230 When nil, all entries in the subtree are considered.
2231 This has only an effect if `org-provide-todo-statistics' is set.
2232 To set this to nil for only a single subtree, use a COOKIE_DATA
2233 property and include the word \"recursive\" into the value."
2237 (defcustom org-after-todo-state-change-hook nil
2238 "Hook which is run after the state of a TODO item was changed.
2239 The new state (a string with a TODO keyword, or nil) is available in the
2240 Lisp variable `org-state'."
2244 (defvar org-blocker-hook nil
2245 "Hook for functions that are allowed to block a state change.
2247 Functions in this hook should not modify the buffer.
2248 Each function gets as its single argument a property list,
2249 see `org-trigger-hook' for more information about this list.
2251 If any of the functions in this hook returns nil, the state change
2254 (defvar org-trigger-hook nil
2255 "Hook for functions that are triggered by a state change.
2257 Each function gets as its single argument a property list with at
2258 least the following elements:
2260 (:type type-of-change :position pos-at-entry-start
2261 :from old-state :to new-state)
2263 Depending on the type, more properties may be present.
2265 This mechanism is currently implemented for:
2269 :type todo-state-change
2270 :from previous state (keyword as a string), or nil, or a symbol
2271 'todo' or 'done', to indicate the general type of state.
2272 :to new state, like in :from")
2274 (defcustom org-enforce-todo-dependencies nil
2275 "Non-nil means undone TODO entries will block switching the parent to DONE.
2276 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2277 be blocked if any prior sibling is not yet done.
2278 Finally, if the parent is blocked because of ordered siblings of its own,
2279 the child will also be blocked."
2280 :set
(lambda (var val
)
2283 (add-hook 'org-blocker-hook
2284 'org-block-todo-from-children-or-siblings-or-parent
)
2285 (remove-hook 'org-blocker-hook
2286 'org-block-todo-from-children-or-siblings-or-parent
)))
2290 (defcustom org-enforce-todo-checkbox-dependencies nil
2291 "Non-nil means unchecked boxes will block switching the parent to DONE.
2292 When this is nil, checkboxes have no influence on switching TODO states.
2293 When non-nil, you first need to check off all check boxes before the TODO
2294 entry can be switched to DONE.
2295 This variable needs to be set before org.el is loaded, and you need to
2296 restart Emacs after a change to make the change effective. The only way
2297 to change is while Emacs is running is through the customize interface."
2298 :set
(lambda (var val
)
2301 (add-hook 'org-blocker-hook
2302 'org-block-todo-from-checkboxes
)
2303 (remove-hook 'org-blocker-hook
2304 'org-block-todo-from-checkboxes
)))
2308 (defcustom org-treat-insert-todo-heading-as-state-change nil
2309 "Non-nil means inserting a TODO heading is treated as state change.
2310 So when the command \\[org-insert-todo-heading] is used, state change
2311 logging will apply if appropriate. When nil, the new TODO item will
2312 be inserted directly, and no logging will take place."
2316 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2317 "Non-nil means switching TODO states with S-cursor counts as state change.
2318 This is the default behavior. However, setting this to nil allows a
2319 convenient way to select a TODO state and bypass any logging associated
2324 (defcustom org-todo-state-tags-triggers nil
2325 "Tag changes that should be triggered by TODO state changes.
2326 This is a list. Each entry is
2328 (state-change (tag . flag) .......)
2330 State-change can be a string with a state, and empty string to indicate the
2331 state that has no TODO keyword, or it can be one of the symbols `todo'
2332 or `done', meaning any not-done or done state, respectively."
2336 (cons (choice :tag
"When changing to"
2337 (const :tag
"Not-done state" todo
)
2338 (const :tag
"Done state" done
)
2339 (string :tag
"State"))
2341 (cons :tag
"Tag action"
2343 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2345 (defcustom org-log-done nil
2346 "Information to record when a task moves to the DONE state.
2348 Possible values are:
2350 nil Don't add anything, just change the keyword
2351 time Add a time stamp to the task
2352 note Prompt for a note and add it with template `org-log-note-headings'
2354 This option can also be set with on a per-file-basis with
2356 #+STARTUP: nologdone
2358 #+STARTUP: lognotedone
2360 You can have local logging settings for a subtree by setting the LOGGING
2361 property to one or more of these keywords."
2363 :group
'org-progress
2365 (const :tag
"No logging" nil
)
2366 (const :tag
"Record CLOSED timestamp" time
)
2367 (const :tag
"Record CLOSED timestamp with note." note
)))
2369 ;; Normalize old uses of org-log-done.
2371 ((eq org-log-done t
) (setq org-log-done
'time
))
2372 ((and (listp org-log-done
) (memq 'done org-log-done
))
2373 (setq org-log-done
'note
)))
2375 (defcustom org-log-reschedule nil
2376 "Information to record when the scheduling date of a tasks is modified.
2378 Possible values are:
2380 nil Don't add anything, just change the date
2381 time Add a time stamp to the task
2382 note Prompt for a note and add it with template `org-log-note-headings'
2384 This option can also be set with on a per-file-basis with
2386 #+STARTUP: nologreschedule
2387 #+STARTUP: logreschedule
2388 #+STARTUP: lognotereschedule"
2390 :group
'org-progress
2392 (const :tag
"No logging" nil
)
2393 (const :tag
"Record timestamp" time
)
2394 (const :tag
"Record timestamp with note." note
)))
2396 (defcustom org-log-redeadline nil
2397 "Information to record when the deadline date of a tasks is modified.
2399 Possible values are:
2401 nil Don't add anything, just change the date
2402 time Add a time stamp to the task
2403 note Prompt for a note and add it with template `org-log-note-headings'
2405 This option can also be set with on a per-file-basis with
2407 #+STARTUP: nologredeadline
2408 #+STARTUP: logredeadline
2409 #+STARTUP: lognoteredeadline
2411 You can have local logging settings for a subtree by setting the LOGGING
2412 property to one or more of these keywords."
2414 :group
'org-progress
2416 (const :tag
"No logging" nil
)
2417 (const :tag
"Record timestamp" time
)
2418 (const :tag
"Record timestamp with note." note
)))
2420 (defcustom org-log-note-clock-out nil
2421 "Non-nil means record a note when clocking out of an item.
2422 This can also be configured on a per-file basis by adding one of
2423 the following lines anywhere in the buffer:
2425 #+STARTUP: lognoteclock-out
2426 #+STARTUP: nolognoteclock-out"
2428 :group
'org-progress
2431 (defcustom org-log-done-with-time t
2432 "Non-nil means the CLOSED time stamp will contain date and time.
2433 When nil, only the date will be recorded."
2434 :group
'org-progress
2437 (defcustom org-log-note-headings
2438 '((done .
"CLOSING NOTE %t")
2439 (state .
"State %-12s from %-12S %t")
2440 (note .
"Note taken on %t")
2441 (reschedule .
"Rescheduled from %S on %t")
2442 (delschedule .
"Not scheduled, was %S on %t")
2443 (redeadline .
"New deadline from %S on %t")
2444 (deldeadline .
"Removed deadline, was %S on %t")
2445 (refile .
"Refiled on %t")
2447 "Headings for notes added to entries.
2448 The value is an alist, with the car being a symbol indicating the note
2449 context, and the cdr is the heading to be used. The heading may also be the
2451 %t in the heading will be replaced by a time stamp.
2452 %T will be an active time stamp instead the default inactive one
2453 %d will be replaced by a short-format time stamp.
2454 %D will be replaced by an active short-format time stamp.
2455 %s will be replaced by the new TODO state, in double quotes.
2456 %S will be replaced by the old TODO state, in double quotes.
2457 %u will be replaced by the user name.
2458 %U will be replaced by the full user name.
2460 In fact, it is not a good idea to change the `state' entry, because
2461 agenda log mode depends on the format of these entries."
2463 :group
'org-progress
2464 :type
'(list :greedy t
2465 (cons (const :tag
"Heading when closing an item" done
) string
)
2467 "Heading when changing todo state (todo sequence only)"
2469 (cons (const :tag
"Heading when just taking a note" note
) string
)
2470 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2471 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2472 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2473 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2474 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2475 (cons (const :tag
"Heading when refiling" refile
) string
)))
2477 (unless (assq 'note org-log-note-headings
)
2478 (push '(note .
"%t") org-log-note-headings
))
2480 (defcustom org-log-into-drawer nil
2481 "Non-nil means insert state change notes and time stamps into a drawer.
2482 When nil, state changes notes will be inserted after the headline and
2483 any scheduling and clock lines, but not inside a drawer.
2485 The value of this variable should be the name of the drawer to use.
2486 LOGBOOK is proposed as the default drawer for this purpose, you can
2487 also set this to a string to define the drawer of your choice.
2489 A value of t is also allowed, representing \"LOGBOOK\".
2491 If this variable is set, `org-log-state-notes-insert-after-drawers'
2494 You can set the property LOG_INTO_DRAWER to overrule this setting for
2497 :group
'org-progress
2499 (const :tag
"Not into a drawer" nil
)
2500 (const :tag
"LOGBOOK" t
)
2501 (string :tag
"Other")))
2503 (if (fboundp 'defvaralias
)
2504 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2506 (defun org-log-into-drawer ()
2507 "Return the value of `org-log-into-drawer', but let properties overrule.
2508 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2509 used instead of the default value."
2510 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit t
)))
2512 ((not p
) org-log-into-drawer
)
2513 ((equal p
"nil") nil
)
2514 ((equal p
"t") "LOGBOOK")
2517 (defcustom org-log-state-notes-insert-after-drawers nil
2518 "Non-nil means insert state change notes after any drawers in entry.
2519 Only the drawers that *immediately* follow the headline and the
2520 deadline/scheduled line are skipped.
2521 When nil, insert notes right after the heading and perhaps the line
2522 with deadline/scheduling if present.
2524 This variable will have no effect if `org-log-into-drawer' is
2527 :group
'org-progress
2530 (defcustom org-log-states-order-reversed t
2531 "Non-nil means the latest state note will be directly after heading.
2532 When nil, the state change notes will be ordered according to time."
2534 :group
'org-progress
2537 (defcustom org-todo-repeat-to-state nil
2538 "The TODO state to which a repeater should return the repeating task.
2539 By default this is the first task in a TODO sequence, or the previous state
2540 in a TODO_TYP set. But you can specify another task here.
2541 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2544 :type
'(choice (const :tag
"Head of sequence" nil
)
2545 (string :tag
"Specific state")))
2547 (defcustom org-log-repeat
'time
2548 "Non-nil means record moving through the DONE state when triggering repeat.
2549 An auto-repeating task is immediately switched back to TODO when
2550 marked DONE. If you are not logging state changes (by adding \"@\"
2551 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2552 record a closing note, there will be no record of the task moving
2553 through DONE. This variable forces taking a note anyway.
2555 nil Don't force a record
2556 time Record a time stamp
2557 note Prompt for a note and add it with template `org-log-note-headings'
2559 This option can also be set with on a per-file-basis with
2561 #+STARTUP: nologrepeat
2562 #+STARTUP: logrepeat
2563 #+STARTUP: lognoterepeat
2565 You can have local logging settings for a subtree by setting the LOGGING
2566 property to one or more of these keywords."
2568 :group
'org-progress
2570 (const :tag
"Don't force a record" nil
)
2571 (const :tag
"Force recording the DONE state" time
)
2572 (const :tag
"Force recording a note with the DONE state" note
)))
2575 (defgroup org-priorities nil
2576 "Priorities in Org-mode."
2577 :tag
"Org Priorities"
2580 (defcustom org-enable-priority-commands t
2581 "Non-nil means priority commands are active.
2582 When nil, these commands will be disabled, so that you never accidentally
2584 :group
'org-priorities
2587 (defcustom org-highest-priority ?A
2588 "The highest priority of TODO items. A character like ?A, ?B etc.
2589 Must have a smaller ASCII number than `org-lowest-priority'."
2590 :group
'org-priorities
2593 (defcustom org-lowest-priority ?C
2594 "The lowest priority of TODO items. A character like ?A, ?B etc.
2595 Must have a larger ASCII number than `org-highest-priority'."
2596 :group
'org-priorities
2599 (defcustom org-default-priority ?B
2600 "The default priority of TODO items.
2601 This is the priority an item gets if no explicit priority is given.
2602 When starting to cycle on an empty priority the first step in the cycle
2603 depends on `org-priority-start-cycle-with-default'. The resulting first
2604 step priority must not exceed the range from `org-highest-priority' to
2605 `org-lowest-priority' which means that `org-default-priority' has to be
2606 in this range exclusive or inclusive the range boundaries. Else the
2607 first step refuses to set the default and the second will fall back
2608 to (depending on the command used) the highest or lowest priority."
2609 :group
'org-priorities
2612 (defcustom org-priority-start-cycle-with-default t
2613 "Non-nil means start with default priority when starting to cycle.
2614 When this is nil, the first step in the cycle will be (depending on the
2615 command used) one higher or lower than the default priority.
2616 See also `org-default-priority'."
2617 :group
'org-priorities
2620 (defcustom org-get-priority-function nil
2621 "Function to extract the priority from a string.
2622 The string is normally the headline. If this is nil Org computes the
2623 priority from the priority cookie like [#A] in the headline. It returns
2624 an integer, increasing by 1000 for each priority level.
2625 The user can set a different function here, which should take a string
2626 as an argument and return the numeric priority."
2627 :group
'org-priorities
2631 (defgroup org-time nil
2632 "Options concerning time stamps and deadlines in Org-mode."
2636 (defcustom org-insert-labeled-timestamps-at-point nil
2637 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2638 When nil, these labeled time stamps are forces into the second line of an
2639 entry, just after the headline. When scheduling from the global TODO list,
2640 the time stamp will always be forced into the second line."
2644 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2645 "Formats for `format-time-string' which are used for time stamps.
2646 It is not recommended to change this constant.")
2648 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2649 "Number of minutes to round time stamps to.
2650 These are two values, the first applies when first creating a time stamp.
2651 The second applies when changing it with the commands `S-up' and `S-down'.
2652 When changing the time stamp, this means that it will change in steps
2653 of N minutes, as given by the second value.
2655 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2656 numbers should be factors of 60, so for example 5, 10, 15.
2658 When this is larger than 1, you can still force an exact time stamp by using
2659 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2660 and by using a prefix arg to `S-up/down' to specify the exact number
2661 of minutes to shift."
2663 :get
#'(lambda (var) ; Make sure both elements are there
2664 (if (integerp (default-value var
))
2665 (list (default-value var
) 5)
2666 (default-value var
)))
2668 (integer :tag
"when inserting times")
2669 (integer :tag
"when modifying times")))
2671 ;; Normalize old customizations of this variable.
2672 (when (integerp org-time-stamp-rounding-minutes
)
2673 (setq org-time-stamp-rounding-minutes
2674 (list org-time-stamp-rounding-minutes
2675 org-time-stamp-rounding-minutes
)))
2677 (defcustom org-display-custom-times nil
2678 "Non-nil means overlay custom formats over all time stamps.
2679 The formats are defined through the variable `org-time-stamp-custom-formats'.
2680 To turn this on on a per-file basis, insert anywhere in the file:
2681 #+STARTUP: customtime"
2685 (make-variable-buffer-local 'org-display-custom-times
)
2687 (defcustom org-time-stamp-custom-formats
2688 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2689 "Custom formats for time stamps. See `format-time-string' for the syntax.
2690 These are overlaid over the default ISO format if the variable
2691 `org-display-custom-times' is set. Time like %H:%M should be at the
2692 end of the second format. The custom formats are also honored by export
2693 commands, if custom time display is turned on at the time of export."
2697 (defun org-time-stamp-format (&optional long inactive
)
2698 "Get the right format for a time string."
2699 (let ((f (if long
(cdr org-time-stamp-formats
)
2700 (car org-time-stamp-formats
))))
2702 (concat "[" (substring f
1 -
1) "]")
2705 (defcustom org-time-clocksum-format
"%d:%02d"
2706 "The format string used when creating CLOCKSUM lines.
2707 This is also used when org-mode generates a time duration."
2711 (defcustom org-time-clocksum-use-fractional nil
2712 "If non-nil, \\[org-clock-display] uses fractional times.
2713 org-mode generates a time duration."
2717 (defcustom org-time-clocksum-fractional-format
"%.2f"
2718 "The format string used when creating CLOCKSUM lines, or when
2719 org-mode generates a time duration."
2723 (defcustom org-deadline-warning-days
14
2724 "No. of days before expiration during which a deadline becomes active.
2725 This variable governs the display in sparse trees and in the agenda.
2726 When 0 or negative, it means use this number (the absolute value of it)
2727 even if a deadline has a different individual lead time specified.
2729 Custom commands can set this variable in the options section."
2731 :group
'org-agenda-daily
/weekly
2734 (defcustom org-read-date-prefer-future t
2735 "Non-nil means assume future for incomplete date input from user.
2736 This affects the following situations:
2737 1. The user gives a month but not a year.
2738 For example, if it is April and you enter \"feb 2\", this will be read
2739 as Feb 2, *next* year. \"May 5\", however, will be this year.
2740 2. The user gives a day, but no month.
2741 For example, if today is the 15th, and you enter \"3\", Org-mode will
2742 read this as the third of *next* month. However, if you enter \"17\",
2743 it will be considered as *this* month.
2745 If you set this variable to the symbol `time', then also the following
2748 3. If the user gives a time.
2749 If the time is before now, it will be interpreted as tomorrow.
2751 Currently none of this works for ISO week specifications.
2753 When this option is nil, the current day, month and year will always be
2756 See also `org-agenda-jump-prefer-future'."
2759 (const :tag
"Never" nil
)
2760 (const :tag
"Check month and day" t
)
2761 (const :tag
"Check month, day, and time" time
)))
2763 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
2764 "Should the agenda jump command prefer the future for incomplete dates?
2765 The default is to do the same as configured in `org-read-date-prefer-future'.
2766 But you can also set a deviating value here.
2767 This may t or nil, or the symbol `org-read-date-prefer-future'."
2772 (const :tag
"Use org-read-date-prefer-future"
2773 org-read-date-prefer-future
)
2774 (const :tag
"Never" nil
)
2775 (const :tag
"Always" t
)))
2777 (defcustom org-read-date-force-compatible-dates t
2778 "Should date/time prompt force dates that are guaranteed to work in Emacs?
2780 Depending on the system Emacs is running on, certain dates cannot
2781 be represented with the type used internally to represent time.
2782 Dates between 1970-1-1 and 2038-1-1 can always be represented
2783 correctly. Some systems allow for earlier dates, some for later,
2784 some for both. One way to find out it to insert any date into an
2785 Org buffer, putting the cursor on the year and hitting S-up and
2786 S-down to test the range.
2788 When this variable is set to t, the date/time prompt will not let
2789 you specify dates outside the 1970-2037 range, so it is certain that
2790 these dates will work in whatever version of Emacs you are
2791 running, and also that you can move a file from one Emacs implementation
2792 to another. WHenever Org is forcing the year for you, it will display
2795 When this variable is nil, Org will check if the date is
2796 representable in the specific Emacs implementation you are using.
2797 If not, it will force a year, usually the current year, and beep
2798 to remind you. Currently this setting is not recommended because
2799 the likelihood that you will open your Org files in an Emacs that
2800 has limited date range is not negligible.
2802 A workaround for this problem is to use diary sexp dates for time
2803 stamps outside of this range."
2808 (defcustom org-read-date-display-live t
2809 "Non-nil means display current interpretation of date prompt live.
2810 This display will be in an overlay, in the minibuffer."
2814 (defcustom org-read-date-popup-calendar t
2815 "Non-nil means pop up a calendar when prompting for a date.
2816 In the calendar, the date can be selected with mouse-1. However, the
2817 minibuffer will also be active, and you can simply enter the date as well.
2818 When nil, only the minibuffer will be available."
2821 (if (fboundp 'defvaralias
)
2822 (defvaralias 'org-popup-calendar-for-date-prompt
2823 'org-read-date-popup-calendar
))
2825 (defcustom org-read-date-minibuffer-setup-hook nil
2826 "Hook to be used to set up keys for the date/time interface.
2827 Add key definitions to `minibuffer-local-map', which will be a temporary
2832 (defcustom org-extend-today-until
0
2833 "The hour when your day really ends. Must be an integer.
2834 This has influence for the following applications:
2835 - When switching the agenda to \"today\". It it is still earlier than
2836 the time given here, the day recognized as TODAY is actually yesterday.
2837 - When a date is read from the user and it is still before the time given
2838 here, the current date and time will be assumed to be yesterday, 23:59.
2839 Also, timestamps inserted in capture templates follow this rule.
2841 IMPORTANT: This is a feature whose implementation is and likely will
2842 remain incomplete. Really, it is only here because past midnight seems to
2843 be the favorite working time of John Wiegley :-)"
2847 (defcustom org-use-effective-time nil
2848 "If non-nil, consider `org-extend-today-until' when creating timestamps.
2849 For example, if `org-extend-today-until' is 8, and it's 4am, then the
2850 \"effective time\" of any timestamps between midnight and 8am will be
2851 23:59 of the previous day."
2856 (defcustom org-edit-timestamp-down-means-later nil
2857 "Non-nil means S-down will increase the time in a time stamp.
2858 When nil, S-up will increase."
2862 (defcustom org-calendar-follow-timestamp-change t
2863 "Non-nil means make the calendar window follow timestamp changes.
2864 When a timestamp is modified and the calendar window is visible, it will be
2865 moved to the new date."
2869 (defgroup org-tags nil
2870 "Options concerning tags in Org-mode."
2874 (defcustom org-tag-alist nil
2875 "List of tags allowed in Org-mode files.
2876 When this list is nil, Org-mode will base TAG input on what is already in the
2878 The value of this variable is an alist, the car of each entry must be a
2879 keyword as a string, the cdr may be a character that is used to select
2880 that tag through the fast-tag-selection interface.
2881 See the manual for details."
2885 (cons (string :tag
"Tag name")
2886 (character :tag
"Access char"))
2887 (list :tag
"Start radio group"
2889 (option (string :tag
"Group description")))
2890 (list :tag
"End radio group"
2892 (option (string :tag
"Group description")))
2893 (const :tag
"New line" (:newline
)))))
2895 (defcustom org-tag-persistent-alist nil
2896 "List of tags that will always appear in all Org-mode files.
2897 This is in addition to any in buffer settings or customizations
2899 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2900 The value of this variable is an alist, the car of each entry must be a
2901 keyword as a string, the cdr may be a character that is used to select
2902 that tag through the fast-tag-selection interface.
2903 See the manual for details.
2904 To disable these tags on a per-file basis, insert anywhere in the file:
2909 (cons (string :tag
"Tag name")
2910 (character :tag
"Access char"))
2911 (const :tag
"Start radio group" (:startgroup
))
2912 (const :tag
"End radio group" (:endgroup
))
2913 (const :tag
"New line" (:newline
)))))
2915 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2916 "If non-nil, always offer completion for all tags of all agenda files.
2917 Instead of customizing this variable directly, you might want to
2918 set it locally for capture buffers, because there no list of
2919 tags in that file can be created dynamically (there are none).
2921 (add-hook 'org-capture-mode-hook
2923 (set (make-local-variable
2924 'org-complete-tags-always-offer-all-agenda-tags)
2930 (defvar org-file-tags nil
2931 "List of tags that can be inherited by all entries in the file.
2932 The tags will be inherited if the variable `org-use-tag-inheritance'
2933 says they should be.
2934 This variable is populated from #+FILETAGS lines.")
2936 (defcustom org-use-fast-tag-selection
'auto
2937 "Non-nil means use fast tag selection scheme.
2938 This is a special interface to select and deselect tags with single keys.
2939 When nil, fast selection is never used.
2940 When the symbol `auto', fast selection is used if and only if selection
2941 characters for tags have been configured, either through the variable
2942 `org-tag-alist' or through a #+TAGS line in the buffer.
2943 When t, fast selection is always used and selection keys are assigned
2944 automatically if necessary."
2947 (const :tag
"Always" t
)
2948 (const :tag
"Never" nil
)
2949 (const :tag
"When selection characters are configured" 'auto
)))
2951 (defcustom org-fast-tag-selection-single-key nil
2952 "Non-nil means fast tag selection exits after first change.
2953 When nil, you have to press RET to exit it.
2954 During fast tag selection, you can toggle this flag with `C-c'.
2955 This variable can also have the value `expert'. In this case, the window
2956 displaying the tags menu is not even shown, until you press C-c again."
2959 (const :tag
"No" nil
)
2960 (const :tag
"Yes" t
)
2961 (const :tag
"Expert" expert
)))
2963 (defvar org-fast-tag-selection-include-todo nil
2964 "Non-nil means fast tags selection interface will also offer TODO states.
2965 This is an undocumented feature, you should not rely on it.")
2967 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2968 "The column to which tags should be indented in a headline.
2969 If this number is positive, it specifies the column. If it is negative,
2970 it means that the tags should be flushright to that column. For example,
2971 -80 works well for a normal 80 character screen.
2972 When 0, place tags directly after headline text, with only one space in
2977 (defcustom org-auto-align-tags t
2978 "Non-nil keeps tags aligned when modifying headlines.
2979 Some operations (i.e. demoting) change the length of a headline and
2980 therefore shift the tags around. With this option turned on, after
2981 each such operation the tags are again aligned to `org-tags-column'."
2985 (defcustom org-use-tag-inheritance t
2986 "Non-nil means tags in levels apply also for sublevels.
2987 When nil, only the tags directly given in a specific line apply there.
2988 This may also be a list of tags that should be inherited, or a regexp that
2989 matches tags that should be inherited. Additional control is possible
2990 with the variable `org-tags-exclude-from-inheritance' which gives an
2991 explicit list of tags to be excluded from inheritance, even if the value of
2992 `org-use-tag-inheritance' would select it for inheritance.
2994 If this option is t, a match early-on in a tree can lead to a large
2995 number of matches in the subtree when constructing the agenda or creating
2996 a sparse tree. If you only want to see the first match in a tree during
2997 a search, check out the variable `org-tags-match-list-sublevels'."
3000 (const :tag
"Not" nil
)
3001 (const :tag
"Always" t
)
3002 (repeat :tag
"Specific tags" (string :tag
"Tag"))
3003 (regexp :tag
"Tags matched by regexp")))
3005 (defcustom org-tags-exclude-from-inheritance nil
3006 "List of tags that should never be inherited.
3007 This is a way to exclude a few tags from inheritance. For way to do
3008 the opposite, to actively allow inheritance for selected tags,
3009 see the variable `org-use-tag-inheritance'."
3011 :type
'(repeat (string :tag
"Tag")))
3013 (defun org-tag-inherit-p (tag)
3014 "Check if TAG is one that should be inherited."
3016 ((member tag org-tags-exclude-from-inheritance
) nil
)
3017 ((eq org-use-tag-inheritance t
) t
)
3018 ((not org-use-tag-inheritance
) nil
)
3019 ((stringp org-use-tag-inheritance
)
3020 (string-match org-use-tag-inheritance tag
))
3021 ((listp org-use-tag-inheritance
)
3022 (member tag org-use-tag-inheritance
))
3023 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3025 (defcustom org-tags-match-list-sublevels t
3026 "Non-nil means list also sublevels of headlines matching a search.
3027 This variable applies to tags/property searches, and also to stuck
3028 projects because this search is based on a tags match as well.
3030 When set to the symbol `indented', sublevels are indented with
3033 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3034 the sublevels of a headline matching a tag search often also match
3035 the same search. Listing all of them can create very long lists.
3036 Setting this variable to nil causes subtrees of a match to be skipped.
3038 This variable is semi-obsolete and probably should always be true. It
3039 is better to limit inheritance to certain tags using the variables
3040 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3043 (const :tag
"No, don't list them" nil
)
3044 (const :tag
"Yes, do list them" t
)
3045 (const :tag
"List them, indented with leading dots" indented
)))
3047 (defcustom org-tags-sort-function nil
3048 "When set, tags are sorted using this function as a comparator."
3051 (const :tag
"No sorting" nil
)
3052 (const :tag
"Alphabetical" string
<)
3053 (const :tag
"Reverse alphabetical" string
>)
3054 (function :tag
"Custom function" nil
)))
3056 (defvar org-tags-history nil
3057 "History of minibuffer reads for tags.")
3058 (defvar org-last-tags-completion-table nil
3059 "The last used completion table for tags.")
3060 (defvar org-after-tags-change-hook nil
3061 "Hook that is run after the tags in a line have changed.")
3063 (defgroup org-properties nil
3064 "Options concerning properties in Org-mode."
3065 :tag
"Org Properties"
3068 (defcustom org-property-format
"%-10s %s"
3069 "How property key/value pairs should be formatted by `indent-line'.
3070 When `indent-line' hits a property definition, it will format the line
3071 according to this format, mainly to make sure that the values are
3072 lined-up with respect to each other."
3073 :group
'org-properties
3076 (defcustom org-properties-postprocess-alist nil
3077 "Alist of properties and functions to adjust inserted values.
3078 Elements of this alist must be of the form
3080 ([string] [function])
3082 where [string] must be a property name and [function] must be a
3083 lambda expression: this lambda expression must take one argument,
3084 the value to adjust, and return the new value as a string.
3086 For example, this element will allow the property \"Remaining\"
3087 to be updated wrt the relation between the \"Effort\" property
3088 and the clock summary:
3090 ((\"Remaining\" (lambda(value)
3091 (let ((clocksum (org-clock-sum-current-item))
3092 (effort (org-duration-string-to-minutes
3093 (org-entry-get (point) \"Effort\"))))
3094 (org-minutes-to-hh:mm-string (- effort clocksum))))))"
3095 :group
'org-properties
3097 :type
'(alist :key-type
(string :tag
"Property")
3098 :value-type
(function :tag
"Function")))
3100 (defcustom org-use-property-inheritance nil
3101 "Non-nil means properties apply also for sublevels.
3103 This setting is chiefly used during property searches. Turning it on can
3104 cause significant overhead when doing a search, which is why it is not
3107 When nil, only the properties directly given in the current entry count.
3108 When t, every property is inherited. The value may also be a list of
3109 properties that should have inheritance, or a regular expression matching
3110 properties that should be inherited.
3112 However, note that some special properties use inheritance under special
3113 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3114 and the properties ending in \"_ALL\" when they are used as descriptor
3115 for valid values of a property.
3117 Note for programmers:
3118 When querying an entry with `org-entry-get', you can control if inheritance
3119 should be used. By default, `org-entry-get' looks only at the local
3120 properties. You can request inheritance by setting the inherit argument
3121 to t (to force inheritance) or to `selective' (to respect the setting
3123 :group
'org-properties
3125 (const :tag
"Not" nil
)
3126 (const :tag
"Always" t
)
3127 (repeat :tag
"Specific properties" (string :tag
"Property"))
3128 (regexp :tag
"Properties matched by regexp")))
3130 (defun org-property-inherit-p (property)
3131 "Check if PROPERTY is one that should be inherited."
3133 ((eq org-use-property-inheritance t
) t
)
3134 ((not org-use-property-inheritance
) nil
)
3135 ((stringp org-use-property-inheritance
)
3136 (string-match org-use-property-inheritance property
))
3137 ((listp org-use-property-inheritance
)
3138 (member property org-use-property-inheritance
))
3139 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3141 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3142 "The default column format, if no other format has been defined.
3143 This variable can be set on the per-file basis by inserting a line
3145 #+COLUMNS: %25ITEM ....."
3146 :group
'org-properties
3149 (defcustom org-columns-ellipses
".."
3150 "The ellipses to be used when a field in column view is truncated.
3151 When this is the empty string, as many characters as possible are shown,
3152 but then there will be no visual indication that the field has been truncated.
3153 When this is a string of length N, the last N characters of a truncated
3154 field are replaced by this string. If the column is narrower than the
3155 ellipses string, only part of the ellipses string will be shown."
3156 :group
'org-properties
3159 (defcustom org-columns-modify-value-for-display-function nil
3160 "Function that modifies values for display in column view.
3161 For example, it can be used to cut out a certain part from a time stamp.
3162 The function must take 2 arguments:
3164 column-title The title of the column (*not* the property name)
3165 value The value that should be modified.
3167 The function should return the value that should be displayed,
3168 or nil if the normal value should be used."
3169 :group
'org-properties
3172 (defcustom org-effort-property
"Effort"
3173 "The property that is being used to keep track of effort estimates.
3174 Effort estimates given in this property need to have the format H:MM."
3175 :group
'org-properties
3176 :group
'org-progress
3177 :type
'(string :tag
"Property"))
3179 (defconst org-global-properties-fixed
3180 '(("VISIBILITY_ALL" .
"folded children content all")
3181 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3182 "List of property/value pairs that can be inherited by any entry.
3184 These are fixed values, for the preset properties. The user variable
3185 that can be used to add to this list is `org-global-properties'.
3187 The entries in this list are cons cells where the car is a property
3188 name and cdr is a string with the value. If the value represents
3189 multiple items like an \"_ALL\" property, separate the items by
3192 (defcustom org-global-properties nil
3193 "List of property/value pairs that can be inherited by any entry.
3195 This list will be combined with the constant `org-global-properties-fixed'.
3197 The entries in this list are cons cells where the car is a property
3198 name and cdr is a string with the value.
3200 You can set buffer-local values for the same purpose in the variable
3201 `org-file-properties' this by adding lines like
3203 #+PROPERTY: NAME VALUE"
3204 :group
'org-properties
3206 (cons (string :tag
"Property")
3207 (string :tag
"Value"))))
3209 (defvar org-file-properties nil
3210 "List of property/value pairs that can be inherited by any entry.
3211 Valid for the current buffer.
3212 This variable is populated from #+PROPERTY lines.")
3213 (make-variable-buffer-local 'org-file-properties
)
3215 (defgroup org-agenda nil
3216 "Options concerning agenda views in Org-mode."
3220 (defvar org-category nil
3221 "Variable used by org files to set a category for agenda display.
3222 Such files should use a file variable to set it, for example
3224 # -*- mode: org; org-category: \"ELisp\"
3226 or contain a special line
3230 If the file does not specify a category, then file's base name
3232 (make-variable-buffer-local 'org-category
)
3233 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3235 (defcustom org-agenda-files nil
3236 "The files to be used for agenda display.
3237 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3238 \\[org-remove-file]. You can also use customize to edit the list.
3240 If an entry is a directory, all files in that directory that are matched by
3241 `org-agenda-file-regexp' will be part of the file list.
3243 If the value of the variable is not a list but a single file name, then
3244 the list of agenda files is actually stored and maintained in that file, one
3245 agenda file per line. In this file paths can be given relative to
3246 `org-directory'. Tilde expansion and environment variable substitution
3250 (repeat :tag
"List of files and directories" file
)
3251 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3253 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3254 "Regular expression to match files for `org-agenda-files'.
3255 If any element in the list in that variable contains a directory instead
3256 of a normal file, all files in that directory that are matched by this
3257 regular expression will be included."
3261 (defcustom org-agenda-text-search-extra-files nil
3262 "List of extra files to be searched by text search commands.
3263 These files will be search in addition to the agenda files by the
3264 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3265 Note that these files will only be searched for text search commands,
3266 not for the other agenda views like todo lists, tag searches or the weekly
3267 agenda. This variable is intended to list notes and possibly archive files
3268 that should also be searched by these two commands.
3269 In fact, if the first element in the list is the symbol `agenda-archives',
3270 than all archive files of all agenda files will be added to the search
3273 :type
'(set :greedy t
3274 (const :tag
"Agenda Archives" agenda-archives
)
3275 (repeat :inline t
(file))))
3277 (if (fboundp 'defvaralias
)
3278 (defvaralias 'org-agenda-multi-occur-extra-files
3279 'org-agenda-text-search-extra-files
))
3281 (defcustom org-agenda-skip-unavailable-files nil
3282 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3283 A nil value means to remove them, after a query, from the list."
3287 (defcustom org-calendar-to-agenda-key
[?c
]
3288 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3289 The command `org-calendar-goto-agenda' will be bound to this key. The
3290 default is the character `c' because then `c' can be used to switch back and
3291 forth between agenda and calendar."
3295 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3296 "The key to be installed in `calendar-mode-map' for adding diary entries.
3297 This option is irrelevant until `org-agenda-diary-file' has been configured
3298 to point to an Org-mode file. When that is the case, the command
3299 `org-agenda-diary-entry' will be bound to the key given here, by default
3300 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3301 if you want to continue doing this, you need to change this to a different
3306 (defcustom org-agenda-diary-file
'diary-file
3307 "File to which to add new entries with the `i' key in agenda and calendar.
3308 When this is the symbol `diary-file', the functionality in the Emacs
3309 calendar will be used to add entries to the `diary-file'. But when this
3310 points to a file, `org-agenda-diary-entry' will be used instead."
3313 (const :tag
"The standard Emacs diary file" diary-file
)
3314 (file :tag
"Special Org file diary entries")))
3316 (eval-after-load "calendar"
3318 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3319 'org-calendar-goto-agenda
)
3320 (add-hook 'calendar-mode-hook
3322 (unless (eq org-agenda-diary-file
'diary-file
)
3323 (define-key calendar-mode-map
3324 org-calendar-insert-diary-entry-key
3325 'org-agenda-diary-entry
))))))
3327 (defgroup org-latex nil
3328 "Options for embedding LaTeX code into Org-mode."
3332 (defcustom org-format-latex-options
3333 '(:foreground default
:background default
:scale
1.0
3334 :html-foreground
"Black" :html-background
"Transparent"
3335 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3336 "Options for creating images from LaTeX fragments.
3337 This is a property list with the following properties:
3338 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3339 `default' means use the foreground of the default face.
3340 :background the background color, or \"Transparent\".
3341 `default' means use the background of the default face.
3342 :scale a scaling factor for the size of the images, to get more pixels
3343 :html-foreground, :html-background, :html-scale
3344 the same numbers for HTML export.
3345 :matchers a list indicating which matchers should be used to
3346 find LaTeX fragments. Valid members of this list are:
3347 \"begin\" find environments
3348 \"$1\" find single characters surrounded by $.$
3349 \"$\" find math expressions surrounded by $...$
3350 \"$$\" find math expressions surrounded by $$....$$
3351 \"\\(\" find math expressions surrounded by \\(...\\)
3352 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3356 (defcustom org-format-latex-signal-error t
3357 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3358 When nil, just push out a message."
3363 (defcustom org-latex-to-mathml-jar-file nil
3364 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3365 Use this to specify additional executable file say a jar file.
3367 When using MathToWeb as the converter, specify the full-path to
3368 your mathtoweb.jar file."
3372 (const :tag
"None" nil
)
3373 (file :tag
"JAR file" :must-match t
)))
3375 (defcustom org-latex-to-mathml-convert-command nil
3376 "Command to convert LaTeX fragments to MathML.
3377 Replace format-specifiers in the command as noted below and use
3378 `shell-command' to convert LaTeX to MathML.
3379 %j: Executable file in fully expanded form as specified by
3380 `org-latex-to-mathml-jar-file'.
3381 %I: Input LaTeX file in fully expanded form
3382 %o: Output MathML file
3383 This command is used by `org-create-math-formula'.
3385 When using MathToWeb as the converter, set this to
3386 \"java -jar %j -unicode -force -df %o %I\"."
3390 (const :tag
"None" nil
)
3391 (string :tag
"\nShell command")))
3393 (defcustom org-latex-create-formula-image-program
'dvipng
3394 "Program to convert LaTeX fragments with.
3396 dvipng Process the LaTeX fragments to dvi file, then convert
3397 dvi files to png files using dvipng.
3398 This will also include processing of non-math environments.
3399 imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
3400 to convert pdf files to png files"
3404 (const :tag
"dvipng" dvipng
)
3405 (const :tag
"imagemagick" imagemagick
)))
3407 (defcustom org-latex-preview-ltxpng-directory
"ltxpng/"
3408 "Path to store latex preview images. A relative path here creates many
3409 directories relative to the processed org files paths. An absolute path
3410 puts all preview images at the same place."
3415 (defun org-format-latex-mathml-available-p ()
3416 "Return t if `org-latex-to-mathml-convert-command' is usable."
3418 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3419 org-latex-to-mathml-convert-command
)
3420 (let ((executable (car (split-string
3421 org-latex-to-mathml-convert-command
))))
3422 (when (executable-find executable
)
3424 "%j" org-latex-to-mathml-convert-command
)
3425 (file-readable-p org-latex-to-mathml-jar-file
)
3428 (defcustom org-format-latex-header
"\\documentclass{article}
3429 \\usepackage[usenames]{color}
3430 \\usepackage{amsmath}
3431 \\usepackage[mathscr]{eucal}
3432 \\pagestyle{empty} % do not remove
3435 % The settings below are copied from fullpage.sty
3436 \\setlength{\\textwidth}{\\paperwidth}
3437 \\addtolength{\\textwidth}{-3cm}
3438 \\setlength{\\oddsidemargin}{1.5cm}
3439 \\addtolength{\\oddsidemargin}{-2.54cm}
3440 \\setlength{\\evensidemargin}{\\oddsidemargin}
3441 \\setlength{\\textheight}{\\paperheight}
3442 \\addtolength{\\textheight}{-\\headheight}
3443 \\addtolength{\\textheight}{-\\headsep}
3444 \\addtolength{\\textheight}{-\\footskip}
3445 \\addtolength{\\textheight}{-3cm}
3446 \\setlength{\\topmargin}{1.5cm}
3447 \\addtolength{\\topmargin}{-2.54cm}"
3448 "The document header used for processing LaTeX fragments.
3449 It is imperative that this header make sure that no page number
3450 appears on the page. The package defined in the variables
3451 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
3452 will either replace the placeholder \"[PACKAGES]\" in this header, or they
3457 (defvar org-format-latex-header-extra nil
)
3459 (defun org-set-packages-alist (var val
)
3460 "Set the packages alist and make sure it has 3 elements per entry."
3461 (set var
(mapcar (lambda (x)
3462 (if (and (consp x
) (= (length x
) 2))
3463 (list (car x
) (nth 1 x
) t
)
3467 (defun org-get-packages-alist (var)
3469 "Get the packages alist and make sure it has 3 elements per entry."
3471 (if (and (consp x
) (= (length x
) 2))
3472 (list (car x
) (nth 1 x
) t
)
3474 (default-value var
)))
3476 ;; The following variables are defined here because is it also used
3477 ;; when formatting latex fragments. Originally it was part of the
3478 ;; LaTeX exporter, which is why the name includes "export".
3479 (defcustom org-export-latex-default-packages-alist
3480 '(("AUTO" "inputenc" t
)
3484 ("" "longtable" nil
)
3496 "Alist of default packages to be inserted in the header.
3497 Change this only if one of the packages here causes an incompatibility
3498 with another package you are using.
3499 The packages in this list are needed by one part or another of Org-mode
3500 to function properly.
3502 - inputenc, fontenc: for basic font and character selection
3503 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
3504 for interpreting the entities in `org-entities'. You can skip some of these
3505 packages if you don't use any of the symbols in it.
3506 - graphicx: for including images
3507 - float, wrapfig: for figure placement
3508 - longtable: for long tables
3509 - hyperref: for cross references
3511 Therefore you should not modify this variable unless you know what you
3512 are doing. The one reason to change it anyway is that you might be loading
3513 some other package that conflicts with one of the default packages.
3514 Each cell is of the format \( \"options\" \"package\" snippet-flag\).
3515 If SNIPPET-FLAG is t, the package also needs to be included when
3516 compiling LaTeX snippets into images for inclusion into HTML."
3517 :group
'org-export-latex
3518 :set
'org-set-packages-alist
3519 :get
'org-get-packages-alist
3523 (list :tag
"options/package pair"
3524 (string :tag
"options")
3525 (string :tag
"package")
3526 (boolean :tag
"Snippet"))
3527 (string :tag
"A line of LaTeX"))))
3529 (defcustom org-export-latex-packages-alist nil
3530 "Alist of packages to be inserted in every LaTeX header.
3531 These will be inserted after `org-export-latex-default-packages-alist'.
3532 Each cell is of the format \( \"options\" \"package\" snippet-flag \).
3533 SNIPPET-FLAG, when t, indicates that this package is also needed when
3534 turning LaTeX snippets into images for inclusion into HTML.
3535 Make sure that you only list packages here which:
3536 - you want in every file
3537 - do not conflict with the default packages in
3538 `org-export-latex-default-packages-alist'
3539 - do not conflict with the setup in `org-format-latex-header'."
3540 :group
'org-export-latex
3541 :set
'org-set-packages-alist
3542 :get
'org-get-packages-alist
3545 (list :tag
"options/package pair"
3546 (string :tag
"options")
3547 (string :tag
"package")
3548 (boolean :tag
"Snippet"))
3549 (string :tag
"A line of LaTeX"))))
3552 (defgroup org-appearance nil
3553 "Settings for Org-mode appearance."
3554 :tag
"Org Appearance"
3557 (defcustom org-level-color-stars-only nil
3558 "Non-nil means fontify only the stars in each headline.
3559 When nil, the entire headline is fontified.
3560 Changing it requires restart of `font-lock-mode' to become effective
3561 also in regions already fontified."
3562 :group
'org-appearance
3565 (defcustom org-hide-leading-stars nil
3566 "Non-nil means hide the first N-1 stars in a headline.
3567 This works by using the face `org-hide' for these stars. This
3568 face is white for a light background, and black for a dark
3569 background. You may have to customize the face `org-hide' to
3571 Changing it requires restart of `font-lock-mode' to become effective
3572 also in regions already fontified.
3573 You may also set this on a per-file basis by adding one of the following
3574 lines to the buffer:
3576 #+STARTUP: hidestars
3577 #+STARTUP: showstars"
3578 :group
'org-appearance
3581 (defcustom org-hidden-keywords nil
3582 "List of symbols corresponding to keywords to be hidden the org buffer.
3583 For example, a value '(title) for this list will make the document's title
3584 appear in the buffer without the initial #+TITLE: keyword."
3585 :group
'org-appearance
3587 :type
'(set (const :tag
"#+AUTHOR" author
)
3588 (const :tag
"#+DATE" date
)
3589 (const :tag
"#+EMAIL" email
)
3590 (const :tag
"#+TITLE" title
)))
3592 (defcustom org-custom-properties nil
3593 "List of properties (as strings) with a special meaning.
3594 The default use of these custom properties is to let the user
3595 hide them with `org-toggle-custom-properties-visibility'."
3596 :group
'org-properties
3597 :group
'org-appearance
3599 :type
'(repeat (string :tag
"Property Name")))
3601 (defcustom org-fontify-done-headline nil
3602 "Non-nil means change the face of a headline if it is marked DONE.
3603 Normally, only the TODO/DONE keyword indicates the state of a headline.
3604 When this is non-nil, the headline after the keyword is set to the
3605 `org-headline-done' as an additional indication."
3606 :group
'org-appearance
3609 (defcustom org-fontify-emphasized-text t
3610 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3611 Changing this variable requires a restart of Emacs to take effect."
3612 :group
'org-appearance
3615 (defcustom org-fontify-whole-heading-line nil
3616 "Non-nil means fontify the whole line for headings.
3617 This is useful when setting a background color for the
3619 :group
'org-appearance
3622 (defcustom org-highlight-latex-fragments-and-specials nil
3623 "Non-nil means fontify what is treated specially by the exporters."
3624 :group
'org-appearance
3627 (defcustom org-hide-emphasis-markers nil
3628 "Non-nil mean font-lock should hide the emphasis marker characters."
3629 :group
'org-appearance
3632 (defcustom org-pretty-entities nil
3633 "Non-nil means show entities as UTF8 characters.
3634 When nil, the \\name form remains in the buffer."
3635 :group
'org-appearance
3639 (defcustom org-pretty-entities-include-sub-superscripts t
3640 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3641 :group
'org-appearance
3645 (defvar org-emph-re nil
3646 "Regular expression for matching emphasis.
3647 After a match, the match groups contain these elements:
3648 0 The match of the full regular expression, including the characters
3649 before and after the proper match
3650 1 The character before the proper match, or empty at beginning of line
3651 2 The proper match, including the leading and trailing markers
3652 3 The leading marker like * or /, indicating the type of highlighting
3653 4 The text between the emphasis markers, not including the markers
3654 5 The character after the match, empty at the end of a line")
3655 (defvar org-verbatim-re nil
3656 "Regular expression for matching verbatim text.")
3657 (defvar org-emphasis-regexp-components
) ; defined just below
3658 (defvar org-emphasis-alist
) ; defined just below
3659 (defun org-set-emph-re (var val
)
3660 "Set variable and compute the emphasis regular expression."
3662 (when (and (boundp 'org-emphasis-alist
)
3663 (boundp 'org-emphasis-regexp-components
)
3664 org-emphasis-alist org-emphasis-regexp-components
)
3665 (let* ((e org-emphasis-regexp-components
)
3671 (body1 (concat body
"*?"))
3672 (markers (mapconcat 'car org-emphasis-alist
""))
3673 (vmarkers (mapconcat
3674 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3675 org-emphasis-alist
"")))
3676 ;; make sure special characters appear at the right position in the class
3677 (if (string-match "\\^" markers
)
3678 (setq markers
(concat (replace-match "" t t markers
) "^")))
3679 (if (string-match "-" markers
)
3680 (setq markers
(concat (replace-match "" t t markers
) "-")))
3681 (if (string-match "\\^" vmarkers
)
3682 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3683 (if (string-match "-" vmarkers
)
3684 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3686 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3687 (int-to-string nl
) "\\}")))
3690 (concat "\\([" pre
"]\\|^\\)"
3692 "\\([" markers
"]\\)"
3700 "\\([" post
"]\\|$\\)"))
3701 (setq org-verbatim-re
3702 (concat "\\([" pre
"]\\|^\\)"
3704 "\\([" vmarkers
"]\\)"
3712 "\\([" post
"]\\|$\\)")))))
3714 (defcustom org-emphasis-regexp-components
3715 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3716 "Components used to build the regular expression for emphasis.
3717 This is a list with five entries. Terminology: In an emphasis string
3718 like \" *strong word* \", we call the initial space PREMATCH, the final
3719 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3720 and \"trong wor\" is the body. The different components in this variable
3721 specify what is allowed/forbidden in each part:
3723 pre Chars allowed as prematch. Beginning of line will be allowed too.
3724 post Chars allowed as postmatch. End of line will be allowed too.
3725 border The chars *forbidden* as border characters.
3726 body-regexp A regexp like \".\" to match a body character. Don't use
3727 non-shy groups here, and don't allow newline here.
3728 newline The maximum number of newlines allowed in an emphasis exp.
3730 Use customize to modify this, or restart Emacs after changing it."
3731 :group
'org-appearance
3732 :set
'org-set-emph-re
3734 (sexp :tag
"Allowed chars in pre ")
3735 (sexp :tag
"Allowed chars in post ")
3736 (sexp :tag
"Forbidden chars in border ")
3737 (sexp :tag
"Regexp for body ")
3738 (integer :tag
"number of newlines allowed")
3739 (option (boolean :tag
"Please ignore this button"))))
3741 (defcustom org-emphasis-alist
3742 `(("*" bold
"<b>" "</b>")
3743 ("/" italic
"<i>" "</i>")
3744 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
3745 ("=" org-code
"<code>" "</code>" verbatim
)
3746 ("~" org-verbatim
"<code>" "</code>" verbatim
)
3747 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
3750 "Special syntax for emphasized text.
3751 Text starting and ending with a special character will be emphasized, for
3752 example *bold*, _underlined_ and /italic/. This variable sets the marker
3753 characters, the face to be used by font-lock for highlighting in Org-mode
3754 Emacs buffers, and the HTML tags to be used for this.
3755 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3756 For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
3757 Use customize to modify this, or restart Emacs after changing it."
3758 :group
'org-appearance
3759 :set
'org-set-emph-re
3762 (string :tag
"Marker character")
3764 (face :tag
"Font-lock-face")
3765 (plist :tag
"Face property list"))
3766 (string :tag
"HTML start tag")
3767 (string :tag
"HTML end tag")
3768 (option (const verbatim
)))))
3770 (defvar org-protecting-blocks
3771 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3772 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3773 This is needed for font-lock setup.")
3775 ;;; Miscellaneous options
3777 (defgroup org-completion nil
3778 "Completion in Org-mode."
3779 :tag
"Org Completion"
3782 (defcustom org-completion-use-ido nil
3783 "Non-nil means use ido completion wherever possible.
3784 Note that `ido-mode' must be active for this variable to be relevant.
3785 If you decide to turn this variable on, you might well want to turn off
3786 `org-outline-path-complete-in-steps'.
3787 See also `org-completion-use-iswitchb'."
3788 :group
'org-completion
3791 (defcustom org-completion-use-iswitchb nil
3792 "Non-nil means use iswitchb completion wherever possible.
3793 Note that `iswitchb-mode' must be active for this variable to be relevant.
3794 If you decide to turn this variable on, you might well want to turn off
3795 `org-outline-path-complete-in-steps'.
3796 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3797 :group
'org-completion
3800 (defcustom org-completion-fallback-command
'hippie-expand
3801 "The expansion command called by \\[pcomplete] in normal context.
3802 Normal means, no org-mode-specific context."
3803 :group
'org-completion
3806 ;;; Functions and variables from their packages
3807 ;; Declared here to avoid compiler warnings
3810 (defvar outline-mode-menu-heading
)
3811 (defvar outline-mode-menu-show
)
3812 (defvar outline-mode-menu-hide
)
3813 (defvar zmacs-regions
) ; XEmacs regions
3816 (defvar mark-active
)
3819 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
3820 (declare-function calendar-forward-day
"cal-move" (arg))
3821 (declare-function calendar-goto-date
"cal-move" (date))
3822 (declare-function calendar-goto-today
"cal-move" ())
3823 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
3824 (defvar calc-embedded-close-formula
)
3825 (defvar calc-embedded-open-formula
)
3826 (declare-function cdlatex-tab
"ext:cdlatex" ())
3827 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
3828 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
3829 (defvar font-lock-unfontify-region-function
)
3830 (declare-function iswitchb-read-buffer
"iswitchb"
3831 (prompt &optional default require-match start matches-set
))
3832 (defvar iswitchb-temp-buflist
)
3833 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
3834 (defvar org-agenda-tags-todo-honor-ignore-options
)
3835 (declare-function org-agenda-skip
"org-agenda" ())
3837 org-agenda-format-item
"org-agenda"
3838 (extra txt
&optional category tags dotime noprefix remove-re habitp
))
3839 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
3840 (declare-function org-agenda-change-all-lines
"org-agenda"
3841 (newhead hdmarker
&optional fixface just-this
))
3842 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
3843 (declare-function org-agenda-maybe-redo
"org-agenda" ())
3844 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
3846 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
3847 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3848 "org-agenda" (&optional end
))
3849 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
3850 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
3851 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
3852 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
3853 (declare-function org-indent-mode
"org-indent" (&optional arg
))
3854 (declare-function parse-time-string
"parse-time" (string))
3855 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
3856 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
3857 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
3858 (defvar remember-data-file
)
3859 (defvar texmathp-why
)
3860 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
3861 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
3863 (defvar w3m-current-url
)
3864 (defvar w3m-current-title
)
3866 (defvar org-latex-regexps
)
3868 ;;; Autoload and prepare some org modules
3870 ;; Some table stuff that needs to be defined here, because it is used
3871 ;; by the functions setting up org-mode or checking for table context.
3873 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
3874 "Detect an org-type or table-type table.")
3875 (defconst org-table-line-regexp
"^[ \t]*|"
3876 "Detect an org-type table line.")
3877 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
3878 "Detect an org-type table line.")
3879 (defconst org-table-hline-regexp
"^[ \t]*|-"
3880 "Detect an org-type table hline.")
3881 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
3882 "Detect a table-type table hline.")
3883 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
3884 "Detect the first line outside a table when searching from within it.
3885 This works for both table types.")
3887 ;; Autoload the functions in org-table.el that are needed by functions here.
3890 (org-autoload "org-table"
3891 '(org-table-begin org-table-blank-field org-table-end
)))
3894 (defun turn-on-orgtbl ()
3895 "Unconditionally turn on `orgtbl-mode'."
3896 (require 'org-table
)
3899 (defun org-at-table-p (&optional table-type
)
3900 "Return t if the cursor is inside an org-type table.
3901 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3902 (if org-enable-table-editor
3904 (beginning-of-line 1)
3905 (looking-at (if table-type org-table-any-line-regexp
3906 org-table-line-regexp
)))
3908 (defsubst org-table-p
() (org-at-table-p))
3910 (defun org-at-table.el-p
()
3911 "Return t if and only if we are at a table.el table."
3912 (and (org-at-table-p 'any
)
3914 (goto-char (org-table-begin 'any
))
3915 (looking-at org-table1-hline-regexp
))))
3916 (defun org-table-recognize-table.el
()
3917 "If there is a table.el table nearby, recognize it and move into it."
3918 (if org-table-tab-recognizes-table.el
3919 (if (org-at-table.el-p
)
3921 (beginning-of-line 1)
3922 (if (looking-at org-table-dataline-regexp
)
3924 (if (looking-at org-table1-hline-regexp
)
3926 (beginning-of-line 2)
3927 (if (looking-at org-table-any-border-regexp
)
3928 (beginning-of-line -
1)))))
3929 (if (re-search-forward "|" (org-table-end t
) t
)
3932 (if (table--at-cell-p (point))
3934 (message "recognizing table.el table...")
3935 (table-recognize-table)
3936 (message "recognizing table.el table...done")))
3937 (error "This should not happen"))
3942 (defun org-at-table-hline-p ()
3943 "Return t if the cursor is inside a hline in a table."
3944 (if org-enable-table-editor
3946 (beginning-of-line 1)
3947 (looking-at org-table-hline-regexp
))
3950 (defvar org-table-clean-did-remove-column nil
)
3952 (defun org-table-map-tables (function &optional quietly
)
3953 "Apply FUNCTION to the start of all tables in the buffer."
3957 (goto-char (point-min))
3958 (while (re-search-forward org-table-any-line-regexp nil t
)
3960 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
3961 (beginning-of-line 1)
3962 (when (and (looking-at org-table-line-regexp
)
3963 ;; Exclude tables in src/example/verbatim/clocktable blocks
3964 (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
3965 (save-excursion (funcall function
))
3966 (or (looking-at org-table-line-regexp
)
3968 (re-search-forward org-table-any-border-regexp nil
1))))
3969 (unless quietly
(message "Mapping tables: done")))
3971 ;; Declare and autoload functions from org-exp.el & Co
3973 (declare-function org-default-export-plist
"org-exp")
3974 (declare-function org-infile-export-plist
"org-exp")
3975 (declare-function org-get-current-options
"org-exp")
3977 ;; Declare and autoload functions from org-agenda.el
3980 (org-autoload "org-agenda"
3981 '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
3983 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock" (beg end
))
3984 (declare-function org-clock-update-mode-line
"org-clock" ())
3985 (declare-function org-resolve-clocks
"org-clock"
3986 (&optional also-non-dangling-p prompt last-valid
))
3987 (defvar org-clock-start-time
)
3988 (defvar org-clock-marker
(make-marker)
3989 "Marker recording the last clock-in.")
3990 (defvar org-clock-hd-marker
(make-marker)
3991 "Marker recording the last clock-in, but the headline position.")
3992 (defvar org-clock-heading
""
3993 "The heading of the current clock entry.")
3994 (defun org-clock-is-active ()
3995 "Return non-nil if clock is currently running.
3996 The return value is actually the clock marker."
3997 (marker-buffer org-clock-marker
))
4000 (org-autoload "org-clock" '(org-clock-remove-overlays
4001 org-clock-update-time-maybe
4002 org-clocktable-shift
)))
4004 (defun org-check-running-clock ()
4005 "Check if the current buffer contains the running clock.
4006 If yes, offer to stop it and to save the buffer with the changes."
4007 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4008 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4011 (when (y-or-n-p "Save changed buffer?")
4014 (defun org-clocktable-try-shift (dir n
)
4015 "Check if this line starts a clock table, if yes, shift the time block."
4016 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4017 (org-clocktable-shift dir n
)))
4020 (defun org-clock-persistence-insinuate ()
4021 "Set up hooks for clock persistence."
4022 (require 'org-clock
)
4023 (add-hook 'org-mode-hook
'org-clock-load
)
4024 (add-hook 'kill-emacs-hook
'org-clock-save
))
4026 ;; Define the variable already here, to make sure we have it.
4027 (defvar org-indent-mode nil
4028 "Non-nil if Org-Indent mode is enabled.
4029 Use the command `org-indent-mode' to change this variable.")
4031 ;; Autoload archiving code
4032 ;; The stuff that is needed for cycling and tags has to be defined here.
4034 (defgroup org-archive nil
4035 "Options concerning archiving in Org-mode."
4037 :group
'org-structure
)
4039 (defcustom org-archive-location
"%s_archive::"
4040 "The location where subtrees should be archived.
4042 The value of this variable is a string, consisting of two parts,
4043 separated by a double-colon. The first part is a filename and
4044 the second part is a headline.
4046 When the filename is omitted, archiving happens in the same file.
4047 %s in the filename will be replaced by the current file
4048 name (without the directory part). Archiving to a different file
4049 is useful to keep archived entries from contributing to the
4052 The archived entries will be filed as subtrees of the specified
4053 headline. When the headline is omitted, the subtrees are simply
4054 filed away at the end of the file, as top-level entries. Also in
4055 the heading you can use %s to represent the file name, this can be
4056 useful when using the same archive for a number of different files.
4058 Here are a few examples:
4060 If the current file is Projects.org, archive in file
4061 Projects.org_archive, as top-level trees. This is the default.
4063 \"::* Archived Tasks\"
4064 Archive in the current file, under the top-level headline
4065 \"* Archived Tasks\".
4067 \"~/org/archive.org::\"
4068 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4070 \"~/org/archive.org::* From %s\"
4071 Archive in file ~/org/archive.org (absolute path), under headlines
4072 \"From FILENAME\" where file name is the current file name.
4074 \"~/org/datetree.org::datetree/* Finished Tasks\"
4075 The \"datetree/\" string is special, signifying to archive
4076 items to the datetree. Items are placed in either the CLOSED
4077 date of the item, or the current date if there is no CLOSED date.
4078 The heading will be a subentry to the current date. There doesn't
4079 need to be a heading, but there always needs to be a slash after
4080 datetree. For example, to store archived items directly in the
4081 datetree, use \"~/org/datetree.org::datetree/\".
4083 \"basement::** Finished Tasks\"
4084 Archive in file ./basement (relative path), as level 3 trees
4085 below the level 2 heading \"** Finished Tasks\".
4087 You may set this option on a per-file basis by adding to the buffer a
4090 #+ARCHIVE: basement::** Finished Tasks
4092 You may also define it locally for a subtree by setting an ARCHIVE property
4093 in the entry. If such a property is found in an entry, or anywhere up
4094 the hierarchy, it will be used."
4098 (defcustom org-archive-tag
"ARCHIVE"
4099 "The tag that marks a subtree as archived.
4100 An archived subtree does not open during visibility cycling, and does
4101 not contribute to the agenda listings.
4102 After changing this, font-lock must be restarted in the relevant buffers to
4103 get the proper fontification."
4105 :group
'org-keywords
4108 (defcustom org-agenda-skip-archived-trees t
4109 "Non-nil means the agenda will skip any items located in archived trees.
4110 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4111 variable is no longer recommended, you should leave it at the value t.
4112 Instead, use the key `v' to cycle the archives-mode in the agenda."
4114 :group
'org-agenda-skip
4117 (defcustom org-columns-skip-archived-trees t
4118 "Non-nil means ignore archived trees when creating column view."
4120 :group
'org-properties
4123 (defcustom org-cycle-open-archived-trees nil
4124 "Non-nil means `org-cycle' will open archived trees.
4125 An archived tree is a tree marked with the tag ARCHIVE.
4126 When nil, archived trees will stay folded. You can still open them with
4127 normal outline commands like `show-all', but not with the cycling commands."
4132 (defcustom org-sparse-tree-open-archived-trees nil
4133 "Non-nil means sparse tree construction shows matches in archived trees.
4134 When nil, matches in these trees are highlighted, but the trees are kept in
4137 :group
'org-sparse-trees
4140 (defcustom org-sparse-tree-default-date-type
'scheduled-or-deadline
4141 "The default date type when building a sparse tree.
4142 When this is nil, a date is a scheduled or a deadline timestamp.
4143 Otherwise, these types are allowed:
4146 active: only active timestamps (<...>)
4147 inactive: only inactive timestamps (<...)
4148 scheduled: only scheduled timestamps
4149 deadline: only deadline timestamps"
4150 :type
'(choice (const :tag
"Scheduled or deadline" 'scheduled-or-deadline
)
4151 (const :tag
"All timestamps" all
)
4152 (const :tag
"Only active timestamps" active
)
4153 (const :tag
"Only inactive timestamps" inactive
)
4154 (const :tag
"Only scheduled timestamps" scheduled
)
4155 (const :tag
"Only deadline timestamps" deadline
))
4157 :group
'org-sparse-trees
)
4159 (defun org-cycle-hide-archived-subtrees (state)
4160 "Re-hide all archived subtrees after a visibility state change."
4161 (when (and (not org-cycle-open-archived-trees
)
4162 (not (memq state
'(overview folded
))))
4164 (let* ((globalp (memq state
'(contents all
)))
4165 (beg (if globalp
(point-min) (point)))
4166 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4167 (org-hide-archived-subtrees beg end
)
4169 (if (looking-at (concat ".*:" org-archive-tag
":"))
4170 (message "%s" (substitute-command-keys
4171 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4173 (defun org-force-cycle-archived ()
4174 "Cycle subtree even if it is archived."
4176 (setq this-command
'org-cycle
)
4177 (let ((org-cycle-open-archived-trees t
))
4178 (call-interactively 'org-cycle
)))
4180 (defun org-hide-archived-subtrees (beg end
)
4181 "Re-hide all archived subtrees after a visibility state change."
4183 (let* ((re (concat ":" org-archive-tag
":")))
4185 (while (re-search-forward re end t
)
4186 (when (org-at-heading-p)
4187 (org-flag-subtree t
)
4188 (org-end-of-subtree t
))))))
4190 (declare-function outline-end-of-heading
"outline" ())
4191 (declare-function outline-flag-region
"outline" (from to flag
))
4192 (defun org-flag-subtree (flag)
4194 (org-back-to-heading t
)
4195 (outline-end-of-heading)
4196 (outline-flag-region (point)
4197 (progn (org-end-of-subtree t
) (point))
4200 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4203 (org-autoload "org-archive"
4204 '(org-add-archive-files)))
4206 ;; Autoload Column View Code
4208 (declare-function org-columns-number-to-string
"org-colview" (n fmt
&optional printf
))
4209 (declare-function org-columns-get-format-and-top-level
"org-colview" ())
4210 (declare-function org-columns-compute
"org-colview" (property))
4212 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4213 '(org-columns-number-to-string
4214 org-columns-get-format-and-top-level
4216 org-columns-remove-overlays
))
4220 (declare-function org-id-store-link
"org-id")
4221 (declare-function org-id-locations-load
"org-id")
4222 (declare-function org-id-locations-save
"org-id")
4223 (defvar org-id-track-globally
)
4224 (org-autoload "org-id"
4227 org-id-get-with-outline-path-completion
4228 org-id-get-with-outline-drilling
))
4230 ;;; Variables for pre-computed regular expressions, all buffer local
4232 (defvar org-drawer-regexp
"^[ \t]*:PROPERTIES:[ \t]*$"
4233 "Matches first line of a hidden block.")
4234 (make-variable-buffer-local 'org-drawer-regexp
)
4235 (defvar org-todo-regexp nil
4236 "Matches any of the TODO state keywords.")
4237 (make-variable-buffer-local 'org-todo-regexp
)
4238 (defvar org-not-done-regexp nil
4239 "Matches any of the TODO state keywords except the last one.")
4240 (make-variable-buffer-local 'org-not-done-regexp
)
4241 (defvar org-not-done-heading-regexp nil
4242 "Matches a TODO headline that is not done.")
4243 (make-variable-buffer-local 'org-not-done-regexp
)
4244 (defvar org-todo-line-regexp nil
4245 "Matches a headline and puts TODO state into group 2 if present.")
4246 (make-variable-buffer-local 'org-todo-line-regexp
)
4247 (defvar org-complex-heading-regexp nil
4248 "Matches a headline and puts everything into groups:
4250 group 2: The todo keyword, maybe
4251 group 3: Priority cookie
4252 group 4: True headline
4254 (make-variable-buffer-local 'org-complex-heading-regexp
)
4255 (defvar org-complex-heading-regexp-format nil
4256 "Printf format to make regexp to match an exact headline.
4257 This regexp will match the headline of any node which has the
4258 exact headline text that is put into the format, but may have any
4259 TODO state, priority and tags.")
4260 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4261 (defvar org-todo-line-tags-regexp nil
4262 "Matches a headline and puts TODO state into group 2 if present.
4263 Also put tags into group 4 if tags are present.")
4264 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4265 (defvar org-ds-keyword-length
12
4266 "Maximum length of the DEADLINE and SCHEDULED keywords.")
4267 (make-variable-buffer-local 'org-ds-keyword-length
)
4268 (defvar org-deadline-regexp nil
4269 "Matches the DEADLINE keyword.")
4270 (make-variable-buffer-local 'org-deadline-regexp
)
4271 (defvar org-deadline-time-regexp nil
4272 "Matches the DEADLINE keyword together with a time stamp.")
4273 (make-variable-buffer-local 'org-deadline-time-regexp
)
4274 (defvar org-deadline-line-regexp nil
4275 "Matches the DEADLINE keyword and the rest of the line.")
4276 (make-variable-buffer-local 'org-deadline-line-regexp
)
4277 (defvar org-scheduled-regexp nil
4278 "Matches the SCHEDULED keyword.")
4279 (make-variable-buffer-local 'org-scheduled-regexp
)
4280 (defvar org-scheduled-time-regexp nil
4281 "Matches the SCHEDULED keyword together with a time stamp.")
4282 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4283 (defvar org-closed-time-regexp nil
4284 "Matches the CLOSED keyword together with a time stamp.")
4285 (make-variable-buffer-local 'org-closed-time-regexp
)
4287 (defvar org-keyword-time-regexp nil
4288 "Matches any of the 4 keywords, together with the time stamp.")
4289 (make-variable-buffer-local 'org-keyword-time-regexp
)
4290 (defvar org-keyword-time-not-clock-regexp nil
4291 "Matches any of the 3 keywords, together with the time stamp.")
4292 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4293 (defvar org-maybe-keyword-time-regexp nil
4294 "Matches a timestamp, possibly preceded by a keyword.")
4295 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4296 (defvar org-all-time-keywords nil
4297 "List of time keywords.")
4298 (make-variable-buffer-local 'org-all-time-keywords
)
4300 (defconst org-plain-time-of-day-regexp
4303 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4306 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4308 "Regular expression to match a plain time or time range.
4309 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4310 groups carry important information:
4312 1 the first time, range or not
4313 8 the second time, if it is a range.")
4315 (defconst org-plain-time-extension-regexp
4318 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4319 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4320 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4321 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4322 groups carry important information:
4325 9 minutes of duration")
4327 (defconst org-stamp-time-of-day-regexp
4329 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4330 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4332 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4333 "Regular expression to match a timestamp time or time range.
4334 After a match, the following groups carry important information:
4336 1 date plus weekday, for back referencing to make sure both times are on the same day
4337 2 the first time, range or not
4338 4 the second time, if it is a range.")
4340 (defconst org-startup-options
4341 '(("fold" org-startup-folded t
)
4342 ("overview" org-startup-folded t
)
4343 ("nofold" org-startup-folded nil
)
4344 ("showall" org-startup-folded nil
)
4345 ("showeverything" org-startup-folded showeverything
)
4346 ("content" org-startup-folded content
)
4347 ("indent" org-startup-indented t
)
4348 ("noindent" org-startup-indented nil
)
4349 ("hidestars" org-hide-leading-stars t
)
4350 ("showstars" org-hide-leading-stars nil
)
4351 ("odd" org-odd-levels-only t
)
4352 ("oddeven" org-odd-levels-only nil
)
4353 ("align" org-startup-align-all-tables t
)
4354 ("noalign" org-startup-align-all-tables nil
)
4355 ("inlineimages" org-startup-with-inline-images t
)
4356 ("noinlineimages" org-startup-with-inline-images nil
)
4357 ("customtime" org-display-custom-times t
)
4358 ("logdone" org-log-done time
)
4359 ("lognotedone" org-log-done note
)
4360 ("nologdone" org-log-done nil
)
4361 ("lognoteclock-out" org-log-note-clock-out t
)
4362 ("nolognoteclock-out" org-log-note-clock-out nil
)
4363 ("logrepeat" org-log-repeat state
)
4364 ("lognoterepeat" org-log-repeat note
)
4365 ("nologrepeat" org-log-repeat nil
)
4366 ("logreschedule" org-log-reschedule time
)
4367 ("lognotereschedule" org-log-reschedule note
)
4368 ("nologreschedule" org-log-reschedule nil
)
4369 ("logredeadline" org-log-redeadline time
)
4370 ("lognoteredeadline" org-log-redeadline note
)
4371 ("nologredeadline" org-log-redeadline nil
)
4372 ("logrefile" org-log-refile time
)
4373 ("lognoterefile" org-log-refile note
)
4374 ("nologrefile" org-log-refile nil
)
4375 ("fninline" org-footnote-define-inline t
)
4376 ("nofninline" org-footnote-define-inline nil
)
4377 ("fnlocal" org-footnote-section nil
)
4378 ("fnauto" org-footnote-auto-label t
)
4379 ("fnprompt" org-footnote-auto-label nil
)
4380 ("fnconfirm" org-footnote-auto-label confirm
)
4381 ("fnplain" org-footnote-auto-label plain
)
4382 ("fnadjust" org-footnote-auto-adjust t
)
4383 ("nofnadjust" org-footnote-auto-adjust nil
)
4384 ("constcgs" constants-unit-system cgs
)
4385 ("constSI" constants-unit-system SI
)
4386 ("noptag" org-tag-persistent-alist nil
)
4387 ("hideblocks" org-hide-block-startup t
)
4388 ("nohideblocks" org-hide-block-startup nil
)
4389 ("beamer" org-startup-with-beamer-mode t
)
4390 ("entitiespretty" org-pretty-entities t
)
4391 ("entitiesplain" org-pretty-entities nil
))
4392 "Variable associated with STARTUP options for org-mode.
4393 Each element is a list of three items: the startup options (as written
4394 in the #+STARTUP line), the corresponding variable, and the value to set
4395 this variable to if the option is found. An optional forth element PUSH
4396 means to push this value onto the list in the variable.")
4398 (defun org-update-property-plist (key val props
)
4399 "Update PROPS with KEY and VAL."
4400 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4401 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4402 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4403 (previous (cdr (assoc key props
))))
4405 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4406 (cons (cons key val
) remainder
))))
4408 (defconst org-block-regexp
4409 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4410 "Regular expression for hiding blocks.")
4411 (defconst org-heading-keyword-regexp-format
4412 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4413 "Printf format for a regexp matching an headline with some keyword.
4414 This regexp will match the headline of any node which has the
4415 exact keyword that is put into the format. The keyword isn't in
4416 any group by default, but the stars and the body are.")
4417 (defconst org-heading-keyword-maybe-regexp-format
4418 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4419 "Printf format for a regexp matching an headline, possibly with some keyword.
4420 This regexp can match any headline with the specified keyword, or
4421 without a keyword. The keyword isn't in any group by default,
4422 but the stars and the body are.")
4424 (defun org-set-regexps-and-options ()
4425 "Precompute regular expressions for current buffer."
4426 (when (derived-mode-p 'org-mode
)
4427 (org-set-local 'org-todo-kwd-alist nil
)
4428 (org-set-local 'org-todo-key-alist nil
)
4429 (org-set-local 'org-todo-key-trigger nil
)
4430 (org-set-local 'org-todo-keywords-1 nil
)
4431 (org-set-local 'org-done-keywords nil
)
4432 (org-set-local 'org-todo-heads nil
)
4433 (org-set-local 'org-todo-sets nil
)
4434 (org-set-local 'org-todo-log-states nil
)
4435 (org-set-local 'org-file-properties nil
)
4436 (org-set-local 'org-file-tags nil
)
4437 (let ((re (org-make-options-regexp
4438 '("CATEGORY" "TODO" "COLUMNS"
4439 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
4440 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
4442 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4444 (scripts org-use-sub-superscripts
)
4445 kwds kws0 kwsa key log value cat arch tags const links hw dws
4446 tail sep kws1 prio props ftags drawers beamer-p
4447 ext-setup-or-nil setup-contents
(start 0))
4451 (goto-char (point-min))
4452 (while (or (and ext-setup-or-nil
4453 (string-match re ext-setup-or-nil start
)
4454 (setq start
(match-end 0)))
4455 (and (setq ext-setup-or-nil nil start
0)
4456 (re-search-forward re nil t
)))
4457 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4458 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4459 (if (stringp value
) (setq value
(org-trim value
)))
4461 ((equal key
"CATEGORY")
4463 ((member key
'("SEQ_TODO" "TODO"))
4464 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4465 ((equal key
"TYP_TODO")
4466 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4467 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4468 ;; general TODO-like setup
4469 (push (cons (intern (downcase (match-string 1 key
)))
4470 (org-split-string value splitre
)) kwds
))
4472 (setq tags
(append tags
(if tags
'("\\n") nil
)
4473 (org-split-string value splitre
))))
4474 ((equal key
"COLUMNS")
4475 (org-set-local 'org-columns-default-format value
))
4477 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4478 (push (cons (match-string 1 value
)
4479 (org-trim (match-string 2 value
)))
4481 ((equal key
"PRIORITIES")
4482 (setq prio
(org-split-string value
" +")))
4483 ((equal key
"PROPERTY")
4484 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4485 (setq props
(org-update-property-plist (match-string 1 value
)
4486 (match-string 2 value
)
4488 ((equal key
"FILETAGS")
4489 (when (string-match "\\S-" value
)
4494 (mapcar (lambda (x) (org-split-string x
":"))
4495 (org-split-string value
)))))))
4496 ((equal key
"DRAWERS")
4497 (setq drawers
(delete-dups (append org-drawers
(org-split-string value splitre
)))))
4498 ((equal key
"CONSTANTS")
4499 (setq const
(append const
(org-split-string value splitre
))))
4500 ((equal key
"STARTUP")
4501 (let ((opts (org-split-string value splitre
))
4503 (while (setq l
(pop opts
))
4504 (when (setq l
(assoc l org-startup-options
))
4505 (setq var
(nth 1 l
) val
(nth 2 l
))
4507 (set (make-local-variable var
) val
)
4508 (if (not (listp (symbol-value var
)))
4509 (set (make-local-variable var
) nil
))
4510 (set (make-local-variable var
) (symbol-value var
))
4511 (add-to-list var val
))))))
4512 ((equal key
"ARCHIVE")
4514 (remove-text-properties 0 (length arch
)
4515 '(face t fontified t
) arch
))
4516 ((equal key
"LATEX_CLASS")
4517 (setq beamer-p
(equal value
"beamer")))
4518 ((equal key
"OPTIONS")
4519 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4520 (setq scripts
(read (match-string 2 value
)))))
4521 ((equal key
"SETUPFILE")
4522 (setq setup-contents
(org-file-contents
4524 (org-remove-double-quotes value
))
4526 (if (not ext-setup-or-nil
)
4527 (setq ext-setup-or-nil setup-contents start
0)
4528 (setq ext-setup-or-nil
4529 (concat (substring ext-setup-or-nil
0 start
)
4530 "\n" setup-contents
"\n"
4531 (substring ext-setup-or-nil start
)))))))
4532 ;; search for property blocks
4533 (goto-char (point-min))
4534 (while (re-search-forward org-block-regexp nil t
)
4535 (when (equal "PROPERTY" (upcase (match-string 1)))
4536 (setq value
(replace-regexp-in-string
4537 "[\n\r]" " " (match-string 4)))
4538 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4539 (setq props
(org-update-property-plist (match-string 1 value
)
4540 (match-string 2 value
)
4542 (org-set-local 'org-use-sub-superscripts scripts
)
4544 (org-set-local 'org-category
(intern cat
))
4545 (push (cons "CATEGORY" cat
) props
))
4547 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4548 (setq prio
(mapcar 'string-to-char prio
))
4549 (org-set-local 'org-highest-priority
(nth 0 prio
))
4550 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4551 (org-set-local 'org-default-priority
(nth 2 prio
)))
4552 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4553 (and ftags
(org-set-local 'org-file-tags
4554 (mapcar 'org-add-prop-inherited ftags
)))
4555 (and drawers
(org-set-local 'org-drawers drawers
))
4556 (and arch
(org-set-local 'org-archive-location arch
))
4557 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4558 ;; Process the TODO keywords
4560 ;; Use the global values as if they had been given locally.
4561 (setq kwds
(default-value 'org-todo-keywords
))
4562 (if (stringp (car kwds
))
4563 (setq kwds
(list (cons org-todo-interpretation
4564 (default-value 'org-todo-keywords
)))))
4565 (setq kwds
(reverse kwds
)))
4566 (setq kwds
(nreverse kwds
))
4568 (while (setq kws
(pop kwds
))
4570 (run-hook-with-args-until-success
4571 'org-todo-setup-filter-hook kws
)
4573 (setq inter
(pop kws
) sep
(member "|" kws
)
4574 kws0
(delete "|" (copy-sequence kws
))
4579 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4581 (setq kw
(match-string 1 x
)
4582 key
(and (match-end 2) (match-string 2 x
))
4583 log
(org-extract-log-state-settings x
))
4584 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4585 (and log
(push log org-todo-log-states
))
4587 (error "Invalid TODO keyword %s" x
)))
4589 kwsa
(if kwsa
(append '((:startgroup
))
4593 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4594 tail
(list inter hw
(car dws
) (org-last dws
))))
4595 (add-to-list 'org-todo-heads hw
'append
)
4596 (push kws1 org-todo-sets
)
4597 (setq org-done-keywords
(append org-done-keywords dws nil
))
4598 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4599 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4600 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4601 (setq org-todo-sets
(nreverse org-todo-sets
)
4602 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4603 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4604 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4605 ;; Process the constants
4608 (while (setq e
(pop const
))
4609 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4610 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4611 (setq org-table-formula-constants-local cst
)))
4613 ;; Process the tags.
4616 (while (setq e
(pop tags
))
4618 ((equal e
"{") (push '(:startgroup
) tgs
))
4619 ((equal e
"}") (push '(:endgroup
) tgs
))
4620 ((equal e
"\\n") (push '(:newline
) tgs
))
4621 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4622 (push (cons (match-string 1 e
)
4623 (string-to-char (match-string 2 e
)))
4625 (t (push (list e
) tgs
))))
4626 (org-set-local 'org-tag-alist nil
)
4627 (while (setq e
(pop tgs
))
4628 (or (and (stringp (car e
))
4629 (assoc (car e
) org-tag-alist
))
4630 (push e org-tag-alist
)))))
4632 ;; Compute the regular expressions and other local variables.
4633 ;; Using `org-outline-regexp-bol' would complicate them much,
4634 ;; because of the fixed white space at the end of that string.
4635 (if (not org-done-keywords
)
4636 (setq org-done-keywords
(and org-todo-keywords-1
4637 (list (org-last org-todo-keywords-1
)))))
4638 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4639 (length org-scheduled-string
)
4640 (length org-clock-string
)
4641 (length org-closed-string
)))
4643 (concat "^[ \t]*:\\("
4644 (mapconcat 'regexp-quote org-drawers
"\\|")
4646 org-not-done-keywords
4647 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4650 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4654 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4656 org-not-done-heading-regexp
4657 (format org-heading-keyword-regexp-format org-not-done-regexp
)
4658 org-todo-line-regexp
4659 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
4660 org-complex-heading-regexp
4661 (concat "^\\(\\*+\\)"
4662 "\\(?: +" org-todo-regexp
"\\)?"
4663 "\\(?: +\\(\\[#.\\]\\)\\)?"
4664 "\\(?: +\\(.*?\\)\\)??"
4665 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
4667 org-complex-heading-regexp-format
4668 (concat "^\\(\\*+\\)"
4669 "\\(?: +" org-todo-regexp
"\\)?"
4670 "\\(?: +\\(\\[#.\\]\\)\\)?"
4672 ;; Stats cookies can be stuck to body.
4673 "\\(?:\\[[0-9%%/]+\\] *\\)?"
4675 "\\(?: *\\[[0-9%%/]+\\]\\)?"
4677 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
4679 org-todo-line-tags-regexp
4680 (concat "^\\(\\*+\\)"
4681 "\\(?: +" org-todo-regexp
"\\)?"
4682 "\\(?: +\\(.*?\\)\\)??"
4683 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
4685 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4686 org-deadline-time-regexp
4687 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4688 org-deadline-line-regexp
4689 (concat "\\<\\(" org-deadline-string
"\\).*")
4690 org-scheduled-regexp
4691 (concat "\\<" org-scheduled-string
)
4692 org-scheduled-time-regexp
4693 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4694 org-closed-time-regexp
4695 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4696 org-keyword-time-regexp
4697 (concat "\\<\\(" org-scheduled-string
4698 "\\|" org-deadline-string
4699 "\\|" org-closed-string
4700 "\\|" org-clock-string
"\\)"
4701 " *[[<]\\([^]>]+\\)[]>]")
4702 org-keyword-time-not-clock-regexp
4703 (concat "\\<\\(" org-scheduled-string
4704 "\\|" org-deadline-string
4705 "\\|" org-closed-string
4707 " *[[<]\\([^]>]+\\)[]>]")
4708 org-maybe-keyword-time-regexp
4709 (concat "\\(\\<\\(" org-scheduled-string
4710 "\\|" org-deadline-string
4711 "\\|" org-closed-string
4712 "\\|" org-clock-string
"\\)\\)?"
4713 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4714 org-all-time-keywords
4715 (mapcar (lambda (w) (substring w
0 -
1))
4716 (list org-scheduled-string org-deadline-string
4717 org-clock-string org-closed-string
))
4719 (org-compute-latex-and-specials-regexp)
4720 (org-set-font-lock-defaults))))
4722 (defun org-file-contents (file &optional noerror
)
4723 "Return the contents of FILE, as a string."
4725 (not (file-readable-p file
)))
4728 (message "Cannot read file \"%s\"" file
)
4731 (error "Cannot read file \"%s\"" file
))
4733 (insert-file-contents file
)
4736 (defun org-extract-log-state-settings (x)
4737 "Extract the log state setting from a TODO keyword string.
4738 This will extract info from a string like \"WAIT(w@/!)\"."
4739 (let (kw key log1 log2
)
4740 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4741 (setq kw
(match-string 1 x
)
4742 key
(and (match-end 2) (match-string 2 x
))
4743 log1
(and (match-end 3) (match-string 3 x
))
4744 log2
(and (match-end 4) (match-string 4 x
)))
4747 (and log1
(if (equal log1
"!") 'time
'note
))
4748 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4750 (defun org-remove-keyword-keys (list)
4751 "Remove a pair of parenthesis at the end of each string in LIST."
4753 (if (string-match "(.*)$" x
)
4754 (substring x
0 (match-beginning 0))
4758 (defun org-assign-fast-keys (alist)
4759 "Assign fast keys to a keyword-key alist.
4760 Respect keys that are already there."
4761 (let (new e
(alt ?
0))
4762 (while (setq e
(pop alist
))
4763 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
4764 (cdr e
)) ;; Key already assigned.
4766 (let ((clist (string-to-list (downcase (car e
))))
4767 (used (append new alist
)))
4768 (when (= (car clist
) ?
@)
4770 (while (and clist
(rassoc (car clist
) used
))
4773 (while (rassoc alt used
)
4775 (push (cons (car e
) (or (car clist
) alt
)) new
))))
4778 ;;; Some variables used in various places
4780 (defvar org-window-configuration nil
4781 "Used in various places to store a window configuration.")
4782 (defvar org-selected-window nil
4783 "Used in various places to store a window configuration.")
4784 (defvar org-finish-function nil
4785 "Function to be called when `C-c C-c' is used.
4786 This is for getting out of special buffers like capture.")
4789 ;; FIXME: Occasionally check by commenting these, to make sure
4790 ;; no other functions uses these, forgetting to let-bind them.
4791 (org-no-warnings (defvar entry
)) ;; unprefixed, from calendar.el
4792 (defvar org-last-state
)
4793 (org-no-warnings (defvar date
)) ;; unprefixed, from calendar.el
4795 ;; Defined somewhere in this file, but used before definition.
4796 (defvar org-entities
) ;; defined in org-entities.el
4797 (defvar org-struct-menu
)
4798 (defvar org-org-menu
)
4799 (defvar org-tbl-menu
)
4801 ;;;; Define the Org-mode
4803 ;; We use a before-change function to check if a table might need
4805 (defvar org-table-may-need-update t
4806 "Indicates that a table might need an update.
4807 This variable is set by `org-before-change-function'.
4808 `org-table-align' sets it back to nil.")
4809 (defun org-before-change-function (beg end
)
4810 "Every change indicates that a table might need an update."
4811 (setq org-table-may-need-update t
))
4812 (defvar org-mode-map
)
4813 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4814 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
4815 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4816 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
4817 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
4818 (defvar org-table-buffer-is-an nil
)
4820 (defvar bidi-paragraph-direction
)
4821 (defvar buffer-face-mode-face
)
4824 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4825 (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22"))
4826 (require 'noutline
"noutline" 'noerror
) ;; stock XEmacs does not have it
4828 ;; Other stuff we need.
4829 (require 'time-date
)
4830 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
4835 (require 'org-entities
)
4836 ;; (require 'org-compat) moved higher up in the file before it is first used
4837 (require 'org-faces
)
4839 (require 'org-pcomplete
)
4841 (require 'org-footnote
)
4848 (require 'ob-tangle
)
4849 (require 'ob-comint
)
4853 (define-derived-mode org-mode outline-mode
"Org"
4854 "Outline-based notes management and organizer, alias
4855 \"Carsten's outline-mode for keeping track of everything.\"
4857 Org-mode develops organizational tasks around a NOTES file which
4858 contains information about projects as plain text. Org-mode is
4859 implemented on top of outline-mode, which is ideal to keep the content
4860 of large files well structured. It supports ToDo items, deadlines and
4861 time stamps, which magically appear in the diary listing of the Emacs
4862 calendar. Tables are easily created with a built-in table editor.
4863 Plain text URL-like links connect to websites, emails (VM), Usenet
4864 messages (Gnus), BBDB entries, and any files related to the project.
4865 For printing and sharing of notes, an Org-mode file (or a part of it)
4866 can be exported as a structured ASCII or HTML file.
4868 The following commands are available:
4872 ;; Get rid of Outline menus, they are not needed
4873 ;; Need to do this here because define-derived-mode sets up
4874 ;; the keymap so late. Still, it is a waste to call this each time
4875 ;; we switch another buffer into org-mode.
4876 (if (featurep 'xemacs
)
4877 (when (boundp 'outline-mode-menu-heading
)
4878 ;; Assume this is Greg's port, it uses easymenu
4879 (easy-menu-remove outline-mode-menu-heading
)
4880 (easy-menu-remove outline-mode-menu-show
)
4881 (easy-menu-remove outline-mode-menu-hide
))
4882 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
4883 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
4884 (define-key org-mode-map
[menu-bar show
] 'undefined
))
4886 (org-load-modules-maybe)
4887 (easy-menu-add org-org-menu
)
4888 (easy-menu-add org-tbl-menu
)
4889 (org-install-agenda-files-menu)
4890 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
4891 (add-to-invisibility-spec '(org-cwidth))
4892 (add-to-invisibility-spec '(org-hide-block . t
))
4893 (when (featurep 'xemacs
)
4894 (org-set-local 'line-move-ignore-invisible t
))
4895 (org-set-local 'outline-regexp org-outline-regexp
)
4896 (org-set-local 'outline-level
'org-outline-level
)
4897 (setq bidi-paragraph-direction
'left-to-right
)
4898 (when (and org-ellipsis
4899 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
4900 (fboundp 'make-glyph-code
))
4901 (unless org-display-table
4902 (setq org-display-table
(make-display-table)))
4903 (set-display-table-slot
4906 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
4908 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
4909 (setq buffer-display-table org-display-table
))
4910 (org-set-regexps-and-options)
4911 (when (and org-tag-faces
(not org-tags-special-faces-re
))
4912 ;; tag faces set outside customize.... force initialization.
4913 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
4915 (org-set-local 'calc-embedded-open-mode
"# ")
4916 (modify-syntax-entry ?
@ "w")
4917 (if org-startup-truncated
(setq truncate-lines t
))
4918 (org-set-local 'font-lock-unfontify-region-function
4919 'org-unfontify-region
)
4920 ;; Activate before-change-function
4921 (org-set-local 'org-table-may-need-update t
)
4922 (org-add-hook 'before-change-functions
'org-before-change-function nil
4924 ;; Check for running clock before killing a buffer
4925 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
4927 (org-set-local 'indent-line-function
'org-indent-line
)
4928 (org-set-local 'indent-region-function
'org-indent-region
)
4929 ;; Initialize radio targets.
4930 (org-update-radio-target-regexp)
4931 ;; Filling and auto-filling.
4934 (org-setup-comments-handling)
4935 ;; Beginning/end of defun
4936 (org-set-local 'beginning-of-defun-function
'org-back-to-heading
)
4937 (org-set-local 'end-of-defun-function
(lambda () (interactive) (org-end-of-subtree nil t
)))
4938 ;; Next error for sparse trees
4939 (org-set-local 'next-error-function
'org-occur-next-match
)
4940 ;; Make sure dependence stuff works reliably, even for users who set it
4942 (if org-enforce-todo-dependencies
4943 (add-hook 'org-blocker-hook
4944 'org-block-todo-from-children-or-siblings-or-parent
)
4945 (remove-hook 'org-blocker-hook
4946 'org-block-todo-from-children-or-siblings-or-parent
))
4947 (if org-enforce-todo-checkbox-dependencies
4948 (add-hook 'org-blocker-hook
4949 'org-block-todo-from-checkboxes
)
4950 (remove-hook 'org-blocker-hook
4951 'org-block-todo-from-checkboxes
))
4953 ;; Align options lines
4955 'align-mode-rules-list
4956 '((org-in-buffer-settings
4957 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4958 (modes .
'(org-mode)))))
4961 (org-set-local 'imenu-create-index-function
4962 'org-imenu-get-tree
)
4964 ;; Make isearch reveal context
4965 (if (or (featurep 'xemacs
)
4966 (not (boundp 'outline-isearch-open-invisible-function
)))
4967 ;; Emacs 21 and XEmacs make use of the hook
4968 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
4969 ;; Emacs 22 deals with this through a special variable
4970 (org-set-local 'outline-isearch-open-invisible-function
4971 (lambda (&rest ignore
) (org-show-context 'isearch
))))
4973 ;; Setup the pcomplete hooks
4974 (set (make-local-variable 'pcomplete-command-completion-function
)
4975 'org-pcomplete-initial
)
4976 (set (make-local-variable 'pcomplete-command-name-function
)
4977 'org-command-at-point
)
4978 (set (make-local-variable 'pcomplete-default-completion-function
)
4980 (set (make-local-variable 'pcomplete-parse-arguments-function
)
4981 'org-parse-arguments
)
4982 (set (make-local-variable 'pcomplete-termination-string
) "")
4983 (when (>= emacs-major-version
23)
4984 (set (make-local-variable 'buffer-face-mode-face
) 'org-default
))
4986 ;; If empty file that did not turn on org-mode automatically, make it to.
4987 (if (and org-insert-mode-line-in-empty-file
4988 (org-called-interactively-p 'any
)
4989 (= (point-min) (point-max)))
4990 (insert "# -*- mode: org -*-\n\n"))
4991 (unless org-inhibit-startup
4992 (and org-startup-with-beamer-mode
(org-beamer-mode))
4993 (when org-startup-align-all-tables
4994 (let ((bmp (buffer-modified-p)))
4995 (org-table-map-tables 'org-table-align
'quietly
)
4996 (set-buffer-modified-p bmp
)))
4997 (when org-startup-with-inline-images
4998 (org-display-inline-images))
4999 (when org-startup-indented
5000 (require 'org-indent
)
5001 (org-indent-mode 1))
5002 (unless org-inhibit-startup-visibility-stuff
5003 (org-set-startup-visibility)))
5004 ;; Try to set org-hide correctly
5005 (set-face-foreground 'org-hide
(org-find-invisible-foreground)))
5007 (when (fboundp 'abbrev-table-put
)
5008 (abbrev-table-put org-mode-abbrev-table
5009 :parents
(list text-mode-abbrev-table
)))
5011 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5014 (defun org-find-invisible-foreground ()
5015 (let ((candidates (remove
5018 (list (face-background 'default
)
5019 (face-background 'org-default
))
5022 (when (boundp alist
)
5023 (cdr (assoc 'background-color
(symbol-value alist
)))))
5024 '(default-frame-alist initial-frame-alist window-system-default-frame-alist
))
5025 (list (face-foreground 'org-hide
))))))
5026 (car (remove nil candidates
))))
5028 (defun org-current-time ()
5029 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
5030 (if (> (car org-time-stamp-rounding-minutes
) 1)
5031 (let ((r (car org-time-stamp-rounding-minutes
))
5032 (time (decode-time)))
5034 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5039 "Return today date, considering `org-extend-today-until'."
5041 (time-subtract (current-time)
5042 (list 0 (* 3600 org-extend-today-until
) 0))))
5044 ;;;; Font-Lock stuff, including the activators
5046 (defvar org-mouse-map
(make-sparse-keymap))
5047 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5048 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5049 (when org-mouse-1-follows-link
5050 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5051 (when org-tab-follows-link
5052 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5053 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5055 (require 'font-lock
)
5057 (defconst org-non-link-chars
"]\t\n\r<>")
5058 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5059 "shell" "elisp" "doi" "message"))
5060 (defvar org-link-types-re nil
5061 "Matches a link that has a url-like prefix like \"http:\"")
5062 (defvar org-link-re-with-space nil
5063 "Matches a link with spaces, optional angular brackets around it.")
5064 (defvar org-link-re-with-space2 nil
5065 "Matches a link with spaces, optional angular brackets around it.")
5066 (defvar org-link-re-with-space3 nil
5067 "Matches a link with spaces, only for internal part in bracket links.")
5068 (defvar org-angle-link-re nil
5069 "Matches link with angular brackets, spaces are allowed.")
5070 (defvar org-plain-link-re nil
5071 "Matches plain link, without spaces.")
5072 (defvar org-bracket-link-regexp nil
5073 "Matches a link in double brackets.")
5074 (defvar org-bracket-link-analytic-regexp nil
5075 "Regular expression used to analyze links.
5076 Here is what the match groups contain after a match:
5082 (defvar org-bracket-link-analytic-regexp
++ nil
5083 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5084 (defvar org-any-link-re nil
5085 "Regular expression matching any link.")
5087 (defcustom org-match-sexp-depth
3
5088 "Number of stacked braces for sub/superscript matching.
5089 This has to be set before loading org.el to be effective."
5090 :group
'org-export-translation
; ??????????????????????????/
5093 (defun org-create-multibrace-regexp (left right n
)
5094 "Create a regular expression which will match a balanced sexp.
5095 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5096 as single character strings.
5097 The regexp returned will match the entire expression including the
5098 delimiters. It will also define a single group which contains the
5099 match except for the outermost delimiters. The maximum depth of
5100 stacked delimiters is N. Escaping delimiters is not possible."
5101 (let* ((nothing (concat "[^" left right
"]*?"))
5104 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5107 re
(concat re or next
)
5108 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5109 (concat left
"\\(" re
"\\)" right
)))
5111 (defvar org-match-substring-regexp
5113 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5114 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5116 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5118 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5119 "The regular expression matching a sub- or superscript.")
5121 (defvar org-match-substring-with-braces-regexp
5123 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5124 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5126 "The regular expression matching a sub- or superscript, forcing braces.")
5128 (defun org-make-link-regexps ()
5129 "Update the link regular expressions.
5130 This should be called after the variable `org-link-types' has changed."
5131 (setq org-link-types-re
5133 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
5134 org-link-re-with-space
5136 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5137 "\\([^" org-non-link-chars
" ]"
5138 "[^" org-non-link-chars
"]*"
5139 "[^" org-non-link-chars
" ]\\)>?")
5140 org-link-re-with-space2
5142 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5143 "\\([^" org-non-link-chars
" ]"
5145 "[^" org-non-link-chars
" ]\\)>?")
5146 org-link-re-with-space3
5148 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5149 "\\([^" org-non-link-chars
" ]"
5153 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5154 "\\([^" org-non-link-chars
" ]"
5155 "[^" org-non-link-chars
"]*"
5159 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5160 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5161 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5162 org-bracket-link-regexp
5163 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5164 org-bracket-link-analytic-regexp
5167 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
5170 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5172 org-bracket-link-analytic-regexp
++
5175 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
5178 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5181 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5182 org-angle-link-re
"\\)\\|\\("
5183 org-plain-link-re
"\\)")))
5185 (org-make-link-regexps)
5187 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5188 "Regular expression for fast time stamp matching.")
5189 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5190 "Regular expression for fast time stamp matching.")
5191 (defconst org-ts-regexp0
5192 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5193 "Regular expression matching time strings for analysis.
5194 This one does not require the space after the date, so it can be used
5195 on a string that terminates immediately after the date.")
5196 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5197 "Regular expression matching time strings for analysis.")
5198 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5199 "Regular expression matching time stamps, with groups.")
5200 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5201 "Regular expression matching time stamps (also [..]), with groups.")
5202 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5203 "Regular expression matching a time stamp range.")
5204 (defconst org-tr-regexp-both
5205 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5206 "Regular expression matching a time stamp range.")
5207 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5208 org-ts-regexp
"\\)?")
5209 "Regular expression matching a time stamp or time stamp range.")
5210 (defconst org-tsr-regexp-both
(concat org-ts-regexp-both
"\\(--?-?"
5211 org-ts-regexp-both
"\\)?")
5212 "Regular expression matching a time stamp or time stamp range.
5213 The time stamps may be either active or inactive.")
5215 (defvar org-emph-face nil
)
5217 (defun org-do-emphasis-faces (limit)
5218 "Run through the buffer and add overlays to emphasized strings."
5220 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5221 (if (not (= (char-after (match-beginning 3))
5222 (char-after (match-beginning 4))))
5225 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5226 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5230 (org-remove-flyspell-overlays-in
5231 (match-beginning 0) (match-end 0)))
5232 (add-text-properties (match-beginning 2) (match-end 2)
5233 '(font-lock-multiline t org-emphasis t
))
5234 (when org-hide-emphasis-markers
5235 (add-text-properties (match-end 4) (match-beginning 5)
5236 '(invisible org-link
))
5237 (add-text-properties (match-beginning 3) (match-end 3)
5238 '(invisible org-link
)))))
5242 (defun org-emphasize (&optional char
)
5243 "Insert or change an emphasis, i.e. a font like bold or italic.
5244 If there is an active region, change that region to a new emphasis.
5245 If there is no region, just insert the marker characters and position
5246 the cursor between them.
5247 CHAR should be either the marker character, or the first character of the
5248 HTML tag associated with that emphasis. If CHAR is a space, the means
5249 to remove the emphasis of the selected region.
5250 If char is not given (for example in an interactive call) it
5251 will be prompted for."
5253 (let ((eal org-emphasis-alist
) e det
5254 (erc org-emphasis-regexp-components
)
5256 (string "") beg end move tag c s
)
5257 (if (org-region-active-p)
5258 (setq beg
(region-beginning) end
(region-end)
5259 string
(buffer-substring beg end
))
5262 (while (setq e
(pop eal
))
5263 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
5265 (push (cons c
(string-to-char (car e
))) det
)
5266 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
5267 (substring tag
1)))))
5268 (setq det
(nreverse det
))
5270 (message "%s" (concat "Emphasis marker or tag:" prompt
))
5271 (setq char
(read-char-exclusive)))
5272 (setq char
(or (cdr (assoc char det
)) char
))
5273 (if (equal char ?\
)
5274 (setq s
"" move nil
)
5275 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5276 (error "No such emphasis marker: \"%c\"" char
))
5277 (setq s
(char-to-string char
)))
5278 (while (and (> (length string
) 1)
5279 (equal (substring string
0 1) (substring string -
1))
5280 (assoc (substring string
0 1) org-emphasis-alist
))
5281 (setq string
(substring string
1 -
1)))
5282 (setq string
(concat s string s
))
5283 (if beg
(delete-region beg end
))
5285 (string-match (concat "[" (nth 0 erc
) "\n]")
5286 (char-to-string (char-before (point)))))
5289 (string-match (concat "[" (nth 1 erc
) "\n]")
5290 (char-to-string (char-after (point)))))
5291 (insert " ") (backward-char 1))
5293 (and move
(backward-char 1))))
5295 (defconst org-nonsticky-props
5296 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link
))
5298 (defsubst org-rear-nonsticky-at
(pos)
5299 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5301 (defun org-activate-plain-links (limit)
5302 "Run through the buffer and add overlays to links."
5305 (when (and (re-search-forward (concat org-plain-link-re
) limit t
)
5306 (not (org-in-src-block-p)))
5307 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5308 (setq f
(get-text-property (match-beginning 0) 'face
))
5309 (unless (or (org-in-src-block-p)
5311 (and (listp f
) (memq 'org-tag f
)))
5312 (add-text-properties (match-beginning 0) (match-end 0)
5313 (list 'mouse-face
'highlight
5315 'keymap org-mouse-map
))
5316 (org-rear-nonsticky-at (match-end 0)))
5319 (defun org-activate-code (limit)
5320 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5322 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5323 (remove-text-properties (match-beginning 0) (match-end 0)
5324 '(display t invisible t intangible t
))
5327 (defcustom org-src-fontify-natively nil
5328 "When non-nil, fontify code in code blocks."
5331 :group
'org-appearance
5334 (defcustom org-allow-promoting-top-level-subtree nil
5335 "When non-nil, allow promoting a top level subtree.
5336 The leading star of the top level headline will be replaced
5340 :group
'org-appearance
)
5342 (defun org-fontify-meta-lines-and-blocks (limit)
5344 (org-fontify-meta-lines-and-blocks-1 limit
)
5345 (error (message "org-mode fontification error"))))
5347 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5348 "Fontify #+ lines and blocks, in the correct ways."
5349 (let ((case-fold-search t
))
5350 (if (re-search-forward
5351 "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5353 (let ((beg (match-beginning 0))
5354 (block-start (match-end 0))
5356 (lang (match-string 7))
5357 (beg1 (line-beginning-position 2))
5358 (dc1 (downcase (match-string 2)))
5359 (dc3 (downcase (match-string 3)))
5360 end end1 quoting block-type ovl
)
5362 ((member dc1
'("+html:" "+ascii:" "+latex:" "+docbook:"))
5363 ;; a single line of backend-specific content
5364 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5365 (remove-text-properties (match-beginning 0) (match-end 0)
5366 '(display t invisible t intangible t
))
5367 (add-text-properties (match-beginning 1) (match-end 3)
5368 '(font-lock-fontified t face org-meta-line
))
5369 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5370 '(font-lock-fontified t face org-block
))
5371 ; for backend-specific code
5373 ((and (match-end 4) (equal dc3
"+begin"))
5375 (setq block-type
(downcase (match-string 5))
5376 quoting
(member block-type org-protecting-blocks
))
5377 (when (re-search-forward
5378 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5379 nil t
) ;; on purpose, we look further than LIMIT
5380 (setq end
(min (point-max) (match-end 0))
5381 end1
(min (point-max) (1- (match-beginning 0))))
5382 (setq block-end
(match-beginning 0))
5384 (remove-text-properties beg end
5385 '(display t invisible t intangible t
)))
5386 (add-text-properties
5388 '(font-lock-fontified t font-lock-multiline t
))
5389 (add-text-properties beg beg1
'(face org-meta-line
))
5390 (add-text-properties end1
(min (point-max) (1+ end
))
5391 '(face org-meta-line
)) ; for end_src
5393 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5394 (org-src-font-lock-fontify-block lang block-start block-end
)
5395 ;; remove old background overlays
5397 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5398 (delete-overlay ov
)))
5399 (overlays-at (/ (+ beg1 block-end
) 2)))
5400 ;; add a background overlay
5401 (setq ovl
(make-overlay beg1 block-end
))
5402 (overlay-put ovl
'face
'org-block-background
)
5403 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5405 (add-text-properties beg1
(min (point-max) (1+ end1
))
5406 '(face org-block
))) ; end of source block
5407 ((not org-fontify-quote-and-verse-blocks
))
5408 ((string= block-type
"quote")
5409 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-quote
)))
5410 ((string= block-type
"verse")
5411 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-verse
))))
5412 (add-text-properties beg beg1
'(face org-block-begin-line
))
5413 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5414 '(face org-block-end-line
))
5416 ((member dc1
'("+title:" "+author:" "+email:" "+date:"))
5417 (add-text-properties
5419 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5420 '(font-lock-fontified t invisible t
)
5421 '(font-lock-fontified t face org-document-info-keyword
)))
5422 (add-text-properties
5423 (match-beginning 6) (1+ (match-end 6))
5424 (if (string-equal dc1
"+title:")
5425 '(font-lock-fontified t face org-document-title
)
5426 '(font-lock-fontified t face org-document-info
))))
5427 ((or (equal dc1
"+results")
5428 (member dc1
'("+begin:" "+end:" "+caption:" "+label:"
5429 "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
5430 "+call:" "+header:" "+headers:" "+name:"))
5431 (and (match-end 4) (equal dc3
"+attr")))
5432 (add-text-properties
5434 '(font-lock-fontified t face org-meta-line
))
5436 ((member dc3
'(" " ""))
5437 (add-text-properties
5439 '(font-lock-fontified t face font-lock-comment-face
)))
5440 ((not (member (char-after beg
) '(?\ ?
\t)))
5441 ;; just any other in-buffer setting, but not indented
5442 (add-text-properties
5444 '(font-lock-fontified t face org-meta-line
))
5448 (defun org-activate-angle-links (limit)
5449 "Run through the buffer and add overlays to links."
5450 (if (and (re-search-forward org-angle-link-re limit t
)
5451 (not (org-in-src-block-p)))
5453 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5454 (add-text-properties (match-beginning 0) (match-end 0)
5455 (list 'mouse-face
'highlight
5456 'keymap org-mouse-map
))
5457 (org-rear-nonsticky-at (match-end 0))
5460 (defun org-activate-footnote-links (limit)
5461 "Run through the buffer and add overlays to footnotes."
5462 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5464 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5465 (org-remove-flyspell-overlays-in beg end
)
5466 (add-text-properties beg end
5467 (list 'mouse-face
'highlight
5468 'keymap org-mouse-map
5470 (if (= (point-at-bol) beg
)
5471 "Footnote definition"
5472 "Footnote reference")
5473 'font-lock-fontified t
5474 'font-lock-multiline t
5475 'face
'org-footnote
))))))
5477 (defun org-activate-bracket-links (limit)
5478 "Run through the buffer and add overlays to bracketed links."
5479 (if (and (re-search-forward org-bracket-link-regexp limit t
)
5480 (not (org-in-src-block-p)))
5481 (let* ((help (concat "LINK: "
5482 (org-match-string-no-properties 1)))
5483 ;; FIXME: above we should remove the escapes.
5484 ;; but that requires another match, protecting match data,
5485 ;; a lot of overhead for font-lock.
5486 (ip (org-maybe-intangible
5487 (list 'invisible
'org-link
5488 'keymap org-mouse-map
'mouse-face
'highlight
5489 'font-lock-multiline t
'help-echo help
)))
5490 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5491 'font-lock-multiline t
'help-echo help
)))
5492 ;; We need to remove the invisible property here. Table narrowing
5493 ;; may have made some of this invisible.
5494 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5495 (remove-text-properties (match-beginning 0) (match-end 0)
5499 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5500 (org-rear-nonsticky-at (match-beginning 3))
5501 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5502 (org-rear-nonsticky-at (match-end 3))
5503 (add-text-properties (match-end 3) (match-end 0) ip
)
5504 (org-rear-nonsticky-at (match-end 0)))
5505 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5506 (org-rear-nonsticky-at (match-beginning 1))
5507 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5508 (org-rear-nonsticky-at (match-end 1))
5509 (add-text-properties (match-end 1) (match-end 0) ip
)
5510 (org-rear-nonsticky-at (match-end 0)))
5513 (defun org-activate-dates (limit)
5514 "Run through the buffer and add overlays to dates."
5515 (if (re-search-forward org-tsr-regexp-both limit t
)
5517 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5518 (add-text-properties (match-beginning 0) (match-end 0)
5519 (list 'mouse-face
'highlight
5520 'keymap org-mouse-map
))
5521 (org-rear-nonsticky-at (match-end 0))
5522 (when org-display-custom-times
5524 (org-display-custom-time (match-beginning 3) (match-end 3)))
5525 (org-display-custom-time (match-beginning 1) (match-end 1)))
5528 (defvar org-target-link-regexp nil
5529 "Regular expression matching radio targets in plain text.")
5530 (make-variable-buffer-local 'org-target-link-regexp
)
5531 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5532 "Regular expression matching a link target.")
5533 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5534 "Regular expression matching a radio target.")
5535 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5536 "Regular expression matching any target.")
5538 (defun org-activate-target-links (limit)
5539 "Run through the buffer and add overlays to target matches."
5540 (when org-target-link-regexp
5541 (let ((case-fold-search t
))
5542 (if (re-search-forward org-target-link-regexp limit t
)
5544 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5545 (add-text-properties (match-beginning 0) (match-end 0)
5546 (list 'mouse-face
'highlight
5547 'keymap org-mouse-map
5548 'help-echo
"Radio target link"
5549 'org-linked-text t
))
5550 (org-rear-nonsticky-at (match-end 0))
5553 (defun org-update-radio-target-regexp ()
5554 "Find all radio targets in this file and update the regular expression."
5556 (when (memq 'radio org-activate-links
)
5557 (setq org-target-link-regexp
5558 (org-make-target-link-regexp (org-all-targets 'radio
)))
5559 (org-restart-font-lock)))
5561 (defun org-hide-wide-columns (limit)
5563 (setq s
(text-property-any (point) (or limit
(point-max))
5566 (setq e
(next-single-property-change s
'org-cwidth
))
5567 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5571 (defvar org-latex-and-specials-regexp nil
5572 "Regular expression for highlighting export special stuff.")
5573 (defvar org-match-substring-regexp
)
5574 (defvar org-match-substring-with-braces-regexp
)
5576 ;; This should be with the exporter code, but we also use if for font-locking
5577 (defconst org-export-html-special-string-regexps
5578 '(("\\\\-" .
"­")
5579 ("---\\([^-]\\)" .
"—\\1")
5580 ("--\\([^-]\\)" .
"–\\1")
5581 ("\\.\\.\\." .
"…"))
5582 "Regular expressions for special string conversion.")
5585 (defun org-compute-latex-and-specials-regexp ()
5586 "Compute regular expression for stuff treated specially by exporters."
5587 (if (not org-highlight-latex-fragments-and-specials
)
5588 (org-set-local 'org-latex-and-specials-regexp nil
)
5591 ((matchers (plist-get org-format-latex-options
:matchers
))
5592 (latexs (delq nil
(mapcar (lambda (x) (if (member (car x
) matchers
) x
))
5593 org-latex-regexps
)))
5594 (org-export-allow-BIND nil
)
5595 (options (org-combine-plists (org-default-export-plist)
5596 (org-infile-export-plist)))
5597 (org-export-with-sub-superscripts (plist-get options
:sub-superscript
))
5598 (org-export-with-LaTeX-fragments (plist-get options
:LaTeX-fragments
))
5599 (org-export-with-TeX-macros (plist-get options
:TeX-macros
))
5600 (org-export-html-expand (plist-get options
:expand-quoted-html
))
5601 (org-export-with-special-strings (plist-get options
:special-strings
))
5604 ((equal org-export-with-sub-superscripts
'{})
5605 (list org-match-substring-with-braces-regexp
))
5606 (org-export-with-sub-superscripts
5607 (list org-match-substring-regexp
))))
5609 (if org-export-with-LaTeX-fragments
5610 (mapcar (lambda (x) (nth 1 x
)) latexs
)))
5612 (if org-export-with-TeX-macros
5613 (list (concat "\\\\"
5619 (append org-entities-user
5621 (if (boundp 'org-latex-entities
)
5623 (or (car-safe x
) x
))
5628 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5629 (re-special (if org-export-with-special-strings
5630 (mapcar (lambda (x) (car x
))
5631 org-export-html-special-string-regexps
)))
5635 (if org-export-html-expand
"@<[^>\n]+>")
5638 'org-latex-and-specials-regexp
5639 (mapconcat 'identity
(append re-latex re-sub re-macros re-special
5642 (defun org-do-latex-and-special-faces (limit)
5643 "Run through the buffer and add overlays to links."
5644 (when org-latex-and-specials-regexp
5646 (while (and (not rtn
) (re-search-forward org-latex-and-specials-regexp
5648 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5650 '(org-code org-verbatim underline
)))
5653 d
(cond ((member (char-after (1+ (match-beginning 0)))
5656 (font-lock-prepend-text-property
5657 (+ d
(match-beginning 0)) (match-end 0)
5658 'face
'org-latex-and-export-specials
)
5659 (add-text-properties (+ d
(match-beginning 0)) (match-end 0)
5660 '(font-lock-multiline t
)))))
5663 (defun org-restart-font-lock ()
5664 "Restart `font-lock-mode', to force refontification."
5665 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5667 (font-lock-mode 1)))
5669 (defun org-all-targets (&optional radio
)
5670 "Return a list of all targets in this file.
5671 With optional argument RADIO, only find radio targets."
5672 (let ((re (if radio org-radio-target-regexp org-target-regexp
))
5675 (goto-char (point-min))
5676 (while (re-search-forward re nil t
)
5677 (add-to-list 'rtn
(downcase (org-match-string-no-properties 1))))
5680 (defun org-make-target-link-regexp (targets)
5681 "Make regular expression matching all strings in TARGETS.
5682 The regular expression finds the targets also if there is a line break
5689 (setq x
(regexp-quote x
))
5690 (while (string-match " +" x
)
5691 (setq x
(replace-match "\\s-+" t t x
)))
5697 (defun org-activate-tags (limit)
5698 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5700 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5701 (add-text-properties (match-beginning 1) (match-end 1)
5702 (list 'mouse-face
'highlight
5703 'keymap org-mouse-map
))
5704 (org-rear-nonsticky-at (match-end 1))
5707 (defun org-outline-level ()
5708 "Compute the outline level of the heading at point.
5709 This function assumes that the cursor is at the beginning of a line matched
5710 by `outline-regexp'. Otherwise it returns garbage.
5711 If this is called at a normal headline, the level is the number of stars.
5712 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
5714 (looking-at org-outline-regexp
)
5715 (1- (- (match-end 0) (match-beginning 0)))))
5717 (defvar org-font-lock-keywords nil
)
5719 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5720 "Regular expression matching a property line.")
5722 (defvar org-font-lock-hook nil
5723 "Functions to be called for special font lock stuff.")
5725 (defvar org-font-lock-set-keywords-hook nil
5726 "Functions that can manipulate `org-font-lock-extra-keywords'.
5727 This is called after `org-font-lock-extra-keywords' is defined, but before
5728 it is installed to be used by font lock. This can be useful if something
5729 needs to be inserted at a specific position in the font-lock sequence.")
5731 (defun org-font-lock-hook (limit)
5732 "Run `org-font-lock-hook' within LIMIT."
5733 (run-hook-with-args 'org-font-lock-hook limit
))
5735 (defun org-set-font-lock-defaults ()
5736 "Set font lock defaults for the current buffer."
5737 (let* ((em org-fontify-emphasized-text
)
5738 (lk org-activate-links
)
5739 (org-font-lock-extra-keywords
5742 '(org-font-lock-hook)
5744 `(,(if org-fontify-whole-heading-line
5745 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5746 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5747 (1 (org-get-level-face 1))
5748 (2 (org-get-level-face 2))
5749 (3 (org-get-level-face 3)))
5751 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5754 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
5755 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
5756 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
5757 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
5759 (list org-drawer-regexp
'(0 'org-special-keyword t
))
5760 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
5762 (list org-property-re
5763 '(1 'org-special-keyword t
)
5764 '(3 'org-property-value t
))
5766 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
5767 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
5768 (if (memq 'plain lk
) '(org-activate-plain-links))
5769 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
5770 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
5771 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
5772 (if (memq 'footnote lk
) '(org-activate-footnote-links))
5773 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
5774 '(org-hide-wide-columns (0 nil append
))
5776 (list (format org-heading-keyword-regexp-format
5778 '(2 (org-get-todo-face 2) t
))
5780 (if org-fontify-done-headline
5781 (list (format org-heading-keyword-regexp-format
5784 (mapconcat 'regexp-quote org-done-keywords
"\\|")
5786 '(2 'org-headline-done t
))
5789 '(org-font-lock-add-priority-faces)
5791 '(org-font-lock-add-tag-faces)
5793 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
5794 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
5795 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
5796 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
5799 (if (featurep 'xemacs
)
5800 '(org-do-emphasis-faces (0 nil append
))
5801 '(org-do-emphasis-faces)))
5803 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
5804 1 'org-checkbox prepend
)
5805 (if (cdr (assq 'checkbox org-list-automatic-rules
))
5806 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5807 (0 (org-get-checkbox-statistics-face) t
)))
5808 ;; Description list items
5809 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
5810 1 'org-list-dt prepend
)
5811 ;; ARCHIVEd headings
5813 org-outline-regexp-bol
5814 "\\(.*:" org-archive-tag
":.*\\)")
5815 '(1 'org-archived prepend
))
5817 '(org-do-latex-and-special-faces)
5818 '(org-fontify-entities)
5819 '(org-raise-scripts)
5821 '(org-activate-code (1 'org-code t
))
5823 (list (format org-heading-keyword-regexp-format
5825 org-comment-string
"\\|" org-quote-string
5827 '(2 'org-special-keyword t
))
5828 ;; Blocks and meta lines
5829 '(org-fontify-meta-lines-and-blocks)
5831 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
5832 (run-hooks 'org-font-lock-set-keywords-hook
)
5833 ;; Now set the full font-lock-keywords
5834 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
5835 (org-set-local 'font-lock-defaults
5836 '(org-font-lock-keywords t nil nil backward-paragraph
))
5837 (kill-local-variable 'font-lock-keywords
) nil
))
5839 (defun org-toggle-pretty-entities ()
5840 "Toggle the composition display of entities as UTF8 characters."
5842 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
5843 (org-restart-font-lock)
5844 (if org-pretty-entities
5845 (message "Entities are displayed as UTF8 characters")
5848 (org-decompose-region (point-min) (point-max))
5849 (message "Entities are displayed plain"))))
5851 (defvar org-custom-properties-overlays nil
5852 "List of overlays used for custom properties.")
5853 (make-variable-buffer-local 'org-custom-properties-overlays
)
5855 (defun org-toggle-custom-properties-visibility ()
5856 "Display or hide properties in `org-custom-properties'."
5858 (if org-custom-properties-overlays
5859 (progn (mapc 'delete-overlay org-custom-properties-overlays
)
5860 (setq org-custom-properties-overlays nil
))
5861 (unless (not org-custom-properties
)
5865 (goto-char (point-min))
5866 (while (re-search-forward org-property-re nil t
)
5868 (when (equal p
(substring (match-string 1) 1 -
1))
5869 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
5870 (overlay-put o
'invisible t
)
5871 (overlay-put o
'org-custom-property t
)
5872 (push o org-custom-properties-overlays
))))
5873 org-custom-properties
)))))))
5875 (defun org-fontify-entities (limit)
5876 "Find an entity to fontify."
5878 (when org-pretty-entities
5880 (while (re-search-forward
5881 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
5883 (if (and (not (org-in-indented-comment-line))
5884 (setq ee
(org-entity-get (match-string 1)))
5885 (= (length (nth 6 ee
)) 1))
5887 ((end (if (equal (match-string 2) "{}")
5890 (add-text-properties
5891 (match-beginning 0) end
5892 (list 'font-lock-fontified t
))
5893 (compose-region (match-beginning 0) end
5899 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
5900 "Fontify string S like in Org-mode."
5903 (let ((org-odd-levels-only odd-levels
))
5905 (font-lock-fontify-buffer)
5911 (defun org-get-level-face (n)
5912 "Get the right face for match N in font-lock matching of headlines."
5913 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
5914 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
5915 (if org-cycle-level-faces
5916 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
5917 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
5919 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
5921 (t (if org-level-color-stars-only nil org-f
))))
5924 (defun org-get-todo-face (kwd)
5925 "Get the right face for a TODO keyword KWD.
5926 If KWD is a number, get the corresponding match group."
5927 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5928 (or (org-face-from-face-or-color
5929 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
5930 (and (member kwd org-done-keywords
) 'org-done
)
5933 (defun org-face-from-face-or-color (context inherit face-or-color
)
5934 "Create a face list that inherits INHERIT, but sets the foreground color.
5935 When FACE-OR-COLOR is not a string, just return it."
5936 (if (stringp face-or-color
)
5937 (list :inherit inherit
5938 (cdr (assoc context org-faces-easy-properties
))
5942 (defun org-font-lock-add-tag-faces (limit)
5943 "Add the special tag faces."
5944 (when (and org-tag-faces org-tags-special-faces-re
)
5945 (while (re-search-forward org-tags-special-faces-re limit t
)
5946 (add-text-properties (match-beginning 1) (match-end 1)
5947 (list 'face
(org-get-tag-face 1)
5948 'font-lock-fontified t
))
5949 (backward-char 1))))
5951 (defun org-font-lock-add-priority-faces (limit)
5952 "Add the special priority faces."
5953 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
5954 (when (save-match-data (org-at-heading-p))
5955 (add-text-properties
5956 (match-beginning 0) (match-end 0)
5957 (list 'face
(or (org-face-from-face-or-color
5958 'priority
'org-special-keyword
5959 (cdr (assoc (char-after (match-beginning 1))
5960 org-priority-faces
)))
5961 'org-special-keyword
)
5962 'font-lock-fontified t
)))))
5964 (defun org-get-tag-face (kwd)
5965 "Get the right face for a TODO keyword KWD.
5966 If KWD is a number, get the corresponding match group."
5967 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5968 (or (org-face-from-face-or-color
5969 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
5972 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
5973 "Remove fontification and activation overlays from links."
5974 (font-lock-default-unfontify-region beg end
)
5975 (let* ((buffer-undo-list t
)
5976 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
5977 (inhibit-modification-hooks t
)
5978 deactivate-mark buffer-file-name buffer-file-truename
)
5979 (org-decompose-region beg end
)
5980 (remove-text-properties beg end
5981 '(mouse-face t keymap t org-linked-text t
5982 invisible t intangible t
5983 org-no-flyspell t org-emphasis t
))
5984 (org-remove-font-lock-display-properties beg end
)))
5986 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
5987 ((raise 0.3) (height 0.7))
5990 "Display properties for showing superscripts and subscripts.")
5992 (defun org-remove-font-lock-display-properties (beg end
)
5993 "Remove specific display properties that have been added by font lock.
5994 The will remove the raise properties that are used to show superscripts
5998 (setq next
(next-single-property-change beg
'display nil end
)
5999 prop
(get-text-property beg
'display
))
6000 (if (member prop org-script-display
)
6001 (put-text-property beg next
'display nil
))
6004 (defun org-raise-scripts (limit)
6005 "Add raise properties to sub/superscripts."
6006 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
6007 (if (re-search-forward
6008 (if (eq org-use-sub-superscripts t
)
6009 org-match-substring-regexp
6010 org-match-substring-with-braces-regexp
)
6012 (let* ((pos (point)) table-p comment-p
6013 (mpos (match-beginning 3))
6014 (emph-p (get-text-property mpos
'org-emphasis
))
6015 (link-p (get-text-property mpos
'mouse-face
))
6016 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6017 (goto-char (point-at-bol))
6018 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
6019 comment-p
(org-looking-at-p "[ \t]*#"))
6021 ;; FIXME: Should we go back one character here, for a_b^c
6022 ;; (goto-char (1- pos)) ;????????????????????
6023 (if (or comment-p emph-p link-p keyw-p
)
6025 (put-text-property (match-beginning 3) (match-end 0)
6027 (if (equal (char-after (match-beginning 2)) ?^
)
6028 (nth (if table-p
3 1) org-script-display
)
6029 (nth (if table-p
2 0) org-script-display
)))
6030 (add-text-properties (match-beginning 2) (match-end 2)
6032 'org-dwidth t
'org-dwidth-n
1))
6033 (if (and (eq (char-after (match-beginning 3)) ?
{)
6034 (eq (char-before (match-end 3)) ?
}))
6036 (add-text-properties
6037 (match-beginning 3) (1+ (match-beginning 3))
6038 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6039 (add-text-properties
6040 (1- (match-end 3)) (match-end 3)
6041 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6044 ;;;; Visibility cycling, including org-goto and indirect buffer
6048 (defvar org-cycle-global-status nil
)
6049 (make-variable-buffer-local 'org-cycle-global-status
)
6050 (defvar org-cycle-subtree-status nil
)
6051 (make-variable-buffer-local 'org-cycle-subtree-status
)
6053 (defvar org-inlinetask-min-level
)
6056 (defun org-cycle (&optional arg
)
6057 "TAB-action and visibility cycling for Org-mode.
6059 This is the command invoked in Org-mode by the TAB key. Its main purpose
6060 is outline visibility cycling, but it also invokes other actions
6061 in special contexts.
6063 - When this function is called with a prefix argument, rotate the entire
6064 buffer through 3 states (global cycling)
6065 1. OVERVIEW: Show only top-level headlines.
6066 2. CONTENTS: Show all headlines of all levels, but no body text.
6067 3. SHOW ALL: Show everything.
6068 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6069 determined by the variable `org-startup-folded', and by any VISIBILITY
6070 properties in the buffer.
6071 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6072 including any drawers.
6074 - When inside a table, re-align the table and move to the next field.
6076 - When point is at the beginning of a headline, rotate the subtree started
6077 by this line through 3 different states (local cycling)
6078 1. FOLDED: Only the main headline is shown.
6079 2. CHILDREN: The main headline and the direct children are shown.
6080 From this state, you can move to one of the children
6081 and zoom in further.
6082 3. SUBTREE: Show the entire subtree, including body text.
6083 If there is no subtree, switch directly from CHILDREN to FOLDED.
6085 - When point is at the beginning of an empty headline and the variable
6086 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6087 of the headline by demoting and promoting it to likely levels. This
6088 speeds up creation document structure by pressing TAB once or several
6089 times right after creating a new headline.
6091 - When there is a numeric prefix, go up to a heading with level ARG, do
6092 a `show-subtree' and return to the previous cursor position. If ARG
6093 is negative, go up that many levels.
6095 - When point is not at the beginning of a headline, execute the global
6096 binding for TAB, which is re-indenting the line. See the option
6097 `org-cycle-emulate-tab' for details.
6099 - Special case: if point is at the beginning of the buffer and there is
6100 no headline in line 1, this function will act as if called with prefix arg
6101 (C-u TAB, same as S-TAB) also when called without prefix arg.
6102 But only if also the variable `org-cycle-global-at-bob' is t."
6104 (org-load-modules-maybe)
6105 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6106 (and org-cycle-level-after-item
/entry-creation
6107 (or (org-cycle-level)
6108 (org-cycle-item-indentation))))
6110 (or org-cycle-max-level
6111 (and (boundp 'org-inlinetask-min-level
)
6112 org-inlinetask-min-level
6113 (1- org-inlinetask-min-level
))))
6114 (nstars (and limit-level
6115 (if org-odd-levels-only
6116 (and limit-level
(1- (* limit-level
2)))
6119 (if (not (derived-mode-p 'org-mode
))
6121 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6122 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6123 (not (looking-at org-outline-regexp
))))
6126 (delq 'org-optimize-window-after-visibility-change
6127 (copy-sequence org-cycle-hook
))
6131 (if (or bob-special
(equal arg
'(4)))
6132 ;; special case: use global cycling
6138 (setq last-command
'dummy
)
6139 (org-set-startup-visibility)
6140 (message "Startup visibility, plus VISIBILITY properties"))
6144 (message "Entire buffer visible, including drawers"))
6146 ;; Table: enter it or move to the next field.
6147 ((org-at-table-p 'any
)
6148 (if (org-at-table.el-p
)
6149 (message "Use C-c ' to edit table.el tables")
6150 (if arg
(org-table-edit-field t
)
6151 (org-table-justify-field-maybe)
6152 (call-interactively 'org-table-next-field
))))
6154 ((run-hook-with-args-until-success
6155 'org-tab-after-check-for-table-hook
))
6157 ;; Global cycling: delegate to `org-cycle-internal-global'.
6158 ((eq arg t
) (org-cycle-internal-global))
6160 ;; Drawers: delegate to `org-flag-drawer'.
6161 ((and org-drawers org-drawer-regexp
6163 (beginning-of-line 1)
6164 (looking-at org-drawer-regexp
)))
6165 (org-flag-drawer ; toggle block visibility
6166 (not (get-char-property (match-end 0) 'invisible
))))
6168 ;; Show-subtree, ARG levels up from here.
6171 (org-back-to-heading)
6172 (outline-up-heading (if (< arg
0) (- arg
)
6173 (- (funcall outline-level
) arg
)))
6174 (org-show-subtree)))
6176 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6177 ((and (featurep 'org-inlinetask
)
6178 (org-inlinetask-at-task-p)
6179 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6180 (org-inlinetask-toggle-visibility))
6182 ((org-try-cdlatex-tab))
6184 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6185 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6186 (save-excursion (beginning-of-line 1)
6187 (looking-at org-outline-regexp
)))
6188 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6189 (org-cycle-internal-local))
6191 ;; From there: TAB emulation and template completion.
6192 (buffer-read-only (org-back-to-heading))
6194 ((run-hook-with-args-until-success
6195 'org-tab-after-check-for-cycling-hook
))
6197 ((org-try-structure-completion))
6199 ((run-hook-with-args-until-success
6200 'org-tab-before-tab-emulation-hook
))
6202 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6204 (not (looking-at org-outline-regexp
))))
6205 (call-interactively (global-key-binding "\t")))
6207 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6208 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6209 (or (and (eq org-cycle-emulate-tab
'white
)
6210 (= (match-end 0) (point-at-eol)))
6211 (and (eq org-cycle-emulate-tab
'whitestart
)
6212 (>= (match-end 0) pos
))))
6214 (eq org-cycle-emulate-tab t
))
6215 (call-interactively (global-key-binding "\t")))
6218 (org-back-to-heading)
6221 (defun org-cycle-internal-global ()
6222 "Do the global cycling action."
6223 ;; Hack to avoid display of messages for .org attachments in Gnus
6224 (let ((ga (string-match "\\*fontification" (buffer-name))))
6226 ((and (eq last-command this-command
)
6227 (eq org-cycle-global-status
'overview
))
6228 ;; We just created the overview - now do table of contents
6229 ;; This can be slow in very large buffers, so indicate action
6230 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6231 (unless ga
(message "CONTENTS..."))
6233 (unless ga
(message "CONTENTS...done"))
6234 (setq org-cycle-global-status
'contents
)
6235 (run-hook-with-args 'org-cycle-hook
'contents
))
6237 ((and (eq last-command this-command
)
6238 (eq org-cycle-global-status
'contents
))
6239 ;; We just showed the table of contents - now show everything
6240 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6242 (unless ga
(message "SHOW ALL"))
6243 (setq org-cycle-global-status
'all
)
6244 (run-hook-with-args 'org-cycle-hook
'all
))
6247 ;; Default action: go to overview
6248 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6250 (unless ga
(message "OVERVIEW"))
6251 (setq org-cycle-global-status
'overview
)
6252 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6254 (defun org-cycle-internal-local ()
6255 "Do the local cycling action."
6256 (let ((goal-column 0) eoh eol eos has-children children-skipped struct
)
6257 ;; First, determine end of headline (EOH), end of subtree or item
6258 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6263 (setq struct
(org-list-struct))
6264 (setq eoh
(point-at-eol))
6265 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6266 (setq has-children
(org-list-has-child-p (point) struct
)))
6267 (org-back-to-heading)
6268 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6269 (setq eos
(save-excursion (1- (org-end-of-subtree t t
))))
6272 (let ((level (funcall outline-level
)))
6273 (outline-next-heading)
6274 (and (org-at-heading-p t
)
6275 (> (funcall outline-level
) level
))))
6277 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6278 ;; Determine end invisible part of buffer (EOL)
6279 (beginning-of-line 2)
6280 ;; XEmacs doesn't have `next-single-char-property-change'
6281 (if (featurep 'xemacs
)
6282 (while (and (not (eobp)) ;; this is like `next-line'
6283 (get-char-property (1- (point)) 'invisible
))
6284 (beginning-of-line 2))
6285 (while (and (not (eobp)) ;; this is like `next-line'
6286 (get-char-property (1- (point)) 'invisible
))
6287 (goto-char (next-single-char-property-change (point) 'invisible
))
6288 (and (eolp) (beginning-of-line 2))))
6290 ;; Find out what to do next and set `this-command'
6293 ;; Nothing is hidden behind this heading
6294 (unless (org-before-first-heading-p)
6295 (run-hook-with-args 'org-pre-cycle-hook
'empty
))
6296 (message "EMPTY ENTRY")
6297 (setq org-cycle-subtree-status nil
)
6300 (outline-next-heading)
6301 (if (outline-invisible-p) (org-flag-heading nil
))))
6302 ((and (or (>= eol eos
)
6303 (not (string-match "\\S-" (buffer-substring eol eos
))))
6305 (not (setq children-skipped
6306 org-cycle-skip-children-state-if-no-children
))))
6307 ;; Entire subtree is hidden in one line: children view
6308 (unless (org-before-first-heading-p)
6309 (run-hook-with-args 'org-pre-cycle-hook
'children
))
6311 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6313 (org-with-limited-levels (show-children))
6314 ;; FIXME: This slows down the func way too much.
6315 ;; How keep drawers hidden in subtree anyway?
6316 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6317 ;; (org-cycle-hide-drawers 'subtree))
6319 ;; Fold every list in subtree to top-level items.
6320 (when (eq org-cycle-include-plain-lists
'integrate
)
6322 (org-back-to-heading)
6323 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6324 (beginning-of-line 1)
6325 (let* ((struct (org-list-struct))
6326 (prevs (org-list-prevs-alist struct
))
6327 (end (org-list-get-bottom-point struct
)))
6328 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6329 (org-list-get-all-items (point) struct prevs
))
6330 (goto-char end
))))))
6331 (message "CHILDREN")
6334 (outline-next-heading)
6335 (if (outline-invisible-p) (org-flag-heading nil
)))
6336 (setq org-cycle-subtree-status
'children
)
6337 (unless (org-before-first-heading-p)
6338 (run-hook-with-args 'org-cycle-hook
'children
)))
6339 ((or children-skipped
6340 (and (eq last-command this-command
)
6341 (eq org-cycle-subtree-status
'children
)))
6342 ;; We just showed the children, or no children are there,
6343 ;; now show everything.
6344 (unless (org-before-first-heading-p)
6345 (run-hook-with-args 'org-pre-cycle-hook
'subtree
))
6346 (outline-flag-region eoh eos nil
)
6347 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6348 (setq org-cycle-subtree-status
'subtree
)
6349 (unless (org-before-first-heading-p)
6350 (run-hook-with-args 'org-cycle-hook
'subtree
)))
6352 ;; Default action: hide the subtree.
6353 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6354 (outline-flag-region eoh eos t
)
6356 (setq org-cycle-subtree-status
'folded
)
6357 (unless (org-before-first-heading-p)
6358 (run-hook-with-args 'org-cycle-hook
'folded
))))))
6361 (defun org-global-cycle (&optional arg
)
6362 "Cycle the global visibility. For details see `org-cycle'.
6363 With \\[universal-argument] prefix arg, switch to startup visibility.
6364 With a numeric prefix, show all headlines up to that level."
6366 (let ((org-cycle-include-plain-lists
6367 (if (derived-mode-p 'org-mode
) org-cycle-include-plain-lists nil
)))
6371 (hide-sublevels arg
)
6372 (setq org-cycle-global-status
'contents
))
6374 (org-set-startup-visibility)
6375 (message "Startup visibility, plus VISIBILITY properties."))
6377 (org-cycle '(4))))))
6379 (defun org-set-startup-visibility ()
6380 "Set the visibility required by startup options and properties."
6382 ((eq org-startup-folded t
)
6384 ((eq org-startup-folded
'content
)
6385 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6386 (org-cycle '(4)) (org-cycle '(4)))))
6387 (unless (eq org-startup-folded
'showeverything
)
6388 (if org-hide-block-startup
(org-hide-block-all))
6389 (org-set-visibility-according-to-property 'no-cleanup
)
6390 (org-cycle-hide-archived-subtrees 'all
)
6391 (org-cycle-hide-drawers 'all
)
6392 (org-cycle-show-empty-lines t
)))
6394 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6395 "Switch subtree visibilities according to :VISIBILITY: property."
6397 (let (org-show-entry-below state
)
6399 (goto-char (point-min))
6400 (while (re-search-forward
6401 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6403 (setq state
(match-string 1))
6405 (org-back-to-heading t
)
6409 ((equal state
'("fold" "folded"))
6411 ((equal state
"children")
6412 (org-show-hidden-entry)
6414 ((equal state
"content")
6417 (org-narrow-to-subtree)
6419 ((member state
'("all" "showall"))
6422 (org-cycle-hide-archived-subtrees 'all
)
6423 (org-cycle-hide-drawers 'all
)
6424 (org-cycle-show-empty-lines 'all
)))))
6426 ;; This function uses outline-regexp instead of the more fundamental
6427 ;; org-outline-regexp so that org-cycle-global works outside of Org
6428 ;; buffers, where outline-regexp is needed.
6429 (defun org-overview ()
6430 "Switch to overview mode, showing only top-level headlines.
6431 Really, this shows all headlines with level equal or greater than the level
6432 of the first headline in the buffer. This is important, because if the
6433 first headline is not level one, then (hide-sublevels 1) gives confusing
6436 (let ((level (save-excursion
6437 (goto-char (point-min))
6438 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6440 (goto-char (match-beginning 0))
6441 (funcall outline-level
))))))
6442 (and level
(hide-sublevels level
))))
6444 (defun org-content (&optional arg
)
6445 "Show all headlines in the buffer, like a table of contents.
6446 With numerical argument N, show content up to level N."
6449 ;; Visit all headings and show their offspring
6450 (and (integerp arg
) (org-overview))
6451 (goto-char (point-max))
6453 (while (and (progn (condition-case nil
6454 (outline-previous-visible-heading 1)
6455 (error (goto-char (point-min))))
6457 (looking-at org-outline-regexp
))
6459 (show-children (1- arg
))
6461 (if (bobp) (throw 'exit nil
))))))
6464 (defun org-optimize-window-after-visibility-change (state)
6465 "Adjust the window after a change in outline visibility.
6466 This function is the default value of the hook `org-cycle-hook'."
6467 (when (get-buffer-window (current-buffer))
6469 ((eq state
'content
) nil
)
6470 ((eq state
'all
) nil
)
6471 ((eq state
'folded
) nil
)
6472 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6473 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6475 (defun org-remove-empty-overlays-at (pos)
6476 "Remove outline overlays that do not contain non-white stuff."
6479 (and (eq 'outline
(overlay-get o
'invisible
))
6480 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6482 (delete-overlay o
)))
6485 (defun org-clean-visibility-after-subtree-move ()
6486 "Fix visibility issues after moving a subtree."
6487 ;; First, find a reasonable region to look at:
6488 ;; Start two siblings above, end three below
6489 (let* ((beg (save-excursion
6490 (and (org-get-last-sibling)
6491 (org-get-last-sibling))
6493 (end (save-excursion
6494 (and (org-get-next-sibling)
6495 (org-get-next-sibling)
6496 (org-get-next-sibling))
6497 (if (org-at-heading-p)
6500 (level (looking-at "\\*+"))
6501 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6504 (narrow-to-region beg end
)
6506 ;; Properly fold already folded siblings
6507 (goto-char (point-min))
6508 (while (re-search-forward re nil t
)
6509 (if (and (not (outline-invisible-p))
6511 (goto-char (point-at-eol)) (outline-invisible-p)))
6513 (org-cycle-show-empty-lines 'overview
)
6514 (org-cycle-hide-drawers 'overview
)))))
6516 (defun org-cycle-show-empty-lines (state)
6517 "Show empty lines above all visible headlines.
6518 The region to be covered depends on STATE when called through
6519 `org-cycle-hook'. Lisp program can use t for STATE to get the
6520 entire buffer covered. Note that an empty line is only shown if there
6521 are at least `org-cycle-separator-lines' empty lines before the headline."
6522 (when (not (= org-cycle-separator-lines
0))
6524 (let* ((n (abs org-cycle-separator-lines
))
6526 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6527 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6528 (t (let ((ns (number-to-string (- n
2))))
6529 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6530 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6533 ((memq state
'(overview contents t
))
6534 (setq beg
(point-min) end
(point-max)))
6535 ((memq state
'(children folded
))
6536 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6537 (beginning-of-line 2)
6541 (while (re-search-forward re end t
)
6542 (unless (get-char-property (match-end 1) 'invisible
)
6543 (setq e
(match-end 1))
6544 (if (< org-cycle-separator-lines
0)
6545 (setq b
(save-excursion
6546 (goto-char (match-beginning 0))
6547 (org-back-over-empty-lines)
6549 (goto-char (max (point-min) (1- (point))))
6553 (setq b
(match-beginning 1)))
6554 (outline-flag-region b e nil
)))))))
6555 ;; Never hide empty lines at the end of the file.
6557 (goto-char (point-max))
6558 (outline-previous-heading)
6559 (outline-end-of-heading)
6560 (if (and (looking-at "[ \t\n]+")
6561 (= (match-end 0) (point-max)))
6562 (outline-flag-region (point) (match-end 0) nil
))))
6564 (defun org-show-empty-lines-in-parent ()
6565 "Move to the parent and re-show empty lines before visible headlines."
6567 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6568 (org-cycle-show-empty-lines context
))))
6570 (defun org-files-list ()
6571 "Return `org-agenda-files' list, plus all open org-mode files.
6572 This is useful for operations that need to scan all of a user's
6573 open and agenda-wise Org files."
6574 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6575 (dolist (buf (buffer-list))
6576 (with-current-buffer buf
6577 (if (and (derived-mode-p 'org-mode
) (buffer-file-name))
6578 (let ((file (expand-file-name (buffer-file-name))))
6579 (unless (member file files
)
6580 (push file files
))))))
6583 (defsubst org-entry-beginning-position
()
6584 "Return the beginning position of the current entry."
6585 (save-excursion (outline-back-to-heading t
) (point)))
6587 (defsubst org-entry-end-position
()
6588 "Return the end position of the current entry."
6589 (save-excursion (outline-next-heading) (point)))
6591 (defun org-cycle-hide-drawers (state)
6592 "Re-hide all drawers after a visibility state change."
6593 (when (and (derived-mode-p 'org-mode
)
6594 (not (memq state
'(overview folded contents
))))
6596 (let* ((globalp (memq state
'(contents all
)))
6597 (beg (if globalp
(point-min) (point)))
6598 (end (if globalp
(point-max)
6599 (if (eq state
'children
)
6600 (save-excursion (outline-next-heading) (point))
6601 (org-end-of-subtree t
)))))
6603 (while (re-search-forward org-drawer-regexp end t
)
6604 (org-flag-drawer t
))))))
6606 (defun org-flag-drawer (flag)
6607 "When FLAG is non-nil, hide the drawer we are within.
6608 Otherwise make it visible."
6610 (beginning-of-line 1)
6611 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6612 (let ((b (match-end 0)))
6613 (if (re-search-forward
6615 (save-excursion (outline-next-heading) (point)) t
)
6616 (outline-flag-region b
(point-at-eol) flag
)
6617 (error ":END: line missing at position %s" b
))))))
6619 (defun org-subtree-end-visible-p ()
6620 "Is the end of the current subtree visible?"
6621 (pos-visible-in-window-p
6622 (save-excursion (org-end-of-subtree t
) (point))))
6624 (defun org-first-headline-recenter (&optional N
)
6625 "Move cursor to the first headline and recenter the headline.
6626 Optional argument N means put the headline into the Nth line of the window."
6627 (goto-char (point-min))
6628 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
6630 (recenter (prefix-numeric-value N
))))
6632 ;;; Saving and restoring visibility
6634 (defun org-outline-overlay-data (&optional use-markers
)
6635 "Return a list of the locations of all outline overlays.
6636 These are overlays with the `invisible' property value `outline'.
6637 The return value is a list of cons cells, with start and stop
6638 positions for each overlay.
6639 If USE-MARKERS is set, return the positions as markers."
6646 (when (eq (overlay-get o
'invisible
) 'outline
)
6647 (setq beg
(overlay-start o
)
6648 end
(overlay-end o
))
6649 (and beg end
(> end beg
)
6651 (cons (move-marker (make-marker) beg
)
6652 (move-marker (make-marker) end
))
6654 (overlays-in (point-min) (point-max))))))))
6656 (defun org-set-outline-overlay-data (data)
6657 "Create visibility overlays for all positions in DATA.
6658 DATA should have been made by `org-outline-overlay-data'."
6665 (outline-flag-region (car c
) (cdr c
) t
))
6668 ;;; Folding of blocks
6670 (defvar org-hide-block-overlays nil
6671 "Overlays hiding blocks.")
6672 (make-variable-buffer-local 'org-hide-block-overlays
)
6674 (defun org-block-map (function &optional start end
)
6675 "Call FUNCTION at the head of all source blocks in the current buffer.
6676 Optional arguments START and END can be used to limit the range."
6677 (let ((start (or start
(point-min)))
6678 (end (or end
(point-max))))
6681 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6684 (goto-char (match-beginning 0))
6685 (funcall function
)))))))
6687 (defun org-hide-block-toggle-all ()
6688 "Toggle the visibility of all blocks in the current buffer."
6689 (org-block-map #'org-hide-block-toggle
))
6691 (defun org-hide-block-all ()
6692 "Fold all blocks in the current buffer."
6694 (org-show-block-all)
6695 (org-block-map #'org-hide-block-toggle-maybe
))
6697 (defun org-show-block-all ()
6698 "Unfold all blocks in the current buffer."
6700 (mapc 'delete-overlay org-hide-block-overlays
)
6701 (setq org-hide-block-overlays nil
))
6703 (defun org-hide-block-toggle-maybe ()
6704 "Toggle visibility of block at point."
6706 (let ((case-fold-search t
))
6708 (beginning-of-line 1)
6709 (looking-at org-block-regexp
))
6710 (progn (org-hide-block-toggle)
6711 t
) ;; to signal that we took action
6712 nil
))) ;; to signal that we did not
6714 (defun org-hide-block-toggle (&optional force
)
6715 "Toggle the visibility of the current block."
6719 (if (re-search-forward org-block-regexp nil t
)
6720 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
6721 (end (match-end 0)) ;; end of entire body
6723 (if (memq t
(mapcar (lambda (overlay)
6724 (eq (overlay-get overlay
'invisible
)
6726 (overlays-at start
)))
6727 (if (or (not force
) (eq force
'off
))
6729 (when (member ov org-hide-block-overlays
)
6730 (setq org-hide-block-overlays
6731 (delq ov org-hide-block-overlays
)))
6732 (when (eq (overlay-get ov
'invisible
)
6734 (delete-overlay ov
)))
6735 (overlays-at start
)))
6736 (setq ov
(make-overlay start end
))
6737 (overlay-put ov
'invisible
'org-hide-block
)
6738 ;; make the block accessible to isearch
6740 ov
'isearch-open-invisible
6742 (when (member ov org-hide-block-overlays
)
6743 (setq org-hide-block-overlays
6744 (delq ov org-hide-block-overlays
)))
6745 (when (eq (overlay-get ov
'invisible
)
6747 (delete-overlay ov
))))
6748 (push ov org-hide-block-overlays
)))
6749 (error "Not looking at a source block"))))
6751 ;; org-tab-after-check-for-cycling-hook
6752 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
6753 ;; Remove overlays when changing major mode
6754 (add-hook 'org-mode-hook
6755 (lambda () (org-add-hook 'change-major-mode-hook
6756 'org-show-block-all
'append
'local
)))
6760 (defvar org-goto-window-configuration nil
)
6761 (defvar org-goto-marker nil
)
6762 (defvar org-goto-map
)
6763 (defun org-goto-map ()
6764 "Set the keymap `org-goto'."
6766 (let ((map (make-sparse-keymap)))
6767 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
6768 mouse-drag-region universal-argument org-occur
))
6770 (while (setq cmd
(pop cmds
))
6771 (substitute-key-definition cmd cmd map global-map
)))
6772 (suppress-keymap map
)
6773 (org-defkey map
"\C-m" 'org-goto-ret
)
6774 (org-defkey map
[(return)] 'org-goto-ret
)
6775 (org-defkey map
[(left)] 'org-goto-left
)
6776 (org-defkey map
[(right)] 'org-goto-right
)
6777 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
6778 (org-defkey map
"\C-i" 'org-cycle
)
6779 (org-defkey map
[(tab)] 'org-cycle
)
6780 (org-defkey map
[(down)] 'outline-next-visible-heading
)
6781 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
6782 (if org-goto-auto-isearch
6783 (if (fboundp 'define-key-after
)
6784 (define-key-after map
[t] 'org-goto-local-auto-isearch)
6786 (org-defkey map "q" 'org-goto-quit)
6787 (org-defkey map "n" 'outline-next-visible-heading)
6788 (org-defkey map "p" 'outline-previous-visible-heading)
6789 (org-defkey map "f" 'outline-forward-same-level)
6790 (org-defkey map "b" 'outline-backward-same-level)
6791 (org-defkey map "u" 'outline-up-heading))
6792 (org-defkey map "/" 'org-occur)
6793 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6794 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6795 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6796 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6797 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6800 (defconst org-goto-help
6801 "Browse buffer copy, to find location or copy text.%s
6802 RET=jump to location C-g=quit and return to previous location
6803 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6805 (defvar org-goto-start-pos) ; dynamically scoped parameter
6807 ;; FIXME: Docstring does not mention both interfaces
6808 (defun org-goto (&optional alternative-interface)
6809 "Look up a different location in the current file, keeping current visibility.
6811 When you want look-up or go to a different location in a
6812 document, the fastest way is often to fold the entire buffer and
6813 then dive into the tree. This method has the disadvantage, that
6814 the previous location will be folded, which may not be what you
6817 This command works around this by showing a copy of the current
6818 buffer in an indirect buffer, in overview mode. You can dive
6819 into the tree in that copy, use org-occur and incremental search
6820 to find a location. When pressing RET or `Q', the command
6821 returns to the original buffer in which the visibility is still
6822 unchanged. After RET it will also jump to the location selected
6823 in the indirect buffer and expose the headline hierarchy above.
6825 With a prefix argument, use the alternative interface: e.g. if
6826 `org-goto-interface' is 'outline use 'outline-path-completion."
6829 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6830 (org-refile-use-outline-path t)
6831 (org-refile-target-verify-function nil)
6833 (if (not alternative-interface)
6835 (if (eq org-goto-interface 'outline)
6836 'outline-path-completion
6838 (org-goto-start-pos (point))
6840 (if (eq interface 'outline)
6841 (car (org-get-location (current-buffer) org-goto-help))
6842 (let ((pa (org-refile-get-location "Goto" nil nil t)))
6843 (org-refile-check-position pa)
6847 (org-mark-ring-push org-goto-start-pos)
6848 (goto-char selected-point)
6849 (if (or (outline-invisible-p) (org-invisible-p2))
6850 (org-show-context 'org-goto)))
6853 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6854 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6855 (defvar org-goto-local-auto-isearch-map) ; defined below
6857 (defun org-get-location (buf help)
6858 "Let the user select a location in the Org-mode buffer BUF.
6859 This function uses a recursive edit. It returns the selected position
6862 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6863 (isearch-hide-immediately nil)
6864 (isearch-search-fun-function
6865 (lambda () 'org-goto-local-search-headings))
6866 (org-goto-selected-point org-goto-exit-command))
6868 (save-window-excursion
6869 (delete-other-windows)
6870 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6871 (org-pop-to-buffer-same-window
6873 (make-indirect-buffer (current-buffer) "*org-goto*")
6874 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6875 (with-output-to-temp-buffer "*Org Help*"
6876 (princ (format help (if org-goto-auto-isearch
6877 " Just type for auto-isearch."
6878 " n/p/f/b/u to navigate, q to quit."))))
6879 (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
6880 (setq buffer-read-only nil)
6881 (let ((org-startup-truncated t)
6882 (org-startup-folded nil)
6883 (org-startup-align-all-tables nil))
6886 (setq buffer-read-only t)
6887 (if (and (boundp 'org-goto-start-pos)
6888 (integer-or-marker-p org-goto-start-pos))
6889 (let ((org-show-hierarchy-above t)
6890 (org-show-siblings t)
6891 (org-show-following-heading t))
6892 (goto-char org-goto-start-pos)
6893 (and (outline-invisible-p) (org-show-context)))
6894 (goto-char (point-min)))
6895 (let (org-special-ctrl-a/e) (org-beginning-of-line))
6896 (message "Select location and press RET")
6897 (use-local-map org-goto-map)
6899 (kill-buffer "*org-goto*")
6900 (cons org-goto-selected-point org-goto-exit-command))))
6902 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6903 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6904 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6905 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6907 (defun org-goto-local-search-headings (string bound noerror)
6908 "Search and make sure that any matches are in headlines."
6910 (while (if isearch-forward
6911 (search-forward string bound noerror)
6912 (search-backward string bound noerror))
6913 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6914 (and (member :headline context)
6915 (not (member :tags context))))
6916 (throw 'return (point))))))
6918 (defun org-goto-local-auto-isearch ()
6921 (goto-char (point-min))
6922 (let ((keys (this-command-keys)))
6923 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6925 (isearch-process-search-char (string-to-char keys)))))
6927 (defun org-goto-ret (&optional arg)
6928 "Finish `org-goto' by going to the new location."
6930 (setq org-goto-selected-point (point)
6931 org-goto-exit-command 'return)
6934 (defun org-goto-left ()
6935 "Finish `org-goto' by going to the new location."
6937 (if (org-at-heading-p)
6939 (beginning-of-line 1)
6940 (setq org-goto-selected-point (point)
6941 org-goto-exit-command 'left)
6943 (error "Not on a heading")))
6945 (defun org-goto-right ()
6946 "Finish `org-goto' by going to the new location."
6948 (if (org-at-heading-p)
6950 (setq org-goto-selected-point (point)
6951 org-goto-exit-command 'right)
6953 (error "Not on a heading")))
6955 (defun org-goto-quit ()
6956 "Finish `org-goto' without cursor motion."
6958 (setq org-goto-selected-point nil)
6959 (setq org-goto-exit-command 'quit)
6962 ;;; Indirect buffer display of subtrees
6964 (defvar org-indirect-dedicated-frame nil
6965 "This is the frame being used for indirect tree display.")
6966 (defvar org-last-indirect-buffer nil)
6968 (defun org-tree-to-indirect-buffer (&optional arg)
6969 "Create indirect buffer and narrow it to current subtree.
6970 With a numerical prefix ARG, go up to this level and then take that tree.
6971 If ARG is negative, go up that many levels.
6973 If `org-indirect-buffer-display' is not `new-frame', the command removes the
6974 indirect buffer previously made with this command, to avoid proliferation of
6975 indirect buffers. However, when you call the command with a \
6976 \\[universal-argument] prefix, or
6977 when `org-indirect-buffer-display' is `new-frame', the last buffer
6978 is kept so that you can work with several indirect buffers at the same time.
6979 If `org-indirect-buffer-display' is `dedicated-frame', the \
6980 \\[universal-argument] prefix also
6981 requests that a new frame be made for the new buffer, so that the dedicated
6982 frame is not changed."
6984 (let ((cbuf (current-buffer))
6985 (cwin (selected-window))
6987 beg end level heading ibuf)
6989 (org-back-to-heading t)
6991 (setq level (org-outline-level))
6992 (if (< arg 0) (setq arg (+ level arg)))
6993 (while (> (setq level (org-outline-level)) arg)
6994 (org-up-heading-safe)))
6996 heading (org-get-heading))
6997 (org-end-of-subtree t t)
6998 (if (org-at-heading-p) (backward-char 1))
7000 (if (and (buffer-live-p org-last-indirect-buffer)
7001 (not (eq org-indirect-buffer-display 'new-frame))
7003 (kill-buffer org-last-indirect-buffer))
7004 (setq ibuf (org-get-indirect-buffer cbuf)
7005 org-last-indirect-buffer ibuf)
7007 ((or (eq org-indirect-buffer-display 'new-frame)
7008 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7009 (select-frame (make-frame))
7010 (delete-other-windows)
7011 (org-pop-to-buffer-same-window ibuf)
7012 (org-set-frame-title heading))
7013 ((eq org-indirect-buffer-display 'dedicated-frame)
7015 (select-frame (or (and org-indirect-dedicated-frame
7016 (frame-live-p org-indirect-dedicated-frame)
7017 org-indirect-dedicated-frame)
7018 (setq org-indirect-dedicated-frame (make-frame)))))
7019 (delete-other-windows)
7020 (org-pop-to-buffer-same-window ibuf)
7021 (org-set-frame-title (concat "Indirect: " heading)))
7022 ((eq org-indirect-buffer-display 'current-window)
7023 (org-pop-to-buffer-same-window ibuf))
7024 ((eq org-indirect-buffer-display 'other-window)
7025 (pop-to-buffer ibuf))
7026 (t (error "Invalid value")))
7027 (if (featurep 'xemacs)
7028 (save-excursion (org-mode) (turn-on-font-lock)))
7029 (narrow-to-region beg end)
7032 (run-hook-with-args 'org-cycle-hook 'all)
7033 (and (window-live-p cwin) (select-window cwin))))
7035 (defun org-get-indirect-buffer (&optional buffer)
7036 (setq buffer (or buffer (current-buffer)))
7037 (let ((n 1) (base (buffer-name buffer)) bname)
7038 (while (buffer-live-p
7039 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7042 (make-indirect-buffer buffer bname 'clone)
7043 (error (make-indirect-buffer buffer bname)))))
7045 (defun org-set-frame-title (title)
7046 "Set the title of the current frame to the string TITLE."
7047 ;; FIXME: how to name a single frame in XEmacs???
7048 (unless (featurep 'xemacs)
7049 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7051 ;;;; Structure editing
7053 ;;; Inserting headlines
7055 (defun org-previous-line-empty-p ()
7058 (or (beginning-of-line 0) t)
7060 (looking-at "[ \t]*$")))))
7062 (defun org-insert-heading (&optional force-heading invisible-ok)
7063 "Insert a new heading or item with same depth at point.
7064 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
7065 If point is at the beginning of a headline, insert a sibling before the
7066 current headline. If point is not at the beginning, split the line,
7067 create the new headline with the text in the current line after point
7068 \(but see also the variable `org-M-RET-may-split-line').
7070 When INVISIBLE-OK is set, stop at invisible headlines when going back.
7071 This is important for non-interactive uses of the command."
7073 (if (or (= (buffer-size) 0)
7074 (and (not (save-excursion
7075 (and (ignore-errors (org-back-to-heading invisible-ok))
7076 (org-at-heading-p))))
7077 (or force-heading (not (org-in-item-p)))))
7080 (run-hooks 'org-insert-heading-hook))
7081 (when (or force-heading (not (org-insert-item)))
7082 (let* ((empty-line-p nil)
7084 (on-heading (org-at-heading-p))
7085 (head (save-excursion
7088 (org-back-to-heading invisible-ok)
7089 (when (and (not on-heading)
7090 (featurep 'org-inlinetask)
7091 (integerp org-inlinetask-min-level)
7092 (>= (length (match-string 0))
7093 org-inlinetask-min-level))
7094 ;; Find a heading level before the inline task
7095 (while (and (setq level (org-up-heading-safe))
7096 (>= level org-inlinetask-min-level)))
7097 (if (org-at-heading-p)
7098 (org-back-to-heading invisible-ok)
7099 (error "This should not happen")))
7100 (setq empty-line-p (org-previous-line-empty-p))
7103 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7104 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7105 pos hide-previous previous-pos)
7107 ((and (org-at-heading-p) (bolp)
7109 (save-excursion (backward-char 1) (not (outline-invisible-p)))))
7110 ;; insert before the current line
7111 (open-line (if blank 2 1)))
7113 (not org-insert-heading-respect-content)
7116 (backward-char 1) (not (outline-invisible-p)))))
7117 ;; insert right here
7120 ;; somewhere in the line
7122 (setq previous-pos (point-at-bol))
7124 (setq hide-previous (outline-invisible-p)))
7125 (and org-insert-heading-respect-content (org-show-subtree))
7127 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
7130 (goto-char (point-at-bol))
7131 (and (looking-at org-complex-heading-regexp)
7133 (> p (match-beginning 4)))))))
7136 (org-insert-heading-respect-content
7137 (org-end-of-subtree nil t)
7138 (when (featurep 'org-inlinetask)
7139 (while (and (not (eobp))
7140 (looking-at "\\(\\*+\\)[ \t]+")
7141 (>= (length (match-string 1))
7142 org-inlinetask-min-level))
7143 (org-end-of-subtree nil t)))
7144 (or (bolp) (newline))
7145 (or (org-previous-line-empty-p)
7146 (and blank (newline)))
7152 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
7153 (setq tags (and (match-end 2) (match-string 2)))
7155 (delete-region (match-beginning 1) (match-end 1)))
7156 (setq pos (point-at-bol))
7157 (or split (end-of-line 1))
7158 (delete-horizontal-space)
7159 (if (string-match "\\`\\*+\\'"
7160 (buffer-substring (point-at-bol) (point)))
7162 (newline (if blank 2 1))
7168 (org-set-tags nil 'align))))
7170 (or split (end-of-line 1))
7171 (newline (if blank 2 1)))))))
7172 (insert head) (just-one-space)
7175 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
7176 (when (and org-insert-heading-respect-content hide-previous)
7178 (goto-char previous-pos)
7180 (run-hooks 'org-insert-heading-hook)))))
7182 (defun org-get-heading (&optional no-tags no-todo)
7183 "Return the heading of the current entry, without the stars.
7184 When NO-TAGS is non-nil, don't include tags.
7185 When NO-TODO is non-nil, don't include TODO keywords."
7187 (org-back-to-heading t)
7189 ((and no-tags no-todo)
7190 (looking-at org-complex-heading-regexp)
7193 (looking-at (concat org-outline-regexp
7195 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7198 (looking-at org-todo-line-regexp)
7200 (t (looking-at org-heading-regexp)
7201 (match-string 2)))))
7203 (defun org-heading-components ()
7204 "Return the components of the current heading.
7205 This is a list with the following elements:
7206 - the level as an integer
7207 - the reduced level, different if `org-odd-levels-only' is set.
7208 - the TODO keyword, or nil
7209 - the priority character, like ?A, or nil if no priority is given
7210 - the headline text itself, or the tags string if no headline text
7211 - the tags string, or nil."
7213 (org-back-to-heading t)
7214 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
7215 (list (length (match-string 1))
7216 (org-reduced-level (length (match-string 1)))
7217 (org-match-string-no-properties 2)
7218 (and (match-end 3) (aref (match-string 3) 2))
7219 (org-match-string-no-properties 4)
7220 (org-match-string-no-properties 5)))))
7222 (defun org-get-entry ()
7223 "Get the entry text, after heading, entire subtree."
7225 (org-back-to-heading t)
7226 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7228 (defun org-insert-heading-after-current ()
7229 "Insert a new heading with same level as current, after current subtree."
7231 (org-back-to-heading)
7232 (org-insert-heading)
7233 (org-move-subtree-down)
7236 (defun org-insert-heading-respect-content ()
7238 (let ((org-insert-heading-respect-content t))
7239 (org-insert-heading t)))
7241 (defun org-insert-todo-heading-respect-content (&optional force-state)
7243 (let ((org-insert-heading-respect-content t))
7244 (org-insert-todo-heading force-state t)))
7246 (defun org-insert-todo-heading (arg &optional force-heading)
7247 "Insert a new heading with the same level and TODO state as current heading.
7248 If the heading has no TODO state, or if the state is DONE, use the first
7249 state (TODO by default). Also with prefix arg, force first state."
7251 (when (or force-heading (not (org-insert-item 'checkbox)))
7252 (org-insert-heading force-heading)
7254 (org-back-to-heading)
7255 (outline-previous-heading)
7256 (looking-at org-todo-line-regexp))
7260 (not (match-beginning 2))
7261 (member (match-string 2) org-done-keywords))
7262 (car org-todo-keywords-1)
7266 (run-hook-with-args-until-success
7267 'org-todo-get-default-hook new-mark-x nil)
7269 (beginning-of-line 1)
7270 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7271 (if org-treat-insert-todo-heading-as-state-change
7273 (insert new-mark " "))))
7274 (when org-provide-todo-statistics
7275 (org-update-parent-todo-statistics))))
7277 (defun org-insert-subheading (arg)
7278 "Insert a new subheading and demote it.
7279 Works for outline headings and for plain lists alike."
7281 (org-insert-heading arg)
7283 ((org-at-heading-p) (org-do-demote))
7284 ((org-at-item-p) (org-indent-item))))
7286 (defun org-insert-todo-subheading (arg)
7287 "Insert a new subheading with TODO keyword or checkbox and demote it.
7288 Works for outline headings and for plain lists alike."
7290 (org-insert-todo-heading arg)
7292 ((org-at-heading-p) (org-do-demote))
7293 ((org-at-item-p) (org-indent-item))))
7295 ;;; Promotion and Demotion
7297 (defvar org-after-demote-entry-hook nil
7298 "Hook run after an entry has been demoted.
7299 The cursor will be at the beginning of the entry.
7300 When a subtree is being demoted, the hook will be called for each node.")
7302 (defvar org-after-promote-entry-hook nil
7303 "Hook run after an entry has been promoted.
7304 The cursor will be at the beginning of the entry.
7305 When a subtree is being promoted, the hook will be called for each node.")
7307 (defun org-promote-subtree ()
7308 "Promote the entire subtree.
7309 See also `org-promote'."
7312 (org-with-limited-levels (org-map-tree 'org-promote)))
7313 (org-fix-position-after-promote))
7315 (defun org-demote-subtree ()
7316 "Demote the entire subtree. See `org-demote'.
7317 See also `org-promote'."
7320 (org-with-limited-levels (org-map-tree 'org-demote)))
7321 (org-fix-position-after-promote))
7324 (defun org-do-promote ()
7325 "Promote the current heading higher up the tree.
7326 If the region is active in `transient-mark-mode', promote all headings
7330 (if (org-region-active-p)
7331 (org-map-region 'org-promote (region-beginning) (region-end))
7333 (org-fix-position-after-promote))
7335 (defun org-do-demote ()
7336 "Demote the current heading lower down the tree.
7337 If the region is active in `transient-mark-mode', demote all headings
7341 (if (org-region-active-p)
7342 (org-map-region 'org-demote (region-beginning) (region-end))
7344 (org-fix-position-after-promote))
7346 (defun org-fix-position-after-promote ()
7347 "Make sure that after pro/demotion cursor position is right."
7348 (let ((pos (point)))
7349 (when (save-excursion
7350 (beginning-of-line 1)
7351 (looking-at org-todo-line-regexp)
7352 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7353 (cond ((eobp) (insert " "))
7354 ((eolp) (insert " "))
7355 ((equal (char-after) ?\ ) (forward-char 1))))))
7357 (defun org-current-level ()
7358 "Return the level of the current entry, or nil if before the first headline.
7359 The level is the number of stars at the beginning of the headline."
7361 (org-with-limited-levels
7362 (if (ignore-errors (org-back-to-heading t))
7363 (funcall outline-level)))))
7365 (defun org-get-previous-line-level ()
7366 "Return the outline depth of the last headline before the current line.
7367 Returns 0 for the first headline in the buffer, and nil if before the
7369 (let ((current-level (org-current-level))
7370 (prev-level (when (> (line-number-at-pos) 1)
7372 (beginning-of-line 0)
7373 (org-current-level)))))
7374 (cond ((null current-level) nil) ; Before first headline
7375 ((null prev-level) 0) ; At first headline
7378 (defun org-reduced-level (l)
7379 "Compute the effective level of a heading.
7380 This takes into account the setting of `org-odd-levels-only'."
7383 (org-odd-levels-only (1+ (floor (/ l 2))))
7386 (defun org-level-increment ()
7387 "Return the number of stars that will be added or removed at a
7388 time to headlines when structure editing, based on the value of
7389 `org-odd-levels-only'."
7390 (if org-odd-levels-only 2 1))
7392 (defun org-get-valid-level (level &optional change)
7393 "Rectify a level change under the influence of `org-odd-levels-only'
7394 LEVEL is a current level, CHANGE is by how much the level should be
7395 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7396 even level numbers will become the next higher odd number."
7397 (if org-odd-levels-only
7398 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7399 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7400 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7401 (max 1 (+ level (or change 0)))))
7403 (if (boundp 'define-obsolete-function-alias)
7404 (if (or (featurep 'xemacs) (< emacs-major-version 23))
7405 (define-obsolete-function-alias 'org-get-legal-level
7406 'org-get-valid-level)
7407 (define-obsolete-function-alias 'org-get-legal-level
7408 'org-get-valid-level "23.1")))
7410 (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
7411 ;; ̀org-with-limited-levels'
7412 (defun org-promote ()
7413 "Promote the current heading higher up the tree.
7414 If the region is active in `transient-mark-mode', promote all headings
7416 (org-back-to-heading t)
7417 (let* ((level (save-match-data (funcall outline-level)))
7418 (after-change-functions (remove 'flyspell-after-change-function
7419 after-change-functions))
7420 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7421 (diff (abs (- level (length up-head) -1))))
7422 (cond ((and (= level 1) org-called-with-limited-levels
7423 org-allow-promoting-top-level-subtree)
7424 (replace-match "# " nil t))
7426 (error "Cannot promote to level 0. UNDO to recover if necessary"))
7427 (t (replace-match up-head nil t)))
7428 ;; Fixup tag positioning
7430 (and org-auto-align-tags (org-set-tags nil t))
7431 (if org-adapt-indentation (org-fixup-indentation (- diff))))
7432 (run-hooks 'org-after-promote-entry-hook)))
7434 (defun org-demote ()
7435 "Demote the current heading lower down the tree.
7436 If the region is active in `transient-mark-mode', demote all headings
7438 (org-back-to-heading t)
7439 (let* ((level (save-match-data (funcall outline-level)))
7440 (after-change-functions (remove 'flyspell-after-change-function
7441 after-change-functions))
7442 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7443 (diff (abs (- level (length down-head) -1))))
7444 (replace-match down-head nil t)
7445 ;; Fixup tag positioning
7446 (and org-auto-align-tags (org-set-tags nil t))
7447 (if org-adapt-indentation (org-fixup-indentation diff))
7448 (run-hooks 'org-after-demote-entry-hook)))
7450 (defun org-cycle-level ()
7451 "Cycle the level of an empty headline through possible states.
7452 This goes first to child, then to parent, level, then up the hierarchy.
7453 After top level, it switches back to sibling level."
7455 (let ((org-adapt-indentation nil))
7456 (when (org-point-at-end-of-empty-headline)
7457 (setq this-command 'org-cycle-level) ; Only needed for caching
7458 (let ((cur-level (org-current-level))
7459 (prev-level (org-get-previous-line-level)))
7461 ;; If first headline in file, promote to top-level.
7463 (loop repeat (/ (- cur-level 1) (org-level-increment))
7464 do (org-do-promote)))
7465 ;; If same level as prev, demote one.
7466 ((= prev-level cur-level)
7468 ;; If parent is top-level, promote to top level if not already.
7470 (loop repeat (/ (- cur-level 1) (org-level-increment))
7471 do (org-do-promote)))
7472 ;; If top-level, return to prev-level.
7474 (loop repeat (/ (- prev-level 1) (org-level-increment))
7475 do (org-do-demote)))
7476 ;; If less than prev-level, promote one.
7477 ((< cur-level prev-level)
7479 ;; If deeper than prev-level, promote until higher than
7481 ((> cur-level prev-level)
7482 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7483 do (org-do-promote))))
7486 (defun org-map-tree (fun)
7487 "Call FUN for every heading underneath the current one."
7488 (org-back-to-heading)
7489 (let ((level (funcall outline-level)))
7493 (outline-next-heading)
7494 (> (funcall outline-level) level))
7498 (defun org-map-region (fun beg end)
7499 "Call FUN for every heading between BEG and END."
7500 (let ((org-ignore-region t))
7502 (setq end (copy-marker end))
7504 (if (and (re-search-forward org-outline-regexp-bol nil t)
7508 (outline-next-heading)
7513 (defvar org-property-end-re) ; silence byte-compiler
7514 (defun org-fixup-indentation (diff)
7515 "Change the indentation in the current entry by DIFF.
7516 However, if any line in the current entry has no indentation, or if it
7517 would end up with no indentation after the change, nothing at all is done."
7519 (let ((end (save-excursion (outline-next-heading)
7521 (prohibit (if (> diff 0)
7523 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7525 (unless (save-excursion (end-of-line 1)
7526 (re-search-forward prohibit end t))
7527 (while (and (< (point) end)
7528 (re-search-forward "^[ \t]+" end t))
7529 (goto-char (match-end 0))
7530 (setq col (current-column))
7531 (if (< diff 0) (replace-match ""))
7532 (org-indent-to-column (+ diff col))))
7533 (move-marker end nil))))
7535 (defun org-convert-to-odd-levels ()
7536 "Convert an org-mode file with all levels allowed to one with odd levels.
7537 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7540 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7541 (let ((outline-level 'org-outline-level)
7542 (org-odd-levels-only nil) n)
7544 (goto-char (point-min))
7545 (while (re-search-forward "^\\*\\*+ " nil t)
7546 (setq n (- (length (match-string 0)) 2))
7547 (while (>= (setq n (1- n)) 0)
7549 (end-of-line 1))))))
7551 (defun org-convert-to-oddeven-levels ()
7552 "Convert an org-mode file with only odd levels to one with odd/even levels.
7553 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7554 file contains a section with an even level, conversion would
7555 destroy the structure of the file. An error is signaled in this
7558 (goto-char (point-min))
7559 ;; First check if there are no even levels
7560 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7561 (org-show-context t)
7562 (error "Not all levels are odd in this file. Conversion not possible"))
7563 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7564 (let ((outline-regexp org-outline-regexp)
7565 (outline-level 'org-outline-level)
7566 (org-odd-levels-only nil) n)
7568 (goto-char (point-min))
7569 (while (re-search-forward "^\\*\\*+ " nil t)
7570 (setq n (/ (1- (length (match-string 0))) 2))
7571 (while (>= (setq n (1- n)) 0)
7573 (end-of-line 1))))))
7575 (defun org-tr-level (n)
7576 "Make N odd if required."
7577 (if org-odd-levels-only (1+ (/ n 2)) n))
7579 ;;; Vertical tree motion, cutting and pasting of subtrees
7581 (defun org-move-subtree-up (&optional arg)
7582 "Move the current subtree up past ARG headlines of the same level."
7584 (org-move-subtree-down (- (prefix-numeric-value arg))))
7586 (defun org-move-subtree-down (&optional arg)
7587 "Move the current subtree down past ARG headlines of the same level."
7589 (setq arg (prefix-numeric-value arg))
7590 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7591 'org-get-last-sibling))
7592 (ins-point (make-marker))
7594 (col (current-column))
7595 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7597 (org-back-to-heading)
7600 (setq ne-beg (org-back-over-empty-lines))
7603 (save-excursion (outline-end-of-heading)
7604 (setq folded (outline-invisible-p)))
7605 (outline-end-of-subtree))
7606 (outline-next-heading)
7607 (setq ne-end (org-back-over-empty-lines))
7610 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7611 ;; include less whitespace
7614 (forward-line (- ne-beg ne-end))
7615 (setq beg (point))))
7616 ;; Find insertion point, with error handling
7618 (or (and (funcall movfunc) (looking-at org-outline-regexp))
7619 (progn (goto-char beg0)
7620 (error "Cannot move past superior level or buffer limit")))
7621 (setq cnt (1- cnt)))
7623 ;; Moving forward - still need to move over subtree
7624 (progn (org-end-of-subtree t t)
7626 (org-back-over-empty-lines)
7627 (or (bolp) (newline)))))
7628 (setq ne-ins (org-back-over-empty-lines))
7629 (move-marker ins-point (point))
7630 (setq txt (buffer-substring beg end))
7631 (org-save-markers-in-region beg end)
7632 (delete-region beg end)
7633 (org-remove-empty-overlays-at beg)
7634 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7635 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7636 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7637 (let ((bbb (point)))
7638 (insert-before-markers txt)
7639 (org-reinstall-markers-in-region bbb)
7640 (move-marker ins-point bbb))
7641 (or (bolp) (insert "\n"))
7642 (setq ins-end (point))
7643 (goto-char ins-point)
7644 (org-skip-whitespace)
7645 (when (and (< arg 0)
7646 (org-first-sibling-p)
7648 ;; Move whitespace back to beginning
7651 (let ((kill-whole-line t))
7652 (kill-line (- ne-ins ne-beg)) (point)))
7653 (insert (make-string (- ne-ins ne-beg) ?\n)))
7654 (move-marker ins-point nil)
7659 (org-cycle-hide-drawers 'children))
7660 (org-clean-visibility-after-subtree-move)
7661 ;; move back to the initial column we were at
7662 (move-to-column col)))
7664 (defvar org-subtree-clip ""
7665 "Clipboard for cut and paste of subtrees.
7666 This is actually only a copy of the kill, because we use the normal kill
7667 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7669 (defvar org-subtree-clip-folded nil
7670 "Was the last copied subtree folded?
7671 This is used to fold the tree back after pasting.")
7673 (defun org-cut-subtree (&optional n)
7674 "Cut the current subtree into the clipboard.
7675 With prefix arg N, cut this many sequential subtrees.
7676 This is a short-hand for marking the subtree and then cutting it."
7678 (org-copy-subtree n 'cut))
7680 (defun org-copy-subtree (&optional n cut force-store-markers)
7681 "Cut the current subtree into the clipboard.
7682 With prefix arg N, cut this many sequential subtrees.
7683 This is a short-hand for marking the subtree and then copying it.
7684 If CUT is non-nil, actually cut the subtree.
7685 If FORCE-STORE-MARKERS is non-nil, store the relative locations
7686 of some markers in the region, even if CUT is non-nil. This is
7687 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
7689 (let (beg end folded (beg0 (point)))
7690 (if (org-called-interactively-p 'any)
7691 (org-back-to-heading nil) ; take what looks like a subtree
7692 (org-back-to-heading t)) ; take what is really there
7694 (skip-chars-forward " \t\r\n")
7696 (save-excursion (outline-end-of-heading)
7697 (setq folded (outline-invisible-p)))
7699 (org-forward-heading-same-level (1- n) t)
7701 (org-end-of-subtree t t))
7705 (setq org-subtree-clip-folded folded)
7706 (when (or cut force-store-markers)
7707 (org-save-markers-in-region beg end))
7708 (if cut (kill-region beg end) (copy-region-as-kill beg end))
7709 (setq org-subtree-clip (current-kill 0))
7710 (message "%s: Subtree(s) with %d characters"
7711 (if cut "Cut" "Copied")
7712 (length org-subtree-clip)))))
7714 (defun org-paste-subtree (&optional level tree for-yank)
7715 "Paste the clipboard as a subtree, with modification of headline level.
7716 The entire subtree is promoted or demoted in order to match a new headline
7719 If the cursor is at the beginning of a headline, the same level as
7720 that headline is used to paste the tree
7722 If not, the new level is derived from the *visible* headings
7723 before and after the insertion point, and taken to be the inferior headline
7724 level of the two. So if the previous visible heading is level 3 and the
7725 next is level 4 (or vice versa), level 4 will be used for insertion.
7726 This makes sure that the subtree remains an independent subtree and does
7727 not swallow low level entries.
7729 You can also force a different level, either by using a numeric prefix
7730 argument, or by inserting the heading marker by hand. For example, if the
7731 cursor is after \"*****\", then the tree will be shifted to level 5.
7733 If optional TREE is given, use this text instead of the kill ring.
7735 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
7736 move back over whitespace before inserting, and move point to the end of
7737 the inserted text when done."
7739 (setq tree (or tree (and kill-ring (current-kill 0))))
7740 (unless (org-kill-is-subtree-p tree)
7742 (substitute-command-keys
7743 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
7744 (org-with-limited-levels
7745 (let* ((visp (not (outline-invisible-p)))
7747 (^re_ "\\(\\*+\\)[ \t]*")
7748 (old-level (if (string-match org-outline-regexp-bol txt)
7749 (- (match-end 0) (match-beginning 0) 1)
7751 (force-level (cond (level (prefix-numeric-value level))
7752 ((and (looking-at "[ \t]*$")
7754 "^\\*+$" (buffer-substring
7755 (point-at-bol) (point))))
7756 (- (match-end 1) (match-beginning 1)))
7758 (looking-at org-outline-regexp))
7759 (- (match-end 0) (point) 1))))
7760 (previous-level (save-excursion
7763 (outline-previous-visible-heading 1)
7764 (if (looking-at ^re_)
7765 (- (match-end 0) (match-beginning 0) 1)
7768 (next-level (save-excursion
7771 (or (looking-at org-outline-regexp)
7772 (outline-next-visible-heading 1))
7773 (if (looking-at ^re_)
7774 (- (match-end 0) (match-beginning 0) 1)
7777 (new-level (or force-level (max previous-level next-level)))
7778 (shift (if (or (= old-level -1)
7780 (= old-level new-level))
7782 (- new-level old-level)))
7783 (delta (if (> shift 0) -1 1))
7784 (func (if (> shift 0) 'org-demote 'org-promote))
7785 (org-odd-levels-only nil)
7787 ;; Remove the forced level indicator
7789 (delete-region (point-at-bol) (point)))
7791 (beginning-of-line (if (bolp) 1 2))
7793 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
7794 (insert-before-markers txt)
7795 (unless (string-match "\n\\'" txt) (insert "\n"))
7796 (setq newend (point))
7797 (org-reinstall-markers-in-region beg)
7800 (skip-chars-forward " \t\n\r")
7802 (if (and (outline-invisible-p) visp)
7803 (save-excursion (outline-show-heading)))
7804 ;; Shift if necessary
7807 (narrow-to-region beg end)
7808 (while (not (= shift 0))
7809 (org-map-region func (point-min) (point-max))
7810 (setq shift (+ delta shift)))
7811 (goto-char (point-min))
7812 (setq newend (point-max))))
7813 (when (or (org-called-interactively-p 'interactive) for-yank)
7814 (message "Clipboard pasted as level %d subtree" new-level))
7815 (if (and (not for-yank) ; in this case, org-yank will decide about folding
7817 (eq org-subtree-clip (current-kill 0))
7818 org-subtree-clip-folded)
7819 ;; The tree was folded before it was killed/copied
7821 (and for-yank (goto-char newend)))))
7823 (defun org-kill-is-subtree-p (&optional txt)
7824 "Check if the current kill is an outline subtree, or a set of trees.
7825 Returns nil if kill does not start with a headline, or if the first
7826 headline level is not the largest headline level in the tree.
7827 So this will actually accept several entries of equal levels as well,
7828 which is OK for `org-paste-subtree'.
7829 If optional TXT is given, check this string instead of the current kill."
7830 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
7831 (re (org-get-limited-outline-regexp))
7832 (^re (concat "^" re))
7833 (start-level (and kill
7835 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
7837 (- (match-end 2) (match-beginning 2) 1)))
7838 (start (1+ (or (match-beginning 2) -1))))
7839 (if (not start-level)
7841 nil) ;; does not even start with a heading
7843 (while (setq start (string-match ^re kill (1+ start)))
7844 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
7848 (defvar org-markers-to-move nil
7849 "Markers that should be moved with a cut-and-paste operation.
7850 Those markers are stored together with their positions relative to
7851 the start of the region.")
7853 (defun org-save-markers-in-region (beg end)
7854 "Check markers in region.
7855 If these markers are between BEG and END, record their position relative
7856 to BEG, so that after moving the block of text, we can put the markers back
7858 This function gets called just before an entry or tree gets cut from the
7859 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
7860 called immediately, to move the markers with the entries."
7861 (setq org-markers-to-move nil)
7862 (when (featurep 'org-clock)
7863 (org-clock-save-markers-for-cut-and-paste beg end))
7864 (when (featurep 'org-agenda)
7865 (org-agenda-save-markers-for-cut-and-paste beg end)))
7867 (defun org-check-and-save-marker (marker beg end)
7868 "Check if MARKER is between BEG and END.
7869 If yes, remember the marker and the distance to BEG."
7870 (when (and (marker-buffer marker)
7871 (equal (marker-buffer marker) (current-buffer)))
7872 (if (and (>= marker beg) (< marker end))
7873 (push (cons marker (- marker beg)) org-markers-to-move))))
7875 (defun org-reinstall-markers-in-region (beg)
7876 "Move all remembered markers to their position relative to BEG."
7878 (move-marker (car x) (+ beg (cdr x))))
7879 org-markers-to-move)
7880 (setq org-markers-to-move nil))
7882 (defun org-narrow-to-subtree ()
7883 "Narrow buffer to the current subtree."
7887 (org-with-limited-levels
7889 (progn (org-back-to-heading t) (point))
7890 (progn (org-end-of-subtree t t)
7891 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
7894 (defun org-narrow-to-block ()
7895 "Narrow buffer to the current block."
7897 (let* ((case-fold-search t)
7898 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
7899 "^[ \t]*#\\+end_.*")))
7901 (narrow-to-region (car blockp) (cdr blockp))
7902 (error "Not in a block"))))
7905 (defvar org-property-drawer-re))
7907 (defvar org-property-start-re) ;; defined below
7908 (defun org-clone-subtree-with-time-shift (n &optional shift)
7909 "Clone the task (subtree) at point N times.
7910 The clones will be inserted as siblings.
7912 In interactive use, the user will be prompted for the number of
7913 clones to be produced, and for a time SHIFT, which may be a
7914 repeater as used in time stamps, for example `+3d'.
7916 When a valid repeater is given and the entry contains any time
7917 stamps, the clones will become a sequence in time, with time
7918 stamps in the subtree shifted for each clone produced. If SHIFT
7919 is nil or the empty string, time stamps will be left alone. The
7920 ID property of the original subtree is removed.
7922 If the original subtree did contain time stamps with a repeater,
7923 the following will happen:
7924 - the repeater will be removed in each clone
7925 - an additional clone will be produced, with the current, unshifted
7926 date(s) in the entry.
7927 - the original entry will be placed *after* all the clones, with
7929 - the start days in the repeater in the original entry will be shifted
7930 to past the last clone.
7931 In this way you can spell out a number of instances of a repeating task,
7932 and still retain the repeater to cover future instances of the task."
7933 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
7934 (let (beg end template task idprop
7935 shift-n shift-what doshift nmin nmax (n-no-remove -1)
7936 (drawer-re org-drawer-regexp))
7937 (if (not (and (integerp n) (> n 0)))
7938 (error "Invalid number of replications %s" n))
7939 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
7940 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
7942 (error "Invalid shift specification %s" shift))
7944 (setq shift-n (string-to-number (match-string 1 shift))
7945 shift-what (cdr (assoc (match-string 2 shift)
7946 '(("d" . day) ("w" . week)
7947 ("m" . month) ("y" . year))))))
7948 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
7949 (setq nmin 1 nmax n)
7950 (org-back-to-heading t)
7952 (setq idprop (org-entry-get nil "ID"))
7953 (org-end-of-subtree t t)
7954 (or (bolp) (insert "\n"))
7956 (setq template (buffer-substring beg end))
7958 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
7959 (delete-region beg end)
7961 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
7963 (loop for n from nmin to nmax do
7968 (goto-char (point-min))
7970 (and idprop (if org-clone-delete-id
7971 (org-entry-delete nil "ID")
7972 (org-id-get-create t)))
7974 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
7976 (goto-char (point-min))
7977 (while (re-search-forward drawer-re nil t)
7979 (org-remove-empty-drawer-at d (point))) org-drawers)))
7980 (goto-char (point-min))
7982 (while (re-search-forward org-ts-regexp-both nil t)
7983 (org-timestamp-change (* n shift-n) shift-what))
7984 (unless (= n n-no-remove)
7985 (goto-char (point-min))
7986 (while (re-search-forward org-ts-regexp nil t)
7988 (goto-char (match-beginning 0))
7989 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
7990 (delete-region (match-beginning 1) (match-end 1)))))))
7991 (setq task (buffer-string)))
7997 (defun org-sort (with-case)
7998 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
7999 Optional argument WITH-CASE means sort case-sensitively."
8002 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
8003 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
8005 (org-call-with-arg 'org-sort-entries with-case))))
8007 (defun org-sort-remove-invisible (s)
8008 (remove-text-properties 0 (length s) org-rm-props s)
8009 (while (string-match org-bracket-link-regexp s)
8010 (setq s (replace-match (if (match-end 2)
8012 (match-string 1 s)) t t s)))
8015 (defvar org-priority-regexp) ; defined later in the file
8017 (defvar org-after-sorting-entries-or-items-hook nil
8018 "Hook that is run after a bunch of entries or items have been sorted.
8019 When children are sorted, the cursor is in the parent line when this
8020 hook gets called. When a region or a plain list is sorted, the cursor
8021 will be in the first entry of the sorted region/list.")
8023 (defun org-sort-entries
8024 (&optional with-case sorting-type getkey-func compare-func property)
8025 "Sort entries on a certain level of an outline tree.
8026 If there is an active region, the entries in the region are sorted.
8027 Else, if the cursor is before the first entry, sort the top-level items.
8028 Else, the children of the entry at point are sorted.
8030 Sorting can be alphabetically, numerically, by date/time as given by
8031 a time stamp, by a property or by priority.
8033 The command prompts for the sorting type unless it has been given to the
8034 function through the SORTING-TYPE argument, which needs to be a character,
8035 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
8036 precise meaning of each character:
8038 n Numerically, by converting the beginning of the entry/item to a number.
8039 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8040 o By order of TODO keywords.
8041 t By date/time, either the first active time stamp in the entry, or, if
8042 none exist, by the first inactive one.
8043 s By the scheduled date/time.
8044 d By deadline date/time.
8045 c By creation time, which is assumed to be the first inactive time stamp
8046 at the beginning of a line.
8047 p By priority according to the cookie.
8048 r By the value of a property.
8050 Capital letters will reverse the sort order.
8052 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8053 called with point at the beginning of the record. It must return either
8054 a string or a number that should serve as the sorting key for that record.
8056 Comparing entries ignores case by default. However, with an optional argument
8057 WITH-CASE, the sorting considers case as well."
8059 (let ((case-func (if with-case 'identity 'downcase))
8061 ;; The clock marker is lost when using `sort-subr', let's
8062 ;; store the clocking string.
8063 (when (equal (marker-buffer org-clock-marker) (current-buffer))
8065 (goto-char org-clock-marker)
8066 (looking-back "^.*") (match-string-no-properties 0))))
8067 start beg end stars re re2
8069 ;; Find beginning and end of region to sort
8071 ((org-region-active-p)
8072 ;; we will sort the region
8073 (setq end (region-end)
8075 (goto-char (region-beginning))
8076 (if (not (org-at-heading-p)) (outline-next-heading))
8077 (setq start (point)))
8078 ((or (org-at-heading-p)
8079 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8080 ;; we will sort the children of the current headline
8081 (org-back-to-heading)
8083 end (progn (org-end-of-subtree t t)
8084 (or (bolp) (insert "\n"))
8085 (org-back-over-empty-lines)
8090 (outline-next-heading))
8092 ;; we will sort the top-level entries in this file
8093 (goto-char (point-min))
8094 (or (org-at-heading-p) (outline-next-heading))
8095 (setq start (point))
8096 (goto-char (point-max))
8097 (beginning-of-line 1)
8098 (when (looking-at ".*?\\S-")
8099 ;; File ends in a non-white line
8102 (setq end (point-max))
8103 (setq what "top-level")
8108 (if (>= beg end) (error "Nothing to sort"))
8110 (looking-at "\\(\\*+\\)")
8111 (setq stars (match-string 1)
8112 re (concat "^" (regexp-quote stars) " +")
8113 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8114 txt (buffer-substring beg end))
8115 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8116 (if (and (not (equal stars "*")) (string-match re2 txt))
8117 (error "Region to sort contains a level above the first entry"))
8119 (unless sorting-type
8121 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8122 [t]ime
[s]cheduled [d]eadline [c]reated
8123 A/N/P/R/O/F/T/S/D/C means reversed:"
8125 (setq sorting-type (read-char-exclusive))
8127 (and (= (downcase sorting-type) ?f)
8129 (org-icompleting-read "Sort using function: "
8130 obarray 'fboundp t nil nil))
8131 (setq getkey-func (intern getkey-func)))
8133 (and (= (downcase sorting-type) ?r)
8135 (org-icompleting-read "Property: "
8136 (mapcar 'list (org-buffer-property-keys t))
8139 (message "Sorting entries...")
8142 (narrow-to-region start end)
8143 (let ((dcst (downcase sorting-type))
8144 (case-fold-search nil)
8145 (now (current-time)))
8147 (/= dcst sorting-type)
8148 ;; This function moves to the beginning character of the "record" to
8151 (if (re-search-forward re nil t)
8152 (goto-char (match-beginning 0))
8153 (goto-char (point-max))))
8154 ;; This function moves to the last character of the "record" being
8159 (outline-forward-same-level 1)
8161 (goto-char (point-max))))))
8162 ;; This function returns the value that gets sorted against.
8166 (if (looking-at org-complex-heading-regexp)
8167 (string-to-number (match-string 4))
8170 (if (looking-at org-complex-heading-regexp)
8171 (funcall case-func (match-string 4))
8174 (let ((end (save-excursion (outline-next-heading) (point))))
8175 (if (or (re-search-forward org-ts-regexp end t)
8176 (re-search-forward org-ts-regexp-both end t))
8177 (org-time-string-to-seconds (match-string 0))
8178 (org-float-time now))))
8180 (let ((end (save-excursion (outline-next-heading) (point))))
8181 (if (re-search-forward
8182 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8184 (org-time-string-to-seconds (match-string 0))
8185 (org-float-time now))))
8187 (let ((end (save-excursion (outline-next-heading) (point))))
8188 (if (re-search-forward org-scheduled-time-regexp end t)
8189 (org-time-string-to-seconds (match-string 1))
8190 (org-float-time now))))
8192 (let ((end (save-excursion (outline-next-heading) (point))))
8193 (if (re-search-forward org-deadline-time-regexp end t)
8194 (org-time-string-to-seconds (match-string 1))
8195 (org-float-time now))))
8197 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8198 (string-to-char (match-string 2))
8199 org-default-priority))
8201 (or (org-entry-get nil property) ""))
8203 (if (looking-at org-complex-heading-regexp)
8204 (- 9999 (length (member (match-string 2)
8205 org-todo-keywords-1)))))
8209 (setq tmp (funcall getkey-func))
8210 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8212 (error "Invalid key function `%s'" getkey-func)))
8213 (t (error "Invalid sorting type `%c'" sorting-type))))
8216 ((= dcst ?a) 'string<)
8217 ((= dcst ?f) compare-func)
8218 ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
8219 (run-hooks 'org-after-sorting-entries-or-items-hook)
8220 ;; Reset the clock marker if needed
8224 (search-forward cmstr nil t)
8225 (move-marker org-clock-marker (point))))
8226 (message "Sorting entries...done")))
8228 (defun org-do-sort (table what &optional with-case sorting-type)
8229 "Sort TABLE of WHAT according to SORTING-TYPE.
8230 The user will be prompted for the SORTING-TYPE if the call to this
8231 function does not specify it. WHAT is only for the prompt, to indicate
8232 what is being sorted. The sorting key will be extracted from
8233 the car of the elements of the table.
8234 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8235 (unless sorting-type
8237 "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
8239 (setq sorting-type (read-char-exclusive)))
8240 (let ((dcst (downcase sorting-type))
8241 extractfun comparefun)
8242 ;; Define the appropriate functions
8245 (setq extractfun 'string-to-number
8246 comparefun (if (= dcst sorting-type) '< '>)))
8248 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8249 (lambda(x) (downcase (org-sort-remove-invisible x))))
8250 comparefun (if (= dcst sorting-type)
8252 (lambda (a b) (and (not (string< a b))
8253 (not (string= a b)))))))
8257 (if (or (string-match org-ts-regexp x)
8258 (string-match org-ts-regexp-both x))
8260 (org-time-string-to-time (match-string 0 x)))
8262 comparefun (if (= dcst sorting-type) '< '>)))
8263 (t (error "Invalid sorting type `%c'" sorting-type)))
8265 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8267 (lambda (a b) (funcall comparefun (car a) (car b))))))
8270 ;;; The orgstruct minor mode
8272 ;; Define a minor mode which can be used in other modes in order to
8273 ;; integrate the org-mode structure editing commands.
8275 ;; This is really a hack, because the org-mode structure commands use
8276 ;; keys which normally belong to the major mode. Here is how it
8277 ;; works: The minor mode defines all the keys necessary to operate the
8278 ;; structure commands, but wraps the commands into a function which
8279 ;; tests if the cursor is currently at a headline or a plain list
8280 ;; item. If that is the case, the structure command is used,
8281 ;; temporarily setting many Org-mode variables like regular
8282 ;; expressions for filling etc. However, when any of those keys is
8283 ;; used at a different location, function uses `key-binding' to look
8284 ;; up if the key has an associated command in another currently active
8285 ;; keymap (minor modes, major mode, global), and executes that
8286 ;; command. There might be problems if any of the keys is otherwise
8287 ;; used as a prefix key.
8289 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
8290 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
8291 ;; addresses this by checking explicitly for both bindings.
8293 (defvar orgstruct-mode-map (make-sparse-keymap)
8294 "Keymap for the minor `orgstruct-mode'.")
8296 (defvar org-local-vars nil
8297 "List of local variables, for use by `orgstruct-mode'.")
8300 (define-minor-mode orgstruct-mode
8301 "Toggle the minor mode `orgstruct-mode'.
8302 This mode is for using Org-mode structure commands in other
8303 modes. The following keys behave as if Org-mode were active, if
8304 the cursor is on a headline, or on a plain list item (both as
8305 defined by Org-mode).
8307 M-up Move entry/item up
8308 M-down Move entry/item down
8311 M-S-up Move entry/item up
8312 M-S-down Move entry/item down
8313 M-S-left Promote subtree
8314 M-S-right Demote subtree
8315 M-q Fill paragraph and items like in Org-mode
8317 C-c - Cycle list bullet
8318 TAB Cycle item visibility
8319 M-RET Insert new heading/item
8320 S-M-RET Insert new TODO heading / Checkbox item
8321 C-c C-c Set tags / toggle checkbox"
8322 nil " OrgStruct" nil
8323 (org-load-modules-maybe)
8324 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
8327 (defun turn-on-orgstruct ()
8328 "Unconditionally turn on `orgstruct-mode'."
8331 (defvar org-fb-vars nil)
8332 (make-variable-buffer-local 'org-fb-vars)
8333 (defun orgstruct++-mode (&optional arg)
8334 "Toggle `orgstruct-mode', the enhanced version of it.
8335 In addition to setting orgstruct-mode, this also exports all
8336 indentation and autofilling variables from org-mode into the
8337 buffer. It will also recognize item context in multiline items."
8339 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8341 (progn (orgstruct-mode -1)
8343 (org-set-local (car v)
8344 (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
8347 (setq org-fb-vars nil)
8352 "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
8353 (symbol-name (car x)))
8354 (setq var (car x) val (nth 1 x))
8355 (push (list var `(quote ,(eval var))) org-fb-vars)
8356 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8358 (org-set-local 'orgstruct-is-++ t))))
8360 (defvar orgstruct-is-++ nil
8361 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8362 (make-variable-buffer-local 'orgstruct-is-++)
8365 (defun turn-on-orgstruct++ ()
8366 "Unconditionally turn on `orgstruct++-mode'."
8367 (orgstruct++-mode 1))
8369 (defun orgstruct-error ()
8370 "Error when there is no default binding for a structure key."
8372 (error "This key has no function outside structure elements"))
8374 (defun orgstruct-setup ()
8375 "Setup orgstruct keymaps."
8379 '([(meta up)] org-metaup)
8380 '([(meta down)] org-metadown)
8381 '([(meta left)] org-metaleft)
8382 '([(meta right)] org-metaright)
8383 '([(meta shift up)] org-shiftmetaup)
8384 '([(meta shift down)] org-shiftmetadown)
8385 '([(meta shift left)] org-shiftmetaleft)
8386 '([(meta shift right)] org-shiftmetaright)
8387 '([?\e (up)] org-metaup)
8388 '([?\e (down)] org-metadown)
8389 '([?\e (left)] org-metaleft)
8390 '([?\e (right)] org-metaright)
8391 '([?\e (shift up)] org-shiftmetaup)
8392 '([?\e (shift down)] org-shiftmetadown)
8393 '([?\e (shift left)] org-shiftmetaleft)
8394 '([?\e (shift right)] org-shiftmetaright)
8395 '([(shift up)] org-shiftup)
8396 '([(shift down)] org-shiftdown)
8397 '([(shift left)] org-shiftleft)
8398 '([(shift right)] org-shiftright)
8399 '("\C-c\C-c" org-ctrl-c-ctrl-c)
8400 '("\M-q" fill-paragraph)
8402 '("\C-c-" org-cycle-list-bullet)))
8404 (while (setq elt (pop bindings))
8405 (setq nfunc (1+ nfunc))
8406 (setq key (org-key (car elt))
8408 cmd (orgstruct-make-binding fun nfunc key))
8409 (org-defkey orgstruct-mode-map key cmd))
8411 ;; Prevent an error for users who forgot to make autoloads
8412 (require 'org-element)
8414 ;; Special treatment needed for TAB and RET
8415 (org-defkey orgstruct-mode-map [(tab)]
8416 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
8417 (org-defkey orgstruct-mode-map "\C-i"
8418 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
8420 (org-defkey orgstruct-mode-map "\M-\C-m"
8421 (orgstruct-make-binding 'org-insert-heading 105
8422 "\M-\C-m" [(meta return)]))
8423 (org-defkey orgstruct-mode-map [(meta return)]
8424 (orgstruct-make-binding 'org-insert-heading 106
8425 [(meta return)] "\M-\C-m"))
8427 (org-defkey orgstruct-mode-map [(shift meta return)]
8428 (orgstruct-make-binding 'org-insert-todo-heading 107
8429 [(meta return)] "\M-\C-m"))
8431 (org-defkey orgstruct-mode-map "\e\C-m"
8432 (orgstruct-make-binding 'org-insert-heading 108
8433 "\e\C-m" [?\e (return)]))
8434 (org-defkey orgstruct-mode-map [?\e (return)]
8435 (orgstruct-make-binding 'org-insert-heading 109
8436 [?\e (return)] "\e\C-m"))
8437 (org-defkey orgstruct-mode-map [?\e (shift return)]
8438 (orgstruct-make-binding 'org-insert-todo-heading 110
8439 [?\e (return)] "\e\C-m"))
8441 (unless org-local-vars
8442 (setq org-local-vars (org-get-local-variables)))
8446 (defun orgstruct-make-binding (fun n &rest keys)
8447 "Create a function for binding in the structure minor mode.
8448 FUN is the command to call inside a table. N is used to create a unique
8449 command name. KEYS are keys that should be checked in for a command
8450 to execute outside of tables."
8453 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
8455 (concat "In Structure, run `" (symbol-name fun) "'.\n"
8456 "Outside of structure, run the binding of `"
8457 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
8461 `(org-context-p 'headline 'item
8462 (and orgstruct-is-++
8463 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
8465 (list 'org-run-like-in-org-mode (list 'quote fun))
8466 (list 'let '(orgstruct-mode)
8467 (list 'call-interactively
8470 (list 'key-binding k))
8472 '('orgstruct-error))))))))
8474 (defun org-contextualize-keys (alist contexts)
8475 "Return valid elements in ALIST depending on CONTEXTS.
8477 `org-agenda-custom-commands' or `org-capture-templates' are the
8478 values used for ALIST, and `org-agenda-custom-commands-contexts'
8479 or `org-capture-templates-contexts' are the associated contexts
8482 ;; normalize contexts
8484 (lambda(c) (cond ((listp (cadr c))
8485 (list (car c) (car c) (cadr c)))
8486 ((string= "" (cadr c))
8487 (list (car c) (car c) (caddr c)))
8490 ;; loop over all commands or templates
8491 (while (setq c (pop a))
8494 ((not (assoc (car c) contexts))
8496 ((and (assoc (car c) contexts)
8497 (setq vrules (org-contextualize-validate-key
8500 (when (not (equal (car vr) (cadr vr)))
8501 (setq repl vr))) vrules)
8502 (if (not repl) (push c r)
8503 (push (cadr repl) s)
8506 (cdr (or (assoc (cadr repl) alist)
8507 (error "Undefined key `%s' as contextual replacement for `%s'"
8508 (cadr repl) (car c)))))
8510 ;; Return limited ALIST, possibly with keys modified, and deduplicated
8515 (let ((tpl (car x)))
8519 (equal y tpl)) s))) x)))
8522 (defun org-contextualize-validate-key (key contexts)
8523 "Check CONTEXTS for agenda or capture KEY."
8525 (while (setq r (pop contexts))
8529 (and (equal key (car r))
8530 (if (functionp rr) (funcall rr)
8531 (or (and (eq (car rr) 'in-file)
8533 (string-match (cdr rr) (buffer-file-name)))
8534 (and (eq (car rr) 'in-mode)
8535 (string-match (cdr rr) (symbol-name major-mode)))
8536 (when (and (eq (car rr) 'not-in-file)
8538 (not (string-match (cdr rr) (buffer-file-name))))
8539 (when (eq (car rr) 'not-in-mode)
8540 (not (string-match (cdr rr) (symbol-name major-mode)))))))
8543 (delete-dups (delq nil res))))
8545 (defun org-context-p (&rest contexts)
8546 "Check if local context is any of CONTEXTS.
8547 Possible values in the list of contexts are `table', `headline', and `item'."
8548 (let ((pos (point)))
8549 (goto-char (point-at-bol))
8550 (prog1 (or (and (memq 'table contexts)
8551 (looking-at "[ \t]*|"))
8552 (and (memq 'headline contexts)
8553 (looking-at org-outline-regexp))
8554 (and (memq 'item contexts)
8555 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
8556 (and (memq 'item-body contexts)
8560 (defun org-get-local-variables ()
8561 "Return a list of all local variables in an Org mode buffer."
8563 (with-current-buffer (get-buffer-create "*Org tmp*")
8566 (setq varlist (buffer-local-variables)))
8567 (kill-buffer "*Org tmp*")
8574 (list (car x) (list 'quote (cdr x)))))
8576 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
8577 (symbol-name (car x)))
8581 (defun org-clone-local-variables (from-buffer &optional regexp)
8582 "Clone local variables from FROM-BUFFER.
8583 Optional argument REGEXP selects variables to clone."
8586 (and (symbolp (car pair))
8588 (string-match regexp (symbol-name (car pair))))
8589 (set (make-local-variable (car pair))
8591 (buffer-local-variables from-buffer)))
8594 (defun org-run-like-in-org-mode (cmd)
8595 "Run a command, pretending that the current buffer is in Org-mode.
8596 This will temporarily bind local variables that are typically bound in
8597 Org-mode to the values they have in Org-mode, and then interactively
8599 (org-load-modules-maybe)
8600 (unless org-local-vars
8601 (setq org-local-vars (org-get-local-variables)))
8602 (eval (list 'let org-local-vars
8603 (list 'call-interactively (list 'quote cmd)))))
8607 (defun org-get-category (&optional pos force-refresh)
8608 "Get the category applying to position POS."
8610 (if force-refresh (org-refresh-category-properties))
8611 (let ((pos (or pos (point))))
8612 (or (get-text-property pos 'org-category)
8613 (progn (org-refresh-category-properties)
8614 (get-text-property pos 'org-category))))))
8616 (defun org-refresh-category-properties ()
8617 "Refresh category text properties in the buffer."
8618 (let ((case-fold-search t)
8619 (inhibit-read-only t)
8621 ((null org-category)
8622 (if buffer-file-name
8623 (file-name-sans-extension
8624 (file-name-nondirectory buffer-file-name))
8626 ((symbolp org-category) (symbol-name org-category))
8628 beg end cat pos optionp)
8633 (goto-char (point-min))
8634 (put-text-property (point) (point-max) 'org-category def-cat)
8635 (while (re-search-forward
8636 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8637 (setq pos (match-end 0)
8638 optionp (equal (char-after (match-beginning 0)) ?#)
8639 cat (org-trim (match-string 2)))
8641 (setq beg (point-at-bol) end (point-max))
8642 (org-back-to-heading t)
8643 (setq beg (point) end (org-end-of-subtree t t)))
8644 (put-text-property beg end 'org-category cat)
8645 (put-text-property beg end 'org-category-position beg)
8646 (goto-char pos)))))))
8648 (defun org-refresh-properties (dprop tprop)
8649 "Refresh buffer text properties.
8650 DPROP is the drawer property and TPROP is the corresponding text
8652 (let ((case-fold-search t)
8653 (inhibit-read-only t) p)
8658 (goto-char (point-min))
8659 (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
8660 (setq p (org-match-string-no-properties 1))
8662 (org-back-to-heading t)
8664 (point-at-bol) (point-at-eol) tprop p))))))))
8669 ;;; Link abbreviations
8671 (defun org-link-expand-abbrev (link)
8672 "Apply replacements as defined in `org-link-abbrev-alist'."
8673 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
8674 (let* ((key (match-string 1 link))
8675 (as (or (assoc key org-link-abbrev-alist-local)
8676 (assoc key org-link-abbrev-alist)))
8677 (tag (and (match-end 2) (match-string 3 link)))
8683 ((symbolp rpl) (funcall rpl tag))
8684 ((string-match "%(\\([^)]+\\))" rpl)
8685 (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
8686 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
8687 ((string-match "%h" rpl)
8688 (replace-match (url-hexify-string (or tag "")) t t rpl))
8689 (t (concat rpl tag)))))
8692 ;;; Storing and inserting links
8694 (defvar org-insert-link-history nil
8695 "Minibuffer history for links inserted with `org-insert-link'.")
8697 (defvar org-stored-links nil
8698 "Contains the links stored with `org-store-link'.")
8700 (defvar org-store-link-plist nil
8701 "Plist with info about the most recently link created with `org-store-link'.")
8703 (defvar org-link-protocols nil
8704 "Link protocols added to Org-mode using `org-add-link-type'.")
8706 (defvar org-store-link-functions nil
8707 "List of functions that are called to create and store a link.
8708 Each function will be called in turn until one returns a non-nil
8709 value. Each function should check if it is responsible for creating
8710 this link (for example by looking at the major mode).
8711 If not, it must exit and return nil.
8712 If yes, it should return a non-nil value after a calling
8713 `org-store-link-props' with a list of properties and values.
8714 Special properties are:
8716 :type The link prefix, like \"http\". This must be given.
8717 :link The link, like \"http://www.astro.uva.nl/~dominik\".
8718 This is obligatory as well.
8719 :description Optional default description for the second pair
8720 of brackets in an Org-mode link. The user can still change
8721 this when inserting this link into an Org-mode buffer.
8723 In addition to these, any additional properties can be specified
8724 and then used in capture templates.")
8726 (defun org-add-link-type (type &optional follow export)
8727 "Add TYPE to the list of `org-link-types'.
8728 Re-compute all regular expressions depending on `org-link-types'
8730 FOLLOW and EXPORT are two functions.
8732 FOLLOW should take the link path as the single argument and do whatever
8733 is necessary to follow the link, for example find a file or display
8736 EXPORT should format the link path for export to one of the export formats.
8737 It should be a function accepting three arguments:
8739 path the path of the link, the text after the prefix (like \"http:\")
8740 desc the description of the link, if any, or a description added by
8741 org-export-normalize-links if there is none
8742 format the export format, a symbol like `html' or `latex' or `ascii'..
8744 The function may use the FORMAT information to return different values
8745 depending on the format. The return value will be put literally into
8746 the exported file. If the return value is nil, this means Org should
8747 do what it normally does with links which do not have EXPORT defined.
8749 Org-mode has a built-in default for exporting links. If you are happy with
8750 this default, there is no need to define an export function for the link
8751 type. For a simple example of an export function, see `org-bbdb.el'."
8752 (add-to-list 'org-link-types type t)
8753 (org-make-link-regexps)
8754 (if (assoc type org-link-protocols)
8755 (setcdr (assoc type org-link-protocols) (list follow export))
8756 (push (list type follow export) org-link-protocols)))
8758 (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
8759 (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
8762 (defun org-store-link (arg)
8763 "\\<org-mode-map>Store an org-link to the current location.
8764 This link is added to `org-stored-links' and can later be inserted
8765 into an org-buffer with \\[org-insert-link].
8767 For some link types, a prefix arg is interpreted:
8768 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
8769 For file links, arg negates `org-context-in-file-links'."
8771 (org-load-modules-maybe)
8772 (setq org-store-link-plist nil) ; reset
8773 (org-with-limited-levels
8774 (let (link cpltxt desc description search txt custom-id agenda-link)
8777 ((run-hook-with-args-until-success 'org-store-link-functions)
8778 (setq link (plist-get org-store-link-plist :link)
8779 desc (or (plist-get org-store-link-plist :description) link)))
8781 ((org-src-edit-buffer-p)
8783 (while (or (not label)
8787 (goto-char (point-min))
8789 (regexp-quote (format org-coderef-label-format label))
8791 (when label (message "Label exists already") (sit-for 2))
8792 (setq label (read-string "Code line label: " label)))
8794 (setq link (format org-coderef-label-format label))
8795 (setq gc (- 79 (length link)))
8796 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
8798 (setq link (concat "(" label ")") desc nil)))
8800 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
8801 ;; We are in the agenda, link to referenced location
8802 (let ((m (or (get-text-property (point) 'org-hd-marker)
8803 (get-text-property (point) 'org-marker))))
8805 (org-with-point-at m
8807 (if (org-called-interactively-p 'any)
8808 (call-interactively 'org-store-link)
8809 (org-store-link nil)))))))
8811 ((eq major-mode 'calendar-mode)
8812 (let ((cd (calendar-cursor-to-date)))
8815 (car org-time-stamp-formats)
8817 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8819 (org-store-link-props :type "calendar" :date cd)))
8821 ((eq major-mode 'help-mode)
8822 (setq link (concat "help:" (save-excursion
8823 (goto-char (point-min))
8824 (looking-at "^[^ ]+")
8826 (org-store-link-props :type "help"))
8828 ((eq major-mode 'w3-mode)
8829 (setq cpltxt (if (and (buffer-name)
8830 (not (string-match "Untitled" (buffer-name))))
8833 link (url-view-url t))
8834 (org-store-link-props :type "w3" :url (url-view-url t)))
8836 ((eq major-mode 'w3m-mode)
8837 (setq cpltxt (or w3m-current-title w3m-current-url)
8838 link w3m-current-url)
8839 (org-store-link-props :type "w3m" :url (url-view-url t)))
8841 ((setq search (run-hook-with-args-until-success
8842 'org-create-file-search-functions))
8843 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8845 (setq cpltxt (or description link)))
8847 ((eq major-mode 'image-mode)
8848 (setq cpltxt (concat "file:"
8849 (abbreviate-file-name buffer-file-name))
8851 (org-store-link-props :type "image" :file buffer-file-name))
8853 ((eq major-mode 'dired-mode)
8854 ;; link to the file in the current line
8855 (let ((file (dired-get-filename nil t)))
8857 (abbreviate-file-name
8858 (expand-file-name (dired-get-filename nil t)))
8859 ;; otherwise, no file so use current directory.
8861 (setq cpltxt (concat "file:" file)
8864 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
8865 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
8867 ((org-in-regexp "<<\\(.*?\\)>>")
8870 (abbreviate-file-name
8871 (buffer-file-name (buffer-base-buffer)))
8872 "::" (match-string 1))
8874 ((and (featurep 'org-id)
8875 (or (eq org-id-link-to-org-use-id t)
8876 (and (org-called-interactively-p 'any)
8877 (or (eq org-id-link-to-org-use-id 'create-if-interactive)
8878 (and (eq org-id-link-to-org-use-id
8879 'create-if-interactive-and-no-custom-id)
8881 (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
8882 ;; We can make a link using the ID.
8883 (setq link (condition-case nil
8884 (prog1 (org-id-store-link)
8885 (setq desc (plist-get org-store-link-plist :description)))
8887 ;; probably before first headline, link to file only
8889 (abbreviate-file-name
8890 (buffer-file-name (buffer-base-buffer))))))))
8892 ;; Just link to current headline
8893 (setq cpltxt (concat "file:"
8894 (abbreviate-file-name
8895 (buffer-file-name (buffer-base-buffer)))))
8896 ;; Add a context search string
8897 (when (org-xor org-context-in-file-links arg)
8899 ((org-at-heading-p) nil)
8900 ((org-region-active-p)
8901 (buffer-substring (region-beginning) (region-end)))))
8902 (when (or (null txt) (string-match "\\S-" txt))
8906 (org-make-org-heading-search-string txt)
8908 desc (or (nth 4 (ignore-errors
8909 (org-heading-components))) "NONE"))))
8910 (if (string-match "::\\'" cpltxt)
8911 (setq cpltxt (substring cpltxt 0 -2)))
8912 (setq link cpltxt))))
8914 ((buffer-file-name (buffer-base-buffer))
8915 ;; Just link to this file here.
8916 (setq cpltxt (concat "file:"
8917 (abbreviate-file-name
8918 (buffer-file-name (buffer-base-buffer)))))
8919 ;; Add a context string
8920 (when (org-xor org-context-in-file-links arg)
8921 (setq txt (if (org-region-active-p)
8922 (buffer-substring (region-beginning) (region-end))
8923 (buffer-substring (point-at-bol) (point-at-eol))))
8924 ;; Only use search option if there is some text.
8925 (when (string-match "\\S-" txt)
8927 (concat cpltxt "::" (org-make-org-heading-search-string txt))
8931 ((org-called-interactively-p 'interactive)
8932 (error "Cannot link to a buffer which is not visiting a file"))
8934 (t (setq link nil)))
8936 (if (consp link) (setq cpltxt (car link) link (cdr link)))
8937 (setq link (or link cpltxt)
8938 desc (or desc cpltxt))
8939 (if (equal desc "NONE") (setq desc nil))
8941 (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
8943 (setq org-stored-links
8944 (cons (list link desc) org-stored-links))
8945 (message "Stored: %s" (or desc link))
8947 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
8949 (setq org-stored-links
8950 (cons (list link desc) org-stored-links))))
8951 (or agenda-link (and link (org-make-link-string link desc)))))))
8953 (defun org-store-link-props (&rest plist)
8954 "Store link properties, extract names and addresses."
8956 (when (setq x (plist-get plist :from))
8957 (setq adr (mail-extract-address-components x))
8958 (setq plist (plist-put plist :fromname (car adr)))
8959 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
8960 (when (setq x (plist-get plist :to))
8961 (setq adr (mail-extract-address-components x))
8962 (setq plist (plist-put plist :toname (car adr)))
8963 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
8964 (let ((from (plist-get plist :from))
8965 (to (plist-get plist :to)))
8966 (when (and from to org-from-is-user-regexp)
8968 (plist-put plist :fromto
8969 (if (string-match org-from-is-user-regexp from)
8971 (concat "from %f"))))))
8972 (setq org-store-link-plist plist))
8974 (defun org-add-link-props (&rest plist)
8975 "Add these properties to the link property list."
8978 (setq key (pop plist) value (pop plist))
8979 (setq org-store-link-plist
8980 (plist-put org-store-link-plist key value)))))
8982 (defun org-email-link-description (&optional fmt)
8983 "Return the description part of an email link.
8984 This takes information from `org-store-link-plist' and formats it
8985 according to FMT (default from `org-email-link-description-format')."
8986 (setq fmt (or fmt org-email-link-description-format))
8987 (let* ((p org-store-link-plist)
8988 (to (plist-get p :toaddress))
8989 (from (plist-get p :fromaddress))
8992 (cons "%c" (plist-get p :fromto))
8993 (cons "%F" (plist-get p :from))
8994 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
8995 (cons "%T" (plist-get p :to))
8996 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
8997 (cons "%s" (plist-get p :subject))
8998 (cons "%d" (plist-get p :date))
8999 (cons "%m" (plist-get p :message-id)))))
9000 (when (string-match "%c" fmt)
9001 ;; Check if the user wrote this message
9002 (if (and org-from-is-user-regexp from to
9003 (save-match-data (string-match org-from-is-user-regexp from)))
9004 (setq fmt (replace-match "to %t" t t fmt))
9005 (setq fmt (replace-match "from %f" t t fmt))))
9006 (org-replace-escapes fmt table)))
9008 (defun org-make-org-heading-search-string (&optional string heading)
9009 "Make search string for STRING or current headline."
9011 (let ((s (or string (org-get-heading)))
9012 (lines org-context-in-file-links))
9013 (unless (and string (not heading))
9014 ;; We are using a headline, clean up garbage in there.
9015 (if (string-match org-todo-regexp s)
9016 (setq s (replace-match "" t t s)))
9017 (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
9018 (setq s (replace-match "" t t s)))
9019 (setq s (org-trim s))
9020 (if (string-match (concat "^\\(" org-quote-string "\\|"
9021 org-comment-string "\\)") s)
9022 (setq s (replace-match "" t t s)))
9023 (while (string-match org-ts-regexp s)
9024 (setq s (replace-match "" t t s))))
9025 (or string (setq s (concat "*" s))) ; Add * for headlines
9026 (when (and string (integerp lines) (> lines 0))
9027 (let ((slines (org-split-string s "\n")))
9028 (when (< lines (length slines))
9031 (reverse (nthcdr (- (length slines) lines)
9032 (reverse slines))) "\n")))))
9033 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
9035 (defun org-make-link-string (link &optional description)
9036 "Make a link with brackets, consisting of LINK and DESCRIPTION."
9037 (unless (string-match "\\S-" link)
9038 (error "Empty link"))
9039 (when (and description
9040 (stringp description)
9041 (not (string-match "\\S-" description)))
9042 (setq description nil))
9043 (when (stringp description)
9044 ;; Remove brackets from the description, they are fatal.
9045 (while (string-match "\\[" description)
9046 (setq description (replace-match "{" t t description)))
9047 (while (string-match "\\]" description)
9048 (setq description (replace-match "}" t t description))))
9049 (when (equal link description)
9050 ;; No description needed, it is identical
9051 (setq description nil))
9052 (when (and (not description)
9053 (not (string-match (org-image-file-name-regexp) link))
9054 (not (equal link (org-link-escape link))))
9055 (setq description (org-extract-attributes link)))
9057 (cond ((string-match (org-image-file-name-regexp) link) link)
9058 ((string-match org-link-types-re link)
9059 (concat (match-string 1 link)
9060 (org-link-escape (substring link (match-end 1)))))
9061 (t (org-link-escape link))))
9062 (concat "[[" link "]"
9063 (if description (concat "[" description "]") "")
9066 (defconst org-link-escape-chars
9067 '(?\ ?\[ ?\] ?\; ?\= ?\+)
9068 "List of characters that should be escaped in link.
9069 This is the list that is used for internal purposes.")
9071 (defconst org-link-escape-chars-browser
9073 "List of escapes for characters that are problematic in links.
9074 This is the list that is used before handing over to the browser.")
9076 (defun org-link-escape (text &optional table merge)
9077 "Return percent escaped representation of TEXT.
9078 TEXT is a string with the text to escape.
9079 Optional argument TABLE is a list with characters that should be
9080 escaped. When nil, `org-link-escape-chars' is used.
9081 If optional argument MERGE is set, merge TABLE into
9082 `org-link-escape-chars'."
9085 (mapc (lambda (defchr)
9086 (unless (member defchr table)
9087 (setq table (cons defchr table)))) org-link-escape-chars))
9089 (setq table org-link-escape-chars)))
9092 (if (or (member char table)
9093 (and (or (< char 32) (= char 37) (> char 126))
9095 (mapconcat (lambda (sequence-element)
9096 (format "%%%.2X" sequence-element))
9097 (or (encode-coding-char char 'utf-8)
9098 (error "Unable to percent escape character: %s"
9099 (char-to-string char))) "")
9100 (char-to-string char))) text ""))
9102 (defun org-link-unescape (str)
9103 "Unhex hexified Unicode strings as returned from the JavaScript function
9104 encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
9105 (unless (and (null str) (string= "" str))
9106 (let ((pos 0) (case-fold-search t) unhexed)
9107 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
9108 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
9109 (setq str (replace-match unhexed t t str))
9110 (setq pos (+ pos (length unhexed))))))
9113 (defun org-link-unescape-compound (hex)
9114 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
9115 Note: this function also decodes single byte encodings like
9116 `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
9118 (let* ((bytes (cdr (split-string hex "%")))
9123 (let* ((val (string-to-number (pop bytes) 16))
9127 ((>= val 252) (cons 6 252))
9128 ((>= val 248) (cons 5 248))
9129 ((>= val 240) (cons 4 240))
9130 ((>= val 224) (cons 3 224))
9131 ((>= val 192) (cons 2 192))
9134 (if (>= val 192) (setq eat (car shift-xor)))
9135 (setq val (logxor val (cdr shift-xor)))
9136 (setq sum (+ (lsh sum (car shift-xor)) val))
9137 (if (> eat 0) (setq eat (- eat 1)))
9139 ((= 0 eat) ;multi byte
9140 (setq ret (concat ret (org-char-to-string sum)))
9142 ((not bytes) ; single byte(s)
9143 (setq ret (org-link-unescape-single-byte-sequence hex))))
9144 )) ;; end (while bytes
9147 (defun org-link-unescape-single-byte-sequence (hex)
9148 "Unhexify hex-encoded single byte character sequences."
9149 (mapconcat (lambda (byte)
9150 (char-to-string (string-to-number byte 16)))
9151 (cdr (split-string hex "%")) ""))
9153 (defun org-xor (a b)
9157 (defun org-fixup-message-id-for-http (s)
9158 "Replace special characters in a message id, so it can be used in an http query."
9159 (when (string-match "%" s)
9160 (setq s (mapconcat (lambda (c)
9163 (char-to-string c)))
9165 (while (string-match "<" s)
9166 (setq s (replace-match "%3C" t t s)))
9167 (while (string-match ">" s)
9168 (setq s (replace-match "%3E" t t s)))
9169 (while (string-match "@" s)
9170 (setq s (replace-match "%40" t t s)))
9173 (defun org-link-prettify (link)
9174 "Return a human-readable representation of LINK.
9175 The car of LINK must be a raw link the cdr of LINK must be either
9176 a link description or nil."
9177 (let ((desc (or (cadr link) "<no description>")))
9178 (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
9179 "<" (car link) ">")))
9182 (defun org-insert-link-global ()
9183 "Insert a link like Org-mode does.
9184 This command can be called in any mode to insert a link in Org-mode syntax."
9186 (org-load-modules-maybe)
9187 (org-run-like-in-org-mode 'org-insert-link))
9189 (defun org-insert-all-links (&optional keep)
9190 "Insert all links in `org-stored-links'."
9192 (let ((links (copy-sequence org-stored-links)) l)
9193 (while (setq l (if keep (pop links) (pop org-stored-links)))
9195 (org-insert-link nil (car l) (cadr l))
9198 (defun org-link-fontify-links-to-this-file ()
9199 "Fontify links to the current file in `org-stored-links'."
9200 (let ((f (buffer-file-name)) a b)
9201 (setq a (mapcar (lambda(l)
9203 (when (and (string-match "^file:\\(.+\\)::" ll)
9204 (equal f (expand-file-name (match-string 1 ll))))
9207 (when (featurep 'org-id)
9208 (setq b (mapcar (lambda(l)
9210 (when (and (string-match "^id:\\(.+\\)$" ll)
9211 (equal f (expand-file-name
9212 (or (org-id-find-id-file
9213 (match-string 1 ll)) ""))))
9217 (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
9218 (delq nil (append a b)))))
9220 (defvar org-link-links-in-this-file nil)
9221 (defun org-insert-link (&optional complete-file link-location default-description)
9222 "Insert a link. At the prompt, enter the link.
9224 Completion can be used to insert any of the link protocol prefixes like
9227 The history can be used to select a link previously stored with
9228 `org-store-link'. When the empty string is entered (i.e. if you just
9229 press RET at the prompt), the link defaults to the most recently
9230 stored link. As SPC triggers completion in the minibuffer, you need to
9231 use M-SPC or C-q SPC to force the insertion of a space character.
9233 You will also be prompted for a description, and if one is given, it will
9234 be displayed in the buffer instead of the link.
9236 If there is already a link at point, this command will allow you to edit link
9237 and description parts.
9239 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9240 be selected using completion. The path to the file will be relative to the
9241 current directory if the file is in the current directory or a subdirectory.
9242 Otherwise, the link will be the absolute path as completed in the minibuffer
9243 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9244 option `org-link-file-path-type'.
9246 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9247 the current directory or below.
9249 With three \\[universal-argument] prefixes, negate the meaning of
9250 `org-keep-stored-link-after-insertion'.
9252 If `org-make-link-description-function' is non-nil, this function will be
9253 called with the link target, and the result will be the default
9256 If the LINK-LOCATION parameter is non-nil, this value will be
9257 used as the link location instead of reading one interactively.
9259 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9260 be used as the default description."
9262 (let* ((wcf (current-window-configuration))
9263 (region (if (org-region-active-p)
9264 (buffer-substring (region-beginning) (region-end))))
9265 (remove (and region (list (region-beginning) (region-end))))
9267 tmphist ; byte-compile incorrectly complains about this
9268 (link link-location)
9269 (abbrevs org-link-abbrev-alist-local)
9270 entry file all-prefixes auto-desc)
9272 (link-location) ; specified by arg, just use it.
9273 ((org-in-regexp org-bracket-link-regexp 1)
9274 ;; We do have a link at point, and we are going to edit it.
9275 (setq remove (list (match-beginning 0) (match-end 0)))
9276 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
9277 (setq link (read-string "Link: "
9279 (org-match-string-no-properties 1)))))
9280 ((or (org-in-regexp org-angle-link-re)
9281 (org-in-regexp org-plain-link-re))
9282 ;; Convert to bracket link
9283 (setq remove (list (match-beginning 0) (match-end 0))
9284 link (read-string "Link: "
9285 (org-remove-angle-brackets (match-string 0)))))
9286 ((member complete-file '((4) (16)))
9287 ;; Completing read for file names.
9288 (setq link (org-file-complete-link complete-file)))
9290 ;; Read link, with completion for stored links.
9291 (org-link-fontify-links-to-this-file)
9292 (org-switch-to-buffer-other-window "*Org Links*")
9293 (with-current-buffer "*Org Links*"
9295 (insert "Insert a link.
9296 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9297 (when org-stored-links
9298 (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9299 (insert (mapconcat 'org-link-prettify
9300 (reverse org-stored-links) "\n")))
9301 (goto-char (point-min)))
9302 (let ((cw (selected-window)))
9303 (select-window (get-buffer-window "*Org Links*" 'visible))
9304 (with-current-buffer "*Org Links*" (setq truncate-lines t))
9305 (unless (pos-visible-in-window-p (point-max))
9306 (org-fit-window-to-buffer))
9307 (and (window-live-p cw) (select-window cw)))
9308 ;; Fake a link history, containing the stored links.
9309 (setq tmphist (append (mapcar 'car org-stored-links)
9310 org-insert-link-history))
9311 (setq all-prefixes (append (mapcar 'car abbrevs)
9312 (mapcar 'car org-link-abbrev-alist)
9317 (let ((org-completion-use-ido nil)
9318 (org-completion-use-iswitchb nil))
9319 (org-completing-read
9322 (mapcar (lambda (x) (list (concat x ":")))
9324 (mapcar 'car org-stored-links)
9325 (mapcar 'cadr org-stored-links))
9328 (caar org-stored-links))))
9329 (if (not (string-match "\\S-" link))
9330 (error "No link selected"))
9332 (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
9334 (if (or (member link all-prefixes)
9335 (and (equal ":" (substring link -1))
9336 (member (substring link 0 -1) all-prefixes)
9337 (setq link (substring link 0 -1))))
9338 (setq link (org-link-try-special-completion link))))
9339 (set-window-configuration wcf)
9340 (kill-buffer "*Org Links*"))
9341 (setq entry (assoc link org-stored-links))
9342 (or entry (push link org-insert-link-history))
9343 (setq desc (or desc (nth 1 entry)))))
9345 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
9346 (not org-keep-stored-link-after-insertion))
9347 (setq org-stored-links (delq (assoc link org-stored-links)
9350 (if (string-match org-plain-link-re link)
9351 ;; URL-like link, normalize the use of angular brackets.
9352 (setq link (org-remove-angle-brackets link)))
9354 ;; Check if we are linking to the current file with a search
9355 ;; option If yes, simplify the link by using only the search
9357 (when (and buffer-file-name
9358 (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
9359 (let* ((path (match-string 1 link))
9360 (case-fold-search nil)
9361 (search (match-string 2 link)))
9363 (if (equal (file-truename buffer-file-name) (file-truename path))
9364 ;; We are linking to this same file, with a search option
9365 (setq link search)))))
9367 ;; Check if we can/should use a relative path. If yes, simplify the link
9368 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
9369 (let* ((type (match-string 1 link))
9370 (path (match-string 2 link))
9372 (case-fold-search nil))
9374 ((or (eq org-link-file-path-type 'absolute)
9375 (equal complete-file '(16)))
9376 (setq path (abbreviate-file-name (expand-file-name path))))
9377 ((eq org-link-file-path-type 'noabbrev)
9378 (setq path (expand-file-name path)))
9379 ((eq org-link-file-path-type 'relative)
9380 (setq path (file-relative-name path)))
9383 (if (string-match (concat "^" (regexp-quote
9385 (file-name-as-directory
9386 default-directory))))
9387 (expand-file-name path))
9388 ;; We are linking a file with relative path name.
9389 (setq path (substring (expand-file-name path)
9391 (setq path (abbreviate-file-name (expand-file-name path)))))))
9392 (setq link (concat type path))
9393 (if (equal desc origpath)
9396 (if org-make-link-description-function
9398 (or (condition-case nil
9399 (funcall org-make-link-description-function link desc)
9400 (error (progn (message "Can't get link description from `%s'"
9401 (symbol-name org-make-link-description-function))
9403 (read-string "Description: " default-description)))
9404 (if default-description (setq desc default-description)
9405 (setq desc (or (and auto-desc desc)
9406 (read-string "Description: " desc)))))
9408 (unless (string-match "\\S-" desc) (setq desc nil))
9409 (if remove (apply 'delete-region remove))
9410 (insert (org-make-link-string link desc))))
9412 (defun org-link-try-special-completion (type)
9413 "If there is completion support for link type TYPE, offer it."
9414 (let ((fun (intern (concat "org-" type "-complete-link"))))
9417 (read-string "Link (no completion support): " (concat type ":")))))
9419 (defun org-file-complete-link (&optional arg)
9420 "Create a file link using completion."
9422 (setq file (read-file-name "File: "))
9423 (let ((pwd (file-name-as-directory (expand-file-name ".")))
9424 (pwd1 (file-name-as-directory (abbreviate-file-name
9425 (expand-file-name ".")))))
9430 (abbreviate-file-name (expand-file-name file)))))
9431 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
9432 (setq link (concat "file:" (match-string 1 file))))
9433 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
9434 (expand-file-name file))
9436 "file:" (match-string 1 (expand-file-name file)))))
9437 (t (setq link (concat "file:" file)))))
9440 (defun org-completing-read (&rest args)
9441 "Completing-read with SPACE being a normal character."
9442 (let ((enable-recursive-minibuffers t)
9443 (minibuffer-local-completion-map
9444 (copy-keymap minibuffer-local-completion-map)))
9445 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
9446 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
9447 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
9448 (apply 'org-icompleting-read args)))
9450 (defun org-completing-read-no-i (&rest args)
9451 (let (org-completion-use-ido org-completion-use-iswitchb)
9452 (apply 'org-completing-read args)))
9454 (defun org-iswitchb-completing-read (prompt choices &rest args)
9455 "Use iswitch as a completing-read replacement to choose from choices.
9456 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
9458 (let* ((iswitchb-use-virtual-buffers nil)
9459 (iswitchb-make-buflist-hook
9461 (setq iswitchb-temp-buflist choices))))
9462 (iswitchb-read-buffer prompt)))
9464 (defun org-icompleting-read (&rest args)
9465 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
9466 (org-without-partial-completion
9467 (if (and org-completion-use-ido
9468 (fboundp 'ido-completing-read)
9469 (boundp 'ido-mode) ido-mode
9470 (listp (second args)))
9471 (let ((ido-enter-matching-directory nil))
9472 (apply 'ido-completing-read (concat (car args))
9473 (if (consp (car (nth 1 args)))
9474 (mapcar 'car (nth 1 args))
9477 (if (and org-completion-use-iswitchb
9478 (boundp 'iswitchb-mode) iswitchb-mode
9479 (listp (second args)))
9480 (apply 'org-iswitchb-completing-read (concat (car args))
9481 (if (consp (car (nth 1 args)))
9482 (mapcar 'car (nth 1 args))
9485 (apply 'completing-read args)))))
9487 (defun org-extract-attributes (s)
9488 "Extract the attributes cookie from a string and set as text property."
9489 (let (a attr (start 0) key value)
9491 (when (string-match "{{\\([^}]+\\)}}$" s)
9492 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
9493 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
9494 (setq key (match-string 1 a) value (match-string 2 a)
9496 attr (plist-put attr (intern key) value))))
9497 (org-add-props s nil 'org-attr attr))
9500 (defun org-extract-attributes-from-string (tag)
9501 (let (key value attr)
9502 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
9503 (setq key (match-string 1 tag) value (match-string 2 tag)
9504 tag (replace-match "" t t tag)
9505 attr (plist-put attr (intern key) value)))
9508 (defun org-attributes-to-string (plist)
9509 "Format a property list into an HTML attribute list."
9510 (let ((s "") key value)
9512 (setq key (pop plist) value (pop plist))
9514 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
9517 ;;; Opening/following a link
9519 (defvar org-link-search-failed nil)
9521 (defvar org-open-link-functions nil
9522 "Hook for functions finding a plain text link.
9523 These functions must take a single argument, the link content.
9524 They will be called for links that look like [[link text][description]]
9525 when LINK TEXT does not have a protocol like \"http:\" and does not look
9526 like a filename (e.g. \"./blue.png\").
9528 These functions will be called *before* Org attempts to resolve the
9529 link by doing text searches in the current buffer - so if you want a
9530 link \"[[target]]\" to still find \"<<target>>\", your function should
9531 handle this as a special case.
9533 When the function does handle the link, it must return a non-nil value.
9534 If it decides that it is not responsible for this link, it must return
9535 nil to indicate that that Org-mode can continue with other options
9536 like exact and fuzzy text search.")
9538 (defun org-next-link ()
9539 "Move forward to the next link.
9540 If the link is in hidden text, expose it."
9542 (when (and org-link-search-failed (eq this-command last-command))
9543 (goto-char (point-min))
9544 (message "Link search wrapped back to beginning of buffer"))
9545 (setq org-link-search-failed nil)
9546 (let* ((pos (point))
9548 (a (assoc :link ct)))
9549 (if a (goto-char (nth 2 a)))
9550 (if (re-search-forward org-any-link-re nil t)
9552 (goto-char (match-beginning 0))
9553 (if (outline-invisible-p) (org-show-context)))
9555 (setq org-link-search-failed t)
9556 (error "No further link found"))))
9558 (defun org-previous-link ()
9559 "Move backward to the previous link.
9560 If the link is in hidden text, expose it."
9562 (when (and org-link-search-failed (eq this-command last-command))
9563 (goto-char (point-max))
9564 (message "Link search wrapped back to end of buffer"))
9565 (setq org-link-search-failed nil)
9566 (let* ((pos (point))
9568 (a (assoc :link ct)))
9569 (if a (goto-char (nth 1 a)))
9570 (if (re-search-backward org-any-link-re nil t)
9572 (goto-char (match-beginning 0))
9573 (if (outline-invisible-p) (org-show-context)))
9575 (setq org-link-search-failed t)
9576 (error "No further link found"))))
9578 (defun org-translate-link (s)
9579 "Translate a link string if a translation function has been defined."
9580 (if (and org-link-translation-function
9581 (fboundp org-link-translation-function)
9582 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
9584 (setq s (funcall org-link-translation-function
9585 (match-string 1 s) (match-string 2 s)))
9586 (concat (car s) ":" (cdr s)))
9589 (defun org-translate-link-from-planner (type path)
9590 "Translate a link from Emacs Planner syntax so that Org can follow it.
9591 This is still an experimental function, your mileage may vary."
9593 ((member type '("http" "https" "news" "ftp"))
9594 ;; standard Internet links are the same.
9596 ((and (equal type "irc") (string-match "^//" path))
9597 ;; Planner has two / at the beginning of an irc link, we have 1.
9598 ;; We should have zero, actually....
9599 (setq path (substring path 1)))
9600 ((and (equal type "lisp") (string-match "^/" path))
9601 ;; Planner has a slash, we do not.
9602 (setq type "elisp" path (substring path 1)))
9603 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
9604 ;; A typical message link. Planner has the id after the final slash,
9605 ;; we separate it with a hash mark
9606 (setq path (concat (match-string 1 path) "#"
9607 (org-remove-angle-brackets (match-string 2 path)))))
9611 (defun org-find-file-at-mouse (ev)
9612 "Open file link or URL at mouse."
9614 (mouse-set-point ev)
9615 (org-open-at-point 'in-emacs))
9617 (defun org-open-at-mouse (ev)
9618 "Open file link or URL at mouse.
9619 See the docstring of `org-open-file' for details."
9621 (mouse-set-point ev)
9622 (if (eq major-mode 'org-agenda-mode)
9623 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
9624 (org-open-at-point))
9626 (defvar org-window-config-before-follow-link nil
9627 "The window configuration before following a link.
9628 This is saved in case the need arises to restore it.")
9630 (defvar org-open-link-marker (make-marker)
9631 "Marker pointing to the location where `org-open-at-point; was called.")
9634 (defun org-open-at-point-global ()
9635 "Follow a link like Org-mode does.
9636 This command can be called in any mode to follow a link that has
9639 (org-run-like-in-org-mode 'org-open-at-point))
9642 (defun org-open-link-from-string (s &optional arg reference-buffer)
9643 "Open a link in the string S, as if it was in Org-mode."
9644 (interactive "sLink: \nP")
9645 (let ((reference-buffer (or reference-buffer (current-buffer))))
9647 (let ((org-inhibit-startup (not reference-buffer)))
9650 (goto-char (point-min))
9651 (when reference-buffer
9652 (setq org-link-abbrev-alist-local
9653 (with-current-buffer reference-buffer
9654 org-link-abbrev-alist-local)))
9655 (org-open-at-point arg reference-buffer)))))
9657 (defvar org-open-at-point-functions nil
9658 "Hook that is run when following a link at point.
9660 Functions in this hook must return t if they identify and follow
9661 a link at point. If they don't find anything interesting at point,
9662 they must return nil.")
9664 (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
9665 (defun org-open-at-point (&optional arg reference-buffer)
9666 "Open link at or after point.
9667 If there is no link at point, this function will search forward up to
9668 the end of the current line.
9669 Normally, files will be opened by an appropriate application. If the
9670 optional prefix argument ARG is non-nil, Emacs will visit the file.
9671 With a double prefix argument, try to open outside of Emacs, in the
9672 application the system uses for this file type."
9674 ;; if in a code block, then open the block's results
9675 (unless (call-interactively #'org-babel-open-src-block-result)
9676 (org-load-modules-maybe)
9677 (move-marker org-open-link-marker (point))
9678 (setq org-window-config-before-follow-link (current-window-configuration))
9679 (org-remove-occur-highlights nil nil t)
9681 ((and (org-at-heading-p)
9682 (not (org-at-timestamp-p t))
9684 (concat org-plain-link-re "\\|"
9685 org-bracket-link-regexp "\\|"
9686 org-angle-link-re "\\|"
9687 "[ \t]:[^ \t\n]+:[ \t]*$")))
9688 (not (get-text-property (point) 'org-linked-text)))
9689 (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
9691 (lk (if (stringp lk0) (list lk0) lk0))
9692 (lkend (cdr lkall)))
9694 (search-forward l nil lkend)
9695 (goto-char (match-beginning 0))
9696 (org-open-at-point))
9698 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
9699 ((run-hook-with-args-until-success 'org-open-at-point-functions))
9700 ((and (org-at-timestamp-p t)
9701 (not (org-in-regexp org-bracket-link-regexp)))
9702 (org-follow-timestamp-link))
9703 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
9704 (not (org-in-regexp org-any-link-re)))
9705 (org-footnote-action))
9707 (let (type path link line search (pos (point)))
9710 (skip-chars-forward "^]\n\r")
9711 (when (org-in-regexp org-bracket-link-regexp 1)
9712 (setq link (org-extract-attributes
9713 (org-link-unescape (org-match-string-no-properties 1))))
9714 (while (string-match " *\n *" link)
9715 (setq link (replace-match " " t t link)))
9716 (setq link (org-link-expand-abbrev link))
9718 ((or (file-name-absolute-p link)
9719 (string-match "^\\.\\.?/" link))
9720 (setq type "file" path link))
9721 ((string-match org-link-re-with-space3 link)
9722 (setq type (match-string 1 link) path (match-string 2 link)))
9723 ((string-match "^help:+\\(.+\\)" link)
9724 (setq type "help" path (match-string 1 link)))
9725 (t (setq type "thisfile" path link)))
9728 (when (get-text-property (point) 'org-linked-text)
9729 (setq type "thisfile"
9730 pos (if (get-text-property (1+ (point)) 'org-linked-text)
9731 (1+ (point)) (point))
9732 path (buffer-substring
9733 (or (previous-single-property-change pos 'org-linked-text)
9735 (or (next-single-property-change pos 'org-linked-text)
9740 (let ((plinkpos (org-in-regexp org-plain-link-re)))
9741 (when (or (org-in-regexp org-angle-link-re)
9742 (and plinkpos (goto-char (car plinkpos))
9743 (save-match-data (not (looking-back "\\[\\[")))))
9744 (setq type (match-string 1)
9745 path (org-link-unescape (match-string 2)))
9748 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
9750 path (match-string 1))
9751 (while (string-match ":" path)
9752 (setq path (replace-match "+" t t path)))
9754 (when (org-in-regexp "<\\([^><\n]+\\)>")
9755 (setq type "tree-match"
9756 path (match-string 1))
9759 (user-error "No link found"))
9761 ;; switch back to reference buffer
9762 ;; needed when if called in a temporary buffer through
9763 ;; org-open-link-from-string
9764 (with-current-buffer (or reference-buffer (current-buffer))
9766 ;; Remove any trailing spaces in path
9767 (if (string-match " +\\'" path)
9768 (setq path (replace-match "" t t path)))
9769 (if (and org-link-translation-function
9770 (fboundp org-link-translation-function))
9771 ;; Check if we need to translate the link
9772 (let ((tmp (funcall org-link-translation-function type path)))
9773 (setq type (car tmp) path (cdr tmp))))
9777 ((assoc type org-link-protocols)
9778 (funcall (nth 1 (assoc type org-link-protocols)) path))
9780 ((equal type "help")
9781 (let ((f-or-v (intern path)))
9782 (cond ((fboundp f-or-v)
9783 (describe-function f-or-v))
9785 (describe-variable f-or-v))
9786 (t (error "Not a known function or variable")))))
9788 ((equal type "mailto")
9789 (let ((cmd (car org-link-mailto-program))
9790 (args (cdr org-link-mailto-program)) args1
9791 (address path) (subject "") a)
9792 (if (string-match "\\(.*\\)::\\(.*\\)" path)
9793 (setq address (match-string 1 path)
9794 subject (org-link-escape (match-string 2 path))))
9797 ((not (stringp (car args))) (push (pop args) args1))
9798 (t (setq a (pop args))
9799 (if (string-match "%a" a)
9800 (setq a (replace-match address t t a)))
9801 (if (string-match "%s" a)
9802 (setq a (replace-match subject t t a)))
9804 (apply cmd (nreverse args1))))
9806 ((member type '("http" "https" "ftp" "news"))
9807 (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
9809 path org-link-escape-chars-browser)
9812 ((string= type "doi")
9813 (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
9815 path org-link-escape-chars-browser)
9818 ((member type '("message"))
9819 (browse-url (concat type ":" path)))
9821 ((string= type "tags")
9822 (org-tags-view arg path))
9824 ((string= type "tree-match")
9825 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
9827 ((string= type "file")
9828 (if (string-match "::\\([0-9]+\\)\\'" path)
9829 (setq line (string-to-number (match-string 1 path))
9830 path (substring path 0 (match-beginning 0)))
9831 (if (string-match "::\\(.+\\)\\'" path)
9832 (setq search (match-string 1 path)
9833 path (substring path 0 (match-beginning 0)))))
9834 (if (string-match "[*?{]" (file-name-nondirectory path))
9836 (org-open-file path arg line search)))
9838 ((string= type "shell")
9839 (let ((buf (generate-new-buffer "*Org Shell Output"))
9841 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
9842 (string-match org-confirm-shell-link-not-regexp cmd))
9843 (not org-confirm-shell-link-function)
9844 (funcall org-confirm-shell-link-function
9845 (format "Execute \"%s\" in shell? "
9846 (org-add-props cmd nil
9847 'face 'org-warning))))
9849 (message "Executing %s" cmd)
9850 (shell-command cmd buf)
9851 (if (featurep 'midnight)
9852 (setq clean-buffer-list-kill-buffer-names
9853 (cons buf clean-buffer-list-kill-buffer-names))))
9856 ((string= type "elisp")
9858 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
9859 (string-match org-confirm-elisp-link-not-regexp cmd))
9860 (not org-confirm-elisp-link-function)
9861 (funcall org-confirm-elisp-link-function
9862 (format "Execute \"%s\" as elisp? "
9863 (org-add-props cmd nil
9864 'face 'org-warning))))
9865 (message "%s => %s" cmd
9866 (if (equal (string-to-char cmd) ?\()
9868 (call-interactively (read cmd))))
9871 ((and (string= type "thisfile")
9872 (run-hook-with-args-until-success
9873 'org-open-link-functions path)))
9875 ((string= type "thisfile")
9877 (switch-to-buffer-other-window
9878 (org-get-buffer-for-internal-link (current-buffer)))
9879 (org-mark-ring-push))
9880 (let ((cmd `(org-link-search
9882 ,(cond ((equal arg '(4)) ''occur)
9883 ((equal arg '(16)) ''org-occur))
9885 (condition-case nil (let ((org-link-search-inhibit-query t))
9887 (error (progn (widen) (eval cmd))))))
9889 (t (browse-url-at-point)))))))
9890 (move-marker org-open-link-marker nil)
9891 (run-hook-with-args 'org-follow-link-hook)))
9893 (defun org-offer-links-in-entry (buffer marker &optional nth zero)
9894 "Offer links in the current entry and return the selected link.
9895 If there is only one link, return it.
9896 If NTH is an integer, return the NTH link found.
9897 If ZERO is a string, check also this string for a link, and if
9898 there is one, return it."
9899 (with-current-buffer buffer
9904 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
9905 "\\(" org-angle-link-re "\\)\\|"
9906 "\\(" org-plain-link-re "\\)"))
9908 (in-emacs (if (integerp nth) nil nth))
9909 have-zero end links link c)
9910 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
9911 (push (match-string 0 zero) links)
9912 (setq cnt (1- cnt) have-zero t))
9914 (org-back-to-heading t)
9915 (setq end (save-excursion (outline-next-heading) (point)))
9916 (while (re-search-forward re end t)
9917 (push (match-string 0) links))
9918 (setq links (org-uniquify (reverse links))))
9921 (message "No links"))
9922 ((equal (length links) 1)
9923 (setq link (car links)))
9924 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
9925 (setq link (nth (if have-zero nth (1- nth)) links)))
9926 (t ; we have to select a link
9928 (save-window-excursion
9929 (delete-other-windows)
9930 (with-output-to-temp-buffer "*Select Link*"
9932 (if (not (string-match org-bracket-link-regexp l))
9933 (princ (format "[%c] %s\n" (incf cnt)
9934 (org-remove-angle-brackets l)))
9936 (princ (format "[%c] %s (%s)\n" (incf cnt)
9937 (match-string 3 l) (match-string 1 l)))
9938 (princ (format "[%c] %s\n" (incf cnt)
9939 (match-string 1 l))))))
9941 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
9942 (message "Select link to open, RET to open all:")
9943 (setq c (read-char-exclusive))
9944 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
9945 (when (equal c ?q) (error "Abort"))
9949 (if have-zero (setq nth (1+ nth)))
9950 (unless (and (integerp nth) (>= (length links) nth))
9951 (error "Invalid link selection"))
9952 (setq link (nth (1- nth) links)))))
9953 (cons link end))))))
9955 ;; Add special file links that specify the way of opening
9957 (org-add-link-type "file+sys" 'org-open-file-with-system)
9958 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
9959 (defun org-open-file-with-system (path)
9960 "Open file at PATH using the system way of opening it."
9961 (org-open-file path 'system))
9962 (defun org-open-file-with-emacs (path)
9963 "Open file at PATH in Emacs."
9964 (org-open-file path 'emacs))
9965 (defun org-remove-file-link-modifiers ()
9966 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
9967 (goto-char (point-min))
9968 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
9970 (replace-match "file:" t t))))
9971 (eval-after-load "org-exp"
9972 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
9973 'org-remove-file-link-modifiers))
9977 (defvar org-create-file-search-functions nil
9978 "List of functions to construct the right search string for a file link.
9979 These functions are called in turn with point at the location to
9980 which the link should point.
9982 A function in the hook should first test if it would like to
9983 handle this file type, for example by checking the `major-mode'
9984 or the file extension. If it decides not to handle this file, it
9985 should just return nil to give other functions a chance. If it
9986 does handle the file, it must return the search string to be used
9987 when following the link. The search string will be part of the
9988 file link, given after a double colon, and `org-open-at-point'
9989 will automatically search for it. If special measures must be
9990 taken to make the search successful, another function should be
9991 added to the companion hook `org-execute-file-search-functions',
9994 A function in this hook may also use `setq' to set the variable
9995 `description' to provide a suggestion for the descriptive text to
9996 be used for this link when it gets inserted into an Org-mode
9997 buffer with \\[org-insert-link].")
9999 (defvar org-execute-file-search-functions nil
10000 "List of functions to execute a file search triggered by a link.
10002 Functions added to this hook must accept a single argument, the
10003 search string that was part of the file link, the part after the
10004 double colon. The function must first check if it would like to
10005 handle this search, for example by checking the `major-mode' or
10006 the file extension. If it decides not to handle this search, it
10007 should just return nil to give other functions a chance. If it
10008 does handle the search, it must return a non-nil value to keep
10009 other functions from trying.
10011 Each function can access the current prefix argument through the
10012 variable `current-prefix-argument'. Note that a single prefix is
10013 used to force opening a link in Emacs, so it may be good to only
10014 use a numeric or double prefix to guide the search function.
10016 In case this is needed, a function in this hook can also restore
10017 the window configuration before `org-open-at-point' was called using:
10019 (set-window-configuration org-window-config-before-follow-link)")
10021 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
10022 (defun org-link-search (s &optional type avoid-pos stealth)
10023 "Search for a link search option.
10024 If S is surrounded by forward slashes, it is interpreted as a
10025 regular expression. In org-mode files, this will create an `org-occur'
10026 sparse tree. In ordinary files, `occur' will be used to list matches.
10027 If the current buffer is in `dired-mode', grep will be used to search
10028 in all files. If AVOID-POS is given, ignore matches near that position.
10030 When optional argument STEALTH is non-nil, do not modify
10031 visibility around point, thus ignoring
10032 `org-show-hierarchy-above', `org-show-following-heading' and
10033 `org-show-siblings' variables."
10034 (let ((case-fold-search t)
10035 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
10036 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
10037 (append '(("") (" ") ("\t") ("\n"))
10038 org-emphasis-alist)
10041 (pre nil) (post nil)
10042 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
10044 ;; First check if there are any special search functions
10045 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
10046 ;; Now try the builtin stuff
10047 ((and (equal (string-to-char s0) ?#)
10050 (goto-char (point-min))
10053 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
10054 (setq type 'dedicated
10055 pos (match-beginning 0))))
10056 ;; There is an exact target for this
10058 (org-back-to-heading t)))
10060 (goto-char (point-min))
10063 (concat "<<" (regexp-quote s0) ">>") nil t)
10064 (setq type 'dedicated
10065 pos (match-beginning 0))))
10066 ;; There is an exact target for this
10069 (goto-char (point-min))
10072 (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
10073 (setq type 'dedicated pos (match-beginning 0))))
10074 ;; Found an invisible target.
10077 (goto-char (point-min))
10080 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
10081 (setq type 'dedicated pos (match-beginning 0))))
10082 ;; Found an element with a matching #+name affiliated keyword.
10084 ((and (string-match "^(\\(.*\\))$" s0)
10086 (goto-char (point-min))
10089 (concat "[^[]" (regexp-quote
10090 (format org-coderef-label-format
10091 (match-string 1 s0))))
10093 (setq type 'dedicated
10094 pos (1+ (match-beginning 0))))))
10095 ;; There is a coderef target for this
10097 ((string-match "^/\\(.*\\)/$" s)
10098 ;; A regular expression
10100 ((derived-mode-p 'org-mode)
10101 (org-occur (match-string 1 s)))
10102 ;;((eq major-mode 'dired-mode)
10103 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
10104 (t (org-do-occur (match-string 1 s)))))
10105 ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
10106 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
10107 (goto-char (point-min))
10109 ((let (case-fold-search)
10110 (re-search-forward (format org-complex-heading-regexp-format
10113 ;; OK, found a match
10114 (setq type 'dedicated)
10115 (goto-char (match-beginning 0)))
10116 ((and (not org-link-search-inhibit-query)
10117 (eq org-link-search-must-match-exact-headline 'query-to-create)
10118 (y-or-n-p "No match - create this as a new heading? "))
10119 (goto-char (point-max))
10120 (or (bolp) (newline))
10121 (insert "* " s "\n")
10122 (beginning-of-line 0))
10125 (error "No match"))))
10127 ;; A normal search string
10128 (when (equal (string-to-char s) ?*)
10129 ;; Anchor on headlines, post may include tags.
10130 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
10131 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
10132 s (substring s 1)))
10133 (remove-text-properties
10135 '(face nil mouse-face nil keymap nil fontified nil) s)
10136 ;; Make a series of regular expressions to find a match
10137 (setq words (org-split-string s "[ \n\r\t]+")
10139 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
10140 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
10142 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
10143 re2a (concat "[ \t\r\n]" re2a_)
10144 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
10145 re4 (concat "[^a-zA-Z_]" re4_)
10147 re1 (concat pre re2 post)
10148 re3 (concat pre (if pre re4_ re4) post)
10149 re5 (concat pre ".*" re4)
10150 re2 (concat pre re2)
10151 re2a (concat pre (if pre re2a_ re2a))
10152 re4 (concat pre (if pre re4_ re4))
10153 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
10154 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
10158 ((eq type 'org-occur) (org-occur reall))
10159 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
10160 (t (goto-char (point-min))
10162 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
10163 (org-search-not-self 1 re1 nil t)
10164 (org-search-not-self 1 re2 nil t)
10165 (org-search-not-self 1 re2a nil t)
10166 (org-search-not-self 1 re3 nil t)
10167 (org-search-not-self 1 re4 nil t)
10168 (org-search-not-self 1 re5 nil t)
10170 (goto-char (match-beginning 1))
10172 (error "No match"))))))
10173 (and (derived-mode-p 'org-mode)
10175 (org-show-context 'link-search))
10178 (defun org-search-not-self (group &rest args)
10179 "Execute `re-search-forward', but only accept matches that do not
10180 enclose the position of `org-open-link-marker'."
10181 (let ((m org-open-link-marker))
10183 (while (apply 're-search-forward args)
10184 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
10185 (goto-char (match-end group))
10186 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
10187 (> (match-beginning 0) (marker-position m))
10188 (< (match-end 0) (marker-position m)))
10190 (or (not (org-in-regexp
10191 org-bracket-link-analytic-regexp 1))
10192 (not (match-end 4)) ; no description
10193 (and (<= (match-beginning 4) (point))
10194 (>= (match-end 4) (point))))))
10195 (throw 'exit (point))))))))
10197 (defun org-get-buffer-for-internal-link (buffer)
10198 "Return a buffer to be used for displaying the link target of internal links."
10200 ((not org-display-internal-link-with-indirect-buffer)
10202 ((string-match "(Clone)$" (buffer-name buffer))
10203 (message "Buffer is already a clone, not making another one")
10204 ;; we also do not modify visibility in this case
10206 (t ; make a new indirect buffer for displaying the link
10207 (let* ((bn (buffer-name buffer))
10208 (ibn (concat bn "(Clone)"))
10209 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
10210 (with-current-buffer ib (org-overview))
10213 (defun org-do-occur (regexp &optional cleanup)
10214 "Call the Emacs command `occur'.
10215 If CLEANUP is non-nil, remove the printout of the regular expression
10216 in the *Occur* buffer. This is useful if the regex is long and not useful
10220 (let ((cwin (selected-window)) win beg end)
10221 (when (setq win (get-buffer-window "*Occur*"))
10222 (select-window win))
10223 (goto-char (point-min))
10224 (when (re-search-forward "match[a-z]+" nil t)
10225 (setq beg (match-end 0))
10226 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
10227 (setq end (1- (match-beginning 0)))))
10228 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
10229 (goto-char (point-min))
10230 (select-window cwin))))
10232 ;;; The mark ring for links jumps
10234 (defvar org-mark-ring nil
10235 "Mark ring for positions before jumps in Org-mode.")
10236 (defvar org-mark-ring-last-goto nil
10237 "Last position in the mark ring used to go back.")
10238 ;; Fill and close the ring
10239 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
10240 (loop for i from 1 to org-mark-ring-length do
10241 (push (make-marker) org-mark-ring))
10242 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
10245 (defun org-mark-ring-push (&optional pos buffer)
10246 "Put the current position or POS into the mark ring and rotate it."
10248 (setq pos (or pos (point)))
10249 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10250 (move-marker (car org-mark-ring)
10252 (or buffer (current-buffer)))
10254 (substitute-command-keys
10255 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10257 (defun org-mark-ring-goto (&optional n)
10258 "Jump to the previous position in the mark ring.
10259 With prefix arg N, jump back that many stored positions. When
10260 called several times in succession, walk through the entire ring.
10261 Org-mode commands jumping to a different position in the current file,
10262 or to another Org-mode file, automatically push the old position
10266 (if (eq last-command this-command)
10267 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10268 (setq p org-mark-ring))
10269 (setq org-mark-ring-last-goto p)
10271 (org-pop-to-buffer-same-window (marker-buffer m))
10273 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
10275 (defun org-remove-angle-brackets (s)
10276 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
10277 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
10279 (defun org-add-angle-brackets (s)
10280 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
10281 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
10283 (defun org-remove-double-quotes (s)
10284 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
10285 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
10288 ;;; Following specific links
10290 (defun org-follow-timestamp-link ()
10291 "Open an agenda view for the time-stamp date/range at point."
10293 ((org-at-date-range-p t)
10294 (let ((org-agenda-start-on-weekday)
10295 (t1 (match-string 1))
10296 (t2 (match-string 2)) tt1 tt2)
10297 (setq tt1 (time-to-days (org-time-string-to-time t1))
10298 tt2 (time-to-days (org-time-string-to-time t2)))
10299 (let ((org-agenda-buffer-tmp-name
10300 (format "*Org Agenda(a:%s)"
10301 (concat (substring t1 0 10) "--" (substring t2 0 10)))))
10302 (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
10303 ((org-at-timestamp-p t)
10304 (let ((org-agenda-buffer-tmp-name
10305 (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
10306 (org-agenda-list nil (time-to-days (org-time-string-to-time
10307 (substring (match-string 1) 0 10)))
10309 (t (error "This should not happen"))))
10312 ;;; Following file links
10313 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
10314 (declare-function mailcap-extension-to-mime "mailcap" (extn))
10315 (declare-function mailcap-mime-info
10316 "mailcap" (string &optional request no-decode))
10317 (defvar org-wait nil)
10318 (defun org-open-file (path &optional in-emacs line search)
10319 "Open the file at PATH.
10320 First, this expands any special file name abbreviations. Then the
10321 configuration variable `org-file-apps' is checked if it contains an
10322 entry for this file type, and if yes, the corresponding command is launched.
10324 If no application is found, Emacs simply visits the file.
10326 With optional prefix argument IN-EMACS, Emacs will visit the file.
10327 With a double \\[universal-argument] \\[universal-argument] \
10328 prefix arg, Org tries to avoid opening in Emacs
10329 and to use an external application to visit the file.
10331 Optional LINE specifies a line to go to, optional SEARCH a string
10332 to search for. If LINE or SEARCH is given, the file will be
10333 opened in Emacs, unless an entry from org-file-apps that makes
10334 use of groups in a regexp matches.
10336 If you want to change the way frames are used when following a
10337 link, please customize `org-link-frame-setup'.
10339 If the file does not exist, an error is thrown."
10340 (let* ((file (if (equal path "")
10342 (substitute-in-file-name (expand-file-name path))))
10343 (file-apps (append org-file-apps (org-default-apps)))
10344 (apps (org-remove-if
10345 'org-file-apps-entry-match-against-dlink-p file-apps))
10346 (apps-dlink (org-remove-if-not
10347 'org-file-apps-entry-match-against-dlink-p file-apps))
10348 (remp (and (assq 'remote apps) (org-file-remote-p file)))
10349 (dirp (if remp nil (file-directory-p file)))
10350 (file (if (and dirp org-open-directory-means-index-dot-org)
10351 (concat (file-name-as-directory file) "index.org")
10353 (a-m-a-p (assq 'auto-mode apps))
10354 (dfile (downcase file))
10355 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
10356 (link (cond ((and (eq line nil)
10360 (concat file "::" (number-to-string line)))
10362 (concat file "::" search))))
10363 (dlink (downcase link))
10364 (old-buffer (current-buffer))
10366 (old-mode major-mode)
10367 ext cmd link-match-data)
10368 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
10369 (setq ext (match-string 1 dfile))
10370 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
10371 (setq ext (match-string 1 dfile))))
10373 ((member in-emacs '((16) system))
10374 (setq cmd (cdr (assoc 'system apps))))
10375 (in-emacs (setq cmd 'emacs))
10377 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
10378 (and dirp (cdr (assoc 'directory apps)))
10379 ; first, try matching against apps-dlink
10380 ; if we get a match here, store the match data for later
10381 (let ((match (assoc-default dlink apps-dlink
10384 (progn (setq link-match-data (match-data))
10386 (progn (setq in-emacs (or in-emacs line search))
10387 nil))) ; if we have no match in apps-dlink,
10388 ; always open the file in emacs if line or search
10389 ; is given (for backwards compatibility)
10390 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
10392 (cdr (assoc ext apps))
10393 (cdr (assoc t apps))))))
10394 (when (eq cmd 'system)
10395 (setq cmd (cdr (assoc 'system apps))))
10396 (when (eq cmd 'default)
10397 (setq cmd (cdr (assoc t apps))))
10398 (when (eq cmd 'mailcap)
10400 (mailcap-parse-mailcaps)
10401 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
10402 (command (mailcap-mime-info mime-type)))
10403 (if (stringp command)
10405 (setq cmd 'emacs))))
10406 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
10407 (not (file-exists-p file))
10408 (not org-open-non-existing-files))
10409 (error "No such file: %s" file))
10411 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
10412 ;; Remove quotes around the file name - we'll use shell-quote-argument.
10413 (while (string-match "['\"]%s['\"]" cmd)
10414 (setq cmd (replace-match "%s" t t cmd)))
10415 (while (string-match "%s" cmd)
10416 (setq cmd (replace-match
10418 (shell-quote-argument
10419 (convert-standard-filename file)))
10422 ;; Replace "%1", "%2" etc. in command with group matches from regex
10424 (let ((match-index 1)
10425 (number-of-groups (- (/ (length link-match-data) 2) 1)))
10426 (set-match-data link-match-data)
10427 (while (<= match-index number-of-groups)
10428 (let ((regex (concat "%" (number-to-string match-index)))
10429 (replace-with (match-string match-index dlink)))
10430 (while (string-match regex cmd)
10431 (setq cmd (replace-match replace-with t t cmd))))
10432 (setq match-index (+ match-index 1)))))
10434 (save-window-excursion
10435 (start-process-shell-command cmd nil cmd)
10436 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
10440 (funcall (cdr (assq 'file org-link-frame-setup)) file)
10442 (if line (org-goto-line line)
10443 (if search (org-link-search search))))
10445 (let ((file (convert-standard-filename file)))
10447 (set-match-data link-match-data)
10449 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
10450 (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
10451 (or (not (equal old-buffer (current-buffer)))
10452 (not (equal old-pos (point))))
10453 (org-mark-ring-push old-pos old-buffer))))
10455 (defun org-file-apps-entry-match-against-dlink-p (entry)
10456 "This function returns non-nil if `entry' uses a regular
10457 expression which should be matched against the whole link by
10460 It assumes that is the case when the entry uses a regular
10461 expression which has at least one grouping construct and the
10462 action is either a lisp form or a command string containing
10463 '%1', i.e. using at least one subexpression match as a
10465 (let ((selector (car entry))
10466 (action (cdr entry)))
10467 (if (stringp selector)
10468 (and (> (regexp-opt-depth selector) 0)
10469 (or (and (stringp action)
10470 (string-match "%[0-9]" action))
10474 (defun org-default-apps ()
10475 "Return the default applications for this operating system."
10477 ((eq system-type 'darwin)
10478 org-file-apps-defaults-macosx)
10479 ((eq system-type 'windows-nt)
10480 org-file-apps-defaults-windowsnt)
10481 (t org-file-apps-defaults-gnu)))
10483 (defun org-apps-regexp-alist (list &optional add-auto-mode)
10484 "Convert extensions to regular expressions in the cars of LIST.
10485 Also, weed out any non-string entries, because the return value is used
10486 only for regexp matching.
10487 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
10488 point to the symbol `emacs', indicating that the file should
10489 be opened in Emacs."
10492 (mapcar (lambda (x)
10493 (if (not (stringp (car x)))
10495 (if (string-match "\\W" (car x))
10497 (cons (concat "\\." (car x) "\\'") (cdr x)))))
10500 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
10502 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
10503 (defun org-file-remote-p (file)
10504 "Test whether FILE specifies a location on a remote system.
10505 Return non-nil if the location is indeed remote.
10507 For example, the filename \"/user@host:/foo\" specifies a location
10508 on the system \"/user@host:\"."
10509 (cond ((fboundp 'file-remote-p)
10510 (file-remote-p file))
10511 ((fboundp 'tramp-handle-file-remote-p)
10512 (tramp-handle-file-remote-p file))
10513 ((and (boundp 'ange-ftp-name-format)
10514 (string-match (car ange-ftp-name-format) file))
10520 (defun org-get-org-file ()
10521 "Read a filename, with default directory `org-directory'."
10522 (let ((default (or org-default-notes-file remember-data-file)))
10523 (read-file-name (format "File name [%s]: " default)
10524 (file-name-as-directory org-directory)
10527 (defun org-notes-order-reversed-p ()
10528 "Check if the current file should receive notes in reversed order."
10530 ((not org-reverse-note-order) nil)
10531 ((eq t org-reverse-note-order) t)
10532 ((not (listp org-reverse-note-order)) nil)
10534 (let ((all org-reverse-note-order)
10536 (while (setq entry (pop all))
10537 (if (string-match (car entry) buffer-file-name)
10538 (throw 'exit (cdr entry))))
10541 (defvar org-refile-target-table nil
10542 "The list of refile targets, created by `org-refile'.")
10544 (defvar org-agenda-new-buffers nil
10545 "Buffers created to visit agenda files.")
10547 (defvar org-refile-cache nil
10548 "Cache for refile targets.")
10550 (defvar org-refile-markers nil
10551 "All the markers used for caching refile locations.")
10553 (defun org-refile-marker (pos)
10554 "Get a new refile marker, but only if caching is in use."
10555 (if (not org-refile-use-cache)
10557 (let ((m (make-marker)))
10558 (move-marker m pos)
10559 (push m org-refile-markers)
10562 (defun org-refile-cache-clear ()
10563 "Clear the refile cache and disable all the markers."
10564 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
10565 (setq org-refile-markers nil)
10566 (setq org-refile-cache nil)
10567 (message "Refile cache has been cleared"))
10569 (defun org-refile-cache-check-set (set)
10570 "Check if all the markers in the cache still have live buffers."
10573 (while (and set (setq marker (nth 3 (pop set))))
10574 ;; if org-refile-use-outline-path is 'file, marker may be nil
10575 (when (and marker (null (marker-buffer marker)))
10576 (message "not found") (sit-for 3)
10577 (throw 'exit nil)))
10580 (defun org-refile-cache-put (set &rest identifiers)
10581 "Push the refile targets SET into the cache, under IDENTIFIERS."
10582 (let* ((key (sha1 (prin1-to-string identifiers)))
10583 (entry (assoc key org-refile-cache)))
10586 (push (cons key set) org-refile-cache))))
10588 (defun org-refile-cache-get (&rest identifiers)
10589 "Retrieve the cached value for refile targets given by IDENTIFIERS."
10591 ((not org-refile-cache) nil)
10592 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
10594 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
10595 org-refile-cache))))
10596 (and set (org-refile-cache-check-set set) set)))))
10598 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
10599 "Produce a table with refile targets."
10600 (let ((case-fold-search nil)
10601 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
10602 (entries (or org-refile-targets '((nil . (:level . 1)))))
10603 targets tgs txt re files f desc descre fast-path-p level pos0)
10604 (message "Getting targets...")
10605 (with-current-buffer (or default-buffer (current-buffer))
10606 (while (setq entry (pop entries))
10607 (setq files (car entry) desc (cdr entry))
10608 (setq fast-path-p nil)
10610 ((null files) (setq files (list (current-buffer))))
10611 ((eq files 'org-agenda-files)
10612 (setq files (org-agenda-files 'unrestricted)))
10613 ((and (symbolp files) (fboundp files))
10614 (setq files (funcall files)))
10615 ((and (symbolp files) (boundp files))
10616 (setq files (symbol-value files))))
10617 (if (stringp files) (setq files (list files)))
10619 ((eq (car desc) :tag)
10620 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
10621 ((eq (car desc) :todo)
10622 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
10623 ((eq (car desc) :regexp)
10624 (setq descre (cdr desc)))
10625 ((eq (car desc) :level)
10626 (setq descre (concat "^\\*\\{" (number-to-string
10627 (if org-odd-levels-only
10628 (1- (* 2 (cdr desc)))
10631 ((eq (car desc) :maxlevel)
10632 (setq fast-path-p t)
10633 (setq descre (concat "^\\*\\{1," (number-to-string
10634 (if org-odd-levels-only
10635 (1- (* 2 (cdr desc)))
10638 (t (error "Bad refiling target description %s" desc)))
10639 (while (setq f (pop files))
10640 (with-current-buffer
10641 (if (bufferp f) f (org-get-agenda-file-buffer f))
10643 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
10645 (if (bufferp f) (setq f (buffer-file-name
10646 (buffer-base-buffer f))))
10647 (setq f (and f (expand-file-name f)))
10648 (if (eq org-refile-use-outline-path 'file)
10649 (push (list (file-name-nondirectory f) f nil nil) tgs))
10653 (goto-char (point-min))
10654 (while (re-search-forward descre nil t)
10655 (goto-char (setq pos0 (point-at-bol)))
10657 (when org-refile-target-verify-function
10659 (or (funcall org-refile-target-verify-function)
10661 (when (and (looking-at org-complex-heading-regexp)
10662 (not (member (match-string 4) excluded-entries))
10664 (setq level (org-reduced-level
10665 (- (match-end 1) (match-beginning 1)))
10666 txt (org-link-display-format (match-string 4))
10667 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
10668 re (format org-complex-heading-regexp-format
10669 (regexp-quote (match-string 4))))
10670 (when org-refile-use-outline-path
10671 (setq txt (mapconcat
10674 (if (eq org-refile-use-outline-path
10676 (list (file-name-nondirectory
10678 (buffer-base-buffer))))
10679 (if (eq org-refile-use-outline-path
10681 (list (buffer-file-name
10682 (buffer-base-buffer)))))
10683 (org-get-outline-path fast-path-p
10687 (push (list txt f re (org-refile-marker (point)))
10689 (when (= (point) pos0)
10690 ;; verification function has not moved point
10691 (goto-char (point-at-eol))))))))
10692 (when org-refile-use-cache
10693 (org-refile-cache-put tgs (buffer-file-name) descre))
10694 (setq targets (append tgs targets))
10696 (message "Getting targets...done")
10697 (nreverse targets)))
10699 (defun org-protect-slash (s)
10700 (while (string-match "/" s)
10701 (setq s (replace-match "\\" t t s)))
10704 (defvar org-olpa (make-vector 20 nil))
10706 (defun org-get-outline-path (&optional fastp level heading)
10707 "Return the outline path to the current entry, as a list.
10709 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
10710 routine which makes outline path derivations for an entire file,
10711 avoiding backtracing. Refile target collection makes use of that."
10715 (error "Outline path failure, more than 19 levels"))
10716 (loop for i from level upto 19 do
10717 (aset org-olpa i nil))
10719 (delq nil (append org-olpa nil))
10720 (aset org-olpa level heading)))
10721 (let (rtn case-fold-search)
10725 (while (org-up-heading-safe)
10726 (when (looking-at org-complex-heading-regexp)
10727 (push (org-match-string-no-properties 4) rtn)))
10730 (defun org-format-outline-path (path &optional width prefix)
10731 "Format the outline path PATH for display.
10732 Width is the maximum number of characters that is available.
10733 Prefix is a prefix to be included in the returned string,
10734 such as the file name."
10735 (setq width (or width 79))
10736 (if prefix (setq width (- width (length prefix))))
10739 (let* ((nsteps (length path))
10740 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
10741 (maxwidth (if (<= total-width width)
10742 10000 ;; everything fits
10743 ;; we need to shorten the level headings
10744 (/ (- width nsteps) nsteps)))
10745 (org-odd-levels-only nil)
10747 (total (1+ (length prefix))))
10748 (setq maxwidth (max maxwidth 10))
10753 (if (and (= n nsteps) (< maxwidth 10000))
10754 (setq maxwidth (- total-width total)))
10755 (if (< (length h) maxwidth)
10756 (progn (setq total (+ total (length h) 1)) h)
10757 (setq h (substring h 0 (- maxwidth 2))
10758 total (+ total maxwidth 1))
10759 (if (string-match "[ \t]+\\'" h)
10760 (setq h (substring h 0 (match-beginning 0))))
10761 (setq h (concat h "..")))
10762 (org-add-props h nil 'face
10763 (nth (% (1- n) org-n-level-faces)
10768 (defun org-display-outline-path (&optional file current)
10769 "Display the current outline path in the echo area."
10771 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
10772 (case-fold-search nil)
10773 (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
10774 (if current (setq path (append path
10776 (org-back-to-heading t)
10777 (if (looking-at org-complex-heading-regexp)
10778 (list (match-string 4)))))))
10780 (org-format-outline-path
10783 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
10785 (defvar org-refile-history nil
10786 "History for refiling operations.")
10788 (defvar org-after-refile-insert-hook nil
10789 "Hook run after `org-refile' has inserted its stuff at the new location.
10790 Note that this is still *before* the stuff will be removed from
10791 the *old* location.")
10793 (defvar org-capture-last-stored-marker)
10794 (defun org-refile (&optional goto default-buffer rfloc)
10795 "Move the entry or entries at point to another heading.
10796 The list of target headings is compiled using the information in
10797 `org-refile-targets', which see.
10799 At the target location, the entry is filed as a subitem of the target
10800 heading. Depending on `org-reverse-note-order', the new subitem will
10801 either be the first or the last subitem.
10803 If there is an active region, all entries in that region will be moved.
10804 However, the region must fulfill the requirement that the first heading
10805 is the first one sets the top-level of the moved text - at most siblings
10806 below it are allowed.
10808 With prefix arg GOTO, the command will only visit the target location
10809 and not actually move anything.
10811 With a double prefix arg \\[universal-argument] \\[universal-argument], \
10812 go to the location where the last refiling operation has put the subtree.
10813 With a prefix argument of `2', refile to the running clock.
10815 RFLOC can be a refile location obtained in a different way.
10817 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
10819 If you are using target caching (see `org-refile-use-cache'),
10820 you have to clear the target cache in order to find new targets.
10821 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
10822 prefix argument (`C-u C-u C-u C-c C-w')."
10825 (if (member goto '(0 (64)))
10826 (org-refile-cache-clear)
10827 (let* ((cbuf (current-buffer))
10828 (regionp (org-region-active-p))
10829 (region-start (and regionp (region-beginning)))
10830 (region-end (and regionp (region-end)))
10831 (region-length (and regionp (- region-end region-start)))
10832 (filename (buffer-file-name (buffer-base-buffer cbuf)))
10833 pos it nbuf file re level reversed)
10834 (setq last-command nil)
10836 (goto-char region-start)
10837 (or (bolp) (goto-char (point-at-bol)))
10838 (setq region-start (point))
10839 (unless (or (org-kill-is-subtree-p
10840 (buffer-substring region-start region-end))
10841 (prog1 org-refile-active-region-within-subtree
10842 (org-toggle-heading)))
10843 (error "The region is not a (sequence of) subtree(s)")))
10844 (if (equal goto '(16))
10845 (org-refile-goto-last-stored)
10847 (and (equal goto 2)
10848 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
10850 (setq it (list (or org-clock-heading "running clock")
10852 (marker-buffer org-clock-hd-marker))
10854 (marker-position org-clock-hd-marker)))
10857 (let (heading-text)
10860 (org-back-to-heading t)
10862 (nth 4 (org-heading-components))))
10863 (org-refile-get-location
10864 (cond (goto "Goto")
10865 (regionp "Refile region to")
10866 (t (concat "Refile subtree \""
10867 heading-text "\" to")))
10869 (and (not (equal '(4) goto))
10870 org-refile-allow-creating-parent-nodes)
10872 (setq file (nth 1 it)
10875 (if (and (not goto)
10877 (equal (buffer-file-name) file)
10879 (and (>= pos region-start)
10880 (<= pos region-end))
10881 (and (>= pos (point))
10882 (< pos (save-excursion
10883 (org-end-of-subtree t t))))))
10884 (error "Cannot refile to position inside the tree or region"))
10886 (setq nbuf (or (find-buffer-visiting file)
10887 (find-file-noselect file)))
10890 (org-pop-to-buffer-same-window nbuf)
10892 (org-show-context 'org-goto))
10895 (org-kill-new (buffer-substring region-start region-end))
10896 (org-save-markers-in-region region-start region-end))
10897 (org-copy-subtree 1 nil t))
10898 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
10899 (find-file-noselect file)))
10900 (setq reversed (org-notes-order-reversed-p))
10907 (looking-at org-outline-regexp)
10908 (setq level (org-get-valid-level (funcall outline-level) 1))
10911 (or (outline-next-heading) (point-max))
10912 (or (save-excursion (org-get-next-sibling))
10913 (org-end-of-subtree t t)
10917 (goto-char (point-max))
10918 (goto-char (point-min))
10919 (or (outline-next-heading) (goto-char (point-max)))))
10920 (if (not (bolp)) (newline))
10921 (org-paste-subtree level)
10922 (when org-log-refile
10923 (org-add-log-setup 'refile nil nil 'findpos
10925 (unless (eq org-log-refile 'note)
10926 (save-excursion (org-add-log-note))))
10927 (and org-auto-align-tags
10928 (let ((org-loop-over-headlines-in-active-region nil))
10929 (org-set-tags nil t)))
10930 (with-demoted-errors
10931 (bookmark-set "org-refile-last-stored"))
10932 ;; If we are refiling for capture, make sure that the
10933 ;; last-capture pointers point here
10934 (when (org-bound-and-true-p org-refile-for-capture)
10935 (with-demoted-errors
10936 (bookmark-set "org-capture-last-stored-marker"))
10937 (move-marker org-capture-last-stored-marker (point)))
10938 (if (fboundp 'deactivate-mark) (deactivate-mark))
10939 (run-hooks 'org-after-refile-insert-hook))))
10941 (delete-region (point) (+ (point) region-length))
10943 (when (featurep 'org-inlinetask)
10944 (org-inlinetask-remove-END-maybe))
10945 (setq org-markers-to-move nil)
10946 (message "Refiled to \"%s\" in file %s" (car it) file)))))))
10948 (defun org-refile-goto-last-stored ()
10949 "Go to the location where the last refile was stored."
10951 (bookmark-jump "org-refile-last-stored")
10952 (message "This is the location of the last refile"))
10954 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
10956 "Prompt the user for a refile location, using PROMPT.
10957 PROMPT should not be suffixed with a colon and a space, because
10958 this function appends the default value from
10959 `org-refile-history' automatically, if that is not empty.
10960 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
10961 this is used for the GOTO interface."
10962 (let ((org-refile-targets org-refile-targets)
10963 (org-refile-use-outline-path org-refile-use-outline-path)
10965 (when (and (derived-mode-p 'org-mode)
10966 (not org-refile-use-cache)
10970 (setq excluded-entries
10971 (append excluded-entries (list (org-get-heading t t)))))))
10972 (setq org-refile-target-table
10973 (org-refile-get-targets default-buffer excluded-entries)))
10974 (unless org-refile-target-table
10975 (error "No refile targets"))
10976 (let* ((prompt (concat prompt
10977 (and (car org-refile-history)
10978 (concat " (default " (car org-refile-history) ")"))
10980 (cbuf (current-buffer))
10981 (partial-completion-mode nil)
10982 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
10983 (cfunc (if (and org-refile-use-outline-path
10984 org-outline-path-complete-in-steps)
10985 'org-olpath-completing-read
10986 'org-icompleting-read))
10987 (extra (if org-refile-use-outline-path "/" ""))
10988 (filename (and cfn (expand-file-name cfn)))
10991 (if (and (not (member org-refile-use-outline-path
10992 '(file full-file-path)))
10993 (not (equal filename (nth 1 x))))
10994 (cons (concat (car x) extra " ("
10995 (file-name-nondirectory (nth 1 x)) ")")
10997 (cons (concat (car x) extra) (cdr x))))
10998 org-refile-target-table))
10999 (completion-ignore-case t)
11000 pa answ parent-target child parent old-hist)
11001 (setq old-hist org-refile-history)
11002 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
11003 nil 'org-refile-history (car org-refile-history)))
11004 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
11005 (org-refile-check-position pa)
11008 (when (or (not org-refile-history)
11009 (not (eq old-hist org-refile-history))
11010 (not (equal (car pa) (car org-refile-history))))
11011 (setq org-refile-history
11012 (cons (car pa) (if (assoc (car org-refile-history) tbl)
11014 (cdr org-refile-history))))
11015 (if (equal (car org-refile-history) (nth 1 org-refile-history))
11016 (pop org-refile-history)))
11018 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
11020 (setq parent (match-string 1 answ)
11021 child (match-string 2 answ))
11022 (setq parent-target (or (assoc parent tbl)
11023 (assoc (concat parent "/") tbl)))
11024 (when (and parent-target
11025 (or (eq new-nodes t)
11026 (and (eq new-nodes 'confirm)
11027 (y-or-n-p (format "Create new node \"%s\"? "
11029 (org-refile-new-child parent-target child)))
11030 (error "Invalid target location")))))
11032 (declare-function org-string-nw-p "org-macs" (s))
11033 (defun org-refile-check-position (refile-pointer)
11034 "Check if the refile pointer matches the headline to which it points."
11035 (let* ((file (nth 1 refile-pointer))
11036 (re (nth 2 refile-pointer))
11037 (pos (nth 3 refile-pointer))
11039 (if (and (not (markerp pos)) (not file))
11040 (error "Please save the buffer to a file before refiling")
11041 (when (org-string-nw-p re)
11042 (setq buffer (if (markerp pos)
11043 (marker-buffer pos)
11044 (or (find-buffer-visiting file)
11045 (find-file-noselect file))))
11046 (with-current-buffer buffer
11051 (beginning-of-line 1)
11052 (unless (org-looking-at-p re)
11053 (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
11055 (defun org-refile-new-child (parent-target child)
11056 "Use refile target PARENT-TARGET to add new CHILD below it."
11057 (unless parent-target
11058 (error "Cannot find parent for new node"))
11059 (let ((file (nth 1 parent-target))
11060 (pos (nth 3 parent-target))
11062 (with-current-buffer (or (find-buffer-visiting file)
11063 (find-file-noselect file))
11069 (goto-char (point-max))
11070 (if (not (bolp)) (newline)))
11071 (when (looking-at org-outline-regexp)
11072 (setq level (funcall outline-level))
11073 (org-end-of-subtree t t))
11074 (org-back-over-empty-lines)
11075 (insert "\n" (make-string
11076 (if pos (org-get-valid-level level 1) 1) ?*)
11078 (beginning-of-line 0)
11079 (list (concat (car parent-target) "/" child) file "" (point)))))))
11081 (defun org-olpath-completing-read (prompt collection &rest args)
11082 "Read an outline path like a file name."
11083 (let ((thetable collection)
11084 (org-completion-use-ido nil) ; does not work with ido.
11085 (org-completion-use-iswitchb nil)) ; or iswitchb
11087 'org-icompleting-read prompt
11088 (lambda (string predicate &optional flag)
11089 (let (rtn r f (l (length string)))
11093 (try-completion string thetable))
11096 (setq rtn (all-completions string thetable predicate))
11099 (setq r (substring x l))
11100 (if (string-match " ([^)]*)$" x)
11101 (setq f (match-string 0 x))
11103 (if (string-match "/" r)
11104 (concat string (substring r 0 (match-end 0)) f)
11109 (assoc string thetable)))))
11112 ;;;; Dynamic blocks
11114 (defun org-find-dblock (name)
11115 "Find the first dynamic block with name NAME in the buffer.
11116 If not found, stay at current position and return nil."
11117 (let ((case-fold-search t) pos)
11119 (goto-char (point-min))
11120 (setq pos (and (re-search-forward
11121 (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
11122 (match-beginning 0))))
11123 (if pos (goto-char pos))
11126 (defconst org-dblock-start-re
11127 "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
11128 "Matches the start line of a dynamic block, with parameters.")
11130 (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
11131 "Matches the end of a dynamic block.")
11133 (defun org-create-dblock (plist)
11134 "Create a dynamic block section, with parameters taken from PLIST.
11135 PLIST must contain a :name entry which is used as name of the block."
11136 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
11139 (let ((col (current-column))
11140 (name (plist-get plist :name)))
11141 (insert "#+BEGIN: " name)
11143 (if (eq (car plist) :name)
11144 (setq plist (cddr plist))
11145 (insert " " (prin1-to-string (pop plist)))))
11146 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
11147 (beginning-of-line -2)))
11149 (defun org-prepare-dblock ()
11150 "Prepare dynamic block for refresh.
11151 This empties the block, puts the cursor at the insert position and returns
11152 the property list including an extra property :name with the block name."
11153 (unless (looking-at org-dblock-start-re)
11154 (error "Not at a dynamic block"))
11155 (let* ((begdel (1+ (match-end 0)))
11156 (name (org-no-properties (match-string 1)))
11157 (params (append (list :name name)
11158 (read (concat "(" (match-string 3) ")")))))
11160 (beginning-of-line 1)
11161 (skip-chars-forward " \t")
11162 (setq params (plist-put params :indentation-column (current-column))))
11163 (unless (re-search-forward org-dblock-end-re nil t)
11164 (error "Dynamic block not terminated"))
11167 (list :content (buffer-substring
11168 begdel (match-beginning 0)))))
11169 (delete-region begdel (match-beginning 0))
11174 (defun org-map-dblocks (&optional command)
11175 "Apply COMMAND to all dynamic blocks in the current buffer.
11176 If COMMAND is not given, use `org-update-dblock'."
11177 (let ((cmd (or command 'org-update-dblock)))
11179 (goto-char (point-min))
11180 (while (re-search-forward org-dblock-start-re nil t)
11181 (goto-char (match-beginning 0))
11183 (condition-case nil
11185 (error (message "Error during update of dynamic block"))))
11186 (unless (re-search-forward org-dblock-end-re nil t)
11187 (error "Dynamic block not terminated"))))))
11189 (defun org-dblock-update (&optional arg)
11190 "User command for updating dynamic blocks.
11191 Update the dynamic block at point. With prefix ARG, update all dynamic
11192 blocks in the buffer."
11195 (org-update-all-dblocks)
11196 (or (looking-at org-dblock-start-re)
11197 (org-beginning-of-dblock))
11198 (org-update-dblock)))
11200 (defun org-update-dblock ()
11201 "Update the dynamic block at point.
11202 This means to empty the block, parse for parameters and then call
11203 the correct writing function."
11205 (save-window-excursion
11206 (let* ((pos (point))
11207 (line (org-current-line))
11208 (params (org-prepare-dblock))
11209 (name (plist-get params :name))
11210 (indent (plist-get params :indentation-column))
11211 (cmd (intern (concat "org-dblock-write:" name))))
11212 (message "Updating dynamic block `%s' at line %d..." name line)
11213 (funcall cmd params)
11214 (message "Updating dynamic block `%s' at line %d...done" name line)
11216 (when (and indent (> indent 0))
11217 (setq indent (make-string indent ?\ ))
11219 (org-beginning-of-dblock)
11221 (while (not (looking-at org-dblock-end-re))
11223 (beginning-of-line 2))
11224 (when (looking-at org-dblock-end-re)
11225 (and (looking-at "[ \t]+")
11226 (replace-match ""))
11227 (insert indent)))))))
11229 (defun org-beginning-of-dblock ()
11230 "Find the beginning of the dynamic block at point.
11231 Error if there is no such block at point."
11232 (let ((pos (point))
11235 (if (and (re-search-backward org-dblock-start-re nil t)
11236 (setq beg (match-beginning 0))
11237 (re-search-forward org-dblock-end-re nil t)
11238 (> (match-end 0) pos))
11241 (error "Not in a dynamic block"))))
11243 (defun org-update-all-dblocks ()
11244 "Update all dynamic blocks in the buffer.
11245 This function can be used in a hook."
11247 (when (derived-mode-p 'org-mode)
11248 (org-map-dblocks 'org-update-dblock)))
11253 (defconst org-additional-option-like-keywords
11254 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
11255 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
11256 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
11257 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
11259 "ORGTBL" "TBLFM:" "TBLNAME:"
11260 "BEGIN_EXAMPLE" "END_EXAMPLE"
11261 "BEGIN_VERBATIM" "END_VERBATIM"
11262 "BEGIN_QUOTE" "END_QUOTE"
11263 "BEGIN_VERSE" "END_VERSE"
11264 "BEGIN_CENTER" "END_CENTER"
11265 "BEGIN_SRC" "END_SRC"
11266 "BEGIN_RESULT" "END_RESULT"
11267 "BEGIN_lstlisting" "END_lstlisting"
11269 "HEADER:" "HEADERS:"
11270 "COLUMNS:" "PROPERTY:"
11271 "CAPTION:" "LABEL:"
11273 "INCLUDE:" "INDEX:"
11277 (defconst org-options-keywords
11278 '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
11279 "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
11280 "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
11281 "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
11282 "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
11283 "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
11284 "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
11286 (defconst org-additional-option-like-keywords-for-flyspell
11289 (mapconcat (lambda(k)
11290 (replace-regexp-in-string
11292 (concat k " " (downcase k) " " (upcase k))))
11293 (append org-options-keywords org-additional-option-like-keywords)
11297 (defcustom org-structure-template-alist
11298 '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
11299 "<src lang=\"?\">\n\n</src>")
11300 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
11301 "<example>\n?\n</example>")
11302 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
11303 "<quote>\n?\n</quote>")
11304 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
11305 "<verse>\n?\n</verse>")
11306 ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
11307 "<verbatim>\n?\n</verbatim>")
11308 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
11309 "<center>\n?\n</center>")
11310 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
11311 "<literal style=\"latex\">\n?\n</literal>")
11313 "<literal style=\"latex\">?</literal>")
11314 ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
11315 "<literal style=\"html\">\n?\n</literal>")
11317 "<literal style=\"html\">?</literal>")
11318 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
11322 ("I" "#+INCLUDE: %file ?"
11323 "<include file=%file markup=\"?\">"))
11324 "Structure completion elements.
11325 This is a list of abbreviation keys and values. The value gets inserted
11326 if you type `<' followed by the key and then press the completion key,
11327 usually `M-TAB'. %file will be replaced by a file name after prompting
11328 for the file using completion. The cursor will be placed at the position
11329 of the `?` in the template.
11330 There are two templates for each key, the first uses the original Org syntax,
11331 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
11332 the default when the /org-mtags.el/ module has been loaded. See also the
11333 variable `org-mtags-prefer-muse-templates'."
11334 :group 'org-completion
11336 (string :tag "Key")
11337 (string :tag "Template")
11338 (string :tag "Muse Template")))
11340 (defun org-try-structure-completion ()
11341 "Try to complete a structure template before point.
11342 This looks for strings like \"<e\" on an otherwise empty line and
11344 (let ((l (buffer-substring (point-at-bol) (point)))
11346 (when (and (looking-at "[ \t]*$")
11347 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
11348 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
11349 (org-complete-expand-structure-template (+ -1 (point-at-bol)
11350 (match-beginning 1)) a)
11353 (defun org-complete-expand-structure-template (start cell)
11354 "Expand a structure template."
11355 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
11356 (rpl (nth (if musep 2 1) cell))
11358 (delete-region start (point))
11359 (when (string-match "\\`#\\+" rpl)
11362 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
11363 (setq ind (buffer-substring (point-at-bol) (point))))
11365 (setq start (point))
11366 (if (string-match "%file" rpl)
11367 (setq rpl (replace-match
11371 (abbreviate-file-name (read-file-name "Include file: ")))
11374 (setq rpl (mapconcat 'identity (split-string rpl "\n")
11375 (concat "\n" ind)))
11377 (if (re-search-backward "\\?" start t) (delete-char 1))))
11379 ;;;; TODO, DEADLINE, Comments
11381 (defun org-toggle-comment ()
11382 "Change the COMMENT state of an entry."
11385 (org-back-to-heading)
11386 (let (case-fold-search)
11388 ((looking-at (format org-heading-keyword-regexp-format
11389 org-comment-string))
11390 (goto-char (match-end 1))
11391 (looking-at (concat " +" org-comment-string))
11392 (replace-match "" t t)
11393 (when (eolp) (insert " ")))
11394 ((looking-at org-outline-regexp)
11395 (goto-char (match-end 0))
11396 (insert org-comment-string " "))))))
11398 (defvar org-last-todo-state-is-todo nil
11399 "This is non-nil when the last TODO state change led to a TODO state.
11400 If the last change removed the TODO tag or switched to DONE, then
11403 (defvar org-setting-tags nil) ; dynamically skipped
11405 (defvar org-todo-setup-filter-hook nil
11406 "Hook for functions that pre-filter todo specs.
11407 Each function takes a todo spec and returns either nil or the spec
11408 transformed into canonical form." )
11410 (defvar org-todo-get-default-hook nil
11411 "Hook for functions that get a default item for todo.
11412 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
11413 nil or a string to be used for the todo mark." )
11415 (defvar org-agenda-headline-snapshot-before-repeat)
11417 (defun org-current-effective-time ()
11418 "Return current time adjusted for `org-extend-today-until' variable."
11419 (let* ((ct (org-current-time))
11420 (dct (decode-time ct))
11422 (if (and org-use-effective-time
11423 (< (nth 2 dct) org-extend-today-until))
11424 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
11428 (defun org-todo-yesterday (&optional arg)
11429 "Like `org-todo' but the time of change will be 23:59 of yesterday."
11431 (if (eq major-mode 'org-agenda-mode)
11432 (apply 'org-agenda-todo-yesterday arg)
11433 (let* ((hour (third (decode-time
11434 (org-current-time))))
11435 (org-extend-today-until (1+ hour)))
11438 (defun org-todo (&optional arg)
11439 "Change the TODO state of an item.
11440 The state of an item is given by a keyword at the start of the heading,
11442 *** TODO Write paper
11445 The different keywords are specified in the variable `org-todo-keywords'.
11446 By default the available states are \"TODO\" and \"DONE\".
11447 So for this example: when the item starts with TODO, it is changed to DONE.
11448 When it starts with DONE, the DONE is removed. And when neither TODO nor
11449 DONE are present, add TODO at the beginning of the heading.
11451 With \\[universal-argument] prefix arg, use completion to determine the new \
11453 With numeric prefix arg, switch to that state.
11454 With a double \\[universal-argument] prefix, switch to the next set of TODO \
11455 keywords (nextset).
11456 With a triple \\[universal-argument] prefix, circumvent any state blocking.
11457 With a numeric prefix arg of 0, inhibit note taking for the change.
11459 For calling through lisp, arg is also interpreted in the following way:
11460 'none -> empty state
11461 \"\"(empty string) -> switch to empty state
11462 'done -> switch to DONE
11463 'nextset -> switch to the next set of keywords
11464 'previousset -> switch to the previous set of keywords
11465 \"WAITING\" -> switch to the specified keyword, but only if it
11466 really is a member of `org-todo-keywords'."
11468 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
11469 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
11470 'region-start-level 'region))
11471 org-loop-over-headlines-in-active-region)
11474 org-loop-over-headlines-in-active-region
11475 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
11476 (if (equal arg '(16)) (setq arg 'nextset))
11477 (let ((org-blocker-hook org-blocker-hook)
11480 (when (equal arg '(64))
11481 (setq arg nil org-blocker-hook nil))
11482 (when (and org-blocker-hook
11483 (or org-inhibit-blocking
11484 (org-entry-get nil "NOBLOCKING")))
11485 (setq org-blocker-hook nil))
11488 (org-back-to-heading t)
11489 (when (looking-at (concat "^\\*+ " org-comment-string))
11490 (org-toggle-comment)
11492 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
11493 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
11494 (looking-at "\\(?: *\\|[ \t]*$\\)"))
11495 (let* ((match-data (match-data))
11496 (startpos (point-at-bol))
11497 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
11498 (org-log-done org-log-done)
11499 (org-log-repeat org-log-repeat)
11500 (org-todo-log-states org-todo-log-states)
11501 (org-inhibit-logging
11503 (progn (setq arg nil) 'note) org-inhibit-logging))
11504 (this (match-string 1))
11505 (hl-pos (match-beginning 0))
11506 (head (org-get-todo-sequence-head this))
11507 (ass (assoc head org-todo-kwd-alist))
11508 (interpret (nth 1 ass))
11509 (done-word (nth 3 ass))
11510 (final-done-word (nth 4 ass))
11511 (org-last-state (or this ""))
11512 (completion-ignore-case t)
11513 (member (member this org-todo-keywords-1))
11514 (tail (cdr member))
11516 ((and org-todo-key-trigger
11517 (or (and (equal arg '(4))
11518 (eq org-use-fast-todo-selection 'prefix))
11519 (and (not arg) org-use-fast-todo-selection
11520 (not (eq org-use-fast-todo-selection
11522 ;; Use fast selection
11523 (org-fast-todo-selection))
11524 ((and (equal arg '(4))
11525 (or (not org-use-fast-todo-selection)
11526 (not org-todo-key-trigger)))
11527 ;; Read a state with completion
11528 (org-icompleting-read
11529 "State: " (mapcar (lambda(x) (list x))
11530 org-todo-keywords-1)
11534 (if tail (car tail) nil)
11535 (car org-todo-keywords-1)))
11537 (if (equal member org-todo-keywords-1)
11540 (nth (- (length org-todo-keywords-1)
11542 org-todo-keywords-1)
11543 (org-last org-todo-keywords-1))))
11544 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
11545 (setq arg nil))) ; hack to fall back to cycling
11547 ;; user or caller requests a specific state
11549 ((equal arg "") nil)
11550 ((eq arg 'none) nil)
11551 ((eq arg 'done) (or done-word (car org-done-keywords)))
11553 (or (car (cdr (member head org-todo-heads)))
11554 (car org-todo-heads)))
11555 ((eq arg 'previousset)
11556 (let ((org-todo-heads (reverse org-todo-heads)))
11557 (or (car (cdr (member head org-todo-heads)))
11558 (car org-todo-heads))))
11559 ((car (member arg org-todo-keywords-1)))
11561 (error "State `%s' not valid in this file" arg))
11562 ((nth (1- (prefix-numeric-value arg))
11563 org-todo-keywords-1))))
11564 ((null member) (or head (car org-todo-keywords-1)))
11565 ((equal this final-done-word) nil) ;; -> make empty
11566 ((null tail) nil) ;; -> first entry
11567 ((memq interpret '(type priority))
11568 (if (eq this-command last-command)
11570 (if (> (length tail) 0)
11571 (or done-word (car org-done-keywords))
11576 (run-hook-with-args-until-success
11577 'org-todo-get-default-hook org-state org-last-state)
11579 (next (if org-state (concat " " org-state " ") " "))
11580 (change-plist (list :type 'todo-state-change :from this :to org-state
11581 :position startpos))
11583 (when org-blocker-hook
11584 (setq org-last-todo-state-is-todo
11585 (not (member this org-done-keywords)))
11586 (unless (save-excursion
11588 (org-with-wide-buffer
11589 (run-hook-with-args-until-failure
11590 'org-blocker-hook change-plist))))
11591 (if (org-called-interactively-p 'interactive)
11592 (error "TODO state change from %s to %s blocked" this org-state)
11594 (message "TODO state change from %s to %s blocked" this org-state)
11595 (throw 'exit nil))))
11596 (store-match-data match-data)
11597 (replace-match next t t)
11598 (unless (pos-visible-in-window-p hl-pos)
11599 (message "TODO state changed to %s" (org-trim next)))
11601 (setq head (org-get-todo-sequence-head org-state)
11602 ass (assoc head org-todo-kwd-alist)
11603 interpret (nth 1 ass)
11604 done-word (nth 3 ass)
11605 final-done-word (nth 4 ass)))
11606 (when (memq arg '(nextset previousset))
11607 (message "Keyword-Set %d/%d: %s"
11608 (- (length org-todo-sets) -1
11609 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
11610 (length org-todo-sets)
11611 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
11612 (setq org-last-todo-state-is-todo
11613 (not (member org-state org-done-keywords)))
11614 (setq now-done-p (and (member org-state org-done-keywords)
11615 (not (member this org-done-keywords))))
11616 (and logging (org-local-logging logging))
11617 (when (and (or org-todo-log-states org-log-done)
11618 (not (eq org-inhibit-logging t))
11619 (not (memq arg '(nextset previousset))))
11620 ;; we need to look at recording a time and note
11621 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
11622 (nth 2 (assoc this org-todo-log-states))))
11623 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
11624 (setq dolog 'time))
11625 (when (and org-state
11626 (member org-state org-not-done-keywords)
11627 (not (member this org-not-done-keywords)))
11628 ;; This is now a todo state and was not one before
11629 ;; If there was a CLOSED time stamp, get rid of it.
11630 (org-add-planning-info nil nil 'closed))
11631 (when (and now-done-p org-log-done)
11632 ;; It is now done, and it was not done before
11633 (org-add-planning-info 'closed (org-current-effective-time))
11634 (if (and (not dolog) (eq 'note org-log-done))
11635 (org-add-log-setup 'done org-state this 'findpos 'note)))
11636 (when (and org-state dolog)
11637 ;; This is a non-nil state, and we need to log it
11638 (org-add-log-setup 'state org-state this 'findpos dolog)))
11639 ;; Fixup tag positioning
11640 (org-todo-trigger-tag-changes org-state)
11641 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
11642 (when org-provide-todo-statistics
11643 (org-update-parent-todo-statistics))
11644 (run-hooks 'org-after-todo-state-change-hook)
11645 (if (and arg (not (member org-state org-done-keywords)))
11646 (setq head (org-get-todo-sequence-head org-state)))
11647 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
11648 ;; Do we need to trigger a repeat?
11650 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
11651 ;; This is for the agenda, take a snapshot of the headline.
11653 (setq org-agenda-headline-snapshot-before-repeat
11654 (org-get-heading))))
11655 (org-auto-repeat-maybe org-state))
11656 ;; Fixup cursor location if close to the keyword
11657 (if (and (outline-on-heading-p)
11659 (save-excursion (beginning-of-line 1)
11660 (looking-at org-todo-line-regexp))
11661 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
11663 (goto-char (or (match-end 2) (match-end 1)))
11664 (and (looking-at " ") (just-one-space))))
11665 (when org-trigger-hook
11667 (run-hook-with-args 'org-trigger-hook change-plist)))
11668 (when commentp (org-toggle-comment))))))))
11670 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
11671 "Block turning an entry into a TODO, using the hierarchy.
11672 This checks whether the current task should be blocked from state
11673 changes. Such blocking occurs when:
11675 1. The task has children which are not all in a completed state.
11677 2. A task has a parent with the property :ORDERED:, and there
11678 are siblings prior to the current task with incomplete
11681 3. The parent of the task is blocked because it has siblings that should
11682 be done first, or is child of a block grandparent TODO entry."
11684 (if (not org-enforce-todo-dependencies)
11685 t ; if locally turned off don't block
11687 ;; If this is not a todo state change, or if this entry is already DONE,
11689 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11690 (member (plist-get change-plist :from)
11691 (cons 'done org-done-keywords))
11692 (member (plist-get change-plist :to)
11693 (cons 'todo org-not-done-keywords))
11694 (not (plist-get change-plist :to)))
11695 (throw 'dont-block t))
11696 ;; If this task has children, and any are undone, it's blocked
11698 (org-back-to-heading t)
11699 (let ((this-level (funcall outline-level)))
11700 (outline-next-heading)
11701 (let ((child-level (funcall outline-level)))
11702 (while (and (not (eobp))
11703 (> child-level this-level))
11704 ;; this todo has children, check whether they are all
11706 (if (and (not (org-entry-is-done-p))
11707 (org-entry-is-todo-p))
11708 (throw 'dont-block nil))
11709 (outline-next-heading)
11710 (setq child-level (funcall outline-level))))))
11711 ;; Otherwise, if the task's parent has the :ORDERED: property, and
11712 ;; any previous siblings are undone, it's blocked
11714 (org-back-to-heading t)
11715 (let* ((pos (point))
11716 (parent-pos (and (org-up-heading-safe) (point))))
11717 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11718 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11720 (re-search-forward org-not-done-heading-regexp pos t))
11721 (throw 'dont-block nil)) ; block, there is an older sibling not done.
11722 ;; Search further up the hierarchy, to see if an ancestor is blocked
11724 (goto-char parent-pos)
11725 (if (not (looking-at org-not-done-heading-regexp))
11726 (throw 'dont-block t)) ; do not block, parent is not a TODO
11728 (setq parent-pos (and (org-up-heading-safe) (point)))
11729 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11730 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11732 (re-search-forward org-not-done-heading-regexp pos t))
11733 (throw 'dont-block nil)))))))) ; block, older sibling not done.
11735 (defcustom org-track-ordered-property-with-tag nil
11736 "Should the ORDERED property also be shown as a tag?
11737 The ORDERED property decides if an entry should require subtasks to be
11738 completed in sequence. Since a property is not very visible, setting
11739 this option means that toggling the ORDERED property with the command
11740 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
11741 not relevant for the behavior, but it makes things more visible.
11743 Note that toggling the tag with tags commands will not change the property
11744 and therefore not influence behavior!
11746 This can be t, meaning the tag ORDERED should be used, It can also be a
11747 string to select a different tag for this task."
11750 (const :tag "No tracking" nil)
11751 (const :tag "Track with ORDERED tag" t)
11752 (string :tag "Use other tag")))
11754 (defun org-toggle-ordered-property ()
11755 "Toggle the ORDERED property of the current entry.
11756 For better visibility, you can track the value of this property with a tag.
11757 See variable `org-track-ordered-property-with-tag'."
11759 (let* ((t1 org-track-ordered-property-with-tag)
11760 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
11762 (org-back-to-heading)
11763 (if (org-entry-get nil "ORDERED")
11765 (org-delete-property "ORDERED")
11766 (and tag (org-toggle-tag tag 'off))
11767 (message "Subtasks can be completed in arbitrary order"))
11768 (org-entry-put nil "ORDERED" "t")
11769 (and tag (org-toggle-tag tag 'on))
11770 (message "Subtasks must be completed in sequence")))))
11772 (defvar org-blocked-by-checkboxes) ; dynamically scoped
11773 (defun org-block-todo-from-checkboxes (change-plist)
11774 "Block turning an entry into a TODO, using checkboxes.
11775 This checks whether the current task should be blocked from state
11776 changes because there are unchecked boxes in this entry."
11777 (if (not org-enforce-todo-checkbox-dependencies)
11778 t ; if locally turned off don't block
11780 ;; If this is not a todo state change, or if this entry is already DONE,
11782 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11783 (member (plist-get change-plist :from)
11784 (cons 'done org-done-keywords))
11785 (member (plist-get change-plist :to)
11786 (cons 'todo org-not-done-keywords))
11787 (not (plist-get change-plist :to)))
11788 (throw 'dont-block t))
11789 ;; If this task has checkboxes that are not checked, it's blocked
11791 (org-back-to-heading t)
11792 (let ((beg (point)) end)
11793 (outline-next-heading)
11796 (if (org-list-search-forward
11797 (concat (org-item-beginning-re)
11798 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
11802 (if (boundp 'org-blocked-by-checkboxes)
11803 (setq org-blocked-by-checkboxes t))
11804 (throw 'dont-block nil)))))
11805 t))) ; do not block
11807 (defun org-entry-blocked-p ()
11808 "Is the current entry blocked?"
11809 (org-with-buffer-modified-unmodified
11810 (if (org-entry-get nil "NOBLOCKING")
11811 nil ;; Never block this entry
11813 (run-hook-with-args-until-failure
11815 (list :type 'todo-state-change
11820 (defun org-update-statistics-cookies (all)
11821 "Update the statistics cookie, either from TODO or from checkboxes.
11822 This should be called with the cursor in a line with a statistics cookie."
11826 (org-update-checkbox-count 'all)
11827 (org-map-entries 'org-update-parent-todo-statistics))
11828 (if (not (org-at-heading-p))
11829 (org-update-checkbox-count)
11830 (let ((pos (point-marker))
11832 (ignore-errors (org-back-to-heading t))
11833 (if (not (org-at-heading-p))
11834 (org-update-checkbox-count)
11835 (setq l1 (org-outline-level))
11836 (setq end (save-excursion
11837 (outline-next-heading)
11838 (if (org-at-heading-p) (setq l2 (org-outline-level)))
11840 (if (and (save-excursion
11842 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
11843 (not (save-excursion (re-search-forward
11844 ":COOKIE_DATA:.*\\<todo\\>" end t))))
11845 (org-update-checkbox-count)
11846 (if (and l2 (> l2 l1))
11849 (org-update-parent-todo-statistics))
11851 (beginning-of-line 1)
11852 (while (re-search-forward
11853 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
11855 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
11857 (move-marker pos nil)))))
11859 (defvar org-entry-property-inherited-from) ;; defined below
11860 (defun org-update-parent-todo-statistics ()
11861 "Update any statistics cookie in the parent of the current headline.
11862 When `org-hierarchical-todo-statistics' is nil, statistics will cover
11863 the entire subtree and this will travel up the hierarchy and update
11864 statistics everywhere."
11865 (let* ((prop (save-excursion (org-up-heading-safe)
11866 (org-entry-get nil "COOKIE_DATA" 'inherit)))
11867 (recursive (or (not org-hierarchical-todo-statistics)
11868 (and prop (string-match "\\<recursive\\>" prop))))
11869 (lim (or (and prop (marker-position org-entry-property-inherited-from))
11872 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
11873 level ltoggle l1 new ndel
11874 (cnt-all 0) (cnt-done 0) is-percent kwd
11875 checkbox-beg ov ovs ove cookie-present)
11878 (beginning-of-line 1)
11879 (setq ltoggle (funcall outline-level))
11880 ;; Three situations are to consider:
11882 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
11883 ;; to the top-level ancestor on the headline;
11885 ;; 2. If parent has "recursive" property, repeat up to the
11886 ;; headline setting that property, taking inheritance into
11889 ;; 3. Else, move up to direct parent and proceed only once.
11890 (while (and (setq level (org-up-heading-safe))
11891 (or recursive first)
11893 (setq first nil cookie-present nil)
11897 (downcase (or (org-entry-get nil "COOKIE_DATA")
11900 (while (re-search-forward box-re (point-at-eol) t)
11901 (setq cnt-all 0 cnt-done 0 cookie-present t)
11902 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
11904 (unless (outline-next-heading) (throw 'exit nil))
11905 (while (and (looking-at org-complex-heading-regexp)
11906 (> (setq l1 (length (match-string 1))) level))
11907 (setq kwd (and (or recursive (= l1 ltoggle))
11909 (if (or (eq org-provide-todo-statistics 'all-headlines)
11910 (and (listp org-provide-todo-statistics)
11911 (or (member kwd org-provide-todo-statistics)
11912 (member kwd org-done-keywords))))
11913 (setq cnt-all (1+ cnt-all))
11914 (if (eq org-provide-todo-statistics t)
11915 (and kwd (setq cnt-all (1+ cnt-all)))))
11916 (and (member kwd org-done-keywords)
11917 (setq cnt-done (1+ cnt-done)))
11918 (outline-next-heading)))
11921 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
11922 (format "[%d/%d]" cnt-done cnt-all))
11923 ndel (- (match-end 0) checkbox-beg))
11924 ;; handle overlays when updating cookie from column view
11925 (when (setq ov (car (overlays-at checkbox-beg)))
11926 (setq ovs (overlay-start ov) ove (overlay-end ov))
11927 (delete-overlay ov))
11928 (goto-char checkbox-beg)
11930 (delete-region (point) (+ (point) ndel))
11931 (when org-auto-align-tags (org-fix-tags-on-the-fly))
11932 (when ov (move-overlay ov ovs ove)))
11933 (when cookie-present
11934 (run-hook-with-args 'org-after-todo-statistics-hook
11935 cnt-done (- cnt-all cnt-done))))))
11936 (run-hooks 'org-todo-statistics-hook)))
11938 (defvar org-after-todo-statistics-hook nil
11939 "Hook that is called after a TODO statistics cookie has been updated.
11940 Each function is called with two arguments: the number of not-done entries
11941 and the number of done entries.
11943 For example, the following function, when added to this hook, will switch
11944 an entry to DONE when all children are done, and back to TODO when new
11945 entries are set to a TODO status. Note that this hook is only called
11946 when there is a statistics cookie in the headline!
11948 (defun org-summary-todo (n-done n-not-done)
11949 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
11950 (let (org-log-done org-log-states) ; turn off logging
11951 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
11954 (defvar org-todo-statistics-hook nil
11955 "Hook that is run whenever Org thinks TODO statistics should be updated.
11956 This hook runs even if there is no statistics cookie present, in which case
11957 `org-after-todo-statistics-hook' would not run.")
11959 (defun org-todo-trigger-tag-changes (state)
11960 "Apply the changes defined in `org-todo-state-tags-triggers'."
11961 (let ((l org-todo-state-tags-triggers)
11963 (when (or (not state) (equal state ""))
11964 (setq changes (append changes (cdr (assoc "" l)))))
11965 (when (and (stringp state) (> (length state) 0))
11966 (setq changes (append changes (cdr (assoc state l)))))
11967 (when (member state org-not-done-keywords)
11968 (setq changes (append changes (cdr (assoc 'todo l)))))
11969 (when (member state org-done-keywords)
11970 (setq changes (append changes (cdr (assoc 'done l)))))
11971 (dolist (c changes)
11972 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
11974 (defun org-local-logging (value)
11975 "Get logging settings from a property VALUE."
11977 ;; directly set the variables, they are already local.
11978 (setq org-log-done nil
11980 org-todo-log-states nil)
11981 (setq words (org-split-string value))
11982 (while (setq w (pop words))
11984 ((setq a (assoc w org-startup-options))
11985 (and (member (nth 1 a) '(org-log-done org-log-repeat))
11986 (set (nth 1 a) (nth 2 a))))
11987 ((setq a (org-extract-log-state-settings w))
11988 (and (member (car a) org-todo-keywords-1)
11989 (push a org-todo-log-states)))))))
11991 (defun org-get-todo-sequence-head (kwd)
11992 "Return the head of the TODO sequence to which KWD belongs.
11993 If KWD is not set, check if there is a text property remembering the
11998 (or (get-text-property (point-at-bol) 'org-todo-head)
12000 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
12001 nil (point-at-eol)))
12002 (get-text-property p 'org-todo-head))))
12003 ((not (member kwd org-todo-keywords-1))
12004 (car org-todo-keywords-1))
12005 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
12007 (defun org-fast-todo-selection ()
12008 "Fast TODO keyword selection with single keys.
12009 Returns the new TODO keyword, or nil if no state change should occur."
12010 (let* ((fulltable org-todo-key-alist)
12011 (done-keywords org-done-keywords) ;; needed for the faces.
12012 (maxlen (apply 'max (mapcar
12014 (if (stringp (car x)) (string-width (car x)) 0))
12017 (fwidth (+ maxlen 3 1 3))
12018 (ncol (/ (- (window-width) 4) fwidth))
12022 (save-window-excursion
12024 (set-buffer (get-buffer-create " *Org todo*"))
12025 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
12027 (org-set-local 'org-done-keywords done-keywords)
12028 (setq tbl fulltable cnt 0)
12029 (while (setq e (pop tbl))
12031 ((equal e '(:startgroup))
12032 (push '() groups) (setq ingroup t)
12033 (when (not (= cnt 0))
12037 ((equal e '(:endgroup))
12038 (setq ingroup nil cnt 0)
12040 ((equal e '(:newline))
12041 (when (not (= cnt 0))
12045 (while (equal (car tbl) '(:newline))
12047 (setq tbl (cdr tbl)))))
12049 (setq tg (car e) c (cdr e))
12050 (if ingroup (push tg (car groups)))
12051 (setq tg (org-add-props tg nil 'face
12052 (org-get-todo-face tg)))
12053 (if (and (= cnt 0) (not ingroup)) (insert " "))
12054 (insert "[" c "] " tg (make-string
12055 (- fwidth 4 (length tg)) ?\ ))
12056 (when (= (setq cnt (1+ cnt)) ncol)
12058 (if ingroup (insert " "))
12061 (goto-char (point-min))
12062 (if (not expert) (org-fit-window-to-buffer))
12063 (message "[a-z..]:Set [SPC]:clear")
12064 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
12067 (and (= c ?q) (not (rassoc c fulltable))))
12068 (setq quit-flag t))
12070 ((setq e (rassoc c fulltable) tg (car e))
12072 (t (setq quit-flag t)))))))
12074 (defun org-entry-is-todo-p ()
12075 (member (org-get-todo-state) org-not-done-keywords))
12077 (defun org-entry-is-done-p ()
12078 (member (org-get-todo-state) org-done-keywords))
12080 (defun org-get-todo-state ()
12082 (org-back-to-heading t)
12083 (and (looking-at org-todo-line-regexp)
12085 (match-string 2))))
12087 (defun org-at-date-range-p (&optional inactive-ok)
12088 "Is the cursor inside a date range?"
12092 (let ((pos (point)))
12093 (skip-chars-backward "^[<\r\n")
12094 (skip-chars-backward "<[")
12095 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12096 (>= (match-end 0) pos)
12098 (skip-chars-backward "^<[\r\n")
12099 (skip-chars-backward "<[")
12100 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12101 (>= (match-end 0) pos)
12105 (defun org-get-repeat (&optional tagline)
12106 "Check if there is a deadline/schedule with repeater in this entry."
12109 (org-back-to-heading t)
12110 (and (re-search-forward (if tagline
12111 (concat tagline "\\s-*" org-repeat-re)
12113 (org-entry-end-position) t)
12114 (match-string-no-properties 1)))))
12116 (defvar org-last-changed-timestamp)
12117 (defvar org-last-inserted-timestamp)
12118 (defvar org-log-post-message)
12119 (defvar org-log-note-purpose)
12120 (defvar org-log-note-how)
12121 (defvar org-log-note-extra)
12122 (defun org-auto-repeat-maybe (done-word)
12123 "Check if the current headline contains a repeated deadline/schedule.
12124 If yes, set TODO state back to what it was and change the base date
12125 of repeating deadline/scheduled time stamps to new date.
12126 This function is run automatically after each state change to a DONE state."
12127 ;; last-state is dynamically scoped into this function
12128 (let* ((repeat (org-get-repeat))
12129 (aa (assoc org-last-state org-todo-kwd-alist))
12130 (interpret (nth 1 aa))
12132 (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
12133 (msg "Entry repeats: ")
12135 (org-todo-log-states nil)
12136 re type n what ts time to-state)
12138 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
12139 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
12140 org-todo-repeat-to-state))
12141 (unless (and to-state (member to-state org-todo-keywords-1))
12142 (setq to-state (if (eq interpret 'type) org-last-state head)))
12143 (org-todo to-state)
12144 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
12145 (org-entry-put nil "LAST_REPEAT" (format-time-string
12146 (org-time-stamp-format t t))))
12147 (when org-log-repeat
12148 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
12149 (memq 'org-add-log-note post-command-hook))
12150 ;; OK, we are already setup for some record
12151 (if (eq org-log-repeat 'note)
12152 ;; make sure we take a note, not only a time stamp
12153 (setq org-log-note-how 'note))
12154 ;; Set up for taking a record
12155 (org-add-log-setup 'state (or done-word (car org-done-keywords))
12157 'findpos org-log-repeat)))
12158 (org-back-to-heading t)
12159 (org-add-planning-info nil nil 'closed)
12160 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12161 org-deadline-time-regexp "\\)\\|\\("
12162 org-ts-regexp "\\)"))
12163 (while (re-search-forward
12164 re (save-excursion (outline-next-heading) (point)) t)
12165 (setq type (if (match-end 1) org-scheduled-string
12166 (if (match-end 3) org-deadline-string "Plain:"))
12167 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
12168 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
12169 (setq n (string-to-number (match-string 2 ts))
12170 what (match-string 3 ts))
12171 (if (equal what "w") (setq n (* n 7) what "d"))
12172 (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
12173 (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
12174 ;; Preparation, see if we need to modify the start date for the change
12175 (when (match-end 1)
12176 (setq time (save-match-data (org-time-string-to-time ts)))
12178 ((equal (match-string 1 ts) ".")
12179 ;; Shift starting date to today
12180 (org-timestamp-change
12181 (- (org-today) (time-to-days time))
12183 ((equal (match-string 1 ts) "+")
12184 (let ((nshiftmax 10) (nshift 0))
12185 (while (or (= nshift 0)
12186 (<= (time-to-days time)
12187 (time-to-days (current-time))))
12188 (when (= (incf nshift) nshiftmax)
12189 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
12191 (org-timestamp-change n (cdr (assoc what whata)))
12192 (org-at-timestamp-p t)
12193 (setq ts (match-string 1))
12194 (setq time (save-match-data (org-time-string-to-time ts)))))
12195 (org-timestamp-change (- n) (cdr (assoc what whata)))
12196 ;; rematch, so that we have everything in place for the real shift
12197 (org-at-timestamp-p t)
12198 (setq ts (match-string 1))
12199 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
12200 (org-timestamp-change n (cdr (assoc what whata)))
12201 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
12202 (setq org-log-post-message msg)
12203 (message "%s" msg))))
12205 (defun org-show-todo-tree (arg)
12206 "Make a compact tree which shows all headlines marked with TODO.
12207 The tree will show the lines where the regexp matches, and all higher
12208 headlines above the match.
12209 With a \\[universal-argument] prefix, prompt for a regexp to match.
12210 With a numeric prefix N, construct a sparse tree for the Nth element
12211 of `org-todo-keywords-1'."
12213 (let ((case-fold-search nil)
12215 (cond ((null arg) org-not-done-regexp)
12217 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
12218 (mapcar 'list org-todo-keywords-1))))
12220 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12222 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12223 (regexp-quote (nth (1- (prefix-numeric-value arg))
12224 org-todo-keywords-1)))
12225 (t (error "Invalid prefix argument: %s" arg)))))
12226 (message "%d TODO entries found"
12227 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
12229 (defun org-deadline (&optional remove time)
12230 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
12231 With argument REMOVE, remove any deadline from the item.
12232 With argument TIME, set the deadline at the corresponding date. TIME
12233 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12235 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12236 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12237 'region-start-level 'region))
12238 org-loop-over-headlines-in-active-region)
12240 `(org-deadline ',remove ,time)
12241 org-loop-over-headlines-in-active-region
12242 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12243 (let* ((old-date (org-entry-get nil "DEADLINE"))
12244 (repeater (and old-date
12246 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12248 (match-string 1 old-date))))
12251 (when (and old-date org-log-redeadline)
12252 (org-add-log-setup 'deldeadline nil old-date 'findpos
12253 org-log-redeadline))
12254 (org-remove-timestamp-with-keyword org-deadline-string)
12255 (message "Item no longer has a deadline."))
12256 (org-add-planning-info 'deadline time 'closed)
12257 (when (and old-date org-log-redeadline
12258 (not (equal old-date
12259 (substring org-last-inserted-timestamp 1 -1))))
12260 (org-add-log-setup 'redeadline nil old-date 'findpos
12261 org-log-redeadline))
12264 (org-back-to-heading t)
12265 (when (re-search-forward (concat org-deadline-string " "
12266 org-last-inserted-timestamp)
12268 (outline-next-heading) (point)) t)
12269 (goto-char (1- (match-end 0)))
12270 (insert " " repeater)
12271 (setq org-last-inserted-timestamp
12272 (concat (substring org-last-inserted-timestamp 0 -1)
12274 (substring org-last-inserted-timestamp -1))))))
12275 (message "Deadline on %s" org-last-inserted-timestamp)))))
12277 (defun org-schedule (&optional remove time)
12278 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
12279 With argument REMOVE, remove any scheduling date from the item.
12280 With argument TIME, scheduled at the corresponding date. TIME can
12281 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12283 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12284 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12285 'region-start-level 'region))
12286 org-loop-over-headlines-in-active-region)
12288 `(org-schedule ',remove ,time)
12289 org-loop-over-headlines-in-active-region
12290 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12291 (let* ((old-date (org-entry-get nil "SCHEDULED"))
12292 (repeater (and old-date
12294 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12296 (match-string 1 old-date))))
12299 (when (and old-date org-log-reschedule)
12300 (org-add-log-setup 'delschedule nil old-date 'findpos
12301 org-log-reschedule))
12302 (org-remove-timestamp-with-keyword org-scheduled-string)
12303 (message "Item is no longer scheduled."))
12304 (org-add-planning-info 'scheduled time 'closed)
12305 (when (and old-date org-log-reschedule
12306 (not (equal old-date
12307 (substring org-last-inserted-timestamp 1 -1))))
12308 (org-add-log-setup 'reschedule nil old-date 'findpos
12309 org-log-reschedule))
12312 (org-back-to-heading t)
12313 (when (re-search-forward (concat org-scheduled-string " "
12314 org-last-inserted-timestamp)
12316 (outline-next-heading) (point)) t)
12317 (goto-char (1- (match-end 0)))
12318 (insert " " repeater)
12319 (setq org-last-inserted-timestamp
12320 (concat (substring org-last-inserted-timestamp 0 -1)
12322 (substring org-last-inserted-timestamp -1))))))
12323 (message "Scheduled to %s" org-last-inserted-timestamp)))))
12325 (defun org-get-scheduled-time (pom &optional inherit)
12326 "Get the scheduled time as a time tuple, of a format suitable
12327 for calling org-schedule with, or if there is no scheduling,
12329 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
12331 (apply 'encode-time (org-parse-time-string time)))))
12333 (defun org-get-deadline-time (pom &optional inherit)
12334 "Get the deadline as a time tuple, of a format suitable for
12335 calling org-deadline with, or if there is no scheduling, returns
12337 (let ((time (org-entry-get pom "DEADLINE" inherit)))
12339 (apply 'encode-time (org-parse-time-string time)))))
12341 (defun org-remove-timestamp-with-keyword (keyword)
12342 "Remove all time stamps with KEYWORD in the current entry."
12343 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
12346 (org-back-to-heading t)
12348 (outline-next-heading)
12349 (while (re-search-backward re beg t)
12351 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
12352 (equal (char-before) ?\ ))
12353 (backward-delete-char 1)
12354 (if (string-match "^[ \t]*$" (buffer-substring
12355 (point-at-bol) (point-at-eol)))
12356 (delete-region (point-at-bol)
12357 (min (point-max) (1+ (point-at-eol))))))))))
12359 (defun org-add-planning-info (what &optional time &rest remove)
12360 "Insert new timestamp with keyword in the line directly after the headline.
12361 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
12362 If non is given, the user is prompted for a date.
12363 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
12366 (let (org-time-was-given org-end-time-was-given ts
12367 end default-time default-input)
12370 (when (and (memq what '(scheduled deadline))
12372 (and (stringp time)
12373 (string-match "^[-+]+[0-9]" time))))
12374 ;; Try to get a default date/time from existing timestamp
12376 (org-back-to-heading t)
12377 (setq end (save-excursion (outline-next-heading) (point)))
12378 (when (re-search-forward (if (eq what 'scheduled)
12379 org-scheduled-time-regexp
12380 org-deadline-time-regexp)
12382 (setq ts (match-string 1)
12384 (apply 'encode-time (org-parse-time-string ts))
12385 default-input (and ts (org-get-compact-tod ts))))))
12389 ;; This is a string (relative or absolute), set proper date
12390 (apply 'encode-time
12391 (org-read-date-analyze
12392 time default-time (decode-time default-time)))
12393 ;; If necessary, get the time from the user
12394 (or time (org-read-date nil 'to-time nil nil
12395 default-time default-input)))))
12397 (when (and org-insert-labeled-timestamps-at-point
12398 (member what '(scheduled deadline)))
12400 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
12401 (org-insert-time-stamp time org-time-was-given
12402 nil nil nil (list org-end-time-was-given))
12406 (let (col list elt ts buffer-invisibility-spec)
12407 (org-back-to-heading t)
12408 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
12409 (goto-char (match-end 1))
12410 (setq col (current-column))
12411 (goto-char (match-end 0))
12412 (if (eobp) (insert "\n") (forward-char 1))
12413 (when (and (not what)
12416 org-keyword-time-not-clock-regexp))))
12417 ;; Nothing to add, nothing to remove...... :-)
12419 (if (and (not (looking-at org-outline-regexp))
12420 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
12422 (not (equal (match-string 1) org-clock-string)))
12423 (narrow-to-region (match-beginning 0) (match-end 0))
12424 (insert-before-markers "\n")
12426 (narrow-to-region (point) (point))
12427 (and org-adapt-indentation (org-indent-to-column col)))
12428 ;; Check if we have to remove something.
12429 (setq list (cons what remove))
12431 (setq elt (pop list))
12432 (when (or (and (eq elt 'scheduled)
12433 (re-search-forward org-scheduled-time-regexp nil t))
12434 (and (eq elt 'deadline)
12435 (re-search-forward org-deadline-time-regexp nil t))
12436 (and (eq elt 'closed)
12437 (re-search-forward org-closed-time-regexp nil t)))
12439 (if (looking-at "--+<[^>]+>") (replace-match ""))))
12440 (and (looking-at "[ \t]+") (replace-match ""))
12441 (and org-adapt-indentation (bolp) (org-indent-to-column col))
12444 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
12445 (cond ((eq what 'scheduled) org-scheduled-string)
12446 ((eq what 'deadline) org-deadline-string)
12447 ((eq what 'closed) org-closed-string))
12449 (setq ts (org-insert-time-stamp
12451 (or org-time-was-given
12452 (and (eq what 'closed) org-log-done-with-time))
12454 nil nil (list org-end-time-was-given)))
12456 (if (not (or (bolp) (eq (char-before) ?\ )
12457 (memq (char-after) '(32 10))
12460 (goto-char (point-min))
12462 (if (and (looking-at "[ \t]*\n")
12463 (equal (char-before) ?\n))
12464 (delete-region (1- (point)) (point-at-eol)))
12467 (defvar org-log-note-marker (make-marker))
12468 (defvar org-log-note-purpose nil)
12469 (defvar org-log-note-state nil)
12470 (defvar org-log-note-previous-state nil)
12471 (defvar org-log-note-how nil)
12472 (defvar org-log-note-extra nil)
12473 (defvar org-log-note-window-configuration nil)
12474 (defvar org-log-note-return-to (make-marker))
12475 (defvar org-log-note-effective-time nil
12476 "Remembered current time so that dynamically scoped
12477 `org-extend-today-until' affects tha timestamps in state change
12480 (defvar org-log-post-message nil
12481 "Message to be displayed after a log note has been stored.
12482 The auto-repeater uses this.")
12484 (defun org-add-note ()
12485 "Add a note to the current entry.
12486 This is done in the same way as adding a state change note."
12488 (org-add-log-setup 'note nil nil 'findpos nil))
12490 (defvar org-property-end-re)
12491 (defun org-add-log-setup (&optional purpose state prev-state
12493 "Set up the post command hook to take a note.
12494 If this is about to TODO state change, the new state is expected in STATE.
12495 When FINDPOS is non-nil, find the correct position for the note in
12496 the current entry. If not, assume that it can be inserted at point.
12497 HOW is an indicator what kind of note should be created.
12498 EXTRA is additional text that will be inserted into the notes buffer."
12499 (let* ((org-log-into-drawer (org-log-into-drawer))
12500 (drawer (cond ((stringp org-log-into-drawer)
12501 org-log-into-drawer)
12502 (org-log-into-drawer "LOGBOOK"))))
12506 (org-back-to-heading t)
12507 (narrow-to-region (point) (save-excursion
12508 (outline-next-heading) (point)))
12509 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
12510 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
12512 (goto-char (match-end 0))
12515 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
12518 (goto-char (match-end 0))
12519 (or org-log-states-order-reversed
12520 (and (re-search-forward org-property-end-re nil t)
12521 (goto-char (1- (match-beginning 0))))))
12522 (insert "\n:" drawer ":\n:END:")
12523 (beginning-of-line 0)
12525 (beginning-of-line 2)
12528 ((and org-log-state-notes-insert-after-drawers
12530 (forward-line) (looking-at org-drawer-regexp)))
12532 (while (looking-at org-drawer-regexp)
12533 (goto-char (match-end 0))
12534 (re-search-forward org-property-end-re (point-max) t)
12536 (forward-line -1)))
12537 (unless org-log-states-order-reversed
12538 (and (= (char-after) ?\n) (forward-char 1))
12539 (org-skip-over-state-notes)
12540 (skip-chars-backward " \t\n\r")))
12541 (move-marker org-log-note-marker (point))
12542 (setq org-log-note-purpose purpose
12543 org-log-note-state state
12544 org-log-note-previous-state prev-state
12545 org-log-note-how how
12546 org-log-note-extra extra
12547 org-log-note-effective-time (org-current-effective-time))
12548 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
12550 (defun org-skip-over-state-notes ()
12551 "Skip past the list of State notes in an entry."
12552 (if (looking-at "\n[ \t]*- State") (forward-char 1))
12553 (when (ignore-errors (goto-char (org-in-item-p)))
12554 (let* ((struct (org-list-struct))
12555 (prevs (org-list-prevs-alist struct)))
12556 (while (looking-at "[ \t]*- State")
12557 (goto-char (or (org-list-get-next-item (point) struct prevs)
12558 (org-list-get-item-end (point) struct)))))))
12560 (defun org-add-log-note (&optional purpose)
12561 "Pop up a window for taking a note, and add this note later at point."
12562 (remove-hook 'post-command-hook 'org-add-log-note)
12563 (setq org-log-note-window-configuration (current-window-configuration))
12564 (delete-other-windows)
12565 (move-marker org-log-note-return-to (point))
12566 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
12567 (goto-char org-log-note-marker)
12568 (org-switch-to-buffer-other-window "*Org Note*")
12570 (if (memq org-log-note-how '(time state))
12571 (let (current-prefix-arg) (org-store-log-note))
12572 (let ((org-inhibit-startup t)) (org-mode))
12573 (insert (format "# Insert note for %s.
12574 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
12576 ((eq org-log-note-purpose 'clock-out) "stopped clock")
12577 ((eq org-log-note-purpose 'done) "closed todo item")
12578 ((eq org-log-note-purpose 'state)
12579 (format "state change from \"%s\" to \"%s\""
12580 (or org-log-note-previous-state "")
12581 (or org-log-note-state "")))
12582 ((eq org-log-note-purpose 'reschedule)
12584 ((eq org-log-note-purpose 'delschedule)
12585 "no longer scheduled")
12586 ((eq org-log-note-purpose 'redeadline)
12587 "changing deadline")
12588 ((eq org-log-note-purpose 'deldeadline)
12589 "removing deadline")
12590 ((eq org-log-note-purpose 'refile)
12592 ((eq org-log-note-purpose 'note)
12594 (t (error "This should not happen")))))
12595 (if org-log-note-extra (insert org-log-note-extra))
12596 (org-set-local 'org-finish-function 'org-store-log-note)
12597 (run-hooks 'org-log-buffer-setup-hook)))
12599 (defvar org-note-abort nil) ; dynamically scoped
12600 (defun org-store-log-note ()
12601 "Finish taking a log note, and insert it to where it belongs."
12602 (let ((txt (buffer-string))
12603 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
12605 (kill-buffer (current-buffer))
12606 (while (string-match "\\`# .*\n[ \t\n]*" txt)
12607 (setq txt (replace-match "" t t txt)))
12608 (if (string-match "\\s-+\\'" txt)
12609 (setq txt (replace-match "" t t txt)))
12610 (setq lines (org-split-string txt "\n"))
12611 (when (and note (string-match "\\S-" note))
12613 (org-replace-escapes
12615 (list (cons "%u" (user-login-name))
12616 (cons "%U" user-full-name)
12617 (cons "%t" (format-time-string
12618 (org-time-stamp-format 'long 'inactive)
12619 org-log-note-effective-time))
12620 (cons "%T" (format-time-string
12621 (org-time-stamp-format 'long nil)
12622 org-log-note-effective-time))
12623 (cons "%d" (format-time-string
12624 (org-time-stamp-format nil 'inactive)
12625 org-log-note-effective-time))
12626 (cons "%D" (format-time-string
12627 (org-time-stamp-format nil nil)
12628 org-log-note-effective-time))
12629 (cons "%s" (if org-log-note-state
12630 (concat "\"" org-log-note-state "\"")
12632 (cons "%S" (if org-log-note-previous-state
12633 (concat "\"" org-log-note-previous-state "\"")
12635 (if lines (setq note (concat note " \\\\")))
12637 (when (or current-prefix-arg org-note-abort)
12638 (when org-log-into-drawer
12639 (org-remove-empty-drawer-at
12640 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
12641 org-log-note-marker))
12644 (with-current-buffer (marker-buffer org-log-note-marker)
12646 (goto-char org-log-note-marker)
12647 (move-marker org-log-note-marker nil)
12649 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
12650 (setq ind (save-excursion
12651 (if (ignore-errors (goto-char (org-in-item-p)))
12652 (let ((struct (org-list-struct)))
12654 (org-list-get-top-point struct) struct))
12655 (skip-chars-backward " \r\t\n")
12657 ((and (org-at-heading-p)
12658 org-adapt-indentation)
12659 (1+ (org-current-level)))
12660 ((org-at-heading-p) 0)
12661 (t (org-get-indentation))))))
12662 (setq bul (org-list-bullet-string "-"))
12663 (org-indent-line-to ind)
12664 (insert bul (pop lines))
12665 (let ((ind-body (+ (length bul) ind)))
12668 (org-indent-line-to ind-body)
12669 (insert (pop lines))))
12670 (message "Note stored")
12671 (org-back-to-heading t)
12672 (org-cycle-hide-drawers 'children)))))
12673 (set-window-configuration org-log-note-window-configuration)
12674 (with-current-buffer (marker-buffer org-log-note-return-to)
12675 (goto-char org-log-note-return-to))
12676 (move-marker org-log-note-return-to nil)
12677 (and org-log-post-message (message "%s" org-log-post-message)))
12679 (defun org-remove-empty-drawer-at (drawer pos)
12680 "Remove an empty drawer DRAWER at position POS.
12681 POS may also be a marker."
12682 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
12688 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
12689 (replace-match ""))))))
12691 (defvar org-ts-type nil)
12692 (defun org-sparse-tree (&optional arg type)
12693 "Create a sparse tree, prompt for the details.
12694 This command can create sparse trees. You first need to select the type
12695 of match used to create the tree:
12697 t Show all TODO entries.
12698 T Show entries with a specific TODO keyword.
12699 m Show entries selected by a tags/property match.
12700 p Enter a property name and its value (both with completion on existing
12701 names/values) and show entries with that property.
12702 r Show entries matching a regular expression (`/' can be used as well).
12703 b Show deadlines and scheduled items before a date.
12704 a Show deadlines and scheduled items after a date.
12705 d Show deadlines due within `org-deadline-warning-days'.
12706 D Show deadlines and scheduled items between a date range."
12708 (let (ans kwd value ts-type)
12709 (setq type (or type org-sparse-tree-default-date-type))
12710 (setq org-ts-type type)
12711 (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"
12712 (cond ((eq type 'all) "all timestamps")
12713 ((eq type 'scheduled) "only scheduled")
12714 ((eq type 'deadline) "only deadline")
12715 ((eq type 'active) "only active timestamps")
12716 ((eq type 'inactive) "only inactive timestamps")
12717 ((eq type 'scheduled-or-deadline) "scheduled/deadline")
12718 (t "scheduled/deadline")))
12719 (setq ans (read-char-exclusive))
12722 (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
12724 (call-interactively 'org-check-deadlines))
12726 (call-interactively 'org-check-before-date))
12728 (call-interactively 'org-check-after-date))
12730 (call-interactively 'org-check-dates-range))
12732 (call-interactively 'org-show-todo-tree))
12734 (org-show-todo-tree '(4)))
12735 ((member ans '(?T ?m))
12736 (call-interactively 'org-match-sparse-tree))
12737 ((member ans '(?p ?P))
12738 (setq kwd (org-icompleting-read "Property: "
12739 (mapcar 'list (org-buffer-property-keys))))
12740 (setq value (org-icompleting-read "Value: "
12741 (mapcar 'list (org-property-values kwd))))
12742 (unless (string-match "\\`{.*}\\'" value)
12743 (setq value (concat "\"" value "\"")))
12744 (org-match-sparse-tree arg (concat kwd "=" value)))
12745 ((member ans '(?r ?R ?/))
12746 (call-interactively 'org-occur))
12747 (t (error "No such sparse tree command \"%c\"" ans)))))
12749 (defvar org-occur-highlights nil
12750 "List of overlays used for occur matches.")
12751 (make-variable-buffer-local 'org-occur-highlights)
12752 (defvar org-occur-parameters nil
12753 "Parameters of the active org-occur calls.
12754 This is a list, each call to org-occur pushes as cons cell,
12755 containing the regular expression and the callback, onto the list.
12756 The list can contain several entries if `org-occur' has been called
12757 several time with the KEEP-PREVIOUS argument. Otherwise, this list
12758 will only contain one set of parameters. When the highlights are
12759 removed (for example with `C-c C-c', or with the next edit (depending
12760 on `org-remove-highlights-with-change'), this variable is emptied
12762 (make-variable-buffer-local 'org-occur-parameters)
12764 (defun org-occur (regexp &optional keep-previous callback)
12765 "Make a compact tree which shows all matches of REGEXP.
12766 The tree will show the lines where the regexp matches, and all higher
12767 headlines above the match. It will also show the heading after the match,
12768 to make sure editing the matching entry is easy.
12769 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
12770 call to `org-occur' will be kept, to allow stacking of calls to this
12772 If CALLBACK is non-nil, it is a function which is called to confirm
12773 that the match should indeed be shown."
12774 (interactive "sRegexp: \nP")
12775 (when (equal regexp "")
12776 (error "Regexp cannot be empty"))
12777 (unless keep-previous
12778 (org-remove-occur-highlights nil nil t))
12779 (push (cons regexp callback) org-occur-parameters)
12782 (goto-char (point-min))
12783 (if (or (not keep-previous) ; do not want to keep
12784 (not org-occur-highlights)) ; no previous matches
12787 (while (re-search-forward regexp nil t)
12788 (when (or (not callback)
12789 (save-match-data (funcall callback)))
12790 (setq cnt (1+ cnt))
12791 (when org-highlight-sparse-tree-matches
12792 (org-highlight-new-match (match-beginning 0) (match-end 0)))
12793 (org-show-context 'occur-tree))))
12794 (when org-remove-highlights-with-change
12795 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
12797 (unless org-sparse-tree-open-archived-trees
12798 (org-hide-archived-subtrees (point-min) (point-max)))
12799 (run-hooks 'org-occur-hook)
12800 (if (org-called-interactively-p 'interactive)
12801 (message "%d match(es) for regexp %s" cnt regexp))
12804 (defun org-occur-next-match (&optional n reset)
12805 "Function for `next-error-function' to find sparse tree matches.
12806 N is the number of matches to move, when negative move backwards.
12807 RESET is entirely ignored - this function always goes back to the
12808 starting point when no match is found."
12809 (let* ((limit (if (< n 0) (point-min) (point-max)))
12810 (search-func (if (< n 0)
12811 'previous-single-char-property-change
12812 'next-single-char-property-change))
12817 (while (setq p1 (funcall search-func (point) 'org-type))
12818 (when (equal p1 limit)
12820 (error "No more matches"))
12821 (when (equal (get-char-property p1 'org-type) 'org-occur)
12825 (throw 'exit (point))))
12828 (error "No more matches"))))
12830 (defun org-show-context (&optional key)
12831 "Make sure point and context are visible.
12832 How much context is shown depends upon the variables
12833 `org-show-hierarchy-above', `org-show-following-heading',
12834 `org-show-entry-below' and `org-show-siblings'."
12835 (let ((heading-p (org-at-heading-p t))
12836 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
12837 (following-p (org-get-alist-option org-show-following-heading key))
12838 (entry-p (org-get-alist-option org-show-entry-below key))
12839 (siblings-p (org-get-alist-option org-show-siblings key)))
12841 ;; Show heading or entry text
12842 (if (and heading-p (not entry-p))
12843 (org-flag-heading nil) ; only show the heading
12844 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
12845 (org-show-hidden-entry))) ; show entire entry
12847 ;; Show next sibling, or heading below text
12849 (and (if heading-p (org-goto-sibling) (outline-next-heading))
12850 (org-flag-heading nil))))
12851 (when siblings-p (org-show-siblings))
12853 ;; show all higher headings, possibly with siblings
12855 (while (and (condition-case nil
12856 (progn (org-up-heading-all 1) t)
12859 (org-flag-heading nil)
12860 (when siblings-p (org-show-siblings))))))))
12862 (defvar org-reveal-start-hook nil
12863 "Hook run before revealing a location.")
12865 (defun org-reveal (&optional siblings)
12866 "Show current entry, hierarchy above it, and the following headline.
12867 This can be used to show a consistent set of context around locations
12868 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
12869 not t for the search context.
12871 With optional argument SIBLINGS, on each level of the hierarchy all
12872 siblings are shown. This repairs the tree structure to what it would
12873 look like when opened with hierarchical calls to `org-cycle'.
12874 With double optional argument \\[universal-argument] \\[universal-argument], \
12875 go to the parent and show the
12878 (run-hooks 'org-reveal-start-hook)
12879 (let ((org-show-hierarchy-above t)
12880 (org-show-following-heading t)
12881 (org-show-siblings (if siblings t org-show-siblings)))
12882 (org-show-context nil))
12883 (when (equal siblings '(16))
12885 (when (org-up-heading-safe)
12887 (run-hook-with-args 'org-cycle-hook 'subtree)))))
12889 (defun org-highlight-new-match (beg end)
12890 "Highlight from BEG to END and mark the highlight is an occur headline."
12891 (let ((ov (make-overlay beg end)))
12892 (overlay-put ov 'face 'secondary-selection)
12893 (overlay-put ov 'org-type 'org-occur)
12894 (push ov org-occur-highlights)))
12896 (defun org-remove-occur-highlights (&optional beg end noremove)
12897 "Remove the occur highlights from the buffer.
12898 BEG and END are ignored. If NOREMOVE is nil, remove this function
12899 from the `before-change-functions' in the current buffer."
12901 (unless org-inhibit-highlight-removal
12902 (mapc 'delete-overlay org-occur-highlights)
12903 (setq org-occur-highlights nil)
12904 (setq org-occur-parameters nil)
12906 (remove-hook 'before-change-functions
12907 'org-remove-occur-highlights 'local))))
12911 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
12912 "Regular expression matching the priority indicator.")
12914 (defvar org-remove-priority-next-time nil)
12916 (defun org-priority-up ()
12917 "Increase the priority of the current item."
12919 (org-priority 'up))
12921 (defun org-priority-down ()
12922 "Decrease the priority of the current item."
12924 (org-priority 'down))
12926 (defun org-priority (&optional action show)
12927 "Change the priority of an item.
12928 ACTION can be `set', `up', `down', or a character."
12930 (if (equal action '(4))
12931 (org-show-priority)
12932 (unless org-enable-priority-commands
12933 (error "Priority commands are disabled"))
12934 (setq action (or action 'set))
12935 (let (current new news have remove)
12937 (org-back-to-heading t)
12938 (if (looking-at org-priority-regexp)
12939 (setq current (string-to-char (match-string 2))
12942 ((eq action 'remove)
12943 (setq remove t new ?\ ))
12944 ((or (eq action 'set)
12945 (if (featurep 'xemacs) (characterp action) (integerp action)))
12946 (if (not (eq action 'set))
12948 (message "Priority %c-%c, SPC to remove: "
12949 org-highest-priority org-lowest-priority)
12951 (setq new (read-char-exclusive))))
12952 (if (and (= (upcase org-highest-priority) org-highest-priority)
12953 (= (upcase org-lowest-priority) org-lowest-priority))
12954 (setq new (upcase new)))
12955 (cond ((equal new ?\ ) (setq remove t))
12956 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
12957 (error "Priority must be between `%c' and `%c'"
12958 org-highest-priority org-lowest-priority))))
12961 (1- current) ; normal cycling
12962 ;; last priority was empty
12963 (if (eq last-command this-command)
12964 org-lowest-priority ; wrap around empty to lowest
12966 (if org-priority-start-cycle-with-default
12967 org-default-priority
12968 (1- org-default-priority))))))
12971 (1+ current) ; normal cycling
12972 ;; last priority was empty
12973 (if (eq last-command this-command)
12974 org-highest-priority ; wrap around empty to highest
12976 (if org-priority-start-cycle-with-default
12977 org-default-priority
12978 (1+ org-default-priority))))))
12979 (t (error "Invalid action")))
12980 (if (or (< (upcase new) org-highest-priority)
12981 (> (upcase new) org-lowest-priority))
12982 (if (and (memq action '(up down))
12983 (not have) (not (eq last-command this-command)))
12984 ;; `new' is from default priority
12986 "The default can not be set, see `org-default-priority' why")
12987 ;; normal cycling: `new' is beyond highest/lowest priority
12988 ;; and is wrapped around to the empty priority
12990 (setq news (format "%c" new))
12993 (replace-match "" t t nil 1)
12994 (replace-match news t t nil 2))
12996 (error "No priority cookie found in line")
12997 (let ((case-fold-search nil))
12998 (looking-at org-todo-line-regexp))
13001 (goto-char (match-end 2))
13002 (insert " [#" news "]"))
13003 (goto-char (match-beginning 3))
13004 (insert "[#" news "] "))))
13005 (org-preserve-lc (org-set-tags nil 'align)))
13007 (message "Priority removed")
13008 (message "Priority of current item set to %s" news)))))
13010 (defun org-show-priority ()
13011 "Show the priority of the current item.
13012 This priority is composed of the main priority given with the [#A] cookies,
13013 and by additional input from the age of a schedules or deadline entry."
13015 (let ((pri (if (eq major-mode 'org-agenda-mode)
13016 (org-get-at-bol 'priority)
13019 (beginning-of-line)
13020 (and (looking-at org-heading-regexp)
13021 (org-get-priority (match-string 0))))))))
13022 (message "Priority is %d" (if pri pri -1000))))
13024 (defun org-get-priority (s)
13025 "Find priority cookie and return priority."
13027 (if (functionp org-get-priority-function)
13028 (funcall org-get-priority-function)
13029 (if (not (string-match org-priority-regexp s))
13030 (* 1000 (- org-lowest-priority org-default-priority))
13031 (* 1000 (- org-lowest-priority
13032 (string-to-char (match-string 2 s))))))))
13036 (defvar org-agenda-archives-mode)
13037 (defvar org-map-continue-from nil
13038 "Position from where mapping should continue.
13039 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
13041 (defvar org-scanner-tags nil
13042 "The current tag list while the tags scanner is running.")
13043 (defvar org-trust-scanner-tags nil
13044 "Should `org-get-tags-at' use the tags for the scanner.
13045 This is for internal dynamical scoping only.
13046 When this is non-nil, the function `org-get-tags-at' will return the value
13047 of `org-scanner-tags' instead of building the list by itself. This
13048 can lead to large speed-ups when the tags scanner is used in a file with
13049 many entries, and when the list of tags is retrieved, for example to
13050 obtain a list of properties. Building the tags list for each entry in such
13051 a file becomes an N^2 operation - but with this variable set, it scales
13054 (defun org-scan-tags (action matcher todo-only &optional start-level)
13055 "Scan headline tags with inheritance and produce output ACTION.
13057 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
13058 or `agenda' to produce an entry list for an agenda view. It can also be
13059 a Lisp form or a function that should be called at each matched headline, in
13060 this case the return value is a list of all return values from these calls.
13062 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
13063 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
13064 only lines with a not-done TODO keyword are included in the output.
13065 This should be the same variable that was scoped into
13066 and set by `org-make-tags-matcher' when it constructed MATCHER.
13068 START-LEVEL can be a string with asterisks, reducing the scope to
13069 headlines matching this string."
13070 (require 'org-agenda)
13071 (let* ((re (concat "^"
13073 ;; Get the correct level to match
13074 (concat "\\*\\{" (number-to-string start-level) "\\} ")
13075 org-outline-regexp)
13077 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
13078 (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
13079 (props (list 'face 'default
13080 'done-face 'org-agenda-done
13081 'undone-face 'default
13082 'mouse-face 'highlight
13083 'org-not-done-regexp org-not-done-regexp
13084 'org-todo-regexp org-todo-regexp
13085 'org-complex-heading-regexp org-complex-heading-regexp
13087 (format "mouse-2 or RET jump to org file %s"
13088 (abbreviate-file-name
13089 (or (buffer-file-name (buffer-base-buffer))
13090 (buffer-name (buffer-base-buffer)))))))
13091 (case-fold-search nil)
13092 (org-map-continue-from nil)
13093 lspos tags tags-list
13094 (tags-alist (list (cons 0 org-file-tags)))
13095 (llast 0) rtn rtn1 level category i txt
13096 todo marker entry priority)
13097 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
13098 (setq action (list 'lambda nil action)))
13100 (goto-char (point-min))
13101 (when (eq action 'sparse-tree)
13103 (org-remove-occur-highlights))
13104 (while (re-search-forward re nil t)
13105 (setq org-map-continue-from nil)
13107 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
13108 tags (if (match-end 4) (org-match-string-no-properties 4)))
13109 (goto-char (setq lspos (match-beginning 0)))
13110 (setq level (org-reduced-level (funcall outline-level))
13111 category (org-get-category))
13112 (setq i llast llast level)
13113 ;; remove tag lists from same and sublevels
13114 (while (>= i level)
13115 (when (setq entry (assoc i tags-alist))
13116 (setq tags-alist (delete entry tags-alist)))
13118 ;; add the next tags
13120 (setq tags (org-split-string tags ":")
13122 (cons (cons level tags) tags-alist)))
13123 ;; compile tags for current headline
13125 (if org-use-tag-inheritance
13126 (apply 'append (mapcar 'cdr (reverse tags-alist)))
13128 org-scanner-tags tags-list)
13129 (when org-use-tag-inheritance
13130 (setcdr (car tags-alist)
13131 (mapcar (lambda (x)
13132 (setq x (copy-sequence x))
13133 (org-add-prop-inherited x))
13134 (cdar tags-alist))))
13135 (when (and tags org-use-tag-inheritance
13136 (or (not (eq t org-use-tag-inheritance))
13137 org-tags-exclude-from-inheritance))
13138 ;; selective inheritance, remove uninherited ones
13139 (setcdr (car tags-alist)
13140 (org-remove-uninherited-tags (cdar tags-alist))))
13143 ;; eval matcher only when the todo condition is OK
13144 (and (or (not todo-only) (member todo org-not-done-keywords))
13145 (let ((case-fold-search t) (org-trust-scanner-tags t))
13148 ;; Call the skipper, but return t if it does not skip,
13149 ;; so that the `and' form continues evaluating
13151 (unless (eq action 'sparse-tree) (org-agenda-skip))
13154 ;; Check if timestamps are deselecting this entry
13155 (or (not todo-only)
13156 (and (member todo org-not-done-keywords)
13157 (or (not org-agenda-tags-todo-honor-ignore-options)
13158 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
13160 ;; select this headline
13162 ((eq action 'sparse-tree)
13163 (and org-highlight-sparse-tree-matches
13164 (org-get-heading) (match-end 0)
13165 (org-highlight-new-match
13166 (match-beginning 1) (match-end 1)))
13167 (org-show-context 'tags-tree))
13168 ((eq action 'agenda)
13169 (setq txt (org-agenda-format-item
13172 (if (eq org-tags-match-list-sublevels 'indented)
13173 (make-string (1- level) ?.) "")
13177 priority (org-get-priority txt))
13179 (setq marker (org-agenda-new-marker))
13180 (org-add-props txt props
13181 'org-marker marker 'org-hd-marker marker 'org-category category
13183 'priority priority 'type "tagsmatch")
13185 ((functionp action)
13186 (setq org-map-continue-from nil)
13188 (setq rtn1 (funcall action))
13190 (t (error "Invalid action")))
13192 ;; if we are to skip sublevels, jump to end of subtree
13193 (unless org-tags-match-list-sublevels
13194 (org-end-of-subtree t)
13195 (backward-char 1))))
13196 ;; Get the correct position from where to continue
13197 (if org-map-continue-from
13198 (goto-char org-map-continue-from)
13199 (and (= (point) lspos) (end-of-line 1)))))
13200 (when (and (eq action 'sparse-tree)
13201 (not org-sparse-tree-open-archived-trees))
13202 (org-hide-archived-subtrees (point-min) (point-max)))
13205 (defun org-remove-uninherited-tags (tags)
13206 "Remove all tags that are not inherited from the list TAGS."
13208 ((eq org-use-tag-inheritance t)
13209 (if org-tags-exclude-from-inheritance
13210 (org-delete-all org-tags-exclude-from-inheritance tags)
13212 ((not org-use-tag-inheritance) nil)
13213 ((stringp org-use-tag-inheritance)
13216 (if (and (string-match org-use-tag-inheritance x)
13217 (not (member x org-tags-exclude-from-inheritance)))
13220 ((listp org-use-tag-inheritance)
13223 (if (member x org-use-tag-inheritance) x nil))
13226 (defun org-match-sparse-tree (&optional todo-only match)
13227 "Create a sparse tree according to tags string MATCH.
13228 MATCH can contain positive and negative selection of tags, like
13229 \"+WORK+URGENT-WITHBOSS\".
13230 If optional argument TODO-ONLY is non-nil, only select lines that are
13233 (org-agenda-prepare-buffers (list (current-buffer)))
13234 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
13236 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
13238 (defvar org-cached-props nil)
13239 (defun org-cached-entry-get (pom property)
13240 (if (or (eq t org-use-property-inheritance)
13241 (and (stringp org-use-property-inheritance)
13242 (string-match org-use-property-inheritance property))
13243 (and (listp org-use-property-inheritance)
13244 (member property org-use-property-inheritance)))
13245 ;; Caching is not possible, check it directly
13246 (org-entry-get pom property 'inherit)
13247 ;; Get all properties, so that we can do complicated checks easily
13248 (cdr (assoc property (or org-cached-props
13249 (setq org-cached-props
13250 (org-entry-properties pom)))))))
13252 (defun org-global-tags-completion-table (&optional files)
13253 "Return the list of all tags in all agenda buffer/files.
13254 Optional FILES argument is a list of files which can be used
13255 instead of the agenda files."
13262 (set-buffer (find-file-noselect file))
13263 (append (org-get-buffer-tags)
13264 (mapcar (lambda (x) (if (stringp (car-safe x))
13265 (list (car-safe x)) nil))
13267 (if (and files (car files))
13269 (org-agenda-files))))))))
13271 (defun org-make-tags-matcher (match)
13272 "Create the TAGS/TODO matcher form for the selection string MATCH.
13274 The variable `todo-only' is scoped dynamically into this function.
13275 It will be set to t if the matcher restricts matching to TODO entries,
13276 otherwise will not be touched.
13278 Returns a cons of the selection string MATCH and the constructed
13279 lisp form implementing the matcher. The matcher is to be evaluated
13280 at an Org entry, with point on the headline, and returns t if the
13281 entry matches the selection string MATCH. The returned lisp form
13282 references two variables with information about the entry, which
13283 must be bound around the form's evaluation: todo, the TODO keyword
13284 at the entry (or nil of none); and tags-list, the list of all tags
13285 at the entry including inherited ones. Additionally, the category
13286 of the entry (if any) must be specified as the text property
13287 'org-category on the headline.
13289 See also `org-scan-tags'.
13291 (declare (special todo-only))
13292 (unless (boundp 'todo-only)
13293 (error "org-make-tags-matcher expects todo-only to be scoped in"))
13295 ;; Get a new match request, with completion
13296 (let ((org-last-tags-completion-table
13297 (org-global-tags-completion-table)))
13298 (setq match (org-completing-read-no-i
13299 "Match: " 'org-tags-completion-function nil nil nil
13300 'org-tags-history))))
13302 ;; Parse the string and create a lisp form
13303 (let ((match0 match)
13304 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
13306 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
13307 orterms term orlist re-p str-p level-p level-op time-p
13308 prop-p pn pv po gv rest)
13309 (if (string-match "/+" match)
13310 ;; match contains also a todo-matching request
13312 (setq tagsmatch (substring match 0 (match-beginning 0))
13313 todomatch (substring match (match-end 0)))
13314 (if (string-match "^!" todomatch)
13315 (setq todo-only t todomatch (substring todomatch 1)))
13316 (if (string-match "^\\s-*$" todomatch)
13317 (setq todomatch nil)))
13318 ;; only matching tags
13319 (setq tagsmatch match todomatch nil))
13321 ;; Make the tags matcher
13322 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
13323 (setq tagsmatcher t)
13324 (setq orterms (org-split-string tagsmatch "|") orlist nil)
13325 (while (setq term (pop orterms))
13326 (while (and (equal (substring term -1) "\\") orterms)
13327 (setq term (concat term "|" (pop orterms)))) ; repair bad split
13328 (while (string-match re term)
13329 (setq rest (substring term (match-end 0))
13330 minus (and (match-end 1)
13331 (equal (match-string 1 term) "-"))
13332 tag (save-match-data (replace-regexp-in-string
13334 (match-string 2 term)))
13335 re-p (equal (string-to-char tag) ?{)
13336 level-p (match-end 4)
13337 prop-p (match-end 5)
13339 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
13341 (setq level-op (org-op-to-function (match-string 3 term)))
13342 `(,level-op level ,(string-to-number
13343 (match-string 4 term))))
13345 (setq pn (match-string 5 term)
13346 po (match-string 6 term)
13347 pv (match-string 7 term)
13348 re-p (equal (string-to-char pv) ?{)
13349 str-p (equal (string-to-char pv) ?\")
13350 time-p (save-match-data
13351 (string-match "^\"[[<].*[]>]\"$" pv))
13352 pv (if (or re-p str-p) (substring pv 1 -1) pv))
13353 (if time-p (setq pv (org-matcher-time pv)))
13354 (setq po (org-op-to-function po (if time-p 'time str-p)))
13356 ((equal pn "CATEGORY")
13357 (setq gv '(get-text-property (point) 'org-category)))
13361 (setq gv `(org-cached-entry-get nil ,pn))))
13364 `(not (string-match ,pv (or ,gv "")))
13365 `(string-match ,pv (or ,gv "")))
13367 `(,po (or ,gv "") ,pv)
13368 `(,po (string-to-number (or ,gv ""))
13369 ,(string-to-number pv) ))))
13370 (t `(member ,tag tags-list)))
13371 mm (if minus (list 'not mm) mm)
13373 (push mm tagsmatcher))
13374 (push (if (> (length tagsmatcher) 1)
13375 (cons 'and tagsmatcher)
13378 (setq tagsmatcher nil))
13379 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
13381 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
13382 ;; Make the todo matcher
13383 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
13384 (setq todomatcher t)
13385 (setq orterms (org-split-string todomatch "|") orlist nil)
13386 (while (setq term (pop orterms))
13387 (while (string-match re term)
13388 (setq minus (and (match-end 1)
13389 (equal (match-string 1 term) "-"))
13390 kwd (match-string 2 term)
13391 re-p (equal (string-to-char kwd) ?{)
13392 term (substring term (match-end 0))
13394 `(string-match ,(substring kwd 1 -1) todo)
13395 (list 'equal 'todo kwd))
13396 mm (if minus (list 'not mm) mm))
13397 (push mm todomatcher))
13398 (push (if (> (length todomatcher) 1)
13399 (cons 'and todomatcher)
13402 (setq todomatcher nil))
13403 (setq todomatcher (if (> (length orlist) 1)
13404 (cons 'or orlist) (car orlist))))
13406 ;; Return the string and lisp forms of the matcher
13407 (setq matcher (if todomatcher
13408 (list 'and tagsmatcher todomatcher)
13411 (setq matcher (list 'and '(member todo org-not-done-keywords)
13413 (cons match0 matcher)))
13415 (defun org-op-to-function (op &optional stringp)
13416 "Turn an operator into the appropriate function."
13419 ((equal op "<" ) '(< string< org-time<))
13420 ((equal op ">" ) '(> org-string> org-time>))
13421 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
13422 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
13423 ((member op '("=" "==")) '(= string= org-time=))
13424 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
13425 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
13427 (defun org<> (a b) (not (= a b)))
13428 (defun org-string<= (a b) (or (string= a b) (string< a b)))
13429 (defun org-string>= (a b) (not (string< a b)))
13430 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
13431 (defun org-string<> (a b) (not (string= a b)))
13432 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
13433 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
13434 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
13435 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
13436 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
13437 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
13439 "Convert S to a floating point time.
13440 If S is already a number, just return it. If it is a string, parse
13441 it as a time string and apply `float-time' to it. If S is nil, just return 0."
13445 (condition-case nil
13446 (float-time (apply 'encode-time (org-parse-time-string s)))
13450 (defun org-time-today ()
13451 "Time in seconds today at 0:00.
13452 Returns the float number of seconds since the beginning of the
13453 epoch to the beginning of today (00:00)."
13454 (float-time (apply 'encode-time
13455 (append '(0 0 0) (nthcdr 3 (decode-time))))))
13457 (defun org-matcher-time (s)
13458 "Interpret a time comparison value."
13461 ((string= s "<now>") (float-time))
13462 ((string= s "<today>") (org-time-today))
13463 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
13464 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
13465 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
13466 (+ (org-time-today)
13467 (* (string-to-number (match-string 1 s))
13468 (cdr (assoc (match-string 2 s)
13469 '(("d" . 86400.0) ("w" . 604800.0)
13470 ("m" . 2678400.0) ("y" . 31557600.0)))))))
13473 (defun org-match-any-p (re list)
13474 "Does re match any element of list?"
13475 (setq list (mapcar (lambda (x) (string-match re x)) list))
13478 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
13479 (defvar org-tags-overlay (make-overlay 1 1))
13480 (org-detach-overlay org-tags-overlay)
13482 (defun org-get-local-tags-at (&optional pos)
13483 "Get a list of tags defined in the current headline."
13484 (org-get-tags-at pos 'local))
13486 (defun org-get-local-tags ()
13487 "Get a list of tags defined in the current headline."
13488 (org-get-tags-at nil 'local))
13490 (defun org-get-tags-at (&optional pos local)
13491 "Get a list of all headline tags applicable at POS.
13492 POS defaults to point. If tags are inherited, the list contains
13493 the targets in the same sequence as the headlines appear, i.e.
13494 the tags of the current headline come last.
13495 When LOCAL is non-nil, only return tags from the current headline,
13496 ignore inherited ones."
13498 (if (and org-trust-scanner-tags
13499 (or (not pos) (equal pos (point)))
13502 (let (tags ltags lastpos parent)
13506 (goto-char (or pos (point)))
13509 (condition-case nil
13511 (org-back-to-heading t)
13512 (while (not (equal lastpos (point)))
13513 (setq lastpos (point))
13515 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
13516 (setq ltags (org-split-string
13517 (org-match-string-no-properties 1) ":"))
13519 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
13522 (org-remove-uninherited-tags ltags)
13525 (or org-use-tag-inheritance (throw 'done t))
13526 (if local (throw 'done t))
13527 (or (org-up-heading-safe) (error nil))
13532 (reverse (delete-dups
13534 (org-remove-uninherited-tags
13535 org-file-tags) tags)))))))))
13537 (defun org-add-prop-inherited (s)
13538 (add-text-properties 0 (length s) '(inherited t) s)
13541 (defun org-toggle-tag (tag &optional onoff)
13542 "Toggle the tag TAG for the current line.
13543 If ONOFF is `on' or `off', don't toggle but set to this state."
13546 (org-back-to-heading t)
13547 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
13550 (setq current (match-string 1))
13551 (replace-match ""))
13553 (setq current (nreverse (org-split-string current ":")))
13557 (or (member tag current) (push tag current)))
13559 (or (not (member tag current)) (setq current (delete tag current))))
13560 (t (if (member tag current)
13561 (setq current (delete tag current))
13563 (push tag current))))
13567 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
13568 (org-set-tags nil t))
13569 (delete-horizontal-space))
13570 (run-hooks 'org-after-tags-change-hook))
13573 (defun org-align-tags-here (to-col)
13574 ;; Assumes that this is a headline
13575 (let ((pos (point)) (col (current-column)) ncol tags-l p)
13576 (beginning-of-line 1)
13577 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13578 (< pos (match-beginning 2)))
13580 (setq tags-l (- (match-end 2) (match-beginning 2)))
13581 (goto-char (match-beginning 1))
13583 (delete-region (point) (1+ (match-beginning 2)))
13584 (setq ncol (max (current-column)
13588 (- (abs to-col) tags-l))))
13590 (insert (make-string (- ncol (current-column)) ?\ ))
13591 (setq ncol (current-column))
13592 (when indent-tabs-mode (tabify p (point-at-eol)))
13593 (org-move-to-column (min ncol col) t))
13596 (defun org-set-tags-command (&optional arg just-align)
13597 "Call the set-tags command for the current entry."
13599 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
13600 (org-set-tags arg just-align)
13602 (org-back-to-heading t)
13603 (org-set-tags arg just-align))))
13605 (defun org-set-tags-to (data)
13606 "Set the tags of the current entry to DATA, replacing the current tags.
13607 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
13608 If DATA is nil or the empty string, any tags will be removed."
13609 (interactive "sTags: ")
13613 ((equal data "") "")
13615 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
13618 (concat ":" (mapconcat 'identity data ":") ":"))))
13621 (org-back-to-heading t)
13622 (when (looking-at org-complex-heading-regexp)
13625 (goto-char (match-beginning 5))
13627 (delete-region (point) (point-at-eol))
13628 (org-set-tags nil 'align))
13629 (goto-char (point-at-eol))
13631 (org-set-tags nil 'align)))
13632 (beginning-of-line 1)
13633 (if (looking-at ".*?\\([ \t]+\\)$")
13634 (delete-region (match-beginning 1) (match-end 1))))))
13636 (defun org-align-all-tags ()
13637 "Align the tags i all headings."
13640 (or (ignore-errors (org-back-to-heading t))
13641 (outline-next-heading))
13642 (if (org-at-heading-p)
13644 (message "No headings"))))
13646 (defvar org-indent-indentation-per-level)
13647 (defun org-set-tags (&optional arg just-align)
13648 "Set the tags for the current headline.
13649 With prefix ARG, realign all tags in headings in the current buffer."
13651 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13652 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
13653 'region-start-level 'region))
13654 org-loop-over-headlines-in-active-region)
13656 ;; We don't use ARG and JUST-ALIGN here these args are not
13657 ;; useful when looping over headlines
13659 org-loop-over-headlines-in-active-region
13660 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
13661 (let* ((re org-outline-regexp-bol)
13662 (current (unless arg (org-get-tags-string)))
13663 (col (current-column))
13664 (org-setting-tags t)
13665 table current-tags inherited-tags ; computed below when needed
13666 tags p0 c0 c1 rpl di tc level)
13669 (goto-char (point-min))
13670 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
13671 (while (re-search-forward re nil t)
13672 (org-set-tags nil t)
13674 (message "All tags realigned to column %d" org-tags-column))
13676 (setq tags current)
13677 ;; Get a new set of tags from the user
13679 (setq table (append org-tag-persistent-alist
13680 (or org-tag-alist (org-get-buffer-tags))
13682 org-complete-tags-always-offer-all-agenda-tags
13683 (org-global-tags-completion-table
13684 (org-agenda-files))))
13685 org-last-tags-completion-table table
13686 current-tags (org-split-string current ":")
13687 inherited-tags (nreverse
13688 (nthcdr (length current-tags)
13689 (nreverse (org-get-tags-at))))
13691 (if (or (eq t org-use-fast-tag-selection)
13692 (and org-use-fast-tag-selection
13693 (delq nil (mapcar 'cdr table))))
13694 (org-fast-tag-selection
13695 current-tags inherited-tags table
13696 (if org-fast-tag-selection-include-todo
13697 org-todo-key-alist))
13698 (let ((org-add-colon-after-tag-completion (< 1 (length table))))
13700 (org-icompleting-read "Tags: "
13701 'org-tags-completion-function
13702 nil nil current 'org-tags-history))))))
13703 (while (string-match "[-+&]+" tags)
13704 ;; No boolean logic, just a list
13705 (setq tags (replace-match ":" t t tags))))
13707 (setq tags (replace-regexp-in-string "[,]" ":" tags))
13709 (if org-tags-sort-function
13710 (setq tags (mapconcat 'identity
13711 (sort (org-split-string
13712 tags (org-re "[^[:alnum:]_@#%]+"))
13713 org-tags-sort-function) ":")))
13715 (if (string-match "\\`[\t ]*\\'" tags)
13717 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
13718 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
13720 ;; Insert new tags at the correct column
13721 (beginning-of-line 1)
13722 (setq level (or (and (looking-at org-outline-regexp)
13723 (- (match-end 0) (point) 1))
13726 ((and (equal current "") (equal tags "")))
13727 ((re-search-forward
13728 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
13730 (if (equal tags "")
13732 (goto-char (match-beginning 0))
13733 (setq c0 (current-column)
13734 ;; compute offset for the case of org-indent-mode active
13735 di (if org-indent-mode
13736 (* (1- org-indent-indentation-per-level) (1- level))
13738 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
13739 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
13740 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
13741 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
13742 (replace-match rpl t t)
13743 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
13745 (t (error "Tags alignment failed")))
13746 (org-move-to-column col)
13748 (run-hooks 'org-after-tags-change-hook))))))
13750 (defun org-change-tag-in-region (beg end tag off)
13751 "Add or remove TAG for each entry in the region.
13752 This works in the agenda, and also in an org-mode buffer."
13754 (list (region-beginning) (region-end)
13755 (let ((org-last-tags-completion-table
13756 (if (derived-mode-p 'org-mode)
13757 (org-get-buffer-tags)
13758 (org-global-tags-completion-table))))
13759 (org-icompleting-read
13760 "Tag: " 'org-tags-completion-function nil nil nil
13761 'org-tags-history))
13763 (message "[s]et or
[r]emove? ")
13764 (equal (read-char-exclusive) ?r))))
13765 (if (fboundp 'deactivate-mark) (deactivate-mark))
13766 (let ((agendap (equal major-mode 'org-agenda-mode))
13767 l1 l2 m buf pos newhead (cnt 0))
13769 (setq l2 (1- (org-current-line)))
13771 (setq l1 (org-current-line))
13772 (loop for l from l1 to l2 do
13774 (setq m (get-text-property (point) 'org-hd-marker))
13775 (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
13777 (setq buf (if agendap (marker-buffer m) (current-buffer))
13778 pos (if agendap m (point)))
13779 (with-current-buffer buf
13783 (setq cnt (1+ cnt))
13784 (org-toggle-tag tag (if off 'off 'on))
13785 (setq newhead (org-get-heading)))))
13786 (and agendap (org-agenda-change-all-lines newhead m))))
13787 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
13789 (defun org-tags-completion-function (string predicate &optional flag)
13790 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
13791 (confirm (lambda (x) (stringp (car x)))))
13792 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
13793 (setq s1 (match-string 1 string)
13794 s2 (match-string 2 string))
13795 (setq s1 "" s2 string))
13799 (setq rtn (try-completion s2 ctable confirm))
13802 (concat s1 s2 (substring rtn (length s2))
13803 (if (and org-add-colon-after-tag-completion
13804 (assoc rtn ctable))
13809 (all-completions s2 ctable confirm)
13813 (assoc s2 ctable)))
13816 (defun org-fast-tag-insert (kwd tags face &optional end)
13817 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
13818 (insert (format "%-12s" (concat kwd ":"))
13819 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
13822 (defun org-fast-tag-show-exit (flag)
13825 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
13826 (replace-match ""))
13829 (org-move-to-column (- (window-width) 19) t)
13830 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
13832 (defun org-set-current-tags-overlay (current prefix)
13833 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
13834 (if (featurep 'xemacs)
13835 (org-overlay-display org-tags-overlay (concat prefix s)
13836 'secondary-selection)
13837 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
13838 (org-overlay-display org-tags-overlay (concat prefix s)))))
13840 (defvar org-last-tag-selection-key nil)
13841 (defun org-fast-tag-selection (current inherited table &optional todo-table)
13842 "Fast tag selection with single keys.
13843 CURRENT is the current list of tags in the headline, INHERITED is the
13844 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
13845 possibly with grouping information. TODO-TABLE is a similar table with
13846 TODO keywords, should these have keys assigned to them.
13847 If the keys are nil, a-z are automatically assigned.
13848 Returns the new tags string, or nil to not change the current settings."
13849 (let* ((fulltable (append table todo-table))
13850 (maxlen (apply 'max (mapcar
13852 (if (stringp (car x)) (string-width (car x)) 0))
13854 (buf (current-buffer))
13855 (expert (eq org-fast-tag-selection-single-key 'expert))
13857 (fwidth (+ maxlen 3 1 3))
13858 (ncol (/ (- (window-width) 4) fwidth))
13861 tg cnt e c char c1 c2 ntable tbl rtn
13862 ov-start ov-end ov-prefix
13863 (exit-after-next org-fast-tag-selection-single-key)
13864 (done-keywords org-done-keywords)
13867 (beginning-of-line 1)
13869 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13870 (setq ov-start (match-beginning 1)
13871 ov-end (match-end 1)
13873 (setq ov-start (1- (point-at-eol))
13874 ov-end (1+ ov-start))
13875 (skip-chars-forward "^\n\r")
13878 (buffer-substring (1- (point)) (point))
13879 (if (> (current-column) org-tags-column)
13881 (make-string (- org-tags-column (current-column)) ?\ ))))))
13882 (move-overlay org-tags-overlay ov-start ov-end)
13883 (save-window-excursion
13885 (set-buffer (get-buffer-create " *Org tags*"))
13886 (delete-other-windows)
13887 (split-window-vertically)
13888 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
13890 (org-set-local 'org-done-keywords done-keywords)
13891 (org-fast-tag-insert "Inherited" inherited i-face "\n")
13892 (org-fast-tag-insert "Current" current c-face "\n\n")
13893 (org-fast-tag-show-exit exit-after-next)
13894 (org-set-current-tags-overlay current ov-prefix)
13895 (setq tbl fulltable char ?a cnt 0)
13896 (while (setq e (pop tbl))
13898 ((equal (car e) :startgroup)
13899 (push '() groups) (setq ingroup t)
13900 (when (not (= cnt 0))
13903 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
13904 ((equal (car e) :endgroup)
13905 (setq ingroup nil cnt 0)
13906 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
13907 ((equal e '(:newline))
13908 (when (not (= cnt 0))
13912 (while (equal (car tbl) '(:newline))
13914 (setq tbl (cdr tbl)))))
13916 (setq tg (copy-sequence (car e)) c2 nil)
13919 ;; automatically assign a character.
13920 (setq c1 (string-to-char
13921 (downcase (substring
13922 tg (if (= (string-to-char tg) ?@) 1 0)))))
13923 (if (or (rassoc c1 ntable) (rassoc c1 table))
13924 (while (or (rassoc char ntable) (rassoc char table))
13925 (setq char (1+ char)))
13927 (setq c (or c2 char)))
13928 (if ingroup (push tg (car groups)))
13929 (setq tg (org-add-props tg nil 'face
13931 ((not (assoc tg table))
13932 (org-get-todo-face tg))
13933 ((member tg current) c-face)
13934 ((member tg inherited) i-face))))
13935 (if (and (= cnt 0) (not ingroup)) (insert " "))
13936 (insert "[" c "] " tg (make-string
13937 (- fwidth 4 (length tg)) ?\ ))
13938 (push (cons tg c) ntable)
13939 (when (= (setq cnt (1+ cnt)) ncol)
13941 (if ingroup (insert " "))
13943 (setq ntable (nreverse ntable))
13945 (goto-char (point-min))
13946 (if (not expert) (org-fit-window-to-buffer))
13950 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
13951 (if (not groups) "no " "")
13952 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
13953 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13954 (setq org-last-tag-selection-key c)
13956 ((= c ?\r) (throw 'exit t))
13958 (setq groups (not groups))
13959 (goto-char (point-min))
13960 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
13963 (org-fast-tag-show-exit
13964 (setq exit-after-next (not exit-after-next)))
13966 (delete-other-windows)
13967 (set-window-buffer (split-window-vertically) " *Org tags*")
13968 (org-switch-to-buffer-other-window " *Org tags*")
13969 (org-fit-window-to-buffer)))
13971 (and (= c ?q) (not (rassoc c ntable))))
13972 (org-detach-overlay org-tags-overlay)
13973 (setq quit-flag t))
13976 (if exit-after-next (setq exit-after-next 'now)))
13978 (condition-case nil
13979 (setq tg (org-icompleting-read
13982 (with-current-buffer buf
13983 (org-get-buffer-tags)))))
13984 (quit (setq tg "")))
13985 (when (string-match "\\S-" tg)
13986 (add-to-list 'buffer-tags (list tg))
13987 (if (member tg current)
13988 (setq current (delete tg current))
13989 (push tg current)))
13990 (if exit-after-next (setq exit-after-next 'now)))
13991 ((setq e (rassoc c todo-table) tg (car e))
13992 (with-current-buffer buf
13993 (save-excursion (org-todo tg)))
13994 (if exit-after-next (setq exit-after-next 'now)))
13995 ((setq e (rassoc c ntable) tg (car e))
13996 (if (member tg current)
13997 (setq current (delete tg current))
13998 (loop for g in groups do
14001 (setq current (delete x current)))
14004 (if exit-after-next (setq exit-after-next 'now))))
14006 ;; Create a sorted list
14010 (assoc b (cdr (memq (assoc a ntable) ntable))))))
14011 (if (eq exit-after-next 'now) (throw 'exit t))
14012 (goto-char (point-min))
14013 (beginning-of-line 2)
14014 (delete-region (point) (point-at-eol))
14015 (org-fast-tag-insert "Current" current c-face)
14016 (org-set-current-tags-overlay current ov-prefix)
14017 (while (re-search-forward
14018 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
14019 (setq tg (match-string 1))
14020 (add-text-properties
14021 (match-beginning 1) (match-end 1)
14024 ((member tg current) c-face)
14025 ((member tg inherited) i-face)
14026 (t (get-text-property (match-beginning 1) 'face))))))
14027 (goto-char (point-min)))))
14028 (org-detach-overlay org-tags-overlay)
14030 (mapconcat 'identity current ":")
14033 (defun org-get-tags-string ()
14034 "Get the TAGS string in the current headline."
14035 (unless (org-at-heading-p t)
14036 (error "Not on a heading"))
14038 (beginning-of-line 1)
14039 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14040 (org-match-string-no-properties 1)
14043 (defun org-get-tags ()
14044 "Get the list of tags specified in the current headline."
14045 (org-split-string (org-get-tags-string) ":"))
14047 (defun org-get-buffer-tags ()
14048 "Get a table of all tags used in the buffer, for completion."
14051 (goto-char (point-min))
14052 (while (re-search-forward
14053 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
14054 (when (equal (char-after (point-at-bol 0)) ?*)
14055 (mapc (lambda (x) (add-to-list 'tags x))
14056 (org-split-string (org-match-string-no-properties 1) ":")))))
14057 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
14058 (mapcar 'list tags)))
14060 ;;;; The mapping API
14062 (defun org-map-entries (func &optional match scope &rest skip)
14063 "Call FUNC at each headline selected by MATCH in SCOPE.
14065 FUNC is a function or a lisp form. The function will be called without
14066 arguments, with the cursor positioned at the beginning of the headline.
14067 The return values of all calls to the function will be collected and
14068 returned as a list.
14070 The call to FUNC will be wrapped into a save-excursion form, so FUNC
14071 does not need to preserve point. After evaluation, the cursor will be
14072 moved to the end of the line (presumably of the headline of the
14073 processed entry) and search continues from there. Under some
14074 circumstances, this may not produce the wanted results. For example,
14075 if you have removed (e.g. archived) the current (sub)tree it could
14076 mean that the next entry will be skipped entirely. In such cases, you
14077 can specify the position from where search should continue by making
14078 FUNC set the variable `org-map-continue-from' to the desired buffer
14081 MATCH is a tags/property/todo match as it is used in the agenda tags view.
14082 Only headlines that are matched by this query will be considered during
14083 the iteration. When MATCH is nil or t, all headlines will be
14084 visited by the iteration.
14086 SCOPE determines the scope of this command. It can be any of:
14088 nil The current buffer, respecting the restriction if any
14089 tree The subtree started with the entry at point
14090 region The entries within the active region, if any
14092 The entries within the active region, but only those at
14093 the same level than the first one.
14094 file The current buffer, without restriction
14096 The current buffer, and any archives associated with it
14097 agenda All agenda files
14098 agenda-with-archives
14099 All agenda files with any archive files associated with them
14101 If this is a list, all files in the list will be scanned
14103 The remaining args are treated as settings for the skipping facilities of
14104 the scanner. The following items can be given here:
14106 archive skip trees with the archive tag.
14107 comment skip trees with the COMMENT keyword
14108 function or Emacs Lisp form:
14109 will be used as value for `org-agenda-skip-function', so whenever
14110 the function returns t, FUNC will not be called for that
14111 entry and search will continue from the point where the
14112 function leaves it.
14114 If your function needs to retrieve the tags including inherited tags
14115 at the *current* entry, you can use the value of the variable
14116 `org-scanner-tags' which will be much faster than getting the value
14117 with `org-get-tags-at'. If your function gets properties with
14118 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
14119 to t around the call to `org-entry-properties' to get the same speedup.
14120 Note that if your function moves around to retrieve tags and properties at
14121 a *different* entry, you cannot use these techniques."
14122 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
14123 (not (org-region-active-p)))
14124 (let* ((org-agenda-archives-mode nil) ; just to make sure
14125 (org-agenda-skip-archived-trees (memq 'archive skip))
14126 (org-agenda-skip-comment-trees (memq 'comment skip))
14127 (org-agenda-skip-function
14128 (car (org-delete-all '(comment archive) skip)))
14129 (org-tags-match-list-sublevels t)
14130 (start-level (eq scope 'region-start-level))
14132 org-todo-keywords-for-agenda
14133 org-done-keywords-for-agenda
14134 org-todo-keyword-alist-for-agenda
14135 org-drawers-for-agenda
14136 org-tag-alist-for-agenda
14140 ((eq match t) (setq matcher t))
14141 ((eq match nil) (setq matcher t))
14142 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
14146 (cond ((eq scope 'tree)
14147 (org-back-to-heading t)
14148 (org-narrow-to-subtree)
14150 ((and (or (eq scope 'region) (eq scope 'region-start-level))
14151 (org-region-active-p))
14152 ;; If needed, set start-level to a string like "2"
14155 (goto-char (region-beginning))
14156 (unless (org-at-heading-p) (outline-next-heading))
14157 (setq start-level (org-current-level))))
14158 (narrow-to-region (region-beginning)
14160 (goto-char (region-end))
14161 (unless (and (bolp) (org-at-heading-p))
14162 (outline-next-heading))
14168 (org-agenda-prepare-buffers
14169 (list (buffer-file-name (current-buffer))))
14170 (setq res (org-scan-tags func matcher todo-only start-level)))
14171 ;; Get the right scope
14173 ((and scope (listp scope) (symbolp (car scope)))
14174 (setq scope (eval scope)))
14175 ((eq scope 'agenda)
14176 (setq scope (org-agenda-files t)))
14177 ((eq scope 'agenda-with-archives)
14178 (setq scope (org-agenda-files t))
14179 (setq scope (org-add-archive-files scope)))
14181 (setq scope (list (buffer-file-name))))
14182 ((eq scope 'file-with-archives)
14183 (setq scope (org-add-archive-files (list (buffer-file-name))))))
14184 (org-agenda-prepare-buffers scope)
14185 (while (setq file (pop scope))
14186 (with-current-buffer (org-find-base-buffer-visiting file)
14190 (goto-char (point-min))
14191 (setq res (append res (org-scan-tags func matcher todo-only))))))))))
14196 ;;; Setting and retrieving properties
14198 (defconst org-special-properties
14199 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
14200 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
14201 "The special properties valid in Org-mode.
14203 These are properties that are not defined in the property drawer,
14204 but in some other way.")
14206 (defconst org-default-properties
14207 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
14208 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
14209 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
14210 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
14211 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
14212 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
14213 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
14214 "Some properties that are used by Org-mode for various purposes.
14215 Being in this list makes sure that they are offered for completion.")
14217 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
14218 "Regular expression matching the first line of a property drawer.")
14220 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
14221 "Regular expression matching the last line of a property drawer.")
14223 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
14224 "Regular expression matching the first line of a property drawer.")
14226 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
14227 "Regular expression matching the first line of a property drawer.")
14229 (defconst org-property-drawer-re
14230 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
14231 org-property-end-re "\\)\n?")
14232 "Matches an entire property drawer.")
14234 (defconst org-clock-drawer-re
14235 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
14236 org-property-end-re "\\)\n?")
14237 "Matches an entire clock drawer.")
14239 (defsubst org-re-property (property)
14240 "Return a regexp matching a PROPERTY line.
14241 Match group 1 will be set to the value."
14242 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
14244 (defsubst org-re-property-keyword (property)
14245 "Return a regexp matching a PROPERTY line, possibly with no
14246 value for the property."
14247 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
14249 (defun org-property-action ()
14250 "Do an action on properties."
14253 (org-at-property-p)
14254 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
14255 (setq c (read-char-exclusive))
14258 (call-interactively 'org-set-property))
14260 (call-interactively 'org-delete-property))
14262 (call-interactively 'org-delete-property-globally))
14264 (call-interactively 'org-compute-property-at-point))
14265 (t (error "No such property action %c" c)))))
14267 (defun org-inc-effort ()
14268 "Increment the value of the effort property in the current entry."
14270 (org-set-effort nil t))
14272 (defun org-set-effort (&optional value increment)
14273 "Set the effort property of the current entry.
14274 With numerical prefix arg, use the nth allowed value, 0 stands for the
14275 10th allowed value.
14277 When INCREMENT is non-nil, set the property to the next allowed value."
14279 (if (equal value 0) (setq value 10))
14280 (let* ((completion-ignore-case t)
14281 (prop org-effort-property)
14282 (cur (org-entry-get nil prop))
14283 (allowed (org-property-get-allowed-values nil prop 'table))
14284 (existing (mapcar 'list (org-property-values prop)))
14287 ((stringp value) value)
14288 ((and allowed (integerp value))
14289 (or (car (nth (1- value) allowed))
14290 (car (org-last allowed))))
14291 ((and allowed increment)
14292 (or (caadr (member (list cur) allowed))
14293 (error "Allowed effort values are not set")))
14295 (message "Select 1-9,0, [RET%s]: %s"
14296 (if cur (concat "=" cur) "")
14297 (mapconcat 'car allowed " "))
14298 (setq rpl (read-char-exclusive))
14299 (if (equal rpl ?\r)
14301 (setq rpl (- rpl ?0))
14302 (if (equal rpl 0) (setq rpl 10))
14303 (if (and (> rpl 0) (<= rpl (length allowed)))
14304 (car (nth (1- rpl) allowed))
14305 (org-completing-read "Effort: " allowed nil))))
14307 (let (org-completion-use-ido org-completion-use-iswitchb)
14308 (org-completing-read
14309 (concat "Effort " (if (and cur (string-match "\\S-" cur))
14310 (concat "[" cur "]") "")
14312 existing nil nil "" nil cur))))))
14313 (unless (equal (org-entry-get nil prop) val)
14314 (org-entry-put nil prop val))
14316 (org-back-to-heading t)
14317 (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
14318 (message "%s is now %s" prop val)))
14320 (defun org-at-property-p ()
14321 "Is cursor inside a property drawer?"
14323 (beginning-of-line 1)
14324 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
14325 (save-match-data ;; Used by calling procedures
14327 (range (unless (org-before-first-heading-p)
14328 (org-get-property-block))))
14329 (and range (<= (car range) p) (< p (cdr range))))))))
14331 (defun org-get-property-block (&optional beg end force)
14332 "Return the (beg . end) range of the body of the property drawer.
14333 BEG and END are the beginning and end of the current subtree, or of
14334 the part before the first headline. If they are not given, they will
14335 be found. If the drawer does not exist and FORCE is non-nil, create
14339 (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
14340 (progn (org-back-to-heading t) (point))))
14341 (end (or end (and (not (outline-next-heading)) (point-max))
14344 (if (re-search-forward org-property-start-re end t)
14345 (setq beg (1+ (match-end 0)))
14348 (org-insert-property-drawer)
14349 (setq end (progn (outline-next-heading) (point))))
14352 (if (re-search-forward org-property-start-re end t)
14353 (setq beg (1+ (match-end 0)))))
14354 (if (re-search-forward org-property-end-re end t)
14355 (setq end (match-beginning 0))
14356 (or force (throw 'exit nil))
14360 (insert ":END:\n"))
14363 (defun org-entry-properties (&optional pom which specific)
14364 "Get all properties of the entry at point-or-marker POM.
14365 This includes the TODO keyword, the tags, time strings for deadline,
14366 scheduled, and clocking, and any additional properties defined in the
14367 entry. The return value is an alist, keys may occur multiple times
14368 if the property key was used several times.
14369 POM may also be nil, in which case the current entry is used.
14370 If WHICH is nil or `all', get all properties. If WHICH is
14371 `special' or `standard', only get that subclass. If WHICH
14372 is a string only get exactly this property. SPECIFIC can be a string, the
14373 specific property we are interested in. Specifying it can speed
14374 things up because then unnecessary parsing is avoided."
14375 (setq which (or which 'all))
14376 (org-with-point-at pom
14377 (let ((clockstr (substring org-clock-string 0 -1))
14378 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
14379 (case-fold-search nil)
14380 beg end range props sum-props key key1 value string clocksum clocksumt)
14382 (when (condition-case nil
14383 (and (derived-mode-p 'org-mode) (org-back-to-heading t))
14386 (setq sum-props (get-text-property (point) 'org-summaries))
14387 (setq clocksum (get-text-property (point) :org-clock-minutes)
14388 clocksumt (get-text-property (point) :org-clock-minutes-today))
14389 (outline-next-heading)
14391 (when (memq which '(all special))
14392 ;; Get the special properties, like TODO and tags
14394 (when (and (or (not specific) (string= specific "TODO"))
14395 (looking-at org-todo-line-regexp) (match-end 2))
14396 (push (cons "TODO" (org-match-string-no-properties 2)) props))
14397 (when (and (or (not specific) (string= specific "PRIORITY"))
14398 (looking-at org-priority-regexp))
14399 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
14400 (when (or (not specific) (string= specific "FILE"))
14401 (push (cons "FILE" buffer-file-name) props))
14402 (when (and (or (not specific) (string= specific "TAGS"))
14403 (setq value (org-get-tags-string))
14404 (string-match "\\S-" value))
14405 (push (cons "TAGS" value) props))
14406 (when (and (or (not specific) (string= specific "ALLTAGS"))
14407 (setq value (org-get-tags-at)))
14408 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
14411 (when (or (not specific) (string= specific "BLOCKED"))
14412 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
14413 (when (or (not specific)
14415 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
14416 "TIMESTAMP" "TIMESTAMP_IA")))
14418 (while (re-search-forward org-maybe-keyword-time-regexp end t)
14419 (setq key (if (match-end 1)
14420 (substring (org-match-string-no-properties 1)
14422 string (if (equal key clockstr)
14424 (buffer-substring-no-properties
14425 (match-beginning 3) (goto-char
14427 (substring (org-match-string-no-properties 3)
14429 ;; Get the correct property name from the key. This is
14430 ;; necessary if the user has configured time keywords.
14431 (setq key1 (concat key ":"))
14435 (if (= (char-after (match-beginning 3)) ?\[)
14436 "TIMESTAMP_IA" "TIMESTAMP")))
14437 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
14438 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
14439 ((equal key1 org-closed-string) (setq key "CLOSED"))
14440 ((equal key1 org-clock-string) (setq key "CLOCK")))
14441 (if (and specific (equal key specific) (not (equal key "CLOCK")))
14443 (push (cons key string) props)
14444 ;; no need to search further if match is found
14446 (when (or (equal key "CLOCK") (not (assoc key props)))
14447 (push (cons key string) props)))))))
14449 (when (memq which '(all standard))
14450 ;; Get the standard properties, like :PROP: ...
14451 (setq range (org-get-property-block beg end))
14453 (goto-char (car range))
14454 (while (re-search-forward
14455 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
14457 (setq key (org-match-string-no-properties 1)
14458 value (org-trim (or (org-match-string-no-properties 2) "")))
14459 (unless (member key excluded)
14460 (push (cons key (or value "")) props)))))
14462 (push (cons "CLOCKSUM"
14463 (org-columns-number-to-string (/ (float clocksum) 60.)
14467 (push (cons "CLOCKSUM_T"
14468 (org-columns-number-to-string (/ (float clocksumt) 60.)
14471 (unless (assoc "CATEGORY" props)
14472 (push (cons "CATEGORY" (org-get-category)) props))
14473 (append sum-props (nreverse props)))))))
14475 (defun org-entry-get (pom property &optional inherit literal-nil)
14476 "Get value of PROPERTY for entry or content at point-or-marker POM.
14477 If INHERIT is non-nil and the entry does not have the property,
14478 then also check higher levels of the hierarchy.
14479 If INHERIT is the symbol `selective', use inheritance only if the setting
14480 in `org-use-property-inheritance' selects PROPERTY for inheritance.
14481 If the property is present but empty, the return value is the empty string.
14482 If the property is not present at all, nil is returned.
14484 If LITERAL-NIL is set, return the string value \"nil\" as a string,
14485 do not interpret it as the list atom nil. This is used for inheritance
14486 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
14487 (org-with-point-at pom
14488 (if (and inherit (if (eq inherit 'selective)
14489 (org-property-inherit-p property)
14491 (org-entry-get-with-inheritance property literal-nil)
14492 (if (member property org-special-properties)
14493 ;; We need a special property. Use `org-entry-properties' to
14494 ;; retrieve it, but specify the wanted property
14495 (cdr (assoc property (org-entry-properties nil 'special property)))
14496 (let ((range (org-get-property-block)))
14497 (when (and range (not (eq (car range) (cdr range))))
14498 (let* ((props (list (or (assoc property org-file-properties)
14499 (assoc property org-global-properties)
14500 (assoc property org-global-properties-fixed))))
14502 (when (re-search-forward
14503 (org-re-property key) (cdr range) t)
14505 (org-update-property-plist
14508 (org-match-string-no-properties 1) "")
14511 (goto-char (car range))
14512 (funcall ap property)
14513 (goto-char (car range))
14514 (while (funcall ap (concat property "+")))
14515 (setq val (cdr (assoc property props)))
14516 (when val (if literal-nil val (org-not-nil val))))))))))
14518 (defun org-property-or-variable-value (var &optional inherit)
14519 "Check if there is a property fixing the value of VAR.
14520 If yes, return this value. If not, return the current value of the variable."
14521 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14522 (if (and prop (stringp prop) (string-match "\\S-" prop))
14524 (symbol-value var))))
14526 (defun org-entry-delete (pom property)
14527 "Delete the property PROPERTY from entry at point-or-marker POM."
14528 (org-with-point-at pom
14529 (if (member property org-special-properties)
14530 nil ; cannot delete these properties.
14531 (let ((range (org-get-property-block)))
14533 (goto-char (car range))
14535 (org-re-property property)
14538 (delete-region (match-beginning 0) (1+ (point-at-eol)))
14542 ;; Multi-values properties are properties that contain multiple values
14543 ;; These values are assumed to be single words, separated by whitespace.
14544 (defun org-entry-add-to-multivalued-property (pom property value)
14545 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
14546 (let* ((old (org-entry-get pom property))
14547 (values (and old (org-split-string old "[ \t]"))))
14548 (setq value (org-entry-protect-space value))
14549 (unless (member value values)
14550 (setq values (cons value values))
14551 (org-entry-put pom property
14552 (mapconcat 'identity values " ")))))
14554 (defun org-entry-remove-from-multivalued-property (pom property value)
14555 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
14556 (let* ((old (org-entry-get pom property))
14557 (values (and old (org-split-string old "[ \t]"))))
14558 (setq value (org-entry-protect-space value))
14559 (when (member value values)
14560 (setq values (delete value values))
14561 (org-entry-put pom property
14562 (mapconcat 'identity values " ")))))
14564 (defun org-entry-member-in-multivalued-property (pom property value)
14565 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
14566 (let* ((old (org-entry-get pom property))
14567 (values (and old (org-split-string old "[ \t]"))))
14568 (setq value (org-entry-protect-space value))
14569 (member value values)))
14571 (defun org-entry-get-multivalued-property (pom property)
14572 "Return a list of values in a multivalued property."
14573 (let* ((value (org-entry-get pom property))
14574 (values (and value (org-split-string value "[ \t]"))))
14575 (mapcar 'org-entry-restore-space values)))
14577 (defun org-entry-put-multivalued-property (pom property &rest values)
14578 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
14579 VALUES should be a list of strings. Spaces will be protected."
14580 (org-entry-put pom property
14581 (mapconcat 'org-entry-protect-space values " "))
14582 (let* ((value (org-entry-get pom property))
14583 (values (and value (org-split-string value "[ \t]"))))
14584 (mapcar 'org-entry-restore-space values)))
14586 (defun org-entry-protect-space (s)
14587 "Protect spaces and newline in string S."
14588 (while (string-match " " s)
14589 (setq s (replace-match "%20" t t s)))
14590 (while (string-match "\n" s)
14591 (setq s (replace-match "%0A" t t s)))
14594 (defun org-entry-restore-space (s)
14595 "Restore spaces and newline in string S."
14596 (while (string-match "%20" s)
14597 (setq s (replace-match " " t t s)))
14598 (while (string-match "%0A" s)
14599 (setq s (replace-match "\n" t t s)))
14602 (defvar org-entry-property-inherited-from (make-marker)
14603 "Marker pointing to the entry from where a property was inherited.
14604 Each call to `org-entry-get-with-inheritance' will set this marker to the
14605 location of the entry where the inheritance search matched. If there was
14606 no match, the marker will point nowhere.
14607 Note that also `org-entry-get' calls this function, if the INHERIT flag
14610 (defun org-entry-get-with-inheritance (property &optional literal-nil)
14611 "Get PROPERTY of entry or content at point, search higher levels if needed.
14612 The search will stop at the first ancestor which has the property defined.
14613 If the value found is \"nil\", return nil to show that the property
14614 should be considered as undefined (this is the meaning of nil here).
14615 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
14616 (move-marker org-entry-property-inherited-from nil)
14623 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
14624 (or (ignore-errors (org-back-to-heading t))
14625 (goto-char (point-min)))
14626 (move-marker org-entry-property-inherited-from (point))
14628 (or (ignore-errors (org-up-heading-safe))
14629 (throw 'ex nil))))))
14631 (cdr (assoc property org-file-properties))
14632 (cdr (assoc property org-global-properties))
14633 (cdr (assoc property org-global-properties-fixed))))
14634 (if literal-nil tmp (org-not-nil tmp))))
14636 (defvar org-property-changed-functions nil
14637 "Hook called when the value of a property has changed.
14638 Each hook function should accept two arguments, the name of the property
14639 and the new value.")
14641 (defun org-entry-put (pom property value)
14642 "Set PROPERTY to VALUE for entry at point-or-marker POM."
14643 (org-with-point-at pom
14644 (org-back-to-heading t)
14645 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
14648 ((equal property "TODO")
14649 (when (and (stringp value) (string-match "\\S-" value)
14650 (not (member value org-todo-keywords-1)))
14651 (error "\"%s\" is not a valid TODO state" value))
14652 (if (or (not value)
14653 (not (string-match "\\S-" value)))
14654 (setq value 'none))
14656 (org-set-tags nil 'align))
14657 ((equal property "PRIORITY")
14658 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
14659 (string-to-char value) ?\ ))
14660 (org-set-tags nil 'align))
14661 ((equal property "SCHEDULED")
14662 (if (re-search-forward org-scheduled-time-regexp end t)
14664 ((eq value 'earlier) (org-timestamp-change -1 'day))
14665 ((eq value 'later) (org-timestamp-change 1 'day))
14666 (t (call-interactively 'org-schedule)))
14667 (call-interactively 'org-schedule)))
14668 ((equal property "DEADLINE")
14669 (if (re-search-forward org-deadline-time-regexp end t)
14671 ((eq value 'earlier) (org-timestamp-change -1 'day))
14672 ((eq value 'later) (org-timestamp-change 1 'day))
14673 (t (call-interactively 'org-deadline)))
14674 (call-interactively 'org-deadline)))
14675 ((member property org-special-properties)
14676 (error "The %s property can not yet be set with `org-entry-put'"
14678 (t ; a non-special property
14679 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
14680 (setq range (org-get-property-block beg end 'force))
14681 (goto-char (car range))
14682 (if (re-search-forward
14683 (org-re-property-keyword property) (cdr range) t)
14685 (delete-region (match-beginning 0) (match-end 0))
14686 (goto-char (match-beginning 0)))
14687 (goto-char (cdr range))
14691 (insert ":" property ":")
14692 (and value (insert " " value))
14693 (org-indent-line)))))
14694 (run-hook-with-args 'org-property-changed-functions property value)))
14696 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
14697 "Get all property keys in the current buffer.
14698 With INCLUDE-SPECIALS, also list the special properties that reflect things
14699 like tags and TODO state.
14700 With INCLUDE-DEFAULTS, also include properties that has special meaning
14701 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
14703 With INCLUDE-COLUMNS, also include property names given in COLUMN
14704 formats in the current buffer."
14705 (let (rtn range cfmt s p)
14709 (goto-char (point-min))
14710 (while (re-search-forward org-property-start-re nil t)
14711 (setq range (org-get-property-block))
14712 (goto-char (car range))
14713 (while (re-search-forward
14714 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
14716 (add-to-list 'rtn (org-match-string-no-properties 1)))
14717 (outline-next-heading))))
14719 (when include-specials
14720 (setq rtn (append org-special-properties rtn)))
14722 (when include-defaults
14723 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
14724 (add-to-list 'rtn org-effort-property))
14726 (when include-columns
14730 (goto-char (point-min))
14731 (while (re-search-forward
14732 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
14734 (setq cfmt (match-string 2) s 0)
14735 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
14737 (setq s (match-end 0)
14738 p (match-string 1 cfmt))
14739 (unless (or (equal p "ITEM")
14740 (member p org-special-properties))
14741 (add-to-list 'rtn (match-string 1 cfmt))))))))
14743 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
14745 (defun org-property-values (key)
14746 "Return a list of all values of property KEY in the current buffer."
14750 (goto-char (point-min))
14751 (let ((re (org-re-property key))
14753 (while (re-search-forward re nil t)
14754 (add-to-list 'values (org-trim (match-string 1))))
14755 (delete "" values)))))
14757 (defun org-insert-property-drawer ()
14758 "Insert a property drawer into the current entry."
14759 (org-back-to-heading t)
14760 (looking-at org-outline-regexp)
14761 (let ((indent (if org-adapt-indentation
14762 (- (match-end 0) (match-beginning 0))
14765 (re (concat "^[ \t]*" org-keyword-time-regexp))
14767 (outline-next-heading)
14770 (while (re-search-forward re end t))
14771 (setq hiddenp (outline-invisible-p))
14773 (and (equal (char-after) ?\n) (forward-char 1))
14774 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
14775 (if (member (match-string 1) '("CLOCK:" ":END:"))
14776 ;; just skip this line
14777 (beginning-of-line 2)
14778 ;; Drawer start, find the end
14779 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
14780 (beginning-of-line 1)))
14781 (org-skip-over-state-notes)
14782 (skip-chars-backward " \t\n\r")
14783 (if (eq (char-before) ?*) (forward-char 1))
14784 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
14785 (beginning-of-line 0)
14786 (org-indent-to-column indent)
14787 (beginning-of-line 2)
14788 (org-indent-to-column indent)
14789 (beginning-of-line 0)
14792 (org-back-to-heading t)
14794 (org-flag-drawer t))))
14796 (defun org-insert-drawer (&optional arg drawer)
14797 "Insert a drawer at point.
14799 Optional argument DRAWER, when non-nil, is a string representing
14800 drawer's name. Otherwise, the user is prompted for a name.
14802 If a region is active, insert the drawer around that region
14805 Point is left between drawer's boundaries."
14807 (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
14809 ;; SYSTEM-DRAWERS is a list of drawer names that are used
14810 ;; internally by Org. They are meant to be inserted
14812 (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
14813 ;; Remove system drawers from list. Note: For some reason,
14814 ;; `org-completing-read' ignores the predicate while
14815 ;; `completing-read' handles it fine.
14816 (drawer (if arg "PROPERTIES"
14819 "Drawer: " org-drawers
14820 (lambda (d) (not (member d system-drawers))))))))
14822 ;; With C-u, fall back on `org-insert-property-drawer'
14823 (arg (org-insert-property-drawer))
14824 ;; With an active region, insert a drawer at point.
14825 ((not (org-region-active-p))
14827 (unless (bolp) (insert "\n"))
14828 (insert (format ":%s:\n\n:END:\n" drawer))
14829 (forward-line -2)))
14830 ;; Otherwise, insert the drawer at point
14832 (let ((rbeg (region-beginning))
14833 (rend (copy-marker (region-end))))
14837 (beginning-of-line)
14838 (when (save-excursion
14839 (re-search-forward org-outline-regexp-bol rend t))
14840 (error "Drawers cannot contain headlines"))
14841 ;; Position point at the beginning of the first
14842 ;; non-blank line in region. Insert drawer's opening
14843 ;; there, then indent it.
14844 (org-skip-whitespace)
14845 (beginning-of-line)
14846 (insert ":" drawer ":\n")
14848 (indent-for-tab-command)
14849 ;; Move point to the beginning of the first blank line
14850 ;; after the last non-blank line in region. Insert
14851 ;; drawer's closing, then indent it.
14853 (skip-chars-backward " \r\t\n")
14855 (deactivate-mark t)
14856 (indent-for-tab-command)
14857 (unless (eolp) (insert "\n")))
14858 ;; Clear marker, whatever the outcome of insertion is.
14859 (set-marker rend nil)))))))
14861 (defvar org-property-set-functions-alist nil
14862 "Property set function alist.
14863 Each entry should have the following format:
14865 (PROPERTY . READ-FUNCTION)
14867 The read function will be called with the same argument as
14868 `org-completing-read'.")
14870 (defun org-set-property-function (property)
14871 "Get the function that should be used to set PROPERTY.
14872 This is computed according to `org-property-set-functions-alist'."
14873 (or (cdr (assoc property org-property-set-functions-alist))
14874 'org-completing-read))
14876 (defun org-read-property-value (property)
14877 "Read PROPERTY value from user."
14878 (let* ((completion-ignore-case t)
14879 (allowed (org-property-get-allowed-values nil property 'table))
14880 (cur (org-entry-get nil property))
14881 (prompt (concat property " value"
14882 (if (and cur (string-match "\\S-" cur))
14883 (concat " [" cur "]") "") ": "))
14884 (set-function (org-set-property-function property))
14886 (funcall set-function prompt allowed nil
14887 (not (get-text-property 0 'org-unrestricted
14889 (let (org-completion-use-ido org-completion-use-iswitchb)
14890 (funcall set-function prompt
14891 (mapcar 'list (org-property-values property))
14892 nil nil "" nil cur)))))
14897 (defvar org-last-set-property nil)
14898 (defun org-read-property-name ()
14899 "Read a property name."
14900 (let* ((completion-ignore-case t)
14901 (keys (org-buffer-property-keys nil t t))
14902 (default-prop (or (save-excursion
14904 (beginning-of-line)
14905 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
14906 (null (string= (match-string 1) "END"))
14907 (match-string 1))))
14908 org-last-set-property))
14909 (property (org-icompleting-read
14911 (if default-prop (concat " [" default-prop "]") "")
14913 (mapcar 'list keys)
14917 (if (member property keys)
14919 (or (cdr (assoc (downcase property)
14920 (mapcar (lambda (x) (cons (downcase x) x))
14924 (defun org-set-property (property value)
14925 "In the current entry, set PROPERTY to VALUE.
14926 When called interactively, this will prompt for a property name, offering
14927 completion on existing and default properties. And then it will prompt
14928 for a value, offering completion either on allowed values (via an inherited
14929 xxx_ALL property) or on existing values in other instances of this property
14930 in the current file."
14931 (interactive (list nil nil))
14932 (let* ((property (or property (org-read-property-name)))
14933 (value (or value (org-read-property-value property)))
14934 (fn (cdr (assoc property org-properties-postprocess-alist))))
14935 (setq org-last-set-property property)
14936 ;; Possibly postprocess the inserted value:
14937 (when fn (setq value (funcall fn value)))
14938 (unless (equal (org-entry-get nil property) value)
14939 (org-entry-put nil property value))))
14941 (defun org-delete-property (property)
14942 "In the current entry, delete PROPERTY."
14944 (let* ((completion-ignore-case t)
14945 (prop (org-icompleting-read "Property: "
14946 (org-entry-properties nil 'standard))))
14948 (message "Property %s %s" property
14949 (if (org-entry-delete nil property)
14951 "was not present in the entry")))
14953 (defun org-delete-property-globally (property)
14954 "Remove PROPERTY globally, from all entries."
14956 (let* ((completion-ignore-case t)
14957 (prop (org-icompleting-read
14958 "Globally remove property: "
14959 (mapcar 'list (org-buffer-property-keys)))))
14964 (goto-char (point-min))
14966 (while (re-search-forward
14967 (org-re-property property)
14969 (setq cnt (1+ cnt))
14970 (delete-region (match-beginning 0) (1+ (point-at-eol))))
14971 (message "Property \"%s\" removed from %d entries" property cnt)))))
14973 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
14975 (defun org-compute-property-at-point ()
14976 "Compute the property at point.
14977 This looks for an enclosing column format, extracts the operator and
14978 then applies it to the property in the column format's scope."
14980 (unless (org-at-property-p)
14981 (error "Not at a property"))
14982 (let ((prop (org-match-string-no-properties 2)))
14983 (org-columns-get-format-and-top-level)
14984 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
14985 (error "No operator defined for property %s" prop))
14986 (org-columns-compute prop)))
14988 (defvar org-property-allowed-value-functions nil
14989 "Hook for functions supplying allowed values for a specific property.
14990 The functions must take a single argument, the name of the property, and
14991 return a flat list of allowed values. If \":ETC\" is one of
14992 the values, this means that these values are intended as defaults for
14993 completion, but that other values should be allowed too.
14994 The functions must return nil if they are not responsible for this
14997 (defun org-property-get-allowed-values (pom property &optional table)
14998 "Get allowed values for the property PROPERTY.
14999 When TABLE is non-nil, return an alist that can directly be used for
15003 ((equal property "TODO")
15004 (setq vals (org-with-point-at pom
15005 (append org-todo-keywords-1 '("")))))
15006 ((equal property "PRIORITY")
15007 (let ((n org-lowest-priority))
15008 (while (>= n org-highest-priority)
15009 (push (char-to-string n) vals)
15011 ((member property org-special-properties))
15012 ((setq vals (run-hook-with-args-until-success
15013 'org-property-allowed-value-functions property)))
15015 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
15016 (when (and vals (string-match "\\S-" vals))
15017 (setq vals (car (read-from-string (concat "(" vals ")"))))
15018 (setq vals (mapcar (lambda (x)
15019 (cond ((stringp x) x)
15020 ((numberp x) (number-to-string x))
15021 ((symbolp x) (symbol-name x))
15024 (when (member ":ETC" vals)
15025 (setq vals (remove ":ETC" vals))
15026 (org-add-props (car vals) '(org-unrestricted t)))
15027 (if table (mapcar 'list vals) vals)))
15029 (defun org-property-previous-allowed-value (&optional previous)
15030 "Switch to the next allowed value for this property."
15032 (org-property-next-allowed-value t))
15034 (defun org-property-next-allowed-value (&optional previous)
15035 "Switch to the next allowed value for this property."
15037 (unless (org-at-property-p)
15038 (error "Not at a property"))
15039 (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
15040 (key (match-string 2))
15041 (value (match-string 3))
15042 (allowed (or (org-property-get-allowed-values (point) key)
15043 (and (member value '("[ ]" "[-]" "[X]"))
15047 (error "Allowed values for this property have not been defined"))
15048 (if previous (setq allowed (reverse allowed)))
15049 (if (member value allowed)
15050 (setq nval (car (cdr (member value allowed)))))
15051 (setq nval (or nval (car allowed)))
15052 (if (equal nval value)
15053 (error "Only one allowed value for this property"))
15054 (org-at-property-p)
15055 (replace-match (concat " :" key ": " nval) t t)
15057 (beginning-of-line 1)
15058 (skip-chars-forward " \t")
15059 (when (equal prop org-effort-property)
15061 (org-back-to-heading t)
15062 (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
15063 (run-hook-with-args 'org-property-changed-functions key nval)))
15065 (defun org-find-olp (path &optional this-buffer)
15066 "Return a marker pointing to the entry at outline path OLP.
15067 If anything goes wrong, throw an error.
15068 You can wrap this call to catch the error like this:
15070 (condition-case msg
15071 (org-mobile-locate-entry (match-string 4))
15072 (error (nth 1 msg)))
15074 The return value will then be either a string with the error message,
15075 or a marker if everything is OK.
15077 If THIS-BUFFER is set, the outline path does not contain a file,
15079 (let* ((file (if this-buffer buffer-file-name (pop path)))
15080 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
15084 limit re end found pos heading cnt flevel)
15085 (unless buffer (error "File not found :%s" file))
15086 (with-current-buffer buffer
15090 (setq limit (point-max))
15091 (goto-char (point-min))
15092 (while (setq heading (pop path))
15093 (setq re (format org-complex-heading-regexp-format
15094 (regexp-quote heading)))
15095 (setq cnt 0 pos (point))
15096 (while (re-search-forward re end t)
15097 (setq level (- (match-end 1) (match-beginning 1)))
15098 (if (and (>= level lmin) (<= level lmax))
15099 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
15100 (when (= cnt 0) (error "Heading not found on level %d: %s"
15102 (when (> cnt 1) (error "Heading not unique on level %d: %s"
15105 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
15106 (setq end (save-excursion (org-end-of-subtree t t))))
15107 (when (org-at-heading-p)
15108 (point-marker)))))))
15110 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
15111 "Find node HEADING in BUFFER.
15112 Return a marker to the heading if it was found, or nil if not.
15113 If POS-ONLY is set, return just the position instead of a marker.
15115 The heading text must match exact, but it may have a TODO keyword,
15116 a priority cookie and tags in the standard locations."
15117 (with-current-buffer (or buffer (current-buffer))
15121 (goto-char (point-min))
15122 (let (case-fold-search)
15123 (if (re-search-forward
15124 (format org-complex-heading-regexp-format
15125 (regexp-quote heading)) nil t)
15127 (match-beginning 0)
15128 (move-marker (make-marker) (match-beginning 0)))))))))
15130 (defun org-find-exact-heading-in-directory (heading &optional dir)
15131 "Find Org node headline HEADING in all .org files in directory DIR.
15132 When the target headline is found, return a marker to this location."
15133 (let ((files (directory-files (or dir default-directory)
15134 nil "\\`[^.#].*\\.org\\'"))
15135 file visiting m buffer)
15137 (while (setq file (pop files))
15138 (message "trying %s" file)
15139 (setq visiting (org-find-base-buffer-visiting file))
15140 (setq buffer (or visiting (find-file-noselect file)))
15141 (setq m (org-find-exact-headline-in-buffer
15143 (when (and (not m) (not visiting)) (kill-buffer buffer))
15144 (and m (throw 'found m))))))
15146 (defun org-find-entry-with-id (ident)
15147 "Locate the entry that contains the ID property with exact value IDENT.
15148 IDENT can be a string, a symbol or a number, this function will search for
15149 the string representation of it.
15150 Return the position where this entry starts, or nil if there is no such entry."
15151 (interactive "sID: ")
15153 ((stringp ident) ident)
15154 ((symbol-name ident) (symbol-name ident))
15155 ((numberp ident) (number-to-string ident))
15156 (t (error "IDENT %s must be a string, symbol or number" ident))))
15157 (case-fold-search nil))
15161 (goto-char (point-min))
15162 (when (re-search-forward
15163 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
15165 (org-back-to-heading t)
15170 (defvar org-last-changed-timestamp nil)
15171 (defvar org-last-inserted-timestamp nil
15172 "The last time stamp inserted with `org-insert-time-stamp'.")
15173 (defvar org-time-was-given) ; dynamically scoped parameter
15174 (defvar org-end-time-was-given) ; dynamically scoped parameter
15175 (defvar org-ts-what) ; dynamically scoped parameter
15177 (defun org-time-stamp (arg &optional inactive)
15178 "Prompt for a date/time and insert a time stamp.
15179 If the user specifies a time like HH:MM or if this command is
15180 called with at least one prefix argument, the time stamp contains
15181 the date and the time. Otherwise, only the date is be included.
15183 All parts of a date not specified by the user is filled in from
15184 the current date/time. So if you just press return without
15185 typing anything, the time stamp will represent the current
15188 If there is already a timestamp at the cursor, it will be
15191 With two universal prefix arguments, insert an active timestamp
15192 with the current time without prompting the user."
15196 ;; Default time is either today, or, when entering a range,
15197 ;; the range start.
15198 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
15200 (re-search-backward
15201 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
15202 (- (point) 20) t)))
15203 (apply 'encode-time (org-parse-time-string (match-string 1)))
15205 (default-input (and ts (org-get-compact-tod ts)))
15206 (repeater (save-excursion
15208 (beginning-of-line)
15209 (when (re-search-forward
15210 "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
15211 (save-excursion (progn (end-of-line) (point))) t)
15212 (match-string 0)))))
15213 org-time-was-given org-end-time-was-given time)
15215 ((and (org-at-timestamp-p t)
15216 (memq last-command '(org-time-stamp org-time-stamp-inactive))
15217 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
15219 (setq time (let ((this-command this-command))
15220 (org-read-date arg 'totime nil nil
15221 default-time default-input inactive)))
15222 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
15223 ((org-at-timestamp-p t)
15224 (setq time (let ((this-command this-command))
15225 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15226 (when (org-at-timestamp-p t) ; just to get the match data
15227 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
15229 (setq org-last-changed-timestamp
15230 (org-insert-time-stamp
15231 time (or org-time-was-given arg)
15232 inactive nil nil (list org-end-time-was-given)))
15233 (when repeater (goto-char (1- (point))) (insert " " repeater)
15234 (setq org-last-changed-timestamp
15235 (concat (substring org-last-inserted-timestamp 0 -1)
15236 " " repeater ">"))))
15237 (message "Timestamp updated"))
15239 (org-insert-time-stamp (current-time) t))
15241 (setq time (let ((this-command this-command))
15242 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15243 (org-insert-time-stamp time (or org-time-was-given arg) inactive
15244 nil nil (list org-end-time-was-given))))))
15246 ;; FIXME: can we use this for something else, like computing time differences?
15247 (defun org-get-compact-tod (s)
15248 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
15249 (let* ((t1 (match-string 1 s))
15250 (h1 (string-to-number (match-string 2 s)))
15251 (m1 (string-to-number (match-string 3 s)))
15252 (t2 (and (match-end 4) (match-string 5 s)))
15253 (h2 (and t2 (string-to-number (match-string 6 s))))
15254 (m2 (and t2 (string-to-number (match-string 7 s))))
15258 (setq dh (- h2 h1) dm (- m2 m1))
15259 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
15260 (concat t1 "+" (number-to-string dh)
15261 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
15263 (defun org-time-stamp-inactive (&optional arg)
15264 "Insert an inactive time stamp.
15265 An inactive time stamp is enclosed in square brackets instead of angle
15266 brackets. It is inactive in the sense that it does not trigger agenda entries,
15267 does not link to the calendar and cannot be changed with the S-cursor keys.
15268 So these are more for recording a certain time/date."
15270 (org-time-stamp arg 'inactive))
15272 (defvar org-date-ovl (make-overlay 1 1))
15273 (overlay-put org-date-ovl 'face 'org-date-selected)
15274 (org-detach-overlay org-date-ovl)
15276 (defvar org-ans1) ; dynamically scoped parameter
15277 (defvar org-ans2) ; dynamically scoped parameter
15279 (defvar org-plain-time-of-day-regexp) ; defined below
15281 (defvar org-overriding-default-time nil) ; dynamically scoped
15282 (defvar org-read-date-overlay nil)
15283 (defvar org-dcst nil) ; dynamically scoped
15284 (defvar org-read-date-history nil)
15285 (defvar org-read-date-final-answer nil)
15286 (defvar org-read-date-analyze-futurep nil)
15287 (defvar org-read-date-analyze-forced-year nil)
15288 (defvar org-read-date-inactive)
15290 (defun org-read-date (&optional org-with-time to-time from-string prompt
15291 default-time default-input inactive)
15292 "Read a date, possibly a time, and make things smooth for the user.
15293 The prompt will suggest to enter an ISO date, but you can also enter anything
15294 which will at least partially be understood by `parse-time-string'.
15295 Unrecognized parts of the date will default to the current day, month, year,
15296 hour and minute. If this command is called to replace a timestamp at point,
15297 or to enter the second timestamp of a range, the default time is taken
15298 from the existing stamp. Furthermore, the command prefers the future,
15299 so if you are giving a date where the year is not given, and the day-month
15300 combination is already past in the current year, it will assume you
15301 mean next year. For details, see the manual. A few examples:
15303 3-2-5 --> 2003-02-05
15304 feb 15 --> currentyear-02-15
15305 2/15 --> currentyear-02-15
15306 sep 12 9 --> 2009-09-12
15307 12:45 --> today 12:45
15308 22 sept 0:34 --> currentyear-09-22 0:34
15309 12 --> currentyear-currentmonth-12
15310 Fri --> nearest Friday (today or later)
15313 Furthermore you can specify a relative date by giving, as the *first* thing
15314 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
15315 change in days weeks, months, years.
15316 With a single plus or minus, the date is relative to today. With a double
15317 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
15318 +4d --> four days from today
15319 +4 --> same as above
15320 +2w --> two weeks from today
15321 ++5 --> five days from default date
15323 The function understands only English month and weekday abbreviations.
15325 While prompting, a calendar is popped up - you can also select the
15326 date with the mouse (button 1). The calendar shows a period of three
15327 months. To scroll it to other months, use the keys `>' and `<'.
15328 If you don't like the calendar, turn it off with
15329 \(setq org-read-date-popup-calendar nil)
15331 With optional argument TO-TIME, the date will immediately be converted
15332 to an internal time.
15333 With an optional argument ORG-WITH-TIME, the prompt will suggest to
15334 also insert a time. Note that when ORG-WITH-TIME is not set, you can
15335 still enter a time, and this function will inform the calling routine
15336 about this change. The calling routine may then choose to change the
15337 format used to insert the time stamp into the buffer to include the time.
15338 With optional argument FROM-STRING, read from this string instead from
15339 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
15340 the time/date that is used for everything that is not specified by the
15342 (require 'parse-time)
15343 (let* ((org-time-stamp-rounding-minutes
15344 (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
15345 (org-dcst org-display-custom-times)
15346 (ct (org-current-time))
15347 (org-def (or org-overriding-default-time default-time ct))
15348 (org-defdecode (decode-time org-def))
15350 (when (< (nth 2 org-defdecode) org-extend-today-until)
15351 (setcar (nthcdr 2 org-defdecode) -1)
15352 (setcar (nthcdr 1 org-defdecode) 59)
15353 (setq org-def (apply 'encode-time org-defdecode)
15354 org-defdecode (decode-time org-def)))))
15355 (mouse-autoselect-window nil) ; Don't let the mouse jump
15356 (calendar-frame-setup nil)
15357 (calendar-setup nil)
15358 (calendar-move-hook nil)
15359 (calendar-view-diary-initially-flag nil)
15360 (calendar-view-holidays-initially-flag nil)
15361 (timestr (format-time-string
15362 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
15363 (prompt (concat (if prompt (concat prompt " ") "")
15364 (format "Date+time [%s]: " timestr)))
15365 ans (org-ans0 "") org-ans1 org-ans2 final)
15368 (from-string (setq ans from-string))
15369 (org-read-date-popup-calendar
15371 (save-window-excursion
15373 (org-eval-in-calendar '(setq cursor-type nil) t)
15376 (calendar-forward-day (- (time-to-days org-def)
15377 (calendar-absolute-from-gregorian
15378 (calendar-current-date))))
15379 (org-eval-in-calendar nil t)
15380 (let* ((old-map (current-local-map))
15381 (map (copy-keymap calendar-mode-map))
15382 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
15383 (org-defkey map (kbd "RET") 'org-calendar-select)
15384 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
15385 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
15386 (org-defkey minibuffer-local-map [(meta shift left)]
15387 (lambda () (interactive)
15388 (org-eval-in-calendar '(calendar-backward-month 1))))
15389 (org-defkey minibuffer-local-map [(meta shift right)]
15390 (lambda () (interactive)
15391 (org-eval-in-calendar '(calendar-forward-month 1))))
15392 (org-defkey minibuffer-local-map [(meta shift up)]
15393 (lambda () (interactive)
15394 (org-eval-in-calendar '(calendar-backward-year 1))))
15395 (org-defkey minibuffer-local-map [(meta shift down)]
15396 (lambda () (interactive)
15397 (org-eval-in-calendar '(calendar-forward-year 1))))
15398 (org-defkey minibuffer-local-map [?\e (shift left)]
15399 (lambda () (interactive)
15400 (org-eval-in-calendar '(calendar-backward-month 1))))
15401 (org-defkey minibuffer-local-map [?\e (shift right)]
15402 (lambda () (interactive)
15403 (org-eval-in-calendar '(calendar-forward-month 1))))
15404 (org-defkey minibuffer-local-map [?\e (shift up)]
15405 (lambda () (interactive)
15406 (org-eval-in-calendar '(calendar-backward-year 1))))
15407 (org-defkey minibuffer-local-map [?\e (shift down)]
15408 (lambda () (interactive)
15409 (org-eval-in-calendar '(calendar-forward-year 1))))
15410 (org-defkey minibuffer-local-map [(shift up)]
15411 (lambda () (interactive)
15412 (org-eval-in-calendar '(calendar-backward-week 1))))
15413 (org-defkey minibuffer-local-map [(shift down)]
15414 (lambda () (interactive)
15415 (org-eval-in-calendar '(calendar-forward-week 1))))
15416 (org-defkey minibuffer-local-map [(shift left)]
15417 (lambda () (interactive)
15418 (org-eval-in-calendar '(calendar-backward-day 1))))
15419 (org-defkey minibuffer-local-map [(shift right)]
15420 (lambda () (interactive)
15421 (org-eval-in-calendar '(calendar-forward-day 1))))
15422 (org-defkey minibuffer-local-map ">"
15423 (lambda () (interactive)
15424 (org-eval-in-calendar '(scroll-calendar-left 1))))
15425 (org-defkey minibuffer-local-map "<"
15426 (lambda () (interactive)
15427 (org-eval-in-calendar '(scroll-calendar-right 1))))
15428 (org-defkey minibuffer-local-map "\C-v"
15429 (lambda () (interactive)
15430 (org-eval-in-calendar
15431 '(calendar-scroll-left-three-months 1))))
15432 (org-defkey minibuffer-local-map "\M-v"
15433 (lambda () (interactive)
15434 (org-eval-in-calendar
15435 '(calendar-scroll-right-three-months 1))))
15436 (run-hooks 'org-read-date-minibuffer-setup-hook)
15439 (use-local-map map)
15440 (setq org-read-date-inactive inactive)
15441 (add-hook 'post-command-hook 'org-read-date-display)
15442 (setq org-ans0 (read-string prompt default-input
15443 'org-read-date-history nil))
15444 ;; org-ans0: from prompt
15445 ;; org-ans1: from mouse click
15446 ;; org-ans2: from calendar motion
15447 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
15448 (remove-hook 'post-command-hook 'org-read-date-display)
15449 (use-local-map old-map)
15450 (when org-read-date-overlay
15451 (delete-overlay org-read-date-overlay)
15452 (setq org-read-date-overlay nil)))))
15453 (bury-buffer "*Calendar*")))))
15455 (t ; Naked prompt only
15457 (setq ans (read-string prompt default-input
15458 'org-read-date-history timestr))
15459 (when org-read-date-overlay
15460 (delete-overlay org-read-date-overlay)
15461 (setq org-read-date-overlay nil)))))
15463 (setq final (org-read-date-analyze ans org-def org-defdecode))
15465 (when org-read-date-analyze-forced-year
15466 (message "Year was forced into %s"
15467 (if org-read-date-force-compatible-dates
15468 "compatible range (1970-2037)"
15469 "range representable on this machine"))
15472 ;; One round trip to get rid of 34th of August and stuff like that....
15473 (setq final (decode-time (apply 'encode-time final)))
15475 (setq org-read-date-final-answer ans)
15478 (apply 'encode-time final)
15479 (if (and (boundp 'org-time-was-given) org-time-was-given)
15480 (format "%04d-%02d-%02d %02d:%02d"
15481 (nth 5 final) (nth 4 final) (nth 3 final)
15482 (nth 2 final) (nth 1 final))
15483 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
15486 (defvar org-defdecode)
15487 (defvar org-with-time)
15488 (defun org-read-date-display ()
15489 "Display the current date prompt interpretation in the minibuffer."
15490 (when org-read-date-display-live
15491 (when org-read-date-overlay
15492 (delete-overlay org-read-date-overlay))
15493 (when (minibufferp (current-buffer))
15496 (while (not (equal (buffer-substring
15497 (max (point-min) (- (point) 4)) (point))
15500 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
15501 " " (or org-ans1 org-ans2)))
15502 (org-end-time-was-given nil)
15503 (f (org-read-date-analyze ans org-def org-defdecode))
15505 org-time-stamp-custom-formats
15506 org-time-stamp-formats))
15507 (fmt (if (or org-with-time
15508 (and (boundp 'org-time-was-given) org-time-was-given))
15511 (txt (format-time-string fmt (apply 'encode-time f)))
15512 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
15513 (txt (concat "=> " txt)))
15514 (when (and org-end-time-was-given
15515 (string-match org-plain-time-of-day-regexp txt))
15516 (setq txt (concat (substring txt 0 (match-end 0)) "-"
15517 org-end-time-was-given
15518 (substring txt (match-end 0)))))
15519 (when org-read-date-analyze-futurep
15520 (setq txt (concat txt " (=>F)")))
15521 (setq org-read-date-overlay
15522 (make-overlay (1- (point-at-eol)) (point-at-eol)))
15523 (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
15525 (defun org-read-date-analyze (ans org-def org-defdecode)
15526 "Analyze the combined answer of the date prompt."
15527 ;; FIXME: cleanup and comment
15528 (let ((nowdecode (decode-time (current-time)))
15529 delta deltan deltaw deltadef year month day
15530 hour minute second wday pm h2 m2 tl wday1
15531 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
15532 (setq org-read-date-analyze-futurep nil
15533 org-read-date-analyze-forced-year nil)
15534 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
15537 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
15538 (setq ans (replace-match "" t t ans)
15540 deltaw (nth 1 delta)
15541 deltadef (nth 2 delta)))
15543 ;; Check if there is an iso week date in there. If yes, store the
15544 ;; info and postpone interpreting it until the rest of the parsing
15546 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
15547 (setq iso-year (if (match-end 1)
15548 (org-small-year-to-year
15549 (string-to-number (match-string 1 ans))))
15550 iso-weekday (if (match-end 3)
15551 (string-to-number (match-string 3 ans)))
15552 iso-week (string-to-number (match-string 2 ans)))
15553 (setq ans (replace-match "" t t ans)))
15555 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
15556 (when (string-match
15557 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
15558 (setq year (if (match-end 2)
15559 (string-to-number (match-string 2 ans))
15560 (progn (setq kill-year t)
15561 (string-to-number (format-time-string "%Y"))))
15562 month (string-to-number (match-string 3 ans))
15563 day (string-to-number (match-string 4 ans)))
15564 (if (< year 100) (setq year (+ 2000 year)))
15565 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15568 ;; Help matching dotted european dates
15569 (when (string-match
15570 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
15571 (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
15573 (string-to-number (format-time-string "%Y")))
15574 day (string-to-number (match-string 1 ans))
15575 month (string-to-number (match-string 2 ans))
15576 ans (replace-match (format "%04d-%02d-%02d" year month day)
15579 ;; Help matching american dates, like 5/30 or 5/30/7
15580 (when (string-match
15581 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
15582 (setq year (if (match-end 4)
15583 (string-to-number (match-string 4 ans))
15584 (progn (setq kill-year t)
15585 (string-to-number (format-time-string "%Y"))))
15586 month (string-to-number (match-string 1 ans))
15587 day (string-to-number (match-string 2 ans)))
15588 (if (< year 100) (setq year (+ 2000 year)))
15589 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15591 ;; Help matching am/pm times, because `parse-time-string' does not do that.
15592 ;; If there is a time with am/pm, and *no* time without it, we convert
15593 ;; so that matching will be successful.
15594 (loop for i from 1 to 2 do ; twice, for end time as well
15595 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
15596 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
15597 (setq hour (string-to-number (match-string 1 ans))
15598 minute (if (match-end 3)
15599 (string-to-number (match-string 3 ans))
15602 (string-to-char (downcase (match-string 4 ans)))))
15603 (if (and (= hour 12) (not pm))
15605 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
15606 (setq ans (replace-match (format "%02d:%02d" hour minute)
15609 ;; Check if a time range is given as a duration
15610 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
15611 (setq hour (string-to-number (match-string 1 ans))
15612 h2 (+ hour (string-to-number (match-string 3 ans)))
15613 minute (string-to-number (match-string 2 ans))
15614 m2 (+ minute (if (match-end 5) (string-to-number
15615 (match-string 5 ans))0)))
15616 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
15617 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
15620 ;; Check if there is a time range
15621 (when (boundp 'org-end-time-was-given)
15622 (setq org-time-was-given nil)
15623 (when (and (string-match org-plain-time-of-day-regexp ans)
15625 (setq org-end-time-was-given (match-string 8 ans))
15626 (setq ans (concat (substring ans 0 (match-beginning 7))
15627 (substring ans (match-end 7))))))
15629 (setq tl (parse-time-string ans)
15630 day (or (nth 3 tl) (nth 3 org-defdecode))
15631 month (or (nth 4 tl)
15632 (if (and org-read-date-prefer-future
15633 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
15634 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
15635 (nth 4 org-defdecode)))
15636 year (or (and (not kill-year) (nth 5 tl))
15637 (if (and org-read-date-prefer-future
15638 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
15639 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
15640 (nth 5 org-defdecode)))
15641 hour (or (nth 2 tl) (nth 2 org-defdecode))
15642 minute (or (nth 1 tl) (nth 1 org-defdecode))
15643 second (or (nth 0 tl) 0)
15646 (when (and (eq org-read-date-prefer-future 'time)
15647 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
15648 (equal day (nth 3 nowdecode))
15649 (equal month (nth 4 nowdecode))
15650 (equal year (nth 5 nowdecode))
15652 (or (< (nth 2 tl) (nth 2 nowdecode))
15653 (and (= (nth 2 tl) (nth 2 nowdecode))
15655 (< (nth 1 tl) (nth 1 nowdecode)))))
15659 ;; Special date definitions below
15662 ;; There was an iso week
15665 (setq year (or iso-year year)
15666 day (or iso-weekday wday 1)
15667 wday nil ; to make sure that the trigger below does not match
15668 iso-date (calendar-gregorian-from-absolute
15669 (calendar-absolute-from-iso
15670 (list iso-week day year))))
15671 ; FIXME: Should we also push ISO weeks into the future?
15672 ; (when (and org-read-date-prefer-future
15674 ; (< (calendar-absolute-from-gregorian iso-date)
15675 ; (time-to-days (current-time))))
15676 ; (setq year (1+ year)
15677 ; iso-date (calendar-gregorian-from-absolute
15678 ; (calendar-absolute-from-iso
15679 ; (list iso-week day year)))))
15680 (setq month (car iso-date)
15681 year (nth 2 iso-date)
15682 day (nth 1 iso-date)))
15686 (let ((now (decode-time (current-time))))
15687 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
15688 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
15689 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
15690 ((equal deltaw "m") (setq month (+ month deltan)))
15691 ((equal deltaw "y") (setq year (+ year deltan)))))
15692 ((and wday (not (nth 3 tl)))
15693 ;; Weekday was given, but no day, so pick that day in the week
15694 ;; on or after the derived date.
15695 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
15696 (unless (equal wday wday1)
15697 (setq day (+ day (% (- wday wday1 -7) 7))))))
15698 (if (and (boundp 'org-time-was-given)
15700 (setq org-time-was-given t))
15701 (if (< year 100) (setq year (+ 2000 year)))
15702 ;; Check of the date is representable
15703 (if org-read-date-force-compatible-dates
15706 (setq year 1970 org-read-date-analyze-forced-year t))
15708 (setq year 2037 org-read-date-analyze-forced-year t)))
15709 (condition-case nil
15710 (ignore (encode-time second minute hour day month year))
15712 (setq year (nth 5 org-defdecode))
15713 (setq org-read-date-analyze-forced-year t))))
15714 (setq org-read-date-analyze-futurep futurep)
15715 (list second minute hour day month year)))
15717 (defvar parse-time-weekdays)
15718 (defun org-read-date-get-relative (s today default)
15719 "Check string S for special relative date string.
15720 TODAY and DEFAULT are internal times, for today and for a default.
15721 Return shift list (N what def-flag)
15722 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
15723 N is the number of WHATs to shift.
15724 DEF-FLAG is t when a double ++ or -- indicates shift relative to
15725 the DEFAULT date rather than TODAY."
15726 (require 'parse-time)
15730 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
15732 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
15733 "\\([ \t]\\|$\\)") s)
15734 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
15735 (let* ((dir (if (> (match-end 1) (match-beginning 1))
15736 (string-to-char (substring (match-string 1 s) -1))
15738 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
15739 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
15740 (what (if (match-end 3) (match-string 3 s) "d"))
15741 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
15742 (date (if rel default today))
15743 (wday (nth 6 (decode-time date)))
15747 (setq delta (mod (+ 7 (- wday1 wday)) 7))
15748 (if (= dir ?-) (setq delta (- delta 7)))
15749 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
15750 (list delta "d" rel))
15751 (list (* n (if (= dir ?-) -1 1)) what rel)))))
15753 (defun org-order-calendar-date-args (arg1 arg2 arg3)
15754 "Turn a user-specified date into the internal representation.
15755 The internal representation needed by the calendar is (month day year).
15756 This is a wrapper to handle the brain-dead convention in calendar that
15757 user function argument order change dependent on argument order."
15758 (if (boundp 'calendar-date-style)
15760 ((eq calendar-date-style 'american)
15761 (list arg1 arg2 arg3))
15762 ((eq calendar-date-style 'european)
15763 (list arg2 arg1 arg3))
15764 ((eq calendar-date-style 'iso)
15765 (list arg2 arg3 arg1)))
15766 (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
15767 (if (org-bound-and-true-p european-calendar-style)
15768 (list arg2 arg1 arg3)
15769 (list arg1 arg2 arg3)))))
15771 (defun org-eval-in-calendar (form &optional keepdate)
15772 "Eval FORM in the calendar window and return to current window.
15773 When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
15774 otherwise stick to the current value of `org-ans2'."
15775 (let ((sf (selected-frame))
15776 (sw (selected-window)))
15777 (select-window (get-buffer-window "*Calendar*" t))
15779 (when (and (not keepdate) (calendar-cursor-to-date))
15780 (let* ((date (calendar-cursor-to-date))
15781 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15782 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
15783 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
15785 (org-select-frame-set-input-focus sf)))
15787 (defun org-calendar-select ()
15788 "Return to `org-read-date' with the date currently selected.
15789 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15791 (when (calendar-cursor-to-date)
15792 (let* ((date (calendar-cursor-to-date))
15793 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15794 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15795 (if (active-minibuffer-window) (exit-minibuffer))))
15797 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
15798 "Insert a date stamp for the date given by the internal TIME.
15799 WITH-HM means use the stamp format that includes the time of the day.
15800 INACTIVE means use square brackets instead of angular ones, so that the
15801 stamp will not contribute to the agenda.
15802 PRE and POST are optional strings to be inserted before and after the
15804 The command returns the inserted time stamp."
15805 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
15807 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
15808 (insert-before-markers (or pre ""))
15809 (when (listp extra)
15810 (setq extra (car extra))
15811 (if (and (stringp extra)
15812 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
15813 (setq extra (format "-%02d:%02d"
15814 (string-to-number (match-string 1 extra))
15815 (string-to-number (match-string 2 extra))))
15818 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
15819 (insert-before-markers (setq stamp (format-time-string fmt time)))
15820 (insert-before-markers (or post ""))
15821 (setq org-last-inserted-timestamp stamp)))
15823 (defun org-toggle-time-stamp-overlays ()
15824 "Toggle the use of custom time stamp formats."
15826 (setq org-display-custom-times (not org-display-custom-times))
15827 (unless org-display-custom-times
15828 (let ((p (point-min)) (bmp (buffer-modified-p)))
15829 (while (setq p (next-single-property-change p 'display))
15830 (if (and (get-text-property p 'display)
15831 (eq (get-text-property p 'face) 'org-date))
15832 (remove-text-properties
15833 p (setq p (next-single-property-change p 'display))
15835 (set-buffer-modified-p bmp)))
15836 (if (featurep 'xemacs)
15837 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
15838 (org-restart-font-lock)
15839 (setq org-table-may-need-update t)
15840 (if org-display-custom-times
15841 (message "Time stamps are overlaid with custom format")
15842 (message "Time stamp overlays removed")))
15844 (defun org-display-custom-time (beg end)
15845 "Overlay modified time stamp format over timestamp between BEG and END."
15846 (let* ((ts (buffer-substring beg end))
15847 t1 w1 with-hm tf time str w2 (off 0))
15849 (setq t1 (org-parse-time-string ts t))
15850 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
15851 (setq off (- (match-end 0) (match-beginning 0)))))
15852 (setq end (- end off))
15853 (setq w1 (- end beg)
15854 with-hm (and (nth 1 t1) (nth 2 t1))
15855 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
15856 time (org-fix-decoded-time t1)
15858 (format-time-string
15859 (substring tf 1 -1) (apply 'encode-time time))
15860 nil 'mouse-face 'highlight)
15862 (if (not (= w2 w1))
15863 (add-text-properties (1+ beg) (+ 2 beg)
15864 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
15865 (if (featurep 'xemacs)
15867 (put-text-property beg end 'invisible t)
15868 (put-text-property beg end 'end-glyph (make-glyph str)))
15869 (put-text-property beg end 'display str))))
15871 (defun org-translate-time (string)
15872 "Translate all timestamps in STRING to custom format.
15873 But do this only if the variable `org-display-custom-times' is set."
15874 (when org-display-custom-times
15877 (re org-ts-regexp-both)
15878 t1 with-hm inactive tf time str beg end)
15879 (while (setq start (string-match re string start))
15880 (setq beg (match-beginning 0)
15882 t1 (save-match-data
15883 (org-parse-time-string (substring string beg end) t))
15884 with-hm (and (nth 1 t1) (nth 2 t1))
15885 inactive (equal (substring string beg (1+ beg)) "[")
15886 tf (funcall (if with-hm 'cdr 'car)
15887 org-time-stamp-custom-formats)
15888 time (org-fix-decoded-time t1)
15889 str (format-time-string
15891 (if inactive "[" "<") (substring tf 1 -1)
15892 (if inactive "]" ">"))
15893 (apply 'encode-time time))
15894 string (replace-match str t t string)
15895 start (+ start (length str)))))))
15898 (defun org-fix-decoded-time (time)
15899 "Set 0 instead of nil for the first 6 elements of time.
15900 Don't touch the rest."
15902 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
15904 (defun org-days-to-time (timestamp-string)
15905 "Difference between TIMESTAMP-STRING and now in days."
15906 (- (time-to-days (org-time-string-to-time timestamp-string))
15907 (time-to-days (current-time))))
15909 (defun org-deadline-close (timestamp-string &optional ndays)
15910 "Is the time in TIMESTAMP-STRING close to the current date?"
15911 (setq ndays (or ndays (org-get-wdays timestamp-string)))
15912 (and (< (org-days-to-time timestamp-string) ndays)
15913 (not (org-entry-is-done-p))))
15915 (defun org-get-wdays (ts)
15916 "Get the deadline lead time appropriate for timestring TS."
15918 ((<= org-deadline-warning-days 0)
15919 ;; 0 or negative, enforce this value no matter what
15920 (- org-deadline-warning-days))
15921 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
15922 ;; lead time is specified.
15923 (floor (* (string-to-number (match-string 1 ts))
15924 (cdr (assoc (match-string 2 ts)
15925 '(("d" . 1) ("w" . 7)
15926 ("m" . 30.4) ("y" . 365.25)))))))
15927 ;; go for the default.
15928 (t org-deadline-warning-days)))
15930 (defun org-calendar-select-mouse (ev)
15931 "Return to `org-read-date' with the date currently selected.
15932 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15934 (mouse-set-point ev)
15935 (when (calendar-cursor-to-date)
15936 (let* ((date (calendar-cursor-to-date))
15937 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15938 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15939 (if (active-minibuffer-window) (exit-minibuffer))))
15941 (defun org-check-deadlines (ndays)
15942 "Check if there are any deadlines due or past due.
15943 A deadline is considered due if it happens within `org-deadline-warning-days'
15944 days from today's date. If the deadline appears in an entry marked DONE,
15945 it is not shown. The prefix arg NDAYS can be used to test that many
15946 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
15948 (let* ((org-warn-days
15950 ((equal ndays '(4)) 100000)
15951 (ndays (prefix-numeric-value ndays))
15952 (t (abs org-deadline-warning-days))))
15953 (case-fold-search nil)
15954 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15956 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
15958 (message "%d deadlines past-due or due within %d days"
15959 (org-occur regexp nil callback)
15962 (defsubst org-re-timestamp (type)
15963 "Return a regexp for timestamp TYPE.
15964 Allowed values for TYPE are:
15966 all: all timestamps
15967 active: only active timestamps (<...>)
15968 inactive: only inactive timestamps ([...])
15969 scheduled: only scheduled timestamps
15970 deadline: only deadline timestamps
15972 When TYPE is nil, fall back on returning a regexp that matches
15973 both scheduled and deadline timestamps."
15974 (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9>
\n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
15975 ((eq type 'active) org-ts-regexp)
15976 ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^
\n>]*?\\)\\]")
15977 ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
15978 ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15979 ((eq type 'scheduled-or-deadline)
15980 (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
15982 (defun org-check-before-date (date)
15983 "Check if there are deadlines or scheduled entries before DATE."
15984 (interactive (list (org-read-date)))
15985 (let ((case-fold-search nil)
15986 (regexp (org-re-timestamp org-ts-type))
15988 (lambda () (time-less-p
15989 (org-time-string-to-time (match-string 1))
15990 (org-time-string-to-time date)))))
15991 (message "%d entries before %s"
15992 (org-occur regexp nil callback) date)))
15994 (defun org-check-after-date (date)
15995 "Check if there are deadlines or scheduled entries after DATE."
15996 (interactive (list (org-read-date)))
15997 (let ((case-fold-search nil)
15998 (regexp (org-re-timestamp org-ts-type))
16002 (org-time-string-to-time (match-string 1))
16003 (org-time-string-to-time date))))))
16004 (message "%d entries after %s"
16005 (org-occur regexp nil callback) date)))
16007 (defun org-check-dates-range (start-date end-date)
16008 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
16009 (interactive (list (org-read-date nil nil nil "Range starts")
16010 (org-read-date nil nil nil "Range end")))
16011 (let ((case-fold-search nil)
16012 (regexp (org-re-timestamp org-ts-type))
16015 (let ((match (match-string 1)))
16018 (org-time-string-to-time match)
16019 (org-time-string-to-time start-date)))
16021 (org-time-string-to-time match)
16022 (org-time-string-to-time end-date)))))))
16023 (message "%d entries between %s and %s"
16024 (org-occur regexp nil callback) start-date end-date)))
16026 (defun org-evaluate-time-range (&optional to-buffer)
16027 "Evaluate a time range by computing the difference between start and end.
16028 Normally the result is just printed in the echo area, but with prefix arg
16029 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
16030 If the time range is actually in a table, the result is inserted into the
16032 For time difference computation, a year is assumed to be exactly 365
16033 days in order to avoid rounding problems."
16036 (org-clock-update-time-maybe)
16038 (unless (org-at-date-range-p t)
16039 (goto-char (point-at-bol))
16040 (re-search-forward org-tr-regexp-both (point-at-eol) t))
16041 (if (not (org-at-date-range-p t))
16042 (error "Not at a time-stamp range, and none found in current line")))
16043 (let* ((ts1 (match-string 1))
16044 (ts2 (match-string 2))
16045 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
16046 (match-end (match-end 0))
16047 (time1 (org-time-string-to-time ts1))
16048 (time2 (org-time-string-to-time ts2))
16049 (t1 (org-float-time time1))
16050 (t2 (org-float-time time2))
16051 (diff (abs (- t2 t1)))
16052 (negative (< (- t2 t1) 0))
16053 ;; (ys (floor (* 365 24 60 60)))
16056 (fy "%dy %dd %02d:%02d")
16058 (fd "%dd %02d:%02d")
16063 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16065 d (floor (/ diff ds)) diff (mod diff ds)
16066 h (floor (/ diff hs)) diff (mod diff hs)
16067 m (floor (/ diff 60)))
16068 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16070 d (floor (+ (/ diff ds) 0.5))
16072 (if (not to-buffer)
16073 (message "%s" (org-make-tdiff-string y d h m))
16074 (if (org-at-table-p)
16076 (goto-char match-end)
16078 (and (looking-at " *|") (goto-char (match-end 0))))
16079 (goto-char match-end))
16081 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
16082 (replace-match ""))
16083 (if negative (insert " -"))
16084 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
16085 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
16086 (insert " " (format fh h m))))
16087 (if align (org-table-align))
16088 (message "Time difference inserted")))))
16090 (defun org-make-tdiff-string (y d h m)
16093 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
16095 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
16097 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
16099 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
16101 (apply 'format fmt (nreverse l))))
16103 (defun org-time-string-to-time (s &optional buffer pos)
16104 "Convert a timestamp string into internal time."
16105 (condition-case errdata
16106 (apply 'encode-time (org-parse-time-string s))
16107 (error (error "Bad timestamp `%s'%s\nError was: %s"
16108 s (if (not (and buffer pos))
16110 (format " at %d in buffer `%s'" pos buffer))
16113 (defun org-time-string-to-seconds (s)
16114 "Convert a timestamp string to a number of seconds."
16115 (org-float-time (org-time-string-to-time s)))
16117 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
16118 "Convert a time stamp to an absolute day number.
16119 If there is a specifier for a cyclic time stamp, get the closest date to
16121 PREFER and SHOW-ALL are passed through to `org-closest-date'.
16122 The variable date is bound by the calendar when this is called."
16124 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
16125 (if (org-diary-sexp-entry (match-string 1 s) "" date)
16128 ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
16129 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
16130 (time-to-days (current-time))) (match-string 0 s)
16133 (condition-case errdata
16134 (apply 'encode-time (org-parse-time-string s))
16135 (error (error "Bad timestamp `%s'%s\nError was: %s"
16136 s (if (not (and buffer pos))
16138 (format " at %d in buffer `%s'" pos buffer))
16139 (cdr errdata))))))))
16141 (defun org-days-to-iso-week (days)
16142 "Return the iso week number."
16144 (car (calendar-iso-from-absolute days)))
16146 (defun org-small-year-to-year (year)
16147 "Convert 2-digit years into 4-digit years.
16148 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
16149 The year 2000 cannot be abbreviated. Any year larger than 99
16150 is returned unchanged."
16152 (setq year (+ 2000 year))
16154 (setq year (+ 1900 year))))
16157 (defun org-time-from-absolute (d)
16158 "Return the time corresponding to date D.
16159 D may be an absolute day number, or a calendar-type list (month day year)."
16160 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
16161 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
16163 (defun org-calendar-holiday ()
16164 "List of holidays, for Diary display in Org-mode."
16165 (require 'holidays)
16167 (if (fboundp 'calendar-check-holidays)
16168 'calendar-check-holidays 'check-calendar-holidays) date)))
16169 (if hl (mapconcat 'identity hl "; "))))
16171 (defun org-diary-sexp-entry (sexp entry date)
16172 "Process a SEXP diary ENTRY for DATE."
16173 (require 'diary-lib)
16174 (let ((result (if calendar-debug-sexp
16175 (let ((stack-trace-on-error t))
16176 (eval (car (read-from-string sexp))))
16177 (condition-case nil
16178 (eval (car (read-from-string sexp)))
16181 (message "Bad sexp at line %d in %s: %s"
16183 (buffer-file-name) sexp)
16185 (cond ((stringp result) (split-string result "; "))
16186 ((and (consp result)
16187 (not (consp (cdr result)))
16188 (stringp (cdr result))) (cdr result))
16189 ((and (consp result)
16190 (stringp (car result))) result)
16193 (defun org-diary-to-ical-string (frombuf)
16194 "Get iCalendar entries from diary entries in buffer FROMBUF.
16195 This uses the icalendar.el library."
16196 (let* ((tmpdir (if (featurep 'xemacs)
16198 temporary-file-directory))
16199 (tmpfile (make-temp-name
16200 (expand-file-name "orgics" tmpdir)))
16202 (with-current-buffer frombuf
16203 (icalendar-export-region (point-min) (point-max) tmpfile)
16204 (setq buf (find-buffer-visiting tmpfile))
16206 (goto-char (point-min))
16207 (if (re-search-forward "^BEGIN:VEVENT" nil t)
16208 (setq b (match-beginning 0)))
16209 (goto-char (point-max))
16210 (if (re-search-backward "^END:VEVENT" nil t)
16211 (setq e (match-end 0)))
16212 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
16214 (delete-file tmpfile)
16217 (defun org-closest-date (start current change prefer show-all)
16218 "Find the date closest to CURRENT that is consistent with START and CHANGE.
16219 When PREFER is `past', return a date that is either CURRENT or past.
16220 When PREFER is `future', return a date that is either CURRENT or future.
16221 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
16222 ;; Make the proper lists from the dates
16224 (let ((a1 '(("h" . hour)
16229 (shour (nth 2 (org-parse-time-string start)))
16230 dn dw sday cday n1 n2 n0
16231 d m y y1 y2 date1 date2 nmonths nm ny m2)
16233 (setq start (org-date-to-gregorian start)
16234 current (org-date-to-gregorian
16237 (time-to-days (current-time))))
16238 sday (calendar-absolute-from-gregorian start)
16239 cday (calendar-absolute-from-gregorian current))
16241 (if (<= cday sday) (throw 'exit sday))
16243 (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
16244 (setq dn (string-to-number (match-string 1 change))
16245 dw (cdr (assoc (match-string 2 change) a1)))
16246 (error "Invalid change specifier: %s" change))
16247 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
16250 (let ((missing-hours
16251 (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
16253 (setq n1 (if (zerop missing-hours) cday
16254 (- cday (1+ (floor (/ missing-hours 24)))))
16255 n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
16257 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
16260 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
16261 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
16262 (setq date1 (list m d y1)
16263 n1 (calendar-absolute-from-gregorian date1)
16264 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
16265 n2 (calendar-absolute-from-gregorian date2)))
16267 ;; approx number of month between the two dates
16268 (setq nmonths (floor (/ (- cday sday) 30.436875)))
16269 ;; How often does dn fit in there?
16270 (setq d (nth 1 start) m (car start) y (nth 2 start)
16271 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
16273 ny (floor (/ m 12))
16276 (while (> m 12) (setq m (- m 12) y (1+ y)))
16277 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
16278 (setq m2 (+ m dn) y2 y)
16279 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16280 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
16281 (while (<= n2 cday)
16282 (setq n1 n2 m m2 y y2)
16283 (setq m2 (+ m dn) y2 y)
16284 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16285 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
16286 ;; Make sure n1 is the earlier date
16287 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
16290 ((eq prefer 'past) (if (= cday n2) n2 n1))
16291 ((eq prefer 'future) (if (= cday n1) n1 n2))
16292 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
16294 ((eq prefer 'past) (if (= cday n2) n2 n1))
16295 ((eq prefer 'future) (if (= cday n1) n1 n2))
16296 (t (if (= cday n1) n1 n2)))))))
16298 (defun org-date-to-gregorian (date)
16299 "Turn any specification of DATE into a Gregorian date for the calendar."
16300 (cond ((integerp date) (calendar-gregorian-from-absolute date))
16301 ((and (listp date) (= (length date) 3)) date)
16303 (setq date (org-parse-time-string date))
16304 (list (nth 4 date) (nth 3 date) (nth 5 date)))
16306 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
16308 (defun org-parse-time-string (s &optional nodefault)
16309 "Parse the standard Org-mode time string.
16310 This should be a lot faster than the normal `parse-time-string'.
16311 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
16312 hour and minute fields will be nil if not given."
16313 (if (string-match org-ts-regexp0 s)
16315 (if (or (match-beginning 8) (not nodefault))
16316 (string-to-number (or (match-string 8 s) "0")))
16317 (if (or (match-beginning 7) (not nodefault))
16318 (string-to-number (or (match-string 7 s) "0")))
16319 (string-to-number (match-string 4 s))
16320 (string-to-number (match-string 3 s))
16321 (string-to-number (match-string 2 s))
16323 (error "Not a standard Org-mode time string: %s" s)))
16325 (defun org-timestamp-up (&optional arg)
16326 "Increase the date item at the cursor by one.
16327 If the cursor is on the year, change the year. If it is on the month,
16328 the day or the time, change that.
16329 With prefix ARG, change by that many units."
16331 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
16333 (defun org-timestamp-down (&optional arg)
16334 "Decrease the date item at the cursor by one.
16335 If the cursor is on the year, change the year. If it is on the month,
16336 the day or the time, change that.
16337 With prefix ARG, change by that many units."
16339 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
16341 (defun org-timestamp-up-day (&optional arg)
16342 "Increase the date in the time stamp by one day.
16343 With prefix ARG, change that many days."
16345 (if (and (not (org-at-timestamp-p t))
16346 (org-at-heading-p))
16348 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
16350 (defun org-timestamp-down-day (&optional arg)
16351 "Decrease the date in the time stamp by one day.
16352 With prefix ARG, change that many days."
16354 (if (and (not (org-at-timestamp-p t))
16355 (org-at-heading-p))
16357 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
16359 (defun org-at-timestamp-p (&optional inactive-ok)
16360 "Determine if the cursor is in or at a timestamp."
16362 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
16364 (ans (or (looking-at tsr)
16366 (skip-chars-backward "^[<\n\r\t")
16367 (if (> (point) (point-min)) (backward-char 1))
16368 (and (looking-at tsr)
16369 (> (- (match-end 0) pos) -1))))))
16371 (boundp 'org-ts-what)
16374 ((= pos (match-beginning 0)) 'bracket)
16375 ;; Point is considered to be "on the bracket" whether
16376 ;; it's really on it or right after it.
16377 ((= pos (1- (match-end 0))) 'bracket)
16378 ((= pos (match-end 0)) 'after)
16379 ((org-pos-in-match-range pos 2) 'year)
16380 ((org-pos-in-match-range pos 3) 'month)
16381 ((org-pos-in-match-range pos 7) 'hour)
16382 ((org-pos-in-match-range pos 8) 'minute)
16383 ((or (org-pos-in-match-range pos 4)
16384 (org-pos-in-match-range pos 5)) 'day)
16385 ((and (> pos (or (match-end 8) (match-end 5)))
16386 (< pos (match-end 0)))
16387 (- pos (or (match-end 8) (match-end 5))))
16391 (defun org-toggle-timestamp-type ()
16392 "Toggle the type (<active> or [inactive]) of a time stamp."
16394 (when (org-at-timestamp-p t)
16395 (let ((beg (match-beginning 0)) (end (match-end 0))
16396 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
16399 (while (re-search-forward "[][<>]" end t)
16400 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
16402 (message "Timestamp is now %sactive"
16403 (if (equal (char-after beg) ?<) "" "in")))))
16405 (defvar org-clock-history) ; defined in org-clock.el
16406 (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
16407 (defun org-timestamp-change (n &optional what updown)
16408 "Change the date in the time stamp at point.
16409 The date will be changed by N times WHAT. WHAT can be `day', `month',
16410 `year', `minute', `second'. If WHAT is not given, the cursor position
16411 in the timestamp determines what will be changed."
16412 (let ((origin (point)) origin-cat
16414 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
16417 ts time time0 fixnext clrgx)
16418 (if (not (org-at-timestamp-p t))
16419 (error "Not at a timestamp"))
16420 (if (and (not what) (eq org-ts-what 'bracket))
16421 (org-toggle-timestamp-type)
16422 ;; Point isn't on brackets. Remember the part of the time-stamp
16423 ;; the point was in. Indeed, size of time-stamps may change,
16424 ;; but point must be kept in the same category nonetheless.
16425 (setq origin-cat org-ts-what)
16426 (if (and (not what) (not (eq org-ts-what 'day))
16427 org-display-custom-times
16428 (get-text-property (point) 'display)
16429 (not (get-text-property (1- (point)) 'display)))
16430 (setq org-ts-what 'day))
16431 (setq org-ts-what (or what org-ts-what)
16432 inactive (= (char-after (match-beginning 0)) ?\[)
16433 ts (match-string 0))
16436 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
16438 (setq extra (match-string 1 ts)))
16439 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
16441 (setq time0 (org-parse-time-string ts))
16443 (eq org-ts-what 'minute)
16444 (not current-prefix-arg))
16445 ;; This looks like s-up and s-down. Change by one rounding step.
16446 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
16447 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
16448 (setcar (cdr time0) (+ (nth 1 time0)
16449 (if (> n 0) (- rem) (- dm rem))))))
16451 (encode-time (or (car time0) 0)
16452 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
16453 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
16454 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
16455 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
16456 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
16458 (when (and (member org-ts-what '(hour minute))
16460 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
16461 (setq extra (org-modify-ts-extra
16463 (if (eq org-ts-what 'hour) 2 5)
16465 (when (integerp org-ts-what)
16466 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
16467 (if (eq what 'calendar)
16468 (let ((cal-date (org-get-date-from-calendar)))
16469 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
16470 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
16471 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
16472 (setcar time0 (or (car time0) 0))
16473 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
16474 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
16475 (setq time (apply 'encode-time time0))))
16476 ;; Insert the new time-stamp, and ensure point stays in the same
16477 ;; category as before (i.e. not after the last position in that
16479 (let ((pos (point)))
16480 ;; Stay before inserted string. `save-excursion' is of no use.
16481 (setq org-last-changed-timestamp
16482 (org-insert-time-stamp time with-hm inactive nil nil extra))
16485 (looking-at org-ts-regexp3)
16487 ;; `day' category ends before `hour' if any, or at
16488 ;; the end of the day name.
16489 ((eq origin-cat 'day)
16490 (min (or (match-beginning 7) (1- (match-end 5))) origin))
16491 ((eq origin-cat 'hour) (min (match-end 7) origin))
16492 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
16493 ((integerp origin-cat) (min (1- (match-end 0)) origin))
16494 ;; `year' and `month' have both fixed size: point
16495 ;; couldn't have moved into another part.
16497 ;; Update clock if on a CLOCK line.
16498 (org-clock-update-time-maybe)
16499 ;; Maybe adjust the closest clock in `org-clock-history'
16500 (when org-clock-adjust-closest
16501 (if (not (and (org-at-clock-log-p)
16502 (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
16503 org-clock-history))))))
16504 (message "No clock to adjust")
16505 (cond ((save-excursion ; fix previous clock?
16506 (re-search-backward org-ts-regexp0 nil t)
16507 (org-looking-back (concat org-clock-string " \\[")))
16508 (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
16509 ((save-excursion ; fix next clock?
16510 (re-search-backward org-ts-regexp0 nil t)
16511 (looking-at (concat org-ts-regexp0 "\\] =>")))
16512 (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
16513 (save-window-excursion
16514 ;; Find closest clock to point, adjust the previous/next one in history
16515 (let* ((p (save-excursion (org-back-to-heading t)))
16516 (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
16518 (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
16519 (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
16521 (message "No clock to adjust")
16523 (org-goto-marker-or-bmk clfixpos)
16525 (when (re-search-forward clrgx nil t)
16526 (goto-char (match-beginning 1))
16527 (let (org-clock-adjust-closest)
16528 (org-timestamp-change n org-ts-what updown))
16529 (message "Clock adjusted in %s for heading: %s"
16530 (file-name-nondirectory (buffer-file-name))
16531 (org-get-heading t t)))))))))
16532 ;; Try to recenter the calendar window, if any.
16533 (if (and org-calendar-follow-timestamp-change
16534 (get-buffer-window "*Calendar*" t)
16535 (memq org-ts-what '(day month year)))
16536 (org-recenter-calendar (time-to-days time))))))
16538 (defun org-modify-ts-extra (s pos n dm)
16539 "Change the different parts of the lead-time and repeat fields in timestamp."
16540 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
16542 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
16544 ((or (org-pos-in-match-range pos 2)
16545 (org-pos-in-match-range pos 3))
16546 (setq m (string-to-number (match-string 3 s))
16547 h (string-to-number (match-string 2 s)))
16548 (if (org-pos-in-match-range pos 2)
16550 (setq n (* dm (org-no-warnings (signum n))))
16551 (when (not (= 0 (setq rem (% m dm))))
16552 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
16554 (if (< m 0) (setq m (+ m 60) h (1- h)))
16555 (if (> m 59) (setq m (- m 60) h (1+ h)))
16556 (setq h (min 24 (max 0 h)))
16557 (setq ng 1 new (format "-%02d:%02d" h m)))
16558 ((org-pos-in-match-range pos 6)
16559 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
16560 ((org-pos-in-match-range pos 5)
16561 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
16563 ((org-pos-in-match-range pos 9)
16564 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
16565 ((org-pos-in-match-range pos 8)
16566 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
16570 (substring s 0 (match-beginning ng))
16572 (substring s (match-end ng))))))
16575 (defun org-recenter-calendar (date)
16576 "If the calendar is visible, recenter it to DATE."
16577 (let ((cwin (get-buffer-window "*Calendar*" t)))
16579 (let ((calendar-move-hook nil))
16580 (with-selected-window cwin
16581 (calendar-goto-date (if (listp date) date
16582 (calendar-gregorian-from-absolute date))))))))
16584 (defun org-goto-calendar (&optional arg)
16585 "Go to the Emacs calendar at the current date.
16586 If there is a time stamp in the current line, go to that date.
16587 A prefix ARG can be used to force the current date."
16589 (let ((tsr org-ts-regexp) diff
16590 (calendar-move-hook nil)
16591 (calendar-view-holidays-initially-flag nil)
16592 (calendar-view-diary-initially-flag nil))
16593 (if (or (org-at-timestamp-p)
16595 (beginning-of-line 1)
16596 (looking-at (concat ".*" tsr))))
16597 (let ((d1 (time-to-days (current-time)))
16599 (org-time-string-to-time (match-string 1)))))
16600 (setq diff (- d2 d1))))
16602 (calendar-goto-today)
16603 (if (and diff (not arg)) (calendar-forward-day diff))))
16605 (defun org-get-date-from-calendar ()
16606 "Return a list (month day year) of date at point in calendar."
16607 (with-current-buffer "*Calendar*"
16609 (calendar-cursor-to-date))))
16611 (defun org-date-from-calendar ()
16612 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
16613 If there is already a time stamp at the cursor position, update it."
16615 (if (org-at-timestamp-p t)
16616 (org-timestamp-change 0 'calendar)
16617 (let ((cal-date (org-get-date-from-calendar)))
16618 (org-insert-time-stamp
16619 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
16621 (defun org-minutes-to-hh:mm-string (m)
16622 "Compute H:MM from a number of minutes."
16623 (let ((h (/ m 60)))
16624 (setq m (- m (* 60 h)))
16625 (format org-time-clocksum-format h m)))
16627 (defun org-hh:mm-string-to-minutes (s)
16628 "Convert a string H:MM to a number of minutes.
16629 If the string is just a number, interpret it as minutes.
16630 In fact, the first hh:mm or number in the string will be taken,
16631 there can be extra stuff in the string.
16632 If no number is found, the return value is 0."
16635 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
16636 (+ (* (string-to-number (match-string 1 s)) 60)
16637 (string-to-number (match-string 2 s))))
16638 ((string-match "\\([0-9]+\\)" s)
16639 (string-to-number (match-string 1 s)))
16642 (defcustom org-effort-durations
16645 ("w" . ,(* 60 8 5))
16646 ("m" . ,(* 60 8 5 4))
16647 ("y" . ,(* 60 8 5 40)))
16648 "Conversion factor to minutes for an effort modifier.
16650 Each entry has the form (MODIFIER . MINUTES).
16652 In an effort string, a number followed by MODIFIER is multiplied
16653 by the specified number of MINUTES to obtain an effort in
16656 For example, if the value of this variable is ((\"hours\" . 60)), then an
16657 effort string \"2hours\" is equivalent to 120 minutes."
16660 :type '(alist :key-type (string :tag "Modifier")
16661 :value-type (number :tag "Minutes")))
16663 (defcustom org-agenda-inhibit-startup t
16664 "Inhibit startup when preparing agenda buffers.
16665 When this variable is `t' (the default), the initialization of
16666 the Org agenda buffers is inhibited: e.g. the visibility state
16667 is not set, the tables are not re-aligned, etc."
16670 :group 'org-agenda)
16672 (defun org-duration-string-to-minutes (s &optional output-to-string)
16673 "Convert a duration string S to minutes.
16675 A bare number is interpreted as minutes, modifiers can be set by
16676 customizing `org-effort-durations' (which see).
16678 Entries containing a colon are interpreted as H:MM by
16679 `org-hh:mm-string-to-minutes'."
16681 (re (concat "\\([0-9.]+\\) *\\("
16682 (regexp-opt (mapcar 'car org-effort-durations))
16684 (while (string-match re s)
16685 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
16686 (string-to-number (match-string 1 s))))
16687 (setq s (replace-match "" nil t s)))
16688 (setq result (floor result))
16689 (incf result (org-hh:mm-string-to-minutes s))
16690 (if output-to-string (number-to-string result) result)))
16694 (defun org-save-all-org-buffers ()
16695 "Save all Org-mode buffers without user confirmation."
16697 (message "Saving all Org-mode buffers...")
16698 (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
16699 (when (featurep 'org-id) (org-id-locations-save))
16700 (message "Saving all Org-mode buffers... done"))
16702 (defun org-revert-all-org-buffers ()
16703 "Revert all Org-mode buffers.
16704 Prompt for confirmation when there are unsaved changes.
16705 Be sure you know what you are doing before letting this function
16706 overwrite your changes.
16708 This function is useful in a setup where one tracks org files
16709 with a version control system, to revert on one machine after pulling
16710 changes from another. I believe the procedure must be like this:
16712 1. M-x org-save-all-org-buffers
16713 2. Pull changes from the other machine, resolve conflicts
16714 3. M-x org-revert-all-org-buffers"
16716 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
16719 (save-window-excursion
16722 (when (and (with-current-buffer b (derived-mode-p 'org-mode))
16723 (with-current-buffer b buffer-file-name))
16724 (org-pop-to-buffer-same-window b)
16725 (revert-buffer t 'no-confirm)))
16727 (when (and (featurep 'org-id) org-id-track-globally)
16728 (org-id-locations-load)))))
16733 (defun org-switchb (&optional arg)
16734 "Switch between Org buffers.
16735 With one prefix argument, restrict available buffers to files.
16736 With two prefix arguments, restrict available buffers to agenda files.
16738 Defaults to `iswitchb' for buffer name completion.
16739 Set `org-completion-use-ido' to make it use ido instead."
16741 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
16742 ((equal arg '(16)) (org-buffer-list 'agenda))
16743 (t (org-buffer-list))))
16744 (org-completion-use-iswitchb org-completion-use-iswitchb)
16745 (org-completion-use-ido org-completion-use-ido))
16746 (unless (or org-completion-use-ido org-completion-use-iswitchb)
16747 (setq org-completion-use-iswitchb t))
16748 (org-pop-to-buffer-same-window
16749 (org-icompleting-read "Org buffer: "
16750 (mapcar 'list (mapcar 'buffer-name blist))
16753 ;;; Define some older names previously used for this functionality
16755 (defalias 'org-ido-switchb 'org-switchb)
16757 (defalias 'org-iswitchb 'org-switchb)
16759 (defun org-buffer-list (&optional predicate exclude-tmp)
16760 "Return a list of Org buffers.
16761 PREDICATE can be `export', `files' or `agenda'.
16763 export restrict the list to Export buffers.
16764 files restrict the list to buffers visiting Org files.
16765 agenda restrict the list to buffers visiting agenda files.
16767 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
16769 (agenda-files (and (eq predicate 'agenda)
16770 (mapcar 'file-truename (org-agenda-files t))))
16773 ((eq predicate 'files)
16774 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
16775 ((eq predicate 'export)
16776 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
16777 ((eq predicate 'agenda)
16779 (with-current-buffer b
16780 (and (derived-mode-p 'org-mode)
16781 (setq bfn (buffer-file-name b))
16782 (member (file-truename bfn) agenda-files)))))
16783 (t (lambda (b) (with-current-buffer b
16784 (or (derived-mode-p 'org-mode)
16785 (string-match "\*Org .*Export"
16786 (buffer-name b)))))))))
16790 (if (and (funcall filter b)
16791 (or (not exclude-tmp)
16792 (not (string-match "tmp" (buffer-name b)))))
16797 (defun org-agenda-files (&optional unrestricted archives)
16798 "Get the list of agenda files.
16799 Optional UNRESTRICTED means return the full list even if a restriction
16800 is currently in place.
16801 When ARCHIVES is t, include all archive files that are really being
16802 used by the agenda files. If ARCHIVE is `ifmode', do this only if
16803 `org-agenda-archives-mode' is t."
16806 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
16807 ((stringp org-agenda-files) (org-read-agenda-file-list))
16808 ((listp org-agenda-files) org-agenda-files)
16809 (t (error "Invalid value of `org-agenda-files'")))))
16810 (setq files (apply 'append
16811 (mapcar (lambda (f)
16812 (if (file-directory-p f)
16814 f t org-agenda-file-regexp)
16817 (when org-agenda-skip-unavailable-files
16818 (setq files (delq nil
16821 (and (file-readable-p file) file)))
16823 (when (or (eq archives t)
16824 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
16825 (setq files (org-add-archive-files files)))
16828 (defun org-agenda-file-p (&optional file)
16829 "Return non-nil, if FILE is an agenda file.
16830 If FILE is omitted, use the file associated with the current
16832 (member (or file (buffer-file-name))
16833 (org-agenda-files t)))
16835 (defun org-edit-agenda-file-list ()
16836 "Edit the list of agenda files.
16837 Depending on setup, this either uses customize to edit the variable
16838 `org-agenda-files', or it visits the file that is holding the list. In the
16839 latter case, the buffer is set up in a way that saving it automatically kills
16840 the buffer and restores the previous window configuration."
16842 (if (stringp org-agenda-files)
16843 (let ((cw (current-window-configuration)))
16844 (find-file org-agenda-files)
16845 (org-set-local 'org-window-configuration cw)
16846 (org-add-hook 'after-save-hook
16848 (set-window-configuration
16849 (prog1 org-window-configuration
16850 (kill-buffer (current-buffer))))
16851 (org-install-agenda-files-menu)
16852 (message "New agenda file list installed"))
16854 (message "%s" (substitute-command-keys
16855 "Edit list and finish with \\[save-buffer]")))
16856 (customize-variable 'org-agenda-files)))
16858 (defun org-store-new-agenda-file-list (list)
16859 "Set new value for the agenda file list and save it correctly."
16860 (if (stringp org-agenda-files)
16861 (let ((fe (org-read-agenda-file-list t)) b u)
16862 (while (setq b (find-buffer-visiting org-agenda-files))
16864 (with-temp-file org-agenda-files
16867 (lambda (f) ;; Keep un-expanded entries.
16868 (if (setq u (assoc f fe))
16873 (let ((org-mode-hook nil) (org-inhibit-startup t)
16874 (org-insert-mode-line-in-empty-file nil))
16875 (setq org-agenda-files list)
16876 (customize-save-variable 'org-agenda-files org-agenda-files))))
16878 (defun org-read-agenda-file-list (&optional pair-with-expansion)
16879 "Read the list of agenda files from a file.
16880 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
16881 filenames, used by `org-store-new-agenda-file-list' to write back
16882 un-expanded file names."
16883 (when (file-directory-p org-agenda-files)
16884 (error "`org-agenda-files' cannot be a single directory"))
16885 (when (stringp org-agenda-files)
16887 (insert-file-contents org-agenda-files)
16890 (let ((e (expand-file-name (substitute-in-file-name f)
16892 (if pair-with-expansion
16895 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
16898 (defun org-cycle-agenda-files ()
16899 "Cycle through the files in `org-agenda-files'.
16900 If the current buffer visits an agenda file, find the next one in the list.
16901 If the current buffer does not, find the first agenda file."
16903 (let* ((fs (org-agenda-files t))
16904 (files (append fs (list (car fs))))
16905 (tcf (if buffer-file-name (file-truename buffer-file-name)))
16907 (unless files (error "No agenda files"))
16909 (while (setq file (pop files))
16910 (if (equal (file-truename file) tcf)
16912 (find-file (car files))
16914 (find-file (car fs)))
16915 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
16917 (defun org-agenda-file-to-front (&optional to-end)
16918 "Move/add the current file to the top of the agenda file list.
16919 If the file is not present in the list, it is added to the front. If it is
16920 present, it is moved there. With optional argument TO-END, add/move to the
16923 (let ((org-agenda-skip-unavailable-files nil)
16924 (file-alist (mapcar (lambda (x)
16925 (cons (file-truename x) x))
16926 (org-agenda-files t)))
16927 (ctf (file-truename
16928 (or buffer-file-name
16929 (error "Please save the current buffer to a file"))))
16931 (setq x (assoc ctf file-alist) had x)
16933 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
16935 (setq file-alist (append (delq x file-alist) (list x)))
16936 (setq file-alist (cons x (delq x file-alist))))
16937 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
16938 (org-install-agenda-files-menu)
16939 (message "File %s to %s of agenda file list"
16940 (if had "moved" "added") (if to-end "end" "front"))))
16942 (defun org-remove-file (&optional file)
16943 "Remove current file from the list of files in variable `org-agenda-files'.
16944 These are the files which are being checked for agenda entries.
16945 Optional argument FILE means use this file instead of the current."
16947 (let* ((org-agenda-skip-unavailable-files nil)
16948 (file (or file buffer-file-name
16949 (error "Current buffer does not visit a file")))
16950 (true-file (file-truename file))
16951 (afile (abbreviate-file-name file))
16952 (files (delq nil (mapcar
16954 (if (equal true-file
16957 (org-agenda-files t)))))
16958 (if (not (= (length files) (length (org-agenda-files t))))
16960 (org-store-new-agenda-file-list files)
16961 (org-install-agenda-files-menu)
16962 (message "Removed file: %s" afile))
16963 (message "File was not in list: %s (not removed)" afile))))
16965 (defun org-file-menu-entry (file)
16966 (vector file (list 'find-file file) t))
16968 (defun org-check-agenda-file (file)
16969 "Make sure FILE exists. If not, ask user what to do."
16970 (when (not (file-exists-p file))
16971 (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
16972 (abbreviate-file-name file))
16973 (let ((r (downcase (read-char-exclusive))))
16976 (org-remove-file file)
16977 (throw 'nextfile t))
16978 (t (error "Abort"))))))
16980 (defun org-get-agenda-file-buffer (file)
16981 "Get a buffer visiting FILE. If the buffer needs to be created, add
16982 it to the list of buffers which might be released later."
16983 (let ((buf (org-find-base-buffer-visiting file)))
16985 buf ; just return it
16986 ;; Make a new buffer and remember it
16987 (setq buf (find-file-noselect file))
16988 (if buf (push buf org-agenda-new-buffers))
16991 (defun org-release-buffers (blist)
16992 "Release all buffers in list, asking the user for confirmation when needed.
16993 When a buffer is unmodified, it is just killed. When modified, it is saved
16994 \(if the user agrees) and then killed."
16996 (while (setq buf (pop blist))
16997 (setq file (buffer-file-name buf))
16998 (when (and (buffer-modified-p buf)
17000 (y-or-n-p (format "Save file %s? " file)))
17001 (with-current-buffer buf (save-buffer)))
17002 (kill-buffer buf))))
17004 (defun org-agenda-prepare-buffers (files)
17005 "Create buffers for all agenda files, protect archived trees and comments."
17007 (let ((pa '(:org-archived t))
17008 (pc '(:org-comment t))
17009 (pall '(:org-archived t :org-comment t))
17010 (inhibit-read-only t)
17011 (org-inhibit-startup org-agenda-inhibit-startup)
17012 (rea (concat ":" org-archive-tag ":"))
17016 (while (setq file (pop files))
17020 (org-check-agenda-file file)
17021 (set-buffer (org-get-agenda-file-buffer file)))
17023 (setq bmp (buffer-modified-p))
17024 (org-refresh-category-properties)
17025 (org-refresh-properties org-effort-property 'org-effort)
17026 (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
17027 (setq org-todo-keywords-for-agenda
17028 (append org-todo-keywords-for-agenda org-todo-keywords-1))
17029 (setq org-done-keywords-for-agenda
17030 (append org-done-keywords-for-agenda org-done-keywords))
17031 (setq org-todo-keyword-alist-for-agenda
17032 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
17033 (setq org-drawers-for-agenda
17034 (append org-drawers-for-agenda org-drawers))
17035 (setq org-tag-alist-for-agenda
17036 (append org-tag-alist-for-agenda org-tag-alist))
17039 (remove-text-properties (point-min) (point-max) pall)
17040 (when org-agenda-skip-archived-trees
17041 (goto-char (point-min))
17042 (while (re-search-forward rea nil t)
17043 (if (org-at-heading-p t)
17044 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
17045 (goto-char (point-min))
17046 (setq re (format org-heading-keyword-regexp-format
17047 org-comment-string))
17048 (while (re-search-forward re nil t)
17049 (add-text-properties
17050 (match-beginning 0) (org-end-of-subtree t) pc)))
17051 (set-buffer-modified-p bmp)))))
17052 (setq org-todo-keywords-for-agenda
17053 (org-uniquify org-todo-keywords-for-agenda))
17054 (setq org-todo-keyword-alist-for-agenda
17055 (org-uniquify org-todo-keyword-alist-for-agenda)
17056 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
17058 ;;;; Embedded LaTeX
17060 (defvar org-cdlatex-mode-map (make-sparse-keymap)
17061 "Keymap for the minor `org-cdlatex-mode'.")
17063 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
17064 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
17065 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
17066 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
17067 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
17069 (defvar org-cdlatex-texmathp-advice-is-done nil
17070 "Flag remembering if we have applied the advice to texmathp already.")
17072 (define-minor-mode org-cdlatex-mode
17073 "Toggle the minor `org-cdlatex-mode'.
17074 This mode supports entering LaTeX environment and math in LaTeX fragments
17076 \\{org-cdlatex-mode-map}"
17078 (when org-cdlatex-mode
17080 (run-hooks 'cdlatex-mode-hook)
17081 (cdlatex-compute-tables))
17082 (unless org-cdlatex-texmathp-advice-is-done
17083 (setq org-cdlatex-texmathp-advice-is-done t)
17084 (defadvice texmathp (around org-math-always-on activate)
17085 "Always return t in org-mode buffers.
17086 This is because we want to insert math symbols without dollars even outside
17087 the LaTeX math segments. If Orgmode thinks that point is actually inside
17088 an embedded LaTeX fragment, let texmathp do its job.
17089 \\[org-cdlatex-mode-map]"
17093 ((not (derived-mode-p 'org-mode)) ad-do-it)
17094 ((eq this-command 'cdlatex-math-symbol)
17095 (setq ad-return-value t
17096 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
17098 (let ((p (org-inside-LaTeX-fragment-p)))
17099 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
17100 (setq ad-return-value t
17101 texmathp-why '("Org-mode embedded math" . 0))
17102 (if p ad-do-it)))))))))
17104 (defun turn-on-org-cdlatex ()
17105 "Unconditionally turn on `org-cdlatex-mode'."
17106 (org-cdlatex-mode 1))
17108 (defun org-inside-LaTeX-fragment-p ()
17109 "Test if point is inside a LaTeX fragment.
17110 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
17111 sequence appearing also before point.
17112 Even though the matchers for math are configurable, this function assumes
17113 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
17114 delimiters are skipped when they have been removed by customization.
17115 The return value is nil, or a cons cell with the delimiter and the
17116 position of this delimiter.
17118 This function does a reasonably good job, but can locally be fooled by
17119 for example currency specifications. For example it will assume being in
17120 inline math after \"$22.34\". The LaTeX fragment formatter will only format
17121 fragments that are properly closed, but during editing, we have to live
17122 with the uncertainty caused by missing closing delimiters. This function
17123 looks only before point, not after."
17125 (let ((pos (point))
17126 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
17128 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
17130 dd-on str (start 0) m re)
17133 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
17134 re (nth 1 (assoc "$" org-latex-regexps)))
17135 (while (string-match re str start)
17137 ((= (match-end 0) (length str))
17138 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
17139 ((= (match-end 0) (- (length str) 5))
17141 (t (setq start (match-end 0))))))
17142 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
17144 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
17145 (and (match-beginning 2) (throw 'exit nil))
17147 (while (re-search-backward "\\$\\$" lim t)
17148 (setq dd-on (not dd-on)))
17150 (if dd-on (cons "$$" m))))))
17152 (defun org-inside-latex-macro-p ()
17153 "Is point inside a LaTeX macro or its arguments?"
17156 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
17158 (defun org-try-cdlatex-tab ()
17159 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
17160 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
17161 - inside a LaTeX fragment, or
17162 - after the first word in a line, where an abbreviation expansion could
17163 insert a LaTeX environment."
17164 (when org-cdlatex-mode
17166 ;; Before any word on the line: No expansion possible.
17167 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
17168 ;; Just after first word on the line: Expand it. Make sure it
17169 ;; cannot happen on headlines, though.
17171 (skip-chars-backward "a-zA-Z0-9*")
17172 (skip-chars-backward " \t")
17173 (and (bolp) (not (org-at-heading-p))))
17175 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
17177 (defun org-cdlatex-underscore-caret (&optional arg)
17178 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
17179 Revert to the normal definition outside of these fragments."
17181 (if (org-inside-LaTeX-fragment-p)
17182 (call-interactively 'cdlatex-sub-superscript)
17183 (let (org-cdlatex-mode)
17184 (call-interactively (key-binding (vector last-input-event))))))
17186 (defun org-cdlatex-math-modify (&optional arg)
17187 "Execute `cdlatex-math-modify' in LaTeX fragments.
17188 Revert to the normal definition outside of these fragments."
17190 (if (org-inside-LaTeX-fragment-p)
17191 (call-interactively 'cdlatex-math-modify)
17192 (let (org-cdlatex-mode)
17193 (call-interactively (key-binding (vector last-input-event))))))
17195 (defvar org-latex-fragment-image-overlays nil
17196 "List of overlays carrying the images of latex fragments.")
17197 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
17199 (defun org-remove-latex-fragment-image-overlays ()
17200 "Remove all overlays with LaTeX fragment images in current buffer."
17201 (mapc 'delete-overlay org-latex-fragment-image-overlays)
17202 (setq org-latex-fragment-image-overlays nil))
17204 (defun org-preview-latex-fragment (&optional subtree)
17205 "Preview the LaTeX fragment at point, or all locally or globally.
17206 If the cursor is in a LaTeX fragment, create the image and overlay
17207 it over the source code. If there is no fragment at point, display
17208 all fragments in the current text, from one headline to the next. With
17209 prefix SUBTREE, display all fragments in the current subtree. With a
17210 double prefix arg \\[universal-argument] \\[universal-argument], or when \
17211 the cursor is before the first headline,
17212 display all fragments in the buffer.
17213 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
17215 (unless buffer-file-name
17216 (error "Can't preview LaTeX fragment in a non-file buffer"))
17217 (org-remove-latex-fragment-image-overlays)
17220 (let (beg end at msg)
17222 ((or (equal subtree '(16))
17223 (not (save-excursion
17224 (re-search-backward org-outline-regexp-bol nil t))))
17225 (setq beg (point-min) end (point-max)
17226 msg "Creating images for buffer...%s"))
17227 ((equal subtree '(4))
17228 (org-back-to-heading)
17229 (setq beg (point) end (org-end-of-subtree t)
17230 msg "Creating images for subtree...%s"))
17232 (if (setq at (org-inside-LaTeX-fragment-p))
17233 (goto-char (max (point-min) (- (cdr at) 2)))
17234 (org-back-to-heading))
17235 (setq beg (point) end (progn (outline-next-heading) (point))
17236 msg (if at "Creating image...%s"
17237 "Creating images for entry...%s"))))
17239 (narrow-to-region beg end)
17242 (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
17243 (file-name-nondirectory
17244 buffer-file-name)))
17245 default-directory 'overlays msg at 'forbuffer
17246 org-latex-create-formula-image-program)
17247 (message msg "done. Use `C-c C-c' to remove images.")))))
17249 (defvar org-latex-regexps
17250 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
17251 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
17252 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
17253 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17254 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17255 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
17256 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
17257 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
17258 "Regular expressions for matching embedded LaTeX.")
17260 (defvar org-export-have-math nil) ;; dynamic scoping
17261 (defun org-format-latex (prefix &optional dir overlays msg at
17262 forbuffer processing-type)
17263 "Replace LaTeX fragments with links to an image, and produce images.
17264 Some of the options can be changed using the variable
17265 `org-format-latex-options'."
17266 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
17267 (let* ((prefixnodir (file-name-nondirectory prefix))
17268 (absprefix (expand-file-name prefix dir))
17269 (todir (file-name-directory absprefix))
17270 (opt org-format-latex-options)
17271 (matchers (plist-get opt :matchers))
17272 (re-list org-latex-regexps)
17273 (org-format-latex-header-extra
17274 (plist-get (org-infile-export-plist) :latex-header-extra))
17275 (cnt 0) txt hash link beg end re e checkdir
17276 executables-checked string
17277 m n block-type block linkfile movefile ov)
17278 ;; Check the different regular expressions
17279 (while (setq e (pop re-list))
17280 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
17281 block (if block-type "\n\n" ""))
17282 (when (member m matchers)
17283 (goto-char (point-min))
17284 (while (re-search-forward re nil t)
17285 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
17286 (not (get-text-property (match-beginning n)
17289 (not (eq (get-char-property (match-beginning n)
17291 'org-latex-overlay))))
17292 (setq org-export-have-math t)
17294 ((eq processing-type 'verbatim)
17295 ;; Leave the text verbatim, just protect it
17296 (add-text-properties (match-beginning n) (match-end n)
17297 '(org-protected t)))
17298 ((eq processing-type 'mathjax)
17299 ;; Prepare for MathJax processing
17300 (setq string (match-string n))
17301 (if (member m '("$" "$1"))
17303 (delete-region (match-beginning n) (match-end n))
17304 (goto-char (match-beginning n))
17305 (insert (org-add-props (concat "\\(" (substring string 1 -1)
17307 '(org-protected t))))
17308 (add-text-properties (match-beginning n) (match-end n)
17309 '(org-protected t))))
17310 ((or (eq processing-type 'dvipng)
17311 (eq processing-type 'imagemagick))
17312 ;; Process to an image
17313 (setq txt (match-string n)
17314 beg (match-beginning n) end (match-end n)
17316 (let (print-length print-level) ; make sure full list is printed
17317 (setq hash (sha1 (prin1-to-string
17318 (list org-format-latex-header
17319 org-format-latex-header-extra
17320 org-export-latex-default-packages-alist
17321 org-export-latex-packages-alist
17322 org-format-latex-options
17324 linkfile (format "%s_%s.png" prefix hash)
17325 movefile (format "%s_%s.png" absprefix hash)))
17326 (setq link (concat block "[[file:" linkfile "]]" block))
17327 (if msg (message msg cnt))
17329 (unless checkdir ; make sure the directory exists
17331 (or (file-directory-p todir) (make-directory todir t)))
17333 ((eq processing-type 'dvipng)
17334 (unless executables-checked
17335 (org-check-external-command
17336 "latex" "needed to convert LaTeX fragments to images")
17337 (org-check-external-command
17338 "dvipng" "needed to convert LaTeX fragments to images")
17339 (setq executables-checked t))
17340 (unless (file-exists-p movefile)
17341 (org-create-formula-image-with-dvipng
17342 txt movefile opt forbuffer)))
17343 ((eq processing-type 'imagemagick)
17344 (unless executables-checked
17345 (org-check-external-command
17346 "convert" "you need to install imagemagick")
17347 (setq executables-checked t))
17348 (unless (file-exists-p movefile)
17349 (org-create-formula-image-with-imagemagick
17350 txt movefile opt forbuffer))))
17354 (if (eq (overlay-get o 'org-overlay-type)
17355 'org-latex-overlay)
17356 (delete-overlay o)))
17357 (overlays-in beg end))
17358 (setq ov (make-overlay beg end))
17359 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
17360 (if (featurep 'xemacs)
17362 (overlay-put ov 'invisible t)
17365 (make-glyph (vector 'png :file movefile))))
17368 (list 'image :type 'png :file movefile :ascent 'center)))
17369 (push ov org-latex-fragment-image-overlays)
17371 (delete-region beg end)
17372 (insert (org-add-props link
17373 (list 'org-latex-src
17374 (replace-regexp-in-string
17376 'org-latex-src-embed-type
17377 (if block-type 'paragraph 'character))))))
17378 ((eq processing-type 'mathml)
17379 ;; Process to MathML
17380 (unless executables-checked
17381 (unless (save-match-data (org-format-latex-mathml-available-p))
17382 (error "LaTeX to MathML converter not configured"))
17383 (setq executables-checked t))
17384 (setq txt (match-string n)
17385 beg (match-beginning n) end (match-end n)
17387 (if msg (message msg cnt))
17389 (delete-region beg end)
17390 (insert (org-format-latex-as-mathml
17391 txt block-type prefix dir)))
17393 (error "Unknown conversion type %s for latex fragments"
17394 processing-type)))))))))
17396 (defun org-create-math-formula (latex-frag &optional mathml-file)
17397 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
17398 Use `org-latex-to-mathml-convert-command'. If the conversion is
17399 sucessful, return the portion between \"<math...> </math>\"
17400 elements otherwise return nil. When MATHML-FILE is specified,
17401 write the results in to that file. When invoked as an
17402 interactive command, prompt for LATEX-FRAG, with initial value
17403 set to the current active region and echo the results for user
17405 (interactive (list (let ((frag (when (org-region-active-p)
17406 (buffer-substring-no-properties
17407 (region-beginning) (region-end)))))
17408 (read-string "LaTeX Fragment: " frag nil frag))))
17409 (unless latex-frag (error "Invalid latex-frag"))
17410 (let* ((tmp-in-file (file-relative-name
17411 (make-temp-name (expand-file-name "ltxmathml-in"))))
17412 (ignore (write-region latex-frag nil tmp-in-file))
17413 (tmp-out-file (file-relative-name
17414 (make-temp-name (expand-file-name "ltxmathml-out"))))
17416 org-latex-to-mathml-convert-command
17417 `((?j . ,(shell-quote-argument
17418 (expand-file-name org-latex-to-mathml-jar-file)))
17419 (?I . ,(shell-quote-argument tmp-in-file))
17420 (?o . ,(shell-quote-argument tmp-out-file)))))
17421 mathml shell-command-output)
17422 (when (org-called-interactively-p 'any)
17423 (unless (org-format-latex-mathml-available-p)
17424 (error "LaTeX to MathML converter not configured")))
17425 (message "Running %s" cmd)
17426 (setq shell-command-output (shell-command-to-string cmd))
17428 (when (file-readable-p tmp-out-file)
17429 (with-current-buffer (find-file-noselect tmp-out-file t)
17430 (goto-char (point-min))
17431 (when (re-search-forward
17434 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
17436 (regexp-quote "</math>")) nil t)
17437 (prog1 (match-string 0) (kill-buffer))))))
17441 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
17443 (write-region mathml nil mathml-file))
17444 (when (org-called-interactively-p 'any)
17446 ((message "LaTeX to MathML conversion failed")
17447 (message shell-command-output)))
17448 (delete-file tmp-in-file)
17449 (when (file-exists-p tmp-out-file)
17450 (delete-file tmp-out-file))
17453 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
17454 prefix &optional dir)
17455 "Use `org-create-math-formula' but check local cache first."
17456 (let* ((absprefix (expand-file-name prefix dir))
17457 (print-length nil) (print-level nil)
17458 (formula-id (concat
17463 org-latex-to-mathml-convert-command)))))
17464 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
17465 (formula-cache-dir (file-name-directory formula-cache)))
17467 (unless (file-directory-p formula-cache-dir)
17468 (make-directory formula-cache-dir t))
17470 (unless (file-exists-p formula-cache)
17471 (org-create-math-formula latex-frag formula-cache))
17473 (if (file-exists-p formula-cache)
17474 ;; Successful conversion. Return the link to MathML file.
17476 (format "[[file:%s]]" (file-relative-name formula-cache dir))
17477 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
17478 'org-latex-src-embed-type (if latex-frag-type
17479 'paragraph 'character)))
17480 ;; Failed conversion. Return the LaTeX fragment verbatim
17481 (add-text-properties
17482 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
17485 ;; This function borrows from Ganesh Swami's latex2png.el
17486 (defun org-create-formula-image-with-dvipng (string tofile options buffer)
17487 "This calls dvipng."
17488 (require 'org-latex)
17489 (let* ((tmpdir (if (featurep 'xemacs)
17491 temporary-file-directory))
17492 (texfilebase (make-temp-name
17493 (expand-file-name "orgtex" tmpdir)))
17494 (texfile (concat texfilebase ".tex"))
17495 (dvifile (concat texfilebase ".dvi"))
17496 (pngfile (concat texfilebase ".png"))
17497 (fnh (if (featurep 'xemacs)
17498 (font-height (face-font 'default))
17499 (face-attribute 'default :height nil)))
17500 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17501 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17502 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17504 (bg (or (plist-get options (if buffer :background :html-background))
17506 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
17507 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
17508 (with-temp-file texfile
17509 (insert (org-splice-latex-header
17510 org-format-latex-header
17511 org-export-latex-default-packages-alist
17512 org-export-latex-packages-alist t
17513 org-format-latex-header-extra))
17514 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
17515 (require 'org-latex)
17516 (org-export-latex-fix-inputenc))
17517 (let ((dir default-directory))
17518 (condition-case nil
17521 (call-process "latex" nil nil nil texfile))
17524 (if (not (file-exists-p dvifile))
17525 (progn (message "Failed to create dvi file from %s" texfile) nil)
17526 (condition-case nil
17527 (if (featurep 'xemacs)
17528 (call-process "dvipng" nil nil nil
17533 (call-process "dvipng" nil nil nil
17536 ;;"-x" scale "-y" scale
17541 (if (not (file-exists-p pngfile))
17542 (if org-format-latex-signal-error
17543 (error "Failed to create png file from %s" texfile)
17544 (message "Failed to create png file from %s" texfile)
17546 ;; Use the requested file name and clean up
17547 (copy-file pngfile tofile 'replace)
17548 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
17549 (if (file-exists-p (concat texfilebase e))
17550 (delete-file (concat texfilebase e))))
17553 (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
17554 (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
17555 "This calls convert, which is included into imagemagick."
17556 (require 'org-latex)
17557 (let* ((tmpdir (if (featurep 'xemacs)
17559 temporary-file-directory))
17560 (texfilebase (make-temp-name
17561 (expand-file-name "orgtex" tmpdir)))
17562 (texfile (concat texfilebase ".tex"))
17563 (pdffile (concat texfilebase ".pdf"))
17564 (pngfile (concat texfilebase ".png"))
17565 (fnh (if (featurep 'xemacs)
17566 (font-height (face-font 'default))
17567 (face-attribute 'default :height nil)))
17568 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17569 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17570 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17572 (bg (or (plist-get options (if buffer :background :html-background))
17574 (if (eq fg 'default) (setq fg (org-latex-color :foreground))
17575 (setq fg (org-latex-color-format fg)))
17576 (if (eq bg 'default) (setq bg (org-latex-color :background))
17577 (setq bg (org-latex-color-format
17578 (if (string= bg "Transparent")(setq bg "white")))))
17579 (with-temp-file texfile
17580 (insert (org-splice-latex-header
17581 org-format-latex-header
17582 org-export-latex-default-packages-alist
17583 org-export-latex-packages-alist t
17584 org-format-latex-header-extra))
17585 (insert "\n\\begin{document}\n"
17586 "\\definecolor{fg}{rgb}{" fg "}\n"
17587 "\\definecolor{bg}{rgb}{" bg "}\n"
17588 "\n\\pagecolor{bg}\n"
17592 "\n\\end{document}\n" )
17593 (require 'org-latex)
17594 (org-export-latex-fix-inputenc))
17595 (let ((dir default-directory) cmd cmds latex-frags-cmds)
17596 (condition-case nil
17599 (setq cmds org-latex-to-pdf-process)
17601 (setq latex-frags-cmds (pop cmds))
17602 (if (listp latex-frags-cmds)
17604 (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
17605 (while latex-frags-cmds
17606 (setq cmd (pop latex-frags-cmds))
17607 (while (string-match "%b" cmd)
17608 (setq cmd (replace-match
17610 (shell-quote-argument texfile))
17612 (while (string-match "%f" cmd)
17613 (setq cmd (replace-match
17615 (shell-quote-argument (file-name-nondirectory texfile)))
17617 (while (string-match "%o" cmd)
17618 (setq cmd (replace-match
17620 (shell-quote-argument (file-name-directory texfile)))
17622 (setq cmd (split-string cmd))
17623 (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
17626 (if (not (file-exists-p pdffile))
17627 (progn (message "Failed to create pdf file from %s" texfile) nil)
17628 (condition-case nil
17629 (if (featurep 'xemacs)
17630 (call-process "convert" nil nil nil
17636 ;; "-sharpen" "0x1.0"
17638 (call-process "convert" nil nil nil
17644 ; "-sharpen" "0x1.0"
17647 (if (not (file-exists-p pngfile))
17648 (if org-format-latex-signal-error
17649 (error "Failed to create png file from %s" texfile)
17650 (message "Failed to create png file from %s" texfile)
17652 ;; Use the requested file name and clean up
17653 (copy-file pngfile tofile 'replace)
17654 (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
17655 (if (file-exists-p (concat texfilebase e))
17656 (delete-file (concat texfilebase e))))
17659 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
17660 "Fill a LaTeX header template TPL.
17661 In the template, the following place holders will be recognized:
17663 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
17664 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
17665 [PACKAGES] \\usepackage statements for PKG
17666 [NO-PACKAGES] do not include PKG
17667 [EXTRA] the string EXTRA
17668 [NO-EXTRA] do not include EXTRA
17670 For backward compatibility, if both the positive and the negative place
17671 holder is missing, the positive one (without the \"NO-\") will be
17672 assumed to be present at the end of the template.
17673 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
17675 SNIPPETS-P indicates if this is run to create snippet images for HTML."
17676 (let (rpl (end ""))
17677 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
17678 (setq rpl (if (or (match-end 1) (not def-pkg))
17679 "" (org-latex-packages-to-string def-pkg snippets-p t))
17680 tpl (replace-match rpl t t tpl))
17681 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
17683 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
17684 (setq rpl (if (or (match-end 1) (not pkg))
17685 "" (org-latex-packages-to-string pkg snippets-p t))
17686 tpl (replace-match rpl t t tpl))
17689 (org-latex-packages-to-string pkg snippets-p)))))
17691 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
17692 (setq rpl (if (or (match-end 1) (not extra))
17693 "" (concat extra "\n"))
17694 tpl (replace-match rpl t t tpl))
17695 (if (and extra (string-match "\\S-" extra))
17696 (setq end (concat end "\n" extra))))
17698 (if (string-match "\\S-" end)
17699 (concat tpl "\n" end)
17702 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
17703 "Turn an alist of packages into a string with the \\usepackage macros."
17704 (setq pkg (mapconcat (lambda(p)
17707 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
17708 (format "%% Package %s omitted" (cadr p)))
17709 ((equal "" (car p))
17710 (format "\\usepackage{%s}" (cadr p)))
17712 (format "\\usepackage[%s]{%s}"
17713 (car p) (cadr p)))))
17716 (if newline (concat pkg "\n") pkg))
17718 (defun org-dvipng-color (attr)
17719 "Return a RGB color specification for dvipng."
17720 (apply 'format "rgb %s %s %s"
17721 (mapcar 'org-normalize-color
17722 (if (featurep 'xemacs)
17723 (color-rgb-components
17724 (face-property 'default
17725 (cond ((eq attr :foreground) 'foreground)
17726 ((eq attr :background) 'background))))
17727 (color-values (face-attribute 'default attr nil))))))
17729 (defun org-latex-color (attr)
17730 "Return a RGB color for the LaTeX color package."
17731 (apply 'format "%s,%s,%s"
17732 (mapcar 'org-normalize-color
17733 (if (featurep 'xemacs)
17734 (color-rgb-components
17735 (face-property 'default
17736 (cond ((eq attr :foreground) 'foreground)
17737 ((eq attr :background) 'background))))
17738 (color-values (face-attribute 'default attr nil))))))
17740 (defun org-latex-color-format (color-name)
17741 "Convert COLOR-NAME to a RGB color value."
17742 (apply 'format "%s,%s,%s"
17743 (mapcar 'org-normalize-color
17744 (color-values color-name))))
17746 (defun org-normalize-color (value)
17747 "Return string to be used as color value for an RGB component."
17748 (format "%g" (/ value 65535.0)))
17753 (defvar org-inline-image-overlays nil)
17754 (make-variable-buffer-local 'org-inline-image-overlays)
17756 (defun org-toggle-inline-images (&optional include-linked)
17757 "Toggle the display of inline images.
17758 INCLUDE-LINKED is passed to `org-display-inline-images'."
17760 (if org-inline-image-overlays
17762 (org-remove-inline-images)
17763 (message "Inline image display turned off"))
17764 (org-display-inline-images include-linked)
17765 (if org-inline-image-overlays
17766 (message "%d images displayed inline"
17767 (length org-inline-image-overlays))
17768 (message "No images to display inline"))))
17770 (defun org-redisplay-inline-images ()
17771 "Refresh the display of inline images."
17773 (if (not org-inline-image-overlays)
17774 (org-toggle-inline-images)
17775 (org-toggle-inline-images)
17776 (org-toggle-inline-images)))
17778 (defun org-display-inline-images (&optional include-linked refresh beg end)
17779 "Display inline images.
17780 Normally only links without a description part are inlined, because this
17781 is how it will work for export. When INCLUDE-LINKED is set, also links
17782 with a description part will be inlined. This can be nice for a quick
17783 look at those images, but it does not reflect what exported files will look
17785 When REFRESH is set, refresh existing images between BEG and END.
17786 This will create new image displays only if necessary.
17787 BEG and END default to the buffer boundaries."
17790 (org-remove-inline-images)
17791 (if (fboundp 'clear-image-cache) (clear-image-cache)))
17795 (setq beg (or beg (point-min)) end (or end (point-max)))
17797 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
17798 (substring (org-image-file-name-regexp) 0 -2)
17799 "\\)\\]" (if include-linked "" "\\]")))
17801 (while (re-search-forward re end t)
17802 (setq old (get-char-property-and-overlay (match-beginning 1)
17803 'org-image-overlay))
17804 (setq file (expand-file-name
17805 (concat (or (match-string 3) "") (match-string 4))))
17806 (when (file-exists-p file)
17807 (if (and (car-safe old) refresh)
17808 (image-refresh (overlay-get (cdr old) 'display))
17809 (setq img (save-match-data (create-image file)))
17811 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
17812 (overlay-put ov 'display img)
17813 (overlay-put ov 'face 'default)
17814 (overlay-put ov 'org-image-overlay t)
17815 (overlay-put ov 'modification-hooks
17816 (list 'org-display-inline-remove-overlay))
17817 (push ov org-inline-image-overlays)))))))))
17819 (define-obsolete-function-alias
17820 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
17822 (defun org-display-inline-remove-overlay (ov after beg end &optional len)
17823 "Remove inline-display overlay if a corresponding region is modified."
17824 (let ((inhibit-modification-hooks t))
17825 (when (and ov after)
17826 (delete ov org-inline-image-overlays)
17827 (delete-overlay ov))))
17829 (defun org-remove-inline-images ()
17830 "Remove inline display of images."
17832 (mapc 'delete-overlay org-inline-image-overlays)
17833 (setq org-inline-image-overlays nil))
17837 ;; Outline functions from `outline-mode-prefix-map'
17838 ;; that can be remapped in Org:
17839 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
17840 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
17841 (define-key org-mode-map [remap outline-forward-same-level]
17842 'org-forward-heading-same-level)
17843 (define-key org-mode-map [remap outline-backward-same-level]
17844 'org-backward-heading-same-level)
17845 (define-key org-mode-map [remap show-branches]
17846 'org-kill-note-or-show-branches)
17847 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
17848 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
17849 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
17851 ;; Outline functions from `outline-mode-prefix-map' that can not
17852 ;; be remapped in Org:
17854 ;; - the column "key binding" shows whether the Outline function is still
17855 ;; available in Org mode on the same key that it has been bound to in
17857 ;; - "overridden": key used for a different functionality in Org mode
17858 ;; - else: key still bound to the same Outline function in Org mode
17860 ;; | Outline function | key binding | Org replacement |
17861 ;; |------------------------------------+-------------+-----------------------|
17862 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
17863 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
17864 ;; | `outline-up-heading' | `C-c C-u' | still same function |
17865 ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
17866 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
17867 ;; | `show-entry' | overridden | no replacement |
17868 ;; | `show-children' | `C-c C-i' | visibility cycling |
17869 ;; | `show-branches' | `C-c C-k' | still same function |
17870 ;; | `show-subtree' | overridden | visibility cycling |
17871 ;; | `show-all' | overridden | no replacement |
17872 ;; | `hide-subtree' | overridden | visibility cycling |
17873 ;; | `hide-body' | overridden | no replacement |
17874 ;; | `hide-entry' | overridden | visibility cycling |
17875 ;; | `hide-leaves' | overridden | no replacement |
17876 ;; | `hide-sublevels' | overridden | no replacement |
17877 ;; | `hide-other' | overridden | no replacement |
17879 ;; Make `C-c C-x' a prefix key
17880 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
17882 ;; TAB key with modifiers
17883 (org-defkey org-mode-map "\C-i" 'org-cycle)
17884 (org-defkey org-mode-map [(tab)] 'org-cycle)
17885 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
17886 (org-defkey org-mode-map "\M-\t" 'pcomplete)
17887 ;; The following line is necessary under Suse GNU/Linux
17888 (unless (featurep 'xemacs)
17889 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
17890 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
17891 (define-key org-mode-map [backtab] 'org-shifttab)
17893 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
17894 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
17895 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
17897 ;; Cursor keys with modifiers
17898 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
17899 (org-defkey org-mode-map [(meta right)] 'org-metaright)
17900 (org-defkey org-mode-map [(meta up)] 'org-metaup)
17901 (org-defkey org-mode-map [(meta down)] 'org-metadown)
17903 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
17904 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
17905 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
17906 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
17908 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
17909 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
17910 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
17911 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
17913 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
17914 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
17915 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
17916 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
17919 (define-key org-mode-map org-babel-key-prefix org-babel-map)
17920 (mapc (lambda (pair)
17921 (define-key org-babel-map (car pair) (cdr pair)))
17922 org-babel-key-bindings)
17924 ;;; Extra keys for tty access.
17925 ;; We only set them when really needed because otherwise the
17926 ;; menus don't show the simple keys
17928 (when (or org-use-extra-keys
17929 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
17930 (not window-system))
17931 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
17932 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
17933 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
17934 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
17935 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
17936 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
17937 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
17938 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
17939 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
17940 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
17941 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
17942 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
17943 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
17944 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
17945 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
17946 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
17947 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
17948 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
17949 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
17950 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
17951 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
17952 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
17953 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
17954 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
17955 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
17956 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
17957 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
17958 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
17960 ;; All the other keys
17962 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
17963 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
17964 (if (boundp 'narrow-map)
17965 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
17966 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
17967 (if (boundp 'narrow-map)
17968 (org-defkey narrow-map "b" 'org-narrow-to-block)
17969 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
17970 (if (boundp 'narrow-map)
17971 (org-defkey narrow-map "e" 'org-narrow-to-element)
17972 (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
17973 (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
17974 (org-defkey org-mode-map "\M-}" 'org-forward-element)
17975 (org-defkey org-mode-map "\M-{" 'org-backward-element)
17976 (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
17977 (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
17978 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
17979 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
17980 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
17981 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
17982 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
17983 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
17984 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
17985 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
17986 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
17987 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
17988 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
17989 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
17990 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
17991 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
17992 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
17993 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
17994 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
17995 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
17996 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
17997 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
17998 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
17999 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
18000 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
18001 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
18002 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
18003 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
18004 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
18005 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
18006 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
18007 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
18008 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
18009 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
18010 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
18011 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
18012 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
18013 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
18014 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
18015 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
18016 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
18017 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
18018 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
18019 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
18020 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
18021 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
18022 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
18023 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
18024 (org-defkey org-mode-map "\C-c^" 'org-sort)
18025 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
18026 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
18027 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
18028 (org-defkey org-mode-map "\C-m" 'org-return)
18029 (org-defkey org-mode-map "\C-j" 'org-return-indent)
18030 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
18031 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
18032 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
18033 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
18034 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
18035 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
18036 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
18037 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
18038 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
18039 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
18040 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
18041 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
18042 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
18043 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
18044 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
18045 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
18046 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
18047 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
18048 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
18049 (org-defkey org-mode-map "\M-h" 'org-mark-element)
18050 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
18051 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
18053 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
18054 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
18055 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
18057 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
18058 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
18059 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
18060 (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
18061 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
18062 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
18063 (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
18064 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
18065 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
18066 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
18067 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
18068 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
18069 (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
18070 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
18071 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
18072 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
18073 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
18074 (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
18075 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
18076 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
18077 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
18078 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
18080 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
18081 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
18082 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
18083 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
18084 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
18086 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
18088 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
18090 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
18091 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
18093 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
18096 (when (featurep 'xemacs)
18097 (org-defkey org-mode-map 'button3 'popup-mode-menu))
18100 (defconst org-speed-commands-default
18102 ("Outline Navigation")
18103 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
18104 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
18105 ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
18106 ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
18107 ("u" . (org-speed-move-safe 'outline-up-heading))
18109 ("g" . (org-refile t))
18110 ("Outline Visibility")
18112 ("C" . org-shifttab)
18113 (" " . org-display-outline-path)
18114 ("=" . org-columns)
18115 ("Outline Structure Editing")
18116 ("U" . org-shiftmetaup)
18117 ("D" . org-shiftmetadown)
18118 ("r" . org-metaright)
18119 ("l" . org-metaleft)
18120 ("R" . org-shiftmetaright)
18121 ("L" . org-shiftmetaleft)
18122 ("i" . (progn (forward-char 1) (call-interactively
18123 'org-insert-heading-respect-content)))
18126 ("a" . org-archive-subtree-default-with-confirmation)
18127 ("." . org-mark-subtree)
18128 ("#" . org-toggle-comment)
18130 ("I" . org-clock-in)
18131 ("O" . org-clock-out)
18132 ("Meta Data Editing")
18134 ("," . (org-priority))
18135 ("0" . (org-priority ?\ ))
18136 ("1" . (org-priority ?A))
18137 ("2" . (org-priority ?B))
18138 ("3" . (org-priority ?C))
18139 (":" . org-set-tags-command)
18140 ("e" . org-set-effort)
18141 ("E" . org-inc-effort)
18142 ("W" . (lambda(m) (interactive "sMinutes before warning: ")
18143 (org-entry-put (point) "APPT_WARNTIME" m)))
18144 ("Agenda Views etc")
18146 ("/" . org-sparse-tree)
18148 ("o" . org-open-at-point)
18149 ("?" . org-speed-command-help)
18150 ("<" . (org-agenda-set-restriction-lock 'subtree))
18151 (">" . (org-agenda-remove-restriction-lock))
18153 "The default speed commands.")
18155 (defun org-print-speed-command (e)
18156 (if (> (length (car e)) 1)
18161 (princ (make-string (length (car e)) ?-))
18165 (if (symbolp (cdr e))
18166 (princ (symbol-name (cdr e)))
18170 (defun org-speed-command-help ()
18171 "Show the available speed commands."
18173 (if (not org-use-speed-commands)
18174 (error "Speed commands are not activated, customize `org-use-speed-commands'")
18175 (with-output-to-temp-buffer "*Help*"
18176 (princ "User-defined Speed commands\n===========================\n")
18177 (mapc 'org-print-speed-command org-speed-commands-user)
18179 (princ "Built-in Speed commands\n=======================\n")
18180 (mapc 'org-print-speed-command org-speed-commands-default))
18181 (with-current-buffer "*Help*"
18182 (setq truncate-lines t))))
18184 (defun org-speed-move-safe (cmd)
18185 "Execute CMD, but make sure that the cursor always ends up in a headline.
18186 If not, return to the original position and throw an error."
18188 (let ((pos (point)))
18189 (call-interactively cmd)
18190 (unless (and (bolp) (org-at-heading-p))
18192 (error "Boundary reached while executing %s" cmd))))
18194 (defvar org-self-insert-command-undo-counter 0)
18196 (defvar org-table-auto-blank-field) ; defined in org-table.el
18197 (defvar org-speed-command nil)
18199 (define-obsolete-function-alias
18200 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
18202 (defun org-speed-command-activate (keys)
18203 "Hook for activating single-letter speed commands.
18204 `org-speed-commands-default' specifies a minimal command set.
18205 Use `org-speed-commands-user' for further customization."
18206 (when (or (and (bolp) (looking-at org-outline-regexp))
18207 (and (functionp org-use-speed-commands)
18208 (funcall org-use-speed-commands)))
18209 (cdr (assoc keys (append org-speed-commands-user
18210 org-speed-commands-default)))))
18212 (define-obsolete-function-alias
18213 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
18215 (defun org-babel-speed-command-activate (keys)
18216 "Hook for activating single-letter code block commands."
18217 (when (and (bolp) (looking-at org-babel-src-block-regexp))
18218 (cdr (assoc keys org-babel-key-bindings))))
18220 (defcustom org-speed-command-hook
18221 '(org-speed-command-default-hook org-babel-speed-command-hook)
18222 "Hook for activating speed commands at strategic locations.
18223 Hook functions are called in sequence until a valid handler is
18226 Each hook takes a single argument, a user-pressed command key
18227 which is also a `self-insert-command' from the global map.
18229 Within the hook, examine the cursor position and the command key
18230 and return nil or a valid handler as appropriate. Handler could
18231 be one of an interactive command, a function, or a form.
18233 Set `org-use-speed-commands' to non-nil value to enable this
18234 hook. The default setting is `org-speed-command-activate'."
18235 :group 'org-structure
18239 (defun org-self-insert-command (N)
18240 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
18241 If the cursor is in a table looking at whitespace, the whitespace is
18242 overwritten, and the table is not marked as requiring realignment."
18244 (org-check-before-invisible-edit 'insert)
18246 ((and org-use-speed-commands
18247 (setq org-speed-command
18248 (run-hook-with-args-until-success
18249 'org-speed-command-hook (this-command-keys))))
18251 ((commandp org-speed-command)
18252 (setq this-command org-speed-command)
18253 (call-interactively org-speed-command))
18254 ((functionp org-speed-command)
18255 (funcall org-speed-command))
18256 ((and org-speed-command (listp org-speed-command))
18257 (eval org-speed-command))
18258 (t (let (org-use-speed-commands)
18259 (call-interactively 'org-self-insert-command)))))
18263 ;; check if we blank the field, and if that triggers align
18264 (and (featurep 'org-table) org-table-auto-blank-field
18265 (member last-command
18266 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
18267 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
18268 ;; got extra space, this field does not determine column width
18269 (let (org-table-may-need-update) (org-table-blank-field))
18270 ;; no extra space, this field may determine column width
18271 (org-table-blank-field)))
18274 (looking-at "[^|\n]* |"))
18275 (let (org-table-may-need-update)
18276 (goto-char (1- (match-end 0)))
18277 (backward-delete-char 1)
18278 (goto-char (match-beginning 0))
18279 (self-insert-command N)))
18281 (setq org-table-may-need-update t)
18282 (self-insert-command N)
18283 (org-fix-tags-on-the-fly)
18284 (if org-self-insert-cluster-for-undo
18285 (if (not (eq last-command 'org-self-insert-command))
18286 (setq org-self-insert-command-undo-counter 1)
18287 (if (>= org-self-insert-command-undo-counter 20)
18288 (setq org-self-insert-command-undo-counter 1)
18289 (and (> org-self-insert-command-undo-counter 0)
18290 buffer-undo-list (listp buffer-undo-list)
18291 (not (cadr buffer-undo-list)) ; remove nil entry
18292 (setcdr buffer-undo-list (cddr buffer-undo-list)))
18293 (setq org-self-insert-command-undo-counter
18294 (1+ org-self-insert-command-undo-counter))))))))
18296 (defun org-check-before-invisible-edit (kind)
18297 "Check is editing if kind KIND would be dangerous with invisible text around.
18298 The detailed reaction depends on the user option `org-catch-invisible-edits'."
18299 ;; First, try to get out of here as quickly as possible, to reduce overhead
18300 (if (and org-catch-invisible-edits
18301 (or (not (boundp 'visible-mode)) (not visible-mode))
18302 (or (get-char-property (point) 'invisible)
18303 (get-char-property (max (point-min) (1- (point))) 'invisible)))
18304 ;; OK, we need to take a closer look
18305 (let* ((invisible-at-point (get-char-property (point) 'invisible))
18306 (invisible-before-point (if (bobp) nil (get-char-property
18307 (1- (point)) 'invisible)))
18308 (border-and-ok-direction
18310 ;; Check if we are acting predictably before invisible text
18311 (and invisible-at-point (not invisible-before-point)
18312 (memq kind '(insert delete-backward)))
18313 ;; Check if we are acting predictably after invisible text
18314 ;; This works not well, and I have turned it off. It seems
18315 ;; better to always show and stop after invisible text.
18316 ;; (and (not invisible-at-point) invisible-before-point
18317 ;; (memq kind '(insert delete)))
18319 (when (or (memq invisible-at-point '(outline org-hide-block t))
18320 (memq invisible-before-point '(outline org-hide-block t)))
18321 (if (eq org-catch-invisible-edits 'error)
18322 (error "Editing in invisible areas is prohibited - make visible first"))
18323 (if (and org-custom-properties-overlays
18324 (y-or-n-p "Display invisible properties in this buffer? "))
18325 (org-toggle-custom-properties-visibility)
18326 ;; Make the area visible
18328 (if invisible-before-point
18329 (goto-char (previous-single-char-property-change
18330 (point) 'invisible)))
18333 ((eq org-catch-invisible-edits 'show)
18334 ;; That's it, we do the edit after showing
18336 "Unfolding invisible region around point before editing")
18338 ((and (eq org-catch-invisible-edits 'smart)
18339 border-and-ok-direction)
18340 (message "Unfolding invisible region around point before editing"))
18342 ;; Don't do the edit, make the user repeat it in full visibility
18343 (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
18345 (defun org-fix-tags-on-the-fly ()
18346 (when (and (equal (char-after (point-at-bol)) ?*)
18347 (org-at-heading-p))
18348 (org-align-tags-here org-tags-column)))
18350 (defun org-delete-backward-char (N)
18351 "Like `delete-backward-char', insert whitespace at field end in tables.
18352 When deleting backwards, in tables this function will insert whitespace in
18353 front of the next \"|\" separator, to keep the table aligned. The table will
18354 still be marked for re-alignment if the field did fill the entire column,
18355 because, in this case the deletion might narrow the column."
18358 (org-check-before-invisible-edit 'delete-backward)
18359 (if (and (org-table-p)
18361 (string-match "|" (buffer-substring (point-at-bol) (point)))
18362 (looking-at ".*?|"))
18363 (let ((pos (point))
18364 (noalign (looking-at "[^|\n\r]* |"))
18365 (c org-table-may-need-update))
18366 (backward-delete-char N)
18367 (if (not overwrite-mode)
18369 (skip-chars-forward "^|")
18371 (goto-char (1- pos))))
18372 ;; noalign: if there were two spaces at the end, this field
18373 ;; does not determine the width of the column.
18374 (if noalign (setq org-table-may-need-update c)))
18375 (backward-delete-char N)
18376 (org-fix-tags-on-the-fly))))
18378 (defun org-delete-char (N)
18379 "Like `delete-char', but insert whitespace at field end in tables.
18380 When deleting characters, in tables this function will insert whitespace in
18381 front of the next \"|\" separator, to keep the table aligned. The table will
18382 still be marked for re-alignment if the field did fill the entire column,
18383 because, in this case the deletion might narrow the column."
18386 (org-check-before-invisible-edit 'delete)
18387 (if (and (org-table-p)
18389 (not (= (char-after) ?|))
18391 (if (looking-at ".*?|")
18392 (let ((pos (point))
18393 (noalign (looking-at "[^|\n\r]* |"))
18394 (c org-table-may-need-update))
18395 (replace-match (concat
18396 (substring (match-string 0) 1 -1)
18399 ;; noalign: if there were two spaces at the end, this field
18400 ;; does not determine the width of the column.
18401 (if noalign (setq org-table-may-need-update c)))
18404 (org-fix-tags-on-the-fly))))
18406 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
18407 (put 'org-self-insert-command 'delete-selection t)
18408 (put 'orgtbl-self-insert-command 'delete-selection t)
18409 (put 'org-delete-char 'delete-selection 'supersede)
18410 (put 'org-delete-backward-char 'delete-selection 'supersede)
18411 (put 'org-yank 'delete-selection 'yank)
18413 ;; Make `flyspell-mode' delay after some commands
18414 (put 'org-self-insert-command 'flyspell-delayed t)
18415 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
18416 (put 'org-delete-char 'flyspell-delayed t)
18417 (put 'org-delete-backward-char 'flyspell-delayed t)
18419 ;; Make pabbrev-mode expand after org-mode commands
18420 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
18421 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
18423 ;; How to do this: Measure non-white length of current string
18424 ;; If equal to column width, we should realign.
18426 (defun org-remap (map &rest commands)
18427 "In MAP, remap the functions given in COMMANDS.
18428 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
18431 (setq old (pop commands) new (pop commands))
18432 (if (fboundp 'command-remapping)
18433 (org-defkey map (vector 'remap old) new)
18434 (substitute-key-definition old new map global-map)))))
18436 (when (eq org-enable-table-editor 'optimized)
18437 ;; If the user wants maximum table support, we need to hijack
18438 ;; some standard editing functions
18439 (org-remap org-mode-map
18440 'self-insert-command 'org-self-insert-command
18441 'delete-char 'org-delete-char
18442 'delete-backward-char 'org-delete-backward-char)
18443 (org-defkey org-mode-map "|" 'org-force-self-insert))
18445 (defvar org-ctrl-c-ctrl-c-hook nil
18446 "Hook for functions attaching themselves to `C-c C-c'.
18448 This can be used to add additional functionality to the C-c C-c
18449 key which executes context-dependent commands. This hook is run
18450 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
18451 run after the last test.
18453 Each function will be called with no arguments. The function
18454 must check if the context is appropriate for it to act. If yes,
18455 it should do its thing and then return a non-nil value. If the
18456 context is wrong, just do nothing and return nil.")
18458 (defvar org-ctrl-c-ctrl-c-final-hook nil
18459 "Hook for functions attaching themselves to `C-c C-c'.
18461 This can be used to add additional functionality to the C-c C-c
18462 key which executes context-dependent commands. This hook is run
18463 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
18464 before the first test.
18466 Each function will be called with no arguments. The function
18467 must check if the context is appropriate for it to act. If yes,
18468 it should do its thing and then return a non-nil value. If the
18469 context is wrong, just do nothing and return nil.")
18471 (defvar org-tab-first-hook nil
18472 "Hook for functions to attach themselves to TAB.
18473 See `org-ctrl-c-ctrl-c-hook' for more information.
18474 This hook runs as the first action when TAB is pressed, even before
18475 `org-cycle' messes around with the `outline-regexp' to cater for
18476 inline tasks and plain list item folding.
18477 If any function in this hook returns t, any other actions that
18478 would have been caused by TAB (such as table field motion or visibility
18479 cycling) will not occur.")
18481 (defvar org-tab-after-check-for-table-hook nil
18482 "Hook for functions to attach themselves to TAB.
18483 See `org-ctrl-c-ctrl-c-hook' for more information.
18484 This hook runs after it has been established that the cursor is not in a
18485 table, but before checking if the cursor is in a headline or if global cycling
18487 If any function in this hook returns t, not other actions like visibility
18488 cycling will be done.")
18490 (defvar org-tab-after-check-for-cycling-hook nil
18491 "Hook for functions to attach themselves to TAB.
18492 See `org-ctrl-c-ctrl-c-hook' for more information.
18493 This hook runs after it has been established that not table field motion and
18494 not visibility should be done because of current context. This is probably
18495 the place where a package like yasnippets can hook in.")
18497 (defvar org-tab-before-tab-emulation-hook nil
18498 "Hook for functions to attach themselves to TAB.
18499 See `org-ctrl-c-ctrl-c-hook' for more information.
18500 This hook runs after every other options for TAB have been exhausted, but
18501 before indentation and \t insertion takes place.")
18503 (defvar org-metaleft-hook nil
18504 "Hook for functions attaching themselves to `M-left'.
18505 See `org-ctrl-c-ctrl-c-hook' for more information.")
18506 (defvar org-metaright-hook nil
18507 "Hook for functions attaching themselves to `M-right'.
18508 See `org-ctrl-c-ctrl-c-hook' for more information.")
18509 (defvar org-metaup-hook nil
18510 "Hook for functions attaching themselves to `M-up'.
18511 See `org-ctrl-c-ctrl-c-hook' for more information.")
18512 (defvar org-metadown-hook nil
18513 "Hook for functions attaching themselves to `M-down'.
18514 See `org-ctrl-c-ctrl-c-hook' for more information.")
18515 (defvar org-shiftmetaleft-hook nil
18516 "Hook for functions attaching themselves to `M-S-left'.
18517 See `org-ctrl-c-ctrl-c-hook' for more information.")
18518 (defvar org-shiftmetaright-hook nil
18519 "Hook for functions attaching themselves to `M-S-right'.
18520 See `org-ctrl-c-ctrl-c-hook' for more information.")
18521 (defvar org-shiftmetaup-hook nil
18522 "Hook for functions attaching themselves to `M-S-up'.
18523 See `org-ctrl-c-ctrl-c-hook' for more information.")
18524 (defvar org-shiftmetadown-hook nil
18525 "Hook for functions attaching themselves to `M-S-down'.
18526 See `org-ctrl-c-ctrl-c-hook' for more information.")
18527 (defvar org-metareturn-hook nil
18528 "Hook for functions attaching themselves to `M-RET'.
18529 See `org-ctrl-c-ctrl-c-hook' for more information.")
18530 (defvar org-shiftup-hook nil
18531 "Hook for functions attaching themselves to `S-up'.
18532 See `org-ctrl-c-ctrl-c-hook' for more information.")
18533 (defvar org-shiftup-final-hook nil
18534 "Hook for functions attaching themselves to `S-up'.
18535 This one runs after all other options except shift-select have been excluded.
18536 See `org-ctrl-c-ctrl-c-hook' for more information.")
18537 (defvar org-shiftdown-hook nil
18538 "Hook for functions attaching themselves to `S-down'.
18539 See `org-ctrl-c-ctrl-c-hook' for more information.")
18540 (defvar org-shiftdown-final-hook nil
18541 "Hook for functions attaching themselves to `S-down'.
18542 This one runs after all other options except shift-select have been excluded.
18543 See `org-ctrl-c-ctrl-c-hook' for more information.")
18544 (defvar org-shiftleft-hook nil
18545 "Hook for functions attaching themselves to `S-left'.
18546 See `org-ctrl-c-ctrl-c-hook' for more information.")
18547 (defvar org-shiftleft-final-hook nil
18548 "Hook for functions attaching themselves to `S-left'.
18549 This one runs after all other options except shift-select have been excluded.
18550 See `org-ctrl-c-ctrl-c-hook' for more information.")
18551 (defvar org-shiftright-hook nil
18552 "Hook for functions attaching themselves to `S-right'.
18553 See `org-ctrl-c-ctrl-c-hook' for more information.")
18554 (defvar org-shiftright-final-hook nil
18555 "Hook for functions attaching themselves to `S-right'.
18556 This one runs after all other options except shift-select have been excluded.
18557 See `org-ctrl-c-ctrl-c-hook' for more information.")
18559 (defun org-modifier-cursor-error ()
18560 "Throw an error, a modified cursor command was applied in wrong context."
18561 (error "This command is active in special context like tables, headlines or items"))
18563 (defun org-shiftselect-error ()
18564 "Throw an error because Shift-Cursor command was applied in wrong context."
18565 (if (and (boundp 'shift-select-mode) shift-select-mode)
18566 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
18567 (error "This command works only in special context like headlines or timestamps")))
18569 (defun org-call-for-shift-select (cmd)
18570 (let ((this-command-keys-shift-translated t))
18571 (call-interactively cmd)))
18573 (defun org-shifttab (&optional arg)
18574 "Global visibility cycling or move to previous table field.
18575 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
18577 See the individual commands for more information."
18580 ((org-at-table-p) (call-interactively 'org-table-previous-field))
18582 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
18583 (message "Content view to level: %d" arg)
18584 (org-content (prefix-numeric-value arg2))
18585 (setq org-cycle-global-status 'overview)))
18586 (t (call-interactively 'org-global-cycle))))
18588 (defun org-shiftmetaleft ()
18589 "Promote subtree or delete table column.
18590 Calls `org-promote-subtree', `org-outdent-item-tree', or
18591 `org-table-delete-column', depending on context. See the
18592 individual commands for more information."
18595 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
18596 ((org-at-table-p) (call-interactively 'org-table-delete-column))
18597 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
18598 ((if (not (org-region-active-p)) (org-at-item-p)
18599 (save-excursion (goto-char (region-beginning))
18601 (call-interactively 'org-outdent-item-tree))
18602 (t (org-modifier-cursor-error))))
18604 (defun org-shiftmetaright ()
18605 "Demote subtree or insert table column.
18606 Calls `org-demote-subtree', `org-indent-item-tree', or
18607 `org-table-insert-column', depending on context. See the
18608 individual commands for more information."
18611 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
18612 ((org-at-table-p) (call-interactively 'org-table-insert-column))
18613 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
18614 ((if (not (org-region-active-p)) (org-at-item-p)
18615 (save-excursion (goto-char (region-beginning))
18617 (call-interactively 'org-indent-item-tree))
18618 (t (org-modifier-cursor-error))))
18620 (defun org-shiftmetaup (&optional arg)
18621 "Move subtree up or kill table row.
18622 Calls `org-move-subtree-up' or `org-table-kill-row' or
18623 `org-move-item-up' or `org-timestamp-up', depending on context.
18624 See the individual commands for more information."
18627 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
18628 ((org-at-table-p) (call-interactively 'org-table-kill-row))
18629 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18630 ((org-at-item-p) (call-interactively 'org-move-item-up))
18631 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18632 (call-interactively 'org-timestamp-up)))
18633 (t (org-modifier-cursor-error))))
18635 (defun org-shiftmetadown (&optional arg)
18636 "Move subtree down or insert table row.
18637 Calls `org-move-subtree-down' or `org-table-insert-row' or
18638 `org-move-item-down' or `org-timestamp-up', depending on context.
18639 See the individual commands for more information."
18642 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
18643 ((org-at-table-p) (call-interactively 'org-table-insert-row))
18644 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18645 ((org-at-item-p) (call-interactively 'org-move-item-down))
18646 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18647 (call-interactively 'org-timestamp-down)))
18648 (t (org-modifier-cursor-error))))
18650 (defsubst org-hidden-tree-error ()
18652 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
18654 (defun org-metaleft (&optional arg)
18655 "Promote heading or move table column to left.
18656 Calls `org-do-promote' or `org-table-move-column', depending on context.
18657 With no specific context, calls the Emacs default `backward-word'.
18658 See the individual commands for more information."
18661 ((run-hook-with-args-until-success 'org-metaleft-hook))
18662 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
18663 ((org-with-limited-levels
18664 (or (org-at-heading-p)
18665 (and (org-region-active-p)
18667 (goto-char (region-beginning))
18668 (org-at-heading-p)))))
18669 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18670 (call-interactively 'org-do-promote))
18671 ;; At an inline task.
18672 ((org-at-heading-p)
18673 (call-interactively 'org-inlinetask-promote))
18674 ((or (org-at-item-p)
18675 (and (org-region-active-p)
18677 (goto-char (region-beginning))
18679 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18680 (call-interactively 'org-outdent-item))
18681 (t (call-interactively 'backward-word))))
18683 (defun org-metaright (&optional arg)
18684 "Demote a subtree, a list item or move table column to right.
18685 In front of a drawer or a block keyword, indent it correctly.
18686 With no specific context, calls the Emacs default `forward-word'.
18687 See the individual commands for more information."
18690 ((run-hook-with-args-until-success 'org-metaright-hook))
18691 ((org-at-table-p) (call-interactively 'org-table-move-column))
18692 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
18693 ((org-at-block-p) (call-interactively 'org-indent-block))
18694 ((org-with-limited-levels
18695 (or (org-at-heading-p)
18696 (and (org-region-active-p)
18698 (goto-char (region-beginning))
18699 (org-at-heading-p)))))
18700 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18701 (call-interactively 'org-do-demote))
18702 ;; At an inline task.
18703 ((org-at-heading-p)
18704 (call-interactively 'org-inlinetask-demote))
18705 ((or (org-at-item-p)
18706 (and (org-region-active-p)
18708 (goto-char (region-beginning))
18710 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18711 (call-interactively 'org-indent-item))
18712 (t (call-interactively 'forward-word))))
18714 (defun org-check-for-hidden (what)
18715 "Check if there are hidden headlines/items in the current visual line.
18716 WHAT can be either `headlines' or `items'. If the current line is
18717 an outline or item heading and it has a folded subtree below it,
18718 this function returns t, nil otherwise."
18720 ((eq what 'headlines) org-outline-regexp-bol)
18721 ((eq what 'items) (org-item-beginning-re))
18722 (t (error "This should not happen"))))
18726 (unless (org-region-active-p)
18727 (setq beg (point-at-bol))
18728 (beginning-of-line 2)
18729 (while (and (not (eobp)) ;; this is like `next-line'
18730 (get-char-property (1- (point)) 'invisible))
18731 (beginning-of-line 2))
18734 (goto-char (point-at-eol))
18735 (setq end (max end (point)))
18736 (while (re-search-forward re end t)
18737 (if (get-char-property (match-beginning 0) 'invisible)
18741 (autoload 'org-element-at-point "org-element")
18742 (autoload 'org-element-type "org-element")
18744 (declare-function org-element-at-point "org-element" (&optional keep-trail))
18745 (declare-function org-element-type "org-element" (element))
18746 (declare-function org-element-contents "org-element" (element))
18747 (declare-function org-element-property "org-element" (property element))
18748 (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
18749 (declare-function org-element-nested-p "org-element" (elem-a elem-b))
18750 (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
18751 (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
18752 (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
18754 (defun org-metaup (&optional arg)
18755 "Move subtree up or move table row up.
18756 Calls `org-move-subtree-up' or `org-table-move-row' or
18757 `org-move-item-up', depending on context. See the individual commands
18758 for more information."
18761 ((run-hook-with-args-until-success 'org-metaup-hook))
18762 ((org-region-active-p)
18763 (let* ((a (min (region-beginning) (region-end)))
18764 (b (1- (max (region-beginning) (region-end))))
18765 (c (save-excursion (goto-char a)
18766 (move-beginning-of-line 0)))
18767 (d (save-excursion (goto-char a)
18768 (move-end-of-line 0) (point))))
18769 (transpose-regions a b c d)
18771 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
18772 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18773 ((org-at-item-p) (call-interactively 'org-move-item-up))
18774 (t (org-drag-element-backward))))
18776 (defun org-metadown (&optional arg)
18777 "Move subtree down or move table row down.
18778 Calls `org-move-subtree-down' or `org-table-move-row' or
18779 `org-move-item-down', depending on context. See the individual
18780 commands for more information."
18783 ((run-hook-with-args-until-success 'org-metadown-hook))
18784 ((org-region-active-p)
18785 (let* ((a (min (region-beginning) (region-end)))
18786 (b (max (region-beginning) (region-end)))
18787 (c (save-excursion (goto-char b)
18788 (move-beginning-of-line 1)))
18789 (d (save-excursion (goto-char b)
18790 (move-end-of-line 1) (1+ (point)))))
18791 (transpose-regions a b c d)
18793 ((org-at-table-p) (call-interactively 'org-table-move-row))
18794 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18795 ((org-at-item-p) (call-interactively 'org-move-item-down))
18796 (t (org-drag-element-forward))))
18798 (defun org-shiftup (&optional arg)
18799 "Increase item in timestamp or increase priority of current headline.
18800 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
18801 depending on context. See the individual commands for more information."
18804 ((run-hook-with-args-until-success 'org-shiftup-hook))
18805 ((and org-support-shift-select (org-region-active-p))
18806 (org-call-for-shift-select 'previous-line))
18807 ((org-at-timestamp-p t)
18808 (call-interactively (if org-edit-timestamp-down-means-later
18809 'org-timestamp-down 'org-timestamp-up)))
18810 ((and (not (eq org-support-shift-select 'always))
18811 org-enable-priority-commands
18812 (org-at-heading-p))
18813 (call-interactively 'org-priority-up))
18814 ((and (not org-support-shift-select) (org-at-item-p))
18815 (call-interactively 'org-previous-item))
18816 ((org-clocktable-try-shift 'up arg))
18817 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
18818 (org-support-shift-select
18819 (org-call-for-shift-select 'previous-line))
18820 (t (org-shiftselect-error))))
18822 (defun org-shiftdown (&optional arg)
18823 "Decrease item in timestamp or decrease priority of current headline.
18824 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
18825 depending on context. See the individual commands for more information."
18828 ((run-hook-with-args-until-success 'org-shiftdown-hook))
18829 ((and org-support-shift-select (org-region-active-p))
18830 (org-call-for-shift-select 'next-line))
18831 ((org-at-timestamp-p t)
18832 (call-interactively (if org-edit-timestamp-down-means-later
18833 'org-timestamp-up 'org-timestamp-down)))
18834 ((and (not (eq org-support-shift-select 'always))
18835 org-enable-priority-commands
18836 (org-at-heading-p))
18837 (call-interactively 'org-priority-down))
18838 ((and (not org-support-shift-select) (org-at-item-p))
18839 (call-interactively 'org-next-item))
18840 ((org-clocktable-try-shift 'down arg))
18841 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
18842 (org-support-shift-select
18843 (org-call-for-shift-select 'next-line))
18844 (t (org-shiftselect-error))))
18846 (defun org-shiftright (&optional arg)
18847 "Cycle the thing at point or in the current line, depending on context.
18848 Depending on context, this does one of the following:
18850 - switch a timestamp at point one day into the future
18851 - on a headline, switch to the next TODO keyword.
18852 - on an item, switch entire list to the next bullet type
18853 - on a property line, switch to the next allowed value
18854 - on a clocktable definition line, move time block into the future"
18857 ((run-hook-with-args-until-success 'org-shiftright-hook))
18858 ((and org-support-shift-select (org-region-active-p))
18859 (org-call-for-shift-select 'forward-char))
18860 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
18861 ((and (not (eq org-support-shift-select 'always))
18862 (org-at-heading-p))
18863 (let ((org-inhibit-logging
18864 (not org-treat-S-cursor-todo-selection-as-state-change))
18865 (org-inhibit-blocking
18866 (not org-treat-S-cursor-todo-selection-as-state-change)))
18867 (org-call-with-arg 'org-todo 'right)))
18868 ((or (and org-support-shift-select
18869 (not (eq org-support-shift-select 'always))
18870 (org-at-item-bullet-p))
18871 (and (not org-support-shift-select) (org-at-item-p)))
18872 (org-call-with-arg 'org-cycle-list-bullet nil))
18873 ((and (not (eq org-support-shift-select 'always))
18874 (org-at-property-p))
18875 (call-interactively 'org-property-next-allowed-value))
18876 ((org-clocktable-try-shift 'right arg))
18877 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
18878 (org-support-shift-select
18879 (org-call-for-shift-select 'forward-char))
18880 (t (org-shiftselect-error))))
18882 (defun org-shiftleft (&optional arg)
18883 "Cycle the thing at point or in the current line, depending on context.
18884 Depending on context, this does one of the following:
18886 - switch a timestamp at point one day into the past
18887 - on a headline, switch to the previous TODO keyword.
18888 - on an item, switch entire list to the previous bullet type
18889 - on a property line, switch to the previous allowed value
18890 - on a clocktable definition line, move time block into the past"
18893 ((run-hook-with-args-until-success 'org-shiftleft-hook))
18894 ((and org-support-shift-select (org-region-active-p))
18895 (org-call-for-shift-select 'backward-char))
18896 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
18897 ((and (not (eq org-support-shift-select 'always))
18898 (org-at-heading-p))
18899 (let ((org-inhibit-logging
18900 (not org-treat-S-cursor-todo-selection-as-state-change))
18901 (org-inhibit-blocking
18902 (not org-treat-S-cursor-todo-selection-as-state-change)))
18903 (org-call-with-arg 'org-todo 'left)))
18904 ((or (and org-support-shift-select
18905 (not (eq org-support-shift-select 'always))
18906 (org-at-item-bullet-p))
18907 (and (not org-support-shift-select) (org-at-item-p)))
18908 (org-call-with-arg 'org-cycle-list-bullet 'previous))
18909 ((and (not (eq org-support-shift-select 'always))
18910 (org-at-property-p))
18911 (call-interactively 'org-property-previous-allowed-value))
18912 ((org-clocktable-try-shift 'left arg))
18913 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
18914 (org-support-shift-select
18915 (org-call-for-shift-select 'backward-char))
18916 (t (org-shiftselect-error))))
18918 (defun org-shiftcontrolright ()
18919 "Switch to next TODO set."
18922 ((and org-support-shift-select (org-region-active-p))
18923 (org-call-for-shift-select 'forward-word))
18924 ((and (not (eq org-support-shift-select 'always))
18925 (org-at-heading-p))
18926 (org-call-with-arg 'org-todo 'nextset))
18927 (org-support-shift-select
18928 (org-call-for-shift-select 'forward-word))
18929 (t (org-shiftselect-error))))
18931 (defun org-shiftcontrolleft ()
18932 "Switch to previous TODO set."
18935 ((and org-support-shift-select (org-region-active-p))
18936 (org-call-for-shift-select 'backward-word))
18937 ((and (not (eq org-support-shift-select 'always))
18938 (org-at-heading-p))
18939 (org-call-with-arg 'org-todo 'previousset))
18940 (org-support-shift-select
18941 (org-call-for-shift-select 'backward-word))
18942 (t (org-shiftselect-error))))
18944 (defun org-shiftcontrolup ()
18945 "Change timestamps synchronously up in CLOCK log lines."
18947 (cond ((and (not org-support-shift-select)
18948 (org-at-clock-log-p)
18949 (org-at-timestamp-p t))
18950 (org-clock-timestamps-up))
18951 (t (org-shiftselect-error))))
18953 (defun org-shiftcontroldown ()
18954 "Change timestamps synchronously down in CLOCK log lines."
18956 (cond ((and (not org-support-shift-select)
18957 (org-at-clock-log-p)
18958 (org-at-timestamp-p t))
18959 (org-clock-timestamps-down))
18960 (t (org-shiftselect-error))))
18962 (defun org-ctrl-c-ret ()
18963 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
18966 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
18967 (t (call-interactively 'org-insert-heading))))
18969 (defun org-find-visible ()
18971 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18972 (get-char-property s 'invisible)))
18974 (defun org-find-invisible ()
18976 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18977 (not (get-char-property s 'invisible))))
18980 (defun org-copy-visible (beg end)
18981 "Copy the visible parts of the region."
18986 (narrow-to-region beg end)
18987 (setq s (goto-char (point-min)))
18988 (while (not (= (point) (point-max)))
18989 (goto-char (org-find-invisible))
18990 (push (buffer-substring s (point)) snippets)
18991 (setq s (goto-char (org-find-visible))))))
18992 (kill-new (apply 'concat (nreverse snippets)))))
18994 (defun org-copy-special ()
18995 "Copy region in table or copy current subtree.
18996 Calls `org-table-copy' or `org-copy-subtree', depending on context.
18997 See the individual commands for more information."
18999 (call-interactively
19000 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
19002 (defun org-cut-special ()
19003 "Cut region in table or cut current subtree.
19004 Calls `org-table-copy' or `org-cut-subtree', depending on context.
19005 See the individual commands for more information."
19007 (call-interactively
19008 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
19010 (defun org-paste-special (arg)
19011 "Paste rectangular region into table, or past subtree relative to level.
19012 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
19013 See the individual commands for more information."
19015 (if (org-at-table-p)
19016 (org-table-paste-rectangle)
19017 (org-paste-subtree arg)))
19019 (defsubst org-in-fixed-width-region-p ()
19020 "Is point in a fixed-width region?"
19022 (eq 'fixed-width (org-element-type (org-element-at-point)))))
19024 (defun org-edit-special (&optional arg)
19025 "Call a special editor for the stuff at point.
19026 When at a table, call the formula editor with `org-table-edit-formulas'.
19027 When in a source code block, call `org-edit-src-code'.
19028 When in a fixed-width region, call `org-edit-fixed-width-region'.
19029 When in an #+include line, visit the included file.
19030 On a link, call `ffap' to visit the link at point.
19031 Otherwise, return a user error."
19033 ;; possibly prep session before editing source
19034 (when (and (org-in-src-block-p) arg)
19035 (let* ((info (org-babel-get-src-block-info))
19036 (lang (nth 0 info))
19037 (params (nth 2 info))
19038 (session (cdr (assoc :session params))))
19039 (when (and info session) ;; we are in a source-code block with a session
19041 (intern (concat "org-babel-prep-session:" lang)) session params))))
19042 (cond ;; proceed with `org-edit-special'
19044 (beginning-of-line 1)
19045 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
19046 (find-file (org-trim (match-string 1))))
19047 ((org-at-table.el-p) (org-edit-src-code))
19048 ((or (org-at-table-p)
19050 (beginning-of-line 1)
19051 (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
19052 (call-interactively 'org-table-edit-formulas))
19053 ((org-in-block-p '("src" "example" "latex" "html")) (org-edit-src-code))
19054 ((org-in-fixed-width-region-p) (org-edit-fixed-width-region))
19055 ((org-at-regexp-p org-any-link-re) (call-interactively 'ffap))
19056 (t (user-error "No special environment to edit here"))))
19058 (defvar org-table-coordinate-overlays) ; defined in org-table.el
19059 (defun org-ctrl-c-ctrl-c (&optional arg)
19060 "Set tags in headline, or update according to changed information at point.
19062 This command does many different things, depending on context:
19064 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
19065 this is what we do.
19067 - If the cursor is on a statistics cookie, update it.
19069 - If the cursor is in a headline, prompt for tags and insert them
19070 into the current line, aligned to `org-tags-column'. When called
19071 with prefix arg, realign all tags in the current buffer.
19073 - If the cursor is in one of the special #+KEYWORD lines, this
19074 triggers scanning the buffer for these lines and updating the
19077 - If the cursor is inside a table, realign the table. This command
19078 works even if the automatic table editor has been turned off.
19080 - If the cursor is on a #+TBLFM line, re-apply the formulas to
19083 - If the cursor is at a footnote reference or definition, jump to
19084 the corresponding definition or references, respectively.
19086 - If the cursor is a the beginning of a dynamic block, update it.
19088 - If the current buffer is a capture buffer, close note and file it.
19090 - If the cursor is on a <<<target>>>, update radio targets and
19091 corresponding links in this buffer.
19093 - If the cursor is on a numbered item in a plain list, renumber the
19096 - If the cursor is on a checkbox, toggle it.
19098 - If the cursor is on a code block, evaluate it. The variable
19099 `org-confirm-babel-evaluate' can be used to control prompting
19100 before code block evaluation, by default every code block
19101 evaluation requires confirmation. Code block evaluation can be
19102 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
19104 (let ((org-enable-table-editor t))
19106 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
19107 org-occur-highlights
19108 org-latex-fragment-image-overlays)
19109 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
19110 (org-remove-occur-highlights)
19111 (org-remove-latex-fragment-image-overlays)
19112 (message "Temporary highlights/overlays removed from current buffer"))
19113 ((and (local-variable-p 'org-finish-function (current-buffer))
19114 (fboundp org-finish-function))
19115 (funcall org-finish-function))
19116 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
19117 ((org-in-regexp org-ts-regexp-both)
19118 (org-timestamp-change 0 'day))
19119 ((or (looking-at org-property-start-re)
19120 (org-at-property-p))
19121 (call-interactively 'org-property-action))
19122 ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
19123 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
19124 (or (org-at-heading-p) (org-at-item-p)))
19125 (call-interactively 'org-update-statistics-cookies))
19126 ((org-at-heading-p) (call-interactively 'org-set-tags))
19127 ((org-at-table.el-p)
19128 (message "Use C-c ' to edit table.el tables"))
19130 (org-table-maybe-eval-formula)
19132 (call-interactively 'org-table-recalculate)
19133 (org-table-maybe-recalculate-line))
19134 (call-interactively 'org-table-align)
19135 (orgtbl-send-table 'maybe))
19136 ((or (org-footnote-at-reference-p)
19137 (org-footnote-at-definition-p))
19138 (call-interactively 'org-footnote-action))
19139 ((org-at-item-checkbox-p)
19140 ;; Cursor at a checkbox: repair list and update checkboxes. Send
19141 ;; list only if at top item.
19142 (let* ((cbox (match-string 1))
19143 (struct (org-list-struct))
19144 (old-struct (copy-tree struct))
19145 (parents (org-list-parents-alist struct))
19146 (orderedp (org-entry-get nil "ORDERED"))
19147 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19149 ;; Use a light version of `org-toggle-checkbox' to avoid
19150 ;; computing list structure twice.
19151 (let ((new-box (cond
19152 ((equal arg '(16)) "[-]")
19153 ((equal arg '(4)) nil)
19154 ((equal "[X]" cbox) "[ ]")
19156 (if (and firstp arg)
19157 ;; If at first item of sub-list, remove check-box from
19158 ;; every item at the same level.
19160 (lambda (pos) (org-list-set-checkbox pos struct new-box))
19161 (org-list-get-all-items
19162 (point-at-bol) struct (org-list-prevs-alist struct)))
19163 (org-list-set-checkbox (point-at-bol) struct new-box)))
19164 ;; Replicate `org-list-write-struct', while grabbing a return
19165 ;; value from `org-list-struct-fix-box'.
19166 (org-list-struct-fix-ind struct parents 2)
19167 (org-list-struct-fix-item-end struct)
19168 (let ((prevs (org-list-prevs-alist struct)))
19169 (org-list-struct-fix-bul struct prevs)
19170 (org-list-struct-fix-ind struct parents)
19172 (org-list-struct-fix-box struct parents prevs orderedp)))
19173 (if (equal struct old-struct)
19174 (user-error "Cannot toggle this checkbox (unchecked subitems?)")
19175 (org-list-struct-apply-struct struct old-struct)
19176 (org-update-checkbox-count-maybe))
19179 "Checkboxes were removed due to unchecked box at line %d"
19180 (org-current-line block-item)))
19181 (when firstp (org-list-send-list 'maybe))))
19183 ;; Cursor at an item: repair list. Do checkbox related actions
19184 ;; only if function was called with an argument. Send list only
19186 (let* ((struct (org-list-struct))
19187 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19190 (setq old-struct (copy-tree struct))
19192 ;; If at first item of sub-list, add check-box to every
19193 ;; item at the same level.
19196 (unless (org-list-get-checkbox pos struct)
19197 (org-list-set-checkbox pos struct "[ ]")))
19198 (org-list-get-all-items
19199 (point-at-bol) struct (org-list-prevs-alist struct)))
19200 (org-list-set-checkbox (point-at-bol) struct "[ ]")))
19201 (org-list-write-struct
19202 struct (org-list-parents-alist struct) old-struct)
19203 (when arg (org-update-checkbox-count-maybe))
19204 (when firstp (org-list-send-list 'maybe))))
19205 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
19207 (beginning-of-line 1)
19208 (save-excursion (org-update-dblock)))
19210 (let ((case-fold-search t))
19211 (beginning-of-line 1)
19212 (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
19214 ((or (equal (match-string 1) "TBLFM")
19215 (equal (match-string 1) "tblfm"))
19216 ;; Recalculate the table before this line
19218 (beginning-of-line 1)
19219 (skip-chars-backward " \r\n\t")
19220 (if (org-at-table-p)
19221 (org-call-with-arg 'org-table-recalculate (or arg t)))))
19223 (let ((org-inhibit-startup-visibility-stuff t)
19224 (org-startup-align-all-tables nil))
19225 (when (boundp 'org-table-coordinate-overlays)
19226 (mapc 'delete-overlay org-table-coordinate-overlays)
19227 (setq org-table-coordinate-overlays nil))
19228 (org-save-outline-visibility 'use-markers (org-mode-restart)))
19229 (message "Local setup has been refreshed"))))
19230 ((org-clock-update-time-maybe))
19232 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
19233 (error "C-c C-c can do nothing useful at this location"))))))
19235 (defun org-mode-restart ()
19236 "Restart Org-mode, to scan again for special lines.
19237 Also updates the keyword regular expressions."
19240 (message "Org-mode restarted"))
19242 (defun org-kill-note-or-show-branches ()
19243 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
19245 (if (not org-finish-function)
19248 (call-interactively 'show-branches))
19249 (let ((org-note-abort t))
19250 (funcall org-finish-function))))
19252 (defun org-return (&optional indent)
19253 "Goto next table row or insert a newline.
19254 Calls `org-table-next-row' or `newline', depending on context.
19255 See the individual commands for more information."
19259 ((or (bobp) (org-in-src-block-p))
19260 (if indent (newline-and-indent) (newline)))
19262 (org-table-justify-field-maybe)
19263 (call-interactively 'org-table-next-row))
19264 ;; when `newline-and-indent' is called within a list, make sure
19265 ;; text moved stays inside the item.
19266 ((and (org-in-item-p) indent)
19267 (if (and (org-at-item-p) (>= (point) (match-end 0)))
19269 (save-match-data (newline))
19270 (org-indent-line-to (length (match-string 0))))
19271 (let ((ind (org-get-indentation)))
19273 (if (org-looking-back org-list-end-re)
19275 (org-indent-line-to ind)))))
19276 ((and org-return-follows-link
19277 (org-at-timestamp-p t)
19278 (not (eq org-ts-what 'after)))
19279 (org-follow-timestamp-link))
19280 ((and org-return-follows-link
19281 (let ((tprop (get-text-property (point) 'face)))
19282 (or (eq tprop 'org-link)
19283 (and (listp tprop) (memq 'org-link tprop)))))
19284 (call-interactively 'org-open-at-point))
19285 ((and (org-at-heading-p)
19287 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
19291 (t (if indent (newline-and-indent) (newline))))))
19293 (defun org-return-indent ()
19294 "Goto next table row or insert a newline and indent.
19295 Calls `org-table-next-row' or `newline-and-indent', depending on
19296 context. See the individual commands for more information."
19300 (defun org-ctrl-c-star ()
19301 "Compute table, or change heading status of lines.
19302 Calls `org-table-recalculate' or `org-toggle-heading',
19303 depending on context."
19307 (call-interactively 'org-table-recalculate))
19309 ;; Convert all lines in region to list items
19310 (call-interactively 'org-toggle-heading))))
19312 (defun org-ctrl-c-minus ()
19313 "Insert separator line in table or modify bullet status of line.
19314 Also turns a plain line or a region of lines into list items.
19315 Calls `org-table-insert-hline', `org-toggle-item', or
19316 `org-cycle-list-bullet', depending on context."
19320 (call-interactively 'org-table-insert-hline))
19321 ((org-region-active-p)
19322 (call-interactively 'org-toggle-item))
19324 (call-interactively 'org-cycle-list-bullet))
19326 (call-interactively 'org-toggle-item))))
19328 (defun org-toggle-item (arg)
19329 "Convert headings or normal lines to items, items to normal lines.
19330 If there is no active region, only the current line is considered.
19332 If the first non blank line in the region is an headline, convert
19333 all headlines to items, shifting text accordingly.
19335 If it is an item, convert all items to normal lines.
19337 If it is normal text, change region into an item. With a prefix
19338 argument ARG, change each line in region into an item."
19342 ;; Shift text in current section to IND, from point to END.
19343 ;; The function leaves point to END line.
19345 (let ((min-i 1000) (end (copy-marker end)))
19346 ;; First determine the minimum indentation (MIN-I) of
19350 (while (< (point) end)
19351 (let ((i (org-get-indentation)))
19353 ;; Skip blank lines and inline tasks.
19354 ((looking-at "^[ \t]*$"))
19355 ((looking-at org-outline-regexp-bol))
19356 ;; We can't find less than 0 indentation.
19357 ((zerop i) (throw 'exit (setq min-i 0)))
19358 ((< i min-i) (setq min-i i))))
19360 ;; Then indent each line so that a line indented to
19361 ;; MIN-I becomes indented to IND. Ignore blank lines
19362 ;; and inline tasks in the process.
19363 (let ((delta (- ind min-i)))
19364 (while (< (point) end)
19365 (unless (or (looking-at "^[ \t]*$")
19366 (looking-at org-outline-regexp-bol))
19367 (org-indent-line-to (+ (org-get-indentation) delta)))
19368 (forward-line)))))))
19371 ;; Return beginning of first non-blank line, starting from
19376 (skip-chars-forward " \r\t\n")
19379 ;; Determine boundaries of changes.
19380 (if (org-region-active-p)
19381 (setq beg (funcall skip-blanks (region-beginning))
19382 end (copy-marker (region-end)))
19383 (setq beg (funcall skip-blanks (point-at-bol))
19384 end (copy-marker (point-at-eol))))
19385 ;; Depending on the starting line, choose an action on the text
19386 ;; between BEG and END.
19387 (org-with-limited-levels
19391 ;; Case 1. Start at an item: de-itemize. Note that it only
19392 ;; happens when a region is active: `org-ctrl-c-minus'
19393 ;; would call `org-cycle-list-bullet' otherwise.
19395 (while (< (point) end)
19396 (when (org-at-item-p)
19397 (skip-chars-forward " \t")
19398 (delete-region (point) (match-end 0)))
19400 ;; Case 2. Start at an heading: convert to items.
19401 ((org-at-heading-p)
19402 (let* ((bul (org-list-bullet-string "-"))
19403 (bul-len (length bul))
19404 ;; Indentation of the first heading. It should be
19405 ;; relative to the indentation of its parent, if any.
19406 (start-ind (save-excursion
19408 ((not org-adapt-indentation) 0)
19409 ((not (outline-previous-heading)) 0)
19410 (t (length (match-string 0))))))
19411 ;; Level of first heading. Further headings will be
19412 ;; compared to it to determine hierarchy in the list.
19413 (ref-level (org-reduced-level (org-outline-level))))
19414 (while (< (point) end)
19415 (let* ((level (org-reduced-level (org-outline-level)))
19416 (delta (max 0 (- level ref-level))))
19417 ;; If current headline is less indented than the first
19418 ;; one, set it as reference, in order to preserve
19420 (when (< level ref-level) (setq ref-level level))
19421 (replace-match bul t t)
19422 (org-indent-line-to (+ start-ind (* delta bul-len)))
19423 ;; Ensure all text down to END (or SECTION-END) belongs
19424 ;; to the newly created item.
19425 (let ((section-end (save-excursion
19426 (or (outline-next-heading) (point)))))
19428 (funcall shift-text
19429 (+ start-ind (* (1+ delta) bul-len))
19430 (min end section-end)))))))
19431 ;; Case 3. Normal line with ARG: turn each non-item line into
19434 (while (< (point) end)
19435 (unless (or (org-at-heading-p) (org-at-item-p))
19436 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
19438 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
19440 ;; Case 4. Normal line without ARG: make the first line of
19441 ;; region an item, and shift indentation of others
19442 ;; lines to set them as item's body.
19443 (t (let* ((bul (org-list-bullet-string "-"))
19444 (bul-len (length bul))
19445 (ref-ind (org-get-indentation)))
19446 (skip-chars-forward " \t")
19449 (while (< (point) end)
19450 ;; Ensure that lines less indented than first one
19451 ;; still get included in item body.
19452 (funcall shift-text
19453 (+ ref-ind bul-len)
19454 (min end (save-excursion (or (outline-next-heading)
19456 (forward-line)))))))))
19458 (defun org-toggle-heading (&optional nstars)
19459 "Convert headings to normal text, or items or text to headings.
19460 If there is no active region, only the current line is considered.
19462 With a \\[universal-argument] prefix, convert the whole list at
19463 point into heading.
19467 - If the first non blank line is an headline, remove the stars
19468 from all headlines in the region.
19470 - If it is a normal line turn each and every normal line (i.e. not an
19471 heading or an item) in the region into a heading.
19473 - If it is a plain list item, turn all plain list items into headings.
19475 When converting a line into a heading, the number of stars is chosen
19476 such that the lines become children of the current entry. However,
19477 when a prefix argument is given, its value determines the number of
19482 ;; Return beginning of first non-blank line, starting from
19487 (while (org-at-comment-p) (forward-line))
19488 (skip-chars-forward " \r\t\n")
19491 ;; Determine boundaries of changes. If a universal prefix has
19492 ;; been given, put the list in a region. If region ends at a bol,
19493 ;; do not consider the last line to be in the region.
19495 (when (and current-prefix-arg (org-at-item-p))
19496 (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
19497 (org-mark-element))
19499 (if (org-region-active-p)
19500 (setq beg (funcall skip-blanks (region-beginning))
19501 end (copy-marker (save-excursion
19502 (goto-char (region-end))
19503 (if (bolp) (point) (point-at-eol)))))
19504 (setq beg (funcall skip-blanks (point-at-bol))
19505 end (copy-marker (point-at-eol))))
19506 ;; Ensure inline tasks don't count as headings.
19507 (org-with-limited-levels
19511 ;; Case 1. Started at an heading: de-star headings.
19512 ((org-at-heading-p)
19513 (while (< (point) end)
19514 (when (org-at-heading-p t)
19515 (looking-at org-outline-regexp) (replace-match "")
19518 ;; Case 2. Started at an item: change items into headlines.
19519 ;; One star will be added by `org-list-to-subtree'.
19521 (let* ((stars (make-string
19523 ;; subtract the star that will be added again by
19524 ;; `org-list-to-subtree'
19525 (1- (prefix-numeric-value current-prefix-arg))
19526 (or (org-current-level) 0))
19529 (cond (nstars "") ; stars from prefix only
19530 ((equal stars "") "") ; before first heading
19531 (org-odd-levels-only "*") ; inside heading, odd
19532 (t "")))) ; inside heading, oddeven
19533 (while (< (point) end)
19534 (when (org-at-item-p)
19535 ;; Pay attention to cases when region ends before list.
19536 (let* ((struct (org-list-struct))
19537 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
19539 (narrow-to-region (point) list-end)
19541 (org-list-to-subtree
19542 (org-list-parse-list t)
19543 '(:istart (concat stars add-stars (funcall get-stars depth))
19544 :icount (concat stars add-stars (funcall get-stars depth)))))))
19547 ;; Case 3. Started at normal text: make every line an heading,
19548 ;; skipping headlines and items.
19549 (t (let* ((stars (make-string
19551 (prefix-numeric-value current-prefix-arg)
19552 (or (org-current-level) 0))
19555 (cond (nstars "") ; stars from prefix only
19556 ((equal stars "") "*") ; before first heading
19557 (org-odd-levels-only "**") ; inside heading, odd
19558 (t "*"))) ; inside heading, oddeven
19559 (rpl (concat stars add-stars " ")))
19560 (while (< (point) end)
19561 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
19562 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
19563 (replace-match (concat rpl (match-string 2))) (setq toggled t))
19564 (forward-line)))))))
19565 (unless toggled (message "Cannot toggle heading from here"))))
19567 (defun org-meta-return (&optional arg)
19568 "Insert a new heading or wrap a region in a table.
19569 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
19570 See the individual commands for more information."
19573 ((run-hook-with-args-until-success 'org-metareturn-hook))
19574 ((or (org-at-drawer-p) (org-at-property-p))
19575 (newline-and-indent))
19577 (call-interactively 'org-table-wrap-region))
19578 (t (call-interactively 'org-insert-heading))))
19582 (defsubst org-in-subtree-not-table-p ()
19583 "Are we in a subtree and not in a table?"
19584 (and (not (org-before-first-heading-p))
19585 (not (org-at-table-p))))
19587 ;; Define the Org-mode menus
19588 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
19590 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
19591 ["Next Field" org-cycle (org-at-table-p)]
19592 ["Previous Field" org-shifttab (org-at-table-p)]
19593 ["Next Row" org-return (org-at-table-p)]
19595 ["Blank Field" org-table-blank-field (org-at-table-p)]
19596 ["Edit Field" org-table-edit-field (org-at-table-p)]
19597 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
19600 ["Move Column Left" org-metaleft (org-at-table-p)]
19601 ["Move Column Right" org-metaright (org-at-table-p)]
19602 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
19603 ["Insert Column" org-shiftmetaright (org-at-table-p)])
19605 ["Move Row Up" org-metaup (org-at-table-p)]
19606 ["Move Row Down" org-metadown (org-at-table-p)]
19607 ["Delete Row" org-shiftmetaup (org-at-table-p)]
19608 ["Insert Row" org-shiftmetadown (org-at-table-p)]
19609 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
19611 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
19613 ["Copy Rectangle" org-copy-special (org-at-table-p)]
19614 ["Cut Rectangle" org-cut-special (org-at-table-p)]
19615 ["Paste Rectangle" org-paste-special (org-at-table-p)]
19616 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
19619 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
19620 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
19621 ["Edit Formulas" org-edit-special (org-at-table-p)]
19623 ["Recalculate line" org-table-recalculate (org-at-table-p)]
19624 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
19625 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
19627 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
19629 ["Sum Column/Rectangle" org-table-sum
19630 (or (org-at-table-p) (org-region-active-p))]
19631 ["Which Column?" org-table-current-column (org-at-table-p)])
19633 org-table-toggle-formula-debugger
19634 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
19635 ["Show Col/Row Numbers"
19636 org-table-toggle-coordinate-overlays
19638 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
19640 ["Create" org-table-create (and (not (org-at-table-p))
19641 org-enable-table-editor)]
19642 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
19643 ["Import from File" org-table-import (not (org-at-table-p))]
19644 ["Export to File" org-table-export (org-at-table-p)]
19646 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
19648 (easy-menu-define org-org-menu org-mode-map "Org menu"
19651 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
19652 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
19653 ["Sparse Tree..." org-sparse-tree t]
19654 ["Reveal Context" org-reveal t]
19655 ["Show All" show-all t]
19657 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
19659 ["New Heading" org-insert-heading t]
19660 ("Navigate Headings"
19661 ["Up" outline-up-heading t]
19662 ["Next" outline-next-visible-heading t]
19663 ["Previous" outline-previous-visible-heading t]
19664 ["Next Same Level" outline-forward-same-level t]
19665 ["Previous Same Level" outline-backward-same-level t]
19667 ["Jump" org-goto t])
19669 ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
19671 ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
19672 ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
19674 ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
19675 ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
19676 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
19678 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
19680 ["Copy visible text" org-copy-visible t]
19682 ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
19683 ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
19684 ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
19685 ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
19687 ["Sort Region/Children" org-sort t]
19689 ["Convert to odd levels" org-convert-to-odd-levels t]
19690 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
19692 ["Emphasis..." org-emphasize t]
19693 ["Edit Source Example" org-edit-special t]
19695 ["Footnote new/jump" org-footnote-action t]
19696 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
19698 ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
19700 ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
19701 ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
19702 ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
19706 ["Store Link (Global)" org-store-link t]
19707 ["Find existing link to here" org-occur-link-in-agenda-files t]
19708 ["Insert Link" org-insert-link t]
19709 ["Follow Link" org-open-at-point t]
19711 ["Next link" org-next-link t]
19712 ["Previous link" org-previous-link t]
19714 ["Descriptive Links"
19715 org-toggle-link-display
19717 :selected org-descriptive-links
19720 org-toggle-link-display
19722 :selected (not org-descriptive-links)])
19725 ["TODO/DONE/-" org-todo t]
19727 ["Next keyword" org-shiftright (org-at-heading-p)]
19728 ["Previous keyword" org-shiftleft (org-at-heading-p)]
19729 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
19730 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
19731 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
19732 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
19733 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
19735 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
19736 :selected org-enforce-todo-dependencies :style toggle :active t]
19737 "Settings for tree at point"
19738 ["Do Children sequentially" org-toggle-ordered-property :style radio
19739 :selected (org-entry-get nil "ORDERED")
19740 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19741 ["Do Children parallel" org-toggle-ordered-property :style radio
19742 :selected (not (org-entry-get nil "ORDERED"))
19743 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19745 ["Set Priority" org-priority t]
19746 ["Priority Up" org-shiftup t]
19747 ["Priority Down" org-shiftdown t]
19749 ["Get news from all feeds" org-feed-update-all t]
19750 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
19751 ["Customize feeds" (customize-variable 'org-feed-alist) t])
19752 ("TAGS and Properties"
19753 ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
19754 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
19756 ["Set property" org-set-property (not (org-before-first-heading-p))]
19757 ["Column view of properties" org-columns t]
19758 ["Insert Column View DBlock" org-insert-columns-dblock t])
19759 ("Dates and Scheduling"
19760 ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
19761 ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
19763 ["1 Day Later" org-shiftright (org-at-timestamp-p)]
19764 ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
19765 ["1 ... Later" org-shiftup (org-at-timestamp-p)]
19766 ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
19767 ["Compute Time Range" org-evaluate-time-range t]
19768 ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
19769 ["Deadline" org-deadline (not (org-before-first-heading-p))]
19771 ["Custom time format" org-toggle-time-stamp-overlays
19772 :style radio :selected org-display-custom-times]
19774 ["Goto Calendar" org-goto-calendar t]
19775 ["Date from Calendar" org-date-from-calendar t]
19777 ["Start/Restart Timer" org-timer-start t]
19778 ["Pause/Continue Timer" org-timer-pause-or-continue t]
19779 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
19780 ["Insert Timer String" org-timer t]
19781 ["Insert Timer Item" org-timer-item t])
19783 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
19784 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
19785 ["Clock out" org-clock-out t]
19786 ["Clock cancel" org-clock-cancel t]
19788 ["Mark as default task" org-clock-mark-default-task t]
19789 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
19790 ["Goto running clock" org-clock-goto t]
19792 ["Display times" org-clock-display t]
19793 ["Create clock table" org-clock-report t]
19795 ["Record DONE time"
19796 (progn (setq org-log-done (not org-log-done))
19797 (message "Switching to %s will %s record a timestamp"
19798 (car org-done-keywords)
19799 (if org-log-done "automatically" "not")))
19800 :style toggle :selected org-log-done])
19802 ["Agenda Command..." org-agenda t]
19803 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
19804 ("File List for Agenda")
19805 ("Special views current file"
19806 ["TODO Tree" org-show-todo-tree t]
19807 ["Check Deadlines" org-check-deadlines t]
19808 ["Timeline" org-timeline t]
19809 ["Tags/Property tree" org-match-sparse-tree t])
19811 ["Export/Publish..." org-export t]
19813 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
19814 :selected org-cdlatex-mode]
19815 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
19816 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
19817 ["Modify math symbol" org-cdlatex-math-modify
19818 (org-inside-LaTeX-fragment-p)]
19819 ["Insert citation" org-reftex-citation t]
19821 ["Template for BEAMER" (progn (require 'org-beamer)
19822 (org-insert-beamer-options-template)) t])
19825 ["Push Files and Views" org-mobile-push t]
19826 ["Get Captured and Flagged" org-mobile-pull t]
19827 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
19829 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
19832 ["Show Version" org-version t]
19833 ["Info Documentation" org-info t])
19835 ["Browse Org Group" org-customize t]
19837 ["Expand This Menu" org-create-customize-menu
19838 (fboundp 'customize-menu-create)])
19839 ["Send bug report" org-submit-bug-report t]
19842 ["Refresh setup current buffer" org-mode-restart t]
19843 ["Reload Org (after update)" org-reload t]
19844 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
19847 (defun org-info (&optional node)
19848 "Read documentation for Org-mode in the info system.
19849 With optional NODE, go directly to that node."
19851 (info (format "(org)%s" (or node ""))))
19854 (defun org-submit-bug-report ()
19855 "Submit a bug report on Org-mode via mail.
19857 Don't hesitate to report any problems or inaccurate documentation.
19859 If you don't have setup sending mail from (X)Emacs, please copy the
19860 output buffer into your mail program, as it gives us important
19861 information about your Org-mode version and configuration."
19863 (require 'reporter)
19864 (org-load-modules-maybe)
19865 (org-require-autoloaded-modules)
19866 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
19867 (reporter-submit-bug-report
19868 "emacs-orgmode@gnu.org"
19869 (org-version nil 'full)
19871 (save-window-excursion
19872 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
19873 (delete-other-windows)
19875 (insert "You are about to submit a bug report to the Org-mode mailing list.
19877 We would like to add your full Org-mode and Outline configuration to the
19878 bug report. This greatly simplifies the work of the maintainer and
19879 other experts on the mailing list.
19881 HOWEVER, some variables you have customized may contain private
19882 information. The names of customers, colleagues, or friends, might
19883 appear in the form of file names, tags, todo states, or search strings.
19884 If you answer yes to the prompt, you might want to check and remove
19885 such private information before sending the email.")
19886 (add-text-properties (point-min) (point-max) '(face org-warning))
19887 (when (yes-or-no-p "Include your Org-mode configuration ")
19891 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
19892 (or (and (symbol-value v)
19893 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
19895 (get v 'custom-type) (get v 'standard-value)
19896 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
19898 (kill-buffer (get-buffer "*Warn about privacy*"))
19901 "Remember to cover the basics, that is, what you expected to happen and
19902 what in fact did happen. You don't know how to make a good report? See
19904 http://orgmode.org/manual/Feedback.html#Feedback
19906 Your bug report will be posted to the Org-mode mailing list.
19907 ------------------------------------------------------------------------")
19909 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
19910 (replace-match "\\1Bug: \\3 [\\2]")))))
19913 (defun org-install-agenda-files-menu ()
19914 (let ((bl (buffer-list)))
19917 (set-buffer (pop bl))
19918 (if (derived-mode-p 'org-mode) (setq bl nil)))
19919 (when (derived-mode-p 'org-mode)
19921 '("Org") "File List for Agenda"
19924 ["Edit File List" (org-edit-agenda-file-list) t]
19925 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
19926 ["Remove Current File from List" org-remove-file t]
19927 ["Cycle through agenda files" org-cycle-agenda-files t]
19928 ["Occur in all agenda files" org-occur-in-agenda-files t]
19930 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
19934 (defun org-require-autoloaded-modules ()
19937 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
19938 org-docbook org-exp org-html org-icalendar
19940 org-publish org-remember org-table
19941 org-timer org-xoxo)))
19944 (defun org-reload (&optional uncompiled)
19945 "Reload all org lisp files.
19946 With prefix arg UNCOMPILED, load the uncompiled versions."
19948 (require 'find-func)
19949 (let* ((file-re "^org\\(-.*\\)?\\.el")
19950 (dir-org (file-name-directory (org-find-library-dir "org")))
19951 (dir-org-contrib (ignore-errors
19952 (file-name-directory
19953 (org-find-library-dir "org-contribdir"))))
19955 (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
19956 (append (list nil "comint" "eval" "exp" "keys"
19957 "lob" "ref" "table" "tangle")
19961 (when (cdr lang) (symbol-name (car lang))))
19962 org-babel-load-languages)))))
19964 (append babel-files
19965 (and dir-org-contrib
19966 (directory-files dir-org-contrib t file-re))
19967 (directory-files dir-org t file-re)))
19968 (remove-re (concat (if (featurep 'xemacs)
19969 "org-colview" "org-colview-xemacs")
19971 (setq files (mapcar 'file-name-sans-extension files))
19972 (setq files (mapcar
19973 (lambda (x) (if (string-match remove-re x) nil x))
19975 (setq files (delq nil files))
19978 (when (featurep (intern (file-name-nondirectory f)))
19979 (if (and (not uncompiled)
19980 (file-exists-p (concat f ".elc")))
19981 (load (concat f ".elc") nil nil 'nosuffix)
19982 (load (concat f ".el") nil nil 'nosuffix))))
19984 (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
19985 (org-version nil 'full 'message))
19988 (defun org-customize ()
19989 "Call the customize function with org as argument."
19991 (org-load-modules-maybe)
19992 (org-require-autoloaded-modules)
19993 (customize-browse 'org))
19995 (defun org-create-customize-menu ()
19996 "Create a full customization menu for Org-mode, insert it into the menu."
19998 (org-load-modules-maybe)
19999 (org-require-autoloaded-modules)
20000 (if (fboundp 'customize-menu-create)
20003 '("Org") "Customize"
20004 `(["Browse Org group" org-customize t]
20006 ,(customize-menu-create 'org)
20007 ["Set" Custom-set t]
20008 ["Save" Custom-save t]
20009 ["Reset to Current" Custom-reset-current t]
20010 ["Reset to Saved" Custom-reset-saved t]
20011 ["Reset to Standard Settings" Custom-reset-standard t]))
20012 (message "\"Org\"-menu now contains full customization menu"))
20013 (error "Cannot expand menu (outdated version of cus-edit.el)")))
20015 ;;;; Miscellaneous stuff
20017 ;;; Generally useful functions
20019 (defun org-get-at-bol (property)
20020 "Get text property PROPERTY at beginning of line."
20021 (get-text-property (point-at-bol) property))
20023 (defun org-find-text-property-in-string (prop s)
20024 "Return the first non-nil value of property PROP in string S."
20025 (or (get-text-property 0 prop s)
20026 (get-text-property (or (next-single-property-change 0 prop s) 0)
20029 (defun org-display-warning (message) ;; Copied from Emacs-Muse
20030 "Display the given MESSAGE as a warning."
20031 (if (fboundp 'display-warning)
20032 (display-warning 'org message
20033 (if (featurep 'xemacs) 'warning :warning))
20034 (let ((buf (get-buffer-create "*Org warnings*")))
20035 (with-current-buffer buf
20036 (goto-char (point-max))
20037 (insert "Warning (Org): " message)
20040 (display-buffer buf)
20043 (defun org-eval (form)
20044 "Eval FORM and return result."
20045 (condition-case error
20047 (error (format "%%![Error: %s]" error))))
20049 (defun org-in-clocktable-p ()
20050 "Check if the cursor is in a clocktable."
20051 (let ((pos (point)) start)
20054 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
20055 (setq start (match-beginning 0))
20056 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
20057 (>= (match-end 0) pos)
20060 (defun org-in-commented-line ()
20061 "Is point in a line starting with `#'?"
20062 (equal (char-after (point-at-bol)) ?#))
20064 (defun org-in-indented-comment-line ()
20065 "Is point in a line starting with `#' after some white space?"
20068 (goto-char (point-at-bol))
20069 (looking-at "[ \t]*#"))))
20071 (defun org-in-verbatim-emphasis ()
20073 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
20075 (defun org-goto-marker-or-bmk (marker &optional bookmark)
20076 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
20077 (if (and marker (marker-buffer marker)
20078 (buffer-live-p (marker-buffer marker)))
20080 (org-pop-to-buffer-same-window (marker-buffer marker))
20081 (if (or (> marker (point-max)) (< marker (point-min)))
20084 (org-show-context 'org-goto))
20086 (bookmark-jump bookmark)
20087 (error "Cannot find location"))))
20089 (defun org-quote-csv-field (s)
20090 "Quote field for inclusion in CSV material."
20091 (if (string-match "[\",]" s)
20092 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
20095 (defun org-force-self-insert (N)
20096 "Needed to enforce self-insert under remapping."
20098 (self-insert-command N))
20100 (defun org-string-width (s)
20101 "Compute width of string, ignoring invisible characters.
20102 This ignores character with invisibility property `org-link', and also
20103 characters with property `org-cwidth', because these will become invisible
20104 upon the next fontification round."
20106 (when (or (eq t buffer-invisibility-spec)
20107 (assq 'org-link buffer-invisibility-spec))
20108 (while (setq b (text-property-any 0 (length s)
20109 'invisible 'org-link s))
20110 (setq s (concat (substring s 0 b)
20111 (substring s (or (next-single-property-change
20112 b 'invisible s) (length s)))))))
20113 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
20114 (setq s (concat (substring s 0 b)
20115 (substring s (or (next-single-property-change
20116 b 'org-cwidth s) (length s))))))
20117 (setq l (string-width s) b -1)
20118 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
20119 (setq l (- l (get-text-property b 'org-dwidth-n s))))
20122 (defun org-shorten-string (s maxlength)
20123 "Shorten string S so tht it is no longer than MAXLENGTH characters.
20124 If the string is shorter or has length MAXLENGTH, just return the
20125 original string. If it is longer, the functions finds a space in the
20126 string, breaks this string off at that locations and adds three dots
20127 as ellipsis. Including the ellipsis, the string will not be longer
20128 than MAXLENGTH. If finding a good breaking point in the string does
20129 not work, the string is just chopped off in the middle of a word
20131 (if (<= (length s) maxlength)
20133 (let* ((n (max (- maxlength 4) 1))
20134 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
20135 (if (string-match re s)
20136 (concat (match-string 1 s) "...")
20137 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
20139 (defun org-get-indentation (&optional line)
20140 "Get the indentation of the current line, interpreting tabs.
20141 When LINE is given, assume it represents a line and compute its indentation."
20143 (if (string-match "^ *" (org-remove-tabs line))
20146 (beginning-of-line 1)
20147 (skip-chars-forward " \t")
20148 (current-column))))
20150 (defun org-get-string-indentation (s)
20151 "What indentation has S due to SPACE and TAB at the beginning of the string?"
20152 (let ((n -1) (i 0) (w tab-width) c)
20154 (while (< (setq n (1+ n)) (length s))
20155 (setq c (aref s n))
20156 (cond ((= c ?\ ) (setq i (1+ i)))
20157 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
20158 (t (throw 'exit t)))))
20161 (defun org-remove-tabs (s &optional width)
20162 "Replace tabulators in S with spaces.
20163 Assumes that s is a single line, starting in column 0."
20164 (setq width (or width tab-width))
20165 (while (string-match "\t" s)
20166 (setq s (replace-match
20168 (- (* width (/ (+ (match-beginning 0) width) width))
20169 (match-beginning 0)) ?\ )
20173 (defun org-fix-indentation (line ind)
20174 "Fix indentation in LINE.
20175 IND is a cons cell with target and minimum indentation.
20176 If the current indentation in LINE is smaller than the minimum,
20177 leave it alone. If it is larger than ind, set it to the target."
20178 (let* ((l (org-remove-tabs line))
20179 (i (org-get-indentation l))
20180 (i1 (car ind)) (i2 (cdr ind)))
20181 (if (>= i i2) (setq l (substring line i2)))
20183 (concat (make-string i1 ?\ ) l)
20186 (defun org-remove-indentation (code &optional n)
20187 "Remove the maximum common indentation from the lines in CODE.
20188 N may optionally be the number of spaces to remove."
20191 (org-do-remove-indentation n)
20194 (defun org-do-remove-indentation (&optional n)
20195 "Remove the maximum common indentation from the buffer."
20196 (untabify (point-min) (point-max))
20197 (let ((min 10000) re)
20200 (goto-char (point-min))
20201 (while (re-search-forward "^ *[^ \n]" nil t)
20202 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
20203 (unless (or (= min 0) (= min 10000))
20204 (setq re (format "^ \\{%d\\}" min))
20205 (goto-char (point-min))
20206 (while (re-search-forward re nil t)
20211 (defun org-fill-template (template alist)
20212 "Find each %key of ALIST in TEMPLATE and replace it."
20213 (let ((case-fold-search nil)
20215 (setq alist (sort (copy-sequence alist)
20216 (lambda (a b) (< (length (car a)) (length (car b))))))
20217 (while (setq entry (pop alist))
20219 (replace-regexp-in-string
20220 (concat "%" (regexp-quote (car entry)))
20221 (or (cdr entry) "") template t t)))
20224 (defun org-base-buffer (buffer)
20225 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
20228 (or (buffer-base-buffer buffer)
20231 (defun org-trim (s)
20232 "Remove whitespace at beginning and end of string."
20233 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
20234 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
20237 (defun org-wrap (string &optional width lines)
20238 "Wrap string to either a number of lines, or a width in characters.
20239 If WIDTH is non-nil, the string is wrapped to that width, however many lines
20240 that costs. If there is a word longer than WIDTH, the text is actually
20241 wrapped to the length of that word.
20242 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
20243 many lines, whatever width that takes.
20244 The return value is a list of lines, without newlines at the end."
20245 (let* ((words (org-split-string string "[ \t\n]+"))
20246 (maxword (apply 'max (mapcar 'org-string-width words)))
20249 (org-do-wrap words (max maxword width)))
20252 (setq ll (org-do-wrap words maxword))
20253 (if (<= (length ll) lines)
20256 (while (> (length ll) lines)
20258 (setq ll (org-do-wrap words w)))
20260 (t (error "Cannot wrap this")))))
20262 (defun org-do-wrap (words width)
20263 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
20266 (setq line (pop words))
20267 (while (and words (< (+ (length line) (length (car words))) width))
20268 (setq line (concat line " " (pop words))))
20269 (setq lines (push line lines)))
20272 (defun org-split-string (string &optional separators)
20273 "Splits STRING into substrings at SEPARATORS.
20274 No empty strings are returned if there are matches at the beginning
20275 and end of string."
20276 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
20280 (while (and (string-match rexp string
20282 (= start (match-beginning 0))
20283 (< start (length string)))
20285 (< (match-beginning 0) (length string)))
20287 (or (eq (match-beginning 0) 0)
20288 (and (eq (match-beginning 0) (match-end 0))
20289 (eq (match-beginning 0) start))
20291 (cons (substring string start (match-beginning 0))
20293 (setq start (match-end 0)))
20294 (or (eq start (length string))
20296 (cons (substring string start)
20300 (defun org-quote-vert (s)
20301 "Replace \"|\" with \"\\vert\"."
20302 (while (string-match "|" s)
20303 (setq s (replace-match "\\vert" t t s)))
20306 (defun org-uuidgen-p (s)
20307 "Is S an ID created by UUIDGEN?"
20308 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
20310 (defun org-in-src-block-p (&optional inside)
20311 "Whether point is in a code source block.
20312 When INSIDE is non-nil, don't consider we are within a src block
20313 when point is at #+BEGIN_SRC or #+END_SRC."
20314 (let ((case-fold-search t) ov)
20315 (or (and (setq ov (overlays-at (point)))
20316 (memq 'org-block-background
20317 (overlay-properties (car ov))))
20321 (beginning-of-line)
20322 (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
20324 (defun org-context ()
20325 "Return a list of contexts of the current cursor position.
20326 If several contexts apply, all are returned.
20327 Each context entry is a list with a symbol naming the context, and
20328 two positions indicating start and end of the context. Possible
20331 :headline anywhere in a headline
20332 :headline-stars on the leading stars in a headline
20333 :todo-keyword on a TODO keyword (including DONE) in a headline
20334 :tags on the TAGS in a headline
20335 :priority on the priority cookie in a headline
20336 :item on the first line of a plain list item
20337 :item-bullet on the bullet/number of a plain list item
20338 :checkbox on the checkbox in a plain list item
20339 :table in an org-mode table
20340 :table-special on a special filed in a table
20341 :table-table in a table.el table
20342 :clocktable in a clocktable
20343 :src-block in a source block
20344 :link on a hyperlink
20345 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
20346 :target on a <<target>>
20347 :radio-target on a <<<radio-target>>>
20348 :latex-fragment on a LaTeX fragment
20349 :latex-preview on a LaTeX fragment with overlaid preview image
20351 This function expects the position to be visible because it uses font-lock
20352 faces as a help to recognize the following contexts: :table-special, :link,
20354 (let* ((f (get-text-property (point) 'face))
20355 (faces (if (listp f) f (list f)))
20356 (case-fold-search t)
20357 (p (point)) clist o)
20358 ;; First the large context
20360 ((org-at-heading-p t)
20361 (push (list :headline (point-at-bol) (point-at-eol)) clist)
20363 (beginning-of-line 1)
20364 (looking-at org-todo-line-tags-regexp))
20365 (push (org-point-in-group p 1 :headline-stars) clist)
20366 (push (org-point-in-group p 2 :todo-keyword) clist)
20367 (push (org-point-in-group p 4 :tags) clist))
20369 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
20370 (if (looking-at "\\[#[A-Z0-9]\\]")
20371 (push (org-point-in-group p 0 :priority) clist)))
20374 (push (org-point-in-group p 2 :item-bullet) clist)
20375 (push (list :item (point-at-bol)
20376 (save-excursion (org-end-of-item) (point)))
20378 (and (org-at-item-checkbox-p)
20379 (push (org-point-in-group p 0 :checkbox) clist)))
20382 (push (list :table (org-table-begin) (org-table-end)) clist)
20383 (if (memq 'org-formula faces)
20384 (push (list :table-special
20385 (previous-single-property-change p 'face)
20386 (next-single-property-change p 'face)) clist)))
20387 ((org-at-table-p 'any)
20388 (push (list :table-table) clist)))
20391 (let ((case-fold-search t))
20392 ;; New the "medium" contexts: clocktables, source blocks
20393 (cond ((org-in-clocktable-p)
20394 (push (list :clocktable
20395 (and (or (looking-at "#\\+BEGIN: clocktable")
20396 (search-backward "#+BEGIN: clocktable" nil t))
20397 (match-beginning 0))
20398 (and (re-search-forward "#\\+END:?" nil t)
20399 (match-end 0))) clist))
20400 ((org-in-src-block-p)
20401 (push (list :src-block
20402 (and (or (looking-at "#\\+BEGIN_SRC")
20403 (search-backward "#+BEGIN_SRC" nil t))
20404 (match-beginning 0))
20405 (and (search-forward "#+END_SRC" nil t)
20406 (match-beginning 0))) clist))))
20409 ;; Now the small context
20411 ((org-at-timestamp-p)
20412 (push (org-point-in-group p 0 :timestamp) clist))
20413 ((memq 'org-link faces)
20415 (previous-single-property-change p 'face)
20416 (next-single-property-change p 'face)) clist))
20417 ((memq 'org-special-keyword faces)
20418 (push (list :keyword
20419 (previous-single-property-change p 'face)
20420 (next-single-property-change p 'face)) clist))
20422 (push (org-point-in-group p 0 :target) clist)
20423 (goto-char (1- (match-beginning 0)))
20424 (if (looking-at org-radio-target-regexp)
20425 (push (org-point-in-group p 0 :radio-target) clist))
20427 ((setq o (car (delq nil
20430 (if (memq x org-latex-fragment-image-overlays) x))
20431 (overlays-at (point))))))
20432 (push (list :latex-fragment
20433 (overlay-start o) (overlay-end o)) clist)
20434 (push (list :latex-preview
20435 (overlay-start o) (overlay-end o)) clist))
20436 ((org-inside-LaTeX-fragment-p)
20437 ;; FIXME: positions wrong.
20438 (push (list :latex-fragment (point) (point)) clist)))
20440 (setq clist (nreverse (delq nil clist)))
20443 ;; FIXME: Compare with at-regexp-p Do we need both?
20444 (defun org-in-regexp (re &optional nlines visually)
20445 "Check if point is inside a match of regexp.
20446 Normally only the current line is checked, but you can include NLINES extra
20447 lines both before and after point into the search.
20448 If VISUALLY is set, require that the cursor is not after the match but
20449 really on, so that the block visually is on the match."
20451 (let ((pos (point))
20452 (eol (point-at-eol (+ 1 (or nlines 0))))
20453 (inc (if visually 1 0)))
20455 (beginning-of-line (- 1 (or nlines 0)))
20456 (while (re-search-forward re eol t)
20457 (if (and (<= (match-beginning 0) pos)
20458 (>= (+ inc (match-end 0)) pos))
20459 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
20461 (defun org-at-regexp-p (regexp)
20462 "Is point inside a match of REGEXP in the current line?"
20465 (let ((pos (point)) (end (point-at-eol)))
20466 (beginning-of-line 1)
20467 (while (re-search-forward regexp end t)
20468 (if (and (<= (match-beginning 0) pos)
20469 (>= (match-end 0) pos))
20473 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
20474 "Non-nil when point is between matches of START-RE and END-RE.
20476 Also return a non-nil value when point is on one of the matches.
20478 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
20479 buffer positions. Default values are the positions of headlines
20480 surrounding the point.
20482 The functions returns a cons cell whose car (resp. cdr) is the
20483 position before START-RE (resp. after END-RE)."
20485 (let ((pos (point))
20486 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
20487 (limit-down (or lim-down (save-excursion (outline-next-heading))))
20490 ;; Point is on a block when on START-RE or if START-RE can be
20491 ;; found before it...
20492 (and (or (org-at-regexp-p start-re)
20493 (re-search-backward start-re limit-up t))
20494 (setq beg (match-beginning 0))
20495 ;; ... and END-RE after it...
20496 (goto-char (match-end 0))
20497 (re-search-forward end-re limit-down t)
20498 (> (setq end (match-end 0)) pos)
20499 ;; ... without another START-RE in-between.
20500 (goto-char (match-beginning 0))
20501 (not (re-search-backward start-re (1+ beg) t))
20503 (cons beg end))))))
20505 (defun org-in-block-p (names)
20506 "Non-nil when point belongs to a block whose name belongs to NAMES.
20508 NAMES is a list of strings containing names of blocks.
20510 Return first block name matched, or nil. Beware that in case of
20511 nested blocks, the returned name may not belong to the closest
20515 (let ((case-fold-search t)
20516 (lim-up (save-excursion (outline-previous-heading)))
20517 (lim-down (save-excursion (outline-next-heading))))
20518 (mapc (lambda (name)
20519 (let ((n (regexp-quote name)))
20520 (when (org-between-regexps-p
20521 (concat "^[ \t]*#\\+begin_" n)
20522 (concat "^[ \t]*#\\+end_" n)
20528 (defun org-occur-in-agenda-files (regexp &optional nlines)
20529 "Call `multi-occur' with buffers for all agenda files."
20530 (interactive "sOrg-files matching: \np")
20531 (let* ((files (org-agenda-files))
20532 (tnames (mapcar 'file-truename files))
20533 (extra org-agenda-text-search-extra-files)
20535 (when (eq (car extra) 'agenda-archives)
20536 (setq extra (cdr extra))
20537 (setq files (org-add-archive-files files)))
20538 (while (setq f (pop extra))
20539 (unless (member (file-truename f) tnames)
20540 (add-to-list 'files f 'append)
20541 (add-to-list 'tnames (file-truename f) 'append)))
20543 (mapcar (lambda (x)
20544 (with-current-buffer
20545 (or (get-file-buffer x) (find-file-noselect x))
20551 (if (boundp 'occur-mode-find-occurrence-hook)
20553 (add-hook 'occur-mode-find-occurrence-hook
20555 (when (derived-mode-p 'org-mode)
20558 (defadvice occur-mode-goto-occurrence
20559 (after org-occur-reveal activate)
20560 (and (derived-mode-p 'org-mode) (org-reveal)))
20561 (defadvice occur-mode-goto-occurrence-other-window
20562 (after org-occur-reveal activate)
20563 (and (derived-mode-p 'org-mode) (org-reveal)))
20564 (defadvice occur-mode-display-occurrence
20565 (after org-occur-reveal activate)
20566 (when (derived-mode-p 'org-mode)
20567 (let ((pos (occur-mode-find-occurrence)))
20568 (with-current-buffer (marker-buffer pos)
20573 (defun org-occur-link-in-agenda-files ()
20574 "Create a link and search for it in the agendas.
20575 The link is not stored in `org-stored-links', it is just created
20576 for the search purpose."
20578 (let ((link (condition-case nil
20579 (org-store-link nil)
20580 (error "Unable to create a link to here"))))
20581 (org-occur-in-agenda-files (regexp-quote link))))
20583 (defun org-uniquify (list)
20584 "Remove duplicate elements from LIST."
20586 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
20589 (defun org-delete-all (elts list)
20590 "Remove all elements in ELTS from LIST."
20592 (setq list (delete (pop elts) list)))
20595 (defun org-count (cl-item cl-seq)
20596 "Count the number of occurrences of ITEM in SEQ.
20597 Taken from `count' in cl-seq.el with all keyword arguments removed."
20598 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
20599 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
20600 (while (< cl-start cl-end)
20601 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
20602 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
20603 (setq cl-start (1+ cl-start)))
20606 (defun org-remove-if (predicate seq)
20607 "Remove everything from SEQ that fulfills PREDICATE."
20611 (if (not (funcall predicate e)) (push e res)))
20614 (defun org-remove-if-not (predicate seq)
20615 "Remove everything from SEQ that does not fulfill PREDICATE."
20619 (if (funcall predicate e) (push e res)))
20622 (defun org-reduce (cl-func cl-seq &rest cl-keys)
20623 "Reduce two-argument FUNCTION across SEQ.
20624 Taken from `reduce' in cl-seq.el with all keyword arguments but
20625 \":initial-value\" removed."
20626 (let ((cl-accum (cond ((memq :initial-value cl-keys)
20627 (cadr (memq :initial-value cl-keys)))
20628 (cl-seq (pop cl-seq))
20629 (t (funcall cl-func)))))
20631 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
20634 (defun org-back-over-empty-lines ()
20635 "Move backwards over whitespace, to the beginning of the first empty line.
20636 Returns the number of empty lines passed."
20637 (let ((pos (point)))
20638 (if (cdr (assoc 'heading org-blank-before-new-entry))
20639 (skip-chars-backward " \t\n\r")
20641 (forward-line -1)))
20642 (beginning-of-line 2)
20643 (goto-char (min (point) pos))
20644 (count-lines (point) pos)))
20646 (defun org-skip-whitespace ()
20647 (skip-chars-forward " \t\n\r"))
20649 (defun org-point-in-group (point group &optional context)
20650 "Check if POINT is in match-group GROUP.
20651 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
20652 match. If the match group does not exist or point is not inside it,
20654 (and (match-beginning group)
20655 (>= point (match-beginning group))
20656 (<= point (match-end group))
20658 (list context (match-beginning group) (match-end group))
20661 (defun org-switch-to-buffer-other-window (&rest args)
20662 "Switch to buffer in a second window on the current frame.
20663 In particular, do not allow pop-up frames.
20664 Returns the newly created buffer."
20666 (apply 'switch-to-buffer-other-window args)))
20668 (defun org-combine-plists (&rest plists)
20669 "Create a single property list from all plists in PLISTS.
20670 The process starts by copying the first list, and then setting properties
20671 from the other lists. Settings in the last list are the most significant
20672 ones and overrule settings in the other lists."
20673 (let ((rtn (copy-sequence (pop plists)))
20676 (setq ls (pop plists))
20678 (setq p (pop ls) v (pop ls))
20679 (setq rtn (plist-put rtn p v))))
20682 (defun org-replace-escapes (string table)
20683 "Replace %-escapes in STRING with values in TABLE.
20684 TABLE is an association list with keys like \"%a\" and string values.
20685 The sequences in STRING may contain normal field width and padding information,
20686 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
20687 so values can contain further %-escapes if they are define later in TABLE."
20688 (let ((tbl (copy-alist table))
20689 (case-fold-search nil)
20692 (while (setq e (pop tbl))
20693 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
20694 (when (and (cdr e) (string-match re (cdr e)))
20695 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
20697 (add-text-properties 0 3 (list 'sref sref) safe)
20698 (setcdr e (replace-match safe t t (cdr e)))))
20699 (while (string-match re string)
20700 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
20702 (setq string (replace-match rpl t t string))))
20703 (while (setq pchg (next-property-change pchg string))
20704 (let ((sref (get-text-property pchg 'sref string)))
20705 (when (and sref (string-match "SREF" string pchg))
20706 (setq string (replace-match sref t t string)))))
20709 (defun org-sublist (list start end)
20710 "Return a section of LIST, from START to END.
20711 Counting starts at 1."
20712 (let (rtn (c start))
20713 (setq list (nthcdr (1- start) list))
20714 (while (and list (<= c end))
20715 (push (pop list) rtn)
20719 (defun org-find-base-buffer-visiting (file)
20720 "Like `find-buffer-visiting' but always return the base buffer and
20721 not an indirect buffer."
20722 (let ((buf (or (get-file-buffer file)
20723 (find-buffer-visiting file))))
20725 (or (buffer-base-buffer buf) buf)
20728 (defun org-image-file-name-regexp (&optional extensions)
20729 "Return regexp matching the file names of images.
20730 If EXTENSIONS is given, only match these."
20731 (if (and (not extensions) (fboundp 'image-file-name-regexp))
20732 (image-file-name-regexp)
20733 (let ((image-file-name-extensions
20735 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
20736 "xbm" "xpm" "pbm" "pgm" "ppm"))))
20738 (regexp-opt (nconc (mapcar 'upcase
20739 image-file-name-extensions)
20740 image-file-name-extensions)
20744 (defun org-file-image-p (file &optional extensions)
20745 "Return non-nil if FILE is an image."
20747 (string-match (org-image-file-name-regexp extensions) file)))
20749 (defun org-get-cursor-date ()
20750 "Return the date at cursor in as a time.
20751 This works in the calendar and in the agenda, anywhere else it just
20752 returns the current time."
20753 (let (date day defd)
20755 ((eq major-mode 'calendar-mode)
20756 (setq date (calendar-cursor-to-date)
20757 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
20758 ((eq major-mode 'org-agenda-mode)
20759 (setq day (get-text-property (point) 'day))
20761 (setq date (calendar-gregorian-from-absolute day)
20762 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
20764 (or defd (current-time))))
20766 (defun org-mark-subtree (&optional up)
20767 "Mark the current subtree.
20768 This puts point at the start of the current subtree, and mark at
20769 the end. If a numeric prefix UP is given, move up into the
20770 hierarchy of headlines by UP levels before marking the subtree."
20772 (org-with-limited-levels
20773 (cond ((org-at-heading-p) (beginning-of-line))
20774 ((org-before-first-heading-p) (error "Not in a subtree"))
20775 (t (outline-previous-visible-heading 1))))
20776 (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
20777 (if (org-called-interactively-p 'any)
20778 (call-interactively 'org-mark-element)
20779 (org-mark-element)))
20783 (defun org-indent-line ()
20784 "Indent line depending on context."
20786 (let* ((pos (point))
20787 (itemp (org-at-item-p))
20788 (case-fold-search t)
20789 (org-drawer-regexp (or org-drawer-regexp "\000"))
20790 (inline-task-p (and (featurep 'org-inlinetask)
20791 (org-inlinetask-in-task-p)))
20792 (inline-re (and inline-task-p
20793 (org-inlinetask-outline-regexp)))
20795 (if (and orgstruct-is-++ (eq pos (point)))
20796 (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
20797 (indent-according-to-mode))
20798 (beginning-of-line 1)
20801 ((looking-at org-outline-regexp) (setq column 0))
20803 ((looking-at "#\\+include:") (setq column 0))
20804 ;; Footnote definition
20805 ((looking-at org-footnote-definition-re) (setq column 0))
20806 ;; Literal examples
20807 ((looking-at "[ \t]*:\\( \\|$\\)")
20808 (setq column (org-get-indentation))) ; do nothing
20810 ((ignore-errors (goto-char (org-in-item-p)))
20811 (setq column (if itemp
20812 (org-get-indentation)
20813 (org-list-item-body-column (point))))
20816 ((and (looking-at "[ \t]*:END:")
20817 (save-excursion (re-search-backward org-drawer-regexp nil t)))
20819 (goto-char (1- (match-beginning 1)))
20820 (setq column (current-column))))
20822 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
20824 (re-search-backward
20825 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
20826 (setq column (org-get-indentation (match-string 0))))
20827 ((and (not (looking-at "[ \t]*#\\+begin_"))
20828 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
20830 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
20832 (cond ((equal (downcase (match-string 1)) "src")
20833 ;; src blocks: let `org-edit-src-exit' handle them
20834 (org-get-indentation))
20835 ((equal (downcase (match-string 1)) "example")
20836 (max (org-get-indentation)
20837 (org-get-indentation (match-string 0))))
20839 (org-get-indentation (match-string 0))))))
20840 ;; This line has nothing special, look at the previous relevant
20841 ;; line to compute indentation
20843 (beginning-of-line 0)
20844 (while (and (not (bobp))
20845 (not (looking-at org-table-line-regexp))
20846 (not (looking-at org-drawer-regexp))
20847 ;; When point started in an inline task, do not move
20848 ;; above task starting line.
20849 (not (and inline-task-p (looking-at inline-re)))
20850 ;; Skip drawers, blocks, empty lines, verbatim,
20851 ;; comments, tables, footnotes definitions, lists,
20853 (or (and (looking-at "[ \t]*:END:")
20854 (re-search-backward org-drawer-regexp nil t))
20855 (and (looking-at "[ \t]*#\\+end_")
20856 (re-search-backward "[ \t]*#\\+begin_"nil t))
20857 (looking-at "[ \t]*[\n:#|]")
20858 (looking-at org-footnote-definition-re)
20859 (and (ignore-errors (goto-char (org-in-item-p)))
20861 (org-list-get-top-point (org-list-struct))))
20862 (and (not inline-task-p)
20863 (featurep 'org-inlinetask)
20864 (org-inlinetask-in-task-p)
20865 (or (org-inlinetask-goto-beginning) t))))
20866 (beginning-of-line 0))
20868 ;; There was an heading above.
20869 ((looking-at "\\*+[ \t]+")
20870 (if (not org-adapt-indentation)
20872 (goto-char (match-end 0))
20873 (setq column (current-column))))
20874 ;; A drawer had started and is unfinished
20875 ((looking-at org-drawer-regexp)
20876 (goto-char (1- (match-beginning 1)))
20877 (setq column (current-column)))
20878 ;; Else, nothing noticeable found: get indentation and go on.
20879 (t (setq column (org-get-indentation))))))
20880 ;; Now apply indentation and move cursor accordingly
20882 (if (<= (current-column) (current-indentation))
20883 (org-indent-line-to column)
20884 (save-excursion (org-indent-line-to column)))
20885 ;; Special polishing for properties, see `org-property-format'
20886 (setq column (current-column))
20887 (beginning-of-line 1)
20889 "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
20890 (replace-match (concat (match-string 1)
20891 (format org-property-format
20892 (match-string 2) (match-string 3)))
20894 (org-move-to-column column))))
20896 (defun org-indent-drawer ()
20897 "Indent the drawer at point."
20900 (e (and (save-excursion (re-search-forward ":END:" nil t))
20905 (when (overlays-at (point))
20906 (member 'invisible (overlay-properties
20907 (car (overlays-at (point)))))))))
20908 (when folded (org-cycle))
20909 (indent-for-tab-command)
20910 (while (and (move-beginning-of-line 2) (< (point) e))
20911 (indent-for-tab-command))
20913 (when folded (org-cycle)))
20914 (message "Drawer at point indented"))
20916 (defun org-indent-block ()
20917 "Indent the block at point."
20920 (case-fold-search t)
20921 (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
20926 (when (overlays-at (point))
20927 (member 'invisible (overlay-properties
20928 (car (overlays-at (point)))))))))
20929 (when folded (org-cycle))
20930 (indent-for-tab-command)
20931 (while (and (move-beginning-of-line 2) (< (point) e))
20932 (indent-for-tab-command))
20934 (when folded (org-cycle)))
20935 (message "Block at point indented"))
20937 (defun org-indent-region (start end)
20941 (let ((line-end (org-current-line end)))
20943 (while (< (org-current-line) line-end)
20944 (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
20945 (t (call-interactively 'org-indent-line)))
20946 (move-beginning-of-line 2)))))
20951 ;; We use our own fill-paragraph and auto-fill functions.
20953 ;; `org-fill-paragraph' relies on adaptive filling and context
20954 ;; checking. Appropriate `fill-prefix' is computed with
20955 ;; `org-adaptive-fill-function'.
20957 ;; `org-auto-fill-function' takes care of auto-filling. It calls
20958 ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
20959 ;; `org-adaptive-fill-function' value. Internally,
20960 ;; `org-comment-line-break-function' breaks the line.
20962 ;; `org-setup-filling' installs filling and auto-filling related
20963 ;; variables during `org-mode' initialization.
20965 (defun org-setup-filling ()
20967 ;; Prevent auto-fill from inserting unwanted new items.
20968 (when (boundp 'fill-nobreak-predicate)
20970 'fill-nobreak-predicate
20972 (append fill-nobreak-predicate
20973 '(org-fill-paragraph-separate-nobreak-p
20974 org-fill-line-break-nobreak-p)))))
20975 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
20976 (org-set-local 'auto-fill-inhibit-regexp nil)
20977 (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
20978 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
20979 (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
20981 (defvar org-element-paragraph-separate) ; org-element.el
20982 (defun org-fill-paragraph-separate-nobreak-p ()
20983 "Non-nil when a line break at point would insert a new item."
20984 (looking-at (substring org-element-paragraph-separate 1)))
20986 (defun org-fill-line-break-nobreak-p ()
20987 "Non-nil when a line break at point would create an Org line break."
20989 (skip-chars-backward "[ \t]")
20990 (skip-chars-backward "\\\\")
20991 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
20993 (declare-function message-in-body-p "message" ())
20994 (defvar org-element--affiliated-re) ; From org-element.el
20995 (defvar orgtbl-line-start-regexp) ; From org-table.el
20996 (defun org-adaptive-fill-function ()
20997 "Compute a fill prefix for the current line.
20998 Return fill prefix, as a string, or nil if current line isn't
20999 meant to be filled."
21002 (when (derived-mode-p 'message-mode)
21004 (beginning-of-line)
21005 (cond ((or (not (message-in-body-p))
21006 (looking-at orgtbl-line-start-regexp))
21008 ((looking-at message-cite-prefix-regexp)
21009 (throw 'exit (match-string-no-properties 0)))
21010 ((looking-at org-outline-regexp)
21011 (throw 'exit (make-string (length (match-string 0)) ? ))))))
21012 (org-with-wide-buffer
21013 (let* ((p (line-beginning-position))
21014 (element (save-excursion (beginning-of-line) (org-element-at-point)))
21015 (type (org-element-type element))
21018 (goto-char (org-element-property :begin element))
21019 (while (looking-at org-element--affiliated-re) (forward-line))
21021 (unless (< p post-affiliated)
21023 (comment (looking-at "[ \t]*# ?") (match-string 0))
21024 (footnote-definition "")
21026 (make-string (org-list-item-body-column post-affiliated) ? ))
21028 ;; Fill prefix is usually the same as the current line,
21029 ;; except if the paragraph is at the beginning of an item.
21030 (let ((parent (org-element-property :parent element)))
21031 (cond ((eq (org-element-type parent) 'item)
21032 (make-string (org-list-item-body-column
21033 (org-element-property :begin parent))
21035 ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
21039 ;; Only fill contents if P is within block boundaries.
21040 (let* ((cbeg (save-excursion (goto-char post-affiliated)
21043 (cend (save-excursion
21044 (goto-char (org-element-property :end element))
21045 (skip-chars-backward " \r\t\n")
21046 (line-beginning-position))))
21047 (when (and (>= p cbeg) (< p cend))
21048 (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
21052 (declare-function message-goto-body "message" ())
21053 (defvar message-cite-prefix-regexp) ; From message.el
21054 (defvar org-element-all-objects) ; From org-element.el
21055 (defun org-fill-paragraph (&optional justify)
21056 "Fill element at point, when applicable.
21058 This function only applies to comment blocks, comments, example
21059 blocks and paragraphs. Also, as a special case, re-align table
21060 when point is at one.
21062 If JUSTIFY is non-nil (interactively, with prefix argument),
21063 justify as well. If `sentence-end-double-space' is non-nil, then
21064 period followed by one space does not end a sentence, so don't
21065 break a line there. The variable `fill-column' controls the
21068 For convenience, when point is at a plain list, an item or
21069 a footnote definition, try to fill the first paragraph within."
21071 (if (and (derived-mode-p 'message-mode)
21072 (or (not (message-in-body-p))
21073 (save-excursion (move-beginning-of-line 1)
21074 (looking-at message-cite-prefix-regexp))))
21075 ;; First ensure filling is correct in message-mode.
21076 (let ((fill-paragraph-function
21077 (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
21078 (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
21079 (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
21080 (paragraph-separate
21081 (cadadr (assoc 'paragraph-separate org-fb-vars))))
21082 (fill-paragraph nil))
21084 ;; Move to end of line in order to get the first paragraph
21085 ;; within a plain list or a footnote definition.
21087 (let ((element (org-element-at-point)))
21088 ;; First check if point is in a blank line at the beginning of
21089 ;; the buffer. In that case, ignore filling.
21090 (if (< (point) (org-element-property :begin element)) t
21091 (case (org-element-type element)
21092 ;; Use major mode filling function is src blocks.
21093 (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
21094 ;; Align Org tables, leave table.el tables as-is.
21095 (table-row (org-table-align) t)
21097 (when (eq (org-element-property :type element) 'org)
21101 ;; Paragraphs may contain `line-break' type objects.
21102 (let ((beg (max (point-min)
21103 (org-element-property :contents-begin element)))
21104 (end (min (point-max)
21105 (org-element-property :contents-end element))))
21106 ;; Do nothing if point is at an affiliated keyword.
21107 (if (< (point) beg) t
21108 (when (derived-mode-p 'message-mode)
21109 ;; In `message-mode', do not fill following
21110 ;; citation in current paragraph nor text before
21112 (let ((body-start (save-excursion (message-goto-body))))
21113 (when body-start (setq beg (max body-start beg))))
21114 (when (save-excursion
21116 (concat "^" message-cite-prefix-regexp) end t))
21117 (setq end (match-beginning 0))))
21118 ;; Fill paragraph, taking line breaks into
21119 ;; consideration. For that, slice the paragraph
21120 ;; using line breaks as separators, and fill the
21121 ;; parts in reverse order to avoid messing with
21127 (fill-region-as-paragraph pos (point) justify)
21129 ;; Find the list of ending positions for line
21130 ;; breaks in the current paragraph. Add paragraph
21131 ;; beginning to include first slice.
21136 (org-element--parse-objects
21137 beg end nil org-element-all-objects)
21139 (lambda (lb) (org-element-property :end lb)))))))
21141 ;; Contents of `comment-block' type elements should be
21142 ;; filled as plain text, but only if point is within block
21145 (let* ((case-fold-search t)
21146 (beg (save-excursion
21147 (goto-char (org-element-property :begin element))
21148 (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
21151 (end (save-excursion
21152 (goto-char (org-element-property :end element))
21153 (re-search-backward "^[ \t]*#\\+end_comment" nil t)
21154 (line-beginning-position))))
21155 (when (and (>= (point) beg) (< (point) end))
21156 (fill-region-as-paragraph
21159 (re-search-backward "^[ \t]*$" beg 'move)
21160 (line-beginning-position))
21162 (beginning-of-line)
21163 (re-search-forward "^[ \t]*$" end 'move)
21164 (line-beginning-position))
21168 (comment (fill-comment-paragraph justify))
21169 ;; Ignore every other element.
21170 (otherwise t)))))))
21172 (defun org-auto-fill-function ()
21173 "Auto-fill function."
21174 ;; Check if auto-filling is meaningful.
21175 (let ((fc (current-fill-column)))
21176 (when (and fc (> (current-column) fc))
21177 (let* ((fill-prefix (org-adaptive-fill-function))
21178 ;; Enforce empty fill prefix, if required. Otherwise, it
21179 ;; will be computed again.
21180 (adaptive-fill-mode (not (equal fill-prefix ""))))
21181 (when fill-prefix (do-auto-fill))))))
21183 (defun org-comment-line-break-function (&optional soft)
21184 "Break line at point and indent, continuing comment if within one.
21185 The inserted newline is marked hard if variable
21186 `use-hard-newlines' is true, unless optional argument SOFT is
21188 (if soft (insert-and-inherit ?\n) (newline 1))
21189 (save-excursion (forward-char -1) (delete-horizontal-space))
21190 (delete-horizontal-space)
21191 (indent-to-left-margin)
21192 (insert-before-markers-and-inherit fill-prefix))
21197 ;; Org comments syntax is quite complex. It requires the entire line
21198 ;; to be just a comment. Also, even with the right syntax at the
21199 ;; beginning of line, some some elements (i.e. verse-block or
21200 ;; example-block) don't accept comments. Usual Emacs comment commands
21201 ;; cannot cope with those requirements. Therefore, Org replaces them.
21203 ;; Org still relies on `comment-dwim', but cannot trust
21204 ;; `comment-only-p'. So, `comment-region-function' and
21205 ;; `uncomment-region-function' both point
21206 ;; to`org-comment-or-uncomment-region'. Eventually,
21207 ;; `org-insert-comment' takes care of insertion of comments at the
21208 ;; beginning of line.
21210 ;; `org-setup-comments-handling' install comments related variables
21211 ;; during `org-mode' initialization.
21213 (defun org-setup-comments-handling ()
21215 (org-set-local 'comment-use-syntax nil)
21216 (org-set-local 'comment-start "# ")
21217 (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
21218 (org-set-local 'comment-insert-comment-function 'org-insert-comment)
21219 (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
21220 (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
21222 (defun org-insert-comment ()
21223 "Insert an empty comment above current line.
21224 If the line is empty, insert comment at its beginning."
21225 (beginning-of-line)
21226 (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
21230 (defvar comment-empty-lines) ; From newcomment.el.
21231 (defun org-comment-or-uncomment-region (beg end &rest ignore)
21232 "Comment or uncomment each non-blank line in the region.
21233 Uncomment each non-blank line between BEG and END if it only
21234 contains commented lines. Otherwise, comment them."
21237 (narrow-to-region (save-excursion (goto-char beg)
21238 (skip-chars-forward " \r\t\n" end)
21239 (line-beginning-position))
21240 (save-excursion (goto-char end)
21241 (skip-chars-backward " \r\t\n" beg)
21242 (line-end-position)))
21244 ;; UNCOMMENTP is non-nil when every non blank line between
21245 ;; BEG and END is a comment.
21247 (goto-char (point-min))
21248 (while (and (not (eobp))
21249 (let ((element (org-element-at-point)))
21250 (and (eq (org-element-type element) 'comment)
21251 (goto-char (min (point-max)
21252 (org-element-property
21253 :end element)))))))
21256 ;; Only blank lines and comments in region: uncomment it.
21258 (goto-char (point-min))
21259 (while (not (eobp))
21260 (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
21261 (replace-match "" nil nil nil 1))
21263 ;; Comment each line in region.
21264 (let ((min-indent (point-max)))
21265 ;; First find the minimum indentation across all lines.
21267 (goto-char (point-min))
21268 (while (and (not (eobp)) (not (zerop min-indent)))
21269 (unless (looking-at "[ \t]*$")
21270 (setq min-indent (min min-indent (current-indentation))))
21272 ;; Then loop over all lines.
21274 (goto-char (point-min))
21275 (while (not (eobp))
21276 (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
21277 (org-move-to-column min-indent t)
21278 (insert comment-start))
21279 (forward-line))))))))
21284 (defun org-toggle-fixed-width-section (arg)
21285 "Toggle the fixed-width export.
21286 If there is no active region, the QUOTE keyword at the current headline is
21287 inserted or removed. When present, it causes the text between this headline
21288 and the next to be exported as fixed-width text, and unmodified.
21289 If there is an active region, this command adds or removes a colon as the
21290 first character of this line. If the first character of a line is a colon,
21291 this line is also exported in fixed-width font."
21294 (regionp (org-region-active-p))
21295 (beg (if regionp (region-beginning) (point)))
21296 (end (if regionp (region-end)))
21297 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
21298 (case-fold-search nil)
21299 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
21304 (setq cc (current-column))
21305 (beginning-of-line 1)
21306 (setq off (looking-at re))
21307 (while (> nlines 0)
21308 (setq nlines (1- nlines))
21309 (beginning-of-line 1)
21312 (org-move-to-column cc t)
21315 ((and off (looking-at re))
21316 (replace-match "" t t nil 1))
21317 ((not off) (org-move-to-column cc t) (insert ": ")))
21320 (org-back-to-heading)
21322 ((looking-at (format org-heading-keyword-regexp-format
21324 (goto-char (match-end 1))
21325 (looking-at (concat " +" org-quote-string))
21326 (replace-match "" t t)
21327 (when (eolp) (insert " ")))
21328 ((looking-at org-outline-regexp)
21329 (goto-char (match-end 0))
21330 (insert org-quote-string " ")))))))
21332 (defun org-reftex-citation ()
21333 "Use reftex-citation to insert a citation into the buffer.
21334 This looks for a line like
21336 #+BIBLIOGRAPHY: foo plain option:-d
21338 and derives from it that foo.bib is the bibliography file relevant
21339 for this document. It then installs the necessary environment for RefTeX
21340 to work in this buffer and calls `reftex-citation' to insert a citation
21343 Export of such citations to both LaTeX and HTML is handled by the contributed
21344 package org-exp-bibtex by Taru Karttunen."
21346 (let ((reftex-docstruct-symbol 'rds)
21347 (reftex-cite-format "\\cite{%l}")
21352 (let ((case-fold-search t)
21353 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
21354 (if (not (save-excursion
21355 (or (re-search-forward re nil t)
21356 (re-search-backward re nil t))))
21357 (error "No bibliography defined in file")
21358 (setq bib (concat (match-string 1) ".bib")
21359 rds (list (list 'bib bib)))))))
21360 (call-interactively 'reftex-citation)))
21362 ;;;; Functions extending outline functionality
21364 (defun org-beginning-of-line (&optional arg)
21365 "Go to the beginning of the current line. If that is invisible, continue
21366 to a visible line beginning. This makes the function of C-a more intuitive.
21367 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
21368 first attempt, and only move to after the tags when the cursor is already
21369 beyond the end of the headline."
21371 (let ((pos (point))
21372 (special (if (consp org-special-ctrl-a/e)
21373 (car org-special-ctrl-a/e)
21374 org-special-ctrl-a/e))
21376 (if (org-bound-and-true-p visual-line-mode)
21377 (beginning-of-visual-line 1)
21378 (beginning-of-line 1))
21379 (if (and arg (fboundp 'move-beginning-of-line))
21380 (call-interactively 'move-beginning-of-line)
21384 (if (org-truely-invisible-p)
21385 (while (and (not (bobp)) (org-truely-invisible-p))
21387 (beginning-of-line 1))
21388 (forward-char 1))))
21391 ((and (looking-at org-complex-heading-regexp)
21392 (= (char-after (match-end 1)) ?\ ))
21393 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
21397 (cond ((> pos refpos) refpos)
21398 ((= pos (point)) refpos)
21400 (cond ((> pos (point)) (point))
21401 ((not (eq last-command this-command)) (point))
21404 ;; Being at an item and not looking at an the item means point
21405 ;; was previously moved to beginning of a visual line, which
21406 ;; doesn't contain the item. Therefore, do nothing special,
21408 (when (looking-at org-list-full-item-re)
21409 ;; Set special position at first white space character after
21410 ;; bullet, and check-box, if any.
21411 (let ((after-bullet
21412 (let ((box (match-end 3)))
21413 (if (not box) (match-end 1)
21414 (let ((after (char-after box)))
21415 (if (and after (= after ? )) (1+ box) box))))))
21416 ;; Special case: Move point to special position when
21417 ;; currently after it or at beginning of line.
21419 (when (or (> pos after-bullet) (= (point) pos))
21420 (goto-char after-bullet))
21421 ;; Reversed case: Move point to special position when
21422 ;; point was already at beginning of line and command is
21424 (when (and (= (point) pos) (eq last-command this-command))
21425 (goto-char after-bullet))))))))
21427 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21429 (defun org-end-of-line (&optional arg)
21430 "Go to the end of the line.
21431 If this is a headline, and `org-special-ctrl-a/e' is set, ignore
21432 tags on the first attempt, and only move to after the tags when
21433 the cursor is already beyond the end of the headline."
21435 (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
21436 org-special-ctrl-a/e))
21437 (move-fun (cond ((org-bound-and-true-p visual-line-mode)
21438 'end-of-visual-line)
21439 ((fboundp 'move-end-of-line) 'move-end-of-line)
21440 (t 'end-of-line))))
21441 (if (or (not special) arg) (call-interactively move-fun)
21442 (let* ((element (save-excursion (beginning-of-line)
21443 (org-element-at-point)))
21444 (type (org-element-type element)))
21446 ((memq type '(headline inlinetask))
21447 (let ((pos (point)))
21448 (beginning-of-line 1)
21449 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
21451 (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
21452 (goto-char (match-beginning 1))
21453 (goto-char (match-end 0)))
21454 (if (or (< pos (match-end 0))
21455 (not (eq this-command last-command)))
21456 (goto-char (match-end 0))
21457 (goto-char (match-beginning 1))))
21458 (call-interactively move-fun))))
21459 ((org-element-property :hiddenp element)
21460 ;; If element is hidden, `move-end-of-line' would put point
21461 ;; after it. Use `end-of-line' to stay on current line.
21462 (call-interactively 'end-of-line))
21463 (t (call-interactively move-fun)))))
21464 (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21466 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
21467 (define-key org-mode-map "\C-e" 'org-end-of-line)
21469 (defun org-backward-sentence (&optional arg)
21470 "Go to beginning of sentence, or beginning of table field.
21471 This will call `backward-sentence' or `org-table-beginning-of-field',
21472 depending on context."
21475 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
21476 (t (call-interactively 'backward-sentence))))
21478 (defun org-forward-sentence (&optional arg)
21479 "Go to end of sentence, or end of table field.
21480 This will call `forward-sentence' or `org-table-end-of-field',
21481 depending on context."
21484 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
21485 (t (call-interactively 'forward-sentence))))
21487 (define-key org-mode-map "\M-a" 'org-backward-sentence)
21488 (define-key org-mode-map "\M-e" 'org-forward-sentence)
21490 (defun org-kill-line (&optional arg)
21491 "Kill line, to tags or end of line."
21494 ((or (not org-special-ctrl-k)
21496 (not (org-at-heading-p)))
21497 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
21498 org-ctrl-k-protect-subtree)
21499 (if (or (eq org-ctrl-k-protect-subtree 'error)
21500 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
21501 (error "C-k aborted - would kill hidden subtree")))
21502 (call-interactively
21503 (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
21504 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
21505 (kill-region (point) (match-beginning 1))
21506 (org-set-tags nil t))
21507 (t (kill-region (point) (point-at-eol)))))
21509 (define-key org-mode-map "\C-k" 'org-kill-line)
21511 (defun org-yank (&optional arg)
21512 "Yank. If the kill is a subtree, treat it specially.
21513 This command will look at the current kill and check if is a single
21514 subtree, or a series of subtrees[1]. If it passes the test, and if the
21515 cursor is at the beginning of a line or after the stars of a currently
21516 empty headline, then the yank is handled specially. How exactly depends
21517 on the value of the following variables, both set by default.
21519 org-yank-folded-subtrees
21520 When set, the subtree(s) will be folded after insertion, but only
21521 if doing so would now swallow text after the yanked text.
21523 org-yank-adjusted-subtrees
21524 When set, the subtree will be promoted or demoted in order to
21525 fit into the local outline tree structure, which means that the level
21526 will be adjusted so that it becomes the smaller one of the two
21527 *visible* surrounding headings.
21529 Any prefix to this command will cause `yank' to be called directly with
21530 no special treatment. In particular, a simple \\[universal-argument] prefix \
21532 plainly yank the text as it is.
21534 \[1] The test checks if the first non-white line is a heading
21535 and if there are no other headings with fewer stars."
21537 (org-yank-generic 'yank arg))
21539 (defun org-yank-generic (command arg)
21540 "Perform some yank-like command.
21542 This function implements the behavior described in the `org-yank'
21543 documentation. However, it has been generalized to work for any
21544 interactive command with similar behavior."
21546 ;; pretend to be command COMMAND
21547 (setq this-command command)
21550 (call-interactively command)
21552 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
21553 (and (org-kill-is-subtree-p)
21555 (and (looking-at "[ \t]*$")
21558 (buffer-substring (point-at-bol) (point)))))))
21561 ((and subtreep org-yank-folded-subtrees)
21562 (let ((beg (point))
21564 (if (and subtreep org-yank-adjusted-subtrees)
21565 (org-paste-subtree nil nil 'for-yank)
21566 (call-interactively command))
21570 (when (and (bolp) subtreep
21571 (not (setq swallowp
21572 (org-yank-folding-would-swallow-text beg end))))
21573 (org-with-limited-levels
21574 (or (looking-at org-outline-regexp)
21575 (re-search-forward org-outline-regexp-bol end t))
21576 (while (and (< (point) end) (looking-at org-outline-regexp))
21578 (org-cycle-show-empty-lines 'folded)
21579 (condition-case nil
21580 (outline-forward-same-level 1)
21581 (error (goto-char end))))))
21584 "Inserted text not folded because that would swallow text"))
21587 (skip-chars-forward " \t\n\r")
21588 (beginning-of-line 1)
21589 (push-mark beg 'nomsg)))
21590 ((and subtreep org-yank-adjusted-subtrees)
21591 (let ((beg (point-at-bol)))
21592 (org-paste-subtree nil nil 'for-yank)
21593 (push-mark beg 'nomsg)))
21595 (call-interactively command))))))
21597 (defun org-yank-folding-would-swallow-text (beg end)
21598 "Would hide-subtree at BEG swallow any text after END?"
21600 (org-with-limited-levels
21603 (when (or (looking-at org-outline-regexp)
21604 (re-search-forward org-outline-regexp-bol end t))
21605 (setq level (org-outline-level)))
21607 (skip-chars-forward " \t\r\n\v\f")
21609 (and (bolp) (looking-at org-outline-regexp)
21610 (<= (org-outline-level) level)))
21611 nil ; Nothing would be swallowed
21612 t))))) ; something would swallow
21614 (define-key org-mode-map "\C-y" 'org-yank)
21616 (defun org-truely-invisible-p ()
21617 "Check if point is at a character currently not visible.
21618 This version does not only check the character property, but also
21620 ;; Early versions of noutline don't have `outline-invisible-p'.
21621 (if (org-bound-and-true-p visible-mode)
21623 (outline-invisible-p)))
21625 (defun org-invisible-p2 ()
21626 "Check if point is at a character currently not visible."
21628 (if (and (eolp) (not (bobp))) (backward-char 1))
21629 ;; Early versions of noutline don't have `outline-invisible-p'.
21630 (outline-invisible-p)))
21632 (defun org-back-to-heading (&optional invisible-ok)
21633 "Call `outline-back-to-heading', but provide a better error message."
21634 (condition-case nil
21635 (outline-back-to-heading invisible-ok)
21636 (error (error "Before first headline at position %d in buffer %s"
21637 (point) (current-buffer)))))
21639 (defun org-before-first-heading-p ()
21640 "Before first heading?"
21643 (null (re-search-backward org-outline-regexp-bol nil t))))
21645 (defun org-at-heading-p (&optional ignored)
21646 (outline-on-heading-p t))
21647 ;; Compatibility alias with Org versions < 7.8.03
21648 (defalias 'org-on-heading-p 'org-at-heading-p)
21650 (defun org-at-comment-p nil
21651 "Is cursor in a line starting with a # character?"
21653 (beginning-of-line)
21654 (looking-at "^#")))
21656 (defun org-at-drawer-p nil
21657 "Is cursor at a drawer keyword?"
21659 (move-beginning-of-line 1)
21660 (looking-at org-drawer-regexp)))
21662 (defun org-at-block-p nil
21663 "Is cursor at a block keyword?"
21665 (move-beginning-of-line 1)
21666 (looking-at org-block-regexp)))
21668 (defun org-point-at-end-of-empty-headline ()
21669 "If point is at the end of an empty headline, return t, else nil.
21670 If the heading only contains a TODO keyword, it is still still considered
21672 (and (looking-at "[ \t]*$")
21673 (when org-todo-line-regexp
21675 (beginning-of-line 1)
21676 (let ((case-fold-search nil))
21677 (looking-at org-todo-line-regexp)
21678 (string= (match-string 3) ""))))))
21680 (defun org-at-heading-or-item-p ()
21681 (or (org-at-heading-p) (org-at-item-p)))
21683 (defun org-at-target-p ()
21684 (or (org-in-regexp org-radio-target-regexp)
21685 (org-in-regexp org-target-regexp)))
21686 ;; Compatibility alias with Org versions < 7.8.03
21687 (defalias 'org-on-target-p 'org-at-target-p)
21689 (defun org-up-heading-all (arg)
21690 "Move to the heading line of which the present line is a subheading.
21691 This function considers both visible and invisible heading lines.
21692 With argument, move up ARG levels."
21693 (if (fboundp 'outline-up-heading-all)
21694 (outline-up-heading-all arg) ; emacs 21 version of outline.el
21695 (outline-up-heading arg t))) ; emacs 22 version of outline.el
21697 (defun org-up-heading-safe ()
21698 "Move to the heading line of which the present line is a subheading.
21699 This version will not throw an error. It will return the level of the
21700 headline found, or nil if no higher level is found.
21702 Also, this function will be a lot faster than `outline-up-heading',
21703 because it relies on stars being the outline starters. This can really
21704 make a significant difference in outlines with very many siblings."
21705 (let (start-level re)
21706 (org-back-to-heading t)
21707 (setq start-level (funcall outline-level))
21708 (if (equal start-level 1)
21710 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
21711 (if (re-search-backward re nil t)
21712 (funcall outline-level)))))
21714 (defun org-first-sibling-p ()
21715 "Is this heading the first child of its parents?"
21717 (let ((re org-outline-regexp-bol)
21719 (unless (org-at-heading-p t)
21720 (error "Not at a heading"))
21721 (setq level (funcall outline-level))
21723 (if (not (re-search-backward re nil t))
21725 (setq l (funcall outline-level))
21728 (defun org-goto-sibling (&optional previous)
21729 "Goto the next sibling, even if it is invisible.
21730 When PREVIOUS is set, go to the previous sibling instead. Returns t
21731 when a sibling was found. When none is found, return nil and don't
21733 (let ((fun (if previous 're-search-backward 're-search-forward))
21735 (re org-outline-regexp-bol)
21737 (when (condition-case nil (org-back-to-heading t) (error nil))
21738 (setq level (funcall outline-level))
21740 (or previous (forward-char 1))
21741 (while (funcall fun re nil t)
21742 (setq l (funcall outline-level))
21743 (when (< l level) (goto-char pos) (throw 'exit nil))
21744 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
21748 (defun org-show-siblings ()
21749 "Show all siblings of the current headline."
21751 (while (org-goto-sibling) (org-flag-heading nil)))
21753 (while (org-goto-sibling 'previous)
21754 (org-flag-heading nil))))
21756 (defun org-goto-first-child ()
21757 "Goto the first child, even if it is invisible.
21758 Return t when a child was found. Otherwise don't move point and
21760 (let (level (pos (point)) (re org-outline-regexp-bol))
21761 (when (condition-case nil (org-back-to-heading t) (error nil))
21762 (setq level (outline-level))
21764 (if (and (re-search-forward re nil t) (> (outline-level) level))
21765 (progn (goto-char (match-beginning 0)) t)
21766 (goto-char pos) nil))))
21768 (defun org-show-hidden-entry ()
21769 "Show an entry where even the heading is hidden."
21773 (defun org-flag-heading (flag &optional entry)
21774 "Flag the current heading. FLAG non-nil means make invisible.
21775 When ENTRY is non-nil, show the entire entry."
21777 (org-back-to-heading t)
21778 ;; Check if we should show the entire entry
21783 (and (outline-next-heading)
21784 (org-flag-heading nil))))
21785 (outline-flag-region (max (point-min) (1- (point)))
21786 (save-excursion (outline-end-of-heading) (point))
21789 (defun org-get-next-sibling ()
21790 "Move to next heading of the same level, and return point.
21791 If there is no such heading, return nil.
21792 This is like outline-next-sibling, but invisible headings are ok."
21793 (let ((level (funcall outline-level)))
21794 (outline-next-heading)
21795 (while (and (not (eobp)) (> (funcall outline-level) level))
21796 (outline-next-heading))
21797 (if (or (eobp) (< (funcall outline-level) level))
21801 (defun org-get-last-sibling ()
21802 "Move to previous heading of the same level, and return point.
21803 If there is no such heading, return nil."
21804 (let ((opoint (point))
21805 (level (funcall outline-level)))
21806 (outline-previous-heading)
21807 (when (and (/= (point) opoint) (outline-on-heading-p t))
21808 (while (and (> (funcall outline-level) level)
21810 (outline-previous-heading))
21811 (if (< (funcall outline-level) level)
21815 (defun org-end-of-subtree (&optional invisible-ok to-heading)
21816 "Goto to the end of a subtree."
21817 ;; This contains an exact copy of the original function, but it uses
21818 ;; `org-back-to-heading', to make it work also in invisible
21819 ;; trees. And is uses an invisible-ok argument.
21820 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
21821 ;; Furthermore, when used inside Org, finding the end of a large subtree
21822 ;; with many children and grandchildren etc, this can be much faster
21823 ;; than the outline version.
21824 (org-back-to-heading invisible-ok)
21826 (level (funcall outline-level)))
21827 (if (and (derived-mode-p 'org-mode) (< level 1000))
21828 ;; A true heading (not a plain list item), in Org-mode
21829 ;; This means we can easily find the end by looking
21830 ;; only for the right number of stars. Using a regexp to do
21831 ;; this is so much faster than using a Lisp loop.
21832 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
21834 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
21835 ;; something else, do it the slow way
21836 (while (and (not (eobp))
21837 (or first (> (funcall outline-level) level)))
21839 (outline-next-heading)))
21841 (if (memq (preceding-char) '(?\n ?\^M))
21843 ;; Go to end of line before heading
21845 (if (memq (preceding-char) '(?\n ?\^M))
21846 ;; leave blank line before heading
21847 (forward-char -1))))))
21850 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
21851 "Use Org version in org-mode, for dramatic speed-up."
21852 (if (derived-mode-p 'org-mode)
21854 (org-end-of-subtree nil t)
21855 (unless (eobp) (backward-char 1)))
21858 (defun org-end-of-meta-data-and-drawers ()
21859 "Jump to the first text after meta data and drawers in the current entry.
21860 This will move over empty lines, lines with planning time stamps,
21861 clocking lines, and drawers."
21862 (org-back-to-heading t)
21863 (let ((end (save-excursion (outline-next-heading) (point)))
21864 (re (concat "\\(" org-drawer-regexp "\\)"
21865 "\\|" "[ \t]*" org-keyword-time-regexp)))
21867 (while (re-search-forward re end t)
21868 (if (not (match-end 1))
21869 ;; empty or planning line
21871 ;; a drawer, find the end
21872 (re-search-forward "^[ \t]*:END:" end 'move)
21874 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
21877 (defun org-forward-heading-same-level (arg &optional invisible-ok)
21878 "Move forward to the arg'th subheading at same level as this one.
21879 Stop at the first and last subheadings of a superior heading.
21880 Normally this only looks at visible headings, but when INVISIBLE-OK is
21881 non-nil it will also look at invisible ones."
21883 (org-back-to-heading invisible-ok)
21885 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21886 (re (format "^\\*\\{1,%d\\} " level))
21890 (while (and (re-search-forward re nil 'move)
21891 (setq l (- (match-end 0) (match-beginning 0) 1))
21894 (progn (backward-char 1) (outline-invisible-p)))
21895 (if (< l level) (setq arg 1)))
21896 (setq arg (1- arg)))
21897 (beginning-of-line 1)))
21899 (defun org-backward-heading-same-level (arg &optional invisible-ok)
21900 "Move backward to the arg'th subheading at same level as this one.
21901 Stop at the first and last subheadings of a superior heading."
21903 (org-back-to-heading)
21905 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21906 (re (format "^\\*\\{1,%d\\} " level))
21909 (while (and (re-search-backward re nil 'move)
21910 (setq l (- (match-end 0) (match-beginning 0) 1))
21913 (outline-invisible-p))
21914 (if (< l level) (setq arg 1)))
21915 (setq arg (1- arg)))))
21917 (defun org-forward-element ()
21918 "Move forward by one element.
21919 Move to the next element at the same level, when possible."
21921 (cond ((eobp) (error "Cannot move further down"))
21922 ((org-with-limited-levels (org-at-heading-p))
21923 (let ((origin (point)))
21924 (org-forward-heading-same-level 1)
21925 (unless (org-with-limited-levels (org-at-heading-p))
21927 (error "Cannot move further down"))))
21929 (let* ((elem (org-element-at-point))
21930 (end (org-element-property :end elem))
21931 (parent (org-element-property :parent elem)))
21932 (if (and parent (= (org-element-property :contents-end parent) end))
21933 (goto-char (org-element-property :end parent))
21934 (goto-char end))))))
21936 (defun org-backward-element ()
21937 "Move backward by one element.
21938 Move to the previous element at the same level, when possible."
21940 (cond ((bobp) (error "Cannot move further up"))
21941 ((org-with-limited-levels (org-at-heading-p))
21942 ;; At an headline, move to the previous one, if any, or stay
21944 (let ((origin (point)))
21945 (org-backward-heading-same-level 1)
21946 (unless (org-with-limited-levels (org-at-heading-p))
21948 (error "Cannot move further up"))))
21950 (let* ((trail (org-element-at-point 'keep-trail))
21952 (prev-elem (nth 1 trail))
21953 (beg (org-element-property :begin elem)))
21955 ;; Move to beginning of current element if point isn't
21957 ((/= (point) beg) (goto-char beg))
21958 (prev-elem (goto-char (org-element-property :begin prev-elem)))
21959 ((org-before-first-heading-p) (goto-char (point-min)))
21960 (t (org-back-to-heading)))))))
21962 (defun org-up-element ()
21963 "Move to upper element."
21965 (if (org-with-limited-levels (org-at-heading-p))
21966 (unless (org-up-heading-safe) (error "No surrounding element"))
21967 (let* ((elem (org-element-at-point))
21968 (parent (org-element-property :parent elem)))
21969 (if parent (goto-char (org-element-property :begin parent))
21970 (if (org-with-limited-levels (org-before-first-heading-p))
21971 (error "No surrounding element")
21972 (org-with-limited-levels (org-back-to-heading)))))))
21974 (defvar org-element-greater-elements)
21975 (defun org-down-element ()
21976 "Move to inner element."
21978 (let ((element (org-element-at-point)))
21980 ((memq (org-element-type element) '(plain-list table))
21981 (goto-char (org-element-property :contents-begin element))
21983 ((memq (org-element-type element) org-element-greater-elements)
21984 ;; If contents are hidden, first disclose them.
21985 (when (org-element-property :hiddenp element) (org-cycle))
21986 (goto-char (or (org-element-property :contents-begin element)
21987 (error "No content for this element"))))
21988 (t (error "No inner element")))))
21990 (defun org-drag-element-backward ()
21991 "Move backward element at point."
21993 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
21994 (let* ((trail (org-element-at-point 'keep-trail))
21996 (prev-elem (nth 1 trail)))
21997 ;; Error out if no previous element or previous element is
21998 ;; a parent of the current one.
21999 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
22000 (error "Cannot drag element backward")
22001 (let ((pos (point)))
22002 (org-element-swap-A-B prev-elem elem)
22003 (goto-char (+ (org-element-property :begin prev-elem)
22004 (- pos (org-element-property :begin elem)))))))))
22006 (defun org-drag-element-forward ()
22007 "Move forward element at point."
22009 (let* ((pos (point))
22010 (elem (org-element-at-point)))
22011 (when (= (point-max) (org-element-property :end elem))
22012 (error "Cannot drag element forward"))
22013 (goto-char (org-element-property :end elem))
22014 (let ((next-elem (org-element-at-point)))
22015 (when (or (org-element-nested-p elem next-elem)
22016 (and (eq (org-element-type next-elem) 'headline)
22017 (not (eq (org-element-type elem) 'headline))))
22019 (error "Cannot drag element forward"))
22020 ;; Compute new position of point: it's shifted by NEXT-ELEM
22021 ;; body's length (without final blanks) and by the length of
22022 ;; blanks between ELEM and NEXT-ELEM.
22023 (let ((size-next (- (save-excursion
22024 (goto-char (org-element-property :end next-elem))
22025 (skip-chars-backward " \r\t\n")
22027 ;; Small correction if buffer doesn't end
22028 ;; with a newline character.
22029 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
22030 (org-element-property :begin next-elem)))
22031 (size-blank (- (org-element-property :end elem)
22033 (goto-char (org-element-property :end elem))
22034 (skip-chars-backward " \r\t\n")
22037 (org-element-swap-A-B elem next-elem)
22038 (goto-char (+ pos size-next size-blank))))))
22040 (defun org-mark-element ()
22041 "Put point at beginning of this element, mark at end.
22043 Interactively, if this command is repeated or (in Transient Mark
22044 mode) if the mark is active, it marks the next element after the
22045 ones already marked."
22047 (let (deactivate-mark)
22048 (if (and (org-called-interactively-p 'any)
22049 (or (and (eq last-command this-command) (mark t))
22050 (and transient-mark-mode mark-active)))
22054 (goto-char (org-element-property :end (org-element-at-point)))))
22055 (let ((element (org-element-at-point)))
22057 (push-mark (org-element-property :end element) t t)
22058 (goto-char (org-element-property :begin element))))))
22060 (defun org-narrow-to-element ()
22061 "Narrow buffer to current element."
22063 (let ((elem (org-element-at-point)))
22065 ((eq (car elem) 'headline)
22067 (org-element-property :begin elem)
22068 (org-element-property :end elem)))
22069 ((memq (car elem) org-element-greater-elements)
22071 (org-element-property :contents-begin elem)
22072 (org-element-property :contents-end elem)))
22075 (org-element-property :begin elem)
22076 (org-element-property :end elem))))))
22078 (defun org-transpose-element ()
22079 "Transpose current and previous elements, keeping blank lines between.
22080 Point is moved after both elements."
22082 (org-skip-whitespace)
22083 (let ((end (org-element-property :end (org-element-at-point))))
22084 (org-drag-element-backward)
22087 (defun org-unindent-buffer ()
22088 "Un-indent the visible part of the buffer.
22089 Relative indentation (between items, inside blocks, etc.) isn't
22092 (unless (eq major-mode 'org-mode)
22093 (error "Cannot un-indent a buffer not in Org mode"))
22094 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
22095 unindent-tree ; For byte-compiler.
22101 (if (memq (org-element-type element) '(headline section))
22102 (funcall unindent-tree (org-element-contents element))
22106 (org-element-property :begin element)
22107 (org-element-property :end element))
22108 (org-do-remove-indentation)))))
22109 (reverse contents))))))
22110 (funcall unindent-tree (org-element-contents parse-tree))))
22112 (defun org-show-subtree ()
22113 "Show everything after this heading at deeper levels."
22115 (outline-flag-region
22118 (org-end-of-subtree t t))
22121 (defun org-show-entry ()
22122 "Show the body directly following this heading.
22123 Show the heading too, if it is currently invisible."
22126 (condition-case nil
22128 (org-back-to-heading t)
22129 (outline-flag-region
22130 (max (point-min) (1- (point)))
22132 (if (re-search-forward
22133 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
22134 (match-beginning 1)
22137 (org-cycle-hide-drawers 'children))
22140 (defun org-make-options-regexp (kwds &optional extra)
22141 "Make a regular expression for keyword lines."
22144 (mapconcat 'regexp-quote kwds "\\|")
22145 (if extra (concat "\\|" extra))
22146 "\\):[ \t]*\\(.*\\)"))
22148 ;; Make isearch reveal the necessary context
22149 (defun org-isearch-end ()
22150 "Reveal context after isearch exits."
22151 (when isearch-success ; only if search was successful
22152 (if (featurep 'xemacs)
22153 ;; Under XEmacs, the hook is run in the correct place,
22154 ;; we directly show the context.
22155 (org-show-context 'isearch)
22156 ;; In Emacs the hook runs *before* restoring the overlays.
22157 ;; So we have to use a one-time post-command-hook to do this.
22158 ;; (Emacs 22 has a special variable, see function `org-mode')
22159 (unless (and (boundp 'isearch-mode-end-hook-quit)
22160 isearch-mode-end-hook-quit)
22161 ;; Only when the isearch was not quitted.
22162 (org-add-hook 'post-command-hook 'org-isearch-post-command
22163 'append 'local)))))
22165 (defun org-isearch-post-command ()
22166 "Remove self from hook, and show context."
22167 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
22168 (org-show-context 'isearch))
22171 ;;;; Integration with and fixes for other packages
22175 (defvar org-imenu-markers nil
22176 "All markers currently used by Imenu.")
22177 (make-variable-buffer-local 'org-imenu-markers)
22179 (defun org-imenu-new-marker (&optional pos)
22180 "Return a new marker for use by Imenu, and remember the marker."
22181 (let ((m (make-marker)))
22182 (move-marker m (or pos (point)))
22183 (push m org-imenu-markers)
22186 (defun org-imenu-get-tree ()
22187 "Produce the index for Imenu."
22188 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
22189 (setq org-imenu-markers nil)
22190 (let* ((n org-imenu-depth)
22191 (re (concat "^" (org-get-limited-outline-regexp)))
22192 (subs (make-vector (1+ n) nil))
22198 (goto-char (point-max))
22199 (while (re-search-backward re nil t)
22200 (setq level (org-reduced-level (funcall outline-level)))
22201 (when (and (<= level n)
22202 (looking-at org-complex-heading-regexp))
22203 (setq head (org-link-display-format
22204 (org-match-string-no-properties 4))
22205 m (org-imenu-new-marker))
22206 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
22207 (if (>= level last-level)
22208 (push (cons head m) (aref subs level))
22209 (push (cons head (aref subs (1+ level))) (aref subs level))
22210 (loop for i from (1+ level) to n do (aset subs i nil)))
22211 (setq last-level level)))))
22214 (eval-after-load "imenu"
22216 (add-hook 'imenu-after-jump-hook
22218 (if (derived-mode-p 'org-mode)
22219 (org-show-context 'org-goto))))))
22221 (defun org-link-display-format (link)
22222 "Replace a link with either the description, or the link target
22223 if no description is present"
22225 (if (string-match org-bracket-link-analytic-regexp link)
22226 (replace-match (if (match-end 5)
22227 (match-string 5 link)
22228 (concat (match-string 1 link)
22229 (match-string 3 link)))
22233 (defun org-toggle-link-display ()
22234 "Toggle the literal or descriptive display of links."
22236 (if org-descriptive-links
22237 (progn (org-remove-from-invisibility-spec '(org-link))
22238 (org-restart-font-lock)
22239 (setq org-descriptive-links nil))
22240 (progn (add-to-invisibility-spec '(org-link))
22241 (org-restart-font-lock)
22242 (setq org-descriptive-links t))))
22244 ;; Speedbar support
22246 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
22247 "Overlay marking the agenda restriction line in speedbar.")
22248 (overlay-put org-speedbar-restriction-lock-overlay
22249 'face 'org-agenda-restriction-lock)
22250 (overlay-put org-speedbar-restriction-lock-overlay
22251 'help-echo "Agendas are currently limited to this item.")
22252 (org-detach-overlay org-speedbar-restriction-lock-overlay)
22254 (defun org-speedbar-set-agenda-restriction ()
22255 "Restrict future agenda commands to the location at point in speedbar.
22256 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
22258 (require 'org-agenda)
22259 (let (p m tp np dir txt)
22261 ((setq p (text-property-any (point-at-bol) (point-at-eol)
22263 (setq m (get-text-property p 'org-imenu-marker))
22264 (with-current-buffer (marker-buffer m)
22266 (org-agenda-set-restriction-lock 'subtree)))
22267 ((setq p (text-property-any (point-at-bol) (point-at-eol)
22268 'speedbar-function 'speedbar-find-file))
22269 (setq tp (previous-single-property-change
22270 (1+ p) 'speedbar-function)
22271 np (next-single-property-change
22272 tp 'speedbar-function)
22273 dir (speedbar-line-directory)
22274 txt (buffer-substring-no-properties (or tp (point-min))
22275 (or np (point-max))))
22276 (with-current-buffer (find-file-noselect
22277 (let ((default-directory dir))
22278 (expand-file-name txt)))
22279 (unless (derived-mode-p 'org-mode)
22280 (error "Cannot restrict to non-Org-mode file"))
22281 (org-agenda-set-restriction-lock 'file)))
22282 (t (error "Don't know how to restrict Org-mode's agenda")))
22283 (move-overlay org-speedbar-restriction-lock-overlay
22284 (point-at-bol) (point-at-eol))
22285 (setq current-prefix-arg nil)
22286 (org-agenda-maybe-redo)))
22288 (eval-after-load "speedbar"
22290 (speedbar-add-supported-extension ".org")
22291 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
22292 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
22293 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
22294 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
22295 (add-hook 'speedbar-visiting-tag-hook
22296 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
22298 ;;; Fixes and Hacks for problems with other packages
22300 ;; Make flyspell not check words in links, to not mess up our keymap
22301 (defun org-mode-flyspell-verify ()
22302 "Don't let flyspell put overlays at active buttons, or on
22303 {todo,all-time,additional-option-like}-keywords."
22304 (let ((pos (max (1- (point)) (point-min)))
22305 (word (thing-at-point 'word)))
22306 (and (not (get-text-property pos 'keymap))
22307 (not (get-text-property pos 'org-no-flyspell))
22308 (not (member word org-todo-keywords-1))
22309 (not (member word org-all-time-keywords))
22310 (not (member word org-options-keywords))
22311 (not (member word (mapcar 'car org-startup-options)))
22312 (not (member word org-additional-option-like-keywords-for-flyspell)))))
22314 (defun org-remove-flyspell-overlays-in (beg end)
22315 "Remove flyspell overlays in region."
22316 (and (org-bound-and-true-p flyspell-mode)
22317 (fboundp 'flyspell-delete-region-overlays)
22318 (flyspell-delete-region-overlays beg end))
22319 (add-text-properties beg end '(org-no-flyspell t)))
22321 ;; Make `bookmark-jump' shows the jump location if it was hidden.
22322 (eval-after-load "bookmark"
22323 '(if (boundp 'bookmark-after-jump-hook)
22324 ;; We can use the hook
22325 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
22326 ;; Hook not available, use advice
22327 (defadvice bookmark-jump (after org-make-visible activate)
22328 "Make the position visible."
22329 (org-bookmark-jump-unhide))))
22331 ;; Make sure saveplace shows the location if it was hidden
22332 (eval-after-load "saveplace"
22333 '(defadvice save-place-find-file-hook (after org-make-visible activate)
22334 "Make the position visible."
22335 (org-bookmark-jump-unhide)))
22337 ;; Make sure ecb shows the location if it was hidden
22338 (eval-after-load "ecb"
22339 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
22340 "Make hierarchy visible when jumping into location from ECB tree buffer."
22341 (if (derived-mode-p 'org-mode)
22342 (org-show-context))))
22344 (defun org-bookmark-jump-unhide ()
22345 "Unhide the current position, to show the bookmark location."
22346 (and (derived-mode-p 'org-mode)
22347 (or (outline-invisible-p)
22348 (save-excursion (goto-char (max (point-min) (1- (point))))
22349 (outline-invisible-p)))
22350 (org-show-context 'bookmark-jump)))
22352 ;; Make session.el ignore our circular variable
22353 (eval-after-load "session"
22354 '(add-to-list 'session-globals-exclude 'org-mark-ring))
22356 ;;;; Experimental code
22358 (defun org-closed-in-range ()
22359 "Sparse tree of items closed in a certain time range.
22360 Still experimental, may disappear in the future."
22362 ;; Get the time interval from the user.
22363 (let* ((time1 (org-float-time
22364 (org-read-date nil 'to-time nil "Starting date: ")))
22365 (time2 (org-float-time
22366 (org-read-date nil 'to-time nil "End date:")))
22367 ;; callback function
22368 (callback (lambda ()
22371 (apply 'encode-time
22372 (org-parse-time-string
22373 (match-string 1))))))
22374 ;; check if time in interval
22375 (and (>= time time1) (<= time time2))))))
22376 ;; make tree, check each match with the callback
22377 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
22383 (run-hooks 'org-load-hook)
22385 ;;; org.el ends here