1 ;;; org.el --- Outline-based notes management and organizer
3 ;; Carstens outline-mode for keeping track of everything.
4 ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
8 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
30 ;; project planning with a fast and effective plain-text system.
32 ;; Org-mode develops organizational tasks around NOTES files that contain
33 ;; information about projects as plain text. Org-mode is implemented on
34 ;; top of outline-mode, which makes it possible to keep the content of
35 ;; large files well structured. Visibility cycling and structure editing
36 ;; help to work with the tree. Tables are easily created with a built-in
37 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
38 ;; and scheduling. It dynamically compiles entries into an agenda that
39 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
40 ;; Plain text URL-like links connect to websites, emails, Usenet
41 ;; messages, BBDB entries, and any files related to the projects. For
42 ;; printing and sharing of notes, an Org-mode file can be exported as a
43 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
44 ;; iCalendar file. It can also serve as a publishing tool for a set of
47 ;; Installation and Activation
48 ;; ---------------------------
49 ;; See the corresponding sections in the manual at
51 ;; http://orgmode.org/org.html#Installation
55 ;; The documentation of Org-mode can be found in the TeXInfo file. The
56 ;; distribution also contains a PDF version of it. At the homepage of
57 ;; Org-mode, you can read the same text online as HTML. There is also an
58 ;; excellent reference card made by Philip Rooke. This card can be found
59 ;; in the etc/ directory of Emacs 22.
61 ;; A list of recent changes can be found at
62 ;; http://orgmode.org/Changes.html
66 (defvar org-inhibit-highlight-removal nil
) ; dynamically scoped param
67 (defvar org-table-formula-constants-local nil
68 "Local version of `org-table-formula-constants'.")
69 (make-variable-buffer-local 'org-table-formula-constants-local
)
71 ;;;; Require other packages
79 (require 'format-spec
)
81 ;; `org-outline-regexp' ought to be a defconst but is let-binding in
82 ;; some places -- e.g. see the macro org-with-limited-levels.
84 ;; In Org buffers, the value of `outline-regexp' is that of
85 ;; `org-outline-regexp'. The only function still directly relying on
86 ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
87 ;; job when `orgstruct-mode' is active.
88 (defvar org-outline-regexp
"\\*+ "
89 "Regexp to match Org headlines.")
91 (defvar org-outline-regexp-bol
"^\\*+ "
92 "Regexp to match Org headlines.
93 This is similar to `org-outline-regexp' but additionally makes
94 sure that we are at the beginning of the line.")
96 (defvar org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
97 "Matches an headline, putting stars and text into groups.
98 Stars are put in group 1 and the trimmed body in group 2.")
100 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
101 (when (fboundp 'defvaralias
)
102 (unless (boundp 'calendar-view-holidays-initially-flag
)
103 (defvaralias 'calendar-view-holidays-initially-flag
104 'view-calendar-holidays-initially
))
105 (unless (boundp 'calendar-view-diary-initially-flag
)
106 (defvaralias 'calendar-view-diary-initially-flag
107 'view-diary-entries-initially
))
108 (unless (boundp 'diary-fancy-buffer
)
109 (defvaralias 'diary-fancy-buffer
'fancy-diary-buffer
)))
111 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
112 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
113 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
114 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
115 (declare-function org-at-clock-log-p
"org-clock" ())
116 (declare-function org-clock-timestamps-up
"org-clock" ())
117 (declare-function org-clock-timestamps-down
"org-clock" ())
119 ;; load languages based on value of `org-babel-load-languages'
120 (defvar org-babel-load-languages
)
122 (defun org-babel-do-load-languages (sym value
)
123 "Load the languages defined in `org-babel-load-languages'."
124 (set-default sym value
)
126 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
129 (require (intern (concat "ob-" lang
))))
131 (funcall 'fmakunbound
132 (intern (concat "org-babel-execute:" lang
)))
133 (funcall 'fmakunbound
134 (intern (concat "org-babel-expand-body:" lang
)))))))
135 org-babel-load-languages
))
137 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
138 "Languages which can be evaluated in Org-mode buffers.
139 This list can be used to load support for any of the languages
140 below, note that each language will depend on a different set of
141 system executables and/or Emacs modes. When a language is
142 \"loaded\", then code blocks in that language can be evaluated
143 with `org-babel-execute-src-block' bound by default to C-c
144 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
145 be set to remove code block evaluation from the C-c C-c
146 keybinding. By default only Emacs Lisp (which has no
147 requirements) is loaded."
149 :set
'org-babel-do-load-languages
151 :type
'(alist :tag
"Babel Languages"
154 (const :tag
"Awk" awk
)
157 (const :tag
"Asymptote" asymptote
)
158 (const :tag
"Calc" calc
)
159 (const :tag
"Clojure" clojure
)
160 (const :tag
"CSS" css
)
161 (const :tag
"Ditaa" ditaa
)
162 (const :tag
"Dot" dot
)
163 (const :tag
"Emacs Lisp" emacs-lisp
)
164 (const :tag
"Fortran" fortran
)
165 (const :tag
"Gnuplot" gnuplot
)
166 (const :tag
"Haskell" haskell
)
168 (const :tag
"Java" java
)
169 (const :tag
"Javascript" js
)
170 (const :tag
"LaTeX" latex
)
171 (const :tag
"Ledger" ledger
)
172 (const :tag
"Lilypond" lilypond
)
173 (const :tag
"Lisp" lisp
)
174 (const :tag
"Maxima" maxima
)
175 (const :tag
"Matlab" matlab
)
176 (const :tag
"Mscgen" mscgen
)
177 (const :tag
"Ocaml" ocaml
)
178 (const :tag
"Octave" octave
)
179 (const :tag
"Org" org
)
180 (const :tag
"Perl" perl
)
181 (const :tag
"Pico Lisp" picolisp
)
182 (const :tag
"PlantUML" plantuml
)
183 (const :tag
"Python" python
)
184 (const :tag
"Ruby" ruby
)
185 (const :tag
"Sass" sass
)
186 (const :tag
"Scala" scala
)
187 (const :tag
"Scheme" scheme
)
188 (const :tag
"Screen" screen
)
189 (const :tag
"Shell Script" sh
)
190 (const :tag
"Shen" shen
)
191 (const :tag
"Sql" sql
)
192 (const :tag
"Sqlite" sqlite
))
193 :value-type
(boolean :tag
"Activate" :value t
)))
195 ;;;; Customization variables
196 (defcustom org-clone-delete-id nil
197 "Remove ID property of clones of a subtree.
198 When non-nil, clones of a subtree don't inherit the ID property.
199 Otherwise they inherit the ID property with a new unique
206 (require 'org-compat
)
209 (defun org-version (&optional here full message
)
210 "Show the org-mode version in the echo area.
211 With prefix argument HERE, insert it at point.
212 When FULL is non-nil, use a verbose version string.
213 When MESSAGE is non-nil, display a message with the version."
215 (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
216 (org-install-dir (ignore-errors (org-find-library-dir "org-install.el")))
218 (and (fboundp 'org-release
) (fboundp 'org-git-version
))
219 (load (concat org-dir
"org-version.el")
220 'noerror
'nomessage
'nosuffix
)))
221 (org-version (org-release))
222 (git-version (org-git-version))
223 (version (format "Org-mode version %s (%s @ %s)"
227 (if (string= org-dir org-install-dir
)
229 (concat "mixed installation! " org-install-dir
" and " org-dir
))
230 "org-install.el can not be found!")))
231 (_version (if full version org-version
)))
232 (if (org-called-interactively-p 'interactive
)
236 (if message
(message _version
))
239 ;;; Compatibility constants
241 ;;; The custom variables
244 "Outline-based notes management and organizer."
249 (defcustom org-mode-hook nil
250 "Mode hook for Org-mode, run after the mode was turned on."
254 (defcustom org-load-hook nil
255 "Hook that is run after org.el has been loaded."
259 (defcustom org-log-buffer-setup-hook nil
260 "Hook that is run after an Org log buffer is created."
265 (defvar org-modules
) ; defined below
266 (defvar org-modules-loaded nil
267 "Have the modules been loaded already?")
269 (defun org-load-modules-maybe (&optional force
)
270 "Load all extensions listed in `org-modules'."
271 (when (or force
(not org-modules-loaded
))
273 (condition-case nil
(require ext
)
274 (error (message "Problems while trying to load feature `%s'" ext
))))
276 (setq org-modules-loaded t
)))
278 (defun org-set-modules (var value
)
279 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
281 (when (featurep 'org
)
282 (org-load-modules-maybe 'force
)))
284 (when (org-bound-and-true-p org-modules
)
285 (let ((a (member 'org-infojs org-modules
)))
286 (and a
(setcar a
'org-jsinfo
))))
288 (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
)
289 "Modules that should always be loaded together with org.el.
290 If a description starts with <C>, the file is not part of Emacs
291 and loading it will require that you have downloaded and properly installed
292 the org-mode distribution.
294 You can also use this system to load external packages (i.e. neither Org
295 core modules, nor modules from the CONTRIB directory). Just add symbols
296 to the end of the list. If the package is called org-xyz.el, then you need
297 to add the symbol `xyz', and the package must have a call to
301 :set
'org-set-modules
304 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
305 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
306 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
307 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
308 (const :tag
" docview: Links to doc-view buffers" org-docview
)
309 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
310 (const :tag
" id: Global IDs for identifying entries" org-id
)
311 (const :tag
" info: Links to Info nodes" org-info
)
312 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
313 (const :tag
" habit: Track your consistency with habits" org-habit
)
314 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
315 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
316 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
317 (const :tag
" mew Links to Mew folders/messages" org-mew
)
318 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
319 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
320 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
321 (const :tag
" special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks
)
322 (const :tag
" vm: Links to VM folders/messages" org-vm
)
323 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
324 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
325 (const :tag
" mouse: Additional mouse support" org-mouse
)
326 (const :tag
" TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler
)
328 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
329 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
330 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
331 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
332 (const :tag
"C collector: Collect properties into tables" org-collector
)
333 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
334 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
335 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
336 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
337 (const :tag
"C eval: Include command output as text" org-eval
)
338 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
339 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
340 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
341 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
342 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
344 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
346 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
347 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
348 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
349 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
350 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
351 (const :tag
"C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber
)
352 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
353 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
354 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
355 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
356 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
357 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
358 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
359 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
360 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
361 (const :tag
"C track: Keep up with Org-mode development" org-track
)
362 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
363 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
364 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
366 (defcustom org-support-shift-select nil
367 "Non-nil means make shift-cursor commands select text when possible.
369 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
370 start selecting a region, or enlarge regions started in this way.
371 In Org-mode, in special contexts, these same keys are used for
372 other purposes, important enough to compete with shift selection.
373 Org tries to balance these needs by supporting `shift-select-mode'
374 outside these special contexts, under control of this variable.
376 The default of this variable is nil, to avoid confusing behavior. Shifted
377 cursor keys will then execute Org commands in the following contexts:
378 - on a headline, changing TODO state (left/right) and priority (up/down)
379 - on a time stamp, changing the time
380 - in a plain list item, changing the bullet type
381 - in a property definition line, switching between allowed values
382 - in the BEGIN line of a clock table (changing the time block).
383 Outside these contexts, the commands will throw an error.
385 When this variable is t and the cursor is not in a special
386 context, Org-mode will support shift-selection for making and
387 enlarging regions. To make this more effective, the bullet
388 cycling will no longer happen anywhere in an item line, but only
389 if the cursor is exactly on the bullet.
391 If you set this variable to the symbol `always', then the keys
392 will not be special in headlines, property lines, and item lines,
393 to make shift selection work there as well. If this is what you
394 want, you can use the following alternative commands: `C-c C-t'
395 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
396 can be used to switch TODO sets, `C-c -' to cycle item bullet
397 types, and properties can be edited by hand or in column view.
399 However, when the cursor is on a timestamp, shift-cursor commands
400 will still edit the time stamp - this is just too good to give up.
402 XEmacs user should have this variable set to nil, because
403 `shift-select-mode' is in Emacs 23 or later only."
406 (const :tag
"Never" nil
)
407 (const :tag
"When outside special context" t
)
408 (const :tag
"Everywhere except timestamps" always
)))
410 (defcustom org-loop-over-headlines-in-active-region nil
411 "Shall some commands act upon headlines in the active region?
413 When set to `t', some commands will be performed in all headlines
414 within the active region.
416 When set to `start-level', some commands will be performed in all
417 headlines within the active region, provided that these headlines
418 are of the same level than the first one.
420 When set to a string, those commands will be performed on the
421 matching headlines within the active region. Such string must be
422 a tags/property/todo match as it is used in the agenda tags view.
424 The list of commands is: `org-schedule', `org-deadline',
425 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
426 `org-archive-to-archive-sibling'. The archiving commands skip
427 already archived entries."
428 :type
'(choice (const :tag
"Don't loop" nil
)
429 (const :tag
"All headlines in active region" t
)
430 (const :tag
"In active region, headlines at the same level than the first one" 'start-level
)
431 (string :tag
"Tags/Property/Todo matcher"))
436 (defgroup org-startup nil
437 "Options concerning startup of Org-mode."
441 (defcustom org-startup-folded t
442 "Non-nil means entering Org-mode will switch to OVERVIEW.
443 This can also be configured on a per-file basis by adding one of
444 the following lines anywhere in the buffer:
446 #+STARTUP: fold (or `overview', this is equivalent)
447 #+STARTUP: nofold (or `showall', this is equivalent)
449 #+STARTUP: showeverything"
452 (const :tag
"nofold: show all" nil
)
453 (const :tag
"fold: overview" t
)
454 (const :tag
"content: all headlines" content
)
455 (const :tag
"show everything, even drawers" showeverything
)))
457 (defcustom org-startup-truncated t
458 "Non-nil means entering Org-mode will set `truncate-lines'.
459 This is useful since some lines containing links can be very long and
460 uninteresting. Also tables look terrible when wrapped."
464 (defcustom org-startup-indented nil
465 "Non-nil means turn on `org-indent-mode' on startup.
466 This can also be configured on a per-file basis by adding one of
467 the following lines anywhere in the buffer:
471 :group
'org-structure
473 (const :tag
"Not" nil
)
474 (const :tag
"Globally (slow on startup in large files)" t
)))
476 (defcustom org-use-sub-superscripts t
477 "Non-nil means interpret \"_\" and \"^\" for export.
478 When this option is turned on, you can use TeX-like syntax for sub- and
479 superscripts. Several characters after \"_\" or \"^\" will be
480 considered as a single item - so grouping with {} is normally not
481 needed. For example, the following things will be parsed as single
482 sub- or superscripts.
484 10^24 or 10^tau several digits will be considered 1 item.
485 10^-12 or 10^-tau a leading sign with digits or a word
486 x^2-y^3 will be read as x^2 - y^3, because items are
487 terminated by almost any nonword/nondigit char.
488 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
490 Still, ambiguity is possible - so when in doubt use {} to enclose the
491 sub/superscript. If you set this variable to the symbol `{}',
492 the braces are *required* in order to trigger interpretations as
493 sub/superscript. This can be helpful in documents that need \"_\"
494 frequently in plain text.
496 Not all export backends support this, but HTML does.
498 This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
500 :group
'org-export-translation
503 (const :tag
"Always interpret" t
)
504 (const :tag
"Only with braces" {})
505 (const :tag
"Never interpret" nil
)))
507 (if (fboundp 'defvaralias
)
508 (defvaralias 'org-export-with-sub-superscripts
'org-use-sub-superscripts
))
511 (defcustom org-startup-with-beamer-mode nil
512 "Non-nil means turn on `org-beamer-mode' on startup.
513 This can also be configured on a per-file basis by adding one of
514 the following lines anywhere in the buffer:
521 (defcustom org-startup-align-all-tables nil
522 "Non-nil means align all tables when visiting a file.
523 This is useful when the column width in tables is forced with <N> cookies
524 in table fields. Such tables will look correct only after the first re-align.
525 This can also be configured on a per-file basis by adding one of
526 the following lines anywhere in the buffer:
532 (defcustom org-startup-with-inline-images nil
533 "Non-nil means show inline images when loading a new Org file.
534 This can also be configured on a per-file basis by adding one of
535 the following lines anywhere in the buffer:
536 #+STARTUP: inlineimages
537 #+STARTUP: noinlineimages"
542 (defcustom org-insert-mode-line-in-empty-file nil
543 "Non-nil means insert the first line setting Org-mode in empty files.
544 When the function `org-mode' is called interactively in an empty file, this
545 normally means that the file name does not automatically trigger Org-mode.
546 To ensure that the file will always be in Org-mode in the future, a
547 line enforcing Org-mode will be inserted into the buffer, if this option
552 (defcustom org-replace-disputed-keys nil
553 "Non-nil means use alternative key bindings for some keys.
554 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
555 These keys are also used by other packages like shift-selection-mode'
556 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
557 If you want to use Org-mode together with one of these other modes,
558 or more generally if you would like to move some Org-mode commands to
559 other keys, set this variable and configure the keys with the variable
562 This option is only relevant at load-time of Org-mode, and must be set
563 *before* org.el is loaded. Changing it requires a restart of Emacs to
568 (defcustom org-use-extra-keys nil
569 "Non-nil means use extra key sequence definitions for certain commands.
570 This happens automatically if you run XEmacs or if `window-system'
571 is nil. This variable lets you do the same manually. You must
572 set it before loading org.
574 Example: on Carbon Emacs 22 running graphically, with an external
575 keyboard on a Powerbook, the default way of setting M-left might
576 not work for either Alt or ESC. Setting this variable will make
581 (if (fboundp 'defvaralias
)
582 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
584 (defcustom org-disputed-keys
585 '(([(shift up
)] .
[(meta p
)])
586 ([(shift down
)] .
[(meta n
)])
587 ([(shift left
)] .
[(meta -
)])
588 ([(shift right
)] .
[(meta +)])
589 ([(control shift right
)] .
[(meta shift
+)])
590 ([(control shift left
)] .
[(meta shift -
)]))
591 "Keys for which Org-mode and other modes compete.
592 This is an alist, cars are the default keys, second element specifies
593 the alternative to use when `org-replace-disputed-keys' is t.
595 Keys can be specified in any syntax supported by `define-key'.
596 The value of this option takes effect only at Org-mode's startup,
597 therefore you'll have to restart Emacs to apply it after changing."
602 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
603 Or return the original if not disputed.
604 Also apply the translations defined in `org-xemacs-key-equivalents'."
605 (when org-replace-disputed-keys
606 (let* ((nkey (key-description key
))
607 (x (org-find-if (lambda (x)
608 (equal (key-description (car x
)) nkey
))
610 (setq key
(if x
(cdr x
) key
))))
611 (when (featurep 'xemacs
)
612 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
615 (defun org-find-if (predicate seq
)
618 (if (funcall predicate
(car seq
))
619 (throw 'exit
(car seq
))
622 (defun org-defkey (keymap key def
)
623 "Define a key, possibly translated, as returned by `org-key'."
624 (define-key keymap
(org-key key
) def
))
626 (defcustom org-ellipsis nil
627 "The ellipsis to use in the Org-mode outline.
628 When nil, just use the standard three dots. When a string, use that instead,
629 When a face, use the standard 3 dots, but with the specified face.
630 The change affects only Org-mode (which will then use its own display table).
631 Changing this requires executing `M-x org-mode' in a buffer to become
634 :type
'(choice (const :tag
"Default" nil
)
635 (face :tag
"Face" :value org-warning
)
636 (string :tag
"String" :value
"...#")))
638 (defvar org-display-table nil
639 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
641 (defgroup org-keywords nil
642 "Keywords in Org-mode."
646 (defcustom org-deadline-string
"DEADLINE:"
647 "String to mark deadline entries.
648 A deadline is this string, followed by a time stamp. Should be a word,
649 terminated by a colon. You can insert a schedule keyword and
650 a timestamp with \\[org-deadline].
651 Changes become only effective after restarting Emacs."
655 (defcustom org-scheduled-string
"SCHEDULED:"
656 "String to mark scheduled TODO entries.
657 A schedule is this string, followed by a time stamp. Should be a word,
658 terminated by a colon. You can insert a schedule keyword and
659 a timestamp with \\[org-schedule].
660 Changes become only effective after restarting Emacs."
664 (defcustom org-closed-string
"CLOSED:"
665 "String used as the prefix for timestamps logging closing a TODO entry."
669 (defcustom org-clock-string
"CLOCK:"
670 "String used as prefix for timestamps clocking work hours on an item."
674 (defconst org-planning-or-clock-line-re
(concat "^[ \t]*\\("
675 org-scheduled-string
"\\|"
676 org-deadline-string
"\\|"
677 org-closed-string
"\\|"
678 org-clock-string
"\\)")
679 "Matches a line with planning or clock info.")
681 (defcustom org-comment-string
"COMMENT"
682 "Entries starting with this keyword will never be exported.
683 An entry can be toggled between COMMENT and normal with
684 \\[org-toggle-comment].
685 Changes become only effective after restarting Emacs."
689 (defcustom org-quote-string
"QUOTE"
690 "Entries starting with this keyword will be exported in fixed-width font.
691 Quoting applies only to the text in the entry following the headline, and does
692 not extend beyond the next headline, even if that is lower level.
693 An entry can be toggled between QUOTE and normal with
694 \\[org-toggle-fixed-width-section]."
698 (defconst org-repeat-re
699 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
700 "Regular expression for specifying repeated events.
701 After a match, group 1 contains the repeat expression.")
703 (defgroup org-structure nil
704 "Options concerning the general structure of Org-mode files."
708 (defgroup org-reveal-location nil
709 "Options about how to make context of a location visible."
710 :tag
"Org Reveal Location"
711 :group
'org-structure
)
713 (defconst org-context-choice
715 (const :tag
"Always" t
)
716 (const :tag
"Never" nil
)
717 (repeat :greedy t
:tag
"Individual contexts"
719 (choice :tag
"Context"
726 (const bookmark-jump
)
730 "Contexts for the reveal options.")
732 (defcustom org-show-hierarchy-above
'((default . t
))
733 "Non-nil means show full hierarchy when revealing a location.
734 Org-mode often shows locations in an org-mode file which might have
735 been invisible before. When this is set, the hierarchy of headings
736 above the exposed location is shown.
737 Turning this off for example for sparse trees makes them very compact.
738 Instead of t, this can also be an alist specifying this option for different
739 contexts. Valid contexts are
740 agenda when exposing an entry from the agenda
741 org-goto when using the command `org-goto' on key C-c C-j
742 occur-tree when using the command `org-occur' on key C-c /
743 tags-tree when constructing a sparse tree based on tags matches
744 link-search when exposing search matches associated with a link
745 mark-goto when exposing the jump goal of a mark
746 bookmark-jump when exposing a bookmark location
747 isearch when exiting from an incremental search
748 default default for all contexts not set explicitly"
749 :group
'org-reveal-location
750 :type org-context-choice
)
752 (defcustom org-show-following-heading
'((default . nil
))
753 "Non-nil means show following heading when revealing a location.
754 Org-mode often shows locations in an org-mode file which might have
755 been invisible before. When this is set, the heading following the
757 Turning this off for example for sparse trees makes them very compact,
758 but makes it harder to edit the location of the match. In such a case,
759 use the command \\[org-reveal] to show more context.
760 Instead of t, this can also be an alist specifying this option for different
761 contexts. See `org-show-hierarchy-above' for valid contexts."
762 :group
'org-reveal-location
763 :type org-context-choice
)
765 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
766 "Non-nil means show all sibling heading when revealing a location.
767 Org-mode often shows locations in an org-mode file which might have
768 been invisible before. When this is set, the sibling of the current entry
769 heading are all made visible. If `org-show-hierarchy-above' is t,
770 the same happens on each level of the hierarchy above the current entry.
772 By default this is on for the isearch context, off for all other contexts.
773 Turning this off for example for sparse trees makes them very compact,
774 but makes it harder to edit the location of the match. In such a case,
775 use the command \\[org-reveal] to show more context.
776 Instead of t, this can also be an alist specifying this option for different
777 contexts. See `org-show-hierarchy-above' for valid contexts."
778 :group
'org-reveal-location
779 :type org-context-choice
)
781 (defcustom org-show-entry-below
'((default . nil
))
782 "Non-nil means show the entry below a headline when revealing a location.
783 Org-mode often shows locations in an org-mode file which might have
784 been invisible before. When this is set, the text below the headline that is
785 exposed is also shown.
787 By default this is off for all contexts.
788 Instead of t, this can also be an alist specifying this option for different
789 contexts. See `org-show-hierarchy-above' for valid contexts."
790 :group
'org-reveal-location
791 :type org-context-choice
)
793 (defcustom org-indirect-buffer-display
'other-window
794 "How should indirect tree buffers be displayed?
795 This applies to indirect buffers created with the commands
796 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
798 current-window Display in the current window
799 other-window Just display in another window.
800 dedicated-frame Create one new frame, and re-use it each time.
801 new-frame Make a new frame each time. Note that in this case
802 previously-made indirect buffers are kept, and you need to
803 kill these buffers yourself."
804 :group
'org-structure
805 :group
'org-agenda-windows
807 (const :tag
"In current window" current-window
)
808 (const :tag
"In current frame, other window" other-window
)
809 (const :tag
"Each time a new frame" new-frame
)
810 (const :tag
"One dedicated frame" dedicated-frame
)))
812 (defcustom org-use-speed-commands nil
813 "Non-nil means activate single letter commands at beginning of a headline.
814 This may also be a function to test for appropriate locations where speed
815 commands should be active."
816 :group
'org-structure
818 (const :tag
"Never" nil
)
819 (const :tag
"At beginning of headline stars" t
)
822 (defcustom org-speed-commands-user nil
823 "Alist of additional speed commands.
824 This list will be checked before `org-speed-commands-default'
825 when the variable `org-use-speed-commands' is non-nil
826 and when the cursor is at the beginning of a headline.
827 The car if each entry is a string with a single letter, which must
828 be assigned to `self-insert-command' in the global map.
829 The cdr is either a command to be called interactively, a function
830 to be called, or a form to be evaluated.
831 An entry that is just a list with a single string will be interpreted
832 as a descriptive headline that will be added when listing the speed
833 commands in the Help buffer using the `?' speed command."
834 :group
'org-structure
835 :type
'(repeat :value
("k" . ignore
)
836 (choice :value
("k" . ignore
)
837 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
838 (cons :tag
"Letter and Command"
839 (string :tag
"Command letter")
844 (defgroup org-cycle nil
845 "Options concerning visibility cycling in Org-mode."
847 :group
'org-structure
)
849 (defcustom org-cycle-skip-children-state-if-no-children t
850 "Non-nil means skip CHILDREN state in entries that don't have any."
854 (defcustom org-cycle-max-level nil
855 "Maximum level which should still be subject to visibility cycling.
856 Levels higher than this will, for cycling, be treated as text, not a headline.
857 When `org-odd-levels-only' is set, a value of N in this variable actually
858 means 2N-1 stars as the limiting headline.
859 When nil, cycle all levels.
860 Note that the limiting level of cycling is also influenced by
861 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
862 `org-inlinetask-min-level' is, cycling will be limited to levels one less
866 (const :tag
"No limit" nil
)
867 (integer :tag
"Maximum level")))
869 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
870 "Names of drawers. Drawers are not opened by cycling on the headline above.
871 Drawers only open with a TAB on the drawer line itself. A drawer looks like
876 The drawer \"PROPERTIES\" is special for capturing properties through
879 Drawers can be defined on the per-file basis with a line like:
881 #+DRAWERS: HIDDEN STATE PROPERTIES"
882 :group
'org-structure
884 :type
'(repeat (string :tag
"Drawer Name")))
886 (defcustom org-hide-block-startup nil
887 "Non-nil means entering Org-mode will fold all blocks.
888 This can also be set in on a per-file basis with
890 #+STARTUP: hideblocks
891 #+STARTUP: showblocks"
896 (defcustom org-cycle-global-at-bob nil
897 "Cycle globally if cursor is at beginning of buffer and not at a headline.
898 This makes it possible to do global cycling without having to use S-TAB or
899 \\[universal-argument] TAB. For this special case to work, the first line \
901 must not be a headline - it may be empty or some other text. When used in
902 this way, `org-cycle-hook' is disables temporarily, to make sure the
903 cursor stays at the beginning of the buffer.
904 When this option is nil, don't do anything special at the beginning
909 (defcustom org-cycle-level-after-item
/entry-creation t
910 "Non-nil means cycle entry level or item indentation in new empty entries.
912 When the cursor is at the end of an empty headline, i.e with only stars
913 and maybe a TODO keyword, TAB will then switch the entry to become a child,
914 and then all possible ancestor states, before returning to the original state.
915 This makes data entry extremely fast: M-RET to create a new headline,
916 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
918 When the cursor is at the end of an empty plain list item, one TAB will
919 make it a subitem, two or more tabs will back up to make this an item
920 higher up in the item hierarchy."
924 (defcustom org-cycle-emulate-tab t
925 "Where should `org-cycle' emulate TAB.
927 white Only in completely white lines
928 whitestart Only at the beginning of lines, before the first non-white char
929 t Everywhere except in headlines
930 exc-hl-bol Everywhere except at the start of a headline
931 If TAB is used in a place where it does not emulate TAB, the current subtree
932 visibility is cycled."
934 :type
'(choice (const :tag
"Never" nil
)
935 (const :tag
"Only in completely white lines" white
)
936 (const :tag
"Before first char in a line" whitestart
)
937 (const :tag
"Everywhere except in headlines" t
)
938 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
941 (defcustom org-cycle-separator-lines
2
942 "Number of empty lines needed to keep an empty line between collapsed trees.
943 If you leave an empty line between the end of a subtree and the following
944 headline, this empty line is hidden when the subtree is folded.
945 Org-mode will leave (exactly) one empty line visible if the number of
946 empty lines is equal or larger to the number given in this variable.
947 So the default 2 means at least 2 empty lines after the end of a subtree
948 are needed to produce free space between a collapsed subtree and the
951 If the number is negative, and the number of empty lines is at least -N,
952 all empty lines are shown.
954 Special case: when 0, never leave empty lines in collapsed view."
957 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
959 (defcustom org-pre-cycle-hook nil
960 "Hook that is run before visibility cycling is happening.
961 The function(s) in this hook must accept a single argument which indicates
962 the new state that will be set right after running this hook. The
963 argument is a symbol. Before a global state change, it can have the values
964 `overview', `content', or `all'. Before a local state change, it can have
965 the values `folded', `children', or `subtree'."
969 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
970 org-cycle-hide-drawers
971 org-cycle-show-empty-lines
972 org-optimize-window-after-visibility-change
)
973 "Hook that is run after `org-cycle' has changed the buffer visibility.
974 The function(s) in this hook must accept a single argument which indicates
975 the new state that was set by the most recent `org-cycle' command. The
976 argument is a symbol. After a global state change, it can have the values
977 `overview', `content', or `all'. After a local state change, it can have
978 the values `folded', `children', or `subtree'."
982 (defgroup org-edit-structure nil
983 "Options concerning structure editing in Org-mode."
984 :tag
"Org Edit Structure"
985 :group
'org-structure
)
987 (defcustom org-odd-levels-only nil
988 "Non-nil means skip even levels and only use odd levels for the outline.
989 This has the effect that two stars are being added/taken away in
990 promotion/demotion commands. It also influences how levels are
991 handled by the exporters.
992 Changing it requires restart of `font-lock-mode' to become effective
993 for fontification also in regions already fontified.
994 You may also set this on a per-file basis by adding one of the following
999 :group
'org-edit-structure
1000 :group
'org-appearance
1003 (defcustom org-adapt-indentation t
1004 "Non-nil means adapt indentation to outline node level.
1006 When this variable is set, Org assumes that you write outlines by
1007 indenting text in each node to align with the headline (after the stars).
1008 The following issues are influenced by this variable:
1010 - When this is set and the *entire* text in an entry is indented, the
1011 indentation is increased by one space in a demotion command, and
1012 decreased by one in a promotion command. If any line in the entry
1013 body starts with text at column 0, indentation is not changed at all.
1015 - Property drawers and planning information is inserted indented when
1016 this variable s set. When nil, they will not be indented.
1018 - TAB indents a line relative to context. The lines below a headline
1019 will be indented when this variable is set.
1021 Note that this is all about true indentation, by adding and removing
1022 space characters. See also `org-indent.el' which does level-dependent
1023 indentation in a virtual way, i.e. at display time in Emacs."
1024 :group
'org-edit-structure
1027 (defcustom org-special-ctrl-a
/e nil
1028 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1030 When t, `C-a' will bring back the cursor to the beginning of the
1031 headline text, i.e. after the stars and after a possible TODO
1032 keyword. In an item, this will be the position after bullet and
1033 check-box, if any. When the cursor is already at that position,
1034 another `C-a' will bring it to the beginning of the line.
1036 `C-e' will jump to the end of the headline, ignoring the presence
1037 of tags in the headline. A second `C-e' will then jump to the
1038 true end of the line, after any tags. This also means that, when
1039 this variable is non-nil, `C-e' also will never jump beyond the
1040 end of the heading of a folded section, i.e. not after the
1043 When set to the symbol `reversed', the first `C-a' or `C-e' works
1044 normally, going to the true line boundary first. Only a directly
1045 following, identical keypress will bring the cursor to the
1048 This may also be a cons cell where the behavior for `C-a' and
1049 `C-e' is set separately."
1050 :group
'org-edit-structure
1052 (const :tag
"off" nil
)
1053 (const :tag
"on: after stars/bullet and before tags first" t
)
1054 (const :tag
"reversed: true line boundary first" reversed
)
1055 (cons :tag
"Set C-a and C-e separately"
1056 (choice :tag
"Special C-a"
1057 (const :tag
"off" nil
)
1058 (const :tag
"on: after stars/bullet first" t
)
1059 (const :tag
"reversed: before stars/bullet first" reversed
))
1060 (choice :tag
"Special C-e"
1061 (const :tag
"off" nil
)
1062 (const :tag
"on: before tags first" t
)
1063 (const :tag
"reversed: after tags first" reversed
)))))
1064 (if (fboundp 'defvaralias
)
1065 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
1067 (defcustom org-special-ctrl-k nil
1068 "Non-nil means `C-k' will behave specially in headlines.
1069 When nil, `C-k' will call the default `kill-line' command.
1070 When t, the following will happen while the cursor is in the headline:
1072 - When the cursor is at the beginning of a headline, kill the entire
1073 line and possible the folded subtree below the line.
1074 - When in the middle of the headline text, kill the headline up to the tags.
1075 - When after the headline text, kill the tags."
1076 :group
'org-edit-structure
1079 (defcustom org-ctrl-k-protect-subtree nil
1080 "Non-nil means, do not delete a hidden subtree with C-k.
1081 When set to the symbol `error', simply throw an error when C-k is
1082 used to kill (part-of) a headline that has hidden text behind it.
1083 Any other non-nil value will result in a query to the user, if it is
1084 OK to kill that hidden subtree. When nil, kill without remorse."
1085 :group
'org-edit-structure
1088 (const :tag
"Do not protect hidden subtrees" nil
)
1089 (const :tag
"Protect hidden subtrees with a security query" t
)
1090 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1092 (defcustom org-catch-invisible-edits nil
1093 "Check if in invisible region before inserting or deleting a character.
1096 nil Do not check, so just do invisible edits.
1097 error Throw an error and do nothing.
1098 show Make point visible, and do the requested edit.
1099 show-and-error Make point visible, then throw an error and abort the edit.
1100 smart Make point visible, and do insertion/deletion if it is
1101 adjacent to visible text and the change feels predictable.
1102 Never delete a previously invisible character or add in the
1103 middle or right after an invisible region. Basically, this
1104 allows insertion and backward-delete right before ellipses.
1105 FIXME: maybe in this case we should not even show?"
1106 :group
'org-edit-structure
1109 (const :tag
"Do not check" nil
)
1110 (const :tag
"Throw error when trying to edit" error
)
1111 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1112 (const :tag
"Show invisible part and do the edit" show
)
1113 (const :tag
"Be smart and do the right thing" smart
)))
1115 (defcustom org-yank-folded-subtrees t
1116 "Non-nil means when yanking subtrees, fold them.
1117 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1118 it starts with a heading and all other headings in it are either children
1119 or siblings, then fold all the subtrees. However, do this only if no
1120 text after the yank would be swallowed into a folded tree by this action."
1121 :group
'org-edit-structure
1124 (defcustom org-yank-adjusted-subtrees nil
1125 "Non-nil means when yanking subtrees, adjust the level.
1126 With this setting, `org-paste-subtree' is used to insert the subtree, see
1127 this function for details."
1128 :group
'org-edit-structure
1131 (defcustom org-M-RET-may-split-line
'((default . t
))
1132 "Non-nil means M-RET will split the line at the cursor position.
1133 When nil, it will go to the end of the line before making a
1135 You may also set this option in a different way for different
1136 contexts. Valid contexts are:
1138 headline when creating a new headline
1139 item when creating a new item
1140 table in a table field
1141 default the value to be used for all contexts not explicitly
1143 :group
'org-structure
1146 (const :tag
"Always" t
)
1147 (const :tag
"Never" nil
)
1148 (repeat :greedy t
:tag
"Individual contexts"
1150 (choice :tag
"Context"
1158 (defcustom org-insert-heading-respect-content nil
1159 "Non-nil means insert new headings after the current subtree.
1160 When nil, the new heading is created directly after the current line.
1161 The commands \\[org-insert-heading-respect-content] and
1162 \\[org-insert-todo-heading-respect-content] turn this variable on
1163 for the duration of the command."
1164 :group
'org-structure
1167 (defcustom org-blank-before-new-entry
'((heading . auto
)
1168 (plain-list-item . auto
))
1169 "Should `org-insert-heading' leave a blank line before new heading/item?
1170 The value is an alist, with `heading' and `plain-list-item' as CAR,
1171 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1172 which case Org will look at the surrounding headings/items and try to
1173 make an intelligent decision whether to insert a blank line or not.
1175 For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
1176 set, the setting here is ignored and no empty line is inserted, to avoid
1177 breaking the list structure."
1178 :group
'org-edit-structure
1180 (cons (const heading
)
1181 (choice (const :tag
"Never" nil
)
1182 (const :tag
"Always" t
)
1183 (const :tag
"Auto" auto
)))
1184 (cons (const plain-list-item
)
1185 (choice (const :tag
"Never" nil
)
1186 (const :tag
"Always" t
)
1187 (const :tag
"Auto" auto
)))))
1189 (defcustom org-insert-heading-hook nil
1190 "Hook being run after inserting a new heading."
1191 :group
'org-edit-structure
1194 (defcustom org-enable-fixed-width-editor t
1195 "Non-nil means lines starting with \":\" are treated as fixed-width.
1196 This currently only means they are never auto-wrapped.
1197 When nil, such lines will be treated like ordinary lines.
1198 See also the QUOTE keyword."
1199 :group
'org-edit-structure
1202 (defcustom org-goto-auto-isearch t
1203 "Non-nil means typing characters in `org-goto' starts incremental search."
1204 :group
'org-edit-structure
1207 (defgroup org-sparse-trees nil
1208 "Options concerning sparse trees in Org-mode."
1209 :tag
"Org Sparse Trees"
1210 :group
'org-structure
)
1212 (defcustom org-highlight-sparse-tree-matches t
1213 "Non-nil means highlight all matches that define a sparse tree.
1214 The highlights will automatically disappear the next time the buffer is
1215 changed by an edit command."
1216 :group
'org-sparse-trees
1219 (defcustom org-remove-highlights-with-change t
1220 "Non-nil means any change to the buffer will remove temporary highlights.
1221 Such highlights are created by `org-occur' and `org-clock-display'.
1222 When nil, `C-c C-c needs to be used to get rid of the highlights.
1223 The highlights created by `org-preview-latex-fragment' always need
1224 `C-c C-c' to be removed."
1225 :group
'org-sparse-trees
1230 (defcustom org-occur-hook
'(org-first-headline-recenter)
1231 "Hook that is run after `org-occur' has constructed a sparse tree.
1232 This can be used to recenter the window to show as much of the structure
1234 :group
'org-sparse-trees
1237 (defgroup org-imenu-and-speedbar nil
1238 "Options concerning imenu and speedbar in Org-mode."
1239 :tag
"Org Imenu and Speedbar"
1240 :group
'org-structure
)
1242 (defcustom org-imenu-depth
2
1243 "The maximum level for Imenu access to Org-mode headlines.
1244 This also applied for speedbar access."
1245 :group
'org-imenu-and-speedbar
1248 (defgroup org-table nil
1249 "Options concerning tables in Org-mode."
1253 (defcustom org-enable-table-editor
'optimized
1254 "Non-nil means lines starting with \"|\" are handled by the table editor.
1255 When nil, such lines will be treated like ordinary lines.
1257 When equal to the symbol `optimized', the table editor will be optimized to
1259 - Automatic overwrite mode in front of whitespace in table fields.
1260 This makes the structure of the table stay in tact as long as the edited
1261 field does not exceed the column width.
1262 - Minimize the number of realigns. Normally, the table is aligned each time
1263 TAB or RET are pressed to move to another field. With optimization this
1264 happens only if changes to a field might have changed the column width.
1265 Optimization requires replacing the functions `self-insert-command',
1266 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1267 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1268 very good at guessing when a re-align will be necessary, but you can always
1269 force one with \\[org-ctrl-c-ctrl-c].
1271 If you would like to use the optimized version in Org-mode, but the
1272 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1274 This variable can be used to turn on and off the table editor during a session,
1275 but in order to toggle optimization, a restart is required.
1277 See also the variable `org-table-auto-blank-field'."
1280 (const :tag
"off" nil
)
1282 (const :tag
"on, optimized" optimized
)))
1284 (defcustom org-self-insert-cluster-for-undo
(version<= emacs-version
"24.1")
1285 "Non-nil means cluster self-insert commands for undo when possible.
1286 If this is set, then, like in the Emacs command loop, 20 consecutive
1287 characters will be undone together.
1288 This is configurable, because there is some impact on typing performance."
1292 (defcustom org-table-tab-recognizes-table.el t
1293 "Non-nil means TAB will automatically notice a table.el table.
1294 When it sees such a table, it moves point into it and - if necessary -
1295 calls `table-recognize-table'."
1296 :group
'org-table-editing
1299 (defgroup org-link nil
1300 "Options concerning links in Org-mode."
1304 (defvar org-link-abbrev-alist-local nil
1305 "Buffer-local version of `org-link-abbrev-alist', which see.
1306 The value of this is taken from the #+LINK lines.")
1307 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1309 (defcustom org-link-abbrev-alist nil
1310 "Alist of link abbreviations.
1311 The car of each element is a string, to be replaced at the start of a link.
1312 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1313 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1315 [[linkkey:tag][description]]
1317 The 'linkkey' must be a word word, starting with a letter, followed
1318 by letters, numbers, '-' or '_'.
1320 If REPLACE is a string, the tag will simply be appended to create the link.
1321 If the string contains \"%s\", the tag will be inserted there. If the string
1322 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1323 at that point (see the function `url-hexify-string'). If the string contains
1324 the specifier \"%(my-function)\", then the custom function `my-function' will
1325 be invoked: this function takes the tag as its only argument and must return
1328 REPLACE may also be a function that will be called with the tag as the
1329 only argument to create the link, which should be returned as a string.
1331 See the manual for examples."
1335 (string :tag
"Protocol")
1337 (string :tag
"Format")
1340 (defcustom org-descriptive-links t
1341 "Non-nil means Org will display descriptive links.
1342 E.g. [[http://orgmode.org][Org website]] will be displayed as
1343 \"Org Website\", hiding the link itself and just displaying its
1344 description. When set to `nil', Org will display the full links
1347 You can interactively set the value of this variable by calling
1348 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1352 (defcustom org-link-file-path-type
'adaptive
1353 "How the path name in file links should be stored.
1356 relative Relative to the current directory, i.e. the directory of the file
1357 into which the link is being inserted.
1358 absolute Absolute path, if possible with ~ for home directory.
1359 noabbrev Absolute path, no abbreviation of home directory.
1360 adaptive Use relative path for files in the current directory and sub-
1361 directories of it. For other files, use an absolute path."
1369 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1370 "Types of links that should be activated in Org-mode files.
1371 This is a list of symbols, each leading to the activation of a certain link
1372 type. In principle, it does not hurt to turn on most link types - there may
1373 be a small gain when turning off unused link types. The types are:
1375 bracket The recommended [[link][description]] or [[link]] links with hiding.
1376 angle Links in angular brackets that may contain whitespace like
1377 <bbdb:Carsten Dominik>.
1378 plain Plain links in normal text, no whitespace, like http://google.com.
1379 radio Text that is matched by a radio target, see manual for details.
1380 tag Tag settings in a headline (link to tag search).
1381 date Time stamps (link to calendar).
1382 footnote Footnote labels.
1384 Changing this variable requires a restart of Emacs to become effective."
1386 :type
'(set :greedy t
1387 (const :tag
"Double bracket links" bracket
)
1388 (const :tag
"Angular bracket links" angle
)
1389 (const :tag
"Plain text links" plain
)
1390 (const :tag
"Radio target matches" radio
)
1391 (const :tag
"Tags" tag
)
1392 (const :tag
"Timestamps" date
)
1393 (const :tag
"Footnotes" footnote
)))
1395 (defcustom org-make-link-description-function nil
1396 "Function to use to generate link descriptions from links.
1397 If nil the link location will be used. This function must take
1398 two parameters; the first is the link and the second the
1399 description `org-insert-link' has generated, and should return the
1400 description to use."
1404 (defgroup org-link-store nil
1405 "Options concerning storing links in Org-mode."
1406 :tag
"Org Store Link"
1409 (defcustom org-url-hexify-p t
1410 "When non-nil, hexify URL when creating a link."
1413 :group
'org-link-store
)
1415 (defcustom org-email-link-description-format
"Email %c: %.30s"
1416 "Format of the description part of a link to an email or usenet message.
1417 The following %-escapes will be replaced by corresponding information:
1419 %F full \"From\" field
1420 %f name, taken from \"From\" field, address if no name
1421 %T full \"To\" field
1422 %t first name in \"To\" field, address if no name
1423 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1424 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1429 You may use normal field width specification between the % and the letter.
1430 This is for example useful to limit the length of the subject.
1432 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1433 :group
'org-link-store
1436 (defcustom org-from-is-user-regexp
1438 (when (and user-mail-address
(not (string= user-mail-address
"")))
1439 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1440 (when (and user-full-name
(not (string= user-full-name
"")))
1441 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1442 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1443 "Regexp matched against the \"From:\" header of an email or usenet message.
1444 It should match if the message is from the user him/herself."
1445 :group
'org-link-store
1448 (defcustom org-link-to-org-use-id
'create-if-interactive-and-no-custom-id
1449 "Non-nil means storing a link to an Org file will use entry IDs.
1451 Note that before this variable is even considered, org-id must be loaded,
1452 so please customize `org-modules' and turn it on.
1454 The variable can have the following values:
1456 t Create an ID if needed to make a link to the current entry.
1458 create-if-interactive
1459 If `org-store-link' is called directly (interactively, as a user
1460 command), do create an ID to support the link. But when doing the
1461 job for remember, only use the ID if it already exists. The
1462 purpose of this setting is to avoid proliferation of unwanted
1463 IDs, just because you happen to be in an Org file when you
1464 call `org-capture' that automatically and preemptively creates a
1465 link. If you do want to get an ID link in a remember template to
1466 an entry not having an ID, create it first by explicitly creating
1467 a link to it, using `C-c C-l' first.
1469 create-if-interactive-and-no-custom-id
1470 Like create-if-interactive, but do not create an ID if there is
1471 a CUSTOM_ID property defined in the entry. This is the default.
1474 Use existing ID, do not create one.
1476 nil Never use an ID to make a link, instead link using a text search for
1478 :group
'org-link-store
1480 (const :tag
"Create ID to make link" t
)
1481 (const :tag
"Create if storing link interactively"
1482 create-if-interactive
)
1483 (const :tag
"Create if storing link interactively and no CUSTOM_ID is present"
1484 create-if-interactive-and-no-custom-id
)
1485 (const :tag
"Only use existing" use-existing
)
1486 (const :tag
"Do not use ID to create link" nil
)))
1488 (defcustom org-context-in-file-links t
1489 "Non-nil means file links from `org-store-link' contain context.
1490 A search string will be added to the file name with :: as separator and
1491 used to find the context when the link is activated by the command
1492 `org-open-at-point'. When this option is t, the entire active region
1493 will be placed in the search string of the file link. If set to a
1494 positive integer, only the first n lines of context will be stored.
1496 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1497 negates this setting for the duration of the command."
1498 :group
'org-link-store
1499 :type
'(choice boolean integer
))
1501 (defcustom org-keep-stored-link-after-insertion nil
1502 "Non-nil means keep link in list for entire session.
1504 The command `org-store-link' adds a link pointing to the current
1505 location to an internal list. These links accumulate during a session.
1506 The command `org-insert-link' can be used to insert links into any
1507 Org-mode file (offering completion for all stored links). When this
1508 option is nil, every link which has been inserted once using \\[org-insert-link]
1509 will be removed from the list, to make completing the unused links
1511 :group
'org-link-store
1514 (defgroup org-link-follow nil
1515 "Options concerning following links in Org-mode."
1516 :tag
"Org Follow Link"
1519 (defcustom org-link-translation-function nil
1520 "Function to translate links with different syntax to Org syntax.
1521 This can be used to translate links created for example by the Planner
1522 or emacs-wiki packages to Org syntax.
1523 The function must accept two parameters, a TYPE containing the link
1524 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1525 which is everything after the link protocol. It should return a cons
1526 with possibly modified values of type and path.
1527 Org contains a function for this, so if you set this variable to
1528 `org-translate-link-from-planner', you should be able follow many
1529 links created by planner."
1530 :group
'org-link-follow
1533 (defcustom org-follow-link-hook nil
1534 "Hook that is run after a link has been followed."
1535 :group
'org-link-follow
1538 (defcustom org-tab-follows-link nil
1539 "Non-nil means on links TAB will follow the link.
1540 Needs to be set before org.el is loaded.
1541 This really should not be used, it does not make sense, and the
1542 implementation is bad."
1543 :group
'org-link-follow
1546 (defcustom org-return-follows-link nil
1547 "Non-nil means on links RET will follow the link."
1548 :group
'org-link-follow
1551 (defcustom org-mouse-1-follows-link
1552 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1553 "Non-nil means mouse-1 on a link will follow the link.
1554 A longer mouse click will still set point. Does not work on XEmacs.
1555 Needs to be set before org.el is loaded."
1556 :group
'org-link-follow
1559 (defcustom org-mark-ring-length
4
1560 "Number of different positions to be recorded in the ring.
1561 Changing this requires a restart of Emacs to work correctly."
1562 :group
'org-link-follow
1565 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1566 "Non-nil means internal links in Org files must exactly match a headline.
1567 When nil, the link search tries to match a phrase with all words
1568 in the search text."
1569 :group
'org-link-follow
1572 (const :tag
"Use fuzzy text search" nil
)
1573 (const :tag
"Match only exact headline" t
)
1574 (const :tag
"Match exact headline or query to create it"
1577 (defcustom org-link-frame-setup
1578 '((vm . vm-visit-folder-other-frame
)
1579 (vm-imap . vm-visit-imap-folder-other-frame
)
1580 (gnus . org-gnus-no-new-news
)
1581 (file . find-file-other-window
)
1582 (wl . wl-other-frame
))
1583 "Setup the frame configuration for following links.
1584 When following a link with Emacs, it may often be useful to display
1585 this link in another window or frame. This variable can be used to
1586 set this up for the different types of links.
1589 `vm-visit-folder-other-window'
1590 `vm-visit-folder-other-frame'
1591 For Gnus, use any of
1594 `org-gnus-no-new-news'
1595 For FILE, use any of
1597 `find-file-other-window'
1598 `find-file-other-frame'
1599 For Wanderlust use any of
1602 For the calendar, use the variable `calendar-setup'.
1603 For BBDB, it is currently only possible to display the matches in
1605 :group
'org-link-follow
1609 (const vm-visit-folder
)
1610 (const vm-visit-folder-other-window
)
1611 (const vm-visit-folder-other-frame
)))
1615 (const gnus-other-frame
)
1616 (const org-gnus-no-new-news
)))
1620 (const find-file-other-window
)
1621 (const find-file-other-frame
)))
1625 (const wl-other-frame
)))))
1627 (defcustom org-display-internal-link-with-indirect-buffer nil
1628 "Non-nil means use indirect buffer to display infile links.
1629 Activating internal links (from one location in a file to another location
1630 in the same file) normally just jumps to the location. When the link is
1631 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1633 another window. When this option is set, the other window actually displays
1634 an indirect buffer clone of the current buffer, to avoid any visibility
1635 changes to the current buffer."
1636 :group
'org-link-follow
1639 (defcustom org-open-non-existing-files nil
1640 "Non-nil means `org-open-file' will open non-existing files.
1641 When nil, an error will be generated.
1642 This variable applies only to external applications because they
1643 might choke on non-existing files. If the link is to a file that
1644 will be opened in Emacs, the variable is ignored."
1645 :group
'org-link-follow
1648 (defcustom org-open-directory-means-index-dot-org nil
1649 "Non-nil means a link to a directory really means to index.org.
1650 When nil, following a directory link will run dired or open a finder/explorer
1651 window on that directory."
1652 :group
'org-link-follow
1655 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1656 "Function and arguments to call for following mailto links.
1657 This is a list with the first element being a Lisp function, and the
1658 remaining elements being arguments to the function. In string arguments,
1659 %a will be replaced by the address, and %s will be replaced by the subject
1660 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1661 :group
'org-link-follow
1663 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1664 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1665 (const :tag
"message-mail" (message-mail "%a" "%s"))
1666 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1668 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1669 "Non-nil means ask for confirmation before executing shell links.
1670 Shell links can be dangerous: just think about a link
1672 [[shell:rm -rf ~/*][Google Search]]
1674 This link would show up in your Org-mode document as \"Google Search\",
1675 but really it would remove your entire home directory.
1676 Therefore we advise against setting this variable to nil.
1677 Just change it to `y-or-n-p' if you want to confirm with a
1678 single keystroke rather than having to type \"yes\"."
1679 :group
'org-link-follow
1681 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1682 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1683 (const :tag
"no confirmation (dangerous)" nil
)))
1684 (put 'org-confirm-shell-link-function
1685 'safe-local-variable
1686 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1688 (defcustom org-confirm-shell-link-not-regexp
""
1689 "A regexp to skip confirmation for shell links."
1690 :group
'org-link-follow
1694 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1695 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1696 Elisp links can be dangerous: just think about a link
1698 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1700 This link would show up in your Org-mode document as \"Google Search\",
1701 but really it would remove your entire home directory.
1702 Therefore we advise against setting this variable to nil.
1703 Just change it to `y-or-n-p' if you want to confirm with a
1704 single keystroke rather than having to type \"yes\"."
1705 :group
'org-link-follow
1707 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1708 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1709 (const :tag
"no confirmation (dangerous)" nil
)))
1710 (put 'org-confirm-shell-link-function
1711 'safe-local-variable
1712 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1714 (defcustom org-confirm-elisp-link-not-regexp
""
1715 "A regexp to skip confirmation for Elisp links."
1716 :group
'org-link-follow
1720 (defconst org-file-apps-defaults-gnu
1724 "Default file applications on a UNIX or GNU/Linux system.
1725 See `org-file-apps'.")
1727 (defconst org-file-apps-defaults-macosx
1730 (system .
"open %s")
1732 ("eps.gz" .
"gv %s")
1734 ("fig" .
"xfig %s"))
1735 "Default file applications on a MacOS X system.
1736 The system \"open\" is known as a default, but we use X11 applications
1737 for some files for which the OS does not have a good default.
1738 See `org-file-apps'.")
1740 (defconst org-file-apps-defaults-windowsnt
1744 (list (if (featurep 'xemacs
)
1745 'mswindows-shell-execute
1749 (list (if (featurep 'xemacs
)
1750 'mswindows-shell-execute
1753 "Default file applications on a Windows NT system.
1754 The system \"open\" is used for most files.
1755 See `org-file-apps'.")
1757 (defcustom org-file-apps
1760 ("\\.mm\\'" . default
)
1761 ("\\.x?html?\\'" . default
)
1762 ("\\.pdf\\'" . default
)
1764 "External applications for opening `file:path' items in a document.
1765 Org-mode uses system defaults for different file types, but
1766 you can use this variable to set the application for a given file
1767 extension. The entries in this list are cons cells where the car identifies
1768 files and the cdr the corresponding command. Possible values for the
1770 \"string\" A string as a file identifier can be interpreted in different
1771 ways, depending on its contents:
1773 - Alphanumeric characters only:
1774 Match links with this file extension.
1775 Example: (\"pdf\" . \"evince %s\")
1776 to open PDFs with evince.
1778 - Regular expression: Match links where the
1779 filename matches the regexp. If you want to
1780 use groups here, use shy groups.
1782 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1783 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1784 to open *.html and *.xhtml with firefox.
1786 - Regular expression which contains (non-shy) groups:
1787 Match links where the whole link, including \"::\", and
1788 anything after that, matches the regexp.
1789 In a custom command string, %1, %2, etc. are replaced with
1790 the parts of the link that were matched by the groups.
1791 For backwards compatibility, if a command string is given
1792 that does not use any of the group matches, this case is
1793 handled identically to the second one (i.e. match against
1795 In a custom lisp form, you can access the group matches with
1796 (match-string n link).
1798 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1799 to open [[file:document.pdf::5]] with evince at page 5.
1801 `directory' Matches a directory
1802 `remote' Matches a remote file, accessible through tramp or efs.
1803 Remote files most likely should be visited through Emacs
1804 because external applications cannot handle such paths.
1805 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1806 so all files Emacs knows how to handle. Using this with
1807 command `emacs' will open most files in Emacs. Beware that this
1808 will also open html files inside Emacs, unless you add
1809 (\"html\" . default) to the list as well.
1810 t Default for files not matched by any of the other options.
1811 `system' The system command to open files, like `open' on Windows
1812 and Mac OS X, and mailcap under GNU/Linux. This is the command
1813 that will be selected if you call `C-c C-o' with a double
1814 \\[universal-argument] \\[universal-argument] prefix.
1816 Possible values for the command are:
1817 `emacs' The file will be visited by the current Emacs process.
1818 `default' Use the default application for this file type, which is the
1819 association for t in the list, most likely in the system-specific
1821 This can be used to overrule an unwanted setting in the
1822 system-specific variable.
1823 `system' Use the system command for opening files, like \"open\".
1824 This command is specified by the entry whose car is `system'.
1825 Most likely, the system-specific version of this variable
1826 does define this command, but you can overrule/replace it
1828 string A command to be executed by a shell; %s will be replaced
1829 by the path to the file.
1830 sexp A Lisp form which will be evaluated. The file path will
1831 be available in the Lisp variable `file'.
1832 For more examples, see the system specific constants
1833 `org-file-apps-defaults-macosx'
1834 `org-file-apps-defaults-windowsnt'
1835 `org-file-apps-defaults-gnu'."
1836 :group
'org-link-follow
1838 (cons (choice :value
""
1839 (string :tag
"Extension")
1840 (const :tag
"System command to open files" system
)
1841 (const :tag
"Default for unrecognized files" t
)
1842 (const :tag
"Remote file" remote
)
1843 (const :tag
"Links to a directory" directory
)
1844 (const :tag
"Any files that have Emacs modes"
1847 (const :tag
"Visit with Emacs" emacs
)
1848 (const :tag
"Use default" default
)
1849 (const :tag
"Use the system command" system
)
1850 (string :tag
"Command")
1851 (sexp :tag
"Lisp form")))))
1853 (defcustom org-doi-server-url
"http://dx.doi.org/"
1854 "The URL of the DOI server."
1857 :group
'org-link-follow
)
1859 (defgroup org-refile nil
1860 "Options concerning refiling entries in Org-mode."
1864 (defcustom org-directory
"~/org"
1865 "Directory with org files.
1866 This is just a default location to look for Org files. There is no need
1867 at all to put your files into this directory. It is only used in the
1868 following situations:
1870 1. When a remember template specifies a target file that is not an
1871 absolute path. The path will then be interpreted relative to
1873 2. When a remember note is filed away in an interactive way (when exiting the
1874 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1875 with `org-directory' as the default path."
1877 :group
'org-remember
1880 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1881 "Default target for storing notes.
1882 Used as a fall back file for org-remember.el and org-capture.el, for
1883 templates that do not specify a target file."
1885 :group
'org-remember
1887 (const :tag
"Default from remember-data-file" nil
)
1890 (defcustom org-goto-interface
'outline
1891 "The default interface to be used for `org-goto'.
1893 outline The interface shows an outline of the relevant file
1894 and the correct heading is found by moving through
1895 the outline or by searching with incremental search.
1896 outline-path-completion Headlines in the current buffer are offered via
1897 completion. This is the interface also used by
1898 the refile command."
1901 (const :tag
"Outline" outline
)
1902 (const :tag
"Outline-path-completion" outline-path-completion
)))
1904 (defcustom org-goto-max-level
5
1905 "Maximum target level when running `org-goto' with refile interface."
1909 (defcustom org-reverse-note-order nil
1910 "Non-nil means store new notes at the beginning of a file or entry.
1911 When nil, new notes will be filed to the end of a file or entry.
1912 This can also be a list with cons cells of regular expressions that
1913 are matched against file names, and values."
1914 :group
'org-remember
1917 (const :tag
"Reverse always" t
)
1918 (const :tag
"Reverse never" nil
)
1919 (repeat :tag
"By file name regexp"
1920 (cons regexp boolean
))))
1922 (defcustom org-log-refile nil
1923 "Information to record when a task is refiled.
1925 Possible values are:
1927 nil Don't add anything
1928 time Add a time stamp to the task
1929 note Prompt for a note and add it with template `org-log-note-headings'
1931 This option can also be set with on a per-file-basis with
1933 #+STARTUP: nologrefile
1934 #+STARTUP: logrefile
1935 #+STARTUP: lognoterefile
1937 You can have local logging settings for a subtree by setting the LOGGING
1938 property to one or more of these keywords.
1940 When bulk-refiling from the agenda, the value `note' is forbidden and
1941 will temporarily be changed to `time'."
1943 :group
'org-progress
1946 (const :tag
"No logging" nil
)
1947 (const :tag
"Record timestamp" time
)
1948 (const :tag
"Record timestamp with note." note
)))
1950 (defcustom org-refile-targets nil
1951 "Targets for refiling entries with \\[org-refile].
1952 This is a list of cons cells. Each cell contains:
1953 - a specification of the files to be considered, either a list of files,
1954 or a symbol whose function or variable value will be used to retrieve
1955 a file name or a list of file names. If you use `org-agenda-files' for
1956 that, all agenda files will be scanned for targets. Nil means consider
1957 headings in the current buffer.
1958 - A specification of how to find candidate refile targets. This may be
1960 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1961 This tag has to be present in all target headlines, inheritance will
1963 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1965 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1966 headlines that are refiling targets.
1967 - a cons cell (:level . N). Any headline of level N is considered a target.
1968 Note that, when `org-odd-levels-only' is set, level corresponds to
1969 order in hierarchy, not to the number of stars.
1970 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1971 Note that, when `org-odd-levels-only' is set, level corresponds to
1972 order in hierarchy, not to the number of stars.
1974 Each element of this list generates a set of possible targets.
1975 The union of these sets is presented (with completion) to
1976 the user by `org-refile'.
1978 You can set the variable `org-refile-target-verify-function' to a function
1979 to verify each headline found by the simple criteria above.
1981 When this variable is nil, all top-level headlines in the current buffer
1982 are used, equivalent to the value `((nil . (:level . 1))'."
1986 (choice :value org-agenda-files
1987 (const :tag
"All agenda files" org-agenda-files
)
1988 (const :tag
"Current buffer" nil
)
1989 (function) (variable) (file))
1990 (choice :tag
"Identify target headline by"
1991 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1992 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1993 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1994 (cons :tag
"Level number" (const :value
:level
) (integer))
1995 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1997 (defcustom org-refile-target-verify-function nil
1998 "Function to verify if the headline at point should be a refile target.
1999 The function will be called without arguments, with point at the
2000 beginning of the headline. It should return t and leave point
2001 where it is if the headline is a valid target for refiling.
2003 If the target should not be selected, the function must return nil.
2004 In addition to this, it may move point to a place from where the search
2005 should be continued. For example, the function may decide that the entire
2006 subtree of the current entry should be excluded and move point to the end
2011 (defcustom org-refile-use-cache nil
2012 "Non-nil means cache refile targets to speed up the process.
2013 The cache for a particular file will be updated automatically when
2014 the buffer has been killed, or when any of the marker used for flagging
2015 refile targets no longer points at a live buffer.
2016 If you have added new entries to a buffer that might themselves be targets,
2017 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
2018 find that easier, `C-u C-u C-u C-c C-w'."
2023 (defcustom org-refile-use-outline-path nil
2024 "Non-nil means provide refile targets as paths.
2025 So a level 3 headline will be available as level1/level2/level3.
2027 When the value is `file', also include the file name (without directory)
2028 into the path. In this case, you can also stop the completion after
2029 the file name, to get entries inserted as top level in the file.
2031 When `full-file-path', include the full file path."
2034 (const :tag
"Not" nil
)
2035 (const :tag
"Yes" t
)
2036 (const :tag
"Start with file name" file
)
2037 (const :tag
"Start with full file path" full-file-path
)))
2039 (defcustom org-outline-path-complete-in-steps t
2040 "Non-nil means complete the outline path in hierarchical steps.
2041 When Org-mode uses the refile interface to select an outline path
2042 \(see variable `org-refile-use-outline-path'), the completion of
2043 the path can be done is a single go, or if can be done in steps down
2044 the headline hierarchy. Going in steps is probably the best if you
2045 do not use a special completion package like `ido' or `icicles'.
2046 However, when using these packages, going in one step can be very
2047 fast, while still showing the whole path to the entry."
2051 (defcustom org-refile-allow-creating-parent-nodes nil
2052 "Non-nil means allow to create new nodes as refile targets.
2053 New nodes are then created by adding \"/new node name\" to the completion
2054 of an existing node. When the value of this variable is `confirm',
2055 new node creation must be confirmed by the user (recommended)
2056 When nil, the completion must match an existing entry.
2058 Note that, if the new heading is not seen by the criteria
2059 listed in `org-refile-targets', multiple instances of the same
2060 heading would be created by trying again to file under the new
2064 (const :tag
"Never" nil
)
2065 (const :tag
"Always" t
)
2066 (const :tag
"Prompt for confirmation" confirm
)))
2068 (defcustom org-refile-active-region-within-subtree nil
2069 "Non-nil means also refile active region within a subtree.
2071 By default `org-refile' doesn't allow refiling regions if they
2072 don't contain a set of subtrees, but it might be convenient to
2073 do so sometimes: in that case, the first line of the region is
2074 converted to a headline before refiling."
2079 (defgroup org-todo nil
2080 "Options concerning TODO items in Org-mode."
2084 (defgroup org-progress nil
2085 "Options concerning Progress logging in Org-mode."
2089 (defvar org-todo-interpretation-widgets
2090 '((:tag
"Sequence (cycling hits every state)" sequence
)
2091 (:tag
"Type (cycling directly to DONE)" type
))
2092 "The available interpretation symbols for customizing `org-todo-keywords'.
2093 Interested libraries should add to this list.")
2095 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2096 "List of TODO entry keyword sequences and their interpretation.
2097 \\<org-mode-map>This is a list of sequences.
2099 Each sequence starts with a symbol, either `sequence' or `type',
2100 indicating if the keywords should be interpreted as a sequence of
2101 action steps, or as different types of TODO items. The first
2102 keywords are states requiring action - these states will select a headline
2103 for inclusion into the global TODO list Org-mode produces. If one of
2104 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2105 signify that no further action is necessary. If \"|\" is not found,
2106 the last keyword is treated as the only DONE state of the sequence.
2108 The command \\[org-todo] cycles an entry through these states, and one
2109 additional state where no keyword is present. For details about this
2110 cycling, see the manual.
2112 TODO keywords and interpretation can also be set on a per-file basis with
2113 the special #+SEQ_TODO and #+TYP_TODO lines.
2115 Each keyword can optionally specify a character for fast state selection
2116 \(in combination with the variable `org-use-fast-todo-selection')
2117 and specifiers for state change logging, using the same syntax that
2118 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2119 the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2120 indicates to record a time stamp each time this state is selected.
2122 Each keyword may also specify if a timestamp or a note should be
2123 recorded when entering or leaving the state, by adding additional
2124 characters in the parenthesis after the keyword. This looks like this:
2125 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2126 record only the time of the state change. With X and Y being either
2127 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2128 Y when leaving the state if and only if the *target* state does not
2129 define X. You may omit any of the fast-selection key or X or /Y,
2130 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2132 For backward compatibility, this variable may also be just a list
2133 of keywords. In this case the interpretation (sequence or type) will be
2134 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2136 :group
'org-keywords
2138 (repeat :tag
"Old syntax, just keywords"
2139 (string :tag
"Keyword"))
2140 (repeat :tag
"New syntax"
2143 :tag
"Interpretation"
2144 ;;Quick and dirty way to see
2145 ;;`org-todo-interpretations'. This takes the
2146 ;;place of item arguments
2154 org-todo-interpretation-widgets
))
2157 (string :tag
"Keyword"))))))
2159 (defvar org-todo-keywords-1 nil
2160 "All TODO and DONE keywords active in a buffer.")
2161 (make-variable-buffer-local 'org-todo-keywords-1
)
2162 (defvar org-todo-keywords-for-agenda nil
)
2163 (defvar org-done-keywords-for-agenda nil
)
2164 (defvar org-drawers-for-agenda nil
)
2165 (defvar org-todo-keyword-alist-for-agenda nil
)
2166 (defvar org-tag-alist-for-agenda nil
)
2167 (defvar org-agenda-contributing-files nil
)
2168 (defvar org-not-done-keywords nil
)
2169 (make-variable-buffer-local 'org-not-done-keywords
)
2170 (defvar org-done-keywords nil
)
2171 (make-variable-buffer-local 'org-done-keywords
)
2172 (defvar org-todo-heads nil
)
2173 (make-variable-buffer-local 'org-todo-heads
)
2174 (defvar org-todo-sets nil
)
2175 (make-variable-buffer-local 'org-todo-sets
)
2176 (defvar org-todo-log-states nil
)
2177 (make-variable-buffer-local 'org-todo-log-states
)
2178 (defvar org-todo-kwd-alist nil
)
2179 (make-variable-buffer-local 'org-todo-kwd-alist
)
2180 (defvar org-todo-key-alist nil
)
2181 (make-variable-buffer-local 'org-todo-key-alist
)
2182 (defvar org-todo-key-trigger nil
)
2183 (make-variable-buffer-local 'org-todo-key-trigger
)
2185 (defcustom org-todo-interpretation
'sequence
2186 "Controls how TODO keywords are interpreted.
2187 This variable is in principle obsolete and is only used for
2188 backward compatibility, if the interpretation of todo keywords is
2189 not given already in `org-todo-keywords'. See that variable for
2192 :group
'org-keywords
2193 :type
'(choice (const sequence
)
2196 (defcustom org-use-fast-todo-selection t
2197 "Non-nil means use the fast todo selection scheme with C-c C-t.
2198 This variable describes if and under what circumstances the cycling
2199 mechanism for TODO keywords will be replaced by a single-key, direct
2202 When nil, fast selection is never used.
2204 When the symbol `prefix', it will be used when `org-todo' is called
2205 with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
2206 `C-u t' in an agenda buffer.
2208 When t, fast selection is used by default. In this case, the prefix
2209 argument forces cycling instead.
2211 In all cases, the special interface is only used if access keys have
2212 actually been assigned by the user, i.e. if keywords in the configuration
2213 are followed by a letter in parenthesis, like TODO(t)."
2216 (const :tag
"Never" nil
)
2217 (const :tag
"By default" t
)
2218 (const :tag
"Only with C-u C-c C-t" prefix
)))
2220 (defcustom org-provide-todo-statistics t
2221 "Non-nil means update todo statistics after insert and toggle.
2222 ALL-HEADLINES means update todo statistics by including headlines
2223 with no TODO keyword as well, counting them as not done.
2224 A list of TODO keywords means the same, but skip keywords that are
2227 When this is set, todo statistics is updated in the parent of the
2228 current entry each time a todo state is changed."
2231 (const :tag
"Yes, only for TODO entries" t
)
2232 (const :tag
"Yes, including all entries" 'all-headlines
)
2233 (repeat :tag
"Yes, for TODOs in this list"
2234 (string :tag
"TODO keyword"))
2235 (other :tag
"No TODO statistics" nil
)))
2237 (defcustom org-hierarchical-todo-statistics t
2238 "Non-nil means TODO statistics covers just direct children.
2239 When nil, all entries in the subtree are considered.
2240 This has only an effect if `org-provide-todo-statistics' is set.
2241 To set this to nil for only a single subtree, use a COOKIE_DATA
2242 property and include the word \"recursive\" into the value."
2246 (defcustom org-after-todo-state-change-hook nil
2247 "Hook which is run after the state of a TODO item was changed.
2248 The new state (a string with a TODO keyword, or nil) is available in the
2249 Lisp variable `org-state'."
2253 (defvar org-blocker-hook nil
2254 "Hook for functions that are allowed to block a state change.
2256 Each function gets as its single argument a property list, see
2257 `org-trigger-hook' for more information about this list.
2259 If any of the functions in this hook returns nil, the state change
2262 (defvar org-trigger-hook nil
2263 "Hook for functions that are triggered by a state change.
2265 Each function gets as its single argument a property list with at least
2266 the following elements:
2268 (:type type-of-change :position pos-at-entry-start
2269 :from old-state :to new-state)
2271 Depending on the type, more properties may be present.
2273 This mechanism is currently implemented for:
2277 :type todo-state-change
2278 :from previous state (keyword as a string), or nil, or a symbol
2279 'todo' or 'done', to indicate the general type of state.
2280 :to new state, like in :from")
2282 (defcustom org-enforce-todo-dependencies nil
2283 "Non-nil means undone TODO entries will block switching the parent to DONE.
2284 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2285 be blocked if any prior sibling is not yet done.
2286 Finally, if the parent is blocked because of ordered siblings of its own,
2287 the child will also be blocked."
2288 :set
(lambda (var val
)
2291 (add-hook 'org-blocker-hook
2292 'org-block-todo-from-children-or-siblings-or-parent
)
2293 (remove-hook 'org-blocker-hook
2294 'org-block-todo-from-children-or-siblings-or-parent
)))
2298 (defcustom org-enforce-todo-checkbox-dependencies nil
2299 "Non-nil means unchecked boxes will block switching the parent to DONE.
2300 When this is nil, checkboxes have no influence on switching TODO states.
2301 When non-nil, you first need to check off all check boxes before the TODO
2302 entry can be switched to DONE.
2303 This variable needs to be set before org.el is loaded, and you need to
2304 restart Emacs after a change to make the change effective. The only way
2305 to change is while Emacs is running is through the customize interface."
2306 :set
(lambda (var val
)
2309 (add-hook 'org-blocker-hook
2310 'org-block-todo-from-checkboxes
)
2311 (remove-hook 'org-blocker-hook
2312 'org-block-todo-from-checkboxes
)))
2316 (defcustom org-treat-insert-todo-heading-as-state-change nil
2317 "Non-nil means inserting a TODO heading is treated as state change.
2318 So when the command \\[org-insert-todo-heading] is used, state change
2319 logging will apply if appropriate. When nil, the new TODO item will
2320 be inserted directly, and no logging will take place."
2324 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2325 "Non-nil means switching TODO states with S-cursor counts as state change.
2326 This is the default behavior. However, setting this to nil allows a
2327 convenient way to select a TODO state and bypass any logging associated
2332 (defcustom org-todo-state-tags-triggers nil
2333 "Tag changes that should be triggered by TODO state changes.
2334 This is a list. Each entry is
2336 (state-change (tag . flag) .......)
2338 State-change can be a string with a state, and empty string to indicate the
2339 state that has no TODO keyword, or it can be one of the symbols `todo'
2340 or `done', meaning any not-done or done state, respectively."
2344 (cons (choice :tag
"When changing to"
2345 (const :tag
"Not-done state" todo
)
2346 (const :tag
"Done state" done
)
2347 (string :tag
"State"))
2349 (cons :tag
"Tag action"
2351 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2353 (defcustom org-log-done nil
2354 "Information to record when a task moves to the DONE state.
2356 Possible values are:
2358 nil Don't add anything, just change the keyword
2359 time Add a time stamp to the task
2360 note Prompt for a note and add it with template `org-log-note-headings'
2362 This option can also be set with on a per-file-basis with
2364 #+STARTUP: nologdone
2366 #+STARTUP: lognotedone
2368 You can have local logging settings for a subtree by setting the LOGGING
2369 property to one or more of these keywords."
2371 :group
'org-progress
2373 (const :tag
"No logging" nil
)
2374 (const :tag
"Record CLOSED timestamp" time
)
2375 (const :tag
"Record CLOSED timestamp with note." note
)))
2377 ;; Normalize old uses of org-log-done.
2379 ((eq org-log-done t
) (setq org-log-done
'time
))
2380 ((and (listp org-log-done
) (memq 'done org-log-done
))
2381 (setq org-log-done
'note
)))
2383 (defcustom org-log-reschedule nil
2384 "Information to record when the scheduling date of a tasks is modified.
2386 Possible values are:
2388 nil Don't add anything, just change the date
2389 time Add a time stamp to the task
2390 note Prompt for a note and add it with template `org-log-note-headings'
2392 This option can also be set with on a per-file-basis with
2394 #+STARTUP: nologreschedule
2395 #+STARTUP: logreschedule
2396 #+STARTUP: lognotereschedule"
2398 :group
'org-progress
2400 (const :tag
"No logging" nil
)
2401 (const :tag
"Record timestamp" time
)
2402 (const :tag
"Record timestamp with note." note
)))
2404 (defcustom org-log-redeadline nil
2405 "Information to record when the deadline date of a tasks is modified.
2407 Possible values are:
2409 nil Don't add anything, just change the date
2410 time Add a time stamp to the task
2411 note Prompt for a note and add it with template `org-log-note-headings'
2413 This option can also be set with on a per-file-basis with
2415 #+STARTUP: nologredeadline
2416 #+STARTUP: logredeadline
2417 #+STARTUP: lognoteredeadline
2419 You can have local logging settings for a subtree by setting the LOGGING
2420 property to one or more of these keywords."
2422 :group
'org-progress
2424 (const :tag
"No logging" nil
)
2425 (const :tag
"Record timestamp" time
)
2426 (const :tag
"Record timestamp with note." note
)))
2428 (defcustom org-log-note-clock-out nil
2429 "Non-nil means record a note when clocking out of an item.
2430 This can also be configured on a per-file basis by adding one of
2431 the following lines anywhere in the buffer:
2433 #+STARTUP: lognoteclock-out
2434 #+STARTUP: nolognoteclock-out"
2436 :group
'org-progress
2439 (defcustom org-log-done-with-time t
2440 "Non-nil means the CLOSED time stamp will contain date and time.
2441 When nil, only the date will be recorded."
2442 :group
'org-progress
2445 (defcustom org-log-note-headings
2446 '((done .
"CLOSING NOTE %t")
2447 (state .
"State %-12s from %-12S %t")
2448 (note .
"Note taken on %t")
2449 (reschedule .
"Rescheduled from %S on %t")
2450 (delschedule .
"Not scheduled, was %S on %t")
2451 (redeadline .
"New deadline from %S on %t")
2452 (deldeadline .
"Removed deadline, was %S on %t")
2453 (refile .
"Refiled on %t")
2455 "Headings for notes added to entries.
2456 The value is an alist, with the car being a symbol indicating the note
2457 context, and the cdr is the heading to be used. The heading may also be the
2459 %t in the heading will be replaced by a time stamp.
2460 %T will be an active time stamp instead the default inactive one
2461 %d will be replaced by a short-format time stamp.
2462 %D will be replaced by an active short-format time stamp.
2463 %s will be replaced by the new TODO state, in double quotes.
2464 %S will be replaced by the old TODO state, in double quotes.
2465 %u will be replaced by the user name.
2466 %U will be replaced by the full user name.
2468 In fact, it is not a good idea to change the `state' entry, because
2469 agenda log mode depends on the format of these entries."
2471 :group
'org-progress
2472 :type
'(list :greedy t
2473 (cons (const :tag
"Heading when closing an item" done
) string
)
2475 "Heading when changing todo state (todo sequence only)"
2477 (cons (const :tag
"Heading when just taking a note" note
) string
)
2478 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2479 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2480 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2481 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2482 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2483 (cons (const :tag
"Heading when refiling" refile
) string
)))
2485 (unless (assq 'note org-log-note-headings
)
2486 (push '(note .
"%t") org-log-note-headings
))
2488 (defcustom org-log-into-drawer nil
2489 "Non-nil means insert state change notes and time stamps into a drawer.
2490 When nil, state changes notes will be inserted after the headline and
2491 any scheduling and clock lines, but not inside a drawer.
2493 The value of this variable should be the name of the drawer to use.
2494 LOGBOOK is proposed as the default drawer for this purpose, you can
2495 also set this to a string to define the drawer of your choice.
2497 A value of t is also allowed, representing \"LOGBOOK\".
2499 If this variable is set, `org-log-state-notes-insert-after-drawers'
2502 You can set the property LOG_INTO_DRAWER to overrule this setting for
2505 :group
'org-progress
2507 (const :tag
"Not into a drawer" nil
)
2508 (const :tag
"LOGBOOK" t
)
2509 (string :tag
"Other")))
2511 (if (fboundp 'defvaralias
)
2512 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2514 (defun org-log-into-drawer ()
2515 "Return the value of `org-log-into-drawer', but let properties overrule.
2516 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2517 used instead of the default value."
2518 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit
)))
2520 ((or (not p
) (equal p
"nil")) org-log-into-drawer
)
2521 ((equal p
"t") "LOGBOOK")
2524 (defcustom org-log-state-notes-insert-after-drawers nil
2525 "Non-nil means insert state change notes after any drawers in entry.
2526 Only the drawers that *immediately* follow the headline and the
2527 deadline/scheduled line are skipped.
2528 When nil, insert notes right after the heading and perhaps the line
2529 with deadline/scheduling if present.
2531 This variable will have no effect if `org-log-into-drawer' is
2534 :group
'org-progress
2537 (defcustom org-log-states-order-reversed t
2538 "Non-nil means the latest state note will be directly after heading.
2539 When nil, the state change notes will be ordered according to time."
2541 :group
'org-progress
2544 (defcustom org-todo-repeat-to-state nil
2545 "The TODO state to which a repeater should return the repeating task.
2546 By default this is the first task in a TODO sequence, or the previous state
2547 in a TODO_TYP set. But you can specify another task here.
2548 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2551 :type
'(choice (const :tag
"Head of sequence" nil
)
2552 (string :tag
"Specific state")))
2554 (defcustom org-log-repeat
'time
2555 "Non-nil means record moving through the DONE state when triggering repeat.
2556 An auto-repeating task is immediately switched back to TODO when
2557 marked DONE. If you are not logging state changes (by adding \"@\"
2558 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2559 record a closing note, there will be no record of the task moving
2560 through DONE. This variable forces taking a note anyway.
2562 nil Don't force a record
2563 time Record a time stamp
2564 note Prompt for a note and add it with template `org-log-note-headings'
2566 This option can also be set with on a per-file-basis with
2568 #+STARTUP: nologrepeat
2569 #+STARTUP: logrepeat
2570 #+STARTUP: lognoterepeat
2572 You can have local logging settings for a subtree by setting the LOGGING
2573 property to one or more of these keywords."
2575 :group
'org-progress
2577 (const :tag
"Don't force a record" nil
)
2578 (const :tag
"Force recording the DONE state" time
)
2579 (const :tag
"Force recording a note with the DONE state" note
)))
2582 (defgroup org-priorities nil
2583 "Priorities in Org-mode."
2584 :tag
"Org Priorities"
2587 (defcustom org-enable-priority-commands t
2588 "Non-nil means priority commands are active.
2589 When nil, these commands will be disabled, so that you never accidentally
2591 :group
'org-priorities
2594 (defcustom org-highest-priority ?A
2595 "The highest priority of TODO items. A character like ?A, ?B etc.
2596 Must have a smaller ASCII number than `org-lowest-priority'."
2597 :group
'org-priorities
2600 (defcustom org-lowest-priority ?C
2601 "The lowest priority of TODO items. A character like ?A, ?B etc.
2602 Must have a larger ASCII number than `org-highest-priority'."
2603 :group
'org-priorities
2606 (defcustom org-default-priority ?B
2607 "The default priority of TODO items.
2608 This is the priority an item gets if no explicit priority is given.
2609 When starting to cycle on an empty priority the first step in the cycle
2610 depends on `org-priority-start-cycle-with-default'. The resulting first
2611 step priority must not exceed the range from `org-highest-priority' to
2612 `org-lowest-priority' which means that `org-default-priority' has to be
2613 in this range exclusive or inclusive the range boundaries. Else the
2614 first step refuses to set the default and the second will fall back
2615 to (depending on the command used) the highest or lowest priority."
2616 :group
'org-priorities
2619 (defcustom org-priority-start-cycle-with-default t
2620 "Non-nil means start with default priority when starting to cycle.
2621 When this is nil, the first step in the cycle will be (depending on the
2622 command used) one higher or lower than the default priority.
2623 See also `org-default-priority'."
2624 :group
'org-priorities
2627 (defcustom org-get-priority-function nil
2628 "Function to extract the priority from a string.
2629 The string is normally the headline. If this is nil Org computes the
2630 priority from the priority cookie like [#A] in the headline. It returns
2631 an integer, increasing by 1000 for each priority level.
2632 The user can set a different function here, which should take a string
2633 as an argument and return the numeric priority."
2634 :group
'org-priorities
2638 (defgroup org-time nil
2639 "Options concerning time stamps and deadlines in Org-mode."
2643 (defcustom org-insert-labeled-timestamps-at-point nil
2644 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2645 When nil, these labeled time stamps are forces into the second line of an
2646 entry, just after the headline. When scheduling from the global TODO list,
2647 the time stamp will always be forced into the second line."
2651 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2652 "Formats for `format-time-string' which are used for time stamps.
2653 It is not recommended to change this constant.")
2655 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2656 "Number of minutes to round time stamps to.
2657 These are two values, the first applies when first creating a time stamp.
2658 The second applies when changing it with the commands `S-up' and `S-down'.
2659 When changing the time stamp, this means that it will change in steps
2660 of N minutes, as given by the second value.
2662 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2663 numbers should be factors of 60, so for example 5, 10, 15.
2665 When this is larger than 1, you can still force an exact time stamp by using
2666 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2667 and by using a prefix arg to `S-up/down' to specify the exact number
2668 of minutes to shift."
2670 :get
#'(lambda (var) ; Make sure both elements are there
2671 (if (integerp (default-value var
))
2672 (list (default-value var
) 5)
2673 (default-value var
)))
2675 (integer :tag
"when inserting times")
2676 (integer :tag
"when modifying times")))
2678 ;; Normalize old customizations of this variable.
2679 (when (integerp org-time-stamp-rounding-minutes
)
2680 (setq org-time-stamp-rounding-minutes
2681 (list org-time-stamp-rounding-minutes
2682 org-time-stamp-rounding-minutes
)))
2684 (defcustom org-display-custom-times nil
2685 "Non-nil means overlay custom formats over all time stamps.
2686 The formats are defined through the variable `org-time-stamp-custom-formats'.
2687 To turn this on on a per-file basis, insert anywhere in the file:
2688 #+STARTUP: customtime"
2692 (make-variable-buffer-local 'org-display-custom-times
)
2694 (defcustom org-time-stamp-custom-formats
2695 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2696 "Custom formats for time stamps. See `format-time-string' for the syntax.
2697 These are overlaid over the default ISO format if the variable
2698 `org-display-custom-times' is set. Time like %H:%M should be at the
2699 end of the second format. The custom formats are also honored by export
2700 commands, if custom time display is turned on at the time of export."
2704 (defun org-time-stamp-format (&optional long inactive
)
2705 "Get the right format for a time string."
2706 (let ((f (if long
(cdr org-time-stamp-formats
)
2707 (car org-time-stamp-formats
))))
2709 (concat "[" (substring f
1 -
1) "]")
2712 (defcustom org-time-clocksum-format
"%d:%02d"
2713 "The format string used when creating CLOCKSUM lines.
2714 This is also used when org-mode generates a time duration."
2718 (defcustom org-time-clocksum-use-fractional nil
2719 "If non-nil, \\[org-clock-display] uses fractional times.
2720 org-mode generates a time duration."
2724 (defcustom org-time-clocksum-fractional-format
"%.2f"
2725 "The format string used when creating CLOCKSUM lines, or when
2726 org-mode generates a time duration."
2730 (defcustom org-deadline-warning-days
14
2731 "No. of days before expiration during which a deadline becomes active.
2732 This variable governs the display in sparse trees and in the agenda.
2733 When 0 or negative, it means use this number (the absolute value of it)
2734 even if a deadline has a different individual lead time specified.
2736 Custom commands can set this variable in the options section."
2738 :group
'org-agenda-daily
/weekly
2741 (defcustom org-read-date-prefer-future t
2742 "Non-nil means assume future for incomplete date input from user.
2743 This affects the following situations:
2744 1. The user gives a month but not a year.
2745 For example, if it is April and you enter \"feb 2\", this will be read
2746 as Feb 2, *next* year. \"May 5\", however, will be this year.
2747 2. The user gives a day, but no month.
2748 For example, if today is the 15th, and you enter \"3\", Org-mode will
2749 read this as the third of *next* month. However, if you enter \"17\",
2750 it will be considered as *this* month.
2752 If you set this variable to the symbol `time', then also the following
2755 3. If the user gives a time.
2756 If the time is before now, it will be interpreted as tomorrow.
2758 Currently none of this works for ISO week specifications.
2760 When this option is nil, the current day, month and year will always be
2763 See also `org-agenda-jump-prefer-future'."
2766 (const :tag
"Never" nil
)
2767 (const :tag
"Check month and day" t
)
2768 (const :tag
"Check month, day, and time" time
)))
2770 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
2771 "Should the agenda jump command prefer the future for incomplete dates?
2772 The default is to do the same as configured in `org-read-date-prefer-future'.
2773 But you can also set a deviating value here.
2774 This may t or nil, or the symbol `org-read-date-prefer-future'."
2779 (const :tag
"Use org-read-date-prefer-future"
2780 org-read-date-prefer-future
)
2781 (const :tag
"Never" nil
)
2782 (const :tag
"Always" t
)))
2784 (defcustom org-read-date-force-compatible-dates t
2785 "Should date/time prompt force dates that are guaranteed to work in Emacs?
2787 Depending on the system Emacs is running on, certain dates cannot
2788 be represented with the type used internally to represent time.
2789 Dates between 1970-1-1 and 2038-1-1 can always be represented
2790 correctly. Some systems allow for earlier dates, some for later,
2791 some for both. One way to find out it to insert any date into an
2792 Org buffer, putting the cursor on the year and hitting S-up and
2793 S-down to test the range.
2795 When this variable is set to t, the date/time prompt will not let
2796 you specify dates outside the 1970-2037 range, so it is certain that
2797 these dates will work in whatever version of Emacs you are
2798 running, and also that you can move a file from one Emacs implementation
2799 to another. WHenever Org is forcing the year for you, it will display
2802 When this variable is nil, Org will check if the date is
2803 representable in the specific Emacs implementation you are using.
2804 If not, it will force a year, usually the current year, and beep
2805 to remind you. Currently this setting is not recommended because
2806 the likelihood that you will open your Org files in an Emacs that
2807 has limited date range is not negligible.
2809 A workaround for this problem is to use diary sexp dates for time
2810 stamps outside of this range."
2815 (defcustom org-read-date-display-live t
2816 "Non-nil means display current interpretation of date prompt live.
2817 This display will be in an overlay, in the minibuffer."
2821 (defcustom org-read-date-popup-calendar t
2822 "Non-nil means pop up a calendar when prompting for a date.
2823 In the calendar, the date can be selected with mouse-1. However, the
2824 minibuffer will also be active, and you can simply enter the date as well.
2825 When nil, only the minibuffer will be available."
2828 (if (fboundp 'defvaralias
)
2829 (defvaralias 'org-popup-calendar-for-date-prompt
2830 'org-read-date-popup-calendar
))
2832 (defcustom org-read-date-minibuffer-setup-hook nil
2833 "Hook to be used to set up keys for the date/time interface.
2834 Add key definitions to `minibuffer-local-map', which will be a temporary
2839 (defcustom org-extend-today-until
0
2840 "The hour when your day really ends. Must be an integer.
2841 This has influence for the following applications:
2842 - When switching the agenda to \"today\". It it is still earlier than
2843 the time given here, the day recognized as TODAY is actually yesterday.
2844 - When a date is read from the user and it is still before the time given
2845 here, the current date and time will be assumed to be yesterday, 23:59.
2846 Also, timestamps inserted in remember templates follow this rule.
2848 IMPORTANT: This is a feature whose implementation is and likely will
2849 remain incomplete. Really, it is only here because past midnight seems to
2850 be the favorite working time of John Wiegley :-)"
2854 (defcustom org-use-effective-time nil
2855 "If non-nil, consider `org-extend-today-until' when creating timestamps.
2856 For example, if `org-extend-today-until' is 8, and it's 4am, then the
2857 \"effective time\" of any timestamps between midnight and 8am will be
2858 23:59 of the previous day."
2863 (defcustom org-edit-timestamp-down-means-later nil
2864 "Non-nil means S-down will increase the time in a time stamp.
2865 When nil, S-up will increase."
2869 (defcustom org-calendar-follow-timestamp-change t
2870 "Non-nil means make the calendar window follow timestamp changes.
2871 When a timestamp is modified and the calendar window is visible, it will be
2872 moved to the new date."
2876 (defgroup org-tags nil
2877 "Options concerning tags in Org-mode."
2881 (defcustom org-tag-alist nil
2882 "List of tags allowed in Org-mode files.
2883 When this list is nil, Org-mode will base TAG input on what is already in the
2885 The value of this variable is an alist, the car of each entry must be a
2886 keyword as a string, the cdr may be a character that is used to select
2887 that tag through the fast-tag-selection interface.
2888 See the manual for details."
2892 (cons (string :tag
"Tag name")
2893 (character :tag
"Access char"))
2894 (list :tag
"Start radio group"
2896 (option (string :tag
"Group description")))
2897 (list :tag
"End radio group"
2899 (option (string :tag
"Group description")))
2900 (const :tag
"New line" (:newline
)))))
2902 (defcustom org-tag-persistent-alist nil
2903 "List of tags that will always appear in all Org-mode files.
2904 This is in addition to any in buffer settings or customizations
2906 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2907 The value of this variable is an alist, the car of each entry must be a
2908 keyword as a string, the cdr may be a character that is used to select
2909 that tag through the fast-tag-selection interface.
2910 See the manual for details.
2911 To disable these tags on a per-file basis, insert anywhere in the file:
2916 (cons (string :tag
"Tag name")
2917 (character :tag
"Access char"))
2918 (const :tag
"Start radio group" (:startgroup
))
2919 (const :tag
"End radio group" (:endgroup
))
2920 (const :tag
"New line" (:newline
)))))
2922 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2923 "If non-nil, always offer completion for all tags of all agenda files.
2924 Instead of customizing this variable directly, you might want to
2925 set it locally for capture buffers, because there no list of
2926 tags in that file can be created dynamically (there are none).
2928 (add-hook 'org-capture-mode-hook
2930 (set (make-local-variable
2931 'org-complete-tags-always-offer-all-agenda-tags)
2937 (defvar org-file-tags nil
2938 "List of tags that can be inherited by all entries in the file.
2939 The tags will be inherited if the variable `org-use-tag-inheritance'
2940 says they should be.
2941 This variable is populated from #+FILETAGS lines.")
2943 (defcustom org-use-fast-tag-selection
'auto
2944 "Non-nil means use fast tag selection scheme.
2945 This is a special interface to select and deselect tags with single keys.
2946 When nil, fast selection is never used.
2947 When the symbol `auto', fast selection is used if and only if selection
2948 characters for tags have been configured, either through the variable
2949 `org-tag-alist' or through a #+TAGS line in the buffer.
2950 When t, fast selection is always used and selection keys are assigned
2951 automatically if necessary."
2954 (const :tag
"Always" t
)
2955 (const :tag
"Never" nil
)
2956 (const :tag
"When selection characters are configured" 'auto
)))
2958 (defcustom org-fast-tag-selection-single-key nil
2959 "Non-nil means fast tag selection exits after first change.
2960 When nil, you have to press RET to exit it.
2961 During fast tag selection, you can toggle this flag with `C-c'.
2962 This variable can also have the value `expert'. In this case, the window
2963 displaying the tags menu is not even shown, until you press C-c again."
2966 (const :tag
"No" nil
)
2967 (const :tag
"Yes" t
)
2968 (const :tag
"Expert" expert
)))
2970 (defvar org-fast-tag-selection-include-todo nil
2971 "Non-nil means fast tags selection interface will also offer TODO states.
2972 This is an undocumented feature, you should not rely on it.")
2974 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2975 "The column to which tags should be indented in a headline.
2976 If this number is positive, it specifies the column. If it is negative,
2977 it means that the tags should be flushright to that column. For example,
2978 -80 works well for a normal 80 character screen.
2979 When 0, place tags directly after headline text, with only one space in
2984 (defcustom org-auto-align-tags t
2985 "Non-nil keeps tags aligned when modifying headlines.
2986 Some operations (i.e. demoting) change the length of a headline and
2987 therefore shift the tags around. With this option turned on, after
2988 each such operation the tags are again aligned to `org-tags-column'."
2992 (defcustom org-use-tag-inheritance t
2993 "Non-nil means tags in levels apply also for sublevels.
2994 When nil, only the tags directly given in a specific line apply there.
2995 This may also be a list of tags that should be inherited, or a regexp that
2996 matches tags that should be inherited. Additional control is possible
2997 with the variable `org-tags-exclude-from-inheritance' which gives an
2998 explicit list of tags to be excluded from inheritance., even if the value of
2999 `org-use-tag-inheritance' would select it for inheritance.
3001 If this option is t, a match early-on in a tree can lead to a large
3002 number of matches in the subtree when constructing the agenda or creating
3003 a sparse tree. If you only want to see the first match in a tree during
3004 a search, check out the variable `org-tags-match-list-sublevels'."
3007 (const :tag
"Not" nil
)
3008 (const :tag
"Always" t
)
3009 (repeat :tag
"Specific tags" (string :tag
"Tag"))
3010 (regexp :tag
"Tags matched by regexp")))
3012 (defcustom org-tags-exclude-from-inheritance nil
3013 "List of tags that should never be inherited.
3014 This is a way to exclude a few tags from inheritance. For way to do
3015 the opposite, to actively allow inheritance for selected tags,
3016 see the variable `org-use-tag-inheritance'."
3018 :type
'(repeat (string :tag
"Tag")))
3020 (defun org-tag-inherit-p (tag)
3021 "Check if TAG is one that should be inherited."
3023 ((member tag org-tags-exclude-from-inheritance
) nil
)
3024 ((eq org-use-tag-inheritance t
) t
)
3025 ((not org-use-tag-inheritance
) nil
)
3026 ((stringp org-use-tag-inheritance
)
3027 (string-match org-use-tag-inheritance tag
))
3028 ((listp org-use-tag-inheritance
)
3029 (member tag org-use-tag-inheritance
))
3030 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3032 (defcustom org-tags-match-list-sublevels t
3033 "Non-nil means list also sublevels of headlines matching a search.
3034 This variable applies to tags/property searches, and also to stuck
3035 projects because this search is based on a tags match as well.
3037 When set to the symbol `indented', sublevels are indented with
3040 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3041 the sublevels of a headline matching a tag search often also match
3042 the same search. Listing all of them can create very long lists.
3043 Setting this variable to nil causes subtrees of a match to be skipped.
3045 This variable is semi-obsolete and probably should always be true. It
3046 is better to limit inheritance to certain tags using the variables
3047 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3050 (const :tag
"No, don't list them" nil
)
3051 (const :tag
"Yes, do list them" t
)
3052 (const :tag
"List them, indented with leading dots" indented
)))
3054 (defcustom org-tags-sort-function nil
3055 "When set, tags are sorted using this function as a comparator."
3058 (const :tag
"No sorting" nil
)
3059 (const :tag
"Alphabetical" string
<)
3060 (const :tag
"Reverse alphabetical" string
>)
3061 (function :tag
"Custom function" nil
)))
3063 (defvar org-tags-history nil
3064 "History of minibuffer reads for tags.")
3065 (defvar org-last-tags-completion-table nil
3066 "The last used completion table for tags.")
3067 (defvar org-after-tags-change-hook nil
3068 "Hook that is run after the tags in a line have changed.")
3070 (defgroup org-properties nil
3071 "Options concerning properties in Org-mode."
3072 :tag
"Org Properties"
3075 (defcustom org-property-format
"%-10s %s"
3076 "How property key/value pairs should be formatted by `indent-line'.
3077 When `indent-line' hits a property definition, it will format the line
3078 according to this format, mainly to make sure that the values are
3079 lined-up with respect to each other."
3080 :group
'org-properties
3083 (defcustom org-properties-postprocess-alist nil
3084 "Alist of properties and functions to adjust inserted values.
3085 Elements of this alist must be of the form
3087 ([string] [function])
3089 where [string] must be a property name and [function] must be a
3090 lambda expression: this lambda expression must take one argument,
3091 the value to adjust, and return the new value as a string.
3093 For example, this element will allow the property \"Remaining\"
3094 to be updated wrt the relation between the \"Effort\" property
3095 and the clock summary:
3097 ((\"Remaining\" (lambda(value)
3098 (let ((clocksum (org-clock-sum-current-item))
3099 (effort (org-duration-string-to-minutes
3100 (org-entry-get (point) \"Effort\"))))
3101 (org-minutes-to-hh:mm-string (- effort clocksum))))))"
3102 :group
'org-properties
3104 :type
'(alist :key-type
(string :tag
"Property")
3105 :value-type
(function :tag
"Function")))
3107 (defcustom org-use-property-inheritance nil
3108 "Non-nil means properties apply also for sublevels.
3110 This setting is chiefly used during property searches. Turning it on can
3111 cause significant overhead when doing a search, which is why it is not
3114 When nil, only the properties directly given in the current entry count.
3115 When t, every property is inherited. The value may also be a list of
3116 properties that should have inheritance, or a regular expression matching
3117 properties that should be inherited.
3119 However, note that some special properties use inheritance under special
3120 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3121 and the properties ending in \"_ALL\" when they are used as descriptor
3122 for valid values of a property.
3124 Note for programmers:
3125 When querying an entry with `org-entry-get', you can control if inheritance
3126 should be used. By default, `org-entry-get' looks only at the local
3127 properties. You can request inheritance by setting the inherit argument
3128 to t (to force inheritance) or to `selective' (to respect the setting
3130 :group
'org-properties
3132 (const :tag
"Not" nil
)
3133 (const :tag
"Always" t
)
3134 (repeat :tag
"Specific properties" (string :tag
"Property"))
3135 (regexp :tag
"Properties matched by regexp")))
3137 (defun org-property-inherit-p (property)
3138 "Check if PROPERTY is one that should be inherited."
3140 ((eq org-use-property-inheritance t
) t
)
3141 ((not org-use-property-inheritance
) nil
)
3142 ((stringp org-use-property-inheritance
)
3143 (string-match org-use-property-inheritance property
))
3144 ((listp org-use-property-inheritance
)
3145 (member property org-use-property-inheritance
))
3146 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3148 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3149 "The default column format, if no other format has been defined.
3150 This variable can be set on the per-file basis by inserting a line
3152 #+COLUMNS: %25ITEM ....."
3153 :group
'org-properties
3156 (defcustom org-columns-ellipses
".."
3157 "The ellipses to be used when a field in column view is truncated.
3158 When this is the empty string, as many characters as possible are shown,
3159 but then there will be no visual indication that the field has been truncated.
3160 When this is a string of length N, the last N characters of a truncated
3161 field are replaced by this string. If the column is narrower than the
3162 ellipses string, only part of the ellipses string will be shown."
3163 :group
'org-properties
3166 (defcustom org-columns-modify-value-for-display-function nil
3167 "Function that modifies values for display in column view.
3168 For example, it can be used to cut out a certain part from a time stamp.
3169 The function must take 2 arguments:
3171 column-title The title of the column (*not* the property name)
3172 value The value that should be modified.
3174 The function should return the value that should be displayed,
3175 or nil if the normal value should be used."
3176 :group
'org-properties
3179 (defcustom org-effort-property
"Effort"
3180 "The property that is being used to keep track of effort estimates.
3181 Effort estimates given in this property need to have the format H:MM."
3182 :group
'org-properties
3183 :group
'org-progress
3184 :type
'(string :tag
"Property"))
3186 (defconst org-global-properties-fixed
3187 '(("VISIBILITY_ALL" .
"folded children content all")
3188 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3189 "List of property/value pairs that can be inherited by any entry.
3191 These are fixed values, for the preset properties. The user variable
3192 that can be used to add to this list is `org-global-properties'.
3194 The entries in this list are cons cells where the car is a property
3195 name and cdr is a string with the value. If the value represents
3196 multiple items like an \"_ALL\" property, separate the items by
3199 (defcustom org-global-properties nil
3200 "List of property/value pairs that can be inherited by any entry.
3202 This list will be combined with the constant `org-global-properties-fixed'.
3204 The entries in this list are cons cells where the car is a property
3205 name and cdr is a string with the value.
3207 You can set buffer-local values for the same purpose in the variable
3208 `org-file-properties' this by adding lines like
3210 #+PROPERTY: NAME VALUE"
3211 :group
'org-properties
3213 (cons (string :tag
"Property")
3214 (string :tag
"Value"))))
3216 (defvar org-file-properties nil
3217 "List of property/value pairs that can be inherited by any entry.
3218 Valid for the current buffer.
3219 This variable is populated from #+PROPERTY lines.")
3220 (make-variable-buffer-local 'org-file-properties
)
3222 (defgroup org-agenda nil
3223 "Options concerning agenda views in Org-mode."
3227 (defvar org-category nil
3228 "Variable used by org files to set a category for agenda display.
3229 Such files should use a file variable to set it, for example
3231 # -*- mode: org; org-category: \"ELisp\"
3233 or contain a special line
3237 If the file does not specify a category, then file's base name
3239 (make-variable-buffer-local 'org-category
)
3240 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3242 (defcustom org-agenda-files nil
3243 "The files to be used for agenda display.
3244 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3245 \\[org-remove-file]. You can also use customize to edit the list.
3247 If an entry is a directory, all files in that directory that are matched by
3248 `org-agenda-file-regexp' will be part of the file list.
3250 If the value of the variable is not a list but a single file name, then
3251 the list of agenda files is actually stored and maintained in that file, one
3252 agenda file per line. In this file paths can be given relative to
3253 `org-directory'. Tilde expansion and environment variable substitution
3257 (repeat :tag
"List of files and directories" file
)
3258 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3260 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3261 "Regular expression to match files for `org-agenda-files'.
3262 If any element in the list in that variable contains a directory instead
3263 of a normal file, all files in that directory that are matched by this
3264 regular expression will be included."
3268 (defcustom org-agenda-text-search-extra-files nil
3269 "List of extra files to be searched by text search commands.
3270 These files will be search in addition to the agenda files by the
3271 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3272 Note that these files will only be searched for text search commands,
3273 not for the other agenda views like todo lists, tag searches or the weekly
3274 agenda. This variable is intended to list notes and possibly archive files
3275 that should also be searched by these two commands.
3276 In fact, if the first element in the list is the symbol `agenda-archives',
3277 than all archive files of all agenda files will be added to the search
3280 :type
'(set :greedy t
3281 (const :tag
"Agenda Archives" agenda-archives
)
3282 (repeat :inline t
(file))))
3284 (if (fboundp 'defvaralias
)
3285 (defvaralias 'org-agenda-multi-occur-extra-files
3286 'org-agenda-text-search-extra-files
))
3288 (defcustom org-agenda-skip-unavailable-files nil
3289 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3290 A nil value means to remove them, after a query, from the list."
3294 (defcustom org-calendar-to-agenda-key
[?c
]
3295 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3296 The command `org-calendar-goto-agenda' will be bound to this key. The
3297 default is the character `c' because then `c' can be used to switch back and
3298 forth between agenda and calendar."
3302 (defcustom org-calendar-agenda-action-key
[?k
]
3303 "The key to be installed in `calendar-mode-map' for agenda-action.
3304 The command `org-agenda-action' will be bound to this key. The
3305 default is the character `k' because we use the same key in the agenda."
3309 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3310 "The key to be installed in `calendar-mode-map' for adding diary entries.
3311 This option is irrelevant until `org-agenda-diary-file' has been configured
3312 to point to an Org-mode file. When that is the case, the command
3313 `org-agenda-diary-entry' will be bound to the key given here, by default
3314 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3315 if you want to continue doing this, you need to change this to a different
3320 (defcustom org-agenda-diary-file
'diary-file
3321 "File to which to add new entries with the `i' key in agenda and calendar.
3322 When this is the symbol `diary-file', the functionality in the Emacs
3323 calendar will be used to add entries to the `diary-file'. But when this
3324 points to a file, `org-agenda-diary-entry' will be used instead."
3327 (const :tag
"The standard Emacs diary file" diary-file
)
3328 (file :tag
"Special Org file diary entries")))
3330 (eval-after-load "calendar"
3332 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3333 'org-calendar-goto-agenda
)
3334 (org-defkey calendar-mode-map org-calendar-agenda-action-key
3336 (add-hook 'calendar-mode-hook
3338 (unless (eq org-agenda-diary-file
'diary-file
)
3339 (define-key calendar-mode-map
3340 org-calendar-insert-diary-entry-key
3341 'org-agenda-diary-entry
))))))
3343 (defgroup org-latex nil
3344 "Options for embedding LaTeX code into Org-mode."
3348 (defcustom org-format-latex-options
3349 '(:foreground default
:background default
:scale
1.0
3350 :html-foreground
"Black" :html-background
"Transparent"
3351 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3352 "Options for creating images from LaTeX fragments.
3353 This is a property list with the following properties:
3354 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3355 `default' means use the foreground of the default face.
3356 :background the background color, or \"Transparent\".
3357 `default' means use the background of the default face.
3358 :scale a scaling factor for the size of the images, to get more pixels
3359 :html-foreground, :html-background, :html-scale
3360 the same numbers for HTML export.
3361 :matchers a list indicating which matchers should be used to
3362 find LaTeX fragments. Valid members of this list are:
3363 \"begin\" find environments
3364 \"$1\" find single characters surrounded by $.$
3365 \"$\" find math expressions surrounded by $...$
3366 \"$$\" find math expressions surrounded by $$....$$
3367 \"\\(\" find math expressions surrounded by \\(...\\)
3368 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3372 (defcustom org-format-latex-signal-error t
3373 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3374 When nil, just push out a message."
3379 (defcustom org-latex-to-mathml-jar-file nil
3380 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3381 Use this to specify additional executable file say a jar file.
3383 When using MathToWeb as the converter, specify the full-path to
3384 your mathtoweb.jar file."
3388 (const :tag
"None" nil
)
3389 (file :tag
"JAR file" :must-match t
)))
3391 (defcustom org-latex-to-mathml-convert-command nil
3392 "Command to convert LaTeX fragments to MathML.
3393 Replace format-specifiers in the command as noted below and use
3394 `shell-command' to convert LaTeX to MathML.
3395 %j: Executable file in fully expanded form as specified by
3396 `org-latex-to-mathml-jar-file'.
3397 %I: Input LaTeX file in fully expanded form
3398 %o: Output MathML file
3399 This command is used by `org-create-math-formula'.
3401 When using MathToWeb as the converter, set this to
3402 \"java -jar %j -unicode -force -df %o %I\"."
3406 (const :tag
"None" nil
)
3407 (string :tag
"\nShell command")))
3409 (defcustom org-latex-create-formula-image-program
'dvipng
3410 "Program to convert LaTeX fragments with.
3412 dvipng Process the LaTeX fragments to dvi file, then convert
3413 dvi files to png files using dvipng.
3414 This will also include processing of non-math environments.
3415 imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
3416 to convert pdf files to png files"
3420 (const :tag
"dvipng" dvipng
)
3421 (const :tag
"imagemagick" imagemagick
)))
3423 (defcustom org-latex-preview-ltxpng-directory
"ltxpng/"
3424 "Path to store latex preview images. A relative path here creates many
3425 directories relative to the processed org files paths. An absolute path
3426 puts all preview images at the same place."
3431 (defun org-format-latex-mathml-available-p ()
3432 "Return t if `org-latex-to-mathml-convert-command' is usable."
3434 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3435 org-latex-to-mathml-convert-command
)
3436 (let ((executable (car (split-string
3437 org-latex-to-mathml-convert-command
))))
3438 (when (executable-find executable
)
3440 "%j" org-latex-to-mathml-convert-command
)
3441 (file-readable-p org-latex-to-mathml-jar-file
)
3444 (defcustom org-format-latex-header
"\\documentclass{article}
3445 \\usepackage[usenames]{color}
3446 \\usepackage{amsmath}
3447 \\usepackage[mathscr]{eucal}
3448 \\pagestyle{empty} % do not remove
3451 % The settings below are copied from fullpage.sty
3452 \\setlength{\\textwidth}{\\paperwidth}
3453 \\addtolength{\\textwidth}{-3cm}
3454 \\setlength{\\oddsidemargin}{1.5cm}
3455 \\addtolength{\\oddsidemargin}{-2.54cm}
3456 \\setlength{\\evensidemargin}{\\oddsidemargin}
3457 \\setlength{\\textheight}{\\paperheight}
3458 \\addtolength{\\textheight}{-\\headheight}
3459 \\addtolength{\\textheight}{-\\headsep}
3460 \\addtolength{\\textheight}{-\\footskip}
3461 \\addtolength{\\textheight}{-3cm}
3462 \\setlength{\\topmargin}{1.5cm}
3463 \\addtolength{\\topmargin}{-2.54cm}"
3464 "The document header used for processing LaTeX fragments.
3465 It is imperative that this header make sure that no page number
3466 appears on the page. The package defined in the variables
3467 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
3468 will either replace the placeholder \"[PACKAGES]\" in this header, or they
3473 (defvar org-format-latex-header-extra nil
)
3475 (defun org-set-packages-alist (var val
)
3476 "Set the packages alist and make sure it has 3 elements per entry."
3477 (set var
(mapcar (lambda (x)
3478 (if (and (consp x
) (= (length x
) 2))
3479 (list (car x
) (nth 1 x
) t
)
3483 (defun org-get-packages-alist (var)
3485 "Get the packages alist and make sure it has 3 elements per entry."
3487 (if (and (consp x
) (= (length x
) 2))
3488 (list (car x
) (nth 1 x
) t
)
3490 (default-value var
)))
3492 ;; The following variables are defined here because is it also used
3493 ;; when formatting latex fragments. Originally it was part of the
3494 ;; LaTeX exporter, which is why the name includes "export".
3495 (defcustom org-export-latex-default-packages-alist
3496 '(("AUTO" "inputenc" t
)
3500 ("" "longtable" nil
)
3512 "Alist of default packages to be inserted in the header.
3513 Change this only if one of the packages here causes an incompatibility
3514 with another package you are using.
3515 The packages in this list are needed by one part or another of Org-mode
3516 to function properly.
3518 - inputenc, fontenc: for basic font and character selection
3519 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
3520 for interpreting the entities in `org-entities'. You can skip some of these
3521 packages if you don't use any of the symbols in it.
3522 - graphicx: for including images
3523 - float, wrapfig: for figure placement
3524 - longtable: for long tables
3525 - hyperref: for cross references
3527 Therefore you should not modify this variable unless you know what you
3528 are doing. The one reason to change it anyway is that you might be loading
3529 some other package that conflicts with one of the default packages.
3530 Each cell is of the format \( \"options\" \"package\" snippet-flag\).
3531 If SNIPPET-FLAG is t, the package also needs to be included when
3532 compiling LaTeX snippets into images for inclusion into HTML."
3533 :group
'org-export-latex
3534 :set
'org-set-packages-alist
3535 :get
'org-get-packages-alist
3539 (list :tag
"options/package pair"
3540 (string :tag
"options")
3541 (string :tag
"package")
3542 (boolean :tag
"Snippet"))
3543 (string :tag
"A line of LaTeX"))))
3545 (defcustom org-export-latex-packages-alist nil
3546 "Alist of packages to be inserted in every LaTeX header.
3547 These will be inserted after `org-export-latex-default-packages-alist'.
3548 Each cell is of the format \( \"options\" \"package\" snippet-flag \).
3549 SNIPPET-FLAG, when t, indicates that this package is also needed when
3550 turning LaTeX snippets into images for inclusion into HTML.
3551 Make sure that you only list packages here which:
3552 - you want in every file
3553 - do not conflict with the default packages in
3554 `org-export-latex-default-packages-alist'
3555 - do not conflict with the setup in `org-format-latex-header'."
3556 :group
'org-export-latex
3557 :set
'org-set-packages-alist
3558 :get
'org-get-packages-alist
3561 (list :tag
"options/package pair"
3562 (string :tag
"options")
3563 (string :tag
"package")
3564 (boolean :tag
"Snippet"))
3565 (string :tag
"A line of LaTeX"))))
3568 (defgroup org-appearance nil
3569 "Settings for Org-mode appearance."
3570 :tag
"Org Appearance"
3573 (defcustom org-level-color-stars-only nil
3574 "Non-nil means fontify only the stars in each headline.
3575 When nil, the entire headline is fontified.
3576 Changing it requires restart of `font-lock-mode' to become effective
3577 also in regions already fontified."
3578 :group
'org-appearance
3581 (defcustom org-hide-leading-stars nil
3582 "Non-nil means hide the first N-1 stars in a headline.
3583 This works by using the face `org-hide' for these stars. This
3584 face is white for a light background, and black for a dark
3585 background. You may have to customize the face `org-hide' to
3587 Changing it requires restart of `font-lock-mode' to become effective
3588 also in regions already fontified.
3589 You may also set this on a per-file basis by adding one of the following
3590 lines to the buffer:
3592 #+STARTUP: hidestars
3593 #+STARTUP: showstars"
3594 :group
'org-appearance
3597 (defcustom org-hidden-keywords nil
3598 "List of symbols corresponding to keywords to be hidden the org buffer.
3599 For example, a value '(title) for this list will make the document's title
3600 appear in the buffer without the initial #+TITLE: keyword."
3601 :group
'org-appearance
3603 :type
'(set (const :tag
"#+AUTHOR" author
)
3604 (const :tag
"#+DATE" date
)
3605 (const :tag
"#+EMAIL" email
)
3606 (const :tag
"#+TITLE" title
)))
3608 (defcustom org-custom-properties nil
3609 "List of properties (as strings) with a special meaning.
3610 The default use of these custom properties is to let the user
3611 hide them with `org-toggle-custom-properties-visibility'."
3612 :group
'org-properties
3613 :group
'org-appearance
3615 :type
'(repeat (string :tag
"Property Name")))
3617 (defcustom org-fontify-done-headline nil
3618 "Non-nil means change the face of a headline if it is marked DONE.
3619 Normally, only the TODO/DONE keyword indicates the state of a headline.
3620 When this is non-nil, the headline after the keyword is set to the
3621 `org-headline-done' as an additional indication."
3622 :group
'org-appearance
3625 (defcustom org-fontify-emphasized-text t
3626 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3627 Changing this variable requires a restart of Emacs to take effect."
3628 :group
'org-appearance
3631 (defcustom org-fontify-whole-heading-line nil
3632 "Non-nil means fontify the whole line for headings.
3633 This is useful when setting a background color for the
3635 :group
'org-appearance
3638 (defcustom org-highlight-latex-fragments-and-specials nil
3639 "Non-nil means fontify what is treated specially by the exporters."
3640 :group
'org-appearance
3643 (defcustom org-hide-emphasis-markers nil
3644 "Non-nil mean font-lock should hide the emphasis marker characters."
3645 :group
'org-appearance
3648 (defcustom org-pretty-entities nil
3649 "Non-nil means show entities as UTF8 characters.
3650 When nil, the \\name form remains in the buffer."
3651 :group
'org-appearance
3655 (defcustom org-pretty-entities-include-sub-superscripts t
3656 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3657 :group
'org-appearance
3661 (defvar org-emph-re nil
3662 "Regular expression for matching emphasis.
3663 After a match, the match groups contain these elements:
3664 0 The match of the full regular expression, including the characters
3665 before and after the proper match
3666 1 The character before the proper match, or empty at beginning of line
3667 2 The proper match, including the leading and trailing markers
3668 3 The leading marker like * or /, indicating the type of highlighting
3669 4 The text between the emphasis markers, not including the markers
3670 5 The character after the match, empty at the end of a line")
3671 (defvar org-verbatim-re nil
3672 "Regular expression for matching verbatim text.")
3673 (defvar org-emphasis-regexp-components
) ; defined just below
3674 (defvar org-emphasis-alist
) ; defined just below
3675 (defun org-set-emph-re (var val
)
3676 "Set variable and compute the emphasis regular expression."
3678 (when (and (boundp 'org-emphasis-alist
)
3679 (boundp 'org-emphasis-regexp-components
)
3680 org-emphasis-alist org-emphasis-regexp-components
)
3681 (let* ((e org-emphasis-regexp-components
)
3687 (body1 (concat body
"*?"))
3688 (markers (mapconcat 'car org-emphasis-alist
""))
3689 (vmarkers (mapconcat
3690 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3691 org-emphasis-alist
"")))
3692 ;; make sure special characters appear at the right position in the class
3693 (if (string-match "\\^" markers
)
3694 (setq markers
(concat (replace-match "" t t markers
) "^")))
3695 (if (string-match "-" markers
)
3696 (setq markers
(concat (replace-match "" t t markers
) "-")))
3697 (if (string-match "\\^" vmarkers
)
3698 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3699 (if (string-match "-" vmarkers
)
3700 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3702 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3703 (int-to-string nl
) "\\}")))
3706 (concat "\\([" pre
"]\\|^\\)"
3708 "\\([" markers
"]\\)"
3716 "\\([" post
"]\\|$\\)"))
3717 (setq org-verbatim-re
3718 (concat "\\([" pre
"]\\|^\\)"
3720 "\\([" vmarkers
"]\\)"
3728 "\\([" post
"]\\|$\\)")))))
3730 (defcustom org-emphasis-regexp-components
3731 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3732 "Components used to build the regular expression for emphasis.
3733 This is a list with five entries. Terminology: In an emphasis string
3734 like \" *strong word* \", we call the initial space PREMATCH, the final
3735 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3736 and \"trong wor\" is the body. The different components in this variable
3737 specify what is allowed/forbidden in each part:
3739 pre Chars allowed as prematch. Beginning of line will be allowed too.
3740 post Chars allowed as postmatch. End of line will be allowed too.
3741 border The chars *forbidden* as border characters.
3742 body-regexp A regexp like \".\" to match a body character. Don't use
3743 non-shy groups here, and don't allow newline here.
3744 newline The maximum number of newlines allowed in an emphasis exp.
3746 Use customize to modify this, or restart Emacs after changing it."
3747 :group
'org-appearance
3748 :set
'org-set-emph-re
3750 (sexp :tag
"Allowed chars in pre ")
3751 (sexp :tag
"Allowed chars in post ")
3752 (sexp :tag
"Forbidden chars in border ")
3753 (sexp :tag
"Regexp for body ")
3754 (integer :tag
"number of newlines allowed")
3755 (option (boolean :tag
"Please ignore this button"))))
3757 (defcustom org-emphasis-alist
3758 `(("*" bold
"<b>" "</b>")
3759 ("/" italic
"<i>" "</i>")
3760 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
3761 ("=" org-code
"<code>" "</code>" verbatim
)
3762 ("~" org-verbatim
"<code>" "</code>" verbatim
)
3763 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
3766 "Special syntax for emphasized text.
3767 Text starting and ending with a special character will be emphasized, for
3768 example *bold*, _underlined_ and /italic/. This variable sets the marker
3769 characters, the face to be used by font-lock for highlighting in Org-mode
3770 Emacs buffers, and the HTML tags to be used for this.
3771 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3772 For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
3773 Use customize to modify this, or restart Emacs after changing it."
3774 :group
'org-appearance
3775 :set
'org-set-emph-re
3778 (string :tag
"Marker character")
3780 (face :tag
"Font-lock-face")
3781 (plist :tag
"Face property list"))
3782 (string :tag
"HTML start tag")
3783 (string :tag
"HTML end tag")
3784 (option (const verbatim
)))))
3786 (defvar org-protecting-blocks
3787 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3788 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3789 This is needed for font-lock setup.")
3791 ;;; Miscellaneous options
3793 (defgroup org-completion nil
3794 "Completion in Org-mode."
3795 :tag
"Org Completion"
3798 (defcustom org-completion-use-ido nil
3799 "Non-nil means use ido completion wherever possible.
3800 Note that `ido-mode' must be active for this variable to be relevant.
3801 If you decide to turn this variable on, you might well want to turn off
3802 `org-outline-path-complete-in-steps'.
3803 See also `org-completion-use-iswitchb'."
3804 :group
'org-completion
3807 (defcustom org-completion-use-iswitchb nil
3808 "Non-nil means use iswitchb completion wherever possible.
3809 Note that `iswitchb-mode' must be active for this variable to be relevant.
3810 If you decide to turn this variable on, you might well want to turn off
3811 `org-outline-path-complete-in-steps'.
3812 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3813 :group
'org-completion
3816 (defcustom org-completion-fallback-command
'hippie-expand
3817 "The expansion command called by \\[pcomplete] in normal context.
3818 Normal means, no org-mode-specific context."
3819 :group
'org-completion
3822 ;;; Functions and variables from their packages
3823 ;; Declared here to avoid compiler warnings
3826 (defvar outline-mode-menu-heading
)
3827 (defvar outline-mode-menu-show
)
3828 (defvar outline-mode-menu-hide
)
3829 (defvar zmacs-regions
) ; XEmacs regions
3832 (defvar mark-active
)
3835 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
3836 (declare-function calendar-forward-day
"cal-move" (arg))
3837 (declare-function calendar-goto-date
"cal-move" (date))
3838 (declare-function calendar-goto-today
"cal-move" ())
3839 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
3840 (defvar calc-embedded-close-formula
)
3841 (defvar calc-embedded-open-formula
)
3842 (declare-function cdlatex-tab
"ext:cdlatex" ())
3843 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
3844 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
3845 (defvar font-lock-unfontify-region-function
)
3846 (declare-function iswitchb-read-buffer
"iswitchb"
3847 (prompt &optional default require-match start matches-set
))
3848 (defvar iswitchb-temp-buflist
)
3849 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
3850 (defvar org-agenda-tags-todo-honor-ignore-options
)
3851 (declare-function org-agenda-skip
"org-agenda" ())
3853 org-agenda-format-item
"org-agenda"
3854 (extra txt
&optional category tags dotime noprefix remove-re habitp
))
3855 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
3856 (declare-function org-agenda-change-all-lines
"org-agenda"
3857 (newhead hdmarker
&optional fixface just-this
))
3858 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
3859 (declare-function org-agenda-maybe-redo
"org-agenda" ())
3860 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
3862 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
3863 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3864 "org-agenda" (&optional end
))
3865 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
3866 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
3867 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
3868 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
3869 (declare-function org-indent-mode
"org-indent" (&optional arg
))
3870 (declare-function parse-time-string
"parse-time" (string))
3871 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
3872 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
3873 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
3874 (defvar remember-data-file
)
3875 (defvar texmathp-why
)
3876 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
3877 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
3879 (defvar w3m-current-url
)
3880 (defvar w3m-current-title
)
3882 (defvar org-latex-regexps
)
3884 ;;; Autoload and prepare some org modules
3886 ;; Some table stuff that needs to be defined here, because it is used
3887 ;; by the functions setting up org-mode or checking for table context.
3889 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
3890 "Detect an org-type or table-type table.")
3891 (defconst org-table-line-regexp
"^[ \t]*|"
3892 "Detect an org-type table line.")
3893 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
3894 "Detect an org-type table line.")
3895 (defconst org-table-hline-regexp
"^[ \t]*|-"
3896 "Detect an org-type table hline.")
3897 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
3898 "Detect a table-type table hline.")
3899 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
3900 "Detect the first line outside a table when searching from within it.
3901 This works for both table types.")
3903 ;; Autoload the functions in org-table.el that are needed by functions here.
3906 (org-autoload "org-table"
3907 '(org-table-align org-table-begin org-table-blank-field
3908 org-table-convert org-table-convert-region org-table-copy-down
3909 org-table-copy-region org-table-create
3910 org-table-create-or-convert-from-region
3911 org-table-create-with-table.el org-table-current-dline
3912 org-table-cut-region org-table-delete-column org-table-edit-field
3913 org-table-edit-formulas org-table-end org-table-eval-formula
3914 org-table-export org-table-field-info
3915 org-table-get-stored-formulas org-table-goto-column
3916 org-table-hline-and-move org-table-import org-table-insert-column
3917 org-table-insert-hline org-table-insert-row org-table-iterate
3918 org-table-justify-field-maybe org-table-kill-row
3919 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3920 org-table-move-column org-table-move-column-left
3921 org-table-move-column-right org-table-move-row
3922 org-table-move-row-down org-table-move-row-up
3923 org-table-next-field org-table-next-row org-table-paste-rectangle
3924 org-table-previous-field org-table-recalculate
3925 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3926 org-table-toggle-coordinate-overlays
3927 org-table-toggle-formula-debugger org-table-wrap-region
3928 orgtbl-mode turn-on-orgtbl org-table-to-lisp
3929 orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
3930 orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo
)))
3932 (defun org-at-table-p (&optional table-type
)
3933 "Return t if the cursor is inside an org-type table.
3934 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3935 (if org-enable-table-editor
3937 (beginning-of-line 1)
3938 (looking-at (if table-type org-table-any-line-regexp
3939 org-table-line-regexp
)))
3941 (defsubst org-table-p
() (org-at-table-p))
3943 (defun org-at-table.el-p
()
3944 "Return t if and only if we are at a table.el table."
3945 (and (org-at-table-p 'any
)
3947 (goto-char (org-table-begin 'any
))
3948 (looking-at org-table1-hline-regexp
))))
3949 (defun org-table-recognize-table.el
()
3950 "If there is a table.el table nearby, recognize it and move into it."
3951 (if org-table-tab-recognizes-table.el
3952 (if (org-at-table.el-p
)
3954 (beginning-of-line 1)
3955 (if (looking-at org-table-dataline-regexp
)
3957 (if (looking-at org-table1-hline-regexp
)
3959 (beginning-of-line 2)
3960 (if (looking-at org-table-any-border-regexp
)
3961 (beginning-of-line -
1)))))
3962 (if (re-search-forward "|" (org-table-end t
) t
)
3965 (if (table--at-cell-p (point))
3967 (message "recognizing table.el table...")
3968 (table-recognize-table)
3969 (message "recognizing table.el table...done")))
3970 (error "This should not happen"))
3975 (defun org-at-table-hline-p ()
3976 "Return t if the cursor is inside a hline in a table."
3977 (if org-enable-table-editor
3979 (beginning-of-line 1)
3980 (looking-at org-table-hline-regexp
))
3983 (defvar org-table-clean-did-remove-column nil
)
3985 (defun org-table-map-tables (function &optional quietly
)
3986 "Apply FUNCTION to the start of all tables in the buffer."
3990 (goto-char (point-min))
3991 (while (re-search-forward org-table-any-line-regexp nil t
)
3993 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
3994 (beginning-of-line 1)
3995 (when (and (looking-at org-table-line-regexp
)
3996 ;; Exclude tables in src/example/verbatim/clocktable blocks
3997 (not (org-in-block-p '("src" "example"))))
3998 (save-excursion (funcall function
))
3999 (or (looking-at org-table-line-regexp
)
4001 (re-search-forward org-table-any-border-regexp nil
1))))
4002 (unless quietly
(message "Mapping tables: done")))
4004 ;; Declare and autoload functions from org-exp.el & Co
4006 (declare-function org-default-export-plist
"org-exp")
4007 (declare-function org-infile-export-plist
"org-exp")
4008 (declare-function org-get-current-options
"org-exp")
4010 (org-autoload "org-exp"
4011 '(org-export org-export-visible
4012 org-insert-export-options-template
4013 org-table-clean-before-export
))
4014 (org-autoload "org-ascii"
4015 '(org-export-as-ascii org-export-ascii-preprocess
4016 org-export-as-ascii-to-buffer org-replace-region-by-ascii
4017 org-export-region-as-ascii
))
4018 (org-autoload "org-latex"
4019 '(org-export-as-latex-batch org-export-as-latex-to-buffer
4020 org-replace-region-by-latex org-export-region-as-latex
4021 org-export-as-latex org-export-as-pdf
4022 org-export-as-pdf-and-open
))
4023 (org-autoload "org-html"
4024 '(org-export-as-html-and-open
4025 org-export-as-html-batch org-export-as-html-to-buffer
4026 org-replace-region-by-html org-export-region-as-html
4027 org-export-as-html
))
4028 (org-autoload "org-docbook"
4029 '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
4030 org-replace-region-by-docbook org-export-region-as-docbook
4031 org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
4032 org-export-as-docbook
))
4033 (org-autoload "org-icalendar"
4034 '(org-export-icalendar-this-file
4035 org-export-icalendar-all-agenda-files
4036 org-export-icalendar-combine-agenda-files
))
4037 (org-autoload "org-xoxo" '(org-export-as-xoxo))
4038 (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning
)))
4040 ;; Declare and autoload functions from org-agenda.el
4043 (org-autoload "org-agenda"
4044 '(org-agenda org-agenda-list org-search-view
4045 org-todo-list org-tags-view org-agenda-list-stuck-projects
4046 org-diary org-agenda-to-appt
4047 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
)))
4049 ;; Autoload org-remember
4052 (org-autoload "org-remember"
4053 '(org-remember-insinuate org-remember-annotation
4054 org-remember-apply-template org-remember org-remember-handler
)))
4057 (org-autoload "org-capture"
4058 '(org-capture org-capture-insert-template-here
4059 org-capture-import-remember-templates
)))
4061 ;; Autoload org-clock.el
4063 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
4065 (declare-function org-clock-update-mode-line
"org-clock" ())
4066 (declare-function org-resolve-clocks
"org-clock"
4067 (&optional also-non-dangling-p prompt last-valid
))
4068 (defvar org-clock-start-time
)
4069 (defvar org-clock-marker
(make-marker)
4070 "Marker recording the last clock-in.")
4071 (defvar org-clock-hd-marker
(make-marker)
4072 "Marker recording the last clock-in, but the headline position.")
4073 (defvar org-clock-heading
""
4074 "The heading of the current clock entry.")
4075 (defun org-clock-is-active ()
4076 "Return non-nil if clock is currently running.
4077 The return value is actually the clock marker."
4078 (marker-buffer org-clock-marker
))
4083 '(org-clock-in org-clock-out org-clock-cancel
4084 org-clock-goto org-clock-sum org-clock-display
4085 org-clock-remove-overlays org-clock-report
4086 org-clocktable-shift org-dblock-write
:clocktable
4087 org-get-clocktable org-resolve-clocks
)))
4089 (defun org-clock-update-time-maybe ()
4090 "If this is a CLOCK line, update it and return t.
4091 Otherwise, return nil."
4094 (beginning-of-line 1)
4095 (skip-chars-forward " \t")
4096 (when (looking-at org-clock-string
)
4097 (let ((re (concat "[ \t]*" org-clock-string
4098 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
4099 "\\([ \t]*=>.*\\)?\\)?"))
4102 ((not (looking-at re
))
4104 ((not (match-end 2))
4105 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4106 (> org-clock-marker
(point))
4107 (<= org-clock-marker
(point-at-eol)))
4108 ;; The clock is running here
4109 (setq org-clock-start-time
4111 (org-parse-time-string (match-string 1))))
4112 (org-clock-update-mode-line)))
4114 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
4116 (setq ts
(match-string 1)
4117 te
(match-string 3))
4118 (setq s
(- (org-float-time
4119 (apply 'encode-time
(org-parse-time-string te
)))
4121 (apply 'encode-time
(org-parse-time-string ts
))))
4124 h
(floor (/ s
3600))
4128 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
4131 (defun org-check-running-clock ()
4132 "Check if the current buffer contains the running clock.
4133 If yes, offer to stop it and to save the buffer with the changes."
4134 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4135 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4138 (when (y-or-n-p "Save changed buffer?")
4141 (defun org-clocktable-try-shift (dir n
)
4142 "Check if this line starts a clock table, if yes, shift the time block."
4143 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4144 (org-clocktable-shift dir n
)))
4146 ;; Autoload org-timer.el
4151 '(org-timer-start org-timer org-timer-item
4152 org-timer-change-times-in-region
4154 org-timer-reset-timers
4155 org-timer-show-remaining-time
)))
4157 ;; Autoload org-feed.el
4162 '(org-feed-update org-feed-update-all org-feed-goto-inbox
)))
4165 ;; Autoload org-indent.el
4167 ;; Define the variable already here, to make sure we have it.
4168 (defvar org-indent-mode nil
4169 "Non-nil if Org-Indent mode is enabled.
4170 Use the command `org-indent-mode' to change this variable.")
4175 '(org-indent-mode)))
4177 ;; Autoload org-mobile.el
4182 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda
)))
4184 ;; Autoload archiving code
4185 ;; The stuff that is needed for cycling and tags has to be defined here.
4187 (defgroup org-archive nil
4188 "Options concerning archiving in Org-mode."
4190 :group
'org-structure
)
4192 (defcustom org-archive-location
"%s_archive::"
4193 "The location where subtrees should be archived.
4195 The value of this variable is a string, consisting of two parts,
4196 separated by a double-colon. The first part is a filename and
4197 the second part is a headline.
4199 When the filename is omitted, archiving happens in the same file.
4200 %s in the filename will be replaced by the current file
4201 name (without the directory part). Archiving to a different file
4202 is useful to keep archived entries from contributing to the
4205 The archived entries will be filed as subtrees of the specified
4206 headline. When the headline is omitted, the subtrees are simply
4207 filed away at the end of the file, as top-level entries. Also in
4208 the heading you can use %s to represent the file name, this can be
4209 useful when using the same archive for a number of different files.
4211 Here are a few examples:
4213 If the current file is Projects.org, archive in file
4214 Projects.org_archive, as top-level trees. This is the default.
4216 \"::* Archived Tasks\"
4217 Archive in the current file, under the top-level headline
4218 \"* Archived Tasks\".
4220 \"~/org/archive.org::\"
4221 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4223 \"~/org/archive.org::* From %s\"
4224 Archive in file ~/org/archive.org (absolute path), under headlines
4225 \"From FILENAME\" where file name is the current file name.
4227 \"~/org/datetree.org::datetree/* Finished Tasks\"
4228 The \"datetree/\" string is special, signifying to archive
4229 items to the datetree. Items are placed in either the CLOSED
4230 date of the item, or the current date if there is no CLOSED date.
4231 The heading will be a subentry to the current date. There doesn't
4232 need to be a heading, but there always needs to be a slash after
4233 datetree. For example, to store archived items directly in the
4234 datetree, use \"~/org/datetree.org::datetree/\".
4236 \"basement::** Finished Tasks\"
4237 Archive in file ./basement (relative path), as level 3 trees
4238 below the level 2 heading \"** Finished Tasks\".
4240 You may set this option on a per-file basis by adding to the buffer a
4243 #+ARCHIVE: basement::** Finished Tasks
4245 You may also define it locally for a subtree by setting an ARCHIVE property
4246 in the entry. If such a property is found in an entry, or anywhere up
4247 the hierarchy, it will be used."
4251 (defcustom org-archive-tag
"ARCHIVE"
4252 "The tag that marks a subtree as archived.
4253 An archived subtree does not open during visibility cycling, and does
4254 not contribute to the agenda listings.
4255 After changing this, font-lock must be restarted in the relevant buffers to
4256 get the proper fontification."
4258 :group
'org-keywords
4261 (defcustom org-agenda-skip-archived-trees t
4262 "Non-nil means the agenda will skip any items located in archived trees.
4263 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4264 variable is no longer recommended, you should leave it at the value t.
4265 Instead, use the key `v' to cycle the archives-mode in the agenda."
4267 :group
'org-agenda-skip
4270 (defcustom org-columns-skip-archived-trees t
4271 "Non-nil means ignore archived trees when creating column view."
4273 :group
'org-properties
4276 (defcustom org-cycle-open-archived-trees nil
4277 "Non-nil means `org-cycle' will open archived trees.
4278 An archived tree is a tree marked with the tag ARCHIVE.
4279 When nil, archived trees will stay folded. You can still open them with
4280 normal outline commands like `show-all', but not with the cycling commands."
4285 (defcustom org-sparse-tree-open-archived-trees nil
4286 "Non-nil means sparse tree construction shows matches in archived trees.
4287 When nil, matches in these trees are highlighted, but the trees are kept in
4290 :group
'org-sparse-trees
4293 (defcustom org-sparse-tree-default-date-type
'scheduled-or-deadline
4294 "The default date type when building a sparse tree.
4295 When this is nil, a date is a scheduled or a deadline timestamp.
4296 Otherwise, these types are allowed:
4299 active: only active timestamps (<...>)
4300 inactive: only inactive timestamps (<...)
4301 scheduled: only scheduled timestamps
4302 deadline: only deadline timestamps"
4303 :type
'(choice (const :tag
"Scheduled or deadline" 'scheduled-or-deadline
)
4304 (const :tag
"All timestamps" all
)
4305 (const :tag
"Only active timestamps" active
)
4306 (const :tag
"Only inactive timestamps" inactive
)
4307 (const :tag
"Only scheduled timestamps" scheduled
)
4308 (const :tag
"Only deadline timestamps" deadline
))
4310 :group
'org-sparse-trees
)
4312 (defun org-cycle-hide-archived-subtrees (state)
4313 "Re-hide all archived subtrees after a visibility state change."
4314 (when (and (not org-cycle-open-archived-trees
)
4315 (not (memq state
'(overview folded
))))
4317 (let* ((globalp (memq state
'(contents all
)))
4318 (beg (if globalp
(point-min) (point)))
4319 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4320 (org-hide-archived-subtrees beg end
)
4322 (if (looking-at (concat ".*:" org-archive-tag
":"))
4323 (message "%s" (substitute-command-keys
4324 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4326 (defun org-force-cycle-archived ()
4327 "Cycle subtree even if it is archived."
4329 (setq this-command
'org-cycle
)
4330 (let ((org-cycle-open-archived-trees t
))
4331 (call-interactively 'org-cycle
)))
4333 (defun org-hide-archived-subtrees (beg end
)
4334 "Re-hide all archived subtrees after a visibility state change."
4336 (let* ((re (concat ":" org-archive-tag
":")))
4338 (while (re-search-forward re end t
)
4339 (when (org-at-heading-p)
4340 (org-flag-subtree t
)
4341 (org-end-of-subtree t
))))))
4343 (declare-function outline-end-of-heading
"outline" ())
4344 (declare-function outline-flag-region
"outline" (from to flag
))
4345 (defun org-flag-subtree (flag)
4347 (org-back-to-heading t
)
4348 (outline-end-of-heading)
4349 (outline-flag-region (point)
4350 (progn (org-end-of-subtree t
) (point))
4353 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4356 (org-autoload "org-archive"
4357 '(org-add-archive-files org-archive-subtree
4358 org-archive-to-archive-sibling org-toggle-archive-tag
4359 org-archive-subtree-default
4360 org-archive-subtree-default-with-confirmation
)))
4362 ;; Autoload Column View Code
4364 (declare-function org-columns-number-to-string
"org-colview" (n fmt
&optional printf
))
4365 (declare-function org-columns-get-format-and-top-level
"org-colview" ())
4366 (declare-function org-columns-compute
"org-colview" (property))
4368 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4369 '(org-columns-number-to-string org-columns-get-format-and-top-level
4370 org-columns-compute org-agenda-columns org-columns-remove-overlays
4371 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
4375 (declare-function org-id-store-link
"org-id")
4376 (declare-function org-id-locations-load
"org-id")
4377 (declare-function org-id-locations-save
"org-id")
4378 (defvar org-id-track-globally
)
4379 (org-autoload "org-id"
4380 '(org-id-get-create org-id-new org-id-copy org-id-get
4381 org-id-get-with-outline-path-completion
4382 org-id-get-with-outline-drilling org-id-store-link
4383 org-id-goto org-id-find org-id-store-link
))
4385 ;; Autoload Plotting Code
4387 (org-autoload "org-plot"
4388 '(org-plot/gnuplot
))
4390 ;;; Variables for pre-computed regular expressions, all buffer local
4392 (defvar org-drawer-regexp
"^[ \t]*:PROPERTIES:[ \t]*$"
4393 "Matches first line of a hidden block.")
4394 (make-variable-buffer-local 'org-drawer-regexp
)
4395 (defvar org-todo-regexp nil
4396 "Matches any of the TODO state keywords.")
4397 (make-variable-buffer-local 'org-todo-regexp
)
4398 (defvar org-not-done-regexp nil
4399 "Matches any of the TODO state keywords except the last one.")
4400 (make-variable-buffer-local 'org-not-done-regexp
)
4401 (defvar org-not-done-heading-regexp nil
4402 "Matches a TODO headline that is not done.")
4403 (make-variable-buffer-local 'org-not-done-regexp
)
4404 (defvar org-todo-line-regexp nil
4405 "Matches a headline and puts TODO state into group 2 if present.")
4406 (make-variable-buffer-local 'org-todo-line-regexp
)
4407 (defvar org-complex-heading-regexp nil
4408 "Matches a headline and puts everything into groups:
4410 group 2: The todo keyword, maybe
4411 group 3: Priority cookie
4412 group 4: True headline
4414 (make-variable-buffer-local 'org-complex-heading-regexp
)
4415 (defvar org-complex-heading-regexp-format nil
4416 "Printf format to make regexp to match an exact headline.
4417 This regexp will match the headline of any node which has the
4418 exact headline text that is put into the format, but may have any
4419 TODO state, priority and tags.")
4420 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4421 (defvar org-todo-line-tags-regexp nil
4422 "Matches a headline and puts TODO state into group 2 if present.
4423 Also put tags into group 4 if tags are present.")
4424 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4425 (defvar org-ds-keyword-length
12
4426 "Maximum length of the DEADLINE and SCHEDULED keywords.")
4427 (make-variable-buffer-local 'org-ds-keyword-length
)
4428 (defvar org-deadline-regexp nil
4429 "Matches the DEADLINE keyword.")
4430 (make-variable-buffer-local 'org-deadline-regexp
)
4431 (defvar org-deadline-time-regexp nil
4432 "Matches the DEADLINE keyword together with a time stamp.")
4433 (make-variable-buffer-local 'org-deadline-time-regexp
)
4434 (defvar org-deadline-line-regexp nil
4435 "Matches the DEADLINE keyword and the rest of the line.")
4436 (make-variable-buffer-local 'org-deadline-line-regexp
)
4437 (defvar org-scheduled-regexp nil
4438 "Matches the SCHEDULED keyword.")
4439 (make-variable-buffer-local 'org-scheduled-regexp
)
4440 (defvar org-scheduled-time-regexp nil
4441 "Matches the SCHEDULED keyword together with a time stamp.")
4442 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4443 (defvar org-closed-time-regexp nil
4444 "Matches the CLOSED keyword together with a time stamp.")
4445 (make-variable-buffer-local 'org-closed-time-regexp
)
4447 (defvar org-keyword-time-regexp nil
4448 "Matches any of the 4 keywords, together with the time stamp.")
4449 (make-variable-buffer-local 'org-keyword-time-regexp
)
4450 (defvar org-keyword-time-not-clock-regexp nil
4451 "Matches any of the 3 keywords, together with the time stamp.")
4452 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4453 (defvar org-maybe-keyword-time-regexp nil
4454 "Matches a timestamp, possibly preceded by a keyword.")
4455 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4456 (defvar org-all-time-keywords nil
4457 "List of time keywords.")
4458 (make-variable-buffer-local 'org-all-time-keywords
)
4460 (defconst org-plain-time-of-day-regexp
4463 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4466 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4468 "Regular expression to match a plain time or time range.
4469 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4470 groups carry important information:
4472 1 the first time, range or not
4473 8 the second time, if it is a range.")
4475 (defconst org-plain-time-extension-regexp
4478 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4479 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4480 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4481 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4482 groups carry important information:
4485 9 minutes of duration")
4487 (defconst org-stamp-time-of-day-regexp
4489 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4490 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4492 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4493 "Regular expression to match a timestamp time or time range.
4494 After a match, the following groups carry important information:
4496 1 date plus weekday, for back referencing to make sure both times are on the same day
4497 2 the first time, range or not
4498 4 the second time, if it is a range.")
4500 (defconst org-startup-options
4501 '(("fold" org-startup-folded t
)
4502 ("overview" org-startup-folded t
)
4503 ("nofold" org-startup-folded nil
)
4504 ("showall" org-startup-folded nil
)
4505 ("showeverything" org-startup-folded showeverything
)
4506 ("content" org-startup-folded content
)
4507 ("indent" org-startup-indented t
)
4508 ("noindent" org-startup-indented nil
)
4509 ("hidestars" org-hide-leading-stars t
)
4510 ("showstars" org-hide-leading-stars nil
)
4511 ("odd" org-odd-levels-only t
)
4512 ("oddeven" org-odd-levels-only nil
)
4513 ("align" org-startup-align-all-tables t
)
4514 ("noalign" org-startup-align-all-tables nil
)
4515 ("inlineimages" org-startup-with-inline-images t
)
4516 ("noinlineimages" org-startup-with-inline-images nil
)
4517 ("customtime" org-display-custom-times t
)
4518 ("logdone" org-log-done time
)
4519 ("lognotedone" org-log-done note
)
4520 ("nologdone" org-log-done nil
)
4521 ("lognoteclock-out" org-log-note-clock-out t
)
4522 ("nolognoteclock-out" org-log-note-clock-out nil
)
4523 ("logrepeat" org-log-repeat state
)
4524 ("lognoterepeat" org-log-repeat note
)
4525 ("nologrepeat" org-log-repeat nil
)
4526 ("logreschedule" org-log-reschedule time
)
4527 ("lognotereschedule" org-log-reschedule note
)
4528 ("nologreschedule" org-log-reschedule nil
)
4529 ("logredeadline" org-log-redeadline time
)
4530 ("lognoteredeadline" org-log-redeadline note
)
4531 ("nologredeadline" org-log-redeadline nil
)
4532 ("logrefile" org-log-refile time
)
4533 ("lognoterefile" org-log-refile note
)
4534 ("nologrefile" org-log-refile nil
)
4535 ("fninline" org-footnote-define-inline t
)
4536 ("nofninline" org-footnote-define-inline nil
)
4537 ("fnlocal" org-footnote-section nil
)
4538 ("fnauto" org-footnote-auto-label t
)
4539 ("fnprompt" org-footnote-auto-label nil
)
4540 ("fnconfirm" org-footnote-auto-label confirm
)
4541 ("fnplain" org-footnote-auto-label plain
)
4542 ("fnadjust" org-footnote-auto-adjust t
)
4543 ("nofnadjust" org-footnote-auto-adjust nil
)
4544 ("constcgs" constants-unit-system cgs
)
4545 ("constSI" constants-unit-system SI
)
4546 ("noptag" org-tag-persistent-alist nil
)
4547 ("hideblocks" org-hide-block-startup t
)
4548 ("nohideblocks" org-hide-block-startup nil
)
4549 ("beamer" org-startup-with-beamer-mode t
)
4550 ("entitiespretty" org-pretty-entities t
)
4551 ("entitiesplain" org-pretty-entities nil
))
4552 "Variable associated with STARTUP options for org-mode.
4553 Each element is a list of three items: the startup options (as written
4554 in the #+STARTUP line), the corresponding variable, and the value to set
4555 this variable to if the option is found. An optional forth element PUSH
4556 means to push this value onto the list in the variable.")
4558 (defun org-update-property-plist (key val props
)
4559 "Update PROPS with KEY and VAL."
4560 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4561 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4562 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4563 (previous (cdr (assoc key props
))))
4565 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4566 (cons (cons key val
) remainder
))))
4568 (defconst org-block-regexp
4569 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4570 "Regular expression for hiding blocks.")
4571 (defconst org-heading-keyword-regexp-format
4572 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4573 "Printf format for a regexp matching an headline with some keyword.
4574 This regexp will match the headline of any node which has the
4575 exact keyword that is put into the format. The keyword isn't in
4576 any group by default, but the stars and the body are.")
4577 (defconst org-heading-keyword-maybe-regexp-format
4578 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4579 "Printf format for a regexp matching an headline, possibly with some keyword.
4580 This regexp can match any headline with the specified keyword, or
4581 without a keyword. The keyword isn't in any group by default,
4582 but the stars and the body are.")
4584 (defun org-set-regexps-and-options ()
4585 "Precompute regular expressions for current buffer."
4586 (when (derived-mode-p 'org-mode
)
4587 (org-set-local 'org-todo-kwd-alist nil
)
4588 (org-set-local 'org-todo-key-alist nil
)
4589 (org-set-local 'org-todo-key-trigger nil
)
4590 (org-set-local 'org-todo-keywords-1 nil
)
4591 (org-set-local 'org-done-keywords nil
)
4592 (org-set-local 'org-todo-heads nil
)
4593 (org-set-local 'org-todo-sets nil
)
4594 (org-set-local 'org-todo-log-states nil
)
4595 (org-set-local 'org-file-properties nil
)
4596 (org-set-local 'org-file-tags nil
)
4597 (let ((re (org-make-options-regexp
4598 '("CATEGORY" "TODO" "COLUMNS"
4599 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
4600 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
4602 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4604 (scripts org-use-sub-superscripts
)
4605 kwds kws0 kwsa key log value cat arch tags const links hw dws
4606 tail sep kws1 prio props ftags drawers beamer-p
4607 ext-setup-or-nil setup-contents
(start 0))
4611 (goto-char (point-min))
4612 (while (or (and ext-setup-or-nil
4613 (string-match re ext-setup-or-nil start
)
4614 (setq start
(match-end 0)))
4615 (and (setq ext-setup-or-nil nil start
0)
4616 (re-search-forward re nil t
)))
4617 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4618 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4619 (if (stringp value
) (setq value
(org-trim value
)))
4621 ((equal key
"CATEGORY")
4623 ((member key
'("SEQ_TODO" "TODO"))
4624 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4625 ((equal key
"TYP_TODO")
4626 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4627 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4628 ;; general TODO-like setup
4629 (push (cons (intern (downcase (match-string 1 key
)))
4630 (org-split-string value splitre
)) kwds
))
4632 (setq tags
(append tags
(if tags
'("\\n") nil
)
4633 (org-split-string value splitre
))))
4634 ((equal key
"COLUMNS")
4635 (org-set-local 'org-columns-default-format value
))
4637 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4638 (push (cons (match-string 1 value
)
4639 (org-trim (match-string 2 value
)))
4641 ((equal key
"PRIORITIES")
4642 (setq prio
(org-split-string value
" +")))
4643 ((equal key
"PROPERTY")
4644 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4645 (setq props
(org-update-property-plist (match-string 1 value
)
4646 (match-string 2 value
)
4648 ((equal key
"FILETAGS")
4649 (when (string-match "\\S-" value
)
4654 (mapcar (lambda (x) (org-split-string x
":"))
4655 (org-split-string value
)))))))
4656 ((equal key
"DRAWERS")
4657 (setq drawers
(delete-dups (append org-drawers
(org-split-string value splitre
)))))
4658 ((equal key
"CONSTANTS")
4659 (setq const
(append const
(org-split-string value splitre
))))
4660 ((equal key
"STARTUP")
4661 (let ((opts (org-split-string value splitre
))
4663 (while (setq l
(pop opts
))
4664 (when (setq l
(assoc l org-startup-options
))
4665 (setq var
(nth 1 l
) val
(nth 2 l
))
4667 (set (make-local-variable var
) val
)
4668 (if (not (listp (symbol-value var
)))
4669 (set (make-local-variable var
) nil
))
4670 (set (make-local-variable var
) (symbol-value var
))
4671 (add-to-list var val
))))))
4672 ((equal key
"ARCHIVE")
4674 (remove-text-properties 0 (length arch
)
4675 '(face t fontified t
) arch
))
4676 ((equal key
"LATEX_CLASS")
4677 (setq beamer-p
(equal value
"beamer")))
4678 ((equal key
"OPTIONS")
4679 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4680 (setq scripts
(read (match-string 2 value
)))))
4681 ((equal key
"SETUPFILE")
4682 (setq setup-contents
(org-file-contents
4684 (org-remove-double-quotes value
))
4686 (if (not ext-setup-or-nil
)
4687 (setq ext-setup-or-nil setup-contents start
0)
4688 (setq ext-setup-or-nil
4689 (concat (substring ext-setup-or-nil
0 start
)
4690 "\n" setup-contents
"\n"
4691 (substring ext-setup-or-nil start
)))))))
4692 ;; search for property blocks
4693 (goto-char (point-min))
4694 (while (re-search-forward org-block-regexp nil t
)
4695 (when (equal "PROPERTY" (upcase (match-string 1)))
4696 (setq value
(replace-regexp-in-string
4697 "[\n\r]" " " (match-string 4)))
4698 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4699 (setq props
(org-update-property-plist (match-string 1 value
)
4700 (match-string 2 value
)
4702 (org-set-local 'org-use-sub-superscripts scripts
)
4704 (org-set-local 'org-category
(intern cat
))
4705 (push (cons "CATEGORY" cat
) props
))
4707 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4708 (setq prio
(mapcar 'string-to-char prio
))
4709 (org-set-local 'org-highest-priority
(nth 0 prio
))
4710 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4711 (org-set-local 'org-default-priority
(nth 2 prio
)))
4712 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4713 (and ftags
(org-set-local 'org-file-tags
4714 (mapcar 'org-add-prop-inherited ftags
)))
4715 (and drawers
(org-set-local 'org-drawers drawers
))
4716 (and arch
(org-set-local 'org-archive-location arch
))
4717 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4718 ;; Process the TODO keywords
4720 ;; Use the global values as if they had been given locally.
4721 (setq kwds
(default-value 'org-todo-keywords
))
4722 (if (stringp (car kwds
))
4723 (setq kwds
(list (cons org-todo-interpretation
4724 (default-value 'org-todo-keywords
)))))
4725 (setq kwds
(reverse kwds
)))
4726 (setq kwds
(nreverse kwds
))
4728 (while (setq kws
(pop kwds
))
4730 (run-hook-with-args-until-success
4731 'org-todo-setup-filter-hook kws
)
4733 (setq inter
(pop kws
) sep
(member "|" kws
)
4734 kws0
(delete "|" (copy-sequence kws
))
4739 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4741 (setq kw
(match-string 1 x
)
4742 key
(and (match-end 2) (match-string 2 x
))
4743 log
(org-extract-log-state-settings x
))
4744 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4745 (and log
(push log org-todo-log-states
))
4747 (error "Invalid TODO keyword %s" x
)))
4749 kwsa
(if kwsa
(append '((:startgroup
))
4753 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4754 tail
(list inter hw
(car dws
) (org-last dws
))))
4755 (add-to-list 'org-todo-heads hw
'append
)
4756 (push kws1 org-todo-sets
)
4757 (setq org-done-keywords
(append org-done-keywords dws nil
))
4758 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4759 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4760 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4761 (setq org-todo-sets
(nreverse org-todo-sets
)
4762 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4763 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4764 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4765 ;; Process the constants
4768 (while (setq e
(pop const
))
4769 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4770 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4771 (setq org-table-formula-constants-local cst
)))
4773 ;; Process the tags.
4776 (while (setq e
(pop tags
))
4778 ((equal e
"{") (push '(:startgroup
) tgs
))
4779 ((equal e
"}") (push '(:endgroup
) tgs
))
4780 ((equal e
"\\n") (push '(:newline
) tgs
))
4781 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4782 (push (cons (match-string 1 e
)
4783 (string-to-char (match-string 2 e
)))
4785 (t (push (list e
) tgs
))))
4786 (org-set-local 'org-tag-alist nil
)
4787 (while (setq e
(pop tgs
))
4788 (or (and (stringp (car e
))
4789 (assoc (car e
) org-tag-alist
))
4790 (push e org-tag-alist
)))))
4792 ;; Compute the regular expressions and other local variables.
4793 ;; Using `org-outline-regexp-bol' would complicate them much,
4794 ;; because of the fixed white space at the end of that string.
4795 (if (not org-done-keywords
)
4796 (setq org-done-keywords
(and org-todo-keywords-1
4797 (list (org-last org-todo-keywords-1
)))))
4798 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4799 (length org-scheduled-string
)
4800 (length org-clock-string
)
4801 (length org-closed-string
)))
4803 (concat "^[ \t]*:\\("
4804 (mapconcat 'regexp-quote org-drawers
"\\|")
4806 org-not-done-keywords
4807 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4810 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4814 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4816 org-not-done-heading-regexp
4817 (format org-heading-keyword-regexp-format org-not-done-regexp
)
4818 org-todo-line-regexp
4819 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
4820 org-complex-heading-regexp
4821 (concat "^\\(\\*+\\)"
4822 "\\(?: +" org-todo-regexp
"\\)?"
4823 "\\(?: +\\(\\[#.\\]\\)\\)?"
4824 "\\(?: +\\(.*?\\)\\)?"
4825 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
4827 org-complex-heading-regexp-format
4828 (concat "^\\(\\*+\\)"
4829 "\\(?: +" org-todo-regexp
"\\)?"
4830 "\\(?: +\\(\\[#.\\]\\)\\)?"
4832 ;; Stats cookies can be stuck to body.
4833 "\\(?:\\[[0-9%%/]+\\] *\\)?"
4835 "\\(?: *\\[[0-9%%/]+\\]\\)?"
4837 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
4839 org-todo-line-tags-regexp
4840 (concat "^\\(\\*+\\)"
4841 "\\(?: +" org-todo-regexp
"\\)?"
4842 "\\(?: +\\(.*?\\)\\)?"
4843 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
4845 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4846 org-deadline-time-regexp
4847 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4848 org-deadline-line-regexp
4849 (concat "\\<\\(" org-deadline-string
"\\).*")
4850 org-scheduled-regexp
4851 (concat "\\<" org-scheduled-string
)
4852 org-scheduled-time-regexp
4853 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4854 org-closed-time-regexp
4855 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4856 org-keyword-time-regexp
4857 (concat "\\<\\(" org-scheduled-string
4858 "\\|" org-deadline-string
4859 "\\|" org-closed-string
4860 "\\|" org-clock-string
"\\)"
4861 " *[[<]\\([^]>]+\\)[]>]")
4862 org-keyword-time-not-clock-regexp
4863 (concat "\\<\\(" org-scheduled-string
4864 "\\|" org-deadline-string
4865 "\\|" org-closed-string
4867 " *[[<]\\([^]>]+\\)[]>]")
4868 org-maybe-keyword-time-regexp
4869 (concat "\\(\\<\\(" org-scheduled-string
4870 "\\|" org-deadline-string
4871 "\\|" org-closed-string
4872 "\\|" org-clock-string
"\\)\\)?"
4873 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4874 org-all-time-keywords
4875 (mapcar (lambda (w) (substring w
0 -
1))
4876 (list org-scheduled-string org-deadline-string
4877 org-clock-string org-closed-string
))
4879 (org-compute-latex-and-specials-regexp)
4880 (org-set-font-lock-defaults))))
4882 (defun org-file-contents (file &optional noerror
)
4883 "Return the contents of FILE, as a string."
4885 (not (file-readable-p file
)))
4888 (message "Cannot read file \"%s\"" file
)
4891 (error "Cannot read file \"%s\"" file
))
4893 (insert-file-contents file
)
4896 (defun org-extract-log-state-settings (x)
4897 "Extract the log state setting from a TODO keyword string.
4898 This will extract info from a string like \"WAIT(w@/!)\"."
4899 (let (kw key log1 log2
)
4900 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4901 (setq kw
(match-string 1 x
)
4902 key
(and (match-end 2) (match-string 2 x
))
4903 log1
(and (match-end 3) (match-string 3 x
))
4904 log2
(and (match-end 4) (match-string 4 x
)))
4907 (and log1
(if (equal log1
"!") 'time
'note
))
4908 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4910 (defun org-remove-keyword-keys (list)
4911 "Remove a pair of parenthesis at the end of each string in LIST."
4913 (if (string-match "(.*)$" x
)
4914 (substring x
0 (match-beginning 0))
4918 (defun org-assign-fast-keys (alist)
4919 "Assign fast keys to a keyword-key alist.
4920 Respect keys that are already there."
4921 (let (new e
(alt ?
0))
4922 (while (setq e
(pop alist
))
4923 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
4924 (cdr e
)) ;; Key already assigned.
4926 (let ((clist (string-to-list (downcase (car e
))))
4927 (used (append new alist
)))
4928 (when (= (car clist
) ?
@)
4930 (while (and clist
(rassoc (car clist
) used
))
4933 (while (rassoc alt used
)
4935 (push (cons (car e
) (or (car clist
) alt
)) new
))))
4938 ;;; Some variables used in various places
4940 (defvar org-window-configuration nil
4941 "Used in various places to store a window configuration.")
4942 (defvar org-selected-window nil
4943 "Used in various places to store a window configuration.")
4944 (defvar org-finish-function nil
4945 "Function to be called when `C-c C-c' is used.
4946 This is for getting out of special buffers like remember.")
4949 ;; FIXME: Occasionally check by commenting these, to make sure
4950 ;; no other functions uses these, forgetting to let-bind them.
4951 (org-no-warnings (defvar entry
)) ;; unprefixed, from calendar.el
4952 (defvar org-last-state
)
4953 (org-no-warnings (defvar date
)) ;; unprefixed, from calendar.el
4955 ;; Defined somewhere in this file, but used before definition.
4956 (defvar org-entities
) ;; defined in org-entities.el
4957 (defvar org-struct-menu
)
4958 (defvar org-org-menu
)
4959 (defvar org-tbl-menu
)
4961 ;;;; Define the Org-mode
4963 ;; We use a before-change function to check if a table might need
4965 (defvar org-table-may-need-update t
4966 "Indicates that a table might need an update.
4967 This variable is set by `org-before-change-function'.
4968 `org-table-align' sets it back to nil.")
4969 (defun org-before-change-function (beg end
)
4970 "Every change indicates that a table might need an update."
4971 (setq org-table-may-need-update t
))
4972 (defvar org-mode-map
)
4973 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4974 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
4975 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4976 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
4977 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
4978 (defvar org-table-buffer-is-an nil
)
4980 (defvar bidi-paragraph-direction
)
4981 (defvar buffer-face-mode-face
)
4984 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4985 (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"))
4986 (require 'noutline
"noutline" 'noerror
) ;; stock XEmacs does not have it
4988 ;; Other stuff we need.
4989 (require 'time-date
)
4990 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
4995 (require 'org-entities
)
4996 ;; (require 'org-compat) moved higher up in the file before it is first used
4997 (require 'org-faces
)
4999 (require 'org-pcomplete
)
5001 (require 'org-footnote
)
5008 (require 'ob-tangle
)
5009 (require 'ob-comint
)
5013 (define-derived-mode org-mode outline-mode
"Org"
5014 "Outline-based notes management and organizer, alias
5015 \"Carsten's outline-mode for keeping track of everything.\"
5017 Org-mode develops organizational tasks around a NOTES file which
5018 contains information about projects as plain text. Org-mode is
5019 implemented on top of outline-mode, which is ideal to keep the content
5020 of large files well structured. It supports ToDo items, deadlines and
5021 time stamps, which magically appear in the diary listing of the Emacs
5022 calendar. Tables are easily created with a built-in table editor.
5023 Plain text URL-like links connect to websites, emails (VM), Usenet
5024 messages (Gnus), BBDB entries, and any files related to the project.
5025 For printing and sharing of notes, an Org-mode file (or a part of it)
5026 can be exported as a structured ASCII or HTML file.
5028 The following commands are available:
5032 ;; Get rid of Outline menus, they are not needed
5033 ;; Need to do this here because define-derived-mode sets up
5034 ;; the keymap so late. Still, it is a waste to call this each time
5035 ;; we switch another buffer into org-mode.
5036 (if (featurep 'xemacs
)
5037 (when (boundp 'outline-mode-menu-heading
)
5038 ;; Assume this is Greg's port, it uses easymenu
5039 (easy-menu-remove outline-mode-menu-heading
)
5040 (easy-menu-remove outline-mode-menu-show
)
5041 (easy-menu-remove outline-mode-menu-hide
))
5042 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
5043 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
5044 (define-key org-mode-map
[menu-bar show
] 'undefined
))
5046 (org-load-modules-maybe)
5047 (easy-menu-add org-org-menu
)
5048 (easy-menu-add org-tbl-menu
)
5049 (org-install-agenda-files-menu)
5050 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
5051 (add-to-invisibility-spec '(org-cwidth))
5052 (add-to-invisibility-spec '(org-hide-block . t
))
5053 (when (featurep 'xemacs
)
5054 (org-set-local 'line-move-ignore-invisible t
))
5055 (org-set-local 'outline-regexp org-outline-regexp
)
5056 (org-set-local 'outline-level
'org-outline-level
)
5057 (setq bidi-paragraph-direction
'left-to-right
)
5058 (when (and org-ellipsis
5059 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
5060 (fboundp 'make-glyph-code
))
5061 (unless org-display-table
5062 (setq org-display-table
(make-display-table)))
5063 (set-display-table-slot
5066 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
5068 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
5069 (setq buffer-display-table org-display-table
))
5070 (org-set-regexps-and-options)
5071 (when (and org-tag-faces
(not org-tags-special-faces-re
))
5072 ;; tag faces set outside customize.... force initialization.
5073 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
5075 (org-set-local 'calc-embedded-open-mode
"# ")
5076 (modify-syntax-entry ?
@ "w")
5077 (if org-startup-truncated
(setq truncate-lines t
))
5078 (org-set-local 'font-lock-unfontify-region-function
5079 'org-unfontify-region
)
5080 ;; Activate before-change-function
5081 (org-set-local 'org-table-may-need-update t
)
5082 (org-add-hook 'before-change-functions
'org-before-change-function nil
5084 ;; Check for running clock before killing a buffer
5085 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
5086 ;; Paragraphs and auto-filling
5087 (org-set-autofill-regexps)
5088 (org-set-local 'indent-line-function
'org-indent-line
)
5089 (org-set-local 'indent-region-function
'org-indent-region
)
5090 (org-update-radio-target-regexp)
5092 (org-set-local 'comment-use-syntax nil
)
5093 (org-set-local 'comment-start
"# ")
5094 (org-set-local 'comment-start-skip
"^\\s-*#\\(?: \\|$\\)")
5095 (org-set-local 'comment-insert-comment-function
'org-insert-comment
)
5096 (org-set-local 'comment-region-function
'org-comment-or-uncomment-region
)
5097 (org-set-local 'uncomment-region-function
'org-comment-or-uncomment-region
)
5098 ;; Beginning/end of defun
5099 (org-set-local 'beginning-of-defun-function
'org-back-to-heading
)
5100 (org-set-local 'end-of-defun-function
(lambda () (interactive) (org-end-of-subtree nil t
)))
5101 ;; Next error for sparse trees
5102 (org-set-local 'next-error-function
'org-occur-next-match
)
5103 ;; Make sure dependence stuff works reliably, even for users who set it
5105 (if org-enforce-todo-dependencies
5106 (add-hook 'org-blocker-hook
5107 'org-block-todo-from-children-or-siblings-or-parent
)
5108 (remove-hook 'org-blocker-hook
5109 'org-block-todo-from-children-or-siblings-or-parent
))
5110 (if org-enforce-todo-checkbox-dependencies
5111 (add-hook 'org-blocker-hook
5112 'org-block-todo-from-checkboxes
)
5113 (remove-hook 'org-blocker-hook
5114 'org-block-todo-from-checkboxes
))
5116 ;; Align options lines
5118 'align-mode-rules-list
5119 '((org-in-buffer-settings
5120 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
5121 (modes .
'(org-mode)))))
5124 (org-set-local 'imenu-create-index-function
5125 'org-imenu-get-tree
)
5127 ;; Make isearch reveal context
5128 (if (or (featurep 'xemacs
)
5129 (not (boundp 'outline-isearch-open-invisible-function
)))
5130 ;; Emacs 21 and XEmacs make use of the hook
5131 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
5132 ;; Emacs 22 deals with this through a special variable
5133 (org-set-local 'outline-isearch-open-invisible-function
5134 (lambda (&rest ignore
) (org-show-context 'isearch
))))
5136 ;; Turn on org-beamer-mode?
5137 (and org-startup-with-beamer-mode
(org-beamer-mode 1))
5139 ;; Setup the pcomplete hooks
5140 (set (make-local-variable 'pcomplete-command-completion-function
)
5141 'org-pcomplete-initial
)
5142 (set (make-local-variable 'pcomplete-command-name-function
)
5143 'org-command-at-point
)
5144 (set (make-local-variable 'pcomplete-default-completion-function
)
5146 (set (make-local-variable 'pcomplete-parse-arguments-function
)
5147 'org-parse-arguments
)
5148 (set (make-local-variable 'pcomplete-termination-string
) "")
5149 (when (>= emacs-major-version
23)
5150 (set (make-local-variable 'buffer-face-mode-face
) 'org-default
))
5152 ;; If empty file that did not turn on org-mode automatically, make it to.
5153 (if (and org-insert-mode-line-in-empty-file
5154 (org-called-interactively-p 'any
)
5155 (= (point-min) (point-max)))
5156 (insert "# -*- mode: org -*-\n\n"))
5157 (unless org-inhibit-startup
5158 (when org-startup-align-all-tables
5159 (let ((bmp (buffer-modified-p)))
5160 (org-table-map-tables 'org-table-align
'quietly
)
5161 (set-buffer-modified-p bmp
)))
5162 (when org-startup-with-inline-images
5163 (org-display-inline-images))
5164 (when org-startup-indented
5165 (require 'org-indent
)
5166 (org-indent-mode 1))
5167 (unless org-inhibit-startup-visibility-stuff
5168 (org-set-startup-visibility))))
5170 (when (fboundp 'abbrev-table-put
)
5171 (abbrev-table-put org-mode-abbrev-table
5172 :parents
(list text-mode-abbrev-table
)))
5174 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5176 (defun org-current-time ()
5177 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
5178 (if (> (car org-time-stamp-rounding-minutes
) 1)
5179 (let ((r (car org-time-stamp-rounding-minutes
))
5180 (time (decode-time)))
5182 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5187 "Return today date, considering `org-extend-today-until'."
5189 (time-subtract (current-time)
5190 (list 0 (* 3600 org-extend-today-until
) 0))))
5192 ;;;; Font-Lock stuff, including the activators
5194 (defvar org-mouse-map
(make-sparse-keymap))
5195 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5196 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5197 (when org-mouse-1-follows-link
5198 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5199 (when org-tab-follows-link
5200 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5201 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5203 (require 'font-lock
)
5205 (defconst org-non-link-chars
"]\t\n\r<>")
5206 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5207 "shell" "elisp" "doi" "message"))
5208 (defvar org-link-types-re nil
5209 "Matches a link that has a url-like prefix like \"http:\"")
5210 (defvar org-link-re-with-space nil
5211 "Matches a link with spaces, optional angular brackets around it.")
5212 (defvar org-link-re-with-space2 nil
5213 "Matches a link with spaces, optional angular brackets around it.")
5214 (defvar org-link-re-with-space3 nil
5215 "Matches a link with spaces, only for internal part in bracket links.")
5216 (defvar org-angle-link-re nil
5217 "Matches link with angular brackets, spaces are allowed.")
5218 (defvar org-plain-link-re nil
5219 "Matches plain link, without spaces.")
5220 (defvar org-bracket-link-regexp nil
5221 "Matches a link in double brackets.")
5222 (defvar org-bracket-link-analytic-regexp nil
5223 "Regular expression used to analyze links.
5224 Here is what the match groups contain after a match:
5230 (defvar org-bracket-link-analytic-regexp
++ nil
5231 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5232 (defvar org-any-link-re nil
5233 "Regular expression matching any link.")
5235 (defcustom org-match-sexp-depth
3
5236 "Number of stacked braces for sub/superscript matching.
5237 This has to be set before loading org.el to be effective."
5238 :group
'org-export-translation
; ??????????????????????????/
5241 (defun org-create-multibrace-regexp (left right n
)
5242 "Create a regular expression which will match a balanced sexp.
5243 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5244 as single character strings.
5245 The regexp returned will match the entire expression including the
5246 delimiters. It will also define a single group which contains the
5247 match except for the outermost delimiters. The maximum depth of
5248 stacked delimiters is N. Escaping delimiters is not possible."
5249 (let* ((nothing (concat "[^" left right
"]*?"))
5252 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5255 re
(concat re or next
)
5256 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5257 (concat left
"\\(" re
"\\)" right
)))
5259 (defvar org-match-substring-regexp
5261 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5262 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5264 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5266 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5267 "The regular expression matching a sub- or superscript.")
5269 (defvar org-match-substring-with-braces-regexp
5271 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5272 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5274 "The regular expression matching a sub- or superscript, forcing braces.")
5276 (defun org-make-link-regexps ()
5277 "Update the link regular expressions.
5278 This should be called after the variable `org-link-types' has changed."
5279 (setq org-link-types-re
5281 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
5282 org-link-re-with-space
5284 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5285 "\\([^" org-non-link-chars
" ]"
5286 "[^" org-non-link-chars
"]*"
5287 "[^" org-non-link-chars
" ]\\)>?")
5288 org-link-re-with-space2
5290 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5291 "\\([^" org-non-link-chars
" ]"
5293 "[^" org-non-link-chars
" ]\\)>?")
5294 org-link-re-with-space3
5296 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5297 "\\([^" org-non-link-chars
" ]"
5301 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5302 "\\([^" org-non-link-chars
" ]"
5303 "[^" org-non-link-chars
"]*"
5307 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5308 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5309 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5310 org-bracket-link-regexp
5311 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5312 org-bracket-link-analytic-regexp
5315 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
5318 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5320 org-bracket-link-analytic-regexp
++
5323 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
5326 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5329 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5330 org-angle-link-re
"\\)\\|\\("
5331 org-plain-link-re
"\\)")))
5333 (org-make-link-regexps)
5335 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5336 "Regular expression for fast time stamp matching.")
5337 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5338 "Regular expression for fast time stamp matching.")
5339 (defconst org-ts-regexp0
5340 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5341 "Regular expression matching time strings for analysis.
5342 This one does not require the space after the date, so it can be used
5343 on a string that terminates immediately after the date.")
5344 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5345 "Regular expression matching time strings for analysis.")
5346 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5347 "Regular expression matching time stamps, with groups.")
5348 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5349 "Regular expression matching time stamps (also [..]), with groups.")
5350 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5351 "Regular expression matching a time stamp range.")
5352 (defconst org-tr-regexp-both
5353 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5354 "Regular expression matching a time stamp range.")
5355 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5356 org-ts-regexp
"\\)?")
5357 "Regular expression matching a time stamp or time stamp range.")
5358 (defconst org-tsr-regexp-both
(concat org-ts-regexp-both
"\\(--?-?"
5359 org-ts-regexp-both
"\\)?")
5360 "Regular expression matching a time stamp or time stamp range.
5361 The time stamps may be either active or inactive.")
5363 (defvar org-emph-face nil
)
5365 (defun org-do-emphasis-faces (limit)
5366 "Run through the buffer and add overlays to emphasized strings."
5368 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5369 (if (not (= (char-after (match-beginning 3))
5370 (char-after (match-beginning 4))))
5373 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5374 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5378 (org-remove-flyspell-overlays-in
5379 (match-beginning 0) (match-end 0)))
5380 (add-text-properties (match-beginning 2) (match-end 2)
5381 '(font-lock-multiline t org-emphasis t
))
5382 (when org-hide-emphasis-markers
5383 (add-text-properties (match-end 4) (match-beginning 5)
5384 '(invisible org-link
))
5385 (add-text-properties (match-beginning 3) (match-end 3)
5386 '(invisible org-link
)))))
5390 (defun org-emphasize (&optional char
)
5391 "Insert or change an emphasis, i.e. a font like bold or italic.
5392 If there is an active region, change that region to a new emphasis.
5393 If there is no region, just insert the marker characters and position
5394 the cursor between them.
5395 CHAR should be either the marker character, or the first character of the
5396 HTML tag associated with that emphasis. If CHAR is a space, the means
5397 to remove the emphasis of the selected region.
5398 If char is not given (for example in an interactive call) it
5399 will be prompted for."
5401 (let ((eal org-emphasis-alist
) e det
5402 (erc org-emphasis-regexp-components
)
5404 (string "") beg end move tag c s
)
5405 (if (org-region-active-p)
5406 (setq beg
(region-beginning) end
(region-end)
5407 string
(buffer-substring beg end
))
5410 (while (setq e
(pop eal
))
5411 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
5413 (push (cons c
(string-to-char (car e
))) det
)
5414 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
5415 (substring tag
1)))))
5416 (setq det
(nreverse det
))
5418 (message "%s" (concat "Emphasis marker or tag:" prompt
))
5419 (setq char
(read-char-exclusive)))
5420 (setq char
(or (cdr (assoc char det
)) char
))
5421 (if (equal char ?\
)
5422 (setq s
"" move nil
)
5423 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5424 (error "No such emphasis marker: \"%c\"" char
))
5425 (setq s
(char-to-string char
)))
5426 (while (and (> (length string
) 1)
5427 (equal (substring string
0 1) (substring string -
1))
5428 (assoc (substring string
0 1) org-emphasis-alist
))
5429 (setq string
(substring string
1 -
1)))
5430 (setq string
(concat s string s
))
5431 (if beg
(delete-region beg end
))
5433 (string-match (concat "[" (nth 0 erc
) "\n]")
5434 (char-to-string (char-before (point)))))
5437 (string-match (concat "[" (nth 1 erc
) "\n]")
5438 (char-to-string (char-after (point)))))
5439 (insert " ") (backward-char 1))
5441 (and move
(backward-char 1))))
5443 (defconst org-nonsticky-props
5444 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text
))
5446 (defsubst org-rear-nonsticky-at
(pos)
5447 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5449 (defun org-activate-plain-links (limit)
5450 "Run through the buffer and add overlays to links."
5453 (if (and (re-search-forward (concat org-plain-link-re
) limit t
)
5454 (or (not (member 'bracket org-activate-links
))
5457 (goto-char (match-beginning 0))
5458 (not (looking-back "\\[\\["))))))
5460 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5461 (setq f
(get-text-property (match-beginning 0) 'face
))
5462 (if (or (eq f
'org-tag
)
5463 (and (listp f
) (memq 'org-tag f
)))
5465 (add-text-properties (match-beginning 0) (match-end 0)
5466 (list 'mouse-face
'highlight
5468 'keymap org-mouse-map
))
5469 (org-rear-nonsticky-at (match-end 0)))
5472 (defun org-activate-code (limit)
5473 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5475 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5476 (remove-text-properties (match-beginning 0) (match-end 0)
5477 '(display t invisible t intangible t
))
5480 (defcustom org-src-fontify-natively nil
5481 "When non-nil, fontify code in code blocks."
5484 :group
'org-appearance
5487 (defcustom org-allow-promoting-top-level-subtree nil
5488 "When non-nil, allow promoting a top level subtree.
5489 The leading star of the top level headline will be replaced
5493 :group
'org-appearance
)
5495 (defun org-fontify-meta-lines-and-blocks (limit)
5497 (org-fontify-meta-lines-and-blocks-1 limit
)
5498 (error (message "org-mode fontification error"))))
5500 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5501 "Fontify #+ lines and blocks, in the correct ways."
5502 (let ((case-fold-search t
))
5503 (if (re-search-forward
5504 "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5506 (let ((beg (match-beginning 0))
5507 (block-start (match-end 0))
5509 (lang (match-string 7))
5510 (beg1 (line-beginning-position 2))
5511 (dc1 (downcase (match-string 2)))
5512 (dc3 (downcase (match-string 3)))
5513 end end1 quoting block-type ovl
)
5515 ((member dc1
'("+html:" "+ascii:" "+latex:" "+docbook:"))
5516 ;; a single line of backend-specific content
5517 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5518 (remove-text-properties (match-beginning 0) (match-end 0)
5519 '(display t invisible t intangible t
))
5520 (add-text-properties (match-beginning 1) (match-end 3)
5521 '(font-lock-fontified t face org-meta-line
))
5522 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5523 '(font-lock-fontified t face org-block
))
5524 ; for backend-specific code
5526 ((and (match-end 4) (equal dc3
"+begin"))
5528 (setq block-type
(downcase (match-string 5))
5529 quoting
(member block-type org-protecting-blocks
))
5530 (when (re-search-forward
5531 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5532 nil t
) ;; on purpose, we look further than LIMIT
5533 (setq end
(min (point-max) (match-end 0))
5534 end1
(min (point-max) (1- (match-beginning 0))))
5535 (setq block-end
(match-beginning 0))
5537 (remove-text-properties beg end
5538 '(display t invisible t intangible t
)))
5539 (add-text-properties
5541 '(font-lock-fontified t font-lock-multiline t
))
5542 (add-text-properties beg beg1
'(face org-meta-line
))
5543 (add-text-properties end1
(min (point-max) (1+ end
))
5544 '(face org-meta-line
)) ; for end_src
5546 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5547 (org-src-font-lock-fontify-block lang block-start block-end
)
5548 ;; remove old background overlays
5550 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5551 (delete-overlay ov
)))
5552 (overlays-at (/ (+ beg1 block-end
) 2)))
5553 ;; add a background overlay
5554 (setq ovl
(make-overlay beg1 block-end
))
5555 (overlay-put ovl
'face
'org-block-background
)
5556 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5558 (add-text-properties beg1
(min (point-max) (1+ end1
))
5559 '(face org-block
))) ; end of source block
5560 ((not org-fontify-quote-and-verse-blocks
))
5561 ((string= block-type
"quote")
5562 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-quote
)))
5563 ((string= block-type
"verse")
5564 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-verse
))))
5565 (add-text-properties beg beg1
'(face org-block-begin-line
))
5566 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5567 '(face org-block-end-line
))
5569 ((member dc1
'("+title:" "+author:" "+email:" "+date:"))
5570 (add-text-properties
5572 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5573 '(font-lock-fontified t invisible t
)
5574 '(font-lock-fontified t face org-document-info-keyword
)))
5575 (add-text-properties
5576 (match-beginning 6) (match-end 6)
5577 (if (string-equal dc1
"+title:")
5578 '(font-lock-fontified t face org-document-title
)
5579 '(font-lock-fontified t face org-document-info
))))
5580 ((or (equal dc1
"+results")
5581 (member dc1
'("+begin:" "+end:" "+caption:" "+label:"
5582 "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
5583 "+call:" "+header:" "+headers:" "+name:"))
5584 (and (match-end 4) (equal dc3
"+attr")))
5585 (add-text-properties
5587 '(font-lock-fontified t face org-meta-line
))
5589 ((member dc3
'(" " ""))
5590 (add-text-properties
5592 '(font-lock-fontified t face font-lock-comment-face
)))
5593 ((not (member (char-after beg
) '(?\ ?
\t)))
5594 ;; just any other in-buffer setting, but not indented
5595 (add-text-properties
5597 '(font-lock-fontified t face org-meta-line
))
5601 (defun org-strip-protective-commas (beg end
)
5602 "Strip protective commas between BEG and END in the current buffer."
5607 (let ((front-line (save-excursion
5609 "[^[:space:]]" end t
)
5610 (goto-char (match-beginning 0))
5612 (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\)" end t
)
5613 (goto-char (match-beginning 1))
5614 (when (= (current-column) front-line
)
5615 (replace-match "" nil nil nil
1)))))))
5617 (defun org-activate-angle-links (limit)
5618 "Run through the buffer and add overlays to links."
5619 (if (re-search-forward org-angle-link-re limit t
)
5621 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5622 (add-text-properties (match-beginning 0) (match-end 0)
5623 (list 'mouse-face
'highlight
5624 'keymap org-mouse-map
))
5625 (org-rear-nonsticky-at (match-end 0))
5628 (defun org-activate-footnote-links (limit)
5629 "Run through the buffer and add overlays to footnotes."
5630 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5632 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5633 (org-remove-flyspell-overlays-in beg end
)
5634 (add-text-properties beg end
5635 (list 'mouse-face
'highlight
5636 'keymap org-mouse-map
5638 (if (= (point-at-bol) beg
)
5639 "Footnote definition"
5640 "Footnote reference")
5641 'font-lock-fontified t
5642 'font-lock-multiline t
5643 'face
'org-footnote
))))))
5645 (defun org-activate-bracket-links (limit)
5646 "Run through the buffer and add overlays to bracketed links."
5647 (if (re-search-forward org-bracket-link-regexp limit t
)
5648 (let* ((help (concat "LINK: "
5649 (org-match-string-no-properties 1)))
5650 ;; FIXME: above we should remove the escapes.
5651 ;; but that requires another match, protecting match data,
5652 ;; a lot of overhead for font-lock.
5653 (ip (org-maybe-intangible
5654 (list 'invisible
'org-link
5655 'keymap org-mouse-map
'mouse-face
'highlight
5656 'font-lock-multiline t
'help-echo help
)))
5657 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5658 'font-lock-multiline t
'help-echo help
)))
5659 ;; We need to remove the invisible property here. Table narrowing
5660 ;; may have made some of this invisible.
5661 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5662 (remove-text-properties (match-beginning 0) (match-end 0)
5666 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5667 (org-rear-nonsticky-at (match-beginning 3))
5668 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5669 (org-rear-nonsticky-at (match-end 3))
5670 (add-text-properties (match-end 3) (match-end 0) ip
)
5671 (org-rear-nonsticky-at (match-end 0)))
5672 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5673 (org-rear-nonsticky-at (match-beginning 1))
5674 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5675 (org-rear-nonsticky-at (match-end 1))
5676 (add-text-properties (match-end 1) (match-end 0) ip
)
5677 (org-rear-nonsticky-at (match-end 0)))
5680 (defun org-activate-dates (limit)
5681 "Run through the buffer and add overlays to dates."
5682 (if (re-search-forward org-tsr-regexp-both limit t
)
5684 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5685 (add-text-properties (match-beginning 0) (match-end 0)
5686 (list 'mouse-face
'highlight
5687 'keymap org-mouse-map
))
5688 (org-rear-nonsticky-at (match-end 0))
5689 (when org-display-custom-times
5691 (org-display-custom-time (match-beginning 3) (match-end 3)))
5692 (org-display-custom-time (match-beginning 1) (match-end 1)))
5695 (defvar org-target-link-regexp nil
5696 "Regular expression matching radio targets in plain text.")
5697 (make-variable-buffer-local 'org-target-link-regexp
)
5698 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5699 "Regular expression matching a link target.")
5700 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5701 "Regular expression matching a radio target.")
5702 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5703 "Regular expression matching any target.")
5705 (defun org-activate-target-links (limit)
5706 "Run through the buffer and add overlays to target matches."
5707 (when org-target-link-regexp
5708 (let ((case-fold-search t
))
5709 (if (re-search-forward org-target-link-regexp limit t
)
5711 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5712 (add-text-properties (match-beginning 0) (match-end 0)
5713 (list 'mouse-face
'highlight
5714 'keymap org-mouse-map
5715 'help-echo
"Radio target link"
5716 'org-linked-text t
))
5717 (org-rear-nonsticky-at (match-end 0))
5720 (defun org-update-radio-target-regexp ()
5721 "Find all radio targets in this file and update the regular expression."
5723 (when (memq 'radio org-activate-links
)
5724 (setq org-target-link-regexp
5725 (org-make-target-link-regexp (org-all-targets 'radio
)))
5726 (org-restart-font-lock)))
5728 (defun org-hide-wide-columns (limit)
5730 (setq s
(text-property-any (point) (or limit
(point-max))
5733 (setq e
(next-single-property-change s
'org-cwidth
))
5734 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5738 (defvar org-latex-and-specials-regexp nil
5739 "Regular expression for highlighting export special stuff.")
5740 (defvar org-match-substring-regexp
)
5741 (defvar org-match-substring-with-braces-regexp
)
5743 ;; This should be with the exporter code, but we also use if for font-locking
5744 (defconst org-export-html-special-string-regexps
5745 '(("\\\\-" .
"­")
5746 ("---\\([^-]\\)" .
"—\\1")
5747 ("--\\([^-]\\)" .
"–\\1")
5748 ("\\.\\.\\." .
"…"))
5749 "Regular expressions for special string conversion.")
5752 (defun org-compute-latex-and-specials-regexp ()
5753 "Compute regular expression for stuff treated specially by exporters."
5754 (if (not org-highlight-latex-fragments-and-specials
)
5755 (org-set-local 'org-latex-and-specials-regexp nil
)
5758 ((matchers (plist-get org-format-latex-options
:matchers
))
5759 (latexs (delq nil
(mapcar (lambda (x) (if (member (car x
) matchers
) x
))
5760 org-latex-regexps
)))
5761 (org-export-allow-BIND nil
)
5762 (options (org-combine-plists (org-default-export-plist)
5763 (org-infile-export-plist)))
5764 (org-export-with-sub-superscripts (plist-get options
:sub-superscript
))
5765 (org-export-with-LaTeX-fragments (plist-get options
:LaTeX-fragments
))
5766 (org-export-with-TeX-macros (plist-get options
:TeX-macros
))
5767 (org-export-html-expand (plist-get options
:expand-quoted-html
))
5768 (org-export-with-special-strings (plist-get options
:special-strings
))
5771 ((equal org-export-with-sub-superscripts
'{})
5772 (list org-match-substring-with-braces-regexp
))
5773 (org-export-with-sub-superscripts
5774 (list org-match-substring-regexp
))))
5776 (if org-export-with-LaTeX-fragments
5777 (mapcar (lambda (x) (nth 1 x
)) latexs
)))
5779 (if org-export-with-TeX-macros
5780 (list (concat "\\\\"
5786 (append org-entities-user
5788 (if (boundp 'org-latex-entities
)
5790 (or (car-safe x
) x
))
5795 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5796 (re-special (if org-export-with-special-strings
5797 (mapcar (lambda (x) (car x
))
5798 org-export-html-special-string-regexps
)))
5802 (if org-export-html-expand
"@<[^>\n]+>")
5805 'org-latex-and-specials-regexp
5806 (mapconcat 'identity
(append re-latex re-sub re-macros re-special
5809 (defun org-do-latex-and-special-faces (limit)
5810 "Run through the buffer and add overlays to links."
5811 (when org-latex-and-specials-regexp
5813 (while (and (not rtn
) (re-search-forward org-latex-and-specials-regexp
5815 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5817 '(org-code org-verbatim underline
)))
5820 d
(cond ((member (char-after (1+ (match-beginning 0)))
5823 (font-lock-prepend-text-property
5824 (+ d
(match-beginning 0)) (match-end 0)
5825 'face
'org-latex-and-export-specials
)
5826 (add-text-properties (+ d
(match-beginning 0)) (match-end 0)
5827 '(font-lock-multiline t
)))))
5830 (defun org-restart-font-lock ()
5831 "Restart `font-lock-mode', to force refontification."
5832 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5834 (font-lock-mode 1)))
5836 (defun org-all-targets (&optional radio
)
5837 "Return a list of all targets in this file.
5838 With optional argument RADIO, only find radio targets."
5839 (let ((re (if radio org-radio-target-regexp org-target-regexp
))
5842 (goto-char (point-min))
5843 (while (re-search-forward re nil t
)
5844 (add-to-list 'rtn
(downcase (org-match-string-no-properties 1))))
5847 (defun org-make-target-link-regexp (targets)
5848 "Make regular expression matching all strings in TARGETS.
5849 The regular expression finds the targets also if there is a line break
5856 (setq x
(regexp-quote x
))
5857 (while (string-match " +" x
)
5858 (setq x
(replace-match "\\s-+" t t x
)))
5864 (defun org-activate-tags (limit)
5865 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5867 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5868 (add-text-properties (match-beginning 1) (match-end 1)
5869 (list 'mouse-face
'highlight
5870 'keymap org-mouse-map
))
5871 (org-rear-nonsticky-at (match-end 1))
5874 (defun org-outline-level ()
5875 "Compute the outline level of the heading at point.
5876 This function assumes that the cursor is at the beginning of a line matched
5877 by `outline-regexp'. Otherwise it returns garbage.
5878 If this is called at a normal headline, the level is the number of stars.
5879 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
5881 (looking-at org-outline-regexp
)
5882 (1- (- (match-end 0) (match-beginning 0)))))
5884 (defvar org-font-lock-keywords nil
)
5886 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5887 "Regular expression matching a property line.")
5889 (defvar org-font-lock-hook nil
5890 "Functions to be called for special font lock stuff.")
5892 (defvar org-font-lock-set-keywords-hook nil
5893 "Functions that can manipulate `org-font-lock-extra-keywords'.
5894 This is called after `org-font-lock-extra-keywords' is defined, but before
5895 it is installed to be used by font lock. This can be useful if something
5896 needs to be inserted at a specific position in the font-lock sequence.")
5898 (defun org-font-lock-hook (limit)
5899 (run-hook-with-args 'org-font-lock-hook limit
))
5901 (defun org-set-font-lock-defaults ()
5902 (let* ((em org-fontify-emphasized-text
)
5903 (lk org-activate-links
)
5904 (org-font-lock-extra-keywords
5907 '(org-font-lock-hook)
5909 `(,(if org-fontify-whole-heading-line
5910 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5911 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5912 (1 (org-get-level-face 1))
5913 (2 (org-get-level-face 2))
5914 (3 (org-get-level-face 3)))
5916 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5919 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
5920 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
5921 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
5922 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
5924 (list org-drawer-regexp
'(0 'org-special-keyword t
))
5925 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
5927 (list org-property-re
5928 '(1 'org-special-keyword t
)
5929 '(3 'org-property-value t
))
5931 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
5932 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
5933 (if (memq 'plain lk
) '(org-activate-plain-links))
5934 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
5935 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
5936 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
5937 (if (memq 'footnote lk
) '(org-activate-footnote-links))
5938 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
5939 '(org-hide-wide-columns (0 nil append
))
5941 (list (format org-heading-keyword-regexp-format
5943 '(2 (org-get-todo-face 2) t
))
5945 (if org-fontify-done-headline
5946 (list (format org-heading-keyword-regexp-format
5949 (mapconcat 'regexp-quote org-done-keywords
"\\|")
5951 '(2 'org-headline-done t
))
5954 '(org-font-lock-add-priority-faces)
5956 '(org-font-lock-add-tag-faces)
5958 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
5959 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
5960 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
5961 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
5964 (if (featurep 'xemacs
)
5965 '(org-do-emphasis-faces (0 nil append
))
5966 '(org-do-emphasis-faces)))
5968 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
5969 1 'org-checkbox prepend
)
5970 (if (cdr (assq 'checkbox org-list-automatic-rules
))
5971 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5972 (0 (org-get-checkbox-statistics-face) t
)))
5973 ;; Description list items
5974 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
5975 1 'org-list-dt prepend
)
5976 ;; ARCHIVEd headings
5978 org-outline-regexp-bol
5979 "\\(.*:" org-archive-tag
":.*\\)")
5980 '(1 'org-archived prepend
))
5982 '(org-do-latex-and-special-faces)
5983 '(org-fontify-entities)
5984 '(org-raise-scripts)
5986 '(org-activate-code (1 'org-code t
))
5988 (list (format org-heading-keyword-regexp-format
5990 org-comment-string
"\\|" org-quote-string
5992 '(2 'org-special-keyword t
))
5993 ;; Blocks and meta lines
5994 '(org-fontify-meta-lines-and-blocks)
5996 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
5997 (run-hooks 'org-font-lock-set-keywords-hook
)
5998 ;; Now set the full font-lock-keywords
5999 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
6000 (org-set-local 'font-lock-defaults
6001 '(org-font-lock-keywords t nil nil backward-paragraph
))
6002 (kill-local-variable 'font-lock-keywords
) nil
))
6004 (defun org-toggle-pretty-entities ()
6005 "Toggle the composition display of entities as UTF8 characters."
6007 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
6008 (org-restart-font-lock)
6009 (if org-pretty-entities
6010 (message "Entities are displayed as UTF8 characters")
6013 (org-decompose-region (point-min) (point-max))
6014 (message "Entities are displayed plain"))))
6016 (defvar org-custom-properties-overlays nil
6017 "List of overlays used for custom properties.")
6018 (make-variable-buffer-local 'org-custom-properties-overlays
)
6020 (defun org-toggle-custom-properties-visibility ()
6021 "Display or hide properties in `org-custom-properties'."
6023 (if org-custom-properties-overlays
6024 (progn (mapc 'delete-overlay org-custom-properties-overlays
)
6025 (setq org-custom-properties-overlays nil
))
6026 (unless (not org-custom-properties
)
6030 (goto-char (point-min))
6031 (while (re-search-forward org-property-re nil t
)
6033 (when (equal p
(substring (match-string 1) 1 -
1))
6034 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
6035 (overlay-put o
'invisible t
)
6036 (overlay-put o
'org-custom-property t
)
6037 (push o org-custom-properties-overlays
))))
6038 org-custom-properties
)))))))
6040 (defun org-fontify-entities (limit)
6041 "Find an entity to fontify."
6043 (when org-pretty-entities
6045 (while (re-search-forward
6046 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
6048 (if (and (not (org-in-indented-comment-line))
6049 (setq ee
(org-entity-get (match-string 1)))
6050 (= (length (nth 6 ee
)) 1))
6052 ((end (if (equal (match-string 2) "{}")
6055 (add-text-properties
6056 (match-beginning 0) end
6057 (list 'font-lock-fontified t
))
6058 (compose-region (match-beginning 0) end
6064 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
6065 "Fontify string S like in Org-mode."
6068 (let ((org-odd-levels-only odd-levels
))
6070 (font-lock-fontify-buffer)
6076 (defun org-get-level-face (n)
6077 "Get the right face for match N in font-lock matching of headlines."
6078 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
6079 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
6080 (if org-cycle-level-faces
6081 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
6082 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
6084 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
6086 (t (if org-level-color-stars-only nil org-f
))))
6089 (defun org-get-todo-face (kwd)
6090 "Get the right face for a TODO keyword KWD.
6091 If KWD is a number, get the corresponding match group."
6092 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6093 (or (org-face-from-face-or-color
6094 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
6095 (and (member kwd org-done-keywords
) 'org-done
)
6098 (defun org-face-from-face-or-color (context inherit face-or-color
)
6099 "Create a face list that inherits INHERIT, but sets the foreground color.
6100 When FACE-OR-COLOR is not a string, just return it."
6101 (if (stringp face-or-color
)
6102 (list :inherit inherit
6103 (cdr (assoc context org-faces-easy-properties
))
6107 (defun org-font-lock-add-tag-faces (limit)
6108 "Add the special tag faces."
6109 (when (and org-tag-faces org-tags-special-faces-re
)
6110 (while (re-search-forward org-tags-special-faces-re limit t
)
6111 (add-text-properties (match-beginning 1) (match-end 1)
6112 (list 'face
(org-get-tag-face 1)
6113 'font-lock-fontified t
))
6114 (backward-char 1))))
6116 (defun org-font-lock-add-priority-faces (limit)
6117 "Add the special priority faces."
6118 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
6119 (when (save-match-data (org-at-heading-p))
6120 (add-text-properties
6121 (match-beginning 0) (match-end 0)
6122 (list 'face
(or (org-face-from-face-or-color
6123 'priority
'org-special-keyword
6124 (cdr (assoc (char-after (match-beginning 1))
6125 org-priority-faces
)))
6126 'org-special-keyword
)
6127 'font-lock-fontified t
)))))
6129 (defun org-get-tag-face (kwd)
6130 "Get the right face for a TODO keyword KWD.
6131 If KWD is a number, get the corresponding match group."
6132 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6133 (or (org-face-from-face-or-color
6134 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
6137 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
6138 "Remove fontification and activation overlays from links."
6139 (font-lock-default-unfontify-region beg end
)
6140 (let* ((buffer-undo-list t
)
6141 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
6142 (inhibit-modification-hooks t
)
6143 deactivate-mark buffer-file-name buffer-file-truename
)
6144 (org-decompose-region beg end
)
6145 (remove-text-properties beg end
6146 '(mouse-face t keymap t org-linked-text t
6147 invisible t intangible t
6148 org-no-flyspell t org-emphasis t
))
6149 (org-remove-font-lock-display-properties beg end
)))
6151 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
6152 ((raise 0.3) (height 0.7))
6155 "Display properties for showing superscripts and subscripts.")
6157 (defun org-remove-font-lock-display-properties (beg end
)
6158 "Remove specific display properties that have been added by font lock.
6159 The will remove the raise properties that are used to show superscripts
6163 (setq next
(next-single-property-change beg
'display nil end
)
6164 prop
(get-text-property beg
'display
))
6165 (if (member prop org-script-display
)
6166 (put-text-property beg next
'display nil
))
6169 (defun org-raise-scripts (limit)
6170 "Add raise properties to sub/superscripts."
6171 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
6172 (if (re-search-forward
6173 (if (eq org-use-sub-superscripts t
)
6174 org-match-substring-regexp
6175 org-match-substring-with-braces-regexp
)
6177 (let* ((pos (point)) table-p comment-p
6178 (mpos (match-beginning 3))
6179 (emph-p (get-text-property mpos
'org-emphasis
))
6180 (link-p (get-text-property mpos
'mouse-face
))
6181 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6182 (goto-char (point-at-bol))
6183 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
6184 comment-p
(org-looking-at-p "[ \t]*#"))
6186 ;; FIXME: Should we go back one character here, for a_b^c
6187 ;; (goto-char (1- pos)) ;????????????????????
6188 (if (or comment-p emph-p link-p keyw-p
)
6190 (put-text-property (match-beginning 3) (match-end 0)
6192 (if (equal (char-after (match-beginning 2)) ?^
)
6193 (nth (if table-p
3 1) org-script-display
)
6194 (nth (if table-p
2 0) org-script-display
)))
6195 (add-text-properties (match-beginning 2) (match-end 2)
6197 'org-dwidth t
'org-dwidth-n
1))
6198 (if (and (eq (char-after (match-beginning 3)) ?
{)
6199 (eq (char-before (match-end 3)) ?
}))
6201 (add-text-properties
6202 (match-beginning 3) (1+ (match-beginning 3))
6203 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6204 (add-text-properties
6205 (1- (match-end 3)) (match-end 3)
6206 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6209 ;;;; Visibility cycling, including org-goto and indirect buffer
6213 (defvar org-cycle-global-status nil
)
6214 (make-variable-buffer-local 'org-cycle-global-status
)
6215 (defvar org-cycle-subtree-status nil
)
6216 (make-variable-buffer-local 'org-cycle-subtree-status
)
6220 (defvar org-inlinetask-min-level
)
6222 (defun org-cycle (&optional arg
)
6223 "TAB-action and visibility cycling for Org-mode.
6225 This is the command invoked in Org-mode by the TAB key. Its main purpose
6226 is outline visibility cycling, but it also invokes other actions
6227 in special contexts.
6229 - When this function is called with a prefix argument, rotate the entire
6230 buffer through 3 states (global cycling)
6231 1. OVERVIEW: Show only top-level headlines.
6232 2. CONTENTS: Show all headlines of all levels, but no body text.
6233 3. SHOW ALL: Show everything.
6234 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6235 determined by the variable `org-startup-folded', and by any VISIBILITY
6236 properties in the buffer.
6237 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6238 including any drawers.
6240 - When inside a table, re-align the table and move to the next field.
6242 - When point is at the beginning of a headline, rotate the subtree started
6243 by this line through 3 different states (local cycling)
6244 1. FOLDED: Only the main headline is shown.
6245 2. CHILDREN: The main headline and the direct children are shown.
6246 From this state, you can move to one of the children
6247 and zoom in further.
6248 3. SUBTREE: Show the entire subtree, including body text.
6249 If there is no subtree, switch directly from CHILDREN to FOLDED.
6251 - When point is at the beginning of an empty headline and the variable
6252 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6253 of the headline by demoting and promoting it to likely levels. This
6254 speeds up creation document structure by pressing TAB once or several
6255 times right after creating a new headline.
6257 - When there is a numeric prefix, go up to a heading with level ARG, do
6258 a `show-subtree' and return to the previous cursor position. If ARG
6259 is negative, go up that many levels.
6261 - When point is not at the beginning of a headline, execute the global
6262 binding for TAB, which is re-indenting the line. See the option
6263 `org-cycle-emulate-tab' for details.
6265 - Special case: if point is at the beginning of the buffer and there is
6266 no headline in line 1, this function will act as if called with prefix arg
6267 (C-u TAB, same as S-TAB) also when called without prefix arg.
6268 But only if also the variable `org-cycle-global-at-bob' is t."
6270 (org-load-modules-maybe)
6271 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6272 (and org-cycle-level-after-item
/entry-creation
6273 (or (org-cycle-level)
6274 (org-cycle-item-indentation))))
6276 (or org-cycle-max-level
6277 (and (boundp 'org-inlinetask-min-level
)
6278 org-inlinetask-min-level
6279 (1- org-inlinetask-min-level
))))
6280 (nstars (and limit-level
6281 (if org-odd-levels-only
6282 (and limit-level
(1- (* limit-level
2)))
6285 (if (not (derived-mode-p 'org-mode
))
6287 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6288 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6289 (not (looking-at org-outline-regexp
))))
6292 (delq 'org-optimize-window-after-visibility-change
6293 (copy-sequence org-cycle-hook
))
6297 (if (or bob-special
(equal arg
'(4)))
6298 ;; special case: use global cycling
6304 (setq last-command
'dummy
)
6305 (org-set-startup-visibility)
6306 (message "Startup visibility, plus VISIBILITY properties"))
6310 (message "Entire buffer visible, including drawers"))
6312 ;; Table: enter it or move to the next field.
6313 ((org-at-table-p 'any
)
6314 (if (org-at-table.el-p
)
6315 (message "Use C-c ' to edit table.el tables")
6316 (if arg
(org-table-edit-field t
)
6317 (org-table-justify-field-maybe)
6318 (call-interactively 'org-table-next-field
))))
6320 ((run-hook-with-args-until-success
6321 'org-tab-after-check-for-table-hook
))
6323 ;; Global cycling: delegate to `org-cycle-internal-global'.
6324 ((eq arg t
) (org-cycle-internal-global))
6326 ;; Drawers: delegate to `org-flag-drawer'.
6327 ((and org-drawers org-drawer-regexp
6329 (beginning-of-line 1)
6330 (looking-at org-drawer-regexp
)))
6331 (org-flag-drawer ; toggle block visibility
6332 (not (get-char-property (match-end 0) 'invisible
))))
6334 ;; Show-subtree, ARG levels up from here.
6337 (org-back-to-heading)
6338 (outline-up-heading (if (< arg
0) (- arg
)
6339 (- (funcall outline-level
) arg
)))
6340 (org-show-subtree)))
6342 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6343 ((and (featurep 'org-inlinetask
)
6344 (org-inlinetask-at-task-p)
6345 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6346 (org-inlinetask-toggle-visibility))
6348 ((org-try-cdlatex-tab))
6350 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6351 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6352 (save-excursion (beginning-of-line 1)
6353 (looking-at org-outline-regexp
)))
6354 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6355 (org-cycle-internal-local))
6357 ;; From there: TAB emulation and template completion.
6358 (buffer-read-only (org-back-to-heading))
6360 ((run-hook-with-args-until-success
6361 'org-tab-after-check-for-cycling-hook
))
6363 ((org-try-structure-completion))
6365 ((run-hook-with-args-until-success
6366 'org-tab-before-tab-emulation-hook
))
6368 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6370 (not (looking-at org-outline-regexp
))))
6371 (call-interactively (global-key-binding "\t")))
6373 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6374 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6375 (or (and (eq org-cycle-emulate-tab
'white
)
6376 (= (match-end 0) (point-at-eol)))
6377 (and (eq org-cycle-emulate-tab
'whitestart
)
6378 (>= (match-end 0) pos
))))
6380 (eq org-cycle-emulate-tab t
))
6381 (call-interactively (global-key-binding "\t")))
6384 (org-back-to-heading)
6387 (defun org-cycle-internal-global ()
6388 "Do the global cycling action."
6389 ;; Hack to avoid display of messages for .org attachments in Gnus
6390 (let ((ga (string-match "\\*fontification" (buffer-name))))
6392 ((and (eq last-command this-command
)
6393 (eq org-cycle-global-status
'overview
))
6394 ;; We just created the overview - now do table of contents
6395 ;; This can be slow in very large buffers, so indicate action
6396 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6397 (unless ga
(message "CONTENTS..."))
6399 (unless ga
(message "CONTENTS...done"))
6400 (setq org-cycle-global-status
'contents
)
6401 (run-hook-with-args 'org-cycle-hook
'contents
))
6403 ((and (eq last-command this-command
)
6404 (eq org-cycle-global-status
'contents
))
6405 ;; We just showed the table of contents - now show everything
6406 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6408 (unless ga
(message "SHOW ALL"))
6409 (setq org-cycle-global-status
'all
)
6410 (run-hook-with-args 'org-cycle-hook
'all
))
6413 ;; Default action: go to overview
6414 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6416 (unless ga
(message "OVERVIEW"))
6417 (setq org-cycle-global-status
'overview
)
6418 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6420 (defun org-cycle-internal-local ()
6421 "Do the local cycling action."
6422 (let ((goal-column 0) eoh eol eos has-children children-skipped struct
)
6423 ;; First, determine end of headline (EOH), end of subtree or item
6424 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6429 (setq struct
(org-list-struct))
6430 (setq eoh
(point-at-eol))
6431 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6432 (setq has-children
(org-list-has-child-p (point) struct
)))
6433 (org-back-to-heading)
6434 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6435 (setq eos
(save-excursion
6436 (org-end-of-subtree t
)
6438 (skip-chars-forward " \t\n"))
6439 (if (eobp) (point) (1- (point)))))
6442 (let ((level (funcall outline-level
)))
6443 (outline-next-heading)
6444 (and (org-at-heading-p t
)
6445 (> (funcall outline-level
) level
))))
6447 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6448 ;; Determine end invisible part of buffer (EOL)
6449 (beginning-of-line 2)
6450 ;; XEmacs doesn't have `next-single-char-property-change'
6451 (if (featurep 'xemacs
)
6452 (while (and (not (eobp)) ;; this is like `next-line'
6453 (get-char-property (1- (point)) 'invisible
))
6454 (beginning-of-line 2))
6455 (while (and (not (eobp)) ;; this is like `next-line'
6456 (get-char-property (1- (point)) 'invisible
))
6457 (goto-char (next-single-char-property-change (point) 'invisible
))
6458 (and (eolp) (beginning-of-line 2))))
6460 ;; Find out what to do next and set `this-command'
6463 ;; Nothing is hidden behind this heading
6464 (run-hook-with-args 'org-pre-cycle-hook
'empty
)
6465 (message "EMPTY ENTRY")
6466 (setq org-cycle-subtree-status nil
)
6469 (outline-next-heading)
6470 (if (outline-invisible-p) (org-flag-heading nil
))))
6471 ((and (or (>= eol eos
)
6472 (not (string-match "\\S-" (buffer-substring eol eos
))))
6474 (not (setq children-skipped
6475 org-cycle-skip-children-state-if-no-children
))))
6476 ;; Entire subtree is hidden in one line: children view
6477 (run-hook-with-args 'org-pre-cycle-hook
'children
)
6479 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6481 (org-with-limited-levels (show-children))
6482 ;; FIXME: This slows down the func way too much.
6483 ;; How keep drawers hidden in subtree anyway?
6484 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6485 ;; (org-cycle-hide-drawers 'subtree))
6487 ;; Fold every list in subtree to top-level items.
6488 (when (eq org-cycle-include-plain-lists
'integrate
)
6490 (org-back-to-heading)
6491 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6492 (beginning-of-line 1)
6493 (let* ((struct (org-list-struct))
6494 (prevs (org-list-prevs-alist struct
))
6495 (end (org-list-get-bottom-point struct
)))
6496 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6497 (org-list-get-all-items (point) struct prevs
))
6498 (goto-char end
))))))
6499 (message "CHILDREN")
6502 (outline-next-heading)
6503 (if (outline-invisible-p) (org-flag-heading nil
)))
6504 (setq org-cycle-subtree-status
'children
)
6505 (run-hook-with-args 'org-cycle-hook
'children
))
6506 ((or children-skipped
6507 (and (eq last-command this-command
)
6508 (eq org-cycle-subtree-status
'children
)))
6509 ;; We just showed the children, or no children are there,
6510 ;; now show everything.
6511 (run-hook-with-args 'org-pre-cycle-hook
'subtree
)
6512 (outline-flag-region eoh eos nil
)
6513 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6514 (setq org-cycle-subtree-status
'subtree
)
6515 (run-hook-with-args 'org-cycle-hook
'subtree
))
6517 ;; Default action: hide the subtree.
6518 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6519 (outline-flag-region eoh eos t
)
6521 (setq org-cycle-subtree-status
'folded
)
6522 (run-hook-with-args 'org-cycle-hook
'folded
)))))
6525 (defun org-global-cycle (&optional arg
)
6526 "Cycle the global visibility. For details see `org-cycle'.
6527 With \\[universal-argument] prefix arg, switch to startup visibility.
6528 With a numeric prefix, show all headlines up to that level."
6530 (let ((org-cycle-include-plain-lists
6531 (if (derived-mode-p 'org-mode
) org-cycle-include-plain-lists nil
)))
6535 (hide-sublevels arg
)
6536 (setq org-cycle-global-status
'contents
))
6538 (org-set-startup-visibility)
6539 (message "Startup visibility, plus VISIBILITY properties."))
6541 (org-cycle '(4))))))
6543 (defun org-set-startup-visibility ()
6544 "Set the visibility required by startup options and properties."
6546 ((eq org-startup-folded t
)
6548 ((eq org-startup-folded
'content
)
6549 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6550 (org-cycle '(4)) (org-cycle '(4)))))
6551 (unless (eq org-startup-folded
'showeverything
)
6552 (if org-hide-block-startup
(org-hide-block-all))
6553 (org-set-visibility-according-to-property 'no-cleanup
)
6554 (org-cycle-hide-archived-subtrees 'all
)
6555 (org-cycle-hide-drawers 'all
)
6556 (org-cycle-show-empty-lines t
)))
6558 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6559 "Switch subtree visibilities according to :VISIBILITY: property."
6561 (let (org-show-entry-below state
)
6563 (goto-char (point-min))
6564 (while (re-search-forward
6565 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6567 (setq state
(match-string 1))
6569 (org-back-to-heading t
)
6573 ((equal state
'("fold" "folded"))
6575 ((equal state
"children")
6576 (org-show-hidden-entry)
6578 ((equal state
"content")
6581 (org-narrow-to-subtree)
6583 ((member state
'("all" "showall"))
6586 (org-cycle-hide-archived-subtrees 'all
)
6587 (org-cycle-hide-drawers 'all
)
6588 (org-cycle-show-empty-lines 'all
)))))
6590 ;; This function uses outline-regexp instead of the more fundamental
6591 ;; org-outline-regexp so that org-cycle-global works outside of Org
6592 ;; buffers, where outline-regexp is needed.
6593 (defun org-overview ()
6594 "Switch to overview mode, showing only top-level headlines.
6595 Really, this shows all headlines with level equal or greater than the level
6596 of the first headline in the buffer. This is important, because if the
6597 first headline is not level one, then (hide-sublevels 1) gives confusing
6600 (let ((level (save-excursion
6601 (goto-char (point-min))
6602 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6604 (goto-char (match-beginning 0))
6605 (funcall outline-level
))))))
6606 (and level
(hide-sublevels level
))))
6608 (defun org-content (&optional arg
)
6609 "Show all headlines in the buffer, like a table of contents.
6610 With numerical argument N, show content up to level N."
6613 ;; Visit all headings and show their offspring
6614 (and (integerp arg
) (org-overview))
6615 (goto-char (point-max))
6617 (while (and (progn (condition-case nil
6618 (outline-previous-visible-heading 1)
6619 (error (goto-char (point-min))))
6621 (looking-at org-outline-regexp
))
6623 (show-children (1- arg
))
6625 (if (bobp) (throw 'exit nil
))))))
6628 (defun org-optimize-window-after-visibility-change (state)
6629 "Adjust the window after a change in outline visibility.
6630 This function is the default value of the hook `org-cycle-hook'."
6631 (when (get-buffer-window (current-buffer))
6633 ((eq state
'content
) nil
)
6634 ((eq state
'all
) nil
)
6635 ((eq state
'folded
) nil
)
6636 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6637 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6639 (defun org-remove-empty-overlays-at (pos)
6640 "Remove outline overlays that do not contain non-white stuff."
6643 (and (eq 'outline
(overlay-get o
'invisible
))
6644 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6646 (delete-overlay o
)))
6649 (defun org-clean-visibility-after-subtree-move ()
6650 "Fix visibility issues after moving a subtree."
6651 ;; First, find a reasonable region to look at:
6652 ;; Start two siblings above, end three below
6653 (let* ((beg (save-excursion
6654 (and (org-get-last-sibling)
6655 (org-get-last-sibling))
6657 (end (save-excursion
6658 (and (org-get-next-sibling)
6659 (org-get-next-sibling)
6660 (org-get-next-sibling))
6661 (if (org-at-heading-p)
6664 (level (looking-at "\\*+"))
6665 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6668 (narrow-to-region beg end
)
6670 ;; Properly fold already folded siblings
6671 (goto-char (point-min))
6672 (while (re-search-forward re nil t
)
6673 (if (and (not (outline-invisible-p))
6675 (goto-char (point-at-eol)) (outline-invisible-p)))
6677 (org-cycle-show-empty-lines 'overview
)
6678 (org-cycle-hide-drawers 'overview
)))))
6680 (defun org-cycle-show-empty-lines (state)
6681 "Show empty lines above all visible headlines.
6682 The region to be covered depends on STATE when called through
6683 `org-cycle-hook'. Lisp program can use t for STATE to get the
6684 entire buffer covered. Note that an empty line is only shown if there
6685 are at least `org-cycle-separator-lines' empty lines before the headline."
6686 (when (not (= org-cycle-separator-lines
0))
6688 (let* ((n (abs org-cycle-separator-lines
))
6690 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6691 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6692 (t (let ((ns (number-to-string (- n
2))))
6693 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6694 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6697 ((memq state
'(overview contents t
))
6698 (setq beg
(point-min) end
(point-max)))
6699 ((memq state
'(children folded
))
6700 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6701 (beginning-of-line 2)
6705 (while (re-search-forward re end t
)
6706 (unless (get-char-property (match-end 1) 'invisible
)
6707 (setq e
(match-end 1))
6708 (if (< org-cycle-separator-lines
0)
6709 (setq b
(save-excursion
6710 (goto-char (match-beginning 0))
6711 (org-back-over-empty-lines)
6713 (goto-char (max (point-min) (1- (point))))
6717 (setq b
(match-beginning 1)))
6718 (outline-flag-region b e nil
)))))))
6719 ;; Never hide empty lines at the end of the file.
6721 (goto-char (point-max))
6722 (outline-previous-heading)
6723 (outline-end-of-heading)
6724 (if (and (looking-at "[ \t\n]+")
6725 (= (match-end 0) (point-max)))
6726 (outline-flag-region (point) (match-end 0) nil
))))
6728 (defun org-show-empty-lines-in-parent ()
6729 "Move to the parent and re-show empty lines before visible headlines."
6731 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6732 (org-cycle-show-empty-lines context
))))
6734 (defun org-files-list ()
6735 "Return `org-agenda-files' list, plus all open org-mode files.
6736 This is useful for operations that need to scan all of a user's
6737 open and agenda-wise Org files."
6738 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6739 (dolist (buf (buffer-list))
6740 (with-current-buffer buf
6741 (if (and (derived-mode-p 'org-mode
) (buffer-file-name))
6742 (let ((file (expand-file-name (buffer-file-name))))
6743 (unless (member file files
)
6744 (push file files
))))))
6747 (defsubst org-entry-beginning-position
()
6748 "Return the beginning position of the current entry."
6749 (save-excursion (outline-back-to-heading t
) (point)))
6751 (defsubst org-entry-end-position
()
6752 "Return the end position of the current entry."
6753 (save-excursion (outline-next-heading) (point)))
6755 (defun org-cycle-hide-drawers (state)
6756 "Re-hide all drawers after a visibility state change."
6757 (when (and (derived-mode-p 'org-mode
)
6758 (not (memq state
'(overview folded contents
))))
6760 (let* ((globalp (memq state
'(contents all
)))
6761 (beg (if globalp
(point-min) (point)))
6762 (end (if globalp
(point-max)
6763 (if (eq state
'children
)
6764 (save-excursion (outline-next-heading) (point))
6765 (org-end-of-subtree t
)))))
6767 (while (re-search-forward org-drawer-regexp end t
)
6768 (org-flag-drawer t
))))))
6770 (defun org-flag-drawer (flag)
6771 "When FLAG is non-nil, hide the drawer we are within.
6772 Otherwise make it visible."
6774 (beginning-of-line 1)
6775 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6776 (let ((b (match-end 0)))
6777 (if (re-search-forward
6779 (save-excursion (outline-next-heading) (point)) t
)
6780 (outline-flag-region b
(point-at-eol) flag
)
6781 (error ":END: line missing at position %s" b
))))))
6783 (defun org-subtree-end-visible-p ()
6784 "Is the end of the current subtree visible?"
6785 (pos-visible-in-window-p
6786 (save-excursion (org-end-of-subtree t
) (point))))
6788 (defun org-first-headline-recenter (&optional N
)
6789 "Move cursor to the first headline and recenter the headline.
6790 Optional argument N means put the headline into the Nth line of the window."
6791 (goto-char (point-min))
6792 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
6794 (recenter (prefix-numeric-value N
))))
6796 ;;; Saving and restoring visibility
6798 (defun org-outline-overlay-data (&optional use-markers
)
6799 "Return a list of the locations of all outline overlays.
6800 These are overlays with the `invisible' property value `outline'.
6801 The return value is a list of cons cells, with start and stop
6802 positions for each overlay.
6803 If USE-MARKERS is set, return the positions as markers."
6810 (when (eq (overlay-get o
'invisible
) 'outline
)
6811 (setq beg
(overlay-start o
)
6812 end
(overlay-end o
))
6813 (and beg end
(> end beg
)
6815 (cons (move-marker (make-marker) beg
)
6816 (move-marker (make-marker) end
))
6818 (overlays-in (point-min) (point-max))))))))
6820 (defun org-set-outline-overlay-data (data)
6821 "Create visibility overlays for all positions in DATA.
6822 DATA should have been made by `org-outline-overlay-data'."
6829 (outline-flag-region (car c
) (cdr c
) t
))
6832 ;;; Folding of blocks
6834 (defvar org-hide-block-overlays nil
6835 "Overlays hiding blocks.")
6836 (make-variable-buffer-local 'org-hide-block-overlays
)
6838 (defun org-block-map (function &optional start end
)
6839 "Call FUNCTION at the head of all source blocks in the current buffer.
6840 Optional arguments START and END can be used to limit the range."
6841 (let ((start (or start
(point-min)))
6842 (end (or end
(point-max))))
6845 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6848 (goto-char (match-beginning 0))
6849 (funcall function
)))))))
6851 (defun org-hide-block-toggle-all ()
6852 "Toggle the visibility of all blocks in the current buffer."
6853 (org-block-map #'org-hide-block-toggle
))
6855 (defun org-hide-block-all ()
6856 "Fold all blocks in the current buffer."
6858 (org-show-block-all)
6859 (org-block-map #'org-hide-block-toggle-maybe
))
6861 (defun org-show-block-all ()
6862 "Unfold all blocks in the current buffer."
6864 (mapc 'delete-overlay org-hide-block-overlays
)
6865 (setq org-hide-block-overlays nil
))
6867 (defun org-hide-block-toggle-maybe ()
6868 "Toggle visibility of block at point."
6870 (let ((case-fold-search t
))
6872 (beginning-of-line 1)
6873 (looking-at org-block-regexp
))
6874 (progn (org-hide-block-toggle)
6875 t
) ;; to signal that we took action
6876 nil
))) ;; to signal that we did not
6878 (defun org-hide-block-toggle (&optional force
)
6879 "Toggle the visibility of the current block."
6883 (if (re-search-forward org-block-regexp nil t
)
6884 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
6885 (end (match-end 0)) ;; end of entire body
6887 (if (memq t
(mapcar (lambda (overlay)
6888 (eq (overlay-get overlay
'invisible
)
6890 (overlays-at start
)))
6891 (if (or (not force
) (eq force
'off
))
6893 (when (member ov org-hide-block-overlays
)
6894 (setq org-hide-block-overlays
6895 (delq ov org-hide-block-overlays
)))
6896 (when (eq (overlay-get ov
'invisible
)
6898 (delete-overlay ov
)))
6899 (overlays-at start
)))
6900 (setq ov
(make-overlay start end
))
6901 (overlay-put ov
'invisible
'org-hide-block
)
6902 ;; make the block accessible to isearch
6904 ov
'isearch-open-invisible
6906 (when (member ov org-hide-block-overlays
)
6907 (setq org-hide-block-overlays
6908 (delq ov org-hide-block-overlays
)))
6909 (when (eq (overlay-get ov
'invisible
)
6911 (delete-overlay ov
))))
6912 (push ov org-hide-block-overlays
)))
6913 (error "Not looking at a source block"))))
6915 ;; org-tab-after-check-for-cycling-hook
6916 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
6917 ;; Remove overlays when changing major mode
6918 (add-hook 'org-mode-hook
6919 (lambda () (org-add-hook 'change-major-mode-hook
6920 'org-show-block-all
'append
'local
)))
6924 (defvar org-goto-window-configuration nil
)
6925 (defvar org-goto-marker nil
)
6926 (defvar org-goto-map
6927 (let ((map (make-sparse-keymap)))
6928 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur
)) cmd
)
6929 (while (setq cmd
(pop cmds
))
6930 (substitute-key-definition cmd cmd map global-map
)))
6931 (suppress-keymap map
)
6932 (org-defkey map
"\C-m" 'org-goto-ret
)
6933 (org-defkey map
[(return)] 'org-goto-ret
)
6934 (org-defkey map
[(left)] 'org-goto-left
)
6935 (org-defkey map
[(right)] 'org-goto-right
)
6936 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
6937 (org-defkey map
"\C-i" 'org-cycle
)
6938 (org-defkey map
[(tab)] 'org-cycle
)
6939 (org-defkey map
[(down)] 'outline-next-visible-heading
)
6940 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
6941 (if org-goto-auto-isearch
6942 (if (fboundp 'define-key-after
)
6943 (define-key-after map
[t] 'org-goto-local-auto-isearch)
6945 (org-defkey map "q" 'org-goto-quit)
6946 (org-defkey map "n" 'outline-next-visible-heading)
6947 (org-defkey map "p" 'outline-previous-visible-heading)
6948 (org-defkey map "f" 'outline-forward-same-level)
6949 (org-defkey map "b" 'outline-backward-same-level)
6950 (org-defkey map "u" 'outline-up-heading))
6951 (org-defkey map "/" 'org-occur)
6952 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6953 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6954 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6955 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6956 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6959 (defconst org-goto-help
6960 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6961 RET=jump to location [Q]uit and return to previous location
6962 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6964 (defvar org-goto-start-pos) ; dynamically scoped parameter
6966 ;; FIXME: Docstring does not mention both interfaces
6967 (defun org-goto (&optional alternative-interface)
6968 "Look up a different location in the current file, keeping current visibility.
6970 When you want look-up or go to a different location in a
6971 document, the fastest way is often to fold the entire buffer and
6972 then dive into the tree. This method has the disadvantage, that
6973 the previous location will be folded, which may not be what you
6976 This command works around this by showing a copy of the current
6977 buffer in an indirect buffer, in overview mode. You can dive
6978 into the tree in that copy, use org-occur and incremental search
6979 to find a location. When pressing RET or `Q', the command
6980 returns to the original buffer in which the visibility is still
6981 unchanged. After RET it will also jump to the location selected
6982 in the indirect buffer and expose the headline hierarchy above.
6984 With a prefix argument, use the alternative interface: e.g. if
6985 `org-goto-interface' is 'outline use 'outline-path-completion."
6987 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6988 (org-refile-use-outline-path t)
6989 (org-refile-target-verify-function nil)
6991 (if (not alternative-interface)
6993 (if (eq org-goto-interface 'outline)
6994 'outline-path-completion
6996 (org-goto-start-pos (point))
6998 (if (eq interface 'outline)
6999 (car (org-get-location (current-buffer) org-goto-help))
7000 (let ((pa (org-refile-get-location "Goto" nil nil t)))
7001 (org-refile-check-position pa)
7005 (org-mark-ring-push org-goto-start-pos)
7006 (goto-char selected-point)
7007 (if (or (outline-invisible-p) (org-invisible-p2))
7008 (org-show-context 'org-goto)))
7011 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
7012 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
7013 (defvar org-goto-local-auto-isearch-map) ; defined below
7015 (defun org-get-location (buf help)
7016 "Let the user select a location in the Org-mode buffer BUF.
7017 This function uses a recursive edit. It returns the selected position
7019 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
7020 (isearch-hide-immediately nil)
7021 (isearch-search-fun-function
7022 (lambda () 'org-goto-local-search-headings))
7023 (org-goto-selected-point org-goto-exit-command)
7025 (special-display-buffer-names nil)
7026 (special-display-regexps nil)
7027 (special-display-function nil))
7029 (save-window-excursion
7030 (delete-other-windows)
7031 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
7032 (org-pop-to-buffer-same-window
7034 (make-indirect-buffer (current-buffer) "*org-goto*")
7035 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
7036 (with-output-to-temp-buffer "*Help*"
7038 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
7039 (setq buffer-read-only nil)
7040 (let ((org-startup-truncated t)
7041 (org-startup-folded nil)
7042 (org-startup-align-all-tables nil))
7045 (setq buffer-read-only t)
7046 (if (and (boundp 'org-goto-start-pos)
7047 (integer-or-marker-p org-goto-start-pos))
7048 (let ((org-show-hierarchy-above t)
7049 (org-show-siblings t)
7050 (org-show-following-heading t))
7051 (goto-char org-goto-start-pos)
7052 (and (outline-invisible-p) (org-show-context)))
7053 (goto-char (point-min)))
7054 (let (org-special-ctrl-a/e) (org-beginning-of-line))
7055 (message "Select location and press RET")
7056 (use-local-map org-goto-map)
7059 (kill-buffer "*org-goto*")
7060 (cons org-goto-selected-point org-goto-exit-command)))
7062 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
7063 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
7064 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
7065 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
7067 (defun org-goto-local-search-headings (string bound noerror)
7068 "Search and make sure that any matches are in headlines."
7070 (while (if isearch-forward
7071 (search-forward string bound noerror)
7072 (search-backward string bound noerror))
7073 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
7074 (and (member :headline context)
7075 (not (member :tags context))))
7076 (throw 'return (point))))))
7078 (defun org-goto-local-auto-isearch ()
7081 (goto-char (point-min))
7082 (let ((keys (this-command-keys)))
7083 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
7085 (isearch-process-search-char (string-to-char keys)))))
7087 (defun org-goto-ret (&optional arg)
7088 "Finish `org-goto' by going to the new location."
7090 (setq org-goto-selected-point (point)
7091 org-goto-exit-command 'return)
7094 (defun org-goto-left ()
7095 "Finish `org-goto' by going to the new location."
7097 (if (org-at-heading-p)
7099 (beginning-of-line 1)
7100 (setq org-goto-selected-point (point)
7101 org-goto-exit-command 'left)
7103 (error "Not on a heading")))
7105 (defun org-goto-right ()
7106 "Finish `org-goto' by going to the new location."
7108 (if (org-at-heading-p)
7110 (setq org-goto-selected-point (point)
7111 org-goto-exit-command 'right)
7113 (error "Not on a heading")))
7115 (defun org-goto-quit ()
7116 "Finish `org-goto' without cursor motion."
7118 (setq org-goto-selected-point nil)
7119 (setq org-goto-exit-command 'quit)
7122 ;;; Indirect buffer display of subtrees
7124 (defvar org-indirect-dedicated-frame nil
7125 "This is the frame being used for indirect tree display.")
7126 (defvar org-last-indirect-buffer nil)
7128 (defun org-tree-to-indirect-buffer (&optional arg)
7129 "Create indirect buffer and narrow it to current subtree.
7130 With numerical prefix ARG, go up to this level and then take that tree.
7131 If ARG is negative, go up that many levels.
7132 If `org-indirect-buffer-display' is not `new-frame', the command removes the
7133 indirect buffer previously made with this command, to avoid proliferation of
7134 indirect buffers. However, when you call the command with a \
7135 \\[universal-argument] prefix, or
7136 when `org-indirect-buffer-display' is `new-frame', the last buffer
7137 is kept so that you can work with several indirect buffers at the same time.
7138 If `org-indirect-buffer-display' is `dedicated-frame', the \
7139 \\[universal-argument] prefix also
7140 requests that a new frame be made for the new buffer, so that the dedicated
7141 frame is not changed."
7143 (let ((cbuf (current-buffer))
7144 (cwin (selected-window))
7146 beg end level heading ibuf)
7148 (org-back-to-heading t)
7150 (setq level (org-outline-level))
7151 (if (< arg 0) (setq arg (+ level arg)))
7152 (while (> (setq level (org-outline-level)) arg)
7153 (outline-up-heading 1 t)))
7155 heading (org-get-heading))
7156 (org-end-of-subtree t t)
7157 (if (org-at-heading-p) (backward-char 1))
7159 (if (and (buffer-live-p org-last-indirect-buffer)
7160 (not (eq org-indirect-buffer-display 'new-frame))
7162 (kill-buffer org-last-indirect-buffer))
7163 (setq ibuf (org-get-indirect-buffer cbuf)
7164 org-last-indirect-buffer ibuf)
7166 ((or (eq org-indirect-buffer-display 'new-frame)
7167 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7168 (select-frame (make-frame))
7169 (delete-other-windows)
7170 (org-pop-to-buffer-same-window ibuf)
7171 (org-set-frame-title heading))
7172 ((eq org-indirect-buffer-display 'dedicated-frame)
7174 (select-frame (or (and org-indirect-dedicated-frame
7175 (frame-live-p org-indirect-dedicated-frame)
7176 org-indirect-dedicated-frame)
7177 (setq org-indirect-dedicated-frame (make-frame)))))
7178 (delete-other-windows)
7179 (org-pop-to-buffer-same-window ibuf)
7180 (org-set-frame-title (concat "Indirect: " heading)))
7181 ((eq org-indirect-buffer-display 'current-window)
7182 (org-pop-to-buffer-same-window ibuf))
7183 ((eq org-indirect-buffer-display 'other-window)
7184 (pop-to-buffer ibuf))
7185 (t (error "Invalid value")))
7186 (if (featurep 'xemacs)
7187 (save-excursion (org-mode) (turn-on-font-lock)))
7188 (narrow-to-region beg end)
7191 (run-hook-with-args 'org-cycle-hook 'all)
7192 (and (window-live-p cwin) (select-window cwin))))
7194 (defun org-get-indirect-buffer (&optional buffer)
7195 (setq buffer (or buffer (current-buffer)))
7196 (let ((n 1) (base (buffer-name buffer)) bname)
7197 (while (buffer-live-p
7198 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7201 (make-indirect-buffer buffer bname 'clone)
7202 (error (make-indirect-buffer buffer bname)))))
7204 (defun org-set-frame-title (title)
7205 "Set the title of the current frame to the string TITLE."
7206 ;; FIXME: how to name a single frame in XEmacs???
7207 (unless (featurep 'xemacs)
7208 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7210 ;;;; Structure editing
7212 ;;; Inserting headlines
7214 (defun org-previous-line-empty-p ()
7217 (or (beginning-of-line 0) t)
7219 (looking-at "[ \t]*$")))))
7221 (defun org-insert-heading (&optional force-heading invisible-ok)
7222 "Insert a new heading or item with same depth at point.
7223 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
7224 If point is at the beginning of a headline, insert a sibling before the
7225 current headline. If point is not at the beginning, split the line,
7226 create the new headline with the text in the current line after point
7227 \(but see also the variable `org-M-RET-may-split-line').
7229 When INVISIBLE-OK is set, stop at invisible headlines when going back.
7230 This is important for non-interactive uses of the command."
7232 (if (or (= (buffer-size) 0)
7233 (and (not (save-excursion
7234 (and (ignore-errors (org-back-to-heading invisible-ok))
7235 (org-at-heading-p))))
7236 (or force-heading (not (org-in-item-p)))))
7239 (run-hooks 'org-insert-heading-hook))
7240 (when (or force-heading (not (org-insert-item)))
7241 (let* ((empty-line-p nil)
7243 (on-heading (org-at-heading-p))
7244 (head (save-excursion
7247 (org-back-to-heading invisible-ok)
7248 (when (and (not on-heading)
7249 (featurep 'org-inlinetask)
7250 (integerp org-inlinetask-min-level)
7251 (>= (length (match-string 0))
7252 org-inlinetask-min-level))
7253 ;; Find a heading level before the inline task
7254 (while (and (setq level (org-up-heading-safe))
7255 (>= level org-inlinetask-min-level)))
7256 (if (org-at-heading-p)
7257 (org-back-to-heading invisible-ok)
7258 (error "This should not happen")))
7259 (setq empty-line-p (org-previous-line-empty-p))
7262 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7263 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7264 pos hide-previous previous-pos)
7266 ((and (org-at-heading-p) (bolp)
7268 (save-excursion (backward-char 1) (not (outline-invisible-p)))))
7269 ;; insert before the current line
7270 (open-line (if blank 2 1)))
7272 (not org-insert-heading-respect-content)
7275 (backward-char 1) (not (outline-invisible-p)))))
7276 ;; insert right here
7279 ;; somewhere in the line
7281 (setq previous-pos (point-at-bol))
7283 (setq hide-previous (outline-invisible-p)))
7284 (and org-insert-heading-respect-content (org-show-subtree))
7286 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
7289 (goto-char (point-at-bol))
7290 (and (looking-at org-complex-heading-regexp)
7292 (> p (match-beginning 4)))))))
7295 (org-insert-heading-respect-content
7296 (org-end-of-subtree nil t)
7297 (when (featurep 'org-inlinetask)
7298 (while (and (not (eobp))
7299 (looking-at "\\(\\*+\\)[ \t]+")
7300 (>= (length (match-string 1))
7301 org-inlinetask-min-level))
7302 (org-end-of-subtree nil t)))
7303 (or (bolp) (newline))
7304 (or (org-previous-line-empty-p)
7305 (and blank (newline)))
7311 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
7312 (setq tags (and (match-end 2) (match-string 2)))
7314 (delete-region (match-beginning 1) (match-end 1)))
7315 (setq pos (point-at-bol))
7316 (or split (end-of-line 1))
7317 (delete-horizontal-space)
7318 (if (string-match "\\`\\*+\\'"
7319 (buffer-substring (point-at-bol) (point)))
7321 (newline (if blank 2 1))
7327 (org-set-tags nil 'align))))
7329 (or split (end-of-line 1))
7330 (newline (if blank 2 1)))))))
7331 (insert head) (just-one-space)
7334 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
7335 (when (and org-insert-heading-respect-content hide-previous)
7337 (goto-char previous-pos)
7339 (run-hooks 'org-insert-heading-hook)))))
7341 (defun org-get-heading (&optional no-tags no-todo)
7342 "Return the heading of the current entry, without the stars.
7343 When NO-TAGS is non-nil, don't include tags.
7344 When NO-TODO is non-nil, don't include TODO keywords."
7346 (org-back-to-heading t)
7348 ((and no-tags no-todo)
7349 (looking-at org-complex-heading-regexp)
7352 (looking-at (concat org-outline-regexp
7354 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7357 (looking-at org-todo-line-regexp)
7359 (t (looking-at org-heading-regexp)
7360 (match-string 2)))))
7362 (defun org-heading-components ()
7363 "Return the components of the current heading.
7364 This is a list with the following elements:
7365 - the level as an integer
7366 - the reduced level, different if `org-odd-levels-only' is set.
7367 - the TODO keyword, or nil
7368 - the priority character, like ?A, or nil if no priority is given
7369 - the headline text itself, or the tags string if no headline text
7370 - the tags string, or nil."
7372 (org-back-to-heading t)
7373 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
7374 (list (length (match-string 1))
7375 (org-reduced-level (length (match-string 1)))
7376 (org-match-string-no-properties 2)
7377 (and (match-end 3) (aref (match-string 3) 2))
7378 (org-match-string-no-properties 4)
7379 (org-match-string-no-properties 5)))))
7381 (defun org-get-entry ()
7382 "Get the entry text, after heading, entire subtree."
7384 (org-back-to-heading t)
7385 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7387 (defun org-insert-heading-after-current ()
7388 "Insert a new heading with same level as current, after current subtree."
7390 (org-back-to-heading)
7391 (org-insert-heading)
7392 (org-move-subtree-down)
7395 (defun org-insert-heading-respect-content ()
7397 (let ((org-insert-heading-respect-content t))
7398 (org-insert-heading t)))
7400 (defun org-insert-todo-heading-respect-content (&optional force-state)
7402 (let ((org-insert-heading-respect-content t))
7403 (org-insert-todo-heading force-state t)))
7405 (defun org-insert-todo-heading (arg &optional force-heading)
7406 "Insert a new heading with the same level and TODO state as current heading.
7407 If the heading has no TODO state, or if the state is DONE, use the first
7408 state (TODO by default). Also with prefix arg, force first state."
7410 (when (or force-heading (not (org-insert-item 'checkbox)))
7411 (org-insert-heading force-heading)
7413 (org-back-to-heading)
7414 (outline-previous-heading)
7415 (looking-at org-todo-line-regexp))
7419 (not (match-beginning 2))
7420 (member (match-string 2) org-done-keywords))
7421 (car org-todo-keywords-1)
7425 (run-hook-with-args-until-success
7426 'org-todo-get-default-hook new-mark-x nil)
7428 (beginning-of-line 1)
7429 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7430 (if org-treat-insert-todo-heading-as-state-change
7432 (insert new-mark " "))))
7433 (when org-provide-todo-statistics
7434 (org-update-parent-todo-statistics))))
7436 (defun org-insert-subheading (arg)
7437 "Insert a new subheading and demote it.
7438 Works for outline headings and for plain lists alike."
7440 (org-insert-heading arg)
7442 ((org-at-heading-p) (org-do-demote))
7443 ((org-at-item-p) (org-indent-item))))
7445 (defun org-insert-todo-subheading (arg)
7446 "Insert a new subheading with TODO keyword or checkbox and demote it.
7447 Works for outline headings and for plain lists alike."
7449 (org-insert-todo-heading arg)
7451 ((org-at-heading-p) (org-do-demote))
7452 ((org-at-item-p) (org-indent-item))))
7454 ;;; Promotion and Demotion
7456 (defvar org-after-demote-entry-hook nil
7457 "Hook run after an entry has been demoted.
7458 The cursor will be at the beginning of the entry.
7459 When a subtree is being demoted, the hook will be called for each node.")
7461 (defvar org-after-promote-entry-hook nil
7462 "Hook run after an entry has been promoted.
7463 The cursor will be at the beginning of the entry.
7464 When a subtree is being promoted, the hook will be called for each node.")
7466 (defun org-promote-subtree ()
7467 "Promote the entire subtree.
7468 See also `org-promote'."
7471 (org-with-limited-levels (org-map-tree 'org-promote)))
7472 (org-fix-position-after-promote))
7474 (defun org-demote-subtree ()
7475 "Demote the entire subtree. See `org-demote'.
7476 See also `org-promote'."
7479 (org-with-limited-levels (org-map-tree 'org-demote)))
7480 (org-fix-position-after-promote))
7483 (defun org-do-promote ()
7484 "Promote the current heading higher up the tree.
7485 If the region is active in `transient-mark-mode', promote all headings
7489 (if (org-region-active-p)
7490 (org-map-region 'org-promote (region-beginning) (region-end))
7492 (org-fix-position-after-promote))
7494 (defun org-do-demote ()
7495 "Demote the current heading lower down the tree.
7496 If the region is active in `transient-mark-mode', demote all headings
7500 (if (org-region-active-p)
7501 (org-map-region 'org-demote (region-beginning) (region-end))
7503 (org-fix-position-after-promote))
7505 (defun org-fix-position-after-promote ()
7506 "Make sure that after pro/demotion cursor position is right."
7507 (let ((pos (point)))
7508 (when (save-excursion
7509 (beginning-of-line 1)
7510 (looking-at org-todo-line-regexp)
7511 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7512 (cond ((eobp) (insert " "))
7513 ((eolp) (insert " "))
7514 ((equal (char-after) ?\ ) (forward-char 1))))))
7516 (defun org-current-level ()
7517 "Return the level of the current entry, or nil if before the first headline.
7518 The level is the number of stars at the beginning of the headline."
7520 (org-with-limited-levels
7521 (if (ignore-errors (org-back-to-heading t))
7522 (funcall outline-level)))))
7524 (defun org-get-previous-line-level ()
7525 "Return the outline depth of the last headline before the current line.
7526 Returns 0 for the first headline in the buffer, and nil if before the
7528 (let ((current-level (org-current-level))
7529 (prev-level (when (> (line-number-at-pos) 1)
7531 (beginning-of-line 0)
7532 (org-current-level)))))
7533 (cond ((null current-level) nil) ; Before first headline
7534 ((null prev-level) 0) ; At first headline
7537 (defun org-reduced-level (l)
7538 "Compute the effective level of a heading.
7539 This takes into account the setting of `org-odd-levels-only'."
7542 (org-odd-levels-only (1+ (floor (/ l 2))))
7545 (defun org-level-increment ()
7546 "Return the number of stars that will be added or removed at a
7547 time to headlines when structure editing, based on the value of
7548 `org-odd-levels-only'."
7549 (if org-odd-levels-only 2 1))
7551 (defun org-get-valid-level (level &optional change)
7552 "Rectify a level change under the influence of `org-odd-levels-only'
7553 LEVEL is a current level, CHANGE is by how much the level should be
7554 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7555 even level numbers will become the next higher odd number."
7556 (if org-odd-levels-only
7557 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7558 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7559 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7560 (max 1 (+ level (or change 0)))))
7562 (if (boundp 'define-obsolete-function-alias)
7563 (if (or (featurep 'xemacs) (< emacs-major-version 23))
7564 (define-obsolete-function-alias 'org-get-legal-level
7565 'org-get-valid-level)
7566 (define-obsolete-function-alias 'org-get-legal-level
7567 'org-get-valid-level "23.1")))
7569 (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
7570 ;; ̀org-with-limited-levels'
7571 (defun org-promote ()
7572 "Promote the current heading higher up the tree.
7573 If the region is active in `transient-mark-mode', promote all headings
7575 (org-back-to-heading t)
7576 (let* ((level (save-match-data (funcall outline-level)))
7577 (after-change-functions (remove 'flyspell-after-change-function
7578 after-change-functions))
7579 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7580 (diff (abs (- level (length up-head) -1))))
7581 (cond ((and (= level 1) org-called-with-limited-levels
7582 org-allow-promoting-top-level-subtree)
7583 (replace-match "# " nil t))
7585 (error "Cannot promote to level 0. UNDO to recover if necessary"))
7586 (t (replace-match up-head nil t)))
7587 ;; Fixup tag positioning
7589 (and org-auto-align-tags (org-set-tags nil t))
7590 (if org-adapt-indentation (org-fixup-indentation (- diff))))
7591 (run-hooks 'org-after-promote-entry-hook)))
7593 (defun org-demote ()
7594 "Demote the current heading lower down the tree.
7595 If the region is active in `transient-mark-mode', demote all headings
7597 (org-back-to-heading t)
7598 (let* ((level (save-match-data (funcall outline-level)))
7599 (after-change-functions (remove 'flyspell-after-change-function
7600 after-change-functions))
7601 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7602 (diff (abs (- level (length down-head) -1))))
7603 (replace-match down-head nil t)
7604 ;; Fixup tag positioning
7605 (and org-auto-align-tags (org-set-tags nil t))
7606 (if org-adapt-indentation (org-fixup-indentation diff))
7607 (run-hooks 'org-after-demote-entry-hook)))
7609 (defun org-cycle-level ()
7610 "Cycle the level of an empty headline through possible states.
7611 This goes first to child, then to parent, level, then up the hierarchy.
7612 After top level, it switches back to sibling level."
7614 (let ((org-adapt-indentation nil))
7615 (when (org-point-at-end-of-empty-headline)
7616 (setq this-command 'org-cycle-level) ; Only needed for caching
7617 (let ((cur-level (org-current-level))
7618 (prev-level (org-get-previous-line-level)))
7620 ;; If first headline in file, promote to top-level.
7622 (loop repeat (/ (- cur-level 1) (org-level-increment))
7623 do (org-do-promote)))
7624 ;; If same level as prev, demote one.
7625 ((= prev-level cur-level)
7627 ;; If parent is top-level, promote to top level if not already.
7629 (loop repeat (/ (- cur-level 1) (org-level-increment))
7630 do (org-do-promote)))
7631 ;; If top-level, return to prev-level.
7633 (loop repeat (/ (- prev-level 1) (org-level-increment))
7634 do (org-do-demote)))
7635 ;; If less than prev-level, promote one.
7636 ((< cur-level prev-level)
7638 ;; If deeper than prev-level, promote until higher than
7640 ((> cur-level prev-level)
7641 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7642 do (org-do-promote))))
7645 (defun org-map-tree (fun)
7646 "Call FUN for every heading underneath the current one."
7647 (org-back-to-heading)
7648 (let ((level (funcall outline-level)))
7652 (outline-next-heading)
7653 (> (funcall outline-level) level))
7657 (defun org-map-region (fun beg end)
7658 "Call FUN for every heading between BEG and END."
7659 (let ((org-ignore-region t))
7661 (setq end (copy-marker end))
7663 (if (and (re-search-forward org-outline-regexp-bol nil t)
7667 (outline-next-heading)
7672 (defvar org-property-end-re) ; silence byte-compiler
7673 (defun org-fixup-indentation (diff)
7674 "Change the indentation in the current entry by DIFF.
7675 However, if any line in the current entry has no indentation, or if it
7676 would end up with no indentation after the change, nothing at all is done."
7678 (let ((end (save-excursion (outline-next-heading)
7680 (prohibit (if (> diff 0)
7682 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7684 (unless (save-excursion (end-of-line 1)
7685 (re-search-forward prohibit end t))
7686 (while (and (< (point) end)
7687 (re-search-forward "^[ \t]+" end t))
7688 (goto-char (match-end 0))
7689 (setq col (current-column))
7690 (if (< diff 0) (replace-match ""))
7691 (org-indent-to-column (+ diff col))))
7692 (move-marker end nil))))
7694 (defun org-convert-to-odd-levels ()
7695 "Convert an org-mode file with all levels allowed to one with odd levels.
7696 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7699 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7700 (let ((outline-level 'org-outline-level)
7701 (org-odd-levels-only nil) n)
7703 (goto-char (point-min))
7704 (while (re-search-forward "^\\*\\*+ " nil t)
7705 (setq n (- (length (match-string 0)) 2))
7706 (while (>= (setq n (1- n)) 0)
7708 (end-of-line 1))))))
7710 (defun org-convert-to-oddeven-levels ()
7711 "Convert an org-mode file with only odd levels to one with odd/even levels.
7712 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7713 file contains a section with an even level, conversion would
7714 destroy the structure of the file. An error is signaled in this
7717 (goto-char (point-min))
7718 ;; First check if there are no even levels
7719 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7720 (org-show-context t)
7721 (error "Not all levels are odd in this file. Conversion not possible"))
7722 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7723 (let ((outline-regexp org-outline-regexp)
7724 (outline-level 'org-outline-level)
7725 (org-odd-levels-only nil) n)
7727 (goto-char (point-min))
7728 (while (re-search-forward "^\\*\\*+ " nil t)
7729 (setq n (/ (1- (length (match-string 0))) 2))
7730 (while (>= (setq n (1- n)) 0)
7732 (end-of-line 1))))))
7734 (defun org-tr-level (n)
7735 "Make N odd if required."
7736 (if org-odd-levels-only (1+ (/ n 2)) n))
7738 ;;; Vertical tree motion, cutting and pasting of subtrees
7740 (defun org-move-subtree-up (&optional arg)
7741 "Move the current subtree up past ARG headlines of the same level."
7743 (org-move-subtree-down (- (prefix-numeric-value arg))))
7745 (defun org-move-subtree-down (&optional arg)
7746 "Move the current subtree down past ARG headlines of the same level."
7748 (setq arg (prefix-numeric-value arg))
7749 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7750 'org-get-last-sibling))
7751 (ins-point (make-marker))
7753 (col (current-column))
7754 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7756 (org-back-to-heading)
7759 (setq ne-beg (org-back-over-empty-lines))
7762 (save-excursion (outline-end-of-heading)
7763 (setq folded (outline-invisible-p)))
7764 (outline-end-of-subtree))
7765 (outline-next-heading)
7766 (setq ne-end (org-back-over-empty-lines))
7769 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7770 ;; include less whitespace
7773 (forward-line (- ne-beg ne-end))
7774 (setq beg (point))))
7775 ;; Find insertion point, with error handling
7777 (or (and (funcall movfunc) (looking-at org-outline-regexp))
7778 (progn (goto-char beg0)
7779 (error "Cannot move past superior level or buffer limit")))
7780 (setq cnt (1- cnt)))
7782 ;; Moving forward - still need to move over subtree
7783 (progn (org-end-of-subtree t t)
7785 (org-back-over-empty-lines)
7786 (or (bolp) (newline)))))
7787 (setq ne-ins (org-back-over-empty-lines))
7788 (move-marker ins-point (point))
7789 (setq txt (buffer-substring beg end))
7790 (org-save-markers-in-region beg end)
7791 (delete-region beg end)
7792 (org-remove-empty-overlays-at beg)
7793 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7794 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7795 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7796 (let ((bbb (point)))
7797 (insert-before-markers txt)
7798 (org-reinstall-markers-in-region bbb)
7799 (move-marker ins-point bbb))
7800 (or (bolp) (insert "\n"))
7801 (setq ins-end (point))
7802 (goto-char ins-point)
7803 (org-skip-whitespace)
7804 (when (and (< arg 0)
7805 (org-first-sibling-p)
7807 ;; Move whitespace back to beginning
7810 (let ((kill-whole-line t))
7811 (kill-line (- ne-ins ne-beg)) (point)))
7812 (insert (make-string (- ne-ins ne-beg) ?\n)))
7813 (move-marker ins-point nil)
7818 (org-cycle-hide-drawers 'children))
7819 (org-clean-visibility-after-subtree-move)
7820 ;; move back to the initial column we were at
7821 (move-to-column col)))
7823 (defvar org-subtree-clip ""
7824 "Clipboard for cut and paste of subtrees.
7825 This is actually only a copy of the kill, because we use the normal kill
7826 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7828 (defvar org-subtree-clip-folded nil
7829 "Was the last copied subtree folded?
7830 This is used to fold the tree back after pasting.")
7832 (defun org-cut-subtree (&optional n)
7833 "Cut the current subtree into the clipboard.
7834 With prefix arg N, cut this many sequential subtrees.
7835 This is a short-hand for marking the subtree and then cutting it."
7837 (org-copy-subtree n 'cut))
7839 (defun org-copy-subtree (&optional n cut force-store-markers)
7840 "Cut the current subtree into the clipboard.
7841 With prefix arg N, cut this many sequential subtrees.
7842 This is a short-hand for marking the subtree and then copying it.
7843 If CUT is non-nil, actually cut the subtree.
7844 If FORCE-STORE-MARKERS is non-nil, store the relative locations
7845 of some markers in the region, even if CUT is non-nil. This is
7846 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
7848 (let (beg end folded (beg0 (point)))
7849 (if (org-called-interactively-p 'any)
7850 (org-back-to-heading nil) ; take what looks like a subtree
7851 (org-back-to-heading t)) ; take what is really there
7852 (org-back-over-empty-lines)
7854 (skip-chars-forward " \t\r\n")
7856 (save-excursion (outline-end-of-heading)
7857 (setq folded (outline-invisible-p)))
7859 (org-forward-heading-same-level (1- n) t)
7861 (org-end-of-subtree t t))
7862 (org-back-over-empty-lines)
7866 (setq org-subtree-clip-folded folded)
7867 (when (or cut force-store-markers)
7868 (org-save-markers-in-region beg end))
7869 (if cut (kill-region beg end) (copy-region-as-kill beg end))
7870 (setq org-subtree-clip (current-kill 0))
7871 (message "%s: Subtree(s) with %d characters"
7872 (if cut "Cut" "Copied")
7873 (length org-subtree-clip)))))
7875 (defun org-paste-subtree (&optional level tree for-yank)
7876 "Paste the clipboard as a subtree, with modification of headline level.
7877 The entire subtree is promoted or demoted in order to match a new headline
7880 If the cursor is at the beginning of a headline, the same level as
7881 that headline is used to paste the tree
7883 If not, the new level is derived from the *visible* headings
7884 before and after the insertion point, and taken to be the inferior headline
7885 level of the two. So if the previous visible heading is level 3 and the
7886 next is level 4 (or vice versa), level 4 will be used for insertion.
7887 This makes sure that the subtree remains an independent subtree and does
7888 not swallow low level entries.
7890 You can also force a different level, either by using a numeric prefix
7891 argument, or by inserting the heading marker by hand. For example, if the
7892 cursor is after \"*****\", then the tree will be shifted to level 5.
7894 If optional TREE is given, use this text instead of the kill ring.
7896 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
7897 move back over whitespace before inserting, and move point to the end of
7898 the inserted text when done."
7900 (setq tree (or tree (and kill-ring (current-kill 0))))
7901 (unless (org-kill-is-subtree-p tree)
7903 (substitute-command-keys
7904 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
7905 (org-with-limited-levels
7906 (let* ((visp (not (outline-invisible-p)))
7908 (^re_ "\\(\\*+\\)[ \t]*")
7909 (old-level (if (string-match org-outline-regexp-bol txt)
7910 (- (match-end 0) (match-beginning 0) 1)
7912 (force-level (cond (level (prefix-numeric-value level))
7913 ((and (looking-at "[ \t]*$")
7915 "^\\*+$" (buffer-substring
7916 (point-at-bol) (point))))
7917 (- (match-end 1) (match-beginning 1)))
7919 (looking-at org-outline-regexp))
7920 (- (match-end 0) (point) 1))))
7921 (previous-level (save-excursion
7924 (outline-previous-visible-heading 1)
7925 (if (looking-at ^re_)
7926 (- (match-end 0) (match-beginning 0) 1)
7929 (next-level (save-excursion
7932 (or (looking-at org-outline-regexp)
7933 (outline-next-visible-heading 1))
7934 (if (looking-at ^re_)
7935 (- (match-end 0) (match-beginning 0) 1)
7938 (new-level (or force-level (max previous-level next-level)))
7939 (shift (if (or (= old-level -1)
7941 (= old-level new-level))
7943 (- new-level old-level)))
7944 (delta (if (> shift 0) -1 1))
7945 (func (if (> shift 0) 'org-demote 'org-promote))
7946 (org-odd-levels-only nil)
7948 ;; Remove the forced level indicator
7950 (delete-region (point-at-bol) (point)))
7952 (beginning-of-line (if (bolp) 1 2))
7953 (unless for-yank (org-back-over-empty-lines))
7955 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
7956 (insert-before-markers txt)
7957 (unless (string-match "\n\\'" txt) (insert "\n"))
7958 (setq newend (point))
7959 (org-reinstall-markers-in-region beg)
7962 (skip-chars-forward " \t\n\r")
7964 (if (and (outline-invisible-p) visp)
7965 (save-excursion (outline-show-heading)))
7966 ;; Shift if necessary
7969 (narrow-to-region beg end)
7970 (while (not (= shift 0))
7971 (org-map-region func (point-min) (point-max))
7972 (setq shift (+ delta shift)))
7973 (goto-char (point-min))
7974 (setq newend (point-max))))
7975 (when (or (org-called-interactively-p 'interactive) for-yank)
7976 (message "Clipboard pasted as level %d subtree" new-level))
7977 (if (and (not for-yank) ; in this case, org-yank will decide about folding
7979 (eq org-subtree-clip (current-kill 0))
7980 org-subtree-clip-folded)
7981 ;; The tree was folded before it was killed/copied
7983 (and for-yank (goto-char newend)))))
7985 (defun org-kill-is-subtree-p (&optional txt)
7986 "Check if the current kill is an outline subtree, or a set of trees.
7987 Returns nil if kill does not start with a headline, or if the first
7988 headline level is not the largest headline level in the tree.
7989 So this will actually accept several entries of equal levels as well,
7990 which is OK for `org-paste-subtree'.
7991 If optional TXT is given, check this string instead of the current kill."
7992 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
7993 (re (org-get-limited-outline-regexp))
7994 (^re (concat "^" re))
7995 (start-level (and kill
7997 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
7999 (- (match-end 2) (match-beginning 2) 1)))
8000 (start (1+ (or (match-beginning 2) -1))))
8001 (if (not start-level)
8003 nil) ;; does not even start with a heading
8005 (while (setq start (string-match ^re kill (1+ start)))
8006 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
8010 (defvar org-markers-to-move nil
8011 "Markers that should be moved with a cut-and-paste operation.
8012 Those markers are stored together with their positions relative to
8013 the start of the region.")
8015 (defun org-save-markers-in-region (beg end)
8016 "Check markers in region.
8017 If these markers are between BEG and END, record their position relative
8018 to BEG, so that after moving the block of text, we can put the markers back
8020 This function gets called just before an entry or tree gets cut from the
8021 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
8022 called immediately, to move the markers with the entries."
8023 (setq org-markers-to-move nil)
8024 (when (featurep 'org-clock)
8025 (org-clock-save-markers-for-cut-and-paste beg end))
8026 (when (featurep 'org-agenda)
8027 (org-agenda-save-markers-for-cut-and-paste beg end)))
8029 (defun org-check-and-save-marker (marker beg end)
8030 "Check if MARKER is between BEG and END.
8031 If yes, remember the marker and the distance to BEG."
8032 (when (and (marker-buffer marker)
8033 (equal (marker-buffer marker) (current-buffer)))
8034 (if (and (>= marker beg) (< marker end))
8035 (push (cons marker (- marker beg)) org-markers-to-move))))
8037 (defun org-reinstall-markers-in-region (beg)
8038 "Move all remembered markers to their position relative to BEG."
8040 (move-marker (car x) (+ beg (cdr x))))
8041 org-markers-to-move)
8042 (setq org-markers-to-move nil))
8044 (defun org-narrow-to-subtree ()
8045 "Narrow buffer to the current subtree."
8049 (org-with-limited-levels
8051 (progn (org-back-to-heading t) (point))
8052 (progn (org-end-of-subtree t t)
8053 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
8056 (defun org-narrow-to-block ()
8057 "Narrow buffer to the current block."
8059 (let* ((case-fold-search t)
8060 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
8061 "^[ \t]*#\\+end_.*")))
8063 (narrow-to-region (car blockp) (cdr blockp))
8064 (error "Not in a block"))))
8067 (defvar org-property-drawer-re))
8069 (defvar org-property-start-re) ;; defined below
8070 (defun org-clone-subtree-with-time-shift (n &optional shift)
8071 "Clone the task (subtree) at point N times.
8072 The clones will be inserted as siblings.
8074 In interactive use, the user will be prompted for the number of
8075 clones to be produced, and for a time SHIFT, which may be a
8076 repeater as used in time stamps, for example `+3d'.
8078 When a valid repeater is given and the entry contains any time
8079 stamps, the clones will become a sequence in time, with time
8080 stamps in the subtree shifted for each clone produced. If SHIFT
8081 is nil or the empty string, time stamps will be left alone. The
8082 ID property of the original subtree is removed.
8084 If the original subtree did contain time stamps with a repeater,
8085 the following will happen:
8086 - the repeater will be removed in each clone
8087 - an additional clone will be produced, with the current, unshifted
8088 date(s) in the entry.
8089 - the original entry will be placed *after* all the clones, with
8091 - the start days in the repeater in the original entry will be shifted
8092 to past the last clone.
8093 In this way you can spell out a number of instances of a repeating task,
8094 and still retain the repeater to cover future instances of the task."
8095 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
8096 (let (beg end template task idprop
8097 shift-n shift-what doshift nmin nmax (n-no-remove -1)
8098 (drawer-re org-drawer-regexp))
8099 (if (not (and (integerp n) (> n 0)))
8100 (error "Invalid number of replications %s" n))
8101 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
8102 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
8104 (error "Invalid shift specification %s" shift))
8106 (setq shift-n (string-to-number (match-string 1 shift))
8107 shift-what (cdr (assoc (match-string 2 shift)
8108 '(("d" . day) ("w" . week)
8109 ("m" . month) ("y" . year))))))
8110 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
8111 (setq nmin 1 nmax n)
8112 (org-back-to-heading t)
8114 (setq idprop (org-entry-get nil "ID"))
8115 (org-end-of-subtree t t)
8116 (or (bolp) (insert "\n"))
8118 (setq template (buffer-substring beg end))
8120 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
8121 (delete-region beg end)
8123 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
8125 (loop for n from nmin to nmax do
8130 (goto-char (point-min))
8132 (and idprop (if org-clone-delete-id
8133 (org-entry-delete nil "ID")
8134 (org-id-get-create t)))
8136 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
8138 (goto-char (point-min))
8139 (while (re-search-forward drawer-re nil t)
8141 (org-remove-empty-drawer-at d (point))) org-drawers)))
8142 (goto-char (point-min))
8144 (while (re-search-forward org-ts-regexp-both nil t)
8145 (org-timestamp-change (* n shift-n) shift-what))
8146 (unless (= n n-no-remove)
8147 (goto-char (point-min))
8148 (while (re-search-forward org-ts-regexp nil t)
8150 (goto-char (match-beginning 0))
8151 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
8152 (delete-region (match-beginning 1) (match-end 1)))))))
8153 (setq task (buffer-string)))
8159 (defun org-sort (with-case)
8160 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
8161 Optional argument WITH-CASE means sort case-sensitively."
8164 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
8165 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
8167 (org-call-with-arg 'org-sort-entries with-case))))
8169 (defun org-sort-remove-invisible (s)
8170 (remove-text-properties 0 (length s) org-rm-props s)
8171 (while (string-match org-bracket-link-regexp s)
8172 (setq s (replace-match (if (match-end 2)
8174 (match-string 1 s)) t t s)))
8177 (defvar org-priority-regexp) ; defined later in the file
8179 (defvar org-after-sorting-entries-or-items-hook nil
8180 "Hook that is run after a bunch of entries or items have been sorted.
8181 When children are sorted, the cursor is in the parent line when this
8182 hook gets called. When a region or a plain list is sorted, the cursor
8183 will be in the first entry of the sorted region/list.")
8185 (defun org-sort-entries
8186 (&optional with-case sorting-type getkey-func compare-func property)
8187 "Sort entries on a certain level of an outline tree.
8188 If there is an active region, the entries in the region are sorted.
8189 Else, if the cursor is before the first entry, sort the top-level items.
8190 Else, the children of the entry at point are sorted.
8192 Sorting can be alphabetically, numerically, by date/time as given by
8193 a time stamp, by a property or by priority.
8195 The command prompts for the sorting type unless it has been given to the
8196 function through the SORTING-TYPE argument, which needs to be a character,
8197 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
8198 precise meaning of each character:
8200 n Numerically, by converting the beginning of the entry/item to a number.
8201 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8202 t By date/time, either the first active time stamp in the entry, or, if
8203 none exist, by the first inactive one.
8204 s By the scheduled date/time.
8205 d By deadline date/time.
8206 c By creation time, which is assumed to be the first inactive time stamp
8207 at the beginning of a line.
8208 p By priority according to the cookie.
8209 r By the value of a property.
8211 Capital letters will reverse the sort order.
8213 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8214 called with point at the beginning of the record. It must return either
8215 a string or a number that should serve as the sorting key for that record.
8217 Comparing entries ignores case by default. However, with an optional argument
8218 WITH-CASE, the sorting considers case as well."
8220 (let ((case-func (if with-case 'identity 'downcase))
8221 start beg end stars re re2
8223 ;; Find beginning and end of region to sort
8225 ((org-region-active-p)
8226 ;; we will sort the region
8227 (setq end (region-end)
8229 (goto-char (region-beginning))
8230 (if (not (org-at-heading-p)) (outline-next-heading))
8231 (setq start (point)))
8232 ((or (org-at-heading-p)
8233 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8234 ;; we will sort the children of the current headline
8235 (org-back-to-heading)
8237 end (progn (org-end-of-subtree t t)
8238 (or (bolp) (insert "\n"))
8239 (org-back-over-empty-lines)
8244 (outline-next-heading))
8246 ;; we will sort the top-level entries in this file
8247 (goto-char (point-min))
8248 (or (org-at-heading-p) (outline-next-heading))
8249 (setq start (point))
8250 (goto-char (point-max))
8251 (beginning-of-line 1)
8252 (when (looking-at ".*?\\S-")
8253 ;; File ends in a non-white line
8256 (setq end (point-max))
8257 (setq what "top-level")
8262 (if (>= beg end) (error "Nothing to sort"))
8264 (looking-at "\\(\\*+\\)")
8265 (setq stars (match-string 1)
8266 re (concat "^" (regexp-quote stars) " +")
8267 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8268 txt (buffer-substring beg end))
8269 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8270 (if (and (not (equal stars "*")) (string-match re2 txt))
8271 (error "Region to sort contains a level above the first entry"))
8273 (unless sorting-type
8275 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8276 [t]ime
[s]cheduled [d]eadline [c]reated
8277 A/N/T/S/D/C/P/O/F means reversed:"
8279 (setq sorting-type (read-char-exclusive))
8281 (and (= (downcase sorting-type) ?f)
8283 (org-icompleting-read "Sort using function: "
8284 obarray 'fboundp t nil nil))
8285 (setq getkey-func (intern getkey-func)))
8287 (and (= (downcase sorting-type) ?r)
8289 (org-icompleting-read "Property: "
8290 (mapcar 'list (org-buffer-property-keys t))
8293 (message "Sorting entries...")
8296 (narrow-to-region start end)
8297 (let ((dcst (downcase sorting-type))
8298 (case-fold-search nil)
8299 (now (current-time)))
8301 (/= dcst sorting-type)
8302 ;; This function moves to the beginning character of the "record" to
8305 (if (re-search-forward re nil t)
8306 (goto-char (match-beginning 0))
8307 (goto-char (point-max))))
8308 ;; This function moves to the last character of the "record" being
8313 (outline-forward-same-level 1)
8315 (goto-char (point-max))))))
8316 ;; This function returns the value that gets sorted against.
8320 (if (looking-at org-complex-heading-regexp)
8321 (string-to-number (match-string 4))
8324 (if (looking-at org-complex-heading-regexp)
8325 (funcall case-func (match-string 4))
8328 (let ((end (save-excursion (outline-next-heading) (point))))
8329 (if (or (re-search-forward org-ts-regexp end t)
8330 (re-search-forward org-ts-regexp-both end t))
8331 (org-time-string-to-seconds (match-string 0))
8332 (org-float-time now))))
8334 (let ((end (save-excursion (outline-next-heading) (point))))
8335 (if (re-search-forward
8336 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8338 (org-time-string-to-seconds (match-string 0))
8339 (org-float-time now))))
8341 (let ((end (save-excursion (outline-next-heading) (point))))
8342 (if (re-search-forward org-scheduled-time-regexp end t)
8343 (org-time-string-to-seconds (match-string 1))
8344 (org-float-time now))))
8346 (let ((end (save-excursion (outline-next-heading) (point))))
8347 (if (re-search-forward org-deadline-time-regexp end t)
8348 (org-time-string-to-seconds (match-string 1))
8349 (org-float-time now))))
8351 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8352 (string-to-char (match-string 2))
8353 org-default-priority))
8355 (or (org-entry-get nil property) ""))
8357 (if (looking-at org-complex-heading-regexp)
8358 (- 9999 (length (member (match-string 2)
8359 org-todo-keywords-1)))))
8363 (setq tmp (funcall getkey-func))
8364 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8366 (error "Invalid key function `%s'" getkey-func)))
8367 (t (error "Invalid sorting type `%c'" sorting-type))))
8370 ((= dcst ?a) 'string<)
8371 ((= dcst ?f) compare-func)
8372 ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
8373 (run-hooks 'org-after-sorting-entries-or-items-hook)
8374 (message "Sorting entries...done")))
8376 (defun org-do-sort (table what &optional with-case sorting-type)
8377 "Sort TABLE of WHAT according to SORTING-TYPE.
8378 The user will be prompted for the SORTING-TYPE if the call to this
8379 function does not specify it. WHAT is only for the prompt, to indicate
8380 what is being sorted. The sorting key will be extracted from
8381 the car of the elements of the table.
8382 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8383 (unless sorting-type
8385 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
8387 (setq sorting-type (read-char-exclusive)))
8388 (let ((dcst (downcase sorting-type))
8389 extractfun comparefun)
8390 ;; Define the appropriate functions
8393 (setq extractfun 'string-to-number
8394 comparefun (if (= dcst sorting-type) '< '>)))
8396 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8397 (lambda(x) (downcase (org-sort-remove-invisible x))))
8398 comparefun (if (= dcst sorting-type)
8400 (lambda (a b) (and (not (string< a b))
8401 (not (string= a b)))))))
8405 (if (or (string-match org-ts-regexp x)
8406 (string-match org-ts-regexp-both x))
8408 (org-time-string-to-time (match-string 0 x)))
8410 comparefun (if (= dcst sorting-type) '< '>)))
8411 (t (error "Invalid sorting type `%c'" sorting-type)))
8413 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8415 (lambda (a b) (funcall comparefun (car a) (car b))))))
8418 ;;; The orgstruct minor mode
8420 ;; Define a minor mode which can be used in other modes in order to
8421 ;; integrate the org-mode structure editing commands.
8423 ;; This is really a hack, because the org-mode structure commands use
8424 ;; keys which normally belong to the major mode. Here is how it
8425 ;; works: The minor mode defines all the keys necessary to operate the
8426 ;; structure commands, but wraps the commands into a function which
8427 ;; tests if the cursor is currently at a headline or a plain list
8428 ;; item. If that is the case, the structure command is used,
8429 ;; temporarily setting many Org-mode variables like regular
8430 ;; expressions for filling etc. However, when any of those keys is
8431 ;; used at a different location, function uses `key-binding' to look
8432 ;; up if the key has an associated command in another currently active
8433 ;; keymap (minor modes, major mode, global), and executes that
8434 ;; command. There might be problems if any of the keys is otherwise
8435 ;; used as a prefix key.
8437 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
8438 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
8439 ;; addresses this by checking explicitly for both bindings.
8441 (defvar orgstruct-mode-map (make-sparse-keymap)
8442 "Keymap for the minor `orgstruct-mode'.")
8444 (defvar org-local-vars nil
8445 "List of local variables, for use by `orgstruct-mode'.")
8448 (define-minor-mode orgstruct-mode
8449 "Toggle the minor mode `orgstruct-mode'.
8450 This mode is for using Org-mode structure commands in other
8451 modes. The following keys behave as if Org-mode were active, if
8452 the cursor is on a headline, or on a plain list item (both as
8453 defined by Org-mode).
8455 M-up Move entry/item up
8456 M-down Move entry/item down
8459 M-S-up Move entry/item up
8460 M-S-down Move entry/item down
8461 M-S-left Promote subtree
8462 M-S-right Demote subtree
8463 M-q Fill paragraph and items like in Org-mode
8465 C-c - Cycle list bullet
8466 TAB Cycle item visibility
8467 M-RET Insert new heading/item
8468 S-M-RET Insert new TODO heading / Checkbox item
8469 C-c C-c Set tags / toggle checkbox"
8470 nil " OrgStruct" nil
8471 (org-load-modules-maybe)
8472 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
8475 (defun turn-on-orgstruct ()
8476 "Unconditionally turn on `orgstruct-mode'."
8479 (defvar org-fb-vars nil)
8480 (make-variable-buffer-local 'org-fb-vars)
8481 (defun orgstruct++-mode (&optional arg)
8482 "Toggle `orgstruct-mode', the enhanced version of it.
8483 In addition to setting orgstruct-mode, this also exports all
8484 indentation and autofilling variables from org-mode into the
8485 buffer. It will also recognize item context in multiline items."
8487 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8489 (progn (orgstruct-mode -1)
8491 (org-set-local (car v)
8492 (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
8495 (setq org-fb-vars nil)
8500 "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
8501 (symbol-name (car x)))
8502 (setq var (car x) val (nth 1 x))
8503 (push (list var `(quote ,(eval var))) org-fb-vars)
8504 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8506 (org-set-local 'orgstruct-is-++ t))))
8508 (defvar orgstruct-is-++ nil
8509 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8510 (make-variable-buffer-local 'orgstruct-is-++)
8513 (defun turn-on-orgstruct++ ()
8514 "Unconditionally turn on `orgstruct++-mode'."
8515 (orgstruct++-mode 1))
8517 (defun orgstruct-error ()
8518 "Error when there is no default binding for a structure key."
8520 (error "This key has no function outside structure elements"))
8522 (defun orgstruct-setup ()
8523 "Setup orgstruct keymaps."
8527 '([(meta up)] org-metaup)
8528 '([(meta down)] org-metadown)
8529 '([(meta left)] org-metaleft)
8530 '([(meta right)] org-metaright)
8531 '([(meta shift up)] org-shiftmetaup)
8532 '([(meta shift down)] org-shiftmetadown)
8533 '([(meta shift left)] org-shiftmetaleft)
8534 '([(meta shift right)] org-shiftmetaright)
8535 '([?\e (up)] org-metaup)
8536 '([?\e (down)] org-metadown)
8537 '([?\e (left)] org-metaleft)
8538 '([?\e (right)] org-metaright)
8539 '([?\e (shift up)] org-shiftmetaup)
8540 '([?\e (shift down)] org-shiftmetadown)
8541 '([?\e (shift left)] org-shiftmetaleft)
8542 '([?\e (shift right)] org-shiftmetaright)
8543 '([(shift up)] org-shiftup)
8544 '([(shift down)] org-shiftdown)
8545 '([(shift left)] org-shiftleft)
8546 '([(shift right)] org-shiftright)
8547 '("\C-c\C-c" org-ctrl-c-ctrl-c)
8548 '("\M-q" fill-paragraph)
8550 '("\C-c-" org-cycle-list-bullet)))
8552 (while (setq elt (pop bindings))
8553 (setq nfunc (1+ nfunc))
8554 (setq key (org-key (car elt))
8556 cmd (orgstruct-make-binding fun nfunc key))
8557 (org-defkey orgstruct-mode-map key cmd))
8559 ;; Prevent an error for users who forgot to make autoloads
8560 (require 'org-element)
8562 ;; Special treatment needed for TAB and RET
8563 (org-defkey orgstruct-mode-map [(tab)]
8564 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
8565 (org-defkey orgstruct-mode-map "\C-i"
8566 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
8568 (org-defkey orgstruct-mode-map "\M-\C-m"
8569 (orgstruct-make-binding 'org-insert-heading 105
8570 "\M-\C-m" [(meta return)]))
8571 (org-defkey orgstruct-mode-map [(meta return)]
8572 (orgstruct-make-binding 'org-insert-heading 106
8573 [(meta return)] "\M-\C-m"))
8575 (org-defkey orgstruct-mode-map [(shift meta return)]
8576 (orgstruct-make-binding 'org-insert-todo-heading 107
8577 [(meta return)] "\M-\C-m"))
8579 (org-defkey orgstruct-mode-map "\e\C-m"
8580 (orgstruct-make-binding 'org-insert-heading 108
8581 "\e\C-m" [?\e (return)]))
8582 (org-defkey orgstruct-mode-map [?\e (return)]
8583 (orgstruct-make-binding 'org-insert-heading 109
8584 [?\e (return)] "\e\C-m"))
8585 (org-defkey orgstruct-mode-map [?\e (shift return)]
8586 (orgstruct-make-binding 'org-insert-todo-heading 110
8587 [?\e (return)] "\e\C-m"))
8589 (unless org-local-vars
8590 (setq org-local-vars (org-get-local-variables)))
8594 (defun orgstruct-make-binding (fun n &rest keys)
8595 "Create a function for binding in the structure minor mode.
8596 FUN is the command to call inside a table. N is used to create a unique
8597 command name. KEYS are keys that should be checked in for a command
8598 to execute outside of tables."
8601 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
8603 (concat "In Structure, run `" (symbol-name fun) "'.\n"
8604 "Outside of structure, run the binding of `"
8605 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
8609 `(org-context-p 'headline 'item
8610 (and orgstruct-is-++
8611 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
8613 (list 'org-run-like-in-org-mode (list 'quote fun))
8614 (list 'let '(orgstruct-mode)
8615 (list 'call-interactively
8618 (list 'key-binding k))
8620 '('orgstruct-error))))))))
8622 (defun org-contextualize-keys (alist contexts)
8623 "Return valid elements in ALIST depending on CONTEXTS.
8625 `org-agenda-custom-commands' or `org-capture-templates' are the
8626 values used for ALIST, and `org-agenda-custom-commands-contexts'
8627 or `org-capture-templates-contexts' are the associated contexts
8630 ;; normalize contexts
8632 (lambda(c) (cond ((listp (cadr c))
8633 (list (car c) (car c) (cadr c)))
8634 ((string= "" (cadr c))
8635 (list (car c) (car c) (caddr c)))
8638 ;; loop over all commands or templates
8639 (while (setq c (pop a))
8642 ((not (assoc (car c) contexts))
8644 ((and (assoc (car c) contexts)
8645 (setq vrules (org-contextualize-validate-key
8648 (when (not (equal (car vr) (cadr vr)))
8649 (setq repl vr))) vrules)
8650 (if (not repl) (push c r)
8651 (push (cadr repl) s)
8654 (cdr (or (assoc (cadr repl) alist)
8655 (error "Undefined key `%s' as contextual replacement for `%s'"
8656 (cadr repl) (car c)))))
8658 ;; Return limited ALIST, possibly with keys modified, and deduplicated
8663 (let ((tpl (car x)))
8667 (equal y tpl)) s))) x)))
8670 (defun org-contextualize-validate-key (key contexts)
8671 "Check CONTEXTS for agenda or capture KEY."
8673 (while (setq r (pop contexts))
8677 (and (equal key (car r))
8678 (if (functionp rr) (funcall rr)
8679 (or (and (eq (car rr) 'in-file)
8681 (string-match (cdr rr) (buffer-file-name)))
8682 (and (eq (car rr) 'in-mode)
8683 (string-match (cdr rr) (symbol-name major-mode)))
8684 (when (and (eq (car rr) 'not-in-file)
8686 (not (string-match (cdr rr) (buffer-file-name))))
8687 (when (eq (car rr) 'not-in-mode)
8688 (not (string-match (cdr rr) (symbol-name major-mode)))))))
8691 (delete-dups (delq nil res))))
8693 (defun org-context-p (&rest contexts)
8694 "Check if local context is any of CONTEXTS.
8695 Possible values in the list of contexts are `table', `headline', and `item'."
8696 (let ((pos (point)))
8697 (goto-char (point-at-bol))
8698 (prog1 (or (and (memq 'table contexts)
8699 (looking-at "[ \t]*|"))
8700 (and (memq 'headline contexts)
8701 (looking-at org-outline-regexp))
8702 (and (memq 'item contexts)
8703 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
8704 (and (memq 'item-body contexts)
8708 (defun org-get-local-variables ()
8709 "Return a list of all local variables in an Org mode buffer."
8711 (with-current-buffer (get-buffer-create "*Org tmp*")
8714 (setq varlist (buffer-local-variables)))
8715 (kill-buffer "*Org tmp*")
8722 (list (car x) (list 'quote (cdr x)))))
8724 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
8725 (symbol-name (car x)))
8729 (defun org-clone-local-variables (from-buffer &optional regexp)
8730 "Clone local variables from FROM-BUFFER.
8731 Optional argument REGEXP selects variables to clone."
8734 (and (symbolp (car pair))
8736 (string-match regexp (symbol-name (car pair))))
8737 (set (make-local-variable (car pair))
8739 (buffer-local-variables from-buffer)))
8742 (defun org-run-like-in-org-mode (cmd)
8743 "Run a command, pretending that the current buffer is in Org-mode.
8744 This will temporarily bind local variables that are typically bound in
8745 Org-mode to the values they have in Org-mode, and then interactively
8747 (org-load-modules-maybe)
8748 (unless org-local-vars
8749 (setq org-local-vars (org-get-local-variables)))
8750 (eval (list 'let org-local-vars
8751 (list 'call-interactively (list 'quote cmd)))))
8755 (defun org-get-category (&optional pos force-refresh)
8756 "Get the category applying to position POS."
8758 (if force-refresh (org-refresh-category-properties))
8759 (let ((pos (or pos (point))))
8760 (or (get-text-property pos 'org-category)
8761 (progn (org-refresh-category-properties)
8762 (get-text-property pos 'org-category))))))
8764 (defun org-refresh-category-properties ()
8765 "Refresh category text properties in the buffer."
8766 (let ((case-fold-search t)
8767 (inhibit-read-only t)
8769 ((null org-category)
8770 (if buffer-file-name
8771 (file-name-sans-extension
8772 (file-name-nondirectory buffer-file-name))
8774 ((symbolp org-category) (symbol-name org-category))
8776 beg end cat pos optionp)
8781 (goto-char (point-min))
8782 (put-text-property (point) (point-max) 'org-category def-cat)
8783 (while (re-search-forward
8784 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8785 (setq pos (match-end 0)
8786 optionp (equal (char-after (match-beginning 0)) ?#)
8787 cat (org-trim (match-string 2)))
8789 (setq beg (point-at-bol) end (point-max))
8790 (org-back-to-heading t)
8791 (setq beg (point) end (org-end-of-subtree t t)))
8792 (put-text-property beg end 'org-category cat)
8793 (put-text-property beg end 'org-category-position beg)
8794 (goto-char pos)))))))
8799 ;;; Link abbreviations
8801 (defun org-link-expand-abbrev (link)
8802 "Apply replacements as defined in `org-link-abbrev-alist'."
8803 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
8804 (let* ((key (match-string 1 link))
8805 (as (or (assoc key org-link-abbrev-alist-local)
8806 (assoc key org-link-abbrev-alist)))
8807 (tag (and (match-end 2) (match-string 3 link)))
8813 ((symbolp rpl) (funcall rpl tag))
8814 ((string-match "%(\\([^)]+\\))" rpl)
8815 (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
8816 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
8817 ((string-match "%h" rpl)
8818 (replace-match (url-hexify-string (or tag "")) t t rpl))
8819 (t (concat rpl tag)))))
8822 ;;; Storing and inserting links
8824 (defvar org-insert-link-history nil
8825 "Minibuffer history for links inserted with `org-insert-link'.")
8827 (defvar org-stored-links nil
8828 "Contains the links stored with `org-store-link'.")
8830 (defvar org-store-link-plist nil
8831 "Plist with info about the most recently link created with `org-store-link'.")
8833 (defvar org-link-protocols nil
8834 "Link protocols added to Org-mode using `org-add-link-type'.")
8836 (defvar org-store-link-functions nil
8837 "List of functions that are called to create and store a link.
8838 Each function will be called in turn until one returns a non-nil
8839 value. Each function should check if it is responsible for creating
8840 this link (for example by looking at the major mode).
8841 If not, it must exit and return nil.
8842 If yes, it should return a non-nil value after a calling
8843 `org-store-link-props' with a list of properties and values.
8844 Special properties are:
8846 :type The link prefix, like \"http\". This must be given.
8847 :link The link, like \"http://www.astro.uva.nl/~dominik\".
8848 This is obligatory as well.
8849 :description Optional default description for the second pair
8850 of brackets in an Org-mode link. The user can still change
8851 this when inserting this link into an Org-mode buffer.
8853 In addition to these, any additional properties can be specified
8854 and then used in remember templates.")
8856 (defun org-add-link-type (type &optional follow export)
8857 "Add TYPE to the list of `org-link-types'.
8858 Re-compute all regular expressions depending on `org-link-types'
8860 FOLLOW and EXPORT are two functions.
8862 FOLLOW should take the link path as the single argument and do whatever
8863 is necessary to follow the link, for example find a file or display
8866 EXPORT should format the link path for export to one of the export formats.
8867 It should be a function accepting three arguments:
8869 path the path of the link, the text after the prefix (like \"http:\")
8870 desc the description of the link, if any, or a description added by
8871 org-export-normalize-links if there is none
8872 format the export format, a symbol like `html' or `latex' or `ascii'..
8874 The function may use the FORMAT information to return different values
8875 depending on the format. The return value will be put literally into
8876 the exported file. If the return value is nil, this means Org should
8877 do what it normally does with links which do not have EXPORT defined.
8879 Org-mode has a built-in default for exporting links. If you are happy with
8880 this default, there is no need to define an export function for the link
8881 type. For a simple example of an export function, see `org-bbdb.el'."
8882 (add-to-list 'org-link-types type t)
8883 (org-make-link-regexps)
8884 (if (assoc type org-link-protocols)
8885 (setcdr (assoc type org-link-protocols) (list follow export))
8886 (push (list type follow export) org-link-protocols)))
8888 (defvar org-agenda-buffer-name)
8891 (defun org-store-link (arg)
8892 "\\<org-mode-map>Store an org-link to the current location.
8893 This link is added to `org-stored-links' and can later be inserted
8894 into an org-buffer with \\[org-insert-link].
8896 For some link types, a prefix arg is interpreted:
8897 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
8898 For file links, arg negates `org-context-in-file-links'."
8900 (org-load-modules-maybe)
8901 (setq org-store-link-plist nil) ; reset
8902 (org-with-limited-levels
8903 (let (link cpltxt desc description search txt custom-id agenda-link)
8906 ((run-hook-with-args-until-success 'org-store-link-functions)
8907 (setq link (plist-get org-store-link-plist :link)
8908 desc (or (plist-get org-store-link-plist :description) link)))
8910 ((org-src-edit-buffer-p)
8912 (while (or (not label)
8916 (goto-char (point-min))
8918 (regexp-quote (format org-coderef-label-format label))
8920 (when label (message "Label exists already") (sit-for 2))
8921 (setq label (read-string "Code line label: " label)))
8923 (setq link (format org-coderef-label-format label))
8924 (setq gc (- 79 (length link)))
8925 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
8927 (setq link (concat "(" label ")") desc nil)))
8929 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
8930 ;; We are in the agenda, link to referenced location
8931 (let ((m (or (get-text-property (point) 'org-hd-marker)
8932 (get-text-property (point) 'org-marker))))
8934 (org-with-point-at m
8936 (if (org-called-interactively-p 'any)
8937 (call-interactively 'org-store-link)
8938 (org-store-link nil)))))))
8940 ((eq major-mode 'calendar-mode)
8941 (let ((cd (calendar-cursor-to-date)))
8944 (car org-time-stamp-formats)
8946 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8948 (org-store-link-props :type "calendar" :date cd)))
8950 ((eq major-mode 'help-mode)
8951 (setq link (concat "help:" (save-excursion
8952 (goto-char (point-min))
8953 (looking-at "^[^ ]+")
8955 (org-store-link-props :type "help"))
8957 ((eq major-mode 'w3-mode)
8958 (setq cpltxt (if (and (buffer-name)
8959 (not (string-match "Untitled" (buffer-name))))
8962 link (url-view-url t))
8963 (org-store-link-props :type "w3" :url (url-view-url t)))
8965 ((eq major-mode 'w3m-mode)
8966 (setq cpltxt (or w3m-current-title w3m-current-url)
8967 link w3m-current-url)
8968 (org-store-link-props :type "w3m" :url (url-view-url t)))
8970 ((setq search (run-hook-with-args-until-success
8971 'org-create-file-search-functions))
8972 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8974 (setq cpltxt (or description link)))
8976 ((eq major-mode 'image-mode)
8977 (setq cpltxt (concat "file:"
8978 (abbreviate-file-name buffer-file-name))
8980 (org-store-link-props :type "image" :file buffer-file-name))
8982 ((eq major-mode 'dired-mode)
8983 ;; link to the file in the current line
8984 (let ((file (dired-get-filename nil t)))
8986 (abbreviate-file-name
8987 (expand-file-name (dired-get-filename nil t)))
8988 ;; otherwise, no file so use current directory.
8990 (setq cpltxt (concat "file:" file)
8993 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
8994 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
8996 ((org-in-regexp "<<\\(.*?\\)>>")
8999 (abbreviate-file-name
9000 (buffer-file-name (buffer-base-buffer)))
9001 "::" (match-string 1))
9003 ((and (featurep 'org-id)
9004 (or (eq org-link-to-org-use-id t)
9005 (and (org-called-interactively-p 'any)
9006 (or (eq org-link-to-org-use-id 'create-if-interactive)
9007 (and (eq org-link-to-org-use-id
9008 'create-if-interactive-and-no-custom-id)
9010 (and org-link-to-org-use-id (org-entry-get nil "ID"))))
9011 ;; We can make a link using the ID.
9012 (setq link (condition-case nil
9013 (prog1 (org-id-store-link)
9014 (setq desc (plist-get org-store-link-plist :description)))
9016 ;; probably before first headline, link to file only
9018 (abbreviate-file-name
9019 (buffer-file-name (buffer-base-buffer))))))))
9021 ;; Just link to current headline
9022 (setq cpltxt (concat "file:"
9023 (abbreviate-file-name
9024 (buffer-file-name (buffer-base-buffer)))))
9025 ;; Add a context search string
9026 (when (org-xor org-context-in-file-links arg)
9028 ((org-at-heading-p) nil)
9029 ((org-region-active-p)
9030 (buffer-substring (region-beginning) (region-end)))))
9031 (when (or (null txt) (string-match "\\S-" txt))
9035 (org-make-org-heading-search-string txt)
9037 desc (or (nth 4 (ignore-errors
9038 (org-heading-components))) "NONE"))))
9039 (if (string-match "::\\'" cpltxt)
9040 (setq cpltxt (substring cpltxt 0 -2)))
9041 (setq link cpltxt))))
9043 ((buffer-file-name (buffer-base-buffer))
9044 ;; Just link to this file here.
9045 (setq cpltxt (concat "file:"
9046 (abbreviate-file-name
9047 (buffer-file-name (buffer-base-buffer)))))
9048 ;; Add a context string
9049 (when (org-xor org-context-in-file-links arg)
9050 (setq txt (if (org-region-active-p)
9051 (buffer-substring (region-beginning) (region-end))
9052 (buffer-substring (point-at-bol) (point-at-eol))))
9053 ;; Only use search option if there is some text.
9054 (when (string-match "\\S-" txt)
9056 (concat cpltxt "::" (org-make-org-heading-search-string txt))
9060 ((org-called-interactively-p 'interactive)
9061 (error "Cannot link to a buffer which is not visiting a file"))
9063 (t (setq link nil)))
9065 (if (consp link) (setq cpltxt (car link) link (cdr link)))
9066 (setq link (or link cpltxt)
9067 desc (or desc cpltxt))
9068 (if (equal desc "NONE") (setq desc nil))
9070 (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
9072 (setq org-stored-links
9073 (cons (list link desc) org-stored-links))
9074 (message "Stored: %s" (or desc link))
9076 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
9078 (setq org-stored-links
9079 (cons (list link desc) org-stored-links))))
9080 (or agenda-link (and link (org-make-link-string link desc)))))))
9082 (defun org-store-link-props (&rest plist)
9083 "Store link properties, extract names and addresses."
9085 (when (setq x (plist-get plist :from))
9086 (setq adr (mail-extract-address-components x))
9087 (setq plist (plist-put plist :fromname (car adr)))
9088 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
9089 (when (setq x (plist-get plist :to))
9090 (setq adr (mail-extract-address-components x))
9091 (setq plist (plist-put plist :toname (car adr)))
9092 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
9093 (let ((from (plist-get plist :from))
9094 (to (plist-get plist :to)))
9095 (when (and from to org-from-is-user-regexp)
9097 (plist-put plist :fromto
9098 (if (string-match org-from-is-user-regexp from)
9100 (concat "from %f"))))))
9101 (setq org-store-link-plist plist))
9103 (defun org-add-link-props (&rest plist)
9104 "Add these properties to the link property list."
9107 (setq key (pop plist) value (pop plist))
9108 (setq org-store-link-plist
9109 (plist-put org-store-link-plist key value)))))
9111 (defun org-email-link-description (&optional fmt)
9112 "Return the description part of an email link.
9113 This takes information from `org-store-link-plist' and formats it
9114 according to FMT (default from `org-email-link-description-format')."
9115 (setq fmt (or fmt org-email-link-description-format))
9116 (let* ((p org-store-link-plist)
9117 (to (plist-get p :toaddress))
9118 (from (plist-get p :fromaddress))
9121 (cons "%c" (plist-get p :fromto))
9122 (cons "%F" (plist-get p :from))
9123 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
9124 (cons "%T" (plist-get p :to))
9125 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
9126 (cons "%s" (plist-get p :subject))
9127 (cons "%d" (plist-get p :date))
9128 (cons "%m" (plist-get p :message-id)))))
9129 (when (string-match "%c" fmt)
9130 ;; Check if the user wrote this message
9131 (if (and org-from-is-user-regexp from to
9132 (save-match-data (string-match org-from-is-user-regexp from)))
9133 (setq fmt (replace-match "to %t" t t fmt))
9134 (setq fmt (replace-match "from %f" t t fmt))))
9135 (org-replace-escapes fmt table)))
9137 (defun org-make-org-heading-search-string (&optional string heading)
9138 "Make search string for STRING or current headline."
9140 (let ((s (or string (org-get-heading)))
9141 (lines org-context-in-file-links))
9142 (unless (and string (not heading))
9143 ;; We are using a headline, clean up garbage in there.
9144 (if (string-match org-todo-regexp s)
9145 (setq s (replace-match "" t t s)))
9146 (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
9147 (setq s (replace-match "" t t s)))
9148 (setq s (org-trim s))
9149 (if (string-match (concat "^\\(" org-quote-string "\\|"
9150 org-comment-string "\\)") s)
9151 (setq s (replace-match "" t t s)))
9152 (while (string-match org-ts-regexp s)
9153 (setq s (replace-match "" t t s))))
9154 (or string (setq s (concat "*" s))) ; Add * for headlines
9155 (when (and string (integerp lines) (> lines 0))
9156 (let ((slines (org-split-string s "\n")))
9157 (when (< lines (length slines))
9160 (reverse (nthcdr (- (length slines) lines)
9161 (reverse slines))) "\n")))))
9162 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
9164 (defun org-make-link-string (link &optional description)
9165 "Make a link with brackets, consisting of LINK and DESCRIPTION."
9166 (unless (string-match "\\S-" link)
9167 (error "Empty link"))
9168 (when (and description
9169 (stringp description)
9170 (not (string-match "\\S-" description)))
9171 (setq description nil))
9172 (when (stringp description)
9173 ;; Remove brackets from the description, they are fatal.
9174 (while (string-match "\\[" description)
9175 (setq description (replace-match "{" t t description)))
9176 (while (string-match "\\]" description)
9177 (setq description (replace-match "}" t t description))))
9178 (when (equal link description)
9179 ;; No description needed, it is identical
9180 (setq description nil))
9181 (when (and (not description)
9182 (not (string-match (org-image-file-name-regexp) link))
9183 (not (equal link (org-link-escape link))))
9184 (setq description (org-extract-attributes link)))
9186 (cond ((string-match (org-image-file-name-regexp) link) link)
9187 ((string-match org-link-types-re link)
9188 (concat (match-string 1 link)
9189 (org-link-escape (substring link (match-end 1)))))
9190 (t (org-link-escape link))))
9191 (concat "[[" link "]"
9192 (if description (concat "[" description "]") "")
9195 (defconst org-link-escape-chars
9196 '(?\ ?\[ ?\] ?\; ?\= ?\+)
9197 "List of characters that should be escaped in link.
9198 This is the list that is used for internal purposes.")
9200 (defconst org-link-escape-chars-browser
9202 "List of escapes for characters that are problematic in links.
9203 This is the list that is used before handing over to the browser.")
9205 (defun org-link-escape (text &optional table merge)
9206 "Return percent escaped representation of TEXT.
9207 TEXT is a string with the text to escape.
9208 Optional argument TABLE is a list with characters that should be
9209 escaped. When nil, `org-link-escape-chars' is used.
9210 If optional argument MERGE is set, merge TABLE into
9211 `org-link-escape-chars'."
9214 (mapc (lambda (defchr)
9215 (unless (member defchr table)
9216 (setq table (cons defchr table)))) org-link-escape-chars))
9218 (setq table org-link-escape-chars)))
9221 (if (or (member char table)
9222 (and (or (< char 32) (= char 37) (> char 126))
9224 (mapconcat (lambda (sequence-element)
9225 (format "%%%.2X" sequence-element))
9226 (or (encode-coding-char char 'utf-8)
9227 (error "Unable to percent escape character: %s"
9228 (char-to-string char))) "")
9229 (char-to-string char))) text ""))
9231 (defun org-link-unescape (str)
9232 "Unhex hexified Unicode strings as returned from the JavaScript function
9233 encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
9234 (unless (and (null str) (string= "" str))
9235 (let ((pos 0) (case-fold-search t) unhexed)
9236 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
9237 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
9238 (setq str (replace-match unhexed t t str))
9239 (setq pos (+ pos (length unhexed))))))
9242 (defun org-link-unescape-compound (hex)
9243 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
9244 Note: this function also decodes single byte encodings like
9245 `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
9247 (let* ((bytes (cdr (split-string hex "%")))
9252 (let* ((val (string-to-number (pop bytes) 16))
9256 ((>= val 252) (cons 6 252))
9257 ((>= val 248) (cons 5 248))
9258 ((>= val 240) (cons 4 240))
9259 ((>= val 224) (cons 3 224))
9260 ((>= val 192) (cons 2 192))
9263 (if (>= val 192) (setq eat (car shift-xor)))
9264 (setq val (logxor val (cdr shift-xor)))
9265 (setq sum (+ (lsh sum (car shift-xor)) val))
9266 (if (> eat 0) (setq eat (- eat 1)))
9268 ((= 0 eat) ;multi byte
9269 (setq ret (concat ret (org-char-to-string sum)))
9271 ((not bytes) ; single byte(s)
9272 (setq ret (org-link-unescape-single-byte-sequence hex))))
9273 )) ;; end (while bytes
9276 (defun org-link-unescape-single-byte-sequence (hex)
9277 "Unhexify hex-encoded single byte character sequences."
9278 (mapconcat (lambda (byte)
9279 (char-to-string (string-to-number byte 16)))
9280 (cdr (split-string hex "%")) ""))
9282 (defun org-xor (a b)
9286 (defun org-fixup-message-id-for-http (s)
9287 "Replace special characters in a message id, so it can be used in an http query."
9288 (when (string-match "%" s)
9289 (setq s (mapconcat (lambda (c)
9292 (char-to-string c)))
9294 (while (string-match "<" s)
9295 (setq s (replace-match "%3C" t t s)))
9296 (while (string-match ">" s)
9297 (setq s (replace-match "%3E" t t s)))
9298 (while (string-match "@" s)
9299 (setq s (replace-match "%40" t t s)))
9302 (defun org-link-prettify (link)
9303 "Return a human-readable representation of LINK.
9304 The car of LINK must be a raw link the cdr of LINK must be either
9305 a link description or nil."
9306 (let ((desc (or (cadr link) "<no description>")))
9307 (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
9308 "<" (car link) ">")))
9311 (defun org-insert-link-global ()
9312 "Insert a link like Org-mode does.
9313 This command can be called in any mode to insert a link in Org-mode syntax."
9315 (org-load-modules-maybe)
9316 (org-run-like-in-org-mode 'org-insert-link))
9318 (defun org-insert-all-links (&optional keep)
9319 "Insert all links in `org-stored-links'."
9321 (let ((links (copy-sequence org-stored-links)) l)
9322 (while (setq l (if keep (pop links) (pop org-stored-links)))
9324 (org-insert-link nil (car l) (cadr l))
9327 (defun org-link-fontify-links-to-this-file ()
9328 "Fontify links to the current file in `org-stored-links'."
9329 (let ((f (buffer-file-name)) a b)
9330 (setq a (mapcar (lambda(l)
9332 (when (and (string-match "^file:\\(.+\\)::" ll)
9333 (equal f (expand-file-name (match-string 1 ll))))
9336 (when (featurep 'org-id)
9337 (setq b (mapcar (lambda(l)
9339 (when (and (string-match "^id:\\(.+\\)$" ll)
9340 (equal f (expand-file-name
9341 (or (org-id-find-id-file
9342 (match-string 1 ll)) ""))))
9346 (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
9347 (delq nil (append a b)))))
9349 (defvar org-link-links-in-this-file nil)
9350 (defun org-insert-link (&optional complete-file link-location default-description)
9351 "Insert a link. At the prompt, enter the link.
9353 Completion can be used to insert any of the link protocol prefixes like
9356 The history can be used to select a link previously stored with
9357 `org-store-link'. When the empty string is entered (i.e. if you just
9358 press RET at the prompt), the link defaults to the most recently
9359 stored link. As SPC triggers completion in the minibuffer, you need to
9360 use M-SPC or C-q SPC to force the insertion of a space character.
9362 You will also be prompted for a description, and if one is given, it will
9363 be displayed in the buffer instead of the link.
9365 If there is already a link at point, this command will allow you to edit link
9366 and description parts.
9368 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9369 be selected using completion. The path to the file will be relative to the
9370 current directory if the file is in the current directory or a subdirectory.
9371 Otherwise, the link will be the absolute path as completed in the minibuffer
9372 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9373 option `org-link-file-path-type'.
9375 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9376 the current directory or below.
9378 With three \\[universal-argument] prefixes, negate the meaning of
9379 `org-keep-stored-link-after-insertion'.
9381 If `org-make-link-description-function' is non-nil, this function will be
9382 called with the link target, and the result will be the default
9385 If the LINK-LOCATION parameter is non-nil, this value will be
9386 used as the link location instead of reading one interactively.
9388 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9389 be used as the default description."
9391 (let* ((wcf (current-window-configuration))
9392 (region (if (org-region-active-p)
9393 (buffer-substring (region-beginning) (region-end))))
9394 (remove (and region (list (region-beginning) (region-end))))
9396 tmphist ; byte-compile incorrectly complains about this
9397 (link link-location)
9398 (abbrevs org-link-abbrev-alist-local)
9399 entry file all-prefixes auto-desc)
9401 (link-location) ; specified by arg, just use it.
9402 ((org-in-regexp org-bracket-link-regexp 1)
9403 ;; We do have a link at point, and we are going to edit it.
9404 (setq remove (list (match-beginning 0) (match-end 0)))
9405 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
9406 (setq link (read-string "Link: "
9408 (org-match-string-no-properties 1)))))
9409 ((or (org-in-regexp org-angle-link-re)
9410 (org-in-regexp org-plain-link-re))
9411 ;; Convert to bracket link
9412 (setq remove (list (match-beginning 0) (match-end 0))
9413 link (read-string "Link: "
9414 (org-remove-angle-brackets (match-string 0)))))
9415 ((member complete-file '((4) (16)))
9416 ;; Completing read for file names.
9417 (setq link (org-file-complete-link complete-file)))
9419 ;; Read link, with completion for stored links.
9420 (org-link-fontify-links-to-this-file)
9421 (org-switch-to-buffer-other-window "*Org Links*")
9422 (with-current-buffer "*Org Links*"
9424 (insert "Insert a link.
9425 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9426 (when org-stored-links
9427 (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9428 (insert (mapconcat 'org-link-prettify
9429 (reverse org-stored-links) "\n")))
9430 (goto-char (point-min)))
9431 (let ((cw (selected-window)))
9432 (select-window (get-buffer-window "*Org Links*" 'visible))
9433 (with-current-buffer "*Org Links*" (setq truncate-lines t))
9434 (unless (pos-visible-in-window-p (point-max))
9435 (org-fit-window-to-buffer))
9436 (and (window-live-p cw) (select-window cw)))
9437 ;; Fake a link history, containing the stored links.
9438 (setq tmphist (append (mapcar 'car org-stored-links)
9439 org-insert-link-history))
9440 (setq all-prefixes (append (mapcar 'car abbrevs)
9441 (mapcar 'car org-link-abbrev-alist)
9446 (let ((org-completion-use-ido nil)
9447 (org-completion-use-iswitchb nil))
9448 (org-completing-read
9451 (mapcar (lambda (x) (list (concat x ":")))
9453 (mapcar 'car org-stored-links)
9454 (mapcar 'cadr org-stored-links))
9457 (caar org-stored-links))))
9458 (if (not (string-match "\\S-" link))
9459 (error "No link selected"))
9461 (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
9463 (if (or (member link all-prefixes)
9464 (and (equal ":" (substring link -1))
9465 (member (substring link 0 -1) all-prefixes)
9466 (setq link (substring link 0 -1))))
9467 (setq link (org-link-try-special-completion link))))
9468 (set-window-configuration wcf)
9469 (kill-buffer "*Org Links*"))
9470 (setq entry (assoc link org-stored-links))
9471 (or entry (push link org-insert-link-history))
9472 (setq desc (or desc (nth 1 entry)))))
9474 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
9475 (not org-keep-stored-link-after-insertion))
9476 (setq org-stored-links (delq (assoc link org-stored-links)
9479 (if (string-match org-plain-link-re link)
9480 ;; URL-like link, normalize the use of angular brackets.
9481 (setq link (org-remove-angle-brackets link)))
9483 ;; Check if we are linking to the current file with a search option
9484 ;; If yes, simplify the link by using only the search option.
9485 (when (and buffer-file-name
9486 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
9487 (let* ((path (match-string 1 link))
9488 (case-fold-search nil)
9489 (search (match-string 2 link)))
9491 (if (equal (file-truename buffer-file-name) (file-truename path))
9492 ;; We are linking to this same file, with a search option
9493 (setq link search)))))
9495 ;; Check if we can/should use a relative path. If yes, simplify the link
9496 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
9497 (let* ((type (match-string 1 link))
9498 (path (match-string 2 link))
9500 (case-fold-search nil))
9502 ((or (eq org-link-file-path-type 'absolute)
9503 (equal complete-file '(16)))
9504 (setq path (abbreviate-file-name (expand-file-name path))))
9505 ((eq org-link-file-path-type 'noabbrev)
9506 (setq path (expand-file-name path)))
9507 ((eq org-link-file-path-type 'relative)
9508 (setq path (file-relative-name path)))
9511 (if (string-match (concat "^" (regexp-quote
9513 (file-name-as-directory
9514 default-directory))))
9515 (expand-file-name path))
9516 ;; We are linking a file with relative path name.
9517 (setq path (substring (expand-file-name path)
9519 (setq path (abbreviate-file-name (expand-file-name path)))))))
9520 (setq link (concat type path))
9521 (if (equal desc origpath)
9524 (if org-make-link-description-function
9525 (setq desc (funcall org-make-link-description-function link desc))
9526 (if default-description (setq desc default-description)
9527 (setq desc (or (and auto-desc desc)
9528 (read-string "Description: " desc)))))
9530 (unless (string-match "\\S-" desc) (setq desc nil))
9531 (if remove (apply 'delete-region remove))
9532 (insert (org-make-link-string link desc))))
9534 (defun org-link-try-special-completion (type)
9535 "If there is completion support for link type TYPE, offer it."
9536 (let ((fun (intern (concat "org-" type "-complete-link"))))
9539 (read-string "Link (no completion support): " (concat type ":")))))
9541 (defun org-file-complete-link (&optional arg)
9542 "Create a file link using completion."
9544 (setq file (read-file-name "File: "))
9545 (let ((pwd (file-name-as-directory (expand-file-name ".")))
9546 (pwd1 (file-name-as-directory (abbreviate-file-name
9547 (expand-file-name ".")))))
9552 (abbreviate-file-name (expand-file-name file)))))
9553 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
9554 (setq link (concat "file:" (match-string 1 file))))
9555 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
9556 (expand-file-name file))
9558 "file:" (match-string 1 (expand-file-name file)))))
9559 (t (setq link (concat "file:" file)))))
9562 (defun org-completing-read (&rest args)
9563 "Completing-read with SPACE being a normal character."
9564 (let ((enable-recursive-minibuffers t)
9565 (minibuffer-local-completion-map
9566 (copy-keymap minibuffer-local-completion-map)))
9567 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
9568 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
9569 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
9570 (apply 'org-icompleting-read args)))
9572 (defun org-completing-read-no-i (&rest args)
9573 (let (org-completion-use-ido org-completion-use-iswitchb)
9574 (apply 'org-completing-read args)))
9576 (defun org-iswitchb-completing-read (prompt choices &rest args)
9577 "Use iswitch as a completing-read replacement to choose from choices.
9578 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
9580 (let* ((iswitchb-use-virtual-buffers nil)
9581 (iswitchb-make-buflist-hook
9583 (setq iswitchb-temp-buflist choices))))
9584 (iswitchb-read-buffer prompt)))
9586 (defun org-icompleting-read (&rest args)
9587 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
9588 (org-without-partial-completion
9589 (if (and org-completion-use-ido
9590 (fboundp 'ido-completing-read)
9591 (boundp 'ido-mode) ido-mode
9592 (listp (second args)))
9593 (let ((ido-enter-matching-directory nil))
9594 (apply 'ido-completing-read (concat (car args))
9595 (if (consp (car (nth 1 args)))
9596 (mapcar 'car (nth 1 args))
9599 (if (and org-completion-use-iswitchb
9600 (boundp 'iswitchb-mode) iswitchb-mode
9601 (listp (second args)))
9602 (apply 'org-iswitchb-completing-read (concat (car args))
9603 (if (consp (car (nth 1 args)))
9604 (mapcar 'car (nth 1 args))
9607 (apply 'completing-read args)))))
9609 (defun org-extract-attributes (s)
9610 "Extract the attributes cookie from a string and set as text property."
9611 (let (a attr (start 0) key value)
9613 (when (string-match "{{\\([^}]+\\)}}$" s)
9614 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
9615 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
9616 (setq key (match-string 1 a) value (match-string 2 a)
9618 attr (plist-put attr (intern key) value))))
9619 (org-add-props s nil 'org-attr attr))
9622 (defun org-extract-attributes-from-string (tag)
9623 (let (key value attr)
9624 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
9625 (setq key (match-string 1 tag) value (match-string 2 tag)
9626 tag (replace-match "" t t tag)
9627 attr (plist-put attr (intern key) value)))
9630 (defun org-attributes-to-string (plist)
9631 "Format a property list into an HTML attribute list."
9632 (let ((s "") key value)
9634 (setq key (pop plist) value (pop plist))
9636 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
9639 ;;; Opening/following a link
9641 (defvar org-link-search-failed nil)
9643 (defvar org-open-link-functions nil
9644 "Hook for functions finding a plain text link.
9645 These functions must take a single argument, the link content.
9646 They will be called for links that look like [[link text][description]]
9647 when LINK TEXT does not have a protocol like \"http:\" and does not look
9648 like a filename (e.g. \"./blue.png\").
9650 These functions will be called *before* Org attempts to resolve the
9651 link by doing text searches in the current buffer - so if you want a
9652 link \"[[target]]\" to still find \"<<target>>\", your function should
9653 handle this as a special case.
9655 When the function does handle the link, it must return a non-nil value.
9656 If it decides that it is not responsible for this link, it must return
9657 nil to indicate that that Org-mode can continue with other options
9658 like exact and fuzzy text search.")
9660 (defun org-next-link ()
9661 "Move forward to the next link.
9662 If the link is in hidden text, expose it."
9664 (when (and org-link-search-failed (eq this-command last-command))
9665 (goto-char (point-min))
9666 (message "Link search wrapped back to beginning of buffer"))
9667 (setq org-link-search-failed nil)
9668 (let* ((pos (point))
9670 (a (assoc :link ct)))
9671 (if a (goto-char (nth 2 a)))
9672 (if (re-search-forward org-any-link-re nil t)
9674 (goto-char (match-beginning 0))
9675 (if (outline-invisible-p) (org-show-context)))
9677 (setq org-link-search-failed t)
9678 (error "No further link found"))))
9680 (defun org-previous-link ()
9681 "Move backward to the previous link.
9682 If the link is in hidden text, expose it."
9684 (when (and org-link-search-failed (eq this-command last-command))
9685 (goto-char (point-max))
9686 (message "Link search wrapped back to end of buffer"))
9687 (setq org-link-search-failed nil)
9688 (let* ((pos (point))
9690 (a (assoc :link ct)))
9691 (if a (goto-char (nth 1 a)))
9692 (if (re-search-backward org-any-link-re nil t)
9694 (goto-char (match-beginning 0))
9695 (if (outline-invisible-p) (org-show-context)))
9697 (setq org-link-search-failed t)
9698 (error "No further link found"))))
9700 (defun org-translate-link (s)
9701 "Translate a link string if a translation function has been defined."
9702 (if (and org-link-translation-function
9703 (fboundp org-link-translation-function)
9704 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
9706 (setq s (funcall org-link-translation-function
9707 (match-string 1 s) (match-string 2 s)))
9708 (concat (car s) ":" (cdr s)))
9711 (defun org-translate-link-from-planner (type path)
9712 "Translate a link from Emacs Planner syntax so that Org can follow it.
9713 This is still an experimental function, your mileage may vary."
9715 ((member type '("http" "https" "news" "ftp"))
9716 ;; standard Internet links are the same.
9718 ((and (equal type "irc") (string-match "^//" path))
9719 ;; Planner has two / at the beginning of an irc link, we have 1.
9720 ;; We should have zero, actually....
9721 (setq path (substring path 1)))
9722 ((and (equal type "lisp") (string-match "^/" path))
9723 ;; Planner has a slash, we do not.
9724 (setq type "elisp" path (substring path 1)))
9725 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
9726 ;; A typical message link. Planner has the id after the final slash,
9727 ;; we separate it with a hash mark
9728 (setq path (concat (match-string 1 path) "#"
9729 (org-remove-angle-brackets (match-string 2 path)))))
9733 (defun org-find-file-at-mouse (ev)
9734 "Open file link or URL at mouse."
9736 (mouse-set-point ev)
9737 (org-open-at-point 'in-emacs))
9739 (defun org-open-at-mouse (ev)
9740 "Open file link or URL at mouse.
9741 See the docstring of `org-open-file' for details."
9743 (mouse-set-point ev)
9744 (if (eq major-mode 'org-agenda-mode)
9745 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
9746 (org-open-at-point))
9748 (defvar org-window-config-before-follow-link nil
9749 "The window configuration before following a link.
9750 This is saved in case the need arises to restore it.")
9752 (defvar org-open-link-marker (make-marker)
9753 "Marker pointing to the location where `org-open-at-point; was called.")
9756 (defun org-open-at-point-global ()
9757 "Follow a link like Org-mode does.
9758 This command can be called in any mode to follow a link that has
9761 (org-run-like-in-org-mode 'org-open-at-point))
9764 (defun org-open-link-from-string (s &optional arg reference-buffer)
9765 "Open a link in the string S, as if it was in Org-mode."
9766 (interactive "sLink: \nP")
9767 (let ((reference-buffer (or reference-buffer (current-buffer))))
9769 (let ((org-inhibit-startup (not reference-buffer)))
9772 (goto-char (point-min))
9773 (when reference-buffer
9774 (setq org-link-abbrev-alist-local
9775 (with-current-buffer reference-buffer
9776 org-link-abbrev-alist-local)))
9777 (org-open-at-point arg reference-buffer)))))
9779 (defvar org-open-at-point-functions nil
9780 "Hook that is run when following a link at point.
9782 Functions in this hook must return t if they identify and follow
9783 a link at point. If they don't find anything interesting at point,
9784 they must return nil.")
9786 (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
9787 (defun org-open-at-point (&optional arg reference-buffer)
9788 "Open link at or after point.
9789 If there is no link at point, this function will search forward up to
9790 the end of the current line.
9791 Normally, files will be opened by an appropriate application. If the
9792 optional prefix argument ARG is non-nil, Emacs will visit the file.
9793 With a double prefix argument, try to open outside of Emacs, in the
9794 application the system uses for this file type."
9796 ;; if in a code block, then open the block's results
9797 (unless (call-interactively #'org-babel-open-src-block-result)
9798 (org-load-modules-maybe)
9799 (move-marker org-open-link-marker (point))
9800 (setq org-window-config-before-follow-link (current-window-configuration))
9801 (org-remove-occur-highlights nil nil t)
9803 ((and (org-at-heading-p)
9804 (not (org-at-timestamp-p t))
9806 (concat org-plain-link-re "\\|"
9807 org-bracket-link-regexp "\\|"
9808 org-angle-link-re "\\|"
9809 "[ \t]:[^ \t\n]+:[ \t]*$")))
9810 (not (get-text-property (point) 'org-linked-text)))
9811 (or (org-offer-links-in-entry arg)
9812 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
9813 ((run-hook-with-args-until-success 'org-open-at-point-functions))
9814 ((org-at-timestamp-p t) (org-follow-timestamp-link))
9815 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
9816 (not (org-in-regexp org-bracket-link-regexp)))
9817 (org-footnote-action))
9819 (let (type path link line search (pos (point)))
9822 (skip-chars-forward "^]\n\r")
9823 (when (org-in-regexp org-bracket-link-regexp 1)
9824 (setq link (org-extract-attributes
9825 (org-link-unescape (org-match-string-no-properties 1))))
9826 (while (string-match " *\n *" link)
9827 (setq link (replace-match " " t t link)))
9828 (setq link (org-link-expand-abbrev link))
9830 ((or (file-name-absolute-p link)
9831 (string-match "^\\.\\.?/" link))
9832 (setq type "file" path link))
9833 ((string-match org-link-re-with-space3 link)
9834 (setq type (match-string 1 link) path (match-string 2 link)))
9835 ((string-match "^help:+\\(.+\\)" link)
9836 (setq type "help" path (match-string 1 link)))
9837 (t (setq type "thisfile" path link)))
9840 (when (get-text-property (point) 'org-linked-text)
9841 (setq type "thisfile"
9842 pos (if (get-text-property (1+ (point)) 'org-linked-text)
9843 (1+ (point)) (point))
9844 path (buffer-substring
9845 (or (previous-single-property-change pos 'org-linked-text)
9847 (or (next-single-property-change pos 'org-linked-text)
9852 (when (or (org-in-regexp org-angle-link-re)
9853 (and (goto-char (car (org-in-regexp org-plain-link-re)))
9854 (save-match-data (not (looking-back "\\[\\[")))))
9855 (setq type (match-string 1)
9856 path (org-link-unescape (match-string 2)))
9859 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
9861 path (match-string 1))
9862 (while (string-match ":" path)
9863 (setq path (replace-match "+" t t path)))
9865 (when (org-in-regexp "<\\([^><\n]+\\)>")
9866 (setq type "tree-match"
9867 path (match-string 1))
9870 (error "No link found"))
9872 ;; switch back to reference buffer
9873 ;; needed when if called in a temporary buffer through
9874 ;; org-open-link-from-string
9875 (with-current-buffer (or reference-buffer (current-buffer))
9877 ;; Remove any trailing spaces in path
9878 (if (string-match " +\\'" path)
9879 (setq path (replace-match "" t t path)))
9880 (if (and org-link-translation-function
9881 (fboundp org-link-translation-function))
9882 ;; Check if we need to translate the link
9883 (let ((tmp (funcall org-link-translation-function type path)))
9884 (setq type (car tmp) path (cdr tmp))))
9888 ((assoc type org-link-protocols)
9889 (funcall (nth 1 (assoc type org-link-protocols)) path))
9891 ((equal type "help")
9892 (let ((f-or-v (intern path)))
9893 (cond ((fboundp f-or-v)
9894 (describe-function f-or-v))
9896 (describe-variable f-or-v))
9897 (t (error "Not a known function or variable")))))
9899 ((equal type "mailto")
9900 (let ((cmd (car org-link-mailto-program))
9901 (args (cdr org-link-mailto-program)) args1
9902 (address path) (subject "") a)
9903 (if (string-match "\\(.*\\)::\\(.*\\)" path)
9904 (setq address (match-string 1 path)
9905 subject (org-link-escape (match-string 2 path))))
9908 ((not (stringp (car args))) (push (pop args) args1))
9909 (t (setq a (pop args))
9910 (if (string-match "%a" a)
9911 (setq a (replace-match address t t a)))
9912 (if (string-match "%s" a)
9913 (setq a (replace-match subject t t a)))
9915 (apply cmd (nreverse args1))))
9917 ((member type '("http" "https" "ftp" "news"))
9918 (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
9920 path org-link-escape-chars-browser)
9923 ((string= type "doi")
9924 (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
9926 path org-link-escape-chars-browser)
9929 ((member type '("message"))
9930 (browse-url (concat type ":" path)))
9932 ((string= type "tags")
9933 (org-tags-view arg path))
9935 ((string= type "tree-match")
9936 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
9938 ((string= type "file")
9939 (if (string-match "::\\([0-9]+\\)\\'" path)
9940 (setq line (string-to-number (match-string 1 path))
9941 path (substring path 0 (match-beginning 0)))
9942 (if (string-match "::\\(.+\\)\\'" path)
9943 (setq search (match-string 1 path)
9944 path (substring path 0 (match-beginning 0)))))
9945 (if (string-match "[*?{]" (file-name-nondirectory path))
9947 (org-open-file path arg line search)))
9949 ((string= type "shell")
9950 (let ((buf (generate-new-buffer "*Org Shell Output"))
9952 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
9953 (string-match org-confirm-shell-link-not-regexp cmd))
9954 (not org-confirm-shell-link-function)
9955 (funcall org-confirm-shell-link-function
9956 (format "Execute \"%s\" in shell? "
9957 (org-add-props cmd nil
9958 'face 'org-warning))))
9960 (message "Executing %s" cmd)
9961 (shell-command cmd buf)
9962 (if (featurep 'midnight)
9963 (setq clean-buffer-list-kill-buffer-names
9964 (cons buf clean-buffer-list-kill-buffer-names))))
9967 ((string= type "elisp")
9969 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
9970 (string-match org-confirm-elisp-link-not-regexp cmd))
9971 (not org-confirm-elisp-link-function)
9972 (funcall org-confirm-elisp-link-function
9973 (format "Execute \"%s\" as elisp? "
9974 (org-add-props cmd nil
9975 'face 'org-warning))))
9976 (message "%s => %s" cmd
9977 (if (equal (string-to-char cmd) ?\()
9979 (call-interactively (read cmd))))
9982 ((and (string= type "thisfile")
9983 (run-hook-with-args-until-success
9984 'org-open-link-functions path)))
9986 ((string= type "thisfile")
9988 (switch-to-buffer-other-window
9989 (org-get-buffer-for-internal-link (current-buffer)))
9990 (org-mark-ring-push))
9991 (let ((cmd `(org-link-search
9993 ,(cond ((equal arg '(4)) ''occur)
9994 ((equal arg '(16)) ''org-occur))
9996 (condition-case nil (let ((org-link-search-inhibit-query t))
9998 (error (progn (widen) (eval cmd))))))
10000 (t (browse-url-at-point)))))))
10001 (move-marker org-open-link-marker nil)
10002 (run-hook-with-args 'org-follow-link-hook)))
10004 (defun org-offer-links-in-entry (&optional nth zero)
10005 "Offer links in the current entry and follow the selected link.
10006 If there is only one link, follow it immediately as well.
10007 If NTH is an integer, immediately pick the NTH link found.
10008 If ZERO is a string, check also this string for a link, and if
10009 there is one, offer it as link number zero."
10010 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
10011 "\\(" org-angle-link-re "\\)\\|"
10012 "\\(" org-plain-link-re "\\)"))
10014 (in-emacs (if (integerp nth) nil nth))
10015 have-zero end links link c)
10016 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
10017 (push (match-string 0 zero) links)
10018 (setq cnt (1- cnt) have-zero t))
10020 (org-back-to-heading t)
10021 (setq end (save-excursion (outline-next-heading) (point)))
10022 (while (re-search-forward re end t)
10023 (push (match-string 0) links))
10024 (setq links (org-uniquify (reverse links))))
10028 (message "No links"))
10029 ((equal (length links) 1)
10030 (setq link (list (car links))))
10031 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
10032 (setq link (list (nth (if have-zero nth (1- nth)) links))))
10033 (t ; we have to select a link
10035 (save-window-excursion
10036 (delete-other-windows)
10037 (with-output-to-temp-buffer "*Select Link*"
10039 (if (not (string-match org-bracket-link-regexp l))
10040 (princ (format "[%c] %s\n" (incf cnt)
10041 (org-remove-angle-brackets l)))
10043 (princ (format "[%c] %s (%s)\n" (incf cnt)
10044 (match-string 3 l) (match-string 1 l)))
10045 (princ (format "[%c] %s\n" (incf cnt)
10046 (match-string 1 l))))))
10048 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
10049 (message "Select link to open, RET to open all:")
10050 (setq c (read-char-exclusive))
10051 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
10052 (when (equal c ?q) (error "Abort"))
10053 (if (equal c ?\C-m)
10055 (setq nth (- c ?0))
10056 (if have-zero (setq nth (1+ nth)))
10057 (unless (and (integerp nth) (>= (length links) nth))
10058 (error "Invalid link selection"))
10059 (setq link (list (nth (1- nth) links))))))
10061 (let ((buf (current-buffer)))
10063 (org-open-link-from-string l in-emacs buf))
10067 ;; Add special file links that specify the way of opening
10069 (org-add-link-type "file+sys" 'org-open-file-with-system)
10070 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
10071 (defun org-open-file-with-system (path)
10072 "Open file at PATH using the system way of opening it."
10073 (org-open-file path 'system))
10074 (defun org-open-file-with-emacs (path)
10075 "Open file at PATH in Emacs."
10076 (org-open-file path 'emacs))
10077 (defun org-remove-file-link-modifiers ()
10078 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
10079 (goto-char (point-min))
10080 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
10081 (org-if-unprotected
10082 (replace-match "file:" t t))))
10083 (eval-after-load "org-exp"
10084 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
10085 'org-remove-file-link-modifiers))
10087 ;;;; Time estimates
10089 (defun org-get-effort (&optional pom)
10090 "Get the effort estimate for the current entry."
10091 (org-entry-get pom org-effort-property))
10095 (defvar org-create-file-search-functions nil
10096 "List of functions to construct the right search string for a file link.
10097 These functions are called in turn with point at the location to
10098 which the link should point.
10100 A function in the hook should first test if it would like to
10101 handle this file type, for example by checking the `major-mode'
10102 or the file extension. If it decides not to handle this file, it
10103 should just return nil to give other functions a chance. If it
10104 does handle the file, it must return the search string to be used
10105 when following the link. The search string will be part of the
10106 file link, given after a double colon, and `org-open-at-point'
10107 will automatically search for it. If special measures must be
10108 taken to make the search successful, another function should be
10109 added to the companion hook `org-execute-file-search-functions',
10112 A function in this hook may also use `setq' to set the variable
10113 `description' to provide a suggestion for the descriptive text to
10114 be used for this link when it gets inserted into an Org-mode
10115 buffer with \\[org-insert-link].")
10117 (defvar org-execute-file-search-functions nil
10118 "List of functions to execute a file search triggered by a link.
10120 Functions added to this hook must accept a single argument, the
10121 search string that was part of the file link, the part after the
10122 double colon. The function must first check if it would like to
10123 handle this search, for example by checking the `major-mode' or
10124 the file extension. If it decides not to handle this search, it
10125 should just return nil to give other functions a chance. If it
10126 does handle the search, it must return a non-nil value to keep
10127 other functions from trying.
10129 Each function can access the current prefix argument through the
10130 variable `current-prefix-argument'. Note that a single prefix is
10131 used to force opening a link in Emacs, so it may be good to only
10132 use a numeric or double prefix to guide the search function.
10134 In case this is needed, a function in this hook can also restore
10135 the window configuration before `org-open-at-point' was called using:
10137 (set-window-configuration org-window-config-before-follow-link)")
10139 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
10140 (defun org-link-search (s &optional type avoid-pos stealth)
10141 "Search for a link search option.
10142 If S is surrounded by forward slashes, it is interpreted as a
10143 regular expression. In org-mode files, this will create an `org-occur'
10144 sparse tree. In ordinary files, `occur' will be used to list matches.
10145 If the current buffer is in `dired-mode', grep will be used to search
10146 in all files. If AVOID-POS is given, ignore matches near that position.
10148 When optional argument STEALTH is non-nil, do not modify
10149 visibility around point, thus ignoring
10150 `org-show-hierarchy-above', `org-show-following-heading' and
10151 `org-show-siblings' variables."
10152 (let ((case-fold-search t)
10153 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
10154 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
10155 (append '(("") (" ") ("\t") ("\n"))
10156 org-emphasis-alist)
10159 (pre nil) (post nil)
10160 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
10162 ;; First check if there are any special search functions
10163 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
10164 ;; Now try the builtin stuff
10165 ((and (equal (string-to-char s0) ?#)
10168 (goto-char (point-min))
10171 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
10172 (setq type 'dedicated
10173 pos (match-beginning 0))))
10174 ;; There is an exact target for this
10176 (org-back-to-heading t)))
10178 (goto-char (point-min))
10181 (concat "<<" (regexp-quote s0) ">>") nil t)
10182 (setq type 'dedicated
10183 pos (match-beginning 0))))
10184 ;; There is an exact target for this
10187 (goto-char (point-min))
10190 (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
10191 (setq type 'dedicated pos (match-beginning 0))))
10192 ;; Found an invisible target.
10195 (goto-char (point-min))
10198 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
10199 (setq type 'dedicated pos (match-beginning 0))))
10200 ;; Found an element with a matching #+name affiliated keyword.
10202 ((and (string-match "^(\\(.*\\))$" s0)
10204 (goto-char (point-min))
10207 (concat "[^[]" (regexp-quote
10208 (format org-coderef-label-format
10209 (match-string 1 s0))))
10211 (setq type 'dedicated
10212 pos (1+ (match-beginning 0))))))
10213 ;; There is a coderef target for this
10215 ((string-match "^/\\(.*\\)/$" s)
10216 ;; A regular expression
10218 ((derived-mode-p 'org-mode)
10219 (org-occur (match-string 1 s)))
10220 ;;((eq major-mode 'dired-mode)
10221 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
10222 (t (org-do-occur (match-string 1 s)))))
10223 ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
10224 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
10225 (goto-char (point-min))
10227 ((let (case-fold-search)
10228 (re-search-forward (format org-complex-heading-regexp-format
10231 ;; OK, found a match
10232 (setq type 'dedicated)
10233 (goto-char (match-beginning 0)))
10234 ((and (not org-link-search-inhibit-query)
10235 (eq org-link-search-must-match-exact-headline 'query-to-create)
10236 (y-or-n-p "No match - create this as a new heading? "))
10237 (goto-char (point-max))
10238 (or (bolp) (newline))
10239 (insert "* " s "\n")
10240 (beginning-of-line 0))
10243 (error "No match"))))
10245 ;; A normal search string
10246 (when (equal (string-to-char s) ?*)
10247 ;; Anchor on headlines, post may include tags.
10248 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
10249 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
10250 s (substring s 1)))
10251 (remove-text-properties
10253 '(face nil mouse-face nil keymap nil fontified nil) s)
10254 ;; Make a series of regular expressions to find a match
10255 (setq words (org-split-string s "[ \n\r\t]+")
10257 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
10258 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
10260 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
10261 re2a (concat "[ \t\r\n]" re2a_)
10262 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
10263 re4 (concat "[^a-zA-Z_]" re4_)
10265 re1 (concat pre re2 post)
10266 re3 (concat pre (if pre re4_ re4) post)
10267 re5 (concat pre ".*" re4)
10268 re2 (concat pre re2)
10269 re2a (concat pre (if pre re2a_ re2a))
10270 re4 (concat pre (if pre re4_ re4))
10271 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
10272 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
10276 ((eq type 'org-occur) (org-occur reall))
10277 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
10278 (t (goto-char (point-min))
10280 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
10281 (org-search-not-self 1 re1 nil t)
10282 (org-search-not-self 1 re2 nil t)
10283 (org-search-not-self 1 re2a nil t)
10284 (org-search-not-self 1 re3 nil t)
10285 (org-search-not-self 1 re4 nil t)
10286 (org-search-not-self 1 re5 nil t)
10288 (goto-char (match-beginning 1))
10290 (error "No match"))))))
10291 (and (derived-mode-p 'org-mode)
10293 (org-show-context 'link-search))
10296 (defun org-search-not-self (group &rest args)
10297 "Execute `re-search-forward', but only accept matches that do not
10298 enclose the position of `org-open-link-marker'."
10299 (let ((m org-open-link-marker))
10301 (while (apply 're-search-forward args)
10302 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
10303 (goto-char (match-end group))
10304 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
10305 (> (match-beginning 0) (marker-position m))
10306 (< (match-end 0) (marker-position m)))
10308 (or (not (org-in-regexp
10309 org-bracket-link-analytic-regexp 1))
10310 (not (match-end 4)) ; no description
10311 (and (<= (match-beginning 4) (point))
10312 (>= (match-end 4) (point))))))
10313 (throw 'exit (point))))))))
10315 (defun org-get-buffer-for-internal-link (buffer)
10316 "Return a buffer to be used for displaying the link target of internal links."
10318 ((not org-display-internal-link-with-indirect-buffer)
10320 ((string-match "(Clone)$" (buffer-name buffer))
10321 (message "Buffer is already a clone, not making another one")
10322 ;; we also do not modify visibility in this case
10324 (t ; make a new indirect buffer for displaying the link
10325 (let* ((bn (buffer-name buffer))
10326 (ibn (concat bn "(Clone)"))
10327 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
10328 (with-current-buffer ib (org-overview))
10331 (defun org-do-occur (regexp &optional cleanup)
10332 "Call the Emacs command `occur'.
10333 If CLEANUP is non-nil, remove the printout of the regular expression
10334 in the *Occur* buffer. This is useful if the regex is long and not useful
10338 (let ((cwin (selected-window)) win beg end)
10339 (when (setq win (get-buffer-window "*Occur*"))
10340 (select-window win))
10341 (goto-char (point-min))
10342 (when (re-search-forward "match[a-z]+" nil t)
10343 (setq beg (match-end 0))
10344 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
10345 (setq end (1- (match-beginning 0)))))
10346 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
10347 (goto-char (point-min))
10348 (select-window cwin))))
10350 ;;; The mark ring for links jumps
10352 (defvar org-mark-ring nil
10353 "Mark ring for positions before jumps in Org-mode.")
10354 (defvar org-mark-ring-last-goto nil
10355 "Last position in the mark ring used to go back.")
10356 ;; Fill and close the ring
10357 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
10358 (loop for i from 1 to org-mark-ring-length do
10359 (push (make-marker) org-mark-ring))
10360 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
10363 (defun org-mark-ring-push (&optional pos buffer)
10364 "Put the current position or POS into the mark ring and rotate it."
10366 (setq pos (or pos (point)))
10367 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10368 (move-marker (car org-mark-ring)
10370 (or buffer (current-buffer)))
10372 (substitute-command-keys
10373 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10375 (defun org-mark-ring-goto (&optional n)
10376 "Jump to the previous position in the mark ring.
10377 With prefix arg N, jump back that many stored positions. When
10378 called several times in succession, walk through the entire ring.
10379 Org-mode commands jumping to a different position in the current file,
10380 or to another Org-mode file, automatically push the old position
10384 (if (eq last-command this-command)
10385 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10386 (setq p org-mark-ring))
10387 (setq org-mark-ring-last-goto p)
10389 (org-pop-to-buffer-same-window (marker-buffer m))
10391 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
10393 (defun org-remove-angle-brackets (s)
10394 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
10395 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
10397 (defun org-add-angle-brackets (s)
10398 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
10399 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
10401 (defun org-remove-double-quotes (s)
10402 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
10403 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
10406 ;;; Following specific links
10408 (defun org-follow-timestamp-link ()
10410 ((org-at-date-range-p t)
10411 (let ((org-agenda-start-on-weekday)
10412 (t1 (match-string 1))
10413 (t2 (match-string 2)))
10414 (setq t1 (time-to-days (org-time-string-to-time t1))
10415 t2 (time-to-days (org-time-string-to-time t2)))
10416 (org-agenda-list nil t1 (1+ (- t2 t1)))))
10417 ((org-at-timestamp-p t)
10418 (org-agenda-list nil (time-to-days (org-time-string-to-time
10419 (substring (match-string 1) 0 10)))
10421 (t (error "This should not happen"))))
10424 ;;; Following file links
10425 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
10426 (declare-function mailcap-extension-to-mime "mailcap" (extn))
10427 (declare-function mailcap-mime-info
10428 "mailcap" (string &optional request no-decode))
10429 (defvar org-wait nil)
10430 (defun org-open-file (path &optional in-emacs line search)
10431 "Open the file at PATH.
10432 First, this expands any special file name abbreviations. Then the
10433 configuration variable `org-file-apps' is checked if it contains an
10434 entry for this file type, and if yes, the corresponding command is launched.
10436 If no application is found, Emacs simply visits the file.
10438 With optional prefix argument IN-EMACS, Emacs will visit the file.
10439 With a double \\[universal-argument] \\[universal-argument] \
10440 prefix arg, Org tries to avoid opening in Emacs
10441 and to use an external application to visit the file.
10443 Optional LINE specifies a line to go to, optional SEARCH a string
10444 to search for. If LINE or SEARCH is given, the file will be
10445 opened in Emacs, unless an entry from org-file-apps that makes
10446 use of groups in a regexp matches.
10448 If you want to change the way frames are used when following a
10449 link, please customize `org-link-frame-setup'.
10451 If the file does not exist, an error is thrown."
10452 (let* ((file (if (equal path "")
10454 (substitute-in-file-name (expand-file-name path))))
10455 (file-apps (append org-file-apps (org-default-apps)))
10456 (apps (org-remove-if
10457 'org-file-apps-entry-match-against-dlink-p file-apps))
10458 (apps-dlink (org-remove-if-not
10459 'org-file-apps-entry-match-against-dlink-p file-apps))
10460 (remp (and (assq 'remote apps) (org-file-remote-p file)))
10461 (dirp (if remp nil (file-directory-p file)))
10462 (file (if (and dirp org-open-directory-means-index-dot-org)
10463 (concat (file-name-as-directory file) "index.org")
10465 (a-m-a-p (assq 'auto-mode apps))
10466 (dfile (downcase file))
10467 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
10468 (link (cond ((and (eq line nil)
10472 (concat file "::" (number-to-string line)))
10474 (concat file "::" search))))
10475 (dlink (downcase link))
10476 (old-buffer (current-buffer))
10478 (old-mode major-mode)
10479 ext cmd link-match-data)
10480 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
10481 (setq ext (match-string 1 dfile))
10482 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
10483 (setq ext (match-string 1 dfile))))
10485 ((member in-emacs '((16) system))
10486 (setq cmd (cdr (assoc 'system apps))))
10487 (in-emacs (setq cmd 'emacs))
10489 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
10490 (and dirp (cdr (assoc 'directory apps)))
10491 ; first, try matching against apps-dlink
10492 ; if we get a match here, store the match data for later
10493 (let ((match (assoc-default dlink apps-dlink
10496 (progn (setq link-match-data (match-data))
10498 (progn (setq in-emacs (or in-emacs line search))
10499 nil))) ; if we have no match in apps-dlink,
10500 ; always open the file in emacs if line or search
10501 ; is given (for backwards compatibility)
10502 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
10504 (cdr (assoc ext apps))
10505 (cdr (assoc t apps))))))
10506 (when (eq cmd 'system)
10507 (setq cmd (cdr (assoc 'system apps))))
10508 (when (eq cmd 'default)
10509 (setq cmd (cdr (assoc t apps))))
10510 (when (eq cmd 'mailcap)
10512 (mailcap-parse-mailcaps)
10513 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
10514 (command (mailcap-mime-info mime-type)))
10515 (if (stringp command)
10517 (setq cmd 'emacs))))
10518 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
10519 (not (file-exists-p file))
10520 (not org-open-non-existing-files))
10521 (error "No such file: %s" file))
10523 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
10524 ;; Remove quotes around the file name - we'll use shell-quote-argument.
10525 (while (string-match "['\"]%s['\"]" cmd)
10526 (setq cmd (replace-match "%s" t t cmd)))
10527 (while (string-match "%s" cmd)
10528 (setq cmd (replace-match
10530 (shell-quote-argument
10531 (convert-standard-filename file)))
10534 ;; Replace "%1", "%2" etc. in command with group matches from regex
10536 (let ((match-index 1)
10537 (number-of-groups (- (/ (length link-match-data) 2) 1)))
10538 (set-match-data link-match-data)
10539 (while (<= match-index number-of-groups)
10540 (let ((regex (concat "%" (number-to-string match-index)))
10541 (replace-with (match-string match-index dlink)))
10542 (while (string-match regex cmd)
10543 (setq cmd (replace-match replace-with t t cmd))))
10544 (setq match-index (+ match-index 1)))))
10546 (save-window-excursion
10547 (start-process-shell-command cmd nil cmd)
10548 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
10552 (funcall (cdr (assq 'file org-link-frame-setup)) file)
10554 (if line (org-goto-line line)
10555 (if search (org-link-search search))))
10557 (let ((file (convert-standard-filename file)))
10559 (set-match-data link-match-data)
10561 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
10562 (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
10563 (or (not (equal old-buffer (current-buffer)))
10564 (not (equal old-pos (point))))
10565 (org-mark-ring-push old-pos old-buffer))))
10567 (defun org-file-apps-entry-match-against-dlink-p (entry)
10568 "This function returns non-nil if `entry' uses a regular
10569 expression which should be matched against the whole link by
10572 It assumes that is the case when the entry uses a regular
10573 expression which has at least one grouping construct and the
10574 action is either a lisp form or a command string containing
10575 '%1', i.e. using at least one subexpression match as a
10577 (let ((selector (car entry))
10578 (action (cdr entry)))
10579 (if (stringp selector)
10580 (and (> (regexp-opt-depth selector) 0)
10581 (or (and (stringp action)
10582 (string-match "%[0-9]" action))
10586 (defun org-default-apps ()
10587 "Return the default applications for this operating system."
10589 ((eq system-type 'darwin)
10590 org-file-apps-defaults-macosx)
10591 ((eq system-type 'windows-nt)
10592 org-file-apps-defaults-windowsnt)
10593 (t org-file-apps-defaults-gnu)))
10595 (defun org-apps-regexp-alist (list &optional add-auto-mode)
10596 "Convert extensions to regular expressions in the cars of LIST.
10597 Also, weed out any non-string entries, because the return value is used
10598 only for regexp matching.
10599 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
10600 point to the symbol `emacs', indicating that the file should
10601 be opened in Emacs."
10604 (mapcar (lambda (x)
10605 (if (not (stringp (car x)))
10607 (if (string-match "\\W" (car x))
10609 (cons (concat "\\." (car x) "\\'") (cdr x)))))
10612 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
10614 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
10615 (defun org-file-remote-p (file)
10616 "Test whether FILE specifies a location on a remote system.
10617 Return non-nil if the location is indeed remote.
10619 For example, the filename \"/user@host:/foo\" specifies a location
10620 on the system \"/user@host:\"."
10621 (cond ((fboundp 'file-remote-p)
10622 (file-remote-p file))
10623 ((fboundp 'tramp-handle-file-remote-p)
10624 (tramp-handle-file-remote-p file))
10625 ((and (boundp 'ange-ftp-name-format)
10626 (string-match (car ange-ftp-name-format) file))
10632 (defun org-get-org-file ()
10633 "Read a filename, with default directory `org-directory'."
10634 (let ((default (or org-default-notes-file remember-data-file)))
10635 (read-file-name (format "File name [%s]: " default)
10636 (file-name-as-directory org-directory)
10639 (defun org-notes-order-reversed-p ()
10640 "Check if the current file should receive notes in reversed order."
10642 ((not org-reverse-note-order) nil)
10643 ((eq t org-reverse-note-order) t)
10644 ((not (listp org-reverse-note-order)) nil)
10646 (let ((all org-reverse-note-order)
10648 (while (setq entry (pop all))
10649 (if (string-match (car entry) buffer-file-name)
10650 (throw 'exit (cdr entry))))
10653 (defvar org-refile-target-table nil
10654 "The list of refile targets, created by `org-refile'.")
10656 (defvar org-agenda-new-buffers nil
10657 "Buffers created to visit agenda files.")
10659 (defvar org-refile-cache nil
10660 "Cache for refile targets.")
10662 (defvar org-refile-markers nil
10663 "All the markers used for caching refile locations.")
10665 (defun org-refile-marker (pos)
10666 "Get a new refile marker, but only if caching is in use."
10667 (if (not org-refile-use-cache)
10669 (let ((m (make-marker)))
10670 (move-marker m pos)
10671 (push m org-refile-markers)
10674 (defun org-refile-cache-clear ()
10675 "Clear the refile cache and disable all the markers."
10676 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
10677 (setq org-refile-markers nil)
10678 (setq org-refile-cache nil)
10679 (message "Refile cache has been cleared"))
10681 (defun org-refile-cache-check-set (set)
10682 "Check if all the markers in the cache still have live buffers."
10685 (while (and set (setq marker (nth 3 (pop set))))
10686 ;; if org-refile-use-outline-path is 'file, marker may be nil
10687 (when (and marker (null (marker-buffer marker)))
10688 (message "not found") (sit-for 3)
10689 (throw 'exit nil)))
10692 (defun org-refile-cache-put (set &rest identifiers)
10693 "Push the refile targets SET into the cache, under IDENTIFIERS."
10694 (let* ((key (sha1 (prin1-to-string identifiers)))
10695 (entry (assoc key org-refile-cache)))
10698 (push (cons key set) org-refile-cache))))
10700 (defun org-refile-cache-get (&rest identifiers)
10701 "Retrieve the cached value for refile targets given by IDENTIFIERS."
10703 ((not org-refile-cache) nil)
10704 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
10706 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
10707 org-refile-cache))))
10708 (and set (org-refile-cache-check-set set) set)))))
10710 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
10711 "Produce a table with refile targets."
10712 (let ((case-fold-search nil)
10713 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
10714 (entries (or org-refile-targets '((nil . (:level . 1)))))
10715 targets tgs txt re files f desc descre fast-path-p level pos0)
10716 (message "Getting targets...")
10717 (with-current-buffer (or default-buffer (current-buffer))
10718 (while (setq entry (pop entries))
10719 (setq files (car entry) desc (cdr entry))
10720 (setq fast-path-p nil)
10722 ((null files) (setq files (list (current-buffer))))
10723 ((eq files 'org-agenda-files)
10724 (setq files (org-agenda-files 'unrestricted)))
10725 ((and (symbolp files) (fboundp files))
10726 (setq files (funcall files)))
10727 ((and (symbolp files) (boundp files))
10728 (setq files (symbol-value files))))
10729 (if (stringp files) (setq files (list files)))
10731 ((eq (car desc) :tag)
10732 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
10733 ((eq (car desc) :todo)
10734 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
10735 ((eq (car desc) :regexp)
10736 (setq descre (cdr desc)))
10737 ((eq (car desc) :level)
10738 (setq descre (concat "^\\*\\{" (number-to-string
10739 (if org-odd-levels-only
10740 (1- (* 2 (cdr desc)))
10743 ((eq (car desc) :maxlevel)
10744 (setq fast-path-p t)
10745 (setq descre (concat "^\\*\\{1," (number-to-string
10746 (if org-odd-levels-only
10747 (1- (* 2 (cdr desc)))
10750 (t (error "Bad refiling target description %s" desc)))
10751 (while (setq f (pop files))
10752 (with-current-buffer
10753 (if (bufferp f) f (org-get-agenda-file-buffer f))
10755 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
10757 (if (bufferp f) (setq f (buffer-file-name
10758 (buffer-base-buffer f))))
10759 (setq f (and f (expand-file-name f)))
10760 (if (eq org-refile-use-outline-path 'file)
10761 (push (list (file-name-nondirectory f) f nil nil) tgs))
10765 (goto-char (point-min))
10766 (while (re-search-forward descre nil t)
10767 (goto-char (setq pos0 (point-at-bol)))
10769 (when org-refile-target-verify-function
10771 (or (funcall org-refile-target-verify-function)
10773 (when (and (looking-at org-complex-heading-regexp)
10774 (not (member (match-string 4) excluded-entries))
10776 (setq level (org-reduced-level
10777 (- (match-end 1) (match-beginning 1)))
10778 txt (org-link-display-format (match-string 4))
10779 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
10780 re (format org-complex-heading-regexp-format
10781 (regexp-quote (match-string 4))))
10782 (when org-refile-use-outline-path
10783 (setq txt (mapconcat
10786 (if (eq org-refile-use-outline-path
10788 (list (file-name-nondirectory
10790 (buffer-base-buffer))))
10791 (if (eq org-refile-use-outline-path
10793 (list (buffer-file-name
10794 (buffer-base-buffer)))))
10795 (org-get-outline-path fast-path-p
10799 (push (list txt f re (org-refile-marker (point)))
10801 (when (= (point) pos0)
10802 ;; verification function has not moved point
10803 (goto-char (point-at-eol))))))))
10804 (when org-refile-use-cache
10805 (org-refile-cache-put tgs (buffer-file-name) descre))
10806 (setq targets (append tgs targets))
10808 (message "Getting targets...done")
10809 (nreverse targets)))
10811 (defun org-protect-slash (s)
10812 (while (string-match "/" s)
10813 (setq s (replace-match "\\" t t s)))
10816 (defvar org-olpa (make-vector 20 nil))
10818 (defun org-get-outline-path (&optional fastp level heading)
10819 "Return the outline path to the current entry, as a list.
10821 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
10822 routine which makes outline path derivations for an entire file,
10823 avoiding backtracing. Refile target collection makes use of that."
10827 (error "Outline path failure, more than 19 levels"))
10828 (loop for i from level upto 19 do
10829 (aset org-olpa i nil))
10831 (delq nil (append org-olpa nil))
10832 (aset org-olpa level heading)))
10833 (let (rtn case-fold-search)
10837 (while (org-up-heading-safe)
10838 (when (looking-at org-complex-heading-regexp)
10839 (push (org-match-string-no-properties 4) rtn)))
10842 (defun org-format-outline-path (path &optional width prefix)
10843 "Format the outline path PATH for display.
10844 Width is the maximum number of characters that is available.
10845 Prefix is a prefix to be included in the returned string,
10846 such as the file name."
10847 (setq width (or width 79))
10848 (if prefix (setq width (- width (length prefix))))
10851 (let* ((nsteps (length path))
10852 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
10853 (maxwidth (if (<= total-width width)
10854 10000 ;; everything fits
10855 ;; we need to shorten the level headings
10856 (/ (- width nsteps) nsteps)))
10857 (org-odd-levels-only nil)
10859 (total (1+ (length prefix))))
10860 (setq maxwidth (max maxwidth 10))
10865 (if (and (= n nsteps) (< maxwidth 10000))
10866 (setq maxwidth (- total-width total)))
10867 (if (< (length h) maxwidth)
10868 (progn (setq total (+ total (length h) 1)) h)
10869 (setq h (substring h 0 (- maxwidth 2))
10870 total (+ total maxwidth 1))
10871 (if (string-match "[ \t]+\\'" h)
10872 (setq h (substring h 0 (match-beginning 0))))
10873 (setq h (concat h "..")))
10874 (org-add-props h nil 'face
10875 (nth (% (1- n) org-n-level-faces)
10880 (defun org-display-outline-path (&optional file current)
10881 "Display the current outline path in the echo area."
10883 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
10884 (case-fold-search nil)
10885 (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
10886 (if current (setq path (append path
10888 (org-back-to-heading t)
10889 (if (looking-at org-complex-heading-regexp)
10890 (list (match-string 4)))))))
10892 (org-format-outline-path
10895 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
10897 (defvar org-refile-history nil
10898 "History for refiling operations.")
10900 (defvar org-after-refile-insert-hook nil
10901 "Hook run after `org-refile' has inserted its stuff at the new location.
10902 Note that this is still *before* the stuff will be removed from
10903 the *old* location.")
10905 (defvar org-capture-last-stored-marker)
10906 (defun org-refile (&optional goto default-buffer rfloc)
10907 "Move the entry or entries at point to another heading.
10908 The list of target headings is compiled using the information in
10909 `org-refile-targets', which see.
10911 At the target location, the entry is filed as a subitem of the target
10912 heading. Depending on `org-reverse-note-order', the new subitem will
10913 either be the first or the last subitem.
10915 If there is an active region, all entries in that region will be moved.
10916 However, the region must fulfill the requirement that the first heading
10917 is the first one sets the top-level of the moved text - at most siblings
10918 below it are allowed.
10920 With prefix arg GOTO, the command will only visit the target location
10921 and not actually move anything.
10923 With a double prefix arg \\[universal-argument] \\[universal-argument], \
10924 go to the location where the last refiling operation has put the subtree.
10925 With a prefix argument of `2', refile to the running clock.
10927 RFLOC can be a refile location obtained in a different way.
10929 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
10931 If you are using target caching (see `org-refile-use-cache'),
10932 you have to clear the target cache in order to find new targets.
10933 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
10934 prefix argument (`C-u C-u C-u C-c C-w')."
10937 (if (member goto '(0 (64)))
10938 (org-refile-cache-clear)
10939 (let* ((cbuf (current-buffer))
10940 (regionp (org-region-active-p))
10941 (region-start (and regionp (region-beginning)))
10942 (region-end (and regionp (region-end)))
10943 (region-length (and regionp (- region-end region-start)))
10944 (filename (buffer-file-name (buffer-base-buffer cbuf)))
10945 pos it nbuf file re level reversed)
10946 (setq last-command nil)
10948 (goto-char region-start)
10949 (or (bolp) (goto-char (point-at-bol)))
10950 (setq region-start (point))
10951 (unless (or (org-kill-is-subtree-p
10952 (buffer-substring region-start region-end))
10953 (prog1 org-refile-active-region-within-subtree
10954 (org-toggle-heading)))
10955 (error "The region is not a (sequence of) subtree(s)")))
10956 (if (equal goto '(16))
10957 (org-refile-goto-last-stored)
10959 (and (equal goto 2)
10960 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
10962 (setq it (list (or org-clock-heading "running clock")
10964 (marker-buffer org-clock-hd-marker))
10966 (marker-position org-clock-hd-marker)))
10969 (let (heading-text)
10972 (org-back-to-heading t)
10974 (nth 4 (org-heading-components))))
10975 (org-refile-get-location
10976 (cond (goto "Goto")
10977 (regionp "Refile region to")
10978 (t (concat "Refile subtree \""
10979 heading-text "\" to")))
10981 (and (not (equal '(4) goto))
10982 org-refile-allow-creating-parent-nodes)
10984 (setq file (nth 1 it)
10987 (if (and (not goto)
10989 (equal (buffer-file-name) file)
10991 (and (>= pos region-start)
10992 (<= pos region-end))
10993 (and (>= pos (point))
10994 (< pos (save-excursion
10995 (org-end-of-subtree t t))))))
10996 (error "Cannot refile to position inside the tree or region"))
10998 (setq nbuf (or (find-buffer-visiting file)
10999 (find-file-noselect file)))
11002 (org-pop-to-buffer-same-window nbuf)
11004 (org-show-context 'org-goto))
11007 (org-kill-new (buffer-substring region-start region-end))
11008 (org-save-markers-in-region region-start region-end))
11009 (org-copy-subtree 1 nil t))
11010 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
11011 (find-file-noselect file)))
11012 (setq reversed (org-notes-order-reversed-p))
11019 (looking-at org-outline-regexp)
11020 (setq level (org-get-valid-level (funcall outline-level) 1))
11023 (or (outline-next-heading) (point-max))
11024 (or (save-excursion (org-get-next-sibling))
11025 (org-end-of-subtree t t)
11029 (goto-char (point-max))
11030 (goto-char (point-min))
11031 (or (outline-next-heading) (goto-char (point-max)))))
11032 (if (not (bolp)) (newline))
11033 (org-paste-subtree level)
11034 (when org-log-refile
11035 (org-add-log-setup 'refile nil nil 'findpos
11037 (unless (eq org-log-refile 'note)
11038 (save-excursion (org-add-log-note))))
11039 (and org-auto-align-tags
11040 (let ((org-loop-over-headlines-in-active-region nil))
11041 (org-set-tags nil t)))
11042 (bookmark-set "org-refile-last-stored")
11043 ;; If we are refiling for capture, make sure that the
11044 ;; last-capture pointers point here
11045 (when (org-bound-and-true-p org-refile-for-capture)
11046 (bookmark-set "org-capture-last-stored-marker")
11047 (move-marker org-capture-last-stored-marker (point)))
11048 (if (fboundp 'deactivate-mark) (deactivate-mark))
11049 (run-hooks 'org-after-refile-insert-hook))))
11051 (delete-region (point) (+ (point) region-length))
11053 (when (featurep 'org-inlinetask)
11054 (org-inlinetask-remove-END-maybe))
11055 (setq org-markers-to-move nil)
11056 (message "Refiled to \"%s\" in file %s" (car it) file)))))))
11058 (defun org-refile-goto-last-stored ()
11059 "Go to the location where the last refile was stored."
11061 (bookmark-jump "org-refile-last-stored")
11062 (message "This is the location of the last refile"))
11064 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
11066 "Prompt the user for a refile location, using PROMPT.
11067 PROMPT should not be suffixed with a colon and a space, because
11068 this function appends the default value from
11069 `org-refile-history' automatically, if that is not empty.
11070 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
11071 this is used for the GOTO interface."
11072 (let ((org-refile-targets org-refile-targets)
11073 (org-refile-use-outline-path org-refile-use-outline-path)
11075 (when (and (derived-mode-p 'org-mode)
11076 (not org-refile-use-cache)
11080 (setq excluded-entries
11081 (append excluded-entries (list (org-get-heading t t)))))))
11082 (setq org-refile-target-table
11083 (org-refile-get-targets default-buffer excluded-entries)))
11084 (unless org-refile-target-table
11085 (error "No refile targets"))
11086 (let* ((prompt (concat prompt
11087 (and (car org-refile-history)
11088 (concat " (default " (car org-refile-history) ")"))
11090 (cbuf (current-buffer))
11091 (partial-completion-mode nil)
11092 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
11093 (cfunc (if (and org-refile-use-outline-path
11094 org-outline-path-complete-in-steps)
11095 'org-olpath-completing-read
11096 'org-icompleting-read))
11097 (extra (if org-refile-use-outline-path "/" ""))
11098 (filename (and cfn (expand-file-name cfn)))
11101 (if (and (not (member org-refile-use-outline-path
11102 '(file full-file-path)))
11103 (not (equal filename (nth 1 x))))
11104 (cons (concat (car x) extra " ("
11105 (file-name-nondirectory (nth 1 x)) ")")
11107 (cons (concat (car x) extra) (cdr x))))
11108 org-refile-target-table))
11109 (completion-ignore-case t)
11110 pa answ parent-target child parent old-hist)
11111 (setq old-hist org-refile-history)
11112 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
11113 nil 'org-refile-history (car org-refile-history)))
11114 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
11115 (org-refile-check-position pa)
11118 (when (or (not org-refile-history)
11119 (not (eq old-hist org-refile-history))
11120 (not (equal (car pa) (car org-refile-history))))
11121 (setq org-refile-history
11122 (cons (car pa) (if (assoc (car org-refile-history) tbl)
11124 (cdr org-refile-history))))
11125 (if (equal (car org-refile-history) (nth 1 org-refile-history))
11126 (pop org-refile-history)))
11128 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
11130 (setq parent (match-string 1 answ)
11131 child (match-string 2 answ))
11132 (setq parent-target (or (assoc parent tbl)
11133 (assoc (concat parent "/") tbl)))
11134 (when (and parent-target
11135 (or (eq new-nodes t)
11136 (and (eq new-nodes 'confirm)
11137 (y-or-n-p (format "Create new node \"%s\"? "
11139 (org-refile-new-child parent-target child)))
11140 (error "Invalid target location")))))
11142 (declare-function org-string-nw-p "org-macs.el" (s))
11143 (defun org-refile-check-position (refile-pointer)
11144 "Check if the refile pointer matches the readline to which it points."
11145 (let* ((file (nth 1 refile-pointer))
11146 (re (nth 2 refile-pointer))
11147 (pos (nth 3 refile-pointer))
11149 (when (org-string-nw-p re)
11150 (setq buffer (if (markerp pos)
11151 (marker-buffer pos)
11152 (or (find-buffer-visiting file)
11153 (find-file-noselect file))))
11154 (with-current-buffer buffer
11159 (beginning-of-line 1)
11160 (unless (org-looking-at-p re)
11161 (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
11163 (defun org-refile-new-child (parent-target child)
11164 "Use refile target PARENT-TARGET to add new CHILD below it."
11165 (unless parent-target
11166 (error "Cannot find parent for new node"))
11167 (let ((file (nth 1 parent-target))
11168 (pos (nth 3 parent-target))
11170 (with-current-buffer (or (find-buffer-visiting file)
11171 (find-file-noselect file))
11177 (goto-char (point-max))
11178 (if (not (bolp)) (newline)))
11179 (when (looking-at org-outline-regexp)
11180 (setq level (funcall outline-level))
11181 (org-end-of-subtree t t))
11182 (org-back-over-empty-lines)
11183 (insert "\n" (make-string
11184 (if pos (org-get-valid-level level 1) 1) ?*)
11186 (beginning-of-line 0)
11187 (list (concat (car parent-target) "/" child) file "" (point)))))))
11189 (defun org-olpath-completing-read (prompt collection &rest args)
11190 "Read an outline path like a file name."
11191 (let ((thetable collection)
11192 (org-completion-use-ido nil) ; does not work with ido.
11193 (org-completion-use-iswitchb nil)) ; or iswitchb
11195 'org-icompleting-read prompt
11196 (lambda (string predicate &optional flag)
11197 (let (rtn r f (l (length string)))
11201 (try-completion string thetable))
11204 (setq rtn (all-completions string thetable predicate))
11207 (setq r (substring x l))
11208 (if (string-match " ([^)]*)$" x)
11209 (setq f (match-string 0 x))
11211 (if (string-match "/" r)
11212 (concat string (substring r 0 (match-end 0)) f)
11217 (assoc string thetable)))))
11220 ;;;; Dynamic blocks
11222 (defun org-find-dblock (name)
11223 "Find the first dynamic block with name NAME in the buffer.
11224 If not found, stay at current position and return nil."
11225 (let ((case-fold-search t) pos)
11227 (goto-char (point-min))
11228 (setq pos (and (re-search-forward
11229 (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
11230 (match-beginning 0))))
11231 (if pos (goto-char pos))
11234 (defconst org-dblock-start-re
11235 "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
11236 "Matches the start line of a dynamic block, with parameters.")
11238 (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
11239 "Matches the end of a dynamic block.")
11241 (defun org-create-dblock (plist)
11242 "Create a dynamic block section, with parameters taken from PLIST.
11243 PLIST must contain a :name entry which is used as name of the block."
11244 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
11247 (let ((col (current-column))
11248 (name (plist-get plist :name)))
11249 (insert "#+BEGIN: " name)
11251 (if (eq (car plist) :name)
11252 (setq plist (cddr plist))
11253 (insert " " (prin1-to-string (pop plist)))))
11254 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
11255 (beginning-of-line -2)))
11257 (defun org-prepare-dblock ()
11258 "Prepare dynamic block for refresh.
11259 This empties the block, puts the cursor at the insert position and returns
11260 the property list including an extra property :name with the block name."
11261 (unless (looking-at org-dblock-start-re)
11262 (error "Not at a dynamic block"))
11263 (let* ((begdel (1+ (match-end 0)))
11264 (name (org-no-properties (match-string 1)))
11265 (params (append (list :name name)
11266 (read (concat "(" (match-string 3) ")")))))
11268 (beginning-of-line 1)
11269 (skip-chars-forward " \t")
11270 (setq params (plist-put params :indentation-column (current-column))))
11271 (unless (re-search-forward org-dblock-end-re nil t)
11272 (error "Dynamic block not terminated"))
11275 (list :content (buffer-substring
11276 begdel (match-beginning 0)))))
11277 (delete-region begdel (match-beginning 0))
11282 (defun org-map-dblocks (&optional command)
11283 "Apply COMMAND to all dynamic blocks in the current buffer.
11284 If COMMAND is not given, use `org-update-dblock'."
11285 (let ((cmd (or command 'org-update-dblock)))
11287 (goto-char (point-min))
11288 (while (re-search-forward org-dblock-start-re nil t)
11289 (goto-char (match-beginning 0))
11291 (condition-case nil
11293 (error (message "Error during update of dynamic block"))))
11294 (unless (re-search-forward org-dblock-end-re nil t)
11295 (error "Dynamic block not terminated"))))))
11297 (defun org-dblock-update (&optional arg)
11298 "User command for updating dynamic blocks.
11299 Update the dynamic block at point. With prefix ARG, update all dynamic
11300 blocks in the buffer."
11303 (org-update-all-dblocks)
11304 (or (looking-at org-dblock-start-re)
11305 (org-beginning-of-dblock))
11306 (org-update-dblock)))
11308 (defun org-update-dblock ()
11309 "Update the dynamic block at point.
11310 This means to empty the block, parse for parameters and then call
11311 the correct writing function."
11313 (save-window-excursion
11314 (let* ((pos (point))
11315 (line (org-current-line))
11316 (params (org-prepare-dblock))
11317 (name (plist-get params :name))
11318 (indent (plist-get params :indentation-column))
11319 (cmd (intern (concat "org-dblock-write:" name))))
11320 (message "Updating dynamic block `%s' at line %d..." name line)
11321 (funcall cmd params)
11322 (message "Updating dynamic block `%s' at line %d...done" name line)
11324 (when (and indent (> indent 0))
11325 (setq indent (make-string indent ?\ ))
11327 (org-beginning-of-dblock)
11329 (while (not (looking-at org-dblock-end-re))
11331 (beginning-of-line 2))
11332 (when (looking-at org-dblock-end-re)
11333 (and (looking-at "[ \t]+")
11334 (replace-match ""))
11335 (insert indent)))))))
11337 (defun org-beginning-of-dblock ()
11338 "Find the beginning of the dynamic block at point.
11339 Error if there is no such block at point."
11340 (let ((pos (point))
11343 (if (and (re-search-backward org-dblock-start-re nil t)
11344 (setq beg (match-beginning 0))
11345 (re-search-forward org-dblock-end-re nil t)
11346 (> (match-end 0) pos))
11349 (error "Not in a dynamic block"))))
11352 (defun org-update-all-dblocks ()
11353 "Update all dynamic blocks in the buffer.
11354 This function can be used in a hook."
11356 (when (derived-mode-p 'org-mode)
11357 (org-map-dblocks 'org-update-dblock)))
11362 (defconst org-additional-option-like-keywords
11363 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
11364 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
11365 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
11366 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
11368 "ORGTBL" "TBLFM:" "TBLNAME:"
11369 "BEGIN_EXAMPLE" "END_EXAMPLE"
11370 "BEGIN_VERBATIM" "END_VERBATIM"
11371 "BEGIN_QUOTE" "END_QUOTE"
11372 "BEGIN_VERSE" "END_VERSE"
11373 "BEGIN_CENTER" "END_CENTER"
11374 "BEGIN_SRC" "END_SRC"
11375 "BEGIN_RESULT" "END_RESULT"
11376 "BEGIN_lstlisting" "END_lstlisting"
11378 "HEADER:" "HEADERS:"
11379 "COLUMNS:" "PROPERTY:"
11380 "CAPTION:" "LABEL:"
11386 (defconst org-options-keywords
11387 '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
11388 "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
11389 "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
11390 "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
11391 "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
11392 "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:"
11393 "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
11395 (defconst org-additional-option-like-keywords-for-flyspell
11398 (mapconcat (lambda(k)
11399 (replace-regexp-in-string
11401 (concat k " " (downcase k) " " (upcase k))))
11402 (append org-options-keywords org-additional-option-like-keywords)
11406 (defcustom org-structure-template-alist
11408 ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
11409 "<src lang=\"?\">\n\n</src>")
11410 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
11411 "<example>\n?\n</example>")
11412 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
11413 "<quote>\n?\n</quote>")
11414 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
11415 "<verse>\n?\n</verse>")
11416 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
11417 "<center>\n?\n</center>")
11418 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
11419 "<literal style=\"latex\">\n?\n</literal>")
11421 "<literal style=\"latex\">?</literal>")
11422 ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
11423 "<literal style=\"html\">\n?\n</literal>")
11425 "<literal style=\"html\">?</literal>")
11426 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
11430 ("I" "#+INCLUDE %file ?"
11431 "<include file=%file markup=\"?\">")
11433 "Structure completion elements.
11434 This is a list of abbreviation keys and values. The value gets inserted
11435 if you type `<' followed by the key and then press the completion key,
11436 usually `M-TAB'. %file will be replaced by a file name after prompting
11437 for the file using completion. The cursor will be placed at the position
11438 of the `?` in the template.
11439 There are two templates for each key, the first uses the original Org syntax,
11440 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
11441 the default when the /org-mtags.el/ module has been loaded. See also the
11442 variable `org-mtags-prefer-muse-templates'."
11443 :group 'org-completion
11445 (string :tag "Key")
11446 (string :tag "Template")
11447 (string :tag "Muse Template")))
11449 (defun org-try-structure-completion ()
11450 "Try to complete a structure template before point.
11451 This looks for strings like \"<e\" on an otherwise empty line and
11453 (let ((l (buffer-substring (point-at-bol) (point)))
11455 (when (and (looking-at "[ \t]*$")
11456 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
11457 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
11458 (org-complete-expand-structure-template (+ -1 (point-at-bol)
11459 (match-beginning 1)) a)
11462 (defun org-complete-expand-structure-template (start cell)
11463 "Expand a structure template."
11464 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
11465 (rpl (nth (if musep 2 1) cell))
11467 (delete-region start (point))
11468 (when (string-match "\\`#\\+" rpl)
11471 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
11472 (setq ind (buffer-substring (point-at-bol) (point))))
11474 (setq start (point))
11475 (if (string-match "%file" rpl)
11476 (setq rpl (replace-match
11480 (abbreviate-file-name (read-file-name "Include file: ")))
11483 (setq rpl (mapconcat 'identity (split-string rpl "\n")
11484 (concat "\n" ind)))
11486 (if (re-search-backward "\\?" start t) (delete-char 1))))
11488 ;;;; TODO, DEADLINE, Comments
11490 (defun org-toggle-comment ()
11491 "Change the COMMENT state of an entry."
11494 (org-back-to-heading)
11495 (let (case-fold-search)
11497 ((looking-at (format org-heading-keyword-regexp-format
11498 org-comment-string))
11499 (goto-char (match-end 1))
11500 (looking-at (concat " +" org-comment-string))
11501 (replace-match "" t t)
11502 (when (eolp) (insert " ")))
11503 ((looking-at org-outline-regexp)
11504 (goto-char (match-end 0))
11505 (insert org-comment-string " "))))))
11507 (defvar org-last-todo-state-is-todo nil
11508 "This is non-nil when the last TODO state change led to a TODO state.
11509 If the last change removed the TODO tag or switched to DONE, then
11512 (defvar org-setting-tags nil) ; dynamically skipped
11514 (defvar org-todo-setup-filter-hook nil
11515 "Hook for functions that pre-filter todo specs.
11516 Each function takes a todo spec and returns either nil or the spec
11517 transformed into canonical form." )
11519 (defvar org-todo-get-default-hook nil
11520 "Hook for functions that get a default item for todo.
11521 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
11522 nil or a string to be used for the todo mark." )
11524 (defvar org-agenda-headline-snapshot-before-repeat)
11526 (defun org-current-effective-time ()
11527 "Return current time adjusted for `org-extend-today-until' variable."
11528 (let* ((ct (org-current-time))
11529 (dct (decode-time ct))
11531 (if (and org-use-effective-time
11532 (< (nth 2 dct) org-extend-today-until))
11533 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
11537 (defun org-todo-yesterday (&optional arg)
11538 "Like `org-todo' but the time of change will be 23:59 of yesterday."
11540 (if (eq major-mode 'org-agenda-mode)
11541 (apply 'org-agenda-todo-yesterday arg)
11542 (let* ((hour (third (decode-time
11543 (org-current-time))))
11544 (org-extend-today-until (1+ hour)))
11547 (defun org-todo (&optional arg)
11548 "Change the TODO state of an item.
11549 The state of an item is given by a keyword at the start of the heading,
11551 *** TODO Write paper
11554 The different keywords are specified in the variable `org-todo-keywords'.
11555 By default the available states are \"TODO\" and \"DONE\".
11556 So for this example: when the item starts with TODO, it is changed to DONE.
11557 When it starts with DONE, the DONE is removed. And when neither TODO nor
11558 DONE are present, add TODO at the beginning of the heading.
11560 With \\[universal-argument] prefix arg, use completion to determine the new \
11562 With numeric prefix arg, switch to that state.
11563 With a double \\[universal-argument] prefix, switch to the next set of TODO \
11564 keywords (nextset).
11565 With a triple \\[universal-argument] prefix, circumvent any state blocking.
11566 With a numeric prefix arg of 0, inhibit note taking for the change.
11568 For calling through lisp, arg is also interpreted in the following way:
11569 'none -> empty state
11570 \"\"(empty string) -> switch to empty state
11571 'done -> switch to DONE
11572 'nextset -> switch to the next set of keywords
11573 'previousset -> switch to the previous set of keywords
11574 \"WAITING\" -> switch to the specified keyword, but only if it
11575 really is a member of `org-todo-keywords'."
11577 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
11578 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
11579 'region-start-level 'region))
11580 org-loop-over-headlines-in-active-region)
11583 org-loop-over-headlines-in-active-region
11584 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
11585 (if (equal arg '(16)) (setq arg 'nextset))
11586 (let ((org-blocker-hook org-blocker-hook)
11587 (case-fold-search nil))
11588 (when (equal arg '(64))
11589 (setq arg nil org-blocker-hook nil))
11590 (when (and org-blocker-hook
11591 (or org-inhibit-blocking
11592 (org-entry-get nil "NOBLOCKING")))
11593 (setq org-blocker-hook nil))
11596 (org-back-to-heading t)
11597 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
11598 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
11599 (looking-at "\\(?: *\\|[ \t]*$\\)"))
11600 (let* ((match-data (match-data))
11601 (startpos (point-at-bol))
11602 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
11603 (org-log-done org-log-done)
11604 (org-log-repeat org-log-repeat)
11605 (org-todo-log-states org-todo-log-states)
11606 (org-inhibit-logging
11608 (progn (setq arg nil) 'note) org-inhibit-logging))
11609 (this (match-string 1))
11610 (hl-pos (match-beginning 0))
11611 (head (org-get-todo-sequence-head this))
11612 (ass (assoc head org-todo-kwd-alist))
11613 (interpret (nth 1 ass))
11614 (done-word (nth 3 ass))
11615 (final-done-word (nth 4 ass))
11616 (org-last-state (or this ""))
11617 (completion-ignore-case t)
11618 (member (member this org-todo-keywords-1))
11619 (tail (cdr member))
11621 ((and org-todo-key-trigger
11622 (or (and (equal arg '(4))
11623 (eq org-use-fast-todo-selection 'prefix))
11624 (and (not arg) org-use-fast-todo-selection
11625 (not (eq org-use-fast-todo-selection
11627 ;; Use fast selection
11628 (org-fast-todo-selection))
11629 ((and (equal arg '(4))
11630 (or (not org-use-fast-todo-selection)
11631 (not org-todo-key-trigger)))
11632 ;; Read a state with completion
11633 (org-icompleting-read
11634 "State: " (mapcar (lambda(x) (list x))
11635 org-todo-keywords-1)
11639 (if tail (car tail) nil)
11640 (car org-todo-keywords-1)))
11642 (if (equal member org-todo-keywords-1)
11645 (nth (- (length org-todo-keywords-1)
11647 org-todo-keywords-1)
11648 (org-last org-todo-keywords-1))))
11649 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
11650 (setq arg nil))) ; hack to fall back to cycling
11652 ;; user or caller requests a specific state
11654 ((equal arg "") nil)
11655 ((eq arg 'none) nil)
11656 ((eq arg 'done) (or done-word (car org-done-keywords)))
11658 (or (car (cdr (member head org-todo-heads)))
11659 (car org-todo-heads)))
11660 ((eq arg 'previousset)
11661 (let ((org-todo-heads (reverse org-todo-heads)))
11662 (or (car (cdr (member head org-todo-heads)))
11663 (car org-todo-heads))))
11664 ((car (member arg org-todo-keywords-1)))
11666 (error "State `%s' not valid in this file" arg))
11667 ((nth (1- (prefix-numeric-value arg))
11668 org-todo-keywords-1))))
11669 ((null member) (or head (car org-todo-keywords-1)))
11670 ((equal this final-done-word) nil) ;; -> make empty
11671 ((null tail) nil) ;; -> first entry
11672 ((memq interpret '(type priority))
11673 (if (eq this-command last-command)
11675 (if (> (length tail) 0)
11676 (or done-word (car org-done-keywords))
11681 (run-hook-with-args-until-success
11682 'org-todo-get-default-hook org-state org-last-state)
11684 (next (if org-state (concat " " org-state " ") " "))
11685 (change-plist (list :type 'todo-state-change :from this :to org-state
11686 :position startpos))
11688 (when org-blocker-hook
11689 (setq org-last-todo-state-is-todo
11690 (not (member this org-done-keywords)))
11691 (unless (save-excursion
11693 (org-with-wide-buffer
11694 (run-hook-with-args-until-failure
11695 'org-blocker-hook change-plist))))
11696 (if (org-called-interactively-p 'interactive)
11697 (error "TODO state change from %s to %s blocked" this org-state)
11699 (message "TODO state change from %s to %s blocked" this org-state)
11700 (throw 'exit nil))))
11701 (store-match-data match-data)
11702 (replace-match next t t)
11703 (unless (pos-visible-in-window-p hl-pos)
11704 (message "TODO state changed to %s" (org-trim next)))
11706 (setq head (org-get-todo-sequence-head org-state)
11707 ass (assoc head org-todo-kwd-alist)
11708 interpret (nth 1 ass)
11709 done-word (nth 3 ass)
11710 final-done-word (nth 4 ass)))
11711 (when (memq arg '(nextset previousset))
11712 (message "Keyword-Set %d/%d: %s"
11713 (- (length org-todo-sets) -1
11714 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
11715 (length org-todo-sets)
11716 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
11717 (setq org-last-todo-state-is-todo
11718 (not (member org-state org-done-keywords)))
11719 (setq now-done-p (and (member org-state org-done-keywords)
11720 (not (member this org-done-keywords))))
11721 (and logging (org-local-logging logging))
11722 (when (and (or org-todo-log-states org-log-done)
11723 (not (eq org-inhibit-logging t))
11724 (not (memq arg '(nextset previousset))))
11725 ;; we need to look at recording a time and note
11726 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
11727 (nth 2 (assoc this org-todo-log-states))))
11728 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
11729 (setq dolog 'time))
11730 (when (and org-state
11731 (member org-state org-not-done-keywords)
11732 (not (member this org-not-done-keywords)))
11733 ;; This is now a todo state and was not one before
11734 ;; If there was a CLOSED time stamp, get rid of it.
11735 (org-add-planning-info nil nil 'closed))
11736 (when (and now-done-p org-log-done)
11737 ;; It is now done, and it was not done before
11738 (org-add-planning-info 'closed (org-current-effective-time))
11739 (if (and (not dolog) (eq 'note org-log-done))
11740 (org-add-log-setup 'done org-state this 'findpos 'note)))
11741 (when (and org-state dolog)
11742 ;; This is a non-nil state, and we need to log it
11743 (org-add-log-setup 'state org-state this 'findpos dolog)))
11744 ;; Fixup tag positioning
11745 (org-todo-trigger-tag-changes org-state)
11746 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
11747 (when org-provide-todo-statistics
11748 (org-update-parent-todo-statistics))
11749 (run-hooks 'org-after-todo-state-change-hook)
11750 (if (and arg (not (member org-state org-done-keywords)))
11751 (setq head (org-get-todo-sequence-head org-state)))
11752 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
11753 ;; Do we need to trigger a repeat?
11755 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
11756 ;; This is for the agenda, take a snapshot of the headline.
11758 (setq org-agenda-headline-snapshot-before-repeat
11759 (org-get-heading))))
11760 (org-auto-repeat-maybe org-state))
11761 ;; Fixup cursor location if close to the keyword
11762 (if (and (outline-on-heading-p)
11764 (save-excursion (beginning-of-line 1)
11765 (looking-at org-todo-line-regexp))
11766 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
11768 (goto-char (or (match-end 2) (match-end 1)))
11769 (and (looking-at " ") (just-one-space))))
11770 (when org-trigger-hook
11772 (run-hook-with-args 'org-trigger-hook change-plist)))))))))
11774 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
11775 "Block turning an entry into a TODO, using the hierarchy.
11776 This checks whether the current task should be blocked from state
11777 changes. Such blocking occurs when:
11779 1. The task has children which are not all in a completed state.
11781 2. A task has a parent with the property :ORDERED:, and there
11782 are siblings prior to the current task with incomplete
11785 3. The parent of the task is blocked because it has siblings that should
11786 be done first, or is child of a block grandparent TODO entry."
11788 (if (not org-enforce-todo-dependencies)
11789 t ; if locally turned off don't block
11791 ;; If this is not a todo state change, or if this entry is already DONE,
11793 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11794 (member (plist-get change-plist :from)
11795 (cons 'done org-done-keywords))
11796 (member (plist-get change-plist :to)
11797 (cons 'todo org-not-done-keywords))
11798 (not (plist-get change-plist :to)))
11799 (throw 'dont-block t))
11800 ;; If this task has children, and any are undone, it's blocked
11802 (org-back-to-heading t)
11803 (let ((this-level (funcall outline-level)))
11804 (outline-next-heading)
11805 (let ((child-level (funcall outline-level)))
11806 (while (and (not (eobp))
11807 (> child-level this-level))
11808 ;; this todo has children, check whether they are all
11810 (if (and (not (org-entry-is-done-p))
11811 (org-entry-is-todo-p))
11812 (throw 'dont-block nil))
11813 (outline-next-heading)
11814 (setq child-level (funcall outline-level))))))
11815 ;; Otherwise, if the task's parent has the :ORDERED: property, and
11816 ;; any previous siblings are undone, it's blocked
11818 (org-back-to-heading t)
11819 (let* ((pos (point))
11820 (parent-pos (and (org-up-heading-safe) (point))))
11821 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11822 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11824 (re-search-forward org-not-done-heading-regexp pos t))
11825 (throw 'dont-block nil)) ; block, there is an older sibling not done.
11826 ;; Search further up the hierarchy, to see if an ancestor is blocked
11828 (goto-char parent-pos)
11829 (if (not (looking-at org-not-done-heading-regexp))
11830 (throw 'dont-block t)) ; do not block, parent is not a TODO
11832 (setq parent-pos (and (org-up-heading-safe) (point)))
11833 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11834 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11836 (re-search-forward org-not-done-heading-regexp pos t))
11837 (throw 'dont-block nil)))))))) ; block, older sibling not done.
11839 (defcustom org-track-ordered-property-with-tag nil
11840 "Should the ORDERED property also be shown as a tag?
11841 The ORDERED property decides if an entry should require subtasks to be
11842 completed in sequence. Since a property is not very visible, setting
11843 this option means that toggling the ORDERED property with the command
11844 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
11845 not relevant for the behavior, but it makes things more visible.
11847 Note that toggling the tag with tags commands will not change the property
11848 and therefore not influence behavior!
11850 This can be t, meaning the tag ORDERED should be used, It can also be a
11851 string to select a different tag for this task."
11854 (const :tag "No tracking" nil)
11855 (const :tag "Track with ORDERED tag" t)
11856 (string :tag "Use other tag")))
11858 (defun org-toggle-ordered-property ()
11859 "Toggle the ORDERED property of the current entry.
11860 For better visibility, you can track the value of this property with a tag.
11861 See variable `org-track-ordered-property-with-tag'."
11863 (let* ((t1 org-track-ordered-property-with-tag)
11864 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
11866 (org-back-to-heading)
11867 (if (org-entry-get nil "ORDERED")
11869 (org-delete-property "ORDERED")
11870 (and tag (org-toggle-tag tag 'off))
11871 (message "Subtasks can be completed in arbitrary order"))
11872 (org-entry-put nil "ORDERED" "t")
11873 (and tag (org-toggle-tag tag 'on))
11874 (message "Subtasks must be completed in sequence")))))
11876 (defvar org-blocked-by-checkboxes) ; dynamically scoped
11877 (defun org-block-todo-from-checkboxes (change-plist)
11878 "Block turning an entry into a TODO, using checkboxes.
11879 This checks whether the current task should be blocked from state
11880 changes because there are unchecked boxes in this entry."
11881 (if (not org-enforce-todo-checkbox-dependencies)
11882 t ; if locally turned off don't block
11884 ;; If this is not a todo state change, or if this entry is already DONE,
11886 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11887 (member (plist-get change-plist :from)
11888 (cons 'done org-done-keywords))
11889 (member (plist-get change-plist :to)
11890 (cons 'todo org-not-done-keywords))
11891 (not (plist-get change-plist :to)))
11892 (throw 'dont-block t))
11893 ;; If this task has checkboxes that are not checked, it's blocked
11895 (org-back-to-heading t)
11896 (let ((beg (point)) end)
11897 (outline-next-heading)
11900 (if (org-list-search-forward
11901 (concat (org-item-beginning-re)
11902 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
11906 (if (boundp 'org-blocked-by-checkboxes)
11907 (setq org-blocked-by-checkboxes t))
11908 (throw 'dont-block nil)))))
11909 t))) ; do not block
11911 (defun org-entry-blocked-p ()
11912 "Is the current entry blocked?"
11913 (if (org-entry-get nil "NOBLOCKING")
11914 nil ;; Never block this entry
11916 (run-hook-with-args-until-failure
11918 (list :type 'todo-state-change
11923 (defun org-update-statistics-cookies (all)
11924 "Update the statistics cookie, either from TODO or from checkboxes.
11925 This should be called with the cursor in a line with a statistics cookie."
11929 (org-update-checkbox-count 'all)
11930 (org-map-entries 'org-update-parent-todo-statistics))
11931 (if (not (org-at-heading-p))
11932 (org-update-checkbox-count)
11933 (let ((pos (move-marker (make-marker) (point)))
11935 (ignore-errors (org-back-to-heading t))
11936 (if (not (org-at-heading-p))
11937 (org-update-checkbox-count)
11938 (setq l1 (org-outline-level))
11939 (setq end (save-excursion
11940 (outline-next-heading)
11941 (if (org-at-heading-p) (setq l2 (org-outline-level)))
11943 (if (and (save-excursion
11945 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
11946 (not (save-excursion (re-search-forward
11947 ":COOKIE_DATA:.*\\<todo\\>" end t))))
11948 (org-update-checkbox-count)
11949 (if (and l2 (> l2 l1))
11952 (org-update-parent-todo-statistics))
11954 (beginning-of-line 1)
11955 (while (re-search-forward
11956 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
11958 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
11960 (move-marker pos nil)))))
11962 (defvar org-entry-property-inherited-from) ;; defined below
11963 (defun org-update-parent-todo-statistics ()
11964 "Update any statistics cookie in the parent of the current headline.
11965 When `org-hierarchical-todo-statistics' is nil, statistics will cover
11966 the entire subtree and this will travel up the hierarchy and update
11967 statistics everywhere."
11968 (let* ((prop (save-excursion (org-up-heading-safe)
11969 (org-entry-get nil "COOKIE_DATA" 'inherit)))
11970 (recursive (or (not org-hierarchical-todo-statistics)
11971 (and prop (string-match "\\<recursive\\>" prop))))
11972 (lim (or (and prop (marker-position org-entry-property-inherited-from))
11975 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
11976 level ltoggle l1 new ndel
11977 (cnt-all 0) (cnt-done 0) is-percent kwd
11978 checkbox-beg ov ovs ove cookie-present)
11981 (beginning-of-line 1)
11982 (setq ltoggle (funcall outline-level))
11983 ;; Three situations are to consider:
11985 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
11986 ;; to the top-level ancestor on the headline;
11988 ;; 2. If parent has "recursive" property, repeat up to the
11989 ;; headline setting that property, taking inheritance into
11992 ;; 3. Else, move up to direct parent and proceed only once.
11993 (while (and (setq level (org-up-heading-safe))
11994 (or recursive first)
11996 (setq first nil cookie-present nil)
12000 (downcase (or (org-entry-get nil "COOKIE_DATA")
12003 (while (re-search-forward box-re (point-at-eol) t)
12004 (setq cnt-all 0 cnt-done 0 cookie-present t)
12005 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
12007 (unless (outline-next-heading) (throw 'exit nil))
12008 (while (and (looking-at org-complex-heading-regexp)
12009 (> (setq l1 (length (match-string 1))) level))
12010 (setq kwd (and (or recursive (= l1 ltoggle))
12012 (if (or (eq org-provide-todo-statistics 'all-headlines)
12013 (and (listp org-provide-todo-statistics)
12014 (or (member kwd org-provide-todo-statistics)
12015 (member kwd org-done-keywords))))
12016 (setq cnt-all (1+ cnt-all))
12017 (if (eq org-provide-todo-statistics t)
12018 (and kwd (setq cnt-all (1+ cnt-all)))))
12019 (and (member kwd org-done-keywords)
12020 (setq cnt-done (1+ cnt-done)))
12021 (outline-next-heading)))
12024 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
12025 (format "[%d/%d]" cnt-done cnt-all))
12026 ndel (- (match-end 0) checkbox-beg))
12027 ;; handle overlays when updating cookie from column view
12028 (when (setq ov (car (overlays-at checkbox-beg)))
12029 (setq ovs (overlay-start ov) ove (overlay-end ov))
12030 (delete-overlay ov))
12031 (goto-char checkbox-beg)
12033 (delete-region (point) (+ (point) ndel))
12034 (when org-auto-align-tags (org-fix-tags-on-the-fly))
12035 (when ov (move-overlay ov ovs ove)))
12036 (when cookie-present
12037 (run-hook-with-args 'org-after-todo-statistics-hook
12038 cnt-done (- cnt-all cnt-done))))))
12039 (run-hooks 'org-todo-statistics-hook)))
12041 (defvar org-after-todo-statistics-hook nil
12042 "Hook that is called after a TODO statistics cookie has been updated.
12043 Each function is called with two arguments: the number of not-done entries
12044 and the number of done entries.
12046 For example, the following function, when added to this hook, will switch
12047 an entry to DONE when all children are done, and back to TODO when new
12048 entries are set to a TODO status. Note that this hook is only called
12049 when there is a statistics cookie in the headline!
12051 (defun org-summary-todo (n-done n-not-done)
12052 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
12053 (let (org-log-done org-log-states) ; turn off logging
12054 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
12057 (defvar org-todo-statistics-hook nil
12058 "Hook that is run whenever Org thinks TODO statistics should be updated.
12059 This hook runs even if there is no statistics cookie present, in which case
12060 `org-after-todo-statistics-hook' would not run.")
12062 (defun org-todo-trigger-tag-changes (state)
12063 "Apply the changes defined in `org-todo-state-tags-triggers'."
12064 (let ((l org-todo-state-tags-triggers)
12066 (when (or (not state) (equal state ""))
12067 (setq changes (append changes (cdr (assoc "" l)))))
12068 (when (and (stringp state) (> (length state) 0))
12069 (setq changes (append changes (cdr (assoc state l)))))
12070 (when (member state org-not-done-keywords)
12071 (setq changes (append changes (cdr (assoc 'todo l)))))
12072 (when (member state org-done-keywords)
12073 (setq changes (append changes (cdr (assoc 'done l)))))
12074 (dolist (c changes)
12075 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
12077 (defun org-local-logging (value)
12078 "Get logging settings from a property VALUE."
12080 ;; directly set the variables, they are already local.
12081 (setq org-log-done nil
12083 org-todo-log-states nil)
12084 (setq words (org-split-string value))
12085 (while (setq w (pop words))
12087 ((setq a (assoc w org-startup-options))
12088 (and (member (nth 1 a) '(org-log-done org-log-repeat))
12089 (set (nth 1 a) (nth 2 a))))
12090 ((setq a (org-extract-log-state-settings w))
12091 (and (member (car a) org-todo-keywords-1)
12092 (push a org-todo-log-states)))))))
12094 (defun org-get-todo-sequence-head (kwd)
12095 "Return the head of the TODO sequence to which KWD belongs.
12096 If KWD is not set, check if there is a text property remembering the
12101 (or (get-text-property (point-at-bol) 'org-todo-head)
12103 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
12104 nil (point-at-eol)))
12105 (get-text-property p 'org-todo-head))))
12106 ((not (member kwd org-todo-keywords-1))
12107 (car org-todo-keywords-1))
12108 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
12110 (defun org-fast-todo-selection ()
12111 "Fast TODO keyword selection with single keys.
12112 Returns the new TODO keyword, or nil if no state change should occur."
12113 (let* ((fulltable org-todo-key-alist)
12114 (done-keywords org-done-keywords) ;; needed for the faces.
12115 (maxlen (apply 'max (mapcar
12117 (if (stringp (car x)) (string-width (car x)) 0))
12120 (fwidth (+ maxlen 3 1 3))
12121 (ncol (/ (- (window-width) 4) fwidth))
12125 (save-window-excursion
12127 (set-buffer (get-buffer-create " *Org todo*"))
12128 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
12130 (org-set-local 'org-done-keywords done-keywords)
12131 (setq tbl fulltable cnt 0)
12132 (while (setq e (pop tbl))
12134 ((equal e '(:startgroup))
12135 (push '() groups) (setq ingroup t)
12136 (when (not (= cnt 0))
12140 ((equal e '(:endgroup))
12141 (setq ingroup nil cnt 0)
12143 ((equal e '(:newline))
12144 (when (not (= cnt 0))
12148 (while (equal (car tbl) '(:newline))
12150 (setq tbl (cdr tbl)))))
12152 (setq tg (car e) c (cdr e))
12153 (if ingroup (push tg (car groups)))
12154 (setq tg (org-add-props tg nil 'face
12155 (org-get-todo-face tg)))
12156 (if (and (= cnt 0) (not ingroup)) (insert " "))
12157 (insert "[" c "] " tg (make-string
12158 (- fwidth 4 (length tg)) ?\ ))
12159 (when (= (setq cnt (1+ cnt)) ncol)
12161 (if ingroup (insert " "))
12164 (goto-char (point-min))
12165 (if (not expert) (org-fit-window-to-buffer))
12166 (message "[a-z..]:Set [SPC]:clear")
12167 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
12170 (and (= c ?q) (not (rassoc c fulltable))))
12171 (setq quit-flag t))
12173 ((setq e (rassoc c fulltable) tg (car e))
12175 (t (setq quit-flag t)))))))
12177 (defun org-entry-is-todo-p ()
12178 (member (org-get-todo-state) org-not-done-keywords))
12180 (defun org-entry-is-done-p ()
12181 (member (org-get-todo-state) org-done-keywords))
12183 (defun org-get-todo-state ()
12185 (org-back-to-heading t)
12186 (and (looking-at org-todo-line-regexp)
12188 (match-string 2))))
12190 (defun org-at-date-range-p (&optional inactive-ok)
12191 "Is the cursor inside a date range?"
12195 (let ((pos (point)))
12196 (skip-chars-backward "^[<\r\n")
12197 (skip-chars-backward "<[")
12198 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12199 (>= (match-end 0) pos)
12201 (skip-chars-backward "^<[\r\n")
12202 (skip-chars-backward "<[")
12203 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12204 (>= (match-end 0) pos)
12208 (defun org-get-repeat (&optional tagline)
12209 "Check if there is a deadline/schedule with repeater in this entry."
12212 (org-back-to-heading t)
12213 (and (re-search-forward (if tagline
12214 (concat tagline "\\s-*" org-repeat-re)
12216 (org-entry-end-position) t)
12217 (match-string-no-properties 1)))))
12219 (defvar org-last-changed-timestamp)
12220 (defvar org-last-inserted-timestamp)
12221 (defvar org-log-post-message)
12222 (defvar org-log-note-purpose)
12223 (defvar org-log-note-how)
12224 (defvar org-log-note-extra)
12225 (defun org-auto-repeat-maybe (done-word)
12226 "Check if the current headline contains a repeated deadline/schedule.
12227 If yes, set TODO state back to what it was and change the base date
12228 of repeating deadline/scheduled time stamps to new date.
12229 This function is run automatically after each state change to a DONE state."
12230 ;; last-state is dynamically scoped into this function
12231 (let* ((repeat (org-get-repeat))
12232 (aa (assoc org-last-state org-todo-kwd-alist))
12233 (interpret (nth 1 aa))
12235 (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
12236 (msg "Entry repeats: ")
12238 (org-todo-log-states nil)
12239 re type n what ts time to-state)
12241 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
12242 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
12243 org-todo-repeat-to-state))
12244 (unless (and to-state (member to-state org-todo-keywords-1))
12245 (setq to-state (if (eq interpret 'type) org-last-state head)))
12246 (org-todo to-state)
12247 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
12248 (org-entry-put nil "LAST_REPEAT" (format-time-string
12249 (org-time-stamp-format t t))))
12250 (when org-log-repeat
12251 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
12252 (memq 'org-add-log-note post-command-hook))
12253 ;; OK, we are already setup for some record
12254 (if (eq org-log-repeat 'note)
12255 ;; make sure we take a note, not only a time stamp
12256 (setq org-log-note-how 'note))
12257 ;; Set up for taking a record
12258 (org-add-log-setup 'state (or done-word (car org-done-keywords))
12260 'findpos org-log-repeat)))
12261 (org-back-to-heading t)
12262 (org-add-planning-info nil nil 'closed)
12263 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12264 org-deadline-time-regexp "\\)\\|\\("
12265 org-ts-regexp "\\)"))
12266 (while (re-search-forward
12267 re (save-excursion (outline-next-heading) (point)) t)
12268 (setq type (if (match-end 1) org-scheduled-string
12269 (if (match-end 3) org-deadline-string "Plain:"))
12270 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
12271 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
12272 (setq n (string-to-number (match-string 2 ts))
12273 what (match-string 3 ts))
12274 (if (equal what "w") (setq n (* n 7) what "d"))
12275 (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
12276 (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
12277 ;; Preparation, see if we need to modify the start date for the change
12278 (when (match-end 1)
12279 (setq time (save-match-data (org-time-string-to-time ts)))
12281 ((equal (match-string 1 ts) ".")
12282 ;; Shift starting date to today
12283 (org-timestamp-change
12284 (- (org-today) (time-to-days time))
12286 ((equal (match-string 1 ts) "+")
12287 (let ((nshiftmax 10) (nshift 0))
12288 (while (or (= nshift 0)
12289 (<= (time-to-days time)
12290 (time-to-days (current-time))))
12291 (when (= (incf nshift) nshiftmax)
12292 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
12294 (org-timestamp-change n (cdr (assoc what whata)))
12295 (org-at-timestamp-p t)
12296 (setq ts (match-string 1))
12297 (setq time (save-match-data (org-time-string-to-time ts)))))
12298 (org-timestamp-change (- n) (cdr (assoc what whata)))
12299 ;; rematch, so that we have everything in place for the real shift
12300 (org-at-timestamp-p t)
12301 (setq ts (match-string 1))
12302 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
12303 (org-timestamp-change n (cdr (assoc what whata)))
12304 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
12305 (setq org-log-post-message msg)
12306 (message "%s" msg))))
12308 (defun org-show-todo-tree (arg)
12309 "Make a compact tree which shows all headlines marked with TODO.
12310 The tree will show the lines where the regexp matches, and all higher
12311 headlines above the match.
12312 With a \\[universal-argument] prefix, prompt for a regexp to match.
12313 With a numeric prefix N, construct a sparse tree for the Nth element
12314 of `org-todo-keywords-1'."
12316 (let ((case-fold-search nil)
12318 (cond ((null arg) org-not-done-regexp)
12320 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
12321 (mapcar 'list org-todo-keywords-1))))
12323 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12325 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12326 (regexp-quote (nth (1- (prefix-numeric-value arg))
12327 org-todo-keywords-1)))
12328 (t (error "Invalid prefix argument: %s" arg)))))
12329 (message "%d TODO entries found"
12330 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
12332 (defun org-deadline (&optional remove time)
12333 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
12334 With argument REMOVE, remove any deadline from the item.
12335 With argument TIME, set the deadline at the corresponding date. TIME
12336 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12338 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12339 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12340 'region-start-level 'region))
12341 org-loop-over-headlines-in-active-region)
12343 `(org-deadline ',remove ,time)
12344 org-loop-over-headlines-in-active-region
12345 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12346 (let* ((old-date (org-entry-get nil "DEADLINE"))
12347 (repeater (and old-date
12349 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12351 (match-string 1 old-date))))
12354 (when (and old-date org-log-redeadline)
12355 (org-add-log-setup 'deldeadline nil old-date 'findpos
12356 org-log-redeadline))
12357 (org-remove-timestamp-with-keyword org-deadline-string)
12358 (message "Item no longer has a deadline."))
12359 (org-add-planning-info 'deadline time 'closed)
12360 (when (and old-date org-log-redeadline
12361 (not (equal old-date
12362 (substring org-last-inserted-timestamp 1 -1))))
12363 (org-add-log-setup 'redeadline nil old-date 'findpos
12364 org-log-redeadline))
12367 (org-back-to-heading t)
12368 (when (re-search-forward (concat org-deadline-string " "
12369 org-last-inserted-timestamp)
12371 (outline-next-heading) (point)) t)
12372 (goto-char (1- (match-end 0)))
12373 (insert " " repeater)
12374 (setq org-last-inserted-timestamp
12375 (concat (substring org-last-inserted-timestamp 0 -1)
12377 (substring org-last-inserted-timestamp -1))))))
12378 (message "Deadline on %s" org-last-inserted-timestamp)))))
12380 (defun org-schedule (&optional remove time)
12381 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
12382 With argument REMOVE, remove any scheduling date from the item.
12383 With argument TIME, scheduled at the corresponding date. TIME can
12384 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12386 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12387 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12388 'region-start-level 'region))
12389 org-loop-over-headlines-in-active-region)
12391 `(org-schedule ',remove ,time)
12392 org-loop-over-headlines-in-active-region
12393 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12394 (let* ((old-date (org-entry-get nil "SCHEDULED"))
12395 (repeater (and old-date
12397 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12399 (match-string 1 old-date))))
12402 (when (and old-date org-log-reschedule)
12403 (org-add-log-setup 'delschedule nil old-date 'findpos
12404 org-log-reschedule))
12405 (org-remove-timestamp-with-keyword org-scheduled-string)
12406 (message "Item is no longer scheduled."))
12407 (org-add-planning-info 'scheduled time 'closed)
12408 (when (and old-date org-log-reschedule
12409 (not (equal old-date
12410 (substring org-last-inserted-timestamp 1 -1))))
12411 (org-add-log-setup 'reschedule nil old-date 'findpos
12412 org-log-reschedule))
12415 (org-back-to-heading t)
12416 (when (re-search-forward (concat org-scheduled-string " "
12417 org-last-inserted-timestamp)
12419 (outline-next-heading) (point)) t)
12420 (goto-char (1- (match-end 0)))
12421 (insert " " repeater)
12422 (setq org-last-inserted-timestamp
12423 (concat (substring org-last-inserted-timestamp 0 -1)
12425 (substring org-last-inserted-timestamp -1))))))
12426 (message "Scheduled to %s" org-last-inserted-timestamp)))))
12428 (defun org-get-scheduled-time (pom &optional inherit)
12429 "Get the scheduled time as a time tuple, of a format suitable
12430 for calling org-schedule with, or if there is no scheduling,
12432 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
12434 (apply 'encode-time (org-parse-time-string time)))))
12436 (defun org-get-deadline-time (pom &optional inherit)
12437 "Get the deadline as a time tuple, of a format suitable for
12438 calling org-deadline with, or if there is no scheduling, returns
12440 (let ((time (org-entry-get pom "DEADLINE" inherit)))
12442 (apply 'encode-time (org-parse-time-string time)))))
12444 (defun org-remove-timestamp-with-keyword (keyword)
12445 "Remove all time stamps with KEYWORD in the current entry."
12446 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
12449 (org-back-to-heading t)
12451 (outline-next-heading)
12452 (while (re-search-backward re beg t)
12454 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
12455 (equal (char-before) ?\ ))
12456 (backward-delete-char 1)
12457 (if (string-match "^[ \t]*$" (buffer-substring
12458 (point-at-bol) (point-at-eol)))
12459 (delete-region (point-at-bol)
12460 (min (point-max) (1+ (point-at-eol))))))))))
12462 (defun org-add-planning-info (what &optional time &rest remove)
12463 "Insert new timestamp with keyword in the line directly after the headline.
12464 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
12465 If non is given, the user is prompted for a date.
12466 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
12469 (let (org-time-was-given org-end-time-was-given ts
12470 end default-time default-input)
12473 (when (and (memq what '(scheduled deadline))
12475 (and (stringp time)
12476 (string-match "^[-+]+[0-9]" time))))
12477 ;; Try to get a default date/time from existing timestamp
12479 (org-back-to-heading t)
12480 (setq end (save-excursion (outline-next-heading) (point)))
12481 (when (re-search-forward (if (eq what 'scheduled)
12482 org-scheduled-time-regexp
12483 org-deadline-time-regexp)
12485 (setq ts (match-string 1)
12487 (apply 'encode-time (org-parse-time-string ts))
12488 default-input (and ts (org-get-compact-tod ts))))))
12492 ;; This is a string (relative or absolute), set proper date
12493 (apply 'encode-time
12494 (org-read-date-analyze
12495 time default-time (decode-time default-time)))
12496 ;; If necessary, get the time from the user
12497 (or time (org-read-date nil 'to-time nil nil
12498 default-time default-input)))))
12500 (when (and org-insert-labeled-timestamps-at-point
12501 (member what '(scheduled deadline)))
12503 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
12504 (org-insert-time-stamp time org-time-was-given
12505 nil nil nil (list org-end-time-was-given))
12509 (let (col list elt ts buffer-invisibility-spec)
12510 (org-back-to-heading t)
12511 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
12512 (goto-char (match-end 1))
12513 (setq col (current-column))
12514 (goto-char (match-end 0))
12515 (if (eobp) (insert "\n") (forward-char 1))
12516 (when (and (not what)
12519 org-keyword-time-not-clock-regexp))))
12520 ;; Nothing to add, nothing to remove...... :-)
12522 (if (and (not (looking-at org-outline-regexp))
12523 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
12525 (not (equal (match-string 1) org-clock-string)))
12526 (narrow-to-region (match-beginning 0) (match-end 0))
12527 (insert-before-markers "\n")
12529 (narrow-to-region (point) (point))
12530 (and org-adapt-indentation (org-indent-to-column col)))
12531 ;; Check if we have to remove something.
12532 (setq list (cons what remove))
12534 (setq elt (pop list))
12535 (when (or (and (eq elt 'scheduled)
12536 (re-search-forward org-scheduled-time-regexp nil t))
12537 (and (eq elt 'deadline)
12538 (re-search-forward org-deadline-time-regexp nil t))
12539 (and (eq elt 'closed)
12540 (re-search-forward org-closed-time-regexp nil t)))
12542 (if (looking-at "--+<[^>]+>") (replace-match ""))))
12543 (and (looking-at "[ \t]+") (replace-match ""))
12544 (and org-adapt-indentation (bolp) (org-indent-to-column col))
12547 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
12548 (cond ((eq what 'scheduled) org-scheduled-string)
12549 ((eq what 'deadline) org-deadline-string)
12550 ((eq what 'closed) org-closed-string))
12552 (setq ts (org-insert-time-stamp
12554 (or org-time-was-given
12555 (and (eq what 'closed) org-log-done-with-time))
12557 nil nil (list org-end-time-was-given)))
12559 (if (not (or (bolp) (eq (char-before) ?\ )
12560 (memq (char-after) '(32 10))
12563 (goto-char (point-min))
12565 (if (and (looking-at "[ \t]*\n")
12566 (equal (char-before) ?\n))
12567 (delete-region (1- (point)) (point-at-eol)))
12570 (defvar org-log-note-marker (make-marker))
12571 (defvar org-log-note-purpose nil)
12572 (defvar org-log-note-state nil)
12573 (defvar org-log-note-previous-state nil)
12574 (defvar org-log-note-how nil)
12575 (defvar org-log-note-extra nil)
12576 (defvar org-log-note-window-configuration nil)
12577 (defvar org-log-note-return-to (make-marker))
12578 (defvar org-log-note-effective-time nil
12579 "Remembered current time so that dynamically scoped
12580 `org-extend-today-until' affects tha timestamps in state change
12583 (defvar org-log-post-message nil
12584 "Message to be displayed after a log note has been stored.
12585 The auto-repeater uses this.")
12587 (defun org-add-note ()
12588 "Add a note to the current entry.
12589 This is done in the same way as adding a state change note."
12591 (org-add-log-setup 'note nil nil 'findpos nil))
12593 (defvar org-property-end-re)
12594 (defun org-add-log-setup (&optional purpose state prev-state
12596 "Set up the post command hook to take a note.
12597 If this is about to TODO state change, the new state is expected in STATE.
12598 When FINDPOS is non-nil, find the correct position for the note in
12599 the current entry. If not, assume that it can be inserted at point.
12600 HOW is an indicator what kind of note should be created.
12601 EXTRA is additional text that will be inserted into the notes buffer."
12602 (let* ((org-log-into-drawer (org-log-into-drawer))
12603 (drawer (cond ((stringp org-log-into-drawer)
12604 org-log-into-drawer)
12605 (org-log-into-drawer "LOGBOOK"))))
12609 (org-back-to-heading t)
12610 (narrow-to-region (point) (save-excursion
12611 (outline-next-heading) (point)))
12612 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
12613 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
12615 (goto-char (match-end 0))
12618 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
12621 (goto-char (match-end 0))
12622 (or org-log-states-order-reversed
12623 (and (re-search-forward org-property-end-re nil t)
12624 (goto-char (1- (match-beginning 0))))))
12625 (insert "\n:" drawer ":\n:END:")
12626 (beginning-of-line 0)
12628 (beginning-of-line 2)
12631 ((and org-log-state-notes-insert-after-drawers
12633 (forward-line) (looking-at org-drawer-regexp)))
12635 (while (looking-at org-drawer-regexp)
12636 (goto-char (match-end 0))
12637 (re-search-forward org-property-end-re (point-max) t)
12639 (forward-line -1)))
12640 (unless org-log-states-order-reversed
12641 (and (= (char-after) ?\n) (forward-char 1))
12642 (org-skip-over-state-notes)
12643 (skip-chars-backward " \t\n\r")))
12644 (move-marker org-log-note-marker (point))
12645 (setq org-log-note-purpose purpose
12646 org-log-note-state state
12647 org-log-note-previous-state prev-state
12648 org-log-note-how how
12649 org-log-note-extra extra
12650 org-log-note-effective-time (org-current-effective-time))
12651 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
12653 (defun org-skip-over-state-notes ()
12654 "Skip past the list of State notes in an entry."
12655 (if (looking-at "\n[ \t]*- State") (forward-char 1))
12656 (when (ignore-errors (goto-char (org-in-item-p)))
12657 (let* ((struct (org-list-struct))
12658 (prevs (org-list-prevs-alist struct)))
12659 (while (looking-at "[ \t]*- State")
12660 (goto-char (or (org-list-get-next-item (point) struct prevs)
12661 (org-list-get-item-end (point) struct)))))))
12663 (defun org-add-log-note (&optional purpose)
12664 "Pop up a window for taking a note, and add this note later at point."
12665 (remove-hook 'post-command-hook 'org-add-log-note)
12666 (setq org-log-note-window-configuration (current-window-configuration))
12667 (delete-other-windows)
12668 (move-marker org-log-note-return-to (point))
12669 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
12670 (goto-char org-log-note-marker)
12671 (org-switch-to-buffer-other-window "*Org Note*")
12673 (if (memq org-log-note-how '(time state))
12674 (let (current-prefix-arg) (org-store-log-note))
12675 (let ((org-inhibit-startup t)) (org-mode))
12676 (insert (format "# Insert note for %s.
12677 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
12679 ((eq org-log-note-purpose 'clock-out) "stopped clock")
12680 ((eq org-log-note-purpose 'done) "closed todo item")
12681 ((eq org-log-note-purpose 'state)
12682 (format "state change from \"%s\" to \"%s\""
12683 (or org-log-note-previous-state "")
12684 (or org-log-note-state "")))
12685 ((eq org-log-note-purpose 'reschedule)
12687 ((eq org-log-note-purpose 'delschedule)
12688 "no longer scheduled")
12689 ((eq org-log-note-purpose 'redeadline)
12690 "changing deadline")
12691 ((eq org-log-note-purpose 'deldeadline)
12692 "removing deadline")
12693 ((eq org-log-note-purpose 'refile)
12695 ((eq org-log-note-purpose 'note)
12697 (t (error "This should not happen")))))
12698 (if org-log-note-extra (insert org-log-note-extra))
12699 (org-set-local 'org-finish-function 'org-store-log-note)
12700 (run-hooks 'org-log-buffer-setup-hook)))
12702 (defvar org-note-abort nil) ; dynamically scoped
12703 (defun org-store-log-note ()
12704 "Finish taking a log note, and insert it to where it belongs."
12705 (let ((txt (buffer-string))
12706 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
12708 (kill-buffer (current-buffer))
12709 (while (string-match "\\`# .*\n[ \t\n]*" txt)
12710 (setq txt (replace-match "" t t txt)))
12711 (if (string-match "\\s-+\\'" txt)
12712 (setq txt (replace-match "" t t txt)))
12713 (setq lines (org-split-string txt "\n"))
12714 (when (and note (string-match "\\S-" note))
12716 (org-replace-escapes
12718 (list (cons "%u" (user-login-name))
12719 (cons "%U" user-full-name)
12720 (cons "%t" (format-time-string
12721 (org-time-stamp-format 'long 'inactive)
12722 org-log-note-effective-time))
12723 (cons "%T" (format-time-string
12724 (org-time-stamp-format 'long nil)
12725 org-log-note-effective-time))
12726 (cons "%d" (format-time-string
12727 (org-time-stamp-format nil 'inactive)
12728 org-log-note-effective-time))
12729 (cons "%D" (format-time-string
12730 (org-time-stamp-format nil nil)
12731 org-log-note-effective-time))
12732 (cons "%s" (if org-log-note-state
12733 (concat "\"" org-log-note-state "\"")
12735 (cons "%S" (if org-log-note-previous-state
12736 (concat "\"" org-log-note-previous-state "\"")
12738 (if lines (setq note (concat note " \\\\")))
12740 (when (or current-prefix-arg org-note-abort)
12741 (when org-log-into-drawer
12742 (org-remove-empty-drawer-at
12743 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
12744 org-log-note-marker))
12747 (with-current-buffer (marker-buffer org-log-note-marker)
12749 (goto-char org-log-note-marker)
12750 (move-marker org-log-note-marker nil)
12752 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
12753 (setq ind (save-excursion
12754 (if (ignore-errors (goto-char (org-in-item-p)))
12755 (let ((struct (org-list-struct)))
12757 (org-list-get-top-point struct) struct))
12758 (skip-chars-backward " \r\t\n")
12760 ((and (org-at-heading-p)
12761 org-adapt-indentation)
12762 (1+ (org-current-level)))
12763 ((org-at-heading-p) 0)
12764 (t (org-get-indentation))))))
12765 (setq bul (org-list-bullet-string "-"))
12766 (org-indent-line-to ind)
12767 (insert bul (pop lines))
12768 (let ((ind-body (+ (length bul) ind)))
12771 (org-indent-line-to ind-body)
12772 (insert (pop lines))))
12773 (message "Note stored")
12774 (org-back-to-heading t)
12775 (org-cycle-hide-drawers 'children)))))
12776 (set-window-configuration org-log-note-window-configuration)
12777 (with-current-buffer (marker-buffer org-log-note-return-to)
12778 (goto-char org-log-note-return-to))
12779 (move-marker org-log-note-return-to nil)
12780 (and org-log-post-message (message "%s" org-log-post-message)))
12782 (defun org-remove-empty-drawer-at (drawer pos)
12783 "Remove an empty drawer DRAWER at position POS.
12784 POS may also be a marker."
12785 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
12791 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
12792 (replace-match ""))))))
12794 (defvar org-ts-type nil)
12795 (defun org-sparse-tree (&optional arg type)
12796 "Create a sparse tree, prompt for the details.
12797 This command can create sparse trees. You first need to select the type
12798 of match used to create the tree:
12800 t Show all TODO entries.
12801 T Show entries with a specific TODO keyword.
12802 m Show entries selected by a tags/property match.
12803 p Enter a property name and its value (both with completion on existing
12804 names/values) and show entries with that property.
12805 r Show entries matching a regular expression (`/' can be used as well).
12806 b Show deadlines and scheduled items before a date.
12807 a Show deadlines and scheduled items after a date.
12808 d Show deadlines due within `org-deadline-warning-days'.
12809 D Show deadlines and scheduled items between a date range."
12811 (let (ans kwd value ts-type)
12812 (setq type (or type org-sparse-tree-default-date-type))
12813 (setq org-ts-type type)
12814 (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
12815 (cond ((eq type 'all) "all timestamps")
12816 ((eq type 'scheduled) "only scheduled")
12817 ((eq type 'deadline) "only deadline")
12818 ((eq type 'active) "only active timestamps")
12819 ((eq type 'inactive) "only inactive timestamps")
12820 ((eq type 'scheduled-or-deadline) "scheduled/deadline")
12821 (t "scheduled/deadline")))
12822 (setq ans (read-char-exclusive))
12825 (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
12827 (call-interactively 'org-check-deadlines))
12829 (call-interactively 'org-check-before-date))
12831 (call-interactively 'org-check-after-date))
12833 (call-interactively 'org-check-dates-range))
12835 (org-show-todo-tree nil))
12837 (org-show-todo-tree '(4)))
12838 ((member ans '(?T ?m))
12839 (call-interactively 'org-match-sparse-tree))
12840 ((member ans '(?p ?P))
12841 (setq kwd (org-icompleting-read "Property: "
12842 (mapcar 'list (org-buffer-property-keys))))
12843 (setq value (org-icompleting-read "Value: "
12844 (mapcar 'list (org-property-values kwd))))
12845 (unless (string-match "\\`{.*}\\'" value)
12846 (setq value (concat "\"" value "\"")))
12847 (org-match-sparse-tree arg (concat kwd "=" value)))
12848 ((member ans '(?r ?R ?/))
12849 (call-interactively 'org-occur))
12850 (t (error "No such sparse tree command \"%c\"" ans)))))
12852 (defvar org-occur-highlights nil
12853 "List of overlays used for occur matches.")
12854 (make-variable-buffer-local 'org-occur-highlights)
12855 (defvar org-occur-parameters nil
12856 "Parameters of the active org-occur calls.
12857 This is a list, each call to org-occur pushes as cons cell,
12858 containing the regular expression and the callback, onto the list.
12859 The list can contain several entries if `org-occur' has been called
12860 several time with the KEEP-PREVIOUS argument. Otherwise, this list
12861 will only contain one set of parameters. When the highlights are
12862 removed (for example with `C-c C-c', or with the next edit (depending
12863 on `org-remove-highlights-with-change'), this variable is emptied
12865 (make-variable-buffer-local 'org-occur-parameters)
12867 (defun org-occur (regexp &optional keep-previous callback)
12868 "Make a compact tree which shows all matches of REGEXP.
12869 The tree will show the lines where the regexp matches, and all higher
12870 headlines above the match. It will also show the heading after the match,
12871 to make sure editing the matching entry is easy.
12872 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
12873 call to `org-occur' will be kept, to allow stacking of calls to this
12875 If CALLBACK is non-nil, it is a function which is called to confirm
12876 that the match should indeed be shown."
12877 (interactive "sRegexp: \nP")
12878 (when (equal regexp "")
12879 (error "Regexp cannot be empty"))
12880 (unless keep-previous
12881 (org-remove-occur-highlights nil nil t))
12882 (push (cons regexp callback) org-occur-parameters)
12885 (goto-char (point-min))
12886 (if (or (not keep-previous) ; do not want to keep
12887 (not org-occur-highlights)) ; no previous matches
12890 (while (re-search-forward regexp nil t)
12891 (when (or (not callback)
12892 (save-match-data (funcall callback)))
12893 (setq cnt (1+ cnt))
12894 (when org-highlight-sparse-tree-matches
12895 (org-highlight-new-match (match-beginning 0) (match-end 0)))
12896 (org-show-context 'occur-tree))))
12897 (when org-remove-highlights-with-change
12898 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
12900 (unless org-sparse-tree-open-archived-trees
12901 (org-hide-archived-subtrees (point-min) (point-max)))
12902 (run-hooks 'org-occur-hook)
12903 (if (org-called-interactively-p 'interactive)
12904 (message "%d match(es) for regexp %s" cnt regexp))
12907 (defun org-occur-next-match (&optional n reset)
12908 "Function for `next-error-function' to find sparse tree matches.
12909 N is the number of matches to move, when negative move backwards.
12910 RESET is entirely ignored - this function always goes back to the
12911 starting point when no match is found."
12912 (let* ((limit (if (< n 0) (point-min) (point-max)))
12913 (search-func (if (< n 0)
12914 'previous-single-char-property-change
12915 'next-single-char-property-change))
12920 (while (setq p1 (funcall search-func (point) 'org-type))
12921 (when (equal p1 limit)
12923 (error "No more matches"))
12924 (when (equal (get-char-property p1 'org-type) 'org-occur)
12928 (throw 'exit (point))))
12931 (error "No more matches"))))
12933 (defun org-show-context (&optional key)
12934 "Make sure point and context are visible.
12935 How much context is shown depends upon the variables
12936 `org-show-hierarchy-above', `org-show-following-heading',
12937 `org-show-entry-below' and `org-show-siblings'."
12938 (let ((heading-p (org-at-heading-p t))
12939 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
12940 (following-p (org-get-alist-option org-show-following-heading key))
12941 (entry-p (org-get-alist-option org-show-entry-below key))
12942 (siblings-p (org-get-alist-option org-show-siblings key)))
12944 ;; Show heading or entry text
12945 (if (and heading-p (not entry-p))
12946 (org-flag-heading nil) ; only show the heading
12947 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
12948 (org-show-hidden-entry))) ; show entire entry
12950 ;; Show next sibling, or heading below text
12952 (and (if heading-p (org-goto-sibling) (outline-next-heading))
12953 (org-flag-heading nil))))
12954 (when siblings-p (org-show-siblings))
12956 ;; show all higher headings, possibly with siblings
12958 (while (and (condition-case nil
12959 (progn (org-up-heading-all 1) t)
12962 (org-flag-heading nil)
12963 (when siblings-p (org-show-siblings))))))))
12965 (defvar org-reveal-start-hook nil
12966 "Hook run before revealing a location.")
12968 (defun org-reveal (&optional siblings)
12969 "Show current entry, hierarchy above it, and the following headline.
12970 This can be used to show a consistent set of context around locations
12971 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
12972 not t for the search context.
12974 With optional argument SIBLINGS, on each level of the hierarchy all
12975 siblings are shown. This repairs the tree structure to what it would
12976 look like when opened with hierarchical calls to `org-cycle'.
12977 With double optional argument \\[universal-argument] \\[universal-argument], \
12978 go to the parent and show the
12981 (run-hooks 'org-reveal-start-hook)
12982 (let ((org-show-hierarchy-above t)
12983 (org-show-following-heading t)
12984 (org-show-siblings (if siblings t org-show-siblings)))
12985 (org-show-context nil))
12986 (when (equal siblings '(16))
12988 (when (org-up-heading-safe)
12990 (run-hook-with-args 'org-cycle-hook 'subtree)))))
12992 (defun org-highlight-new-match (beg end)
12993 "Highlight from BEG to END and mark the highlight is an occur headline."
12994 (let ((ov (make-overlay beg end)))
12995 (overlay-put ov 'face 'secondary-selection)
12996 (overlay-put ov 'org-type 'org-occur)
12997 (push ov org-occur-highlights)))
12999 (defun org-remove-occur-highlights (&optional beg end noremove)
13000 "Remove the occur highlights from the buffer.
13001 BEG and END are ignored. If NOREMOVE is nil, remove this function
13002 from the `before-change-functions' in the current buffer."
13004 (unless org-inhibit-highlight-removal
13005 (mapc 'delete-overlay org-occur-highlights)
13006 (setq org-occur-highlights nil)
13007 (setq org-occur-parameters nil)
13009 (remove-hook 'before-change-functions
13010 'org-remove-occur-highlights 'local))))
13014 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
13015 "Regular expression matching the priority indicator.")
13017 (defvar org-remove-priority-next-time nil)
13019 (defun org-priority-up ()
13020 "Increase the priority of the current item."
13022 (org-priority 'up))
13024 (defun org-priority-down ()
13025 "Decrease the priority of the current item."
13027 (org-priority 'down))
13029 (defun org-priority (&optional action)
13030 "Change the priority of an item.
13031 ACTION can be `set', `up', `down', or a character."
13033 (unless org-enable-priority-commands
13034 (error "Priority commands are disabled"))
13035 (setq action (or action 'set))
13036 (let (current new news have remove)
13038 (org-back-to-heading t)
13039 (if (looking-at org-priority-regexp)
13040 (setq current (string-to-char (match-string 2))
13043 ((eq action 'remove)
13044 (setq remove t new ?\ ))
13045 ((or (eq action 'set)
13046 (if (featurep 'xemacs) (characterp action) (integerp action)))
13047 (if (not (eq action 'set))
13049 (message "Priority %c-%c, SPC to remove: "
13050 org-highest-priority org-lowest-priority)
13052 (setq new (read-char-exclusive))))
13053 (if (and (= (upcase org-highest-priority) org-highest-priority)
13054 (= (upcase org-lowest-priority) org-lowest-priority))
13055 (setq new (upcase new)))
13056 (cond ((equal new ?\ ) (setq remove t))
13057 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
13058 (error "Priority must be between `%c' and `%c'"
13059 org-highest-priority org-lowest-priority))))
13062 (1- current) ; normal cycling
13063 ;; last priority was empty
13064 (if (eq last-command this-command)
13065 org-lowest-priority ; wrap around empty to lowest
13067 (if org-priority-start-cycle-with-default
13068 org-default-priority
13069 (1- org-default-priority))))))
13072 (1+ current) ; normal cycling
13073 ;; last priority was empty
13074 (if (eq last-command this-command)
13075 org-highest-priority ; wrap around empty to highest
13077 (if org-priority-start-cycle-with-default
13078 org-default-priority
13079 (1+ org-default-priority))))))
13080 (t (error "Invalid action")))
13081 (if (or (< (upcase new) org-highest-priority)
13082 (> (upcase new) org-lowest-priority))
13083 (if (and (memq action '(up down))
13084 (not have) (not (eq last-command this-command)))
13085 ;; `new' is from default priority
13087 "The default can not be set, see `org-default-priority' why")
13088 ;; normal cycling: `new' is beyond highest/lowest priority
13089 ;; and is wrapped around to the empty priority
13091 (setq news (format "%c" new))
13094 (replace-match "" t t nil 1)
13095 (replace-match news t t nil 2))
13097 (error "No priority cookie found in line")
13098 (let ((case-fold-search nil))
13099 (looking-at org-todo-line-regexp))
13102 (goto-char (match-end 2))
13103 (insert " [#" news "]"))
13104 (goto-char (match-beginning 3))
13105 (insert "[#" news "] "))))
13106 (org-preserve-lc (org-set-tags nil 'align)))
13108 (message "Priority removed")
13109 (message "Priority of current item set to %s" news))))
13111 (defun org-get-priority (s)
13112 "Find priority cookie and return priority."
13113 (if (functionp org-get-priority-function)
13114 (funcall org-get-priority-function)
13116 (if (not (string-match org-priority-regexp s))
13117 (* 1000 (- org-lowest-priority org-default-priority))
13118 (* 1000 (- org-lowest-priority
13119 (string-to-char (match-string 2 s))))))))
13123 (defvar org-agenda-archives-mode)
13124 (defvar org-map-continue-from nil
13125 "Position from where mapping should continue.
13126 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
13128 (defvar org-scanner-tags nil
13129 "The current tag list while the tags scanner is running.")
13130 (defvar org-trust-scanner-tags nil
13131 "Should `org-get-tags-at' use the tags for the scanner.
13132 This is for internal dynamical scoping only.
13133 When this is non-nil, the function `org-get-tags-at' will return the value
13134 of `org-scanner-tags' instead of building the list by itself. This
13135 can lead to large speed-ups when the tags scanner is used in a file with
13136 many entries, and when the list of tags is retrieved, for example to
13137 obtain a list of properties. Building the tags list for each entry in such
13138 a file becomes an N^2 operation - but with this variable set, it scales
13141 (defun org-scan-tags (action matcher todo-only &optional start-level)
13142 "Scan headline tags with inheritance and produce output ACTION.
13144 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
13145 or `agenda' to produce an entry list for an agenda view. It can also be
13146 a Lisp form or a function that should be called at each matched headline, in
13147 this case the return value is a list of all return values from these calls.
13149 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
13150 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
13151 only lines with a not-done TODO keyword are included in the output.
13152 This should be the same variable that was scoped into
13153 and set by `org-make-tags-matcher' when it constructed MATCHER.
13155 START-LEVEL can be a string with asterisks, reducing the scope to
13156 headlines matching this string."
13157 (require 'org-agenda)
13158 (let* ((re (concat "^"
13160 ;; Get the correct level to match
13161 (concat "\\*\\{" (number-to-string start-level) "\\} ")
13162 org-outline-regexp)
13164 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
13165 (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
13166 (props (list 'face 'default
13167 'done-face 'org-agenda-done
13168 'undone-face 'default
13169 'mouse-face 'highlight
13170 'org-not-done-regexp org-not-done-regexp
13171 'org-todo-regexp org-todo-regexp
13172 'org-complex-heading-regexp org-complex-heading-regexp
13174 (format "mouse-2 or RET jump to org file %s"
13175 (abbreviate-file-name
13176 (or (buffer-file-name (buffer-base-buffer))
13177 (buffer-name (buffer-base-buffer)))))))
13178 (case-fold-search nil)
13179 (org-map-continue-from nil)
13180 lspos tags tags-list
13181 (tags-alist (list (cons 0 org-file-tags)))
13182 (llast 0) rtn rtn1 level category i txt
13183 todo marker entry priority)
13184 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
13185 (setq action (list 'lambda nil action)))
13187 (goto-char (point-min))
13188 (when (eq action 'sparse-tree)
13190 (org-remove-occur-highlights))
13191 (while (re-search-forward re nil t)
13192 (setq org-map-continue-from nil)
13194 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
13195 tags (if (match-end 4) (org-match-string-no-properties 4)))
13196 (goto-char (setq lspos (match-beginning 0)))
13197 (setq level (org-reduced-level (funcall outline-level))
13198 category (org-get-category))
13199 (setq i llast llast level)
13200 ;; remove tag lists from same and sublevels
13201 (while (>= i level)
13202 (when (setq entry (assoc i tags-alist))
13203 (setq tags-alist (delete entry tags-alist)))
13205 ;; add the next tags
13207 (setq tags (org-split-string tags ":")
13209 (cons (cons level tags) tags-alist)))
13210 ;; compile tags for current headline
13212 (if org-use-tag-inheritance
13213 (apply 'append (mapcar 'cdr (reverse tags-alist)))
13215 org-scanner-tags tags-list)
13216 (when org-use-tag-inheritance
13217 (setcdr (car tags-alist)
13218 (mapcar (lambda (x)
13219 (setq x (copy-sequence x))
13220 (org-add-prop-inherited x))
13221 (cdar tags-alist))))
13222 (when (and tags org-use-tag-inheritance
13223 (or (not (eq t org-use-tag-inheritance))
13224 org-tags-exclude-from-inheritance))
13225 ;; selective inheritance, remove uninherited ones
13226 (setcdr (car tags-alist)
13227 (org-remove-uninherited-tags (cdar tags-alist))))
13230 ;; eval matcher only when the todo condition is OK
13231 (and (or (not todo-only) (member todo org-not-done-keywords))
13232 (let ((case-fold-search t) (org-trust-scanner-tags t))
13235 ;; Call the skipper, but return t if it does not skip,
13236 ;; so that the `and' form continues evaluating
13238 (unless (eq action 'sparse-tree) (org-agenda-skip))
13241 ;; Check if timestamps are deselecting this entry
13242 (or (not todo-only)
13243 (and (member todo org-not-done-keywords)
13244 (or (not org-agenda-tags-todo-honor-ignore-options)
13245 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
13247 ;; Extra check for the archive tag
13248 ;; FIXME: Does the skipper already do this????
13250 (not (member org-archive-tag tags-list))
13251 ;; we have an archive tag, should we use this anyway?
13252 (or (not org-agenda-skip-archived-trees)
13253 (and (eq action 'agenda) org-agenda-archives-mode))))
13255 ;; select this headline
13258 ((eq action 'sparse-tree)
13259 (and org-highlight-sparse-tree-matches
13260 (org-get-heading) (match-end 0)
13261 (org-highlight-new-match
13262 (match-beginning 1) (match-end 1)))
13263 (org-show-context 'tags-tree))
13264 ((eq action 'agenda)
13265 (setq txt (org-agenda-format-item
13268 (if (eq org-tags-match-list-sublevels 'indented)
13269 (make-string (1- level) ?.) "")
13273 priority (org-get-priority txt))
13275 (setq marker (org-agenda-new-marker))
13276 (org-add-props txt props
13277 'org-marker marker 'org-hd-marker marker 'org-category category
13279 'priority priority 'type "tagsmatch")
13281 ((functionp action)
13282 (setq org-map-continue-from nil)
13284 (setq rtn1 (funcall action))
13286 (t (error "Invalid action")))
13288 ;; if we are to skip sublevels, jump to end of subtree
13289 (unless org-tags-match-list-sublevels
13290 (org-end-of-subtree t)
13291 (backward-char 1))))
13292 ;; Get the correct position from where to continue
13293 (if org-map-continue-from
13294 (goto-char org-map-continue-from)
13295 (and (= (point) lspos) (end-of-line 1)))))
13296 (when (and (eq action 'sparse-tree)
13297 (not org-sparse-tree-open-archived-trees))
13298 (org-hide-archived-subtrees (point-min) (point-max)))
13301 (defun org-remove-uninherited-tags (tags)
13302 "Remove all tags that are not inherited from the list TAGS."
13304 ((eq org-use-tag-inheritance t)
13305 (if org-tags-exclude-from-inheritance
13306 (org-delete-all org-tags-exclude-from-inheritance tags)
13308 ((not org-use-tag-inheritance) nil)
13309 ((stringp org-use-tag-inheritance)
13312 (if (and (string-match org-use-tag-inheritance x)
13313 (not (member x org-tags-exclude-from-inheritance)))
13316 ((listp org-use-tag-inheritance)
13319 (if (member x org-use-tag-inheritance) x nil))
13322 (defun org-match-sparse-tree (&optional todo-only match)
13323 "Create a sparse tree according to tags string MATCH.
13324 MATCH can contain positive and negative selection of tags, like
13325 \"+WORK+URGENT-WITHBOSS\".
13326 If optional argument TODO-ONLY is non-nil, only select lines that are
13329 (org-prepare-agenda-buffers (list (current-buffer)))
13330 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
13332 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
13334 (defvar org-cached-props nil)
13335 (defun org-cached-entry-get (pom property)
13336 (if (or (eq t org-use-property-inheritance)
13337 (and (stringp org-use-property-inheritance)
13338 (string-match org-use-property-inheritance property))
13339 (and (listp org-use-property-inheritance)
13340 (member property org-use-property-inheritance)))
13341 ;; Caching is not possible, check it directly
13342 (org-entry-get pom property 'inherit)
13343 ;; Get all properties, so that we can do complicated checks easily
13344 (cdr (assoc property (or org-cached-props
13345 (setq org-cached-props
13346 (org-entry-properties pom)))))))
13348 (defun org-global-tags-completion-table (&optional files)
13349 "Return the list of all tags in all agenda buffer/files.
13350 Optional FILES argument is a list of files which can be used
13351 instead of the agenda files."
13358 (set-buffer (find-file-noselect file))
13359 (append (org-get-buffer-tags)
13360 (mapcar (lambda (x) (if (stringp (car-safe x))
13361 (list (car-safe x)) nil))
13363 (if (and files (car files))
13365 (org-agenda-files))))))))
13367 (defun org-make-tags-matcher (match)
13368 "Create the TAGS/TODO matcher form for the selection string MATCH.
13370 The variable `todo-only' is scoped dynamically into this function.
13371 It will be set to t if the matcher restricts matching to TODO entries,
13372 otherwise will not be touched.
13374 Returns a cons of the selection string MATCH and the constructed
13375 lisp form implementing the matcher. The matcher is to be evaluated
13376 at an Org entry, with point on the headline, and returns t if the
13377 entry matches the selection string MATCH. The returned lisp form
13378 references two variables with information about the entry, which
13379 must be bound around the form's evaluation: todo, the TODO keyword
13380 at the entry (or nil of none); and tags-list, the list of all tags
13381 at the entry including inherited ones. Additionally, the category
13382 of the entry (if any) must be specified as the text property
13383 'org-category on the headline.
13385 See also `org-scan-tags'.
13387 (declare (special todo-only))
13388 (unless (boundp 'todo-only)
13389 (error "org-make-tags-matcher expects todo-only to be scoped in"))
13391 ;; Get a new match request, with completion
13392 (let ((org-last-tags-completion-table
13393 (org-global-tags-completion-table)))
13394 (setq match (org-completing-read-no-i
13395 "Match: " 'org-tags-completion-function nil nil nil
13396 'org-tags-history))))
13398 ;; Parse the string and create a lisp form
13399 (let ((match0 match)
13400 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
13402 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
13403 orterms term orlist re-p str-p level-p level-op time-p
13404 prop-p pn pv po gv rest)
13405 (if (string-match "/+" match)
13406 ;; match contains also a todo-matching request
13408 (setq tagsmatch (substring match 0 (match-beginning 0))
13409 todomatch (substring match (match-end 0)))
13410 (if (string-match "^!" todomatch)
13411 (setq todo-only t todomatch (substring todomatch 1)))
13412 (if (string-match "^\\s-*$" todomatch)
13413 (setq todomatch nil)))
13414 ;; only matching tags
13415 (setq tagsmatch match todomatch nil))
13417 ;; Make the tags matcher
13418 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
13419 (setq tagsmatcher t)
13420 (setq orterms (org-split-string tagsmatch "|") orlist nil)
13421 (while (setq term (pop orterms))
13422 (while (and (equal (substring term -1) "\\") orterms)
13423 (setq term (concat term "|" (pop orterms)))) ; repair bad split
13424 (while (string-match re term)
13425 (setq rest (substring term (match-end 0))
13426 minus (and (match-end 1)
13427 (equal (match-string 1 term) "-"))
13428 tag (save-match-data (replace-regexp-in-string
13430 (match-string 2 term)))
13431 re-p (equal (string-to-char tag) ?{)
13432 level-p (match-end 4)
13433 prop-p (match-end 5)
13435 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
13437 (setq level-op (org-op-to-function (match-string 3 term)))
13438 `(,level-op level ,(string-to-number
13439 (match-string 4 term))))
13441 (setq pn (match-string 5 term)
13442 po (match-string 6 term)
13443 pv (match-string 7 term)
13444 re-p (equal (string-to-char pv) ?{)
13445 str-p (equal (string-to-char pv) ?\")
13446 time-p (save-match-data
13447 (string-match "^\"[[<].*[]>]\"$" pv))
13448 pv (if (or re-p str-p) (substring pv 1 -1) pv))
13449 (if time-p (setq pv (org-matcher-time pv)))
13450 (setq po (org-op-to-function po (if time-p 'time str-p)))
13452 ((equal pn "CATEGORY")
13453 (setq gv '(get-text-property (point) 'org-category)))
13457 (setq gv `(org-cached-entry-get nil ,pn))))
13460 `(not (string-match ,pv (or ,gv "")))
13461 `(string-match ,pv (or ,gv "")))
13463 `(,po (or ,gv "") ,pv)
13464 `(,po (string-to-number (or ,gv ""))
13465 ,(string-to-number pv) ))))
13466 (t `(member ,tag tags-list)))
13467 mm (if minus (list 'not mm) mm)
13469 (push mm tagsmatcher))
13470 (push (if (> (length tagsmatcher) 1)
13471 (cons 'and tagsmatcher)
13474 (setq tagsmatcher nil))
13475 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
13477 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
13478 ;; Make the todo matcher
13479 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
13480 (setq todomatcher t)
13481 (setq orterms (org-split-string todomatch "|") orlist nil)
13482 (while (setq term (pop orterms))
13483 (while (string-match re term)
13484 (setq minus (and (match-end 1)
13485 (equal (match-string 1 term) "-"))
13486 kwd (match-string 2 term)
13487 re-p (equal (string-to-char kwd) ?{)
13488 term (substring term (match-end 0))
13490 `(string-match ,(substring kwd 1 -1) todo)
13491 (list 'equal 'todo kwd))
13492 mm (if minus (list 'not mm) mm))
13493 (push mm todomatcher))
13494 (push (if (> (length todomatcher) 1)
13495 (cons 'and todomatcher)
13498 (setq todomatcher nil))
13499 (setq todomatcher (if (> (length orlist) 1)
13500 (cons 'or orlist) (car orlist))))
13502 ;; Return the string and lisp forms of the matcher
13503 (setq matcher (if todomatcher
13504 (list 'and tagsmatcher todomatcher)
13507 (setq matcher (list 'and '(member todo org-not-done-keywords)
13509 (cons match0 matcher)))
13511 (defun org-op-to-function (op &optional stringp)
13512 "Turn an operator into the appropriate function."
13515 ((equal op "<" ) '(< string< org-time<))
13516 ((equal op ">" ) '(> org-string> org-time>))
13517 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
13518 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
13519 ((member op '("=" "==")) '(= string= org-time=))
13520 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
13521 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
13523 (defun org<> (a b) (not (= a b)))
13524 (defun org-string<= (a b) (or (string= a b) (string< a b)))
13525 (defun org-string>= (a b) (not (string< a b)))
13526 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
13527 (defun org-string<> (a b) (not (string= a b)))
13528 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
13529 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
13530 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
13531 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
13532 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
13533 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
13535 "Convert S to a floating point time.
13536 If S is already a number, just return it. If it is a string, parse
13537 it as a time string and apply `float-time' to it. If S is nil, just return 0."
13541 (condition-case nil
13542 (float-time (apply 'encode-time (org-parse-time-string s)))
13546 (defun org-time-today ()
13547 "Time in seconds today at 0:00.
13548 Returns the float number of seconds since the beginning of the
13549 epoch to the beginning of today (00:00)."
13550 (float-time (apply 'encode-time
13551 (append '(0 0 0) (nthcdr 3 (decode-time))))))
13553 (defun org-matcher-time (s)
13554 "Interpret a time comparison value."
13557 ((string= s "<now>") (float-time))
13558 ((string= s "<today>") (org-time-today))
13559 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
13560 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
13561 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
13562 (+ (org-time-today)
13563 (* (string-to-number (match-string 1 s))
13564 (cdr (assoc (match-string 2 s)
13565 '(("d" . 86400.0) ("w" . 604800.0)
13566 ("m" . 2678400.0) ("y" . 31557600.0)))))))
13569 (defun org-match-any-p (re list)
13570 "Does re match any element of list?"
13571 (setq list (mapcar (lambda (x) (string-match re x)) list))
13574 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
13575 (defvar org-tags-overlay (make-overlay 1 1))
13576 (org-detach-overlay org-tags-overlay)
13578 (defun org-get-local-tags-at (&optional pos)
13579 "Get a list of tags defined in the current headline."
13580 (org-get-tags-at pos 'local))
13582 (defun org-get-local-tags ()
13583 "Get a list of tags defined in the current headline."
13584 (org-get-tags-at nil 'local))
13586 (defun org-get-tags-at (&optional pos local)
13587 "Get a list of all headline tags applicable at POS.
13588 POS defaults to point. If tags are inherited, the list contains
13589 the targets in the same sequence as the headlines appear, i.e.
13590 the tags of the current headline come last.
13591 When LOCAL is non-nil, only return tags from the current headline,
13592 ignore inherited ones."
13594 (if (and org-trust-scanner-tags
13595 (or (not pos) (equal pos (point)))
13598 (let (tags ltags lastpos parent)
13602 (goto-char (or pos (point)))
13605 (condition-case nil
13607 (org-back-to-heading t)
13608 (while (not (equal lastpos (point)))
13609 (setq lastpos (point))
13611 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
13612 (setq ltags (org-split-string
13613 (org-match-string-no-properties 1) ":"))
13615 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
13618 (org-remove-uninherited-tags ltags)
13621 (or org-use-tag-inheritance (throw 'done t))
13622 (if local (throw 'done t))
13623 (or (org-up-heading-safe) (error nil))
13628 (append (org-remove-uninherited-tags org-file-tags) tags))))))
13630 (defun org-add-prop-inherited (s)
13631 (add-text-properties 0 (length s) '(inherited t) s)
13634 (defun org-toggle-tag (tag &optional onoff)
13635 "Toggle the tag TAG for the current line.
13636 If ONOFF is `on' or `off', don't toggle but set to this state."
13639 (org-back-to-heading t)
13640 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
13643 (setq current (match-string 1))
13644 (replace-match ""))
13646 (setq current (nreverse (org-split-string current ":")))
13650 (or (member tag current) (push tag current)))
13652 (or (not (member tag current)) (setq current (delete tag current))))
13653 (t (if (member tag current)
13654 (setq current (delete tag current))
13656 (push tag current))))
13660 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
13661 (org-set-tags nil t))
13662 (delete-horizontal-space))
13663 (run-hooks 'org-after-tags-change-hook))
13666 (defun org-align-tags-here (to-col)
13667 ;; Assumes that this is a headline
13668 (let ((pos (point)) (col (current-column)) ncol tags-l p)
13669 (beginning-of-line 1)
13670 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13671 (< pos (match-beginning 2)))
13673 (setq tags-l (- (match-end 2) (match-beginning 2)))
13674 (goto-char (match-beginning 1))
13676 (delete-region (point) (1+ (match-beginning 2)))
13677 (setq ncol (max (current-column)
13681 (- (abs to-col) tags-l))))
13683 (insert (make-string (- ncol (current-column)) ?\ ))
13684 (setq ncol (current-column))
13685 (when indent-tabs-mode (tabify p (point-at-eol)))
13686 (org-move-to-column (min ncol col) t))
13689 (defun org-set-tags-command (&optional arg just-align)
13690 "Call the set-tags command for the current entry."
13692 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
13693 (org-set-tags arg just-align)
13695 (org-back-to-heading t)
13696 (org-set-tags arg just-align))))
13698 (defun org-set-tags-to (data)
13699 "Set the tags of the current entry to DATA, replacing the current tags.
13700 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
13701 If DATA is nil or the empty string, any tags will be removed."
13702 (interactive "sTags: ")
13706 ((equal data "") "")
13708 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
13711 (concat ":" (mapconcat 'identity data ":") ":"))))
13714 (org-back-to-heading t)
13715 (when (looking-at org-complex-heading-regexp)
13718 (goto-char (match-beginning 5))
13720 (delete-region (point) (point-at-eol))
13721 (org-set-tags nil 'align))
13722 (goto-char (point-at-eol))
13724 (org-set-tags nil 'align)))
13725 (beginning-of-line 1)
13726 (if (looking-at ".*?\\([ \t]+\\)$")
13727 (delete-region (match-beginning 1) (match-end 1))))))
13729 (defun org-align-all-tags ()
13730 "Align the tags i all headings."
13733 (or (ignore-errors (org-back-to-heading t))
13734 (outline-next-heading))
13735 (if (org-at-heading-p)
13737 (message "No headings"))))
13739 (defvar org-indent-indentation-per-level)
13740 (defun org-set-tags (&optional arg just-align)
13741 "Set the tags for the current headline.
13742 With prefix ARG, realign all tags in headings in the current buffer."
13744 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13745 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
13746 'region-start-level 'region))
13747 org-loop-over-headlines-in-active-region)
13749 ;; We don't use ARG and JUST-ALIGN here these args are not
13750 ;; useful when looping over headlines
13752 org-loop-over-headlines-in-active-region
13753 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
13754 (let* ((re org-outline-regexp-bol)
13755 (current (unless arg (org-get-tags-string)))
13756 (col (current-column))
13757 (org-setting-tags t)
13758 table current-tags inherited-tags ; computed below when needed
13759 tags p0 c0 c1 rpl di tc level)
13762 (goto-char (point-min))
13763 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
13764 (while (re-search-forward re nil t)
13765 (org-set-tags nil t)
13767 (message "All tags realigned to column %d" org-tags-column))
13769 (setq tags current)
13770 ;; Get a new set of tags from the user
13772 (setq table (append org-tag-persistent-alist
13773 (or org-tag-alist (org-get-buffer-tags))
13775 org-complete-tags-always-offer-all-agenda-tags
13776 (org-global-tags-completion-table
13777 (org-agenda-files))))
13778 org-last-tags-completion-table table
13779 current-tags (org-split-string current ":")
13780 inherited-tags (nreverse
13781 (nthcdr (length current-tags)
13782 (nreverse (org-get-tags-at))))
13784 (if (or (eq t org-use-fast-tag-selection)
13785 (and org-use-fast-tag-selection
13786 (delq nil (mapcar 'cdr table))))
13787 (org-fast-tag-selection
13788 current-tags inherited-tags table
13789 (if org-fast-tag-selection-include-todo
13790 org-todo-key-alist))
13791 (let ((org-add-colon-after-tag-completion (< 1 (length table))))
13793 (org-icompleting-read "Tags: "
13794 'org-tags-completion-function
13795 nil nil current 'org-tags-history))))))
13796 (while (string-match "[-+&]+" tags)
13797 ;; No boolean logic, just a list
13798 (setq tags (replace-match ":" t t tags))))
13800 (setq tags (replace-regexp-in-string "[,]" ":" tags))
13802 (if org-tags-sort-function
13803 (setq tags (mapconcat 'identity
13804 (sort (org-split-string
13805 tags (org-re "[^[:alnum:]_@#%]+"))
13806 org-tags-sort-function) ":")))
13808 (if (string-match "\\`[\t ]*\\'" tags)
13810 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
13811 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
13813 ;; Insert new tags at the correct column
13814 (beginning-of-line 1)
13815 (setq level (or (and (looking-at org-outline-regexp)
13816 (- (match-end 0) (point) 1))
13819 ((and (equal current "") (equal tags "")))
13820 ((re-search-forward
13821 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
13823 (if (equal tags "")
13825 (goto-char (match-beginning 0))
13826 (setq c0 (current-column)
13827 ;; compute offset for the case of org-indent-mode active
13828 di (if org-indent-mode
13829 (* (1- org-indent-indentation-per-level) (1- level))
13831 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
13832 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
13833 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
13834 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
13835 (replace-match rpl t t)
13836 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
13838 (t (error "Tags alignment failed")))
13839 (org-move-to-column col)
13841 (run-hooks 'org-after-tags-change-hook))))))
13843 (defun org-change-tag-in-region (beg end tag off)
13844 "Add or remove TAG for each entry in the region.
13845 This works in the agenda, and also in an org-mode buffer."
13847 (list (region-beginning) (region-end)
13848 (let ((org-last-tags-completion-table
13849 (if (derived-mode-p 'org-mode)
13850 (org-get-buffer-tags)
13851 (org-global-tags-completion-table))))
13852 (org-icompleting-read
13853 "Tag: " 'org-tags-completion-function nil nil nil
13854 'org-tags-history))
13856 (message "[s]et or
[r]emove? ")
13857 (equal (read-char-exclusive) ?r))))
13858 (if (fboundp 'deactivate-mark) (deactivate-mark))
13859 (let ((agendap (equal major-mode 'org-agenda-mode))
13860 l1 l2 m buf pos newhead (cnt 0))
13862 (setq l2 (1- (org-current-line)))
13864 (setq l1 (org-current-line))
13865 (loop for l from l1 to l2 do
13867 (setq m (get-text-property (point) 'org-hd-marker))
13868 (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
13870 (setq buf (if agendap (marker-buffer m) (current-buffer))
13871 pos (if agendap m (point)))
13872 (with-current-buffer buf
13876 (setq cnt (1+ cnt))
13877 (org-toggle-tag tag (if off 'off 'on))
13878 (setq newhead (org-get-heading)))))
13879 (and agendap (org-agenda-change-all-lines newhead m))))
13880 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
13882 (defun org-tags-completion-function (string predicate &optional flag)
13883 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
13884 (confirm (lambda (x) (stringp (car x)))))
13885 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
13886 (setq s1 (match-string 1 string)
13887 s2 (match-string 2 string))
13888 (setq s1 "" s2 string))
13892 (setq rtn (try-completion s2 ctable confirm))
13895 (concat s1 s2 (substring rtn (length s2))
13896 (if (and org-add-colon-after-tag-completion
13897 (assoc rtn ctable))
13902 (all-completions s2 ctable confirm)
13906 (assoc s2 ctable)))
13909 (defun org-fast-tag-insert (kwd tags face &optional end)
13910 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
13911 (insert (format "%-12s" (concat kwd ":"))
13912 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
13915 (defun org-fast-tag-show-exit (flag)
13918 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
13919 (replace-match ""))
13922 (org-move-to-column (- (window-width) 19) t)
13923 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
13925 (defun org-set-current-tags-overlay (current prefix)
13926 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
13927 (if (featurep 'xemacs)
13928 (org-overlay-display org-tags-overlay (concat prefix s)
13929 'secondary-selection)
13930 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
13931 (org-overlay-display org-tags-overlay (concat prefix s)))))
13933 (defvar org-last-tag-selection-key nil)
13934 (defun org-fast-tag-selection (current inherited table &optional todo-table)
13935 "Fast tag selection with single keys.
13936 CURRENT is the current list of tags in the headline, INHERITED is the
13937 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
13938 possibly with grouping information. TODO-TABLE is a similar table with
13939 TODO keywords, should these have keys assigned to them.
13940 If the keys are nil, a-z are automatically assigned.
13941 Returns the new tags string, or nil to not change the current settings."
13942 (let* ((fulltable (append table todo-table))
13943 (maxlen (apply 'max (mapcar
13945 (if (stringp (car x)) (string-width (car x)) 0))
13947 (buf (current-buffer))
13948 (expert (eq org-fast-tag-selection-single-key 'expert))
13950 (fwidth (+ maxlen 3 1 3))
13951 (ncol (/ (- (window-width) 4) fwidth))
13954 tg cnt e c char c1 c2 ntable tbl rtn
13955 ov-start ov-end ov-prefix
13956 (exit-after-next org-fast-tag-selection-single-key)
13957 (done-keywords org-done-keywords)
13960 (beginning-of-line 1)
13962 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13963 (setq ov-start (match-beginning 1)
13964 ov-end (match-end 1)
13966 (setq ov-start (1- (point-at-eol))
13967 ov-end (1+ ov-start))
13968 (skip-chars-forward "^\n\r")
13971 (buffer-substring (1- (point)) (point))
13972 (if (> (current-column) org-tags-column)
13974 (make-string (- org-tags-column (current-column)) ?\ ))))))
13975 (move-overlay org-tags-overlay ov-start ov-end)
13976 (save-window-excursion
13978 (set-buffer (get-buffer-create " *Org tags*"))
13979 (delete-other-windows)
13980 (split-window-vertically)
13981 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
13983 (org-set-local 'org-done-keywords done-keywords)
13984 (org-fast-tag-insert "Inherited" inherited i-face "\n")
13985 (org-fast-tag-insert "Current" current c-face "\n\n")
13986 (org-fast-tag-show-exit exit-after-next)
13987 (org-set-current-tags-overlay current ov-prefix)
13988 (setq tbl fulltable char ?a cnt 0)
13989 (while (setq e (pop tbl))
13991 ((equal (car e) :startgroup)
13992 (push '() groups) (setq ingroup t)
13993 (when (not (= cnt 0))
13996 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
13997 ((equal (car e) :endgroup)
13998 (setq ingroup nil cnt 0)
13999 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
14000 ((equal e '(:newline))
14001 (when (not (= cnt 0))
14005 (while (equal (car tbl) '(:newline))
14007 (setq tbl (cdr tbl)))))
14009 (setq tg (copy-sequence (car e)) c2 nil)
14012 ;; automatically assign a character.
14013 (setq c1 (string-to-char
14014 (downcase (substring
14015 tg (if (= (string-to-char tg) ?@) 1 0)))))
14016 (if (or (rassoc c1 ntable) (rassoc c1 table))
14017 (while (or (rassoc char ntable) (rassoc char table))
14018 (setq char (1+ char)))
14020 (setq c (or c2 char)))
14021 (if ingroup (push tg (car groups)))
14022 (setq tg (org-add-props tg nil 'face
14024 ((not (assoc tg table))
14025 (org-get-todo-face tg))
14026 ((member tg current) c-face)
14027 ((member tg inherited) i-face))))
14028 (if (and (= cnt 0) (not ingroup)) (insert " "))
14029 (insert "[" c "] " tg (make-string
14030 (- fwidth 4 (length tg)) ?\ ))
14031 (push (cons tg c) ntable)
14032 (when (= (setq cnt (1+ cnt)) ncol)
14034 (if ingroup (insert " "))
14036 (setq ntable (nreverse ntable))
14038 (goto-char (point-min))
14039 (if (not expert) (org-fit-window-to-buffer))
14043 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
14044 (if (not groups) "no " "")
14045 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
14046 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
14047 (setq org-last-tag-selection-key c)
14049 ((= c ?\r) (throw 'exit t))
14051 (setq groups (not groups))
14052 (goto-char (point-min))
14053 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
14056 (org-fast-tag-show-exit
14057 (setq exit-after-next (not exit-after-next)))
14059 (delete-other-windows)
14060 (set-window-buffer (split-window-vertically) " *Org tags*")
14061 (org-switch-to-buffer-other-window " *Org tags*")
14062 (org-fit-window-to-buffer)))
14064 (and (= c ?q) (not (rassoc c ntable))))
14065 (org-detach-overlay org-tags-overlay)
14066 (setq quit-flag t))
14069 (if exit-after-next (setq exit-after-next 'now)))
14071 (condition-case nil
14072 (setq tg (org-icompleting-read
14075 (with-current-buffer buf
14076 (org-get-buffer-tags)))))
14077 (quit (setq tg "")))
14078 (when (string-match "\\S-" tg)
14079 (add-to-list 'buffer-tags (list tg))
14080 (if (member tg current)
14081 (setq current (delete tg current))
14082 (push tg current)))
14083 (if exit-after-next (setq exit-after-next 'now)))
14084 ((setq e (rassoc c todo-table) tg (car e))
14085 (with-current-buffer buf
14086 (save-excursion (org-todo tg)))
14087 (if exit-after-next (setq exit-after-next 'now)))
14088 ((setq e (rassoc c ntable) tg (car e))
14089 (if (member tg current)
14090 (setq current (delete tg current))
14091 (loop for g in groups do
14094 (setq current (delete x current)))
14097 (if exit-after-next (setq exit-after-next 'now))))
14099 ;; Create a sorted list
14103 (assoc b (cdr (memq (assoc a ntable) ntable))))))
14104 (if (eq exit-after-next 'now) (throw 'exit t))
14105 (goto-char (point-min))
14106 (beginning-of-line 2)
14107 (delete-region (point) (point-at-eol))
14108 (org-fast-tag-insert "Current" current c-face)
14109 (org-set-current-tags-overlay current ov-prefix)
14110 (while (re-search-forward
14111 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
14112 (setq tg (match-string 1))
14113 (add-text-properties
14114 (match-beginning 1) (match-end 1)
14117 ((member tg current) c-face)
14118 ((member tg inherited) i-face)
14119 (t (get-text-property (match-beginning 1) 'face))))))
14120 (goto-char (point-min)))))
14121 (org-detach-overlay org-tags-overlay)
14123 (mapconcat 'identity current ":")
14126 (defun org-get-tags-string ()
14127 "Get the TAGS string in the current headline."
14128 (unless (org-at-heading-p t)
14129 (error "Not on a heading"))
14131 (beginning-of-line 1)
14132 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14133 (org-match-string-no-properties 1)
14136 (defun org-get-tags ()
14137 "Get the list of tags specified in the current headline."
14138 (org-split-string (org-get-tags-string) ":"))
14140 (defun org-get-buffer-tags ()
14141 "Get a table of all tags used in the buffer, for completion."
14144 (goto-char (point-min))
14145 (while (re-search-forward
14146 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
14147 (when (equal (char-after (point-at-bol 0)) ?*)
14148 (mapc (lambda (x) (add-to-list 'tags x))
14149 (org-split-string (org-match-string-no-properties 1) ":")))))
14150 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
14151 (mapcar 'list tags)))
14153 ;;;; The mapping API
14156 (defun org-map-entries (func &optional match scope &rest skip)
14157 "Call FUNC at each headline selected by MATCH in SCOPE.
14159 FUNC is a function or a lisp form. The function will be called without
14160 arguments, with the cursor positioned at the beginning of the headline.
14161 The return values of all calls to the function will be collected and
14162 returned as a list.
14164 The call to FUNC will be wrapped into a save-excursion form, so FUNC
14165 does not need to preserve point. After evaluation, the cursor will be
14166 moved to the end of the line (presumably of the headline of the
14167 processed entry) and search continues from there. Under some
14168 circumstances, this may not produce the wanted results. For example,
14169 if you have removed (e.g. archived) the current (sub)tree it could
14170 mean that the next entry will be skipped entirely. In such cases, you
14171 can specify the position from where search should continue by making
14172 FUNC set the variable `org-map-continue-from' to the desired buffer
14175 MATCH is a tags/property/todo match as it is used in the agenda tags view.
14176 Only headlines that are matched by this query will be considered during
14177 the iteration. When MATCH is nil or t, all headlines will be
14178 visited by the iteration.
14180 SCOPE determines the scope of this command. It can be any of:
14182 nil The current buffer, respecting the restriction if any
14183 tree The subtree started with the entry at point
14184 region The entries within the active region, if any
14186 The entries within the active region, but only those at
14187 the same level than the first one.
14188 file The current buffer, without restriction
14190 The current buffer, and any archives associated with it
14191 agenda All agenda files
14192 agenda-with-archives
14193 All agenda files with any archive files associated with them
14195 If this is a list, all files in the list will be scanned
14197 The remaining args are treated as settings for the skipping facilities of
14198 the scanner. The following items can be given here:
14200 archive skip trees with the archive tag.
14201 comment skip trees with the COMMENT keyword
14202 function or Emacs Lisp form:
14203 will be used as value for `org-agenda-skip-function', so whenever
14204 the function returns t, FUNC will not be called for that
14205 entry and search will continue from the point where the
14206 function leaves it.
14208 If your function needs to retrieve the tags including inherited tags
14209 at the *current* entry, you can use the value of the variable
14210 `org-scanner-tags' which will be much faster than getting the value
14211 with `org-get-tags-at'. If your function gets properties with
14212 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
14213 to t around the call to `org-entry-properties' to get the same speedup.
14214 Note that if your function moves around to retrieve tags and properties at
14215 a *different* entry, you cannot use these techniques."
14216 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
14217 (not (org-region-active-p)))
14218 (let* ((org-agenda-archives-mode nil) ; just to make sure
14219 (org-agenda-skip-archived-trees (memq 'archive skip))
14220 (org-agenda-skip-comment-trees (memq 'comment skip))
14221 (org-agenda-skip-function
14222 (car (org-delete-all '(comment archive) skip)))
14223 (org-tags-match-list-sublevels t)
14224 (start-level (eq scope 'region-start-level))
14226 org-todo-keywords-for-agenda
14227 org-done-keywords-for-agenda
14228 org-todo-keyword-alist-for-agenda
14229 org-drawers-for-agenda
14230 org-tag-alist-for-agenda
14234 ((eq match t) (setq matcher t))
14235 ((eq match nil) (setq matcher t))
14236 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
14240 (cond ((eq scope 'tree)
14241 (org-back-to-heading t)
14242 (org-narrow-to-subtree)
14244 ((and (or (eq scope 'region) (eq scope 'region-start-level))
14245 (org-region-active-p))
14246 ;; If needed, set start-level to a string like "2"
14249 (goto-char (region-beginning))
14250 (unless (org-at-heading-p) (outline-next-heading))
14251 (setq start-level (org-current-level))))
14252 (narrow-to-region (region-beginning)
14254 (goto-char (region-end))
14255 (unless (and (bolp) (org-at-heading-p))
14256 (outline-next-heading))
14262 (org-prepare-agenda-buffers
14263 (list (buffer-file-name (current-buffer))))
14264 (setq res (org-scan-tags func matcher todo-only start-level)))
14265 ;; Get the right scope
14267 ((and scope (listp scope) (symbolp (car scope)))
14268 (setq scope (eval scope)))
14269 ((eq scope 'agenda)
14270 (setq scope (org-agenda-files t)))
14271 ((eq scope 'agenda-with-archives)
14272 (setq scope (org-agenda-files t))
14273 (setq scope (org-add-archive-files scope)))
14275 (setq scope (list (buffer-file-name))))
14276 ((eq scope 'file-with-archives)
14277 (setq scope (org-add-archive-files (list (buffer-file-name))))))
14278 (org-prepare-agenda-buffers scope)
14279 (while (setq file (pop scope))
14280 (with-current-buffer (org-find-base-buffer-visiting file)
14284 (goto-char (point-min))
14285 (setq res (append res (org-scan-tags func matcher todo-only))))))))))
14290 ;;; Setting and retrieving properties
14292 (defconst org-special-properties
14293 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
14294 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
14295 "The special properties valid in Org-mode.
14297 These are properties that are not defined in the property drawer,
14298 but in some other way.")
14300 (defconst org-default-properties
14301 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
14302 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
14303 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
14304 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
14305 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
14306 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
14307 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
14308 "Some properties that are used by Org-mode for various purposes.
14309 Being in this list makes sure that they are offered for completion.")
14311 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
14312 "Regular expression matching the first line of a property drawer.")
14314 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
14315 "Regular expression matching the last line of a property drawer.")
14317 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
14318 "Regular expression matching the first line of a property drawer.")
14320 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
14321 "Regular expression matching the first line of a property drawer.")
14323 (defconst org-property-drawer-re
14324 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
14325 org-property-end-re "\\)\n?")
14326 "Matches an entire property drawer.")
14328 (defconst org-clock-drawer-re
14329 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
14330 org-property-end-re "\\)\n?")
14331 "Matches an entire clock drawer.")
14333 (defsubst org-re-property (property)
14334 "Return a regexp matching a PROPERTY line.
14335 Match group 1 will be set to the value."
14336 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
14338 (defsubst org-re-property-keyword (property)
14339 "Return a regexp matching a PROPERTY line, possibly with no
14340 value for the property."
14341 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
14343 (defun org-property-action ()
14344 "Do an action on properties."
14347 (org-at-property-p)
14348 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
14349 (setq c (read-char-exclusive))
14352 (call-interactively 'org-set-property))
14354 (call-interactively 'org-delete-property))
14356 (call-interactively 'org-delete-property-globally))
14358 (call-interactively 'org-compute-property-at-point))
14359 (t (error "No such property action %c" c)))))
14361 (defun org-inc-effort ()
14362 "Increment the value of the effort property in the current entry."
14364 (org-set-effort nil t))
14366 (defun org-set-effort (&optional value increment)
14367 "Set the effort property of the current entry.
14368 With numerical prefix arg, use the nth allowed value, 0 stands for the
14369 10th allowed value.
14371 When INCREMENT is non-nil, set the property to the next allowed value."
14373 (if (equal value 0) (setq value 10))
14374 (let* ((completion-ignore-case t)
14375 (prop org-effort-property)
14376 (cur (org-entry-get nil prop))
14377 (allowed (org-property-get-allowed-values nil prop 'table))
14378 (existing (mapcar 'list (org-property-values prop)))
14381 ((stringp value) value)
14382 ((and allowed (integerp value))
14383 (or (car (nth (1- value) allowed))
14384 (car (org-last allowed))))
14385 ((and allowed increment)
14386 (or (caadr (member (list cur) allowed))
14387 (error "Allowed effort values are not set")))
14389 (message "Select 1-9,0, [RET%s]: %s"
14390 (if cur (concat "=" cur) "")
14391 (mapconcat 'car allowed " "))
14392 (setq rpl (read-char-exclusive))
14393 (if (equal rpl ?\r)
14395 (setq rpl (- rpl ?0))
14396 (if (equal rpl 0) (setq rpl 10))
14397 (if (and (> rpl 0) (<= rpl (length allowed)))
14398 (car (nth (1- rpl) allowed))
14399 (org-completing-read "Effort: " allowed nil))))
14401 (let (org-completion-use-ido org-completion-use-iswitchb)
14402 (org-completing-read
14403 (concat "Effort " (if (and cur (string-match "\\S-" cur))
14404 (concat "[" cur "]") "")
14406 existing nil nil "" nil cur))))))
14407 (unless (equal (org-entry-get nil prop) val)
14408 (org-entry-put nil prop val))
14409 (message "%s is now %s" prop val)))
14411 (defun org-at-property-p ()
14412 "Is cursor inside a property drawer?"
14414 (beginning-of-line 1)
14415 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
14416 (save-match-data ;; Used by calling procedures
14418 (range (unless (org-before-first-heading-p)
14419 (org-get-property-block))))
14420 (and range (<= (car range) p) (< p (cdr range))))))))
14422 (defun org-get-property-block (&optional beg end force)
14423 "Return the (beg . end) range of the body of the property drawer.
14424 BEG and END are the beginning and end of the current subtree, or of
14425 the part before the first headline. If they are not given, they will
14426 be found. If the drawer does not exist and FORCE is non-nil, create
14430 (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
14431 (progn (org-back-to-heading t) (point))))
14432 (end (or end (and (not (outline-next-heading)) (point-max))
14435 (if (re-search-forward org-property-start-re end t)
14436 (setq beg (1+ (match-end 0)))
14439 (org-insert-property-drawer)
14440 (setq end (progn (outline-next-heading) (point))))
14443 (if (re-search-forward org-property-start-re end t)
14444 (setq beg (1+ (match-end 0)))))
14445 (if (re-search-forward org-property-end-re end t)
14446 (setq end (match-beginning 0))
14447 (or force (throw 'exit nil))
14451 (insert ":END:\n"))
14454 (defun org-entry-properties (&optional pom which specific)
14455 "Get all properties of the entry at point-or-marker POM.
14456 This includes the TODO keyword, the tags, time strings for deadline,
14457 scheduled, and clocking, and any additional properties defined in the
14458 entry. The return value is an alist, keys may occur multiple times
14459 if the property key was used several times.
14460 POM may also be nil, in which case the current entry is used.
14461 If WHICH is nil or `all', get all properties. If WHICH is
14462 `special' or `standard', only get that subclass. If WHICH
14463 is a string only get exactly this property. SPECIFIC can be a string, the
14464 specific property we are interested in. Specifying it can speed
14465 things up because then unnecessary parsing is avoided."
14466 (setq which (or which 'all))
14467 (org-with-point-at pom
14468 (let ((clockstr (substring org-clock-string 0 -1))
14469 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
14470 (case-fold-search nil)
14471 beg end range props sum-props key key1 value string clocksum clocksumt)
14473 (when (condition-case nil
14474 (and (derived-mode-p 'org-mode) (org-back-to-heading t))
14477 (setq sum-props (get-text-property (point) 'org-summaries))
14478 (setq clocksum (get-text-property (point) :org-clock-minutes)
14479 clocksumt (get-text-property (point) :org-clock-minutes-today))
14480 (outline-next-heading)
14482 (when (memq which '(all special))
14483 ;; Get the special properties, like TODO and tags
14485 (when (and (or (not specific) (string= specific "TODO"))
14486 (looking-at org-todo-line-regexp) (match-end 2))
14487 (push (cons "TODO" (org-match-string-no-properties 2)) props))
14488 (when (and (or (not specific) (string= specific "PRIORITY"))
14489 (looking-at org-priority-regexp))
14490 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
14491 (when (or (not specific) (string= specific "FILE"))
14492 (push (cons "FILE" buffer-file-name) props))
14493 (when (and (or (not specific) (string= specific "TAGS"))
14494 (setq value (org-get-tags-string))
14495 (string-match "\\S-" value))
14496 (push (cons "TAGS" value) props))
14497 (when (and (or (not specific) (string= specific "ALLTAGS"))
14498 (setq value (org-get-tags-at)))
14499 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
14502 (when (or (not specific) (string= specific "BLOCKED"))
14503 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
14504 (when (or (not specific)
14506 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
14507 "TIMESTAMP" "TIMESTAMP_IA")))
14509 (while (re-search-forward org-maybe-keyword-time-regexp end t)
14510 (setq key (if (match-end 1)
14511 (substring (org-match-string-no-properties 1)
14513 string (if (equal key clockstr)
14515 (buffer-substring-no-properties
14516 (match-beginning 3) (goto-char
14518 (substring (org-match-string-no-properties 3)
14520 ;; Get the correct property name from the key. This is
14521 ;; necessary if the user has configured time keywords.
14522 (setq key1 (concat key ":"))
14526 (if (= (char-after (match-beginning 3)) ?\[)
14527 "TIMESTAMP_IA" "TIMESTAMP")))
14528 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
14529 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
14530 ((equal key1 org-closed-string) (setq key "CLOSED"))
14531 ((equal key1 org-clock-string) (setq key "CLOCK")))
14532 (if (and specific (equal key specific) (not (equal key "CLOCK")))
14534 (push (cons key string) props)
14535 ;; no need to search further if match is found
14537 (when (or (equal key "CLOCK") (not (assoc key props)))
14538 (push (cons key string) props)))))))
14540 (when (memq which '(all standard))
14541 ;; Get the standard properties, like :PROP: ...
14542 (setq range (org-get-property-block beg end))
14544 (goto-char (car range))
14545 (while (re-search-forward
14546 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
14548 (setq key (org-match-string-no-properties 1)
14549 value (org-trim (or (org-match-string-no-properties 2) "")))
14550 (unless (member key excluded)
14551 (push (cons key (or value "")) props)))))
14553 (push (cons "CLOCKSUM"
14554 (org-columns-number-to-string (/ (float clocksum) 60.)
14558 (push (cons "CLOCKSUM_T"
14559 (org-columns-number-to-string (/ (float clocksumt) 60.)
14562 (unless (assoc "CATEGORY" props)
14563 (push (cons "CATEGORY" (org-get-category)) props))
14564 (append sum-props (nreverse props)))))))
14566 (defun org-entry-get (pom property &optional inherit literal-nil)
14567 "Get value of PROPERTY for entry or content at point-or-marker POM.
14568 If INHERIT is non-nil and the entry does not have the property,
14569 then also check higher levels of the hierarchy.
14570 If INHERIT is the symbol `selective', use inheritance only if the setting
14571 in `org-use-property-inheritance' selects PROPERTY for inheritance.
14572 If the property is present but empty, the return value is the empty string.
14573 If the property is not present at all, nil is returned.
14575 If LITERAL-NIL is set, return the string value \"nil\" as a string,
14576 do not interpret it as the list atom nil. This is used for inheritance
14577 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
14578 (org-with-point-at pom
14579 (if (and inherit (if (eq inherit 'selective)
14580 (org-property-inherit-p property)
14582 (org-entry-get-with-inheritance property literal-nil)
14583 (if (member property org-special-properties)
14584 ;; We need a special property. Use `org-entry-properties' to
14585 ;; retrieve it, but specify the wanted property
14586 (cdr (assoc property (org-entry-properties nil 'special property)))
14587 (let* ((range (org-get-property-block))
14588 (props (list (or (assoc property org-file-properties)
14589 (assoc property org-global-properties)
14590 (assoc property org-global-properties-fixed))))
14592 (when (re-search-forward
14593 (org-re-property key) (cdr range) t)
14595 (org-update-property-plist
14598 (org-match-string-no-properties 1) "")
14601 (when (and range (goto-char (car range)))
14602 (funcall ap property)
14603 (goto-char (car range))
14604 (while (funcall ap (concat property "+")))
14605 (setq val (cdr (assoc property props)))
14606 (when val (if literal-nil val (org-not-nil val)))))))))
14608 (defun org-property-or-variable-value (var &optional inherit)
14609 "Check if there is a property fixing the value of VAR.
14610 If yes, return this value. If not, return the current value of the variable."
14611 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14612 (if (and prop (stringp prop) (string-match "\\S-" prop))
14614 (symbol-value var))))
14616 (defun org-entry-delete (pom property)
14617 "Delete the property PROPERTY from entry at point-or-marker POM."
14618 (org-with-point-at pom
14619 (if (member property org-special-properties)
14620 nil ; cannot delete these properties.
14621 (let ((range (org-get-property-block)))
14623 (goto-char (car range))
14625 (org-re-property property)
14628 (delete-region (match-beginning 0) (1+ (point-at-eol)))
14632 ;; Multi-values properties are properties that contain multiple values
14633 ;; These values are assumed to be single words, separated by whitespace.
14634 (defun org-entry-add-to-multivalued-property (pom property value)
14635 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
14636 (let* ((old (org-entry-get pom property))
14637 (values (and old (org-split-string old "[ \t]"))))
14638 (setq value (org-entry-protect-space value))
14639 (unless (member value values)
14640 (setq values (cons value values))
14641 (org-entry-put pom property
14642 (mapconcat 'identity values " ")))))
14644 (defun org-entry-remove-from-multivalued-property (pom property value)
14645 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
14646 (let* ((old (org-entry-get pom property))
14647 (values (and old (org-split-string old "[ \t]"))))
14648 (setq value (org-entry-protect-space value))
14649 (when (member value values)
14650 (setq values (delete value values))
14651 (org-entry-put pom property
14652 (mapconcat 'identity values " ")))))
14654 (defun org-entry-member-in-multivalued-property (pom property value)
14655 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
14656 (let* ((old (org-entry-get pom property))
14657 (values (and old (org-split-string old "[ \t]"))))
14658 (setq value (org-entry-protect-space value))
14659 (member value values)))
14661 (defun org-entry-get-multivalued-property (pom property)
14662 "Return a list of values in a multivalued property."
14663 (let* ((value (org-entry-get pom property))
14664 (values (and value (org-split-string value "[ \t]"))))
14665 (mapcar 'org-entry-restore-space values)))
14667 (defun org-entry-put-multivalued-property (pom property &rest values)
14668 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
14669 VALUES should be a list of strings. Spaces will be protected."
14670 (org-entry-put pom property
14671 (mapconcat 'org-entry-protect-space values " "))
14672 (let* ((value (org-entry-get pom property))
14673 (values (and value (org-split-string value "[ \t]"))))
14674 (mapcar 'org-entry-restore-space values)))
14676 (defun org-entry-protect-space (s)
14677 "Protect spaces and newline in string S."
14678 (while (string-match " " s)
14679 (setq s (replace-match "%20" t t s)))
14680 (while (string-match "\n" s)
14681 (setq s (replace-match "%0A" t t s)))
14684 (defun org-entry-restore-space (s)
14685 "Restore spaces and newline in string S."
14686 (while (string-match "%20" s)
14687 (setq s (replace-match " " t t s)))
14688 (while (string-match "%0A" s)
14689 (setq s (replace-match "\n" t t s)))
14692 (defvar org-entry-property-inherited-from (make-marker)
14693 "Marker pointing to the entry from where a property was inherited.
14694 Each call to `org-entry-get-with-inheritance' will set this marker to the
14695 location of the entry where the inheritance search matched. If there was
14696 no match, the marker will point nowhere.
14697 Note that also `org-entry-get' calls this function, if the INHERIT flag
14700 (defun org-entry-get-with-inheritance (property &optional literal-nil)
14701 "Get PROPERTY of entry or content at point, search higher levels if needed.
14702 The search will stop at the first ancestor which has the property defined.
14703 If the value found is \"nil\", return nil to show that the property
14704 should be considered as undefined (this is the meaning of nil here).
14705 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
14706 (move-marker org-entry-property-inherited-from nil)
14713 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
14714 (or (ignore-errors (org-back-to-heading t))
14715 (goto-char (point-min)))
14716 (move-marker org-entry-property-inherited-from (point))
14718 (or (ignore-errors (org-up-heading-safe))
14719 (throw 'ex nil))))))
14721 (cdr (assoc property org-file-properties))
14722 (cdr (assoc property org-global-properties))
14723 (cdr (assoc property org-global-properties-fixed))))
14724 (if literal-nil tmp (org-not-nil tmp))))
14726 (defvar org-property-changed-functions nil
14727 "Hook called when the value of a property has changed.
14728 Each hook function should accept two arguments, the name of the property
14729 and the new value.")
14731 (defun org-entry-put (pom property value)
14732 "Set PROPERTY to VALUE for entry at point-or-marker POM."
14733 (org-with-point-at pom
14734 (org-back-to-heading t)
14735 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
14738 ((equal property "TODO")
14739 (when (and (stringp value) (string-match "\\S-" value)
14740 (not (member value org-todo-keywords-1)))
14741 (error "\"%s\" is not a valid TODO state" value))
14742 (if (or (not value)
14743 (not (string-match "\\S-" value)))
14744 (setq value 'none))
14746 (org-set-tags nil 'align))
14747 ((equal property "PRIORITY")
14748 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
14749 (string-to-char value) ?\ ))
14750 (org-set-tags nil 'align))
14751 ((equal property "SCHEDULED")
14752 (if (re-search-forward org-scheduled-time-regexp end t)
14754 ((eq value 'earlier) (org-timestamp-change -1 'day))
14755 ((eq value 'later) (org-timestamp-change 1 'day))
14756 (t (call-interactively 'org-schedule)))
14757 (call-interactively 'org-schedule)))
14758 ((equal property "DEADLINE")
14759 (if (re-search-forward org-deadline-time-regexp end t)
14761 ((eq value 'earlier) (org-timestamp-change -1 'day))
14762 ((eq value 'later) (org-timestamp-change 1 'day))
14763 (t (call-interactively 'org-deadline)))
14764 (call-interactively 'org-deadline)))
14765 ((member property org-special-properties)
14766 (error "The %s property can not yet be set with `org-entry-put'"
14768 (t ; a non-special property
14769 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
14770 (setq range (org-get-property-block beg end 'force))
14771 (goto-char (car range))
14772 (if (re-search-forward
14773 (org-re-property-keyword property) (cdr range) t)
14775 (delete-region (match-beginning 0) (match-end 0))
14776 (goto-char (match-beginning 0)))
14777 (goto-char (cdr range))
14781 (insert ":" property ":")
14782 (and value (insert " " value))
14783 (org-indent-line)))))
14784 (run-hook-with-args 'org-property-changed-functions property value)))
14786 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
14787 "Get all property keys in the current buffer.
14788 With INCLUDE-SPECIALS, also list the special properties that reflect things
14789 like tags and TODO state.
14790 With INCLUDE-DEFAULTS, also include properties that has special meaning
14791 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
14793 With INCLUDE-COLUMNS, also include property names given in COLUMN
14794 formats in the current buffer."
14795 (let (rtn range cfmt s p)
14799 (goto-char (point-min))
14800 (while (re-search-forward org-property-start-re nil t)
14801 (setq range (org-get-property-block))
14802 (goto-char (car range))
14803 (while (re-search-forward
14804 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
14806 (add-to-list 'rtn (org-match-string-no-properties 1)))
14807 (outline-next-heading))))
14809 (when include-specials
14810 (setq rtn (append org-special-properties rtn)))
14812 (when include-defaults
14813 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
14814 (add-to-list 'rtn org-effort-property))
14816 (when include-columns
14820 (goto-char (point-min))
14821 (while (re-search-forward
14822 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
14824 (setq cfmt (match-string 2) s 0)
14825 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
14827 (setq s (match-end 0)
14828 p (match-string 1 cfmt))
14829 (unless (or (equal p "ITEM")
14830 (member p org-special-properties))
14831 (add-to-list 'rtn (match-string 1 cfmt))))))))
14833 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
14835 (defun org-property-values (key)
14836 "Return a list of all values of property KEY in the current buffer."
14840 (goto-char (point-min))
14841 (let ((re (org-re-property key))
14843 (while (re-search-forward re nil t)
14844 (add-to-list 'values (org-trim (match-string 1))))
14845 (delete "" values)))))
14847 (defun org-insert-property-drawer ()
14848 "Insert a property drawer into the current entry."
14849 (org-back-to-heading t)
14850 (looking-at org-outline-regexp)
14851 (let ((indent (if org-adapt-indentation
14852 (- (match-end 0) (match-beginning 0))
14855 (re (concat "^[ \t]*" org-keyword-time-regexp))
14857 (outline-next-heading)
14860 (while (re-search-forward re end t))
14861 (setq hiddenp (outline-invisible-p))
14863 (and (equal (char-after) ?\n) (forward-char 1))
14864 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
14865 (if (member (match-string 1) '("CLOCK:" ":END:"))
14866 ;; just skip this line
14867 (beginning-of-line 2)
14868 ;; Drawer start, find the end
14869 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
14870 (beginning-of-line 1)))
14871 (org-skip-over-state-notes)
14872 (skip-chars-backward " \t\n\r")
14873 (if (eq (char-before) ?*) (forward-char 1))
14874 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
14875 (beginning-of-line 0)
14876 (org-indent-to-column indent)
14877 (beginning-of-line 2)
14878 (org-indent-to-column indent)
14879 (beginning-of-line 0)
14882 (org-back-to-heading t)
14884 (org-flag-drawer t))))
14886 (defun org-insert-drawer (&optional arg drawer)
14887 "Insert a drawer at point.
14889 Optional argument DRAWER, when non-nil, is a string representing
14890 drawer's name. Otherwise, the user is prompted for a name.
14892 If a region is active, insert the drawer around that region
14895 Point is left between drawer's boundaries."
14897 (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
14899 ;; SYSTEM-DRAWERS is a list of drawer names that are used
14900 ;; internally by Org. They are meant to be inserted
14902 (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
14903 ;; Remove system drawers from list. Note: For some reason,
14904 ;; `org-completing-read' ignores the predicate while
14905 ;; `completing-read' handles it fine.
14906 (drawer (if arg "PROPERTIES"
14909 "Drawer: " org-drawers
14910 (lambda (d) (not (member d system-drawers))))))))
14912 ;; With C-u, fall back on `org-insert-property-drawer'
14913 (arg (org-insert-property-drawer))
14914 ;; With an active region, insert a drawer at point.
14915 ((not (org-region-active-p))
14917 (unless (bolp) (insert "\n"))
14918 (insert (format ":%s:\n\n:END:\n" drawer))
14919 (forward-line -2)))
14920 ;; Otherwise, insert the drawer at point
14922 (let ((rbeg (region-beginning))
14923 (rend (copy-marker (region-end))))
14927 (beginning-of-line)
14928 (when (save-excursion
14929 (re-search-forward org-outline-regexp-bol rend t))
14930 (error "Drawers cannot contain headlines"))
14931 ;; Position point at the beginning of the first
14932 ;; non-blank line in region. Insert drawer's opening
14933 ;; there, then indent it.
14934 (org-skip-whitespace)
14935 (beginning-of-line)
14936 (insert ":" drawer ":\n")
14938 (indent-for-tab-command)
14939 ;; Move point to the beginning of the first blank line
14940 ;; after the last non-blank line in region. Insert
14941 ;; drawer's closing, then indent it.
14943 (skip-chars-backward " \r\t\n")
14945 (indent-for-tab-command)
14946 (unless (eolp) (insert "\n")))
14947 ;; Clear marker, whatever the outcome of insertion is.
14948 (set-marker rend nil)))))))
14950 (defvar org-property-set-functions-alist nil
14951 "Property set function alist.
14952 Each entry should have the following format:
14954 (PROPERTY . READ-FUNCTION)
14956 The read function will be called with the same argument as
14957 `org-completing-read'.")
14959 (defun org-set-property-function (property)
14960 "Get the function that should be used to set PROPERTY.
14961 This is computed according to `org-property-set-functions-alist'."
14962 (or (cdr (assoc property org-property-set-functions-alist))
14963 'org-completing-read))
14965 (defun org-read-property-value (property)
14966 "Read PROPERTY value from user."
14967 (let* ((completion-ignore-case t)
14968 (allowed (org-property-get-allowed-values nil property 'table))
14969 (cur (org-entry-get nil property))
14970 (prompt (concat property " value"
14971 (if (and cur (string-match "\\S-" cur))
14972 (concat " [" cur "]") "") ": "))
14973 (set-function (org-set-property-function property))
14975 (funcall set-function prompt allowed nil
14976 (not (get-text-property 0 'org-unrestricted
14978 (let (org-completion-use-ido org-completion-use-iswitchb)
14979 (funcall set-function prompt
14980 (mapcar 'list (org-property-values property))
14981 nil nil "" nil cur)))))
14986 (defvar org-last-set-property nil)
14987 (defun org-read-property-name ()
14988 "Read a property name."
14989 (let* ((completion-ignore-case t)
14990 (keys (org-buffer-property-keys nil t t))
14991 (default-prop (or (save-excursion
14993 (beginning-of-line)
14994 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
14995 (null (string= (match-string 1) "END"))
14996 (match-string 1))))
14997 org-last-set-property))
14998 (property (org-icompleting-read
15000 (if default-prop (concat " [" default-prop "]") "")
15002 (mapcar 'list keys)
15006 (if (member property keys)
15008 (or (cdr (assoc (downcase property)
15009 (mapcar (lambda (x) (cons (downcase x) x))
15013 (defun org-set-property (property value)
15014 "In the current entry, set PROPERTY to VALUE.
15015 When called interactively, this will prompt for a property name, offering
15016 completion on existing and default properties. And then it will prompt
15017 for a value, offering completion either on allowed values (via an inherited
15018 xxx_ALL property) or on existing values in other instances of this property
15019 in the current file."
15020 (interactive (list nil nil))
15021 (let* ((property (or property (org-read-property-name)))
15022 (value (or value (org-read-property-value property)))
15023 (fn (cdr (assoc property org-properties-postprocess-alist))))
15024 (setq org-last-set-property property)
15025 ;; Possibly postprocess the inserted value:
15026 (when fn (setq value (funcall fn value)))
15027 (unless (equal (org-entry-get nil property) value)
15028 (org-entry-put nil property value))))
15030 (defun org-delete-property (property)
15031 "In the current entry, delete PROPERTY."
15033 (let* ((completion-ignore-case t)
15034 (prop (org-icompleting-read "Property: "
15035 (org-entry-properties nil 'standard))))
15037 (message "Property %s %s" property
15038 (if (org-entry-delete nil property)
15040 "was not present in the entry")))
15042 (defun org-delete-property-globally (property)
15043 "Remove PROPERTY globally, from all entries."
15045 (let* ((completion-ignore-case t)
15046 (prop (org-icompleting-read
15047 "Globally remove property: "
15048 (mapcar 'list (org-buffer-property-keys)))))
15053 (goto-char (point-min))
15055 (while (re-search-forward
15056 (org-re-property property)
15058 (setq cnt (1+ cnt))
15059 (delete-region (match-beginning 0) (1+ (point-at-eol))))
15060 (message "Property \"%s\" removed from %d entries" property cnt)))))
15062 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
15064 (defun org-compute-property-at-point ()
15065 "Compute the property at point.
15066 This looks for an enclosing column format, extracts the operator and
15067 then applies it to the property in the column format's scope."
15069 (unless (org-at-property-p)
15070 (error "Not at a property"))
15071 (let ((prop (org-match-string-no-properties 2)))
15072 (org-columns-get-format-and-top-level)
15073 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
15074 (error "No operator defined for property %s" prop))
15075 (org-columns-compute prop)))
15077 (defvar org-property-allowed-value-functions nil
15078 "Hook for functions supplying allowed values for a specific property.
15079 The functions must take a single argument, the name of the property, and
15080 return a flat list of allowed values. If \":ETC\" is one of
15081 the values, this means that these values are intended as defaults for
15082 completion, but that other values should be allowed too.
15083 The functions must return nil if they are not responsible for this
15086 (defun org-property-get-allowed-values (pom property &optional table)
15087 "Get allowed values for the property PROPERTY.
15088 When TABLE is non-nil, return an alist that can directly be used for
15092 ((equal property "TODO")
15093 (setq vals (org-with-point-at pom
15094 (append org-todo-keywords-1 '("")))))
15095 ((equal property "PRIORITY")
15096 (let ((n org-lowest-priority))
15097 (while (>= n org-highest-priority)
15098 (push (char-to-string n) vals)
15100 ((member property org-special-properties))
15101 ((setq vals (run-hook-with-args-until-success
15102 'org-property-allowed-value-functions property)))
15104 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
15105 (when (and vals (string-match "\\S-" vals))
15106 (setq vals (car (read-from-string (concat "(" vals ")"))))
15107 (setq vals (mapcar (lambda (x)
15108 (cond ((stringp x) x)
15109 ((numberp x) (number-to-string x))
15110 ((symbolp x) (symbol-name x))
15113 (when (member ":ETC" vals)
15114 (setq vals (remove ":ETC" vals))
15115 (org-add-props (car vals) '(org-unrestricted t)))
15116 (if table (mapcar 'list vals) vals)))
15118 (defun org-property-previous-allowed-value (&optional previous)
15119 "Switch to the next allowed value for this property."
15121 (org-property-next-allowed-value t))
15123 (defun org-property-next-allowed-value (&optional previous)
15124 "Switch to the next allowed value for this property."
15126 (unless (org-at-property-p)
15127 (error "Not at a property"))
15128 (let* ((key (match-string 2))
15129 (value (match-string 3))
15130 (allowed (or (org-property-get-allowed-values (point) key)
15131 (and (member value '("[ ]" "[-]" "[X]"))
15135 (error "Allowed values for this property have not been defined"))
15136 (if previous (setq allowed (reverse allowed)))
15137 (if (member value allowed)
15138 (setq nval (car (cdr (member value allowed)))))
15139 (setq nval (or nval (car allowed)))
15140 (if (equal nval value)
15141 (error "Only one allowed value for this property"))
15142 (org-at-property-p)
15143 (replace-match (concat " :" key ": " nval) t t)
15145 (beginning-of-line 1)
15146 (skip-chars-forward " \t")
15147 (run-hook-with-args 'org-property-changed-functions key nval)))
15149 (defun org-find-olp (path &optional this-buffer)
15150 "Return a marker pointing to the entry at outline path OLP.
15151 If anything goes wrong, throw an error.
15152 You can wrap this call to catch the error like this:
15154 (condition-case msg
15155 (org-mobile-locate-entry (match-string 4))
15156 (error (nth 1 msg)))
15158 The return value will then be either a string with the error message,
15159 or a marker if everything is OK.
15161 If THIS-BUFFER is set, the outline path does not contain a file,
15163 (let* ((file (if this-buffer buffer-file-name (pop path)))
15164 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
15168 limit re end found pos heading cnt flevel)
15169 (unless buffer (error "File not found :%s" file))
15170 (with-current-buffer buffer
15174 (setq limit (point-max))
15175 (goto-char (point-min))
15176 (while (setq heading (pop path))
15177 (setq re (format org-complex-heading-regexp-format
15178 (regexp-quote heading)))
15179 (setq cnt 0 pos (point))
15180 (while (re-search-forward re end t)
15181 (setq level (- (match-end 1) (match-beginning 1)))
15182 (if (and (>= level lmin) (<= level lmax))
15183 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
15184 (when (= cnt 0) (error "Heading not found on level %d: %s"
15186 (when (> cnt 1) (error "Heading not unique on level %d: %s"
15189 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
15190 (setq end (save-excursion (org-end-of-subtree t t))))
15191 (when (org-at-heading-p)
15192 (move-marker (make-marker) (point))))))))
15194 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
15195 "Find node HEADING in BUFFER.
15196 Return a marker to the heading if it was found, or nil if not.
15197 If POS-ONLY is set, return just the position instead of a marker.
15199 The heading text must match exact, but it may have a TODO keyword,
15200 a priority cookie and tags in the standard locations."
15201 (with-current-buffer (or buffer (current-buffer))
15205 (goto-char (point-min))
15206 (let (case-fold-search)
15207 (if (re-search-forward
15208 (format org-complex-heading-regexp-format
15209 (regexp-quote heading)) nil t)
15211 (match-beginning 0)
15212 (move-marker (make-marker) (match-beginning 0)))))))))
15214 (defun org-find-exact-heading-in-directory (heading &optional dir)
15215 "Find Org node headline HEADING in all .org files in directory DIR.
15216 When the target headline is found, return a marker to this location."
15217 (let ((files (directory-files (or dir default-directory)
15218 nil "\\`[^.#].*\\.org\\'"))
15219 file visiting m buffer)
15221 (while (setq file (pop files))
15222 (message "trying %s" file)
15223 (setq visiting (org-find-base-buffer-visiting file))
15224 (setq buffer (or visiting (find-file-noselect file)))
15225 (setq m (org-find-exact-headline-in-buffer
15227 (when (and (not m) (not visiting)) (kill-buffer buffer))
15228 (and m (throw 'found m))))))
15230 (defun org-find-entry-with-id (ident)
15231 "Locate the entry that contains the ID property with exact value IDENT.
15232 IDENT can be a string, a symbol or a number, this function will search for
15233 the string representation of it.
15234 Return the position where this entry starts, or nil if there is no such entry."
15235 (interactive "sID: ")
15237 ((stringp ident) ident)
15238 ((symbol-name ident) (symbol-name ident))
15239 ((numberp ident) (number-to-string ident))
15240 (t (error "IDENT %s must be a string, symbol or number" ident))))
15241 (case-fold-search nil))
15245 (goto-char (point-min))
15246 (when (re-search-forward
15247 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
15249 (org-back-to-heading t)
15254 (defvar org-last-changed-timestamp nil)
15255 (defvar org-last-inserted-timestamp nil
15256 "The last time stamp inserted with `org-insert-time-stamp'.")
15257 (defvar org-time-was-given) ; dynamically scoped parameter
15258 (defvar org-end-time-was-given) ; dynamically scoped parameter
15259 (defvar org-ts-what) ; dynamically scoped parameter
15261 (defun org-time-stamp (arg &optional inactive)
15262 "Prompt for a date/time and insert a time stamp.
15263 If the user specifies a time like HH:MM or if this command is
15264 called with at least one prefix argument, the time stamp contains
15265 the date and the time. Otherwise, only the date is be included.
15267 All parts of a date not specified by the user is filled in from
15268 the current date/time. So if you just press return without
15269 typing anything, the time stamp will represent the current
15272 If there is already a timestamp at the cursor, it will be
15275 With two universal prefix arguments, insert an active timestamp
15276 with the current time without prompting the user."
15280 ;; Default time is either today, or, when entering a range,
15281 ;; the range start.
15282 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
15284 (re-search-backward
15285 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
15286 (- (point) 20) t)))
15287 (apply 'encode-time (org-parse-time-string (match-string 1)))
15289 (default-input (and ts (org-get-compact-tod ts)))
15290 (repeater (save-excursion
15292 (beginning-of-line)
15293 (when (re-search-forward
15294 "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
15295 (save-excursion (progn (end-of-line) (point))) t)
15296 (match-string 0)))))
15297 org-time-was-given org-end-time-was-given time)
15299 ((and (org-at-timestamp-p t)
15300 (memq last-command '(org-time-stamp org-time-stamp-inactive))
15301 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
15303 (setq time (let ((this-command this-command))
15304 (org-read-date arg 'totime nil nil
15305 default-time default-input inactive)))
15306 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
15307 ((org-at-timestamp-p t)
15308 (setq time (let ((this-command this-command))
15309 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15310 (when (org-at-timestamp-p t) ; just to get the match data
15311 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
15313 (setq org-last-changed-timestamp
15314 (org-insert-time-stamp
15315 time (or org-time-was-given arg)
15316 inactive nil nil (list org-end-time-was-given)))
15317 (when repeater (goto-char (1- (point))) (insert " " repeater)
15318 (setq org-last-changed-timestamp
15319 (concat (substring org-last-inserted-timestamp 0 -1)
15320 " " repeater ">"))))
15321 (message "Timestamp updated"))
15323 (org-insert-time-stamp (current-time) t))
15325 (setq time (let ((this-command this-command))
15326 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15327 (org-insert-time-stamp time (or org-time-was-given arg) inactive
15328 nil nil (list org-end-time-was-given))))))
15330 ;; FIXME: can we use this for something else, like computing time differences?
15331 (defun org-get-compact-tod (s)
15332 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
15333 (let* ((t1 (match-string 1 s))
15334 (h1 (string-to-number (match-string 2 s)))
15335 (m1 (string-to-number (match-string 3 s)))
15336 (t2 (and (match-end 4) (match-string 5 s)))
15337 (h2 (and t2 (string-to-number (match-string 6 s))))
15338 (m2 (and t2 (string-to-number (match-string 7 s))))
15342 (setq dh (- h2 h1) dm (- m2 m1))
15343 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
15344 (concat t1 "+" (number-to-string dh)
15345 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
15347 (defun org-time-stamp-inactive (&optional arg)
15348 "Insert an inactive time stamp.
15349 An inactive time stamp is enclosed in square brackets instead of angle
15350 brackets. It is inactive in the sense that it does not trigger agenda entries,
15351 does not link to the calendar and cannot be changed with the S-cursor keys.
15352 So these are more for recording a certain time/date."
15354 (org-time-stamp arg 'inactive))
15356 (defvar org-date-ovl (make-overlay 1 1))
15357 (overlay-put org-date-ovl 'face 'org-date-selected)
15358 (org-detach-overlay org-date-ovl)
15360 (defvar org-ans1) ; dynamically scoped parameter
15361 (defvar org-ans2) ; dynamically scoped parameter
15363 (defvar org-plain-time-of-day-regexp) ; defined below
15365 (defvar org-overriding-default-time nil) ; dynamically scoped
15366 (defvar org-read-date-overlay nil)
15367 (defvar org-dcst nil) ; dynamically scoped
15368 (defvar org-read-date-history nil)
15369 (defvar org-read-date-final-answer nil)
15370 (defvar org-read-date-analyze-futurep nil)
15371 (defvar org-read-date-analyze-forced-year nil)
15372 (defvar org-read-date-inactive)
15374 (defun org-read-date (&optional org-with-time to-time from-string prompt
15375 default-time default-input inactive)
15376 "Read a date, possibly a time, and make things smooth for the user.
15377 The prompt will suggest to enter an ISO date, but you can also enter anything
15378 which will at least partially be understood by `parse-time-string'.
15379 Unrecognized parts of the date will default to the current day, month, year,
15380 hour and minute. If this command is called to replace a timestamp at point,
15381 or to enter the second timestamp of a range, the default time is taken
15382 from the existing stamp. Furthermore, the command prefers the future,
15383 so if you are giving a date where the year is not given, and the day-month
15384 combination is already past in the current year, it will assume you
15385 mean next year. For details, see the manual. A few examples:
15387 3-2-5 --> 2003-02-05
15388 feb 15 --> currentyear-02-15
15389 2/15 --> currentyear-02-15
15390 sep 12 9 --> 2009-09-12
15391 12:45 --> today 12:45
15392 22 sept 0:34 --> currentyear-09-22 0:34
15393 12 --> currentyear-currentmonth-12
15394 Fri --> nearest Friday (today or later)
15397 Furthermore you can specify a relative date by giving, as the *first* thing
15398 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
15399 change in days weeks, months, years.
15400 With a single plus or minus, the date is relative to today. With a double
15401 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
15402 +4d --> four days from today
15403 +4 --> same as above
15404 +2w --> two weeks from today
15405 ++5 --> five days from default date
15407 The function understands only English month and weekday abbreviations.
15409 While prompting, a calendar is popped up - you can also select the
15410 date with the mouse (button 1). The calendar shows a period of three
15411 months. To scroll it to other months, use the keys `>' and `<'.
15412 If you don't like the calendar, turn it off with
15413 \(setq org-read-date-popup-calendar nil)
15415 With optional argument TO-TIME, the date will immediately be converted
15416 to an internal time.
15417 With an optional argument WITH-TIME, the prompt will suggest to also
15418 insert a time. Note that when WITH-TIME is not set, you can still
15419 enter a time, and this function will inform the calling routine about
15420 this change. The calling routine may then choose to change the format
15421 used to insert the time stamp into the buffer to include the time.
15422 With optional argument FROM-STRING, read from this string instead from
15423 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
15424 the time/date that is used for everything that is not specified by the
15426 (require 'parse-time)
15427 (let* ((org-time-stamp-rounding-minutes
15428 (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
15429 (org-dcst org-display-custom-times)
15430 (ct (org-current-time))
15431 (org-def (or org-overriding-default-time default-time ct))
15432 (org-defdecode (decode-time org-def))
15434 (when (< (nth 2 org-defdecode) org-extend-today-until)
15435 (setcar (nthcdr 2 org-defdecode) -1)
15436 (setcar (nthcdr 1 org-defdecode) 59)
15437 (setq org-def (apply 'encode-time org-defdecode)
15438 org-defdecode (decode-time org-def)))))
15439 (calendar-frame-setup nil)
15440 (calendar-setup nil)
15441 (calendar-move-hook nil)
15442 (calendar-view-diary-initially-flag nil)
15443 (calendar-view-holidays-initially-flag nil)
15444 (timestr (format-time-string
15445 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
15446 (prompt (concat (if prompt (concat prompt " ") "")
15447 (format "Date+time [%s]: " timestr)))
15448 ans (org-ans0 "") org-ans1 org-ans2 final)
15451 (from-string (setq ans from-string))
15452 (org-read-date-popup-calendar
15454 (save-window-excursion
15456 (org-eval-in-calendar '(setq cursor-type nil) t)
15459 (calendar-forward-day (- (time-to-days org-def)
15460 (calendar-absolute-from-gregorian
15461 (calendar-current-date))))
15462 (org-eval-in-calendar nil t)
15463 (let* ((old-map (current-local-map))
15464 (map (copy-keymap calendar-mode-map))
15465 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
15466 (org-defkey map (kbd "RET") 'org-calendar-select)
15467 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
15468 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
15469 (org-defkey minibuffer-local-map [(meta shift left)]
15470 (lambda () (interactive)
15471 (org-eval-in-calendar '(calendar-backward-month 1))))
15472 (org-defkey minibuffer-local-map [(meta shift right)]
15473 (lambda () (interactive)
15474 (org-eval-in-calendar '(calendar-forward-month 1))))
15475 (org-defkey minibuffer-local-map [(meta shift up)]
15476 (lambda () (interactive)
15477 (org-eval-in-calendar '(calendar-backward-year 1))))
15478 (org-defkey minibuffer-local-map [(meta shift down)]
15479 (lambda () (interactive)
15480 (org-eval-in-calendar '(calendar-forward-year 1))))
15481 (org-defkey minibuffer-local-map [?\e (shift left)]
15482 (lambda () (interactive)
15483 (org-eval-in-calendar '(calendar-backward-month 1))))
15484 (org-defkey minibuffer-local-map [?\e (shift right)]
15485 (lambda () (interactive)
15486 (org-eval-in-calendar '(calendar-forward-month 1))))
15487 (org-defkey minibuffer-local-map [?\e (shift up)]
15488 (lambda () (interactive)
15489 (org-eval-in-calendar '(calendar-backward-year 1))))
15490 (org-defkey minibuffer-local-map [?\e (shift down)]
15491 (lambda () (interactive)
15492 (org-eval-in-calendar '(calendar-forward-year 1))))
15493 (org-defkey minibuffer-local-map [(shift up)]
15494 (lambda () (interactive)
15495 (org-eval-in-calendar '(calendar-backward-week 1))))
15496 (org-defkey minibuffer-local-map [(shift down)]
15497 (lambda () (interactive)
15498 (org-eval-in-calendar '(calendar-forward-week 1))))
15499 (org-defkey minibuffer-local-map [(shift left)]
15500 (lambda () (interactive)
15501 (org-eval-in-calendar '(calendar-backward-day 1))))
15502 (org-defkey minibuffer-local-map [(shift right)]
15503 (lambda () (interactive)
15504 (org-eval-in-calendar '(calendar-forward-day 1))))
15505 (org-defkey minibuffer-local-map ">"
15506 (lambda () (interactive)
15507 (org-eval-in-calendar '(scroll-calendar-left 1))))
15508 (org-defkey minibuffer-local-map "<"
15509 (lambda () (interactive)
15510 (org-eval-in-calendar '(scroll-calendar-right 1))))
15511 (org-defkey minibuffer-local-map "\C-v"
15512 (lambda () (interactive)
15513 (org-eval-in-calendar
15514 '(calendar-scroll-left-three-months 1))))
15515 (org-defkey minibuffer-local-map "\M-v"
15516 (lambda () (interactive)
15517 (org-eval-in-calendar
15518 '(calendar-scroll-right-three-months 1))))
15519 (run-hooks 'org-read-date-minibuffer-setup-hook)
15522 (use-local-map map)
15523 (setq org-read-date-inactive inactive)
15524 (add-hook 'post-command-hook 'org-read-date-display)
15525 (setq org-ans0 (read-string prompt default-input
15526 'org-read-date-history nil))
15527 ;; org-ans0: from prompt
15528 ;; org-ans1: from mouse click
15529 ;; org-ans2: from calendar motion
15530 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
15531 (remove-hook 'post-command-hook 'org-read-date-display)
15532 (use-local-map old-map)
15533 (when org-read-date-overlay
15534 (delete-overlay org-read-date-overlay)
15535 (setq org-read-date-overlay nil)))))
15536 (bury-buffer "*Calendar*")))))
15538 (t ; Naked prompt only
15540 (setq ans (read-string prompt default-input
15541 'org-read-date-history timestr))
15542 (when org-read-date-overlay
15543 (delete-overlay org-read-date-overlay)
15544 (setq org-read-date-overlay nil)))))
15546 (setq final (org-read-date-analyze ans org-def org-defdecode))
15548 (when org-read-date-analyze-forced-year
15549 (message "Year was forced into %s"
15550 (if org-read-date-force-compatible-dates
15551 "compatible range (1970-2037)"
15552 "range representable on this machine"))
15555 ;; One round trip to get rid of 34th of August and stuff like that....
15556 (setq final (decode-time (apply 'encode-time final)))
15558 (setq org-read-date-final-answer ans)
15561 (apply 'encode-time final)
15562 (if (and (boundp 'org-time-was-given) org-time-was-given)
15563 (format "%04d-%02d-%02d %02d:%02d"
15564 (nth 5 final) (nth 4 final) (nth 3 final)
15565 (nth 2 final) (nth 1 final))
15566 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
15569 (defvar org-defdecode)
15570 (defvar org-with-time)
15571 (defun org-read-date-display ()
15572 "Display the current date prompt interpretation in the minibuffer."
15573 (when org-read-date-display-live
15574 (when org-read-date-overlay
15575 (delete-overlay org-read-date-overlay))
15576 (when (minibufferp (current-buffer))
15579 (while (not (equal (buffer-substring
15580 (max (point-min) (- (point) 4)) (point))
15583 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
15584 " " (or org-ans1 org-ans2)))
15585 (org-end-time-was-given nil)
15586 (f (org-read-date-analyze ans org-def org-defdecode))
15588 org-time-stamp-custom-formats
15589 org-time-stamp-formats))
15590 (fmt (if (or org-with-time
15591 (and (boundp 'org-time-was-given) org-time-was-given))
15594 (txt (format-time-string fmt (apply 'encode-time f)))
15595 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
15596 (txt (concat "=> " txt)))
15597 (when (and org-end-time-was-given
15598 (string-match org-plain-time-of-day-regexp txt))
15599 (setq txt (concat (substring txt 0 (match-end 0)) "-"
15600 org-end-time-was-given
15601 (substring txt (match-end 0)))))
15602 (when org-read-date-analyze-futurep
15603 (setq txt (concat txt " (=>F)")))
15604 (setq org-read-date-overlay
15605 (make-overlay (1- (point-at-eol)) (point-at-eol)))
15606 (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
15608 (defun org-read-date-analyze (ans org-def org-defdecode)
15609 "Analyze the combined answer of the date prompt."
15610 ;; FIXME: cleanup and comment
15611 (let ((nowdecode (decode-time (current-time)))
15612 delta deltan deltaw deltadef year month day
15613 hour minute second wday pm h2 m2 tl wday1
15614 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
15615 (setq org-read-date-analyze-futurep nil
15616 org-read-date-analyze-forced-year nil)
15617 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
15620 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
15621 (unless (save-match-data (string-match org-plain-time-of-day-regexp ans))
15622 (setq ans (replace-match "" t t ans)
15624 deltaw (nth 1 delta)
15625 deltadef (nth 2 delta))))
15627 ;; Check if there is an iso week date in there
15628 ;; If yes, store the info and postpone interpreting it until the rest
15629 ;; of the parsing is done
15630 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
15631 (setq iso-year (if (match-end 1)
15632 (org-small-year-to-year
15633 (string-to-number (match-string 1 ans))))
15634 iso-weekday (if (match-end 3)
15635 (string-to-number (match-string 3 ans)))
15636 iso-week (string-to-number (match-string 2 ans)))
15637 (setq ans (replace-match "" t t ans)))
15639 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
15640 (when (string-match
15641 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
15642 (setq year (if (match-end 2)
15643 (string-to-number (match-string 2 ans))
15644 (progn (setq kill-year t)
15645 (string-to-number (format-time-string "%Y"))))
15646 month (string-to-number (match-string 3 ans))
15647 day (string-to-number (match-string 4 ans)))
15648 (if (< year 100) (setq year (+ 2000 year)))
15649 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15652 ;; Help matching dotted european dates
15653 (when (string-match
15654 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
15655 (setq year (if (match-end 3)
15656 (string-to-number (match-string 3 ans))
15657 (progn (setq kill-year t)
15658 (string-to-number (format-time-string "%Y"))))
15659 day (string-to-number (match-string 1 ans))
15660 month (string-to-number (match-string 2 ans))
15661 ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15664 ;; Help matching american dates, like 5/30 or 5/30/7
15665 (when (string-match
15666 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
15667 (setq year (if (match-end 4)
15668 (string-to-number (match-string 4 ans))
15669 (progn (setq kill-year t)
15670 (string-to-number (format-time-string "%Y"))))
15671 month (string-to-number (match-string 1 ans))
15672 day (string-to-number (match-string 2 ans)))
15673 (if (< year 100) (setq year (+ 2000 year)))
15674 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15676 ;; Help matching am/pm times, because `parse-time-string' does not do that.
15677 ;; If there is a time with am/pm, and *no* time without it, we convert
15678 ;; so that matching will be successful.
15679 (loop for i from 1 to 2 do ; twice, for end time as well
15680 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
15681 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
15682 (setq hour (string-to-number (match-string 1 ans))
15683 minute (if (match-end 3)
15684 (string-to-number (match-string 3 ans))
15687 (string-to-char (downcase (match-string 4 ans)))))
15688 (if (and (= hour 12) (not pm))
15690 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
15691 (setq ans (replace-match (format "%02d:%02d" hour minute)
15694 ;; Check if a time range is given as a duration
15695 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
15696 (setq hour (string-to-number (match-string 1 ans))
15697 h2 (+ hour (string-to-number (match-string 3 ans)))
15698 minute (string-to-number (match-string 2 ans))
15699 m2 (+ minute (if (match-end 5) (string-to-number
15700 (match-string 5 ans))0)))
15701 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
15702 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
15705 ;; Check if there is a time range
15706 (when (boundp 'org-end-time-was-given)
15707 (setq org-time-was-given nil)
15708 (when (and (string-match org-plain-time-of-day-regexp ans)
15710 (setq org-end-time-was-given (match-string 8 ans))
15711 (setq ans (concat (substring ans 0 (match-beginning 7))
15712 (substring ans (match-end 7))))))
15714 (setq tl (parse-time-string ans)
15715 day (or (nth 3 tl) (nth 3 org-defdecode))
15716 month (or (nth 4 tl)
15717 (if (and org-read-date-prefer-future
15718 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
15719 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
15720 (nth 4 org-defdecode)))
15721 year (or (and (not kill-year) (nth 5 tl))
15722 (if (and org-read-date-prefer-future
15723 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
15724 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
15725 (nth 5 org-defdecode)))
15726 hour (or (nth 2 tl) (nth 2 org-defdecode))
15727 minute (or (nth 1 tl) (nth 1 org-defdecode))
15728 second (or (nth 0 tl) 0)
15731 (when (and (eq org-read-date-prefer-future 'time)
15732 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
15733 (equal day (nth 3 nowdecode))
15734 (equal month (nth 4 nowdecode))
15735 (equal year (nth 5 nowdecode))
15737 (or (< (nth 2 tl) (nth 2 nowdecode))
15738 (and (= (nth 2 tl) (nth 2 nowdecode))
15740 (< (nth 1 tl) (nth 1 nowdecode)))))
15744 ;; Special date definitions below
15747 ;; There was an iso week
15750 (setq year (or iso-year year)
15751 day (or iso-weekday wday 1)
15752 wday nil ; to make sure that the trigger below does not match
15753 iso-date (calendar-gregorian-from-absolute
15754 (calendar-absolute-from-iso
15755 (list iso-week day year))))
15756 ; FIXME: Should we also push ISO weeks into the future?
15757 ; (when (and org-read-date-prefer-future
15759 ; (< (calendar-absolute-from-gregorian iso-date)
15760 ; (time-to-days (current-time))))
15761 ; (setq year (1+ year)
15762 ; iso-date (calendar-gregorian-from-absolute
15763 ; (calendar-absolute-from-iso
15764 ; (list iso-week day year)))))
15765 (setq month (car iso-date)
15766 year (nth 2 iso-date)
15767 day (nth 1 iso-date)))
15771 (let ((now (decode-time (current-time))))
15772 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
15773 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
15774 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
15775 ((equal deltaw "m") (setq month (+ month deltan)))
15776 ((equal deltaw "y") (setq year (+ year deltan)))))
15777 ((and wday (not (nth 3 tl)))
15778 ;; Weekday was given, but no day, so pick that day in the week
15779 ;; on or after the derived date.
15780 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
15781 (unless (equal wday wday1)
15782 (setq day (+ day (% (- wday wday1 -7) 7))))))
15783 (if (and (boundp 'org-time-was-given)
15785 (setq org-time-was-given t))
15786 (if (< year 100) (setq year (+ 2000 year)))
15787 ;; Check of the date is representable
15788 (if org-read-date-force-compatible-dates
15791 (setq year 1970 org-read-date-analyze-forced-year t))
15793 (setq year 2037 org-read-date-analyze-forced-year t)))
15794 (condition-case nil
15795 (ignore (encode-time second minute hour day month year))
15797 (setq year (nth 5 org-defdecode))
15798 (setq org-read-date-analyze-forced-year t))))
15799 (setq org-read-date-analyze-futurep futurep)
15800 (list second minute hour day month year)))
15802 (defvar parse-time-weekdays)
15803 (defun org-read-date-get-relative (s today default)
15804 "Check string S for special relative date string.
15805 TODAY and DEFAULT are internal times, for today and for a default.
15806 Return shift list (N what def-flag)
15807 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
15808 N is the number of WHATs to shift.
15809 DEF-FLAG is t when a double ++ or -- indicates shift relative to
15810 the DEFAULT date rather than TODAY."
15811 (require 'parse-time)
15815 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
15817 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
15818 "\\([ \t]\\|$\\)") s)
15819 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
15820 (let* ((dir (if (> (match-end 1) (match-beginning 1))
15821 (string-to-char (substring (match-string 1 s) -1))
15823 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
15824 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
15825 (what (if (match-end 3) (match-string 3 s) "d"))
15826 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
15827 (date (if rel default today))
15828 (wday (nth 6 (decode-time date)))
15832 (setq delta (mod (+ 7 (- wday1 wday)) 7))
15833 (if (= dir ?-) (setq delta (- delta 7)))
15834 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
15835 (list delta "d" rel))
15836 (list (* n (if (= dir ?-) -1 1)) what rel)))))
15838 (defun org-order-calendar-date-args (arg1 arg2 arg3)
15839 "Turn a user-specified date into the internal representation.
15840 The internal representation needed by the calendar is (month day year).
15841 This is a wrapper to handle the brain-dead convention in calendar that
15842 user function argument order change dependent on argument order."
15843 (if (boundp 'calendar-date-style)
15845 ((eq calendar-date-style 'american)
15846 (list arg1 arg2 arg3))
15847 ((eq calendar-date-style 'european)
15848 (list arg2 arg1 arg3))
15849 ((eq calendar-date-style 'iso)
15850 (list arg2 arg3 arg1)))
15851 (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
15852 (if (org-bound-and-true-p european-calendar-style)
15853 (list arg2 arg1 arg3)
15854 (list arg1 arg2 arg3)))))
15856 (defun org-eval-in-calendar (form &optional keepdate)
15857 "Eval FORM in the calendar window and return to current window.
15858 When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
15859 otherwise stick to the current value of `org-ans2'."
15860 (let ((sf (selected-frame))
15861 (sw (selected-window)))
15862 (select-window (get-buffer-window "*Calendar*" t))
15864 (when (and (not keepdate) (calendar-cursor-to-date))
15865 (let* ((date (calendar-cursor-to-date))
15866 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15867 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
15868 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
15870 (org-select-frame-set-input-focus sf)))
15872 (defun org-calendar-select ()
15873 "Return to `org-read-date' with the date currently selected.
15874 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15876 (when (calendar-cursor-to-date)
15877 (let* ((date (calendar-cursor-to-date))
15878 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15879 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15880 (if (active-minibuffer-window) (exit-minibuffer))))
15882 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
15883 "Insert a date stamp for the date given by the internal TIME.
15884 WITH-HM means use the stamp format that includes the time of the day.
15885 INACTIVE means use square brackets instead of angular ones, so that the
15886 stamp will not contribute to the agenda.
15887 PRE and POST are optional strings to be inserted before and after the
15889 The command returns the inserted time stamp."
15890 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
15892 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
15893 (insert-before-markers (or pre ""))
15894 (when (listp extra)
15895 (setq extra (car extra))
15896 (if (and (stringp extra)
15897 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
15898 (setq extra (format "-%02d:%02d"
15899 (string-to-number (match-string 1 extra))
15900 (string-to-number (match-string 2 extra))))
15903 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
15904 (insert-before-markers (setq stamp (format-time-string fmt time)))
15905 (insert-before-markers (or post ""))
15906 (setq org-last-inserted-timestamp stamp)))
15908 (defun org-toggle-time-stamp-overlays ()
15909 "Toggle the use of custom time stamp formats."
15911 (setq org-display-custom-times (not org-display-custom-times))
15912 (unless org-display-custom-times
15913 (let ((p (point-min)) (bmp (buffer-modified-p)))
15914 (while (setq p (next-single-property-change p 'display))
15915 (if (and (get-text-property p 'display)
15916 (eq (get-text-property p 'face) 'org-date))
15917 (remove-text-properties
15918 p (setq p (next-single-property-change p 'display))
15920 (set-buffer-modified-p bmp)))
15921 (if (featurep 'xemacs)
15922 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
15923 (org-restart-font-lock)
15924 (setq org-table-may-need-update t)
15925 (if org-display-custom-times
15926 (message "Time stamps are overlaid with custom format")
15927 (message "Time stamp overlays removed")))
15929 (defun org-display-custom-time (beg end)
15930 "Overlay modified time stamp format over timestamp between BEG and END."
15931 (let* ((ts (buffer-substring beg end))
15932 t1 w1 with-hm tf time str w2 (off 0))
15934 (setq t1 (org-parse-time-string ts t))
15935 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
15936 (setq off (- (match-end 0) (match-beginning 0)))))
15937 (setq end (- end off))
15938 (setq w1 (- end beg)
15939 with-hm (and (nth 1 t1) (nth 2 t1))
15940 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
15941 time (org-fix-decoded-time t1)
15943 (format-time-string
15944 (substring tf 1 -1) (apply 'encode-time time))
15945 nil 'mouse-face 'highlight)
15947 (if (not (= w2 w1))
15948 (add-text-properties (1+ beg) (+ 2 beg)
15949 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
15950 (if (featurep 'xemacs)
15952 (put-text-property beg end 'invisible t)
15953 (put-text-property beg end 'end-glyph (make-glyph str)))
15954 (put-text-property beg end 'display str))))
15956 (defun org-translate-time (string)
15957 "Translate all timestamps in STRING to custom format.
15958 But do this only if the variable `org-display-custom-times' is set."
15959 (when org-display-custom-times
15962 (re org-ts-regexp-both)
15963 t1 with-hm inactive tf time str beg end)
15964 (while (setq start (string-match re string start))
15965 (setq beg (match-beginning 0)
15967 t1 (save-match-data
15968 (org-parse-time-string (substring string beg end) t))
15969 with-hm (and (nth 1 t1) (nth 2 t1))
15970 inactive (equal (substring string beg (1+ beg)) "[")
15971 tf (funcall (if with-hm 'cdr 'car)
15972 org-time-stamp-custom-formats)
15973 time (org-fix-decoded-time t1)
15974 str (format-time-string
15976 (if inactive "[" "<") (substring tf 1 -1)
15977 (if inactive "]" ">"))
15978 (apply 'encode-time time))
15979 string (replace-match str t t string)
15980 start (+ start (length str)))))))
15983 (defun org-fix-decoded-time (time)
15984 "Set 0 instead of nil for the first 6 elements of time.
15985 Don't touch the rest."
15987 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
15989 (defun org-days-to-time (timestamp-string)
15990 "Difference between TIMESTAMP-STRING and now in days."
15991 (- (time-to-days (org-time-string-to-time timestamp-string))
15992 (time-to-days (current-time))))
15994 (defun org-deadline-close (timestamp-string &optional ndays)
15995 "Is the time in TIMESTAMP-STRING close to the current date?"
15996 (setq ndays (or ndays (org-get-wdays timestamp-string)))
15997 (and (< (org-days-to-time timestamp-string) ndays)
15998 (not (org-entry-is-done-p))))
16000 (defun org-get-wdays (ts)
16001 "Get the deadline lead time appropriate for timestring TS."
16003 ((<= org-deadline-warning-days 0)
16004 ;; 0 or negative, enforce this value no matter what
16005 (- org-deadline-warning-days))
16006 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
16007 ;; lead time is specified.
16008 (floor (* (string-to-number (match-string 1 ts))
16009 (cdr (assoc (match-string 2 ts)
16010 '(("d" . 1) ("w" . 7)
16011 ("m" . 30.4) ("y" . 365.25)))))))
16012 ;; go for the default.
16013 (t org-deadline-warning-days)))
16015 (defun org-calendar-select-mouse (ev)
16016 "Return to `org-read-date' with the date currently selected.
16017 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
16019 (mouse-set-point ev)
16020 (when (calendar-cursor-to-date)
16021 (let* ((date (calendar-cursor-to-date))
16022 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16023 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
16024 (if (active-minibuffer-window) (exit-minibuffer))))
16026 (defun org-check-deadlines (ndays)
16027 "Check if there are any deadlines due or past due.
16028 A deadline is considered due if it happens within `org-deadline-warning-days'
16029 days from today's date. If the deadline appears in an entry marked DONE,
16030 it is not shown. The prefix arg NDAYS can be used to test that many
16031 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
16033 (let* ((org-warn-days
16035 ((equal ndays '(4)) 100000)
16036 (ndays (prefix-numeric-value ndays))
16037 (t (abs org-deadline-warning-days))))
16038 (case-fold-search nil)
16039 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
16041 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
16043 (message "%d deadlines past-due or due within %d days"
16044 (org-occur regexp nil callback)
16047 (defsubst org-re-timestamp (type)
16048 "Return a regexp for timestamp TYPE.
16049 Allowed values for TYPE are:
16051 all: all timestamps
16052 active: only active timestamps (<...>)
16053 inactive: only inactive timestamps ([...])
16054 scheduled: only scheduled timestamps
16055 deadline: only deadline timestamps
16057 When TYPE is nil, fall back on returning a regexp that matches
16058 both scheduled and deadline timestamps."
16059 (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9>
\n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
16060 ((eq type 'active) org-ts-regexp)
16061 ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^
\n>]*?\\)\\]")
16062 ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
16063 ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
16064 ((eq type 'scheduled-or-deadline)
16065 (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
16067 (defun org-check-before-date (date)
16068 "Check if there are deadlines or scheduled entries before DATE."
16069 (interactive (list (org-read-date)))
16070 (let ((case-fold-search nil)
16071 (regexp (org-re-timestamp org-ts-type))
16073 (lambda () (time-less-p
16074 (org-time-string-to-time (match-string 1))
16075 (org-time-string-to-time date)))))
16076 (message "%d entries before %s"
16077 (org-occur regexp nil callback) date)))
16079 (defun org-check-after-date (date)
16080 "Check if there are deadlines or scheduled entries after DATE."
16081 (interactive (list (org-read-date)))
16082 (let ((case-fold-search nil)
16083 (regexp (org-re-timestamp org-ts-type))
16087 (org-time-string-to-time (match-string 1))
16088 (org-time-string-to-time date))))))
16089 (message "%d entries after %s"
16090 (org-occur regexp nil callback) date)))
16092 (defun org-check-dates-range (start-date end-date)
16093 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
16094 (interactive (list (org-read-date nil nil nil "Range starts")
16095 (org-read-date nil nil nil "Range end")))
16096 (let ((case-fold-search nil)
16097 (regexp (org-re-timestamp org-ts-type))
16100 (let ((match (match-string 1)))
16103 (org-time-string-to-time match)
16104 (org-time-string-to-time start-date)))
16106 (org-time-string-to-time match)
16107 (org-time-string-to-time end-date)))))))
16108 (message "%d entries between %s and %s"
16109 (org-occur regexp nil callback) start-date end-date)))
16111 (defun org-evaluate-time-range (&optional to-buffer)
16112 "Evaluate a time range by computing the difference between start and end.
16113 Normally the result is just printed in the echo area, but with prefix arg
16114 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
16115 If the time range is actually in a table, the result is inserted into the
16117 For time difference computation, a year is assumed to be exactly 365
16118 days in order to avoid rounding problems."
16121 (org-clock-update-time-maybe)
16123 (unless (org-at-date-range-p t)
16124 (goto-char (point-at-bol))
16125 (re-search-forward org-tr-regexp-both (point-at-eol) t))
16126 (if (not (org-at-date-range-p t))
16127 (error "Not at a time-stamp range, and none found in current line")))
16128 (let* ((ts1 (match-string 1))
16129 (ts2 (match-string 2))
16130 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
16131 (match-end (match-end 0))
16132 (time1 (org-time-string-to-time ts1))
16133 (time2 (org-time-string-to-time ts2))
16134 (t1 (org-float-time time1))
16135 (t2 (org-float-time time2))
16136 (diff (abs (- t2 t1)))
16137 (negative (< (- t2 t1) 0))
16138 ;; (ys (floor (* 365 24 60 60)))
16141 (fy "%dy %dd %02d:%02d")
16143 (fd "%dd %02d:%02d")
16148 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16150 d (floor (/ diff ds)) diff (mod diff ds)
16151 h (floor (/ diff hs)) diff (mod diff hs)
16152 m (floor (/ diff 60)))
16153 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16155 d (floor (+ (/ diff ds) 0.5))
16157 (if (not to-buffer)
16158 (message "%s" (org-make-tdiff-string y d h m))
16159 (if (org-at-table-p)
16161 (goto-char match-end)
16163 (and (looking-at " *|") (goto-char (match-end 0))))
16164 (goto-char match-end))
16166 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
16167 (replace-match ""))
16168 (if negative (insert " -"))
16169 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
16170 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
16171 (insert " " (format fh h m))))
16172 (if align (org-table-align))
16173 (message "Time difference inserted")))))
16175 (defun org-make-tdiff-string (y d h m)
16178 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
16180 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
16182 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
16184 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
16186 (apply 'format fmt (nreverse l))))
16188 (defun org-time-string-to-time (s &optional buffer pos)
16189 (condition-case errdata
16190 (apply 'encode-time (org-parse-time-string s))
16191 (error (error "Bad timestamp `%s'%s\nError was: %s"
16192 s (if (not (and buffer pos))
16194 (format " at %d in buffer `%s'" pos buffer))
16197 (defun org-time-string-to-seconds (s)
16198 (org-float-time (org-time-string-to-time s)))
16200 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
16201 "Convert a time stamp to an absolute day number.
16202 If there is a specifier for a cyclic time stamp, get the closest date to
16204 PREFER and SHOW-ALL are passed through to `org-closest-date'.
16205 The variable date is bound by the calendar when this is called."
16207 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
16208 (if (org-diary-sexp-entry (match-string 1 s) "" date)
16211 ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
16212 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
16213 (time-to-days (current-time))) (match-string 0 s)
16216 (condition-case errdata
16217 (apply 'encode-time (org-parse-time-string s))
16218 (error (error "Bad timestamp `%s'%s\nError was: %s"
16219 s (if (not (and buffer pos))
16221 (format " at %d in buffer `%s'" pos buffer))
16222 (cdr errdata))))))))
16224 (defun org-days-to-iso-week (days)
16225 "Return the iso week number."
16227 (car (calendar-iso-from-absolute days)))
16229 (defun org-small-year-to-year (year)
16230 "Convert 2-digit years into 4-digit years.
16231 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
16232 The year 2000 cannot be abbreviated. Any year larger than 99
16233 is returned unchanged."
16235 (setq year (+ 2000 year))
16237 (setq year (+ 1900 year))))
16240 (defun org-time-from-absolute (d)
16241 "Return the time corresponding to date D.
16242 D may be an absolute day number, or a calendar-type list (month day year)."
16243 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
16244 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
16246 (defun org-calendar-holiday ()
16247 "List of holidays, for Diary display in Org-mode."
16248 (require 'holidays)
16250 (if (fboundp 'calendar-check-holidays)
16251 'calendar-check-holidays 'check-calendar-holidays) date)))
16252 (if hl (mapconcat 'identity hl "; "))))
16254 (defun org-diary-sexp-entry (sexp entry date)
16255 "Process a SEXP diary ENTRY for DATE."
16256 (require 'diary-lib)
16257 (let ((result (if calendar-debug-sexp
16258 (let ((stack-trace-on-error t))
16259 (eval (car (read-from-string sexp))))
16260 (condition-case nil
16261 (eval (car (read-from-string sexp)))
16264 (message "Bad sexp at line %d in %s: %s"
16266 (buffer-file-name) sexp)
16268 (cond ((stringp result) (split-string result "; "))
16269 ((and (consp result)
16270 (not (consp (cdr result)))
16271 (stringp (cdr result))) (cdr result))
16272 ((and (consp result)
16273 (stringp (car result))) result)
16276 (defun org-diary-to-ical-string (frombuf)
16277 "Get iCalendar entries from diary entries in buffer FROMBUF.
16278 This uses the icalendar.el library."
16279 (let* ((tmpdir (if (featurep 'xemacs)
16281 temporary-file-directory))
16282 (tmpfile (make-temp-name
16283 (expand-file-name "orgics" tmpdir)))
16285 (with-current-buffer frombuf
16286 (icalendar-export-region (point-min) (point-max) tmpfile)
16287 (setq buf (find-buffer-visiting tmpfile))
16289 (goto-char (point-min))
16290 (if (re-search-forward "^BEGIN:VEVENT" nil t)
16291 (setq b (match-beginning 0)))
16292 (goto-char (point-max))
16293 (if (re-search-backward "^END:VEVENT" nil t)
16294 (setq e (match-end 0)))
16295 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
16297 (delete-file tmpfile)
16300 (defun org-closest-date (start current change prefer show-all)
16301 "Find the date closest to CURRENT that is consistent with START and CHANGE.
16302 When PREFER is `past', return a date that is either CURRENT or past.
16303 When PREFER is `future', return a date that is either CURRENT or future.
16304 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
16305 ;; Make the proper lists from the dates
16307 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
16308 dn dw sday cday n1 n2 n0
16309 d m y y1 y2 date1 date2 nmonths nm ny m2)
16311 (setq start (org-date-to-gregorian start)
16312 current (org-date-to-gregorian
16315 (time-to-days (current-time))))
16316 sday (calendar-absolute-from-gregorian start)
16317 cday (calendar-absolute-from-gregorian current))
16319 (if (<= cday sday) (throw 'exit sday))
16321 (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
16322 (setq dn (string-to-number (match-string 1 change))
16323 dw (cdr (assoc (match-string 2 change) a1)))
16324 (error "Invalid change specifier: %s" change))
16325 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
16328 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
16331 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
16332 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
16333 (setq date1 (list m d y1)
16334 n1 (calendar-absolute-from-gregorian date1)
16335 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
16336 n2 (calendar-absolute-from-gregorian date2)))
16338 ;; approx number of month between the two dates
16339 (setq nmonths (floor (/ (- cday sday) 30.436875)))
16340 ;; How often does dn fit in there?
16341 (setq d (nth 1 start) m (car start) y (nth 2 start)
16342 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
16344 ny (floor (/ m 12))
16347 (while (> m 12) (setq m (- m 12) y (1+ y)))
16348 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
16349 (setq m2 (+ m dn) y2 y)
16350 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16351 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
16352 (while (<= n2 cday)
16353 (setq n1 n2 m m2 y y2)
16354 (setq m2 (+ m dn) y2 y)
16355 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16356 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
16357 ;; Make sure n1 is the earlier date
16358 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
16361 ((eq prefer 'past) (if (= cday n2) n2 n1))
16362 ((eq prefer 'future) (if (= cday n1) n1 n2))
16363 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
16365 ((eq prefer 'past) (if (= cday n2) n2 n1))
16366 ((eq prefer 'future) (if (= cday n1) n1 n2))
16367 (t (if (= cday n1) n1 n2)))))))
16369 (defun org-date-to-gregorian (date)
16370 "Turn any specification of DATE into a Gregorian date for the calendar."
16371 (cond ((integerp date) (calendar-gregorian-from-absolute date))
16372 ((and (listp date) (= (length date) 3)) date)
16374 (setq date (org-parse-time-string date))
16375 (list (nth 4 date) (nth 3 date) (nth 5 date)))
16377 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
16379 (defun org-parse-time-string (s &optional nodefault)
16380 "Parse the standard Org-mode time string.
16381 This should be a lot faster than the normal `parse-time-string'.
16382 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
16383 hour and minute fields will be nil if not given."
16384 (if (string-match org-ts-regexp0 s)
16386 (if (or (match-beginning 8) (not nodefault))
16387 (string-to-number (or (match-string 8 s) "0")))
16388 (if (or (match-beginning 7) (not nodefault))
16389 (string-to-number (or (match-string 7 s) "0")))
16390 (string-to-number (match-string 4 s))
16391 (string-to-number (match-string 3 s))
16392 (string-to-number (match-string 2 s))
16394 (error "Not a standard Org-mode time string: %s" s)))
16396 (defun org-timestamp-up (&optional arg)
16397 "Increase the date item at the cursor by one.
16398 If the cursor is on the year, change the year. If it is on the month,
16399 the day or the time, change that.
16400 With prefix ARG, change by that many units."
16402 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
16404 (defun org-timestamp-down (&optional arg)
16405 "Decrease the date item at the cursor by one.
16406 If the cursor is on the year, change the year. If it is on the month,
16407 the day or the time, change that.
16408 With prefix ARG, change by that many units."
16410 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
16412 (defun org-timestamp-up-day (&optional arg)
16413 "Increase the date in the time stamp by one day.
16414 With prefix ARG, change that many days."
16416 (if (and (not (org-at-timestamp-p t))
16417 (org-at-heading-p))
16419 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
16421 (defun org-timestamp-down-day (&optional arg)
16422 "Decrease the date in the time stamp by one day.
16423 With prefix ARG, change that many days."
16425 (if (and (not (org-at-timestamp-p t))
16426 (org-at-heading-p))
16428 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
16430 (defun org-at-timestamp-p (&optional inactive-ok)
16431 "Determine if the cursor is in or at a timestamp."
16433 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
16435 (ans (or (looking-at tsr)
16437 (skip-chars-backward "^[<\n\r\t")
16438 (if (> (point) (point-min)) (backward-char 1))
16439 (and (looking-at tsr)
16440 (> (- (match-end 0) pos) -1))))))
16442 (boundp 'org-ts-what)
16445 ((= pos (match-beginning 0)) 'bracket)
16446 ;; Point is considered to be "on the bracket" whether
16447 ;; it's really on it or right after it.
16448 ((= pos (1- (match-end 0))) 'bracket)
16449 ((= pos (match-end 0)) 'after)
16450 ((org-pos-in-match-range pos 2) 'year)
16451 ((org-pos-in-match-range pos 3) 'month)
16452 ((org-pos-in-match-range pos 7) 'hour)
16453 ((org-pos-in-match-range pos 8) 'minute)
16454 ((or (org-pos-in-match-range pos 4)
16455 (org-pos-in-match-range pos 5)) 'day)
16456 ((and (> pos (or (match-end 8) (match-end 5)))
16457 (< pos (match-end 0)))
16458 (- pos (or (match-end 8) (match-end 5))))
16462 (defun org-toggle-timestamp-type ()
16463 "Toggle the type (<active> or [inactive]) of a time stamp."
16465 (when (org-at-timestamp-p t)
16466 (let ((beg (match-beginning 0)) (end (match-end 0))
16467 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
16470 (while (re-search-forward "[][<>]" end t)
16471 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
16473 (message "Timestamp is now %sactive"
16474 (if (equal (char-after beg) ?<) "" "in")))))
16476 (defvar org-clock-history) ; defined in org-clock.el
16477 (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
16478 (defun org-timestamp-change (n &optional what updown)
16479 "Change the date in the time stamp at point.
16480 The date will be changed by N times WHAT. WHAT can be `day', `month',
16481 `year', `minute', `second'. If WHAT is not given, the cursor position
16482 in the timestamp determines what will be changed."
16483 (let ((origin (point)) origin-cat
16485 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
16488 ts time time0 fixnext clrgx)
16489 (if (not (org-at-timestamp-p t))
16490 (error "Not at a timestamp"))
16491 (if (and (not what) (eq org-ts-what 'bracket))
16492 (org-toggle-timestamp-type)
16493 ;; Point isn't on brackets. Remember the part of the time-stamp
16494 ;; the point was in. Indeed, size of time-stamps may change,
16495 ;; but point must be kept in the same category nonetheless.
16496 (setq origin-cat org-ts-what)
16497 (if (and (not what) (not (eq org-ts-what 'day))
16498 org-display-custom-times
16499 (get-text-property (point) 'display)
16500 (not (get-text-property (1- (point)) 'display)))
16501 (setq org-ts-what 'day))
16502 (setq org-ts-what (or what org-ts-what)
16503 inactive (= (char-after (match-beginning 0)) ?\[)
16504 ts (match-string 0))
16507 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
16509 (setq extra (match-string 1 ts)))
16510 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
16512 (setq time0 (org-parse-time-string ts))
16514 (eq org-ts-what 'minute)
16515 (not current-prefix-arg))
16516 ;; This looks like s-up and s-down. Change by one rounding step.
16517 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
16518 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
16519 (setcar (cdr time0) (+ (nth 1 time0)
16520 (if (> n 0) (- rem) (- dm rem))))))
16522 (encode-time (or (car time0) 0)
16523 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
16524 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
16525 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
16526 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
16527 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
16529 (when (and (member org-ts-what '(hour minute))
16531 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
16532 (setq extra (org-modify-ts-extra
16534 (if (eq org-ts-what 'hour) 2 5)
16536 (when (integerp org-ts-what)
16537 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
16538 (if (eq what 'calendar)
16539 (let ((cal-date (org-get-date-from-calendar)))
16540 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
16541 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
16542 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
16543 (setcar time0 (or (car time0) 0))
16544 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
16545 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
16546 (setq time (apply 'encode-time time0))))
16547 ;; Insert the new time-stamp, and ensure point stays in the same
16548 ;; category as before (i.e. not after the last position in that
16550 (let ((pos (point)))
16551 ;; Stay before inserted string. `save-excursion' is of no use.
16552 (setq org-last-changed-timestamp
16553 (org-insert-time-stamp time with-hm inactive nil nil extra))
16556 (looking-at org-ts-regexp3)
16558 ;; `day' category ends before `hour' if any, or at
16559 ;; the end of the day name.
16560 ((eq origin-cat 'day)
16561 (min (or (match-beginning 7) (1- (match-end 5))) origin))
16562 ((eq origin-cat 'hour) (min (match-end 7) origin))
16563 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
16564 ((integerp origin-cat) (min (1- (match-end 0)) origin))
16565 ;; `year' and `month' have both fixed size: point
16566 ;; couldn't have moved into another part.
16568 ;; Update clock if on a CLOCK line.
16569 (org-clock-update-time-maybe)
16570 ;; Maybe adjust the closest clock in `org-clock-history'
16571 (when org-clock-adjust-closest
16572 (if (not (and (org-at-clock-log-p)
16573 (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
16574 org-clock-history))))))
16575 (message "No clock to adjust")
16576 (cond ((save-excursion ; fix previous clock?
16577 (re-search-backward org-ts-regexp0 nil t)
16578 (org-looking-back (concat org-clock-string " \\[")))
16579 (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
16580 ((save-excursion ; fix next clock?
16581 (re-search-backward org-ts-regexp0 nil t)
16582 (looking-at (concat org-ts-regexp0 "\\] =>")))
16583 (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
16584 (save-window-excursion
16585 ;; Find closest clock to point, adjust the previous/next one in history
16586 (let* ((p (save-excursion (org-back-to-heading t)))
16587 (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
16589 (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
16590 (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
16592 (message "No clock to adjust")
16594 (org-goto-marker-or-bmk clfixpos)
16596 (when (re-search-forward clrgx nil t)
16597 (goto-char (match-beginning 1))
16598 (let (org-clock-adjust-closest)
16599 (org-timestamp-change n org-ts-what updown))
16600 (message "Clock adjusted in %s for heading: %s"
16601 (file-name-nondirectory (buffer-file-name))
16602 (org-get-heading t t)))))))))
16603 ;; Try to recenter the calendar window, if any.
16604 (if (and org-calendar-follow-timestamp-change
16605 (get-buffer-window "*Calendar*" t)
16606 (memq org-ts-what '(day month year)))
16607 (org-recenter-calendar (time-to-days time))))))
16609 (defun org-modify-ts-extra (s pos n dm)
16610 "Change the different parts of the lead-time and repeat fields in timestamp."
16611 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
16613 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
16615 ((or (org-pos-in-match-range pos 2)
16616 (org-pos-in-match-range pos 3))
16617 (setq m (string-to-number (match-string 3 s))
16618 h (string-to-number (match-string 2 s)))
16619 (if (org-pos-in-match-range pos 2)
16621 (setq n (* dm (org-no-warnings (signum n))))
16622 (when (not (= 0 (setq rem (% m dm))))
16623 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
16625 (if (< m 0) (setq m (+ m 60) h (1- h)))
16626 (if (> m 59) (setq m (- m 60) h (1+ h)))
16627 (setq h (min 24 (max 0 h)))
16628 (setq ng 1 new (format "-%02d:%02d" h m)))
16629 ((org-pos-in-match-range pos 6)
16630 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
16631 ((org-pos-in-match-range pos 5)
16632 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
16634 ((org-pos-in-match-range pos 9)
16635 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
16636 ((org-pos-in-match-range pos 8)
16637 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
16641 (substring s 0 (match-beginning ng))
16643 (substring s (match-end ng))))))
16646 (defun org-recenter-calendar (date)
16647 "If the calendar is visible, recenter it to DATE."
16648 (let ((cwin (get-buffer-window "*Calendar*" t)))
16650 (let ((calendar-move-hook nil))
16651 (with-selected-window cwin
16652 (calendar-goto-date (if (listp date) date
16653 (calendar-gregorian-from-absolute date))))))))
16655 (defun org-goto-calendar (&optional arg)
16656 "Go to the Emacs calendar at the current date.
16657 If there is a time stamp in the current line, go to that date.
16658 A prefix ARG can be used to force the current date."
16660 (let ((tsr org-ts-regexp) diff
16661 (calendar-move-hook nil)
16662 (calendar-view-holidays-initially-flag nil)
16663 (calendar-view-diary-initially-flag nil))
16664 (if (or (org-at-timestamp-p)
16666 (beginning-of-line 1)
16667 (looking-at (concat ".*" tsr))))
16668 (let ((d1 (time-to-days (current-time)))
16670 (org-time-string-to-time (match-string 1)))))
16671 (setq diff (- d2 d1))))
16673 (calendar-goto-today)
16674 (if (and diff (not arg)) (calendar-forward-day diff))))
16676 (defun org-get-date-from-calendar ()
16677 "Return a list (month day year) of date at point in calendar."
16678 (with-current-buffer "*Calendar*"
16680 (calendar-cursor-to-date))))
16682 (defun org-date-from-calendar ()
16683 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
16684 If there is already a time stamp at the cursor position, update it."
16686 (if (org-at-timestamp-p t)
16687 (org-timestamp-change 0 'calendar)
16688 (let ((cal-date (org-get-date-from-calendar)))
16689 (org-insert-time-stamp
16690 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
16692 (defun org-minutes-to-hh:mm-string (m)
16693 "Compute H:MM from a number of minutes."
16694 (let ((h (/ m 60)))
16695 (setq m (- m (* 60 h)))
16696 (format org-time-clocksum-format h m)))
16698 (defun org-hh:mm-string-to-minutes (s)
16699 "Convert a string H:MM to a number of minutes.
16700 If the string is just a number, interpret it as minutes.
16701 In fact, the first hh:mm or number in the string will be taken,
16702 there can be extra stuff in the string.
16703 If no number is found, the return value is 0."
16706 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
16707 (+ (* (string-to-number (match-string 1 s)) 60)
16708 (string-to-number (match-string 2 s))))
16709 ((string-match "\\([0-9]+\\)" s)
16710 (string-to-number (match-string 1 s)))
16713 (defcustom org-effort-durations
16716 ("w" . ,(* 60 8 5))
16717 ("m" . ,(* 60 8 5 4))
16718 ("y" . ,(* 60 8 5 40)))
16719 "Conversion factor to minutes for an effort modifier.
16721 Each entry has the form (MODIFIER . MINUTES).
16723 In an effort string, a number followed by MODIFIER is multiplied
16724 by the specified number of MINUTES to obtain an effort in
16727 For example, if the value of this variable is ((\"hours\" . 60)), then an
16728 effort string \"2hours\" is equivalent to 120 minutes."
16731 :type '(alist :key-type (string :tag "Modifier")
16732 :value-type (number :tag "Minutes")))
16734 (defun org-duration-string-to-minutes (s &optional output-to-string)
16735 "Convert a duration string S to minutes.
16737 A bare number is interpreted as minutes, modifiers can be set by
16738 customizing `org-effort-durations' (which see).
16740 Entries containing a colon are interpreted as H:MM by
16741 `org-hh:mm-string-to-minutes'."
16743 (re (concat "\\([0-9.]+\\) *\\("
16744 (regexp-opt (mapcar 'car org-effort-durations))
16746 (while (string-match re s)
16747 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
16748 (string-to-number (match-string 1 s))))
16749 (setq s (replace-match "" nil t s)))
16750 (setq result (floor result))
16751 (incf result (org-hh:mm-string-to-minutes s))
16752 (if output-to-string (number-to-string result) result)))
16756 (defun org-save-all-org-buffers ()
16757 "Save all Org-mode buffers without user confirmation."
16759 (message "Saving all Org-mode buffers...")
16760 (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
16761 (when (featurep 'org-id) (org-id-locations-save))
16762 (message "Saving all Org-mode buffers... done"))
16764 (defun org-revert-all-org-buffers ()
16765 "Revert all Org-mode buffers.
16766 Prompt for confirmation when there are unsaved changes.
16767 Be sure you know what you are doing before letting this function
16768 overwrite your changes.
16770 This function is useful in a setup where one tracks org files
16771 with a version control system, to revert on one machine after pulling
16772 changes from another. I believe the procedure must be like this:
16774 1. M-x org-save-all-org-buffers
16775 2. Pull changes from the other machine, resolve conflicts
16776 3. M-x org-revert-all-org-buffers"
16778 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
16781 (save-window-excursion
16784 (when (and (with-current-buffer b (derived-mode-p 'org-mode))
16785 (with-current-buffer b buffer-file-name))
16786 (org-pop-to-buffer-same-window b)
16787 (revert-buffer t 'no-confirm)))
16789 (when (and (featurep 'org-id) org-id-track-globally)
16790 (org-id-locations-load)))))
16795 (defun org-switchb (&optional arg)
16796 "Switch between Org buffers.
16797 With one prefix argument, restrict available buffers to files.
16798 With two prefix arguments, restrict available buffers to agenda files.
16800 Defaults to `iswitchb' for buffer name completion.
16801 Set `org-completion-use-ido' to make it use ido instead."
16803 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
16804 ((equal arg '(16)) (org-buffer-list 'agenda))
16805 (t (org-buffer-list))))
16806 (org-completion-use-iswitchb org-completion-use-iswitchb)
16807 (org-completion-use-ido org-completion-use-ido))
16808 (unless (or org-completion-use-ido org-completion-use-iswitchb)
16809 (setq org-completion-use-iswitchb t))
16810 (org-pop-to-buffer-same-window
16811 (org-icompleting-read "Org buffer: "
16812 (mapcar 'list (mapcar 'buffer-name blist))
16815 ;;; Define some older names previously used for this functionality
16817 (defalias 'org-ido-switchb 'org-switchb)
16819 (defalias 'org-iswitchb 'org-switchb)
16821 (defun org-buffer-list (&optional predicate exclude-tmp)
16822 "Return a list of Org buffers.
16823 PREDICATE can be `export', `files' or `agenda'.
16825 export restrict the list to Export buffers.
16826 files restrict the list to buffers visiting Org files.
16827 agenda restrict the list to buffers visiting agenda files.
16829 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
16831 (agenda-files (and (eq predicate 'agenda)
16832 (mapcar 'file-truename (org-agenda-files t))))
16835 ((eq predicate 'files)
16836 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
16837 ((eq predicate 'export)
16838 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
16839 ((eq predicate 'agenda)
16841 (with-current-buffer b
16842 (and (derived-mode-p 'org-mode)
16843 (setq bfn (buffer-file-name b))
16844 (member (file-truename bfn) agenda-files)))))
16845 (t (lambda (b) (with-current-buffer b
16846 (or (derived-mode-p 'org-mode)
16847 (string-match "\*Org .*Export"
16848 (buffer-name b)))))))))
16852 (if (and (funcall filter b)
16853 (or (not exclude-tmp)
16854 (not (string-match "tmp" (buffer-name b)))))
16859 (defun org-agenda-files (&optional unrestricted archives)
16860 "Get the list of agenda files.
16861 Optional UNRESTRICTED means return the full list even if a restriction
16862 is currently in place.
16863 When ARCHIVES is t, include all archive files that are really being
16864 used by the agenda files. If ARCHIVE is `ifmode', do this only if
16865 `org-agenda-archives-mode' is t."
16868 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
16869 ((stringp org-agenda-files) (org-read-agenda-file-list))
16870 ((listp org-agenda-files) org-agenda-files)
16871 (t (error "Invalid value of `org-agenda-files'")))))
16872 (setq files (apply 'append
16873 (mapcar (lambda (f)
16874 (if (file-directory-p f)
16876 f t org-agenda-file-regexp)
16879 (when org-agenda-skip-unavailable-files
16880 (setq files (delq nil
16883 (and (file-readable-p file) file)))
16885 (when (or (eq archives t)
16886 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
16887 (setq files (org-add-archive-files files)))
16890 (defun org-agenda-file-p (&optional file)
16891 "Return non-nil, if FILE is an agenda file.
16892 If FILE is omitted, use the file associated with the current
16894 (member (or file (buffer-file-name))
16895 (org-agenda-files t)))
16897 (defun org-edit-agenda-file-list ()
16898 "Edit the list of agenda files.
16899 Depending on setup, this either uses customize to edit the variable
16900 `org-agenda-files', or it visits the file that is holding the list. In the
16901 latter case, the buffer is set up in a way that saving it automatically kills
16902 the buffer and restores the previous window configuration."
16904 (if (stringp org-agenda-files)
16905 (let ((cw (current-window-configuration)))
16906 (find-file org-agenda-files)
16907 (org-set-local 'org-window-configuration cw)
16908 (org-add-hook 'after-save-hook
16910 (set-window-configuration
16911 (prog1 org-window-configuration
16912 (kill-buffer (current-buffer))))
16913 (org-install-agenda-files-menu)
16914 (message "New agenda file list installed"))
16916 (message "%s" (substitute-command-keys
16917 "Edit list and finish with \\[save-buffer]")))
16918 (customize-variable 'org-agenda-files)))
16920 (defun org-store-new-agenda-file-list (list)
16921 "Set new value for the agenda file list and save it correctly."
16922 (if (stringp org-agenda-files)
16923 (let ((fe (org-read-agenda-file-list t)) b u)
16924 (while (setq b (find-buffer-visiting org-agenda-files))
16926 (with-temp-file org-agenda-files
16929 (lambda (f) ;; Keep un-expanded entries.
16930 (if (setq u (assoc f fe))
16935 (let ((org-mode-hook nil) (org-inhibit-startup t)
16936 (org-insert-mode-line-in-empty-file nil))
16937 (setq org-agenda-files list)
16938 (customize-save-variable 'org-agenda-files org-agenda-files))))
16940 (defun org-read-agenda-file-list (&optional pair-with-expansion)
16941 "Read the list of agenda files from a file.
16942 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
16943 filenames, used by `org-store-new-agenda-file-list' to write back
16944 un-expanded file names."
16945 (when (file-directory-p org-agenda-files)
16946 (error "`org-agenda-files' cannot be a single directory"))
16947 (when (stringp org-agenda-files)
16949 (insert-file-contents org-agenda-files)
16952 (let ((e (expand-file-name (substitute-in-file-name f)
16954 (if pair-with-expansion
16957 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
16960 (defun org-cycle-agenda-files ()
16961 "Cycle through the files in `org-agenda-files'.
16962 If the current buffer visits an agenda file, find the next one in the list.
16963 If the current buffer does not, find the first agenda file."
16965 (let* ((fs (org-agenda-files t))
16966 (files (append fs (list (car fs))))
16967 (tcf (if buffer-file-name (file-truename buffer-file-name)))
16969 (unless files (error "No agenda files"))
16971 (while (setq file (pop files))
16972 (if (equal (file-truename file) tcf)
16974 (find-file (car files))
16976 (find-file (car fs)))
16977 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
16979 (defun org-agenda-file-to-front (&optional to-end)
16980 "Move/add the current file to the top of the agenda file list.
16981 If the file is not present in the list, it is added to the front. If it is
16982 present, it is moved there. With optional argument TO-END, add/move to the
16985 (let ((org-agenda-skip-unavailable-files nil)
16986 (file-alist (mapcar (lambda (x)
16987 (cons (file-truename x) x))
16988 (org-agenda-files t)))
16989 (ctf (file-truename buffer-file-name))
16991 (setq x (assoc ctf file-alist) had x)
16993 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
16995 (setq file-alist (append (delq x file-alist) (list x)))
16996 (setq file-alist (cons x (delq x file-alist))))
16997 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
16998 (org-install-agenda-files-menu)
16999 (message "File %s to %s of agenda file list"
17000 (if had "moved" "added") (if to-end "end" "front"))))
17002 (defun org-remove-file (&optional file)
17003 "Remove current file from the list of files in variable `org-agenda-files'.
17004 These are the files which are being checked for agenda entries.
17005 Optional argument FILE means use this file instead of the current."
17007 (let* ((org-agenda-skip-unavailable-files nil)
17008 (file (or file buffer-file-name))
17009 (true-file (file-truename file))
17010 (afile (abbreviate-file-name file))
17011 (files (delq nil (mapcar
17013 (if (equal true-file
17016 (org-agenda-files t)))))
17017 (if (not (= (length files) (length (org-agenda-files t))))
17019 (org-store-new-agenda-file-list files)
17020 (org-install-agenda-files-menu)
17021 (message "Removed file: %s" afile))
17022 (message "File was not in list: %s (not removed)" afile))))
17024 (defun org-file-menu-entry (file)
17025 (vector file (list 'find-file file) t))
17027 (defun org-check-agenda-file (file)
17028 "Make sure FILE exists. If not, ask user what to do."
17029 (when (not (file-exists-p file))
17030 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
17031 (abbreviate-file-name file))
17032 (let ((r (downcase (read-char-exclusive))))
17035 (org-remove-file file)
17036 (throw 'nextfile t))
17037 (t (error "Abort"))))))
17039 (defun org-get-agenda-file-buffer (file)
17040 "Get a buffer visiting FILE. If the buffer needs to be created, add
17041 it to the list of buffers which might be released later."
17042 (let ((buf (org-find-base-buffer-visiting file)))
17044 buf ; just return it
17045 ;; Make a new buffer and remember it
17046 (setq buf (find-file-noselect file))
17047 (if buf (push buf org-agenda-new-buffers))
17050 (defun org-release-buffers (blist)
17051 "Release all buffers in list, asking the user for confirmation when needed.
17052 When a buffer is unmodified, it is just killed. When modified, it is saved
17053 \(if the user agrees) and then killed."
17055 (while (setq buf (pop blist))
17056 (setq file (buffer-file-name buf))
17057 (when (and (buffer-modified-p buf)
17059 (y-or-n-p (format "Save file %s? " file)))
17060 (with-current-buffer buf (save-buffer)))
17061 (kill-buffer buf))))
17063 (defun org-prepare-agenda-buffers (files)
17064 "Create buffers for all agenda files, protect archived trees and comments."
17066 (let ((pa '(:org-archived t))
17067 (pc '(:org-comment t))
17068 (pall '(:org-archived t :org-comment t))
17069 (inhibit-read-only t)
17070 (rea (concat ":" org-archive-tag ":"))
17074 (while (setq file (pop files))
17078 (org-check-agenda-file file)
17079 (set-buffer (org-get-agenda-file-buffer file)))
17081 (setq bmp (buffer-modified-p))
17082 (org-refresh-category-properties)
17083 (setq org-todo-keywords-for-agenda
17084 (append org-todo-keywords-for-agenda org-todo-keywords-1))
17085 (setq org-done-keywords-for-agenda
17086 (append org-done-keywords-for-agenda org-done-keywords))
17087 (setq org-todo-keyword-alist-for-agenda
17088 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
17089 (setq org-drawers-for-agenda
17090 (append org-drawers-for-agenda org-drawers))
17091 (setq org-tag-alist-for-agenda
17092 (append org-tag-alist-for-agenda org-tag-alist))
17095 (remove-text-properties (point-min) (point-max) pall)
17096 (when org-agenda-skip-archived-trees
17097 (goto-char (point-min))
17098 (while (re-search-forward rea nil t)
17099 (if (org-at-heading-p t)
17100 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
17101 (goto-char (point-min))
17102 (setq re (format org-heading-keyword-regexp-format
17103 org-comment-string))
17104 (while (re-search-forward re nil t)
17105 (add-text-properties
17106 (match-beginning 0) (org-end-of-subtree t) pc)))
17107 (set-buffer-modified-p bmp)))))
17108 (setq org-todo-keywords-for-agenda
17109 (org-uniquify org-todo-keywords-for-agenda))
17110 (setq org-todo-keyword-alist-for-agenda
17111 (org-uniquify org-todo-keyword-alist-for-agenda)
17112 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
17114 ;;;; Embedded LaTeX
17116 (defvar org-cdlatex-mode-map (make-sparse-keymap)
17117 "Keymap for the minor `org-cdlatex-mode'.")
17119 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
17120 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
17121 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
17122 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
17123 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
17125 (defvar org-cdlatex-texmathp-advice-is-done nil
17126 "Flag remembering if we have applied the advice to texmathp already.")
17128 (define-minor-mode org-cdlatex-mode
17129 "Toggle the minor `org-cdlatex-mode'.
17130 This mode supports entering LaTeX environment and math in LaTeX fragments
17132 \\{org-cdlatex-mode-map}"
17134 (when org-cdlatex-mode
17136 (run-hooks 'cdlatex-mode-hook)
17137 (cdlatex-compute-tables))
17138 (unless org-cdlatex-texmathp-advice-is-done
17139 (setq org-cdlatex-texmathp-advice-is-done t)
17140 (defadvice texmathp (around org-math-always-on activate)
17141 "Always return t in org-mode buffers.
17142 This is because we want to insert math symbols without dollars even outside
17143 the LaTeX math segments. If Orgmode thinks that point is actually inside
17144 an embedded LaTeX fragment, let texmathp do its job.
17145 \\[org-cdlatex-mode-map]"
17149 ((not (derived-mode-p 'org-mode)) ad-do-it)
17150 ((eq this-command 'cdlatex-math-symbol)
17151 (setq ad-return-value t
17152 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
17154 (let ((p (org-inside-LaTeX-fragment-p)))
17155 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
17156 (setq ad-return-value t
17157 texmathp-why '("Org-mode embedded math" . 0))
17158 (if p ad-do-it)))))))))
17160 (defun turn-on-org-cdlatex ()
17161 "Unconditionally turn on `org-cdlatex-mode'."
17162 (org-cdlatex-mode 1))
17164 (defun org-inside-LaTeX-fragment-p ()
17165 "Test if point is inside a LaTeX fragment.
17166 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
17167 sequence appearing also before point.
17168 Even though the matchers for math are configurable, this function assumes
17169 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
17170 delimiters are skipped when they have been removed by customization.
17171 The return value is nil, or a cons cell with the delimiter and the
17172 position of this delimiter.
17174 This function does a reasonably good job, but can locally be fooled by
17175 for example currency specifications. For example it will assume being in
17176 inline math after \"$22.34\". The LaTeX fragment formatter will only format
17177 fragments that are properly closed, but during editing, we have to live
17178 with the uncertainty caused by missing closing delimiters. This function
17179 looks only before point, not after."
17181 (let ((pos (point))
17182 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
17184 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
17186 dd-on str (start 0) m re)
17189 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
17190 re (nth 1 (assoc "$" org-latex-regexps)))
17191 (while (string-match re str start)
17193 ((= (match-end 0) (length str))
17194 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
17195 ((= (match-end 0) (- (length str) 5))
17197 (t (setq start (match-end 0))))))
17198 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
17200 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
17201 (and (match-beginning 2) (throw 'exit nil))
17203 (while (re-search-backward "\\$\\$" lim t)
17204 (setq dd-on (not dd-on)))
17206 (if dd-on (cons "$$" m))))))
17208 (defun org-inside-latex-macro-p ()
17209 "Is point inside a LaTeX macro or its arguments?"
17212 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
17214 (defun org-try-cdlatex-tab ()
17215 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
17216 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
17217 - inside a LaTeX fragment, or
17218 - after the first word in a line, where an abbreviation expansion could
17219 insert a LaTeX environment."
17220 (when org-cdlatex-mode
17222 ;; Before any word on the line: No expansion possible.
17223 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
17224 ;; Just after first word on the line: Expand it. Make sure it
17225 ;; cannot happen on headlines, though.
17227 (skip-chars-backward "a-zA-Z0-9*")
17228 (skip-chars-backward " \t")
17229 (and (bolp) (not (org-at-heading-p))))
17231 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
17233 (defun org-cdlatex-underscore-caret (&optional arg)
17234 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
17235 Revert to the normal definition outside of these fragments."
17237 (if (org-inside-LaTeX-fragment-p)
17238 (call-interactively 'cdlatex-sub-superscript)
17239 (let (org-cdlatex-mode)
17240 (call-interactively (key-binding (vector last-input-event))))))
17242 (defun org-cdlatex-math-modify (&optional arg)
17243 "Execute `cdlatex-math-modify' in LaTeX fragments.
17244 Revert to the normal definition outside of these fragments."
17246 (if (org-inside-LaTeX-fragment-p)
17247 (call-interactively 'cdlatex-math-modify)
17248 (let (org-cdlatex-mode)
17249 (call-interactively (key-binding (vector last-input-event))))))
17251 (defvar org-latex-fragment-image-overlays nil
17252 "List of overlays carrying the images of latex fragments.")
17253 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
17255 (defun org-remove-latex-fragment-image-overlays ()
17256 "Remove all overlays with LaTeX fragment images in current buffer."
17257 (mapc 'delete-overlay org-latex-fragment-image-overlays)
17258 (setq org-latex-fragment-image-overlays nil))
17260 (defun org-preview-latex-fragment (&optional subtree)
17261 "Preview the LaTeX fragment at point, or all locally or globally.
17262 If the cursor is in a LaTeX fragment, create the image and overlay
17263 it over the source code. If there is no fragment at point, display
17264 all fragments in the current text, from one headline to the next. With
17265 prefix SUBTREE, display all fragments in the current subtree. With a
17266 double prefix arg \\[universal-argument] \\[universal-argument], or when \
17267 the cursor is before the first headline,
17268 display all fragments in the buffer.
17269 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
17271 (unless buffer-file-name
17272 (error "Can't preview LaTeX fragment in a non-file buffer"))
17273 (org-remove-latex-fragment-image-overlays)
17276 (let (beg end at msg)
17278 ((or (equal subtree '(16))
17279 (not (save-excursion
17280 (re-search-backward org-outline-regexp-bol nil t))))
17281 (setq beg (point-min) end (point-max)
17282 msg "Creating images for buffer...%s"))
17283 ((equal subtree '(4))
17284 (org-back-to-heading)
17285 (setq beg (point) end (org-end-of-subtree t)
17286 msg "Creating images for subtree...%s"))
17288 (if (setq at (org-inside-LaTeX-fragment-p))
17289 (goto-char (max (point-min) (- (cdr at) 2)))
17290 (org-back-to-heading))
17291 (setq beg (point) end (progn (outline-next-heading) (point))
17292 msg (if at "Creating image...%s"
17293 "Creating images for entry...%s"))))
17295 (narrow-to-region beg end)
17298 (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
17299 (file-name-nondirectory
17300 buffer-file-name)))
17301 default-directory 'overlays msg at 'forbuffer
17302 org-latex-create-formula-image-program)
17303 (message msg "done. Use `C-c C-c' to remove images.")))))
17305 (defvar org-latex-regexps
17306 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
17307 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
17308 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
17309 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17310 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17311 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
17312 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
17313 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
17314 "Regular expressions for matching embedded LaTeX.")
17316 (defvar org-export-have-math nil) ;; dynamic scoping
17317 (defun org-format-latex (prefix &optional dir overlays msg at
17318 forbuffer processing-type)
17319 "Replace LaTeX fragments with links to an image, and produce images.
17320 Some of the options can be changed using the variable
17321 `org-format-latex-options'."
17322 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
17323 (let* ((prefixnodir (file-name-nondirectory prefix))
17324 (absprefix (expand-file-name prefix dir))
17325 (todir (file-name-directory absprefix))
17326 (opt org-format-latex-options)
17327 (matchers (plist-get opt :matchers))
17328 (re-list org-latex-regexps)
17329 (org-format-latex-header-extra
17330 (plist-get (org-infile-export-plist) :latex-header-extra))
17331 (cnt 0) txt hash link beg end re e checkdir
17332 executables-checked string
17333 m n block-type block linkfile movefile ov)
17334 ;; Check the different regular expressions
17335 (while (setq e (pop re-list))
17336 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
17337 block (if block-type "\n\n" ""))
17338 (when (member m matchers)
17339 (goto-char (point-min))
17340 (while (re-search-forward re nil t)
17341 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
17342 (not (get-text-property (match-beginning n)
17345 (not (eq (get-char-property (match-beginning n)
17347 'org-latex-overlay))))
17348 (setq org-export-have-math t)
17350 ((eq processing-type 'verbatim)
17351 ;; Leave the text verbatim, just protect it
17352 (add-text-properties (match-beginning n) (match-end n)
17353 '(org-protected t)))
17354 ((eq processing-type 'mathjax)
17355 ;; Prepare for MathJax processing
17356 (setq string (match-string n))
17357 (if (member m '("$" "$1"))
17359 (delete-region (match-beginning n) (match-end n))
17360 (goto-char (match-beginning n))
17361 (insert (org-add-props (concat "\\(" (substring string 1 -1)
17363 '(org-protected t))))
17364 (add-text-properties (match-beginning n) (match-end n)
17365 '(org-protected t))))
17366 ((or (eq processing-type 'dvipng)
17367 (eq processing-type 'imagemagick))
17368 ;; Process to an image
17369 (setq txt (match-string n)
17370 beg (match-beginning n) end (match-end n)
17372 (let (print-length print-level) ; make sure full list is printed
17373 (setq hash (sha1 (prin1-to-string
17374 (list org-format-latex-header
17375 org-format-latex-header-extra
17376 org-export-latex-default-packages-alist
17377 org-export-latex-packages-alist
17378 org-format-latex-options
17380 linkfile (format "%s_%s.png" prefix hash)
17381 movefile (format "%s_%s.png" absprefix hash)))
17382 (setq link (concat block "[[file:" linkfile "]]" block))
17383 (if msg (message msg cnt))
17385 (unless checkdir ; make sure the directory exists
17387 (or (file-directory-p todir) (make-directory todir t)))
17389 ((eq processing-type 'dvipng)
17390 (unless executables-checked
17391 (org-check-external-command
17392 "latex" "needed to convert LaTeX fragments to images")
17393 (org-check-external-command
17394 "dvipng" "needed to convert LaTeX fragments to images")
17395 (setq executables-checked t))
17396 (unless (file-exists-p movefile)
17397 (org-create-formula-image-with-dvipng
17398 txt movefile opt forbuffer)))
17399 ((eq processing-type 'imagemagick)
17400 (unless executables-checked
17401 (org-check-external-command
17402 "convert" "you need to install imagemagick")
17403 (setq executables-checked t))
17404 (unless (file-exists-p movefile)
17405 (org-create-formula-image-with-imagemagick
17406 txt movefile opt forbuffer))))
17410 (if (eq (overlay-get o 'org-overlay-type)
17411 'org-latex-overlay)
17412 (delete-overlay o)))
17413 (overlays-in beg end))
17414 (setq ov (make-overlay beg end))
17415 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
17416 (if (featurep 'xemacs)
17418 (overlay-put ov 'invisible t)
17421 (make-glyph (vector 'png :file movefile))))
17424 (list 'image :type 'png :file movefile :ascent 'center)))
17425 (push ov org-latex-fragment-image-overlays)
17427 (delete-region beg end)
17428 (insert (org-add-props link
17429 (list 'org-latex-src
17430 (replace-regexp-in-string
17432 'org-latex-src-embed-type
17433 (if block-type 'paragraph 'character))))))
17434 ((eq processing-type 'mathml)
17435 ;; Process to MathML
17436 (unless executables-checked
17437 (unless (save-match-data (org-format-latex-mathml-available-p))
17438 (error "LaTeX to MathML converter not configured"))
17439 (setq executables-checked t))
17440 (setq txt (match-string n)
17441 beg (match-beginning n) end (match-end n)
17443 (if msg (message msg cnt))
17445 (delete-region beg end)
17446 (insert (org-format-latex-as-mathml
17447 txt block-type prefix dir)))
17449 (error "Unknown conversion type %s for latex fragments"
17450 processing-type)))))))))
17452 (defun org-create-math-formula (latex-frag &optional mathml-file)
17453 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
17454 Use `org-latex-to-mathml-convert-command'. If the conversion is
17455 sucessful, return the portion between \"<math...> </math>\"
17456 elements otherwise return nil. When MATHML-FILE is specified,
17457 write the results in to that file. When invoked as an
17458 interactive command, prompt for LATEX-FRAG, with initial value
17459 set to the current active region and echo the results for user
17461 (interactive (list (let ((frag (when (org-region-active-p)
17462 (buffer-substring-no-properties
17463 (region-beginning) (region-end)))))
17464 (read-string "LaTeX Fragment: " frag nil frag))))
17465 (unless latex-frag (error "Invalid latex-frag"))
17466 (let* ((tmp-in-file (file-relative-name
17467 (make-temp-name (expand-file-name "ltxmathml-in"))))
17468 (ignore (write-region latex-frag nil tmp-in-file))
17469 (tmp-out-file (file-relative-name
17470 (make-temp-name (expand-file-name "ltxmathml-out"))))
17472 org-latex-to-mathml-convert-command
17473 `((?j . ,(shell-quote-argument
17474 (expand-file-name org-latex-to-mathml-jar-file)))
17475 (?I . ,(shell-quote-argument tmp-in-file))
17476 (?o . ,(shell-quote-argument tmp-out-file)))))
17477 mathml shell-command-output)
17478 (when (org-called-interactively-p 'any)
17479 (unless (org-format-latex-mathml-available-p)
17480 (error "LaTeX to MathML converter not configured")))
17481 (message "Running %s" cmd)
17482 (setq shell-command-output (shell-command-to-string cmd))
17484 (when (file-readable-p tmp-out-file)
17485 (with-current-buffer (find-file-noselect tmp-out-file t)
17486 (goto-char (point-min))
17487 (when (re-search-forward
17490 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
17492 (regexp-quote "</math>")) nil t)
17493 (prog1 (match-string 0) (kill-buffer))))))
17497 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
17499 (write-region mathml nil mathml-file))
17500 (when (org-called-interactively-p 'any)
17502 ((message "LaTeX to MathML conversion failed")
17503 (message shell-command-output)))
17504 (delete-file tmp-in-file)
17505 (when (file-exists-p tmp-out-file)
17506 (delete-file tmp-out-file))
17509 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
17510 prefix &optional dir)
17511 "Use `org-create-math-formula' but check local cache first."
17512 (let* ((absprefix (expand-file-name prefix dir))
17513 (print-length nil) (print-level nil)
17514 (formula-id (concat
17519 org-latex-to-mathml-convert-command)))))
17520 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
17521 (formula-cache-dir (file-name-directory formula-cache)))
17523 (unless (file-directory-p formula-cache-dir)
17524 (make-directory formula-cache-dir t))
17526 (unless (file-exists-p formula-cache)
17527 (org-create-math-formula latex-frag formula-cache))
17529 (if (file-exists-p formula-cache)
17530 ;; Successful conversion. Return the link to MathML file.
17532 (format "[[file:%s]]" (file-relative-name formula-cache dir))
17533 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
17534 'org-latex-src-embed-type (if latex-frag-type
17535 'paragraph 'character)))
17536 ;; Failed conversion. Return the LaTeX fragment verbatim
17537 (add-text-properties
17538 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
17541 ;; This function borrows from Ganesh Swami's latex2png.el
17542 (defun org-create-formula-image-with-dvipng (string tofile options buffer)
17543 "This calls dvipng."
17544 (require 'org-latex)
17545 (let* ((tmpdir (if (featurep 'xemacs)
17547 temporary-file-directory))
17548 (texfilebase (make-temp-name
17549 (expand-file-name "orgtex" tmpdir)))
17550 (texfile (concat texfilebase ".tex"))
17551 (dvifile (concat texfilebase ".dvi"))
17552 (pngfile (concat texfilebase ".png"))
17553 (fnh (if (featurep 'xemacs)
17554 (font-height (face-font 'default))
17555 (face-attribute 'default :height nil)))
17556 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17557 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17558 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17560 (bg (or (plist-get options (if buffer :background :html-background))
17562 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
17563 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
17564 (with-temp-file texfile
17565 (insert (org-splice-latex-header
17566 org-format-latex-header
17567 org-export-latex-default-packages-alist
17568 org-export-latex-packages-alist t
17569 org-format-latex-header-extra))
17570 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
17571 (require 'org-latex)
17572 (org-export-latex-fix-inputenc))
17573 (let ((dir default-directory))
17574 (condition-case nil
17577 (call-process "latex" nil nil nil texfile))
17580 (if (not (file-exists-p dvifile))
17581 (progn (message "Failed to create dvi file from %s" texfile) nil)
17582 (condition-case nil
17583 (if (featurep 'xemacs)
17584 (call-process "dvipng" nil nil nil
17589 (call-process "dvipng" nil nil nil
17592 ;;"-x" scale "-y" scale
17597 (if (not (file-exists-p pngfile))
17598 (if org-format-latex-signal-error
17599 (error "Failed to create png file from %s" texfile)
17600 (message "Failed to create png file from %s" texfile)
17602 ;; Use the requested file name and clean up
17603 (copy-file pngfile tofile 'replace)
17604 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
17605 (if (file-exists-p (concat texfilebase e))
17606 (delete-file (concat texfilebase e))))
17609 (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
17610 (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
17611 "This calls convert, which is included into imagemagick."
17612 (require 'org-latex)
17613 (let* ((tmpdir (if (featurep 'xemacs)
17615 temporary-file-directory))
17616 (texfilebase (make-temp-name
17617 (expand-file-name "orgtex" tmpdir)))
17618 (texfile (concat texfilebase ".tex"))
17619 (pdffile (concat texfilebase ".pdf"))
17620 (pngfile (concat texfilebase ".png"))
17621 (fnh (if (featurep 'xemacs)
17622 (font-height (face-font 'default))
17623 (face-attribute 'default :height nil)))
17624 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17625 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17626 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17628 (bg (or (plist-get options (if buffer :background :html-background))
17630 (if (eq fg 'default) (setq fg (org-latex-color :foreground))
17631 (setq fg (org-latex-color-format fg)))
17632 (if (eq bg 'default) (setq bg (org-latex-color :background))
17633 (setq bg (org-latex-color-format
17634 (if (string= bg "Transparent")(setq bg "white")))))
17635 (with-temp-file texfile
17636 (insert (org-splice-latex-header
17637 org-format-latex-header
17638 org-export-latex-default-packages-alist
17639 org-export-latex-packages-alist t
17640 org-format-latex-header-extra))
17641 (insert "\n\\begin{document}\n"
17642 "\\definecolor{fg}{rgb}{" fg "}\n"
17643 "\\definecolor{bg}{rgb}{" bg "}\n"
17644 "\n\\pagecolor{bg}\n"
17648 "\n\\end{document}\n" )
17649 (require 'org-latex)
17650 (org-export-latex-fix-inputenc))
17651 (let ((dir default-directory) cmd cmds latex-frags-cmds)
17652 (condition-case nil
17655 (setq cmds org-latex-to-pdf-process)
17657 (setq latex-frags-cmds (pop cmds))
17658 (if (listp latex-frags-cmds)
17660 (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
17661 (while latex-frags-cmds
17662 (setq cmd (pop latex-frags-cmds))
17663 (while (string-match "%b" cmd)
17664 (setq cmd (replace-match
17666 (shell-quote-argument texfile))
17668 (while (string-match "%f" cmd)
17669 (setq cmd (replace-match
17671 (shell-quote-argument (file-name-nondirectory texfile)))
17673 (while (string-match "%o" cmd)
17674 (setq cmd (replace-match
17676 (shell-quote-argument (file-name-directory texfile)))
17678 (setq cmd (split-string cmd))
17679 (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
17682 (if (not (file-exists-p pdffile))
17683 (progn (message "Failed to create pdf file from %s" texfile) nil)
17684 (condition-case nil
17685 (if (featurep 'xemacs)
17686 (call-process "convert" nil nil nil
17692 ;; "-sharpen" "0x1.0"
17694 (call-process "convert" nil nil nil
17700 ; "-sharpen" "0x1.0"
17703 (if (not (file-exists-p pngfile))
17704 (if org-format-latex-signal-error
17705 (error "Failed to create png file from %s" texfile)
17706 (message "Failed to create png file from %s" texfile)
17708 ;; Use the requested file name and clean up
17709 (copy-file pngfile tofile 'replace)
17710 (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
17711 (if (file-exists-p (concat texfilebase e))
17712 (delete-file (concat texfilebase e))))
17715 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
17716 "Fill a LaTeX header template TPL.
17717 In the template, the following place holders will be recognized:
17719 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
17720 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
17721 [PACKAGES] \\usepackage statements for PKG
17722 [NO-PACKAGES] do not include PKG
17723 [EXTRA] the string EXTRA
17724 [NO-EXTRA] do not include EXTRA
17726 For backward compatibility, if both the positive and the negative place
17727 holder is missing, the positive one (without the \"NO-\") will be
17728 assumed to be present at the end of the template.
17729 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
17731 SNIPPETS-P indicates if this is run to create snippet images for HTML."
17732 (let (rpl (end ""))
17733 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
17734 (setq rpl (if (or (match-end 1) (not def-pkg))
17735 "" (org-latex-packages-to-string def-pkg snippets-p t))
17736 tpl (replace-match rpl t t tpl))
17737 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
17739 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
17740 (setq rpl (if (or (match-end 1) (not pkg))
17741 "" (org-latex-packages-to-string pkg snippets-p t))
17742 tpl (replace-match rpl t t tpl))
17745 (org-latex-packages-to-string pkg snippets-p)))))
17747 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
17748 (setq rpl (if (or (match-end 1) (not extra))
17749 "" (concat extra "\n"))
17750 tpl (replace-match rpl t t tpl))
17751 (if (and extra (string-match "\\S-" extra))
17752 (setq end (concat end "\n" extra))))
17754 (if (string-match "\\S-" end)
17755 (concat tpl "\n" end)
17758 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
17759 "Turn an alist of packages into a string with the \\usepackage macros."
17760 (setq pkg (mapconcat (lambda(p)
17763 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
17764 (format "%% Package %s omitted" (cadr p)))
17765 ((equal "" (car p))
17766 (format "\\usepackage{%s}" (cadr p)))
17768 (format "\\usepackage[%s]{%s}"
17769 (car p) (cadr p)))))
17772 (if newline (concat pkg "\n") pkg))
17774 (defun org-dvipng-color (attr)
17775 "Return a RGB color specification for dvipng."
17776 (apply 'format "rgb %s %s %s"
17777 (mapcar 'org-normalize-color
17778 (if (featurep 'xemacs)
17779 (color-rgb-components
17780 (face-property 'default
17781 (cond ((eq attr :foreground) 'foreground)
17782 ((eq attr :background) 'background))))
17783 (color-values (face-attribute 'default attr nil))))))
17785 (defun org-latex-color (attr)
17786 "Return a RGB color for the LaTeX color package."
17787 (apply 'format "%s,%s,%s"
17788 (mapcar 'org-normalize-color
17789 (if (featurep 'xemacs)
17790 (color-rgb-components
17791 (face-property 'default
17792 (cond ((eq attr :foreground) 'foreground)
17793 ((eq attr :background) 'background))))
17794 (color-values (face-attribute 'default attr nil))))))
17796 (defun org-latex-color-format (color-name)
17797 "Convert COLOR-NAME to a RGB color value."
17798 (apply 'format "%s,%s,%s"
17799 (mapcar 'org-normalize-color
17800 (color-values color-name))))
17802 (defun org-normalize-color (value)
17803 "Return string to be used as color value for an RGB component."
17804 (format "%g" (/ value 65535.0)))
17809 (defvar org-inline-image-overlays nil)
17810 (make-variable-buffer-local 'org-inline-image-overlays)
17812 (defun org-toggle-inline-images (&optional include-linked)
17813 "Toggle the display of inline images.
17814 INCLUDE-LINKED is passed to `org-display-inline-images'."
17816 (if org-inline-image-overlays
17818 (org-remove-inline-images)
17819 (message "Inline image display turned off"))
17820 (org-display-inline-images include-linked)
17821 (if org-inline-image-overlays
17822 (message "%d images displayed inline"
17823 (length org-inline-image-overlays))
17824 (message "No images to display inline"))))
17826 (defun org-redisplay-inline-images ()
17827 "Refresh the display of inline images."
17829 (if (not org-inline-image-overlays)
17830 (org-toggle-inline-images)
17831 (org-toggle-inline-images)
17832 (org-toggle-inline-images)))
17834 (defun org-display-inline-images (&optional include-linked refresh beg end)
17835 "Display inline images.
17836 Normally only links without a description part are inlined, because this
17837 is how it will work for export. When INCLUDE-LINKED is set, also links
17838 with a description part will be inlined. This can be nice for a quick
17839 look at those images, but it does not reflect what exported files will look
17841 When REFRESH is set, refresh existing images between BEG and END.
17842 This will create new image displays only if necessary.
17843 BEG and END default to the buffer boundaries."
17846 (org-remove-inline-images)
17847 (if (fboundp 'clear-image-cache) (clear-image-cache)))
17851 (setq beg (or beg (point-min)) end (or end (point-max)))
17853 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
17854 (substring (org-image-file-name-regexp) 0 -2)
17855 "\\)\\]" (if include-linked "" "\\]")))
17857 (while (re-search-forward re end t)
17858 (setq old (get-char-property-and-overlay (match-beginning 1)
17859 'org-image-overlay))
17860 (setq file (expand-file-name
17861 (concat (or (match-string 3) "") (match-string 4))))
17862 (when (file-exists-p file)
17863 (if (and (car-safe old) refresh)
17864 (image-refresh (overlay-get (cdr old) 'display))
17865 (setq img (save-match-data (create-image file)))
17867 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
17868 (overlay-put ov 'display img)
17869 (overlay-put ov 'face 'default)
17870 (overlay-put ov 'org-image-overlay t)
17871 (overlay-put ov 'modification-hooks
17872 (list 'org-display-inline-modification-hook))
17873 (push ov org-inline-image-overlays)))))))))
17875 (defun org-display-inline-modification-hook (ov after beg end &optional len)
17876 "Remove inline-display overlay if a corresponding region is modified."
17877 (let ((inhibit-modification-hooks t))
17878 (when (and ov after)
17879 (delete ov org-inline-image-overlays)
17880 (delete-overlay ov))))
17882 (defun org-remove-inline-images ()
17883 "Remove inline display of images."
17885 (mapc 'delete-overlay org-inline-image-overlays)
17886 (setq org-inline-image-overlays nil))
17890 ;; Outline functions from `outline-mode-prefix-map'
17891 ;; that can be remapped in Org:
17892 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
17893 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
17894 (define-key org-mode-map [remap outline-forward-same-level]
17895 'org-forward-heading-same-level)
17896 (define-key org-mode-map [remap outline-backward-same-level]
17897 'org-backward-heading-same-level)
17898 (define-key org-mode-map [remap show-branches]
17899 'org-kill-note-or-show-branches)
17900 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
17901 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
17902 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
17904 ;; Outline functions from `outline-mode-prefix-map' that can not
17905 ;; be remapped in Org:
17907 ;; - the column "key binding" shows whether the Outline function is still
17908 ;; available in Org mode on the same key that it has been bound to in
17910 ;; - "overridden": key used for a different functionality in Org mode
17911 ;; - else: key still bound to the same Outline function in Org mode
17913 ;; | Outline function | key binding | Org replacement |
17914 ;; |------------------------------------+-------------+-----------------------|
17915 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
17916 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
17917 ;; | `outline-up-heading' | `C-c C-u' | still same function |
17918 ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
17919 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
17920 ;; | `show-entry' | overridden | no replacement |
17921 ;; | `show-children' | `C-c C-i' | visibility cycling |
17922 ;; | `show-branches' | `C-c C-k' | still same function |
17923 ;; | `show-subtree' | overridden | visibility cycling |
17924 ;; | `show-all' | overridden | no replacement |
17925 ;; | `hide-subtree' | overridden | visibility cycling |
17926 ;; | `hide-body' | overridden | no replacement |
17927 ;; | `hide-entry' | overridden | visibility cycling |
17928 ;; | `hide-leaves' | overridden | no replacement |
17929 ;; | `hide-sublevels' | overridden | no replacement |
17930 ;; | `hide-other' | overridden | no replacement |
17932 ;; Make `C-c C-x' a prefix key
17933 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
17935 ;; TAB key with modifiers
17936 (org-defkey org-mode-map "\C-i" 'org-cycle)
17937 (org-defkey org-mode-map [(tab)] 'org-cycle)
17938 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
17939 (org-defkey org-mode-map "\M-\t" 'pcomplete)
17940 ;; The following line is necessary under Suse GNU/Linux
17941 (unless (featurep 'xemacs)
17942 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
17943 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
17944 (define-key org-mode-map [backtab] 'org-shifttab)
17946 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
17947 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
17948 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
17950 ;; Cursor keys with modifiers
17951 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
17952 (org-defkey org-mode-map [(meta right)] 'org-metaright)
17953 (org-defkey org-mode-map [(meta up)] 'org-metaup)
17954 (org-defkey org-mode-map [(meta down)] 'org-metadown)
17956 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
17957 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
17958 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
17959 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
17961 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
17962 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
17963 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
17964 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
17966 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
17967 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
17968 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
17969 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
17972 (define-key org-mode-map org-babel-key-prefix org-babel-map)
17973 (mapc (lambda (pair)
17974 (define-key org-babel-map (car pair) (cdr pair)))
17975 org-babel-key-bindings)
17977 ;;; Extra keys for tty access.
17978 ;; We only set them when really needed because otherwise the
17979 ;; menus don't show the simple keys
17981 (when (or org-use-extra-keys
17982 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
17983 (not window-system))
17984 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
17985 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
17986 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
17987 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
17988 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
17989 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
17990 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
17991 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
17992 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
17993 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
17994 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
17995 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
17996 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
17997 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
17998 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
17999 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
18000 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
18001 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
18002 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
18003 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
18004 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
18005 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
18006 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
18007 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
18008 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
18009 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
18010 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
18011 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
18013 ;; All the other keys
18015 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
18016 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
18017 (if (boundp 'narrow-map)
18018 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
18019 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
18020 (if (boundp 'narrow-map)
18021 (org-defkey narrow-map "b" 'org-narrow-to-block)
18022 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
18023 (if (boundp 'narrow-map)
18024 (org-defkey narrow-map "e" 'org-narrow-to-element)
18025 (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
18026 (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
18027 (org-defkey org-mode-map "\M-}" 'org-forward-element)
18028 (org-defkey org-mode-map "\M-{" 'org-backward-element)
18029 (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
18030 (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
18031 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
18032 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
18033 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
18034 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
18035 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
18036 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
18037 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
18038 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
18039 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
18040 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
18041 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
18042 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
18043 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
18044 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
18045 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
18046 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
18047 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
18048 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
18049 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
18050 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
18051 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
18052 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
18053 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
18054 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
18055 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
18056 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
18057 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
18058 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
18059 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
18060 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
18061 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
18062 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
18063 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
18064 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
18065 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
18066 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
18067 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
18068 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
18069 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
18070 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
18071 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
18072 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
18073 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
18074 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
18075 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
18076 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
18077 (org-defkey org-mode-map "\C-c^" 'org-sort)
18078 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
18079 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
18080 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
18081 (org-defkey org-mode-map "\C-m" 'org-return)
18082 (org-defkey org-mode-map "\C-j" 'org-return-indent)
18083 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
18084 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
18085 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
18086 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
18087 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
18088 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
18089 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
18090 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
18091 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
18092 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
18093 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
18094 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
18095 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
18096 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
18097 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
18098 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
18099 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
18100 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
18101 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
18102 (org-defkey org-mode-map "\M-h" 'org-mark-element)
18103 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
18104 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
18106 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
18107 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
18108 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
18109 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
18111 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
18112 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
18113 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
18114 (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
18115 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
18116 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
18117 (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
18118 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
18119 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
18120 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
18121 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
18122 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
18123 (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
18124 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
18125 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
18126 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
18127 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
18128 (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
18129 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
18130 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
18131 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
18132 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
18134 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
18135 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
18136 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
18137 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
18138 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
18140 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
18142 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
18144 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
18145 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
18147 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
18150 (when (featurep 'xemacs)
18151 (org-defkey org-mode-map 'button3 'popup-mode-menu))
18154 (defconst org-speed-commands-default
18156 ("Outline Navigation")
18157 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
18158 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
18159 ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
18160 ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
18161 ("u" . (org-speed-move-safe 'outline-up-heading))
18163 ("g" . (org-refile t))
18164 ("Outline Visibility")
18166 ("C" . org-shifttab)
18167 (" " . org-display-outline-path)
18168 (":" . org-columns)
18169 ("Outline Structure Editing")
18170 ("U" . org-shiftmetaup)
18171 ("D" . org-shiftmetadown)
18172 ("r" . org-metaright)
18173 ("l" . org-metaleft)
18174 ("R" . org-shiftmetaright)
18175 ("L" . org-shiftmetaleft)
18176 ("i" . (progn (forward-char 1) (call-interactively
18177 'org-insert-heading-respect-content)))
18180 ("a" . org-archive-subtree-default-with-confirmation)
18181 ("." . org-mark-subtree)
18182 ("#" . org-toggle-comment)
18184 ("I" . org-clock-in)
18185 ("O" . org-clock-out)
18186 ("Meta Data Editing")
18188 ("0" . (org-priority ?\ ))
18189 ("1" . (org-priority ?A))
18190 ("2" . (org-priority ?B))
18191 ("3" . (org-priority ?C))
18192 (";" . org-set-tags-command)
18193 ("e" . org-set-effort)
18194 ("E" . org-inc-effort)
18195 ("W" . (lambda(m) (interactive "sMinutes before warning: ")
18196 (org-entry-put (point) "APPT_WARNTIME" m)))
18197 ("Agenda Views etc")
18199 ("/" . org-sparse-tree)
18201 ("o" . org-open-at-point)
18202 ("?" . org-speed-command-help)
18203 ("<" . (org-agenda-set-restriction-lock 'subtree))
18204 (">" . (org-agenda-remove-restriction-lock))
18206 "The default speed commands.")
18208 (defun org-print-speed-command (e)
18209 (if (> (length (car e)) 1)
18214 (princ (make-string (length (car e)) ?-))
18218 (if (symbolp (cdr e))
18219 (princ (symbol-name (cdr e)))
18223 (defun org-speed-command-help ()
18224 "Show the available speed commands."
18226 (if (not org-use-speed-commands)
18227 (error "Speed commands are not activated, customize `org-use-speed-commands'")
18228 (with-output-to-temp-buffer "*Help*"
18229 (princ "User-defined Speed commands\n===========================\n")
18230 (mapc 'org-print-speed-command org-speed-commands-user)
18232 (princ "Built-in Speed commands\n=======================\n")
18233 (mapc 'org-print-speed-command org-speed-commands-default))
18234 (with-current-buffer "*Help*"
18235 (setq truncate-lines t))))
18237 (defun org-speed-move-safe (cmd)
18238 "Execute CMD, but make sure that the cursor always ends up in a headline.
18239 If not, return to the original position and throw an error."
18241 (let ((pos (point)))
18242 (call-interactively cmd)
18243 (unless (and (bolp) (org-at-heading-p))
18245 (error "Boundary reached while executing %s" cmd))))
18247 (defvar org-self-insert-command-undo-counter 0)
18249 (defvar org-table-auto-blank-field) ; defined in org-table.el
18250 (defvar org-speed-command nil)
18252 (defun org-speed-command-default-hook (keys)
18253 "Hook for activating single-letter speed commands.
18254 `org-speed-commands-default' specifies a minimal command set.
18255 Use `org-speed-commands-user' for further customization."
18256 (when (or (and (bolp) (looking-at org-outline-regexp))
18257 (and (functionp org-use-speed-commands)
18258 (funcall org-use-speed-commands)))
18259 (cdr (assoc keys (append org-speed-commands-user
18260 org-speed-commands-default)))))
18262 (defun org-babel-speed-command-hook (keys)
18263 "Hook for activating single-letter code block commands."
18264 (when (and (bolp) (looking-at org-babel-src-block-regexp))
18265 (cdr (assoc keys org-babel-key-bindings))))
18267 (defcustom org-speed-command-hook
18268 '(org-speed-command-default-hook org-babel-speed-command-hook)
18269 "Hook for activating speed commands at strategic locations.
18270 Hook functions are called in sequence until a valid handler is
18273 Each hook takes a single argument, a user-pressed command key
18274 which is also a `self-insert-command' from the global map.
18276 Within the hook, examine the cursor position and the command key
18277 and return nil or a valid handler as appropriate. Handler could
18278 be one of an interactive command, a function, or a form.
18280 Set `org-use-speed-commands' to non-nil value to enable this
18281 hook. The default setting is `org-speed-command-default-hook'."
18282 :group 'org-structure
18286 (defun org-self-insert-command (N)
18287 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
18288 If the cursor is in a table looking at whitespace, the whitespace is
18289 overwritten, and the table is not marked as requiring realignment."
18291 (org-check-before-invisible-edit 'insert)
18293 ((and org-use-speed-commands
18294 (setq org-speed-command
18295 (run-hook-with-args-until-success
18296 'org-speed-command-hook (this-command-keys))))
18298 ((commandp org-speed-command)
18299 (setq this-command org-speed-command)
18300 (call-interactively org-speed-command))
18301 ((functionp org-speed-command)
18302 (funcall org-speed-command))
18303 ((and org-speed-command (listp org-speed-command))
18304 (eval org-speed-command))
18305 (t (let (org-use-speed-commands)
18306 (call-interactively 'org-self-insert-command)))))
18310 ;; check if we blank the field, and if that triggers align
18311 (and (featurep 'org-table) org-table-auto-blank-field
18312 (member last-command
18313 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
18314 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
18315 ;; got extra space, this field does not determine column width
18316 (let (org-table-may-need-update) (org-table-blank-field))
18317 ;; no extra space, this field may determine column width
18318 (org-table-blank-field)))
18321 (looking-at "[^|\n]* |"))
18322 (let (org-table-may-need-update)
18323 (goto-char (1- (match-end 0)))
18324 (backward-delete-char 1)
18325 (goto-char (match-beginning 0))
18326 (self-insert-command N)))
18328 (setq org-table-may-need-update t)
18329 (self-insert-command N)
18330 (org-fix-tags-on-the-fly)
18331 (if org-self-insert-cluster-for-undo
18332 (if (not (eq last-command 'org-self-insert-command))
18333 (setq org-self-insert-command-undo-counter 1)
18334 (if (>= org-self-insert-command-undo-counter 20)
18335 (setq org-self-insert-command-undo-counter 1)
18336 (and (> org-self-insert-command-undo-counter 0)
18337 buffer-undo-list (listp buffer-undo-list)
18338 (not (cadr buffer-undo-list)) ; remove nil entry
18339 (setcdr buffer-undo-list (cddr buffer-undo-list)))
18340 (setq org-self-insert-command-undo-counter
18341 (1+ org-self-insert-command-undo-counter))))))))
18343 (defun org-check-before-invisible-edit (kind)
18344 "Check is editing if kind KIND would be dangerous with invisible text around.
18345 The detailed reaction depends on the user option `org-catch-invisible-edits'."
18346 ;; First, try to get out of here as quickly as possible, to reduce overhead
18347 (if (and org-catch-invisible-edits
18348 (or (not (boundp 'visible-mode)) (not visible-mode))
18349 (or (get-char-property (point) 'invisible)
18350 (get-char-property (max (point-min) (1- (point))) 'invisible)))
18351 ;; OK, we need to take a closer look
18352 (let* ((invisible-at-point (get-char-property (point) 'invisible))
18353 (invisible-before-point (if (bobp) nil (get-char-property
18354 (1- (point)) 'invisible)))
18355 (border-and-ok-direction
18357 ;; Check if we are acting predictably before invisible text
18358 (and invisible-at-point (not invisible-before-point)
18359 (memq kind '(insert delete-backward)))
18360 ;; Check if we are acting predictably after invisible text
18361 ;; This works not well, and I have turned it off. It seems
18362 ;; better to always show and stop after invisible text.
18363 ;; (and (not invisible-at-point) invisible-before-point
18364 ;; (memq kind '(insert delete)))
18366 (when (or (memq invisible-at-point '(outline org-hide-block t))
18367 (memq invisible-before-point '(outline org-hide-block t)))
18368 (if (eq org-catch-invisible-edits 'error)
18369 (error "Editing in invisible areas is prohibited - make visible first"))
18370 (if (and org-custom-properties-overlays
18371 (y-or-n-p "Display invisible properties in this buffer? "))
18372 (org-toggle-custom-properties-visibility)
18373 ;; Make the area visible
18375 (if invisible-before-point
18376 (goto-char (previous-single-char-property-change
18377 (point) 'invisible)))
18380 ((eq org-catch-invisible-edits 'show)
18381 ;; That's it, we do the edit after showing
18383 "Unfolding invisible region around point before editing")
18385 ((and (eq org-catch-invisible-edits 'smart)
18386 border-and-ok-direction)
18387 (message "Unfolding invisible region around point before editing"))
18389 ;; Don't do the edit, make the user repeat it in full visibility
18390 (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
18392 (defun org-fix-tags-on-the-fly ()
18393 (when (and (equal (char-after (point-at-bol)) ?*)
18394 (org-at-heading-p))
18395 (org-align-tags-here org-tags-column)))
18397 (defun org-delete-backward-char (N)
18398 "Like `delete-backward-char', insert whitespace at field end in tables.
18399 When deleting backwards, in tables this function will insert whitespace in
18400 front of the next \"|\" separator, to keep the table aligned. The table will
18401 still be marked for re-alignment if the field did fill the entire column,
18402 because, in this case the deletion might narrow the column."
18404 (org-check-before-invisible-edit 'delete-backward)
18405 (if (and (org-table-p)
18407 (string-match "|" (buffer-substring (point-at-bol) (point)))
18408 (looking-at ".*?|"))
18409 (let ((pos (point))
18410 (noalign (looking-at "[^|\n\r]* |"))
18411 (c org-table-may-need-update))
18412 (backward-delete-char N)
18413 (if (not overwrite-mode)
18415 (skip-chars-forward "^|")
18417 (goto-char (1- pos))))
18418 ;; noalign: if there were two spaces at the end, this field
18419 ;; does not determine the width of the column.
18420 (if noalign (setq org-table-may-need-update c)))
18421 (backward-delete-char N)
18422 (org-fix-tags-on-the-fly)))
18424 (defun org-delete-char (N)
18425 "Like `delete-char', but insert whitespace at field end in tables.
18426 When deleting characters, in tables this function will insert whitespace in
18427 front of the next \"|\" separator, to keep the table aligned. The table will
18428 still be marked for re-alignment if the field did fill the entire column,
18429 because, in this case the deletion might narrow the column."
18431 (org-check-before-invisible-edit 'delete)
18432 (if (and (org-table-p)
18434 (not (= (char-after) ?|))
18436 (if (looking-at ".*?|")
18437 (let ((pos (point))
18438 (noalign (looking-at "[^|\n\r]* |"))
18439 (c org-table-may-need-update))
18440 (replace-match (concat
18441 (substring (match-string 0) 1 -1)
18444 ;; noalign: if there were two spaces at the end, this field
18445 ;; does not determine the width of the column.
18446 (if noalign (setq org-table-may-need-update c)))
18449 (org-fix-tags-on-the-fly)))
18451 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
18452 (put 'org-self-insert-command 'delete-selection t)
18453 (put 'orgtbl-self-insert-command 'delete-selection t)
18454 (put 'org-delete-char 'delete-selection 'supersede)
18455 (put 'org-delete-backward-char 'delete-selection 'supersede)
18456 (put 'org-yank 'delete-selection 'yank)
18458 ;; Make `flyspell-mode' delay after some commands
18459 (put 'org-self-insert-command 'flyspell-delayed t)
18460 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
18461 (put 'org-delete-char 'flyspell-delayed t)
18462 (put 'org-delete-backward-char 'flyspell-delayed t)
18464 ;; Make pabbrev-mode expand after org-mode commands
18465 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
18466 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
18468 ;; How to do this: Measure non-white length of current string
18469 ;; If equal to column width, we should realign.
18471 (defun org-remap (map &rest commands)
18472 "In MAP, remap the functions given in COMMANDS.
18473 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
18476 (setq old (pop commands) new (pop commands))
18477 (if (fboundp 'command-remapping)
18478 (org-defkey map (vector 'remap old) new)
18479 (substitute-key-definition old new map global-map)))))
18481 (when (eq org-enable-table-editor 'optimized)
18482 ;; If the user wants maximum table support, we need to hijack
18483 ;; some standard editing functions
18484 (org-remap org-mode-map
18485 'self-insert-command 'org-self-insert-command
18486 'delete-char 'org-delete-char
18487 'delete-backward-char 'org-delete-backward-char)
18488 (org-defkey org-mode-map "|" 'org-force-self-insert))
18490 (defvar org-ctrl-c-ctrl-c-hook nil
18491 "Hook for functions attaching themselves to `C-c C-c'.
18493 This can be used to add additional functionality to the C-c C-c
18494 key which executes context-dependent commands. This hook is run
18495 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
18496 run after the last test.
18498 Each function will be called with no arguments. The function
18499 must check if the context is appropriate for it to act. If yes,
18500 it should do its thing and then return a non-nil value. If the
18501 context is wrong, just do nothing and return nil.")
18503 (defvar org-ctrl-c-ctrl-c-final-hook nil
18504 "Hook for functions attaching themselves to `C-c C-c'.
18506 This can be used to add additional functionality to the C-c C-c
18507 key which executes context-dependent commands. This hook is run
18508 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
18509 before the first test.
18511 Each function will be called with no arguments. The function
18512 must check if the context is appropriate for it to act. If yes,
18513 it should do its thing and then return a non-nil value. If the
18514 context is wrong, just do nothing and return nil.")
18516 (defvar org-tab-first-hook nil
18517 "Hook for functions to attach themselves to TAB.
18518 See `org-ctrl-c-ctrl-c-hook' for more information.
18519 This hook runs as the first action when TAB is pressed, even before
18520 `org-cycle' messes around with the `outline-regexp' to cater for
18521 inline tasks and plain list item folding.
18522 If any function in this hook returns t, any other actions that
18523 would have been caused by TAB (such as table field motion or visibility
18524 cycling) will not occur.")
18526 (defvar org-tab-after-check-for-table-hook nil
18527 "Hook for functions to attach themselves to TAB.
18528 See `org-ctrl-c-ctrl-c-hook' for more information.
18529 This hook runs after it has been established that the cursor is not in a
18530 table, but before checking if the cursor is in a headline or if global cycling
18532 If any function in this hook returns t, not other actions like visibility
18533 cycling will be done.")
18535 (defvar org-tab-after-check-for-cycling-hook nil
18536 "Hook for functions to attach themselves to TAB.
18537 See `org-ctrl-c-ctrl-c-hook' for more information.
18538 This hook runs after it has been established that not table field motion and
18539 not visibility should be done because of current context. This is probably
18540 the place where a package like yasnippets can hook in.")
18542 (defvar org-tab-before-tab-emulation-hook nil
18543 "Hook for functions to attach themselves to TAB.
18544 See `org-ctrl-c-ctrl-c-hook' for more information.
18545 This hook runs after every other options for TAB have been exhausted, but
18546 before indentation and \t insertion takes place.")
18548 (defvar org-metaleft-hook nil
18549 "Hook for functions attaching themselves to `M-left'.
18550 See `org-ctrl-c-ctrl-c-hook' for more information.")
18551 (defvar org-metaright-hook nil
18552 "Hook for functions attaching themselves to `M-right'.
18553 See `org-ctrl-c-ctrl-c-hook' for more information.")
18554 (defvar org-metaup-hook nil
18555 "Hook for functions attaching themselves to `M-up'.
18556 See `org-ctrl-c-ctrl-c-hook' for more information.")
18557 (defvar org-metadown-hook nil
18558 "Hook for functions attaching themselves to `M-down'.
18559 See `org-ctrl-c-ctrl-c-hook' for more information.")
18560 (defvar org-shiftmetaleft-hook nil
18561 "Hook for functions attaching themselves to `M-S-left'.
18562 See `org-ctrl-c-ctrl-c-hook' for more information.")
18563 (defvar org-shiftmetaright-hook nil
18564 "Hook for functions attaching themselves to `M-S-right'.
18565 See `org-ctrl-c-ctrl-c-hook' for more information.")
18566 (defvar org-shiftmetaup-hook nil
18567 "Hook for functions attaching themselves to `M-S-up'.
18568 See `org-ctrl-c-ctrl-c-hook' for more information.")
18569 (defvar org-shiftmetadown-hook nil
18570 "Hook for functions attaching themselves to `M-S-down'.
18571 See `org-ctrl-c-ctrl-c-hook' for more information.")
18572 (defvar org-metareturn-hook nil
18573 "Hook for functions attaching themselves to `M-RET'.
18574 See `org-ctrl-c-ctrl-c-hook' for more information.")
18575 (defvar org-shiftup-hook nil
18576 "Hook for functions attaching themselves to `S-up'.
18577 See `org-ctrl-c-ctrl-c-hook' for more information.")
18578 (defvar org-shiftup-final-hook nil
18579 "Hook for functions attaching themselves to `S-up'.
18580 This one runs after all other options except shift-select have been excluded.
18581 See `org-ctrl-c-ctrl-c-hook' for more information.")
18582 (defvar org-shiftdown-hook nil
18583 "Hook for functions attaching themselves to `S-down'.
18584 See `org-ctrl-c-ctrl-c-hook' for more information.")
18585 (defvar org-shiftdown-final-hook nil
18586 "Hook for functions attaching themselves to `S-down'.
18587 This one runs after all other options except shift-select have been excluded.
18588 See `org-ctrl-c-ctrl-c-hook' for more information.")
18589 (defvar org-shiftleft-hook nil
18590 "Hook for functions attaching themselves to `S-left'.
18591 See `org-ctrl-c-ctrl-c-hook' for more information.")
18592 (defvar org-shiftleft-final-hook nil
18593 "Hook for functions attaching themselves to `S-left'.
18594 This one runs after all other options except shift-select have been excluded.
18595 See `org-ctrl-c-ctrl-c-hook' for more information.")
18596 (defvar org-shiftright-hook nil
18597 "Hook for functions attaching themselves to `S-right'.
18598 See `org-ctrl-c-ctrl-c-hook' for more information.")
18599 (defvar org-shiftright-final-hook nil
18600 "Hook for functions attaching themselves to `S-right'.
18601 This one runs after all other options except shift-select have been excluded.
18602 See `org-ctrl-c-ctrl-c-hook' for more information.")
18604 (defun org-modifier-cursor-error ()
18605 "Throw an error, a modified cursor command was applied in wrong context."
18606 (error "This command is active in special context like tables, headlines or items"))
18608 (defun org-shiftselect-error ()
18609 "Throw an error because Shift-Cursor command was applied in wrong context."
18610 (if (and (boundp 'shift-select-mode) shift-select-mode)
18611 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
18612 (error "This command works only in special context like headlines or timestamps")))
18614 (defun org-call-for-shift-select (cmd)
18615 (let ((this-command-keys-shift-translated t))
18616 (call-interactively cmd)))
18618 (defun org-shifttab (&optional arg)
18619 "Global visibility cycling or move to previous table field.
18620 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
18622 See the individual commands for more information."
18625 ((org-at-table-p) (call-interactively 'org-table-previous-field))
18627 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
18628 (message "Content view to level: %d" arg)
18629 (org-content (prefix-numeric-value arg2))
18630 (setq org-cycle-global-status 'overview)))
18631 (t (call-interactively 'org-global-cycle))))
18633 (defun org-shiftmetaleft ()
18634 "Promote subtree or delete table column.
18635 Calls `org-promote-subtree', `org-outdent-item-tree', or
18636 `org-table-delete-column', depending on context. See the
18637 individual commands for more information."
18640 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
18641 ((org-at-table-p) (call-interactively 'org-table-delete-column))
18642 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
18643 ((if (not (org-region-active-p)) (org-at-item-p)
18644 (save-excursion (goto-char (region-beginning))
18646 (call-interactively 'org-outdent-item-tree))
18647 (t (org-modifier-cursor-error))))
18649 (defun org-shiftmetaright ()
18650 "Demote subtree or insert table column.
18651 Calls `org-demote-subtree', `org-indent-item-tree', or
18652 `org-table-insert-column', depending on context. See the
18653 individual commands for more information."
18656 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
18657 ((org-at-table-p) (call-interactively 'org-table-insert-column))
18658 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
18659 ((if (not (org-region-active-p)) (org-at-item-p)
18660 (save-excursion (goto-char (region-beginning))
18662 (call-interactively 'org-indent-item-tree))
18663 (t (org-modifier-cursor-error))))
18665 (defun org-shiftmetaup (&optional arg)
18666 "Move subtree up or kill table row.
18667 Calls `org-move-subtree-up' or `org-table-kill-row' or
18668 `org-move-item-up' or `org-timestamp-up', depending on context.
18669 See the individual commands for more information."
18672 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
18673 ((org-at-table-p) (call-interactively 'org-table-kill-row))
18674 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18675 ((org-at-item-p) (call-interactively 'org-move-item-up))
18676 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18677 (call-interactively 'org-timestamp-up)))
18678 (t (org-modifier-cursor-error))))
18680 (defun org-shiftmetadown (&optional arg)
18681 "Move subtree down or insert table row.
18682 Calls `org-move-subtree-down' or `org-table-insert-row' or
18683 `org-move-item-down' or `org-timestamp-up', depending on context.
18684 See the individual commands for more information."
18687 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
18688 ((org-at-table-p) (call-interactively 'org-table-insert-row))
18689 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18690 ((org-at-item-p) (call-interactively 'org-move-item-down))
18691 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18692 (call-interactively 'org-timestamp-down)))
18693 (t (org-modifier-cursor-error))))
18695 (defsubst org-hidden-tree-error ()
18697 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
18699 (defun org-metaleft (&optional arg)
18700 "Promote heading or move table column to left.
18701 Calls `org-do-promote' or `org-table-move-column', depending on context.
18702 With no specific context, calls the Emacs default `backward-word'.
18703 See the individual commands for more information."
18706 ((run-hook-with-args-until-success 'org-metaleft-hook))
18707 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
18708 ((org-with-limited-levels
18709 (or (org-at-heading-p)
18710 (and (org-region-active-p)
18712 (goto-char (region-beginning))
18713 (org-at-heading-p)))))
18714 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18715 (call-interactively 'org-do-promote))
18716 ;; At an inline task.
18717 ((org-at-heading-p)
18718 (call-interactively 'org-inlinetask-promote))
18719 ((or (org-at-item-p)
18720 (and (org-region-active-p)
18722 (goto-char (region-beginning))
18724 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18725 (call-interactively 'org-outdent-item))
18726 (t (call-interactively 'backward-word))))
18728 (defun org-metaright (&optional arg)
18729 "Demote a subtree, a list item or move table column to right.
18730 In front of a drawer or a block keyword, indent it correctly.
18731 With no specific context, calls the Emacs default `forward-word'.
18732 See the individual commands for more information."
18735 ((run-hook-with-args-until-success 'org-metaright-hook))
18736 ((org-at-table-p) (call-interactively 'org-table-move-column))
18737 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
18738 ((org-at-block-p) (call-interactively 'org-indent-block))
18739 ((org-with-limited-levels
18740 (or (org-at-heading-p)
18741 (and (org-region-active-p)
18743 (goto-char (region-beginning))
18744 (org-at-heading-p)))))
18745 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18746 (call-interactively 'org-do-demote))
18747 ;; At an inline task.
18748 ((org-at-heading-p)
18749 (call-interactively 'org-inlinetask-demote))
18750 ((or (org-at-item-p)
18751 (and (org-region-active-p)
18753 (goto-char (region-beginning))
18755 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18756 (call-interactively 'org-indent-item))
18757 (t (call-interactively 'forward-word))))
18759 (defun org-check-for-hidden (what)
18760 "Check if there are hidden headlines/items in the current visual line.
18761 WHAT can be either `headlines' or `items'. If the current line is
18762 an outline or item heading and it has a folded subtree below it,
18763 this function returns t, nil otherwise."
18765 ((eq what 'headlines) org-outline-regexp-bol)
18766 ((eq what 'items) (org-item-beginning-re))
18767 (t (error "This should not happen"))))
18771 (unless (org-region-active-p)
18772 (setq beg (point-at-bol))
18773 (beginning-of-line 2)
18774 (while (and (not (eobp)) ;; this is like `next-line'
18775 (get-char-property (1- (point)) 'invisible))
18776 (beginning-of-line 2))
18779 (goto-char (point-at-eol))
18780 (setq end (max end (point)))
18781 (while (re-search-forward re end t)
18782 (if (get-char-property (match-beginning 0) 'invisible)
18786 (autoload 'org-element-at-point "org-element")
18788 (declare-function org-element-at-point "org-element" (&optional keep-trail))
18789 (declare-function org-element-type "org-element" (element))
18790 (declare-function org-element-contents "org-element" (element))
18791 (declare-function org-element-property "org-element" (property element))
18792 (declare-function org-element-paragraph-parser "org-element" (limit))
18793 (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
18794 (declare-function org-element-nested-p "org-element" (elem-a elem-b))
18795 (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
18796 (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
18797 (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
18799 (defun org-metaup (&optional arg)
18800 "Move subtree up or move table row up.
18801 Calls `org-move-subtree-up' or `org-table-move-row' or
18802 `org-move-item-up', depending on context. See the individual commands
18803 for more information."
18806 ((run-hook-with-args-until-success 'org-metaup-hook))
18807 ((org-region-active-p)
18808 (let* ((a (min (region-beginning) (region-end)))
18809 (b (1- (max (region-beginning) (region-end))))
18810 (c (save-excursion (goto-char a)
18811 (move-beginning-of-line 0)))
18812 (d (save-excursion (goto-char a)
18813 (move-end-of-line 0) (point))))
18814 (transpose-regions a b c d)
18816 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
18817 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18818 ((org-at-item-p) (call-interactively 'org-move-item-up))
18819 (t (org-drag-element-backward))))
18821 (defun org-metadown (&optional arg)
18822 "Move subtree down or move table row down.
18823 Calls `org-move-subtree-down' or `org-table-move-row' or
18824 `org-move-item-down', depending on context. See the individual
18825 commands for more information."
18828 ((run-hook-with-args-until-success 'org-metadown-hook))
18829 ((org-region-active-p)
18830 (let* ((a (min (region-beginning) (region-end)))
18831 (b (max (region-beginning) (region-end)))
18832 (c (save-excursion (goto-char b)
18833 (move-beginning-of-line 1)))
18834 (d (save-excursion (goto-char b)
18835 (move-end-of-line 1) (1+ (point)))))
18836 (transpose-regions a b c d)
18838 ((org-at-table-p) (call-interactively 'org-table-move-row))
18839 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18840 ((org-at-item-p) (call-interactively 'org-move-item-down))
18841 (t (org-drag-element-forward))))
18843 (defun org-shiftup (&optional arg)
18844 "Increase item in timestamp or increase priority of current headline.
18845 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
18846 depending on context. See the individual commands for more information."
18849 ((run-hook-with-args-until-success 'org-shiftup-hook))
18850 ((and org-support-shift-select (org-region-active-p))
18851 (org-call-for-shift-select 'previous-line))
18852 ((org-at-timestamp-p t)
18853 (call-interactively (if org-edit-timestamp-down-means-later
18854 'org-timestamp-down 'org-timestamp-up)))
18855 ((and (not (eq org-support-shift-select 'always))
18856 org-enable-priority-commands
18857 (org-at-heading-p))
18858 (call-interactively 'org-priority-up))
18859 ((and (not org-support-shift-select) (org-at-item-p))
18860 (call-interactively 'org-previous-item))
18861 ((org-clocktable-try-shift 'up arg))
18862 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
18863 (org-support-shift-select
18864 (org-call-for-shift-select 'previous-line))
18865 (t (org-shiftselect-error))))
18867 (defun org-shiftdown (&optional arg)
18868 "Decrease item in timestamp or decrease priority of current headline.
18869 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
18870 depending on context. See the individual commands for more information."
18873 ((run-hook-with-args-until-success 'org-shiftdown-hook))
18874 ((and org-support-shift-select (org-region-active-p))
18875 (org-call-for-shift-select 'next-line))
18876 ((org-at-timestamp-p t)
18877 (call-interactively (if org-edit-timestamp-down-means-later
18878 'org-timestamp-up 'org-timestamp-down)))
18879 ((and (not (eq org-support-shift-select 'always))
18880 org-enable-priority-commands
18881 (org-at-heading-p))
18882 (call-interactively 'org-priority-down))
18883 ((and (not org-support-shift-select) (org-at-item-p))
18884 (call-interactively 'org-next-item))
18885 ((org-clocktable-try-shift 'down arg))
18886 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
18887 (org-support-shift-select
18888 (org-call-for-shift-select 'next-line))
18889 (t (org-shiftselect-error))))
18891 (defun org-shiftright (&optional arg)
18892 "Cycle the thing at point or in the current line, depending on context.
18893 Depending on context, this does one of the following:
18895 - switch a timestamp at point one day into the future
18896 - on a headline, switch to the next TODO keyword.
18897 - on an item, switch entire list to the next bullet type
18898 - on a property line, switch to the next allowed value
18899 - on a clocktable definition line, move time block into the future"
18902 ((run-hook-with-args-until-success 'org-shiftright-hook))
18903 ((and org-support-shift-select (org-region-active-p))
18904 (org-call-for-shift-select 'forward-char))
18905 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
18906 ((and (not (eq org-support-shift-select 'always))
18907 (org-at-heading-p))
18908 (let ((org-inhibit-logging
18909 (not org-treat-S-cursor-todo-selection-as-state-change))
18910 (org-inhibit-blocking
18911 (not org-treat-S-cursor-todo-selection-as-state-change)))
18912 (org-call-with-arg 'org-todo 'right)))
18913 ((or (and org-support-shift-select
18914 (not (eq org-support-shift-select 'always))
18915 (org-at-item-bullet-p))
18916 (and (not org-support-shift-select) (org-at-item-p)))
18917 (org-call-with-arg 'org-cycle-list-bullet nil))
18918 ((and (not (eq org-support-shift-select 'always))
18919 (org-at-property-p))
18920 (call-interactively 'org-property-next-allowed-value))
18921 ((org-clocktable-try-shift 'right arg))
18922 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
18923 (org-support-shift-select
18924 (org-call-for-shift-select 'forward-char))
18925 (t (org-shiftselect-error))))
18927 (defun org-shiftleft (&optional arg)
18928 "Cycle the thing at point or in the current line, depending on context.
18929 Depending on context, this does one of the following:
18931 - switch a timestamp at point one day into the past
18932 - on a headline, switch to the previous TODO keyword.
18933 - on an item, switch entire list to the previous bullet type
18934 - on a property line, switch to the previous allowed value
18935 - on a clocktable definition line, move time block into the past"
18938 ((run-hook-with-args-until-success 'org-shiftleft-hook))
18939 ((and org-support-shift-select (org-region-active-p))
18940 (org-call-for-shift-select 'backward-char))
18941 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
18942 ((and (not (eq org-support-shift-select 'always))
18943 (org-at-heading-p))
18944 (let ((org-inhibit-logging
18945 (not org-treat-S-cursor-todo-selection-as-state-change))
18946 (org-inhibit-blocking
18947 (not org-treat-S-cursor-todo-selection-as-state-change)))
18948 (org-call-with-arg 'org-todo 'left)))
18949 ((or (and org-support-shift-select
18950 (not (eq org-support-shift-select 'always))
18951 (org-at-item-bullet-p))
18952 (and (not org-support-shift-select) (org-at-item-p)))
18953 (org-call-with-arg 'org-cycle-list-bullet 'previous))
18954 ((and (not (eq org-support-shift-select 'always))
18955 (org-at-property-p))
18956 (call-interactively 'org-property-previous-allowed-value))
18957 ((org-clocktable-try-shift 'left arg))
18958 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
18959 (org-support-shift-select
18960 (org-call-for-shift-select 'backward-char))
18961 (t (org-shiftselect-error))))
18963 (defun org-shiftcontrolright ()
18964 "Switch to next TODO set."
18967 ((and org-support-shift-select (org-region-active-p))
18968 (org-call-for-shift-select 'forward-word))
18969 ((and (not (eq org-support-shift-select 'always))
18970 (org-at-heading-p))
18971 (org-call-with-arg 'org-todo 'nextset))
18972 (org-support-shift-select
18973 (org-call-for-shift-select 'forward-word))
18974 (t (org-shiftselect-error))))
18976 (defun org-shiftcontrolleft ()
18977 "Switch to previous TODO set."
18980 ((and org-support-shift-select (org-region-active-p))
18981 (org-call-for-shift-select 'backward-word))
18982 ((and (not (eq org-support-shift-select 'always))
18983 (org-at-heading-p))
18984 (org-call-with-arg 'org-todo 'previousset))
18985 (org-support-shift-select
18986 (org-call-for-shift-select 'backward-word))
18987 (t (org-shiftselect-error))))
18989 (defun org-shiftcontrolup ()
18990 "Change timestamps synchronously up in CLOCK log lines."
18992 (cond ((and (not org-support-shift-select)
18993 (org-at-clock-log-p)
18994 (org-at-timestamp-p t))
18995 (org-clock-timestamps-up))
18996 (t (org-shiftselect-error))))
18998 (defun org-shiftcontroldown ()
18999 "Change timestamps synchronously down in CLOCK log lines."
19001 (cond ((and (not org-support-shift-select)
19002 (org-at-clock-log-p)
19003 (org-at-timestamp-p t))
19004 (org-clock-timestamps-down))
19005 (t (org-shiftselect-error))))
19007 (defun org-ctrl-c-ret ()
19008 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
19011 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
19012 (t (call-interactively 'org-insert-heading))))
19014 (defun org-find-visible ()
19016 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
19017 (get-char-property s 'invisible)))
19019 (defun org-find-invisible ()
19021 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
19022 (not (get-char-property s 'invisible))))
19025 (defun org-copy-visible (beg end)
19026 "Copy the visible parts of the region."
19031 (narrow-to-region beg end)
19032 (setq s (goto-char (point-min)))
19033 (while (not (= (point) (point-max)))
19034 (goto-char (org-find-invisible))
19035 (push (buffer-substring s (point)) snippets)
19036 (setq s (goto-char (org-find-visible))))))
19037 (kill-new (apply 'concat (nreverse snippets)))))
19039 (defun org-copy-special ()
19040 "Copy region in table or copy current subtree.
19041 Calls `org-table-copy' or `org-copy-subtree', depending on context.
19042 See the individual commands for more information."
19044 (call-interactively
19045 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
19047 (defun org-cut-special ()
19048 "Cut region in table or cut current subtree.
19049 Calls `org-table-copy' or `org-cut-subtree', depending on context.
19050 See the individual commands for more information."
19052 (call-interactively
19053 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
19055 (defun org-paste-special (arg)
19056 "Paste rectangular region into table, or past subtree relative to level.
19057 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
19058 See the individual commands for more information."
19060 (if (org-at-table-p)
19061 (org-table-paste-rectangle)
19062 (org-paste-subtree arg)))
19064 (defun org-edit-special (&optional arg)
19065 "Call a special editor for the stuff at point.
19066 When at a table, call the formula editor with `org-table-edit-formulas'.
19067 When at the first line of an src example, call `org-edit-src-code'.
19068 When in an #+include line, visit the include file. Otherwise call
19069 `ffap' to visit the file at point."
19071 ;; possibly prep session before editing source
19073 (let* ((info (org-babel-get-src-block-info))
19074 (lang (nth 0 info))
19075 (params (nth 2 info))
19076 (session (cdr (assoc :session params))))
19077 (when (and info session) ;; we are in a source-code block with a session
19079 (intern (concat "org-babel-prep-session:" lang)) session params))))
19080 (cond ;; proceed with `org-edit-special'
19082 (beginning-of-line 1)
19083 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
19084 (find-file (org-trim (match-string 1))))
19085 ((org-edit-src-code))
19086 ((org-edit-fixed-width-region))
19087 ((org-at-table.el-p)
19088 (org-edit-src-code))
19089 ((or (org-at-table-p)
19091 (beginning-of-line 1)
19092 (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
19093 (call-interactively 'org-table-edit-formulas))
19094 (t (call-interactively 'ffap))))
19096 (defvar org-table-coordinate-overlays) ; defined in org-table.el
19097 (defun org-ctrl-c-ctrl-c (&optional arg)
19098 "Set tags in headline, or update according to changed information at point.
19100 This command does many different things, depending on context:
19102 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
19103 this is what we do.
19105 - If the cursor is on a statistics cookie, update it.
19107 - If the cursor is in a headline, prompt for tags and insert them
19108 into the current line, aligned to `org-tags-column'. When called
19109 with prefix arg, realign all tags in the current buffer.
19111 - If the cursor is in one of the special #+KEYWORD lines, this
19112 triggers scanning the buffer for these lines and updating the
19115 - If the cursor is inside a table, realign the table. This command
19116 works even if the automatic table editor has been turned off.
19118 - If the cursor is on a #+TBLFM line, re-apply the formulas to
19121 - If the cursor is at a footnote reference or definition, jump to
19122 the corresponding definition or references, respectively.
19124 - If the cursor is a the beginning of a dynamic block, update it.
19126 - If the current buffer is a capture buffer, close note and file it.
19128 - If the cursor is on a <<<target>>>, update radio targets and
19129 corresponding links in this buffer.
19131 - If the cursor is on a numbered item in a plain list, renumber the
19134 - If the cursor is on a checkbox, toggle it.
19136 - If the cursor is on a code block, evaluate it. The variable
19137 `org-confirm-babel-evaluate' can be used to control prompting
19138 before code block evaluation, by default every code block
19139 evaluation requires confirmation. Code block evaluation can be
19140 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
19142 (let ((org-enable-table-editor t))
19144 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
19145 org-occur-highlights
19146 org-latex-fragment-image-overlays)
19147 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
19148 (org-remove-occur-highlights)
19149 (org-remove-latex-fragment-image-overlays)
19150 (message "Temporary highlights/overlays removed from current buffer"))
19151 ((and (local-variable-p 'org-finish-function (current-buffer))
19152 (fboundp org-finish-function))
19153 (funcall org-finish-function))
19154 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
19155 ((org-in-regexp org-ts-regexp-both)
19156 (org-timestamp-change 0 'day))
19157 ((or (looking-at org-property-start-re)
19158 (org-at-property-p))
19159 (call-interactively 'org-property-action))
19160 ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
19161 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
19162 (or (org-at-heading-p) (org-at-item-p)))
19163 (call-interactively 'org-update-statistics-cookies))
19164 ((org-at-heading-p) (call-interactively 'org-set-tags))
19165 ((org-at-table.el-p)
19166 (message "Use C-c ' to edit table.el tables"))
19168 (org-table-maybe-eval-formula)
19170 (call-interactively 'org-table-recalculate)
19171 (org-table-maybe-recalculate-line))
19172 (call-interactively 'org-table-align)
19173 (orgtbl-send-table 'maybe))
19174 ((or (org-footnote-at-reference-p)
19175 (org-footnote-at-definition-p))
19176 (call-interactively 'org-footnote-action))
19177 ((org-at-item-checkbox-p)
19178 ;; Cursor at a checkbox: repair list and update checkboxes. Send
19179 ;; list only if at top item.
19180 (let* ((cbox (match-string 1))
19181 (struct (org-list-struct))
19182 (old-struct (copy-tree struct))
19183 (parents (org-list-parents-alist struct))
19184 (orderedp (org-entry-get nil "ORDERED"))
19185 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19187 ;; Use a light version of `org-toggle-checkbox' to avoid
19188 ;; computing list structure twice.
19189 (let ((new-box (cond
19190 ((equal arg '(16)) "[-]")
19191 ((equal arg '(4)) nil)
19192 ((equal "[X]" cbox) "[ ]")
19194 (if (and firstp arg)
19195 ;; If at first item of sub-list, remove check-box from
19196 ;; every item at the same level.
19198 (lambda (pos) (org-list-set-checkbox pos struct new-box))
19199 (org-list-get-all-items
19200 (point-at-bol) struct (org-list-prevs-alist struct)))
19201 (org-list-set-checkbox (point-at-bol) struct new-box)))
19202 ;; Replicate `org-list-write-struct', while grabbing a return
19203 ;; value from `org-list-struct-fix-box'.
19204 (org-list-struct-fix-ind struct parents 2)
19205 (org-list-struct-fix-item-end struct)
19206 (let ((prevs (org-list-prevs-alist struct)))
19207 (org-list-struct-fix-bul struct prevs)
19208 (org-list-struct-fix-ind struct parents)
19210 (org-list-struct-fix-box struct parents prevs orderedp)))
19211 (org-list-struct-apply-struct struct old-struct)
19212 (org-update-checkbox-count-maybe)
19215 "Checkboxes were removed due to unchecked box at line %d"
19216 (org-current-line block-item)))
19217 (when firstp (org-list-send-list 'maybe))))
19219 ;; Cursor at an item: repair list. Do checkbox related actions
19220 ;; only if function was called with an argument. Send list only
19222 (let* ((struct (org-list-struct))
19223 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19226 (setq old-struct (copy-tree struct))
19228 ;; If at first item of sub-list, add check-box to every
19229 ;; item at the same level.
19232 (unless (org-list-get-checkbox pos struct)
19233 (org-list-set-checkbox pos struct "[ ]")))
19234 (org-list-get-all-items
19235 (point-at-bol) struct (org-list-prevs-alist struct)))
19236 (org-list-set-checkbox (point-at-bol) struct "[ ]")))
19237 (org-list-write-struct
19238 struct (org-list-parents-alist struct) old-struct)
19239 (when arg (org-update-checkbox-count-maybe))
19240 (when firstp (org-list-send-list 'maybe))))
19241 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
19243 (beginning-of-line 1)
19244 (save-excursion (org-update-dblock)))
19246 (let ((case-fold-search t))
19247 (beginning-of-line 1)
19248 (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
19250 ((or (equal (match-string 1) "TBLFM")
19251 (equal (match-string 1) "tblfm"))
19252 ;; Recalculate the table before this line
19254 (beginning-of-line 1)
19255 (skip-chars-backward " \r\n\t")
19256 (if (org-at-table-p)
19257 (org-call-with-arg 'org-table-recalculate (or arg t)))))
19259 (let ((org-inhibit-startup-visibility-stuff t)
19260 (org-startup-align-all-tables nil))
19261 (when (boundp 'org-table-coordinate-overlays)
19262 (mapc 'delete-overlay org-table-coordinate-overlays)
19263 (setq org-table-coordinate-overlays nil))
19264 (org-save-outline-visibility 'use-markers (org-mode-restart)))
19265 (message "Local setup has been refreshed"))))
19266 ((org-clock-update-time-maybe))
19268 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
19269 (error "C-c C-c can do nothing useful at this location"))))))
19271 (defun org-mode-restart ()
19272 "Restart Org-mode, to scan again for special lines.
19273 Also updates the keyword regular expressions."
19276 (message "Org-mode restarted"))
19278 (defun org-kill-note-or-show-branches ()
19279 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
19281 (if (not org-finish-function)
19284 (call-interactively 'show-branches))
19285 (let ((org-note-abort t))
19286 (funcall org-finish-function))))
19288 (defun org-return (&optional indent)
19289 "Goto next table row or insert a newline.
19290 Calls `org-table-next-row' or `newline', depending on context.
19291 See the individual commands for more information."
19295 ((or (bobp) (org-in-src-block-p))
19296 (if indent (newline-and-indent) (newline)))
19298 (org-table-justify-field-maybe)
19299 (call-interactively 'org-table-next-row))
19300 ;; when `newline-and-indent' is called within a list, make sure
19301 ;; text moved stays inside the item.
19302 ((and (org-in-item-p) indent)
19303 (if (and (org-at-item-p) (>= (point) (match-end 0)))
19305 (save-match-data (newline))
19306 (org-indent-line-to (length (match-string 0))))
19307 (let ((ind (org-get-indentation)))
19309 (if (org-looking-back org-list-end-re)
19311 (org-indent-line-to ind)))))
19312 ((and org-return-follows-link
19313 (org-at-timestamp-p t)
19314 (not (eq org-ts-what 'after)))
19315 (org-follow-timestamp-link))
19316 ((and org-return-follows-link
19317 (let ((tprop (get-text-property (point) 'face)))
19318 (or (eq tprop 'org-link)
19319 (and (listp tprop) (memq 'org-link tprop)))))
19320 (call-interactively 'org-open-at-point))
19321 ((and (org-at-heading-p)
19323 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
19327 (t (if indent (newline-and-indent) (newline))))))
19329 (defun org-return-indent ()
19330 "Goto next table row or insert a newline and indent.
19331 Calls `org-table-next-row' or `newline-and-indent', depending on
19332 context. See the individual commands for more information."
19336 (defun org-ctrl-c-star ()
19337 "Compute table, or change heading status of lines.
19338 Calls `org-table-recalculate' or `org-toggle-heading',
19339 depending on context."
19343 (call-interactively 'org-table-recalculate))
19345 ;; Convert all lines in region to list items
19346 (call-interactively 'org-toggle-heading))))
19348 (defun org-ctrl-c-minus ()
19349 "Insert separator line in table or modify bullet status of line.
19350 Also turns a plain line or a region of lines into list items.
19351 Calls `org-table-insert-hline', `org-toggle-item', or
19352 `org-cycle-list-bullet', depending on context."
19356 (call-interactively 'org-table-insert-hline))
19357 ((org-region-active-p)
19358 (call-interactively 'org-toggle-item))
19360 (call-interactively 'org-cycle-list-bullet))
19362 (call-interactively 'org-toggle-item))))
19364 (defun org-toggle-item (arg)
19365 "Convert headings or normal lines to items, items to normal lines.
19366 If there is no active region, only the current line is considered.
19368 If the first non blank line in the region is an headline, convert
19369 all headlines to items, shifting text accordingly.
19371 If it is an item, convert all items to normal lines.
19373 If it is normal text, change region into an item. With a prefix
19374 argument ARG, change each line in region into an item."
19378 ;; Shift text in current section to IND, from point to END.
19379 ;; The function leaves point to END line.
19381 (let ((min-i 1000) (end (copy-marker end)))
19382 ;; First determine the minimum indentation (MIN-I) of
19386 (while (< (point) end)
19387 (let ((i (org-get-indentation)))
19389 ;; Skip blank lines and inline tasks.
19390 ((looking-at "^[ \t]*$"))
19391 ((looking-at org-outline-regexp-bol))
19392 ;; We can't find less than 0 indentation.
19393 ((zerop i) (throw 'exit (setq min-i 0)))
19394 ((< i min-i) (setq min-i i))))
19396 ;; Then indent each line so that a line indented to
19397 ;; MIN-I becomes indented to IND. Ignore blank lines
19398 ;; and inline tasks in the process.
19399 (let ((delta (- ind min-i)))
19400 (while (< (point) end)
19401 (unless (or (looking-at "^[ \t]*$")
19402 (looking-at org-outline-regexp-bol))
19403 (org-indent-line-to (+ (org-get-indentation) delta)))
19404 (forward-line)))))))
19407 ;; Return beginning of first non-blank line, starting from
19412 (skip-chars-forward " \r\t\n")
19415 ;; Determine boundaries of changes.
19416 (if (org-region-active-p)
19417 (setq beg (funcall skip-blanks (region-beginning))
19418 end (copy-marker (region-end)))
19419 (setq beg (funcall skip-blanks (point-at-bol))
19420 end (copy-marker (point-at-eol))))
19421 ;; Depending on the starting line, choose an action on the text
19422 ;; between BEG and END.
19423 (org-with-limited-levels
19427 ;; Case 1. Start at an item: de-itemize. Note that it only
19428 ;; happens when a region is active: `org-ctrl-c-minus'
19429 ;; would call `org-cycle-list-bullet' otherwise.
19431 (while (< (point) end)
19432 (when (org-at-item-p)
19433 (skip-chars-forward " \t")
19434 (delete-region (point) (match-end 0)))
19436 ;; Case 2. Start at an heading: convert to items.
19437 ((org-at-heading-p)
19438 (let* ((bul (org-list-bullet-string "-"))
19439 (bul-len (length bul))
19440 ;; Indentation of the first heading. It should be
19441 ;; relative to the indentation of its parent, if any.
19442 (start-ind (save-excursion
19444 ((not org-adapt-indentation) 0)
19445 ((not (outline-previous-heading)) 0)
19446 (t (length (match-string 0))))))
19447 ;; Level of first heading. Further headings will be
19448 ;; compared to it to determine hierarchy in the list.
19449 (ref-level (org-reduced-level (org-outline-level))))
19450 (while (< (point) end)
19451 (let* ((level (org-reduced-level (org-outline-level)))
19452 (delta (max 0 (- level ref-level))))
19453 ;; If current headline is less indented than the first
19454 ;; one, set it as reference, in order to preserve
19456 (when (< level ref-level) (setq ref-level level))
19457 (replace-match bul t t)
19458 (org-indent-line-to (+ start-ind (* delta bul-len)))
19459 ;; Ensure all text down to END (or SECTION-END) belongs
19460 ;; to the newly created item.
19461 (let ((section-end (save-excursion
19462 (or (outline-next-heading) (point)))))
19464 (funcall shift-text
19465 (+ start-ind (* (1+ delta) bul-len))
19466 (min end section-end)))))))
19467 ;; Case 3. Normal line with ARG: turn each non-item line into
19470 (while (< (point) end)
19471 (unless (or (org-at-heading-p) (org-at-item-p))
19472 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
19474 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
19476 ;; Case 4. Normal line without ARG: make the first line of
19477 ;; region an item, and shift indentation of others
19478 ;; lines to set them as item's body.
19479 (t (let* ((bul (org-list-bullet-string "-"))
19480 (bul-len (length bul))
19481 (ref-ind (org-get-indentation)))
19482 (skip-chars-forward " \t")
19485 (while (< (point) end)
19486 ;; Ensure that lines less indented than first one
19487 ;; still get included in item body.
19488 (funcall shift-text
19489 (+ ref-ind bul-len)
19490 (min end (save-excursion (or (outline-next-heading)
19492 (forward-line)))))))))
19494 (defun org-toggle-heading (&optional nstars)
19495 "Convert headings to normal text, or items or text to headings.
19496 If there is no active region, only the current line is considered.
19498 With a \\[universal-argument] prefix, convert the whole list at
19499 point into heading.
19503 - If the first non blank line is an headline, remove the stars
19504 from all headlines in the region.
19506 - If it is a normal line turn each and every normal line (i.e. not an
19507 heading or an item) in the region into a heading.
19509 - If it is a plain list item, turn all plain list items into headings.
19511 When converting a line into a heading, the number of stars is chosen
19512 such that the lines become children of the current entry. However,
19513 when a prefix argument is given, its value determines the number of
19518 ;; Return beginning of first non-blank line, starting from
19523 (while (org-at-comment-p) (forward-line))
19524 (skip-chars-forward " \r\t\n")
19527 ;; Determine boundaries of changes. If a universal prefix has
19528 ;; been given, put the list in a region. If region ends at a bol,
19529 ;; do not consider the last line to be in the region.
19531 (when (and current-prefix-arg (org-at-item-p))
19532 (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
19533 (org-mark-element))
19535 (if (org-region-active-p)
19536 (setq beg (funcall skip-blanks (region-beginning))
19537 end (copy-marker (save-excursion
19538 (goto-char (region-end))
19539 (if (bolp) (point) (point-at-eol)))))
19540 (setq beg (funcall skip-blanks (point-at-bol))
19541 end (copy-marker (point-at-eol))))
19542 ;; Ensure inline tasks don't count as headings.
19543 (org-with-limited-levels
19547 ;; Case 1. Started at an heading: de-star headings.
19548 ((org-at-heading-p)
19549 (while (< (point) end)
19550 (when (org-at-heading-p t)
19551 (looking-at org-outline-regexp) (replace-match "")
19554 ;; Case 2. Started at an item: change items into headlines.
19555 ;; One star will be added by `org-list-to-subtree'.
19557 (let* ((stars (make-string
19559 ;; subtract the star that will be added again by
19560 ;; `org-list-to-subtree'
19561 (1- (prefix-numeric-value current-prefix-arg))
19562 (or (org-current-level) 0))
19565 (cond (nstars "") ; stars from prefix only
19566 ((equal stars "") "") ; before first heading
19567 (org-odd-levels-only "*") ; inside heading, odd
19568 (t "")))) ; inside heading, oddeven
19569 (while (< (point) end)
19570 (when (org-at-item-p)
19571 ;; Pay attention to cases when region ends before list.
19572 (let* ((struct (org-list-struct))
19573 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
19575 (narrow-to-region (point) list-end)
19577 (org-list-to-subtree
19578 (org-list-parse-list t)
19579 '(:istart (concat stars add-stars (funcall get-stars depth))
19580 :icount (concat stars add-stars (funcall get-stars depth)))))))
19583 ;; Case 3. Started at normal text: make every line an heading,
19584 ;; skipping headlines and items.
19585 (t (let* ((stars (make-string
19587 (prefix-numeric-value current-prefix-arg)
19588 (or (org-current-level) 0))
19591 (cond (nstars "") ; stars from prefix only
19592 ((equal stars "") "*") ; before first heading
19593 (org-odd-levels-only "**") ; inside heading, odd
19594 (t "*"))) ; inside heading, oddeven
19595 (rpl (concat stars add-stars " ")))
19596 (while (< (point) end)
19597 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
19598 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
19599 (replace-match (concat rpl (match-string 2))) (setq toggled t))
19600 (forward-line)))))))
19601 (unless toggled (message "Cannot toggle heading from here"))))
19603 (defun org-meta-return (&optional arg)
19604 "Insert a new heading or wrap a region in a table.
19605 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
19606 See the individual commands for more information."
19609 ((run-hook-with-args-until-success 'org-metareturn-hook))
19610 ((or (org-at-drawer-p) (org-at-property-p))
19611 (newline-and-indent))
19613 (call-interactively 'org-table-wrap-region))
19614 (t (call-interactively 'org-insert-heading))))
19618 ;; Define the Org-mode menus
19619 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
19621 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
19622 ["Next Field" org-cycle (org-at-table-p)]
19623 ["Previous Field" org-shifttab (org-at-table-p)]
19624 ["Next Row" org-return (org-at-table-p)]
19626 ["Blank Field" org-table-blank-field (org-at-table-p)]
19627 ["Edit Field" org-table-edit-field (org-at-table-p)]
19628 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
19631 ["Move Column Left" org-metaleft (org-at-table-p)]
19632 ["Move Column Right" org-metaright (org-at-table-p)]
19633 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
19634 ["Insert Column" org-shiftmetaright (org-at-table-p)])
19636 ["Move Row Up" org-metaup (org-at-table-p)]
19637 ["Move Row Down" org-metadown (org-at-table-p)]
19638 ["Delete Row" org-shiftmetaup (org-at-table-p)]
19639 ["Insert Row" org-shiftmetadown (org-at-table-p)]
19640 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
19642 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
19644 ["Copy Rectangle" org-copy-special (org-at-table-p)]
19645 ["Cut Rectangle" org-cut-special (org-at-table-p)]
19646 ["Paste Rectangle" org-paste-special (org-at-table-p)]
19647 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
19650 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
19651 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
19652 ["Edit Formulas" org-edit-special (org-at-table-p)]
19654 ["Recalculate line" org-table-recalculate (org-at-table-p)]
19655 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
19656 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
19658 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
19660 ["Sum Column/Rectangle" org-table-sum
19661 (or (org-at-table-p) (org-region-active-p))]
19662 ["Which Column?" org-table-current-column (org-at-table-p)])
19664 org-table-toggle-formula-debugger
19665 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
19666 ["Show Col/Row Numbers"
19667 org-table-toggle-coordinate-overlays
19669 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
19671 ["Create" org-table-create (and (not (org-at-table-p))
19672 org-enable-table-editor)]
19673 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
19674 ["Import from File" org-table-import (not (org-at-table-p))]
19675 ["Export to File" org-table-export (org-at-table-p)]
19677 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
19679 (easy-menu-define org-org-menu org-mode-map "Org menu"
19682 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
19683 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
19684 ["Sparse Tree..." org-sparse-tree t]
19685 ["Reveal Context" org-reveal t]
19686 ["Show All" show-all t]
19688 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
19690 ["New Heading" org-insert-heading t]
19691 ("Navigate Headings"
19692 ["Up" outline-up-heading t]
19693 ["Next" outline-next-visible-heading t]
19694 ["Previous" outline-previous-visible-heading t]
19695 ["Next Same Level" outline-forward-same-level t]
19696 ["Previous Same Level" outline-backward-same-level t]
19698 ["Jump" org-goto t])
19700 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
19701 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
19703 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
19704 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
19705 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
19707 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
19709 ["Copy visible text" org-copy-visible t]
19711 ["Promote Heading" org-metaleft (not (org-at-table-p))]
19712 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
19713 ["Demote Heading" org-metaright (not (org-at-table-p))]
19714 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
19716 ["Sort Region/Children" org-sort (not (org-at-table-p))]
19718 ["Convert to odd levels" org-convert-to-odd-levels t]
19719 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
19721 ["Emphasis..." org-emphasize t]
19722 ["Edit Source Example" org-edit-special t]
19724 ["Footnote new/jump" org-footnote-action t]
19725 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
19727 ["Archive (default method)" org-archive-subtree-default t]
19729 ["Move Subtree to Archive file" org-advertized-archive-subtree t]
19730 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
19731 ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
19735 ["Store Link (Global)" org-store-link t]
19736 ["Find existing link to here" org-occur-link-in-agenda-files t]
19737 ["Insert Link" org-insert-link t]
19738 ["Follow Link" org-open-at-point t]
19740 ["Next link" org-next-link t]
19741 ["Previous link" org-previous-link t]
19743 ["Descriptive Links"
19744 org-toggle-link-display
19746 :selected org-descriptive-links
19749 org-toggle-link-display
19751 :selected (not org-descriptive-links)])
19754 ["TODO/DONE/-" org-todo t]
19756 ["Next keyword" org-shiftright (org-at-heading-p)]
19757 ["Previous keyword" org-shiftleft (org-at-heading-p)]
19758 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
19759 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
19760 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
19761 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
19762 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
19764 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
19765 :selected org-enforce-todo-dependencies :style toggle :active t]
19766 "Settings for tree at point"
19767 ["Do Children sequentially" org-toggle-ordered-property :style radio
19768 :selected (org-entry-get nil "ORDERED")
19769 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19770 ["Do Children parallel" org-toggle-ordered-property :style radio
19771 :selected (not (org-entry-get nil "ORDERED"))
19772 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19774 ["Set Priority" org-priority t]
19775 ["Priority Up" org-shiftup t]
19776 ["Priority Down" org-shiftdown t]
19778 ["Get news from all feeds" org-feed-update-all t]
19779 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
19780 ["Customize feeds" (customize-variable 'org-feed-alist) t])
19781 ("TAGS and Properties"
19782 ["Set Tags" org-set-tags-command t]
19783 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
19785 ["Set property" org-set-property t]
19786 ["Column view of properties" org-columns t]
19787 ["Insert Column View DBlock" org-insert-columns-dblock t])
19788 ("Dates and Scheduling"
19789 ["Timestamp" org-time-stamp t]
19790 ["Timestamp (inactive)" org-time-stamp-inactive t]
19792 ["1 Day Later" org-shiftright t]
19793 ["1 Day Earlier" org-shiftleft t]
19794 ["1 ... Later" org-shiftup t]
19795 ["1 ... Earlier" org-shiftdown t])
19796 ["Compute Time Range" org-evaluate-time-range t]
19797 ["Schedule Item" org-schedule t]
19798 ["Deadline" org-deadline t]
19800 ["Custom time format" org-toggle-time-stamp-overlays
19801 :style radio :selected org-display-custom-times]
19803 ["Goto Calendar" org-goto-calendar t]
19804 ["Date from Calendar" org-date-from-calendar t]
19806 ["Start/Restart Timer" org-timer-start t]
19807 ["Pause/Continue Timer" org-timer-pause-or-continue t]
19808 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
19809 ["Insert Timer String" org-timer t]
19810 ["Insert Timer Item" org-timer-item t])
19812 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
19813 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
19814 ["Clock out" org-clock-out t]
19815 ["Clock cancel" org-clock-cancel t]
19817 ["Mark as default task" org-clock-mark-default-task t]
19818 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
19819 ["Goto running clock" org-clock-goto t]
19821 ["Display times" org-clock-display t]
19822 ["Create clock table" org-clock-report t]
19824 ["Record DONE time"
19825 (progn (setq org-log-done (not org-log-done))
19826 (message "Switching to %s will %s record a timestamp"
19827 (car org-done-keywords)
19828 (if org-log-done "automatically" "not")))
19829 :style toggle :selected org-log-done])
19831 ["Agenda Command..." org-agenda t]
19832 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
19833 ("File List for Agenda")
19834 ("Special views current file"
19835 ["TODO Tree" org-show-todo-tree t]
19836 ["Check Deadlines" org-check-deadlines t]
19837 ["Timeline" org-timeline t]
19838 ["Tags/Property tree" org-match-sparse-tree t])
19840 ["Export/Publish..." org-export t]
19842 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
19843 :selected org-cdlatex-mode]
19844 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
19845 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
19846 ["Modify math symbol" org-cdlatex-math-modify
19847 (org-inside-LaTeX-fragment-p)]
19848 ["Insert citation" org-reftex-citation t]
19850 ["Template for BEAMER" (progn (require 'org-beamer)
19851 (org-insert-beamer-options-template)) t])
19854 ["Push Files and Views" org-mobile-push t]
19855 ["Get Captured and Flagged" org-mobile-pull t]
19856 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
19858 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
19861 ["Show Version" org-version t]
19862 ["Info Documentation" org-info t])
19864 ["Browse Org Group" org-customize t]
19866 ["Expand This Menu" org-create-customize-menu
19867 (fboundp 'customize-menu-create)])
19868 ["Send bug report" org-submit-bug-report t]
19871 ["Refresh setup current buffer" org-mode-restart t]
19872 ["Reload Org (after update)" org-reload t]
19873 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
19876 (defun org-info (&optional node)
19877 "Read documentation for Org-mode in the info system.
19878 With optional NODE, go directly to that node."
19880 (info (format "(org)%s" (or node ""))))
19883 (defun org-submit-bug-report ()
19884 "Submit a bug report on Org-mode via mail.
19886 Don't hesitate to report any problems or inaccurate documentation.
19888 If you don't have setup sending mail from (X)Emacs, please copy the
19889 output buffer into your mail program, as it gives us important
19890 information about your Org-mode version and configuration."
19892 (require 'reporter)
19893 (org-load-modules-maybe)
19894 (org-require-autoloaded-modules)
19895 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
19896 (reporter-submit-bug-report
19897 "emacs-orgmode@gnu.org"
19898 (org-version nil 'full)
19900 (save-window-excursion
19901 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
19902 (delete-other-windows)
19904 (insert "You are about to submit a bug report to the Org-mode mailing list.
19906 We would like to add your full Org-mode and Outline configuration to the
19907 bug report. This greatly simplifies the work of the maintainer and
19908 other experts on the mailing list.
19910 HOWEVER, some variables you have customized may contain private
19911 information. The names of customers, colleagues, or friends, might
19912 appear in the form of file names, tags, todo states, or search strings.
19913 If you answer yes to the prompt, you might want to check and remove
19914 such private information before sending the email.")
19915 (add-text-properties (point-min) (point-max) '(face org-warning))
19916 (when (yes-or-no-p "Include your Org-mode configuration ")
19920 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
19921 (or (and (symbol-value v)
19922 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
19924 (get v 'custom-type) (get v 'standard-value)
19925 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
19927 (kill-buffer (get-buffer "*Warn about privacy*"))
19930 "Remember to cover the basics, that is, what you expected to happen and
19931 what in fact did happen. You don't know how to make a good report? See
19933 http://orgmode.org/manual/Feedback.html#Feedback
19935 Your bug report will be posted to the Org-mode mailing list.
19936 ------------------------------------------------------------------------")
19938 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
19939 (replace-match "\\1Bug: \\3 [\\2]")))))
19942 (defun org-install-agenda-files-menu ()
19943 (let ((bl (buffer-list)))
19946 (set-buffer (pop bl))
19947 (if (derived-mode-p 'org-mode) (setq bl nil)))
19948 (when (derived-mode-p 'org-mode)
19950 '("Org") "File List for Agenda"
19953 ["Edit File List" (org-edit-agenda-file-list) t]
19954 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
19955 ["Remove Current File from List" org-remove-file t]
19956 ["Cycle through agenda files" org-cycle-agenda-files t]
19957 ["Occur in all agenda files" org-occur-in-agenda-files t]
19959 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
19964 (defun org-require-autoloaded-modules ()
19967 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
19968 org-docbook org-exp org-html org-icalendar
19970 org-publish org-remember org-table
19971 org-timer org-xoxo)))
19974 (defun org-reload (&optional uncompiled)
19975 "Reload all org lisp files.
19976 With prefix arg UNCOMPILED, load the uncompiled versions."
19978 (require 'find-func)
19979 (let* ((file-re "^org\\(-.*\\)?\\.el")
19980 (dir-org (file-name-directory (org-find-library-dir "org")))
19981 (dir-org-contrib (ignore-errors
19982 (file-name-directory
19983 (org-find-library-dir "org-contribdir"))))
19985 (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
19986 (append (list nil "comint" "eval" "exp" "keys"
19987 "lob" "ref" "table" "tangle")
19991 (when (cdr lang) (symbol-name (car lang))))
19992 org-babel-load-languages)))))
19994 (append babel-files
19995 (and dir-org-contrib
19996 (directory-files dir-org-contrib t file-re))
19997 (directory-files dir-org t file-re)))
19998 (remove-re (concat (if (featurep 'xemacs)
19999 "org-colview" "org-colview-xemacs")
20001 (setq files (mapcar 'file-name-sans-extension files))
20002 (setq files (mapcar
20003 (lambda (x) (if (string-match remove-re x) nil x))
20005 (setq files (delq nil files))
20008 (when (featurep (intern (file-name-nondirectory f)))
20009 (if (and (not uncompiled)
20010 (file-exists-p (concat f ".elc")))
20011 (load (concat f ".elc") nil nil 'nosuffix)
20012 (load (concat f ".el") nil nil 'nosuffix))))
20014 (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
20015 (org-version nil 'full 'message))
20018 (defun org-customize ()
20019 "Call the customize function with org as argument."
20021 (org-load-modules-maybe)
20022 (org-require-autoloaded-modules)
20023 (customize-browse 'org))
20025 (defun org-create-customize-menu ()
20026 "Create a full customization menu for Org-mode, insert it into the menu."
20028 (org-load-modules-maybe)
20029 (org-require-autoloaded-modules)
20030 (if (fboundp 'customize-menu-create)
20033 '("Org") "Customize"
20034 `(["Browse Org group" org-customize t]
20036 ,(customize-menu-create 'org)
20037 ["Set" Custom-set t]
20038 ["Save" Custom-save t]
20039 ["Reset to Current" Custom-reset-current t]
20040 ["Reset to Saved" Custom-reset-saved t]
20041 ["Reset to Standard Settings" Custom-reset-standard t]))
20042 (message "\"Org\"-menu now contains full customization menu"))
20043 (error "Cannot expand menu (outdated version of cus-edit.el)")))
20045 ;;;; Miscellaneous stuff
20047 ;;; Generally useful functions
20049 (defun org-get-at-bol (property)
20050 "Get text property PROPERTY at beginning of line."
20051 (get-text-property (point-at-bol) property))
20053 (defun org-find-text-property-in-string (prop s)
20054 "Return the first non-nil value of property PROP in string S."
20055 (or (get-text-property 0 prop s)
20056 (get-text-property (or (next-single-property-change 0 prop s) 0)
20059 (defun org-display-warning (message) ;; Copied from Emacs-Muse
20060 "Display the given MESSAGE as a warning."
20061 (if (fboundp 'display-warning)
20062 (display-warning 'org message
20063 (if (featurep 'xemacs) 'warning :warning))
20064 (let ((buf (get-buffer-create "*Org warnings*")))
20065 (with-current-buffer buf
20066 (goto-char (point-max))
20067 (insert "Warning (Org): " message)
20070 (display-buffer buf)
20073 (defun org-eval (form)
20074 "Eval FORM and return result."
20075 (condition-case error
20077 (error (format "%%![Error: %s]" error))))
20079 (defun org-in-clocktable-p ()
20080 "Check if the cursor is in a clocktable."
20081 (let ((pos (point)) start)
20084 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
20085 (setq start (match-beginning 0))
20086 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
20087 (>= (match-end 0) pos)
20090 (defun org-in-commented-line ()
20091 "Is point in a line starting with `#'?"
20092 (equal (char-after (point-at-bol)) ?#))
20094 (defun org-in-indented-comment-line ()
20095 "Is point in a line starting with `#' after some white space?"
20098 (goto-char (point-at-bol))
20099 (looking-at "[ \t]*#"))))
20101 (defun org-in-verbatim-emphasis ()
20103 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
20105 (defun org-goto-marker-or-bmk (marker &optional bookmark)
20106 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
20107 (if (and marker (marker-buffer marker)
20108 (buffer-live-p (marker-buffer marker)))
20110 (org-pop-to-buffer-same-window (marker-buffer marker))
20111 (if (or (> marker (point-max)) (< marker (point-min)))
20114 (org-show-context 'org-goto))
20116 (bookmark-jump bookmark)
20117 (error "Cannot find location"))))
20119 (defun org-quote-csv-field (s)
20120 "Quote field for inclusion in CSV material."
20121 (if (string-match "[\",]" s)
20122 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
20125 (defun org-force-self-insert (N)
20126 "Needed to enforce self-insert under remapping."
20128 (self-insert-command N))
20130 (defun org-string-width (s)
20131 "Compute width of string, ignoring invisible characters.
20132 This ignores character with invisibility property `org-link', and also
20133 characters with property `org-cwidth', because these will become invisible
20134 upon the next fontification round."
20136 (when (or (eq t buffer-invisibility-spec)
20137 (assq 'org-link buffer-invisibility-spec))
20138 (while (setq b (text-property-any 0 (length s)
20139 'invisible 'org-link s))
20140 (setq s (concat (substring s 0 b)
20141 (substring s (or (next-single-property-change
20142 b 'invisible s) (length s)))))))
20143 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
20144 (setq s (concat (substring s 0 b)
20145 (substring s (or (next-single-property-change
20146 b 'org-cwidth s) (length s))))))
20147 (setq l (string-width s) b -1)
20148 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
20149 (setq l (- l (get-text-property b 'org-dwidth-n s))))
20152 (defun org-shorten-string (s maxlength)
20153 "Shorten string S so tht it is no longer than MAXLENGTH characters.
20154 If the string is shorter or has length MAXLENGTH, just return the
20155 original string. If it is longer, the functions finds a space in the
20156 string, breaks this string off at that locations and adds three dots
20157 as ellipsis. Including the ellipsis, the string will not be longer
20158 than MAXLENGTH. If finding a good breaking point in the string does
20159 not work, the string is just chopped off in the middle of a word
20161 (if (<= (length s) maxlength)
20163 (let* ((n (max (- maxlength 4) 1))
20164 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
20165 (if (string-match re s)
20166 (concat (match-string 1 s) "...")
20167 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
20169 (defun org-get-indentation (&optional line)
20170 "Get the indentation of the current line, interpreting tabs.
20171 When LINE is given, assume it represents a line and compute its indentation."
20173 (if (string-match "^ *" (org-remove-tabs line))
20176 (beginning-of-line 1)
20177 (skip-chars-forward " \t")
20178 (current-column))))
20180 (defun org-get-string-indentation (s)
20181 "What indentation has S due to SPACE and TAB at the beginning of the string?"
20182 (let ((n -1) (i 0) (w tab-width) c)
20184 (while (< (setq n (1+ n)) (length s))
20185 (setq c (aref s n))
20186 (cond ((= c ?\ ) (setq i (1+ i)))
20187 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
20188 (t (throw 'exit t)))))
20191 (defun org-remove-tabs (s &optional width)
20192 "Replace tabulators in S with spaces.
20193 Assumes that s is a single line, starting in column 0."
20194 (setq width (or width tab-width))
20195 (while (string-match "\t" s)
20196 (setq s (replace-match
20198 (- (* width (/ (+ (match-beginning 0) width) width))
20199 (match-beginning 0)) ?\ )
20203 (defun org-fix-indentation (line ind)
20204 "Fix indentation in LINE.
20205 IND is a cons cell with target and minimum indentation.
20206 If the current indentation in LINE is smaller than the minimum,
20207 leave it alone. If it is larger than ind, set it to the target."
20208 (let* ((l (org-remove-tabs line))
20209 (i (org-get-indentation l))
20210 (i1 (car ind)) (i2 (cdr ind)))
20211 (if (>= i i2) (setq l (substring line i2)))
20213 (concat (make-string i1 ?\ ) l)
20216 (defun org-remove-indentation (code &optional n)
20217 "Remove the maximum common indentation from the lines in CODE.
20218 N may optionally be the number of spaces to remove."
20221 (org-do-remove-indentation n)
20224 (defun org-do-remove-indentation (&optional n)
20225 "Remove the maximum common indentation from the buffer."
20226 (untabify (point-min) (point-max))
20227 (let ((min 10000) re)
20230 (goto-char (point-min))
20231 (while (re-search-forward "^ *[^ \n]" nil t)
20232 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
20233 (unless (or (= min 0) (= min 10000))
20234 (setq re (format "^ \\{%d\\}" min))
20235 (goto-char (point-min))
20236 (while (re-search-forward re nil t)
20241 (defun org-fill-template (template alist)
20242 "Find each %key of ALIST in TEMPLATE and replace it."
20243 (let ((case-fold-search nil)
20245 (setq alist (sort (copy-sequence alist)
20246 (lambda (a b) (< (length (car a)) (length (car b))))))
20247 (while (setq entry (pop alist))
20249 (replace-regexp-in-string
20250 (concat "%" (regexp-quote (car entry)))
20251 (or (cdr entry) "") template t t)))
20254 (defun org-base-buffer (buffer)
20255 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
20258 (or (buffer-base-buffer buffer)
20261 (defun org-trim (s)
20262 "Remove whitespace at beginning and end of string."
20263 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
20264 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
20267 (defun org-wrap (string &optional width lines)
20268 "Wrap string to either a number of lines, or a width in characters.
20269 If WIDTH is non-nil, the string is wrapped to that width, however many lines
20270 that costs. If there is a word longer than WIDTH, the text is actually
20271 wrapped to the length of that word.
20272 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
20273 many lines, whatever width that takes.
20274 The return value is a list of lines, without newlines at the end."
20275 (let* ((words (org-split-string string "[ \t\n]+"))
20276 (maxword (apply 'max (mapcar 'org-string-width words)))
20279 (org-do-wrap words (max maxword width)))
20282 (setq ll (org-do-wrap words maxword))
20283 (if (<= (length ll) lines)
20286 (while (> (length ll) lines)
20288 (setq ll (org-do-wrap words w)))
20290 (t (error "Cannot wrap this")))))
20292 (defun org-do-wrap (words width)
20293 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
20296 (setq line (pop words))
20297 (while (and words (< (+ (length line) (length (car words))) width))
20298 (setq line (concat line " " (pop words))))
20299 (setq lines (push line lines)))
20302 (defun org-split-string (string &optional separators)
20303 "Splits STRING into substrings at SEPARATORS.
20304 No empty strings are returned if there are matches at the beginning
20305 and end of string."
20306 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
20310 (while (and (string-match rexp string
20312 (= start (match-beginning 0))
20313 (< start (length string)))
20315 (< (match-beginning 0) (length string)))
20317 (or (eq (match-beginning 0) 0)
20318 (and (eq (match-beginning 0) (match-end 0))
20319 (eq (match-beginning 0) start))
20321 (cons (substring string start (match-beginning 0))
20323 (setq start (match-end 0)))
20324 (or (eq start (length string))
20326 (cons (substring string start)
20330 (defun org-quote-vert (s)
20331 "Replace \"|\" with \"\\vert\"."
20332 (while (string-match "|" s)
20333 (setq s (replace-match "\\vert" t t s)))
20336 (defun org-uuidgen-p (s)
20337 "Is S an ID created by UUIDGEN?"
20338 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
20340 (defun org-in-src-block-p nil
20341 "Whether point is in a code source block."
20343 (when (setq ov (overlays-at (point)))
20344 (memq 'org-block-background
20345 (overlay-properties
20348 (defun org-context ()
20349 "Return a list of contexts of the current cursor position.
20350 If several contexts apply, all are returned.
20351 Each context entry is a list with a symbol naming the context, and
20352 two positions indicating start and end of the context. Possible
20355 :headline anywhere in a headline
20356 :headline-stars on the leading stars in a headline
20357 :todo-keyword on a TODO keyword (including DONE) in a headline
20358 :tags on the TAGS in a headline
20359 :priority on the priority cookie in a headline
20360 :item on the first line of a plain list item
20361 :item-bullet on the bullet/number of a plain list item
20362 :checkbox on the checkbox in a plain list item
20363 :table in an org-mode table
20364 :table-special on a special filed in a table
20365 :table-table in a table.el table
20366 :clocktable in a clocktable
20367 :src-block in a source block
20368 :link on a hyperlink
20369 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
20370 :target on a <<target>>
20371 :radio-target on a <<<radio-target>>>
20372 :latex-fragment on a LaTeX fragment
20373 :latex-preview on a LaTeX fragment with overlaid preview image
20375 This function expects the position to be visible because it uses font-lock
20376 faces as a help to recognize the following contexts: :table-special, :link,
20378 (let* ((f (get-text-property (point) 'face))
20379 (faces (if (listp f) f (list f)))
20380 (case-fold-search t)
20381 (p (point)) clist o)
20382 ;; First the large context
20384 ((org-at-heading-p t)
20385 (push (list :headline (point-at-bol) (point-at-eol)) clist)
20387 (beginning-of-line 1)
20388 (looking-at org-todo-line-tags-regexp))
20389 (push (org-point-in-group p 1 :headline-stars) clist)
20390 (push (org-point-in-group p 2 :todo-keyword) clist)
20391 (push (org-point-in-group p 4 :tags) clist))
20393 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
20394 (if (looking-at "\\[#[A-Z0-9]\\]")
20395 (push (org-point-in-group p 0 :priority) clist)))
20398 (push (org-point-in-group p 2 :item-bullet) clist)
20399 (push (list :item (point-at-bol)
20400 (save-excursion (org-end-of-item) (point)))
20402 (and (org-at-item-checkbox-p)
20403 (push (org-point-in-group p 0 :checkbox) clist)))
20406 (push (list :table (org-table-begin) (org-table-end)) clist)
20407 (if (memq 'org-formula faces)
20408 (push (list :table-special
20409 (previous-single-property-change p 'face)
20410 (next-single-property-change p 'face)) clist)))
20411 ((org-at-table-p 'any)
20412 (push (list :table-table) clist)))
20415 (let ((case-fold-search t))
20416 ;; New the "medium" contexts: clocktables, source blocks
20417 (cond ((org-in-clocktable-p)
20418 (push (list :clocktable
20419 (and (or (looking-at "#\\+BEGIN: clocktable")
20420 (search-backward "#+BEGIN: clocktable" nil t))
20421 (match-beginning 0))
20422 (and (re-search-forward "#\\+END:?" nil t)
20423 (match-end 0))) clist))
20424 ((org-in-src-block-p)
20425 (push (list :src-block
20426 (and (or (looking-at "#\\+BEGIN_SRC")
20427 (search-backward "#+BEGIN_SRC" nil t))
20428 (match-beginning 0))
20429 (and (search-forward "#+END_SRC" nil t)
20430 (match-beginning 0))) clist))))
20433 ;; Now the small context
20435 ((org-at-timestamp-p)
20436 (push (org-point-in-group p 0 :timestamp) clist))
20437 ((memq 'org-link faces)
20439 (previous-single-property-change p 'face)
20440 (next-single-property-change p 'face)) clist))
20441 ((memq 'org-special-keyword faces)
20442 (push (list :keyword
20443 (previous-single-property-change p 'face)
20444 (next-single-property-change p 'face)) clist))
20446 (push (org-point-in-group p 0 :target) clist)
20447 (goto-char (1- (match-beginning 0)))
20448 (if (looking-at org-radio-target-regexp)
20449 (push (org-point-in-group p 0 :radio-target) clist))
20451 ((setq o (car (delq nil
20454 (if (memq x org-latex-fragment-image-overlays) x))
20455 (overlays-at (point))))))
20456 (push (list :latex-fragment
20457 (overlay-start o) (overlay-end o)) clist)
20458 (push (list :latex-preview
20459 (overlay-start o) (overlay-end o)) clist))
20460 ((org-inside-LaTeX-fragment-p)
20461 ;; FIXME: positions wrong.
20462 (push (list :latex-fragment (point) (point)) clist)))
20464 (setq clist (nreverse (delq nil clist)))
20467 ;; FIXME: Compare with at-regexp-p Do we need both?
20468 (defun org-in-regexp (re &optional nlines visually)
20469 "Check if point is inside a match of regexp.
20470 Normally only the current line is checked, but you can include NLINES extra
20471 lines both before and after point into the search.
20472 If VISUALLY is set, require that the cursor is not after the match but
20473 really on, so that the block visually is on the match."
20475 (let ((pos (point))
20476 (eol (point-at-eol (+ 1 (or nlines 0))))
20477 (inc (if visually 1 0)))
20479 (beginning-of-line (- 1 (or nlines 0)))
20480 (while (re-search-forward re eol t)
20481 (if (and (<= (match-beginning 0) pos)
20482 (>= (+ inc (match-end 0)) pos))
20483 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
20485 (defun org-at-regexp-p (regexp)
20486 "Is point inside a match of REGEXP in the current line?"
20489 (let ((pos (point)) (end (point-at-eol)))
20490 (beginning-of-line 1)
20491 (while (re-search-forward regexp end t)
20492 (if (and (<= (match-beginning 0) pos)
20493 (>= (match-end 0) pos))
20497 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
20498 "Non-nil when point is between matches of START-RE and END-RE.
20500 Also return a non-nil value when point is on one of the matches.
20502 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
20503 buffer positions. Default values are the positions of headlines
20504 surrounding the point.
20506 The functions returns a cons cell whose car (resp. cdr) is the
20507 position before START-RE (resp. after END-RE)."
20509 (let ((pos (point))
20510 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
20511 (limit-down (or lim-down (save-excursion (outline-next-heading))))
20514 ;; Point is on a block when on START-RE or if START-RE can be
20515 ;; found before it...
20516 (and (or (org-at-regexp-p start-re)
20517 (re-search-backward start-re limit-up t))
20518 (setq beg (match-beginning 0))
20519 ;; ... and END-RE after it...
20520 (goto-char (match-end 0))
20521 (re-search-forward end-re limit-down t)
20522 (> (setq end (match-end 0)) pos)
20523 ;; ... without another START-RE in-between.
20524 (goto-char (match-beginning 0))
20525 (not (re-search-backward start-re (1+ beg) t))
20527 (cons beg end))))))
20529 (defun org-in-block-p (names)
20530 "Non-nil when point belongs to a block whose name belongs to NAMES.
20532 NAMES is a list of strings containing names of blocks.
20534 Return first block name matched, or nil. Beware that in case of
20535 nested blocks, the returned name may not belong to the closest
20539 (let ((case-fold-search t)
20540 (lim-up (save-excursion (outline-previous-heading)))
20541 (lim-down (save-excursion (outline-next-heading))))
20542 (mapc (lambda (name)
20543 (let ((n (regexp-quote name)))
20544 (when (org-between-regexps-p
20545 (concat "^[ \t]*#\\+begin_" n)
20546 (concat "^[ \t]*#\\+end_" n)
20552 (defun org-occur-in-agenda-files (regexp &optional nlines)
20553 "Call `multi-occur' with buffers for all agenda files."
20554 (interactive "sOrg-files matching: \np")
20555 (let* ((files (org-agenda-files))
20556 (tnames (mapcar 'file-truename files))
20557 (extra org-agenda-text-search-extra-files)
20559 (when (eq (car extra) 'agenda-archives)
20560 (setq extra (cdr extra))
20561 (setq files (org-add-archive-files files)))
20562 (while (setq f (pop extra))
20563 (unless (member (file-truename f) tnames)
20564 (add-to-list 'files f 'append)
20565 (add-to-list 'tnames (file-truename f) 'append)))
20567 (mapcar (lambda (x)
20568 (with-current-buffer
20569 (or (get-file-buffer x) (find-file-noselect x))
20575 (if (boundp 'occur-mode-find-occurrence-hook)
20577 (add-hook 'occur-mode-find-occurrence-hook
20579 (when (derived-mode-p 'org-mode)
20582 (defadvice occur-mode-goto-occurrence
20583 (after org-occur-reveal activate)
20584 (and (derived-mode-p 'org-mode) (org-reveal)))
20585 (defadvice occur-mode-goto-occurrence-other-window
20586 (after org-occur-reveal activate)
20587 (and (derived-mode-p 'org-mode) (org-reveal)))
20588 (defadvice occur-mode-display-occurrence
20589 (after org-occur-reveal activate)
20590 (when (derived-mode-p 'org-mode)
20591 (let ((pos (occur-mode-find-occurrence)))
20592 (with-current-buffer (marker-buffer pos)
20597 (defun org-occur-link-in-agenda-files ()
20598 "Create a link and search for it in the agendas.
20599 The link is not stored in `org-stored-links', it is just created
20600 for the search purpose."
20602 (let ((link (condition-case nil
20603 (org-store-link nil)
20604 (error "Unable to create a link to here"))))
20605 (org-occur-in-agenda-files (regexp-quote link))))
20607 (defun org-uniquify (list)
20608 "Remove duplicate elements from LIST."
20610 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
20613 (defun org-delete-all (elts list)
20614 "Remove all elements in ELTS from LIST."
20616 (setq list (delete (pop elts) list)))
20619 (defun org-count (cl-item cl-seq)
20620 "Count the number of occurrences of ITEM in SEQ.
20621 Taken from `count' in cl-seq.el with all keyword arguments removed."
20622 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
20623 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
20624 (while (< cl-start cl-end)
20625 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
20626 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
20627 (setq cl-start (1+ cl-start)))
20630 (defun org-remove-if (predicate seq)
20631 "Remove everything from SEQ that fulfills PREDICATE."
20635 (if (not (funcall predicate e)) (push e res)))
20638 (defun org-remove-if-not (predicate seq)
20639 "Remove everything from SEQ that does not fulfill PREDICATE."
20643 (if (funcall predicate e) (push e res)))
20646 (defun org-reduce (cl-func cl-seq &rest cl-keys)
20647 "Reduce two-argument FUNCTION across SEQ.
20648 Taken from `reduce' in cl-seq.el with all keyword arguments but
20649 \":initial-value\" removed."
20650 (let ((cl-accum (cond ((memq :initial-value cl-keys)
20651 (cadr (memq :initial-value cl-keys)))
20652 (cl-seq (pop cl-seq))
20653 (t (funcall cl-func)))))
20655 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
20658 (defun org-back-over-empty-lines ()
20659 "Move backwards over whitespace, to the beginning of the first empty line.
20660 Returns the number of empty lines passed."
20661 (let ((pos (point)))
20662 (if (cdr (assoc 'heading org-blank-before-new-entry))
20663 (skip-chars-backward " \t\n\r")
20665 (forward-line -1)))
20666 (beginning-of-line 2)
20667 (goto-char (min (point) pos))
20668 (count-lines (point) pos)))
20670 (defun org-skip-whitespace ()
20671 (skip-chars-forward " \t\n\r"))
20673 (defun org-point-in-group (point group &optional context)
20674 "Check if POINT is in match-group GROUP.
20675 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
20676 match. If the match group does not exist or point is not inside it,
20678 (and (match-beginning group)
20679 (>= point (match-beginning group))
20680 (<= point (match-end group))
20682 (list context (match-beginning group) (match-end group))
20685 (defun org-switch-to-buffer-other-window (&rest args)
20686 "Switch to buffer in a second window on the current frame.
20687 In particular, do not allow pop-up frames.
20688 Returns the newly created buffer."
20689 (let (pop-up-frames special-display-buffer-names special-display-regexps
20690 special-display-function)
20691 (apply 'switch-to-buffer-other-window args)))
20693 (defun org-combine-plists (&rest plists)
20694 "Create a single property list from all plists in PLISTS.
20695 The process starts by copying the first list, and then setting properties
20696 from the other lists. Settings in the last list are the most significant
20697 ones and overrule settings in the other lists."
20698 (let ((rtn (copy-sequence (pop plists)))
20701 (setq ls (pop plists))
20703 (setq p (pop ls) v (pop ls))
20704 (setq rtn (plist-put rtn p v))))
20707 (defun org-replace-escapes (string table)
20708 "Replace %-escapes in STRING with values in TABLE.
20709 TABLE is an association list with keys like \"%a\" and string values.
20710 The sequences in STRING may contain normal field width and padding information,
20711 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
20712 so values can contain further %-escapes if they are define later in TABLE."
20713 (let ((tbl (copy-alist table))
20714 (case-fold-search nil)
20717 (while (setq e (pop tbl))
20718 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
20719 (when (and (cdr e) (string-match re (cdr e)))
20720 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
20722 (add-text-properties 0 3 (list 'sref sref) safe)
20723 (setcdr e (replace-match safe t t (cdr e)))))
20724 (while (string-match re string)
20725 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
20727 (setq string (replace-match rpl t t string))))
20728 (while (setq pchg (next-property-change pchg string))
20729 (let ((sref (get-text-property pchg 'sref string)))
20730 (when (and sref (string-match "SREF" string pchg))
20731 (setq string (replace-match sref t t string)))))
20734 (defun org-sublist (list start end)
20735 "Return a section of LIST, from START to END.
20736 Counting starts at 1."
20737 (let (rtn (c start))
20738 (setq list (nthcdr (1- start) list))
20739 (while (and list (<= c end))
20740 (push (pop list) rtn)
20744 (defun org-find-base-buffer-visiting (file)
20745 "Like `find-buffer-visiting' but always return the base buffer and
20746 not an indirect buffer."
20747 (let ((buf (or (get-file-buffer file)
20748 (find-buffer-visiting file))))
20750 (or (buffer-base-buffer buf) buf)
20753 (defun org-image-file-name-regexp (&optional extensions)
20754 "Return regexp matching the file names of images.
20755 If EXTENSIONS is given, only match these."
20756 (if (and (not extensions) (fboundp 'image-file-name-regexp))
20757 (image-file-name-regexp)
20758 (let ((image-file-name-extensions
20760 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
20761 "xbm" "xpm" "pbm" "pgm" "ppm"))))
20763 (regexp-opt (nconc (mapcar 'upcase
20764 image-file-name-extensions)
20765 image-file-name-extensions)
20769 (defun org-file-image-p (file &optional extensions)
20770 "Return non-nil if FILE is an image."
20772 (string-match (org-image-file-name-regexp extensions) file)))
20774 (defun org-get-cursor-date ()
20775 "Return the date at cursor in as a time.
20776 This works in the calendar and in the agenda, anywhere else it just
20777 returns the current time."
20778 (let (date day defd)
20780 ((eq major-mode 'calendar-mode)
20781 (setq date (calendar-cursor-to-date)
20782 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
20783 ((eq major-mode 'org-agenda-mode)
20784 (setq day (get-text-property (point) 'day))
20786 (setq date (calendar-gregorian-from-absolute day)
20787 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
20789 (or defd (current-time))))
20791 (defvar org-agenda-action-marker (make-marker)
20792 "Marker pointing to the entry for the next agenda action.")
20794 (defun org-mark-entry-for-agenda-action ()
20795 "Mark the current entry as target of an agenda action.
20796 Agenda actions are actions executed from the agenda with the key `k',
20797 which make use of the date at the cursor."
20799 (move-marker org-agenda-action-marker
20800 (save-excursion (org-back-to-heading t) (point))
20803 "Entry marked for action; press `k' at desired date in agenda or calendar"))
20805 (defun org-mark-subtree (&optional up)
20806 "Mark the current subtree.
20807 This puts point at the start of the current subtree, and mark at
20808 the end. If a numeric prefix UP is given, move up into the
20809 hierarchy of headlines by UP levels before marking the subtree."
20811 (org-with-limited-levels
20812 (cond ((org-at-heading-p) (beginning-of-line))
20813 ((org-before-first-heading-p) (error "Not in a subtree"))
20814 (t (outline-previous-visible-heading 1))))
20815 (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
20816 (if (org-called-interactively-p 'any)
20817 (call-interactively 'org-mark-element)
20818 (org-mark-element)))
20822 (defun org-indent-line ()
20823 "Indent line depending on context."
20825 (let* ((pos (point))
20826 (itemp (org-at-item-p))
20827 (case-fold-search t)
20828 (org-drawer-regexp (or org-drawer-regexp "\000"))
20829 (inline-task-p (and (featurep 'org-inlinetask)
20830 (org-inlinetask-in-task-p)))
20831 (inline-re (and inline-task-p
20832 (org-inlinetask-outline-regexp)))
20834 (if (and orgstruct-is-++ (eq pos (point)))
20835 (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
20836 (indent-according-to-mode))
20837 (beginning-of-line 1)
20840 ((looking-at org-outline-regexp) (setq column 0))
20842 ((looking-at "#\\+include:") (setq column 0))
20843 ;; Footnote definition
20844 ((looking-at org-footnote-definition-re) (setq column 0))
20845 ;; Literal examples
20846 ((looking-at "[ \t]*:\\( \\|$\\)")
20847 (setq column (org-get-indentation))) ; do nothing
20849 ((ignore-errors (goto-char (org-in-item-p)))
20850 (setq column (if itemp
20851 (org-get-indentation)
20852 (org-list-item-body-column (point))))
20855 ((and (looking-at "[ \t]*:END:")
20856 (save-excursion (re-search-backward org-drawer-regexp nil t)))
20858 (goto-char (1- (match-beginning 1)))
20859 (setq column (current-column))))
20861 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
20863 (re-search-backward
20864 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
20865 (setq column (org-get-indentation (match-string 0))))
20866 ((and (not (looking-at "[ \t]*#\\+begin_"))
20867 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
20869 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
20871 (cond ((equal (downcase (match-string 1)) "src")
20872 ;; src blocks: let `org-edit-src-exit' handle them
20873 (org-get-indentation))
20874 ((equal (downcase (match-string 1)) "example")
20875 (max (org-get-indentation)
20876 (org-get-indentation (match-string 0))))
20878 (org-get-indentation (match-string 0))))))
20879 ;; This line has nothing special, look at the previous relevant
20880 ;; line to compute indentation
20882 (beginning-of-line 0)
20883 (while (and (not (bobp))
20884 (not (looking-at org-drawer-regexp))
20885 ;; When point started in an inline task, do not move
20886 ;; above task starting line.
20887 (not (and inline-task-p (looking-at inline-re)))
20888 ;; Skip drawers, blocks, empty lines, verbatim,
20889 ;; comments, tables, footnotes definitions, lists,
20891 (or (and (looking-at "[ \t]*:END:")
20892 (re-search-backward org-drawer-regexp nil t))
20893 (and (looking-at "[ \t]*#\\+end_")
20894 (re-search-backward "[ \t]*#\\+begin_"nil t))
20895 (looking-at "[ \t]*[\n:#|]")
20896 (looking-at org-footnote-definition-re)
20897 (and (ignore-errors (goto-char (org-in-item-p)))
20899 (org-list-get-top-point (org-list-struct))))
20900 (and (not inline-task-p)
20901 (featurep 'org-inlinetask)
20902 (org-inlinetask-in-task-p)
20903 (or (org-inlinetask-goto-beginning) t))))
20904 (beginning-of-line 0))
20906 ;; There was an heading above.
20907 ((looking-at "\\*+[ \t]+")
20908 (if (not org-adapt-indentation)
20910 (goto-char (match-end 0))
20911 (setq column (current-column))))
20912 ;; A drawer had started and is unfinished
20913 ((looking-at org-drawer-regexp)
20914 (goto-char (1- (match-beginning 1)))
20915 (setq column (current-column)))
20916 ;; Else, nothing noticeable found: get indentation and go on.
20917 (t (setq column (org-get-indentation))))))
20918 ;; Now apply indentation and move cursor accordingly
20920 (if (<= (current-column) (current-indentation))
20921 (org-indent-line-to column)
20922 (save-excursion (org-indent-line-to column)))
20923 ;; Special polishing for properties, see `org-property-format'
20924 (setq column (current-column))
20925 (beginning-of-line 1)
20927 "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
20928 (replace-match (concat (match-string 1)
20929 (format org-property-format
20930 (match-string 2) (match-string 3)))
20932 (org-move-to-column column))))
20934 (defun org-indent-drawer ()
20935 "Indent the drawer at point."
20938 (e (and (save-excursion (re-search-forward ":END:" nil t))
20943 (when (overlays-at (point))
20944 (member 'invisible (overlay-properties
20945 (car (overlays-at (point)))))))))
20946 (when folded (org-cycle))
20947 (indent-for-tab-command)
20948 (while (and (move-beginning-of-line 2) (< (point) e))
20949 (indent-for-tab-command))
20951 (when folded (org-cycle)))
20952 (message "Drawer at point indented"))
20954 (defun org-indent-block ()
20955 "Indent the block at point."
20958 (case-fold-search t)
20959 (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
20964 (when (overlays-at (point))
20965 (member 'invisible (overlay-properties
20966 (car (overlays-at (point)))))))))
20967 (when folded (org-cycle))
20968 (indent-for-tab-command)
20969 (while (and (move-beginning-of-line 2) (< (point) e))
20970 (indent-for-tab-command))
20972 (when folded (org-cycle)))
20973 (message "Block at point indented"))
20975 (defun org-indent-region (start end)
20979 (let ((line-end (org-current-line end)))
20981 (while (< (org-current-line) line-end)
20982 (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
20983 (t (call-interactively 'org-indent-line)))
20984 (move-beginning-of-line 2)))))
20989 ;; We use our own fill-paragraph and auto-fill functions. These
20990 ;; functions will shadow `fill-prefix' (computed internally with
20991 ;; `org-fill-context-prefix') and pass through to
20992 ;; `fill-region-as-paragraph' and `do-auto-fill' as appropriate.
20994 (defun org-set-autofill-regexps ()
20996 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
20997 ;; Prevent auto-fill from inserting unwanted new items.
20998 (when (boundp 'fill-nobreak-predicate)
21000 'fill-nobreak-predicate
21002 (append fill-nobreak-predicate
21003 '(org-fill-paragraph-separate-nobreak-p
21004 org-fill-line-break-nobreak-p)))))
21005 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
21006 (org-set-local 'comment-line-break-function 'org-comment-line-break-function)
21007 (org-set-local 'align-mode-rules-list
21008 '((org-in-buffer-settings
21009 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
21010 (modes . '(org-mode))))))
21012 (defun org-fill-paragraph-separate-nobreak-p ()
21013 "Non-nil when a line break at point would insert a new item."
21014 (looking-at (substring org-element-paragraph-separate 1)))
21016 (defun org-fill-line-break-nobreak-p ()
21017 "Non-nil when a line break at point would create an Org line break."
21019 (skip-chars-backward "[ \t]")
21020 (skip-chars-backward "\\\\")
21021 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
21023 (declare-function message-in-body-p "message" ())
21024 (defvar org-element--affiliated-re) ; From org-element.el
21025 (defun org-fill-context-prefix (p)
21026 "Compute a fill prefix for the line at point P.
21027 Return fill prefix, as a string, or nil if current line isn't
21028 meant to be filled."
21029 (org-with-wide-buffer
21030 (unless (and (derived-mode-p 'message-mode) (not (message-in-body-p)))
21031 ;; FIXME: This is really the job of orgstruct++-mode
21033 (beginning-of-line)
21034 (let* ((element (org-element-at-point))
21035 (type (org-element-type element))
21038 (goto-char (org-element-property :begin element))
21039 (while (looking-at org-element--affiliated-re) (forward-line))
21041 (unless (< p post-affiliated)
21043 (comment (looking-at "[ \t]*# ?") (match-string 0))
21044 (footnote-definition "")
21046 (make-string (org-list-item-body-column post-affiliated) ? ))
21048 ;; Fill prefix is usually the same as the current line,
21049 ;; except if the paragraph is at the beginning of an item.
21050 (let ((parent (org-element-property :parent element)))
21051 (cond ((eq (org-element-type parent) 'item)
21052 (make-string (org-list-item-body-column
21053 (org-element-property :begin parent))
21055 ((looking-at "\\s-+") (match-string 0))
21058 ;; Only fill contents if P is within block boundaries.
21059 (let* ((cbeg (save-excursion (goto-char post-affiliated)
21062 (cend (save-excursion
21063 (goto-char (org-element-property :end element))
21064 (skip-chars-backward " \r\t\n")
21065 (line-beginning-position))))
21066 (when (and (>= p cbeg) (< p cend))
21067 (if (looking-at "\\s-+") (match-string 0) ""))))))))))
21069 (declare-function message-goto-body "message" ())
21070 (defvar message-cite-prefix-regexp) ; From message.el
21071 (defvar org-element-all-objects) ; From org-element.el
21072 (defun org-fill-paragraph (&optional justify)
21073 "Fill element at point, when applicable.
21075 This function only applies to comment blocks, comments, example
21076 blocks and paragraphs. Also, as a special case, re-align table
21077 when point is at one.
21079 If JUSTIFY is non-nil (interactively, with prefix argument),
21080 justify as well. If `sentence-end-double-space' is non-nil, then
21081 period followed by one space does not end a sentence, so don't
21082 break a line there. The variable `fill-column' controls the
21085 For convenience, when point is at a plain list, an item or
21086 a footnote definition, try to fill the first paragraph within."
21087 ;; Falls back on message-fill-paragraph when necessary
21089 (if (and (derived-mode-p 'message-mode)
21090 (or (not (message-in-body-p))
21091 (save-excursion (move-beginning-of-line 1)
21092 (looking-at message-cite-prefix-regexp))))
21093 (let ((fill-paragraph-function
21094 (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
21095 (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
21096 (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
21097 (paragraph-separate
21098 (cadadr (assoc 'paragraph-separate org-fb-vars))))
21101 ;; Move to end of line in order to get the first paragraph within
21102 ;; a plain list or a footnote definition.
21104 (let ((element (org-element-at-point)))
21105 ;; First check if point is in a blank line at the beginning of the
21106 ;; buffer. In that case, ignore filling.
21107 (if (< (point) (org-element-property :begin element)) t
21108 (case (org-element-type element)
21109 ;; Align Org tables, leave table.el tables as-is.
21110 (table-row (org-table-align) t)
21112 (when (eq (org-element-property :type element) 'org)
21115 ;; Elements that may contain `line-break' type objects.
21117 (let ((beg (max (point-min)
21118 (org-element-property :contents-begin element)))
21119 (end (min (point-max)
21120 (org-element-property :contents-end element))))
21121 ;; Do nothing if point is at an affiliated keyword.
21122 (if (< (point) beg) t
21123 (when (derived-mode-p 'message-mode)
21124 ;; In `message-mode', do not fill following
21125 ;; citation in current paragraph nor text before
21127 (let ((body-start (save-excursion (message-goto-body))))
21128 (when body-start (setq beg (max body-start beg))))
21129 (when (save-excursion
21131 (concat "^" message-cite-prefix-regexp) end t))
21132 (setq end (match-beginning 0))))
21133 ;; Fill paragraph, taking line breaks into consideration.
21134 ;; For that, slice the paragraph using line breaks as
21135 ;; separators, and fill the parts in reverse order to
21136 ;; avoid messing with markers.
21141 (let ((fill-prefix (org-fill-context-prefix pos)))
21142 (fill-region-as-paragraph pos (point) justify))
21144 ;; Find the list of ending positions for line breaks
21145 ;; in the current paragraph. Add paragraph beginning
21146 ;; to include first slice.
21151 (org-element--parse-objects
21152 beg end nil org-element-all-objects)
21154 (lambda (lb) (org-element-property :end lb)))))))
21156 ;; Contents of `comment-block' type elements should be filled as
21159 (let ((fill-prefix (org-fill-context-prefix (point))))
21161 (fill-region-as-paragraph
21163 (goto-char (org-element-property :begin element))
21164 (while (looking-at org-element--affiliated-re)
21169 (goto-char (org-element-property :end element))
21170 (skip-chars-backward " \r\t\n")
21171 (line-beginning-position))
21173 ;; Fill comments, indented or not.
21175 (let ((fill-prefix (org-fill-context-prefix (point))))
21177 (fill-region-as-paragraph
21179 (goto-char (org-element-property :begin element))
21180 (while (looking-at org-element--affiliated-re)
21184 (goto-char (org-element-property :end element))
21185 (skip-chars-backward " \r\t\n")
21186 (line-end-position))))))
21187 ;; Ignore every other element.
21188 (otherwise t)))))))
21190 (defun org-auto-fill-function ()
21191 "Auto-fill function."
21192 ;; Check if auto-filling is meaningful before computing fill prefix.
21193 (let ((fc (current-fill-column)))
21194 (when (and fc (> (current-column) fc))
21195 (let ((fill-prefix (org-fill-context-prefix (point))))
21196 (when fill-prefix (do-auto-fill))))))
21201 ;; We control comments everywhere. `org-comment-or-uncomment-region'
21202 ;; and `org-insert-comment' takes care of `comment-dwim' behaviour
21203 ;; while `org-comment-line-break-function' handles auto-filling in
21206 (defun org-insert-comment ()
21207 "Insert an empty comment above current line.
21208 If the line is empty, insert comment at its beginning."
21209 (beginning-of-line)
21210 (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
21214 (defun org-comment-or-uncomment-region (beg end &rest ignore)
21215 "Comment or uncomment each non-blank line in the region.
21216 Uncomment each non-blank line between BEG and END if it only
21217 contains commented lines. Otherwise, comment them."
21220 (skip-chars-forward " \r\t\n" end)
21221 (beginning-of-line)
21223 ;; UNCOMMENTP is non-nil when every non blank line between
21224 ;; BEG and END is a comment.
21226 (while (progn (and (not (eobp))
21227 (let ((element (org-element-at-point)))
21228 (and (eq (org-element-type element) 'comment)
21229 (goto-char (org-element-property
21230 :end element)))))))
21232 ;; Remove or adding comment markers is going to change end
21233 ;; position so make it a marker.
21234 (end (copy-marker end)))
21235 (while (< (point) end)
21236 (unless (looking-at "\\s-*$")
21237 (if (not uncommentp) (progn (back-to-indentation) (insert "# "))
21238 ;; Only comments and blank lines in region: uncomment it.
21239 (looking-at "[ \t]*\\(# ?\\)")
21240 (replace-match "" nil nil nil 1)))
21242 (set-marker end nil))))
21244 (defun org-comment-line-break-function (&optional soft)
21245 "Break line at point and indent, continuing comment if within one.
21246 The inserted newline is marked hard if variable
21247 `use-hard-newlines' is true, unless optional argument SOFT is
21249 (if soft (insert-and-inherit ?\n) (newline 1))
21250 (save-excursion (forward-char -1) (delete-horizontal-space))
21251 (delete-horizontal-space)
21252 (indent-to-left-margin)
21253 (insert-before-markers-and-inherit fill-prefix))
21258 (defun org-toggle-fixed-width-section (arg)
21259 "Toggle the fixed-width export.
21260 If there is no active region, the QUOTE keyword at the current headline is
21261 inserted or removed. When present, it causes the text between this headline
21262 and the next to be exported as fixed-width text, and unmodified.
21263 If there is an active region, this command adds or removes a colon as the
21264 first character of this line. If the first character of a line is a colon,
21265 this line is also exported in fixed-width font."
21268 (regionp (org-region-active-p))
21269 (beg (if regionp (region-beginning) (point)))
21270 (end (if regionp (region-end)))
21271 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
21272 (case-fold-search nil)
21273 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
21278 (setq cc (current-column))
21279 (beginning-of-line 1)
21280 (setq off (looking-at re))
21281 (while (> nlines 0)
21282 (setq nlines (1- nlines))
21283 (beginning-of-line 1)
21286 (org-move-to-column cc t)
21289 ((and off (looking-at re))
21290 (replace-match "" t t nil 1))
21291 ((not off) (org-move-to-column cc t) (insert ": ")))
21294 (org-back-to-heading)
21296 ((looking-at (format org-heading-keyword-regexp-format
21298 (goto-char (match-end 1))
21299 (looking-at (concat " +" org-quote-string))
21300 (replace-match "" t t)
21301 (when (eolp) (insert " ")))
21302 ((looking-at org-outline-regexp)
21303 (goto-char (match-end 0))
21304 (insert org-quote-string " ")))))))
21306 (defun org-reftex-citation ()
21307 "Use reftex-citation to insert a citation into the buffer.
21308 This looks for a line like
21310 #+BIBLIOGRAPHY: foo plain option:-d
21312 and derives from it that foo.bib is the bibliography file relevant
21313 for this document. It then installs the necessary environment for RefTeX
21314 to work in this buffer and calls `reftex-citation' to insert a citation
21317 Export of such citations to both LaTeX and HTML is handled by the contributed
21318 package org-exp-bibtex by Taru Karttunen."
21320 (let ((reftex-docstruct-symbol 'rds)
21321 (reftex-cite-format "\\cite{%l}")
21326 (let ((case-fold-search t)
21327 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
21328 (if (not (save-excursion
21329 (or (re-search-forward re nil t)
21330 (re-search-backward re nil t))))
21331 (error "No bibliography defined in file")
21332 (setq bib (concat (match-string 1) ".bib")
21333 rds (list (list 'bib bib)))))))
21334 (call-interactively 'reftex-citation)))
21336 ;;;; Functions extending outline functionality
21338 (defun org-beginning-of-line (&optional arg)
21339 "Go to the beginning of the current line. If that is invisible, continue
21340 to a visible line beginning. This makes the function of C-a more intuitive.
21341 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
21342 first attempt, and only move to after the tags when the cursor is already
21343 beyond the end of the headline."
21345 (let ((pos (point))
21346 (special (if (consp org-special-ctrl-a/e)
21347 (car org-special-ctrl-a/e)
21348 org-special-ctrl-a/e))
21350 (if (org-bound-and-true-p line-move-visual)
21351 (beginning-of-visual-line 1)
21352 (beginning-of-line 1))
21353 (if (and arg (fboundp 'move-beginning-of-line))
21354 (call-interactively 'move-beginning-of-line)
21358 (if (org-truely-invisible-p)
21359 (while (and (not (bobp)) (org-truely-invisible-p))
21361 (beginning-of-line 1))
21362 (forward-char 1))))
21365 ((and (looking-at org-complex-heading-regexp)
21366 (= (char-after (match-end 1)) ?\ ))
21367 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
21371 (cond ((> pos refpos) refpos)
21372 ((= pos (point)) refpos)
21374 (cond ((> pos (point)) (point))
21375 ((not (eq last-command this-command)) (point))
21378 ;; Being at an item and not looking at an the item means point
21379 ;; was previously moved to beginning of a visual line, which
21380 ;; doesn't contain the item. Therefore, do nothing special,
21382 (when (looking-at org-list-full-item-re)
21383 ;; Set special position at first white space character after
21384 ;; bullet, and check-box, if any.
21385 (let ((after-bullet
21386 (let ((box (match-end 3)))
21387 (if (not box) (match-end 1)
21388 (let ((after (char-after box)))
21389 (if (and after (= after ? )) (1+ box) box))))))
21390 ;; Special case: Move point to special position when
21391 ;; currently after it or at beginning of line.
21393 (when (or (> pos after-bullet) (= (point) pos))
21394 (goto-char after-bullet))
21395 ;; Reversed case: Move point to special position when
21396 ;; point was already at beginning of line and command is
21398 (when (and (= (point) pos) (eq last-command this-command))
21399 (goto-char after-bullet))))))))
21401 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21403 (defun org-end-of-line (&optional arg)
21404 "Go to the end of the line.
21405 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
21406 first attempt, and only move to after the tags when the cursor is already
21407 beyond the end of the headline."
21409 (let ((special (if (consp org-special-ctrl-a/e)
21410 (cdr org-special-ctrl-a/e)
21411 org-special-ctrl-a/e)))
21413 ((or (not special) arg
21414 (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
21415 (call-interactively
21416 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
21417 ((fboundp 'move-end-of-line) 'move-end-of-line)
21418 (t 'end-of-line))))
21419 ((org-at-heading-p)
21420 (let ((pos (point)))
21421 (beginning-of-line 1)
21422 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
21424 (if (or (< pos (match-beginning 1))
21425 (= pos (match-end 0)))
21426 (goto-char (match-beginning 1))
21427 (goto-char (match-end 0)))
21428 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
21429 (goto-char (match-end 0))
21430 (goto-char (match-beginning 1))))
21431 (call-interactively (if (fboundp 'move-end-of-line)
21435 (move-end-of-line 1)
21436 (when (overlays-at (1- (point))) (backward-char 1)))
21437 ;; At an item: Move before any hidden text.
21438 (t (call-interactively
21439 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
21440 ((fboundp 'move-end-of-line) 'move-end-of-line)
21441 (t 'end-of-line)))))
21443 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21445 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
21446 (define-key org-mode-map "\C-e" 'org-end-of-line)
21448 (defun org-backward-sentence (&optional arg)
21449 "Go to beginning of sentence, or beginning of table field.
21450 This will call `backward-sentence' or `org-table-beginning-of-field',
21451 depending on context."
21454 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
21455 (t (call-interactively 'backward-sentence))))
21457 (defun org-forward-sentence (&optional arg)
21458 "Go to end of sentence, or end of table field.
21459 This will call `forward-sentence' or `org-table-end-of-field',
21460 depending on context."
21463 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
21464 (t (call-interactively 'forward-sentence))))
21466 (define-key org-mode-map "\M-a" 'org-backward-sentence)
21467 (define-key org-mode-map "\M-e" 'org-forward-sentence)
21469 (defun org-kill-line (&optional arg)
21470 "Kill line, to tags or end of line."
21473 ((or (not org-special-ctrl-k)
21475 (not (org-at-heading-p)))
21476 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
21477 org-ctrl-k-protect-subtree)
21478 (if (or (eq org-ctrl-k-protect-subtree 'error)
21479 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
21480 (error "C-k aborted - would kill hidden subtree")))
21481 (call-interactively
21482 (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
21483 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
21484 (kill-region (point) (match-beginning 1))
21485 (org-set-tags nil t))
21486 (t (kill-region (point) (point-at-eol)))))
21488 (define-key org-mode-map "\C-k" 'org-kill-line)
21490 (defun org-yank (&optional arg)
21491 "Yank. If the kill is a subtree, treat it specially.
21492 This command will look at the current kill and check if is a single
21493 subtree, or a series of subtrees[1]. If it passes the test, and if the
21494 cursor is at the beginning of a line or after the stars of a currently
21495 empty headline, then the yank is handled specially. How exactly depends
21496 on the value of the following variables, both set by default.
21498 org-yank-folded-subtrees
21499 When set, the subtree(s) will be folded after insertion, but only
21500 if doing so would now swallow text after the yanked text.
21502 org-yank-adjusted-subtrees
21503 When set, the subtree will be promoted or demoted in order to
21504 fit into the local outline tree structure, which means that the level
21505 will be adjusted so that it becomes the smaller one of the two
21506 *visible* surrounding headings.
21508 Any prefix to this command will cause `yank' to be called directly with
21509 no special treatment. In particular, a simple \\[universal-argument] prefix \
21511 plainly yank the text as it is.
21513 \[1] The test checks if the first non-white line is a heading
21514 and if there are no other headings with fewer stars."
21516 (org-yank-generic 'yank arg))
21518 (defun org-yank-generic (command arg)
21519 "Perform some yank-like command.
21521 This function implements the behavior described in the `org-yank'
21522 documentation. However, it has been generalized to work for any
21523 interactive command with similar behavior."
21525 ;; pretend to be command COMMAND
21526 (setq this-command command)
21529 (call-interactively command)
21531 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
21532 (and (org-kill-is-subtree-p)
21534 (and (looking-at "[ \t]*$")
21537 (buffer-substring (point-at-bol) (point)))))))
21540 ((and subtreep org-yank-folded-subtrees)
21541 (let ((beg (point))
21543 (if (and subtreep org-yank-adjusted-subtrees)
21544 (org-paste-subtree nil nil 'for-yank)
21545 (call-interactively command))
21549 (when (and (bolp) subtreep
21550 (not (setq swallowp
21551 (org-yank-folding-would-swallow-text beg end))))
21552 (org-with-limited-levels
21553 (or (looking-at org-outline-regexp)
21554 (re-search-forward org-outline-regexp-bol end t))
21555 (while (and (< (point) end) (looking-at org-outline-regexp))
21557 (org-cycle-show-empty-lines 'folded)
21558 (condition-case nil
21559 (outline-forward-same-level 1)
21560 (error (goto-char end))))))
21563 "Inserted text not folded because that would swallow text"))
21566 (skip-chars-forward " \t\n\r")
21567 (beginning-of-line 1)
21568 (push-mark beg 'nomsg)))
21569 ((and subtreep org-yank-adjusted-subtrees)
21570 (let ((beg (point-at-bol)))
21571 (org-paste-subtree nil nil 'for-yank)
21572 (push-mark beg 'nomsg)))
21574 (call-interactively command))))))
21576 (defun org-yank-folding-would-swallow-text (beg end)
21577 "Would hide-subtree at BEG swallow any text after END?"
21579 (org-with-limited-levels
21582 (when (or (looking-at org-outline-regexp)
21583 (re-search-forward org-outline-regexp-bol end t))
21584 (setq level (org-outline-level)))
21586 (skip-chars-forward " \t\r\n\v\f")
21588 (and (bolp) (looking-at org-outline-regexp)
21589 (<= (org-outline-level) level)))
21590 nil ; Nothing would be swallowed
21591 t))))) ; something would swallow
21593 (define-key org-mode-map "\C-y" 'org-yank)
21595 (defun org-truely-invisible-p ()
21596 "Check if point is at a character currently not visible.
21597 This version does not only check the character property, but also
21599 ;; Early versions of noutline don't have `outline-invisible-p'.
21600 (if (org-bound-and-true-p visible-mode)
21602 (outline-invisible-p)))
21604 (defun org-invisible-p2 ()
21605 "Check if point is at a character currently not visible."
21607 (if (and (eolp) (not (bobp))) (backward-char 1))
21608 ;; Early versions of noutline don't have `outline-invisible-p'.
21609 (outline-invisible-p)))
21611 (defun org-back-to-heading (&optional invisible-ok)
21612 "Call `outline-back-to-heading', but provide a better error message."
21613 (condition-case nil
21614 (outline-back-to-heading invisible-ok)
21615 (error (error "Before first headline at position %d in buffer %s"
21616 (point) (current-buffer)))))
21618 (defun org-before-first-heading-p ()
21619 "Before first heading?"
21622 (null (re-search-backward org-outline-regexp-bol nil t))))
21624 (defun org-at-heading-p (&optional ignored)
21625 (outline-on-heading-p t))
21626 ;; Compatibility alias with Org versions < 7.8.03
21627 (defalias 'org-on-heading-p 'org-at-heading-p)
21629 (defun org-at-comment-p nil
21630 "Is cursor in a line starting with a # character?"
21632 (beginning-of-line)
21633 (looking-at "^#")))
21635 (defun org-at-drawer-p nil
21636 "Is cursor at a drawer keyword?"
21638 (move-beginning-of-line 1)
21639 (looking-at org-drawer-regexp)))
21641 (defun org-at-block-p nil
21642 "Is cursor at a block keyword?"
21644 (move-beginning-of-line 1)
21645 (looking-at org-block-regexp)))
21647 (defun org-point-at-end-of-empty-headline ()
21648 "If point is at the end of an empty headline, return t, else nil.
21649 If the heading only contains a TODO keyword, it is still still considered
21651 (and (looking-at "[ \t]*$")
21652 (when org-todo-line-regexp
21654 (beginning-of-line 1)
21655 (let ((case-fold-search nil))
21656 (looking-at org-todo-line-regexp)
21657 (string= (match-string 3) ""))))))
21659 (defun org-at-heading-or-item-p ()
21660 (or (org-at-heading-p) (org-at-item-p)))
21662 (defun org-at-target-p ()
21663 (or (org-in-regexp org-radio-target-regexp)
21664 (org-in-regexp org-target-regexp)))
21665 ;; Compatibility alias with Org versions < 7.8.03
21666 (defalias 'org-on-target-p 'org-at-target-p)
21668 (defun org-up-heading-all (arg)
21669 "Move to the heading line of which the present line is a subheading.
21670 This function considers both visible and invisible heading lines.
21671 With argument, move up ARG levels."
21672 (if (fboundp 'outline-up-heading-all)
21673 (outline-up-heading-all arg) ; emacs 21 version of outline.el
21674 (outline-up-heading arg t))) ; emacs 22 version of outline.el
21676 (defun org-up-heading-safe ()
21677 "Move to the heading line of which the present line is a subheading.
21678 This version will not throw an error. It will return the level of the
21679 headline found, or nil if no higher level is found.
21681 Also, this function will be a lot faster than `outline-up-heading',
21682 because it relies on stars being the outline starters. This can really
21683 make a significant difference in outlines with very many siblings."
21684 (let (start-level re)
21685 (org-back-to-heading t)
21686 (setq start-level (funcall outline-level))
21687 (if (equal start-level 1)
21689 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
21690 (if (re-search-backward re nil t)
21691 (funcall outline-level)))))
21693 (defun org-first-sibling-p ()
21694 "Is this heading the first child of its parents?"
21696 (let ((re org-outline-regexp-bol)
21698 (unless (org-at-heading-p t)
21699 (error "Not at a heading"))
21700 (setq level (funcall outline-level))
21702 (if (not (re-search-backward re nil t))
21704 (setq l (funcall outline-level))
21707 (defun org-goto-sibling (&optional previous)
21708 "Goto the next sibling, even if it is invisible.
21709 When PREVIOUS is set, go to the previous sibling instead. Returns t
21710 when a sibling was found. When none is found, return nil and don't
21712 (let ((fun (if previous 're-search-backward 're-search-forward))
21714 (re org-outline-regexp-bol)
21716 (when (condition-case nil (org-back-to-heading t) (error nil))
21717 (setq level (funcall outline-level))
21719 (or previous (forward-char 1))
21720 (while (funcall fun re nil t)
21721 (setq l (funcall outline-level))
21722 (when (< l level) (goto-char pos) (throw 'exit nil))
21723 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
21727 (defun org-show-siblings ()
21728 "Show all siblings of the current headline."
21730 (while (org-goto-sibling) (org-flag-heading nil)))
21732 (while (org-goto-sibling 'previous)
21733 (org-flag-heading nil))))
21735 (defun org-goto-first-child ()
21736 "Goto the first child, even if it is invisible.
21737 Return t when a child was found. Otherwise don't move point and
21739 (let (level (pos (point)) (re org-outline-regexp-bol))
21740 (when (condition-case nil (org-back-to-heading t) (error nil))
21741 (setq level (outline-level))
21743 (if (and (re-search-forward re nil t) (> (outline-level) level))
21744 (progn (goto-char (match-beginning 0)) t)
21745 (goto-char pos) nil))))
21747 (defun org-show-hidden-entry ()
21748 "Show an entry where even the heading is hidden."
21752 (defun org-flag-heading (flag &optional entry)
21753 "Flag the current heading. FLAG non-nil means make invisible.
21754 When ENTRY is non-nil, show the entire entry."
21756 (org-back-to-heading t)
21757 ;; Check if we should show the entire entry
21762 (and (outline-next-heading)
21763 (org-flag-heading nil))))
21764 (outline-flag-region (max (point-min) (1- (point)))
21765 (save-excursion (outline-end-of-heading) (point))
21768 (defun org-get-next-sibling ()
21769 "Move to next heading of the same level, and return point.
21770 If there is no such heading, return nil.
21771 This is like outline-next-sibling, but invisible headings are ok."
21772 (let ((level (funcall outline-level)))
21773 (outline-next-heading)
21774 (while (and (not (eobp)) (> (funcall outline-level) level))
21775 (outline-next-heading))
21776 (if (or (eobp) (< (funcall outline-level) level))
21780 (defun org-get-last-sibling ()
21781 "Move to previous heading of the same level, and return point.
21782 If there is no such heading, return nil."
21783 (let ((opoint (point))
21784 (level (funcall outline-level)))
21785 (outline-previous-heading)
21786 (when (and (/= (point) opoint) (outline-on-heading-p t))
21787 (while (and (> (funcall outline-level) level)
21789 (outline-previous-heading))
21790 (if (< (funcall outline-level) level)
21794 (defun org-end-of-subtree (&optional invisible-OK to-heading)
21795 ;; This contains an exact copy of the original function, but it uses
21796 ;; `org-back-to-heading', to make it work also in invisible
21797 ;; trees. And is uses an invisible-OK argument.
21798 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
21799 ;; Furthermore, when used inside Org, finding the end of a large subtree
21800 ;; with many children and grandchildren etc, this can be much faster
21801 ;; than the outline version.
21802 (org-back-to-heading invisible-OK)
21804 (level (funcall outline-level)))
21805 (if (and (derived-mode-p 'org-mode) (< level 1000))
21806 ;; A true heading (not a plain list item), in Org-mode
21807 ;; This means we can easily find the end by looking
21808 ;; only for the right number of stars. Using a regexp to do
21809 ;; this is so much faster than using a Lisp loop.
21810 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
21812 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
21813 ;; something else, do it the slow way
21814 (while (and (not (eobp))
21815 (or first (> (funcall outline-level) level)))
21817 (outline-next-heading)))
21819 (if (memq (preceding-char) '(?\n ?\^M))
21821 ;; Go to end of line before heading
21823 (if (memq (preceding-char) '(?\n ?\^M))
21824 ;; leave blank line before heading
21825 (forward-char -1))))))
21828 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
21829 "Use Org version in org-mode, for dramatic speed-up."
21830 (if (derived-mode-p 'org-mode)
21832 (org-end-of-subtree nil t)
21833 (unless (eobp) (backward-char 1)))
21836 (defun org-end-of-meta-data-and-drawers ()
21837 "Jump to the first text after meta data and drawers in the current entry.
21838 This will move over empty lines, lines with planning time stamps,
21839 clocking lines, and drawers."
21840 (org-back-to-heading t)
21841 (let ((end (save-excursion (outline-next-heading) (point)))
21842 (re (concat "\\(" org-drawer-regexp "\\)"
21843 "\\|" "[ \t]*" org-keyword-time-regexp)))
21845 (while (re-search-forward re end t)
21846 (if (not (match-end 1))
21847 ;; empty or planning line
21849 ;; a drawer, find the end
21850 (re-search-forward "^[ \t]*:END:" end 'move)
21852 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
21855 (defun org-forward-heading-same-level (arg &optional invisible-ok)
21856 "Move forward to the arg'th subheading at same level as this one.
21857 Stop at the first and last subheadings of a superior heading.
21858 Normally this only looks at visible headings, but when INVISIBLE-OK is
21859 non-nil it will also look at invisible ones."
21861 (org-back-to-heading invisible-ok)
21863 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21864 (re (format "^\\*\\{1,%d\\} " level))
21868 (while (and (re-search-forward re nil 'move)
21869 (setq l (- (match-end 0) (match-beginning 0) 1))
21872 (progn (backward-char 1) (outline-invisible-p)))
21873 (if (< l level) (setq arg 1)))
21874 (setq arg (1- arg)))
21875 (beginning-of-line 1)))
21877 (defun org-backward-heading-same-level (arg &optional invisible-ok)
21878 "Move backward to the arg'th subheading at same level as this one.
21879 Stop at the first and last subheadings of a superior heading."
21881 (org-back-to-heading)
21883 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21884 (re (format "^\\*\\{1,%d\\} " level))
21887 (while (and (re-search-backward re nil 'move)
21888 (setq l (- (match-end 0) (match-beginning 0) 1))
21891 (outline-invisible-p))
21892 (if (< l level) (setq arg 1)))
21893 (setq arg (1- arg)))))
21896 (defun org-forward-element ()
21897 "Move forward by one element.
21898 Move to the next element at the same level, when possible."
21900 (cond ((eobp) (error "Cannot move further down"))
21901 ((org-with-limited-levels (org-at-heading-p))
21902 (let ((origin (point)))
21903 (org-forward-heading-same-level 1)
21904 (unless (org-with-limited-levels (org-at-heading-p))
21906 (error "Cannot move further down"))))
21908 (let* ((elem (org-element-at-point))
21909 (end (org-element-property :end elem))
21910 (parent (org-element-property :parent elem)))
21911 (if (and parent (= (org-element-property :contents-end parent) end))
21912 (goto-char (org-element-property :end parent))
21913 (goto-char end))))))
21916 (defun org-backward-element ()
21917 "Move backward by one element.
21918 Move to the previous element at the same level, when possible."
21920 (if (org-with-limited-levels (org-at-heading-p))
21921 ;; At an headline, move to the previous one, if any, or stay
21923 (let ((origin (point)))
21924 (org-backward-heading-same-level 1)
21925 (unless (org-with-limited-levels (org-at-heading-p))
21927 (error "Cannot move further up")))
21928 (let* ((trail (org-element-at-point 'keep-trail))
21930 (prev-elem (nth 1 trail))
21931 (beg (org-element-property :begin elem)))
21933 ;; Move to beginning of current element if point isn't there
21935 ((/= (point) beg) (goto-char beg))
21936 ((not prev-elem) (error "Cannot move further up"))
21937 (t (goto-char (org-element-property :begin prev-elem)))))))
21940 (defun org-up-element ()
21941 "Move to upper element."
21943 (if (org-with-limited-levels (org-at-heading-p))
21944 (unless (org-up-heading-safe) (error "No surrounding element"))
21945 (let* ((elem (org-element-at-point))
21946 (parent (org-element-property :parent elem)))
21947 (if parent (goto-char (org-element-property :begin parent))
21948 (if (org-with-limited-levels (org-before-first-heading-p))
21949 (error "No surrounding element")
21950 (org-with-limited-levels (org-back-to-heading)))))))
21953 (defvar org-element-greater-elements)
21954 (defun org-down-element ()
21955 "Move to inner element."
21957 (let ((element (org-element-at-point)))
21959 ((memq (org-element-type element) '(plain-list table))
21960 (goto-char (org-element-property :contents-begin element))
21962 ((memq (org-element-type element) org-element-greater-elements)
21963 ;; If contents are hidden, first disclose them.
21964 (when (org-element-property :hiddenp element) (org-cycle))
21965 (goto-char (or (org-element-property :contents-begin element)
21966 (error "No content for this element"))))
21967 (t (error "No inner element")))))
21970 (defun org-drag-element-backward ()
21971 "Move backward element at point."
21973 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
21974 (let* ((trail (org-element-at-point 'keep-trail))
21976 (prev-elem (nth 1 trail)))
21977 ;; Error out if no previous element or previous element is
21978 ;; a parent of the current one.
21979 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
21980 (error "Cannot drag element backward")
21981 (let ((pos (point)))
21982 (org-element-swap-A-B prev-elem elem)
21983 (goto-char (+ (org-element-property :begin prev-elem)
21984 (- pos (org-element-property :begin elem)))))))))
21987 (defun org-drag-element-forward ()
21988 "Move forward element at point."
21990 (let* ((pos (point))
21991 (elem (org-element-at-point)))
21992 (when (= (point-max) (org-element-property :end elem))
21993 (error "Cannot drag element forward"))
21994 (goto-char (org-element-property :end elem))
21995 (let ((next-elem (org-element-at-point)))
21996 (when (or (org-element-nested-p elem next-elem)
21997 (and (eq (org-element-type next-elem) 'headline)
21998 (not (eq (org-element-type elem) 'headline))))
22000 (error "Cannot drag element forward"))
22001 ;; Compute new position of point: it's shifted by NEXT-ELEM
22002 ;; body's length (without final blanks) and by the length of
22003 ;; blanks between ELEM and NEXT-ELEM.
22004 (let ((size-next (- (save-excursion
22005 (goto-char (org-element-property :end next-elem))
22006 (skip-chars-backward " \r\t\n")
22008 ;; Small correction if buffer doesn't end
22009 ;; with a newline character.
22010 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
22011 (org-element-property :begin next-elem)))
22012 (size-blank (- (org-element-property :end elem)
22014 (goto-char (org-element-property :end elem))
22015 (skip-chars-backward " \r\t\n")
22018 (org-element-swap-A-B elem next-elem)
22019 (goto-char (+ pos size-next size-blank))))))
22022 (defun org-mark-element ()
22023 "Put point at beginning of this element, mark at end.
22025 Interactively, if this command is repeated or (in Transient Mark
22026 mode) if the mark is active, it marks the next element after the
22027 ones already marked."
22029 (let (deactivate-mark)
22030 (if (and (org-called-interactively-p 'any)
22031 (or (and (eq last-command this-command) (mark t))
22032 (and transient-mark-mode mark-active)))
22036 (goto-char (org-element-property :end (org-element-at-point)))))
22037 (let ((element (org-element-at-point)))
22039 (push-mark (org-element-property :end element) t t)
22040 (goto-char (org-element-property :begin element))))))
22043 (defun org-narrow-to-element ()
22044 "Narrow buffer to current element."
22046 (let ((elem (org-element-at-point)))
22048 ((eq (car elem) 'headline)
22050 (org-element-property :begin elem)
22051 (org-element-property :end elem)))
22052 ((memq (car elem) org-element-greater-elements)
22054 (org-element-property :contents-begin elem)
22055 (org-element-property :contents-end elem)))
22058 (org-element-property :begin elem)
22059 (org-element-property :end elem))))))
22062 (defun org-transpose-element ()
22063 "Transpose current and previous elements, keeping blank lines between.
22064 Point is moved after both elements."
22066 (org-skip-whitespace)
22067 (let ((end (org-element-property :end (org-element-at-point))))
22068 (org-drag-element-backward)
22072 (defun org-unindent-buffer ()
22073 "Un-indent the visible part of the buffer.
22074 Relative indentation (between items, inside blocks, etc.) isn't
22077 (unless (eq major-mode 'org-mode)
22078 (error "Cannot un-indent a buffer not in Org mode"))
22079 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
22080 unindent-tree ; For byte-compiler.
22086 (if (memq (org-element-type element) '(headline section))
22087 (funcall unindent-tree (org-element-contents element))
22091 (org-element-property :begin element)
22092 (org-element-property :end element))
22093 (org-do-remove-indentation)))))
22094 (reverse contents))))))
22095 (funcall unindent-tree (org-element-contents parse-tree))))
22097 (defun org-show-subtree ()
22098 "Show everything after this heading at deeper levels."
22100 (outline-flag-region
22103 (org-end-of-subtree t t))
22106 (defun org-show-entry ()
22107 "Show the body directly following this heading.
22108 Show the heading too, if it is currently invisible."
22111 (condition-case nil
22113 (org-back-to-heading t)
22114 (outline-flag-region
22115 (max (point-min) (1- (point)))
22117 (if (re-search-forward
22118 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
22119 (match-beginning 1)
22122 (org-cycle-hide-drawers 'children))
22125 (defun org-make-options-regexp (kwds &optional extra)
22126 "Make a regular expression for keyword lines."
22129 (mapconcat 'regexp-quote kwds "\\|")
22130 (if extra (concat "\\|" extra))
22131 "\\):[ \t]*\\(.*\\)"))
22133 ;; Make isearch reveal the necessary context
22134 (defun org-isearch-end ()
22135 "Reveal context after isearch exits."
22136 (when isearch-success ; only if search was successful
22137 (if (featurep 'xemacs)
22138 ;; Under XEmacs, the hook is run in the correct place,
22139 ;; we directly show the context.
22140 (org-show-context 'isearch)
22141 ;; In Emacs the hook runs *before* restoring the overlays.
22142 ;; So we have to use a one-time post-command-hook to do this.
22143 ;; (Emacs 22 has a special variable, see function `org-mode')
22144 (unless (and (boundp 'isearch-mode-end-hook-quit)
22145 isearch-mode-end-hook-quit)
22146 ;; Only when the isearch was not quitted.
22147 (org-add-hook 'post-command-hook 'org-isearch-post-command
22148 'append 'local)))))
22150 (defun org-isearch-post-command ()
22151 "Remove self from hook, and show context."
22152 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
22153 (org-show-context 'isearch))
22156 ;;;; Integration with and fixes for other packages
22160 (defvar org-imenu-markers nil
22161 "All markers currently used by Imenu.")
22162 (make-variable-buffer-local 'org-imenu-markers)
22164 (defun org-imenu-new-marker (&optional pos)
22165 "Return a new marker for use by Imenu, and remember the marker."
22166 (let ((m (make-marker)))
22167 (move-marker m (or pos (point)))
22168 (push m org-imenu-markers)
22171 (defun org-imenu-get-tree ()
22172 "Produce the index for Imenu."
22173 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
22174 (setq org-imenu-markers nil)
22175 (let* ((n org-imenu-depth)
22176 (re (concat "^" (org-get-limited-outline-regexp)))
22177 (subs (make-vector (1+ n) nil))
22183 (goto-char (point-max))
22184 (while (re-search-backward re nil t)
22185 (setq level (org-reduced-level (funcall outline-level)))
22186 (when (and (<= level n)
22187 (looking-at org-complex-heading-regexp))
22188 (setq head (org-link-display-format
22189 (org-match-string-no-properties 4))
22190 m (org-imenu-new-marker))
22191 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
22192 (if (>= level last-level)
22193 (push (cons head m) (aref subs level))
22194 (push (cons head (aref subs (1+ level))) (aref subs level))
22195 (loop for i from (1+ level) to n do (aset subs i nil)))
22196 (setq last-level level)))))
22199 (eval-after-load "imenu"
22201 (add-hook 'imenu-after-jump-hook
22203 (if (derived-mode-p 'org-mode)
22204 (org-show-context 'org-goto))))))
22206 (defun org-link-display-format (link)
22207 "Replace a link with either the description, or the link target
22208 if no description is present"
22210 (if (string-match org-bracket-link-analytic-regexp link)
22211 (replace-match (if (match-end 5)
22212 (match-string 5 link)
22213 (concat (match-string 1 link)
22214 (match-string 3 link)))
22218 (defun org-toggle-link-display ()
22219 "Toggle the literal or descriptive display of links."
22221 (if org-descriptive-links
22222 (progn (org-remove-from-invisibility-spec '(org-link))
22223 (org-restart-font-lock)
22224 (setq org-descriptive-links nil))
22225 (progn (add-to-invisibility-spec '(org-link))
22226 (org-restart-font-lock)
22227 (setq org-descriptive-links t))))
22229 ;; Speedbar support
22231 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
22232 "Overlay marking the agenda restriction line in speedbar.")
22233 (overlay-put org-speedbar-restriction-lock-overlay
22234 'face 'org-agenda-restriction-lock)
22235 (overlay-put org-speedbar-restriction-lock-overlay
22236 'help-echo "Agendas are currently limited to this item.")
22237 (org-detach-overlay org-speedbar-restriction-lock-overlay)
22239 (defun org-speedbar-set-agenda-restriction ()
22240 "Restrict future agenda commands to the location at point in speedbar.
22241 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
22243 (require 'org-agenda)
22244 (let (p m tp np dir txt)
22246 ((setq p (text-property-any (point-at-bol) (point-at-eol)
22248 (setq m (get-text-property p 'org-imenu-marker))
22249 (with-current-buffer (marker-buffer m)
22251 (org-agenda-set-restriction-lock 'subtree)))
22252 ((setq p (text-property-any (point-at-bol) (point-at-eol)
22253 'speedbar-function 'speedbar-find-file))
22254 (setq tp (previous-single-property-change
22255 (1+ p) 'speedbar-function)
22256 np (next-single-property-change
22257 tp 'speedbar-function)
22258 dir (speedbar-line-directory)
22259 txt (buffer-substring-no-properties (or tp (point-min))
22260 (or np (point-max))))
22261 (with-current-buffer (find-file-noselect
22262 (let ((default-directory dir))
22263 (expand-file-name txt)))
22264 (unless (derived-mode-p 'org-mode)
22265 (error "Cannot restrict to non-Org-mode file"))
22266 (org-agenda-set-restriction-lock 'file)))
22267 (t (error "Don't know how to restrict Org-mode's agenda")))
22268 (move-overlay org-speedbar-restriction-lock-overlay
22269 (point-at-bol) (point-at-eol))
22270 (setq current-prefix-arg nil)
22271 (org-agenda-maybe-redo)))
22273 (eval-after-load "speedbar"
22275 (speedbar-add-supported-extension ".org")
22276 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
22277 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
22278 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
22279 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
22280 (add-hook 'speedbar-visiting-tag-hook
22281 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
22283 ;;; Fixes and Hacks for problems with other packages
22285 ;; Make flyspell not check words in links, to not mess up our keymap
22286 (defun org-mode-flyspell-verify ()
22287 "Don't let flyspell put overlays at active buttons, or on
22288 {todo,all-time,additional-option-like}-keywords."
22289 (let ((pos (max (1- (point)) (point-min)))
22290 (word (thing-at-point 'word)))
22291 (and (not (get-text-property pos 'keymap))
22292 (not (get-text-property pos 'org-no-flyspell))
22293 (not (member word org-todo-keywords-1))
22294 (not (member word org-all-time-keywords))
22295 (not (member word org-options-keywords))
22296 (not (member word (mapcar 'car org-startup-options)))
22297 (not (member word org-additional-option-like-keywords-for-flyspell)))))
22299 (defun org-remove-flyspell-overlays-in (beg end)
22300 "Remove flyspell overlays in region."
22301 (and (org-bound-and-true-p flyspell-mode)
22302 (fboundp 'flyspell-delete-region-overlays)
22303 (flyspell-delete-region-overlays beg end))
22304 (add-text-properties beg end '(org-no-flyspell t)))
22306 ;; Make `bookmark-jump' shows the jump location if it was hidden.
22307 (eval-after-load "bookmark"
22308 '(if (boundp 'bookmark-after-jump-hook)
22309 ;; We can use the hook
22310 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
22311 ;; Hook not available, use advice
22312 (defadvice bookmark-jump (after org-make-visible activate)
22313 "Make the position visible."
22314 (org-bookmark-jump-unhide))))
22316 ;; Make sure saveplace shows the location if it was hidden
22317 (eval-after-load "saveplace"
22318 '(defadvice save-place-find-file-hook (after org-make-visible activate)
22319 "Make the position visible."
22320 (org-bookmark-jump-unhide)))
22322 ;; Make sure ecb shows the location if it was hidden
22323 (eval-after-load "ecb"
22324 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
22325 "Make hierarchy visible when jumping into location from ECB tree buffer."
22326 (if (derived-mode-p 'org-mode)
22327 (org-show-context))))
22329 (defun org-bookmark-jump-unhide ()
22330 "Unhide the current position, to show the bookmark location."
22331 (and (derived-mode-p 'org-mode)
22332 (or (outline-invisible-p)
22333 (save-excursion (goto-char (max (point-min) (1- (point))))
22334 (outline-invisible-p)))
22335 (org-show-context 'bookmark-jump)))
22337 ;; Make session.el ignore our circular variable
22338 (eval-after-load "session"
22339 '(add-to-list 'session-globals-exclude 'org-mark-ring))
22341 ;;;; Experimental code
22343 (defun org-closed-in-range ()
22344 "Sparse tree of items closed in a certain time range.
22345 Still experimental, may disappear in the future."
22347 ;; Get the time interval from the user.
22348 (let* ((time1 (org-float-time
22349 (org-read-date nil 'to-time nil "Starting date: ")))
22350 (time2 (org-float-time
22351 (org-read-date nil 'to-time nil "End date:")))
22352 ;; callback function
22353 (callback (lambda ()
22356 (apply 'encode-time
22357 (org-parse-time-string
22358 (match-string 1))))))
22359 ;; check if time in interval
22360 (and (>= time time1) (<= time time2))))))
22361 ;; make tree, check each match with the callback
22362 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
22368 (run-hooks 'org-load-hook)
22370 ;;; org.el ends here