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.")
90 (defconst org-outline-regexp-bol
"^\\*+ "
91 "Regexp to match Org headlines.
92 This is similar to `org-outline-regexp' but additionally makes
93 sure that we are at the beginning of the line.")
95 (defconst org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
96 "Matches an headline, putting stars and text into groups.
97 Stars are put in group 1 and the trimmed body in group 2.")
99 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
100 (when (fboundp 'defvaralias
)
101 (unless (boundp 'calendar-view-holidays-initially-flag
)
102 (defvaralias 'calendar-view-holidays-initially-flag
103 'view-calendar-holidays-initially
))
104 (unless (boundp 'calendar-view-diary-initially-flag
)
105 (defvaralias 'calendar-view-diary-initially-flag
106 'view-diary-entries-initially
))
107 (unless (boundp 'diary-fancy-buffer
)
108 (defvaralias 'diary-fancy-buffer
'fancy-diary-buffer
)))
110 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
111 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
112 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
113 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
114 (declare-function org-at-clock-log-p
"org-clock" ())
115 (declare-function org-clock-timestamps-up
"org-clock" ())
116 (declare-function org-clock-timestamps-down
"org-clock" ())
118 ;; load languages based on value of `org-babel-load-languages'
119 (defvar org-babel-load-languages
)
121 (defun org-babel-do-load-languages (sym value
)
122 "Load the languages defined in `org-babel-load-languages'."
123 (set-default sym value
)
125 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
128 (require (intern (concat "ob-" lang
))))
130 (funcall 'fmakunbound
131 (intern (concat "org-babel-execute:" lang
)))
132 (funcall 'fmakunbound
133 (intern (concat "org-babel-expand-body:" lang
)))))))
134 org-babel-load-languages
))
136 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
137 "Languages which can be evaluated in Org-mode buffers.
138 This list can be used to load support for any of the languages
139 below, note that each language will depend on a different set of
140 system executables and/or Emacs modes. When a language is
141 \"loaded\", then code blocks in that language can be evaluated
142 with `org-babel-execute-src-block' bound by default to C-c
143 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
144 be set to remove code block evaluation from the C-c C-c
145 keybinding. By default only Emacs Lisp (which has no
146 requirements) is loaded."
148 :set
'org-babel-do-load-languages
150 :type
'(alist :tag
"Babel Languages"
153 (const :tag
"Awk" awk
)
156 (const :tag
"Asymptote" asymptote
)
157 (const :tag
"Calc" calc
)
158 (const :tag
"Clojure" clojure
)
159 (const :tag
"CSS" css
)
160 (const :tag
"Ditaa" ditaa
)
161 (const :tag
"Dot" dot
)
162 (const :tag
"Emacs Lisp" emacs-lisp
)
163 (const :tag
"Fortran" fortran
)
164 (const :tag
"Gnuplot" gnuplot
)
165 (const :tag
"Haskell" haskell
)
167 (const :tag
"Java" java
)
168 (const :tag
"Javascript" js
)
169 (const :tag
"LaTeX" latex
)
170 (const :tag
"Ledger" ledger
)
171 (const :tag
"Lilypond" lilypond
)
172 (const :tag
"Lisp" lisp
)
173 (const :tag
"Maxima" maxima
)
174 (const :tag
"Matlab" matlab
)
175 (const :tag
"Mscgen" mscgen
)
176 (const :tag
"Ocaml" ocaml
)
177 (const :tag
"Octave" octave
)
178 (const :tag
"Org" org
)
179 (const :tag
"Perl" perl
)
180 (const :tag
"Pico Lisp" picolisp
)
181 (const :tag
"PlantUML" plantuml
)
182 (const :tag
"Python" python
)
183 (const :tag
"Ruby" ruby
)
184 (const :tag
"Sass" sass
)
185 (const :tag
"Scala" scala
)
186 (const :tag
"Scheme" scheme
)
187 (const :tag
"Screen" screen
)
188 (const :tag
"Shell Script" sh
)
189 (const :tag
"Shen" shen
)
190 (const :tag
"Sql" sql
)
191 (const :tag
"Sqlite" sqlite
))
192 :value-type
(boolean :tag
"Activate" :value t
)))
194 ;;;; Customization variables
195 (defcustom org-clone-delete-id nil
196 "Remove ID property of clones of a subtree.
197 When non-nil, clones of a subtree don't inherit the ID property.
198 Otherwise they inherit the ID property with a new unique
205 (require 'org-compat
)
208 (defun org-version (&optional here full message
)
209 "Show the org-mode version in the echo area.
210 With prefix argument HERE, insert it at point.
211 When FULL is non-nil, use a verbose version string.
212 When MESSAGE is non-nil, display a message with the version."
214 (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
215 (org-install-dir (ignore-errors (org-find-library-dir "org-install.el")))
217 (and (fboundp 'org-release
) (fboundp 'org-git-version
))
218 (load (concat org-dir
"org-version.el")
219 'noerror
'nomessage
'nosuffix
)))
220 (org-version (org-release))
221 (git-version (org-git-version))
222 (version (format "Org-mode version %s (%s @ %s)"
226 (if (string= org-dir org-install-dir
)
228 (concat "mixed installation! " org-install-dir
" and " org-dir
))
229 "org-install.el can not be found!")))
230 (_version (if full version org-version
)))
231 (if (org-called-interactively-p 'interactive
)
235 (if message
(message _version
))
238 ;;; Compatibility constants
240 ;;; The custom variables
243 "Outline-based notes management and organizer."
248 (defcustom org-mode-hook nil
249 "Mode hook for Org-mode, run after the mode was turned on."
253 (defcustom org-load-hook nil
254 "Hook that is run after org.el has been loaded."
258 (defcustom org-log-buffer-setup-hook nil
259 "Hook that is run after an Org log buffer is created."
264 (defvar org-modules
) ; defined below
265 (defvar org-modules-loaded nil
266 "Have the modules been loaded already?")
268 (defun org-load-modules-maybe (&optional force
)
269 "Load all extensions listed in `org-modules'."
270 (when (or force
(not org-modules-loaded
))
272 (condition-case nil
(require ext
)
273 (error (message "Problems while trying to load feature `%s'" ext
))))
275 (setq org-modules-loaded t
)))
277 (defun org-set-modules (var value
)
278 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
280 (when (featurep 'org
)
281 (org-load-modules-maybe 'force
)))
283 (when (org-bound-and-true-p org-modules
)
284 (let ((a (member 'org-infojs org-modules
)))
285 (and a
(setcar a
'org-jsinfo
))))
287 (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
)
288 "Modules that should always be loaded together with org.el.
289 If a description starts with <C>, the file is not part of Emacs
290 and loading it will require that you have downloaded and properly installed
291 the org-mode distribution.
293 You can also use this system to load external packages (i.e. neither Org
294 core modules, nor modules from the CONTRIB directory). Just add symbols
295 to the end of the list. If the package is called org-xyz.el, then you need
296 to add the symbol `xyz', and the package must have a call to
300 :set
'org-set-modules
303 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
304 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
305 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
306 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
307 (const :tag
" docview: Links to doc-view buffers" org-docview
)
308 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
309 (const :tag
" id: Global IDs for identifying entries" org-id
)
310 (const :tag
" info: Links to Info nodes" org-info
)
311 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
312 (const :tag
" habit: Track your consistency with habits" org-habit
)
313 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
314 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
315 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
316 (const :tag
" mew Links to Mew folders/messages" org-mew
)
317 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
318 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
319 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
320 (const :tag
" special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks
)
321 (const :tag
" vm: Links to VM folders/messages" org-vm
)
322 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
323 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
324 (const :tag
" mouse: Additional mouse support" org-mouse
)
325 (const :tag
" TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler
)
327 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
328 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
329 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
330 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
331 (const :tag
"C collector: Collect properties into tables" org-collector
)
332 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
333 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
334 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
335 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
336 (const :tag
"C eval: Include command output as text" org-eval
)
337 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
338 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
339 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
340 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
341 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
343 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
345 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
346 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
347 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
348 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
349 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
350 (const :tag
"C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber
)
351 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
352 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
353 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
354 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
355 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
356 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
357 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
358 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
359 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
360 (const :tag
"C track: Keep up with Org-mode development" org-track
)
361 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
362 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
363 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
365 (defcustom org-support-shift-select nil
366 "Non-nil means make shift-cursor commands select text when possible.
368 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
369 start selecting a region, or enlarge regions started in this way.
370 In Org-mode, in special contexts, these same keys are used for
371 other purposes, important enough to compete with shift selection.
372 Org tries to balance these needs by supporting `shift-select-mode'
373 outside these special contexts, under control of this variable.
375 The default of this variable is nil, to avoid confusing behavior. Shifted
376 cursor keys will then execute Org commands in the following contexts:
377 - on a headline, changing TODO state (left/right) and priority (up/down)
378 - on a time stamp, changing the time
379 - in a plain list item, changing the bullet type
380 - in a property definition line, switching between allowed values
381 - in the BEGIN line of a clock table (changing the time block).
382 Outside these contexts, the commands will throw an error.
384 When this variable is t and the cursor is not in a special
385 context, Org-mode will support shift-selection for making and
386 enlarging regions. To make this more effective, the bullet
387 cycling will no longer happen anywhere in an item line, but only
388 if the cursor is exactly on the bullet.
390 If you set this variable to the symbol `always', then the keys
391 will not be special in headlines, property lines, and item lines,
392 to make shift selection work there as well. If this is what you
393 want, you can use the following alternative commands: `C-c C-t'
394 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
395 can be used to switch TODO sets, `C-c -' to cycle item bullet
396 types, and properties can be edited by hand or in column view.
398 However, when the cursor is on a timestamp, shift-cursor commands
399 will still edit the time stamp - this is just too good to give up.
401 XEmacs user should have this variable set to nil, because
402 `shift-select-mode' is in Emacs 23 or later only."
405 (const :tag
"Never" nil
)
406 (const :tag
"When outside special context" t
)
407 (const :tag
"Everywhere except timestamps" always
)))
409 (defcustom org-loop-over-headlines-in-active-region nil
410 "Shall some commands act upon headlines in the active region?
412 When set to `t', some commands will be performed in all headlines
413 within the active region.
415 When set to `start-level', some commands will be performed in all
416 headlines within the active region, provided that these headlines
417 are of the same level than the first one.
419 When set to a string, those commands will be performed on the
420 matching headlines within the active region. Such string must be
421 a tags/property/todo match as it is used in the agenda tags view.
423 The list of commands is: `org-schedule', `org-deadline',
424 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
425 `org-archive-to-archive-sibling'. The archiving commands skip
426 already archived entries."
427 :type
'(choice (const :tag
"Don't loop" nil
)
428 (const :tag
"All headlines in active region" t
)
429 (const :tag
"In active region, headlines at the same level than the first one" 'start-level
)
430 (string :tag
"Tags/Property/Todo matcher"))
435 (defgroup org-startup nil
436 "Options concerning startup of Org-mode."
440 (defcustom org-startup-folded t
441 "Non-nil means entering Org-mode will switch to OVERVIEW.
442 This can also be configured on a per-file basis by adding one of
443 the following lines anywhere in the buffer:
445 #+STARTUP: fold (or `overview', this is equivalent)
446 #+STARTUP: nofold (or `showall', this is equivalent)
448 #+STARTUP: showeverything"
451 (const :tag
"nofold: show all" nil
)
452 (const :tag
"fold: overview" t
)
453 (const :tag
"content: all headlines" content
)
454 (const :tag
"show everything, even drawers" showeverything
)))
456 (defcustom org-startup-truncated t
457 "Non-nil means entering Org-mode will set `truncate-lines'.
458 This is useful since some lines containing links can be very long and
459 uninteresting. Also tables look terrible when wrapped."
463 (defcustom org-startup-indented nil
464 "Non-nil means turn on `org-indent-mode' on startup.
465 This can also be configured on a per-file basis by adding one of
466 the following lines anywhere in the buffer:
470 :group
'org-structure
472 (const :tag
"Not" nil
)
473 (const :tag
"Globally (slow on startup in large files)" t
)))
475 (defcustom org-use-sub-superscripts t
476 "Non-nil means interpret \"_\" and \"^\" for export.
477 When this option is turned on, you can use TeX-like syntax for sub- and
478 superscripts. Several characters after \"_\" or \"^\" will be
479 considered as a single item - so grouping with {} is normally not
480 needed. For example, the following things will be parsed as single
481 sub- or superscripts.
483 10^24 or 10^tau several digits will be considered 1 item.
484 10^-12 or 10^-tau a leading sign with digits or a word
485 x^2-y^3 will be read as x^2 - y^3, because items are
486 terminated by almost any nonword/nondigit char.
487 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
489 Still, ambiguity is possible - so when in doubt use {} to enclose the
490 sub/superscript. If you set this variable to the symbol `{}',
491 the braces are *required* in order to trigger interpretations as
492 sub/superscript. This can be helpful in documents that need \"_\"
493 frequently in plain text.
495 Not all export backends support this, but HTML does.
497 This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
499 :group
'org-export-translation
502 (const :tag
"Always interpret" t
)
503 (const :tag
"Only with braces" {})
504 (const :tag
"Never interpret" nil
)))
506 (if (fboundp 'defvaralias
)
507 (defvaralias 'org-export-with-sub-superscripts
'org-use-sub-superscripts
))
510 (defcustom org-startup-with-beamer-mode nil
511 "Non-nil means turn on `org-beamer-mode' on startup.
512 This can also be configured on a per-file basis by adding one of
513 the following lines anywhere in the buffer:
520 (defcustom org-startup-align-all-tables nil
521 "Non-nil means align all tables when visiting a file.
522 This is useful when the column width in tables is forced with <N> cookies
523 in table fields. Such tables will look correct only after the first re-align.
524 This can also be configured on a per-file basis by adding one of
525 the following lines anywhere in the buffer:
531 (defcustom org-startup-with-inline-images nil
532 "Non-nil means show inline images when loading a new Org file.
533 This can also be configured on a per-file basis by adding one of
534 the following lines anywhere in the buffer:
535 #+STARTUP: inlineimages
536 #+STARTUP: noinlineimages"
541 (defcustom org-insert-mode-line-in-empty-file nil
542 "Non-nil means insert the first line setting Org-mode in empty files.
543 When the function `org-mode' is called interactively in an empty file, this
544 normally means that the file name does not automatically trigger Org-mode.
545 To ensure that the file will always be in Org-mode in the future, a
546 line enforcing Org-mode will be inserted into the buffer, if this option
551 (defcustom org-replace-disputed-keys nil
552 "Non-nil means use alternative key bindings for some keys.
553 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
554 These keys are also used by other packages like shift-selection-mode'
555 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
556 If you want to use Org-mode together with one of these other modes,
557 or more generally if you would like to move some Org-mode commands to
558 other keys, set this variable and configure the keys with the variable
561 This option is only relevant at load-time of Org-mode, and must be set
562 *before* org.el is loaded. Changing it requires a restart of Emacs to
567 (defcustom org-use-extra-keys nil
568 "Non-nil means use extra key sequence definitions for certain commands.
569 This happens automatically if you run XEmacs or if `window-system'
570 is nil. This variable lets you do the same manually. You must
571 set it before loading org.
573 Example: on Carbon Emacs 22 running graphically, with an external
574 keyboard on a Powerbook, the default way of setting M-left might
575 not work for either Alt or ESC. Setting this variable will make
580 (if (fboundp 'defvaralias
)
581 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
583 (defcustom org-disputed-keys
584 '(([(shift up
)] .
[(meta p
)])
585 ([(shift down
)] .
[(meta n
)])
586 ([(shift left
)] .
[(meta -
)])
587 ([(shift right
)] .
[(meta +)])
588 ([(control shift right
)] .
[(meta shift
+)])
589 ([(control shift left
)] .
[(meta shift -
)]))
590 "Keys for which Org-mode and other modes compete.
591 This is an alist, cars are the default keys, second element specifies
592 the alternative to use when `org-replace-disputed-keys' is t.
594 Keys can be specified in any syntax supported by `define-key'.
595 The value of this option takes effect only at Org-mode's startup,
596 therefore you'll have to restart Emacs to apply it after changing."
601 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
602 Or return the original if not disputed.
603 Also apply the translations defined in `org-xemacs-key-equivalents'."
604 (when org-replace-disputed-keys
605 (let* ((nkey (key-description key
))
606 (x (org-find-if (lambda (x)
607 (equal (key-description (car x
)) nkey
))
609 (setq key
(if x
(cdr x
) key
))))
610 (when (featurep 'xemacs
)
611 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
614 (defun org-find-if (predicate seq
)
617 (if (funcall predicate
(car seq
))
618 (throw 'exit
(car seq
))
621 (defun org-defkey (keymap key def
)
622 "Define a key, possibly translated, as returned by `org-key'."
623 (define-key keymap
(org-key key
) def
))
625 (defcustom org-ellipsis nil
626 "The ellipsis to use in the Org-mode outline.
627 When nil, just use the standard three dots. When a string, use that instead,
628 When a face, use the standard 3 dots, but with the specified face.
629 The change affects only Org-mode (which will then use its own display table).
630 Changing this requires executing `M-x org-mode' in a buffer to become
633 :type
'(choice (const :tag
"Default" nil
)
634 (face :tag
"Face" :value org-warning
)
635 (string :tag
"String" :value
"...#")))
637 (defvar org-display-table nil
638 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
640 (defgroup org-keywords nil
641 "Keywords in Org-mode."
645 (defcustom org-deadline-string
"DEADLINE:"
646 "String to mark deadline entries.
647 A deadline is this string, followed by a time stamp. Should be a word,
648 terminated by a colon. You can insert a schedule keyword and
649 a timestamp with \\[org-deadline].
650 Changes become only effective after restarting Emacs."
654 (defcustom org-scheduled-string
"SCHEDULED:"
655 "String to mark scheduled TODO entries.
656 A schedule is this string, followed by a time stamp. Should be a word,
657 terminated by a colon. You can insert a schedule keyword and
658 a timestamp with \\[org-schedule].
659 Changes become only effective after restarting Emacs."
663 (defcustom org-closed-string
"CLOSED:"
664 "String used as the prefix for timestamps logging closing a TODO entry."
668 (defcustom org-clock-string
"CLOCK:"
669 "String used as prefix for timestamps clocking work hours on an item."
673 (defconst org-planning-or-clock-line-re
(concat "^[ \t]*\\("
674 org-scheduled-string
"\\|"
675 org-deadline-string
"\\|"
676 org-closed-string
"\\|"
677 org-clock-string
"\\)")
678 "Matches a line with planning or clock info.")
680 (defcustom org-comment-string
"COMMENT"
681 "Entries starting with this keyword will never be exported.
682 An entry can be toggled between COMMENT and normal with
683 \\[org-toggle-comment].
684 Changes become only effective after restarting Emacs."
688 (defcustom org-quote-string
"QUOTE"
689 "Entries starting with this keyword will be exported in fixed-width font.
690 Quoting applies only to the text in the entry following the headline, and does
691 not extend beyond the next headline, even if that is lower level.
692 An entry can be toggled between QUOTE and normal with
693 \\[org-toggle-fixed-width-section]."
697 (defconst org-repeat-re
698 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
699 "Regular expression for specifying repeated events.
700 After a match, group 1 contains the repeat expression.")
702 (defgroup org-structure nil
703 "Options concerning the general structure of Org-mode files."
707 (defgroup org-reveal-location nil
708 "Options about how to make context of a location visible."
709 :tag
"Org Reveal Location"
710 :group
'org-structure
)
712 (defconst org-context-choice
714 (const :tag
"Always" t
)
715 (const :tag
"Never" nil
)
716 (repeat :greedy t
:tag
"Individual contexts"
718 (choice :tag
"Context"
725 (const bookmark-jump
)
729 "Contexts for the reveal options.")
731 (defcustom org-show-hierarchy-above
'((default . t
))
732 "Non-nil means show full hierarchy when revealing a location.
733 Org-mode often shows locations in an org-mode file which might have
734 been invisible before. When this is set, the hierarchy of headings
735 above the exposed location is shown.
736 Turning this off for example for sparse trees makes them very compact.
737 Instead of t, this can also be an alist specifying this option for different
738 contexts. Valid contexts are
739 agenda when exposing an entry from the agenda
740 org-goto when using the command `org-goto' on key C-c C-j
741 occur-tree when using the command `org-occur' on key C-c /
742 tags-tree when constructing a sparse tree based on tags matches
743 link-search when exposing search matches associated with a link
744 mark-goto when exposing the jump goal of a mark
745 bookmark-jump when exposing a bookmark location
746 isearch when exiting from an incremental search
747 default default for all contexts not set explicitly"
748 :group
'org-reveal-location
749 :type org-context-choice
)
751 (defcustom org-show-following-heading
'((default . nil
))
752 "Non-nil means show following heading when revealing a location.
753 Org-mode often shows locations in an org-mode file which might have
754 been invisible before. When this is set, the heading following the
756 Turning this off for example for sparse trees makes them very compact,
757 but makes it harder to edit the location of the match. In such a case,
758 use the command \\[org-reveal] to show more context.
759 Instead of t, this can also be an alist specifying this option for different
760 contexts. See `org-show-hierarchy-above' for valid contexts."
761 :group
'org-reveal-location
762 :type org-context-choice
)
764 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
765 "Non-nil means show all sibling heading when revealing a location.
766 Org-mode often shows locations in an org-mode file which might have
767 been invisible before. When this is set, the sibling of the current entry
768 heading are all made visible. If `org-show-hierarchy-above' is t,
769 the same happens on each level of the hierarchy above the current entry.
771 By default this is on for the isearch context, off for all other contexts.
772 Turning this off for example for sparse trees makes them very compact,
773 but makes it harder to edit the location of the match. In such a case,
774 use the command \\[org-reveal] to show more context.
775 Instead of t, this can also be an alist specifying this option for different
776 contexts. See `org-show-hierarchy-above' for valid contexts."
777 :group
'org-reveal-location
778 :type org-context-choice
)
780 (defcustom org-show-entry-below
'((default . nil
))
781 "Non-nil means show the entry below a headline when revealing a location.
782 Org-mode often shows locations in an org-mode file which might have
783 been invisible before. When this is set, the text below the headline that is
784 exposed is also shown.
786 By default this is off for all contexts.
787 Instead of t, this can also be an alist specifying this option for different
788 contexts. See `org-show-hierarchy-above' for valid contexts."
789 :group
'org-reveal-location
790 :type org-context-choice
)
792 (defcustom org-indirect-buffer-display
'other-window
793 "How should indirect tree buffers be displayed?
794 This applies to indirect buffers created with the commands
795 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
797 current-window Display in the current window
798 other-window Just display in another window.
799 dedicated-frame Create one new frame, and re-use it each time.
800 new-frame Make a new frame each time. Note that in this case
801 previously-made indirect buffers are kept, and you need to
802 kill these buffers yourself."
803 :group
'org-structure
804 :group
'org-agenda-windows
806 (const :tag
"In current window" current-window
)
807 (const :tag
"In current frame, other window" other-window
)
808 (const :tag
"Each time a new frame" new-frame
)
809 (const :tag
"One dedicated frame" dedicated-frame
)))
811 (defcustom org-use-speed-commands nil
812 "Non-nil means activate single letter commands at beginning of a headline.
813 This may also be a function to test for appropriate locations where speed
814 commands should be active."
815 :group
'org-structure
817 (const :tag
"Never" nil
)
818 (const :tag
"At beginning of headline stars" t
)
821 (defcustom org-speed-commands-user nil
822 "Alist of additional speed commands.
823 This list will be checked before `org-speed-commands-default'
824 when the variable `org-use-speed-commands' is non-nil
825 and when the cursor is at the beginning of a headline.
826 The car if each entry is a string with a single letter, which must
827 be assigned to `self-insert-command' in the global map.
828 The cdr is either a command to be called interactively, a function
829 to be called, or a form to be evaluated.
830 An entry that is just a list with a single string will be interpreted
831 as a descriptive headline that will be added when listing the speed
832 commands in the Help buffer using the `?' speed command."
833 :group
'org-structure
834 :type
'(repeat :value
("k" . ignore
)
835 (choice :value
("k" . ignore
)
836 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
837 (cons :tag
"Letter and Command"
838 (string :tag
"Command letter")
843 (defgroup org-cycle nil
844 "Options concerning visibility cycling in Org-mode."
846 :group
'org-structure
)
848 (defcustom org-cycle-skip-children-state-if-no-children t
849 "Non-nil means skip CHILDREN state in entries that don't have any."
853 (defcustom org-cycle-max-level nil
854 "Maximum level which should still be subject to visibility cycling.
855 Levels higher than this will, for cycling, be treated as text, not a headline.
856 When `org-odd-levels-only' is set, a value of N in this variable actually
857 means 2N-1 stars as the limiting headline.
858 When nil, cycle all levels.
859 Note that the limiting level of cycling is also influenced by
860 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
861 `org-inlinetask-min-level' is, cycling will be limited to levels one less
865 (const :tag
"No limit" nil
)
866 (integer :tag
"Maximum level")))
868 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
869 "Names of drawers. Drawers are not opened by cycling on the headline above.
870 Drawers only open with a TAB on the drawer line itself. A drawer looks like
875 The drawer \"PROPERTIES\" is special for capturing properties through
878 Drawers can be defined on the per-file basis with a line like:
880 #+DRAWERS: HIDDEN STATE PROPERTIES"
881 :group
'org-structure
883 :type
'(repeat (string :tag
"Drawer Name")))
885 (defcustom org-hide-block-startup nil
886 "Non-nil means entering Org-mode will fold all blocks.
887 This can also be set in on a per-file basis with
889 #+STARTUP: hideblocks
890 #+STARTUP: showblocks"
895 (defcustom org-cycle-global-at-bob nil
896 "Cycle globally if cursor is at beginning of buffer and not at a headline.
897 This makes it possible to do global cycling without having to use S-TAB or
898 \\[universal-argument] TAB. For this special case to work, the first line \
900 must not be a headline - it may be empty or some other text. When used in
901 this way, `org-cycle-hook' is disables temporarily, to make sure the
902 cursor stays at the beginning of the buffer.
903 When this option is nil, don't do anything special at the beginning
908 (defcustom org-cycle-level-after-item
/entry-creation t
909 "Non-nil means cycle entry level or item indentation in new empty entries.
911 When the cursor is at the end of an empty headline, i.e with only stars
912 and maybe a TODO keyword, TAB will then switch the entry to become a child,
913 and then all possible ancestor states, before returning to the original state.
914 This makes data entry extremely fast: M-RET to create a new headline,
915 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
917 When the cursor is at the end of an empty plain list item, one TAB will
918 make it a subitem, two or more tabs will back up to make this an item
919 higher up in the item hierarchy."
923 (defcustom org-cycle-emulate-tab t
924 "Where should `org-cycle' emulate TAB.
926 white Only in completely white lines
927 whitestart Only at the beginning of lines, before the first non-white char
928 t Everywhere except in headlines
929 exc-hl-bol Everywhere except at the start of a headline
930 If TAB is used in a place where it does not emulate TAB, the current subtree
931 visibility is cycled."
933 :type
'(choice (const :tag
"Never" nil
)
934 (const :tag
"Only in completely white lines" white
)
935 (const :tag
"Before first char in a line" whitestart
)
936 (const :tag
"Everywhere except in headlines" t
)
937 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
940 (defcustom org-cycle-separator-lines
2
941 "Number of empty lines needed to keep an empty line between collapsed trees.
942 If you leave an empty line between the end of a subtree and the following
943 headline, this empty line is hidden when the subtree is folded.
944 Org-mode will leave (exactly) one empty line visible if the number of
945 empty lines is equal or larger to the number given in this variable.
946 So the default 2 means at least 2 empty lines after the end of a subtree
947 are needed to produce free space between a collapsed subtree and the
950 If the number is negative, and the number of empty lines is at least -N,
951 all empty lines are shown.
953 Special case: when 0, never leave empty lines in collapsed view."
956 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
958 (defcustom org-pre-cycle-hook nil
959 "Hook that is run before visibility cycling is happening.
960 The function(s) in this hook must accept a single argument which indicates
961 the new state that will be set right after running this hook. The
962 argument is a symbol. Before a global state change, it can have the values
963 `overview', `content', or `all'. Before a local state change, it can have
964 the values `folded', `children', or `subtree'."
968 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
969 org-cycle-hide-drawers
970 org-cycle-show-empty-lines
971 org-optimize-window-after-visibility-change
)
972 "Hook that is run after `org-cycle' has changed the buffer visibility.
973 The function(s) in this hook must accept a single argument which indicates
974 the new state that was set by the most recent `org-cycle' command. The
975 argument is a symbol. After a global state change, it can have the values
976 `overview', `content', or `all'. After a local state change, it can have
977 the values `folded', `children', or `subtree'."
981 (defgroup org-edit-structure nil
982 "Options concerning structure editing in Org-mode."
983 :tag
"Org Edit Structure"
984 :group
'org-structure
)
986 (defcustom org-odd-levels-only nil
987 "Non-nil means skip even levels and only use odd levels for the outline.
988 This has the effect that two stars are being added/taken away in
989 promotion/demotion commands. It also influences how levels are
990 handled by the exporters.
991 Changing it requires restart of `font-lock-mode' to become effective
992 for fontification also in regions already fontified.
993 You may also set this on a per-file basis by adding one of the following
998 :group
'org-edit-structure
999 :group
'org-appearance
1002 (defcustom org-adapt-indentation t
1003 "Non-nil means adapt indentation to outline node level.
1005 When this variable is set, Org assumes that you write outlines by
1006 indenting text in each node to align with the headline (after the stars).
1007 The following issues are influenced by this variable:
1009 - When this is set and the *entire* text in an entry is indented, the
1010 indentation is increased by one space in a demotion command, and
1011 decreased by one in a promotion command. If any line in the entry
1012 body starts with text at column 0, indentation is not changed at all.
1014 - Property drawers and planning information is inserted indented when
1015 this variable s set. When nil, they will not be indented.
1017 - TAB indents a line relative to context. The lines below a headline
1018 will be indented when this variable is set.
1020 Note that this is all about true indentation, by adding and removing
1021 space characters. See also `org-indent.el' which does level-dependent
1022 indentation in a virtual way, i.e. at display time in Emacs."
1023 :group
'org-edit-structure
1026 (defcustom org-special-ctrl-a
/e nil
1027 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1029 When t, `C-a' will bring back the cursor to the beginning of the
1030 headline text, i.e. after the stars and after a possible TODO
1031 keyword. In an item, this will be the position after bullet and
1032 check-box, if any. When the cursor is already at that position,
1033 another `C-a' will bring it to the beginning of the line.
1035 `C-e' will jump to the end of the headline, ignoring the presence
1036 of tags in the headline. A second `C-e' will then jump to the
1037 true end of the line, after any tags. This also means that, when
1038 this variable is non-nil, `C-e' also will never jump beyond the
1039 end of the heading of a folded section, i.e. not after the
1042 When set to the symbol `reversed', the first `C-a' or `C-e' works
1043 normally, going to the true line boundary first. Only a directly
1044 following, identical keypress will bring the cursor to the
1047 This may also be a cons cell where the behavior for `C-a' and
1048 `C-e' is set separately."
1049 :group
'org-edit-structure
1051 (const :tag
"off" nil
)
1052 (const :tag
"on: after stars/bullet and before tags first" t
)
1053 (const :tag
"reversed: true line boundary first" reversed
)
1054 (cons :tag
"Set C-a and C-e separately"
1055 (choice :tag
"Special C-a"
1056 (const :tag
"off" nil
)
1057 (const :tag
"on: after stars/bullet first" t
)
1058 (const :tag
"reversed: before stars/bullet first" reversed
))
1059 (choice :tag
"Special C-e"
1060 (const :tag
"off" nil
)
1061 (const :tag
"on: before tags first" t
)
1062 (const :tag
"reversed: after tags first" reversed
)))))
1063 (if (fboundp 'defvaralias
)
1064 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
1066 (defcustom org-special-ctrl-k nil
1067 "Non-nil means `C-k' will behave specially in headlines.
1068 When nil, `C-k' will call the default `kill-line' command.
1069 When t, the following will happen while the cursor is in the headline:
1071 - When the cursor is at the beginning of a headline, kill the entire
1072 line and possible the folded subtree below the line.
1073 - When in the middle of the headline text, kill the headline up to the tags.
1074 - When after the headline text, kill the tags."
1075 :group
'org-edit-structure
1078 (defcustom org-ctrl-k-protect-subtree nil
1079 "Non-nil means, do not delete a hidden subtree with C-k.
1080 When set to the symbol `error', simply throw an error when C-k is
1081 used to kill (part-of) a headline that has hidden text behind it.
1082 Any other non-nil value will result in a query to the user, if it is
1083 OK to kill that hidden subtree. When nil, kill without remorse."
1084 :group
'org-edit-structure
1087 (const :tag
"Do not protect hidden subtrees" nil
)
1088 (const :tag
"Protect hidden subtrees with a security query" t
)
1089 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1091 (defcustom org-catch-invisible-edits nil
1092 "Check if in invisible region before inserting or deleting a character.
1095 nil Do not check, so just do invisible edits.
1096 error Throw an error and do nothing.
1097 show Make point visible, and do the requested edit.
1098 show-and-error Make point visible, then throw an error and abort the edit.
1099 smart Make point visible, and do insertion/deletion if it is
1100 adjacent to visible text and the change feels predictable.
1101 Never delete a previously invisible character or add in the
1102 middle or right after an invisible region. Basically, this
1103 allows insertion and backward-delete right before ellipses.
1104 FIXME: maybe in this case we should not even show?"
1105 :group
'org-edit-structure
1108 (const :tag
"Do not check" nil
)
1109 (const :tag
"Throw error when trying to edit" error
)
1110 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1111 (const :tag
"Show invisible part and do the edit" show
)
1112 (const :tag
"Be smart and do the right thing" smart
)))
1114 (defcustom org-yank-folded-subtrees t
1115 "Non-nil means when yanking subtrees, fold them.
1116 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1117 it starts with a heading and all other headings in it are either children
1118 or siblings, then fold all the subtrees. However, do this only if no
1119 text after the yank would be swallowed into a folded tree by this action."
1120 :group
'org-edit-structure
1123 (defcustom org-yank-adjusted-subtrees nil
1124 "Non-nil means when yanking subtrees, adjust the level.
1125 With this setting, `org-paste-subtree' is used to insert the subtree, see
1126 this function for details."
1127 :group
'org-edit-structure
1130 (defcustom org-M-RET-may-split-line
'((default . t
))
1131 "Non-nil means M-RET will split the line at the cursor position.
1132 When nil, it will go to the end of the line before making a
1134 You may also set this option in a different way for different
1135 contexts. Valid contexts are:
1137 headline when creating a new headline
1138 item when creating a new item
1139 table in a table field
1140 default the value to be used for all contexts not explicitly
1142 :group
'org-structure
1145 (const :tag
"Always" t
)
1146 (const :tag
"Never" nil
)
1147 (repeat :greedy t
:tag
"Individual contexts"
1149 (choice :tag
"Context"
1157 (defcustom org-insert-heading-respect-content nil
1158 "Non-nil means insert new headings after the current subtree.
1159 When nil, the new heading is created directly after the current line.
1160 The commands \\[org-insert-heading-respect-content] and
1161 \\[org-insert-todo-heading-respect-content] turn this variable on
1162 for the duration of the command."
1163 :group
'org-structure
1166 (defcustom org-blank-before-new-entry
'((heading . auto
)
1167 (plain-list-item . auto
))
1168 "Should `org-insert-heading' leave a blank line before new heading/item?
1169 The value is an alist, with `heading' and `plain-list-item' as CAR,
1170 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1171 which case Org will look at the surrounding headings/items and try to
1172 make an intelligent decision whether to insert a blank line or not.
1174 For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
1175 set, the setting here is ignored and no empty line is inserted, to avoid
1176 breaking the list structure."
1177 :group
'org-edit-structure
1179 (cons (const heading
)
1180 (choice (const :tag
"Never" nil
)
1181 (const :tag
"Always" t
)
1182 (const :tag
"Auto" auto
)))
1183 (cons (const plain-list-item
)
1184 (choice (const :tag
"Never" nil
)
1185 (const :tag
"Always" t
)
1186 (const :tag
"Auto" auto
)))))
1188 (defcustom org-insert-heading-hook nil
1189 "Hook being run after inserting a new heading."
1190 :group
'org-edit-structure
1193 (defcustom org-enable-fixed-width-editor t
1194 "Non-nil means lines starting with \":\" are treated as fixed-width.
1195 This currently only means they are never auto-wrapped.
1196 When nil, such lines will be treated like ordinary lines.
1197 See also the QUOTE keyword."
1198 :group
'org-edit-structure
1201 (defcustom org-goto-auto-isearch t
1202 "Non-nil means typing characters in `org-goto' starts incremental search."
1203 :group
'org-edit-structure
1206 (defgroup org-sparse-trees nil
1207 "Options concerning sparse trees in Org-mode."
1208 :tag
"Org Sparse Trees"
1209 :group
'org-structure
)
1211 (defcustom org-highlight-sparse-tree-matches t
1212 "Non-nil means highlight all matches that define a sparse tree.
1213 The highlights will automatically disappear the next time the buffer is
1214 changed by an edit command."
1215 :group
'org-sparse-trees
1218 (defcustom org-remove-highlights-with-change t
1219 "Non-nil means any change to the buffer will remove temporary highlights.
1220 Such highlights are created by `org-occur' and `org-clock-display'.
1221 When nil, `C-c C-c needs to be used to get rid of the highlights.
1222 The highlights created by `org-preview-latex-fragment' always need
1223 `C-c C-c' to be removed."
1224 :group
'org-sparse-trees
1229 (defcustom org-occur-hook
'(org-first-headline-recenter)
1230 "Hook that is run after `org-occur' has constructed a sparse tree.
1231 This can be used to recenter the window to show as much of the structure
1233 :group
'org-sparse-trees
1236 (defgroup org-imenu-and-speedbar nil
1237 "Options concerning imenu and speedbar in Org-mode."
1238 :tag
"Org Imenu and Speedbar"
1239 :group
'org-structure
)
1241 (defcustom org-imenu-depth
2
1242 "The maximum level for Imenu access to Org-mode headlines.
1243 This also applied for speedbar access."
1244 :group
'org-imenu-and-speedbar
1247 (defgroup org-table nil
1248 "Options concerning tables in Org-mode."
1252 (defcustom org-enable-table-editor
'optimized
1253 "Non-nil means lines starting with \"|\" are handled by the table editor.
1254 When nil, such lines will be treated like ordinary lines.
1256 When equal to the symbol `optimized', the table editor will be optimized to
1258 - Automatic overwrite mode in front of whitespace in table fields.
1259 This makes the structure of the table stay in tact as long as the edited
1260 field does not exceed the column width.
1261 - Minimize the number of realigns. Normally, the table is aligned each time
1262 TAB or RET are pressed to move to another field. With optimization this
1263 happens only if changes to a field might have changed the column width.
1264 Optimization requires replacing the functions `self-insert-command',
1265 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1266 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1267 very good at guessing when a re-align will be necessary, but you can always
1268 force one with \\[org-ctrl-c-ctrl-c].
1270 If you would like to use the optimized version in Org-mode, but the
1271 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1273 This variable can be used to turn on and off the table editor during a session,
1274 but in order to toggle optimization, a restart is required.
1276 See also the variable `org-table-auto-blank-field'."
1279 (const :tag
"off" nil
)
1281 (const :tag
"on, optimized" optimized
)))
1283 (defcustom org-self-insert-cluster-for-undo
(version<= emacs-version
"24.1")
1284 "Non-nil means cluster self-insert commands for undo when possible.
1285 If this is set, then, like in the Emacs command loop, 20 consecutive
1286 characters will be undone together.
1287 This is configurable, because there is some impact on typing performance."
1291 (defcustom org-table-tab-recognizes-table.el t
1292 "Non-nil means TAB will automatically notice a table.el table.
1293 When it sees such a table, it moves point into it and - if necessary -
1294 calls `table-recognize-table'."
1295 :group
'org-table-editing
1298 (defgroup org-link nil
1299 "Options concerning links in Org-mode."
1303 (defvar org-link-abbrev-alist-local nil
1304 "Buffer-local version of `org-link-abbrev-alist', which see.
1305 The value of this is taken from the #+LINK lines.")
1306 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1308 (defcustom org-link-abbrev-alist nil
1309 "Alist of link abbreviations.
1310 The car of each element is a string, to be replaced at the start of a link.
1311 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1312 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1314 [[linkkey:tag][description]]
1316 The 'linkkey' must be a word word, starting with a letter, followed
1317 by letters, numbers, '-' or '_'.
1319 If REPLACE is a string, the tag will simply be appended to create the link.
1320 If the string contains \"%s\", the tag will be inserted there. If the string
1321 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1322 at that point (see the function `url-hexify-string'). If the string contains
1323 the specifier \"%(my-function)\", then the custom function `my-function' will
1324 be invoked: this function takes the tag as its only argument and must return
1327 REPLACE may also be a function that will be called with the tag as the
1328 only argument to create the link, which should be returned as a string.
1330 See the manual for examples."
1334 (string :tag
"Protocol")
1336 (string :tag
"Format")
1339 (defcustom org-descriptive-links t
1340 "Non-nil means Org will display descriptive links.
1341 E.g. [[http://orgmode.org][Org website]] will be displayed as
1342 \"Org Website\", hiding the link itself and just displaying its
1343 description. When set to `nil', Org will display the full links
1346 You can interactively set the value of this variable by calling
1347 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1351 (defcustom org-link-file-path-type
'adaptive
1352 "How the path name in file links should be stored.
1355 relative Relative to the current directory, i.e. the directory of the file
1356 into which the link is being inserted.
1357 absolute Absolute path, if possible with ~ for home directory.
1358 noabbrev Absolute path, no abbreviation of home directory.
1359 adaptive Use relative path for files in the current directory and sub-
1360 directories of it. For other files, use an absolute path."
1368 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1369 "Types of links that should be activated in Org-mode files.
1370 This is a list of symbols, each leading to the activation of a certain link
1371 type. In principle, it does not hurt to turn on most link types - there may
1372 be a small gain when turning off unused link types. The types are:
1374 bracket The recommended [[link][description]] or [[link]] links with hiding.
1375 angle Links in angular brackets that may contain whitespace like
1376 <bbdb:Carsten Dominik>.
1377 plain Plain links in normal text, no whitespace, like http://google.com.
1378 radio Text that is matched by a radio target, see manual for details.
1379 tag Tag settings in a headline (link to tag search).
1380 date Time stamps (link to calendar).
1381 footnote Footnote labels.
1383 Changing this variable requires a restart of Emacs to become effective."
1385 :type
'(set :greedy t
1386 (const :tag
"Double bracket links" bracket
)
1387 (const :tag
"Angular bracket links" angle
)
1388 (const :tag
"Plain text links" plain
)
1389 (const :tag
"Radio target matches" radio
)
1390 (const :tag
"Tags" tag
)
1391 (const :tag
"Timestamps" date
)
1392 (const :tag
"Footnotes" footnote
)))
1394 (defcustom org-make-link-description-function nil
1395 "Function to use to generate link descriptions from links.
1396 If nil the link location will be used. This function must take
1397 two parameters; the first is the link and the second the
1398 description `org-insert-link' has generated, and should return the
1399 description to use."
1403 (defgroup org-link-store nil
1404 "Options concerning storing links in Org-mode."
1405 :tag
"Org Store Link"
1408 (defcustom org-email-link-description-format
"Email %c: %.30s"
1409 "Format of the description part of a link to an email or usenet message.
1410 The following %-escapes will be replaced by corresponding information:
1412 %F full \"From\" field
1413 %f name, taken from \"From\" field, address if no name
1414 %T full \"To\" field
1415 %t first name in \"To\" field, address if no name
1416 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1417 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1422 You may use normal field width specification between the % and the letter.
1423 This is for example useful to limit the length of the subject.
1425 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1426 :group
'org-link-store
1429 (defcustom org-from-is-user-regexp
1431 (when (and user-mail-address
(not (string= user-mail-address
"")))
1432 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1433 (when (and user-full-name
(not (string= user-full-name
"")))
1434 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1435 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1436 "Regexp matched against the \"From:\" header of an email or usenet message.
1437 It should match if the message is from the user him/herself."
1438 :group
'org-link-store
1441 (defcustom org-link-to-org-use-id
'create-if-interactive-and-no-custom-id
1442 "Non-nil means storing a link to an Org file will use entry IDs.
1444 Note that before this variable is even considered, org-id must be loaded,
1445 so please customize `org-modules' and turn it on.
1447 The variable can have the following values:
1449 t Create an ID if needed to make a link to the current entry.
1451 create-if-interactive
1452 If `org-store-link' is called directly (interactively, as a user
1453 command), do create an ID to support the link. But when doing the
1454 job for remember, only use the ID if it already exists. The
1455 purpose of this setting is to avoid proliferation of unwanted
1456 IDs, just because you happen to be in an Org file when you
1457 call `org-remember' that automatically and preemptively
1458 creates a link. If you do want to get an ID link in a remember
1459 template to an entry not having an ID, create it first by
1460 explicitly creating a link to it, using `C-c C-l' first.
1462 create-if-interactive-and-no-custom-id
1463 Like create-if-interactive, but do not create an ID if there is
1464 a CUSTOM_ID property defined in the entry. This is the default.
1467 Use existing ID, do not create one.
1469 nil Never use an ID to make a link, instead link using a text search for
1471 :group
'org-link-store
1473 (const :tag
"Create ID to make link" t
)
1474 (const :tag
"Create if storing link interactively"
1475 create-if-interactive
)
1476 (const :tag
"Create if storing link interactively and no CUSTOM_ID is present"
1477 create-if-interactive-and-no-custom-id
)
1478 (const :tag
"Only use existing" use-existing
)
1479 (const :tag
"Do not use ID to create link" nil
)))
1481 (defcustom org-context-in-file-links t
1482 "Non-nil means file links from `org-store-link' contain context.
1483 A search string will be added to the file name with :: as separator and
1484 used to find the context when the link is activated by the command
1485 `org-open-at-point'. When this option is t, the entire active region
1486 will be placed in the search string of the file link. If set to a
1487 positive integer, only the first n lines of context will be stored.
1489 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1490 negates this setting for the duration of the command."
1491 :group
'org-link-store
1492 :type
'(choice boolean integer
))
1494 (defcustom org-keep-stored-link-after-insertion nil
1495 "Non-nil means keep link in list for entire session.
1497 The command `org-store-link' adds a link pointing to the current
1498 location to an internal list. These links accumulate during a session.
1499 The command `org-insert-link' can be used to insert links into any
1500 Org-mode file (offering completion for all stored links). When this
1501 option is nil, every link which has been inserted once using \\[org-insert-link]
1502 will be removed from the list, to make completing the unused links
1504 :group
'org-link-store
1507 (defgroup org-link-follow nil
1508 "Options concerning following links in Org-mode."
1509 :tag
"Org Follow Link"
1512 (defcustom org-link-translation-function nil
1513 "Function to translate links with different syntax to Org syntax.
1514 This can be used to translate links created for example by the Planner
1515 or emacs-wiki packages to Org syntax.
1516 The function must accept two parameters, a TYPE containing the link
1517 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1518 which is everything after the link protocol. It should return a cons
1519 with possibly modified values of type and path.
1520 Org contains a function for this, so if you set this variable to
1521 `org-translate-link-from-planner', you should be able follow many
1522 links created by planner."
1523 :group
'org-link-follow
1526 (defcustom org-follow-link-hook nil
1527 "Hook that is run after a link has been followed."
1528 :group
'org-link-follow
1531 (defcustom org-tab-follows-link nil
1532 "Non-nil means on links TAB will follow the link.
1533 Needs to be set before org.el is loaded.
1534 This really should not be used, it does not make sense, and the
1535 implementation is bad."
1536 :group
'org-link-follow
1539 (defcustom org-return-follows-link nil
1540 "Non-nil means on links RET will follow the link."
1541 :group
'org-link-follow
1544 (defcustom org-mouse-1-follows-link
1545 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1546 "Non-nil means mouse-1 on a link will follow the link.
1547 A longer mouse click will still set point. Does not work on XEmacs.
1548 Needs to be set before org.el is loaded."
1549 :group
'org-link-follow
1552 (defcustom org-mark-ring-length
4
1553 "Number of different positions to be recorded in the ring.
1554 Changing this requires a restart of Emacs to work correctly."
1555 :group
'org-link-follow
1558 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1559 "Non-nil means internal links in Org files must exactly match a headline.
1560 When nil, the link search tries to match a phrase with all words
1561 in the search text."
1562 :group
'org-link-follow
1565 (const :tag
"Use fuzzy text search" nil
)
1566 (const :tag
"Match only exact headline" t
)
1567 (const :tag
"Match exact headline or query to create it"
1570 (defcustom org-link-frame-setup
1571 '((vm . vm-visit-folder-other-frame
)
1572 (vm-imap . vm-visit-imap-folder-other-frame
)
1573 (gnus . org-gnus-no-new-news
)
1574 (file . find-file-other-window
)
1575 (wl . wl-other-frame
))
1576 "Setup the frame configuration for following links.
1577 When following a link with Emacs, it may often be useful to display
1578 this link in another window or frame. This variable can be used to
1579 set this up for the different types of links.
1582 `vm-visit-folder-other-window'
1583 `vm-visit-folder-other-frame'
1584 For Gnus, use any of
1587 `org-gnus-no-new-news'
1588 For FILE, use any of
1590 `find-file-other-window'
1591 `find-file-other-frame'
1592 For Wanderlust use any of
1595 For the calendar, use the variable `calendar-setup'.
1596 For BBDB, it is currently only possible to display the matches in
1598 :group
'org-link-follow
1602 (const vm-visit-folder
)
1603 (const vm-visit-folder-other-window
)
1604 (const vm-visit-folder-other-frame
)))
1608 (const gnus-other-frame
)
1609 (const org-gnus-no-new-news
)))
1613 (const find-file-other-window
)
1614 (const find-file-other-frame
)))
1618 (const wl-other-frame
)))))
1620 (defcustom org-display-internal-link-with-indirect-buffer nil
1621 "Non-nil means use indirect buffer to display infile links.
1622 Activating internal links (from one location in a file to another location
1623 in the same file) normally just jumps to the location. When the link is
1624 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1626 another window. When this option is set, the other window actually displays
1627 an indirect buffer clone of the current buffer, to avoid any visibility
1628 changes to the current buffer."
1629 :group
'org-link-follow
1632 (defcustom org-open-non-existing-files nil
1633 "Non-nil means `org-open-file' will open non-existing files.
1634 When nil, an error will be generated.
1635 This variable applies only to external applications because they
1636 might choke on non-existing files. If the link is to a file that
1637 will be opened in Emacs, the variable is ignored."
1638 :group
'org-link-follow
1641 (defcustom org-open-directory-means-index-dot-org nil
1642 "Non-nil means a link to a directory really means to index.org.
1643 When nil, following a directory link will run dired or open a finder/explorer
1644 window on that directory."
1645 :group
'org-link-follow
1648 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1649 "Function and arguments to call for following mailto links.
1650 This is a list with the first element being a Lisp function, and the
1651 remaining elements being arguments to the function. In string arguments,
1652 %a will be replaced by the address, and %s will be replaced by the subject
1653 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1654 :group
'org-link-follow
1656 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1657 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1658 (const :tag
"message-mail" (message-mail "%a" "%s"))
1659 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1661 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1662 "Non-nil means ask for confirmation before executing shell links.
1663 Shell links can be dangerous: just think about a link
1665 [[shell:rm -rf ~/*][Google Search]]
1667 This link would show up in your Org-mode document as \"Google Search\",
1668 but really it would remove your entire home directory.
1669 Therefore we advise against setting this variable to nil.
1670 Just change it to `y-or-n-p' if you want to confirm with a
1671 single keystroke rather than having to type \"yes\"."
1672 :group
'org-link-follow
1674 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1675 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1676 (const :tag
"no confirmation (dangerous)" nil
)))
1677 (put 'org-confirm-shell-link-function
1678 'safe-local-variable
1679 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1681 (defcustom org-confirm-shell-link-not-regexp
""
1682 "A regexp to skip confirmation for shell links."
1683 :group
'org-link-follow
1687 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1688 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1689 Elisp links can be dangerous: just think about a link
1691 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1693 This link would show up in your Org-mode document as \"Google Search\",
1694 but really it would remove your entire home directory.
1695 Therefore we advise against setting this variable to nil.
1696 Just change it to `y-or-n-p' if you want to confirm with a
1697 single keystroke rather than having to type \"yes\"."
1698 :group
'org-link-follow
1700 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1701 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1702 (const :tag
"no confirmation (dangerous)" nil
)))
1703 (put 'org-confirm-shell-link-function
1704 'safe-local-variable
1705 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1707 (defcustom org-confirm-elisp-link-not-regexp
""
1708 "A regexp to skip confirmation for Elisp links."
1709 :group
'org-link-follow
1713 (defconst org-file-apps-defaults-gnu
1717 "Default file applications on a UNIX or GNU/Linux system.
1718 See `org-file-apps'.")
1720 (defconst org-file-apps-defaults-macosx
1723 (system .
"open %s")
1725 ("eps.gz" .
"gv %s")
1727 ("fig" .
"xfig %s"))
1728 "Default file applications on a MacOS X system.
1729 The system \"open\" is known as a default, but we use X11 applications
1730 for some files for which the OS does not have a good default.
1731 See `org-file-apps'.")
1733 (defconst org-file-apps-defaults-windowsnt
1737 (list (if (featurep 'xemacs
)
1738 'mswindows-shell-execute
1742 (list (if (featurep 'xemacs
)
1743 'mswindows-shell-execute
1746 "Default file applications on a Windows NT system.
1747 The system \"open\" is used for most files.
1748 See `org-file-apps'.")
1750 (defcustom org-file-apps
1753 ("\\.mm\\'" . default
)
1754 ("\\.x?html?\\'" . default
)
1755 ("\\.pdf\\'" . default
)
1757 "External applications for opening `file:path' items in a document.
1758 Org-mode uses system defaults for different file types, but
1759 you can use this variable to set the application for a given file
1760 extension. The entries in this list are cons cells where the car identifies
1761 files and the cdr the corresponding command. Possible values for the
1763 \"string\" A string as a file identifier can be interpreted in different
1764 ways, depending on its contents:
1766 - Alphanumeric characters only:
1767 Match links with this file extension.
1768 Example: (\"pdf\" . \"evince %s\")
1769 to open PDFs with evince.
1771 - Regular expression: Match links where the
1772 filename matches the regexp. If you want to
1773 use groups here, use shy groups.
1775 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1776 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1777 to open *.html and *.xhtml with firefox.
1779 - Regular expression which contains (non-shy) groups:
1780 Match links where the whole link, including \"::\", and
1781 anything after that, matches the regexp.
1782 In a custom command string, %1, %2, etc. are replaced with
1783 the parts of the link that were matched by the groups.
1784 For backwards compatibility, if a command string is given
1785 that does not use any of the group matches, this case is
1786 handled identically to the second one (i.e. match against
1788 In a custom lisp form, you can access the group matches with
1789 (match-string n link).
1791 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1792 to open [[file:document.pdf::5]] with evince at page 5.
1794 `directory' Matches a directory
1795 `remote' Matches a remote file, accessible through tramp or efs.
1796 Remote files most likely should be visited through Emacs
1797 because external applications cannot handle such paths.
1798 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1799 so all files Emacs knows how to handle. Using this with
1800 command `emacs' will open most files in Emacs. Beware that this
1801 will also open html files inside Emacs, unless you add
1802 (\"html\" . default) to the list as well.
1803 t Default for files not matched by any of the other options.
1804 `system' The system command to open files, like `open' on Windows
1805 and Mac OS X, and mailcap under GNU/Linux. This is the command
1806 that will be selected if you call `C-c C-o' with a double
1807 \\[universal-argument] \\[universal-argument] prefix.
1809 Possible values for the command are:
1810 `emacs' The file will be visited by the current Emacs process.
1811 `default' Use the default application for this file type, which is the
1812 association for t in the list, most likely in the system-specific
1814 This can be used to overrule an unwanted setting in the
1815 system-specific variable.
1816 `system' Use the system command for opening files, like \"open\".
1817 This command is specified by the entry whose car is `system'.
1818 Most likely, the system-specific version of this variable
1819 does define this command, but you can overrule/replace it
1821 string A command to be executed by a shell; %s will be replaced
1822 by the path to the file.
1823 sexp A Lisp form which will be evaluated. The file path will
1824 be available in the Lisp variable `file'.
1825 For more examples, see the system specific constants
1826 `org-file-apps-defaults-macosx'
1827 `org-file-apps-defaults-windowsnt'
1828 `org-file-apps-defaults-gnu'."
1829 :group
'org-link-follow
1831 (cons (choice :value
""
1832 (string :tag
"Extension")
1833 (const :tag
"System command to open files" system
)
1834 (const :tag
"Default for unrecognized files" t
)
1835 (const :tag
"Remote file" remote
)
1836 (const :tag
"Links to a directory" directory
)
1837 (const :tag
"Any files that have Emacs modes"
1840 (const :tag
"Visit with Emacs" emacs
)
1841 (const :tag
"Use default" default
)
1842 (const :tag
"Use the system command" system
)
1843 (string :tag
"Command")
1844 (sexp :tag
"Lisp form")))))
1846 (defcustom org-doi-server-url
"http://dx.doi.org/"
1847 "The URL of the DOI server."
1850 :group
'org-link-follow
)
1852 (defgroup org-refile nil
1853 "Options concerning refiling entries in Org-mode."
1857 (defcustom org-directory
"~/org"
1858 "Directory with org files.
1859 This is just a default location to look for Org files. There is no need
1860 at all to put your files into this directory. It is only used in the
1861 following situations:
1863 1. When a remember template specifies a target file that is not an
1864 absolute path. The path will then be interpreted relative to
1866 2. When a remember note is filed away in an interactive way (when exiting the
1867 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1868 with `org-directory' as the default path."
1870 :group
'org-remember
1873 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1874 "Default target for storing notes.
1875 Used as a fall back file for org-remember.el and org-capture.el, for
1876 templates that do not specify a target file."
1878 :group
'org-remember
1880 (const :tag
"Default from remember-data-file" nil
)
1883 (defcustom org-goto-interface
'outline
1884 "The default interface to be used for `org-goto'.
1886 outline The interface shows an outline of the relevant file
1887 and the correct heading is found by moving through
1888 the outline or by searching with incremental search.
1889 outline-path-completion Headlines in the current buffer are offered via
1890 completion. This is the interface also used by
1891 the refile command."
1894 (const :tag
"Outline" outline
)
1895 (const :tag
"Outline-path-completion" outline-path-completion
)))
1897 (defcustom org-goto-max-level
5
1898 "Maximum target level when running `org-goto' with refile interface."
1902 (defcustom org-reverse-note-order nil
1903 "Non-nil means store new notes at the beginning of a file or entry.
1904 When nil, new notes will be filed to the end of a file or entry.
1905 This can also be a list with cons cells of regular expressions that
1906 are matched against file names, and values."
1907 :group
'org-remember
1910 (const :tag
"Reverse always" t
)
1911 (const :tag
"Reverse never" nil
)
1912 (repeat :tag
"By file name regexp"
1913 (cons regexp boolean
))))
1915 (defcustom org-log-refile nil
1916 "Information to record when a task is refiled.
1918 Possible values are:
1920 nil Don't add anything
1921 time Add a time stamp to the task
1922 note Prompt for a note and add it with template `org-log-note-headings'
1924 This option can also be set with on a per-file-basis with
1926 #+STARTUP: nologrefile
1927 #+STARTUP: logrefile
1928 #+STARTUP: lognoterefile
1930 You can have local logging settings for a subtree by setting the LOGGING
1931 property to one or more of these keywords.
1933 When bulk-refiling from the agenda, the value `note' is forbidden and
1934 will temporarily be changed to `time'."
1936 :group
'org-progress
1939 (const :tag
"No logging" nil
)
1940 (const :tag
"Record timestamp" time
)
1941 (const :tag
"Record timestamp with note." note
)))
1943 (defcustom org-refile-targets nil
1944 "Targets for refiling entries with \\[org-refile].
1945 This is a list of cons cells. Each cell contains:
1946 - a specification of the files to be considered, either a list of files,
1947 or a symbol whose function or variable value will be used to retrieve
1948 a file name or a list of file names. If you use `org-agenda-files' for
1949 that, all agenda files will be scanned for targets. Nil means consider
1950 headings in the current buffer.
1951 - A specification of how to find candidate refile targets. This may be
1953 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1954 This tag has to be present in all target headlines, inheritance will
1956 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1958 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1959 headlines that are refiling targets.
1960 - a cons cell (:level . N). Any headline of level N is considered a target.
1961 Note that, when `org-odd-levels-only' is set, level corresponds to
1962 order in hierarchy, not to the number of stars.
1963 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1964 Note that, when `org-odd-levels-only' is set, level corresponds to
1965 order in hierarchy, not to the number of stars.
1967 Each element of this list generates a set of possible targets.
1968 The union of these sets is presented (with completion) to
1969 the user by `org-refile'.
1971 You can set the variable `org-refile-target-verify-function' to a function
1972 to verify each headline found by the simple criteria above.
1974 When this variable is nil, all top-level headlines in the current buffer
1975 are used, equivalent to the value `((nil . (:level . 1))'."
1979 (choice :value org-agenda-files
1980 (const :tag
"All agenda files" org-agenda-files
)
1981 (const :tag
"Current buffer" nil
)
1982 (function) (variable) (file))
1983 (choice :tag
"Identify target headline by"
1984 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1985 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1986 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1987 (cons :tag
"Level number" (const :value
:level
) (integer))
1988 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1990 (defcustom org-refile-target-verify-function nil
1991 "Function to verify if the headline at point should be a refile target.
1992 The function will be called without arguments, with point at the
1993 beginning of the headline. It should return t and leave point
1994 where it is if the headline is a valid target for refiling.
1996 If the target should not be selected, the function must return nil.
1997 In addition to this, it may move point to a place from where the search
1998 should be continued. For example, the function may decide that the entire
1999 subtree of the current entry should be excluded and move point to the end
2004 (defcustom org-refile-use-cache nil
2005 "Non-nil means cache refile targets to speed up the process.
2006 The cache for a particular file will be updated automatically when
2007 the buffer has been killed, or when any of the marker used for flagging
2008 refile targets no longer points at a live buffer.
2009 If you have added new entries to a buffer that might themselves be targets,
2010 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
2011 find that easier, `C-u C-u C-u C-c C-w'."
2016 (defcustom org-refile-use-outline-path nil
2017 "Non-nil means provide refile targets as paths.
2018 So a level 3 headline will be available as level1/level2/level3.
2020 When the value is `file', also include the file name (without directory)
2021 into the path. In this case, you can also stop the completion after
2022 the file name, to get entries inserted as top level in the file.
2024 When `full-file-path', include the full file path."
2027 (const :tag
"Not" nil
)
2028 (const :tag
"Yes" t
)
2029 (const :tag
"Start with file name" file
)
2030 (const :tag
"Start with full file path" full-file-path
)))
2032 (defcustom org-outline-path-complete-in-steps t
2033 "Non-nil means complete the outline path in hierarchical steps.
2034 When Org-mode uses the refile interface to select an outline path
2035 \(see variable `org-refile-use-outline-path'), the completion of
2036 the path can be done is a single go, or if can be done in steps down
2037 the headline hierarchy. Going in steps is probably the best if you
2038 do not use a special completion package like `ido' or `icicles'.
2039 However, when using these packages, going in one step can be very
2040 fast, while still showing the whole path to the entry."
2044 (defcustom org-refile-allow-creating-parent-nodes nil
2045 "Non-nil means allow to create new nodes as refile targets.
2046 New nodes are then created by adding \"/new node name\" to the completion
2047 of an existing node. When the value of this variable is `confirm',
2048 new node creation must be confirmed by the user (recommended)
2049 When nil, the completion must match an existing entry.
2051 Note that, if the new heading is not seen by the criteria
2052 listed in `org-refile-targets', multiple instances of the same
2053 heading would be created by trying again to file under the new
2057 (const :tag
"Never" nil
)
2058 (const :tag
"Always" t
)
2059 (const :tag
"Prompt for confirmation" confirm
)))
2061 (defcustom org-refile-active-region-within-subtree nil
2062 "Non-nil means also refile active region within a subtree.
2064 By default `org-refile' doesn't allow refiling regions if they
2065 don't contain a set of subtrees, but it might be convenient to
2066 do so sometimes: in that case, the first line of the region is
2067 converted to a headline before refiling."
2072 (defgroup org-todo nil
2073 "Options concerning TODO items in Org-mode."
2077 (defgroup org-progress nil
2078 "Options concerning Progress logging in Org-mode."
2082 (defvar org-todo-interpretation-widgets
2083 '((:tag
"Sequence (cycling hits every state)" sequence
)
2084 (:tag
"Type (cycling directly to DONE)" type
))
2085 "The available interpretation symbols for customizing `org-todo-keywords'.
2086 Interested libraries should add to this list.")
2088 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2089 "List of TODO entry keyword sequences and their interpretation.
2090 \\<org-mode-map>This is a list of sequences.
2092 Each sequence starts with a symbol, either `sequence' or `type',
2093 indicating if the keywords should be interpreted as a sequence of
2094 action steps, or as different types of TODO items. The first
2095 keywords are states requiring action - these states will select a headline
2096 for inclusion into the global TODO list Org-mode produces. If one of
2097 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2098 signify that no further action is necessary. If \"|\" is not found,
2099 the last keyword is treated as the only DONE state of the sequence.
2101 The command \\[org-todo] cycles an entry through these states, and one
2102 additional state where no keyword is present. For details about this
2103 cycling, see the manual.
2105 TODO keywords and interpretation can also be set on a per-file basis with
2106 the special #+SEQ_TODO and #+TYP_TODO lines.
2108 Each keyword can optionally specify a character for fast state selection
2109 \(in combination with the variable `org-use-fast-todo-selection')
2110 and specifiers for state change logging, using the same syntax that
2111 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2112 the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2113 indicates to record a time stamp each time this state is selected.
2115 Each keyword may also specify if a timestamp or a note should be
2116 recorded when entering or leaving the state, by adding additional
2117 characters in the parenthesis after the keyword. This looks like this:
2118 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2119 record only the time of the state change. With X and Y being either
2120 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2121 Y when leaving the state if and only if the *target* state does not
2122 define X. You may omit any of the fast-selection key or X or /Y,
2123 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2125 For backward compatibility, this variable may also be just a list
2126 of keywords. In this case the interpretation (sequence or type) will be
2127 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2129 :group
'org-keywords
2131 (repeat :tag
"Old syntax, just keywords"
2132 (string :tag
"Keyword"))
2133 (repeat :tag
"New syntax"
2136 :tag
"Interpretation"
2137 ;;Quick and dirty way to see
2138 ;;`org-todo-interpretations'. This takes the
2139 ;;place of item arguments
2147 org-todo-interpretation-widgets
))
2150 (string :tag
"Keyword"))))))
2152 (defvar org-todo-keywords-1 nil
2153 "All TODO and DONE keywords active in a buffer.")
2154 (make-variable-buffer-local 'org-todo-keywords-1
)
2155 (defvar org-todo-keywords-for-agenda nil
)
2156 (defvar org-done-keywords-for-agenda nil
)
2157 (defvar org-drawers-for-agenda nil
)
2158 (defvar org-todo-keyword-alist-for-agenda nil
)
2159 (defvar org-tag-alist-for-agenda nil
)
2160 (defvar org-agenda-contributing-files nil
)
2161 (defvar org-not-done-keywords nil
)
2162 (make-variable-buffer-local 'org-not-done-keywords
)
2163 (defvar org-done-keywords nil
)
2164 (make-variable-buffer-local 'org-done-keywords
)
2165 (defvar org-todo-heads nil
)
2166 (make-variable-buffer-local 'org-todo-heads
)
2167 (defvar org-todo-sets nil
)
2168 (make-variable-buffer-local 'org-todo-sets
)
2169 (defvar org-todo-log-states nil
)
2170 (make-variable-buffer-local 'org-todo-log-states
)
2171 (defvar org-todo-kwd-alist nil
)
2172 (make-variable-buffer-local 'org-todo-kwd-alist
)
2173 (defvar org-todo-key-alist nil
)
2174 (make-variable-buffer-local 'org-todo-key-alist
)
2175 (defvar org-todo-key-trigger nil
)
2176 (make-variable-buffer-local 'org-todo-key-trigger
)
2178 (defcustom org-todo-interpretation
'sequence
2179 "Controls how TODO keywords are interpreted.
2180 This variable is in principle obsolete and is only used for
2181 backward compatibility, if the interpretation of todo keywords is
2182 not given already in `org-todo-keywords'. See that variable for
2185 :group
'org-keywords
2186 :type
'(choice (const sequence
)
2189 (defcustom org-use-fast-todo-selection t
2190 "Non-nil means use the fast todo selection scheme with C-c C-t.
2191 This variable describes if and under what circumstances the cycling
2192 mechanism for TODO keywords will be replaced by a single-key, direct
2195 When nil, fast selection is never used.
2197 When the symbol `prefix', it will be used when `org-todo' is called
2198 with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
2199 `C-u t' in an agenda buffer.
2201 When t, fast selection is used by default. In this case, the prefix
2202 argument forces cycling instead.
2204 In all cases, the special interface is only used if access keys have
2205 actually been assigned by the user, i.e. if keywords in the configuration
2206 are followed by a letter in parenthesis, like TODO(t)."
2209 (const :tag
"Never" nil
)
2210 (const :tag
"By default" t
)
2211 (const :tag
"Only with C-u C-c C-t" prefix
)))
2213 (defcustom org-provide-todo-statistics t
2214 "Non-nil means update todo statistics after insert and toggle.
2215 ALL-HEADLINES means update todo statistics by including headlines
2216 with no TODO keyword as well, counting them as not done.
2217 A list of TODO keywords means the same, but skip keywords that are
2220 When this is set, todo statistics is updated in the parent of the
2221 current entry each time a todo state is changed."
2224 (const :tag
"Yes, only for TODO entries" t
)
2225 (const :tag
"Yes, including all entries" 'all-headlines
)
2226 (repeat :tag
"Yes, for TODOs in this list"
2227 (string :tag
"TODO keyword"))
2228 (other :tag
"No TODO statistics" nil
)))
2230 (defcustom org-hierarchical-todo-statistics t
2231 "Non-nil means TODO statistics covers just direct children.
2232 When nil, all entries in the subtree are considered.
2233 This has only an effect if `org-provide-todo-statistics' is set.
2234 To set this to nil for only a single subtree, use a COOKIE_DATA
2235 property and include the word \"recursive\" into the value."
2239 (defcustom org-after-todo-state-change-hook nil
2240 "Hook which is run after the state of a TODO item was changed.
2241 The new state (a string with a TODO keyword, or nil) is available in the
2242 Lisp variable `org-state'."
2246 (defvar org-blocker-hook nil
2247 "Hook for functions that are allowed to block a state change.
2249 Each function gets as its single argument a property list, see
2250 `org-trigger-hook' for more information about this list.
2252 If any of the functions in this hook returns nil, the state change
2255 (defvar org-trigger-hook nil
2256 "Hook for functions that are triggered by a state change.
2258 Each function gets as its single argument a property list with at least
2259 the following elements:
2261 (:type type-of-change :position pos-at-entry-start
2262 :from old-state :to new-state)
2264 Depending on the type, more properties may be present.
2266 This mechanism is currently implemented for:
2270 :type todo-state-change
2271 :from previous state (keyword as a string), or nil, or a symbol
2272 'todo' or 'done', to indicate the general type of state.
2273 :to new state, like in :from")
2275 (defcustom org-enforce-todo-dependencies nil
2276 "Non-nil means undone TODO entries will block switching the parent to DONE.
2277 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2278 be blocked if any prior sibling is not yet done.
2279 Finally, if the parent is blocked because of ordered siblings of its own,
2280 the child will also be blocked."
2281 :set
(lambda (var val
)
2284 (add-hook 'org-blocker-hook
2285 'org-block-todo-from-children-or-siblings-or-parent
)
2286 (remove-hook 'org-blocker-hook
2287 'org-block-todo-from-children-or-siblings-or-parent
)))
2291 (defcustom org-enforce-todo-checkbox-dependencies nil
2292 "Non-nil means unchecked boxes will block switching the parent to DONE.
2293 When this is nil, checkboxes have no influence on switching TODO states.
2294 When non-nil, you first need to check off all check boxes before the TODO
2295 entry can be switched to DONE.
2296 This variable needs to be set before org.el is loaded, and you need to
2297 restart Emacs after a change to make the change effective. The only way
2298 to change is while Emacs is running is through the customize interface."
2299 :set
(lambda (var val
)
2302 (add-hook 'org-blocker-hook
2303 'org-block-todo-from-checkboxes
)
2304 (remove-hook 'org-blocker-hook
2305 'org-block-todo-from-checkboxes
)))
2309 (defcustom org-treat-insert-todo-heading-as-state-change nil
2310 "Non-nil means inserting a TODO heading is treated as state change.
2311 So when the command \\[org-insert-todo-heading] is used, state change
2312 logging will apply if appropriate. When nil, the new TODO item will
2313 be inserted directly, and no logging will take place."
2317 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2318 "Non-nil means switching TODO states with S-cursor counts as state change.
2319 This is the default behavior. However, setting this to nil allows a
2320 convenient way to select a TODO state and bypass any logging associated
2325 (defcustom org-todo-state-tags-triggers nil
2326 "Tag changes that should be triggered by TODO state changes.
2327 This is a list. Each entry is
2329 (state-change (tag . flag) .......)
2331 State-change can be a string with a state, and empty string to indicate the
2332 state that has no TODO keyword, or it can be one of the symbols `todo'
2333 or `done', meaning any not-done or done state, respectively."
2337 (cons (choice :tag
"When changing to"
2338 (const :tag
"Not-done state" todo
)
2339 (const :tag
"Done state" done
)
2340 (string :tag
"State"))
2342 (cons :tag
"Tag action"
2344 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2346 (defcustom org-log-done nil
2347 "Information to record when a task moves to the DONE state.
2349 Possible values are:
2351 nil Don't add anything, just change the keyword
2352 time Add a time stamp to the task
2353 note Prompt for a note and add it with template `org-log-note-headings'
2355 This option can also be set with on a per-file-basis with
2357 #+STARTUP: nologdone
2359 #+STARTUP: lognotedone
2361 You can have local logging settings for a subtree by setting the LOGGING
2362 property to one or more of these keywords."
2364 :group
'org-progress
2366 (const :tag
"No logging" nil
)
2367 (const :tag
"Record CLOSED timestamp" time
)
2368 (const :tag
"Record CLOSED timestamp with note." note
)))
2370 ;; Normalize old uses of org-log-done.
2372 ((eq org-log-done t
) (setq org-log-done
'time
))
2373 ((and (listp org-log-done
) (memq 'done org-log-done
))
2374 (setq org-log-done
'note
)))
2376 (defcustom org-log-reschedule nil
2377 "Information to record when the scheduling date of a tasks is modified.
2379 Possible values are:
2381 nil Don't add anything, just change the date
2382 time Add a time stamp to the task
2383 note Prompt for a note and add it with template `org-log-note-headings'
2385 This option can also be set with on a per-file-basis with
2387 #+STARTUP: nologreschedule
2388 #+STARTUP: logreschedule
2389 #+STARTUP: lognotereschedule"
2391 :group
'org-progress
2393 (const :tag
"No logging" nil
)
2394 (const :tag
"Record timestamp" time
)
2395 (const :tag
"Record timestamp with note." note
)))
2397 (defcustom org-log-redeadline nil
2398 "Information to record when the deadline date of a tasks is modified.
2400 Possible values are:
2402 nil Don't add anything, just change the date
2403 time Add a time stamp to the task
2404 note Prompt for a note and add it with template `org-log-note-headings'
2406 This option can also be set with on a per-file-basis with
2408 #+STARTUP: nologredeadline
2409 #+STARTUP: logredeadline
2410 #+STARTUP: lognoteredeadline
2412 You can have local logging settings for a subtree by setting the LOGGING
2413 property to one or more of these keywords."
2415 :group
'org-progress
2417 (const :tag
"No logging" nil
)
2418 (const :tag
"Record timestamp" time
)
2419 (const :tag
"Record timestamp with note." note
)))
2421 (defcustom org-log-note-clock-out nil
2422 "Non-nil means record a note when clocking out of an item.
2423 This can also be configured on a per-file basis by adding one of
2424 the following lines anywhere in the buffer:
2426 #+STARTUP: lognoteclock-out
2427 #+STARTUP: nolognoteclock-out"
2429 :group
'org-progress
2432 (defcustom org-log-done-with-time t
2433 "Non-nil means the CLOSED time stamp will contain date and time.
2434 When nil, only the date will be recorded."
2435 :group
'org-progress
2438 (defcustom org-log-note-headings
2439 '((done .
"CLOSING NOTE %t")
2440 (state .
"State %-12s from %-12S %t")
2441 (note .
"Note taken on %t")
2442 (reschedule .
"Rescheduled from %S on %t")
2443 (delschedule .
"Not scheduled, was %S on %t")
2444 (redeadline .
"New deadline from %S on %t")
2445 (deldeadline .
"Removed deadline, was %S on %t")
2446 (refile .
"Refiled on %t")
2448 "Headings for notes added to entries.
2449 The value is an alist, with the car being a symbol indicating the note
2450 context, and the cdr is the heading to be used. The heading may also be the
2452 %t in the heading will be replaced by a time stamp.
2453 %T will be an active time stamp instead the default inactive one
2454 %d will be replaced by a short-format time stamp.
2455 %D will be replaced by an active short-format time stamp.
2456 %s will be replaced by the new TODO state, in double quotes.
2457 %S will be replaced by the old TODO state, in double quotes.
2458 %u will be replaced by the user name.
2459 %U will be replaced by the full user name.
2461 In fact, it is not a good idea to change the `state' entry, because
2462 agenda log mode depends on the format of these entries."
2464 :group
'org-progress
2465 :type
'(list :greedy t
2466 (cons (const :tag
"Heading when closing an item" done
) string
)
2468 "Heading when changing todo state (todo sequence only)"
2470 (cons (const :tag
"Heading when just taking a note" note
) string
)
2471 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2472 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2473 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2474 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2475 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2476 (cons (const :tag
"Heading when refiling" refile
) string
)))
2478 (unless (assq 'note org-log-note-headings
)
2479 (push '(note .
"%t") org-log-note-headings
))
2481 (defcustom org-log-into-drawer nil
2482 "Non-nil means insert state change notes and time stamps into a drawer.
2483 When nil, state changes notes will be inserted after the headline and
2484 any scheduling and clock lines, but not inside a drawer.
2486 The value of this variable should be the name of the drawer to use.
2487 LOGBOOK is proposed as the default drawer for this purpose, you can
2488 also set this to a string to define the drawer of your choice.
2490 A value of t is also allowed, representing \"LOGBOOK\".
2492 If this variable is set, `org-log-state-notes-insert-after-drawers'
2495 You can set the property LOG_INTO_DRAWER to overrule this setting for
2498 :group
'org-progress
2500 (const :tag
"Not into a drawer" nil
)
2501 (const :tag
"LOGBOOK" t
)
2502 (string :tag
"Other")))
2504 (if (fboundp 'defvaralias
)
2505 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2507 (defun org-log-into-drawer ()
2508 "Return the value of `org-log-into-drawer', but let properties overrule.
2509 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2510 used instead of the default value."
2511 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit
)))
2513 ((or (not p
) (equal p
"nil")) org-log-into-drawer
)
2514 ((equal p
"t") "LOGBOOK")
2517 (defcustom org-log-state-notes-insert-after-drawers nil
2518 "Non-nil means insert state change notes after any drawers in entry.
2519 Only the drawers that *immediately* follow the headline and the
2520 deadline/scheduled line are skipped.
2521 When nil, insert notes right after the heading and perhaps the line
2522 with deadline/scheduling if present.
2524 This variable will have no effect if `org-log-into-drawer' is
2527 :group
'org-progress
2530 (defcustom org-log-states-order-reversed t
2531 "Non-nil means the latest state note will be directly after heading.
2532 When nil, the state change notes will be ordered according to time."
2534 :group
'org-progress
2537 (defcustom org-todo-repeat-to-state nil
2538 "The TODO state to which a repeater should return the repeating task.
2539 By default this is the first task in a TODO sequence, or the previous state
2540 in a TODO_TYP set. But you can specify another task here.
2541 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2544 :type
'(choice (const :tag
"Head of sequence" nil
)
2545 (string :tag
"Specific state")))
2547 (defcustom org-log-repeat
'time
2548 "Non-nil means record moving through the DONE state when triggering repeat.
2549 An auto-repeating task is immediately switched back to TODO when
2550 marked DONE. If you are not logging state changes (by adding \"@\"
2551 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2552 record a closing note, there will be no record of the task moving
2553 through DONE. This variable forces taking a note anyway.
2555 nil Don't force a record
2556 time Record a time stamp
2559 This option can also be set with on a per-file-basis with
2561 #+STARTUP: logrepeat
2562 #+STARTUP: lognoterepeat
2563 #+STARTUP: nologrepeat
2565 You can have local logging settings for a subtree by setting the LOGGING
2566 property to one or more of these keywords."
2568 :group
'org-progress
2570 (const :tag
"Don't force a record" nil
)
2571 (const :tag
"Force recording the DONE state" time
)
2572 (const :tag
"Force recording a note with the DONE state" note
)))
2575 (defgroup org-priorities nil
2576 "Priorities in Org-mode."
2577 :tag
"Org Priorities"
2580 (defcustom org-enable-priority-commands t
2581 "Non-nil means priority commands are active.
2582 When nil, these commands will be disabled, so that you never accidentally
2584 :group
'org-priorities
2587 (defcustom org-highest-priority ?A
2588 "The highest priority of TODO items. A character like ?A, ?B etc.
2589 Must have a smaller ASCII number than `org-lowest-priority'."
2590 :group
'org-priorities
2593 (defcustom org-lowest-priority ?C
2594 "The lowest priority of TODO items. A character like ?A, ?B etc.
2595 Must have a larger ASCII number than `org-highest-priority'."
2596 :group
'org-priorities
2599 (defcustom org-default-priority ?B
2600 "The default priority of TODO items.
2601 This is the priority an item gets if no explicit priority is given.
2602 When starting to cycle on an empty priority the first step in the cycle
2603 depends on `org-priority-start-cycle-with-default'. The resulting first
2604 step priority must not exceed the range from `org-highest-priority' to
2605 `org-lowest-priority' which means that `org-default-priority' has to be
2606 in this range exclusive or inclusive the range boundaries. Else the
2607 first step refuses to set the default and the second will fall back
2608 to (depending on the command used) the highest or lowest priority."
2609 :group
'org-priorities
2612 (defcustom org-priority-start-cycle-with-default t
2613 "Non-nil means start with default priority when starting to cycle.
2614 When this is nil, the first step in the cycle will be (depending on the
2615 command used) one higher or lower than the default priority.
2616 See also `org-default-priority'."
2617 :group
'org-priorities
2620 (defcustom org-get-priority-function nil
2621 "Function to extract the priority from a string.
2622 The string is normally the headline. If this is nil Org computes the
2623 priority from the priority cookie like [#A] in the headline. It returns
2624 an integer, increasing by 1000 for each priority level.
2625 The user can set a different function here, which should take a string
2626 as an argument and return the numeric priority."
2627 :group
'org-priorities
2631 (defgroup org-time nil
2632 "Options concerning time stamps and deadlines in Org-mode."
2636 (defcustom org-insert-labeled-timestamps-at-point nil
2637 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2638 When nil, these labeled time stamps are forces into the second line of an
2639 entry, just after the headline. When scheduling from the global TODO list,
2640 the time stamp will always be forced into the second line."
2644 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2645 "Formats for `format-time-string' which are used for time stamps.
2646 It is not recommended to change this constant.")
2648 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2649 "Number of minutes to round time stamps to.
2650 These are two values, the first applies when first creating a time stamp.
2651 The second applies when changing it with the commands `S-up' and `S-down'.
2652 When changing the time stamp, this means that it will change in steps
2653 of N minutes, as given by the second value.
2655 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2656 numbers should be factors of 60, so for example 5, 10, 15.
2658 When this is larger than 1, you can still force an exact time stamp by using
2659 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2660 and by using a prefix arg to `S-up/down' to specify the exact number
2661 of minutes to shift."
2663 :get
#'(lambda (var) ; Make sure both elements are there
2664 (if (integerp (default-value var
))
2665 (list (default-value var
) 5)
2666 (default-value var
)))
2668 (integer :tag
"when inserting times")
2669 (integer :tag
"when modifying times")))
2671 ;; Normalize old customizations of this variable.
2672 (when (integerp org-time-stamp-rounding-minutes
)
2673 (setq org-time-stamp-rounding-minutes
2674 (list org-time-stamp-rounding-minutes
2675 org-time-stamp-rounding-minutes
)))
2677 (defcustom org-display-custom-times nil
2678 "Non-nil means overlay custom formats over all time stamps.
2679 The formats are defined through the variable `org-time-stamp-custom-formats'.
2680 To turn this on on a per-file basis, insert anywhere in the file:
2681 #+STARTUP: customtime"
2685 (make-variable-buffer-local 'org-display-custom-times
)
2687 (defcustom org-time-stamp-custom-formats
2688 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2689 "Custom formats for time stamps. See `format-time-string' for the syntax.
2690 These are overlaid over the default ISO format if the variable
2691 `org-display-custom-times' is set. Time like %H:%M should be at the
2692 end of the second format. The custom formats are also honored by export
2693 commands, if custom time display is turned on at the time of export."
2697 (defun org-time-stamp-format (&optional long inactive
)
2698 "Get the right format for a time string."
2699 (let ((f (if long
(cdr org-time-stamp-formats
)
2700 (car org-time-stamp-formats
))))
2702 (concat "[" (substring f
1 -
1) "]")
2705 (defcustom org-time-clocksum-format
"%d:%02d"
2706 "The format string used when creating CLOCKSUM lines.
2707 This is also used when org-mode generates a time duration."
2711 (defcustom org-time-clocksum-use-fractional nil
2712 "If non-nil, \\[org-clock-display] uses fractional times.
2713 org-mode generates a time duration."
2717 (defcustom org-time-clocksum-fractional-format
"%.2f"
2718 "The format string used when creating CLOCKSUM lines, or when
2719 org-mode generates a time duration."
2723 (defcustom org-deadline-warning-days
14
2724 "No. of days before expiration during which a deadline becomes active.
2725 This variable governs the display in sparse trees and in the agenda.
2726 When 0 or negative, it means use this number (the absolute value of it)
2727 even if a deadline has a different individual lead time specified.
2729 Custom commands can set this variable in the options section."
2731 :group
'org-agenda-daily
/weekly
2734 (defcustom org-read-date-prefer-future t
2735 "Non-nil means assume future for incomplete date input from user.
2736 This affects the following situations:
2737 1. The user gives a month but not a year.
2738 For example, if it is April and you enter \"feb 2\", this will be read
2739 as Feb 2, *next* year. \"May 5\", however, will be this year.
2740 2. The user gives a day, but no month.
2741 For example, if today is the 15th, and you enter \"3\", Org-mode will
2742 read this as the third of *next* month. However, if you enter \"17\",
2743 it will be considered as *this* month.
2745 If you set this variable to the symbol `time', then also the following
2748 3. If the user gives a time, but no day. If the time is before now,
2749 to will be interpreted as tomorrow.
2751 Currently none of this works for ISO week specifications.
2753 When this option is nil, the current day, month and year will always be
2756 See also `org-agenda-jump-prefer-future'."
2759 (const :tag
"Never" nil
)
2760 (const :tag
"Check month and day" t
)
2761 (const :tag
"Check month, day, and time" time
)))
2763 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
2764 "Should the agenda jump command prefer the future for incomplete dates?
2765 The default is to do the same as configured in `org-read-date-prefer-future'.
2766 But you can also set a deviating value here.
2767 This may t or nil, or the symbol `org-read-date-prefer-future'."
2772 (const :tag
"Use org-read-date-prefer-future"
2773 org-read-date-prefer-future
)
2774 (const :tag
"Never" nil
)
2775 (const :tag
"Always" t
)))
2777 (defcustom org-read-date-force-compatible-dates t
2778 "Should date/time prompt force dates that are guaranteed to work in Emacs?
2780 Depending on the system Emacs is running on, certain dates cannot
2781 be represented with the type used internally to represent time.
2782 Dates between 1970-1-1 and 2038-1-1 can always be represented
2783 correctly. Some systems allow for earlier dates, some for later,
2784 some for both. One way to find out it to insert any date into an
2785 Org buffer, putting the cursor on the year and hitting S-up and
2786 S-down to test the range.
2788 When this variable is set to t, the date/time prompt will not let
2789 you specify dates outside the 1970-2037 range, so it is certain that
2790 these dates will work in whatever version of Emacs you are
2791 running, and also that you can move a file from one Emacs implementation
2792 to another. WHenever Org is forcing the year for you, it will display
2795 When this variable is nil, Org will check if the date is
2796 representable in the specific Emacs implementation you are using.
2797 If not, it will force a year, usually the current year, and beep
2798 to remind you. Currently this setting is not recommended because
2799 the likelihood that you will open your Org files in an Emacs that
2800 has limited date range is not negligible.
2802 A workaround for this problem is to use diary sexp dates for time
2803 stamps outside of this range."
2808 (defcustom org-read-date-display-live t
2809 "Non-nil means display current interpretation of date prompt live.
2810 This display will be in an overlay, in the minibuffer."
2814 (defcustom org-read-date-popup-calendar t
2815 "Non-nil means pop up a calendar when prompting for a date.
2816 In the calendar, the date can be selected with mouse-1. However, the
2817 minibuffer will also be active, and you can simply enter the date as well.
2818 When nil, only the minibuffer will be available."
2821 (if (fboundp 'defvaralias
)
2822 (defvaralias 'org-popup-calendar-for-date-prompt
2823 'org-read-date-popup-calendar
))
2825 (defcustom org-read-date-minibuffer-setup-hook nil
2826 "Hook to be used to set up keys for the date/time interface.
2827 Add key definitions to `minibuffer-local-map', which will be a temporary
2832 (defcustom org-extend-today-until
0
2833 "The hour when your day really ends. Must be an integer.
2834 This has influence for the following applications:
2835 - When switching the agenda to \"today\". It it is still earlier than
2836 the time given here, the day recognized as TODAY is actually yesterday.
2837 - When a date is read from the user and it is still before the time given
2838 here, the current date and time will be assumed to be yesterday, 23:59.
2839 Also, timestamps inserted in remember templates follow this rule.
2841 IMPORTANT: This is a feature whose implementation is and likely will
2842 remain incomplete. Really, it is only here because past midnight seems to
2843 be the favorite working time of John Wiegley :-)"
2847 (defcustom org-use-effective-time nil
2848 "If non-nil, consider `org-extend-today-until' when creating timestamps.
2849 For example, if `org-extend-today-until' is 8, and it's 4am, then the
2850 \"effective time\" of any timestamps between midnight and 8am will be
2851 23:59 of the previous day."
2856 (defcustom org-edit-timestamp-down-means-later nil
2857 "Non-nil means S-down will increase the time in a time stamp.
2858 When nil, S-up will increase."
2862 (defcustom org-calendar-follow-timestamp-change t
2863 "Non-nil means make the calendar window follow timestamp changes.
2864 When a timestamp is modified and the calendar window is visible, it will be
2865 moved to the new date."
2869 (defgroup org-tags nil
2870 "Options concerning tags in Org-mode."
2874 (defcustom org-tag-alist nil
2875 "List of tags allowed in Org-mode files.
2876 When this list is nil, Org-mode will base TAG input on what is already in the
2878 The value of this variable is an alist, the car of each entry must be a
2879 keyword as a string, the cdr may be a character that is used to select
2880 that tag through the fast-tag-selection interface.
2881 See the manual for details."
2885 (cons (string :tag
"Tag name")
2886 (character :tag
"Access char"))
2887 (list :tag
"Start radio group"
2889 (option (string :tag
"Group description")))
2890 (list :tag
"End radio group"
2892 (option (string :tag
"Group description")))
2893 (const :tag
"New line" (:newline
)))))
2895 (defcustom org-tag-persistent-alist nil
2896 "List of tags that will always appear in all Org-mode files.
2897 This is in addition to any in buffer settings or customizations
2899 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2900 The value of this variable is an alist, the car of each entry must be a
2901 keyword as a string, the cdr may be a character that is used to select
2902 that tag through the fast-tag-selection interface.
2903 See the manual for details.
2904 To disable these tags on a per-file basis, insert anywhere in the file:
2909 (cons (string :tag
"Tag name")
2910 (character :tag
"Access char"))
2911 (const :tag
"Start radio group" (:startgroup
))
2912 (const :tag
"End radio group" (:endgroup
))
2913 (const :tag
"New line" (:newline
)))))
2915 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2916 "If non-nil, always offer completion for all tags of all agenda files.
2917 Instead of customizing this variable directly, you might want to
2918 set it locally for capture buffers, because there no list of
2919 tags in that file can be created dynamically (there are none).
2921 (add-hook 'org-capture-mode-hook
2923 (set (make-local-variable
2924 'org-complete-tags-always-offer-all-agenda-tags)
2930 (defvar org-file-tags nil
2931 "List of tags that can be inherited by all entries in the file.
2932 The tags will be inherited if the variable `org-use-tag-inheritance'
2933 says they should be.
2934 This variable is populated from #+FILETAGS lines.")
2936 (defcustom org-use-fast-tag-selection
'auto
2937 "Non-nil means use fast tag selection scheme.
2938 This is a special interface to select and deselect tags with single keys.
2939 When nil, fast selection is never used.
2940 When the symbol `auto', fast selection is used if and only if selection
2941 characters for tags have been configured, either through the variable
2942 `org-tag-alist' or through a #+TAGS line in the buffer.
2943 When t, fast selection is always used and selection keys are assigned
2944 automatically if necessary."
2947 (const :tag
"Always" t
)
2948 (const :tag
"Never" nil
)
2949 (const :tag
"When selection characters are configured" 'auto
)))
2951 (defcustom org-fast-tag-selection-single-key nil
2952 "Non-nil means fast tag selection exits after first change.
2953 When nil, you have to press RET to exit it.
2954 During fast tag selection, you can toggle this flag with `C-c'.
2955 This variable can also have the value `expert'. In this case, the window
2956 displaying the tags menu is not even shown, until you press C-c again."
2959 (const :tag
"No" nil
)
2960 (const :tag
"Yes" t
)
2961 (const :tag
"Expert" expert
)))
2963 (defvar org-fast-tag-selection-include-todo nil
2964 "Non-nil means fast tags selection interface will also offer TODO states.
2965 This is an undocumented feature, you should not rely on it.")
2967 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2968 "The column to which tags should be indented in a headline.
2969 If this number is positive, it specifies the column. If it is negative,
2970 it means that the tags should be flushright to that column. For example,
2971 -80 works well for a normal 80 character screen.
2972 When 0, place tags directly after headline text, with only one space in
2977 (defcustom org-auto-align-tags t
2978 "Non-nil keeps tags aligned when modifying headlines.
2979 Some operations (i.e. demoting) change the length of a headline and
2980 therefore shift the tags around. With this option turned on, after
2981 each such operation the tags are again aligned to `org-tags-column'."
2985 (defcustom org-use-tag-inheritance t
2986 "Non-nil means tags in levels apply also for sublevels.
2987 When nil, only the tags directly given in a specific line apply there.
2988 This may also be a list of tags that should be inherited, or a regexp that
2989 matches tags that should be inherited. Additional control is possible
2990 with the variable `org-tags-exclude-from-inheritance' which gives an
2991 explicit list of tags to be excluded from inheritance., even if the value of
2992 `org-use-tag-inheritance' would select it for inheritance.
2994 If this option is t, a match early-on in a tree can lead to a large
2995 number of matches in the subtree when constructing the agenda or creating
2996 a sparse tree. If you only want to see the first match in a tree during
2997 a search, check out the variable `org-tags-match-list-sublevels'."
3000 (const :tag
"Not" nil
)
3001 (const :tag
"Always" t
)
3002 (repeat :tag
"Specific tags" (string :tag
"Tag"))
3003 (regexp :tag
"Tags matched by regexp")))
3005 (defcustom org-tags-exclude-from-inheritance nil
3006 "List of tags that should never be inherited.
3007 This is a way to exclude a few tags from inheritance. For way to do
3008 the opposite, to actively allow inheritance for selected tags,
3009 see the variable `org-use-tag-inheritance'."
3011 :type
'(repeat (string :tag
"Tag")))
3013 (defun org-tag-inherit-p (tag)
3014 "Check if TAG is one that should be inherited."
3016 ((member tag org-tags-exclude-from-inheritance
) nil
)
3017 ((eq org-use-tag-inheritance t
) t
)
3018 ((not org-use-tag-inheritance
) nil
)
3019 ((stringp org-use-tag-inheritance
)
3020 (string-match org-use-tag-inheritance tag
))
3021 ((listp org-use-tag-inheritance
)
3022 (member tag org-use-tag-inheritance
))
3023 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3025 (defcustom org-tags-match-list-sublevels t
3026 "Non-nil means list also sublevels of headlines matching a search.
3027 This variable applies to tags/property searches, and also to stuck
3028 projects because this search is based on a tags match as well.
3030 When set to the symbol `indented', sublevels are indented with
3033 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3034 the sublevels of a headline matching a tag search often also match
3035 the same search. Listing all of them can create very long lists.
3036 Setting this variable to nil causes subtrees of a match to be skipped.
3038 This variable is semi-obsolete and probably should always be true. It
3039 is better to limit inheritance to certain tags using the variables
3040 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3043 (const :tag
"No, don't list them" nil
)
3044 (const :tag
"Yes, do list them" t
)
3045 (const :tag
"List them, indented with leading dots" indented
)))
3047 (defcustom org-tags-sort-function nil
3048 "When set, tags are sorted using this function as a comparator."
3051 (const :tag
"No sorting" nil
)
3052 (const :tag
"Alphabetical" string
<)
3053 (const :tag
"Reverse alphabetical" string
>)
3054 (function :tag
"Custom function" nil
)))
3056 (defvar org-tags-history nil
3057 "History of minibuffer reads for tags.")
3058 (defvar org-last-tags-completion-table nil
3059 "The last used completion table for tags.")
3060 (defvar org-after-tags-change-hook nil
3061 "Hook that is run after the tags in a line have changed.")
3063 (defgroup org-properties nil
3064 "Options concerning properties in Org-mode."
3065 :tag
"Org Properties"
3068 (defcustom org-property-format
"%-10s %s"
3069 "How property key/value pairs should be formatted by `indent-line'.
3070 When `indent-line' hits a property definition, it will format the line
3071 according to this format, mainly to make sure that the values are
3072 lined-up with respect to each other."
3073 :group
'org-properties
3076 (defcustom org-properties-postprocess-alist nil
3077 "Alist of properties and functions to adjust inserted values.
3078 Elements of this alist must be of the form
3080 ([string] [function])
3082 where [string] must be a property name and [function] must be a
3083 lambda expression: this lambda expression must take one argument,
3084 the value to adjust, and return the new value as a string.
3086 For example, this element will allow the property \"Remaining\"
3087 to be updated wrt the relation between the \"Effort\" property
3088 and the clock summary:
3090 ((\"Remaining\" (lambda(value)
3091 (let ((clocksum (org-clock-sum-current-item))
3092 (effort (org-duration-string-to-minutes
3093 (org-entry-get (point) \"Effort\"))))
3094 (org-minutes-to-hh:mm-string (- effort clocksum))))))"
3095 :group
'org-properties
3097 :type
'(alist :key-type
(string :tag
"Property")
3098 :value-type
(function :tag
"Function")))
3100 (defcustom org-use-property-inheritance nil
3101 "Non-nil means properties apply also for sublevels.
3103 This setting is chiefly used during property searches. Turning it on can
3104 cause significant overhead when doing a search, which is why it is not
3107 When nil, only the properties directly given in the current entry count.
3108 When t, every property is inherited. The value may also be a list of
3109 properties that should have inheritance, or a regular expression matching
3110 properties that should be inherited.
3112 However, note that some special properties use inheritance under special
3113 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3114 and the properties ending in \"_ALL\" when they are used as descriptor
3115 for valid values of a property.
3117 Note for programmers:
3118 When querying an entry with `org-entry-get', you can control if inheritance
3119 should be used. By default, `org-entry-get' looks only at the local
3120 properties. You can request inheritance by setting the inherit argument
3121 to t (to force inheritance) or to `selective' (to respect the setting
3123 :group
'org-properties
3125 (const :tag
"Not" nil
)
3126 (const :tag
"Always" t
)
3127 (repeat :tag
"Specific properties" (string :tag
"Property"))
3128 (regexp :tag
"Properties matched by regexp")))
3130 (defun org-property-inherit-p (property)
3131 "Check if PROPERTY is one that should be inherited."
3133 ((eq org-use-property-inheritance t
) t
)
3134 ((not org-use-property-inheritance
) nil
)
3135 ((stringp org-use-property-inheritance
)
3136 (string-match org-use-property-inheritance property
))
3137 ((listp org-use-property-inheritance
)
3138 (member property org-use-property-inheritance
))
3139 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3141 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3142 "The default column format, if no other format has been defined.
3143 This variable can be set on the per-file basis by inserting a line
3145 #+COLUMNS: %25ITEM ....."
3146 :group
'org-properties
3149 (defcustom org-columns-ellipses
".."
3150 "The ellipses to be used when a field in column view is truncated.
3151 When this is the empty string, as many characters as possible are shown,
3152 but then there will be no visual indication that the field has been truncated.
3153 When this is a string of length N, the last N characters of a truncated
3154 field are replaced by this string. If the column is narrower than the
3155 ellipses string, only part of the ellipses string will be shown."
3156 :group
'org-properties
3159 (defcustom org-columns-modify-value-for-display-function nil
3160 "Function that modifies values for display in column view.
3161 For example, it can be used to cut out a certain part from a time stamp.
3162 The function must take 2 arguments:
3164 column-title The title of the column (*not* the property name)
3165 value The value that should be modified.
3167 The function should return the value that should be displayed,
3168 or nil if the normal value should be used."
3169 :group
'org-properties
3172 (defcustom org-effort-property
"Effort"
3173 "The property that is being used to keep track of effort estimates.
3174 Effort estimates given in this property need to have the format H:MM."
3175 :group
'org-properties
3176 :group
'org-progress
3177 :type
'(string :tag
"Property"))
3179 (defconst org-global-properties-fixed
3180 '(("VISIBILITY_ALL" .
"folded children content all")
3181 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3182 "List of property/value pairs that can be inherited by any entry.
3184 These are fixed values, for the preset properties. The user variable
3185 that can be used to add to this list is `org-global-properties'.
3187 The entries in this list are cons cells where the car is a property
3188 name and cdr is a string with the value. If the value represents
3189 multiple items like an \"_ALL\" property, separate the items by
3192 (defcustom org-global-properties nil
3193 "List of property/value pairs that can be inherited by any entry.
3195 This list will be combined with the constant `org-global-properties-fixed'.
3197 The entries in this list are cons cells where the car is a property
3198 name and cdr is a string with the value.
3200 You can set buffer-local values for the same purpose in the variable
3201 `org-file-properties' this by adding lines like
3203 #+PROPERTY: NAME VALUE"
3204 :group
'org-properties
3206 (cons (string :tag
"Property")
3207 (string :tag
"Value"))))
3209 (defvar org-file-properties nil
3210 "List of property/value pairs that can be inherited by any entry.
3211 Valid for the current buffer.
3212 This variable is populated from #+PROPERTY lines.")
3213 (make-variable-buffer-local 'org-file-properties
)
3215 (defgroup org-agenda nil
3216 "Options concerning agenda views in Org-mode."
3220 (defvar org-category nil
3221 "Variable used by org files to set a category for agenda display.
3222 Such files should use a file variable to set it, for example
3224 # -*- mode: org; org-category: \"ELisp\"
3226 or contain a special line
3230 If the file does not specify a category, then file's base name
3232 (make-variable-buffer-local 'org-category
)
3233 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3235 (defcustom org-agenda-files nil
3236 "The files to be used for agenda display.
3237 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3238 \\[org-remove-file]. You can also use customize to edit the list.
3240 If an entry is a directory, all files in that directory that are matched by
3241 `org-agenda-file-regexp' will be part of the file list.
3243 If the value of the variable is not a list but a single file name, then
3244 the list of agenda files is actually stored and maintained in that file, one
3245 agenda file per line. In this file paths can be given relative to
3246 `org-directory'. Tilde expansion and environment variable substitution
3250 (repeat :tag
"List of files and directories" file
)
3251 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3253 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3254 "Regular expression to match files for `org-agenda-files'.
3255 If any element in the list in that variable contains a directory instead
3256 of a normal file, all files in that directory that are matched by this
3257 regular expression will be included."
3261 (defcustom org-agenda-text-search-extra-files nil
3262 "List of extra files to be searched by text search commands.
3263 These files will be search in addition to the agenda files by the
3264 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3265 Note that these files will only be searched for text search commands,
3266 not for the other agenda views like todo lists, tag searches or the weekly
3267 agenda. This variable is intended to list notes and possibly archive files
3268 that should also be searched by these two commands.
3269 In fact, if the first element in the list is the symbol `agenda-archives',
3270 than all archive files of all agenda files will be added to the search
3273 :type
'(set :greedy t
3274 (const :tag
"Agenda Archives" agenda-archives
)
3275 (repeat :inline t
(file))))
3277 (if (fboundp 'defvaralias
)
3278 (defvaralias 'org-agenda-multi-occur-extra-files
3279 'org-agenda-text-search-extra-files
))
3281 (defcustom org-agenda-skip-unavailable-files nil
3282 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3283 A nil value means to remove them, after a query, from the list."
3287 (defcustom org-calendar-to-agenda-key
[?c
]
3288 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3289 The command `org-calendar-goto-agenda' will be bound to this key. The
3290 default is the character `c' because then `c' can be used to switch back and
3291 forth between agenda and calendar."
3295 (defcustom org-calendar-agenda-action-key
[?k
]
3296 "The key to be installed in `calendar-mode-map' for agenda-action.
3297 The command `org-agenda-action' will be bound to this key. The
3298 default is the character `k' because we use the same key in the agenda."
3302 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3303 "The key to be installed in `calendar-mode-map' for adding diary entries.
3304 This option is irrelevant until `org-agenda-diary-file' has been configured
3305 to point to an Org-mode file. When that is the case, the command
3306 `org-agenda-diary-entry' will be bound to the key given here, by default
3307 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3308 if you want to continue doing this, you need to change this to a different
3313 (defcustom org-agenda-diary-file
'diary-file
3314 "File to which to add new entries with the `i' key in agenda and calendar.
3315 When this is the symbol `diary-file', the functionality in the Emacs
3316 calendar will be used to add entries to the `diary-file'. But when this
3317 points to a file, `org-agenda-diary-entry' will be used instead."
3320 (const :tag
"The standard Emacs diary file" diary-file
)
3321 (file :tag
"Special Org file diary entries")))
3323 (eval-after-load "calendar"
3325 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3326 'org-calendar-goto-agenda
)
3327 (org-defkey calendar-mode-map org-calendar-agenda-action-key
3329 (add-hook 'calendar-mode-hook
3331 (unless (eq org-agenda-diary-file
'diary-file
)
3332 (define-key calendar-mode-map
3333 org-calendar-insert-diary-entry-key
3334 'org-agenda-diary-entry
))))))
3336 (defgroup org-latex nil
3337 "Options for embedding LaTeX code into Org-mode."
3341 (defcustom org-format-latex-options
3342 '(:foreground default
:background default
:scale
1.0
3343 :html-foreground
"Black" :html-background
"Transparent"
3344 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3345 "Options for creating images from LaTeX fragments.
3346 This is a property list with the following properties:
3347 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3348 `default' means use the foreground of the default face.
3349 :background the background color, or \"Transparent\".
3350 `default' means use the background of the default face.
3351 :scale a scaling factor for the size of the images, to get more pixels
3352 :html-foreground, :html-background, :html-scale
3353 the same numbers for HTML export.
3354 :matchers a list indicating which matchers should be used to
3355 find LaTeX fragments. Valid members of this list are:
3356 \"begin\" find environments
3357 \"$1\" find single characters surrounded by $.$
3358 \"$\" find math expressions surrounded by $...$
3359 \"$$\" find math expressions surrounded by $$....$$
3360 \"\\(\" find math expressions surrounded by \\(...\\)
3361 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3365 (defcustom org-format-latex-signal-error t
3366 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3367 When nil, just push out a message."
3372 (defcustom org-latex-to-mathml-jar-file nil
3373 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3374 Use this to specify additional executable file say a jar file.
3376 When using MathToWeb as the converter, specify the full-path to
3377 your mathtoweb.jar file."
3381 (const :tag
"None" nil
)
3382 (file :tag
"JAR file" :must-match t
)))
3384 (defcustom org-latex-to-mathml-convert-command nil
3385 "Command to convert LaTeX fragments to MathML.
3386 Replace format-specifiers in the command as noted below and use
3387 `shell-command' to convert LaTeX to MathML.
3388 %j: Executable file in fully expanded form as specified by
3389 `org-latex-to-mathml-jar-file'.
3390 %I: Input LaTeX file in fully expanded form
3391 %o: Output MathML file
3392 This command is used by `org-create-math-formula'.
3394 When using MathToWeb as the converter, set this to
3395 \"java -jar %j -unicode -force -df %o %I\"."
3399 (const :tag
"None" nil
)
3400 (string :tag
"\nShell command")))
3402 (defcustom org-latex-create-formula-image-program
'dvipng
3403 "Program to convert LaTeX fragments with.
3405 dvipng Process the LaTeX fragments to dvi file, then convert
3406 dvi files to png files using dvipng.
3407 This will also include processing of non-math environments.
3408 imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
3409 to convert pdf files to png files"
3413 (const :tag
"dvipng" dvipng
)
3414 (const :tag
"imagemagick" imagemagick
)))
3416 (defun org-format-latex-mathml-available-p ()
3417 "Return t if `org-latex-to-mathml-convert-command' is usable."
3419 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3420 org-latex-to-mathml-convert-command
)
3421 (let ((executable (car (split-string
3422 org-latex-to-mathml-convert-command
))))
3423 (when (executable-find executable
)
3425 "%j" org-latex-to-mathml-convert-command
)
3426 (file-readable-p org-latex-to-mathml-jar-file
)
3429 (defcustom org-format-latex-header
"\\documentclass{article}
3430 \\usepackage[usenames]{color}
3431 \\usepackage{amsmath}
3432 \\usepackage[mathscr]{eucal}
3433 \\pagestyle{empty} % do not remove
3436 % The settings below are copied from fullpage.sty
3437 \\setlength{\\textwidth}{\\paperwidth}
3438 \\addtolength{\\textwidth}{-3cm}
3439 \\setlength{\\oddsidemargin}{1.5cm}
3440 \\addtolength{\\oddsidemargin}{-2.54cm}
3441 \\setlength{\\evensidemargin}{\\oddsidemargin}
3442 \\setlength{\\textheight}{\\paperheight}
3443 \\addtolength{\\textheight}{-\\headheight}
3444 \\addtolength{\\textheight}{-\\headsep}
3445 \\addtolength{\\textheight}{-\\footskip}
3446 \\addtolength{\\textheight}{-3cm}
3447 \\setlength{\\topmargin}{1.5cm}
3448 \\addtolength{\\topmargin}{-2.54cm}"
3449 "The document header used for processing LaTeX fragments.
3450 It is imperative that this header make sure that no page number
3451 appears on the page. The package defined in the variables
3452 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
3453 will either replace the placeholder \"[PACKAGES]\" in this header, or they
3458 (defvar org-format-latex-header-extra nil
)
3460 (defun org-set-packages-alist (var val
)
3461 "Set the packages alist and make sure it has 3 elements per entry."
3462 (set var
(mapcar (lambda (x)
3463 (if (and (consp x
) (= (length x
) 2))
3464 (list (car x
) (nth 1 x
) t
)
3468 (defun org-get-packages-alist (var)
3470 "Get the packages alist and make sure it has 3 elements per entry."
3472 (if (and (consp x
) (= (length x
) 2))
3473 (list (car x
) (nth 1 x
) t
)
3475 (default-value var
)))
3477 ;; The following variables are defined here because is it also used
3478 ;; when formatting latex fragments. Originally it was part of the
3479 ;; LaTeX exporter, which is why the name includes "export".
3480 (defcustom org-export-latex-default-packages-alist
3481 '(("AUTO" "inputenc" t
)
3485 ("" "longtable" nil
)
3497 "Alist of default packages to be inserted in the header.
3498 Change this only if one of the packages here causes an incompatibility
3499 with another package you are using.
3500 The packages in this list are needed by one part or another of Org-mode
3501 to function properly.
3503 - inputenc, fontenc: for basic font and character selection
3504 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
3505 for interpreting the entities in `org-entities'. You can skip some of these
3506 packages if you don't use any of the symbols in it.
3507 - graphicx: for including images
3508 - float, wrapfig: for figure placement
3509 - longtable: for long tables
3510 - hyperref: for cross references
3512 Therefore you should not modify this variable unless you know what you
3513 are doing. The one reason to change it anyway is that you might be loading
3514 some other package that conflicts with one of the default packages.
3515 Each cell is of the format \( \"options\" \"package\" snippet-flag\).
3516 If SNIPPET-FLAG is t, the package also needs to be included when
3517 compiling LaTeX snippets into images for inclusion into HTML."
3518 :group
'org-export-latex
3519 :set
'org-set-packages-alist
3520 :get
'org-get-packages-alist
3524 (list :tag
"options/package pair"
3525 (string :tag
"options")
3526 (string :tag
"package")
3527 (boolean :tag
"Snippet"))
3528 (string :tag
"A line of LaTeX"))))
3530 (defcustom org-export-latex-packages-alist nil
3531 "Alist of packages to be inserted in every LaTeX header.
3532 These will be inserted after `org-export-latex-default-packages-alist'.
3533 Each cell is of the format \( \"options\" \"package\" snippet-flag \).
3534 SNIPPET-FLAG, when t, indicates that this package is also needed when
3535 turning LaTeX snippets into images for inclusion into HTML.
3536 Make sure that you only list packages here which:
3537 - you want in every file
3538 - do not conflict with the default packages in
3539 `org-export-latex-default-packages-alist'
3540 - do not conflict with the setup in `org-format-latex-header'."
3541 :group
'org-export-latex
3542 :set
'org-set-packages-alist
3543 :get
'org-get-packages-alist
3546 (list :tag
"options/package pair"
3547 (string :tag
"options")
3548 (string :tag
"package")
3549 (boolean :tag
"Snippet"))
3550 (string :tag
"A line of LaTeX"))))
3553 (defgroup org-appearance nil
3554 "Settings for Org-mode appearance."
3555 :tag
"Org Appearance"
3558 (defcustom org-level-color-stars-only nil
3559 "Non-nil means fontify only the stars in each headline.
3560 When nil, the entire headline is fontified.
3561 Changing it requires restart of `font-lock-mode' to become effective
3562 also in regions already fontified."
3563 :group
'org-appearance
3566 (defcustom org-hide-leading-stars nil
3567 "Non-nil means hide the first N-1 stars in a headline.
3568 This works by using the face `org-hide' for these stars. This
3569 face is white for a light background, and black for a dark
3570 background. You may have to customize the face `org-hide' to
3572 Changing it requires restart of `font-lock-mode' to become effective
3573 also in regions already fontified.
3574 You may also set this on a per-file basis by adding one of the following
3575 lines to the buffer:
3577 #+STARTUP: hidestars
3578 #+STARTUP: showstars"
3579 :group
'org-appearance
3582 (defcustom org-hidden-keywords nil
3583 "List of symbols corresponding to keywords to be hidden the org buffer.
3584 For example, a value '(title) for this list will make the document's title
3585 appear in the buffer without the initial #+TITLE: keyword."
3586 :group
'org-appearance
3588 :type
'(set (const :tag
"#+AUTHOR" author
)
3589 (const :tag
"#+DATE" date
)
3590 (const :tag
"#+EMAIL" email
)
3591 (const :tag
"#+TITLE" title
)))
3593 (defcustom org-fontify-done-headline nil
3594 "Non-nil means change the face of a headline if it is marked DONE.
3595 Normally, only the TODO/DONE keyword indicates the state of a headline.
3596 When this is non-nil, the headline after the keyword is set to the
3597 `org-headline-done' as an additional indication."
3598 :group
'org-appearance
3601 (defcustom org-fontify-emphasized-text t
3602 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3603 Changing this variable requires a restart of Emacs to take effect."
3604 :group
'org-appearance
3607 (defcustom org-fontify-whole-heading-line nil
3608 "Non-nil means fontify the whole line for headings.
3609 This is useful when setting a background color for the
3611 :group
'org-appearance
3614 (defcustom org-highlight-latex-fragments-and-specials nil
3615 "Non-nil means fontify what is treated specially by the exporters."
3616 :group
'org-appearance
3619 (defcustom org-hide-emphasis-markers nil
3620 "Non-nil mean font-lock should hide the emphasis marker characters."
3621 :group
'org-appearance
3624 (defcustom org-pretty-entities nil
3625 "Non-nil means show entities as UTF8 characters.
3626 When nil, the \\name form remains in the buffer."
3627 :group
'org-appearance
3631 (defcustom org-pretty-entities-include-sub-superscripts t
3632 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3633 :group
'org-appearance
3637 (defvar org-emph-re nil
3638 "Regular expression for matching emphasis.
3639 After a match, the match groups contain these elements:
3640 0 The match of the full regular expression, including the characters
3641 before and after the proper match
3642 1 The character before the proper match, or empty at beginning of line
3643 2 The proper match, including the leading and trailing markers
3644 3 The leading marker like * or /, indicating the type of highlighting
3645 4 The text between the emphasis markers, not including the markers
3646 5 The character after the match, empty at the end of a line")
3647 (defvar org-verbatim-re nil
3648 "Regular expression for matching verbatim text.")
3649 (defvar org-emphasis-regexp-components
) ; defined just below
3650 (defvar org-emphasis-alist
) ; defined just below
3651 (defun org-set-emph-re (var val
)
3652 "Set variable and compute the emphasis regular expression."
3654 (when (and (boundp 'org-emphasis-alist
)
3655 (boundp 'org-emphasis-regexp-components
)
3656 org-emphasis-alist org-emphasis-regexp-components
)
3657 (let* ((e org-emphasis-regexp-components
)
3663 (body1 (concat body
"*?"))
3664 (markers (mapconcat 'car org-emphasis-alist
""))
3665 (vmarkers (mapconcat
3666 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3667 org-emphasis-alist
"")))
3668 ;; make sure special characters appear at the right position in the class
3669 (if (string-match "\\^" markers
)
3670 (setq markers
(concat (replace-match "" t t markers
) "^")))
3671 (if (string-match "-" markers
)
3672 (setq markers
(concat (replace-match "" t t markers
) "-")))
3673 (if (string-match "\\^" vmarkers
)
3674 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3675 (if (string-match "-" vmarkers
)
3676 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3678 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3679 (int-to-string nl
) "\\}")))
3682 (concat "\\([" pre
"]\\|^\\)"
3684 "\\([" markers
"]\\)"
3692 "\\([" post
"]\\|$\\)"))
3693 (setq org-verbatim-re
3694 (concat "\\([" pre
"]\\|^\\)"
3696 "\\([" vmarkers
"]\\)"
3704 "\\([" post
"]\\|$\\)")))))
3706 (defcustom org-emphasis-regexp-components
3707 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3708 "Components used to build the regular expression for emphasis.
3709 This is a list with five entries. Terminology: In an emphasis string
3710 like \" *strong word* \", we call the initial space PREMATCH, the final
3711 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3712 and \"trong wor\" is the body. The different components in this variable
3713 specify what is allowed/forbidden in each part:
3715 pre Chars allowed as prematch. Beginning of line will be allowed too.
3716 post Chars allowed as postmatch. End of line will be allowed too.
3717 border The chars *forbidden* as border characters.
3718 body-regexp A regexp like \".\" to match a body character. Don't use
3719 non-shy groups here, and don't allow newline here.
3720 newline The maximum number of newlines allowed in an emphasis exp.
3722 Use customize to modify this, or restart Emacs after changing it."
3723 :group
'org-appearance
3724 :set
'org-set-emph-re
3726 (sexp :tag
"Allowed chars in pre ")
3727 (sexp :tag
"Allowed chars in post ")
3728 (sexp :tag
"Forbidden chars in border ")
3729 (sexp :tag
"Regexp for body ")
3730 (integer :tag
"number of newlines allowed")
3731 (option (boolean :tag
"Please ignore this button"))))
3733 (defcustom org-emphasis-alist
3734 `(("*" bold
"<b>" "</b>")
3735 ("/" italic
"<i>" "</i>")
3736 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
3737 ("=" org-code
"<code>" "</code>" verbatim
)
3738 ("~" org-verbatim
"<code>" "</code>" verbatim
)
3739 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
3742 "Special syntax for emphasized text.
3743 Text starting and ending with a special character will be emphasized, for
3744 example *bold*, _underlined_ and /italic/. This variable sets the marker
3745 characters, the face to be used by font-lock for highlighting in Org-mode
3746 Emacs buffers, and the HTML tags to be used for this.
3747 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3748 For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
3749 Use customize to modify this, or restart Emacs after changing it."
3750 :group
'org-appearance
3751 :set
'org-set-emph-re
3754 (string :tag
"Marker character")
3756 (face :tag
"Font-lock-face")
3757 (plist :tag
"Face property list"))
3758 (string :tag
"HTML start tag")
3759 (string :tag
"HTML end tag")
3760 (option (const verbatim
)))))
3762 (defvar org-protecting-blocks
3763 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3764 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3765 This is needed for font-lock setup.")
3767 ;;; Miscellaneous options
3769 (defgroup org-completion nil
3770 "Completion in Org-mode."
3771 :tag
"Org Completion"
3774 (defcustom org-completion-use-ido nil
3775 "Non-nil means use ido completion wherever possible.
3776 Note that `ido-mode' must be active for this variable to be relevant.
3777 If you decide to turn this variable on, you might well want to turn off
3778 `org-outline-path-complete-in-steps'.
3779 See also `org-completion-use-iswitchb'."
3780 :group
'org-completion
3783 (defcustom org-completion-use-iswitchb nil
3784 "Non-nil means use iswitchb completion wherever possible.
3785 Note that `iswitchb-mode' must be active for this variable to be relevant.
3786 If you decide to turn this variable on, you might well want to turn off
3787 `org-outline-path-complete-in-steps'.
3788 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3789 :group
'org-completion
3792 (defcustom org-completion-fallback-command
'hippie-expand
3793 "The expansion command called by \\[pcomplete] in normal context.
3794 Normal means, no org-mode-specific context."
3795 :group
'org-completion
3798 ;;; Functions and variables from their packages
3799 ;; Declared here to avoid compiler warnings
3802 (defvar outline-mode-menu-heading
)
3803 (defvar outline-mode-menu-show
)
3804 (defvar outline-mode-menu-hide
)
3805 (defvar zmacs-regions
) ; XEmacs regions
3808 (defvar mark-active
)
3811 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
3812 (declare-function calendar-forward-day
"cal-move" (arg))
3813 (declare-function calendar-goto-date
"cal-move" (date))
3814 (declare-function calendar-goto-today
"cal-move" ())
3815 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
3816 (defvar calc-embedded-close-formula
)
3817 (defvar calc-embedded-open-formula
)
3818 (declare-function cdlatex-tab
"ext:cdlatex" ())
3819 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
3820 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
3821 (defvar font-lock-unfontify-region-function
)
3822 (declare-function iswitchb-read-buffer
"iswitchb"
3823 (prompt &optional default require-match start matches-set
))
3824 (defvar iswitchb-temp-buflist
)
3825 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
3826 (defvar org-agenda-tags-todo-honor-ignore-options
)
3827 (declare-function org-agenda-skip
"org-agenda" ())
3829 org-agenda-format-item
"org-agenda"
3830 (extra txt
&optional category tags dotime noprefix remove-re habitp
))
3831 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
3832 (declare-function org-agenda-change-all-lines
"org-agenda"
3833 (newhead hdmarker
&optional fixface just-this
))
3834 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
3835 (declare-function org-agenda-maybe-redo
"org-agenda" ())
3836 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
3838 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
3839 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3840 "org-agenda" (&optional end
))
3841 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
3842 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
3843 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
3844 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
3845 (declare-function org-indent-mode
"org-indent" (&optional arg
))
3846 (declare-function parse-time-string
"parse-time" (string))
3847 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
3848 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
3849 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
3850 (defvar remember-data-file
)
3851 (defvar texmathp-why
)
3852 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
3853 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
3855 (defvar w3m-current-url
)
3856 (defvar w3m-current-title
)
3858 (defvar org-latex-regexps
)
3860 ;;; Autoload and prepare some org modules
3862 ;; Some table stuff that needs to be defined here, because it is used
3863 ;; by the functions setting up org-mode or checking for table context.
3865 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
3866 "Detect an org-type or table-type table.")
3867 (defconst org-table-line-regexp
"^[ \t]*|"
3868 "Detect an org-type table line.")
3869 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
3870 "Detect an org-type table line.")
3871 (defconst org-table-hline-regexp
"^[ \t]*|-"
3872 "Detect an org-type table hline.")
3873 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
3874 "Detect a table-type table hline.")
3875 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
3876 "Detect the first line outside a table when searching from within it.
3877 This works for both table types.")
3879 ;; Autoload the functions in org-table.el that are needed by functions here.
3882 (org-autoload "org-table"
3883 '(org-table-align org-table-begin org-table-blank-field
3884 org-table-convert org-table-convert-region org-table-copy-down
3885 org-table-copy-region org-table-create
3886 org-table-create-or-convert-from-region
3887 org-table-create-with-table.el org-table-current-dline
3888 org-table-cut-region org-table-delete-column org-table-edit-field
3889 org-table-edit-formulas org-table-end org-table-eval-formula
3890 org-table-export org-table-field-info
3891 org-table-get-stored-formulas org-table-goto-column
3892 org-table-hline-and-move org-table-import org-table-insert-column
3893 org-table-insert-hline org-table-insert-row org-table-iterate
3894 org-table-justify-field-maybe org-table-kill-row
3895 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3896 org-table-move-column org-table-move-column-left
3897 org-table-move-column-right org-table-move-row
3898 org-table-move-row-down org-table-move-row-up
3899 org-table-next-field org-table-next-row org-table-paste-rectangle
3900 org-table-previous-field org-table-recalculate
3901 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3902 org-table-toggle-coordinate-overlays
3903 org-table-toggle-formula-debugger org-table-wrap-region
3904 orgtbl-mode turn-on-orgtbl org-table-to-lisp
3905 orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
3906 orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo
)))
3908 (defun org-at-table-p (&optional table-type
)
3909 "Return t if the cursor is inside an org-type table.
3910 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3911 (if org-enable-table-editor
3913 (beginning-of-line 1)
3914 (looking-at (if table-type org-table-any-line-regexp
3915 org-table-line-regexp
)))
3917 (defsubst org-table-p
() (org-at-table-p))
3919 (defun org-at-table.el-p
()
3920 "Return t if and only if we are at a table.el table."
3921 (and (org-at-table-p 'any
)
3923 (goto-char (org-table-begin 'any
))
3924 (looking-at org-table1-hline-regexp
))))
3925 (defun org-table-recognize-table.el
()
3926 "If there is a table.el table nearby, recognize it and move into it."
3927 (if org-table-tab-recognizes-table.el
3928 (if (org-at-table.el-p
)
3930 (beginning-of-line 1)
3931 (if (looking-at org-table-dataline-regexp
)
3933 (if (looking-at org-table1-hline-regexp
)
3935 (beginning-of-line 2)
3936 (if (looking-at org-table-any-border-regexp
)
3937 (beginning-of-line -
1)))))
3938 (if (re-search-forward "|" (org-table-end t
) t
)
3941 (if (table--at-cell-p (point))
3943 (message "recognizing table.el table...")
3944 (table-recognize-table)
3945 (message "recognizing table.el table...done")))
3946 (error "This should not happen"))
3951 (defun org-at-table-hline-p ()
3952 "Return t if the cursor is inside a hline in a table."
3953 (if org-enable-table-editor
3955 (beginning-of-line 1)
3956 (looking-at org-table-hline-regexp
))
3959 (defvar org-table-clean-did-remove-column nil
)
3961 (defun org-table-map-tables (function &optional quietly
)
3962 "Apply FUNCTION to the start of all tables in the buffer."
3966 (goto-char (point-min))
3967 (while (re-search-forward org-table-any-line-regexp nil t
)
3969 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
3970 (beginning-of-line 1)
3971 (when (and (looking-at org-table-line-regexp
)
3972 ;; Exclude tables in src/example/verbatim/clocktable blocks
3973 (not (org-in-block-p '("src" "example"))))
3974 (save-excursion (funcall function
))
3975 (or (looking-at org-table-line-regexp
)
3977 (re-search-forward org-table-any-border-regexp nil
1))))
3978 (unless quietly
(message "Mapping tables: done")))
3980 ;; Declare and autoload functions from org-exp.el & Co
3982 (declare-function org-default-export-plist
"org-exp")
3983 (declare-function org-infile-export-plist
"org-exp")
3984 (declare-function org-get-current-options
"org-exp")
3986 (org-autoload "org-exp"
3987 '(org-export org-export-visible
3988 org-insert-export-options-template
3989 org-table-clean-before-export
))
3990 (org-autoload "org-ascii"
3991 '(org-export-as-ascii org-export-ascii-preprocess
3992 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3993 org-export-region-as-ascii
))
3994 (org-autoload "org-latex"
3995 '(org-export-as-latex-batch org-export-as-latex-to-buffer
3996 org-replace-region-by-latex org-export-region-as-latex
3997 org-export-as-latex org-export-as-pdf
3998 org-export-as-pdf-and-open
))
3999 (org-autoload "org-html"
4000 '(org-export-as-html-and-open
4001 org-export-as-html-batch org-export-as-html-to-buffer
4002 org-replace-region-by-html org-export-region-as-html
4003 org-export-as-html
))
4004 (org-autoload "org-docbook"
4005 '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
4006 org-replace-region-by-docbook org-export-region-as-docbook
4007 org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
4008 org-export-as-docbook
))
4009 (org-autoload "org-icalendar"
4010 '(org-export-icalendar-this-file
4011 org-export-icalendar-all-agenda-files
4012 org-export-icalendar-combine-agenda-files
))
4013 (org-autoload "org-xoxo" '(org-export-as-xoxo))
4014 (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning
)))
4016 ;; Declare and autoload functions from org-agenda.el
4019 (org-autoload "org-agenda"
4020 '(org-agenda org-agenda-list org-search-view
4021 org-todo-list org-tags-view org-agenda-list-stuck-projects
4022 org-diary org-agenda-to-appt
4023 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
)))
4025 ;; Autoload org-remember
4028 (org-autoload "org-remember"
4029 '(org-remember-insinuate org-remember-annotation
4030 org-remember-apply-template org-remember org-remember-handler
)))
4033 (org-autoload "org-capture"
4034 '(org-capture org-capture-insert-template-here
4035 org-capture-import-remember-templates
)))
4037 ;; Autoload org-clock.el
4039 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
4041 (declare-function org-clock-update-mode-line
"org-clock" ())
4042 (declare-function org-resolve-clocks
"org-clock"
4043 (&optional also-non-dangling-p prompt last-valid
))
4044 (defvar org-clock-start-time
)
4045 (defvar org-clock-marker
(make-marker)
4046 "Marker recording the last clock-in.")
4047 (defvar org-clock-hd-marker
(make-marker)
4048 "Marker recording the last clock-in, but the headline position.")
4049 (defvar org-clock-heading
""
4050 "The heading of the current clock entry.")
4051 (defun org-clock-is-active ()
4052 "Return non-nil if clock is currently running.
4053 The return value is actually the clock marker."
4054 (marker-buffer org-clock-marker
))
4059 '(org-clock-in org-clock-out org-clock-cancel
4060 org-clock-goto org-clock-sum org-clock-display
4061 org-clock-remove-overlays org-clock-report
4062 org-clocktable-shift org-dblock-write
:clocktable
4063 org-get-clocktable org-resolve-clocks
)))
4065 (defun org-clock-update-time-maybe ()
4066 "If this is a CLOCK line, update it and return t.
4067 Otherwise, return nil."
4070 (beginning-of-line 1)
4071 (skip-chars-forward " \t")
4072 (when (looking-at org-clock-string
)
4073 (let ((re (concat "[ \t]*" org-clock-string
4074 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
4075 "\\([ \t]*=>.*\\)?\\)?"))
4078 ((not (looking-at re
))
4080 ((not (match-end 2))
4081 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4082 (> org-clock-marker
(point))
4083 (<= org-clock-marker
(point-at-eol)))
4084 ;; The clock is running here
4085 (setq org-clock-start-time
4087 (org-parse-time-string (match-string 1))))
4088 (org-clock-update-mode-line)))
4090 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
4092 (setq ts
(match-string 1)
4093 te
(match-string 3))
4094 (setq s
(- (org-float-time
4095 (apply 'encode-time
(org-parse-time-string te
)))
4097 (apply 'encode-time
(org-parse-time-string ts
))))
4100 h
(floor (/ s
3600))
4104 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
4107 (defun org-check-running-clock ()
4108 "Check if the current buffer contains the running clock.
4109 If yes, offer to stop it and to save the buffer with the changes."
4110 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4111 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4114 (when (y-or-n-p "Save changed buffer?")
4117 (defun org-clocktable-try-shift (dir n
)
4118 "Check if this line starts a clock table, if yes, shift the time block."
4119 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4120 (org-clocktable-shift dir n
)))
4122 ;; Autoload org-timer.el
4127 '(org-timer-start org-timer org-timer-item
4128 org-timer-change-times-in-region
4130 org-timer-reset-timers
4131 org-timer-show-remaining-time
)))
4133 ;; Autoload org-feed.el
4138 '(org-feed-update org-feed-update-all org-feed-goto-inbox
)))
4141 ;; Autoload org-indent.el
4143 ;; Define the variable already here, to make sure we have it.
4144 (defvar org-indent-mode nil
4145 "Non-nil if Org-Indent mode is enabled.
4146 Use the command `org-indent-mode' to change this variable.")
4151 '(org-indent-mode)))
4153 ;; Autoload org-mobile.el
4158 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda
)))
4160 ;; Autoload archiving code
4161 ;; The stuff that is needed for cycling and tags has to be defined here.
4163 (defgroup org-archive nil
4164 "Options concerning archiving in Org-mode."
4166 :group
'org-structure
)
4168 (defcustom org-archive-location
"%s_archive::"
4169 "The location where subtrees should be archived.
4171 The value of this variable is a string, consisting of two parts,
4172 separated by a double-colon. The first part is a filename and
4173 the second part is a headline.
4175 When the filename is omitted, archiving happens in the same file.
4176 %s in the filename will be replaced by the current file
4177 name (without the directory part). Archiving to a different file
4178 is useful to keep archived entries from contributing to the
4181 The archived entries will be filed as subtrees of the specified
4182 headline. When the headline is omitted, the subtrees are simply
4183 filed away at the end of the file, as top-level entries. Also in
4184 the heading you can use %s to represent the file name, this can be
4185 useful when using the same archive for a number of different files.
4187 Here are a few examples:
4189 If the current file is Projects.org, archive in file
4190 Projects.org_archive, as top-level trees. This is the default.
4192 \"::* Archived Tasks\"
4193 Archive in the current file, under the top-level headline
4194 \"* Archived Tasks\".
4196 \"~/org/archive.org::\"
4197 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4199 \"~/org/archive.org::* From %s\"
4200 Archive in file ~/org/archive.org (absolute path), under headlines
4201 \"From FILENAME\" where file name is the current file name.
4203 \"~/org/datetree.org::datetree/* Finished Tasks\"
4204 The \"datetree/\" string is special, signifying to archive
4205 items to the datetree. Items are placed in either the CLOSED
4206 date of the item, or the current date if there is no CLOSED date.
4207 The heading will be a subentry to the current date. There doesn't
4208 need to be a heading, but there always needs to be a slash after
4209 datetree. For example, to store archived items directly in the
4210 datetree, use \"~/org/datetree.org::datetree/\".
4212 \"basement::** Finished Tasks\"
4213 Archive in file ./basement (relative path), as level 3 trees
4214 below the level 2 heading \"** Finished Tasks\".
4216 You may set this option on a per-file basis by adding to the buffer a
4219 #+ARCHIVE: basement::** Finished Tasks
4221 You may also define it locally for a subtree by setting an ARCHIVE property
4222 in the entry. If such a property is found in an entry, or anywhere up
4223 the hierarchy, it will be used."
4227 (defcustom org-archive-tag
"ARCHIVE"
4228 "The tag that marks a subtree as archived.
4229 An archived subtree does not open during visibility cycling, and does
4230 not contribute to the agenda listings.
4231 After changing this, font-lock must be restarted in the relevant buffers to
4232 get the proper fontification."
4234 :group
'org-keywords
4237 (defcustom org-agenda-skip-archived-trees t
4238 "Non-nil means the agenda will skip any items located in archived trees.
4239 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4240 variable is no longer recommended, you should leave it at the value t.
4241 Instead, use the key `v' to cycle the archives-mode in the agenda."
4243 :group
'org-agenda-skip
4246 (defcustom org-columns-skip-archived-trees t
4247 "Non-nil means ignore archived trees when creating column view."
4249 :group
'org-properties
4252 (defcustom org-cycle-open-archived-trees nil
4253 "Non-nil means `org-cycle' will open archived trees.
4254 An archived tree is a tree marked with the tag ARCHIVE.
4255 When nil, archived trees will stay folded. You can still open them with
4256 normal outline commands like `show-all', but not with the cycling commands."
4261 (defcustom org-sparse-tree-open-archived-trees nil
4262 "Non-nil means sparse tree construction shows matches in archived trees.
4263 When nil, matches in these trees are highlighted, but the trees are kept in
4266 :group
'org-sparse-trees
4269 (defcustom org-sparse-tree-default-date-type
'scheduled-or-deadline
4270 "The default date type when building a sparse tree.
4271 When this is nil, a date is a scheduled or a deadline timestamp.
4272 Otherwise, these types are allowed:
4275 active: only active timestamps (<...>)
4276 inactive: only inactive timestamps (<...)
4277 scheduled: only scheduled timestamps
4278 deadline: only deadline timestamps"
4279 :type
'(choice (const :tag
"Scheduled or deadline" 'scheduled-or-deadline
)
4280 (const :tag
"All timestamps" all
)
4281 (const :tag
"Only active timestamps" active
)
4282 (const :tag
"Only inactive timestamps" inactive
)
4283 (const :tag
"Only scheduled timestamps" scheduled
)
4284 (const :tag
"Only deadline timestamps" deadline
))
4285 :group
'org-sparse-trees
4288 (defun org-cycle-hide-archived-subtrees (state)
4289 "Re-hide all archived subtrees after a visibility state change."
4290 (when (and (not org-cycle-open-archived-trees
)
4291 (not (memq state
'(overview folded
))))
4293 (let* ((globalp (memq state
'(contents all
)))
4294 (beg (if globalp
(point-min) (point)))
4295 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4296 (org-hide-archived-subtrees beg end
)
4298 (if (looking-at (concat ".*:" org-archive-tag
":"))
4299 (message "%s" (substitute-command-keys
4300 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4302 (defun org-force-cycle-archived ()
4303 "Cycle subtree even if it is archived."
4305 (setq this-command
'org-cycle
)
4306 (let ((org-cycle-open-archived-trees t
))
4307 (call-interactively 'org-cycle
)))
4309 (defun org-hide-archived-subtrees (beg end
)
4310 "Re-hide all archived subtrees after a visibility state change."
4312 (let* ((re (concat ":" org-archive-tag
":")))
4314 (while (re-search-forward re end t
)
4315 (when (org-at-heading-p)
4316 (org-flag-subtree t
)
4317 (org-end-of-subtree t
))))))
4319 (defun org-flag-subtree (flag)
4321 (org-back-to-heading t
)
4322 (outline-end-of-heading)
4323 (outline-flag-region (point)
4324 (progn (org-end-of-subtree t
) (point))
4327 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4330 (org-autoload "org-archive"
4331 '(org-add-archive-files org-archive-subtree
4332 org-archive-to-archive-sibling org-toggle-archive-tag
4333 org-archive-subtree-default
4334 org-archive-subtree-default-with-confirmation
)))
4336 ;; Autoload Column View Code
4338 (declare-function org-columns-number-to-string
"org-colview")
4339 (declare-function org-columns-get-format-and-top-level
"org-colview")
4340 (declare-function org-columns-compute
"org-colview")
4342 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4343 '(org-columns-number-to-string org-columns-get-format-and-top-level
4344 org-columns-compute org-agenda-columns org-columns-remove-overlays
4345 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
4349 (declare-function org-id-store-link
"org-id")
4350 (declare-function org-id-locations-load
"org-id")
4351 (declare-function org-id-locations-save
"org-id")
4352 (defvar org-id-track-globally
)
4353 (org-autoload "org-id"
4354 '(org-id-get-create org-id-new org-id-copy org-id-get
4355 org-id-get-with-outline-path-completion
4356 org-id-get-with-outline-drilling org-id-store-link
4357 org-id-goto org-id-find org-id-store-link
))
4359 ;; Autoload Plotting Code
4361 (org-autoload "org-plot"
4362 '(org-plot/gnuplot
))
4364 ;;; Variables for pre-computed regular expressions, all buffer local
4366 (defvar org-drawer-regexp
"^[ \t]*:PROPERTIES:[ \t]*$"
4367 "Matches first line of a hidden block.")
4368 (make-variable-buffer-local 'org-drawer-regexp
)
4369 (defvar org-todo-regexp nil
4370 "Matches any of the TODO state keywords.")
4371 (make-variable-buffer-local 'org-todo-regexp
)
4372 (defvar org-not-done-regexp nil
4373 "Matches any of the TODO state keywords except the last one.")
4374 (make-variable-buffer-local 'org-not-done-regexp
)
4375 (defvar org-not-done-heading-regexp nil
4376 "Matches a TODO headline that is not done.")
4377 (make-variable-buffer-local 'org-not-done-regexp
)
4378 (defvar org-todo-line-regexp nil
4379 "Matches a headline and puts TODO state into group 2 if present.")
4380 (make-variable-buffer-local 'org-todo-line-regexp
)
4381 (defvar org-complex-heading-regexp nil
4382 "Matches a headline and puts everything into groups:
4384 group 2: The todo keyword, maybe
4385 group 3: Priority cookie
4386 group 4: True headline
4388 (make-variable-buffer-local 'org-complex-heading-regexp
)
4389 (defvar org-complex-heading-regexp-format nil
4390 "Printf format to make regexp to match an exact headline.
4391 This regexp will match the headline of any node which has the
4392 exact headline text that is put into the format, but may have any
4393 TODO state, priority and tags.")
4394 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4395 (defvar org-todo-line-tags-regexp nil
4396 "Matches a headline and puts TODO state into group 2 if present.
4397 Also put tags into group 4 if tags are present.")
4398 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4399 (defvar org-ds-keyword-length
12
4400 "Maximum length of the Deadline and SCHEDULED keywords.")
4401 (make-variable-buffer-local 'org-ds-keyword-length
)
4402 (defvar org-deadline-regexp nil
4403 "Matches the DEADLINE keyword.")
4404 (make-variable-buffer-local 'org-deadline-regexp
)
4405 (defvar org-deadline-time-regexp nil
4406 "Matches the DEADLINE keyword together with a time stamp.")
4407 (make-variable-buffer-local 'org-deadline-time-regexp
)
4408 (defvar org-deadline-line-regexp nil
4409 "Matches the DEADLINE keyword and the rest of the line.")
4410 (make-variable-buffer-local 'org-deadline-line-regexp
)
4411 (defvar org-scheduled-regexp nil
4412 "Matches the SCHEDULED keyword.")
4413 (make-variable-buffer-local 'org-scheduled-regexp
)
4414 (defvar org-scheduled-time-regexp nil
4415 "Matches the SCHEDULED keyword together with a time stamp.")
4416 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4417 (defvar org-closed-time-regexp nil
4418 "Matches the CLOSED keyword together with a time stamp.")
4419 (make-variable-buffer-local 'org-closed-time-regexp
)
4421 (defvar org-keyword-time-regexp nil
4422 "Matches any of the 4 keywords, together with the time stamp.")
4423 (make-variable-buffer-local 'org-keyword-time-regexp
)
4424 (defvar org-keyword-time-not-clock-regexp nil
4425 "Matches any of the 3 keywords, together with the time stamp.")
4426 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4427 (defvar org-maybe-keyword-time-regexp nil
4428 "Matches a timestamp, possibly preceded by a keyword.")
4429 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4430 (defvar org-all-time-keywords nil
4431 "List of time keywords.")
4432 (make-variable-buffer-local 'org-all-time-keywords
)
4434 (defconst org-plain-time-of-day-regexp
4437 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4440 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4442 "Regular expression to match a plain time or time range.
4443 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4444 groups carry important information:
4446 1 the first time, range or not
4447 8 the second time, if it is a range.")
4449 (defconst org-plain-time-extension-regexp
4452 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4453 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4454 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4455 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4456 groups carry important information:
4459 9 minutes of duration")
4461 (defconst org-stamp-time-of-day-regexp
4463 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4464 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4466 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4467 "Regular expression to match a timestamp time or time range.
4468 After a match, the following groups carry important information:
4470 1 date plus weekday, for back referencing to make sure both times are on the same day
4471 2 the first time, range or not
4472 4 the second time, if it is a range.")
4474 (defconst org-startup-options
4475 '(("fold" org-startup-folded t
)
4476 ("overview" org-startup-folded t
)
4477 ("nofold" org-startup-folded nil
)
4478 ("showall" org-startup-folded nil
)
4479 ("showeverything" org-startup-folded showeverything
)
4480 ("content" org-startup-folded content
)
4481 ("indent" org-startup-indented t
)
4482 ("noindent" org-startup-indented nil
)
4483 ("hidestars" org-hide-leading-stars t
)
4484 ("showstars" org-hide-leading-stars nil
)
4485 ("odd" org-odd-levels-only t
)
4486 ("oddeven" org-odd-levels-only nil
)
4487 ("align" org-startup-align-all-tables t
)
4488 ("noalign" org-startup-align-all-tables nil
)
4489 ("inlineimages" org-startup-with-inline-images t
)
4490 ("noinlineimages" org-startup-with-inline-images nil
)
4491 ("customtime" org-display-custom-times t
)
4492 ("logdone" org-log-done time
)
4493 ("lognotedone" org-log-done note
)
4494 ("nologdone" org-log-done nil
)
4495 ("lognoteclock-out" org-log-note-clock-out t
)
4496 ("nolognoteclock-out" org-log-note-clock-out nil
)
4497 ("logrepeat" org-log-repeat state
)
4498 ("lognoterepeat" org-log-repeat note
)
4499 ("nologrepeat" org-log-repeat nil
)
4500 ("logreschedule" org-log-reschedule time
)
4501 ("lognotereschedule" org-log-reschedule note
)
4502 ("nologreschedule" org-log-reschedule nil
)
4503 ("logredeadline" org-log-redeadline time
)
4504 ("lognoteredeadline" org-log-redeadline note
)
4505 ("nologredeadline" org-log-redeadline nil
)
4506 ("logrefile" org-log-refile time
)
4507 ("lognoterefile" org-log-refile note
)
4508 ("nologrefile" org-log-refile nil
)
4509 ("fninline" org-footnote-define-inline t
)
4510 ("nofninline" org-footnote-define-inline nil
)
4511 ("fnlocal" org-footnote-section nil
)
4512 ("fnauto" org-footnote-auto-label t
)
4513 ("fnprompt" org-footnote-auto-label nil
)
4514 ("fnconfirm" org-footnote-auto-label confirm
)
4515 ("fnplain" org-footnote-auto-label plain
)
4516 ("fnadjust" org-footnote-auto-adjust t
)
4517 ("nofnadjust" org-footnote-auto-adjust nil
)
4518 ("constcgs" constants-unit-system cgs
)
4519 ("constSI" constants-unit-system SI
)
4520 ("noptag" org-tag-persistent-alist nil
)
4521 ("hideblocks" org-hide-block-startup t
)
4522 ("nohideblocks" org-hide-block-startup nil
)
4523 ("beamer" org-startup-with-beamer-mode t
)
4524 ("entitiespretty" org-pretty-entities t
)
4525 ("entitiesplain" org-pretty-entities nil
))
4526 "Variable associated with STARTUP options for org-mode.
4527 Each element is a list of three items: the startup options (as written
4528 in the #+STARTUP line), the corresponding variable, and the value to set
4529 this variable to if the option is found. An optional forth element PUSH
4530 means to push this value onto the list in the variable.")
4532 (defun org-update-property-plist (key val props
)
4533 "Update PROPS with KEY and VAL."
4534 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4535 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4536 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4537 (previous (cdr (assoc key props
))))
4539 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4540 (cons (cons key val
) remainder
))))
4542 (defconst org-block-regexp
4543 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4544 "Regular expression for hiding blocks.")
4545 (defconst org-heading-keyword-regexp-format
4546 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4547 "Printf format for a regexp matching an headline with some keyword.
4548 This regexp will match the headline of any node which has the
4549 exact keyword that is put into the format. The keyword isn't in
4550 any group by default, but the stars and the body are.")
4551 (defconst org-heading-keyword-maybe-regexp-format
4552 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4553 "Printf format for a regexp matching an headline, possibly with some keyword.
4554 This regexp can match any headline with the specified keyword, or
4555 without a keyword. The keyword isn't in any group by default,
4556 but the stars and the body are.")
4558 (defun org-set-regexps-and-options ()
4559 "Precompute regular expressions for current buffer."
4560 (when (derived-mode-p 'org-mode
)
4561 (org-set-local 'org-todo-kwd-alist nil
)
4562 (org-set-local 'org-todo-key-alist nil
)
4563 (org-set-local 'org-todo-key-trigger nil
)
4564 (org-set-local 'org-todo-keywords-1 nil
)
4565 (org-set-local 'org-done-keywords nil
)
4566 (org-set-local 'org-todo-heads nil
)
4567 (org-set-local 'org-todo-sets nil
)
4568 (org-set-local 'org-todo-log-states nil
)
4569 (org-set-local 'org-file-properties nil
)
4570 (org-set-local 'org-file-tags nil
)
4571 (let ((re (org-make-options-regexp
4572 '("CATEGORY" "TODO" "COLUMNS"
4573 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
4574 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
4576 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4578 (scripts org-use-sub-superscripts
)
4579 kwds kws0 kwsa key log value cat arch tags const links hw dws
4580 tail sep kws1 prio props ftags drawers beamer-p
4581 ext-setup-or-nil setup-contents
(start 0))
4585 (goto-char (point-min))
4586 (while (or (and ext-setup-or-nil
4587 (string-match re ext-setup-or-nil start
)
4588 (setq start
(match-end 0)))
4589 (and (setq ext-setup-or-nil nil start
0)
4590 (re-search-forward re nil t
)))
4591 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4592 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4593 (if (stringp value
) (setq value
(org-trim value
)))
4595 ((equal key
"CATEGORY")
4597 ((member key
'("SEQ_TODO" "TODO"))
4598 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4599 ((equal key
"TYP_TODO")
4600 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4601 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4602 ;; general TODO-like setup
4603 (push (cons (intern (downcase (match-string 1 key
)))
4604 (org-split-string value splitre
)) kwds
))
4606 (setq tags
(append tags
(if tags
'("\\n") nil
)
4607 (org-split-string value splitre
))))
4608 ((equal key
"COLUMNS")
4609 (org-set-local 'org-columns-default-format value
))
4611 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4612 (push (cons (match-string 1 value
)
4613 (org-trim (match-string 2 value
)))
4615 ((equal key
"PRIORITIES")
4616 (setq prio
(org-split-string value
" +")))
4617 ((equal key
"PROPERTY")
4618 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4619 (setq props
(org-update-property-plist (match-string 1 value
)
4620 (match-string 2 value
)
4622 ((equal key
"FILETAGS")
4623 (when (string-match "\\S-" value
)
4628 (mapcar (lambda (x) (org-split-string x
":"))
4629 (org-split-string value
)))))))
4630 ((equal key
"DRAWERS")
4631 (setq drawers
(delete-dups (append org-drawers
(org-split-string value splitre
)))))
4632 ((equal key
"CONSTANTS")
4633 (setq const
(append const
(org-split-string value splitre
))))
4634 ((equal key
"STARTUP")
4635 (let ((opts (org-split-string value splitre
))
4637 (while (setq l
(pop opts
))
4638 (when (setq l
(assoc l org-startup-options
))
4639 (setq var
(nth 1 l
) val
(nth 2 l
))
4641 (set (make-local-variable var
) val
)
4642 (if (not (listp (symbol-value var
)))
4643 (set (make-local-variable var
) nil
))
4644 (set (make-local-variable var
) (symbol-value var
))
4645 (add-to-list var val
))))))
4646 ((equal key
"ARCHIVE")
4648 (remove-text-properties 0 (length arch
)
4649 '(face t fontified t
) arch
))
4650 ((equal key
"LATEX_CLASS")
4651 (setq beamer-p
(equal value
"beamer")))
4652 ((equal key
"OPTIONS")
4653 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4654 (setq scripts
(read (match-string 2 value
)))))
4655 ((equal key
"SETUPFILE")
4656 (setq setup-contents
(org-file-contents
4658 (org-remove-double-quotes value
))
4660 (if (not ext-setup-or-nil
)
4661 (setq ext-setup-or-nil setup-contents start
0)
4662 (setq ext-setup-or-nil
4663 (concat (substring ext-setup-or-nil
0 start
)
4664 "\n" setup-contents
"\n"
4665 (substring ext-setup-or-nil start
)))))))
4666 ;; search for property blocks
4667 (goto-char (point-min))
4668 (while (re-search-forward org-block-regexp nil t
)
4669 (when (equal "PROPERTY" (upcase (match-string 1)))
4670 (setq value
(replace-regexp-in-string
4671 "[\n\r]" " " (match-string 4)))
4672 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4673 (setq props
(org-update-property-plist (match-string 1 value
)
4674 (match-string 2 value
)
4676 (org-set-local 'org-use-sub-superscripts scripts
)
4678 (org-set-local 'org-category
(intern cat
))
4679 (push (cons "CATEGORY" cat
) props
))
4681 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4682 (setq prio
(mapcar 'string-to-char prio
))
4683 (org-set-local 'org-highest-priority
(nth 0 prio
))
4684 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4685 (org-set-local 'org-default-priority
(nth 2 prio
)))
4686 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4687 (and ftags
(org-set-local 'org-file-tags
4688 (mapcar 'org-add-prop-inherited ftags
)))
4689 (and drawers
(org-set-local 'org-drawers drawers
))
4690 (and arch
(org-set-local 'org-archive-location arch
))
4691 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4692 ;; Process the TODO keywords
4694 ;; Use the global values as if they had been given locally.
4695 (setq kwds
(default-value 'org-todo-keywords
))
4696 (if (stringp (car kwds
))
4697 (setq kwds
(list (cons org-todo-interpretation
4698 (default-value 'org-todo-keywords
)))))
4699 (setq kwds
(reverse kwds
)))
4700 (setq kwds
(nreverse kwds
))
4702 (while (setq kws
(pop kwds
))
4704 (run-hook-with-args-until-success
4705 'org-todo-setup-filter-hook kws
)
4707 (setq inter
(pop kws
) sep
(member "|" kws
)
4708 kws0
(delete "|" (copy-sequence kws
))
4713 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4715 (setq kw
(match-string 1 x
)
4716 key
(and (match-end 2) (match-string 2 x
))
4717 log
(org-extract-log-state-settings x
))
4718 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4719 (and log
(push log org-todo-log-states
))
4721 (error "Invalid TODO keyword %s" x
)))
4723 kwsa
(if kwsa
(append '((:startgroup
))
4727 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4728 tail
(list inter hw
(car dws
) (org-last dws
))))
4729 (add-to-list 'org-todo-heads hw
'append
)
4730 (push kws1 org-todo-sets
)
4731 (setq org-done-keywords
(append org-done-keywords dws nil
))
4732 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4733 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4734 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4735 (setq org-todo-sets
(nreverse org-todo-sets
)
4736 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4737 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4738 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4739 ;; Process the constants
4742 (while (setq e
(pop const
))
4743 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4744 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4745 (setq org-table-formula-constants-local cst
)))
4747 ;; Process the tags.
4750 (while (setq e
(pop tags
))
4752 ((equal e
"{") (push '(:startgroup
) tgs
))
4753 ((equal e
"}") (push '(:endgroup
) tgs
))
4754 ((equal e
"\\n") (push '(:newline
) tgs
))
4755 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4756 (push (cons (match-string 1 e
)
4757 (string-to-char (match-string 2 e
)))
4759 (t (push (list e
) tgs
))))
4760 (org-set-local 'org-tag-alist nil
)
4761 (while (setq e
(pop tgs
))
4762 (or (and (stringp (car e
))
4763 (assoc (car e
) org-tag-alist
))
4764 (push e org-tag-alist
)))))
4766 ;; Compute the regular expressions and other local variables.
4767 ;; Using `org-outline-regexp-bol' would complicate them much,
4768 ;; because of the fixed white space at the end of that string.
4769 (if (not org-done-keywords
)
4770 (setq org-done-keywords
(and org-todo-keywords-1
4771 (list (org-last org-todo-keywords-1
)))))
4772 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4773 (length org-scheduled-string
)
4774 (length org-clock-string
)
4775 (length org-closed-string
)))
4777 (concat "^[ \t]*:\\("
4778 (mapconcat 'regexp-quote org-drawers
"\\|")
4780 org-not-done-keywords
4781 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4784 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4788 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4790 org-not-done-heading-regexp
4791 (format org-heading-keyword-regexp-format org-not-done-regexp
)
4792 org-todo-line-regexp
4793 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
4794 org-complex-heading-regexp
4795 (concat "^\\(\\*+\\)"
4796 "\\(?: +" org-todo-regexp
"\\)?"
4797 "\\(?: +\\(\\[#.\\]\\)\\)?"
4798 "\\(?: +\\(.*?\\)\\)?"
4799 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
4801 org-complex-heading-regexp-format
4802 (concat "^\\(\\*+\\)"
4803 "\\(?: +" org-todo-regexp
"\\)?"
4804 "\\(?: +\\(\\[#.\\]\\)\\)?"
4806 ;; Stats cookies can be stuck to body.
4807 "\\(?:\\[[0-9%%/]+\\] *\\)?"
4809 "\\(?: *\\[[0-9%%/]+\\]\\)?"
4811 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
4813 org-todo-line-tags-regexp
4814 (concat "^\\(\\*+\\)"
4815 "\\(?: +" org-todo-regexp
"\\)?"
4816 "\\(?: +\\(.*?\\)\\)?"
4817 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
4819 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4820 org-deadline-time-regexp
4821 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4822 org-deadline-line-regexp
4823 (concat "\\<\\(" org-deadline-string
"\\).*")
4824 org-scheduled-regexp
4825 (concat "\\<" org-scheduled-string
)
4826 org-scheduled-time-regexp
4827 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4828 org-closed-time-regexp
4829 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4830 org-keyword-time-regexp
4831 (concat "\\<\\(" org-scheduled-string
4832 "\\|" org-deadline-string
4833 "\\|" org-closed-string
4834 "\\|" org-clock-string
"\\)"
4835 " *[[<]\\([^]>]+\\)[]>]")
4836 org-keyword-time-not-clock-regexp
4837 (concat "\\<\\(" org-scheduled-string
4838 "\\|" org-deadline-string
4839 "\\|" org-closed-string
4841 " *[[<]\\([^]>]+\\)[]>]")
4842 org-maybe-keyword-time-regexp
4843 (concat "\\(\\<\\(" org-scheduled-string
4844 "\\|" org-deadline-string
4845 "\\|" org-closed-string
4846 "\\|" org-clock-string
"\\)\\)?"
4847 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4848 org-all-time-keywords
4849 (mapcar (lambda (w) (substring w
0 -
1))
4850 (list org-scheduled-string org-deadline-string
4851 org-clock-string org-closed-string
))
4853 (org-compute-latex-and-specials-regexp)
4854 (org-set-font-lock-defaults))))
4856 (defun org-file-contents (file &optional noerror
)
4857 "Return the contents of FILE, as a string."
4859 (not (file-readable-p file
)))
4862 (message "Cannot read file \"%s\"" file
)
4865 (error "Cannot read file \"%s\"" file
))
4867 (insert-file-contents file
)
4870 (defun org-extract-log-state-settings (x)
4871 "Extract the log state setting from a TODO keyword string.
4872 This will extract info from a string like \"WAIT(w@/!)\"."
4873 (let (kw key log1 log2
)
4874 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4875 (setq kw
(match-string 1 x
)
4876 key
(and (match-end 2) (match-string 2 x
))
4877 log1
(and (match-end 3) (match-string 3 x
))
4878 log2
(and (match-end 4) (match-string 4 x
)))
4881 (and log1
(if (equal log1
"!") 'time
'note
))
4882 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4884 (defun org-remove-keyword-keys (list)
4885 "Remove a pair of parenthesis at the end of each string in LIST."
4887 (if (string-match "(.*)$" x
)
4888 (substring x
0 (match-beginning 0))
4892 (defun org-assign-fast-keys (alist)
4893 "Assign fast keys to a keyword-key alist.
4894 Respect keys that are already there."
4895 (let (new e
(alt ?
0))
4896 (while (setq e
(pop alist
))
4897 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
4898 (cdr e
)) ;; Key already assigned.
4900 (let ((clist (string-to-list (downcase (car e
))))
4901 (used (append new alist
)))
4902 (when (= (car clist
) ?
@)
4904 (while (and clist
(rassoc (car clist
) used
))
4907 (while (rassoc alt used
)
4909 (push (cons (car e
) (or (car clist
) alt
)) new
))))
4912 ;;; Some variables used in various places
4914 (defvar org-window-configuration nil
4915 "Used in various places to store a window configuration.")
4916 (defvar org-selected-window nil
4917 "Used in various places to store a window configuration.")
4918 (defvar org-finish-function nil
4919 "Function to be called when `C-c C-c' is used.
4920 This is for getting out of special buffers like remember.")
4923 ;; FIXME: Occasionally check by commenting these, to make sure
4924 ;; no other functions uses these, forgetting to let-bind them.
4925 (org-no-warnings (defvar entry
)) ;; unprefixed, from calendar.el
4926 (defvar org-last-state
)
4927 (org-no-warnings (defvar date
)) ;; unprefixed, from calendar.el
4929 ;; Defined somewhere in this file, but used before definition.
4930 (defvar org-entities
) ;; defined in org-entities.el
4931 (defvar org-struct-menu
)
4932 (defvar org-org-menu
)
4933 (defvar org-tbl-menu
)
4935 ;;;; Define the Org-mode
4937 ;; We use a before-change function to check if a table might need
4939 (defvar org-table-may-need-update t
4940 "Indicates that a table might need an update.
4941 This variable is set by `org-before-change-function'.
4942 `org-table-align' sets it back to nil.")
4943 (defun org-before-change-function (beg end
)
4944 "Every change indicates that a table might need an update."
4945 (setq org-table-may-need-update t
))
4946 (defvar org-mode-map
)
4947 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4948 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
4949 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4950 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
4951 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
4952 (defvar org-table-buffer-is-an nil
)
4954 (defvar bidi-paragraph-direction
)
4955 (defvar buffer-face-mode-face
)
4958 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4959 (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22"))
4960 (require 'noutline
"noutline" 'noerror
) ;; stock XEmacs does not have it
4962 ;; Other stuff we need.
4963 (require 'time-date
)
4964 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
4969 (require 'org-entities
)
4970 ;; (require 'org-compat) moved higher up in the file before it is first used
4971 (require 'org-faces
)
4973 (require 'org-pcomplete
)
4975 (require 'org-footnote
)
4976 (require 'org-element
)
4983 (require 'ob-tangle
)
4984 (require 'ob-comint
)
4988 (define-derived-mode org-mode outline-mode
"Org"
4989 "Outline-based notes management and organizer, alias
4990 \"Carsten's outline-mode for keeping track of everything.\"
4992 Org-mode develops organizational tasks around a NOTES file which
4993 contains information about projects as plain text. Org-mode is
4994 implemented on top of outline-mode, which is ideal to keep the content
4995 of large files well structured. It supports ToDo items, deadlines and
4996 time stamps, which magically appear in the diary listing of the Emacs
4997 calendar. Tables are easily created with a built-in table editor.
4998 Plain text URL-like links connect to websites, emails (VM), Usenet
4999 messages (Gnus), BBDB entries, and any files related to the project.
5000 For printing and sharing of notes, an Org-mode file (or a part of it)
5001 can be exported as a structured ASCII or HTML file.
5003 The following commands are available:
5007 ;; Get rid of Outline menus, they are not needed
5008 ;; Need to do this here because define-derived-mode sets up
5009 ;; the keymap so late. Still, it is a waste to call this each time
5010 ;; we switch another buffer into org-mode.
5011 (if (featurep 'xemacs
)
5012 (when (boundp 'outline-mode-menu-heading
)
5013 ;; Assume this is Greg's port, it uses easymenu
5014 (easy-menu-remove outline-mode-menu-heading
)
5015 (easy-menu-remove outline-mode-menu-show
)
5016 (easy-menu-remove outline-mode-menu-hide
))
5017 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
5018 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
5019 (define-key org-mode-map
[menu-bar show
] 'undefined
))
5021 (org-load-modules-maybe)
5022 (easy-menu-add org-org-menu
)
5023 (easy-menu-add org-tbl-menu
)
5024 (org-install-agenda-files-menu)
5025 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
5026 (add-to-invisibility-spec '(org-cwidth))
5027 (add-to-invisibility-spec '(org-hide-block . t
))
5028 (when (featurep 'xemacs
)
5029 (org-set-local 'line-move-ignore-invisible t
))
5030 (org-set-local 'outline-regexp org-outline-regexp
)
5031 (org-set-local 'outline-level
'org-outline-level
)
5032 (setq bidi-paragraph-direction
'left-to-right
)
5033 (when (and org-ellipsis
5034 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
5035 (fboundp 'make-glyph-code
))
5036 (unless org-display-table
5037 (setq org-display-table
(make-display-table)))
5038 (set-display-table-slot
5041 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
5043 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
5044 (setq buffer-display-table org-display-table
))
5045 (org-set-regexps-and-options)
5046 (when (and org-tag-faces
(not org-tags-special-faces-re
))
5047 ;; tag faces set outside customize.... force initialization.
5048 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
5050 (org-set-local 'calc-embedded-open-mode
"# ")
5051 (modify-syntax-entry ?
@ "w")
5052 (if org-startup-truncated
(setq truncate-lines t
))
5053 (org-set-local 'font-lock-unfontify-region-function
5054 'org-unfontify-region
)
5055 ;; Activate before-change-function
5056 (org-set-local 'org-table-may-need-update t
)
5057 (org-add-hook 'before-change-functions
'org-before-change-function nil
5059 ;; Check for running clock before killing a buffer
5060 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
5061 ;; Paragraphs and auto-filling
5062 (org-set-autofill-regexps)
5063 (org-set-local 'indent-line-function
'org-indent-line
)
5064 (org-set-local 'indent-region-function
'org-indent-region
)
5065 (org-update-radio-target-regexp)
5067 (org-set-local 'comment-use-syntax nil
)
5068 (org-set-local 'comment-start
"#")
5069 (org-set-local 'comment-start-skip
"# ?")
5070 (org-set-local 'comment-insert-comment-function
'org-insert-comment
)
5071 (org-set-local 'comment-region-function
'org-comment-or-uncomment-region
)
5072 (org-set-local 'uncomment-region-function
'org-comment-or-uncomment-region
)
5073 ;; Beginning/end of defun
5074 (org-set-local 'beginning-of-defun-function
'org-beginning-of-defun
)
5075 (org-set-local 'end-of-defun-function
'org-end-of-defun
)
5076 ;; Next error for sparse trees
5077 (org-set-local 'next-error-function
'org-occur-next-match
)
5078 ;; Make sure dependence stuff works reliably, even for users who set it
5080 (if org-enforce-todo-dependencies
5081 (add-hook 'org-blocker-hook
5082 'org-block-todo-from-children-or-siblings-or-parent
)
5083 (remove-hook 'org-blocker-hook
5084 'org-block-todo-from-children-or-siblings-or-parent
))
5085 (if org-enforce-todo-checkbox-dependencies
5086 (add-hook 'org-blocker-hook
5087 'org-block-todo-from-checkboxes
)
5088 (remove-hook 'org-blocker-hook
5089 'org-block-todo-from-checkboxes
))
5091 ;; Align options lines
5093 'align-mode-rules-list
5094 '((org-in-buffer-settings
5095 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
5096 (modes .
'(org-mode)))))
5099 (org-set-local 'imenu-create-index-function
5100 'org-imenu-get-tree
)
5102 ;; Make isearch reveal context
5103 (if (or (featurep 'xemacs
)
5104 (not (boundp 'outline-isearch-open-invisible-function
)))
5105 ;; Emacs 21 and XEmacs make use of the hook
5106 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
5107 ;; Emacs 22 deals with this through a special variable
5108 (org-set-local 'outline-isearch-open-invisible-function
5109 (lambda (&rest ignore
) (org-show-context 'isearch
))))
5111 ;; Turn on org-beamer-mode?
5112 (and org-startup-with-beamer-mode
(org-beamer-mode 1))
5114 ;; Setup the pcomplete hooks
5115 (set (make-local-variable 'pcomplete-command-completion-function
)
5116 'org-pcomplete-initial
)
5117 (set (make-local-variable 'pcomplete-command-name-function
)
5118 'org-command-at-point
)
5119 (set (make-local-variable 'pcomplete-default-completion-function
)
5121 (set (make-local-variable 'pcomplete-parse-arguments-function
)
5122 'org-parse-arguments
)
5123 (set (make-local-variable 'pcomplete-termination-string
) "")
5124 (when (>= emacs-major-version
23)
5125 (set (make-local-variable 'buffer-face-mode-face
) 'org-default
))
5127 ;; If empty file that did not turn on org-mode automatically, make it to.
5128 (if (and org-insert-mode-line-in-empty-file
5129 (org-called-interactively-p 'any
)
5130 (= (point-min) (point-max)))
5131 (insert "# -*- mode: org -*-\n\n"))
5132 (unless org-inhibit-startup
5133 (when org-startup-align-all-tables
5134 (let ((bmp (buffer-modified-p)))
5135 (org-table-map-tables 'org-table-align
'quietly
)
5136 (set-buffer-modified-p bmp
)))
5137 (when org-startup-with-inline-images
5138 (org-display-inline-images))
5139 (when org-startup-indented
5140 (require 'org-indent
)
5141 (org-indent-mode 1))
5142 (unless org-inhibit-startup-visibility-stuff
5143 (org-set-startup-visibility))))
5145 (when (fboundp 'abbrev-table-put
)
5146 (abbrev-table-put org-mode-abbrev-table
5147 :parents
(list text-mode-abbrev-table
)))
5149 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5151 (defun org-current-time ()
5152 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
5153 (if (> (car org-time-stamp-rounding-minutes
) 1)
5154 (let ((r (car org-time-stamp-rounding-minutes
))
5155 (time (decode-time)))
5157 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5162 "Return today date, considering `org-extend-today-until'."
5164 (time-subtract (current-time)
5165 (list 0 (* 3600 org-extend-today-until
) 0))))
5167 ;;;; Font-Lock stuff, including the activators
5169 (defvar org-mouse-map
(make-sparse-keymap))
5170 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5171 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5172 (when org-mouse-1-follows-link
5173 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5174 (when org-tab-follows-link
5175 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5176 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5178 (require 'font-lock
)
5180 (defconst org-non-link-chars
"]\t\n\r<>")
5181 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5182 "shell" "elisp" "doi" "message"))
5183 (defvar org-link-types-re nil
5184 "Matches a link that has a url-like prefix like \"http:\"")
5185 (defvar org-link-re-with-space nil
5186 "Matches a link with spaces, optional angular brackets around it.")
5187 (defvar org-link-re-with-space2 nil
5188 "Matches a link with spaces, optional angular brackets around it.")
5189 (defvar org-link-re-with-space3 nil
5190 "Matches a link with spaces, only for internal part in bracket links.")
5191 (defvar org-angle-link-re nil
5192 "Matches link with angular brackets, spaces are allowed.")
5193 (defvar org-plain-link-re nil
5194 "Matches plain link, without spaces.")
5195 (defvar org-bracket-link-regexp nil
5196 "Matches a link in double brackets.")
5197 (defvar org-bracket-link-analytic-regexp nil
5198 "Regular expression used to analyze links.
5199 Here is what the match groups contain after a match:
5205 (defvar org-bracket-link-analytic-regexp
++ nil
5206 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5207 (defvar org-any-link-re nil
5208 "Regular expression matching any link.")
5210 (defcustom org-match-sexp-depth
3
5211 "Number of stacked braces for sub/superscript matching.
5212 This has to be set before loading org.el to be effective."
5213 :group
'org-export-translation
; ??????????????????????????/
5216 (defun org-create-multibrace-regexp (left right n
)
5217 "Create a regular expression which will match a balanced sexp.
5218 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5219 as single character strings.
5220 The regexp returned will match the entire expression including the
5221 delimiters. It will also define a single group which contains the
5222 match except for the outermost delimiters. The maximum depth of
5223 stacked delimiters is N. Escaping delimiters is not possible."
5224 (let* ((nothing (concat "[^" left right
"]*?"))
5227 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5230 re
(concat re or next
)
5231 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5232 (concat left
"\\(" re
"\\)" right
)))
5234 (defvar org-match-substring-regexp
5236 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5237 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5239 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5241 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5242 "The regular expression matching a sub- or superscript.")
5244 (defvar org-match-substring-with-braces-regexp
5246 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5247 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5249 "The regular expression matching a sub- or superscript, forcing braces.")
5251 (defun org-make-link-regexps ()
5252 "Update the link regular expressions.
5253 This should be called after the variable `org-link-types' has changed."
5254 (setq org-link-types-re
5256 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
5257 org-link-re-with-space
5259 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5260 "\\([^" org-non-link-chars
" ]"
5261 "[^" org-non-link-chars
"]*"
5262 "[^" org-non-link-chars
" ]\\)>?")
5263 org-link-re-with-space2
5265 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5266 "\\([^" org-non-link-chars
" ]"
5268 "[^" org-non-link-chars
" ]\\)>?")
5269 org-link-re-with-space3
5271 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5272 "\\([^" org-non-link-chars
" ]"
5276 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5277 "\\([^" org-non-link-chars
" ]"
5278 "[^" org-non-link-chars
"]*"
5282 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5283 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5284 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5285 org-bracket-link-regexp
5286 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5287 org-bracket-link-analytic-regexp
5290 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
5293 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5295 org-bracket-link-analytic-regexp
++
5298 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
5301 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5304 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5305 org-angle-link-re
"\\)\\|\\("
5306 org-plain-link-re
"\\)")))
5308 (org-make-link-regexps)
5310 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5311 "Regular expression for fast time stamp matching.")
5312 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5313 "Regular expression for fast time stamp matching.")
5314 (defconst org-ts-regexp0
5315 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5316 "Regular expression matching time strings for analysis.
5317 This one does not require the space after the date, so it can be used
5318 on a string that terminates immediately after the date.")
5319 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5320 "Regular expression matching time strings for analysis.")
5321 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5322 "Regular expression matching time stamps, with groups.")
5323 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5324 "Regular expression matching time stamps (also [..]), with groups.")
5325 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5326 "Regular expression matching a time stamp range.")
5327 (defconst org-tr-regexp-both
5328 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5329 "Regular expression matching a time stamp range.")
5330 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5331 org-ts-regexp
"\\)?")
5332 "Regular expression matching a time stamp or time stamp range.")
5333 (defconst org-tsr-regexp-both
(concat org-ts-regexp-both
"\\(--?-?"
5334 org-ts-regexp-both
"\\)?")
5335 "Regular expression matching a time stamp or time stamp range.
5336 The time stamps may be either active or inactive.")
5338 (defvar org-emph-face nil
)
5340 (defun org-do-emphasis-faces (limit)
5341 "Run through the buffer and add overlays to emphasized strings."
5343 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5344 (if (not (= (char-after (match-beginning 3))
5345 (char-after (match-beginning 4))))
5348 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5349 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5353 (org-remove-flyspell-overlays-in
5354 (match-beginning 0) (match-end 0)))
5355 (add-text-properties (match-beginning 2) (match-end 2)
5356 '(font-lock-multiline t org-emphasis t
))
5357 (when org-hide-emphasis-markers
5358 (add-text-properties (match-end 4) (match-beginning 5)
5359 '(invisible org-link
))
5360 (add-text-properties (match-beginning 3) (match-end 3)
5361 '(invisible org-link
)))))
5365 (defun org-emphasize (&optional char
)
5366 "Insert or change an emphasis, i.e. a font like bold or italic.
5367 If there is an active region, change that region to a new emphasis.
5368 If there is no region, just insert the marker characters and position
5369 the cursor between them.
5370 CHAR should be either the marker character, or the first character of the
5371 HTML tag associated with that emphasis. If CHAR is a space, the means
5372 to remove the emphasis of the selected region.
5373 If char is not given (for example in an interactive call) it
5374 will be prompted for."
5376 (let ((eal org-emphasis-alist
) e det
5377 (erc org-emphasis-regexp-components
)
5379 (string "") beg end move tag c s
)
5380 (if (org-region-active-p)
5381 (setq beg
(region-beginning) end
(region-end)
5382 string
(buffer-substring beg end
))
5385 (while (setq e
(pop eal
))
5386 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
5388 (push (cons c
(string-to-char (car e
))) det
)
5389 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
5390 (substring tag
1)))))
5391 (setq det
(nreverse det
))
5393 (message "%s" (concat "Emphasis marker or tag:" prompt
))
5394 (setq char
(read-char-exclusive)))
5395 (setq char
(or (cdr (assoc char det
)) char
))
5396 (if (equal char ?\
)
5397 (setq s
"" move nil
)
5398 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5399 (error "No such emphasis marker: \"%c\"" char
))
5400 (setq s
(char-to-string char
)))
5401 (while (and (> (length string
) 1)
5402 (equal (substring string
0 1) (substring string -
1))
5403 (assoc (substring string
0 1) org-emphasis-alist
))
5404 (setq string
(substring string
1 -
1)))
5405 (setq string
(concat s string s
))
5406 (if beg
(delete-region beg end
))
5408 (string-match (concat "[" (nth 0 erc
) "\n]")
5409 (char-to-string (char-before (point)))))
5412 (string-match (concat "[" (nth 1 erc
) "\n]")
5413 (char-to-string (char-after (point)))))
5414 (insert " ") (backward-char 1))
5416 (and move
(backward-char 1))))
5418 (defconst org-nonsticky-props
5419 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text
))
5421 (defsubst org-rear-nonsticky-at
(pos)
5422 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5424 (defun org-activate-plain-links (limit)
5425 "Run through the buffer and add overlays to links."
5428 (if (re-search-forward org-plain-link-re limit t
)
5430 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5431 (setq f
(get-text-property (match-beginning 0) 'face
))
5432 (if (or (eq f
'org-tag
)
5433 (and (listp f
) (memq 'org-tag f
)))
5435 (add-text-properties (match-beginning 0) (match-end 0)
5436 (list 'mouse-face
'highlight
5438 'keymap org-mouse-map
))
5439 (org-rear-nonsticky-at (match-end 0)))
5442 (defun org-activate-code (limit)
5443 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5445 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5446 (remove-text-properties (match-beginning 0) (match-end 0)
5447 '(display t invisible t intangible t
))
5450 (defcustom org-src-fontify-natively nil
5451 "When non-nil, fontify code in code blocks."
5454 :group
'org-appearance
5457 (defcustom org-src-prevent-auto-filling nil
5458 "When non-nil, prevent auto-filling in src blocks."
5461 :group
'org-appearance
5464 (defcustom org-allow-promoting-top-level-subtree nil
5465 "When non-nil, allow promoting a top level subtree.
5466 The leading star of the top level headline will be replaced
5470 :group
'org-appearance
)
5472 (defun org-fontify-meta-lines-and-blocks (limit)
5474 (org-fontify-meta-lines-and-blocks-1 limit
)
5475 (error (message "org-mode fontification error"))))
5477 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5478 "Fontify #+ lines and blocks, in the correct ways."
5479 (let ((case-fold-search t
))
5480 (if (re-search-forward
5481 "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5483 (let ((beg (match-beginning 0))
5484 (block-start (match-end 0))
5486 (lang (match-string 7))
5487 (beg1 (line-beginning-position 2))
5488 (dc1 (downcase (match-string 2)))
5489 (dc3 (downcase (match-string 3)))
5490 end end1 quoting block-type ovl
)
5492 ((member dc1
'("+html:" "+ascii:" "+latex:" "+docbook:"))
5493 ;; a single line of backend-specific content
5494 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5495 (remove-text-properties (match-beginning 0) (match-end 0)
5496 '(display t invisible t intangible t
))
5497 (add-text-properties (match-beginning 1) (match-end 3)
5498 '(font-lock-fontified t face org-meta-line
))
5499 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5500 '(font-lock-fontified t face org-block
))
5501 ; for backend-specific code
5503 ((and (match-end 4) (equal dc3
"+begin"))
5505 (setq block-type
(downcase (match-string 5))
5506 quoting
(member block-type org-protecting-blocks
))
5507 (when (re-search-forward
5508 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5509 nil t
) ;; on purpose, we look further than LIMIT
5510 (setq end
(min (point-max) (match-end 0))
5511 end1
(min (point-max) (1- (match-beginning 0))))
5512 (setq block-end
(match-beginning 0))
5514 (remove-text-properties beg end
5515 '(display t invisible t intangible t
)))
5516 (add-text-properties
5518 '(font-lock-fontified t font-lock-multiline t
))
5519 (add-text-properties beg beg1
'(face org-meta-line
))
5520 (add-text-properties end1
(min (point-max) (1+ end
))
5521 '(face org-meta-line
)) ; for end_src
5523 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5524 (org-src-font-lock-fontify-block lang block-start block-end
)
5525 ;; remove old background overlays
5527 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5528 (delete-overlay ov
)))
5529 (overlays-at (/ (+ beg1 block-end
) 2)))
5530 ;; add a background overlay
5531 (setq ovl
(make-overlay beg1 block-end
))
5532 (overlay-put ovl
'face
'org-block-background
)
5533 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5535 (add-text-properties beg1
(min (point-max) (1+ end1
))
5536 '(face org-block
))) ; end of source block
5537 ((not org-fontify-quote-and-verse-blocks
))
5538 ((string= block-type
"quote")
5539 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-quote
)))
5540 ((string= block-type
"verse")
5541 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-verse
))))
5542 (add-text-properties beg beg1
'(face org-block-begin-line
))
5543 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5544 '(face org-block-end-line
))
5546 ((member dc1
'("+title:" "+author:" "+email:" "+date:"))
5547 (add-text-properties
5549 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5550 '(font-lock-fontified t invisible t
)
5551 '(font-lock-fontified t face org-document-info-keyword
)))
5552 (add-text-properties
5553 (match-beginning 6) (match-end 6)
5554 (if (string-equal dc1
"+title:")
5555 '(font-lock-fontified t face org-document-title
)
5556 '(font-lock-fontified t face org-document-info
))))
5557 ((or (member dc1
'("+begin:" "+end:" "+caption:" "+label:"
5558 "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
5559 "+call:" "+header:" "+headers:" "+name:"))
5560 (and (match-end 4) (equal dc3
"+attr")))
5561 (add-text-properties
5563 '(font-lock-fontified t face org-meta-line
))
5565 ((member dc3
'(" " ""))
5566 (add-text-properties
5568 '(font-lock-fontified t face font-lock-comment-face
)))
5569 ((not (member (char-after beg
) '(?\ ?
\t)))
5570 ;; just any other in-buffer setting, but not indented
5571 (add-text-properties
5573 '(font-lock-fontified t face org-meta-line
))
5577 (defun org-strip-protective-commas (beg end
)
5578 "Strip protective commas between BEG and END in the current buffer."
5583 (let ((front-line (save-excursion
5585 "[^[:space:]]" end t
)
5586 (goto-char (match-beginning 0))
5588 (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\)" end t
)
5589 (goto-char (match-beginning 1))
5590 (when (= (current-column) front-line
)
5591 (replace-match "" nil nil nil
1)))))))
5593 (defun org-activate-angle-links (limit)
5594 "Run through the buffer and add overlays to links."
5595 (if (re-search-forward org-angle-link-re limit t
)
5597 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5598 (add-text-properties (match-beginning 0) (match-end 0)
5599 (list 'mouse-face
'highlight
5600 'keymap org-mouse-map
))
5601 (org-rear-nonsticky-at (match-end 0))
5604 (defun org-activate-footnote-links (limit)
5605 "Run through the buffer and add overlays to footnotes."
5606 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5608 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5609 (org-remove-flyspell-overlays-in beg end
)
5610 (add-text-properties beg end
5611 (list 'mouse-face
'highlight
5612 'keymap org-mouse-map
5614 (if (= (point-at-bol) beg
)
5615 "Footnote definition"
5616 "Footnote reference")
5617 'font-lock-fontified t
5618 'font-lock-multiline t
5619 'face
'org-footnote
))))))
5621 (defun org-activate-bracket-links (limit)
5622 "Run through the buffer and add overlays to bracketed links."
5623 (if (re-search-forward org-bracket-link-regexp limit t
)
5624 (let* ((help (concat "LINK: "
5625 (org-match-string-no-properties 1)))
5626 ;; FIXME: above we should remove the escapes.
5627 ;; but that requires another match, protecting match data,
5628 ;; a lot of overhead for font-lock.
5629 (ip (org-maybe-intangible
5630 (list 'invisible
'org-link
5631 'keymap org-mouse-map
'mouse-face
'highlight
5632 'font-lock-multiline t
'help-echo help
)))
5633 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5634 'font-lock-multiline t
'help-echo help
)))
5635 ;; We need to remove the invisible property here. Table narrowing
5636 ;; may have made some of this invisible.
5637 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5638 (remove-text-properties (match-beginning 0) (match-end 0)
5642 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5643 (org-rear-nonsticky-at (match-beginning 3))
5644 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5645 (org-rear-nonsticky-at (match-end 3))
5646 (add-text-properties (match-end 3) (match-end 0) ip
)
5647 (org-rear-nonsticky-at (match-end 0)))
5648 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5649 (org-rear-nonsticky-at (match-beginning 1))
5650 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5651 (org-rear-nonsticky-at (match-end 1))
5652 (add-text-properties (match-end 1) (match-end 0) ip
)
5653 (org-rear-nonsticky-at (match-end 0)))
5656 (defun org-activate-dates (limit)
5657 "Run through the buffer and add overlays to dates."
5658 (if (re-search-forward org-tsr-regexp-both limit t
)
5660 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5661 (add-text-properties (match-beginning 0) (match-end 0)
5662 (list 'mouse-face
'highlight
5663 'keymap org-mouse-map
))
5664 (org-rear-nonsticky-at (match-end 0))
5665 (when org-display-custom-times
5667 (org-display-custom-time (match-beginning 3) (match-end 3)))
5668 (org-display-custom-time (match-beginning 1) (match-end 1)))
5671 (defvar org-target-link-regexp nil
5672 "Regular expression matching radio targets in plain text.")
5673 (make-variable-buffer-local 'org-target-link-regexp
)
5674 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5675 "Regular expression matching a link target.")
5676 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5677 "Regular expression matching a radio target.")
5678 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5679 "Regular expression matching any target.")
5681 (defun org-activate-target-links (limit)
5682 "Run through the buffer and add overlays to target matches."
5683 (when org-target-link-regexp
5684 (let ((case-fold-search t
))
5685 (if (re-search-forward org-target-link-regexp limit t
)
5687 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5688 (add-text-properties (match-beginning 0) (match-end 0)
5689 (list 'mouse-face
'highlight
5690 'keymap org-mouse-map
5691 'help-echo
"Radio target link"
5692 'org-linked-text t
))
5693 (org-rear-nonsticky-at (match-end 0))
5696 (defun org-update-radio-target-regexp ()
5697 "Find all radio targets in this file and update the regular expression."
5699 (when (memq 'radio org-activate-links
)
5700 (setq org-target-link-regexp
5701 (org-make-target-link-regexp (org-all-targets 'radio
)))
5702 (org-restart-font-lock)))
5704 (defun org-hide-wide-columns (limit)
5706 (setq s
(text-property-any (point) (or limit
(point-max))
5709 (setq e
(next-single-property-change s
'org-cwidth
))
5710 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5714 (defvar org-latex-and-specials-regexp nil
5715 "Regular expression for highlighting export special stuff.")
5716 (defvar org-match-substring-regexp
)
5717 (defvar org-match-substring-with-braces-regexp
)
5719 ;; This should be with the exporter code, but we also use if for font-locking
5720 (defconst org-export-html-special-string-regexps
5721 '(("\\\\-" .
"­")
5722 ("---\\([^-]\\)" .
"—\\1")
5723 ("--\\([^-]\\)" .
"–\\1")
5724 ("\\.\\.\\." .
"…"))
5725 "Regular expressions for special string conversion.")
5728 (defun org-compute-latex-and-specials-regexp ()
5729 "Compute regular expression for stuff treated specially by exporters."
5730 (if (not org-highlight-latex-fragments-and-specials
)
5731 (org-set-local 'org-latex-and-specials-regexp nil
)
5734 ((matchers (plist-get org-format-latex-options
:matchers
))
5735 (latexs (delq nil
(mapcar (lambda (x) (if (member (car x
) matchers
) x
))
5736 org-latex-regexps
)))
5737 (org-export-allow-BIND nil
)
5738 (options (org-combine-plists (org-default-export-plist)
5739 (org-infile-export-plist)))
5740 (org-export-with-sub-superscripts (plist-get options
:sub-superscript
))
5741 (org-export-with-LaTeX-fragments (plist-get options
:LaTeX-fragments
))
5742 (org-export-with-TeX-macros (plist-get options
:TeX-macros
))
5743 (org-export-html-expand (plist-get options
:expand-quoted-html
))
5744 (org-export-with-special-strings (plist-get options
:special-strings
))
5747 ((equal org-export-with-sub-superscripts
'{})
5748 (list org-match-substring-with-braces-regexp
))
5749 (org-export-with-sub-superscripts
5750 (list org-match-substring-regexp
))
5753 (if org-export-with-LaTeX-fragments
5754 (mapcar (lambda (x) (nth 1 x
)) latexs
)))
5756 (if org-export-with-TeX-macros
5757 (list (concat "\\\\"
5763 (append org-entities-user
5765 (if (boundp 'org-latex-entities
)
5767 (or (car-safe x
) x
))
5772 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5773 (re-special (if org-export-with-special-strings
5774 (mapcar (lambda (x) (car x
))
5775 org-export-html-special-string-regexps
)))
5779 (if org-export-html-expand
"@<[^>\n]+>")
5782 'org-latex-and-specials-regexp
5783 (mapconcat 'identity
(append re-latex re-sub re-macros re-special
5786 (defun org-do-latex-and-special-faces (limit)
5787 "Run through the buffer and add overlays to links."
5788 (when org-latex-and-specials-regexp
5790 (while (and (not rtn
) (re-search-forward org-latex-and-specials-regexp
5792 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5794 '(org-code org-verbatim underline
)))
5797 d
(cond ((member (char-after (1+ (match-beginning 0)))
5800 (font-lock-prepend-text-property
5801 (+ d
(match-beginning 0)) (match-end 0)
5802 'face
'org-latex-and-export-specials
)
5803 (add-text-properties (+ d
(match-beginning 0)) (match-end 0)
5804 '(font-lock-multiline t
)))))
5807 (defun org-restart-font-lock ()
5808 "Restart `font-lock-mode', to force refontification."
5809 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5811 (font-lock-mode 1)))
5813 (defun org-all-targets (&optional radio
)
5814 "Return a list of all targets in this file.
5815 With optional argument RADIO, only find radio targets."
5816 (let ((re (if radio org-radio-target-regexp org-target-regexp
))
5819 (goto-char (point-min))
5820 (while (re-search-forward re nil t
)
5821 (add-to-list 'rtn
(downcase (org-match-string-no-properties 1))))
5824 (defun org-make-target-link-regexp (targets)
5825 "Make regular expression matching all strings in TARGETS.
5826 The regular expression finds the targets also if there is a line break
5833 (setq x
(regexp-quote x
))
5834 (while (string-match " +" x
)
5835 (setq x
(replace-match "\\s-+" t t x
)))
5841 (defun org-activate-tags (limit)
5842 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5844 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5845 (add-text-properties (match-beginning 1) (match-end 1)
5846 (list 'mouse-face
'highlight
5847 'keymap org-mouse-map
))
5848 (org-rear-nonsticky-at (match-end 1))
5851 (defun org-outline-level ()
5852 "Compute the outline level of the heading at point.
5853 This function assumes that the cursor is at the beginning of a line matched
5854 by `outline-regexp'. Otherwise it returns garbage.
5855 If this is called at a normal headline, the level is the number of stars.
5856 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
5858 (looking-at org-outline-regexp
)
5859 (1- (- (match-end 0) (match-beginning 0)))))
5861 (defvar org-font-lock-keywords nil
)
5863 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5864 "Regular expression matching a property line.")
5866 (defvar org-font-lock-hook nil
5867 "Functions to be called for special font lock stuff.")
5869 (defvar org-font-lock-set-keywords-hook nil
5870 "Functions that can manipulate `org-font-lock-extra-keywords'.
5871 This is called after `org-font-lock-extra-keywords' is defined, but before
5872 it is installed to be used by font lock. This can be useful if something
5873 needs to be inserted at a specific position in the font-lock sequence.")
5875 (defun org-font-lock-hook (limit)
5876 (run-hook-with-args 'org-font-lock-hook limit
))
5878 (defun org-set-font-lock-defaults ()
5879 (let* ((em org-fontify-emphasized-text
)
5880 (lk org-activate-links
)
5881 (org-font-lock-extra-keywords
5884 '(org-font-lock-hook)
5886 `(,(if org-fontify-whole-heading-line
5887 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5888 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5889 (1 (org-get-level-face 1))
5890 (2 (org-get-level-face 2))
5891 (3 (org-get-level-face 3)))
5893 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5896 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
5897 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
5898 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
5899 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
5901 (list org-drawer-regexp
'(0 'org-special-keyword t
))
5902 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
5904 (list org-property-re
5905 '(1 'org-special-keyword t
)
5906 '(3 'org-property-value t
))
5908 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
5909 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
5910 (if (memq 'plain lk
) '(org-activate-plain-links))
5911 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
5912 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
5913 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
5914 (if (memq 'footnote lk
) '(org-activate-footnote-links))
5915 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
5916 '(org-hide-wide-columns (0 nil append
))
5918 (list (format org-heading-keyword-regexp-format
5920 '(2 (org-get-todo-face 2) t
))
5922 (if org-fontify-done-headline
5923 (list (format org-heading-keyword-regexp-format
5926 (mapconcat 'regexp-quote org-done-keywords
"\\|")
5928 '(2 'org-headline-done t
))
5931 '(org-font-lock-add-priority-faces)
5933 '(org-font-lock-add-tag-faces)
5935 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
5936 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
5937 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
5938 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
5941 (if (featurep 'xemacs
)
5942 '(org-do-emphasis-faces (0 nil append
))
5943 '(org-do-emphasis-faces)))
5945 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
5946 1 'org-checkbox prepend
)
5947 (if (cdr (assq 'checkbox org-list-automatic-rules
))
5948 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5949 (0 (org-get-checkbox-statistics-face) t
)))
5950 ;; Description list items
5951 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
5952 1 'org-list-dt prepend
)
5953 ;; ARCHIVEd headings
5955 org-outline-regexp-bol
5956 "\\(.*:" org-archive-tag
":.*\\)")
5957 '(1 'org-archived prepend
))
5959 '(org-do-latex-and-special-faces)
5960 '(org-fontify-entities)
5961 '(org-raise-scripts)
5963 '(org-activate-code (1 'org-code t
))
5965 (list (format org-heading-keyword-regexp-format
5967 org-comment-string
"\\|" org-quote-string
5969 '(2 'org-special-keyword t
))
5970 '("^[ \t]*#.*" (0 'font-lock-comment-face t
))
5971 ;; Blocks and meta lines
5972 '(org-fontify-meta-lines-and-blocks)
5974 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
5975 (run-hooks 'org-font-lock-set-keywords-hook
)
5976 ;; Now set the full font-lock-keywords
5977 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
5978 (org-set-local 'font-lock-defaults
5979 '(org-font-lock-keywords t nil nil backward-paragraph
))
5980 (kill-local-variable 'font-lock-keywords
) nil
))
5982 (defun org-toggle-pretty-entities ()
5983 "Toggle the composition display of entities as UTF8 characters."
5985 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
5986 (org-restart-font-lock)
5987 (if org-pretty-entities
5988 (message "Entities are displayed as UTF8 characters")
5991 (org-decompose-region (point-min) (point-max))
5992 (message "Entities are displayed plain"))))
5994 (defun org-fontify-entities (limit)
5995 "Find an entity to fontify."
5997 (when org-pretty-entities
5999 (while (re-search-forward
6000 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
6002 (if (and (not (org-in-indented-comment-line))
6003 (setq ee
(org-entity-get (match-string 1)))
6004 (= (length (nth 6 ee
)) 1))
6006 ((end (if (equal (match-string 2) "{}")
6009 (add-text-properties
6010 (match-beginning 0) end
6011 (list 'font-lock-fontified t
))
6012 (compose-region (match-beginning 0) end
6018 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
6019 "Fontify string S like in Org-mode."
6022 (let ((org-odd-levels-only odd-levels
))
6024 (font-lock-fontify-buffer)
6030 (defun org-get-level-face (n)
6031 "Get the right face for match N in font-lock matching of headlines."
6032 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
6033 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
6034 (if org-cycle-level-faces
6035 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
6036 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
6038 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
6040 (t (if org-level-color-stars-only nil org-f
))))
6043 (defun org-get-todo-face (kwd)
6044 "Get the right face for a TODO keyword KWD.
6045 If KWD is a number, get the corresponding match group."
6046 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6047 (or (org-face-from-face-or-color
6048 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
6049 (and (member kwd org-done-keywords
) 'org-done
)
6052 (defun org-face-from-face-or-color (context inherit face-or-color
)
6053 "Create a face list that inherits INHERIT, but sets the foreground color.
6054 When FACE-OR-COLOR is not a string, just return it."
6055 (if (stringp face-or-color
)
6056 (list :inherit inherit
6057 (cdr (assoc context org-faces-easy-properties
))
6061 (defun org-font-lock-add-tag-faces (limit)
6062 "Add the special tag faces."
6063 (when (and org-tag-faces org-tags-special-faces-re
)
6064 (while (re-search-forward org-tags-special-faces-re limit t
)
6065 (add-text-properties (match-beginning 1) (match-end 1)
6066 (list 'face
(org-get-tag-face 1)
6067 'font-lock-fontified t
))
6068 (backward-char 1))))
6070 (defun org-font-lock-add-priority-faces (limit)
6071 "Add the special priority faces."
6072 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
6073 (when (save-match-data (org-at-heading-p))
6074 (add-text-properties
6075 (match-beginning 0) (match-end 0)
6076 (list 'face
(or (org-face-from-face-or-color
6077 'priority
'org-special-keyword
6078 (cdr (assoc (char-after (match-beginning 1))
6079 org-priority-faces
)))
6080 'org-special-keyword
)
6081 'font-lock-fontified t
)))))
6083 (defun org-get-tag-face (kwd)
6084 "Get the right face for a TODO keyword KWD.
6085 If KWD is a number, get the corresponding match group."
6086 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6087 (or (org-face-from-face-or-color
6088 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
6091 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
6092 "Remove fontification and activation overlays from links."
6093 (font-lock-default-unfontify-region beg end
)
6094 (let* ((buffer-undo-list t
)
6095 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
6096 (inhibit-modification-hooks t
)
6097 deactivate-mark buffer-file-name buffer-file-truename
)
6098 (org-decompose-region beg end
)
6099 (remove-text-properties beg end
6100 '(mouse-face t keymap t org-linked-text t
6101 invisible t intangible t
6102 org-no-flyspell t org-emphasis t
))
6103 (org-remove-font-lock-display-properties beg end
)))
6105 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
6106 ((raise 0.3) (height 0.7))
6109 "Display properties for showing superscripts and subscripts.")
6111 (defun org-remove-font-lock-display-properties (beg end
)
6112 "Remove specific display properties that have been added by font lock.
6113 The will remove the raise properties that are used to show superscripts
6117 (setq next
(next-single-property-change beg
'display nil end
)
6118 prop
(get-text-property beg
'display
))
6119 (if (member prop org-script-display
)
6120 (put-text-property beg next
'display nil
))
6123 (defun org-raise-scripts (limit)
6124 "Add raise properties to sub/superscripts."
6125 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
6126 (if (re-search-forward
6127 (if (eq org-use-sub-superscripts t
)
6128 org-match-substring-regexp
6129 org-match-substring-with-braces-regexp
)
6131 (let* ((pos (point)) table-p comment-p
6132 (mpos (match-beginning 3))
6133 (emph-p (get-text-property mpos
'org-emphasis
))
6134 (link-p (get-text-property mpos
'mouse-face
))
6135 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6136 (goto-char (point-at-bol))
6137 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
6138 comment-p
(org-looking-at-p "[ \t]*#"))
6140 ;; FIXME: Should we go back one character here, for a_b^c
6141 ;; (goto-char (1- pos)) ;????????????????????
6142 (if (or comment-p emph-p link-p keyw-p
)
6144 (put-text-property (match-beginning 3) (match-end 0)
6146 (if (equal (char-after (match-beginning 2)) ?^
)
6147 (nth (if table-p
3 1) org-script-display
)
6148 (nth (if table-p
2 0) org-script-display
)))
6149 (add-text-properties (match-beginning 2) (match-end 2)
6151 'org-dwidth t
'org-dwidth-n
1))
6152 (if (and (eq (char-after (match-beginning 3)) ?
{)
6153 (eq (char-before (match-end 3)) ?
}))
6155 (add-text-properties
6156 (match-beginning 3) (1+ (match-beginning 3))
6157 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6158 (add-text-properties
6159 (1- (match-end 3)) (match-end 3)
6160 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6163 ;;;; Visibility cycling, including org-goto and indirect buffer
6167 (defvar org-cycle-global-status nil
)
6168 (make-variable-buffer-local 'org-cycle-global-status
)
6169 (defvar org-cycle-subtree-status nil
)
6170 (make-variable-buffer-local 'org-cycle-subtree-status
)
6174 (defvar org-inlinetask-min-level
)
6176 (defun org-cycle (&optional arg
)
6177 "TAB-action and visibility cycling for Org-mode.
6179 This is the command invoked in Org-mode by the TAB key. Its main purpose
6180 is outline visibility cycling, but it also invokes other actions
6181 in special contexts.
6183 - When this function is called with a prefix argument, rotate the entire
6184 buffer through 3 states (global cycling)
6185 1. OVERVIEW: Show only top-level headlines.
6186 2. CONTENTS: Show all headlines of all levels, but no body text.
6187 3. SHOW ALL: Show everything.
6188 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6189 determined by the variable `org-startup-folded', and by any VISIBILITY
6190 properties in the buffer.
6191 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6192 including any drawers.
6194 - When inside a table, re-align the table and move to the next field.
6196 - When point is at the beginning of a headline, rotate the subtree started
6197 by this line through 3 different states (local cycling)
6198 1. FOLDED: Only the main headline is shown.
6199 2. CHILDREN: The main headline and the direct children are shown.
6200 From this state, you can move to one of the children
6201 and zoom in further.
6202 3. SUBTREE: Show the entire subtree, including body text.
6203 If there is no subtree, switch directly from CHILDREN to FOLDED.
6205 - When point is at the beginning of an empty headline and the variable
6206 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6207 of the headline by demoting and promoting it to likely levels. This
6208 speeds up creation document structure by pressing TAB once or several
6209 times right after creating a new headline.
6211 - When there is a numeric prefix, go up to a heading with level ARG, do
6212 a `show-subtree' and return to the previous cursor position. If ARG
6213 is negative, go up that many levels.
6215 - When point is not at the beginning of a headline, execute the global
6216 binding for TAB, which is re-indenting the line. See the option
6217 `org-cycle-emulate-tab' for details.
6219 - Special case: if point is at the beginning of the buffer and there is
6220 no headline in line 1, this function will act as if called with prefix arg
6221 (C-u TAB, same as S-TAB) also when called without prefix arg.
6222 But only if also the variable `org-cycle-global-at-bob' is t."
6224 (org-load-modules-maybe)
6225 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6226 (and org-cycle-level-after-item
/entry-creation
6227 (or (org-cycle-level)
6228 (org-cycle-item-indentation))))
6230 (or org-cycle-max-level
6231 (and (boundp 'org-inlinetask-min-level
)
6232 org-inlinetask-min-level
6233 (1- org-inlinetask-min-level
))))
6234 (nstars (and limit-level
6235 (if org-odd-levels-only
6236 (and limit-level
(1- (* limit-level
2)))
6239 (if (not (derived-mode-p 'org-mode
))
6241 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6242 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6243 (not (looking-at org-outline-regexp
))))
6246 (delq 'org-optimize-window-after-visibility-change
6247 (copy-sequence org-cycle-hook
))
6251 (if (or bob-special
(equal arg
'(4)))
6252 ;; special case: use global cycling
6258 (setq last-command
'dummy
)
6259 (org-set-startup-visibility)
6260 (message "Startup visibility, plus VISIBILITY properties"))
6264 (message "Entire buffer visible, including drawers"))
6266 ;; Table: enter it or move to the next field.
6267 ((org-at-table-p 'any
)
6268 (if (org-at-table.el-p
)
6269 (message "Use C-c ' to edit table.el tables")
6270 (if arg
(org-table-edit-field t
)
6271 (org-table-justify-field-maybe)
6272 (call-interactively 'org-table-next-field
))))
6274 ((run-hook-with-args-until-success
6275 'org-tab-after-check-for-table-hook
))
6277 ;; Global cycling: delegate to `org-cycle-internal-global'.
6278 ((eq arg t
) (org-cycle-internal-global))
6280 ;; Drawers: delegate to `org-flag-drawer'.
6281 ((and org-drawers org-drawer-regexp
6283 (beginning-of-line 1)
6284 (looking-at org-drawer-regexp
)))
6285 (org-flag-drawer ; toggle block visibility
6286 (not (get-char-property (match-end 0) 'invisible
))))
6288 ;; Show-subtree, ARG levels up from here.
6291 (org-back-to-heading)
6292 (outline-up-heading (if (< arg
0) (- arg
)
6293 (- (funcall outline-level
) arg
)))
6294 (org-show-subtree)))
6296 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6297 ((and (featurep 'org-inlinetask
)
6298 (org-inlinetask-at-task-p)
6299 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6300 (org-inlinetask-toggle-visibility))
6302 ((org-try-cdlatex-tab))
6304 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6305 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6306 (save-excursion (beginning-of-line 1)
6307 (looking-at org-outline-regexp
)))
6308 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6309 (org-cycle-internal-local))
6311 ;; From there: TAB emulation and template completion.
6312 (buffer-read-only (org-back-to-heading))
6314 ((run-hook-with-args-until-success
6315 'org-tab-after-check-for-cycling-hook
))
6317 ((org-try-structure-completion))
6319 ((run-hook-with-args-until-success
6320 'org-tab-before-tab-emulation-hook
))
6322 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6324 (not (looking-at org-outline-regexp
))))
6325 (call-interactively (global-key-binding "\t")))
6327 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6328 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6329 (or (and (eq org-cycle-emulate-tab
'white
)
6330 (= (match-end 0) (point-at-eol)))
6331 (and (eq org-cycle-emulate-tab
'whitestart
)
6332 (>= (match-end 0) pos
))))
6334 (eq org-cycle-emulate-tab t
))
6335 (call-interactively (global-key-binding "\t")))
6338 (org-back-to-heading)
6341 (defun org-cycle-internal-global ()
6342 "Do the global cycling action."
6343 ;; Hack to avoid display of messages for .org attachments in Gnus
6344 (let ((ga (string-match "\\*fontification" (buffer-name))))
6346 ((and (eq last-command this-command
)
6347 (eq org-cycle-global-status
'overview
))
6348 ;; We just created the overview - now do table of contents
6349 ;; This can be slow in very large buffers, so indicate action
6350 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6351 (unless ga
(message "CONTENTS..."))
6353 (unless ga
(message "CONTENTS...done"))
6354 (setq org-cycle-global-status
'contents
)
6355 (run-hook-with-args 'org-cycle-hook
'contents
))
6357 ((and (eq last-command this-command
)
6358 (eq org-cycle-global-status
'contents
))
6359 ;; We just showed the table of contents - now show everything
6360 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6362 (unless ga
(message "SHOW ALL"))
6363 (setq org-cycle-global-status
'all
)
6364 (run-hook-with-args 'org-cycle-hook
'all
))
6367 ;; Default action: go to overview
6368 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6370 (unless ga
(message "OVERVIEW"))
6371 (setq org-cycle-global-status
'overview
)
6372 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6374 (defun org-cycle-internal-local ()
6375 "Do the local cycling action."
6376 (let ((goal-column 0) eoh eol eos has-children children-skipped struct
)
6377 ;; First, determine end of headline (EOH), end of subtree or item
6378 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6383 (setq struct
(org-list-struct))
6384 (setq eoh
(point-at-eol))
6385 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6386 (setq has-children
(org-list-has-child-p (point) struct
)))
6387 (org-back-to-heading)
6388 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6389 (setq eos
(save-excursion
6390 (org-end-of-subtree t
)
6392 (skip-chars-forward " \t\n"))
6393 (if (eobp) (point) (1- (point)))))
6396 (let ((level (funcall outline-level
)))
6397 (outline-next-heading)
6398 (and (org-at-heading-p t
)
6399 (> (funcall outline-level
) level
))))
6401 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6402 ;; Determine end invisible part of buffer (EOL)
6403 (beginning-of-line 2)
6404 ;; XEmacs doesn't have `next-single-char-property-change'
6405 (if (featurep 'xemacs
)
6406 (while (and (not (eobp)) ;; this is like `next-line'
6407 (get-char-property (1- (point)) 'invisible
))
6408 (beginning-of-line 2))
6409 (while (and (not (eobp)) ;; this is like `next-line'
6410 (get-char-property (1- (point)) 'invisible
))
6411 (goto-char (next-single-char-property-change (point) 'invisible
))
6412 (and (eolp) (beginning-of-line 2))))
6414 ;; Find out what to do next and set `this-command'
6417 ;; Nothing is hidden behind this heading
6418 (run-hook-with-args 'org-pre-cycle-hook
'empty
)
6419 (message "EMPTY ENTRY")
6420 (setq org-cycle-subtree-status nil
)
6423 (outline-next-heading)
6424 (if (outline-invisible-p) (org-flag-heading nil
))))
6425 ((and (or (>= eol eos
)
6426 (not (string-match "\\S-" (buffer-substring eol eos
))))
6428 (not (setq children-skipped
6429 org-cycle-skip-children-state-if-no-children
))))
6430 ;; Entire subtree is hidden in one line: children view
6431 (run-hook-with-args 'org-pre-cycle-hook
'children
)
6433 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6435 (org-with-limited-levels (show-children))
6436 ;; FIXME: This slows down the func way too much.
6437 ;; How keep drawers hidden in subtree anyway?
6438 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6439 ;; (org-cycle-hide-drawers 'subtree))
6441 ;; Fold every list in subtree to top-level items.
6442 (when (eq org-cycle-include-plain-lists
'integrate
)
6444 (org-back-to-heading)
6445 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6446 (beginning-of-line 1)
6447 (let* ((struct (org-list-struct))
6448 (prevs (org-list-prevs-alist struct
))
6449 (end (org-list-get-bottom-point struct
)))
6450 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6451 (org-list-get-all-items (point) struct prevs
))
6452 (goto-char end
))))))
6453 (message "CHILDREN")
6456 (outline-next-heading)
6457 (if (outline-invisible-p) (org-flag-heading nil
)))
6458 (setq org-cycle-subtree-status
'children
)
6459 (run-hook-with-args 'org-cycle-hook
'children
))
6460 ((or children-skipped
6461 (and (eq last-command this-command
)
6462 (eq org-cycle-subtree-status
'children
)))
6463 ;; We just showed the children, or no children are there,
6464 ;; now show everything.
6465 (run-hook-with-args 'org-pre-cycle-hook
'subtree
)
6466 (outline-flag-region eoh eos nil
)
6467 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6468 (setq org-cycle-subtree-status
'subtree
)
6469 (run-hook-with-args 'org-cycle-hook
'subtree
))
6471 ;; Default action: hide the subtree.
6472 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6473 (outline-flag-region eoh eos t
)
6475 (setq org-cycle-subtree-status
'folded
)
6476 (run-hook-with-args 'org-cycle-hook
'folded
)))))
6479 (defun org-global-cycle (&optional arg
)
6480 "Cycle the global visibility. For details see `org-cycle'.
6481 With \\[universal-argument] prefix arg, switch to startup visibility.
6482 With a numeric prefix, show all headlines up to that level."
6484 (let ((org-cycle-include-plain-lists
6485 (if (derived-mode-p 'org-mode
) org-cycle-include-plain-lists nil
)))
6489 (hide-sublevels arg
)
6490 (setq org-cycle-global-status
'contents
))
6492 (org-set-startup-visibility)
6493 (message "Startup visibility, plus VISIBILITY properties."))
6495 (org-cycle '(4))))))
6497 (defun org-set-startup-visibility ()
6498 "Set the visibility required by startup options and properties."
6500 ((eq org-startup-folded t
)
6502 ((eq org-startup-folded
'content
)
6503 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6504 (org-cycle '(4)) (org-cycle '(4)))))
6505 (unless (eq org-startup-folded
'showeverything
)
6506 (if org-hide-block-startup
(org-hide-block-all))
6507 (org-set-visibility-according-to-property 'no-cleanup
)
6508 (org-cycle-hide-archived-subtrees 'all
)
6509 (org-cycle-hide-drawers 'all
)
6510 (org-cycle-show-empty-lines t
)))
6512 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6513 "Switch subtree visibilities according to :VISIBILITY: property."
6515 (let (org-show-entry-below state
)
6517 (goto-char (point-min))
6518 (while (re-search-forward
6519 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6521 (setq state
(match-string 1))
6523 (org-back-to-heading t
)
6527 ((equal state
'("fold" "folded"))
6529 ((equal state
"children")
6530 (org-show-hidden-entry)
6532 ((equal state
"content")
6535 (org-narrow-to-subtree)
6537 ((member state
'("all" "showall"))
6540 (org-cycle-hide-archived-subtrees 'all
)
6541 (org-cycle-hide-drawers 'all
)
6542 (org-cycle-show-empty-lines 'all
)))))
6544 ;; This function uses outline-regexp instead of the more fundamental
6545 ;; org-outline-regexp so that org-cycle-global works outside of Org
6546 ;; buffers, where outline-regexp is needed.
6547 (defun org-overview ()
6548 "Switch to overview mode, showing only top-level headlines.
6549 Really, this shows all headlines with level equal or greater than the level
6550 of the first headline in the buffer. This is important, because if the
6551 first headline is not level one, then (hide-sublevels 1) gives confusing
6554 (let ((level (save-excursion
6555 (goto-char (point-min))
6556 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6558 (goto-char (match-beginning 0))
6559 (funcall outline-level
))))))
6560 (and level
(hide-sublevels level
))))
6562 (defun org-content (&optional arg
)
6563 "Show all headlines in the buffer, like a table of contents.
6564 With numerical argument N, show content up to level N."
6567 ;; Visit all headings and show their offspring
6568 (and (integerp arg
) (org-overview))
6569 (goto-char (point-max))
6571 (while (and (progn (condition-case nil
6572 (outline-previous-visible-heading 1)
6573 (error (goto-char (point-min))))
6575 (looking-at org-outline-regexp
))
6577 (show-children (1- arg
))
6579 (if (bobp) (throw 'exit nil
))))))
6582 (defun org-optimize-window-after-visibility-change (state)
6583 "Adjust the window after a change in outline visibility.
6584 This function is the default value of the hook `org-cycle-hook'."
6585 (when (get-buffer-window (current-buffer))
6587 ((eq state
'content
) nil
)
6588 ((eq state
'all
) nil
)
6589 ((eq state
'folded
) nil
)
6590 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6591 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6593 (defun org-remove-empty-overlays-at (pos)
6594 "Remove outline overlays that do not contain non-white stuff."
6597 (and (eq 'outline
(overlay-get o
'invisible
))
6598 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6600 (delete-overlay o
)))
6603 (defun org-clean-visibility-after-subtree-move ()
6604 "Fix visibility issues after moving a subtree."
6605 ;; First, find a reasonable region to look at:
6606 ;; Start two siblings above, end three below
6607 (let* ((beg (save-excursion
6608 (and (org-get-last-sibling)
6609 (org-get-last-sibling))
6611 (end (save-excursion
6612 (and (org-get-next-sibling)
6613 (org-get-next-sibling)
6614 (org-get-next-sibling))
6615 (if (org-at-heading-p)
6618 (level (looking-at "\\*+"))
6619 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6622 (narrow-to-region beg end
)
6624 ;; Properly fold already folded siblings
6625 (goto-char (point-min))
6626 (while (re-search-forward re nil t
)
6627 (if (and (not (outline-invisible-p))
6629 (goto-char (point-at-eol)) (outline-invisible-p)))
6631 (org-cycle-show-empty-lines 'overview
)
6632 (org-cycle-hide-drawers 'overview
)))))
6634 (defun org-cycle-show-empty-lines (state)
6635 "Show empty lines above all visible headlines.
6636 The region to be covered depends on STATE when called through
6637 `org-cycle-hook'. Lisp program can use t for STATE to get the
6638 entire buffer covered. Note that an empty line is only shown if there
6639 are at least `org-cycle-separator-lines' empty lines before the headline."
6640 (when (not (= org-cycle-separator-lines
0))
6642 (let* ((n (abs org-cycle-separator-lines
))
6644 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6645 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6646 (t (let ((ns (number-to-string (- n
2))))
6647 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6648 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6651 ((memq state
'(overview contents t
))
6652 (setq beg
(point-min) end
(point-max)))
6653 ((memq state
'(children folded
))
6654 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6655 (beginning-of-line 2)
6659 (while (re-search-forward re end t
)
6660 (unless (get-char-property (match-end 1) 'invisible
)
6661 (setq e
(match-end 1))
6662 (if (< org-cycle-separator-lines
0)
6663 (setq b
(save-excursion
6664 (goto-char (match-beginning 0))
6665 (org-back-over-empty-lines)
6667 (goto-char (max (point-min) (1- (point))))
6671 (setq b
(match-beginning 1)))
6672 (outline-flag-region b e nil
)))))))
6673 ;; Never hide empty lines at the end of the file.
6675 (goto-char (point-max))
6676 (outline-previous-heading)
6677 (outline-end-of-heading)
6678 (if (and (looking-at "[ \t\n]+")
6679 (= (match-end 0) (point-max)))
6680 (outline-flag-region (point) (match-end 0) nil
))))
6682 (defun org-show-empty-lines-in-parent ()
6683 "Move to the parent and re-show empty lines before visible headlines."
6685 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6686 (org-cycle-show-empty-lines context
))))
6688 (defun org-files-list ()
6689 "Return `org-agenda-files' list, plus all open org-mode files.
6690 This is useful for operations that need to scan all of a user's
6691 open and agenda-wise Org files."
6692 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6693 (dolist (buf (buffer-list))
6694 (with-current-buffer buf
6695 (if (and (derived-mode-p 'org-mode
) (buffer-file-name))
6696 (let ((file (expand-file-name (buffer-file-name))))
6697 (unless (member file files
)
6698 (push file files
))))))
6701 (defsubst org-entry-beginning-position
()
6702 "Return the beginning position of the current entry."
6703 (save-excursion (outline-back-to-heading t
) (point)))
6705 (defsubst org-entry-end-position
()
6706 "Return the end position of the current entry."
6707 (save-excursion (outline-next-heading) (point)))
6709 (defun org-cycle-hide-drawers (state)
6710 "Re-hide all drawers after a visibility state change."
6711 (when (and (derived-mode-p 'org-mode
)
6712 (not (memq state
'(overview folded contents
))))
6714 (let* ((globalp (memq state
'(contents all
)))
6715 (beg (if globalp
(point-min) (point)))
6716 (end (if globalp
(point-max)
6717 (if (eq state
'children
)
6718 (save-excursion (outline-next-heading) (point))
6719 (org-end-of-subtree t
)))))
6721 (while (re-search-forward org-drawer-regexp end t
)
6722 (org-flag-drawer t
))))))
6724 (defun org-flag-drawer (flag)
6725 "When FLAG is non-nil, hide the drawer we are within.
6726 Otherwise make it visible."
6728 (beginning-of-line 1)
6729 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6730 (let ((b (match-end 0)))
6731 (if (re-search-forward
6733 (save-excursion (outline-next-heading) (point)) t
)
6734 (outline-flag-region b
(point-at-eol) flag
)
6735 (error ":END: line missing at position %s" b
))))))
6737 (defun org-subtree-end-visible-p ()
6738 "Is the end of the current subtree visible?"
6739 (pos-visible-in-window-p
6740 (save-excursion (org-end-of-subtree t
) (point))))
6742 (defun org-first-headline-recenter (&optional N
)
6743 "Move cursor to the first headline and recenter the headline.
6744 Optional argument N means put the headline into the Nth line of the window."
6745 (goto-char (point-min))
6746 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
6748 (recenter (prefix-numeric-value N
))))
6750 ;;; Saving and restoring visibility
6752 (defun org-outline-overlay-data (&optional use-markers
)
6753 "Return a list of the locations of all outline overlays.
6754 These are overlays with the `invisible' property value `outline'.
6755 The return value is a list of cons cells, with start and stop
6756 positions for each overlay.
6757 If USE-MARKERS is set, return the positions as markers."
6764 (when (eq (overlay-get o
'invisible
) 'outline
)
6765 (setq beg
(overlay-start o
)
6766 end
(overlay-end o
))
6767 (and beg end
(> end beg
)
6769 (cons (move-marker (make-marker) beg
)
6770 (move-marker (make-marker) end
))
6772 (overlays-in (point-min) (point-max))))))))
6774 (defun org-set-outline-overlay-data (data)
6775 "Create visibility overlays for all positions in DATA.
6776 DATA should have been made by `org-outline-overlay-data'."
6783 (outline-flag-region (car c
) (cdr c
) t
))
6786 ;;; Folding of blocks
6788 (defvar org-hide-block-overlays nil
6789 "Overlays hiding blocks.")
6790 (make-variable-buffer-local 'org-hide-block-overlays
)
6792 (defun org-block-map (function &optional start end
)
6793 "Call FUNCTION at the head of all source blocks in the current buffer.
6794 Optional arguments START and END can be used to limit the range."
6795 (let ((start (or start
(point-min)))
6796 (end (or end
(point-max))))
6799 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6802 (goto-char (match-beginning 0))
6803 (funcall function
)))))))
6805 (defun org-hide-block-toggle-all ()
6806 "Toggle the visibility of all blocks in the current buffer."
6807 (org-block-map #'org-hide-block-toggle
))
6809 (defun org-hide-block-all ()
6810 "Fold all blocks in the current buffer."
6812 (org-show-block-all)
6813 (org-block-map #'org-hide-block-toggle-maybe
))
6815 (defun org-show-block-all ()
6816 "Unfold all blocks in the current buffer."
6818 (mapc 'delete-overlay org-hide-block-overlays
)
6819 (setq org-hide-block-overlays nil
))
6821 (defun org-hide-block-toggle-maybe ()
6822 "Toggle visibility of block at point."
6824 (let ((case-fold-search t
))
6826 (beginning-of-line 1)
6827 (looking-at org-block-regexp
))
6828 (progn (org-hide-block-toggle)
6829 t
) ;; to signal that we took action
6830 nil
))) ;; to signal that we did not
6832 (defun org-hide-block-toggle (&optional force
)
6833 "Toggle the visibility of the current block."
6837 (if (re-search-forward org-block-regexp nil t
)
6838 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
6839 (end (match-end 0)) ;; end of entire body
6841 (if (memq t
(mapcar (lambda (overlay)
6842 (eq (overlay-get overlay
'invisible
)
6844 (overlays-at start
)))
6845 (if (or (not force
) (eq force
'off
))
6847 (when (member ov org-hide-block-overlays
)
6848 (setq org-hide-block-overlays
6849 (delq ov org-hide-block-overlays
)))
6850 (when (eq (overlay-get ov
'invisible
)
6852 (delete-overlay ov
)))
6853 (overlays-at start
)))
6854 (setq ov
(make-overlay start end
))
6855 (overlay-put ov
'invisible
'org-hide-block
)
6856 ;; make the block accessible to isearch
6858 ov
'isearch-open-invisible
6860 (when (member ov org-hide-block-overlays
)
6861 (setq org-hide-block-overlays
6862 (delq ov org-hide-block-overlays
)))
6863 (when (eq (overlay-get ov
'invisible
)
6865 (delete-overlay ov
))))
6866 (push ov org-hide-block-overlays
)))
6867 (error "Not looking at a source block"))))
6869 ;; org-tab-after-check-for-cycling-hook
6870 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
6871 ;; Remove overlays when changing major mode
6872 (add-hook 'org-mode-hook
6873 (lambda () (org-add-hook 'change-major-mode-hook
6874 'org-show-block-all
'append
'local
)))
6878 (defvar org-goto-window-configuration nil
)
6879 (defvar org-goto-marker nil
)
6880 (defvar org-goto-map
6881 (let ((map (make-sparse-keymap)))
6882 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur
)) cmd
)
6883 (while (setq cmd
(pop cmds
))
6884 (substitute-key-definition cmd cmd map global-map
)))
6885 (suppress-keymap map
)
6886 (org-defkey map
"\C-m" 'org-goto-ret
)
6887 (org-defkey map
[(return)] 'org-goto-ret
)
6888 (org-defkey map
[(left)] 'org-goto-left
)
6889 (org-defkey map
[(right)] 'org-goto-right
)
6890 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
6891 (org-defkey map
"\C-i" 'org-cycle
)
6892 (org-defkey map
[(tab)] 'org-cycle
)
6893 (org-defkey map
[(down)] 'outline-next-visible-heading
)
6894 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
6895 (if org-goto-auto-isearch
6896 (if (fboundp 'define-key-after
)
6897 (define-key-after map
[t] 'org-goto-local-auto-isearch)
6899 (org-defkey map "q" 'org-goto-quit)
6900 (org-defkey map "n" 'outline-next-visible-heading)
6901 (org-defkey map "p" 'outline-previous-visible-heading)
6902 (org-defkey map "f" 'outline-forward-same-level)
6903 (org-defkey map "b" 'outline-backward-same-level)
6904 (org-defkey map "u" 'outline-up-heading))
6905 (org-defkey map "/" 'org-occur)
6906 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6907 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6908 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6909 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6910 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6913 (defconst org-goto-help
6914 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6915 RET=jump to location [Q]uit and return to previous location
6916 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6918 (defvar org-goto-start-pos) ; dynamically scoped parameter
6920 ;; FIXME: Docstring does not mention both interfaces
6921 (defun org-goto (&optional alternative-interface)
6922 "Look up a different location in the current file, keeping current visibility.
6924 When you want look-up or go to a different location in a
6925 document, the fastest way is often to fold the entire buffer and
6926 then dive into the tree. This method has the disadvantage, that
6927 the previous location will be folded, which may not be what you
6930 This command works around this by showing a copy of the current
6931 buffer in an indirect buffer, in overview mode. You can dive
6932 into the tree in that copy, use org-occur and incremental search
6933 to find a location. When pressing RET or `Q', the command
6934 returns to the original buffer in which the visibility is still
6935 unchanged. After RET it will also jump to the location selected
6936 in the indirect buffer and expose the headline hierarchy above.
6938 With a prefix argument, use the alternative interface: e.g. if
6939 `org-goto-interface' is 'outline use 'outline-path-completion."
6941 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6942 (org-refile-use-outline-path t)
6943 (org-refile-target-verify-function nil)
6945 (if (not alternative-interface)
6947 (if (eq org-goto-interface 'outline)
6948 'outline-path-completion
6950 (org-goto-start-pos (point))
6952 (if (eq interface 'outline)
6953 (car (org-get-location (current-buffer) org-goto-help))
6954 (let ((pa (org-refile-get-location "Goto" nil nil t)))
6955 (org-refile-check-position pa)
6959 (org-mark-ring-push org-goto-start-pos)
6960 (goto-char selected-point)
6961 (if (or (outline-invisible-p) (org-invisible-p2))
6962 (org-show-context 'org-goto)))
6965 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6966 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6967 (defvar org-goto-local-auto-isearch-map) ; defined below
6969 (defun org-get-location (buf help)
6970 "Let the user select a location in the Org-mode buffer BUF.
6971 This function uses a recursive edit. It returns the selected position
6973 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6974 (isearch-hide-immediately nil)
6975 (isearch-search-fun-function
6976 (lambda () 'org-goto-local-search-headings))
6977 (org-goto-selected-point org-goto-exit-command)
6979 (special-display-buffer-names nil)
6980 (special-display-regexps nil)
6981 (special-display-function nil))
6983 (save-window-excursion
6984 (delete-other-windows)
6985 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6986 (org-pop-to-buffer-same-window
6988 (make-indirect-buffer (current-buffer) "*org-goto*")
6989 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6990 (with-output-to-temp-buffer "*Help*"
6992 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
6993 (setq buffer-read-only nil)
6994 (let ((org-startup-truncated t)
6995 (org-startup-folded nil)
6996 (org-startup-align-all-tables nil))
6999 (setq buffer-read-only t)
7000 (if (and (boundp 'org-goto-start-pos)
7001 (integer-or-marker-p org-goto-start-pos))
7002 (let ((org-show-hierarchy-above t)
7003 (org-show-siblings t)
7004 (org-show-following-heading t))
7005 (goto-char org-goto-start-pos)
7006 (and (outline-invisible-p) (org-show-context)))
7007 (goto-char (point-min)))
7008 (let (org-special-ctrl-a/e) (org-beginning-of-line))
7009 (message "Select location and press RET")
7010 (use-local-map org-goto-map)
7013 (kill-buffer "*org-goto*")
7014 (cons org-goto-selected-point org-goto-exit-command)))
7016 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
7017 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
7018 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
7019 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
7021 (defun org-goto-local-search-headings (string bound noerror)
7022 "Search and make sure that any matches are in headlines."
7024 (while (if isearch-forward
7025 (search-forward string bound noerror)
7026 (search-backward string bound noerror))
7027 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
7028 (and (member :headline context)
7029 (not (member :tags context))))
7030 (throw 'return (point))))))
7032 (defun org-goto-local-auto-isearch ()
7035 (goto-char (point-min))
7036 (let ((keys (this-command-keys)))
7037 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
7039 (isearch-process-search-char (string-to-char keys)))))
7041 (defun org-goto-ret (&optional arg)
7042 "Finish `org-goto' by going to the new location."
7044 (setq org-goto-selected-point (point)
7045 org-goto-exit-command 'return)
7048 (defun org-goto-left ()
7049 "Finish `org-goto' by going to the new location."
7051 (if (org-at-heading-p)
7053 (beginning-of-line 1)
7054 (setq org-goto-selected-point (point)
7055 org-goto-exit-command 'left)
7057 (error "Not on a heading")))
7059 (defun org-goto-right ()
7060 "Finish `org-goto' by going to the new location."
7062 (if (org-at-heading-p)
7064 (setq org-goto-selected-point (point)
7065 org-goto-exit-command 'right)
7067 (error "Not on a heading")))
7069 (defun org-goto-quit ()
7070 "Finish `org-goto' without cursor motion."
7072 (setq org-goto-selected-point nil)
7073 (setq org-goto-exit-command 'quit)
7076 ;;; Indirect buffer display of subtrees
7078 (defvar org-indirect-dedicated-frame nil
7079 "This is the frame being used for indirect tree display.")
7080 (defvar org-last-indirect-buffer nil)
7082 (defun org-tree-to-indirect-buffer (&optional arg)
7083 "Create indirect buffer and narrow it to current subtree.
7084 With numerical prefix ARG, go up to this level and then take that tree.
7085 If ARG is negative, go up that many levels.
7086 If `org-indirect-buffer-display' is not `new-frame', the command removes the
7087 indirect buffer previously made with this command, to avoid proliferation of
7088 indirect buffers. However, when you call the command with a \
7089 \\[universal-argument] prefix, or
7090 when `org-indirect-buffer-display' is `new-frame', the last buffer
7091 is kept so that you can work with several indirect buffers at the same time.
7092 If `org-indirect-buffer-display' is `dedicated-frame', the \
7093 \\[universal-argument] prefix also
7094 requests that a new frame be made for the new buffer, so that the dedicated
7095 frame is not changed."
7097 (let ((cbuf (current-buffer))
7098 (cwin (selected-window))
7100 beg end level heading ibuf)
7102 (org-back-to-heading t)
7104 (setq level (org-outline-level))
7105 (if (< arg 0) (setq arg (+ level arg)))
7106 (while (> (setq level (org-outline-level)) arg)
7107 (outline-up-heading 1 t)))
7109 heading (org-get-heading))
7110 (org-end-of-subtree t t)
7111 (if (org-at-heading-p) (backward-char 1))
7113 (if (and (buffer-live-p org-last-indirect-buffer)
7114 (not (eq org-indirect-buffer-display 'new-frame))
7116 (kill-buffer org-last-indirect-buffer))
7117 (setq ibuf (org-get-indirect-buffer cbuf)
7118 org-last-indirect-buffer ibuf)
7120 ((or (eq org-indirect-buffer-display 'new-frame)
7121 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7122 (select-frame (make-frame))
7123 (delete-other-windows)
7124 (org-pop-to-buffer-same-window ibuf)
7125 (org-set-frame-title heading))
7126 ((eq org-indirect-buffer-display 'dedicated-frame)
7128 (select-frame (or (and org-indirect-dedicated-frame
7129 (frame-live-p org-indirect-dedicated-frame)
7130 org-indirect-dedicated-frame)
7131 (setq org-indirect-dedicated-frame (make-frame)))))
7132 (delete-other-windows)
7133 (org-pop-to-buffer-same-window ibuf)
7134 (org-set-frame-title (concat "Indirect: " heading)))
7135 ((eq org-indirect-buffer-display 'current-window)
7136 (org-pop-to-buffer-same-window ibuf))
7137 ((eq org-indirect-buffer-display 'other-window)
7138 (pop-to-buffer ibuf))
7139 (t (error "Invalid value")))
7140 (if (featurep 'xemacs)
7141 (save-excursion (org-mode) (turn-on-font-lock)))
7142 (narrow-to-region beg end)
7145 (run-hook-with-args 'org-cycle-hook 'all)
7146 (and (window-live-p cwin) (select-window cwin))))
7148 (defun org-get-indirect-buffer (&optional buffer)
7149 (setq buffer (or buffer (current-buffer)))
7150 (let ((n 1) (base (buffer-name buffer)) bname)
7151 (while (buffer-live-p
7152 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7155 (make-indirect-buffer buffer bname 'clone)
7156 (error (make-indirect-buffer buffer bname)))))
7158 (defun org-set-frame-title (title)
7159 "Set the title of the current frame to the string TITLE."
7160 ;; FIXME: how to name a single frame in XEmacs???
7161 (unless (featurep 'xemacs)
7162 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7164 ;;;; Structure editing
7166 ;;; Inserting headlines
7168 (defun org-previous-line-empty-p ()
7171 (or (beginning-of-line 0) t)
7173 (looking-at "[ \t]*$")))))
7175 (defun org-insert-heading (&optional force-heading invisible-ok)
7176 "Insert a new heading or item with same depth at point.
7177 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
7178 If point is at the beginning of a headline, insert a sibling before the
7179 current headline. If point is not at the beginning, split the line,
7180 create the new headline with the text in the current line after point
7181 \(but see also the variable `org-M-RET-may-split-line').
7183 When INVISIBLE-OK is set, stop at invisible headlines when going back.
7184 This is important for non-interactive uses of the command."
7186 (if (or (= (buffer-size) 0)
7187 (and (not (save-excursion
7188 (and (ignore-errors (org-back-to-heading invisible-ok))
7189 (org-at-heading-p))))
7190 (or force-heading (not (org-in-item-p)))))
7193 (run-hooks 'org-insert-heading-hook))
7194 (when (or force-heading (not (org-insert-item)))
7195 (let* ((empty-line-p nil)
7197 (on-heading (org-at-heading-p))
7198 (head (save-excursion
7201 (org-back-to-heading invisible-ok)
7202 (when (and (not on-heading)
7203 (featurep 'org-inlinetask)
7204 (integerp org-inlinetask-min-level)
7205 (>= (length (match-string 0))
7206 org-inlinetask-min-level))
7207 ;; Find a heading level before the inline task
7208 (while (and (setq level (org-up-heading-safe))
7209 (>= level org-inlinetask-min-level)))
7210 (if (org-at-heading-p)
7211 (org-back-to-heading invisible-ok)
7212 (error "This should not happen")))
7213 (setq empty-line-p (org-previous-line-empty-p))
7216 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7217 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7218 pos hide-previous previous-pos)
7220 ((and (org-at-heading-p) (bolp)
7222 (save-excursion (backward-char 1) (not (outline-invisible-p)))))
7223 ;; insert before the current line
7224 (open-line (if blank 2 1)))
7226 (not org-insert-heading-respect-content)
7229 (backward-char 1) (not (outline-invisible-p)))))
7230 ;; insert right here
7233 ;; somewhere in the line
7235 (setq previous-pos (point-at-bol))
7237 (setq hide-previous (outline-invisible-p)))
7238 (and org-insert-heading-respect-content (org-show-subtree))
7240 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
7243 (goto-char (point-at-bol))
7244 (and (looking-at org-complex-heading-regexp)
7246 (> p (match-beginning 4)))))))
7249 (org-insert-heading-respect-content
7250 (org-end-of-subtree nil t)
7251 (when (featurep 'org-inlinetask)
7252 (while (and (not (eobp))
7253 (looking-at "\\(\\*+\\)[ \t]+")
7254 (>= (length (match-string 1))
7255 org-inlinetask-min-level))
7256 (org-end-of-subtree nil t)))
7257 (or (bolp) (newline))
7258 (or (org-previous-line-empty-p)
7259 (and blank (newline)))
7265 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
7266 (setq tags (and (match-end 2) (match-string 2)))
7268 (delete-region (match-beginning 1) (match-end 1)))
7269 (setq pos (point-at-bol))
7270 (or split (end-of-line 1))
7271 (delete-horizontal-space)
7272 (if (string-match "\\`\\*+\\'"
7273 (buffer-substring (point-at-bol) (point)))
7275 (newline (if blank 2 1))
7281 (org-set-tags nil 'align))))
7283 (or split (end-of-line 1))
7284 (newline (if blank 2 1)))))))
7285 (insert head) (just-one-space)
7288 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
7289 (when (and org-insert-heading-respect-content hide-previous)
7291 (goto-char previous-pos)
7293 (run-hooks 'org-insert-heading-hook)))))
7295 (defun org-get-heading (&optional no-tags no-todo)
7296 "Return the heading of the current entry, without the stars.
7297 When NO-TAGS is non-nil, don't include tags.
7298 When NO-TODO is non-nil, don't include TODO keywords."
7300 (org-back-to-heading t)
7302 ((and no-tags no-todo)
7303 (looking-at org-complex-heading-regexp)
7306 (looking-at (concat org-outline-regexp
7308 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7311 (looking-at org-todo-line-regexp)
7313 (t (looking-at org-heading-regexp)
7314 (match-string 2)))))
7316 (defun org-heading-components ()
7317 "Return the components of the current heading.
7318 This is a list with the following elements:
7319 - the level as an integer
7320 - the reduced level, different if `org-odd-levels-only' is set.
7321 - the TODO keyword, or nil
7322 - the priority character, like ?A, or nil if no priority is given
7323 - the headline text itself, or the tags string if no headline text
7324 - the tags string, or nil."
7326 (org-back-to-heading t)
7327 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
7328 (list (length (match-string 1))
7329 (org-reduced-level (length (match-string 1)))
7330 (org-match-string-no-properties 2)
7331 (and (match-end 3) (aref (match-string 3) 2))
7332 (org-match-string-no-properties 4)
7333 (org-match-string-no-properties 5)))))
7335 (defun org-get-entry ()
7336 "Get the entry text, after heading, entire subtree."
7338 (org-back-to-heading t)
7339 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7341 (defun org-insert-heading-after-current ()
7342 "Insert a new heading with same level as current, after current subtree."
7344 (org-back-to-heading)
7345 (org-insert-heading)
7346 (org-move-subtree-down)
7349 (defun org-insert-heading-respect-content ()
7351 (let ((org-insert-heading-respect-content t))
7352 (org-insert-heading t)))
7354 (defun org-insert-todo-heading-respect-content (&optional force-state)
7356 (let ((org-insert-heading-respect-content t))
7357 (org-insert-todo-heading force-state t)))
7359 (defun org-insert-todo-heading (arg &optional force-heading)
7360 "Insert a new heading with the same level and TODO state as current heading.
7361 If the heading has no TODO state, or if the state is DONE, use the first
7362 state (TODO by default). Also with prefix arg, force first state."
7364 (when (or force-heading (not (org-insert-item 'checkbox)))
7365 (org-insert-heading force-heading)
7367 (org-back-to-heading)
7368 (outline-previous-heading)
7369 (looking-at org-todo-line-regexp))
7373 (not (match-beginning 2))
7374 (member (match-string 2) org-done-keywords))
7375 (car org-todo-keywords-1)
7379 (run-hook-with-args-until-success
7380 'org-todo-get-default-hook new-mark-x nil)
7382 (beginning-of-line 1)
7383 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7384 (if org-treat-insert-todo-heading-as-state-change
7386 (insert new-mark " "))))
7387 (when org-provide-todo-statistics
7388 (org-update-parent-todo-statistics))))
7390 (defun org-insert-subheading (arg)
7391 "Insert a new subheading and demote it.
7392 Works for outline headings and for plain lists alike."
7394 (org-insert-heading arg)
7396 ((org-at-heading-p) (org-do-demote))
7397 ((org-at-item-p) (org-indent-item))))
7399 (defun org-insert-todo-subheading (arg)
7400 "Insert a new subheading with TODO keyword or checkbox and demote it.
7401 Works for outline headings and for plain lists alike."
7403 (org-insert-todo-heading arg)
7405 ((org-at-heading-p) (org-do-demote))
7406 ((org-at-item-p) (org-indent-item))))
7408 ;;; Promotion and Demotion
7410 (defvar org-after-demote-entry-hook nil
7411 "Hook run after an entry has been demoted.
7412 The cursor will be at the beginning of the entry.
7413 When a subtree is being demoted, the hook will be called for each node.")
7415 (defvar org-after-promote-entry-hook nil
7416 "Hook run after an entry has been promoted.
7417 The cursor will be at the beginning of the entry.
7418 When a subtree is being promoted, the hook will be called for each node.")
7420 (defun org-promote-subtree ()
7421 "Promote the entire subtree.
7422 See also `org-promote'."
7425 (org-with-limited-levels (org-map-tree 'org-promote)))
7426 (org-fix-position-after-promote))
7428 (defun org-demote-subtree ()
7429 "Demote the entire subtree. See `org-demote'.
7430 See also `org-promote'."
7433 (org-with-limited-levels (org-map-tree 'org-demote)))
7434 (org-fix-position-after-promote))
7437 (defun org-do-promote ()
7438 "Promote the current heading higher up the tree.
7439 If the region is active in `transient-mark-mode', promote all headings
7443 (if (org-region-active-p)
7444 (org-map-region 'org-promote (region-beginning) (region-end))
7446 (org-fix-position-after-promote))
7448 (defun org-do-demote ()
7449 "Demote the current heading lower down the tree.
7450 If the region is active in `transient-mark-mode', demote all headings
7454 (if (org-region-active-p)
7455 (org-map-region 'org-demote (region-beginning) (region-end))
7457 (org-fix-position-after-promote))
7459 (defun org-fix-position-after-promote ()
7460 "Make sure that after pro/demotion cursor position is right."
7461 (let ((pos (point)))
7462 (when (save-excursion
7463 (beginning-of-line 1)
7464 (looking-at org-todo-line-regexp)
7465 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7466 (cond ((eobp) (insert " "))
7467 ((eolp) (insert " "))
7468 ((equal (char-after) ?\ ) (forward-char 1))))))
7470 (defun org-current-level ()
7471 "Return the level of the current entry, or nil if before the first headline.
7472 The level is the number of stars at the beginning of the headline."
7474 (org-with-limited-levels
7475 (if (ignore-errors (org-back-to-heading t))
7476 (funcall outline-level)))))
7478 (defun org-get-previous-line-level ()
7479 "Return the outline depth of the last headline before the current line.
7480 Returns 0 for the first headline in the buffer, and nil if before the
7482 (let ((current-level (org-current-level))
7483 (prev-level (when (> (line-number-at-pos) 1)
7485 (beginning-of-line 0)
7486 (org-current-level)))))
7487 (cond ((null current-level) nil) ; Before first headline
7488 ((null prev-level) 0) ; At first headline
7491 (defun org-reduced-level (l)
7492 "Compute the effective level of a heading.
7493 This takes into account the setting of `org-odd-levels-only'."
7496 (org-odd-levels-only (1+ (floor (/ l 2))))
7499 (defun org-level-increment ()
7500 "Return the number of stars that will be added or removed at a
7501 time to headlines when structure editing, based on the value of
7502 `org-odd-levels-only'."
7503 (if org-odd-levels-only 2 1))
7505 (defun org-get-valid-level (level &optional change)
7506 "Rectify a level change under the influence of `org-odd-levels-only'
7507 LEVEL is a current level, CHANGE is by how much the level should be
7508 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7509 even level numbers will become the next higher odd number."
7510 (if org-odd-levels-only
7511 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7512 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7513 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7514 (max 1 (+ level (or change 0)))))
7516 (if (boundp 'define-obsolete-function-alias)
7517 (if (or (featurep 'xemacs) (< emacs-major-version 23))
7518 (define-obsolete-function-alias 'org-get-legal-level
7519 'org-get-valid-level)
7520 (define-obsolete-function-alias 'org-get-legal-level
7521 'org-get-valid-level "23.1")))
7523 (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
7524 ;; ̀org-with-limited-levels'
7525 (defun org-promote ()
7526 "Promote the current heading higher up the tree.
7527 If the region is active in `transient-mark-mode', promote all headings
7529 (org-back-to-heading t)
7530 (let* ((level (save-match-data (funcall outline-level)))
7531 (after-change-functions (remove 'flyspell-after-change-function
7532 after-change-functions))
7533 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7534 (diff (abs (- level (length up-head) -1))))
7535 (cond ((and (= level 1) org-called-with-limited-levels
7536 org-allow-promoting-top-level-subtree)
7537 (replace-match "# " nil t))
7539 (error "Cannot promote to level 0. UNDO to recover if necessary"))
7540 (t (replace-match up-head nil t)))
7541 ;; Fixup tag positioning
7543 (and org-auto-align-tags (org-set-tags nil t))
7544 (if org-adapt-indentation (org-fixup-indentation (- diff))))
7545 (run-hooks 'org-after-promote-entry-hook)))
7547 (defun org-demote ()
7548 "Demote the current heading lower down the tree.
7549 If the region is active in `transient-mark-mode', demote all headings
7551 (org-back-to-heading t)
7552 (let* ((level (save-match-data (funcall outline-level)))
7553 (after-change-functions (remove 'flyspell-after-change-function
7554 after-change-functions))
7555 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7556 (diff (abs (- level (length down-head) -1))))
7557 (replace-match down-head nil t)
7558 ;; Fixup tag positioning
7559 (and org-auto-align-tags (org-set-tags nil t))
7560 (if org-adapt-indentation (org-fixup-indentation diff))
7561 (run-hooks 'org-after-demote-entry-hook)))
7563 (defun org-cycle-level ()
7564 "Cycle the level of an empty headline through possible states.
7565 This goes first to child, then to parent, level, then up the hierarchy.
7566 After top level, it switches back to sibling level."
7568 (let ((org-adapt-indentation nil))
7569 (when (org-point-at-end-of-empty-headline)
7570 (setq this-command 'org-cycle-level) ; Only needed for caching
7571 (let ((cur-level (org-current-level))
7572 (prev-level (org-get-previous-line-level)))
7574 ;; If first headline in file, promote to top-level.
7576 (loop repeat (/ (- cur-level 1) (org-level-increment))
7577 do (org-do-promote)))
7578 ;; If same level as prev, demote one.
7579 ((= prev-level cur-level)
7581 ;; If parent is top-level, promote to top level if not already.
7583 (loop repeat (/ (- cur-level 1) (org-level-increment))
7584 do (org-do-promote)))
7585 ;; If top-level, return to prev-level.
7587 (loop repeat (/ (- prev-level 1) (org-level-increment))
7588 do (org-do-demote)))
7589 ;; If less than prev-level, promote one.
7590 ((< cur-level prev-level)
7592 ;; If deeper than prev-level, promote until higher than
7594 ((> cur-level prev-level)
7595 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7596 do (org-do-promote))))
7599 (defun org-map-tree (fun)
7600 "Call FUN for every heading underneath the current one."
7601 (org-back-to-heading)
7602 (let ((level (funcall outline-level)))
7606 (outline-next-heading)
7607 (> (funcall outline-level) level))
7611 (defun org-map-region (fun beg end)
7612 "Call FUN for every heading between BEG and END."
7613 (let ((org-ignore-region t))
7615 (setq end (copy-marker end))
7617 (if (and (re-search-forward org-outline-regexp-bol nil t)
7621 (outline-next-heading)
7626 (defvar org-property-end-re) ; silence byte-compiler
7627 (defun org-fixup-indentation (diff)
7628 "Change the indentation in the current entry by DIFF.
7629 However, if any line in the current entry has no indentation, or if it
7630 would end up with no indentation after the change, nothing at all is done."
7632 (let ((end (save-excursion (outline-next-heading)
7634 (prohibit (if (> diff 0)
7636 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7638 (unless (save-excursion (end-of-line 1)
7639 (re-search-forward prohibit end t))
7640 (while (and (< (point) end)
7641 (re-search-forward "^[ \t]+" end t))
7642 (goto-char (match-end 0))
7643 (setq col (current-column))
7644 (if (< diff 0) (replace-match ""))
7645 (org-indent-to-column (+ diff col))))
7646 (move-marker end nil))))
7648 (defun org-convert-to-odd-levels ()
7649 "Convert an org-mode file with all levels allowed to one with odd levels.
7650 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7653 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7654 (let ((outline-level 'org-outline-level)
7655 (org-odd-levels-only nil) n)
7657 (goto-char (point-min))
7658 (while (re-search-forward "^\\*\\*+ " nil t)
7659 (setq n (- (length (match-string 0)) 2))
7660 (while (>= (setq n (1- n)) 0)
7662 (end-of-line 1))))))
7664 (defun org-convert-to-oddeven-levels ()
7665 "Convert an org-mode file with only odd levels to one with odd/even levels.
7666 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7667 file contains a section with an even level, conversion would
7668 destroy the structure of the file. An error is signaled in this
7671 (goto-char (point-min))
7672 ;; First check if there are no even levels
7673 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7674 (org-show-context t)
7675 (error "Not all levels are odd in this file. Conversion not possible"))
7676 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7677 (let ((outline-regexp org-outline-regexp)
7678 (outline-level 'org-outline-level)
7679 (org-odd-levels-only nil) n)
7681 (goto-char (point-min))
7682 (while (re-search-forward "^\\*\\*+ " nil t)
7683 (setq n (/ (1- (length (match-string 0))) 2))
7684 (while (>= (setq n (1- n)) 0)
7686 (end-of-line 1))))))
7688 (defun org-tr-level (n)
7689 "Make N odd if required."
7690 (if org-odd-levels-only (1+ (/ n 2)) n))
7692 ;;; Vertical tree motion, cutting and pasting of subtrees
7694 (defun org-move-subtree-up (&optional arg)
7695 "Move the current subtree up past ARG headlines of the same level."
7697 (org-move-subtree-down (- (prefix-numeric-value arg))))
7699 (defun org-move-subtree-down (&optional arg)
7700 "Move the current subtree down past ARG headlines of the same level."
7702 (setq arg (prefix-numeric-value arg))
7703 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7704 'org-get-last-sibling))
7705 (ins-point (make-marker))
7707 (col (current-column))
7708 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7710 (org-back-to-heading)
7713 (setq ne-beg (org-back-over-empty-lines))
7716 (save-excursion (outline-end-of-heading)
7717 (setq folded (outline-invisible-p)))
7718 (outline-end-of-subtree))
7719 (outline-next-heading)
7720 (setq ne-end (org-back-over-empty-lines))
7723 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7724 ;; include less whitespace
7727 (forward-line (- ne-beg ne-end))
7728 (setq beg (point))))
7729 ;; Find insertion point, with error handling
7731 (or (and (funcall movfunc) (looking-at org-outline-regexp))
7732 (progn (goto-char beg0)
7733 (error "Cannot move past superior level or buffer limit")))
7734 (setq cnt (1- cnt)))
7736 ;; Moving forward - still need to move over subtree
7737 (progn (org-end-of-subtree t t)
7739 (org-back-over-empty-lines)
7740 (or (bolp) (newline)))))
7741 (setq ne-ins (org-back-over-empty-lines))
7742 (move-marker ins-point (point))
7743 (setq txt (buffer-substring beg end))
7744 (org-save-markers-in-region beg end)
7745 (delete-region beg end)
7746 (org-remove-empty-overlays-at beg)
7747 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7748 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7749 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7750 (let ((bbb (point)))
7751 (insert-before-markers txt)
7752 (org-reinstall-markers-in-region bbb)
7753 (move-marker ins-point bbb))
7754 (or (bolp) (insert "\n"))
7755 (setq ins-end (point))
7756 (goto-char ins-point)
7757 (org-skip-whitespace)
7758 (when (and (< arg 0)
7759 (org-first-sibling-p)
7761 ;; Move whitespace back to beginning
7764 (let ((kill-whole-line t))
7765 (kill-line (- ne-ins ne-beg)) (point)))
7766 (insert (make-string (- ne-ins ne-beg) ?\n)))
7767 (move-marker ins-point nil)
7772 (org-cycle-hide-drawers 'children))
7773 (org-clean-visibility-after-subtree-move)
7774 ;; move back to the initial column we were at
7775 (move-to-column col)))
7777 (defvar org-subtree-clip ""
7778 "Clipboard for cut and paste of subtrees.
7779 This is actually only a copy of the kill, because we use the normal kill
7780 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7782 (defvar org-subtree-clip-folded nil
7783 "Was the last copied subtree folded?
7784 This is used to fold the tree back after pasting.")
7786 (defun org-cut-subtree (&optional n)
7787 "Cut the current subtree into the clipboard.
7788 With prefix arg N, cut this many sequential subtrees.
7789 This is a short-hand for marking the subtree and then cutting it."
7791 (org-copy-subtree n 'cut))
7793 (defun org-copy-subtree (&optional n cut force-store-markers)
7794 "Cut the current subtree into the clipboard.
7795 With prefix arg N, cut this many sequential subtrees.
7796 This is a short-hand for marking the subtree and then copying it.
7797 If CUT is non-nil, actually cut the subtree.
7798 If FORCE-STORE-MARKERS is non-nil, store the relative locations
7799 of some markers in the region, even if CUT is non-nil. This is
7800 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
7802 (let (beg end folded (beg0 (point)))
7803 (if (org-called-interactively-p 'any)
7804 (org-back-to-heading nil) ; take what looks like a subtree
7805 (org-back-to-heading t)) ; take what is really there
7806 (org-back-over-empty-lines)
7808 (skip-chars-forward " \t\r\n")
7810 (save-excursion (outline-end-of-heading)
7811 (setq folded (outline-invisible-p)))
7813 (org-forward-same-level (1- n) t)
7815 (org-end-of-subtree t t))
7816 (org-back-over-empty-lines)
7820 (setq org-subtree-clip-folded folded)
7821 (when (or cut force-store-markers)
7822 (org-save-markers-in-region beg end))
7823 (if cut (kill-region beg end) (copy-region-as-kill beg end))
7824 (setq org-subtree-clip (current-kill 0))
7825 (message "%s: Subtree(s) with %d characters"
7826 (if cut "Cut" "Copied")
7827 (length org-subtree-clip)))))
7829 (defun org-paste-subtree (&optional level tree for-yank)
7830 "Paste the clipboard as a subtree, with modification of headline level.
7831 The entire subtree is promoted or demoted in order to match a new headline
7834 If the cursor is at the beginning of a headline, the same level as
7835 that headline is used to paste the tree
7837 If not, the new level is derived from the *visible* headings
7838 before and after the insertion point, and taken to be the inferior headline
7839 level of the two. So if the previous visible heading is level 3 and the
7840 next is level 4 (or vice versa), level 4 will be used for insertion.
7841 This makes sure that the subtree remains an independent subtree and does
7842 not swallow low level entries.
7844 You can also force a different level, either by using a numeric prefix
7845 argument, or by inserting the heading marker by hand. For example, if the
7846 cursor is after \"*****\", then the tree will be shifted to level 5.
7848 If optional TREE is given, use this text instead of the kill ring.
7850 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
7851 move back over whitespace before inserting, and move point to the end of
7852 the inserted text when done."
7854 (setq tree (or tree (and kill-ring (current-kill 0))))
7855 (unless (org-kill-is-subtree-p tree)
7857 (substitute-command-keys
7858 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
7859 (org-with-limited-levels
7860 (let* ((visp (not (outline-invisible-p)))
7862 (^re_ "\\(\\*+\\)[ \t]*")
7863 (old-level (if (string-match org-outline-regexp-bol txt)
7864 (- (match-end 0) (match-beginning 0) 1)
7866 (force-level (cond (level (prefix-numeric-value level))
7867 ((and (looking-at "[ \t]*$")
7869 "^\\*+$" (buffer-substring
7870 (point-at-bol) (point))))
7871 (- (match-end 1) (match-beginning 1)))
7873 (looking-at org-outline-regexp))
7874 (- (match-end 0) (point) 1))
7876 (previous-level (save-excursion
7879 (outline-previous-visible-heading 1)
7880 (if (looking-at ^re_)
7881 (- (match-end 0) (match-beginning 0) 1)
7884 (next-level (save-excursion
7887 (or (looking-at org-outline-regexp)
7888 (outline-next-visible-heading 1))
7889 (if (looking-at ^re_)
7890 (- (match-end 0) (match-beginning 0) 1)
7893 (new-level (or force-level (max previous-level next-level)))
7894 (shift (if (or (= old-level -1)
7896 (= old-level new-level))
7898 (- new-level old-level)))
7899 (delta (if (> shift 0) -1 1))
7900 (func (if (> shift 0) 'org-demote 'org-promote))
7901 (org-odd-levels-only nil)
7903 ;; Remove the forced level indicator
7905 (delete-region (point-at-bol) (point)))
7907 (beginning-of-line (if (bolp) 1 2))
7908 (unless for-yank (org-back-over-empty-lines))
7910 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
7911 (insert-before-markers txt)
7912 (unless (string-match "\n\\'" txt) (insert "\n"))
7913 (setq newend (point))
7914 (org-reinstall-markers-in-region beg)
7917 (skip-chars-forward " \t\n\r")
7919 (if (and (outline-invisible-p) visp)
7920 (save-excursion (outline-show-heading)))
7921 ;; Shift if necessary
7924 (narrow-to-region beg end)
7925 (while (not (= shift 0))
7926 (org-map-region func (point-min) (point-max))
7927 (setq shift (+ delta shift)))
7928 (goto-char (point-min))
7929 (setq newend (point-max))))
7930 (when (or (org-called-interactively-p 'interactive) for-yank)
7931 (message "Clipboard pasted as level %d subtree" new-level))
7932 (if (and (not for-yank) ; in this case, org-yank will decide about folding
7934 (eq org-subtree-clip (current-kill 0))
7935 org-subtree-clip-folded)
7936 ;; The tree was folded before it was killed/copied
7938 (and for-yank (goto-char newend)))))
7940 (defun org-kill-is-subtree-p (&optional txt)
7941 "Check if the current kill is an outline subtree, or a set of trees.
7942 Returns nil if kill does not start with a headline, or if the first
7943 headline level is not the largest headline level in the tree.
7944 So this will actually accept several entries of equal levels as well,
7945 which is OK for `org-paste-subtree'.
7946 If optional TXT is given, check this string instead of the current kill."
7947 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
7948 (re (org-get-limited-outline-regexp))
7949 (^re (concat "^" re))
7950 (start-level (and kill
7952 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
7954 (- (match-end 2) (match-beginning 2) 1)))
7955 (start (1+ (or (match-beginning 2) -1))))
7956 (if (not start-level)
7958 nil) ;; does not even start with a heading
7960 (while (setq start (string-match ^re kill (1+ start)))
7961 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
7965 (defvar org-markers-to-move nil
7966 "Markers that should be moved with a cut-and-paste operation.
7967 Those markers are stored together with their positions relative to
7968 the start of the region.")
7970 (defun org-save-markers-in-region (beg end)
7971 "Check markers in region.
7972 If these markers are between BEG and END, record their position relative
7973 to BEG, so that after moving the block of text, we can put the markers back
7975 This function gets called just before an entry or tree gets cut from the
7976 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
7977 called immediately, to move the markers with the entries."
7978 (setq org-markers-to-move nil)
7979 (when (featurep 'org-clock)
7980 (org-clock-save-markers-for-cut-and-paste beg end))
7981 (when (featurep 'org-agenda)
7982 (org-agenda-save-markers-for-cut-and-paste beg end)))
7984 (defun org-check-and-save-marker (marker beg end)
7985 "Check if MARKER is between BEG and END.
7986 If yes, remember the marker and the distance to BEG."
7987 (when (and (marker-buffer marker)
7988 (equal (marker-buffer marker) (current-buffer)))
7989 (if (and (>= marker beg) (< marker end))
7990 (push (cons marker (- marker beg)) org-markers-to-move))))
7992 (defun org-reinstall-markers-in-region (beg)
7993 "Move all remembered markers to their position relative to BEG."
7995 (move-marker (car x) (+ beg (cdr x))))
7996 org-markers-to-move)
7997 (setq org-markers-to-move nil))
7999 (defun org-narrow-to-subtree ()
8000 "Narrow buffer to the current subtree."
8004 (org-with-limited-levels
8006 (progn (org-back-to-heading t) (point))
8007 (progn (org-end-of-subtree t t)
8008 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
8011 (defun org-narrow-to-block ()
8012 "Narrow buffer to the current block."
8014 (let* ((case-fold-search t)
8015 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
8016 "^[ \t]*#\\+end_.*")))
8018 (narrow-to-region (car blockp) (cdr blockp))
8019 (error "Not in a block"))))
8022 (defvar org-property-drawer-re))
8024 (defvar org-property-start-re) ;; defined below
8025 (defun org-clone-subtree-with-time-shift (n &optional shift)
8026 "Clone the task (subtree) at point N times.
8027 The clones will be inserted as siblings.
8029 In interactive use, the user will be prompted for the number of
8030 clones to be produced, and for a time SHIFT, which may be a
8031 repeater as used in time stamps, for example `+3d'.
8033 When a valid repeater is given and the entry contains any time
8034 stamps, the clones will become a sequence in time, with time
8035 stamps in the subtree shifted for each clone produced. If SHIFT
8036 is nil or the empty string, time stamps will be left alone. The
8037 ID property of the original subtree is removed.
8039 If the original subtree did contain time stamps with a repeater,
8040 the following will happen:
8041 - the repeater will be removed in each clone
8042 - an additional clone will be produced, with the current, unshifted
8043 date(s) in the entry.
8044 - the original entry will be placed *after* all the clones, with
8046 - the start days in the repeater in the original entry will be shifted
8047 to past the last clone.
8048 In this way you can spell out a number of instances of a repeating task,
8049 and still retain the repeater to cover future instances of the task."
8050 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
8051 (let (beg end template task idprop
8052 shift-n shift-what doshift nmin nmax (n-no-remove -1)
8053 (drawer-re org-drawer-regexp))
8054 (if (not (and (integerp n) (> n 0)))
8055 (error "Invalid number of replications %s" n))
8056 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
8057 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
8059 (error "Invalid shift specification %s" shift))
8061 (setq shift-n (string-to-number (match-string 1 shift))
8062 shift-what (cdr (assoc (match-string 2 shift)
8063 '(("d" . day) ("w" . week)
8064 ("m" . month) ("y" . year))))))
8065 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
8066 (setq nmin 1 nmax n)
8067 (org-back-to-heading t)
8069 (setq idprop (org-entry-get nil "ID"))
8070 (org-end-of-subtree t t)
8071 (or (bolp) (insert "\n"))
8073 (setq template (buffer-substring beg end))
8075 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
8076 (delete-region beg end)
8078 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
8080 (loop for n from nmin to nmax do
8085 (goto-char (point-min))
8087 (and idprop (if org-clone-delete-id
8088 (org-entry-delete nil "ID")
8089 (org-id-get-create t)))
8091 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
8093 (goto-char (point-min))
8094 (while (re-search-forward drawer-re nil t)
8096 (org-remove-empty-drawer-at d (point))) org-drawers)))
8097 (goto-char (point-min))
8099 (while (re-search-forward org-ts-regexp-both nil t)
8100 (org-timestamp-change (* n shift-n) shift-what))
8101 (unless (= n n-no-remove)
8102 (goto-char (point-min))
8103 (while (re-search-forward org-ts-regexp nil t)
8105 (goto-char (match-beginning 0))
8106 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
8107 (delete-region (match-beginning 1) (match-end 1)))))))
8108 (setq task (buffer-string)))
8114 (defun org-sort (with-case)
8115 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
8116 Optional argument WITH-CASE means sort case-sensitively."
8119 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
8120 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
8122 (org-call-with-arg 'org-sort-entries with-case))))
8124 (defun org-sort-remove-invisible (s)
8125 (remove-text-properties 0 (length s) org-rm-props s)
8126 (while (string-match org-bracket-link-regexp s)
8127 (setq s (replace-match (if (match-end 2)
8129 (match-string 1 s)) t t s)))
8132 (defvar org-priority-regexp) ; defined later in the file
8134 (defvar org-after-sorting-entries-or-items-hook nil
8135 "Hook that is run after a bunch of entries or items have been sorted.
8136 When children are sorted, the cursor is in the parent line when this
8137 hook gets called. When a region or a plain list is sorted, the cursor
8138 will be in the first entry of the sorted region/list.")
8140 (defun org-sort-entries
8141 (&optional with-case sorting-type getkey-func compare-func property)
8142 "Sort entries on a certain level of an outline tree.
8143 If there is an active region, the entries in the region are sorted.
8144 Else, if the cursor is before the first entry, sort the top-level items.
8145 Else, the children of the entry at point are sorted.
8147 Sorting can be alphabetically, numerically, by date/time as given by
8148 a time stamp, by a property or by priority.
8150 The command prompts for the sorting type unless it has been given to the
8151 function through the SORTING-TYPE argument, which needs to be a character,
8152 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
8153 precise meaning of each character:
8155 n Numerically, by converting the beginning of the entry/item to a number.
8156 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8157 t By date/time, either the first active time stamp in the entry, or, if
8158 none exist, by the first inactive one.
8159 s By the scheduled date/time.
8160 d By deadline date/time.
8161 c By creation time, which is assumed to be the first inactive time stamp
8162 at the beginning of a line.
8163 p By priority according to the cookie.
8164 r By the value of a property.
8166 Capital letters will reverse the sort order.
8168 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8169 called with point at the beginning of the record. It must return either
8170 a string or a number that should serve as the sorting key for that record.
8172 Comparing entries ignores case by default. However, with an optional argument
8173 WITH-CASE, the sorting considers case as well."
8175 (let ((case-func (if with-case 'identity 'downcase))
8176 start beg end stars re re2
8178 ;; Find beginning and end of region to sort
8180 ((org-region-active-p)
8181 ;; we will sort the region
8182 (setq end (region-end)
8184 (goto-char (region-beginning))
8185 (if (not (org-at-heading-p)) (outline-next-heading))
8186 (setq start (point)))
8187 ((or (org-at-heading-p)
8188 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8189 ;; we will sort the children of the current headline
8190 (org-back-to-heading)
8192 end (progn (org-end-of-subtree t t)
8193 (or (bolp) (insert "\n"))
8194 (org-back-over-empty-lines)
8199 (outline-next-heading))
8201 ;; we will sort the top-level entries in this file
8202 (goto-char (point-min))
8203 (or (org-at-heading-p) (outline-next-heading))
8204 (setq start (point))
8205 (goto-char (point-max))
8206 (beginning-of-line 1)
8207 (when (looking-at ".*?\\S-")
8208 ;; File ends in a non-white line
8211 (setq end (point-max))
8212 (setq what "top-level")
8217 (if (>= beg end) (error "Nothing to sort"))
8219 (looking-at "\\(\\*+\\)")
8220 (setq stars (match-string 1)
8221 re (concat "^" (regexp-quote stars) " +")
8222 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8223 txt (buffer-substring beg end))
8224 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8225 (if (and (not (equal stars "*")) (string-match re2 txt))
8226 (error "Region to sort contains a level above the first entry"))
8228 (unless sorting-type
8230 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8231 [t]ime
[s]cheduled [d]eadline [c]reated
8232 A/N/T/S/D/C/P/O/F means reversed:"
8234 (setq sorting-type (read-char-exclusive))
8236 (and (= (downcase sorting-type) ?f)
8238 (org-icompleting-read "Sort using function: "
8239 obarray 'fboundp t nil nil))
8240 (setq getkey-func (intern getkey-func)))
8242 (and (= (downcase sorting-type) ?r)
8244 (org-icompleting-read "Property: "
8245 (mapcar 'list (org-buffer-property-keys t))
8248 (message "Sorting entries...")
8251 (narrow-to-region start end)
8252 (let ((dcst (downcase sorting-type))
8253 (case-fold-search nil)
8254 (now (current-time)))
8256 (/= dcst sorting-type)
8257 ;; This function moves to the beginning character of the "record" to
8260 (if (re-search-forward re nil t)
8261 (goto-char (match-beginning 0))
8262 (goto-char (point-max))))
8263 ;; This function moves to the last character of the "record" being
8268 (outline-forward-same-level 1)
8270 (goto-char (point-max))))))
8271 ;; This function returns the value that gets sorted against.
8275 (if (looking-at org-complex-heading-regexp)
8276 (string-to-number (match-string 4))
8279 (if (looking-at org-complex-heading-regexp)
8280 (funcall case-func (match-string 4))
8283 (let ((end (save-excursion (outline-next-heading) (point))))
8284 (if (or (re-search-forward org-ts-regexp end t)
8285 (re-search-forward org-ts-regexp-both end t))
8286 (org-time-string-to-seconds (match-string 0))
8287 (org-float-time now))))
8289 (let ((end (save-excursion (outline-next-heading) (point))))
8290 (if (re-search-forward
8291 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8293 (org-time-string-to-seconds (match-string 0))
8294 (org-float-time now))))
8296 (let ((end (save-excursion (outline-next-heading) (point))))
8297 (if (re-search-forward org-scheduled-time-regexp end t)
8298 (org-time-string-to-seconds (match-string 1))
8299 (org-float-time now))))
8301 (let ((end (save-excursion (outline-next-heading) (point))))
8302 (if (re-search-forward org-deadline-time-regexp end t)
8303 (org-time-string-to-seconds (match-string 1))
8304 (org-float-time now))))
8306 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8307 (string-to-char (match-string 2))
8308 org-default-priority))
8310 (or (org-entry-get nil property) ""))
8312 (if (looking-at org-complex-heading-regexp)
8313 (- 9999 (length (member (match-string 2)
8314 org-todo-keywords-1)))))
8318 (setq tmp (funcall getkey-func))
8319 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8321 (error "Invalid key function `%s'" getkey-func)))
8322 (t (error "Invalid sorting type `%c'" sorting-type))))
8325 ((= dcst ?a) 'string<)
8326 ((= dcst ?f) compare-func)
8327 ((member dcst '(?p ?t ?s ?d ?c)) '<)
8329 (run-hooks 'org-after-sorting-entries-or-items-hook)
8330 (message "Sorting entries...done")))
8332 (defun org-do-sort (table what &optional with-case sorting-type)
8333 "Sort TABLE of WHAT according to SORTING-TYPE.
8334 The user will be prompted for the SORTING-TYPE if the call to this
8335 function does not specify it. WHAT is only for the prompt, to indicate
8336 what is being sorted. The sorting key will be extracted from
8337 the car of the elements of the table.
8338 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8339 (unless sorting-type
8341 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
8343 (setq sorting-type (read-char-exclusive)))
8344 (let ((dcst (downcase sorting-type))
8345 extractfun comparefun)
8346 ;; Define the appropriate functions
8349 (setq extractfun 'string-to-number
8350 comparefun (if (= dcst sorting-type) '< '>)))
8352 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8353 (lambda(x) (downcase (org-sort-remove-invisible x))))
8354 comparefun (if (= dcst sorting-type)
8356 (lambda (a b) (and (not (string< a b))
8357 (not (string= a b)))))))
8361 (if (or (string-match org-ts-regexp x)
8362 (string-match org-ts-regexp-both x))
8364 (org-time-string-to-time (match-string 0 x)))
8366 comparefun (if (= dcst sorting-type) '< '>)))
8367 (t (error "Invalid sorting type `%c'" sorting-type)))
8369 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8371 (lambda (a b) (funcall comparefun (car a) (car b))))))
8374 ;;; The orgstruct minor mode
8376 ;; Define a minor mode which can be used in other modes in order to
8377 ;; integrate the org-mode structure editing commands.
8379 ;; This is really a hack, because the org-mode structure commands use
8380 ;; keys which normally belong to the major mode. Here is how it
8381 ;; works: The minor mode defines all the keys necessary to operate the
8382 ;; structure commands, but wraps the commands into a function which
8383 ;; tests if the cursor is currently at a headline or a plain list
8384 ;; item. If that is the case, the structure command is used,
8385 ;; temporarily setting many Org-mode variables like regular
8386 ;; expressions for filling etc. However, when any of those keys is
8387 ;; used at a different location, function uses `key-binding' to look
8388 ;; up if the key has an associated command in another currently active
8389 ;; keymap (minor modes, major mode, global), and executes that
8390 ;; command. There might be problems if any of the keys is otherwise
8391 ;; used as a prefix key.
8393 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
8394 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
8395 ;; addresses this by checking explicitly for both bindings.
8397 (defvar orgstruct-mode-map (make-sparse-keymap)
8398 "Keymap for the minor `orgstruct-mode'.")
8400 (defvar org-local-vars nil
8401 "List of local variables, for use by `orgstruct-mode'.")
8404 (define-minor-mode orgstruct-mode
8405 "Toggle the minor mode `orgstruct-mode'.
8406 This mode is for using Org-mode structure commands in other
8407 modes. The following keys behave as if Org-mode were active, if
8408 the cursor is on a headline, or on a plain list item (both as
8409 defined by Org-mode).
8411 M-up Move entry/item up
8412 M-down Move entry/item down
8415 M-S-up Move entry/item up
8416 M-S-down Move entry/item down
8417 M-S-left Promote subtree
8418 M-S-right Demote subtree
8419 M-q Fill paragraph and items like in Org-mode
8421 C-c - Cycle list bullet
8422 TAB Cycle item visibility
8423 M-RET Insert new heading/item
8424 S-M-RET Insert new TODO heading / Checkbox item
8425 C-c C-c Set tags / toggle checkbox"
8426 nil " OrgStruct" nil
8427 (org-load-modules-maybe)
8428 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
8431 (defun turn-on-orgstruct ()
8432 "Unconditionally turn on `orgstruct-mode'."
8435 (defvar org-fb-vars nil)
8436 (make-variable-buffer-local 'org-fb-vars)
8437 (defun orgstruct++-mode (&optional arg)
8438 "Toggle `orgstruct-mode', the enhanced version of it.
8439 In addition to setting orgstruct-mode, this also exports all
8440 indentation and autofilling variables from org-mode into the
8441 buffer. It will also recognize item context in multiline items."
8443 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8445 (progn (orgstruct-mode -1)
8447 (org-set-local (car v)
8448 (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
8451 (setq org-fb-vars nil)
8456 "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
8457 (symbol-name (car x)))
8458 (setq var (car x) val (nth 1 x))
8459 (push (list var `(quote ,(eval var))) org-fb-vars)
8460 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8462 (org-set-local 'orgstruct-is-++ t))))
8464 (defvar orgstruct-is-++ nil
8465 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8466 (make-variable-buffer-local 'orgstruct-is-++)
8469 (defun turn-on-orgstruct++ ()
8470 "Unconditionally turn on `orgstruct++-mode'."
8471 (orgstruct++-mode 1))
8473 (defun orgstruct-error ()
8474 "Error when there is no default binding for a structure key."
8476 (error "This key has no function outside structure elements"))
8478 (defun orgstruct-setup ()
8479 "Setup orgstruct keymaps."
8483 '([(meta up)] org-metaup)
8484 '([(meta down)] org-metadown)
8485 '([(meta left)] org-metaleft)
8486 '([(meta right)] org-metaright)
8487 '([(meta shift up)] org-shiftmetaup)
8488 '([(meta shift down)] org-shiftmetadown)
8489 '([(meta shift left)] org-shiftmetaleft)
8490 '([(meta shift right)] org-shiftmetaright)
8491 '([?\e (up)] org-metaup)
8492 '([?\e (down)] org-metadown)
8493 '([?\e (left)] org-metaleft)
8494 '([?\e (right)] org-metaright)
8495 '([?\e (shift up)] org-shiftmetaup)
8496 '([?\e (shift down)] org-shiftmetadown)
8497 '([?\e (shift left)] org-shiftmetaleft)
8498 '([?\e (shift right)] org-shiftmetaright)
8499 '([(shift up)] org-shiftup)
8500 '([(shift down)] org-shiftdown)
8501 '([(shift left)] org-shiftleft)
8502 '([(shift right)] org-shiftright)
8503 '("\C-c\C-c" org-ctrl-c-ctrl-c)
8504 '("\M-q" fill-paragraph)
8506 '("\C-c-" org-cycle-list-bullet)))
8508 (while (setq elt (pop bindings))
8509 (setq nfunc (1+ nfunc))
8510 (setq key (org-key (car elt))
8512 cmd (orgstruct-make-binding fun nfunc key))
8513 (org-defkey orgstruct-mode-map key cmd))
8515 ;; Special treatment needed for TAB and RET
8516 (org-defkey orgstruct-mode-map [(tab)]
8517 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
8518 (org-defkey orgstruct-mode-map "\C-i"
8519 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
8521 (org-defkey orgstruct-mode-map "\M-\C-m"
8522 (orgstruct-make-binding 'org-insert-heading 105
8523 "\M-\C-m" [(meta return)]))
8524 (org-defkey orgstruct-mode-map [(meta return)]
8525 (orgstruct-make-binding 'org-insert-heading 106
8526 [(meta return)] "\M-\C-m"))
8528 (org-defkey orgstruct-mode-map [(shift meta return)]
8529 (orgstruct-make-binding 'org-insert-todo-heading 107
8530 [(meta return)] "\M-\C-m"))
8532 (org-defkey orgstruct-mode-map "\e\C-m"
8533 (orgstruct-make-binding 'org-insert-heading 108
8534 "\e\C-m" [?\e (return)]))
8535 (org-defkey orgstruct-mode-map [?\e (return)]
8536 (orgstruct-make-binding 'org-insert-heading 109
8537 [?\e (return)] "\e\C-m"))
8538 (org-defkey orgstruct-mode-map [?\e (shift return)]
8539 (orgstruct-make-binding 'org-insert-todo-heading 110
8540 [?\e (return)] "\e\C-m"))
8542 (unless org-local-vars
8543 (setq org-local-vars (org-get-local-variables)))
8547 (defun orgstruct-make-binding (fun n &rest keys)
8548 "Create a function for binding in the structure minor mode.
8549 FUN is the command to call inside a table. N is used to create a unique
8550 command name. KEYS are keys that should be checked in for a command
8551 to execute outside of tables."
8554 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
8556 (concat "In Structure, run `" (symbol-name fun) "'.\n"
8557 "Outside of structure, run the binding of `"
8558 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
8562 `(org-context-p 'headline 'item
8563 (and orgstruct-is-++
8564 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
8566 (list 'org-run-like-in-org-mode (list 'quote fun))
8567 (list 'let '(orgstruct-mode)
8568 (list 'call-interactively
8571 (list 'key-binding k))
8573 '('orgstruct-error))))))))
8575 (defun org-context-p (&rest contexts)
8576 "Check if local context is any of CONTEXTS.
8577 Possible values in the list of contexts are `table', `headline', and `item'."
8578 (let ((pos (point)))
8579 (goto-char (point-at-bol))
8580 (prog1 (or (and (memq 'table contexts)
8581 (looking-at "[ \t]*|"))
8582 (and (memq 'headline contexts)
8583 (looking-at org-outline-regexp))
8584 (and (memq 'item contexts)
8585 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
8586 (and (memq 'item-body contexts)
8590 (defun org-get-local-variables ()
8591 "Return a list of all local variables in an Org mode buffer."
8593 (with-current-buffer (get-buffer-create "*Org tmp*")
8596 (setq varlist (buffer-local-variables)))
8597 (kill-buffer "*Org tmp*")
8604 (list (car x) (list 'quote (cdr x)))))
8606 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
8607 (symbol-name (car x)))
8611 (defun org-clone-local-variables (from-buffer &optional regexp)
8612 "Clone local variables from FROM-BUFFER.
8613 Optional argument REGEXP selects variables to clone."
8616 (and (symbolp (car pair))
8618 (string-match regexp (symbol-name (car pair))))
8619 (set (make-local-variable (car pair))
8621 (buffer-local-variables from-buffer)))
8624 (defun org-run-like-in-org-mode (cmd)
8625 "Run a command, pretending that the current buffer is in Org-mode.
8626 This will temporarily bind local variables that are typically bound in
8627 Org-mode to the values they have in Org-mode, and then interactively
8629 (org-load-modules-maybe)
8630 (unless org-local-vars
8631 (setq org-local-vars (org-get-local-variables)))
8632 (eval (list 'let org-local-vars
8633 (list 'call-interactively (list 'quote cmd)))))
8637 (defun org-get-category (&optional pos force-refresh)
8638 "Get the category applying to position POS."
8640 (if force-refresh (org-refresh-category-properties))
8641 (let ((pos (or pos (point))))
8642 (or (get-text-property pos 'org-category)
8643 (progn (org-refresh-category-properties)
8644 (get-text-property pos 'org-category))))))
8646 (defun org-refresh-category-properties ()
8647 "Refresh category text properties in the buffer."
8648 (let ((inhibit-read-only t)
8650 ((null org-category)
8651 (if buffer-file-name
8652 (file-name-sans-extension
8653 (file-name-nondirectory buffer-file-name))
8655 ((symbolp org-category) (symbol-name org-category))
8657 beg end cat pos optionp)
8662 (goto-char (point-min))
8663 (put-text-property (point) (point-max) 'org-category def-cat)
8664 (while (re-search-forward
8665 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8666 (setq pos (match-end 0)
8667 optionp (equal (char-after (match-beginning 0)) ?#)
8668 cat (org-trim (match-string 2)))
8670 (setq beg (point-at-bol) end (point-max))
8671 (org-back-to-heading t)
8672 (setq beg (point) end (org-end-of-subtree t t)))
8673 (put-text-property beg end 'org-category cat)
8674 (put-text-property beg end 'org-category-position beg)
8675 (goto-char pos)))))))
8680 ;;; Link abbreviations
8682 (defun org-link-expand-abbrev (link)
8683 "Apply replacements as defined in `org-link-abbrev-alist'."
8684 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
8685 (let* ((key (match-string 1 link))
8686 (as (or (assoc key org-link-abbrev-alist-local)
8687 (assoc key org-link-abbrev-alist)))
8688 (tag (and (match-end 2) (match-string 3 link)))
8694 ((symbolp rpl) (funcall rpl tag))
8695 ((string-match "%(\\([^)]+\\))" rpl)
8696 (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
8697 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
8698 ((string-match "%h" rpl)
8699 (replace-match (url-hexify-string (or tag "")) t t rpl))
8700 (t (concat rpl tag)))))
8703 ;;; Storing and inserting links
8705 (defvar org-insert-link-history nil
8706 "Minibuffer history for links inserted with `org-insert-link'.")
8708 (defvar org-stored-links nil
8709 "Contains the links stored with `org-store-link'.")
8711 (defvar org-store-link-plist nil
8712 "Plist with info about the most recently link created with `org-store-link'.")
8714 (defvar org-link-protocols nil
8715 "Link protocols added to Org-mode using `org-add-link-type'.")
8717 (defvar org-store-link-functions nil
8718 "List of functions that are called to create and store a link.
8719 Each function will be called in turn until one returns a non-nil
8720 value. Each function should check if it is responsible for creating
8721 this link (for example by looking at the major mode).
8722 If not, it must exit and return nil.
8723 If yes, it should return a non-nil value after a calling
8724 `org-store-link-props' with a list of properties and values.
8725 Special properties are:
8727 :type The link prefix, like \"http\". This must be given.
8728 :link The link, like \"http://www.astro.uva.nl/~dominik\".
8729 This is obligatory as well.
8730 :description Optional default description for the second pair
8731 of brackets in an Org-mode link. The user can still change
8732 this when inserting this link into an Org-mode buffer.
8734 In addition to these, any additional properties can be specified
8735 and then used in remember templates.")
8737 (defun org-add-link-type (type &optional follow export)
8738 "Add TYPE to the list of `org-link-types'.
8739 Re-compute all regular expressions depending on `org-link-types'
8741 FOLLOW and EXPORT are two functions.
8743 FOLLOW should take the link path as the single argument and do whatever
8744 is necessary to follow the link, for example find a file or display
8747 EXPORT should format the link path for export to one of the export formats.
8748 It should be a function accepting three arguments:
8750 path the path of the link, the text after the prefix (like \"http:\")
8751 desc the description of the link, if any, or a description added by
8752 org-export-normalize-links if there is none
8753 format the export format, a symbol like `html' or `latex' or `ascii'..
8755 The function may use the FORMAT information to return different values
8756 depending on the format. The return value will be put literally into
8757 the exported file. If the return value is nil, this means Org should
8758 do what it normally does with links which do not have EXPORT defined.
8760 Org-mode has a built-in default for exporting links. If you are happy with
8761 this default, there is no need to define an export function for the link
8762 type. For a simple example of an export function, see `org-bbdb.el'."
8763 (add-to-list 'org-link-types type t)
8764 (org-make-link-regexps)
8765 (if (assoc type org-link-protocols)
8766 (setcdr (assoc type org-link-protocols) (list follow export))
8767 (push (list type follow export) org-link-protocols)))
8769 (defvar org-agenda-buffer-name)
8772 (defun org-store-link (arg)
8773 "\\<org-mode-map>Store an org-link to the current location.
8774 This link is added to `org-stored-links' and can later be inserted
8775 into an org-buffer with \\[org-insert-link].
8777 For some link types, a prefix arg is interpreted:
8778 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
8779 For file links, arg negates `org-context-in-file-links'."
8781 (org-load-modules-maybe)
8782 (setq org-store-link-plist nil) ; reset
8783 (org-with-limited-levels
8784 (let (link cpltxt desc description search txt custom-id agenda-link)
8787 ((run-hook-with-args-until-success 'org-store-link-functions)
8788 (setq link (plist-get org-store-link-plist :link)
8789 desc (or (plist-get org-store-link-plist :description) link)))
8791 ((org-src-edit-buffer-p)
8793 (while (or (not label)
8797 (goto-char (point-min))
8799 (regexp-quote (format org-coderef-label-format label))
8801 (when label (message "Label exists already") (sit-for 2))
8802 (setq label (read-string "Code line label: " label)))
8804 (setq link (format org-coderef-label-format label))
8805 (setq gc (- 79 (length link)))
8806 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
8808 (setq link (concat "(" label ")") desc nil)))
8810 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
8811 ;; We are in the agenda, link to referenced location
8812 (let ((m (or (get-text-property (point) 'org-hd-marker)
8813 (get-text-property (point) 'org-marker))))
8815 (org-with-point-at m
8817 (if (org-called-interactively-p 'any)
8818 (call-interactively 'org-store-link)
8819 (org-store-link nil)))))))
8821 ((eq major-mode 'calendar-mode)
8822 (let ((cd (calendar-cursor-to-date)))
8825 (car org-time-stamp-formats)
8827 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8829 (org-store-link-props :type "calendar" :date cd)))
8831 ((eq major-mode 'w3-mode)
8832 (setq cpltxt (if (and (buffer-name)
8833 (not (string-match "Untitled" (buffer-name))))
8836 link (org-make-link (url-view-url t)))
8837 (org-store-link-props :type "w3" :url (url-view-url t)))
8839 ((eq major-mode 'w3m-mode)
8840 (setq cpltxt (or w3m-current-title w3m-current-url)
8841 link (org-make-link w3m-current-url))
8842 (org-store-link-props :type "w3m" :url (url-view-url t)))
8844 ((setq search (run-hook-with-args-until-success
8845 'org-create-file-search-functions))
8846 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8848 (setq cpltxt (or description link)))
8850 ((eq major-mode 'image-mode)
8851 (setq cpltxt (concat "file:"
8852 (abbreviate-file-name buffer-file-name))
8853 link (org-make-link cpltxt))
8854 (org-store-link-props :type "image" :file buffer-file-name))
8856 ((eq major-mode 'dired-mode)
8857 ;; link to the file in the current line
8858 (let ((file (dired-get-filename nil t)))
8860 (abbreviate-file-name
8861 (expand-file-name (dired-get-filename nil t)))
8862 ;; otherwise, no file so use current directory.
8864 (setq cpltxt (concat "file:" file)
8865 link (org-make-link cpltxt))))
8867 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
8868 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
8870 ((org-in-regexp "<<\\(.*?\\)>>")
8873 (abbreviate-file-name
8874 (buffer-file-name (buffer-base-buffer)))
8875 "::" (match-string 1))
8876 link (org-make-link cpltxt)))
8877 ((and (featurep 'org-id)
8878 (or (eq org-link-to-org-use-id t)
8879 (and (eq org-link-to-org-use-id 'create-if-interactive)
8880 (org-called-interactively-p 'any))
8881 (and (eq org-link-to-org-use-id
8882 'create-if-interactive-and-no-custom-id)
8883 (org-called-interactively-p 'any)
8885 (and org-link-to-org-use-id
8886 (org-entry-get nil "ID"))))
8887 ;; We can make a link using the ID.
8888 (setq link (condition-case nil
8889 (prog1 (org-id-store-link)
8890 (setq desc (plist-get org-store-link-plist
8893 ;; probably before first headline, link to file only
8895 (abbreviate-file-name
8896 (buffer-file-name (buffer-base-buffer))))))))
8898 ;; Just link to current headline
8899 (setq cpltxt (concat "file:"
8900 (abbreviate-file-name
8901 (buffer-file-name (buffer-base-buffer)))))
8902 ;; Add a context search string
8903 (when (org-xor org-context-in-file-links arg)
8905 ((org-at-heading-p) nil)
8906 ((org-region-active-p)
8907 (buffer-substring (region-beginning) (region-end)))
8909 (when (or (null txt) (string-match "\\S-" txt))
8913 (org-make-org-heading-search-string txt)
8915 desc (or (nth 4 (ignore-errors
8916 (org-heading-components))) "NONE"))))
8917 (if (string-match "::\\'" cpltxt)
8918 (setq cpltxt (substring cpltxt 0 -2)))
8919 (setq link (org-make-link cpltxt)))))
8921 ((buffer-file-name (buffer-base-buffer))
8922 ;; Just link to this file here.
8923 (setq cpltxt (concat "file:"
8924 (abbreviate-file-name
8925 (buffer-file-name (buffer-base-buffer)))))
8926 ;; Add a context string
8927 (when (org-xor org-context-in-file-links arg)
8928 (setq txt (if (org-region-active-p)
8929 (buffer-substring (region-beginning) (region-end))
8930 (buffer-substring (point-at-bol) (point-at-eol))))
8931 ;; Only use search option if there is some text.
8932 (when (string-match "\\S-" txt)
8934 (concat cpltxt "::" (org-make-org-heading-search-string txt))
8936 (setq link (org-make-link cpltxt)))
8938 ((org-called-interactively-p 'interactive)
8939 (error "Cannot link to a buffer which is not visiting a file"))
8941 (t (setq link nil)))
8943 (if (consp link) (setq cpltxt (car link) link (cdr link)))
8944 (setq link (or link cpltxt)
8945 desc (or desc cpltxt))
8946 (if (equal desc "NONE") (setq desc nil))
8948 (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
8950 (setq org-stored-links
8951 (cons (list link desc) org-stored-links))
8952 (message "Stored: %s" (or desc link))
8954 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
8956 (setq org-stored-links
8957 (cons (list link desc) org-stored-links))))
8958 (or agenda-link (and link (org-make-link-string link desc)))))))
8960 (defun org-store-link-props (&rest plist)
8961 "Store link properties, extract names and addresses."
8963 (when (setq x (plist-get plist :from))
8964 (setq adr (mail-extract-address-components x))
8965 (setq plist (plist-put plist :fromname (car adr)))
8966 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
8967 (when (setq x (plist-get plist :to))
8968 (setq adr (mail-extract-address-components x))
8969 (setq plist (plist-put plist :toname (car adr)))
8970 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
8971 (let ((from (plist-get plist :from))
8972 (to (plist-get plist :to)))
8973 (when (and from to org-from-is-user-regexp)
8975 (plist-put plist :fromto
8976 (if (string-match org-from-is-user-regexp from)
8978 (concat "from %f"))))))
8979 (setq org-store-link-plist plist))
8981 (defun org-add-link-props (&rest plist)
8982 "Add these properties to the link property list."
8985 (setq key (pop plist) value (pop plist))
8986 (setq org-store-link-plist
8987 (plist-put org-store-link-plist key value)))))
8989 (defun org-email-link-description (&optional fmt)
8990 "Return the description part of an email link.
8991 This takes information from `org-store-link-plist' and formats it
8992 according to FMT (default from `org-email-link-description-format')."
8993 (setq fmt (or fmt org-email-link-description-format))
8994 (let* ((p org-store-link-plist)
8995 (to (plist-get p :toaddress))
8996 (from (plist-get p :fromaddress))
8999 (cons "%c" (plist-get p :fromto))
9000 (cons "%F" (plist-get p :from))
9001 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
9002 (cons "%T" (plist-get p :to))
9003 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
9004 (cons "%s" (plist-get p :subject))
9005 (cons "%d" (plist-get p :date))
9006 (cons "%m" (plist-get p :message-id)))))
9007 (when (string-match "%c" fmt)
9008 ;; Check if the user wrote this message
9009 (if (and org-from-is-user-regexp from to
9010 (save-match-data (string-match org-from-is-user-regexp from)))
9011 (setq fmt (replace-match "to %t" t t fmt))
9012 (setq fmt (replace-match "from %f" t t fmt))))
9013 (org-replace-escapes fmt table)))
9015 (defun org-make-org-heading-search-string (&optional string heading)
9016 "Make search string for STRING or current headline."
9018 (let ((s (or string (org-get-heading)))
9019 (lines org-context-in-file-links))
9020 (unless (and string (not heading))
9021 ;; We are using a headline, clean up garbage in there.
9022 (if (string-match org-todo-regexp s)
9023 (setq s (replace-match "" t t s)))
9024 (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
9025 (setq s (replace-match "" t t s)))
9026 (setq s (org-trim s))
9027 (if (string-match (concat "^\\(" org-quote-string "\\|"
9028 org-comment-string "\\)") s)
9029 (setq s (replace-match "" t t s)))
9030 (while (string-match org-ts-regexp s)
9031 (setq s (replace-match "" t t s))))
9032 (or string (setq s (concat "*" s))) ; Add * for headlines
9033 (when (and string (integerp lines) (> lines 0))
9034 (let ((slines (org-split-string s "\n")))
9035 (when (< lines (length slines))
9038 (reverse (nthcdr (- (length slines) lines)
9039 (reverse slines))) "\n")))))
9040 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
9042 (defun org-make-link (&rest strings)
9043 "Concatenate STRINGS."
9044 (apply 'concat strings))
9046 (defun org-make-link-string (link &optional description)
9047 "Make a link with brackets, consisting of LINK and DESCRIPTION."
9048 (unless (string-match "\\S-" link)
9049 (error "Empty link"))
9050 (when (and description
9051 (stringp description)
9052 (not (string-match "\\S-" description)))
9053 (setq description nil))
9054 (when (stringp description)
9055 ;; Remove brackets from the description, they are fatal.
9056 (while (string-match "\\[" description)
9057 (setq description (replace-match "{" t t description)))
9058 (while (string-match "\\]" description)
9059 (setq description (replace-match "}" t t description))))
9060 (when (equal link description)
9061 ;; No description needed, it is identical
9062 (setq description nil))
9063 (when (and (not description)
9064 (not (string-match (org-image-file-name-regexp) link))
9065 (not (equal link (org-link-escape link))))
9066 (setq description (org-extract-attributes link)))
9068 (cond ((string-match (org-image-file-name-regexp) link) link)
9069 ((string-match org-link-types-re link)
9070 (concat (match-string 1 link)
9071 (org-link-escape (substring link (match-end 1)))))
9072 (t (org-link-escape link))))
9073 (concat "[[" link "]"
9074 (if description (concat "[" description "]") "")
9077 (defconst org-link-escape-chars
9078 '(?\ ?\[ ?\] ?\; ?\= ?\+)
9079 "List of characters that should be escaped in link.
9080 This is the list that is used for internal purposes.")
9082 (defvar org-url-encoding-use-url-hexify nil)
9084 (defconst org-link-escape-chars-browser
9086 "List of escapes for characters that are problematic in links.
9087 This is the list that is used before handing over to the browser.")
9089 (defun org-link-escape (text &optional table merge)
9090 "Return percent escaped representation of TEXT.
9091 TEXT is a string with the text to escape.
9092 Optional argument TABLE is a list with characters that should be
9093 escaped. When nil, `org-link-escape-chars' is used.
9094 If optional argument MERGE is set, merge TABLE into
9095 `org-link-escape-chars'."
9096 (if (and org-url-encoding-use-url-hexify (not table))
9097 (url-hexify-string text)
9100 (mapc (lambda (defchr)
9101 (unless (member defchr table)
9102 (setq table (cons defchr table)))) org-link-escape-chars))
9104 (setq table org-link-escape-chars)))
9107 (if (or (member char table)
9108 (< char 32) (= char 37) (> char 126))
9109 (mapconcat (lambda (sequence-element)
9110 (format "%%%.2X" sequence-element))
9111 (or (encode-coding-char char 'utf-8)
9112 (error "Unable to percent escape character: %s"
9113 (char-to-string char))) "")
9114 (char-to-string char))) text "")))
9116 (defun org-link-unescape (str)
9117 "Unhex hexified Unicode strings as returned from the JavaScript function
9118 encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
9119 (unless (and (null str) (string= "" str))
9120 (let ((pos 0) (case-fold-search t) unhexed)
9121 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
9122 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
9123 (setq str (replace-match unhexed t t str))
9124 (setq pos (+ pos (length unhexed))))))
9127 (defun org-link-unescape-compound (hex)
9128 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
9129 Note: this function also decodes single byte encodings like
9130 `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
9132 (let* ((bytes (cdr (split-string hex "%")))
9137 (let* ((val (string-to-number (pop bytes) 16))
9141 ((>= val 252) (cons 6 252))
9142 ((>= val 248) (cons 5 248))
9143 ((>= val 240) (cons 4 240))
9144 ((>= val 224) (cons 3 224))
9145 ((>= val 192) (cons 2 192))
9148 (if (>= val 192) (setq eat (car shift-xor)))
9149 (setq val (logxor val (cdr shift-xor)))
9150 (setq sum (+ (lsh sum (car shift-xor)) val))
9151 (if (> eat 0) (setq eat (- eat 1)))
9153 ((= 0 eat) ;multi byte
9154 (setq ret (concat ret (org-char-to-string sum)))
9156 ((not bytes) ; single byte(s)
9157 (setq ret (org-link-unescape-single-byte-sequence hex))))
9158 )) ;; end (while bytes
9161 (defun org-link-unescape-single-byte-sequence (hex)
9162 "Unhexify hex-encoded single byte character sequences."
9163 (mapconcat (lambda (byte)
9164 (char-to-string (string-to-number byte 16)))
9165 (cdr (split-string hex "%")) ""))
9167 (defun org-xor (a b)
9171 (defun org-fixup-message-id-for-http (s)
9172 "Replace special characters in a message id, so it can be used in an http query."
9173 (when (string-match "%" s)
9174 (setq s (mapconcat (lambda (c)
9177 (char-to-string c)))
9179 (while (string-match "<" s)
9180 (setq s (replace-match "%3C" t t s)))
9181 (while (string-match ">" s)
9182 (setq s (replace-match "%3E" t t s)))
9183 (while (string-match "@" s)
9184 (setq s (replace-match "%40" t t s)))
9187 (defun org-link-prettify (link)
9188 "Return a human-readable representation of LINK.
9189 The car of LINK must be a raw link the cdr of LINK must be either
9190 a link description or nil."
9191 (let ((desc (or (cadr link) "<no description>")))
9192 (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
9193 "[[" (car link) "]]")))
9196 (defun org-insert-link-global ()
9197 "Insert a link like Org-mode does.
9198 This command can be called in any mode to insert a link in Org-mode syntax."
9200 (org-load-modules-maybe)
9201 (org-run-like-in-org-mode 'org-insert-link))
9203 (defun org-insert-all-links (&optional keep)
9204 "Insert all links in `org-stored-links'."
9206 (let ((links (copy-sequence org-stored-links)) l)
9207 (while (setq l (if keep (pop links) (pop org-stored-links)))
9209 (org-insert-link nil (car l) (cadr l))
9212 (defun org-insert-link (&optional complete-file link-location default-description)
9213 "Insert a link. At the prompt, enter the link.
9215 Completion can be used to insert any of the link protocol prefixes like
9218 The history can be used to select a link previously stored with
9219 `org-store-link'. When the empty string is entered (i.e. if you just
9220 press RET at the prompt), the link defaults to the most recently
9221 stored link. As SPC triggers completion in the minibuffer, you need to
9222 use M-SPC or C-q SPC to force the insertion of a space character.
9224 You will also be prompted for a description, and if one is given, it will
9225 be displayed in the buffer instead of the link.
9227 If there is already a link at point, this command will allow you to edit link
9228 and description parts.
9230 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9231 be selected using completion. The path to the file will be relative to the
9232 current directory if the file is in the current directory or a subdirectory.
9233 Otherwise, the link will be the absolute path as completed in the minibuffer
9234 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9235 option `org-link-file-path-type'.
9237 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9238 the current directory or below.
9240 With three \\[universal-argument] prefixes, negate the meaning of
9241 `org-keep-stored-link-after-insertion'.
9243 If `org-make-link-description-function' is non-nil, this function will be
9244 called with the link target, and the result will be the default
9247 If the LINK-LOCATION parameter is non-nil, this value will be
9248 used as the link location instead of reading one interactively.
9250 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9251 be used as the default description."
9253 (let* ((wcf (current-window-configuration))
9254 (region (if (org-region-active-p)
9255 (buffer-substring (region-beginning) (region-end))))
9256 (remove (and region (list (region-beginning) (region-end))))
9258 tmphist ; byte-compile incorrectly complains about this
9259 (link link-location)
9260 entry file all-prefixes)
9262 (link-location) ; specified by arg, just use it.
9263 ((org-in-regexp org-bracket-link-regexp 1)
9264 ;; We do have a link at point, and we are going to edit it.
9265 (setq remove (list (match-beginning 0) (match-end 0)))
9266 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
9267 (setq link (read-string "Link: "
9269 (org-match-string-no-properties 1)))))
9270 ((or (org-in-regexp org-angle-link-re)
9271 (org-in-regexp org-plain-link-re))
9272 ;; Convert to bracket link
9273 (setq remove (list (match-beginning 0) (match-end 0))
9274 link (read-string "Link: "
9275 (org-remove-angle-brackets (match-string 0)))))
9276 ((member complete-file '((4) (16)))
9277 ;; Completing read for file names.
9278 (setq link (org-file-complete-link complete-file)))
9280 ;; Read link, with completion for stored links.
9281 (with-output-to-temp-buffer "*Org Links*"
9282 (princ "Insert a link.
9283 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9284 (when org-stored-links
9285 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9286 (princ (mapconcat 'org-link-prettify
9287 (reverse org-stored-links) "\n"))))
9288 (let ((cw (selected-window)))
9289 (select-window (get-buffer-window "*Org Links*" 'visible))
9290 (with-current-buffer "*Org Links*" (setq truncate-lines t))
9291 (unless (pos-visible-in-window-p (point-max))
9292 (org-fit-window-to-buffer))
9293 (and (window-live-p cw) (select-window cw)))
9294 ;; Fake a link history, containing the stored links.
9295 (setq tmphist (append (mapcar 'car org-stored-links)
9296 org-insert-link-history))
9297 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
9298 (mapcar 'car org-link-abbrev-alist)
9303 (let ((org-completion-use-ido nil)
9304 (org-completion-use-iswitchb nil))
9305 (org-completing-read
9308 (mapcar (lambda (x) (list (concat x ":")))
9310 (mapcar 'car org-stored-links))
9313 (car (car org-stored-links)))))
9314 (if (not (string-match "\\S-" link))
9315 (error "No link selected"))
9316 (if (or (member link all-prefixes)
9317 (and (equal ":" (substring link -1))
9318 (member (substring link 0 -1) all-prefixes)
9319 (setq link (substring link 0 -1))))
9320 (setq link (org-link-try-special-completion link))))
9321 (set-window-configuration wcf)
9322 (kill-buffer "*Org Links*"))
9323 (setq entry (assoc link org-stored-links))
9324 (or entry (push link org-insert-link-history))
9325 (setq desc (or desc (nth 1 entry)))))
9327 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
9328 (not org-keep-stored-link-after-insertion))
9329 (setq org-stored-links (delq (assoc link org-stored-links)
9332 (if (string-match org-plain-link-re link)
9333 ;; URL-like link, normalize the use of angular brackets.
9334 (setq link (org-make-link (org-remove-angle-brackets link))))
9336 ;; Check if we are linking to the current file with a search option
9337 ;; If yes, simplify the link by using only the search option.
9338 (when (and buffer-file-name
9339 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
9340 (let* ((path (match-string 1 link))
9341 (case-fold-search nil)
9342 (search (match-string 2 link)))
9344 (if (equal (file-truename buffer-file-name) (file-truename path))
9345 ;; We are linking to this same file, with a search option
9346 (setq link search)))))
9348 ;; Check if we can/should use a relative path. If yes, simplify the link
9349 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
9350 (let* ((type (match-string 1 link))
9351 (path (match-string 2 link))
9353 (case-fold-search nil))
9355 ((or (eq org-link-file-path-type 'absolute)
9356 (equal complete-file '(16)))
9357 (setq path (abbreviate-file-name (expand-file-name path))))
9358 ((eq org-link-file-path-type 'noabbrev)
9359 (setq path (expand-file-name path)))
9360 ((eq org-link-file-path-type 'relative)
9361 (setq path (file-relative-name path)))
9364 (if (string-match (concat "^" (regexp-quote
9366 (file-name-as-directory
9367 default-directory))))
9368 (expand-file-name path))
9369 ;; We are linking a file with relative path name.
9370 (setq path (substring (expand-file-name path)
9372 (setq path (abbreviate-file-name (expand-file-name path)))))))
9373 (setq link (concat type path))
9374 (if (equal desc origpath)
9377 (if org-make-link-description-function
9378 (setq desc (funcall org-make-link-description-function link desc))
9379 (if default-description (setq desc default-description)
9380 (setq desc (read-string "Description: " desc))))
9382 (unless (string-match "\\S-" desc) (setq desc nil))
9383 (if remove (apply 'delete-region remove))
9384 (insert (org-make-link-string link desc))))
9386 (defun org-link-try-special-completion (type)
9387 "If there is completion support for link type TYPE, offer it."
9388 (let ((fun (intern (concat "org-" type "-complete-link"))))
9391 (read-string "Link (no completion support): " (concat type ":")))))
9393 (defun org-file-complete-link (&optional arg)
9394 "Create a file link using completion."
9396 (setq file (read-file-name "File: "))
9397 (let ((pwd (file-name-as-directory (expand-file-name ".")))
9398 (pwd1 (file-name-as-directory (abbreviate-file-name
9399 (expand-file-name ".")))))
9402 (setq link (org-make-link
9404 (abbreviate-file-name (expand-file-name file)))))
9405 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
9406 (setq link (org-make-link "file:" (match-string 1 file))))
9407 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
9408 (expand-file-name file))
9409 (setq link (org-make-link
9410 "file:" (match-string 1 (expand-file-name file)))))
9411 (t (setq link (org-make-link "file:" file)))))
9414 (defun org-completing-read (&rest args)
9415 "Completing-read with SPACE being a normal character."
9416 (let ((enable-recursive-minibuffers t)
9417 (minibuffer-local-completion-map
9418 (copy-keymap minibuffer-local-completion-map)))
9419 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
9420 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
9421 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
9422 (apply 'org-icompleting-read args)))
9424 (defun org-completing-read-no-i (&rest args)
9425 (let (org-completion-use-ido org-completion-use-iswitchb)
9426 (apply 'org-completing-read args)))
9428 (defun org-iswitchb-completing-read (prompt choices &rest args)
9429 "Use iswitch as a completing-read replacement to choose from choices.
9430 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
9432 (let* ((iswitchb-use-virtual-buffers nil)
9433 (iswitchb-make-buflist-hook
9435 (setq iswitchb-temp-buflist choices))))
9436 (iswitchb-read-buffer prompt)))
9438 (defun org-icompleting-read (&rest args)
9439 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
9440 (org-without-partial-completion
9441 (if (and org-completion-use-ido
9442 (fboundp 'ido-completing-read)
9443 (boundp 'ido-mode) ido-mode
9444 (listp (second args)))
9445 (let ((ido-enter-matching-directory nil))
9446 (apply 'ido-completing-read (concat (car args))
9447 (if (consp (car (nth 1 args)))
9448 (mapcar 'car (nth 1 args))
9451 (if (and org-completion-use-iswitchb
9452 (boundp 'iswitchb-mode) iswitchb-mode
9453 (listp (second args)))
9454 (apply 'org-iswitchb-completing-read (concat (car args))
9455 (if (consp (car (nth 1 args)))
9456 (mapcar 'car (nth 1 args))
9459 (apply 'completing-read args)))))
9461 (defun org-extract-attributes (s)
9462 "Extract the attributes cookie from a string and set as text property."
9463 (let (a attr (start 0) key value)
9465 (when (string-match "{{\\([^}]+\\)}}$" s)
9466 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
9467 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
9468 (setq key (match-string 1 a) value (match-string 2 a)
9470 attr (plist-put attr (intern key) value))))
9471 (org-add-props s nil 'org-attr attr))
9474 (defun org-extract-attributes-from-string (tag)
9475 (let (key value attr)
9476 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
9477 (setq key (match-string 1 tag) value (match-string 2 tag)
9478 tag (replace-match "" t t tag)
9479 attr (plist-put attr (intern key) value)))
9482 (defun org-attributes-to-string (plist)
9483 "Format a property list into an HTML attribute list."
9484 (let ((s "") key value)
9486 (setq key (pop plist) value (pop plist))
9488 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
9491 ;;; Opening/following a link
9493 (defvar org-link-search-failed nil)
9495 (defvar org-open-link-functions nil
9496 "Hook for functions finding a plain text link.
9497 These functions must take a single argument, the link content.
9498 They will be called for links that look like [[link text][description]]
9499 when LINK TEXT does not have a protocol like \"http:\" and does not look
9500 like a filename (e.g. \"./blue.png\").
9502 These functions will be called *before* Org attempts to resolve the
9503 link by doing text searches in the current buffer - so if you want a
9504 link \"[[target]]\" to still find \"<<target>>\", your function should
9505 handle this as a special case.
9507 When the function does handle the link, it must return a non-nil value.
9508 If it decides that it is not responsible for this link, it must return
9509 nil to indicate that that Org-mode can continue with other options
9510 like exact and fuzzy text search.")
9512 (defun org-next-link ()
9513 "Move forward to the next link.
9514 If the link is in hidden text, expose it."
9516 (when (and org-link-search-failed (eq this-command last-command))
9517 (goto-char (point-min))
9518 (message "Link search wrapped back to beginning of buffer"))
9519 (setq org-link-search-failed nil)
9520 (let* ((pos (point))
9522 (a (assoc :link ct)))
9523 (if a (goto-char (nth 2 a)))
9524 (if (re-search-forward org-any-link-re nil t)
9526 (goto-char (match-beginning 0))
9527 (if (outline-invisible-p) (org-show-context)))
9529 (setq org-link-search-failed t)
9530 (error "No further link found"))))
9532 (defun org-previous-link ()
9533 "Move backward to the previous link.
9534 If the link is in hidden text, expose it."
9536 (when (and org-link-search-failed (eq this-command last-command))
9537 (goto-char (point-max))
9538 (message "Link search wrapped back to end of buffer"))
9539 (setq org-link-search-failed nil)
9540 (let* ((pos (point))
9542 (a (assoc :link ct)))
9543 (if a (goto-char (nth 1 a)))
9544 (if (re-search-backward org-any-link-re nil t)
9546 (goto-char (match-beginning 0))
9547 (if (outline-invisible-p) (org-show-context)))
9549 (setq org-link-search-failed t)
9550 (error "No further link found"))))
9552 (defun org-translate-link (s)
9553 "Translate a link string if a translation function has been defined."
9554 (if (and org-link-translation-function
9555 (fboundp org-link-translation-function)
9556 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
9558 (setq s (funcall org-link-translation-function
9559 (match-string 1 s) (match-string 2 s)))
9560 (concat (car s) ":" (cdr s)))
9563 (defun org-translate-link-from-planner (type path)
9564 "Translate a link from Emacs Planner syntax so that Org can follow it.
9565 This is still an experimental function, your mileage may vary."
9567 ((member type '("http" "https" "news" "ftp"))
9568 ;; standard Internet links are the same.
9570 ((and (equal type "irc") (string-match "^//" path))
9571 ;; Planner has two / at the beginning of an irc link, we have 1.
9572 ;; We should have zero, actually....
9573 (setq path (substring path 1)))
9574 ((and (equal type "lisp") (string-match "^/" path))
9575 ;; Planner has a slash, we do not.
9576 (setq type "elisp" path (substring path 1)))
9577 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
9578 ;; A typical message link. Planner has the id after the final slash,
9579 ;; we separate it with a hash mark
9580 (setq path (concat (match-string 1 path) "#"
9581 (org-remove-angle-brackets (match-string 2 path)))))
9585 (defun org-find-file-at-mouse (ev)
9586 "Open file link or URL at mouse."
9588 (mouse-set-point ev)
9589 (org-open-at-point 'in-emacs))
9591 (defun org-open-at-mouse (ev)
9592 "Open file link or URL at mouse.
9593 See the docstring of `org-open-file' for details."
9595 (mouse-set-point ev)
9596 (if (eq major-mode 'org-agenda-mode)
9597 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
9598 (org-open-at-point))
9600 (defvar org-window-config-before-follow-link nil
9601 "The window configuration before following a link.
9602 This is saved in case the need arises to restore it.")
9604 (defvar org-open-link-marker (make-marker)
9605 "Marker pointing to the location where `org-open-at-point; was called.")
9608 (defun org-open-at-point-global ()
9609 "Follow a link like Org-mode does.
9610 This command can be called in any mode to follow a link that has
9613 (org-run-like-in-org-mode 'org-open-at-point))
9616 (defun org-open-link-from-string (s &optional arg reference-buffer)
9617 "Open a link in the string S, as if it was in Org-mode."
9618 (interactive "sLink: \nP")
9619 (let ((reference-buffer (or reference-buffer (current-buffer))))
9621 (let ((org-inhibit-startup (not reference-buffer)))
9624 (goto-char (point-min))
9625 (when reference-buffer
9626 (setq org-link-abbrev-alist-local
9627 (with-current-buffer reference-buffer
9628 org-link-abbrev-alist-local)))
9629 (org-open-at-point arg reference-buffer)))))
9631 (defvar org-open-at-point-functions nil
9632 "Hook that is run when following a link at point.
9634 Functions in this hook must return t if they identify and follow
9635 a link at point. If they don't find anything interesting at point,
9636 they must return nil.")
9638 (defun org-open-at-point (&optional arg reference-buffer)
9639 "Open link at or after point.
9640 If there is no link at point, this function will search forward up to
9641 the end of the current line.
9642 Normally, files will be opened by an appropriate application. If the
9643 optional prefix argument ARG is non-nil, Emacs will visit the file.
9644 With a double prefix argument, try to open outside of Emacs, in the
9645 application the system uses for this file type."
9647 ;; if in a code block, then open the block's results
9648 (unless (call-interactively #'org-babel-open-src-block-result)
9649 (org-load-modules-maybe)
9650 (move-marker org-open-link-marker (point))
9651 (setq org-window-config-before-follow-link (current-window-configuration))
9652 (org-remove-occur-highlights nil nil t)
9654 ((and (org-at-heading-p)
9655 (not (org-at-timestamp-p t))
9657 (concat org-plain-link-re "\\|"
9658 org-bracket-link-regexp "\\|"
9659 org-angle-link-re "\\|"
9660 "[ \t]:[^ \t\n]+:[ \t]*$")))
9661 (not (get-text-property (point) 'org-linked-text)))
9662 (or (org-offer-links-in-entry arg)
9663 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
9664 ((run-hook-with-args-until-success 'org-open-at-point-functions))
9665 ((org-at-timestamp-p t) (org-follow-timestamp-link))
9666 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
9667 (not (org-in-regexp org-bracket-link-regexp)))
9668 (org-footnote-action))
9670 (let (type path link line search (pos (point)))
9673 (skip-chars-forward "^]\n\r")
9674 (when (org-in-regexp org-bracket-link-regexp 1)
9675 (setq link (org-extract-attributes
9676 (org-link-unescape (org-match-string-no-properties 1))))
9677 (while (string-match " *\n *" link)
9678 (setq link (replace-match " " t t link)))
9679 (setq link (org-link-expand-abbrev link))
9681 ((or (file-name-absolute-p link)
9682 (string-match "^\\.\\.?/" link))
9683 (setq type "file" path link))
9684 ((string-match org-link-re-with-space3 link)
9685 (setq type (match-string 1 link) path (match-string 2 link)))
9686 (t (setq type "thisfile" path link)))
9689 (when (get-text-property (point) 'org-linked-text)
9690 (setq type "thisfile"
9691 pos (if (get-text-property (1+ (point)) 'org-linked-text)
9692 (1+ (point)) (point))
9693 path (buffer-substring
9694 (or (previous-single-property-change pos 'org-linked-text)
9696 (or (next-single-property-change pos 'org-linked-text)
9701 (when (or (org-in-regexp org-angle-link-re)
9702 (org-in-regexp org-plain-link-re))
9703 (setq type (match-string 1)
9704 path (org-link-unescape (match-string 2)))
9707 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
9709 path (match-string 1))
9710 (while (string-match ":" path)
9711 (setq path (replace-match "+" t t path)))
9713 (when (org-in-regexp "<\\([^><\n]+\\)>")
9714 (setq type "tree-match"
9715 path (match-string 1))
9718 (error "No link found"))
9720 ;; switch back to reference buffer
9721 ;; needed when if called in a temporary buffer through
9722 ;; org-open-link-from-string
9723 (with-current-buffer (or reference-buffer (current-buffer))
9725 ;; Remove any trailing spaces in path
9726 (if (string-match " +\\'" path)
9727 (setq path (replace-match "" t t path)))
9728 (if (and org-link-translation-function
9729 (fboundp org-link-translation-function))
9730 ;; Check if we need to translate the link
9731 (let ((tmp (funcall org-link-translation-function type path)))
9732 (setq type (car tmp) path (cdr tmp))))
9736 ((assoc type org-link-protocols)
9737 (funcall (nth 1 (assoc type org-link-protocols)) path))
9739 ((equal type "mailto")
9740 (let ((cmd (car org-link-mailto-program))
9741 (args (cdr org-link-mailto-program)) args1
9742 (address path) (subject "") a)
9743 (if (string-match "\\(.*\\)::\\(.*\\)" path)
9744 (setq address (match-string 1 path)
9745 subject (org-link-escape (match-string 2 path))))
9748 ((not (stringp (car args))) (push (pop args) args1))
9749 (t (setq a (pop args))
9750 (if (string-match "%a" a)
9751 (setq a (replace-match address t t a)))
9752 (if (string-match "%s" a)
9753 (setq a (replace-match subject t t a)))
9755 (apply cmd (nreverse args1))))
9757 ((member type '("http" "https" "ftp" "news"))
9758 (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
9760 path org-link-escape-chars-browser)
9763 ((string= type "doi")
9764 (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
9766 path org-link-escape-chars-browser)
9769 ((member type '("message"))
9770 (browse-url (concat type ":" path)))
9772 ((string= type "tags")
9773 (org-tags-view arg path))
9775 ((string= type "tree-match")
9776 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
9778 ((string= type "file")
9779 (if (string-match "::\\([0-9]+\\)\\'" path)
9780 (setq line (string-to-number (match-string 1 path))
9781 path (substring path 0 (match-beginning 0)))
9782 (if (string-match "::\\(.+\\)\\'" path)
9783 (setq search (match-string 1 path)
9784 path (substring path 0 (match-beginning 0)))))
9785 (if (string-match "[*?{]" (file-name-nondirectory path))
9787 (org-open-file path arg line search)))
9789 ((string= type "shell")
9791 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
9792 (string-match org-confirm-shell-link-not-regexp cmd))
9793 (not org-confirm-shell-link-function)
9794 (funcall org-confirm-shell-link-function
9795 (format "Execute \"%s\" in shell? "
9796 (org-add-props cmd nil
9797 'face 'org-warning))))
9799 (message "Executing %s" cmd)
9800 (shell-command cmd))
9803 ((string= type "elisp")
9805 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
9806 (string-match org-confirm-elisp-link-not-regexp cmd))
9807 (not org-confirm-elisp-link-function)
9808 (funcall org-confirm-elisp-link-function
9809 (format "Execute \"%s\" as elisp? "
9810 (org-add-props cmd nil
9811 'face 'org-warning))))
9812 (message "%s => %s" cmd
9813 (if (equal (string-to-char cmd) ?\()
9815 (call-interactively (read cmd))))
9818 ((and (string= type "thisfile")
9819 (run-hook-with-args-until-success
9820 'org-open-link-functions path)))
9822 ((string= type "thisfile")
9824 (switch-to-buffer-other-window
9825 (org-get-buffer-for-internal-link (current-buffer)))
9826 (org-mark-ring-push))
9827 (let ((cmd `(org-link-search
9829 ,(cond ((equal arg '(4)) ''occur)
9830 ((equal arg '(16)) ''org-occur)
9833 (condition-case nil (let ((org-link-search-inhibit-query t))
9835 (error (progn (widen) (eval cmd))))))
9838 (browse-url-at-point)))))))
9839 (move-marker org-open-link-marker nil)
9840 (run-hook-with-args 'org-follow-link-hook)))
9842 (defun org-offer-links-in-entry (&optional nth zero)
9843 "Offer links in the current entry and follow the selected link.
9844 If there is only one link, follow it immediately as well.
9845 If NTH is an integer, immediately pick the NTH link found.
9846 If ZERO is a string, check also this string for a link, and if
9847 there is one, offer it as link number zero."
9848 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
9849 "\\(" org-angle-link-re "\\)\\|"
9850 "\\(" org-plain-link-re "\\)"))
9852 (in-emacs (if (integerp nth) nil nth))
9853 have-zero end links link c)
9854 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
9855 (push (match-string 0 zero) links)
9856 (setq cnt (1- cnt) have-zero t))
9858 (org-back-to-heading t)
9859 (setq end (save-excursion (outline-next-heading) (point)))
9860 (while (re-search-forward re end t)
9861 (push (match-string 0) links))
9862 (setq links (org-uniquify (reverse links))))
9866 (message "No links"))
9867 ((equal (length links) 1)
9868 (setq link (list (car links))))
9869 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
9870 (setq link (list (nth (if have-zero nth (1- nth)) links))))
9871 (t ; we have to select a link
9873 (save-window-excursion
9874 (delete-other-windows)
9875 (with-output-to-temp-buffer "*Select Link*"
9877 (if (not (string-match org-bracket-link-regexp l))
9878 (princ (format "[%c] %s\n" (incf cnt)
9879 (org-remove-angle-brackets l)))
9881 (princ (format "[%c] %s (%s)\n" (incf cnt)
9882 (match-string 3 l) (match-string 1 l)))
9883 (princ (format "[%c] %s\n" (incf cnt)
9884 (match-string 1 l))))))
9886 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
9887 (message "Select link to open, RET to open all:")
9888 (setq c (read-char-exclusive))
9889 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
9890 (when (equal c ?q) (error "Abort"))
9894 (if have-zero (setq nth (1+ nth)))
9895 (unless (and (integerp nth) (>= (length links) nth))
9896 (error "Invalid link selection"))
9897 (setq link (list (nth (1- nth) links))))))
9899 (let ((buf (current-buffer)))
9901 (org-open-link-from-string l in-emacs buf))
9905 ;; Add special file links that specify the way of opening
9907 (org-add-link-type "file+sys" 'org-open-file-with-system)
9908 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
9909 (defun org-open-file-with-system (path)
9910 "Open file at PATH using the system way of opening it."
9911 (org-open-file path 'system))
9912 (defun org-open-file-with-emacs (path)
9913 "Open file at PATH in Emacs."
9914 (org-open-file path 'emacs))
9915 (defun org-remove-file-link-modifiers ()
9916 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
9917 (goto-char (point-min))
9918 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
9920 (replace-match "file:" t t))))
9921 (eval-after-load "org-exp"
9922 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
9923 'org-remove-file-link-modifiers))
9927 (defun org-get-effort (&optional pom)
9928 "Get the effort estimate for the current entry."
9929 (org-entry-get pom org-effort-property))
9933 (defvar org-create-file-search-functions nil
9934 "List of functions to construct the right search string for a file link.
9935 These functions are called in turn with point at the location to
9936 which the link should point.
9938 A function in the hook should first test if it would like to
9939 handle this file type, for example by checking the `major-mode'
9940 or the file extension. If it decides not to handle this file, it
9941 should just return nil to give other functions a chance. If it
9942 does handle the file, it must return the search string to be used
9943 when following the link. The search string will be part of the
9944 file link, given after a double colon, and `org-open-at-point'
9945 will automatically search for it. If special measures must be
9946 taken to make the search successful, another function should be
9947 added to the companion hook `org-execute-file-search-functions',
9950 A function in this hook may also use `setq' to set the variable
9951 `description' to provide a suggestion for the descriptive text to
9952 be used for this link when it gets inserted into an Org-mode
9953 buffer with \\[org-insert-link].")
9955 (defvar org-execute-file-search-functions nil
9956 "List of functions to execute a file search triggered by a link.
9958 Functions added to this hook must accept a single argument, the
9959 search string that was part of the file link, the part after the
9960 double colon. The function must first check if it would like to
9961 handle this search, for example by checking the `major-mode' or
9962 the file extension. If it decides not to handle this search, it
9963 should just return nil to give other functions a chance. If it
9964 does handle the search, it must return a non-nil value to keep
9965 other functions from trying.
9967 Each function can access the current prefix argument through the
9968 variable `current-prefix-argument'. Note that a single prefix is
9969 used to force opening a link in Emacs, so it may be good to only
9970 use a numeric or double prefix to guide the search function.
9972 In case this is needed, a function in this hook can also restore
9973 the window configuration before `org-open-at-point' was called using:
9975 (set-window-configuration org-window-config-before-follow-link)")
9977 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
9978 (defun org-link-search (s &optional type avoid-pos stealth)
9979 "Search for a link search option.
9980 If S is surrounded by forward slashes, it is interpreted as a
9981 regular expression. In org-mode files, this will create an `org-occur'
9982 sparse tree. In ordinary files, `occur' will be used to list matches.
9983 If the current buffer is in `dired-mode', grep will be used to search
9984 in all files. If AVOID-POS is given, ignore matches near that position.
9986 When optional argument STEALTH is non-nil, do not modify
9987 visibility around point, thus ignoring
9988 `org-show-hierarchy-above', `org-show-following-heading' and
9989 `org-show-siblings' variables."
9990 (let ((case-fold-search t)
9991 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
9992 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
9993 (append '(("") (" ") ("\t") ("\n"))
9997 (pre nil) (post nil)
9998 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
10000 ;; First check if there are any special search functions
10001 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
10002 ;; Now try the builtin stuff
10003 ((and (equal (string-to-char s0) ?#)
10006 (goto-char (point-min))
10009 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
10010 (setq type 'dedicated
10011 pos (match-beginning 0))))
10012 ;; There is an exact target for this
10014 (org-back-to-heading t)))
10016 (goto-char (point-min))
10019 (concat "<<" (regexp-quote s0) ">>") nil t)
10020 (setq type 'dedicated
10021 pos (match-beginning 0))))
10022 ;; There is an exact target for this
10025 (goto-char (point-min))
10028 (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
10029 (setq type 'dedicated pos (match-beginning 0))))
10030 ;; Found an invisible target.
10033 (goto-char (point-min))
10036 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
10037 (setq type 'dedicated pos (match-beginning 0))))
10038 ;; Found an element with a matching #+name affiliated keyword.
10040 ((and (string-match "^(\\(.*\\))$" s0)
10042 (goto-char (point-min))
10045 (concat "[^[]" (regexp-quote
10046 (format org-coderef-label-format
10047 (match-string 1 s0))))
10049 (setq type 'dedicated
10050 pos (1+ (match-beginning 0))))))
10051 ;; There is a coderef target for this
10053 ((string-match "^/\\(.*\\)/$" s)
10054 ;; A regular expression
10056 ((derived-mode-p 'org-mode)
10057 (org-occur (match-string 1 s)))
10058 ;;((eq major-mode 'dired-mode)
10059 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
10060 (t (org-do-occur (match-string 1 s)))))
10061 ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
10062 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
10063 (goto-char (point-min))
10065 ((let (case-fold-search)
10066 (re-search-forward (format org-complex-heading-regexp-format
10069 ;; OK, found a match
10070 (setq type 'dedicated)
10071 (goto-char (match-beginning 0)))
10072 ((and (not org-link-search-inhibit-query)
10073 (eq org-link-search-must-match-exact-headline 'query-to-create)
10074 (y-or-n-p "No match - create this as a new heading? "))
10075 (goto-char (point-max))
10076 (or (bolp) (newline))
10077 (insert "* " s "\n")
10078 (beginning-of-line 0))
10081 (error "No match"))))
10083 ;; A normal search string
10084 (when (equal (string-to-char s) ?*)
10085 ;; Anchor on headlines, post may include tags.
10086 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
10087 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
10088 s (substring s 1)))
10089 (remove-text-properties
10091 '(face nil mouse-face nil keymap nil fontified nil) s)
10092 ;; Make a series of regular expressions to find a match
10093 (setq words (org-split-string s "[ \n\r\t]+")
10095 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
10096 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
10098 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
10099 re2a (concat "[ \t\r\n]" re2a_)
10100 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
10101 re4 (concat "[^a-zA-Z_]" re4_)
10103 re1 (concat pre re2 post)
10104 re3 (concat pre (if pre re4_ re4) post)
10105 re5 (concat pre ".*" re4)
10106 re2 (concat pre re2)
10107 re2a (concat pre (if pre re2a_ re2a))
10108 re4 (concat pre (if pre re4_ re4))
10109 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
10110 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
10114 ((eq type 'org-occur) (org-occur reall))
10115 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
10116 (t (goto-char (point-min))
10118 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
10119 (org-search-not-self 1 re1 nil t)
10120 (org-search-not-self 1 re2 nil t)
10121 (org-search-not-self 1 re2a nil t)
10122 (org-search-not-self 1 re3 nil t)
10123 (org-search-not-self 1 re4 nil t)
10124 (org-search-not-self 1 re5 nil t)
10126 (goto-char (match-beginning 1))
10128 (error "No match"))))))
10129 (and (derived-mode-p 'org-mode)
10131 (org-show-context 'link-search))
10134 (defun org-search-not-self (group &rest args)
10135 "Execute `re-search-forward', but only accept matches that do not
10136 enclose the position of `org-open-link-marker'."
10137 (let ((m org-open-link-marker))
10139 (while (apply 're-search-forward args)
10140 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
10141 (goto-char (match-end group))
10142 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
10143 (> (match-beginning 0) (marker-position m))
10144 (< (match-end 0) (marker-position m)))
10146 (or (not (org-in-regexp
10147 org-bracket-link-analytic-regexp 1))
10148 (not (match-end 4)) ; no description
10149 (and (<= (match-beginning 4) (point))
10150 (>= (match-end 4) (point))))))
10151 (throw 'exit (point))))))))
10153 (defun org-get-buffer-for-internal-link (buffer)
10154 "Return a buffer to be used for displaying the link target of internal links."
10156 ((not org-display-internal-link-with-indirect-buffer)
10158 ((string-match "(Clone)$" (buffer-name buffer))
10159 (message "Buffer is already a clone, not making another one")
10160 ;; we also do not modify visibility in this case
10162 (t ; make a new indirect buffer for displaying the link
10163 (let* ((bn (buffer-name buffer))
10164 (ibn (concat bn "(Clone)"))
10165 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
10166 (with-current-buffer ib (org-overview))
10169 (defun org-do-occur (regexp &optional cleanup)
10170 "Call the Emacs command `occur'.
10171 If CLEANUP is non-nil, remove the printout of the regular expression
10172 in the *Occur* buffer. This is useful if the regex is long and not useful
10176 (let ((cwin (selected-window)) win beg end)
10177 (when (setq win (get-buffer-window "*Occur*"))
10178 (select-window win))
10179 (goto-char (point-min))
10180 (when (re-search-forward "match[a-z]+" nil t)
10181 (setq beg (match-end 0))
10182 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
10183 (setq end (1- (match-beginning 0)))))
10184 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
10185 (goto-char (point-min))
10186 (select-window cwin))))
10188 ;;; The mark ring for links jumps
10190 (defvar org-mark-ring nil
10191 "Mark ring for positions before jumps in Org-mode.")
10192 (defvar org-mark-ring-last-goto nil
10193 "Last position in the mark ring used to go back.")
10194 ;; Fill and close the ring
10195 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
10196 (loop for i from 1 to org-mark-ring-length do
10197 (push (make-marker) org-mark-ring))
10198 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
10201 (defun org-mark-ring-push (&optional pos buffer)
10202 "Put the current position or POS into the mark ring and rotate it."
10204 (setq pos (or pos (point)))
10205 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10206 (move-marker (car org-mark-ring)
10208 (or buffer (current-buffer)))
10210 (substitute-command-keys
10211 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10213 (defun org-mark-ring-goto (&optional n)
10214 "Jump to the previous position in the mark ring.
10215 With prefix arg N, jump back that many stored positions. When
10216 called several times in succession, walk through the entire ring.
10217 Org-mode commands jumping to a different position in the current file,
10218 or to another Org-mode file, automatically push the old position
10222 (if (eq last-command this-command)
10223 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10224 (setq p org-mark-ring))
10225 (setq org-mark-ring-last-goto p)
10227 (org-pop-to-buffer-same-window (marker-buffer m))
10229 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
10231 (defun org-remove-angle-brackets (s)
10232 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
10233 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
10235 (defun org-add-angle-brackets (s)
10236 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
10237 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
10239 (defun org-remove-double-quotes (s)
10240 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
10241 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
10244 ;;; Following specific links
10246 (defun org-follow-timestamp-link ()
10248 ((org-at-date-range-p t)
10249 (let ((org-agenda-start-on-weekday)
10250 (t1 (match-string 1))
10251 (t2 (match-string 2)))
10252 (setq t1 (time-to-days (org-time-string-to-time t1))
10253 t2 (time-to-days (org-time-string-to-time t2)))
10254 (org-agenda-list nil t1 (1+ (- t2 t1)))))
10255 ((org-at-timestamp-p t)
10256 (org-agenda-list nil (time-to-days (org-time-string-to-time
10257 (substring (match-string 1) 0 10)))
10259 (t (error "This should not happen"))))
10262 ;;; Following file links
10263 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
10264 (declare-function mailcap-extension-to-mime "mailcap" (extn))
10265 (declare-function mailcap-mime-info
10266 "mailcap" (string &optional request no-decode))
10267 (defvar org-wait nil)
10268 (defun org-open-file (path &optional in-emacs line search)
10269 "Open the file at PATH.
10270 First, this expands any special file name abbreviations. Then the
10271 configuration variable `org-file-apps' is checked if it contains an
10272 entry for this file type, and if yes, the corresponding command is launched.
10274 If no application is found, Emacs simply visits the file.
10276 With optional prefix argument IN-EMACS, Emacs will visit the file.
10277 With a double \\[universal-argument] \\[universal-argument] \
10278 prefix arg, Org tries to avoid opening in Emacs
10279 and to use an external application to visit the file.
10281 Optional LINE specifies a line to go to, optional SEARCH a string
10282 to search for. If LINE or SEARCH is given, the file will be
10283 opened in Emacs, unless an entry from org-file-apps that makes
10284 use of groups in a regexp matches.
10286 If you want to change the way frames are used when following a
10287 link, please customize `org-link-frame-setup'.
10289 If the file does not exist, an error is thrown."
10290 (let* ((file (if (equal path "")
10292 (substitute-in-file-name (expand-file-name path))))
10293 (file-apps (append org-file-apps (org-default-apps)))
10294 (apps (org-remove-if
10295 'org-file-apps-entry-match-against-dlink-p file-apps))
10296 (apps-dlink (org-remove-if-not
10297 'org-file-apps-entry-match-against-dlink-p file-apps))
10298 (remp (and (assq 'remote apps) (org-file-remote-p file)))
10299 (dirp (if remp nil (file-directory-p file)))
10300 (file (if (and dirp org-open-directory-means-index-dot-org)
10301 (concat (file-name-as-directory file) "index.org")
10303 (a-m-a-p (assq 'auto-mode apps))
10304 (dfile (downcase file))
10305 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
10306 (link (cond ((and (eq line nil)
10310 (concat file "::" (number-to-string line)))
10312 (concat file "::" search))))
10313 (dlink (downcase link))
10314 (old-buffer (current-buffer))
10316 (old-mode major-mode)
10317 ext cmd link-match-data)
10318 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
10319 (setq ext (match-string 1 dfile))
10320 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
10321 (setq ext (match-string 1 dfile))))
10323 ((member in-emacs '((16) system))
10324 (setq cmd (cdr (assoc 'system apps))))
10325 (in-emacs (setq cmd 'emacs))
10327 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
10328 (and dirp (cdr (assoc 'directory apps)))
10329 ; first, try matching against apps-dlink
10330 ; if we get a match here, store the match data for later
10331 (let ((match (assoc-default dlink apps-dlink
10334 (progn (setq link-match-data (match-data))
10336 (progn (setq in-emacs (or in-emacs line search))
10337 nil))) ; if we have no match in apps-dlink,
10338 ; always open the file in emacs if line or search
10339 ; is given (for backwards compatibility)
10340 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
10342 (cdr (assoc ext apps))
10343 (cdr (assoc t apps))))))
10344 (when (eq cmd 'system)
10345 (setq cmd (cdr (assoc 'system apps))))
10346 (when (eq cmd 'default)
10347 (setq cmd (cdr (assoc t apps))))
10348 (when (eq cmd 'mailcap)
10350 (mailcap-parse-mailcaps)
10351 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
10352 (command (mailcap-mime-info mime-type)))
10353 (if (stringp command)
10355 (setq cmd 'emacs))))
10356 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
10357 (not (file-exists-p file))
10358 (not org-open-non-existing-files))
10359 (error "No such file: %s" file))
10361 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
10362 ;; Remove quotes around the file name - we'll use shell-quote-argument.
10363 (while (string-match "['\"]%s['\"]" cmd)
10364 (setq cmd (replace-match "%s" t t cmd)))
10365 (while (string-match "%s" cmd)
10366 (setq cmd (replace-match
10368 (shell-quote-argument
10369 (convert-standard-filename file)))
10372 ;; Replace "%1", "%2" etc. in command with group matches from regex
10374 (let ((match-index 1)
10375 (number-of-groups (- (/ (length link-match-data) 2) 1)))
10376 (set-match-data link-match-data)
10377 (while (<= match-index number-of-groups)
10378 (let ((regex (concat "%" (number-to-string match-index)))
10379 (replace-with (match-string match-index dlink)))
10380 (while (string-match regex cmd)
10381 (setq cmd (replace-match replace-with t t cmd))))
10382 (setq match-index (+ match-index 1)))))
10384 (save-window-excursion
10385 (start-process-shell-command cmd nil cmd)
10386 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
10390 (funcall (cdr (assq 'file org-link-frame-setup)) file)
10392 (if line (org-goto-line line)
10393 (if search (org-link-search search))))
10395 (let ((file (convert-standard-filename file)))
10397 (set-match-data link-match-data)
10399 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
10400 (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
10401 (or (not (equal old-buffer (current-buffer)))
10402 (not (equal old-pos (point))))
10403 (org-mark-ring-push old-pos old-buffer))))
10405 (defun org-file-apps-entry-match-against-dlink-p (entry)
10406 "This function returns non-nil if `entry' uses a regular
10407 expression which should be matched against the whole link by
10410 It assumes that is the case when the entry uses a regular
10411 expression which has at least one grouping construct and the
10412 action is either a lisp form or a command string containing
10413 '%1', i.e. using at least one subexpression match as a
10415 (let ((selector (car entry))
10416 (action (cdr entry)))
10417 (if (stringp selector)
10418 (and (> (regexp-opt-depth selector) 0)
10419 (or (and (stringp action)
10420 (string-match "%[0-9]" action))
10424 (defun org-default-apps ()
10425 "Return the default applications for this operating system."
10427 ((eq system-type 'darwin)
10428 org-file-apps-defaults-macosx)
10429 ((eq system-type 'windows-nt)
10430 org-file-apps-defaults-windowsnt)
10431 (t org-file-apps-defaults-gnu)))
10433 (defun org-apps-regexp-alist (list &optional add-auto-mode)
10434 "Convert extensions to regular expressions in the cars of LIST.
10435 Also, weed out any non-string entries, because the return value is used
10436 only for regexp matching.
10437 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
10438 point to the symbol `emacs', indicating that the file should
10439 be opened in Emacs."
10442 (mapcar (lambda (x)
10443 (if (not (stringp (car x)))
10445 (if (string-match "\\W" (car x))
10447 (cons (concat "\\." (car x) "\\'") (cdr x)))))
10450 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
10452 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
10453 (defun org-file-remote-p (file)
10454 "Test whether FILE specifies a location on a remote system.
10455 Return non-nil if the location is indeed remote.
10457 For example, the filename \"/user@host:/foo\" specifies a location
10458 on the system \"/user@host:\"."
10459 (cond ((fboundp 'file-remote-p)
10460 (file-remote-p file))
10461 ((fboundp 'tramp-handle-file-remote-p)
10462 (tramp-handle-file-remote-p file))
10463 ((and (boundp 'ange-ftp-name-format)
10464 (string-match (car ange-ftp-name-format) file))
10471 (defun org-get-org-file ()
10472 "Read a filename, with default directory `org-directory'."
10473 (let ((default (or org-default-notes-file remember-data-file)))
10474 (read-file-name (format "File name [%s]: " default)
10475 (file-name-as-directory org-directory)
10478 (defun org-notes-order-reversed-p ()
10479 "Check if the current file should receive notes in reversed order."
10481 ((not org-reverse-note-order) nil)
10482 ((eq t org-reverse-note-order) t)
10483 ((not (listp org-reverse-note-order)) nil)
10485 (let ((all org-reverse-note-order)
10487 (while (setq entry (pop all))
10488 (if (string-match (car entry) buffer-file-name)
10489 (throw 'exit (cdr entry))))
10492 (defvar org-refile-target-table nil
10493 "The list of refile targets, created by `org-refile'.")
10495 (defvar org-agenda-new-buffers nil
10496 "Buffers created to visit agenda files.")
10498 (defvar org-refile-cache nil
10499 "Cache for refile targets.")
10501 (defvar org-refile-markers nil
10502 "All the markers used for caching refile locations.")
10504 (defun org-refile-marker (pos)
10505 "Get a new refile marker, but only if caching is in use."
10506 (if (not org-refile-use-cache)
10508 (let ((m (make-marker)))
10509 (move-marker m pos)
10510 (push m org-refile-markers)
10513 (defun org-refile-cache-clear ()
10514 "Clear the refile cache and disable all the markers."
10515 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
10516 (setq org-refile-markers nil)
10517 (setq org-refile-cache nil)
10518 (message "Refile cache has been cleared"))
10520 (defun org-refile-cache-check-set (set)
10521 "Check if all the markers in the cache still have live buffers."
10524 (while (and set (setq marker (nth 3 (pop set))))
10525 ;; if org-refile-use-outline-path is 'file, marker may be nil
10526 (when (and marker (null (marker-buffer marker)))
10527 (message "not found") (sit-for 3)
10528 (throw 'exit nil)))
10531 (defun org-refile-cache-put (set &rest identifiers)
10532 "Push the refile targets SET into the cache, under IDENTIFIERS."
10533 (let* ((key (sha1 (prin1-to-string identifiers)))
10534 (entry (assoc key org-refile-cache)))
10537 (push (cons key set) org-refile-cache))))
10539 (defun org-refile-cache-get (&rest identifiers)
10540 "Retrieve the cached value for refile targets given by IDENTIFIERS."
10542 ((not org-refile-cache) nil)
10543 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
10545 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
10546 org-refile-cache))))
10547 (and set (org-refile-cache-check-set set) set)))))
10549 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
10550 "Produce a table with refile targets."
10551 (let ((case-fold-search nil)
10552 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
10553 (entries (or org-refile-targets '((nil . (:level . 1)))))
10554 targets tgs txt re files f desc descre fast-path-p level pos0)
10555 (message "Getting targets...")
10556 (with-current-buffer (or default-buffer (current-buffer))
10557 (while (setq entry (pop entries))
10558 (setq files (car entry) desc (cdr entry))
10559 (setq fast-path-p nil)
10561 ((null files) (setq files (list (current-buffer))))
10562 ((eq files 'org-agenda-files)
10563 (setq files (org-agenda-files 'unrestricted)))
10564 ((and (symbolp files) (fboundp files))
10565 (setq files (funcall files)))
10566 ((and (symbolp files) (boundp files))
10567 (setq files (symbol-value files))))
10568 (if (stringp files) (setq files (list files)))
10570 ((eq (car desc) :tag)
10571 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
10572 ((eq (car desc) :todo)
10573 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
10574 ((eq (car desc) :regexp)
10575 (setq descre (cdr desc)))
10576 ((eq (car desc) :level)
10577 (setq descre (concat "^\\*\\{" (number-to-string
10578 (if org-odd-levels-only
10579 (1- (* 2 (cdr desc)))
10582 ((eq (car desc) :maxlevel)
10583 (setq fast-path-p t)
10584 (setq descre (concat "^\\*\\{1," (number-to-string
10585 (if org-odd-levels-only
10586 (1- (* 2 (cdr desc)))
10589 (t (error "Bad refiling target description %s" desc)))
10590 (while (setq f (pop files))
10591 (with-current-buffer
10592 (if (bufferp f) f (org-get-agenda-file-buffer f))
10594 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
10596 (if (bufferp f) (setq f (buffer-file-name
10597 (buffer-base-buffer f))))
10598 (setq f (and f (expand-file-name f)))
10599 (if (eq org-refile-use-outline-path 'file)
10600 (push (list (file-name-nondirectory f) f nil nil) tgs))
10604 (goto-char (point-min))
10605 (while (re-search-forward descre nil t)
10606 (goto-char (setq pos0 (point-at-bol)))
10608 (when org-refile-target-verify-function
10610 (or (funcall org-refile-target-verify-function)
10612 (when (and (looking-at org-complex-heading-regexp)
10613 (not (member (match-string 4) excluded-entries))
10615 (setq level (org-reduced-level
10616 (- (match-end 1) (match-beginning 1)))
10617 txt (org-link-display-format (match-string 4))
10618 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
10619 re (format org-complex-heading-regexp-format
10620 (regexp-quote (match-string 4))))
10621 (when org-refile-use-outline-path
10622 (setq txt (mapconcat
10625 (if (eq org-refile-use-outline-path
10627 (list (file-name-nondirectory
10629 (buffer-base-buffer))))
10630 (if (eq org-refile-use-outline-path
10632 (list (buffer-file-name
10633 (buffer-base-buffer)))))
10634 (org-get-outline-path fast-path-p
10638 (push (list txt f re (org-refile-marker (point)))
10640 (when (= (point) pos0)
10641 ;; verification function has not moved point
10642 (goto-char (point-at-eol))))))))
10643 (when org-refile-use-cache
10644 (org-refile-cache-put tgs (buffer-file-name) descre))
10645 (setq targets (append tgs targets))
10647 (message "Getting targets...done")
10648 (nreverse targets)))
10650 (defun org-protect-slash (s)
10651 (while (string-match "/" s)
10652 (setq s (replace-match "\\" t t s)))
10655 (defvar org-olpa (make-vector 20 nil))
10657 (defun org-get-outline-path (&optional fastp level heading)
10658 "Return the outline path to the current entry, as a list.
10660 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
10661 routine which makes outline path derivations for an entire file,
10662 avoiding backtracing. Refile target collection makes use of that."
10666 (error "Outline path failure, more than 19 levels"))
10667 (loop for i from level upto 19 do
10668 (aset org-olpa i nil))
10670 (delq nil (append org-olpa nil))
10671 (aset org-olpa level heading)))
10672 (let (rtn case-fold-search)
10676 (while (org-up-heading-safe)
10677 (when (looking-at org-complex-heading-regexp)
10678 (push (org-match-string-no-properties 4) rtn)))
10681 (defun org-format-outline-path (path &optional width prefix)
10682 "Format the outline path PATH for display.
10683 Width is the maximum number of characters that is available.
10684 Prefix is a prefix to be included in the returned string,
10685 such as the file name."
10686 (setq width (or width 79))
10687 (if prefix (setq width (- width (length prefix))))
10690 (let* ((nsteps (length path))
10691 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
10692 (maxwidth (if (<= total-width width)
10693 10000 ;; everything fits
10694 ;; we need to shorten the level headings
10695 (/ (- width nsteps) nsteps)))
10696 (org-odd-levels-only nil)
10698 (total (1+ (length prefix))))
10699 (setq maxwidth (max maxwidth 10))
10704 (if (and (= n nsteps) (< maxwidth 10000))
10705 (setq maxwidth (- total-width total)))
10706 (if (< (length h) maxwidth)
10707 (progn (setq total (+ total (length h) 1)) h)
10708 (setq h (substring h 0 (- maxwidth 2))
10709 total (+ total maxwidth 1))
10710 (if (string-match "[ \t]+\\'" h)
10711 (setq h (substring h 0 (match-beginning 0))))
10712 (setq h (concat h "..")))
10713 (org-add-props h nil 'face
10714 (nth (% (1- n) org-n-level-faces)
10719 (defun org-display-outline-path (&optional file current)
10720 "Display the current outline path in the echo area."
10722 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
10723 (case-fold-search nil)
10724 (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
10725 (if current (setq path (append path
10727 (org-back-to-heading t)
10728 (if (looking-at org-complex-heading-regexp)
10729 (list (match-string 4)))))))
10731 (org-format-outline-path
10734 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
10736 (defvar org-refile-history nil
10737 "History for refiling operations.")
10739 (defvar org-after-refile-insert-hook nil
10740 "Hook run after `org-refile' has inserted its stuff at the new location.
10741 Note that this is still *before* the stuff will be removed from
10742 the *old* location.")
10744 (defvar org-capture-last-stored-marker)
10745 (defun org-refile (&optional goto default-buffer rfloc)
10746 "Move the entry or entries at point to another heading.
10747 The list of target headings is compiled using the information in
10748 `org-refile-targets', which see.
10750 At the target location, the entry is filed as a subitem of the target
10751 heading. Depending on `org-reverse-note-order', the new subitem will
10752 either be the first or the last subitem.
10754 If there is an active region, all entries in that region will be moved.
10755 However, the region must fulfill the requirement that the first heading
10756 is the first one sets the top-level of the moved text - at most siblings
10757 below it are allowed.
10759 With prefix arg GOTO, the command will only visit the target location
10760 and not actually move anything.
10762 With a double prefix arg \\[universal-argument] \\[universal-argument], \
10763 go to the location where the last refiling operation has put the subtree.
10764 With a prefix argument of `2', refile to the running clock.
10766 RFLOC can be a refile location obtained in a different way.
10768 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
10770 If you are using target caching (see `org-refile-use-cache'),
10771 you have to clear the target cache in order to find new targets.
10772 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
10773 prefix argument (`C-u C-u C-u C-c C-w')."
10776 (if (member goto '(0 (64)))
10777 (org-refile-cache-clear)
10778 (let* ((cbuf (current-buffer))
10779 (regionp (org-region-active-p))
10780 (region-start (and regionp (region-beginning)))
10781 (region-end (and regionp (region-end)))
10782 (region-length (and regionp (- region-end region-start)))
10783 (filename (buffer-file-name (buffer-base-buffer cbuf)))
10784 pos it nbuf file re level reversed)
10785 (setq last-command nil)
10787 (goto-char region-start)
10788 (or (bolp) (goto-char (point-at-bol)))
10789 (setq region-start (point))
10790 (unless (or (org-kill-is-subtree-p
10791 (buffer-substring region-start region-end))
10792 (prog1 org-refile-active-region-within-subtree
10793 (org-toggle-heading)))
10794 (error "The region is not a (sequence of) subtree(s)")))
10795 (if (equal goto '(16))
10796 (org-refile-goto-last-stored)
10798 (and (equal goto 2)
10799 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
10801 (setq it (list (or org-clock-heading "running clock")
10803 (marker-buffer org-clock-hd-marker))
10805 (marker-position org-clock-hd-marker)))
10808 (let (heading-text)
10811 (org-back-to-heading t)
10813 (nth 4 (org-heading-components))))
10814 (org-refile-get-location
10815 (cond (goto "Goto")
10816 (regionp "Refile region to")
10817 (t (concat "Refile subtree \""
10818 heading-text "\" to")))
10820 (and (not (equal '(4) goto))
10821 org-refile-allow-creating-parent-nodes)
10823 (setq file (nth 1 it)
10826 (if (and (not goto)
10828 (equal (buffer-file-name) file)
10830 (and (>= pos region-start)
10831 (<= pos region-end))
10832 (and (>= pos (point))
10833 (< pos (save-excursion
10834 (org-end-of-subtree t t))))))
10835 (error "Cannot refile to position inside the tree or region"))
10837 (setq nbuf (or (find-buffer-visiting file)
10838 (find-file-noselect file)))
10841 (org-pop-to-buffer-same-window nbuf)
10843 (org-show-context 'org-goto))
10846 (org-kill-new (buffer-substring region-start region-end))
10847 (org-save-markers-in-region region-start region-end))
10848 (org-copy-subtree 1 nil t))
10849 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
10850 (find-file-noselect file)))
10851 (setq reversed (org-notes-order-reversed-p))
10858 (looking-at org-outline-regexp)
10859 (setq level (org-get-valid-level (funcall outline-level) 1))
10862 (or (outline-next-heading) (point-max))
10863 (or (save-excursion (org-get-next-sibling))
10864 (org-end-of-subtree t t)
10868 (goto-char (point-max))
10869 (goto-char (point-min))
10870 (or (outline-next-heading) (goto-char (point-max)))))
10871 (if (not (bolp)) (newline))
10872 (org-paste-subtree level)
10873 (when org-log-refile
10874 (org-add-log-setup 'refile nil nil 'findpos
10876 (unless (eq org-log-refile 'note)
10877 (save-excursion (org-add-log-note))))
10878 (and org-auto-align-tags
10879 (let ((org-loop-over-headlines-in-active-region nil))
10880 (org-set-tags nil t)))
10881 (bookmark-set "org-refile-last-stored")
10882 ;; If we are refiling for capture, make sure that the
10883 ;; last-capture pointers point here
10884 (when (org-bound-and-true-p org-refile-for-capture)
10885 (bookmark-set "org-capture-last-stored-marker")
10886 (move-marker org-capture-last-stored-marker (point)))
10887 (if (fboundp 'deactivate-mark) (deactivate-mark))
10888 (run-hooks 'org-after-refile-insert-hook))))
10890 (delete-region (point) (+ (point) region-length))
10892 (when (featurep 'org-inlinetask)
10893 (org-inlinetask-remove-END-maybe))
10894 (setq org-markers-to-move nil)
10895 (message "Refiled to \"%s\" in file %s" (car it) file)))))))
10897 (defun org-refile-goto-last-stored ()
10898 "Go to the location where the last refile was stored."
10900 (bookmark-jump "org-refile-last-stored")
10901 (message "This is the location of the last refile"))
10903 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
10905 "Prompt the user for a refile location, using PROMPT.
10906 PROMPT should not be suffixed with a colon and a space, because
10907 this function appends the default value from
10908 `org-refile-history' automatically, if that is not empty.
10909 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
10910 this is used for the GOTO interface."
10911 (let ((org-refile-targets org-refile-targets)
10912 (org-refile-use-outline-path org-refile-use-outline-path)
10914 (when (and (derived-mode-p 'org-mode)
10915 (not org-refile-use-cache)
10919 (setq excluded-entries
10920 (append excluded-entries (list (org-get-heading t t)))))))
10921 (setq org-refile-target-table
10922 (org-refile-get-targets default-buffer excluded-entries)))
10923 (unless org-refile-target-table
10924 (error "No refile targets"))
10925 (let* ((prompt (concat prompt
10926 (and (car org-refile-history)
10927 (concat " (default " (car org-refile-history) ")"))
10929 (cbuf (current-buffer))
10930 (partial-completion-mode nil)
10931 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
10932 (cfunc (if (and org-refile-use-outline-path
10933 org-outline-path-complete-in-steps)
10934 'org-olpath-completing-read
10935 'org-icompleting-read))
10936 (extra (if org-refile-use-outline-path "/" ""))
10937 (filename (and cfn (expand-file-name cfn)))
10940 (if (and (not (member org-refile-use-outline-path
10941 '(file full-file-path)))
10942 (not (equal filename (nth 1 x))))
10943 (cons (concat (car x) extra " ("
10944 (file-name-nondirectory (nth 1 x)) ")")
10946 (cons (concat (car x) extra) (cdr x))))
10947 org-refile-target-table))
10948 (completion-ignore-case t)
10949 pa answ parent-target child parent old-hist)
10950 (setq old-hist org-refile-history)
10951 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
10952 nil 'org-refile-history (car org-refile-history)))
10953 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
10954 (org-refile-check-position pa)
10957 (when (or (not org-refile-history)
10958 (not (eq old-hist org-refile-history))
10959 (not (equal (car pa) (car org-refile-history))))
10960 (setq org-refile-history
10961 (cons (car pa) (if (assoc (car org-refile-history) tbl)
10963 (cdr org-refile-history))))
10964 (if (equal (car org-refile-history) (nth 1 org-refile-history))
10965 (pop org-refile-history)))
10967 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
10969 (setq parent (match-string 1 answ)
10970 child (match-string 2 answ))
10971 (setq parent-target (or (assoc parent tbl)
10972 (assoc (concat parent "/") tbl)))
10973 (when (and parent-target
10974 (or (eq new-nodes t)
10975 (and (eq new-nodes 'confirm)
10976 (y-or-n-p (format "Create new node \"%s\"? "
10978 (org-refile-new-child parent-target child)))
10979 (error "Invalid target location")))))
10981 (declare-function org-string-nw-p "org-macs.el" (s))
10982 (defun org-refile-check-position (refile-pointer)
10983 "Check if the refile pointer matches the readline to which it points."
10984 (let* ((file (nth 1 refile-pointer))
10985 (re (nth 2 refile-pointer))
10986 (pos (nth 3 refile-pointer))
10988 (when (org-string-nw-p re)
10989 (setq buffer (if (markerp pos)
10990 (marker-buffer pos)
10991 (or (find-buffer-visiting file)
10992 (find-file-noselect file))))
10993 (with-current-buffer buffer
10998 (beginning-of-line 1)
10999 (unless (org-looking-at-p re)
11000 (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
11002 (defun org-refile-new-child (parent-target child)
11003 "Use refile target PARENT-TARGET to add new CHILD below it."
11004 (unless parent-target
11005 (error "Cannot find parent for new node"))
11006 (let ((file (nth 1 parent-target))
11007 (pos (nth 3 parent-target))
11009 (with-current-buffer (or (find-buffer-visiting file)
11010 (find-file-noselect file))
11016 (goto-char (point-max))
11017 (if (not (bolp)) (newline)))
11018 (when (looking-at org-outline-regexp)
11019 (setq level (funcall outline-level))
11020 (org-end-of-subtree t t))
11021 (org-back-over-empty-lines)
11022 (insert "\n" (make-string
11023 (if pos (org-get-valid-level level 1) 1) ?*)
11025 (beginning-of-line 0)
11026 (list (concat (car parent-target) "/" child) file "" (point)))))))
11028 (defun org-olpath-completing-read (prompt collection &rest args)
11029 "Read an outline path like a file name."
11030 (let ((thetable collection)
11031 (org-completion-use-ido nil) ; does not work with ido.
11032 (org-completion-use-iswitchb nil)) ; or iswitchb
11034 'org-icompleting-read prompt
11035 (lambda (string predicate &optional flag)
11036 (let (rtn r f (l (length string)))
11040 (try-completion string thetable))
11043 (setq rtn (all-completions string thetable predicate))
11046 (setq r (substring x l))
11047 (if (string-match " ([^)]*)$" x)
11048 (setq f (match-string 0 x))
11050 (if (string-match "/" r)
11051 (concat string (substring r 0 (match-end 0)) f)
11056 (assoc string thetable)))))
11059 ;;;; Dynamic blocks
11061 (defun org-find-dblock (name)
11062 "Find the first dynamic block with name NAME in the buffer.
11063 If not found, stay at current position and return nil."
11066 (goto-char (point-min))
11067 (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
11069 (match-beginning 0))))
11070 (if pos (goto-char pos))
11073 (defconst org-dblock-start-re
11074 "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
11075 "Matches the start line of a dynamic block, with parameters.")
11077 (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
11078 "Matches the end of a dynamic block.")
11080 (defun org-create-dblock (plist)
11081 "Create a dynamic block section, with parameters taken from PLIST.
11082 PLIST must contain a :name entry which is used as name of the block."
11083 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
11086 (let ((col (current-column))
11087 (name (plist-get plist :name)))
11088 (insert "#+BEGIN: " name)
11090 (if (eq (car plist) :name)
11091 (setq plist (cddr plist))
11092 (insert " " (prin1-to-string (pop plist)))))
11093 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
11094 (beginning-of-line -2)))
11096 (defun org-prepare-dblock ()
11097 "Prepare dynamic block for refresh.
11098 This empties the block, puts the cursor at the insert position and returns
11099 the property list including an extra property :name with the block name."
11100 (unless (looking-at org-dblock-start-re)
11101 (error "Not at a dynamic block"))
11102 (let* ((begdel (1+ (match-end 0)))
11103 (name (org-no-properties (match-string 1)))
11104 (params (append (list :name name)
11105 (read (concat "(" (match-string 3) ")")))))
11107 (beginning-of-line 1)
11108 (skip-chars-forward " \t")
11109 (setq params (plist-put params :indentation-column (current-column))))
11110 (unless (re-search-forward org-dblock-end-re nil t)
11111 (error "Dynamic block not terminated"))
11114 (list :content (buffer-substring
11115 begdel (match-beginning 0)))))
11116 (delete-region begdel (match-beginning 0))
11121 (defun org-map-dblocks (&optional command)
11122 "Apply COMMAND to all dynamic blocks in the current buffer.
11123 If COMMAND is not given, use `org-update-dblock'."
11124 (let ((cmd (or command 'org-update-dblock)))
11126 (goto-char (point-min))
11127 (while (re-search-forward org-dblock-start-re nil t)
11128 (goto-char (match-beginning 0))
11130 (condition-case nil
11132 (error (message "Error during update of dynamic block"))))
11133 (unless (re-search-forward org-dblock-end-re nil t)
11134 (error "Dynamic block not terminated"))))))
11136 (defun org-dblock-update (&optional arg)
11137 "User command for updating dynamic blocks.
11138 Update the dynamic block at point. With prefix ARG, update all dynamic
11139 blocks in the buffer."
11142 (org-update-all-dblocks)
11143 (or (looking-at org-dblock-start-re)
11144 (org-beginning-of-dblock))
11145 (org-update-dblock)))
11147 (defun org-update-dblock ()
11148 "Update the dynamic block at point.
11149 This means to empty the block, parse for parameters and then call
11150 the correct writing function."
11152 (save-window-excursion
11153 (let* ((pos (point))
11154 (line (org-current-line))
11155 (params (org-prepare-dblock))
11156 (name (plist-get params :name))
11157 (indent (plist-get params :indentation-column))
11158 (cmd (intern (concat "org-dblock-write:" name))))
11159 (message "Updating dynamic block `%s' at line %d..." name line)
11160 (funcall cmd params)
11161 (message "Updating dynamic block `%s' at line %d...done" name line)
11163 (when (and indent (> indent 0))
11164 (setq indent (make-string indent ?\ ))
11166 (org-beginning-of-dblock)
11168 (while (not (looking-at org-dblock-end-re))
11170 (beginning-of-line 2))
11171 (when (looking-at org-dblock-end-re)
11172 (and (looking-at "[ \t]+")
11173 (replace-match ""))
11174 (insert indent)))))))
11176 (defun org-beginning-of-dblock ()
11177 "Find the beginning of the dynamic block at point.
11178 Error if there is no such block at point."
11179 (let ((pos (point))
11182 (if (and (re-search-backward org-dblock-start-re nil t)
11183 (setq beg (match-beginning 0))
11184 (re-search-forward org-dblock-end-re nil t)
11185 (> (match-end 0) pos))
11188 (error "Not in a dynamic block"))))
11191 (defun org-update-all-dblocks ()
11192 "Update all dynamic blocks in the buffer.
11193 This function can be used in a hook."
11195 (when (derived-mode-p 'org-mode)
11196 (org-map-dblocks 'org-update-dblock)))
11201 (defconst org-additional-option-like-keywords
11202 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
11203 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
11204 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
11205 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
11207 "ORGTBL" "TBLFM:" "TBLNAME:"
11208 "BEGIN_EXAMPLE" "END_EXAMPLE"
11209 "BEGIN_VERBATIM" "END_VERBATIM"
11210 "BEGIN_QUOTE" "END_QUOTE"
11211 "BEGIN_VERSE" "END_VERSE"
11212 "BEGIN_CENTER" "END_CENTER"
11213 "BEGIN_SRC" "END_SRC"
11214 "BEGIN_RESULT" "END_RESULT"
11215 "BEGIN_lstlisting" "END_lstlisting"
11217 "HEADER:" "HEADERS:"
11218 "COLUMNS:" "PROPERTY:"
11219 "CAPTION:" "LABEL:"
11225 (defconst org-options-keywords
11226 '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
11227 "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
11228 "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
11229 "LINK_UP:" "LINK_HOME:" "LINK:"
11230 "XSLT:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
11231 "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:"
11232 "FILETAGS:" "ARCHIVE:"))
11234 (defconst org-additional-option-like-keywords-for-flyspell
11237 (mapconcat (lambda(k)
11238 (replace-regexp-in-string
11240 (concat k " " (downcase k) " " (upcase k))))
11241 (append org-options-keywords org-additional-option-like-keywords)
11245 (defcustom org-structure-template-alist
11247 ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
11248 "<src lang=\"?\">\n\n</src>")
11249 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
11250 "<example>\n?\n</example>")
11251 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
11252 "<quote>\n?\n</quote>")
11253 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
11254 "<verse>\n?\n</verse>")
11255 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
11256 "<center>\n?\n</center>")
11257 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
11258 "<literal style=\"latex\">\n?\n</literal>")
11260 "<literal style=\"latex\">?</literal>")
11261 ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
11262 "<literal style=\"html\">\n?\n</literal>")
11264 "<literal style=\"html\">?</literal>")
11265 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
11269 ("I" "#+INCLUDE %file ?"
11270 "<include file=%file markup=\"?\">")
11272 "Structure completion elements.
11273 This is a list of abbreviation keys and values. The value gets inserted
11274 if you type `<' followed by the key and then press the completion key,
11275 usually `M-TAB'. %file will be replaced by a file name after prompting
11276 for the file using completion. The cursor will be placed at the position
11277 of the `?` in the template.
11278 There are two templates for each key, the first uses the original Org syntax,
11279 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
11280 the default when the /org-mtags.el/ module has been loaded. See also the
11281 variable `org-mtags-prefer-muse-templates'."
11282 :group 'org-completion
11284 (string :tag "Key")
11285 (string :tag "Template")
11286 (string :tag "Muse Template")))
11288 (defun org-try-structure-completion ()
11289 "Try to complete a structure template before point.
11290 This looks for strings like \"<e\" on an otherwise empty line and
11292 (let ((l (buffer-substring (point-at-bol) (point)))
11294 (when (and (looking-at "[ \t]*$")
11295 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
11296 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
11297 (org-complete-expand-structure-template (+ -1 (point-at-bol)
11298 (match-beginning 1)) a)
11301 (defun org-complete-expand-structure-template (start cell)
11302 "Expand a structure template."
11303 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
11304 (rpl (nth (if musep 2 1) cell))
11306 (delete-region start (point))
11307 (when (string-match "\\`#\\+" rpl)
11310 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
11311 (setq ind (buffer-substring (point-at-bol) (point))))
11313 (setq start (point))
11314 (if (string-match "%file" rpl)
11315 (setq rpl (replace-match
11319 (abbreviate-file-name (read-file-name "Include file: ")))
11322 (setq rpl (mapconcat 'identity (split-string rpl "\n")
11323 (concat "\n" ind)))
11325 (if (re-search-backward "\\?" start t) (delete-char 1))))
11327 ;;;; TODO, DEADLINE, Comments
11329 (defun org-toggle-comment ()
11330 "Change the COMMENT state of an entry."
11333 (org-back-to-heading)
11334 (let (case-fold-search)
11336 ((looking-at (format org-heading-keyword-regexp-format
11337 org-comment-string))
11338 (goto-char (match-end 1))
11339 (looking-at (concat " +" org-comment-string))
11340 (replace-match "" t t)
11341 (when (eolp) (insert " ")))
11342 ((looking-at org-outline-regexp)
11343 (goto-char (match-end 0))
11344 (insert org-comment-string " "))))))
11346 (defvar org-last-todo-state-is-todo nil
11347 "This is non-nil when the last TODO state change led to a TODO state.
11348 If the last change removed the TODO tag or switched to DONE, then
11351 (defvar org-setting-tags nil) ; dynamically skipped
11353 (defvar org-todo-setup-filter-hook nil
11354 "Hook for functions that pre-filter todo specs.
11355 Each function takes a todo spec and returns either nil or the spec
11356 transformed into canonical form." )
11358 (defvar org-todo-get-default-hook nil
11359 "Hook for functions that get a default item for todo.
11360 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
11361 nil or a string to be used for the todo mark." )
11363 (defvar org-agenda-headline-snapshot-before-repeat)
11365 (defun org-current-effective-time ()
11366 "Return current time adjusted for `org-extend-today-until' variable."
11367 (let* ((ct (org-current-time))
11368 (dct (decode-time ct))
11370 (if (and org-use-effective-time
11371 (< (nth 2 dct) org-extend-today-until))
11372 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
11376 (defun org-todo-yesterday (&optional arg)
11377 "Like `org-todo' but the time of change will be 23:59 of yesterday."
11379 (if (eq major-mode 'org-agenda-mode)
11380 (apply 'org-agenda-todo-yesterday arg)
11381 (let* ((hour (third (decode-time
11382 (org-current-time))))
11383 (org-extend-today-until (1+ hour)))
11386 (defun org-todo (&optional arg)
11387 "Change the TODO state of an item.
11388 The state of an item is given by a keyword at the start of the heading,
11390 *** TODO Write paper
11393 The different keywords are specified in the variable `org-todo-keywords'.
11394 By default the available states are \"TODO\" and \"DONE\".
11395 So for this example: when the item starts with TODO, it is changed to DONE.
11396 When it starts with DONE, the DONE is removed. And when neither TODO nor
11397 DONE are present, add TODO at the beginning of the heading.
11399 With \\[universal-argument] prefix arg, use completion to determine the new \
11401 With numeric prefix arg, switch to that state.
11402 With a double \\[universal-argument] prefix, switch to the next set of TODO \
11403 keywords (nextset).
11404 With a triple \\[universal-argument] prefix, circumvent any state blocking.
11405 With a numeric prefix arg of 0, inhibit note taking for the change.
11407 For calling through lisp, arg is also interpreted in the following way:
11408 'none -> empty state
11409 \"\"(empty string) -> switch to empty state
11410 'done -> switch to DONE
11411 'nextset -> switch to the next set of keywords
11412 'previousset -> switch to the previous set of keywords
11413 \"WAITING\" -> switch to the specified keyword, but only if it
11414 really is a member of `org-todo-keywords'."
11416 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
11417 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
11418 'region-start-level 'region))
11419 org-loop-over-headlines-in-active-region)
11422 org-loop-over-headlines-in-active-region
11423 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
11424 (if (equal arg '(16)) (setq arg 'nextset))
11425 (let ((org-blocker-hook org-blocker-hook)
11426 (case-fold-search nil))
11427 (when (equal arg '(64))
11428 (setq arg nil org-blocker-hook nil))
11429 (when (and org-blocker-hook
11430 (or org-inhibit-blocking
11431 (org-entry-get nil "NOBLOCKING")))
11432 (setq org-blocker-hook nil))
11435 (org-back-to-heading t)
11436 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
11437 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
11438 (looking-at "\\(?: *\\|[ \t]*$\\)"))
11439 (let* ((match-data (match-data))
11440 (startpos (point-at-bol))
11441 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
11442 (org-log-done org-log-done)
11443 (org-log-repeat org-log-repeat)
11444 (org-todo-log-states org-todo-log-states)
11445 (org-inhibit-logging
11447 (progn (setq arg nil) 'note) org-inhibit-logging))
11448 (this (match-string 1))
11449 (hl-pos (match-beginning 0))
11450 (head (org-get-todo-sequence-head this))
11451 (ass (assoc head org-todo-kwd-alist))
11452 (interpret (nth 1 ass))
11453 (done-word (nth 3 ass))
11454 (final-done-word (nth 4 ass))
11455 (org-last-state (or this ""))
11456 (completion-ignore-case t)
11457 (member (member this org-todo-keywords-1))
11458 (tail (cdr member))
11460 ((and org-todo-key-trigger
11461 (or (and (equal arg '(4))
11462 (eq org-use-fast-todo-selection 'prefix))
11463 (and (not arg) org-use-fast-todo-selection
11464 (not (eq org-use-fast-todo-selection
11466 ;; Use fast selection
11467 (org-fast-todo-selection))
11468 ((and (equal arg '(4))
11469 (or (not org-use-fast-todo-selection)
11470 (not org-todo-key-trigger)))
11471 ;; Read a state with completion
11472 (org-icompleting-read
11473 "State: " (mapcar (lambda(x) (list x))
11474 org-todo-keywords-1)
11478 (if tail (car tail) nil)
11479 (car org-todo-keywords-1)))
11481 (if (equal member org-todo-keywords-1)
11484 (nth (- (length org-todo-keywords-1)
11486 org-todo-keywords-1)
11487 (org-last org-todo-keywords-1))))
11488 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
11489 (setq arg nil))) ; hack to fall back to cycling
11491 ;; user or caller requests a specific state
11493 ((equal arg "") nil)
11494 ((eq arg 'none) nil)
11495 ((eq arg 'done) (or done-word (car org-done-keywords)))
11497 (or (car (cdr (member head org-todo-heads)))
11498 (car org-todo-heads)))
11499 ((eq arg 'previousset)
11500 (let ((org-todo-heads (reverse org-todo-heads)))
11501 (or (car (cdr (member head org-todo-heads)))
11502 (car org-todo-heads))))
11503 ((car (member arg org-todo-keywords-1)))
11505 (error "State `%s' not valid in this file" arg))
11506 ((nth (1- (prefix-numeric-value arg))
11507 org-todo-keywords-1))))
11508 ((null member) (or head (car org-todo-keywords-1)))
11509 ((equal this final-done-word) nil) ;; -> make empty
11510 ((null tail) nil) ;; -> first entry
11511 ((memq interpret '(type priority))
11512 (if (eq this-command last-command)
11514 (if (> (length tail) 0)
11515 (or done-word (car org-done-keywords))
11520 (run-hook-with-args-until-success
11521 'org-todo-get-default-hook org-state org-last-state)
11523 (next (if org-state (concat " " org-state " ") " "))
11524 (change-plist (list :type 'todo-state-change :from this :to org-state
11525 :position startpos))
11527 (when org-blocker-hook
11528 (setq org-last-todo-state-is-todo
11529 (not (member this org-done-keywords)))
11530 (unless (save-excursion
11532 (org-with-wide-buffer
11533 (run-hook-with-args-until-failure
11534 'org-blocker-hook change-plist))))
11535 (if (org-called-interactively-p 'interactive)
11536 (error "TODO state change from %s to %s blocked" this org-state)
11538 (message "TODO state change from %s to %s blocked" this org-state)
11539 (throw 'exit nil))))
11540 (store-match-data match-data)
11541 (replace-match next t t)
11542 (unless (pos-visible-in-window-p hl-pos)
11543 (message "TODO state changed to %s" (org-trim next)))
11545 (setq head (org-get-todo-sequence-head org-state)
11546 ass (assoc head org-todo-kwd-alist)
11547 interpret (nth 1 ass)
11548 done-word (nth 3 ass)
11549 final-done-word (nth 4 ass)))
11550 (when (memq arg '(nextset previousset))
11551 (message "Keyword-Set %d/%d: %s"
11552 (- (length org-todo-sets) -1
11553 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
11554 (length org-todo-sets)
11555 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
11556 (setq org-last-todo-state-is-todo
11557 (not (member org-state org-done-keywords)))
11558 (setq now-done-p (and (member org-state org-done-keywords)
11559 (not (member this org-done-keywords))))
11560 (and logging (org-local-logging logging))
11561 (when (and (or org-todo-log-states org-log-done)
11562 (not (eq org-inhibit-logging t))
11563 (not (memq arg '(nextset previousset))))
11564 ;; we need to look at recording a time and note
11565 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
11566 (nth 2 (assoc this org-todo-log-states))))
11567 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
11568 (setq dolog 'time))
11569 (when (and org-state
11570 (member org-state org-not-done-keywords)
11571 (not (member this org-not-done-keywords)))
11572 ;; This is now a todo state and was not one before
11573 ;; If there was a CLOSED time stamp, get rid of it.
11574 (org-add-planning-info nil nil 'closed))
11575 (when (and now-done-p org-log-done)
11576 ;; It is now done, and it was not done before
11577 (org-add-planning-info 'closed (org-current-effective-time))
11578 (if (and (not dolog) (eq 'note org-log-done))
11579 (org-add-log-setup 'done org-state this 'findpos 'note)))
11580 (when (and org-state dolog)
11581 ;; This is a non-nil state, and we need to log it
11582 (org-add-log-setup 'state org-state this 'findpos dolog)))
11583 ;; Fixup tag positioning
11584 (org-todo-trigger-tag-changes org-state)
11585 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
11586 (when org-provide-todo-statistics
11587 (org-update-parent-todo-statistics))
11588 (run-hooks 'org-after-todo-state-change-hook)
11589 (if (and arg (not (member org-state org-done-keywords)))
11590 (setq head (org-get-todo-sequence-head org-state)))
11591 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
11592 ;; Do we need to trigger a repeat?
11594 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
11595 ;; This is for the agenda, take a snapshot of the headline.
11597 (setq org-agenda-headline-snapshot-before-repeat
11598 (org-get-heading))))
11599 (org-auto-repeat-maybe org-state))
11600 ;; Fixup cursor location if close to the keyword
11601 (if (and (outline-on-heading-p)
11603 (save-excursion (beginning-of-line 1)
11604 (looking-at org-todo-line-regexp))
11605 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
11607 (goto-char (or (match-end 2) (match-end 1)))
11608 (and (looking-at " ") (just-one-space))))
11609 (when org-trigger-hook
11611 (run-hook-with-args 'org-trigger-hook change-plist)))))))))
11613 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
11614 "Block turning an entry into a TODO, using the hierarchy.
11615 This checks whether the current task should be blocked from state
11616 changes. Such blocking occurs when:
11618 1. The task has children which are not all in a completed state.
11620 2. A task has a parent with the property :ORDERED:, and there
11621 are siblings prior to the current task with incomplete
11624 3. The parent of the task is blocked because it has siblings that should
11625 be done first, or is child of a block grandparent TODO entry."
11627 (if (not org-enforce-todo-dependencies)
11628 t ; if locally turned off don't block
11630 ;; If this is not a todo state change, or if this entry is already DONE,
11632 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11633 (member (plist-get change-plist :from)
11634 (cons 'done org-done-keywords))
11635 (member (plist-get change-plist :to)
11636 (cons 'todo org-not-done-keywords))
11637 (not (plist-get change-plist :to)))
11638 (throw 'dont-block t))
11639 ;; If this task has children, and any are undone, it's blocked
11641 (org-back-to-heading t)
11642 (let ((this-level (funcall outline-level)))
11643 (outline-next-heading)
11644 (let ((child-level (funcall outline-level)))
11645 (while (and (not (eobp))
11646 (> child-level this-level))
11647 ;; this todo has children, check whether they are all
11649 (if (and (not (org-entry-is-done-p))
11650 (org-entry-is-todo-p))
11651 (throw 'dont-block nil))
11652 (outline-next-heading)
11653 (setq child-level (funcall outline-level))))))
11654 ;; Otherwise, if the task's parent has the :ORDERED: property, and
11655 ;; any previous siblings are undone, it's blocked
11657 (org-back-to-heading t)
11658 (let* ((pos (point))
11659 (parent-pos (and (org-up-heading-safe) (point))))
11660 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11661 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11663 (re-search-forward org-not-done-heading-regexp pos t))
11664 (throw 'dont-block nil)) ; block, there is an older sibling not done.
11665 ;; Search further up the hierarchy, to see if an ancestor is blocked
11667 (goto-char parent-pos)
11668 (if (not (looking-at org-not-done-heading-regexp))
11669 (throw 'dont-block t)) ; do not block, parent is not a TODO
11671 (setq parent-pos (and (org-up-heading-safe) (point)))
11672 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11673 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11675 (re-search-forward org-not-done-heading-regexp pos t))
11676 (throw 'dont-block nil)))))))) ; block, older sibling not done.
11678 (defcustom org-track-ordered-property-with-tag nil
11679 "Should the ORDERED property also be shown as a tag?
11680 The ORDERED property decides if an entry should require subtasks to be
11681 completed in sequence. Since a property is not very visible, setting
11682 this option means that toggling the ORDERED property with the command
11683 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
11684 not relevant for the behavior, but it makes things more visible.
11686 Note that toggling the tag with tags commands will not change the property
11687 and therefore not influence behavior!
11689 This can be t, meaning the tag ORDERED should be used, It can also be a
11690 string to select a different tag for this task."
11693 (const :tag "No tracking" nil)
11694 (const :tag "Track with ORDERED tag" t)
11695 (string :tag "Use other tag")))
11697 (defun org-toggle-ordered-property ()
11698 "Toggle the ORDERED property of the current entry.
11699 For better visibility, you can track the value of this property with a tag.
11700 See variable `org-track-ordered-property-with-tag'."
11702 (let* ((t1 org-track-ordered-property-with-tag)
11703 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
11705 (org-back-to-heading)
11706 (if (org-entry-get nil "ORDERED")
11708 (org-delete-property "ORDERED")
11709 (and tag (org-toggle-tag tag 'off))
11710 (message "Subtasks can be completed in arbitrary order"))
11711 (org-entry-put nil "ORDERED" "t")
11712 (and tag (org-toggle-tag tag 'on))
11713 (message "Subtasks must be completed in sequence")))))
11715 (defvar org-blocked-by-checkboxes) ; dynamically scoped
11716 (defun org-block-todo-from-checkboxes (change-plist)
11717 "Block turning an entry into a TODO, using checkboxes.
11718 This checks whether the current task should be blocked from state
11719 changes because there are unchecked boxes in this entry."
11720 (if (not org-enforce-todo-checkbox-dependencies)
11721 t ; if locally turned off don't block
11723 ;; If this is not a todo state change, or if this entry is already DONE,
11725 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11726 (member (plist-get change-plist :from)
11727 (cons 'done org-done-keywords))
11728 (member (plist-get change-plist :to)
11729 (cons 'todo org-not-done-keywords))
11730 (not (plist-get change-plist :to)))
11731 (throw 'dont-block t))
11732 ;; If this task has checkboxes that are not checked, it's blocked
11734 (org-back-to-heading t)
11735 (let ((beg (point)) end)
11736 (outline-next-heading)
11739 (if (org-list-search-forward
11740 (concat (org-item-beginning-re)
11741 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
11745 (if (boundp 'org-blocked-by-checkboxes)
11746 (setq org-blocked-by-checkboxes t))
11747 (throw 'dont-block nil)))))
11748 t))) ; do not block
11750 (defun org-entry-blocked-p ()
11751 "Is the current entry blocked?"
11752 (if (org-entry-get nil "NOBLOCKING")
11753 nil ;; Never block this entry
11755 (run-hook-with-args-until-failure
11757 (list :type 'todo-state-change
11762 (defun org-update-statistics-cookies (all)
11763 "Update the statistics cookie, either from TODO or from checkboxes.
11764 This should be called with the cursor in a line with a statistics cookie."
11768 (org-update-checkbox-count 'all)
11769 (org-map-entries 'org-update-parent-todo-statistics))
11770 (if (not (org-at-heading-p))
11771 (org-update-checkbox-count)
11772 (let ((pos (move-marker (make-marker) (point)))
11774 (ignore-errors (org-back-to-heading t))
11775 (if (not (org-at-heading-p))
11776 (org-update-checkbox-count)
11777 (setq l1 (org-outline-level))
11778 (setq end (save-excursion
11779 (outline-next-heading)
11780 (if (org-at-heading-p) (setq l2 (org-outline-level)))
11782 (if (and (save-excursion
11784 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
11785 (not (save-excursion (re-search-forward
11786 ":COOKIE_DATA:.*\\<todo\\>" end t))))
11787 (org-update-checkbox-count)
11788 (if (and l2 (> l2 l1))
11791 (org-update-parent-todo-statistics))
11793 (beginning-of-line 1)
11794 (while (re-search-forward
11795 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
11797 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
11799 (move-marker pos nil)))))
11801 (defvar org-entry-property-inherited-from) ;; defined below
11802 (defun org-update-parent-todo-statistics ()
11803 "Update any statistics cookie in the parent of the current headline.
11804 When `org-hierarchical-todo-statistics' is nil, statistics will cover
11805 the entire subtree and this will travel up the hierarchy and update
11806 statistics everywhere."
11807 (let* ((prop (save-excursion (org-up-heading-safe)
11808 (org-entry-get nil "COOKIE_DATA" 'inherit)))
11809 (recursive (or (not org-hierarchical-todo-statistics)
11810 (and prop (string-match "\\<recursive\\>" prop))))
11811 (lim (or (and prop (marker-position org-entry-property-inherited-from))
11814 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
11815 level ltoggle l1 new ndel
11816 (cnt-all 0) (cnt-done 0) is-percent kwd
11817 checkbox-beg ov ovs ove cookie-present)
11820 (beginning-of-line 1)
11821 (setq ltoggle (funcall outline-level))
11822 ;; Three situations are to consider:
11824 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
11825 ;; to the top-level ancestor on the headline;
11827 ;; 2. If parent has "recursive" property, repeat up to the
11828 ;; headline setting that property, taking inheritance into
11831 ;; 3. Else, move up to direct parent and proceed only once.
11832 (while (and (setq level (org-up-heading-safe))
11833 (or recursive first)
11835 (setq first nil cookie-present nil)
11839 (downcase (or (org-entry-get nil "COOKIE_DATA")
11842 (while (re-search-forward box-re (point-at-eol) t)
11843 (setq cnt-all 0 cnt-done 0 cookie-present t)
11844 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
11846 (unless (outline-next-heading) (throw 'exit nil))
11847 (while (and (looking-at org-complex-heading-regexp)
11848 (> (setq l1 (length (match-string 1))) level))
11849 (setq kwd (and (or recursive (= l1 ltoggle))
11851 (if (or (eq org-provide-todo-statistics 'all-headlines)
11852 (and (listp org-provide-todo-statistics)
11853 (or (member kwd org-provide-todo-statistics)
11854 (member kwd org-done-keywords))))
11855 (setq cnt-all (1+ cnt-all))
11856 (if (eq org-provide-todo-statistics t)
11857 (and kwd (setq cnt-all (1+ cnt-all)))))
11858 (and (member kwd org-done-keywords)
11859 (setq cnt-done (1+ cnt-done)))
11860 (outline-next-heading)))
11863 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
11864 (format "[%d/%d]" cnt-done cnt-all))
11865 ndel (- (match-end 0) checkbox-beg))
11866 ;; handle overlays when updating cookie from column view
11867 (when (setq ov (car (overlays-at checkbox-beg)))
11868 (setq ovs (overlay-start ov) ove (overlay-end ov))
11869 (delete-overlay ov))
11870 (goto-char checkbox-beg)
11872 (delete-region (point) (+ (point) ndel))
11873 (when org-auto-align-tags (org-fix-tags-on-the-fly))
11874 (when ov (move-overlay ov ovs ove)))
11875 (when cookie-present
11876 (run-hook-with-args 'org-after-todo-statistics-hook
11877 cnt-done (- cnt-all cnt-done))))))
11878 (run-hooks 'org-todo-statistics-hook)))
11880 (defvar org-after-todo-statistics-hook nil
11881 "Hook that is called after a TODO statistics cookie has been updated.
11882 Each function is called with two arguments: the number of not-done entries
11883 and the number of done entries.
11885 For example, the following function, when added to this hook, will switch
11886 an entry to DONE when all children are done, and back to TODO when new
11887 entries are set to a TODO status. Note that this hook is only called
11888 when there is a statistics cookie in the headline!
11890 (defun org-summary-todo (n-done n-not-done)
11891 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
11892 (let (org-log-done org-log-states) ; turn off logging
11893 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
11896 (defvar org-todo-statistics-hook nil
11897 "Hook that is run whenever Org thinks TODO statistics should be updated.
11898 This hook runs even if there is no statistics cookie present, in which case
11899 `org-after-todo-statistics-hook' would not run.")
11901 (defun org-todo-trigger-tag-changes (state)
11902 "Apply the changes defined in `org-todo-state-tags-triggers'."
11903 (let ((l org-todo-state-tags-triggers)
11905 (when (or (not state) (equal state ""))
11906 (setq changes (append changes (cdr (assoc "" l)))))
11907 (when (and (stringp state) (> (length state) 0))
11908 (setq changes (append changes (cdr (assoc state l)))))
11909 (when (member state org-not-done-keywords)
11910 (setq changes (append changes (cdr (assoc 'todo l)))))
11911 (when (member state org-done-keywords)
11912 (setq changes (append changes (cdr (assoc 'done l)))))
11913 (dolist (c changes)
11914 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
11916 (defun org-local-logging (value)
11917 "Get logging settings from a property VALUE."
11919 ;; directly set the variables, they are already local.
11920 (setq org-log-done nil
11922 org-todo-log-states nil)
11923 (setq words (org-split-string value))
11924 (while (setq w (pop words))
11926 ((setq a (assoc w org-startup-options))
11927 (and (member (nth 1 a) '(org-log-done org-log-repeat))
11928 (set (nth 1 a) (nth 2 a))))
11929 ((setq a (org-extract-log-state-settings w))
11930 (and (member (car a) org-todo-keywords-1)
11931 (push a org-todo-log-states)))))))
11933 (defun org-get-todo-sequence-head (kwd)
11934 "Return the head of the TODO sequence to which KWD belongs.
11935 If KWD is not set, check if there is a text property remembering the
11940 (or (get-text-property (point-at-bol) 'org-todo-head)
11942 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
11943 nil (point-at-eol)))
11944 (get-text-property p 'org-todo-head))))
11945 ((not (member kwd org-todo-keywords-1))
11946 (car org-todo-keywords-1))
11947 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
11949 (defun org-fast-todo-selection ()
11950 "Fast TODO keyword selection with single keys.
11951 Returns the new TODO keyword, or nil if no state change should occur."
11952 (let* ((fulltable org-todo-key-alist)
11953 (done-keywords org-done-keywords) ;; needed for the faces.
11954 (maxlen (apply 'max (mapcar
11956 (if (stringp (car x)) (string-width (car x)) 0))
11959 (fwidth (+ maxlen 3 1 3))
11960 (ncol (/ (- (window-width) 4) fwidth))
11964 (save-window-excursion
11966 (set-buffer (get-buffer-create " *Org todo*"))
11967 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
11969 (org-set-local 'org-done-keywords done-keywords)
11970 (setq tbl fulltable cnt 0)
11971 (while (setq e (pop tbl))
11973 ((equal e '(:startgroup))
11974 (push '() groups) (setq ingroup t)
11975 (when (not (= cnt 0))
11979 ((equal e '(:endgroup))
11980 (setq ingroup nil cnt 0)
11982 ((equal e '(:newline))
11983 (when (not (= cnt 0))
11987 (while (equal (car tbl) '(:newline))
11989 (setq tbl (cdr tbl)))))
11991 (setq tg (car e) c (cdr e))
11992 (if ingroup (push tg (car groups)))
11993 (setq tg (org-add-props tg nil 'face
11994 (org-get-todo-face tg)))
11995 (if (and (= cnt 0) (not ingroup)) (insert " "))
11996 (insert "[" c "] " tg (make-string
11997 (- fwidth 4 (length tg)) ?\ ))
11998 (when (= (setq cnt (1+ cnt)) ncol)
12000 (if ingroup (insert " "))
12003 (goto-char (point-min))
12004 (if (not expert) (org-fit-window-to-buffer))
12005 (message "[a-z..]:Set [SPC]:clear")
12006 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
12009 (and (= c ?q) (not (rassoc c fulltable))))
12010 (setq quit-flag t))
12012 ((setq e (rassoc c fulltable) tg (car e))
12014 (t (setq quit-flag t)))))))
12016 (defun org-entry-is-todo-p ()
12017 (member (org-get-todo-state) org-not-done-keywords))
12019 (defun org-entry-is-done-p ()
12020 (member (org-get-todo-state) org-done-keywords))
12022 (defun org-get-todo-state ()
12024 (org-back-to-heading t)
12025 (and (looking-at org-todo-line-regexp)
12027 (match-string 2))))
12029 (defun org-at-date-range-p (&optional inactive-ok)
12030 "Is the cursor inside a date range?"
12034 (let ((pos (point)))
12035 (skip-chars-backward "^[<\r\n")
12036 (skip-chars-backward "<[")
12037 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12038 (>= (match-end 0) pos)
12040 (skip-chars-backward "^<[\r\n")
12041 (skip-chars-backward "<[")
12042 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12043 (>= (match-end 0) pos)
12047 (defun org-get-repeat (&optional tagline)
12048 "Check if there is a deadline/schedule with repeater in this entry."
12051 (org-back-to-heading t)
12052 (and (re-search-forward (if tagline
12053 (concat tagline "\\s-*" org-repeat-re)
12055 (org-entry-end-position) t)
12056 (match-string-no-properties 1)))))
12058 (defvar org-last-changed-timestamp)
12059 (defvar org-last-inserted-timestamp)
12060 (defvar org-log-post-message)
12061 (defvar org-log-note-purpose)
12062 (defvar org-log-note-how)
12063 (defvar org-log-note-extra)
12064 (defun org-auto-repeat-maybe (done-word)
12065 "Check if the current headline contains a repeated deadline/schedule.
12066 If yes, set TODO state back to what it was and change the base date
12067 of repeating deadline/scheduled time stamps to new date.
12068 This function is run automatically after each state change to a DONE state."
12069 ;; last-state is dynamically scoped into this function
12070 (let* ((repeat (org-get-repeat))
12071 (aa (assoc org-last-state org-todo-kwd-alist))
12072 (interpret (nth 1 aa))
12074 (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
12075 (msg "Entry repeats: ")
12077 (org-todo-log-states nil)
12078 re type n what ts time to-state)
12080 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
12081 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
12082 org-todo-repeat-to-state))
12083 (unless (and to-state (member to-state org-todo-keywords-1))
12084 (setq to-state (if (eq interpret 'type) org-last-state head)))
12085 (org-todo to-state)
12086 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
12087 (org-entry-put nil "LAST_REPEAT" (format-time-string
12088 (org-time-stamp-format t t))))
12089 (when org-log-repeat
12090 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
12091 (memq 'org-add-log-note post-command-hook))
12092 ;; OK, we are already setup for some record
12093 (if (eq org-log-repeat 'note)
12094 ;; make sure we take a note, not only a time stamp
12095 (setq org-log-note-how 'note))
12096 ;; Set up for taking a record
12097 (org-add-log-setup 'state (or done-word (car org-done-keywords))
12099 'findpos org-log-repeat)))
12100 (org-back-to-heading t)
12101 (org-add-planning-info nil nil 'closed)
12102 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12103 org-deadline-time-regexp "\\)\\|\\("
12104 org-ts-regexp "\\)"))
12105 (while (re-search-forward
12106 re (save-excursion (outline-next-heading) (point)) t)
12107 (setq type (if (match-end 1) org-scheduled-string
12108 (if (match-end 3) org-deadline-string "Plain:"))
12109 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
12110 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
12111 (setq n (string-to-number (match-string 2 ts))
12112 what (match-string 3 ts))
12113 (if (equal what "w") (setq n (* n 7) what "d"))
12114 (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
12115 (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
12116 ;; Preparation, see if we need to modify the start date for the change
12117 (when (match-end 1)
12118 (setq time (save-match-data (org-time-string-to-time ts)))
12120 ((equal (match-string 1 ts) ".")
12121 ;; Shift starting date to today
12122 (org-timestamp-change
12123 (- (org-today) (time-to-days time))
12125 ((equal (match-string 1 ts) "+")
12126 (let ((nshiftmax 10) (nshift 0))
12127 (while (or (= nshift 0)
12128 (<= (time-to-days time)
12129 (time-to-days (current-time))))
12130 (when (= (incf nshift) nshiftmax)
12131 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
12133 (org-timestamp-change n (cdr (assoc what whata)))
12134 (org-at-timestamp-p t)
12135 (setq ts (match-string 1))
12136 (setq time (save-match-data (org-time-string-to-time ts)))))
12137 (org-timestamp-change (- n) (cdr (assoc what whata)))
12138 ;; rematch, so that we have everything in place for the real shift
12139 (org-at-timestamp-p t)
12140 (setq ts (match-string 1))
12141 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
12142 (org-timestamp-change n (cdr (assoc what whata)))
12143 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
12144 (setq org-log-post-message msg)
12145 (message "%s" msg))))
12147 (defun org-show-todo-tree (arg)
12148 "Make a compact tree which shows all headlines marked with TODO.
12149 The tree will show the lines where the regexp matches, and all higher
12150 headlines above the match.
12151 With a \\[universal-argument] prefix, prompt for a regexp to match.
12152 With a numeric prefix N, construct a sparse tree for the Nth element
12153 of `org-todo-keywords-1'."
12155 (let ((case-fold-search nil)
12157 (cond ((null arg) org-not-done-regexp)
12159 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
12160 (mapcar 'list org-todo-keywords-1))))
12162 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12164 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12165 (regexp-quote (nth (1- (prefix-numeric-value arg))
12166 org-todo-keywords-1)))
12167 (t (error "Invalid prefix argument: %s" arg)))))
12168 (message "%d TODO entries found"
12169 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
12171 (defun org-deadline (&optional remove time)
12172 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
12173 With argument REMOVE, remove any deadline from the item.
12174 With argument TIME, set the deadline at the corresponding date. TIME
12175 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12177 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12178 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12179 'region-start-level 'region))
12180 org-loop-over-headlines-in-active-region)
12182 `(org-deadline ',remove ,time)
12183 org-loop-over-headlines-in-active-region
12184 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12185 (let* ((old-date (org-entry-get nil "DEADLINE"))
12186 (repeater (and old-date
12188 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12190 (match-string 1 old-date))))
12193 (when (and old-date org-log-redeadline)
12194 (org-add-log-setup 'deldeadline nil old-date 'findpos
12195 org-log-redeadline))
12196 (org-remove-timestamp-with-keyword org-deadline-string)
12197 (message "Item no longer has a deadline."))
12198 (org-add-planning-info 'deadline time 'closed)
12199 (when (and old-date org-log-redeadline
12200 (not (equal old-date
12201 (substring org-last-inserted-timestamp 1 -1))))
12202 (org-add-log-setup 'redeadline nil old-date 'findpos
12203 org-log-redeadline))
12206 (org-back-to-heading t)
12207 (when (re-search-forward (concat org-deadline-string " "
12208 org-last-inserted-timestamp)
12210 (outline-next-heading) (point)) t)
12211 (goto-char (1- (match-end 0)))
12212 (insert " " repeater)
12213 (setq org-last-inserted-timestamp
12214 (concat (substring org-last-inserted-timestamp 0 -1)
12216 (substring org-last-inserted-timestamp -1))))))
12217 (message "Deadline on %s" org-last-inserted-timestamp)))))
12219 (defun org-schedule (&optional remove time)
12220 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
12221 With argument REMOVE, remove any scheduling date from the item.
12222 With argument TIME, scheduled at the corresponding date. TIME can
12223 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12225 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12226 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12227 'region-start-level 'region))
12228 org-loop-over-headlines-in-active-region)
12230 `(org-schedule ',remove ,time)
12231 org-loop-over-headlines-in-active-region
12232 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12233 (let* ((old-date (org-entry-get nil "SCHEDULED"))
12234 (repeater (and old-date
12236 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12238 (match-string 1 old-date))))
12241 (when (and old-date org-log-reschedule)
12242 (org-add-log-setup 'delschedule nil old-date 'findpos
12243 org-log-reschedule))
12244 (org-remove-timestamp-with-keyword org-scheduled-string)
12245 (message "Item is no longer scheduled."))
12246 (org-add-planning-info 'scheduled time 'closed)
12247 (when (and old-date org-log-reschedule
12248 (not (equal old-date
12249 (substring org-last-inserted-timestamp 1 -1))))
12250 (org-add-log-setup 'reschedule nil old-date 'findpos
12251 org-log-reschedule))
12254 (org-back-to-heading t)
12255 (when (re-search-forward (concat org-scheduled-string " "
12256 org-last-inserted-timestamp)
12258 (outline-next-heading) (point)) t)
12259 (goto-char (1- (match-end 0)))
12260 (insert " " repeater)
12261 (setq org-last-inserted-timestamp
12262 (concat (substring org-last-inserted-timestamp 0 -1)
12264 (substring org-last-inserted-timestamp -1))))))
12265 (message "Scheduled to %s" org-last-inserted-timestamp)))))
12267 (defun org-get-scheduled-time (pom &optional inherit)
12268 "Get the scheduled time as a time tuple, of a format suitable
12269 for calling org-schedule with, or if there is no scheduling,
12271 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
12273 (apply 'encode-time (org-parse-time-string time)))))
12275 (defun org-get-deadline-time (pom &optional inherit)
12276 "Get the deadline as a time tuple, of a format suitable for
12277 calling org-deadline with, or if there is no scheduling, returns
12279 (let ((time (org-entry-get pom "DEADLINE" inherit)))
12281 (apply 'encode-time (org-parse-time-string time)))))
12283 (defun org-remove-timestamp-with-keyword (keyword)
12284 "Remove all time stamps with KEYWORD in the current entry."
12285 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
12288 (org-back-to-heading t)
12290 (outline-next-heading)
12291 (while (re-search-backward re beg t)
12293 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
12294 (equal (char-before) ?\ ))
12295 (backward-delete-char 1)
12296 (if (string-match "^[ \t]*$" (buffer-substring
12297 (point-at-bol) (point-at-eol)))
12298 (delete-region (point-at-bol)
12299 (min (point-max) (1+ (point-at-eol))))))))))
12301 (defun org-add-planning-info (what &optional time &rest remove)
12302 "Insert new timestamp with keyword in the line directly after the headline.
12303 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
12304 If non is given, the user is prompted for a date.
12305 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
12308 (let (org-time-was-given org-end-time-was-given ts
12309 end default-time default-input)
12312 (when (and (memq what '(scheduled deadline))
12314 (and (stringp time)
12315 (string-match "^[-+]+[0-9]" time))))
12316 ;; Try to get a default date/time from existing timestamp
12318 (org-back-to-heading t)
12319 (setq end (save-excursion (outline-next-heading) (point)))
12320 (when (re-search-forward (if (eq what 'scheduled)
12321 org-scheduled-time-regexp
12322 org-deadline-time-regexp)
12324 (setq ts (match-string 1)
12326 (apply 'encode-time (org-parse-time-string ts))
12327 default-input (and ts (org-get-compact-tod ts))))))
12331 ;; This is a string (relative or absolute), set proper date
12332 (apply 'encode-time
12333 (org-read-date-analyze
12334 time default-time (decode-time default-time)))
12335 ;; If necessary, get the time from the user
12336 (or time (org-read-date nil 'to-time nil nil
12337 default-time default-input)))))
12339 (when (and org-insert-labeled-timestamps-at-point
12340 (member what '(scheduled deadline)))
12342 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
12343 (org-insert-time-stamp time org-time-was-given
12344 nil nil nil (list org-end-time-was-given))
12348 (let (col list elt ts buffer-invisibility-spec)
12349 (org-back-to-heading t)
12350 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
12351 (goto-char (match-end 1))
12352 (setq col (current-column))
12353 (goto-char (match-end 0))
12354 (if (eobp) (insert "\n") (forward-char 1))
12355 (when (and (not what)
12358 org-keyword-time-not-clock-regexp))))
12359 ;; Nothing to add, nothing to remove...... :-)
12361 (if (and (not (looking-at org-outline-regexp))
12362 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
12364 (not (equal (match-string 1) org-clock-string)))
12365 (narrow-to-region (match-beginning 0) (match-end 0))
12366 (insert-before-markers "\n")
12368 (narrow-to-region (point) (point))
12369 (and org-adapt-indentation (org-indent-to-column col)))
12370 ;; Check if we have to remove something.
12371 (setq list (cons what remove))
12373 (setq elt (pop list))
12374 (when (or (and (eq elt 'scheduled)
12375 (re-search-forward org-scheduled-time-regexp nil t))
12376 (and (eq elt 'deadline)
12377 (re-search-forward org-deadline-time-regexp nil t))
12378 (and (eq elt 'closed)
12379 (re-search-forward org-closed-time-regexp nil t)))
12381 (if (looking-at "--+<[^>]+>") (replace-match ""))))
12382 (and (looking-at "[ \t]+") (replace-match ""))
12383 (and org-adapt-indentation (bolp) (org-indent-to-column col))
12386 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
12387 (cond ((eq what 'scheduled) org-scheduled-string)
12388 ((eq what 'deadline) org-deadline-string)
12389 ((eq what 'closed) org-closed-string))
12391 (setq ts (org-insert-time-stamp
12393 (or org-time-was-given
12394 (and (eq what 'closed) org-log-done-with-time))
12396 nil nil (list org-end-time-was-given)))
12398 (if (not (or (bolp) (eq (char-before) ?\ )
12399 (memq (char-after) '(32 10))
12402 (goto-char (point-min))
12404 (if (and (looking-at "[ \t]*\n")
12405 (equal (char-before) ?\n))
12406 (delete-region (1- (point)) (point-at-eol)))
12409 (defvar org-log-note-marker (make-marker))
12410 (defvar org-log-note-purpose nil)
12411 (defvar org-log-note-state nil)
12412 (defvar org-log-note-previous-state nil)
12413 (defvar org-log-note-how nil)
12414 (defvar org-log-note-extra nil)
12415 (defvar org-log-note-window-configuration nil)
12416 (defvar org-log-note-return-to (make-marker))
12417 (defvar org-log-note-effective-time nil
12418 "Remembered current time so that dynamically scoped
12419 `org-extend-today-until' affects tha timestamps in state change
12422 (defvar org-log-post-message nil
12423 "Message to be displayed after a log note has been stored.
12424 The auto-repeater uses this.")
12426 (defun org-add-note ()
12427 "Add a note to the current entry.
12428 This is done in the same way as adding a state change note."
12430 (org-add-log-setup 'note nil nil 'findpos nil))
12432 (defvar org-property-end-re)
12433 (defun org-add-log-setup (&optional purpose state prev-state
12435 "Set up the post command hook to take a note.
12436 If this is about to TODO state change, the new state is expected in STATE.
12437 When FINDPOS is non-nil, find the correct position for the note in
12438 the current entry. If not, assume that it can be inserted at point.
12439 HOW is an indicator what kind of note should be created.
12440 EXTRA is additional text that will be inserted into the notes buffer."
12441 (let* ((org-log-into-drawer (org-log-into-drawer))
12442 (drawer (cond ((stringp org-log-into-drawer)
12443 org-log-into-drawer)
12444 (org-log-into-drawer "LOGBOOK")
12449 (org-back-to-heading t)
12450 (narrow-to-region (point) (save-excursion
12451 (outline-next-heading) (point)))
12452 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
12453 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
12455 (goto-char (match-end 0))
12458 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
12461 (goto-char (match-end 0))
12462 (or org-log-states-order-reversed
12463 (and (re-search-forward org-property-end-re nil t)
12464 (goto-char (1- (match-beginning 0))))))
12465 (insert "\n:" drawer ":\n:END:")
12466 (beginning-of-line 0)
12468 (beginning-of-line 2)
12471 ((and org-log-state-notes-insert-after-drawers
12473 (forward-line) (looking-at org-drawer-regexp)))
12475 (while (looking-at org-drawer-regexp)
12476 (goto-char (match-end 0))
12477 (re-search-forward org-property-end-re (point-max) t)
12479 (forward-line -1)))
12480 (unless org-log-states-order-reversed
12481 (and (= (char-after) ?\n) (forward-char 1))
12482 (org-skip-over-state-notes)
12483 (skip-chars-backward " \t\n\r")))
12484 (move-marker org-log-note-marker (point))
12485 (setq org-log-note-purpose purpose
12486 org-log-note-state state
12487 org-log-note-previous-state prev-state
12488 org-log-note-how how
12489 org-log-note-extra extra
12490 org-log-note-effective-time (org-current-effective-time))
12491 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
12493 (defun org-skip-over-state-notes ()
12494 "Skip past the list of State notes in an entry."
12495 (if (looking-at "\n[ \t]*- State") (forward-char 1))
12496 (when (ignore-errors (goto-char (org-in-item-p)))
12497 (let* ((struct (org-list-struct))
12498 (prevs (org-list-prevs-alist struct)))
12499 (while (looking-at "[ \t]*- State")
12500 (goto-char (or (org-list-get-next-item (point) struct prevs)
12501 (org-list-get-item-end (point) struct)))))))
12503 (defun org-add-log-note (&optional purpose)
12504 "Pop up a window for taking a note, and add this note later at point."
12505 (remove-hook 'post-command-hook 'org-add-log-note)
12506 (setq org-log-note-window-configuration (current-window-configuration))
12507 (delete-other-windows)
12508 (move-marker org-log-note-return-to (point))
12509 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
12510 (goto-char org-log-note-marker)
12511 (org-switch-to-buffer-other-window "*Org Note*")
12513 (if (memq org-log-note-how '(time state))
12514 (let (current-prefix-arg) (org-store-log-note))
12515 (let ((org-inhibit-startup t)) (org-mode))
12516 (insert (format "# Insert note for %s.
12517 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
12519 ((eq org-log-note-purpose 'clock-out) "stopped clock")
12520 ((eq org-log-note-purpose 'done) "closed todo item")
12521 ((eq org-log-note-purpose 'state)
12522 (format "state change from \"%s\" to \"%s\""
12523 (or org-log-note-previous-state "")
12524 (or org-log-note-state "")))
12525 ((eq org-log-note-purpose 'reschedule)
12527 ((eq org-log-note-purpose 'delschedule)
12528 "no longer scheduled")
12529 ((eq org-log-note-purpose 'redeadline)
12530 "changing deadline")
12531 ((eq org-log-note-purpose 'deldeadline)
12532 "removing deadline")
12533 ((eq org-log-note-purpose 'refile)
12535 ((eq org-log-note-purpose 'note)
12537 (t (error "This should not happen")))))
12538 (if org-log-note-extra (insert org-log-note-extra))
12539 (org-set-local 'org-finish-function 'org-store-log-note)
12540 (run-hooks 'org-log-buffer-setup-hook)))
12542 (defvar org-note-abort nil) ; dynamically scoped
12543 (defun org-store-log-note ()
12544 "Finish taking a log note, and insert it to where it belongs."
12545 (let ((txt (buffer-string))
12546 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
12548 (kill-buffer (current-buffer))
12549 (while (string-match "\\`#.*\n[ \t\n]*" txt)
12550 (setq txt (replace-match "" t t txt)))
12551 (if (string-match "\\s-+\\'" txt)
12552 (setq txt (replace-match "" t t txt)))
12553 (setq lines (org-split-string txt "\n"))
12554 (when (and note (string-match "\\S-" note))
12556 (org-replace-escapes
12558 (list (cons "%u" (user-login-name))
12559 (cons "%U" user-full-name)
12560 (cons "%t" (format-time-string
12561 (org-time-stamp-format 'long 'inactive)
12562 org-log-note-effective-time))
12563 (cons "%T" (format-time-string
12564 (org-time-stamp-format 'long nil)
12565 org-log-note-effective-time))
12566 (cons "%d" (format-time-string
12567 (org-time-stamp-format nil 'inactive)
12568 org-log-note-effective-time))
12569 (cons "%D" (format-time-string
12570 (org-time-stamp-format nil nil)
12571 org-log-note-effective-time))
12572 (cons "%s" (if org-log-note-state
12573 (concat "\"" org-log-note-state "\"")
12575 (cons "%S" (if org-log-note-previous-state
12576 (concat "\"" org-log-note-previous-state "\"")
12578 (if lines (setq note (concat note " \\\\")))
12580 (when (or current-prefix-arg org-note-abort)
12581 (when org-log-into-drawer
12582 (org-remove-empty-drawer-at
12583 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
12584 org-log-note-marker))
12587 (with-current-buffer (marker-buffer org-log-note-marker)
12589 (goto-char org-log-note-marker)
12590 (move-marker org-log-note-marker nil)
12592 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
12593 (setq ind (save-excursion
12594 (if (ignore-errors (goto-char (org-in-item-p)))
12595 (let ((struct (org-list-struct)))
12597 (org-list-get-top-point struct) struct))
12598 (skip-chars-backward " \r\t\n")
12600 ((and (org-at-heading-p)
12601 org-adapt-indentation)
12602 (1+ (org-current-level)))
12603 ((org-at-heading-p) 0)
12604 (t (org-get-indentation))))))
12605 (setq bul (org-list-bullet-string "-"))
12606 (org-indent-line-to ind)
12607 (insert bul (pop lines))
12608 (let ((ind-body (+ (length bul) ind)))
12611 (org-indent-line-to ind-body)
12612 (insert (pop lines))))
12613 (message "Note stored")
12614 (org-back-to-heading t)
12615 (org-cycle-hide-drawers 'children)))))
12616 (set-window-configuration org-log-note-window-configuration)
12617 (with-current-buffer (marker-buffer org-log-note-return-to)
12618 (goto-char org-log-note-return-to))
12619 (move-marker org-log-note-return-to nil)
12620 (and org-log-post-message (message "%s" org-log-post-message)))
12622 (defun org-remove-empty-drawer-at (drawer pos)
12623 "Remove an empty drawer DRAWER at position POS.
12624 POS may also be a marker."
12625 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
12631 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
12632 (replace-match ""))))))
12634 (defvar org-ts-type nil)
12635 (defun org-sparse-tree (&optional arg type)
12636 "Create a sparse tree, prompt for the details.
12637 This command can create sparse trees. You first need to select the type
12638 of match used to create the tree:
12640 t Show all TODO entries.
12641 T Show entries with a specific TODO keyword.
12642 m Show entries selected by a tags/property match.
12643 p Enter a property name and its value (both with completion on existing
12644 names/values) and show entries with that property.
12645 r Show entries matching a regular expression (`/' can be used as well).
12646 b Show deadlines and scheduled items before a date.
12647 a Show deadlines and scheduled items after a date.
12648 d Show deadlines due within `org-deadline-warning-days'.
12649 D Show deadlines and scheduled items between a date range."
12651 (let (ans kwd value ts-type)
12652 (setq type (or type org-sparse-tree-default-date-type))
12653 (setq org-ts-type type)
12654 (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"
12655 (cond ((eq type 'all) "all timestamps")
12656 ((eq type 'scheduled) "only scheduled")
12657 ((eq type 'deadline) "only deadline")
12658 ((eq type 'active) "only active timestamps")
12659 ((eq type 'inactive) "only inactive timestamps")
12660 ((eq type 'scheduled-or-deadline) "scheduled/deadline")
12661 (t "scheduled/deadline")))
12662 (setq ans (read-char-exclusive))
12665 (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
12667 (call-interactively 'org-check-deadlines))
12669 (call-interactively 'org-check-before-date))
12671 (call-interactively 'org-check-after-date))
12673 (call-interactively 'org-check-dates-range))
12675 (org-show-todo-tree nil))
12677 (org-show-todo-tree '(4)))
12678 ((member ans '(?T ?m))
12679 (call-interactively 'org-match-sparse-tree))
12680 ((member ans '(?p ?P))
12681 (setq kwd (org-icompleting-read "Property: "
12682 (mapcar 'list (org-buffer-property-keys))))
12683 (setq value (org-icompleting-read "Value: "
12684 (mapcar 'list (org-property-values kwd))))
12685 (unless (string-match "\\`{.*}\\'" value)
12686 (setq value (concat "\"" value "\"")))
12687 (org-match-sparse-tree arg (concat kwd "=" value)))
12688 ((member ans '(?r ?R ?/))
12689 (call-interactively 'org-occur))
12690 (t (error "No such sparse tree command \"%c\"" ans)))))
12692 (defvar org-occur-highlights nil
12693 "List of overlays used for occur matches.")
12694 (make-variable-buffer-local 'org-occur-highlights)
12695 (defvar org-occur-parameters nil
12696 "Parameters of the active org-occur calls.
12697 This is a list, each call to org-occur pushes as cons cell,
12698 containing the regular expression and the callback, onto the list.
12699 The list can contain several entries if `org-occur' has been called
12700 several time with the KEEP-PREVIOUS argument. Otherwise, this list
12701 will only contain one set of parameters. When the highlights are
12702 removed (for example with `C-c C-c', or with the next edit (depending
12703 on `org-remove-highlights-with-change'), this variable is emptied
12705 (make-variable-buffer-local 'org-occur-parameters)
12707 (defun org-occur (regexp &optional keep-previous callback)
12708 "Make a compact tree which shows all matches of REGEXP.
12709 The tree will show the lines where the regexp matches, and all higher
12710 headlines above the match. It will also show the heading after the match,
12711 to make sure editing the matching entry is easy.
12712 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
12713 call to `org-occur' will be kept, to allow stacking of calls to this
12715 If CALLBACK is non-nil, it is a function which is called to confirm
12716 that the match should indeed be shown."
12717 (interactive "sRegexp: \nP")
12718 (when (equal regexp "")
12719 (error "Regexp cannot be empty"))
12720 (unless keep-previous
12721 (org-remove-occur-highlights nil nil t))
12722 (push (cons regexp callback) org-occur-parameters)
12725 (goto-char (point-min))
12726 (if (or (not keep-previous) ; do not want to keep
12727 (not org-occur-highlights)) ; no previous matches
12730 (while (re-search-forward regexp nil t)
12731 (when (or (not callback)
12732 (save-match-data (funcall callback)))
12733 (setq cnt (1+ cnt))
12734 (when org-highlight-sparse-tree-matches
12735 (org-highlight-new-match (match-beginning 0) (match-end 0)))
12736 (org-show-context 'occur-tree))))
12737 (when org-remove-highlights-with-change
12738 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
12740 (unless org-sparse-tree-open-archived-trees
12741 (org-hide-archived-subtrees (point-min) (point-max)))
12742 (run-hooks 'org-occur-hook)
12743 (if (org-called-interactively-p 'interactive)
12744 (message "%d match(es) for regexp %s" cnt regexp))
12747 (defun org-occur-next-match (&optional n reset)
12748 "Function for `next-error-function' to find sparse tree matches.
12749 N is the number of matches to move, when negative move backwards.
12750 RESET is entirely ignored - this function always goes back to the
12751 starting point when no match is found."
12752 (let* ((limit (if (< n 0) (point-min) (point-max)))
12753 (search-func (if (< n 0)
12754 'previous-single-char-property-change
12755 'next-single-char-property-change))
12760 (while (setq p1 (funcall search-func (point) 'org-type))
12761 (when (equal p1 limit)
12763 (error "No more matches"))
12764 (when (equal (get-char-property p1 'org-type) 'org-occur)
12768 (throw 'exit (point))))
12771 (error "No more matches"))))
12773 (defun org-show-context (&optional key)
12774 "Make sure point and context are visible.
12775 How much context is shown depends upon the variables
12776 `org-show-hierarchy-above', `org-show-following-heading',
12777 `org-show-entry-below' and `org-show-siblings'."
12778 (let ((heading-p (org-at-heading-p t))
12779 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
12780 (following-p (org-get-alist-option org-show-following-heading key))
12781 (entry-p (org-get-alist-option org-show-entry-below key))
12782 (siblings-p (org-get-alist-option org-show-siblings key)))
12784 ;; Show heading or entry text
12785 (if (and heading-p (not entry-p))
12786 (org-flag-heading nil) ; only show the heading
12787 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
12788 (org-show-hidden-entry))) ; show entire entry
12790 ;; Show next sibling, or heading below text
12792 (and (if heading-p (org-goto-sibling) (outline-next-heading))
12793 (org-flag-heading nil))))
12794 (when siblings-p (org-show-siblings))
12796 ;; show all higher headings, possibly with siblings
12798 (while (and (condition-case nil
12799 (progn (org-up-heading-all 1) t)
12802 (org-flag-heading nil)
12803 (when siblings-p (org-show-siblings))))))))
12805 (defvar org-reveal-start-hook nil
12806 "Hook run before revealing a location.")
12808 (defun org-reveal (&optional siblings)
12809 "Show current entry, hierarchy above it, and the following headline.
12810 This can be used to show a consistent set of context around locations
12811 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
12812 not t for the search context.
12814 With optional argument SIBLINGS, on each level of the hierarchy all
12815 siblings are shown. This repairs the tree structure to what it would
12816 look like when opened with hierarchical calls to `org-cycle'.
12817 With double optional argument \\[universal-argument] \\[universal-argument], \
12818 go to the parent and show the
12821 (run-hooks 'org-reveal-start-hook)
12822 (let ((org-show-hierarchy-above t)
12823 (org-show-following-heading t)
12824 (org-show-siblings (if siblings t org-show-siblings)))
12825 (org-show-context nil))
12826 (when (equal siblings '(16))
12828 (when (org-up-heading-safe)
12830 (run-hook-with-args 'org-cycle-hook 'subtree)))))
12832 (defun org-highlight-new-match (beg end)
12833 "Highlight from BEG to END and mark the highlight is an occur headline."
12834 (let ((ov (make-overlay beg end)))
12835 (overlay-put ov 'face 'secondary-selection)
12836 (overlay-put ov 'org-type 'org-occur)
12837 (push ov org-occur-highlights)))
12839 (defun org-remove-occur-highlights (&optional beg end noremove)
12840 "Remove the occur highlights from the buffer.
12841 BEG and END are ignored. If NOREMOVE is nil, remove this function
12842 from the `before-change-functions' in the current buffer."
12844 (unless org-inhibit-highlight-removal
12845 (mapc 'delete-overlay org-occur-highlights)
12846 (setq org-occur-highlights nil)
12847 (setq org-occur-parameters nil)
12849 (remove-hook 'before-change-functions
12850 'org-remove-occur-highlights 'local))))
12854 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
12855 "Regular expression matching the priority indicator.")
12857 (defvar org-remove-priority-next-time nil)
12859 (defun org-priority-up ()
12860 "Increase the priority of the current item."
12862 (org-priority 'up))
12864 (defun org-priority-down ()
12865 "Decrease the priority of the current item."
12867 (org-priority 'down))
12869 (defun org-priority (&optional action)
12870 "Change the priority of an item by ARG.
12871 ACTION can be `set', `up', `down', or a character."
12873 (unless org-enable-priority-commands
12874 (error "Priority commands are disabled"))
12875 (setq action (or action 'set))
12876 (let (current new news have remove)
12878 (org-back-to-heading t)
12879 (if (looking-at org-priority-regexp)
12880 (setq current (string-to-char (match-string 2))
12883 ((eq action 'remove)
12884 (setq remove t new ?\ ))
12885 ((or (eq action 'set)
12886 (if (featurep 'xemacs) (characterp action) (integerp action)))
12887 (if (not (eq action 'set))
12889 (message "Priority %c-%c, SPC to remove: "
12890 org-highest-priority org-lowest-priority)
12892 (setq new (read-char-exclusive))))
12893 (if (and (= (upcase org-highest-priority) org-highest-priority)
12894 (= (upcase org-lowest-priority) org-lowest-priority))
12895 (setq new (upcase new)))
12896 (cond ((equal new ?\ ) (setq remove t))
12897 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
12898 (error "Priority must be between `%c' and `%c'"
12899 org-highest-priority org-lowest-priority))))
12902 (1- current) ; normal cycling
12903 ;; last priority was empty
12904 (if (eq last-command this-command)
12905 org-lowest-priority ; wrap around empty to lowest
12907 (if org-priority-start-cycle-with-default
12908 org-default-priority
12909 (1- org-default-priority))))))
12912 (1+ current) ; normal cycling
12913 ;; last priority was empty
12914 (if (eq last-command this-command)
12915 org-highest-priority ; wrap around empty to highest
12917 (if org-priority-start-cycle-with-default
12918 org-default-priority
12919 (1+ org-default-priority))))))
12920 (t (error "Invalid action")))
12921 (if (or (< (upcase new) org-highest-priority)
12922 (> (upcase new) org-lowest-priority))
12923 (if (and (memq action '(up down))
12924 (not have) (not (eq last-command this-command)))
12925 ;; `new' is from default priority
12927 "The default can not be set, see `org-default-priority' why")
12928 ;; normal cycling: `new' is beyond highest/lowest priority
12929 ;; and is wrapped around to the empty priority
12931 (setq news (format "%c" new))
12934 (replace-match "" t t nil 1)
12935 (replace-match news t t nil 2))
12937 (error "No priority cookie found in line")
12938 (let ((case-fold-search nil))
12939 (looking-at org-todo-line-regexp))
12942 (goto-char (match-end 2))
12943 (insert " [#" news "]"))
12944 (goto-char (match-beginning 3))
12945 (insert "[#" news "] "))))
12946 (org-preserve-lc (org-set-tags nil 'align)))
12948 (message "Priority removed")
12949 (message "Priority of current item set to %s" news))))
12951 (defun org-get-priority (s)
12952 "Find priority cookie and return priority."
12953 (if (functionp org-get-priority-function)
12954 (funcall org-get-priority-function)
12956 (if (not (string-match org-priority-regexp s))
12957 (* 1000 (- org-lowest-priority org-default-priority))
12958 (* 1000 (- org-lowest-priority
12959 (string-to-char (match-string 2 s))))))))
12963 (defvar org-agenda-archives-mode)
12964 (defvar org-map-continue-from nil
12965 "Position from where mapping should continue.
12966 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
12968 (defvar org-scanner-tags nil
12969 "The current tag list while the tags scanner is running.")
12970 (defvar org-trust-scanner-tags nil
12971 "Should `org-get-tags-at' use the tags for the scanner.
12972 This is for internal dynamical scoping only.
12973 When this is non-nil, the function `org-get-tags-at' will return the value
12974 of `org-scanner-tags' instead of building the list by itself. This
12975 can lead to large speed-ups when the tags scanner is used in a file with
12976 many entries, and when the list of tags is retrieved, for example to
12977 obtain a list of properties. Building the tags list for each entry in such
12978 a file becomes an N^2 operation - but with this variable set, it scales
12981 (defun org-scan-tags (action matcher todo-only &optional start-level)
12982 "Scan headline tags with inheritance and produce output ACTION.
12984 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
12985 or `agenda' to produce an entry list for an agenda view. It can also be
12986 a Lisp form or a function that should be called at each matched headline, in
12987 this case the return value is a list of all return values from these calls.
12989 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
12990 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
12991 only lines with a not-done TODO keyword are included in the output.
12992 This should be the same variable that was scoped into
12993 and set by `org-make-tags-matcher' when it constructed MATCHER.
12995 START-LEVEL can be a string with asterisks, reducing the scope to
12996 headlines matching this string."
12997 (require 'org-agenda)
12998 (let* ((re (concat "^"
13000 ;; Get the correct level to match
13001 (concat "\\*\\{" (number-to-string start-level) "\\} ")
13002 org-outline-regexp)
13004 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
13005 (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
13006 (props (list 'face 'default
13007 'done-face 'org-agenda-done
13008 'undone-face 'default
13009 'mouse-face 'highlight
13010 'org-not-done-regexp org-not-done-regexp
13011 'org-todo-regexp org-todo-regexp
13012 'org-complex-heading-regexp org-complex-heading-regexp
13014 (format "mouse-2 or RET jump to org file %s"
13015 (abbreviate-file-name
13016 (or (buffer-file-name (buffer-base-buffer))
13017 (buffer-name (buffer-base-buffer)))))))
13018 (case-fold-search nil)
13019 (org-map-continue-from nil)
13020 lspos tags tags-list
13021 (tags-alist (list (cons 0 org-file-tags)))
13022 (llast 0) rtn rtn1 level category i txt
13023 todo marker entry priority)
13024 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
13025 (setq action (list 'lambda nil action)))
13027 (goto-char (point-min))
13028 (when (eq action 'sparse-tree)
13030 (org-remove-occur-highlights))
13031 (while (re-search-forward re nil t)
13032 (setq org-map-continue-from nil)
13034 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
13035 tags (if (match-end 4) (org-match-string-no-properties 4)))
13036 (goto-char (setq lspos (match-beginning 0)))
13037 (setq level (org-reduced-level (funcall outline-level))
13038 category (org-get-category))
13039 (setq i llast llast level)
13040 ;; remove tag lists from same and sublevels
13041 (while (>= i level)
13042 (when (setq entry (assoc i tags-alist))
13043 (setq tags-alist (delete entry tags-alist)))
13045 ;; add the next tags
13047 (setq tags (org-split-string tags ":")
13049 (cons (cons level tags) tags-alist)))
13050 ;; compile tags for current headline
13052 (if org-use-tag-inheritance
13053 (apply 'append (mapcar 'cdr (reverse tags-alist)))
13055 org-scanner-tags tags-list)
13056 (when org-use-tag-inheritance
13057 (setcdr (car tags-alist)
13058 (mapcar (lambda (x)
13059 (setq x (copy-sequence x))
13060 (org-add-prop-inherited x))
13061 (cdar tags-alist))))
13062 (when (and tags org-use-tag-inheritance
13063 (or (not (eq t org-use-tag-inheritance))
13064 org-tags-exclude-from-inheritance))
13065 ;; selective inheritance, remove uninherited ones
13066 (setcdr (car tags-alist)
13067 (org-remove-uninherited-tags (cdar tags-alist))))
13070 ;; eval matcher only when the todo condition is OK
13071 (and (or (not todo-only) (member todo org-not-done-keywords))
13072 (let ((case-fold-search t) (org-trust-scanner-tags t))
13075 ;; Call the skipper, but return t if it does not skip,
13076 ;; so that the `and' form continues evaluating
13078 (unless (eq action 'sparse-tree) (org-agenda-skip))
13081 ;; Check if timestamps are deselecting this entry
13082 (or (not todo-only)
13083 (and (member todo org-not-done-keywords)
13084 (or (not org-agenda-tags-todo-honor-ignore-options)
13085 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
13087 ;; Extra check for the archive tag
13088 ;; FIXME: Does the skipper already do this????
13090 (not (member org-archive-tag tags-list))
13091 ;; we have an archive tag, should we use this anyway?
13092 (or (not org-agenda-skip-archived-trees)
13093 (and (eq action 'agenda) org-agenda-archives-mode))))
13095 ;; select this headline
13098 ((eq action 'sparse-tree)
13099 (and org-highlight-sparse-tree-matches
13100 (org-get-heading) (match-end 0)
13101 (org-highlight-new-match
13102 (match-beginning 1) (match-end 1)))
13103 (org-show-context 'tags-tree))
13104 ((eq action 'agenda)
13105 (setq txt (org-agenda-format-item
13108 (if (eq org-tags-match-list-sublevels 'indented)
13109 (make-string (1- level) ?.) "")
13113 priority (org-get-priority txt))
13115 (setq marker (org-agenda-new-marker))
13116 (org-add-props txt props
13117 'org-marker marker 'org-hd-marker marker 'org-category category
13119 'priority priority 'type "tagsmatch")
13121 ((functionp action)
13122 (setq org-map-continue-from nil)
13124 (setq rtn1 (funcall action))
13126 (t (error "Invalid action")))
13128 ;; if we are to skip sublevels, jump to end of subtree
13129 (unless org-tags-match-list-sublevels
13130 (org-end-of-subtree t)
13131 (backward-char 1))))
13132 ;; Get the correct position from where to continue
13133 (if org-map-continue-from
13134 (goto-char org-map-continue-from)
13135 (and (= (point) lspos) (end-of-line 1)))))
13136 (when (and (eq action 'sparse-tree)
13137 (not org-sparse-tree-open-archived-trees))
13138 (org-hide-archived-subtrees (point-min) (point-max)))
13141 (defun org-remove-uninherited-tags (tags)
13142 "Remove all tags that are not inherited from the list TAGS."
13144 ((eq org-use-tag-inheritance t)
13145 (if org-tags-exclude-from-inheritance
13146 (org-delete-all org-tags-exclude-from-inheritance tags)
13148 ((not org-use-tag-inheritance) nil)
13149 ((stringp org-use-tag-inheritance)
13152 (if (and (string-match org-use-tag-inheritance x)
13153 (not (member x org-tags-exclude-from-inheritance)))
13156 ((listp org-use-tag-inheritance)
13159 (if (member x org-use-tag-inheritance) x nil))
13162 (defun org-match-sparse-tree (&optional todo-only match)
13163 "Create a sparse tree according to tags string MATCH.
13164 MATCH can contain positive and negative selection of tags, like
13165 \"+WORK+URGENT-WITHBOSS\".
13166 If optional argument TODO-ONLY is non-nil, only select lines that are
13169 (org-prepare-agenda-buffers (list (current-buffer)))
13170 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
13172 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
13174 (defvar org-cached-props nil)
13175 (defun org-cached-entry-get (pom property)
13176 (if (or (eq t org-use-property-inheritance)
13177 (and (stringp org-use-property-inheritance)
13178 (string-match org-use-property-inheritance property))
13179 (and (listp org-use-property-inheritance)
13180 (member property org-use-property-inheritance)))
13181 ;; Caching is not possible, check it directly
13182 (org-entry-get pom property 'inherit)
13183 ;; Get all properties, so that we can do complicated checks easily
13184 (cdr (assoc property (or org-cached-props
13185 (setq org-cached-props
13186 (org-entry-properties pom)))))))
13188 (defun org-global-tags-completion-table (&optional files)
13189 "Return the list of all tags in all agenda buffer/files.
13190 Optional FILES argument is a list of files to which can be used
13191 instead of the agenda files."
13198 (set-buffer (find-file-noselect file))
13199 (append (org-get-buffer-tags)
13200 (mapcar (lambda (x) (if (stringp (car-safe x))
13201 (list (car-safe x)) nil))
13203 (if (and files (car files))
13205 (org-agenda-files))))))))
13207 (defun org-make-tags-matcher (match)
13208 "Create the TAGS/TODO matcher form for the selection string MATCH.
13210 The variable `todo-only' is scoped dynamically into this function; it will be
13211 set to t if the matcher restricts matching to TODO entries,
13212 otherwise will not be touched.
13214 Returns a cons of the selection string MATCH and the constructed
13215 lisp form implementing the matcher. The matcher is to be
13216 evaluated at an Org entry, with point on the headline,
13217 and returns t if the entry matches the
13218 selection string MATCH. The returned lisp form references
13219 two variables with information about the entry, which must be
13220 bound around the form's evaluation: todo, the TODO keyword at the
13221 entry (or nil of none); and tags-list, the list of all tags at the
13222 entry including inherited ones. Additionally, the category
13223 of the entry (if any) must be specified as the text property
13224 'org-category on the headline.
13226 See also `org-scan-tags'.
13228 (declare (special todo-only))
13229 (unless (boundp 'todo-only)
13230 (error "org-make-tags-matcher expects todo-only to be scoped in"))
13232 ;; Get a new match request, with completion
13233 (let ((org-last-tags-completion-table
13234 (org-global-tags-completion-table)))
13235 (setq match (org-completing-read-no-i
13236 "Match: " 'org-tags-completion-function nil nil nil
13237 'org-tags-history))))
13239 ;; Parse the string and create a lisp form
13240 (let ((match0 match)
13241 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
13243 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
13244 orterms term orlist re-p str-p level-p level-op time-p
13245 prop-p pn pv po gv rest)
13246 (if (string-match "/+" match)
13247 ;; match contains also a todo-matching request
13249 (setq tagsmatch (substring match 0 (match-beginning 0))
13250 todomatch (substring match (match-end 0)))
13251 (if (string-match "^!" todomatch)
13252 (setq todo-only t todomatch (substring todomatch 1)))
13253 (if (string-match "^\\s-*$" todomatch)
13254 (setq todomatch nil)))
13255 ;; only matching tags
13256 (setq tagsmatch match todomatch nil))
13258 ;; Make the tags matcher
13259 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
13260 (setq tagsmatcher t)
13261 (setq orterms (org-split-string tagsmatch "|") orlist nil)
13262 (while (setq term (pop orterms))
13263 (while (and (equal (substring term -1) "\\") orterms)
13264 (setq term (concat term "|" (pop orterms)))) ; repair bad split
13265 (while (string-match re term)
13266 (setq rest (substring term (match-end 0))
13267 minus (and (match-end 1)
13268 (equal (match-string 1 term) "-"))
13269 tag (save-match-data (replace-regexp-in-string
13271 (match-string 2 term)))
13272 re-p (equal (string-to-char tag) ?{)
13273 level-p (match-end 4)
13274 prop-p (match-end 5)
13276 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
13278 (setq level-op (org-op-to-function (match-string 3 term)))
13279 `(,level-op level ,(string-to-number
13280 (match-string 4 term))))
13282 (setq pn (match-string 5 term)
13283 po (match-string 6 term)
13284 pv (match-string 7 term)
13285 re-p (equal (string-to-char pv) ?{)
13286 str-p (equal (string-to-char pv) ?\")
13287 time-p (save-match-data
13288 (string-match "^\"[[<].*[]>]\"$" pv))
13289 pv (if (or re-p str-p) (substring pv 1 -1) pv))
13290 (if time-p (setq pv (org-matcher-time pv)))
13291 (setq po (org-op-to-function po (if time-p 'time str-p)))
13293 ((equal pn "CATEGORY")
13294 (setq gv '(get-text-property (point) 'org-category)))
13298 (setq gv `(org-cached-entry-get nil ,pn))))
13301 `(not (string-match ,pv (or ,gv "")))
13302 `(string-match ,pv (or ,gv "")))
13304 `(,po (or ,gv "") ,pv)
13305 `(,po (string-to-number (or ,gv ""))
13306 ,(string-to-number pv) ))))
13307 (t `(member ,tag tags-list)))
13308 mm (if minus (list 'not mm) mm)
13310 (push mm tagsmatcher))
13311 (push (if (> (length tagsmatcher) 1)
13312 (cons 'and tagsmatcher)
13315 (setq tagsmatcher nil))
13316 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
13318 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
13319 ;; Make the todo matcher
13320 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
13321 (setq todomatcher t)
13322 (setq orterms (org-split-string todomatch "|") orlist nil)
13323 (while (setq term (pop orterms))
13324 (while (string-match re term)
13325 (setq minus (and (match-end 1)
13326 (equal (match-string 1 term) "-"))
13327 kwd (match-string 2 term)
13328 re-p (equal (string-to-char kwd) ?{)
13329 term (substring term (match-end 0))
13331 `(string-match ,(substring kwd 1 -1) todo)
13332 (list 'equal 'todo kwd))
13333 mm (if minus (list 'not mm) mm))
13334 (push mm todomatcher))
13335 (push (if (> (length todomatcher) 1)
13336 (cons 'and todomatcher)
13339 (setq todomatcher nil))
13340 (setq todomatcher (if (> (length orlist) 1)
13341 (cons 'or orlist) (car orlist))))
13343 ;; Return the string and lisp forms of the matcher
13344 (setq matcher (if todomatcher
13345 (list 'and tagsmatcher todomatcher)
13348 (setq matcher (list 'and '(member todo org-not-done-keywords)
13350 (cons match0 matcher)))
13352 (defun org-op-to-function (op &optional stringp)
13353 "Turn an operator into the appropriate function."
13356 ((equal op "<" ) '(< string< org-time<))
13357 ((equal op ">" ) '(> org-string> org-time>))
13358 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
13359 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
13360 ((member op '("=" "==")) '(= string= org-time=))
13361 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
13362 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
13364 (defun org<> (a b) (not (= a b)))
13365 (defun org-string<= (a b) (or (string= a b) (string< a b)))
13366 (defun org-string>= (a b) (not (string< a b)))
13367 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
13368 (defun org-string<> (a b) (not (string= a b)))
13369 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
13370 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
13371 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
13372 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
13373 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
13374 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
13376 "Convert S to a floating point time.
13377 If S is already a number, just return it. If it is a string, parse
13378 it as a time string and apply `float-time' to it. If S is nil, just return 0."
13382 (condition-case nil
13383 (float-time (apply 'encode-time (org-parse-time-string s)))
13387 (defun org-time-today ()
13388 "Time in seconds today at 0:00.
13389 Returns the float number of seconds since the beginning of the
13390 epoch to the beginning of today (00:00)."
13391 (float-time (apply 'encode-time
13392 (append '(0 0 0) (nthcdr 3 (decode-time))))))
13394 (defun org-matcher-time (s)
13395 "Interpret a time comparison value."
13398 ((string= s "<now>") (float-time))
13399 ((string= s "<today>") (org-time-today))
13400 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
13401 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
13402 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
13403 (+ (org-time-today)
13404 (* (string-to-number (match-string 1 s))
13405 (cdr (assoc (match-string 2 s)
13406 '(("d" . 86400.0) ("w" . 604800.0)
13407 ("m" . 2678400.0) ("y" . 31557600.0)))))))
13410 (defun org-match-any-p (re list)
13411 "Does re match any element of list?"
13412 (setq list (mapcar (lambda (x) (string-match re x)) list))
13415 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
13416 (defvar org-tags-overlay (make-overlay 1 1))
13417 (org-detach-overlay org-tags-overlay)
13419 (defun org-get-local-tags-at (&optional pos)
13420 "Get a list of tags defined in the current headline."
13421 (org-get-tags-at pos 'local))
13423 (defun org-get-local-tags ()
13424 "Get a list of tags defined in the current headline."
13425 (org-get-tags-at nil 'local))
13427 (defun org-get-tags-at (&optional pos local)
13428 "Get a list of all headline tags applicable at POS.
13429 POS defaults to point. If tags are inherited, the list contains
13430 the targets in the same sequence as the headlines appear, i.e.
13431 the tags of the current headline come last.
13432 When LOCAL is non-nil, only return tags from the current headline,
13433 ignore inherited ones."
13435 (if (and org-trust-scanner-tags
13436 (or (not pos) (equal pos (point)))
13439 (let (tags ltags lastpos parent)
13443 (goto-char (or pos (point)))
13446 (condition-case nil
13448 (org-back-to-heading t)
13449 (while (not (equal lastpos (point)))
13450 (setq lastpos (point))
13452 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
13453 (setq ltags (org-split-string
13454 (org-match-string-no-properties 1) ":"))
13456 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
13459 (org-remove-uninherited-tags ltags)
13462 (or org-use-tag-inheritance (throw 'done t))
13463 (if local (throw 'done t))
13464 (or (org-up-heading-safe) (error nil))
13469 (append (org-remove-uninherited-tags org-file-tags) tags))))))
13471 (defun org-add-prop-inherited (s)
13472 (add-text-properties 0 (length s) '(inherited t) s)
13475 (defun org-toggle-tag (tag &optional onoff)
13476 "Toggle the tag TAG for the current line.
13477 If ONOFF is `on' or `off', don't toggle but set to this state."
13480 (org-back-to-heading t)
13481 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
13484 (setq current (match-string 1))
13485 (replace-match ""))
13487 (setq current (nreverse (org-split-string current ":")))
13491 (or (member tag current) (push tag current)))
13493 (or (not (member tag current)) (setq current (delete tag current))))
13494 (t (if (member tag current)
13495 (setq current (delete tag current))
13497 (push tag current))))
13501 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
13502 (org-set-tags nil t))
13503 (delete-horizontal-space))
13504 (run-hooks 'org-after-tags-change-hook))
13507 (defun org-align-tags-here (to-col)
13508 ;; Assumes that this is a headline
13509 (let ((pos (point)) (col (current-column)) ncol tags-l p)
13510 (beginning-of-line 1)
13511 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13512 (< pos (match-beginning 2)))
13514 (setq tags-l (- (match-end 2) (match-beginning 2)))
13515 (goto-char (match-beginning 1))
13517 (delete-region (point) (1+ (match-beginning 2)))
13518 (setq ncol (max (current-column)
13522 (- (abs to-col) tags-l))))
13524 (insert (make-string (- ncol (current-column)) ?\ ))
13525 (setq ncol (current-column))
13526 (when indent-tabs-mode (tabify p (point-at-eol)))
13527 (org-move-to-column (min ncol col) t))
13530 (defun org-set-tags-command (&optional arg just-align)
13531 "Call the set-tags command for the current entry."
13533 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
13534 (org-set-tags arg just-align)
13536 (org-back-to-heading t)
13537 (org-set-tags arg just-align))))
13539 (defun org-set-tags-to (data)
13540 "Set the tags of the current entry to DATA, replacing the current tags.
13541 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
13542 If DATA is nil or the empty string, any tags will be removed."
13543 (interactive "sTags: ")
13547 ((equal data "") "")
13549 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
13552 (concat ":" (mapconcat 'identity data ":") ":"))
13556 (org-back-to-heading t)
13557 (when (looking-at org-complex-heading-regexp)
13560 (goto-char (match-beginning 5))
13562 (delete-region (point) (point-at-eol))
13563 (org-set-tags nil 'align))
13564 (goto-char (point-at-eol))
13566 (org-set-tags nil 'align)))
13567 (beginning-of-line 1)
13568 (if (looking-at ".*?\\([ \t]+\\)$")
13569 (delete-region (match-beginning 1) (match-end 1))))))
13571 (defun org-align-all-tags ()
13572 "Align the tags i all headings."
13575 (or (ignore-errors (org-back-to-heading t))
13576 (outline-next-heading))
13577 (if (org-at-heading-p)
13579 (message "No headings"))))
13581 (defvar org-indent-indentation-per-level)
13582 (defun org-set-tags (&optional arg just-align)
13583 "Set the tags for the current headline.
13584 With prefix ARG, realign all tags in headings in the current buffer."
13586 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13587 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
13588 'region-start-level 'region))
13589 org-loop-over-headlines-in-active-region)
13591 ;; We don't use ARG and JUST-ALIGN here these args are not
13592 ;; useful when looping over headlines
13594 org-loop-over-headlines-in-active-region
13595 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
13596 (let* ((re org-outline-regexp-bol)
13597 (current (unless arg (org-get-tags-string)))
13598 (col (current-column))
13599 (org-setting-tags t)
13600 table current-tags inherited-tags ; computed below when needed
13601 tags p0 c0 c1 rpl di tc level)
13604 (goto-char (point-min))
13605 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
13606 (while (re-search-forward re nil t)
13607 (org-set-tags nil t)
13609 (message "All tags realigned to column %d" org-tags-column))
13611 (setq tags current)
13612 ;; Get a new set of tags from the user
13614 (setq table (append org-tag-persistent-alist
13615 (or org-tag-alist (org-get-buffer-tags))
13617 org-complete-tags-always-offer-all-agenda-tags
13618 (org-global-tags-completion-table
13619 (org-agenda-files))))
13620 org-last-tags-completion-table table
13621 current-tags (org-split-string current ":")
13622 inherited-tags (nreverse
13623 (nthcdr (length current-tags)
13624 (nreverse (org-get-tags-at))))
13626 (if (or (eq t org-use-fast-tag-selection)
13627 (and org-use-fast-tag-selection
13628 (delq nil (mapcar 'cdr table))))
13629 (org-fast-tag-selection
13630 current-tags inherited-tags table
13631 (if org-fast-tag-selection-include-todo
13632 org-todo-key-alist))
13633 (let ((org-add-colon-after-tag-completion (< 1 (length table))))
13635 (org-icompleting-read "Tags: "
13636 'org-tags-completion-function
13637 nil nil current 'org-tags-history))))))
13638 (while (string-match "[-+&]+" tags)
13639 ;; No boolean logic, just a list
13640 (setq tags (replace-match ":" t t tags))))
13642 (setq tags (replace-regexp-in-string "[,]" ":" tags))
13644 (if org-tags-sort-function
13645 (setq tags (mapconcat 'identity
13646 (sort (org-split-string
13647 tags (org-re "[^[:alnum:]_@#%]+"))
13648 org-tags-sort-function) ":")))
13650 (if (string-match "\\`[\t ]*\\'" tags)
13652 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
13653 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
13655 ;; Insert new tags at the correct column
13656 (beginning-of-line 1)
13657 (setq level (or (and (looking-at org-outline-regexp)
13658 (- (match-end 0) (point) 1))
13661 ((and (equal current "") (equal tags "")))
13662 ((re-search-forward
13663 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
13665 (if (equal tags "")
13667 (goto-char (match-beginning 0))
13668 (setq c0 (current-column)
13669 ;; compute offset for the case of org-indent-mode active
13670 di (if org-indent-mode
13671 (* (1- org-indent-indentation-per-level) (1- level))
13673 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
13674 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
13675 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
13676 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
13677 (replace-match rpl t t)
13678 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
13680 (t (error "Tags alignment failed")))
13681 (org-move-to-column col)
13683 (run-hooks 'org-after-tags-change-hook))))))
13685 (defun org-change-tag-in-region (beg end tag off)
13686 "Add or remove TAG for each entry in the region.
13687 This works in the agenda, and also in an org-mode buffer."
13689 (list (region-beginning) (region-end)
13690 (let ((org-last-tags-completion-table
13691 (if (derived-mode-p 'org-mode)
13692 (org-get-buffer-tags)
13693 (org-global-tags-completion-table))))
13694 (org-icompleting-read
13695 "Tag: " 'org-tags-completion-function nil nil nil
13696 'org-tags-history))
13698 (message "[s]et or
[r]emove? ")
13699 (equal (read-char-exclusive) ?r))))
13700 (if (fboundp 'deactivate-mark) (deactivate-mark))
13701 (let ((agendap (equal major-mode 'org-agenda-mode))
13702 l1 l2 m buf pos newhead (cnt 0))
13704 (setq l2 (1- (org-current-line)))
13706 (setq l1 (org-current-line))
13707 (loop for l from l1 to l2 do
13709 (setq m (get-text-property (point) 'org-hd-marker))
13710 (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
13712 (setq buf (if agendap (marker-buffer m) (current-buffer))
13713 pos (if agendap m (point)))
13714 (with-current-buffer buf
13718 (setq cnt (1+ cnt))
13719 (org-toggle-tag tag (if off 'off 'on))
13720 (setq newhead (org-get-heading)))))
13721 (and agendap (org-agenda-change-all-lines newhead m))))
13722 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
13724 (defun org-tags-completion-function (string predicate &optional flag)
13725 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
13726 (confirm (lambda (x) (stringp (car x)))))
13727 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
13728 (setq s1 (match-string 1 string)
13729 s2 (match-string 2 string))
13730 (setq s1 "" s2 string))
13734 (setq rtn (try-completion s2 ctable confirm))
13737 (concat s1 s2 (substring rtn (length s2))
13738 (if (and org-add-colon-after-tag-completion
13739 (assoc rtn ctable))
13744 (all-completions s2 ctable confirm)
13748 (assoc s2 ctable)))
13751 (defun org-fast-tag-insert (kwd tags face &optional end)
13752 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
13753 (insert (format "%-12s" (concat kwd ":"))
13754 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
13757 (defun org-fast-tag-show-exit (flag)
13760 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
13761 (replace-match ""))
13764 (org-move-to-column (- (window-width) 19) t)
13765 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
13767 (defun org-set-current-tags-overlay (current prefix)
13768 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
13769 (if (featurep 'xemacs)
13770 (org-overlay-display org-tags-overlay (concat prefix s)
13771 'secondary-selection)
13772 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
13773 (org-overlay-display org-tags-overlay (concat prefix s)))))
13775 (defvar org-last-tag-selection-key nil)
13776 (defun org-fast-tag-selection (current inherited table &optional todo-table)
13777 "Fast tag selection with single keys.
13778 CURRENT is the current list of tags in the headline, INHERITED is the
13779 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
13780 possibly with grouping information. TODO-TABLE is a similar table with
13781 TODO keywords, should these have keys assigned to them.
13782 If the keys are nil, a-z are automatically assigned.
13783 Returns the new tags string, or nil to not change the current settings."
13784 (let* ((fulltable (append table todo-table))
13785 (maxlen (apply 'max (mapcar
13787 (if (stringp (car x)) (string-width (car x)) 0))
13789 (buf (current-buffer))
13790 (expert (eq org-fast-tag-selection-single-key 'expert))
13792 (fwidth (+ maxlen 3 1 3))
13793 (ncol (/ (- (window-width) 4) fwidth))
13796 tg cnt e c char c1 c2 ntable tbl rtn
13797 ov-start ov-end ov-prefix
13798 (exit-after-next org-fast-tag-selection-single-key)
13799 (done-keywords org-done-keywords)
13802 (beginning-of-line 1)
13804 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13805 (setq ov-start (match-beginning 1)
13806 ov-end (match-end 1)
13808 (setq ov-start (1- (point-at-eol))
13809 ov-end (1+ ov-start))
13810 (skip-chars-forward "^\n\r")
13813 (buffer-substring (1- (point)) (point))
13814 (if (> (current-column) org-tags-column)
13816 (make-string (- org-tags-column (current-column)) ?\ ))))))
13817 (move-overlay org-tags-overlay ov-start ov-end)
13818 (save-window-excursion
13820 (set-buffer (get-buffer-create " *Org tags*"))
13821 (delete-other-windows)
13822 (split-window-vertically)
13823 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
13825 (org-set-local 'org-done-keywords done-keywords)
13826 (org-fast-tag-insert "Inherited" inherited i-face "\n")
13827 (org-fast-tag-insert "Current" current c-face "\n\n")
13828 (org-fast-tag-show-exit exit-after-next)
13829 (org-set-current-tags-overlay current ov-prefix)
13830 (setq tbl fulltable char ?a cnt 0)
13831 (while (setq e (pop tbl))
13833 ((equal (car e) :startgroup)
13834 (push '() groups) (setq ingroup t)
13835 (when (not (= cnt 0))
13838 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
13839 ((equal (car e) :endgroup)
13840 (setq ingroup nil cnt 0)
13841 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
13842 ((equal e '(:newline))
13843 (when (not (= cnt 0))
13847 (while (equal (car tbl) '(:newline))
13849 (setq tbl (cdr tbl)))))
13851 (setq tg (copy-sequence (car e)) c2 nil)
13854 ;; automatically assign a character.
13855 (setq c1 (string-to-char
13856 (downcase (substring
13857 tg (if (= (string-to-char tg) ?@) 1 0)))))
13858 (if (or (rassoc c1 ntable) (rassoc c1 table))
13859 (while (or (rassoc char ntable) (rassoc char table))
13860 (setq char (1+ char)))
13862 (setq c (or c2 char)))
13863 (if ingroup (push tg (car groups)))
13864 (setq tg (org-add-props tg nil 'face
13866 ((not (assoc tg table))
13867 (org-get-todo-face tg))
13868 ((member tg current) c-face)
13869 ((member tg inherited) i-face)
13871 (if (and (= cnt 0) (not ingroup)) (insert " "))
13872 (insert "[" c "] " tg (make-string
13873 (- fwidth 4 (length tg)) ?\ ))
13874 (push (cons tg c) ntable)
13875 (when (= (setq cnt (1+ cnt)) ncol)
13877 (if ingroup (insert " "))
13879 (setq ntable (nreverse ntable))
13881 (goto-char (point-min))
13882 (if (not expert) (org-fit-window-to-buffer))
13886 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
13887 (if (not groups) "no " "")
13888 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
13889 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13890 (setq org-last-tag-selection-key c)
13892 ((= c ?\r) (throw 'exit t))
13894 (setq groups (not groups))
13895 (goto-char (point-min))
13896 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
13899 (org-fast-tag-show-exit
13900 (setq exit-after-next (not exit-after-next)))
13902 (delete-other-windows)
13903 (set-window-buffer (split-window-vertically) " *Org tags*")
13904 (org-switch-to-buffer-other-window " *Org tags*")
13905 (org-fit-window-to-buffer)))
13907 (and (= c ?q) (not (rassoc c ntable))))
13908 (org-detach-overlay org-tags-overlay)
13909 (setq quit-flag t))
13912 (if exit-after-next (setq exit-after-next 'now)))
13914 (condition-case nil
13915 (setq tg (org-icompleting-read
13918 (with-current-buffer buf
13919 (org-get-buffer-tags)))))
13920 (quit (setq tg "")))
13921 (when (string-match "\\S-" tg)
13922 (add-to-list 'buffer-tags (list tg))
13923 (if (member tg current)
13924 (setq current (delete tg current))
13925 (push tg current)))
13926 (if exit-after-next (setq exit-after-next 'now)))
13927 ((setq e (rassoc c todo-table) tg (car e))
13928 (with-current-buffer buf
13929 (save-excursion (org-todo tg)))
13930 (if exit-after-next (setq exit-after-next 'now)))
13931 ((setq e (rassoc c ntable) tg (car e))
13932 (if (member tg current)
13933 (setq current (delete tg current))
13934 (loop for g in groups do
13937 (setq current (delete x current)))
13940 (if exit-after-next (setq exit-after-next 'now))))
13942 ;; Create a sorted list
13946 (assoc b (cdr (memq (assoc a ntable) ntable))))))
13947 (if (eq exit-after-next 'now) (throw 'exit t))
13948 (goto-char (point-min))
13949 (beginning-of-line 2)
13950 (delete-region (point) (point-at-eol))
13951 (org-fast-tag-insert "Current" current c-face)
13952 (org-set-current-tags-overlay current ov-prefix)
13953 (while (re-search-forward
13954 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
13955 (setq tg (match-string 1))
13956 (add-text-properties
13957 (match-beginning 1) (match-end 1)
13960 ((member tg current) c-face)
13961 ((member tg inherited) i-face)
13962 (t (get-text-property (match-beginning 1) 'face))))))
13963 (goto-char (point-min)))))
13964 (org-detach-overlay org-tags-overlay)
13966 (mapconcat 'identity current ":")
13969 (defun org-get-tags-string ()
13970 "Get the TAGS string in the current headline."
13971 (unless (org-at-heading-p t)
13972 (error "Not on a heading"))
13974 (beginning-of-line 1)
13975 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13976 (org-match-string-no-properties 1)
13979 (defun org-get-tags ()
13980 "Get the list of tags specified in the current headline."
13981 (org-split-string (org-get-tags-string) ":"))
13983 (defun org-get-buffer-tags ()
13984 "Get a table of all tags used in the buffer, for completion."
13987 (goto-char (point-min))
13988 (while (re-search-forward
13989 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
13990 (when (equal (char-after (point-at-bol 0)) ?*)
13991 (mapc (lambda (x) (add-to-list 'tags x))
13992 (org-split-string (org-match-string-no-properties 1) ":")))))
13993 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
13994 (mapcar 'list tags)))
13996 ;;;; The mapping API
13999 (defun org-map-entries (func &optional match scope &rest skip)
14000 "Call FUNC at each headline selected by MATCH in SCOPE.
14002 FUNC is a function or a lisp form. The function will be called without
14003 arguments, with the cursor positioned at the beginning of the headline.
14004 The return values of all calls to the function will be collected and
14005 returned as a list.
14007 The call to FUNC will be wrapped into a save-excursion form, so FUNC
14008 does not need to preserve point. After evaluation, the cursor will be
14009 moved to the end of the line (presumably of the headline of the
14010 processed entry) and search continues from there. Under some
14011 circumstances, this may not produce the wanted results. For example,
14012 if you have removed (e.g. archived) the current (sub)tree it could
14013 mean that the next entry will be skipped entirely. In such cases, you
14014 can specify the position from where search should continue by making
14015 FUNC set the variable `org-map-continue-from' to the desired buffer
14018 MATCH is a tags/property/todo match as it is used in the agenda tags view.
14019 Only headlines that are matched by this query will be considered during
14020 the iteration. When MATCH is nil or t, all headlines will be
14021 visited by the iteration.
14023 SCOPE determines the scope of this command. It can be any of:
14025 nil The current buffer, respecting the restriction if any
14026 tree The subtree started with the entry at point
14027 region The entries within the active region, if any
14029 The entries within the active region, but only those at
14030 the same level than the first one.
14031 file The current buffer, without restriction
14033 The current buffer, and any archives associated with it
14034 agenda All agenda files
14035 agenda-with-archives
14036 All agenda files with any archive files associated with them
14038 If this is a list, all files in the list will be scanned
14040 The remaining args are treated as settings for the skipping facilities of
14041 the scanner. The following items can be given here:
14043 archive skip trees with the archive tag.
14044 comment skip trees with the COMMENT keyword
14045 function or Emacs Lisp form:
14046 will be used as value for `org-agenda-skip-function', so whenever
14047 the function returns t, FUNC will not be called for that
14048 entry and search will continue from the point where the
14049 function leaves it.
14051 If your function needs to retrieve the tags including inherited tags
14052 at the *current* entry, you can use the value of the variable
14053 `org-scanner-tags' which will be much faster than getting the value
14054 with `org-get-tags-at'. If your function gets properties with
14055 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
14056 to t around the call to `org-entry-properties' to get the same speedup.
14057 Note that if your function moves around to retrieve tags and properties at
14058 a *different* entry, you cannot use these techniques."
14059 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
14060 (not (org-region-active-p)))
14061 (let* ((org-agenda-archives-mode nil) ; just to make sure
14062 (org-agenda-skip-archived-trees (memq 'archive skip))
14063 (org-agenda-skip-comment-trees (memq 'comment skip))
14064 (org-agenda-skip-function
14065 (car (org-delete-all '(comment archive) skip)))
14066 (org-tags-match-list-sublevels t)
14067 (start-level (eq scope 'region-start-level))
14069 org-todo-keywords-for-agenda
14070 org-done-keywords-for-agenda
14071 org-todo-keyword-alist-for-agenda
14072 org-drawers-for-agenda
14073 org-tag-alist-for-agenda
14077 ((eq match t) (setq matcher t))
14078 ((eq match nil) (setq matcher t))
14079 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
14083 (cond ((eq scope 'tree)
14084 (org-back-to-heading t)
14085 (org-narrow-to-subtree)
14087 ((and (or (eq scope 'region) (eq scope 'region-start-level))
14088 (org-region-active-p))
14089 ;; If needed, set start-level to a string like "2"
14092 (goto-char (region-beginning))
14093 (unless (org-at-heading-p) (outline-next-heading))
14094 (setq start-level (org-current-level))))
14095 (narrow-to-region (region-beginning)
14097 (goto-char (region-end))
14098 (unless (and (bolp) (org-at-heading-p))
14099 (outline-next-heading))
14105 (org-prepare-agenda-buffers
14106 (list (buffer-file-name (current-buffer))))
14107 (setq res (org-scan-tags func matcher todo-only start-level)))
14108 ;; Get the right scope
14110 ((and scope (listp scope) (symbolp (car scope)))
14111 (setq scope (eval scope)))
14112 ((eq scope 'agenda)
14113 (setq scope (org-agenda-files t)))
14114 ((eq scope 'agenda-with-archives)
14115 (setq scope (org-agenda-files t))
14116 (setq scope (org-add-archive-files scope)))
14118 (setq scope (list (buffer-file-name))))
14119 ((eq scope 'file-with-archives)
14120 (setq scope (org-add-archive-files (list (buffer-file-name))))))
14121 (org-prepare-agenda-buffers scope)
14122 (while (setq file (pop scope))
14123 (with-current-buffer (org-find-base-buffer-visiting file)
14127 (goto-char (point-min))
14128 (setq res (append res (org-scan-tags func matcher todo-only))))))))))
14133 ;;; Setting and retrieving properties
14135 (defconst org-special-properties
14136 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
14137 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
14138 "The special properties valid in Org-mode.
14140 These are properties that are not defined in the property drawer,
14141 but in some other way.")
14143 (defconst org-default-properties
14144 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
14145 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
14146 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
14147 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
14148 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
14149 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
14150 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
14151 "Some properties that are used by Org-mode for various purposes.
14152 Being in this list makes sure that they are offered for completion.")
14154 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
14155 "Regular expression matching the first line of a property drawer.")
14157 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
14158 "Regular expression matching the last line of a property drawer.")
14160 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
14161 "Regular expression matching the first line of a property drawer.")
14163 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
14164 "Regular expression matching the first line of a property drawer.")
14166 (defconst org-property-drawer-re
14167 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
14168 org-property-end-re "\\)\n?")
14169 "Matches an entire property drawer.")
14171 (defconst org-clock-drawer-re
14172 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
14173 org-property-end-re "\\)\n?")
14174 "Matches an entire clock drawer.")
14176 (defsubst org-re-property (property)
14177 "Return a regexp matching a PROPERTY line.
14178 Match group 1 will be set to the value."
14179 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
14181 (defsubst org-re-property-keyword (property)
14182 "Return a regexp matching a PROPERTY line, possibly with no
14183 value for the property."
14184 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
14186 (defun org-property-action ()
14187 "Do an action on properties."
14190 (org-at-property-p)
14191 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
14192 (setq c (read-char-exclusive))
14195 (call-interactively 'org-set-property))
14197 (call-interactively 'org-delete-property))
14199 (call-interactively 'org-delete-property-globally))
14201 (call-interactively 'org-compute-property-at-point))
14202 (t (error "No such property action %c" c)))))
14204 (defun org-inc-effort ()
14205 "Increment the value of the effort property in the current entry."
14207 (org-set-effort nil t))
14209 (defun org-set-effort (&optional value increment)
14210 "Set the effort property of the current entry.
14211 With numerical prefix arg, use the nth allowed value, 0 stands for the
14212 10th allowed value.
14214 When INCREMENT is non-nil, set the property to the next allowed value."
14216 (if (equal value 0) (setq value 10))
14217 (let* ((completion-ignore-case t)
14218 (prop org-effort-property)
14219 (cur (org-entry-get nil prop))
14220 (allowed (org-property-get-allowed-values nil prop 'table))
14221 (existing (mapcar 'list (org-property-values prop)))
14224 ((stringp value) value)
14225 ((and allowed (integerp value))
14226 (or (car (nth (1- value) allowed))
14227 (car (org-last allowed))))
14228 ((and allowed increment)
14229 (or (caadr (member (list cur) allowed))
14230 (error "Allowed effort values are not set")))
14232 (message "Select 1-9,0, [RET%s]: %s"
14233 (if cur (concat "=" cur) "")
14234 (mapconcat 'car allowed " "))
14235 (setq rpl (read-char-exclusive))
14236 (if (equal rpl ?\r)
14238 (setq rpl (- rpl ?0))
14239 (if (equal rpl 0) (setq rpl 10))
14240 (if (and (> rpl 0) (<= rpl (length allowed)))
14241 (car (nth (1- rpl) allowed))
14242 (org-completing-read "Effort: " allowed nil))))
14244 (let (org-completion-use-ido org-completion-use-iswitchb)
14245 (org-completing-read
14246 (concat "Effort " (if (and cur (string-match "\\S-" cur))
14247 (concat "[" cur "]") "")
14249 existing nil nil "" nil cur))))))
14250 (unless (equal (org-entry-get nil prop) val)
14251 (org-entry-put nil prop val))
14252 (message "%s is now %s" prop val)))
14254 (defun org-at-property-p ()
14255 "Is cursor inside a property drawer?"
14257 (beginning-of-line 1)
14258 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
14259 (save-match-data ;; Used by calling procedures
14261 (range (unless (org-before-first-heading-p)
14262 (org-get-property-block))))
14263 (and range (<= (car range) p) (< p (cdr range))))))))
14265 (defun org-get-property-block (&optional beg end force)
14266 "Return the (beg . end) range of the body of the property drawer.
14267 BEG and END can be beginning and end of subtree, if not given
14268 they will be found.
14269 If the drawer does not exist and FORCE is non-nil, create the drawer."
14272 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
14273 (end (or end (progn (outline-next-heading) (point)))))
14275 (if (re-search-forward org-property-start-re end t)
14276 (setq beg (1+ (match-end 0)))
14279 (org-insert-property-drawer)
14280 (setq end (progn (outline-next-heading) (point))))
14283 (if (re-search-forward org-property-start-re end t)
14284 (setq beg (1+ (match-end 0)))))
14285 (if (re-search-forward org-property-end-re end t)
14286 (setq end (match-beginning 0))
14287 (or force (throw 'exit nil))
14291 (insert ":END:\n"))
14294 (defun org-entry-properties (&optional pom which specific)
14295 "Get all properties of the entry at point-or-marker POM.
14296 This includes the TODO keyword, the tags, time strings for deadline,
14297 scheduled, and clocking, and any additional properties defined in the
14298 entry. The return value is an alist, keys may occur multiple times
14299 if the property key was used several times.
14300 POM may also be nil, in which case the current entry is used.
14301 If WHICH is nil or `all', get all properties. If WHICH is
14302 `special' or `standard', only get that subclass. If WHICH
14303 is a string only get exactly this property. SPECIFIC can be a string, the
14304 specific property we are interested in. Specifying it can speed
14305 things up because then unnecessary parsing is avoided."
14306 (setq which (or which 'all))
14307 (org-with-point-at pom
14308 (let ((clockstr (substring org-clock-string 0 -1))
14309 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
14310 (case-fold-search nil)
14311 beg end range props sum-props key key1 value string clocksum)
14313 (when (condition-case nil
14314 (and (derived-mode-p 'org-mode) (org-back-to-heading t))
14317 (setq sum-props (get-text-property (point) 'org-summaries))
14318 (setq clocksum (get-text-property (point) :org-clock-minutes))
14319 (outline-next-heading)
14321 (when (memq which '(all special))
14322 ;; Get the special properties, like TODO and tags
14324 (when (and (or (not specific) (string= specific "TODO"))
14325 (looking-at org-todo-line-regexp) (match-end 2))
14326 (push (cons "TODO" (org-match-string-no-properties 2)) props))
14327 (when (and (or (not specific) (string= specific "PRIORITY"))
14328 (looking-at org-priority-regexp))
14329 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
14330 (when (or (not specific) (string= specific "FILE"))
14331 (push (cons "FILE" buffer-file-name) props))
14332 (when (and (or (not specific) (string= specific "TAGS"))
14333 (setq value (org-get-tags-string))
14334 (string-match "\\S-" value))
14335 (push (cons "TAGS" value) props))
14336 (when (and (or (not specific) (string= specific "ALLTAGS"))
14337 (setq value (org-get-tags-at)))
14338 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
14341 (when (or (not specific) (string= specific "BLOCKED"))
14342 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
14343 (when (or (not specific)
14345 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
14346 "TIMESTAMP" "TIMESTAMP_IA")))
14348 (while (re-search-forward org-maybe-keyword-time-regexp end t)
14349 (setq key (if (match-end 1)
14350 (substring (org-match-string-no-properties 1)
14352 string (if (equal key clockstr)
14356 (match-beginning 3) (goto-char
14358 (substring (org-match-string-no-properties 3)
14360 ;; Get the correct property name from the key. This is
14361 ;; necessary if the user has configured time keywords.
14362 (setq key1 (concat key ":"))
14366 (if (= (char-after (match-beginning 3)) ?\[)
14367 "TIMESTAMP_IA" "TIMESTAMP")))
14368 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
14369 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
14370 ((equal key1 org-closed-string) (setq key "CLOSED"))
14371 ((equal key1 org-clock-string) (setq key "CLOCK")))
14372 (if (and specific (equal key specific) (not (equal key "CLOCK")))
14374 (push (cons key string) props)
14375 ;; no need to search further if match is found
14377 (when (or (equal key "CLOCK") (not (assoc key props)))
14378 (push (cons key string) props))))))
14381 (when (memq which '(all standard))
14382 ;; Get the standard properties, like :PROP: ...
14383 (setq range (org-get-property-block beg end))
14385 (goto-char (car range))
14386 (while (re-search-forward
14387 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
14389 (setq key (org-match-string-no-properties 1)
14390 value (org-trim (or (org-match-string-no-properties 2) "")))
14391 (unless (member key excluded)
14392 (push (cons key (or value "")) props)))))
14394 (push (cons "CLOCKSUM"
14395 (org-columns-number-to-string (/ (float clocksum) 60.)
14398 (unless (assoc "CATEGORY" props)
14399 (push (cons "CATEGORY" (org-get-category)) props))
14400 (append sum-props (nreverse props)))))))
14402 (defun org-entry-get (pom property &optional inherit literal-nil)
14403 "Get value of PROPERTY for entry at point-or-marker POM.
14404 If INHERIT is non-nil and the entry does not have the property,
14405 then also check higher levels of the hierarchy.
14406 If INHERIT is the symbol `selective', use inheritance only if the setting
14407 in `org-use-property-inheritance' selects PROPERTY for inheritance.
14408 If the property is present but empty, the return value is the empty string.
14409 If the property is not present at all, nil is returned.
14411 If LITERAL-NIL is set, return the string value \"nil\" as a string,
14412 do not interpret it as the list atom nil. This is used for inheritance
14413 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
14414 (org-with-point-at pom
14415 (if (and inherit (if (eq inherit 'selective)
14416 (org-property-inherit-p property)
14418 (org-entry-get-with-inheritance property literal-nil)
14419 (if (member property org-special-properties)
14420 ;; We need a special property. Use `org-entry-properties' to
14421 ;; retrieve it, but specify the wanted property
14422 (cdr (assoc property (org-entry-properties nil 'special property)))
14423 (let ((range (unless (org-before-first-heading-p)
14424 (org-get-property-block)))
14425 (props (list (or (assoc property org-file-properties)
14426 (assoc property org-global-properties)
14427 (assoc property org-global-properties-fixed))))
14429 (org-flet ((ap (key)
14430 (when (re-search-forward
14431 (org-re-property key) (cdr range) t)
14433 (org-update-property-plist
14436 (org-match-string-no-properties 1) "")
14438 (when (and range (goto-char (car range)))
14440 (goto-char (car range))
14441 (while (ap (concat property "+")))
14442 (setq val (cdr (assoc property props)))
14443 (when val (if literal-nil val (org-not-nil val))))))))))
14445 (defun org-property-or-variable-value (var &optional inherit)
14446 "Check if there is a property fixing the value of VAR.
14447 If yes, return this value. If not, return the current value of the variable."
14448 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14449 (if (and prop (stringp prop) (string-match "\\S-" prop))
14451 (symbol-value var))))
14453 (defun org-entry-delete (pom property)
14454 "Delete the property PROPERTY from entry at point-or-marker POM."
14455 (org-with-point-at pom
14456 (if (member property org-special-properties)
14457 nil ; cannot delete these properties.
14458 (let ((range (org-get-property-block)))
14460 (goto-char (car range))
14462 (org-re-property property)
14465 (delete-region (match-beginning 0) (1+ (point-at-eol)))
14469 ;; Multi-values properties are properties that contain multiple values
14470 ;; These values are assumed to be single words, separated by whitespace.
14471 (defun org-entry-add-to-multivalued-property (pom property value)
14472 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
14473 (let* ((old (org-entry-get pom property))
14474 (values (and old (org-split-string old "[ \t]"))))
14475 (setq value (org-entry-protect-space value))
14476 (unless (member value values)
14477 (setq values (cons value values))
14478 (org-entry-put pom property
14479 (mapconcat 'identity values " ")))))
14481 (defun org-entry-remove-from-multivalued-property (pom property value)
14482 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
14483 (let* ((old (org-entry-get pom property))
14484 (values (and old (org-split-string old "[ \t]"))))
14485 (setq value (org-entry-protect-space value))
14486 (when (member value values)
14487 (setq values (delete value values))
14488 (org-entry-put pom property
14489 (mapconcat 'identity values " ")))))
14491 (defun org-entry-member-in-multivalued-property (pom property value)
14492 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
14493 (let* ((old (org-entry-get pom property))
14494 (values (and old (org-split-string old "[ \t]"))))
14495 (setq value (org-entry-protect-space value))
14496 (member value values)))
14498 (defun org-entry-get-multivalued-property (pom property)
14499 "Return a list of values in a multivalued property."
14500 (let* ((value (org-entry-get pom property))
14501 (values (and value (org-split-string value "[ \t]"))))
14502 (mapcar 'org-entry-restore-space values)))
14504 (defun org-entry-put-multivalued-property (pom property &rest values)
14505 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
14506 VALUES should be a list of strings. Spaces will be protected."
14507 (org-entry-put pom property
14508 (mapconcat 'org-entry-protect-space values " "))
14509 (let* ((value (org-entry-get pom property))
14510 (values (and value (org-split-string value "[ \t]"))))
14511 (mapcar 'org-entry-restore-space values)))
14513 (defun org-entry-protect-space (s)
14514 "Protect spaces and newline in string S."
14515 (while (string-match " " s)
14516 (setq s (replace-match "%20" t t s)))
14517 (while (string-match "\n" s)
14518 (setq s (replace-match "%0A" t t s)))
14521 (defun org-entry-restore-space (s)
14522 "Restore spaces and newline in string S."
14523 (while (string-match "%20" s)
14524 (setq s (replace-match " " t t s)))
14525 (while (string-match "%0A" s)
14526 (setq s (replace-match "\n" t t s)))
14529 (defvar org-entry-property-inherited-from (make-marker)
14530 "Marker pointing to the entry from where a property was inherited.
14531 Each call to `org-entry-get-with-inheritance' will set this marker to the
14532 location of the entry where the inheritance search matched. If there was
14533 no match, the marker will point nowhere.
14534 Note that also `org-entry-get' calls this function, if the INHERIT flag
14537 (defun org-entry-get-with-inheritance (property &optional literal-nil)
14538 "Get entry property, and search higher levels if not present.
14539 The search will stop at the first ancestor which has the property defined.
14540 If the value found is \"nil\", return nil to show that the property
14541 should be considered as undefined (this is the meaning of nil here).
14542 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
14543 (move-marker org-entry-property-inherited-from nil)
14545 (unless (org-before-first-heading-p)
14551 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
14552 (org-back-to-heading t)
14553 (move-marker org-entry-property-inherited-from (point))
14555 (or (org-up-heading-safe) (throw 'ex nil)))))))
14557 (cdr (assoc property org-file-properties))
14558 (cdr (assoc property org-global-properties))
14559 (cdr (assoc property org-global-properties-fixed))))
14560 (if literal-nil tmp (org-not-nil tmp))))
14562 (defvar org-property-changed-functions nil
14563 "Hook called when the value of a property has changed.
14564 Each hook function should accept two arguments, the name of the property
14565 and the new value.")
14567 (defun org-entry-put (pom property value)
14568 "Set PROPERTY to VALUE for entry at point-or-marker POM."
14569 (org-with-point-at pom
14570 (org-back-to-heading t)
14571 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
14574 ((equal property "TODO")
14575 (when (and (stringp value) (string-match "\\S-" value)
14576 (not (member value org-todo-keywords-1)))
14577 (error "\"%s\" is not a valid TODO state" value))
14578 (if (or (not value)
14579 (not (string-match "\\S-" value)))
14580 (setq value 'none))
14582 (org-set-tags nil 'align))
14583 ((equal property "PRIORITY")
14584 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
14585 (string-to-char value) ?\ ))
14586 (org-set-tags nil 'align))
14587 ((equal property "SCHEDULED")
14588 (if (re-search-forward org-scheduled-time-regexp end t)
14590 ((eq value 'earlier) (org-timestamp-change -1 'day))
14591 ((eq value 'later) (org-timestamp-change 1 'day))
14592 (t (call-interactively 'org-schedule)))
14593 (call-interactively 'org-schedule)))
14594 ((equal property "DEADLINE")
14595 (if (re-search-forward org-deadline-time-regexp end t)
14597 ((eq value 'earlier) (org-timestamp-change -1 'day))
14598 ((eq value 'later) (org-timestamp-change 1 'day))
14599 (t (call-interactively 'org-deadline)))
14600 (call-interactively 'org-deadline)))
14601 ((member property org-special-properties)
14602 (error "The %s property can not yet be set with `org-entry-put'"
14604 (t ; a non-special property
14605 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
14606 (setq range (org-get-property-block beg end 'force))
14607 (goto-char (car range))
14608 (if (re-search-forward
14609 (org-re-property-keyword property) (cdr range) t)
14611 (delete-region (match-beginning 0) (match-end 0))
14612 (goto-char (match-beginning 0)))
14613 (goto-char (cdr range))
14617 (insert ":" property ":")
14618 (and value (insert " " value))
14619 (org-indent-line)))))
14620 (run-hook-with-args 'org-property-changed-functions property value)))
14622 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
14623 "Get all property keys in the current buffer.
14624 With INCLUDE-SPECIALS, also list the special properties that reflect things
14625 like tags and TODO state.
14626 With INCLUDE-DEFAULTS, also include properties that has special meaning
14627 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
14629 With INCLUDE-COLUMNS, also include property names given in COLUMN
14630 formats in the current buffer."
14631 (let (rtn range cfmt s p)
14635 (goto-char (point-min))
14636 (while (re-search-forward org-property-start-re nil t)
14637 (setq range (org-get-property-block))
14638 (goto-char (car range))
14639 (while (re-search-forward
14640 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
14642 (add-to-list 'rtn (org-match-string-no-properties 1)))
14643 (outline-next-heading))))
14645 (when include-specials
14646 (setq rtn (append org-special-properties rtn)))
14648 (when include-defaults
14649 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
14650 (add-to-list 'rtn org-effort-property))
14652 (when include-columns
14656 (goto-char (point-min))
14657 (while (re-search-forward
14658 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
14660 (setq cfmt (match-string 2) s 0)
14661 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
14663 (setq s (match-end 0)
14664 p (match-string 1 cfmt))
14665 (unless (or (equal p "ITEM")
14666 (member p org-special-properties))
14667 (add-to-list 'rtn (match-string 1 cfmt))))))))
14669 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
14671 (defun org-property-values (key)
14672 "Return a list of all values of property KEY in the current buffer."
14676 (goto-char (point-min))
14677 (let ((re (org-re-property key))
14679 (while (re-search-forward re nil t)
14680 (add-to-list 'values (org-trim (match-string 1))))
14681 (delete "" values)))))
14683 (defun org-insert-property-drawer ()
14684 "Insert a property drawer into the current entry."
14685 (org-back-to-heading t)
14686 (looking-at org-outline-regexp)
14687 (let ((indent (if org-adapt-indentation
14688 (- (match-end 0) (match-beginning 0))
14691 (re (concat "^[ \t]*" org-keyword-time-regexp))
14693 (outline-next-heading)
14696 (while (re-search-forward re end t))
14697 (setq hiddenp (outline-invisible-p))
14699 (and (equal (char-after) ?\n) (forward-char 1))
14700 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
14701 (if (member (match-string 1) '("CLOCK:" ":END:"))
14702 ;; just skip this line
14703 (beginning-of-line 2)
14704 ;; Drawer start, find the end
14705 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
14706 (beginning-of-line 1)))
14707 (org-skip-over-state-notes)
14708 (skip-chars-backward " \t\n\r")
14709 (if (eq (char-before) ?*) (forward-char 1))
14710 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
14711 (beginning-of-line 0)
14712 (org-indent-to-column indent)
14713 (beginning-of-line 2)
14714 (org-indent-to-column indent)
14715 (beginning-of-line 0)
14718 (org-back-to-heading t)
14720 (org-flag-drawer t))))
14722 (defun org-insert-drawer (&optional arg drawer)
14723 "Insert a drawer at point.
14725 Optional argument DRAWER, when non-nil, is a string representing
14726 drawer's name. Otherwise, the user is prompted for a name.
14728 If a region is active, insert the drawer around that region
14731 Point is left between drawer's boundaries."
14733 (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
14735 ;; SYSTEM-DRAWERS is a list of drawer names that are used
14736 ;; internally by Org. They are meant to be inserted
14738 (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
14739 ;; Remove system drawers from list. Note: For some reason,
14740 ;; `org-completing-read' ignores the predicate while
14741 ;; `completing-read' handles it fine.
14742 (drawer (if arg "PROPERTIES"
14745 "Drawer: " org-drawers
14746 (lambda (d) (not (member d system-drawers))))))))
14748 ;; With C-u, fall back on `org-insert-property-drawer'
14749 (arg (org-insert-property-drawer))
14750 ;; With an active region, insert a drawer at point.
14751 ((not (org-region-active-p))
14753 (unless (bolp) (insert "\n"))
14754 (insert (format ":%s:\n\n:END:\n" drawer))
14755 (forward-line -2)))
14756 ;; Otherwise, insert the drawer at point
14758 (let ((rbeg (region-beginning))
14759 (rend (copy-marker (region-end))))
14763 (beginning-of-line)
14764 (when (save-excursion
14765 (re-search-forward org-outline-regexp-bol rend t))
14766 (error "Drawers cannot contain headlines"))
14767 ;; Position point at the beginning of the first
14768 ;; non-blank line in region. Insert drawer's opening
14769 ;; there, then indent it.
14770 (org-skip-whitespace)
14771 (beginning-of-line)
14772 (insert ":" drawer ":\n")
14774 (indent-for-tab-command)
14775 ;; Move point to the beginning of the first blank line
14776 ;; after the last non-blank line in region. Insert
14777 ;; drawer's closing, then indent it.
14779 (skip-chars-backward " \r\t\n")
14781 (indent-for-tab-command)
14782 (unless (eolp) (insert "\n")))
14783 ;; Clear marker, whatever the outcome of insertion is.
14784 (set-marker rend nil)))))))
14786 (defvar org-property-set-functions-alist nil
14787 "Property set function alist.
14788 Each entry should have the following format:
14790 (PROPERTY . READ-FUNCTION)
14792 The read function will be called with the same argument as
14793 `org-completing-read'.")
14795 (defun org-set-property-function (property)
14796 "Get the function that should be used to set PROPERTY.
14797 This is computed according to `org-property-set-functions-alist'."
14798 (or (cdr (assoc property org-property-set-functions-alist))
14799 'org-completing-read))
14801 (defun org-read-property-value (property)
14802 "Read PROPERTY value from user."
14803 (let* ((completion-ignore-case t)
14804 (allowed (org-property-get-allowed-values nil property 'table))
14805 (cur (org-entry-get nil property))
14806 (prompt (concat property " value"
14807 (if (and cur (string-match "\\S-" cur))
14808 (concat " [" cur "]") "") ": "))
14809 (set-function (org-set-property-function property))
14811 (funcall set-function prompt allowed nil
14812 (not (get-text-property 0 'org-unrestricted
14814 (let (org-completion-use-ido org-completion-use-iswitchb)
14815 (funcall set-function prompt
14816 (mapcar 'list (org-property-values property))
14817 nil nil "" nil cur)))))
14822 (defvar org-last-set-property nil)
14823 (defun org-read-property-name ()
14824 "Read a property name."
14825 (let* ((completion-ignore-case t)
14826 (keys (org-buffer-property-keys nil t t))
14827 (default-prop (or (save-excursion
14829 (beginning-of-line)
14830 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
14831 (null (string= (match-string 1) "END"))
14832 (match-string 1))))
14833 org-last-set-property))
14834 (property (org-icompleting-read
14836 (if default-prop (concat " [" default-prop "]") "")
14838 (mapcar 'list keys)
14842 (if (member property keys)
14844 (or (cdr (assoc (downcase property)
14845 (mapcar (lambda (x) (cons (downcase x) x))
14849 (defun org-set-property (property value)
14850 "In the current entry, set PROPERTY to VALUE.
14851 When called interactively, this will prompt for a property name, offering
14852 completion on existing and default properties. And then it will prompt
14853 for a value, offering completion either on allowed values (via an inherited
14854 xxx_ALL property) or on existing values in other instances of this property
14855 in the current file."
14856 (interactive (list nil nil))
14857 (let* ((property (or property (org-read-property-name)))
14858 (value (or value (org-read-property-value property)))
14859 (fn (cdr (assoc property org-properties-postprocess-alist))))
14860 (setq org-last-set-property property)
14861 ;; Possibly postprocess the inserted value:
14862 (when fn (setq value (funcall fn value)))
14863 (unless (equal (org-entry-get nil property) value)
14864 (org-entry-put nil property value))))
14866 (defun org-delete-property (property)
14867 "In the current entry, delete PROPERTY."
14869 (let* ((completion-ignore-case t)
14870 (prop (org-icompleting-read "Property: "
14871 (org-entry-properties nil 'standard))))
14873 (message "Property %s %s" property
14874 (if (org-entry-delete nil property)
14876 "was not present in the entry")))
14878 (defun org-delete-property-globally (property)
14879 "Remove PROPERTY globally, from all entries."
14881 (let* ((completion-ignore-case t)
14882 (prop (org-icompleting-read
14883 "Globally remove property: "
14884 (mapcar 'list (org-buffer-property-keys)))))
14889 (goto-char (point-min))
14891 (while (re-search-forward
14892 (org-re-property property)
14894 (setq cnt (1+ cnt))
14895 (delete-region (match-beginning 0) (1+ (point-at-eol))))
14896 (message "Property \"%s\" removed from %d entries" property cnt)))))
14898 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
14900 (defun org-compute-property-at-point ()
14901 "Compute the property at point.
14902 This looks for an enclosing column format, extracts the operator and
14903 then applies it to the property in the column format's scope."
14905 (unless (org-at-property-p)
14906 (error "Not at a property"))
14907 (let ((prop (org-match-string-no-properties 2)))
14908 (org-columns-get-format-and-top-level)
14909 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
14910 (error "No operator defined for property %s" prop))
14911 (org-columns-compute prop)))
14913 (defvar org-property-allowed-value-functions nil
14914 "Hook for functions supplying allowed values for a specific property.
14915 The functions must take a single argument, the name of the property, and
14916 return a flat list of allowed values. If \":ETC\" is one of
14917 the values, this means that these values are intended as defaults for
14918 completion, but that other values should be allowed too.
14919 The functions must return nil if they are not responsible for this
14922 (defun org-property-get-allowed-values (pom property &optional table)
14923 "Get allowed values for the property PROPERTY.
14924 When TABLE is non-nil, return an alist that can directly be used for
14928 ((equal property "TODO")
14929 (setq vals (org-with-point-at pom
14930 (append org-todo-keywords-1 '("")))))
14931 ((equal property "PRIORITY")
14932 (let ((n org-lowest-priority))
14933 (while (>= n org-highest-priority)
14934 (push (char-to-string n) vals)
14936 ((member property org-special-properties))
14937 ((setq vals (run-hook-with-args-until-success
14938 'org-property-allowed-value-functions property)))
14940 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
14941 (when (and vals (string-match "\\S-" vals))
14942 (setq vals (car (read-from-string (concat "(" vals ")"))))
14943 (setq vals (mapcar (lambda (x)
14944 (cond ((stringp x) x)
14945 ((numberp x) (number-to-string x))
14946 ((symbolp x) (symbol-name x))
14949 (when (member ":ETC" vals)
14950 (setq vals (remove ":ETC" vals))
14951 (org-add-props (car vals) '(org-unrestricted t)))
14952 (if table (mapcar 'list vals) vals)))
14954 (defun org-property-previous-allowed-value (&optional previous)
14955 "Switch to the next allowed value for this property."
14957 (org-property-next-allowed-value t))
14959 (defun org-property-next-allowed-value (&optional previous)
14960 "Switch to the next allowed value for this property."
14962 (unless (org-at-property-p)
14963 (error "Not at a property"))
14964 (let* ((key (match-string 2))
14965 (value (match-string 3))
14966 (allowed (or (org-property-get-allowed-values (point) key)
14967 (and (member value '("[ ]" "[-]" "[X]"))
14971 (error "Allowed values for this property have not been defined"))
14972 (if previous (setq allowed (reverse allowed)))
14973 (if (member value allowed)
14974 (setq nval (car (cdr (member value allowed)))))
14975 (setq nval (or nval (car allowed)))
14976 (if (equal nval value)
14977 (error "Only one allowed value for this property"))
14978 (org-at-property-p)
14979 (replace-match (concat " :" key ": " nval) t t)
14981 (beginning-of-line 1)
14982 (skip-chars-forward " \t")
14983 (run-hook-with-args 'org-property-changed-functions key nval)))
14985 (defun org-find-olp (path &optional this-buffer)
14986 "Return a marker pointing to the entry at outline path OLP.
14987 If anything goes wrong, throw an error.
14988 You can wrap this call to catch the error like this:
14990 (condition-case msg
14991 (org-mobile-locate-entry (match-string 4))
14992 (error (nth 1 msg)))
14994 The return value will then be either a string with the error message,
14995 or a marker if everything is OK.
14997 If THIS-BUFFER is set, the outline path does not contain a file,
14999 (let* ((file (if this-buffer buffer-file-name (pop path)))
15000 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
15004 limit re end found pos heading cnt flevel)
15005 (unless buffer (error "File not found :%s" file))
15006 (with-current-buffer buffer
15010 (setq limit (point-max))
15011 (goto-char (point-min))
15012 (while (setq heading (pop path))
15013 (setq re (format org-complex-heading-regexp-format
15014 (regexp-quote heading)))
15015 (setq cnt 0 pos (point))
15016 (while (re-search-forward re end t)
15017 (setq level (- (match-end 1) (match-beginning 1)))
15018 (if (and (>= level lmin) (<= level lmax))
15019 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
15020 (when (= cnt 0) (error "Heading not found on level %d: %s"
15022 (when (> cnt 1) (error "Heading not unique on level %d: %s"
15025 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
15026 (setq end (save-excursion (org-end-of-subtree t t))))
15027 (when (org-at-heading-p)
15028 (move-marker (make-marker) (point))))))))
15030 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
15031 "Find node HEADING in BUFFER.
15032 Return a marker to the heading if it was found, or nil if not.
15033 If POS-ONLY is set, return just the position instead of a marker.
15035 The heading text must match exact, but it may have a TODO keyword,
15036 a priority cookie and tags in the standard locations."
15037 (with-current-buffer (or buffer (current-buffer))
15041 (goto-char (point-min))
15042 (let (case-fold-search)
15043 (if (re-search-forward
15044 (format org-complex-heading-regexp-format
15045 (regexp-quote heading)) nil t)
15047 (match-beginning 0)
15048 (move-marker (make-marker) (match-beginning 0)))))))))
15050 (defun org-find-exact-heading-in-directory (heading &optional dir)
15051 "Find Org node headline HEADING in all .org files in directory DIR.
15052 When the target headline is found, return a marker to this location."
15053 (let ((files (directory-files (or dir default-directory)
15054 nil "\\`[^.#].*\\.org\\'"))
15055 file visiting m buffer)
15057 (while (setq file (pop files))
15058 (message "trying %s" file)
15059 (setq visiting (org-find-base-buffer-visiting file))
15060 (setq buffer (or visiting (find-file-noselect file)))
15061 (setq m (org-find-exact-headline-in-buffer
15063 (when (and (not m) (not visiting)) (kill-buffer buffer))
15064 (and m (throw 'found m))))))
15066 (defun org-find-entry-with-id (ident)
15067 "Locate the entry that contains the ID property with exact value IDENT.
15068 IDENT can be a string, a symbol or a number, this function will search for
15069 the string representation of it.
15070 Return the position where this entry starts, or nil if there is no such entry."
15071 (interactive "sID: ")
15073 ((stringp ident) ident)
15074 ((symbol-name ident) (symbol-name ident))
15075 ((numberp ident) (number-to-string ident))
15076 (t (error "IDENT %s must be a string, symbol or number" ident))))
15077 (case-fold-search nil))
15081 (goto-char (point-min))
15082 (when (re-search-forward
15083 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
15085 (org-back-to-heading t)
15090 (defvar org-last-changed-timestamp nil)
15091 (defvar org-last-inserted-timestamp nil
15092 "The last time stamp inserted with `org-insert-time-stamp'.")
15093 (defvar org-time-was-given) ; dynamically scoped parameter
15094 (defvar org-end-time-was-given) ; dynamically scoped parameter
15095 (defvar org-ts-what) ; dynamically scoped parameter
15097 (defun org-time-stamp (arg &optional inactive)
15098 "Prompt for a date/time and insert a time stamp.
15099 If the user specifies a time like HH:MM or if this command is
15100 called with at least one prefix argument, the time stamp contains
15101 the date and the time. Otherwise, only the date is be included.
15103 All parts of a date not specified by the user is filled in from
15104 the current date/time. So if you just press return without
15105 typing anything, the time stamp will represent the current
15108 If there is already a timestamp at the cursor, it will be
15111 With two universal prefix arguments, insert an active timestamp
15112 with the current time without prompting the user."
15116 ;; Default time is either today, or, when entering a range,
15117 ;; the range start.
15118 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
15120 (re-search-backward
15121 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
15122 (- (point) 20) t)))
15123 (apply 'encode-time (org-parse-time-string (match-string 1)))
15125 (default-input (and ts (org-get-compact-tod ts)))
15126 (repeater (save-excursion
15128 (beginning-of-line)
15129 (when (re-search-forward
15130 "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
15131 (save-excursion (progn (end-of-line) (point))) t)
15132 (match-string 0)))))
15133 org-time-was-given org-end-time-was-given time)
15135 ((and (org-at-timestamp-p t)
15136 (memq last-command '(org-time-stamp org-time-stamp-inactive))
15137 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
15139 (setq time (let ((this-command this-command))
15140 (org-read-date arg 'totime nil nil
15141 default-time default-input inactive)))
15142 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
15143 ((org-at-timestamp-p t)
15144 (setq time (let ((this-command this-command))
15145 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15146 (when (org-at-timestamp-p t) ; just to get the match data
15147 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
15149 (setq org-last-changed-timestamp
15150 (org-insert-time-stamp
15151 time (or org-time-was-given arg)
15152 inactive nil nil (list org-end-time-was-given)))
15153 (when repeater (goto-char (1- (point))) (insert " " repeater)
15154 (setq org-last-changed-timestamp
15155 (concat (substring org-last-inserted-timestamp 0 -1)
15156 " " repeater ">"))))
15157 (message "Timestamp updated"))
15159 (org-insert-time-stamp (current-time) t))
15161 (setq time (let ((this-command this-command))
15162 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15163 (org-insert-time-stamp time (or org-time-was-given arg) inactive
15164 nil nil (list org-end-time-was-given))))))
15166 ;; FIXME: can we use this for something else, like computing time differences?
15167 (defun org-get-compact-tod (s)
15168 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
15169 (let* ((t1 (match-string 1 s))
15170 (h1 (string-to-number (match-string 2 s)))
15171 (m1 (string-to-number (match-string 3 s)))
15172 (t2 (and (match-end 4) (match-string 5 s)))
15173 (h2 (and t2 (string-to-number (match-string 6 s))))
15174 (m2 (and t2 (string-to-number (match-string 7 s))))
15178 (setq dh (- h2 h1) dm (- m2 m1))
15179 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
15180 (concat t1 "+" (number-to-string dh)
15181 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
15183 (defun org-time-stamp-inactive (&optional arg)
15184 "Insert an inactive time stamp.
15185 An inactive time stamp is enclosed in square brackets instead of angle
15186 brackets. It is inactive in the sense that it does not trigger agenda entries,
15187 does not link to the calendar and cannot be changed with the S-cursor keys.
15188 So these are more for recording a certain time/date."
15190 (org-time-stamp arg 'inactive))
15192 (defvar org-date-ovl (make-overlay 1 1))
15193 (overlay-put org-date-ovl 'face 'org-date-selected)
15194 (org-detach-overlay org-date-ovl)
15196 (defvar org-ans1) ; dynamically scoped parameter
15197 (defvar org-ans2) ; dynamically scoped parameter
15199 (defvar org-plain-time-of-day-regexp) ; defined below
15201 (defvar org-overriding-default-time nil) ; dynamically scoped
15202 (defvar org-read-date-overlay nil)
15203 (defvar org-dcst nil) ; dynamically scoped
15204 (defvar org-read-date-history nil)
15205 (defvar org-read-date-final-answer nil)
15206 (defvar org-read-date-analyze-futurep nil)
15207 (defvar org-read-date-analyze-forced-year nil)
15208 (defvar org-read-date-inactive)
15210 (defun org-read-date (&optional org-with-time to-time from-string prompt
15211 default-time default-input inactive)
15212 "Read a date, possibly a time, and make things smooth for the user.
15213 The prompt will suggest to enter an ISO date, but you can also enter anything
15214 which will at least partially be understood by `parse-time-string'.
15215 Unrecognized parts of the date will default to the current day, month, year,
15216 hour and minute. If this command is called to replace a timestamp at point,
15217 or to enter the second timestamp of a range, the default time is taken
15218 from the existing stamp. Furthermore, the command prefers the future,
15219 so if you are giving a date where the year is not given, and the day-month
15220 combination is already past in the current year, it will assume you
15221 mean next year. For details, see the manual. A few examples:
15223 3-2-5 --> 2003-02-05
15224 feb 15 --> currentyear-02-15
15225 2/15 --> currentyear-02-15
15226 sep 12 9 --> 2009-09-12
15227 12:45 --> today 12:45
15228 22 sept 0:34 --> currentyear-09-22 0:34
15229 12 --> currentyear-currentmonth-12
15230 Fri --> nearest Friday (today or later)
15233 Furthermore you can specify a relative date by giving, as the *first* thing
15234 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
15235 change in days weeks, months, years.
15236 With a single plus or minus, the date is relative to today. With a double
15237 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
15238 +4d --> four days from today
15239 +4 --> same as above
15240 +2w --> two weeks from today
15241 ++5 --> five days from default date
15243 The function understands only English month and weekday abbreviations.
15245 While prompting, a calendar is popped up - you can also select the
15246 date with the mouse (button 1). The calendar shows a period of three
15247 months. To scroll it to other months, use the keys `>' and `<'.
15248 If you don't like the calendar, turn it off with
15249 \(setq org-read-date-popup-calendar nil)
15251 With optional argument TO-TIME, the date will immediately be converted
15252 to an internal time.
15253 With an optional argument WITH-TIME, the prompt will suggest to also
15254 insert a time. Note that when WITH-TIME is not set, you can still
15255 enter a time, and this function will inform the calling routine about
15256 this change. The calling routine may then choose to change the format
15257 used to insert the time stamp into the buffer to include the time.
15258 With optional argument FROM-STRING, read from this string instead from
15259 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
15260 the time/date that is used for everything that is not specified by the
15262 (require 'parse-time)
15263 (let* ((org-time-stamp-rounding-minutes
15264 (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
15265 (org-dcst org-display-custom-times)
15266 (ct (org-current-time))
15267 (org-def (or org-overriding-default-time default-time ct))
15268 (org-defdecode (decode-time org-def))
15270 (when (< (nth 2 org-defdecode) org-extend-today-until)
15271 (setcar (nthcdr 2 org-defdecode) -1)
15272 (setcar (nthcdr 1 org-defdecode) 59)
15273 (setq org-def (apply 'encode-time org-defdecode)
15274 org-defdecode (decode-time org-def)))))
15275 (calendar-frame-setup nil)
15276 (calendar-setup nil)
15277 (calendar-move-hook nil)
15278 (calendar-view-diary-initially-flag nil)
15279 (calendar-view-holidays-initially-flag nil)
15280 (timestr (format-time-string
15281 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
15282 (prompt (concat (if prompt (concat prompt " ") "")
15283 (format "Date+time [%s]: " timestr)))
15284 ans (org-ans0 "") org-ans1 org-ans2 final)
15287 (from-string (setq ans from-string))
15288 (org-read-date-popup-calendar
15290 (save-window-excursion
15292 (org-eval-in-calendar '(setq cursor-type nil) t)
15295 (calendar-forward-day (- (time-to-days org-def)
15296 (calendar-absolute-from-gregorian
15297 (calendar-current-date))))
15298 (org-eval-in-calendar nil t)
15299 (let* ((old-map (current-local-map))
15300 (map (copy-keymap calendar-mode-map))
15301 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
15302 (org-defkey map (kbd "RET") 'org-calendar-select)
15303 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
15304 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
15305 (org-defkey minibuffer-local-map [(meta shift left)]
15306 (lambda () (interactive)
15307 (org-eval-in-calendar '(calendar-backward-month 1))))
15308 (org-defkey minibuffer-local-map [(meta shift right)]
15309 (lambda () (interactive)
15310 (org-eval-in-calendar '(calendar-forward-month 1))))
15311 (org-defkey minibuffer-local-map [(meta shift up)]
15312 (lambda () (interactive)
15313 (org-eval-in-calendar '(calendar-backward-year 1))))
15314 (org-defkey minibuffer-local-map [(meta shift down)]
15315 (lambda () (interactive)
15316 (org-eval-in-calendar '(calendar-forward-year 1))))
15317 (org-defkey minibuffer-local-map [?\e (shift left)]
15318 (lambda () (interactive)
15319 (org-eval-in-calendar '(calendar-backward-month 1))))
15320 (org-defkey minibuffer-local-map [?\e (shift right)]
15321 (lambda () (interactive)
15322 (org-eval-in-calendar '(calendar-forward-month 1))))
15323 (org-defkey minibuffer-local-map [?\e (shift up)]
15324 (lambda () (interactive)
15325 (org-eval-in-calendar '(calendar-backward-year 1))))
15326 (org-defkey minibuffer-local-map [?\e (shift down)]
15327 (lambda () (interactive)
15328 (org-eval-in-calendar '(calendar-forward-year 1))))
15329 (org-defkey minibuffer-local-map [(shift up)]
15330 (lambda () (interactive)
15331 (org-eval-in-calendar '(calendar-backward-week 1))))
15332 (org-defkey minibuffer-local-map [(shift down)]
15333 (lambda () (interactive)
15334 (org-eval-in-calendar '(calendar-forward-week 1))))
15335 (org-defkey minibuffer-local-map [(shift left)]
15336 (lambda () (interactive)
15337 (org-eval-in-calendar '(calendar-backward-day 1))))
15338 (org-defkey minibuffer-local-map [(shift right)]
15339 (lambda () (interactive)
15340 (org-eval-in-calendar '(calendar-forward-day 1))))
15341 (org-defkey minibuffer-local-map ">"
15342 (lambda () (interactive)
15343 (org-eval-in-calendar '(scroll-calendar-left 1))))
15344 (org-defkey minibuffer-local-map "<"
15345 (lambda () (interactive)
15346 (org-eval-in-calendar '(scroll-calendar-right 1))))
15347 (org-defkey minibuffer-local-map "\C-v"
15348 (lambda () (interactive)
15349 (org-eval-in-calendar
15350 '(calendar-scroll-left-three-months 1))))
15351 (org-defkey minibuffer-local-map "\M-v"
15352 (lambda () (interactive)
15353 (org-eval-in-calendar
15354 '(calendar-scroll-right-three-months 1))))
15355 (run-hooks 'org-read-date-minibuffer-setup-hook)
15358 (use-local-map map)
15359 (setq org-read-date-inactive inactive)
15360 (add-hook 'post-command-hook 'org-read-date-display)
15361 (setq org-ans0 (read-string prompt default-input
15362 'org-read-date-history nil))
15363 ;; org-ans0: from prompt
15364 ;; org-ans1: from mouse click
15365 ;; org-ans2: from calendar motion
15366 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
15367 (remove-hook 'post-command-hook 'org-read-date-display)
15368 (use-local-map old-map)
15369 (when org-read-date-overlay
15370 (delete-overlay org-read-date-overlay)
15371 (setq org-read-date-overlay nil)))))
15372 (bury-buffer "*Calendar*")))))
15374 (t ; Naked prompt only
15376 (setq ans (read-string prompt default-input
15377 'org-read-date-history timestr))
15378 (when org-read-date-overlay
15379 (delete-overlay org-read-date-overlay)
15380 (setq org-read-date-overlay nil)))))
15382 (setq final (org-read-date-analyze ans org-def org-defdecode))
15384 (when org-read-date-analyze-forced-year
15385 (message "Year was forced into %s"
15386 (if org-read-date-force-compatible-dates
15387 "compatible range (1970-2037)"
15388 "range representable on this machine"))
15391 ;; One round trip to get rid of 34th of August and stuff like that....
15392 (setq final (decode-time (apply 'encode-time final)))
15394 (setq org-read-date-final-answer ans)
15397 (apply 'encode-time final)
15398 (if (and (boundp 'org-time-was-given) org-time-was-given)
15399 (format "%04d-%02d-%02d %02d:%02d"
15400 (nth 5 final) (nth 4 final) (nth 3 final)
15401 (nth 2 final) (nth 1 final))
15402 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
15405 (defvar org-defdecode)
15406 (defvar org-with-time)
15407 (defun org-read-date-display ()
15408 "Display the current date prompt interpretation in the minibuffer."
15409 (when org-read-date-display-live
15410 (when org-read-date-overlay
15411 (delete-overlay org-read-date-overlay))
15412 (when (minibufferp (current-buffer))
15415 (while (not (equal (buffer-substring
15416 (max (point-min) (- (point) 4)) (point))
15419 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
15420 " " (or org-ans1 org-ans2)))
15421 (org-end-time-was-given nil)
15422 (f (org-read-date-analyze ans org-def org-defdecode))
15424 org-time-stamp-custom-formats
15425 org-time-stamp-formats))
15426 (fmt (if (or org-with-time
15427 (and (boundp 'org-time-was-given) org-time-was-given))
15430 (txt (format-time-string fmt (apply 'encode-time f)))
15431 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
15432 (txt (concat "=> " txt)))
15433 (when (and org-end-time-was-given
15434 (string-match org-plain-time-of-day-regexp txt))
15435 (setq txt (concat (substring txt 0 (match-end 0)) "-"
15436 org-end-time-was-given
15437 (substring txt (match-end 0)))))
15438 (when org-read-date-analyze-futurep
15439 (setq txt (concat txt " (=>F)")))
15440 (setq org-read-date-overlay
15441 (make-overlay (1- (point-at-eol)) (point-at-eol)))
15442 (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
15444 (defun org-read-date-analyze (ans org-def org-defdecode)
15445 "Analyze the combined answer of the date prompt."
15446 ;; FIXME: cleanup and comment
15447 (let ((nowdecode (decode-time (current-time)))
15448 delta deltan deltaw deltadef year month day
15449 hour minute second wday pm h2 m2 tl wday1
15450 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
15451 (setq org-read-date-analyze-futurep nil
15452 org-read-date-analyze-forced-year nil)
15453 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
15456 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
15457 (setq ans (replace-match "" t t ans)
15459 deltaw (nth 1 delta)
15460 deltadef (nth 2 delta)))
15462 ;; Check if there is an iso week date in there
15463 ;; If yes, store the info and postpone interpreting it until the rest
15464 ;; of the parsing is done
15465 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
15466 (setq iso-year (if (match-end 1)
15467 (org-small-year-to-year
15468 (string-to-number (match-string 1 ans))))
15469 iso-weekday (if (match-end 3)
15470 (string-to-number (match-string 3 ans)))
15471 iso-week (string-to-number (match-string 2 ans)))
15472 (setq ans (replace-match "" t t ans)))
15474 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
15475 (when (string-match
15476 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
15477 (setq year (if (match-end 2)
15478 (string-to-number (match-string 2 ans))
15479 (progn (setq kill-year t)
15480 (string-to-number (format-time-string "%Y"))))
15481 month (string-to-number (match-string 3 ans))
15482 day (string-to-number (match-string 4 ans)))
15483 (if (< year 100) (setq year (+ 2000 year)))
15484 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15487 ;; Help matching dotted european dates
15488 (when (string-match
15489 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
15490 (setq year (if (match-end 3)
15491 (string-to-number (match-string 3 ans))
15492 (progn (setq kill-year t)
15493 (string-to-number (format-time-string "%Y"))))
15494 day (string-to-number (match-string 1 ans))
15495 month (string-to-number (match-string 2 ans))
15496 ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15499 ;; Help matching american dates, like 5/30 or 5/30/7
15500 (when (string-match
15501 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
15502 (setq year (if (match-end 4)
15503 (string-to-number (match-string 4 ans))
15504 (progn (setq kill-year t)
15505 (string-to-number (format-time-string "%Y"))))
15506 month (string-to-number (match-string 1 ans))
15507 day (string-to-number (match-string 2 ans)))
15508 (if (< year 100) (setq year (+ 2000 year)))
15509 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15511 ;; Help matching am/pm times, because `parse-time-string' does not do that.
15512 ;; If there is a time with am/pm, and *no* time without it, we convert
15513 ;; so that matching will be successful.
15514 (loop for i from 1 to 2 do ; twice, for end time as well
15515 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
15516 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
15517 (setq hour (string-to-number (match-string 1 ans))
15518 minute (if (match-end 3)
15519 (string-to-number (match-string 3 ans))
15522 (string-to-char (downcase (match-string 4 ans)))))
15523 (if (and (= hour 12) (not pm))
15525 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
15526 (setq ans (replace-match (format "%02d:%02d" hour minute)
15529 ;; Check if a time range is given as a duration
15530 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
15531 (setq hour (string-to-number (match-string 1 ans))
15532 h2 (+ hour (string-to-number (match-string 3 ans)))
15533 minute (string-to-number (match-string 2 ans))
15534 m2 (+ minute (if (match-end 5) (string-to-number
15535 (match-string 5 ans))0)))
15536 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
15537 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
15540 ;; Check if there is a time range
15541 (when (boundp 'org-end-time-was-given)
15542 (setq org-time-was-given nil)
15543 (when (and (string-match org-plain-time-of-day-regexp ans)
15545 (setq org-end-time-was-given (match-string 8 ans))
15546 (setq ans (concat (substring ans 0 (match-beginning 7))
15547 (substring ans (match-end 7))))))
15549 (setq tl (parse-time-string ans)
15550 day (or (nth 3 tl) (nth 3 org-defdecode))
15551 month (or (nth 4 tl)
15552 (if (and org-read-date-prefer-future
15553 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
15554 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
15555 (nth 4 org-defdecode)))
15556 year (or (and (not kill-year) (nth 5 tl))
15557 (if (and org-read-date-prefer-future
15558 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
15559 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
15560 (nth 5 org-defdecode)))
15561 hour (or (nth 2 tl) (nth 2 org-defdecode))
15562 minute (or (nth 1 tl) (nth 1 org-defdecode))
15563 second (or (nth 0 tl) 0)
15566 (when (and (eq org-read-date-prefer-future 'time)
15567 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
15568 (equal day (nth 3 nowdecode))
15569 (equal month (nth 4 nowdecode))
15570 (equal year (nth 5 nowdecode))
15572 (or (< (nth 2 tl) (nth 2 nowdecode))
15573 (and (= (nth 2 tl) (nth 2 nowdecode))
15575 (< (nth 1 tl) (nth 1 nowdecode)))))
15579 ;; Special date definitions below
15582 ;; There was an iso week
15585 (setq year (or iso-year year)
15586 day (or iso-weekday wday 1)
15587 wday nil ; to make sure that the trigger below does not match
15588 iso-date (calendar-gregorian-from-absolute
15589 (calendar-absolute-from-iso
15590 (list iso-week day year))))
15591 ; FIXME: Should we also push ISO weeks into the future?
15592 ; (when (and org-read-date-prefer-future
15594 ; (< (calendar-absolute-from-gregorian iso-date)
15595 ; (time-to-days (current-time))))
15596 ; (setq year (1+ year)
15597 ; iso-date (calendar-gregorian-from-absolute
15598 ; (calendar-absolute-from-iso
15599 ; (list iso-week day year)))))
15600 (setq month (car iso-date)
15601 year (nth 2 iso-date)
15602 day (nth 1 iso-date)))
15606 (let ((now (decode-time (current-time))))
15607 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
15608 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
15609 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
15610 ((equal deltaw "m") (setq month (+ month deltan)))
15611 ((equal deltaw "y") (setq year (+ year deltan)))))
15612 ((and wday (not (nth 3 tl)))
15614 ;; Weekday was given, but no day, so pick that day in the week
15615 ;; on or after the derived date.
15616 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
15617 (unless (equal wday wday1)
15618 (setq day (+ day (% (- wday wday1 -7) 7))))))
15619 (if (and (boundp 'org-time-was-given)
15621 (setq org-time-was-given t))
15622 (if (< year 100) (setq year (+ 2000 year)))
15623 ;; Check of the date is representable
15624 (if org-read-date-force-compatible-dates
15627 (setq year 1970 org-read-date-analyze-forced-year t))
15629 (setq year 2037 org-read-date-analyze-forced-year t)))
15630 (condition-case nil
15631 (ignore (encode-time second minute hour day month year))
15633 (setq year (nth 5 org-defdecode))
15634 (setq org-read-date-analyze-forced-year t))))
15635 (setq org-read-date-analyze-futurep futurep)
15636 (list second minute hour day month year)))
15638 (defvar parse-time-weekdays)
15639 (defun org-read-date-get-relative (s today default)
15640 "Check string S for special relative date string.
15641 TODAY and DEFAULT are internal times, for today and for a default.
15642 Return shift list (N what def-flag)
15643 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
15644 N is the number of WHATs to shift.
15645 DEF-FLAG is t when a double ++ or -- indicates shift relative to
15646 the DEFAULT date rather than TODAY."
15647 (require 'parse-time)
15651 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
15653 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
15654 "\\([ \t]\\|$\\)") s)
15655 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
15656 (let* ((dir (if (> (match-end 1) (match-beginning 1))
15657 (string-to-char (substring (match-string 1 s) -1))
15659 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
15660 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
15661 (what (if (match-end 3) (match-string 3 s) "d"))
15662 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
15663 (date (if rel default today))
15664 (wday (nth 6 (decode-time date)))
15668 (setq delta (mod (+ 7 (- wday1 wday)) 7))
15669 (if (= dir ?-) (setq delta (- delta 7)))
15670 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
15671 (list delta "d" rel))
15672 (list (* n (if (= dir ?-) -1 1)) what rel)))))
15674 (defun org-order-calendar-date-args (arg1 arg2 arg3)
15675 "Turn a user-specified date into the internal representation.
15676 The internal representation needed by the calendar is (month day year).
15677 This is a wrapper to handle the brain-dead convention in calendar that
15678 user function argument order change dependent on argument order."
15679 (if (boundp 'calendar-date-style)
15681 ((eq calendar-date-style 'american)
15682 (list arg1 arg2 arg3))
15683 ((eq calendar-date-style 'european)
15684 (list arg2 arg1 arg3))
15685 ((eq calendar-date-style 'iso)
15686 (list arg2 arg3 arg1)))
15687 (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
15688 (if (org-bound-and-true-p european-calendar-style)
15689 (list arg2 arg1 arg3)
15690 (list arg1 arg2 arg3)))))
15692 (defun org-eval-in-calendar (form &optional keepdate)
15693 "Eval FORM in the calendar window and return to current window.
15694 When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
15695 otherwise stick to the current value of `org-ans2'."
15696 (let ((sf (selected-frame))
15697 (sw (selected-window)))
15698 (select-window (get-buffer-window "*Calendar*" t))
15700 (when (and (not keepdate) (calendar-cursor-to-date))
15701 (let* ((date (calendar-cursor-to-date))
15702 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15703 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
15704 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
15706 (org-select-frame-set-input-focus sf)))
15708 (defun org-calendar-select ()
15709 "Return to `org-read-date' with the date currently selected.
15710 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15712 (when (calendar-cursor-to-date)
15713 (let* ((date (calendar-cursor-to-date))
15714 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15715 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15716 (if (active-minibuffer-window) (exit-minibuffer))))
15718 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
15719 "Insert a date stamp for the date given by the internal TIME.
15720 WITH-HM means use the stamp format that includes the time of the day.
15721 INACTIVE means use square brackets instead of angular ones, so that the
15722 stamp will not contribute to the agenda.
15723 PRE and POST are optional strings to be inserted before and after the
15725 The command returns the inserted time stamp."
15726 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
15728 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
15729 (insert-before-markers (or pre ""))
15730 (when (listp extra)
15731 (setq extra (car extra))
15732 (if (and (stringp extra)
15733 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
15734 (setq extra (format "-%02d:%02d"
15735 (string-to-number (match-string 1 extra))
15736 (string-to-number (match-string 2 extra))))
15739 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
15740 (insert-before-markers (setq stamp (format-time-string fmt time)))
15741 (insert-before-markers (or post ""))
15742 (setq org-last-inserted-timestamp stamp)))
15744 (defun org-toggle-time-stamp-overlays ()
15745 "Toggle the use of custom time stamp formats."
15747 (setq org-display-custom-times (not org-display-custom-times))
15748 (unless org-display-custom-times
15749 (let ((p (point-min)) (bmp (buffer-modified-p)))
15750 (while (setq p (next-single-property-change p 'display))
15751 (if (and (get-text-property p 'display)
15752 (eq (get-text-property p 'face) 'org-date))
15753 (remove-text-properties
15754 p (setq p (next-single-property-change p 'display))
15756 (set-buffer-modified-p bmp)))
15757 (if (featurep 'xemacs)
15758 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
15759 (org-restart-font-lock)
15760 (setq org-table-may-need-update t)
15761 (if org-display-custom-times
15762 (message "Time stamps are overlaid with custom format")
15763 (message "Time stamp overlays removed")))
15765 (defun org-display-custom-time (beg end)
15766 "Overlay modified time stamp format over timestamp between BEG and END."
15767 (let* ((ts (buffer-substring beg end))
15768 t1 w1 with-hm tf time str w2 (off 0))
15770 (setq t1 (org-parse-time-string ts t))
15771 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
15772 (setq off (- (match-end 0) (match-beginning 0)))))
15773 (setq end (- end off))
15774 (setq w1 (- end beg)
15775 with-hm (and (nth 1 t1) (nth 2 t1))
15776 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
15777 time (org-fix-decoded-time t1)
15779 (format-time-string
15780 (substring tf 1 -1) (apply 'encode-time time))
15781 nil 'mouse-face 'highlight)
15783 (if (not (= w2 w1))
15784 (add-text-properties (1+ beg) (+ 2 beg)
15785 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
15786 (if (featurep 'xemacs)
15788 (put-text-property beg end 'invisible t)
15789 (put-text-property beg end 'end-glyph (make-glyph str)))
15790 (put-text-property beg end 'display str))))
15792 (defun org-translate-time (string)
15793 "Translate all timestamps in STRING to custom format.
15794 But do this only if the variable `org-display-custom-times' is set."
15795 (when org-display-custom-times
15798 (re org-ts-regexp-both)
15799 t1 with-hm inactive tf time str beg end)
15800 (while (setq start (string-match re string start))
15801 (setq beg (match-beginning 0)
15803 t1 (save-match-data
15804 (org-parse-time-string (substring string beg end) t))
15805 with-hm (and (nth 1 t1) (nth 2 t1))
15806 inactive (equal (substring string beg (1+ beg)) "[")
15807 tf (funcall (if with-hm 'cdr 'car)
15808 org-time-stamp-custom-formats)
15809 time (org-fix-decoded-time t1)
15810 str (format-time-string
15812 (if inactive "[" "<") (substring tf 1 -1)
15813 (if inactive "]" ">"))
15814 (apply 'encode-time time))
15815 string (replace-match str t t string)
15816 start (+ start (length str)))))))
15819 (defun org-fix-decoded-time (time)
15820 "Set 0 instead of nil for the first 6 elements of time.
15821 Don't touch the rest."
15823 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
15825 (defun org-days-to-time (timestamp-string)
15826 "Difference between TIMESTAMP-STRING and now in days."
15827 (- (time-to-days (org-time-string-to-time timestamp-string))
15828 (time-to-days (current-time))))
15830 (defun org-deadline-close (timestamp-string &optional ndays)
15831 "Is the time in TIMESTAMP-STRING close to the current date?"
15832 (setq ndays (or ndays (org-get-wdays timestamp-string)))
15833 (and (< (org-days-to-time timestamp-string) ndays)
15834 (not (org-entry-is-done-p))))
15836 (defun org-get-wdays (ts)
15837 "Get the deadline lead time appropriate for timestring TS."
15839 ((<= org-deadline-warning-days 0)
15840 ;; 0 or negative, enforce this value no matter what
15841 (- org-deadline-warning-days))
15842 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
15843 ;; lead time is specified.
15844 (floor (* (string-to-number (match-string 1 ts))
15845 (cdr (assoc (match-string 2 ts)
15846 '(("d" . 1) ("w" . 7)
15847 ("m" . 30.4) ("y" . 365.25)))))))
15848 ;; go for the default.
15849 (t org-deadline-warning-days)))
15851 (defun org-calendar-select-mouse (ev)
15852 "Return to `org-read-date' with the date currently selected.
15853 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15855 (mouse-set-point ev)
15856 (when (calendar-cursor-to-date)
15857 (let* ((date (calendar-cursor-to-date))
15858 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15859 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15860 (if (active-minibuffer-window) (exit-minibuffer))))
15862 (defun org-check-deadlines (ndays)
15863 "Check if there are any deadlines due or past due.
15864 A deadline is considered due if it happens within `org-deadline-warning-days'
15865 days from today's date. If the deadline appears in an entry marked DONE,
15866 it is not shown. The prefix arg NDAYS can be used to test that many
15867 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
15869 (let* ((org-warn-days
15871 ((equal ndays '(4)) 100000)
15872 (ndays (prefix-numeric-value ndays))
15873 (t (abs org-deadline-warning-days))))
15874 (case-fold-search nil)
15875 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15877 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
15879 (message "%d deadlines past-due or due within %d days"
15880 (org-occur regexp nil callback)
15883 (defsubst org-re-timestamp (type)
15884 "Return a regexp for timestamp TYPE.
15885 Allowed values for TYPE are:
15887 all: all timestamps
15888 active: only active timestamps (<...>)
15889 inactive: only inactive timestamps ([...])
15890 scheduled: only scheduled timestamps
15891 deadline: only deadline timestamps
15893 When TYPE is nil, fall back on returning a regexp that matches
15894 both scheduled and deadline timestamps."
15895 (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9>
\n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
15896 ((eq type 'active) org-ts-regexp)
15897 ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^
\n>]*?\\)\\]")
15898 ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
15899 ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15900 ((eq type 'scheduled-or-deadline)
15901 (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
15903 (defun org-check-before-date (date)
15904 "Check if there are deadlines or scheduled entries before DATE."
15905 (interactive (list (org-read-date)))
15906 (let ((case-fold-search nil)
15907 (regexp (org-re-timestamp org-ts-type))
15909 (lambda () (time-less-p
15910 (org-time-string-to-time (match-string 1))
15911 (org-time-string-to-time date)))))
15912 (message "%d entries before %s"
15913 (org-occur regexp nil callback) date)))
15915 (defun org-check-after-date (date)
15916 "Check if there are deadlines or scheduled entries after DATE."
15917 (interactive (list (org-read-date)))
15918 (let ((case-fold-search nil)
15919 (regexp (org-re-timestamp org-ts-type))
15923 (org-time-string-to-time (match-string 1))
15924 (org-time-string-to-time date))))))
15925 (message "%d entries after %s"
15926 (org-occur regexp nil callback) date)))
15928 (defun org-check-dates-range (start-date end-date)
15929 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
15930 (interactive (list (org-read-date nil nil nil "Range starts")
15931 (org-read-date nil nil nil "Range end")))
15932 (let ((case-fold-search nil)
15933 (regexp (org-re-timestamp org-ts-type))
15936 (let ((match (match-string 1)))
15939 (org-time-string-to-time match)
15940 (org-time-string-to-time start-date)))
15942 (org-time-string-to-time match)
15943 (org-time-string-to-time end-date)))))))
15944 (message "%d entries between %s and %s"
15945 (org-occur regexp nil callback) start-date end-date)))
15947 (defun org-evaluate-time-range (&optional to-buffer)
15948 "Evaluate a time range by computing the difference between start and end.
15949 Normally the result is just printed in the echo area, but with prefix arg
15950 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
15951 If the time range is actually in a table, the result is inserted into the
15953 For time difference computation, a year is assumed to be exactly 365
15954 days in order to avoid rounding problems."
15957 (org-clock-update-time-maybe)
15959 (unless (org-at-date-range-p t)
15960 (goto-char (point-at-bol))
15961 (re-search-forward org-tr-regexp-both (point-at-eol) t))
15962 (if (not (org-at-date-range-p t))
15963 (error "Not at a time-stamp range, and none found in current line")))
15964 (let* ((ts1 (match-string 1))
15965 (ts2 (match-string 2))
15966 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
15967 (match-end (match-end 0))
15968 (time1 (org-time-string-to-time ts1))
15969 (time2 (org-time-string-to-time ts2))
15970 (t1 (org-float-time time1))
15971 (t2 (org-float-time time2))
15972 (diff (abs (- t2 t1)))
15973 (negative (< (- t2 t1) 0))
15974 ;; (ys (floor (* 365 24 60 60)))
15977 (fy "%dy %dd %02d:%02d")
15979 (fd "%dd %02d:%02d")
15984 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15986 d (floor (/ diff ds)) diff (mod diff ds)
15987 h (floor (/ diff hs)) diff (mod diff hs)
15988 m (floor (/ diff 60)))
15989 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15991 d (floor (+ (/ diff ds) 0.5))
15993 (if (not to-buffer)
15994 (message "%s" (org-make-tdiff-string y d h m))
15995 (if (org-at-table-p)
15997 (goto-char match-end)
15999 (and (looking-at " *|") (goto-char (match-end 0))))
16000 (goto-char match-end))
16002 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
16003 (replace-match ""))
16004 (if negative (insert " -"))
16005 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
16006 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
16007 (insert " " (format fh h m))))
16008 (if align (org-table-align))
16009 (message "Time difference inserted")))))
16011 (defun org-make-tdiff-string (y d h m)
16014 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
16016 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
16018 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
16020 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
16022 (apply 'format fmt (nreverse l))))
16024 (defun org-time-string-to-time (s &optional buffer pos)
16025 (condition-case errdata
16026 (apply 'encode-time (org-parse-time-string s))
16027 (error (error "Bad timestamp `%s'%s\nError was: %s"
16028 s (if (not (and buffer pos))
16030 (format " at %d in buffer `%s'" pos buffer))
16033 (defun org-time-string-to-seconds (s)
16034 (org-float-time (org-time-string-to-time s)))
16036 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
16037 "Convert a time stamp to an absolute day number.
16038 If there is a specifier for a cyclic time stamp, get the closest date to
16040 PREFER and SHOW-ALL are passed through to `org-closest-date'.
16041 The variable date is bound by the calendar when this is called."
16043 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
16044 (if (org-diary-sexp-entry (match-string 1 s) "" date)
16047 ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
16048 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
16049 (time-to-days (current-time))) (match-string 0 s)
16052 (condition-case errdata
16053 (apply 'encode-time (org-parse-time-string s))
16054 (error (error "Bad timestamp `%s'%s\nError was: %s"
16055 s (if (not (and buffer pos))
16057 (format " at %d in buffer `%s'" pos buffer))
16058 (cdr errdata))))))))
16060 (defun org-days-to-iso-week (days)
16061 "Return the iso week number."
16063 (car (calendar-iso-from-absolute days)))
16065 (defun org-small-year-to-year (year)
16066 "Convert 2-digit years into 4-digit years.
16067 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
16068 The year 2000 cannot be abbreviated. Any year larger than 99
16069 is returned unchanged."
16071 (setq year (+ 2000 year))
16073 (setq year (+ 1900 year))))
16076 (defun org-time-from-absolute (d)
16077 "Return the time corresponding to date D.
16078 D may be an absolute day number, or a calendar-type list (month day year)."
16079 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
16080 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
16082 (defun org-calendar-holiday ()
16083 "List of holidays, for Diary display in Org-mode."
16084 (require 'holidays)
16086 (if (fboundp 'calendar-check-holidays)
16087 'calendar-check-holidays 'check-calendar-holidays) date)))
16088 (if hl (mapconcat 'identity hl "; "))))
16090 (defun org-diary-sexp-entry (sexp entry date)
16091 "Process a SEXP diary ENTRY for DATE."
16092 (require 'diary-lib)
16093 (let ((result (if calendar-debug-sexp
16094 (let ((stack-trace-on-error t))
16095 (eval (car (read-from-string sexp))))
16096 (condition-case nil
16097 (eval (car (read-from-string sexp)))
16100 (message "Bad sexp at line %d in %s: %s"
16102 (buffer-file-name) sexp)
16104 (cond ((stringp result) (split-string result "; "))
16105 ((and (consp result)
16106 (not (consp (cdr result)))
16107 (stringp (cdr result))) (cdr result))
16108 ((and (consp result)
16109 (stringp (car result))) result)
16113 (defun org-diary-to-ical-string (frombuf)
16114 "Get iCalendar entries from diary entries in buffer FROMBUF.
16115 This uses the icalendar.el library."
16116 (let* ((tmpdir (if (featurep 'xemacs)
16118 temporary-file-directory))
16119 (tmpfile (make-temp-name
16120 (expand-file-name "orgics" tmpdir)))
16122 (with-current-buffer frombuf
16123 (icalendar-export-region (point-min) (point-max) tmpfile)
16124 (setq buf (find-buffer-visiting tmpfile))
16126 (goto-char (point-min))
16127 (if (re-search-forward "^BEGIN:VEVENT" nil t)
16128 (setq b (match-beginning 0)))
16129 (goto-char (point-max))
16130 (if (re-search-backward "^END:VEVENT" nil t)
16131 (setq e (match-end 0)))
16132 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
16134 (delete-file tmpfile)
16137 (defun org-closest-date (start current change prefer show-all)
16138 "Find the date closest to CURRENT that is consistent with START and CHANGE.
16139 When PREFER is `past', return a date that is either CURRENT or past.
16140 When PREFER is `future', return a date that is either CURRENT or future.
16141 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
16142 ;; Make the proper lists from the dates
16144 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
16145 dn dw sday cday n1 n2 n0
16146 d m y y1 y2 date1 date2 nmonths nm ny m2)
16148 (setq start (org-date-to-gregorian start)
16149 current (org-date-to-gregorian
16152 (time-to-days (current-time))))
16153 sday (calendar-absolute-from-gregorian start)
16154 cday (calendar-absolute-from-gregorian current))
16156 (if (<= cday sday) (throw 'exit sday))
16158 (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
16159 (setq dn (string-to-number (match-string 1 change))
16160 dw (cdr (assoc (match-string 2 change) a1)))
16161 (error "Invalid change specifier: %s" change))
16162 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
16165 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
16168 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
16169 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
16170 (setq date1 (list m d y1)
16171 n1 (calendar-absolute-from-gregorian date1)
16172 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
16173 n2 (calendar-absolute-from-gregorian date2)))
16175 ;; approx number of month between the two dates
16176 (setq nmonths (floor (/ (- cday sday) 30.436875)))
16177 ;; How often does dn fit in there?
16178 (setq d (nth 1 start) m (car start) y (nth 2 start)
16179 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
16181 ny (floor (/ m 12))
16184 (while (> m 12) (setq m (- m 12) y (1+ y)))
16185 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
16186 (setq m2 (+ m dn) y2 y)
16187 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16188 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
16189 (while (<= n2 cday)
16190 (setq n1 n2 m m2 y y2)
16191 (setq m2 (+ m dn) y2 y)
16192 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16193 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
16194 ;; Make sure n1 is the earlier date
16195 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
16198 ((eq prefer 'past) (if (= cday n2) n2 n1))
16199 ((eq prefer 'future) (if (= cday n1) n1 n2))
16200 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
16202 ((eq prefer 'past) (if (= cday n2) n2 n1))
16203 ((eq prefer 'future) (if (= cday n1) n1 n2))
16204 (t (if (= cday n1) n1 n2)))))))
16206 (defun org-date-to-gregorian (date)
16207 "Turn any specification of DATE into a Gregorian date for the calendar."
16208 (cond ((integerp date) (calendar-gregorian-from-absolute date))
16209 ((and (listp date) (= (length date) 3)) date)
16211 (setq date (org-parse-time-string date))
16212 (list (nth 4 date) (nth 3 date) (nth 5 date)))
16214 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
16216 (defun org-parse-time-string (s &optional nodefault)
16217 "Parse the standard Org-mode time string.
16218 This should be a lot faster than the normal `parse-time-string'.
16219 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
16220 hour and minute fields will be nil if not given."
16221 (if (string-match org-ts-regexp0 s)
16223 (if (or (match-beginning 8) (not nodefault))
16224 (string-to-number (or (match-string 8 s) "0")))
16225 (if (or (match-beginning 7) (not nodefault))
16226 (string-to-number (or (match-string 7 s) "0")))
16227 (string-to-number (match-string 4 s))
16228 (string-to-number (match-string 3 s))
16229 (string-to-number (match-string 2 s))
16231 (error "Not a standard Org-mode time string: %s" s)))
16233 (defun org-timestamp-up (&optional arg)
16234 "Increase the date item at the cursor by one.
16235 If the cursor is on the year, change the year. If it is on the month,
16236 the day or the time, change that.
16237 With prefix ARG, change by that many units."
16239 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
16241 (defun org-timestamp-down (&optional arg)
16242 "Decrease the date item at the cursor by one.
16243 If the cursor is on the year, change the year. If it is on the month,
16244 the day or the time, change that.
16245 With prefix ARG, change by that many units."
16247 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
16249 (defun org-timestamp-up-day (&optional arg)
16250 "Increase the date in the time stamp by one day.
16251 With prefix ARG, change that many days."
16253 (if (and (not (org-at-timestamp-p t))
16254 (org-at-heading-p))
16256 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
16258 (defun org-timestamp-down-day (&optional arg)
16259 "Decrease the date in the time stamp by one day.
16260 With prefix ARG, change that many days."
16262 (if (and (not (org-at-timestamp-p t))
16263 (org-at-heading-p))
16265 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
16267 (defun org-at-timestamp-p (&optional inactive-ok)
16268 "Determine if the cursor is in or at a timestamp."
16270 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
16272 (ans (or (looking-at tsr)
16274 (skip-chars-backward "^[<\n\r\t")
16275 (if (> (point) (point-min)) (backward-char 1))
16276 (and (looking-at tsr)
16277 (> (- (match-end 0) pos) -1))))))
16279 (boundp 'org-ts-what)
16282 ((= pos (match-beginning 0)) 'bracket)
16283 ;; Point is considered to be "on the bracket" whether
16284 ;; it's really on it or right after it.
16285 ((= pos (1- (match-end 0))) 'bracket)
16286 ((= pos (match-end 0)) 'after)
16287 ((org-pos-in-match-range pos 2) 'year)
16288 ((org-pos-in-match-range pos 3) 'month)
16289 ((org-pos-in-match-range pos 7) 'hour)
16290 ((org-pos-in-match-range pos 8) 'minute)
16291 ((or (org-pos-in-match-range pos 4)
16292 (org-pos-in-match-range pos 5)) 'day)
16293 ((and (> pos (or (match-end 8) (match-end 5)))
16294 (< pos (match-end 0)))
16295 (- pos (or (match-end 8) (match-end 5))))
16299 (defun org-toggle-timestamp-type ()
16300 "Toggle the type (<active> or [inactive]) of a time stamp."
16302 (when (org-at-timestamp-p t)
16303 (let ((beg (match-beginning 0)) (end (match-end 0))
16304 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
16307 (while (re-search-forward "[][<>]" end t)
16308 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
16310 (message "Timestamp is now %sactive"
16311 (if (equal (char-after beg) ?<) "" "in")))))
16313 (defvar org-clock-history) ; defined in org-clock.el
16314 (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
16315 (defun org-timestamp-change (n &optional what updown)
16316 "Change the date in the time stamp at point.
16317 The date will be changed by N times WHAT. WHAT can be `day', `month',
16318 `year', `minute', `second'. If WHAT is not given, the cursor position
16319 in the timestamp determines what will be changed."
16320 (let ((origin (point)) origin-cat
16322 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
16325 ts time time0 fixnext clrgx)
16326 (if (not (org-at-timestamp-p t))
16327 (error "Not at a timestamp"))
16328 (if (and (not what) (eq org-ts-what 'bracket))
16329 (org-toggle-timestamp-type)
16330 ;; Point isn't on brackets. Remember the part of the time-stamp
16331 ;; the point was in. Indeed, size of time-stamps may change,
16332 ;; but point must be kept in the same category nonetheless.
16333 (setq origin-cat org-ts-what)
16334 (if (and (not what) (not (eq org-ts-what 'day))
16335 org-display-custom-times
16336 (get-text-property (point) 'display)
16337 (not (get-text-property (1- (point)) 'display)))
16338 (setq org-ts-what 'day))
16339 (setq org-ts-what (or what org-ts-what)
16340 inactive (= (char-after (match-beginning 0)) ?\[)
16341 ts (match-string 0))
16344 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
16346 (setq extra (match-string 1 ts)))
16347 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
16349 (setq time0 (org-parse-time-string ts))
16351 (eq org-ts-what 'minute)
16352 (not current-prefix-arg))
16353 ;; This looks like s-up and s-down. Change by one rounding step.
16354 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
16355 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
16356 (setcar (cdr time0) (+ (nth 1 time0)
16357 (if (> n 0) (- rem) (- dm rem))))))
16359 (encode-time (or (car time0) 0)
16360 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
16361 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
16362 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
16363 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
16364 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
16366 (when (and (member org-ts-what '(hour minute))
16368 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
16369 (setq extra (org-modify-ts-extra
16371 (if (eq org-ts-what 'hour) 2 5)
16373 (when (integerp org-ts-what)
16374 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
16375 (if (eq what 'calendar)
16376 (let ((cal-date (org-get-date-from-calendar)))
16377 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
16378 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
16379 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
16380 (setcar time0 (or (car time0) 0))
16381 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
16382 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
16383 (setq time (apply 'encode-time time0))))
16384 ;; Insert the new time-stamp, and ensure point stays in the same
16385 ;; category as before (i.e. not after the last position in that
16387 (let ((pos (point)))
16388 ;; Stay before inserted string. `save-excursion' is of no use.
16389 (setq org-last-changed-timestamp
16390 (org-insert-time-stamp time with-hm inactive nil nil extra))
16393 (looking-at org-ts-regexp3)
16395 ;; `day' category ends before `hour' if any, or at
16396 ;; the end of the day name.
16397 ((eq origin-cat 'day)
16398 (min (or (match-beginning 7) (1- (match-end 5))) origin))
16399 ((eq origin-cat 'hour) (min (match-end 7) origin))
16400 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
16401 ((integerp origin-cat) (min (1- (match-end 0)) origin))
16402 ;; `year' and `month' have both fixed size: point
16403 ;; couldn't have moved into another part.
16405 ;; Update clock if on a CLOCK line.
16406 (org-clock-update-time-maybe)
16407 ;; Maybe adjust the closest clock in `org-clock-history'
16408 (when org-clock-adjust-closest
16409 (if (not (and (org-at-clock-log-p)
16410 (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
16411 org-clock-history))))))
16412 (message "No clock to adjust")
16413 (cond ((save-excursion ; fix previous clock?
16414 (re-search-backward org-ts-regexp0 nil t)
16415 (looking-back (concat org-clock-string " \\[")))
16416 (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
16417 ((save-excursion ; fix next clock?
16418 (re-search-backward org-ts-regexp0 nil t)
16419 (looking-at (concat org-ts-regexp0 "\\] =>")))
16420 (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
16421 (save-window-excursion
16422 ;; Find closest clock to point, adjust the previous/next one in history
16423 (let* ((p (save-excursion (org-back-to-heading t)))
16424 (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
16426 (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
16427 (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
16429 (message "No clock to adjust")
16430 (org-goto-marker-or-bmk clfixpos)
16432 (when (re-search-forward clrgx nil t)
16433 (goto-char (match-beginning 1))
16434 (let (org-clock-adjust-closest)
16435 (org-timestamp-change n org-ts-what updown))
16436 (message "Clock adjusted in %s for heading: %s"
16437 (file-name-nondirectory (buffer-file-name))
16438 (org-get-heading t t))))))))
16439 ;; Try to recenter the calendar window, if any.
16440 (if (and org-calendar-follow-timestamp-change
16441 (get-buffer-window "*Calendar*" t)
16442 (memq org-ts-what '(day month year)))
16443 (org-recenter-calendar (time-to-days time))))))
16445 (defun org-modify-ts-extra (s pos n dm)
16446 "Change the different parts of the lead-time and repeat fields in timestamp."
16447 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
16449 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
16451 ((or (org-pos-in-match-range pos 2)
16452 (org-pos-in-match-range pos 3))
16453 (setq m (string-to-number (match-string 3 s))
16454 h (string-to-number (match-string 2 s)))
16455 (if (org-pos-in-match-range pos 2)
16457 (setq n (* dm (org-no-warnings (signum n))))
16458 (when (not (= 0 (setq rem (% m dm))))
16459 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
16461 (if (< m 0) (setq m (+ m 60) h (1- h)))
16462 (if (> m 59) (setq m (- m 60) h (1+ h)))
16463 (setq h (min 24 (max 0 h)))
16464 (setq ng 1 new (format "-%02d:%02d" h m)))
16465 ((org-pos-in-match-range pos 6)
16466 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
16467 ((org-pos-in-match-range pos 5)
16468 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
16470 ((org-pos-in-match-range pos 9)
16471 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
16472 ((org-pos-in-match-range pos 8)
16473 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
16477 (substring s 0 (match-beginning ng))
16479 (substring s (match-end ng))))))
16482 (defun org-recenter-calendar (date)
16483 "If the calendar is visible, recenter it to DATE."
16484 (let ((cwin (get-buffer-window "*Calendar*" t)))
16486 (let ((calendar-move-hook nil))
16487 (with-selected-window cwin
16488 (calendar-goto-date (if (listp date) date
16489 (calendar-gregorian-from-absolute date))))))))
16491 (defun org-goto-calendar (&optional arg)
16492 "Go to the Emacs calendar at the current date.
16493 If there is a time stamp in the current line, go to that date.
16494 A prefix ARG can be used to force the current date."
16496 (let ((tsr org-ts-regexp) diff
16497 (calendar-move-hook nil)
16498 (calendar-view-holidays-initially-flag nil)
16499 (calendar-view-diary-initially-flag nil))
16500 (if (or (org-at-timestamp-p)
16502 (beginning-of-line 1)
16503 (looking-at (concat ".*" tsr))))
16504 (let ((d1 (time-to-days (current-time)))
16506 (org-time-string-to-time (match-string 1)))))
16507 (setq diff (- d2 d1))))
16509 (calendar-goto-today)
16510 (if (and diff (not arg)) (calendar-forward-day diff))))
16512 (defun org-get-date-from-calendar ()
16513 "Return a list (month day year) of date at point in calendar."
16514 (with-current-buffer "*Calendar*"
16516 (calendar-cursor-to-date))))
16518 (defun org-date-from-calendar ()
16519 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
16520 If there is already a time stamp at the cursor position, update it."
16522 (if (org-at-timestamp-p t)
16523 (org-timestamp-change 0 'calendar)
16524 (let ((cal-date (org-get-date-from-calendar)))
16525 (org-insert-time-stamp
16526 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
16528 (defun org-minutes-to-hh:mm-string (m)
16529 "Compute H:MM from a number of minutes."
16530 (let ((h (/ m 60)))
16531 (setq m (- m (* 60 h)))
16532 (format org-time-clocksum-format h m)))
16534 (defun org-hh:mm-string-to-minutes (s)
16535 "Convert a string H:MM to a number of minutes.
16536 If the string is just a number, interpret it as minutes.
16537 In fact, the first hh:mm or number in the string will be taken,
16538 there can be extra stuff in the string.
16539 If no number is found, the return value is 0."
16542 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
16543 (+ (* (string-to-number (match-string 1 s)) 60)
16544 (string-to-number (match-string 2 s))))
16545 ((string-match "\\([0-9]+\\)" s)
16546 (string-to-number (match-string 1 s)))
16549 (defcustom org-effort-durations
16552 ("w" . ,(* 60 8 5))
16553 ("m" . ,(* 60 8 5 4))
16554 ("y" . ,(* 60 8 5 40)))
16555 "Conversion factor to minutes for an effort modifier.
16557 Each entry has the form (MODIFIER . MINUTES).
16559 In an effort string, a number followed by MODIFIER is multiplied
16560 by the specified number of MINUTES to obtain an effort in
16563 For example, if the value of this variable is ((\"hours\" . 60)), then an
16564 effort string \"2hours\" is equivalent to 120 minutes."
16567 :type '(alist :key-type (string :tag "Modifier")
16568 :value-type (number :tag "Minutes")))
16570 (defun org-duration-string-to-minutes (s &optional output-to-string)
16571 "Convert a duration string S to minutes.
16573 A bare number is interpreted as minutes, modifiers can be set by
16574 customizing `org-effort-durations' (which see).
16576 Entries containing a colon are interpreted as H:MM by
16577 `org-hh:mm-string-to-minutes'."
16579 (re (concat "\\([0-9.]+\\) *\\("
16580 (regexp-opt (mapcar 'car org-effort-durations))
16582 (while (string-match re s)
16583 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
16584 (string-to-number (match-string 1 s))))
16585 (setq s (replace-match "" nil t s)))
16586 (setq result (floor result))
16587 (incf result (org-hh:mm-string-to-minutes s))
16588 (if output-to-string (number-to-string result) result)))
16592 (defun org-save-all-org-buffers ()
16593 "Save all Org-mode buffers without user confirmation."
16595 (message "Saving all Org-mode buffers...")
16596 (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
16597 (when (featurep 'org-id) (org-id-locations-save))
16598 (message "Saving all Org-mode buffers... done"))
16600 (defun org-revert-all-org-buffers ()
16601 "Revert all Org-mode buffers.
16602 Prompt for confirmation when there are unsaved changes.
16603 Be sure you know what you are doing before letting this function
16604 overwrite your changes.
16606 This function is useful in a setup where one tracks org files
16607 with a version control system, to revert on one machine after pulling
16608 changes from another. I believe the procedure must be like this:
16610 1. M-x org-save-all-org-buffers
16611 2. Pull changes from the other machine, resolve conflicts
16612 3. M-x org-revert-all-org-buffers"
16614 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
16617 (save-window-excursion
16620 (when (and (with-current-buffer b (derived-mode-p 'org-mode))
16621 (with-current-buffer b buffer-file-name))
16622 (org-pop-to-buffer-same-window b)
16623 (revert-buffer t 'no-confirm)))
16625 (when (and (featurep 'org-id) org-id-track-globally)
16626 (org-id-locations-load)))))
16631 (defun org-switchb (&optional arg)
16632 "Switch between Org buffers.
16633 With one prefix argument, restrict available buffers to files.
16634 With two prefix arguments, restrict available buffers to agenda files.
16636 Defaults to `iswitchb' for buffer name completion.
16637 Set `org-completion-use-ido' to make it use ido instead."
16639 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
16640 ((equal arg '(16)) (org-buffer-list 'agenda))
16641 (t (org-buffer-list))))
16642 (org-completion-use-iswitchb org-completion-use-iswitchb)
16643 (org-completion-use-ido org-completion-use-ido))
16644 (unless (or org-completion-use-ido org-completion-use-iswitchb)
16645 (setq org-completion-use-iswitchb t))
16646 (org-pop-to-buffer-same-window
16647 (org-icompleting-read "Org buffer: "
16648 (mapcar 'list (mapcar 'buffer-name blist))
16651 ;;; Define some older names previously used for this functionality
16653 (defalias 'org-ido-switchb 'org-switchb)
16655 (defalias 'org-iswitchb 'org-switchb)
16657 (defun org-buffer-list (&optional predicate exclude-tmp)
16658 "Return a list of Org buffers.
16659 PREDICATE can be `export', `files' or `agenda'.
16661 export restrict the list to Export buffers.
16662 files restrict the list to buffers visiting Org files.
16663 agenda restrict the list to buffers visiting agenda files.
16665 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
16667 (agenda-files (and (eq predicate 'agenda)
16668 (mapcar 'file-truename (org-agenda-files t))))
16671 ((eq predicate 'files)
16672 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
16673 ((eq predicate 'export)
16674 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
16675 ((eq predicate 'agenda)
16677 (with-current-buffer b
16678 (and (derived-mode-p 'org-mode)
16679 (setq bfn (buffer-file-name b))
16680 (member (file-truename bfn) agenda-files)))))
16681 (t (lambda (b) (with-current-buffer b
16682 (or (derived-mode-p 'org-mode)
16683 (string-match "\*Org .*Export"
16684 (buffer-name b)))))))))
16688 (if (and (funcall filter b)
16689 (or (not exclude-tmp)
16690 (not (string-match "tmp" (buffer-name b)))))
16695 (defun org-agenda-files (&optional unrestricted archives)
16696 "Get the list of agenda files.
16697 Optional UNRESTRICTED means return the full list even if a restriction
16698 is currently in place.
16699 When ARCHIVES is t, include all archive files that are really being
16700 used by the agenda files. If ARCHIVE is `ifmode', do this only if
16701 `org-agenda-archives-mode' is t."
16704 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
16705 ((stringp org-agenda-files) (org-read-agenda-file-list))
16706 ((listp org-agenda-files) org-agenda-files)
16707 (t (error "Invalid value of `org-agenda-files'")))))
16708 (setq files (apply 'append
16709 (mapcar (lambda (f)
16710 (if (file-directory-p f)
16712 f t org-agenda-file-regexp)
16715 (when org-agenda-skip-unavailable-files
16716 (setq files (delq nil
16719 (and (file-readable-p file) file)))
16721 (when (or (eq archives t)
16722 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
16723 (setq files (org-add-archive-files files)))
16726 (defun org-agenda-file-p (&optional file)
16727 "Return non-nil, if FILE is an agenda file.
16728 If FILE is omitted, use the file associated with the current
16730 (member (or file (buffer-file-name))
16731 (org-agenda-files t)))
16733 (defun org-edit-agenda-file-list ()
16734 "Edit the list of agenda files.
16735 Depending on setup, this either uses customize to edit the variable
16736 `org-agenda-files', or it visits the file that is holding the list. In the
16737 latter case, the buffer is set up in a way that saving it automatically kills
16738 the buffer and restores the previous window configuration."
16740 (if (stringp org-agenda-files)
16741 (let ((cw (current-window-configuration)))
16742 (find-file org-agenda-files)
16743 (org-set-local 'org-window-configuration cw)
16744 (org-add-hook 'after-save-hook
16746 (set-window-configuration
16747 (prog1 org-window-configuration
16748 (kill-buffer (current-buffer))))
16749 (org-install-agenda-files-menu)
16750 (message "New agenda file list installed"))
16752 (message "%s" (substitute-command-keys
16753 "Edit list and finish with \\[save-buffer]")))
16754 (customize-variable 'org-agenda-files)))
16756 (defun org-store-new-agenda-file-list (list)
16757 "Set new value for the agenda file list and save it correctly."
16758 (if (stringp org-agenda-files)
16759 (let ((fe (org-read-agenda-file-list t)) b u)
16760 (while (setq b (find-buffer-visiting org-agenda-files))
16762 (with-temp-file org-agenda-files
16765 (lambda (f) ;; Keep un-expanded entries.
16766 (if (setq u (assoc f fe))
16771 (let ((org-mode-hook nil) (org-inhibit-startup t)
16772 (org-insert-mode-line-in-empty-file nil))
16773 (setq org-agenda-files list)
16774 (customize-save-variable 'org-agenda-files org-agenda-files))))
16776 (defun org-read-agenda-file-list (&optional pair-with-expansion)
16777 "Read the list of agenda files from a file.
16778 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
16779 filenames, used by `org-store-new-agenda-file-list' to write back
16780 un-expanded file names."
16781 (when (file-directory-p org-agenda-files)
16782 (error "`org-agenda-files' cannot be a single directory"))
16783 (when (stringp org-agenda-files)
16785 (insert-file-contents org-agenda-files)
16788 (let ((e (expand-file-name (substitute-in-file-name f)
16790 (if pair-with-expansion
16793 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
16796 (defun org-cycle-agenda-files ()
16797 "Cycle through the files in `org-agenda-files'.
16798 If the current buffer visits an agenda file, find the next one in the list.
16799 If the current buffer does not, find the first agenda file."
16801 (let* ((fs (org-agenda-files t))
16802 (files (append fs (list (car fs))))
16803 (tcf (if buffer-file-name (file-truename buffer-file-name)))
16805 (unless files (error "No agenda files"))
16807 (while (setq file (pop files))
16808 (if (equal (file-truename file) tcf)
16810 (find-file (car files))
16812 (find-file (car fs)))
16813 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
16815 (defun org-agenda-file-to-front (&optional to-end)
16816 "Move/add the current file to the top of the agenda file list.
16817 If the file is not present in the list, it is added to the front. If it is
16818 present, it is moved there. With optional argument TO-END, add/move to the
16821 (let ((org-agenda-skip-unavailable-files nil)
16822 (file-alist (mapcar (lambda (x)
16823 (cons (file-truename x) x))
16824 (org-agenda-files t)))
16825 (ctf (file-truename buffer-file-name))
16827 (setq x (assoc ctf file-alist) had x)
16829 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
16831 (setq file-alist (append (delq x file-alist) (list x)))
16832 (setq file-alist (cons x (delq x file-alist))))
16833 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
16834 (org-install-agenda-files-menu)
16835 (message "File %s to %s of agenda file list"
16836 (if had "moved" "added") (if to-end "end" "front"))))
16838 (defun org-remove-file (&optional file)
16839 "Remove current file from the list of files in variable `org-agenda-files'.
16840 These are the files which are being checked for agenda entries.
16841 Optional argument FILE means use this file instead of the current."
16843 (let* ((org-agenda-skip-unavailable-files nil)
16844 (file (or file buffer-file-name))
16845 (true-file (file-truename file))
16846 (afile (abbreviate-file-name file))
16847 (files (delq nil (mapcar
16849 (if (equal true-file
16852 (org-agenda-files t)))))
16853 (if (not (= (length files) (length (org-agenda-files t))))
16855 (org-store-new-agenda-file-list files)
16856 (org-install-agenda-files-menu)
16857 (message "Removed file: %s" afile))
16858 (message "File was not in list: %s (not removed)" afile))))
16860 (defun org-file-menu-entry (file)
16861 (vector file (list 'find-file file) t))
16863 (defun org-check-agenda-file (file)
16864 "Make sure FILE exists. If not, ask user what to do."
16865 (when (not (file-exists-p file))
16866 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
16867 (abbreviate-file-name file))
16868 (let ((r (downcase (read-char-exclusive))))
16871 (org-remove-file file)
16872 (throw 'nextfile t))
16873 (t (error "Abort"))))))
16875 (defun org-get-agenda-file-buffer (file)
16876 "Get a buffer visiting FILE. If the buffer needs to be created, add
16877 it to the list of buffers which might be released later."
16878 (let ((buf (org-find-base-buffer-visiting file)))
16880 buf ; just return it
16881 ;; Make a new buffer and remember it
16882 (setq buf (find-file-noselect file))
16883 (if buf (push buf org-agenda-new-buffers))
16886 (defun org-release-buffers (blist)
16887 "Release all buffers in list, asking the user for confirmation when needed.
16888 When a buffer is unmodified, it is just killed. When modified, it is saved
16889 \(if the user agrees) and then killed."
16891 (while (setq buf (pop blist))
16892 (setq file (buffer-file-name buf))
16893 (when (and (buffer-modified-p buf)
16895 (y-or-n-p (format "Save file %s? " file)))
16896 (with-current-buffer buf (save-buffer)))
16897 (kill-buffer buf))))
16899 (defun org-prepare-agenda-buffers (files)
16900 "Create buffers for all agenda files, protect archived trees and comments."
16902 (let ((pa '(:org-archived t))
16903 (pc '(:org-comment t))
16904 (pall '(:org-archived t :org-comment t))
16905 (inhibit-read-only t)
16906 (rea (concat ":" org-archive-tag ":"))
16910 (while (setq file (pop files))
16914 (org-check-agenda-file file)
16915 (set-buffer (org-get-agenda-file-buffer file)))
16917 (setq bmp (buffer-modified-p))
16918 (org-refresh-category-properties)
16919 (setq org-todo-keywords-for-agenda
16920 (append org-todo-keywords-for-agenda org-todo-keywords-1))
16921 (setq org-done-keywords-for-agenda
16922 (append org-done-keywords-for-agenda org-done-keywords))
16923 (setq org-todo-keyword-alist-for-agenda
16924 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
16925 (setq org-drawers-for-agenda
16926 (append org-drawers-for-agenda org-drawers))
16927 (setq org-tag-alist-for-agenda
16928 (append org-tag-alist-for-agenda org-tag-alist))
16931 (remove-text-properties (point-min) (point-max) pall)
16932 (when org-agenda-skip-archived-trees
16933 (goto-char (point-min))
16934 (while (re-search-forward rea nil t)
16935 (if (org-at-heading-p t)
16936 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
16937 (goto-char (point-min))
16938 (setq re (format org-heading-keyword-regexp-format
16939 org-comment-string))
16940 (while (re-search-forward re nil t)
16941 (add-text-properties
16942 (match-beginning 0) (org-end-of-subtree t) pc)))
16943 (set-buffer-modified-p bmp)))))
16944 (setq org-todo-keywords-for-agenda
16945 (org-uniquify org-todo-keywords-for-agenda))
16946 (setq org-todo-keyword-alist-for-agenda
16947 (org-uniquify org-todo-keyword-alist-for-agenda)
16948 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
16950 ;;;; Embedded LaTeX
16952 (defvar org-cdlatex-mode-map (make-sparse-keymap)
16953 "Keymap for the minor `org-cdlatex-mode'.")
16955 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
16956 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
16957 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
16958 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
16959 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
16961 (defvar org-cdlatex-texmathp-advice-is-done nil
16962 "Flag remembering if we have applied the advice to texmathp already.")
16964 (define-minor-mode org-cdlatex-mode
16965 "Toggle the minor `org-cdlatex-mode'.
16966 This mode supports entering LaTeX environment and math in LaTeX fragments
16968 \\{org-cdlatex-mode-map}"
16970 (when org-cdlatex-mode
16972 (run-hooks 'cdlatex-mode-hook)
16973 (cdlatex-compute-tables))
16974 (unless org-cdlatex-texmathp-advice-is-done
16975 (setq org-cdlatex-texmathp-advice-is-done t)
16976 (defadvice texmathp (around org-math-always-on activate)
16977 "Always return t in org-mode buffers.
16978 This is because we want to insert math symbols without dollars even outside
16979 the LaTeX math segments. If Orgmode thinks that point is actually inside
16980 an embedded LaTeX fragment, let texmathp do its job.
16981 \\[org-cdlatex-mode-map]"
16985 ((not (derived-mode-p 'org-mode)) ad-do-it)
16986 ((eq this-command 'cdlatex-math-symbol)
16987 (setq ad-return-value t
16988 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
16990 (let ((p (org-inside-LaTeX-fragment-p)))
16991 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
16992 (setq ad-return-value t
16993 texmathp-why '("Org-mode embedded math" . 0))
16994 (if p ad-do-it)))))))))
16996 (defun turn-on-org-cdlatex ()
16997 "Unconditionally turn on `org-cdlatex-mode'."
16998 (org-cdlatex-mode 1))
17000 (defun org-inside-LaTeX-fragment-p ()
17001 "Test if point is inside a LaTeX fragment.
17002 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
17003 sequence appearing also before point.
17004 Even though the matchers for math are configurable, this function assumes
17005 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
17006 delimiters are skipped when they have been removed by customization.
17007 The return value is nil, or a cons cell with the delimiter and the
17008 position of this delimiter.
17010 This function does a reasonably good job, but can locally be fooled by
17011 for example currency specifications. For example it will assume being in
17012 inline math after \"$22.34\". The LaTeX fragment formatter will only format
17013 fragments that are properly closed, but during editing, we have to live
17014 with the uncertainty caused by missing closing delimiters. This function
17015 looks only before point, not after."
17017 (let ((pos (point))
17018 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
17020 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
17022 dd-on str (start 0) m re)
17025 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
17026 re (nth 1 (assoc "$" org-latex-regexps)))
17027 (while (string-match re str start)
17029 ((= (match-end 0) (length str))
17030 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
17031 ((= (match-end 0) (- (length str) 5))
17033 (t (setq start (match-end 0))))))
17034 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
17036 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
17037 (and (match-beginning 2) (throw 'exit nil))
17039 (while (re-search-backward "\\$\\$" lim t)
17040 (setq dd-on (not dd-on)))
17042 (if dd-on (cons "$$" m))))))
17044 (defun org-inside-latex-macro-p ()
17045 "Is point inside a LaTeX macro or its arguments?"
17048 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
17050 (defun org-try-cdlatex-tab ()
17051 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
17052 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
17053 - inside a LaTeX fragment, or
17054 - after the first word in a line, where an abbreviation expansion could
17055 insert a LaTeX environment."
17056 (when org-cdlatex-mode
17058 ;; Before any word on the line: No expansion possible.
17059 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
17060 ;; Just after first word on the line: Expand it. Make sure it
17061 ;; cannot happen on headlines, though.
17063 (skip-chars-backward "a-zA-Z0-9*")
17064 (skip-chars-backward " \t")
17065 (and (bolp) (not (org-at-heading-p))))
17067 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
17069 (defun org-cdlatex-underscore-caret (&optional arg)
17070 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
17071 Revert to the normal definition outside of these fragments."
17073 (if (org-inside-LaTeX-fragment-p)
17074 (call-interactively 'cdlatex-sub-superscript)
17075 (let (org-cdlatex-mode)
17076 (call-interactively (key-binding (vector last-input-event))))))
17078 (defun org-cdlatex-math-modify (&optional arg)
17079 "Execute `cdlatex-math-modify' in LaTeX fragments.
17080 Revert to the normal definition outside of these fragments."
17082 (if (org-inside-LaTeX-fragment-p)
17083 (call-interactively 'cdlatex-math-modify)
17084 (let (org-cdlatex-mode)
17085 (call-interactively (key-binding (vector last-input-event))))))
17087 (defvar org-latex-fragment-image-overlays nil
17088 "List of overlays carrying the images of latex fragments.")
17089 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
17091 (defun org-remove-latex-fragment-image-overlays ()
17092 "Remove all overlays with LaTeX fragment images in current buffer."
17093 (mapc 'delete-overlay org-latex-fragment-image-overlays)
17094 (setq org-latex-fragment-image-overlays nil))
17096 (defun org-preview-latex-fragment (&optional subtree)
17097 "Preview the LaTeX fragment at point, or all locally or globally.
17098 If the cursor is in a LaTeX fragment, create the image and overlay
17099 it over the source code. If there is no fragment at point, display
17100 all fragments in the current text, from one headline to the next. With
17101 prefix SUBTREE, display all fragments in the current subtree. With a
17102 double prefix arg \\[universal-argument] \\[universal-argument], or when \
17103 the cursor is before the first headline,
17104 display all fragments in the buffer.
17105 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
17107 (unless buffer-file-name
17108 (error "Can't preview LaTeX fragment in a non-file buffer"))
17109 (org-remove-latex-fragment-image-overlays)
17112 (let (beg end at msg)
17114 ((or (equal subtree '(16))
17115 (not (save-excursion
17116 (re-search-backward org-outline-regexp-bol nil t))))
17117 (setq beg (point-min) end (point-max)
17118 msg "Creating images for buffer...%s"))
17119 ((equal subtree '(4))
17120 (org-back-to-heading)
17121 (setq beg (point) end (org-end-of-subtree t)
17122 msg "Creating images for subtree...%s"))
17124 (if (setq at (org-inside-LaTeX-fragment-p))
17125 (goto-char (max (point-min) (- (cdr at) 2)))
17126 (org-back-to-heading))
17127 (setq beg (point) end (progn (outline-next-heading) (point))
17128 msg (if at "Creating image...%s"
17129 "Creating images for entry...%s"))))
17131 (narrow-to-region beg end)
17134 (concat "ltxpng/" (file-name-sans-extension
17135 (file-name-nondirectory
17136 buffer-file-name)))
17137 default-directory 'overlays msg at 'forbuffer
17138 org-latex-create-formula-image-program)
17139 (message msg "done. Use `C-c C-c' to remove images.")))))
17141 (defvar org-latex-regexps
17142 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
17143 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
17144 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
17145 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17146 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17147 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
17148 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
17149 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
17150 "Regular expressions for matching embedded LaTeX.")
17152 (defvar org-export-have-math nil) ;; dynamic scoping
17153 (defun org-format-latex (prefix &optional dir overlays msg at
17154 forbuffer processing-type)
17155 "Replace LaTeX fragments with links to an image, and produce images.
17156 Some of the options can be changed using the variable
17157 `org-format-latex-options'."
17158 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
17159 (let* ((prefixnodir (file-name-nondirectory prefix))
17160 (absprefix (expand-file-name prefix dir))
17161 (todir (file-name-directory absprefix))
17162 (opt org-format-latex-options)
17163 (matchers (plist-get opt :matchers))
17164 (re-list org-latex-regexps)
17165 (org-format-latex-header-extra
17166 (plist-get (org-infile-export-plist) :latex-header-extra))
17167 (cnt 0) txt hash link beg end re e checkdir
17168 executables-checked string
17169 m n block-type block linkfile movefile ov)
17170 ;; Check the different regular expressions
17171 (while (setq e (pop re-list))
17172 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
17173 block (if block-type "\n\n" ""))
17174 (when (member m matchers)
17175 (goto-char (point-min))
17176 (while (re-search-forward re nil t)
17177 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
17178 (not (get-text-property (match-beginning n)
17181 (not (eq (get-char-property (match-beginning n)
17183 'org-latex-overlay))))
17184 (setq org-export-have-math t)
17186 ((eq processing-type 'verbatim)
17187 ;; Leave the text verbatim, just protect it
17188 (add-text-properties (match-beginning n) (match-end n)
17189 '(org-protected t)))
17190 ((eq processing-type 'mathjax)
17191 ;; Prepare for MathJax processing
17192 (setq string (match-string n))
17193 (if (member m '("$" "$1"))
17195 (delete-region (match-beginning n) (match-end n))
17196 (goto-char (match-beginning n))
17197 (insert (org-add-props (concat "\\(" (substring string 1 -1)
17199 '(org-protected t))))
17200 (add-text-properties (match-beginning n) (match-end n)
17201 '(org-protected t))))
17202 ((or (eq processing-type 'dvipng)
17203 (eq processing-type 'imagemagick))
17204 ;; Process to an image
17205 (setq txt (match-string n)
17206 beg (match-beginning n) end (match-end n)
17208 (let (print-length print-level) ; make sure full list is printed
17209 (setq hash (sha1 (prin1-to-string
17210 (list org-format-latex-header
17211 org-format-latex-header-extra
17212 org-export-latex-default-packages-alist
17213 org-export-latex-packages-alist
17214 org-format-latex-options
17216 linkfile (format "%s_%s.png" prefix hash)
17217 movefile (format "%s_%s.png" absprefix hash)))
17218 (setq link (concat block "[[file:" linkfile "]]" block))
17219 (if msg (message msg cnt))
17221 (unless checkdir ; make sure the directory exists
17223 (or (file-directory-p todir) (make-directory todir t)))
17225 ((eq processing-type 'dvipng)
17226 (unless executables-checked
17227 (org-check-external-command
17228 "latex" "needed to convert LaTeX fragments to images")
17229 (org-check-external-command
17230 "dvipng" "needed to convert LaTeX fragments to images")
17231 (setq executables-checked t))
17232 (unless (file-exists-p movefile)
17233 (org-create-formula-image-with-dvipng
17234 txt movefile opt forbuffer)))
17235 ((eq processing-type 'imagemagick)
17236 (unless executables-checked
17237 (org-check-external-command
17238 "convert" "you need to install imagemagick")
17239 (setq executables-checked t))
17240 (unless (file-exists-p movefile)
17241 (org-create-formula-image-with-imagemagick
17242 txt movefile opt forbuffer))))
17246 (if (eq (overlay-get o 'org-overlay-type)
17247 'org-latex-overlay)
17248 (delete-overlay o)))
17249 (overlays-in beg end))
17250 (setq ov (make-overlay beg end))
17251 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
17252 (if (featurep 'xemacs)
17254 (overlay-put ov 'invisible t)
17257 (make-glyph (vector 'png :file movefile))))
17260 (list 'image :type 'png :file movefile :ascent 'center)))
17261 (push ov org-latex-fragment-image-overlays)
17263 (delete-region beg end)
17264 (insert (org-add-props link
17265 (list 'org-latex-src
17266 (replace-regexp-in-string
17268 'org-latex-src-embed-type
17269 (if block-type 'paragraph 'character))))))
17270 ((eq processing-type 'mathml)
17271 ;; Process to MathML
17272 (unless executables-checked
17273 (unless (save-match-data (org-format-latex-mathml-available-p))
17274 (error "LaTeX to MathML converter not configured"))
17275 (setq executables-checked t))
17276 (setq txt (match-string n)
17277 beg (match-beginning n) end (match-end n)
17279 (if msg (message msg cnt))
17281 (delete-region beg end)
17282 (insert (org-format-latex-as-mathml
17283 txt block-type prefix dir)))
17285 (error "Unknown conversion type %s for latex fragments"
17286 processing-type)))))))))
17288 (defun org-create-math-formula (latex-frag &optional mathml-file)
17289 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
17290 Use `org-latex-to-mathml-convert-command'. If the conversion is
17291 sucessful, return the portion between \"<math...> </math>\"
17292 elements otherwise return nil. When MATHML-FILE is specified,
17293 write the results in to that file. When invoked as an
17294 interactive command, prompt for LATEX-FRAG, with initial value
17295 set to the current active region and echo the results for user
17297 (interactive (list (let ((frag (when (region-active-p)
17298 (buffer-substring-no-properties
17299 (region-beginning) (region-end)))))
17300 (read-string "LaTeX Fragment: " frag nil frag))))
17301 (unless latex-frag (error "Invalid latex-frag"))
17302 (let* ((tmp-in-file (file-relative-name
17303 (make-temp-name (expand-file-name "ltxmathml-in"))))
17304 (ignore (write-region latex-frag nil tmp-in-file))
17305 (tmp-out-file (file-relative-name
17306 (make-temp-name (expand-file-name "ltxmathml-out"))))
17308 org-latex-to-mathml-convert-command
17309 `((?j . ,(shell-quote-argument
17310 (expand-file-name org-latex-to-mathml-jar-file)))
17311 (?I . ,(shell-quote-argument tmp-in-file))
17312 (?o . ,(shell-quote-argument tmp-out-file)))))
17313 mathml shell-command-output)
17314 (when (org-called-interactively-p 'any)
17315 (unless (org-format-latex-mathml-available-p)
17316 (error "LaTeX to MathML converter not configured")))
17317 (message "Running %s" cmd)
17318 (setq shell-command-output (shell-command-to-string cmd))
17320 (when (file-readable-p tmp-out-file)
17321 (with-current-buffer (find-file-noselect tmp-out-file t)
17322 (goto-char (point-min))
17323 (when (re-search-forward
17326 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
17328 (regexp-quote "</math>")) nil t)
17329 (prog1 (match-string 0) (kill-buffer))))))
17333 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
17335 (write-region mathml nil mathml-file))
17336 (when (org-called-interactively-p 'any)
17338 ((message "LaTeX to MathML conversion failed")
17339 (message shell-command-output)))
17340 (delete-file tmp-in-file)
17341 (when (file-exists-p tmp-out-file)
17342 (delete-file tmp-out-file))
17345 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
17346 prefix &optional dir)
17347 "Use `org-create-math-formula' but check local cache first."
17348 (let* ((absprefix (expand-file-name prefix dir))
17349 (print-length nil) (print-level nil)
17350 (formula-id (concat
17355 org-latex-to-mathml-convert-command)))))
17356 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
17357 (formula-cache-dir (file-name-directory formula-cache)))
17359 (unless (file-directory-p formula-cache-dir)
17360 (make-directory formula-cache-dir t))
17362 (unless (file-exists-p formula-cache)
17363 (org-create-math-formula latex-frag formula-cache))
17365 (if (file-exists-p formula-cache)
17366 ;; Successful conversion. Return the link to MathML file.
17368 (format "[[file:%s]]" (file-relative-name formula-cache dir))
17369 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
17370 'org-latex-src-embed-type (if latex-frag-type
17371 'paragraph 'character)))
17372 ;; Failed conversion. Return the LaTeX fragment verbatim
17373 (add-text-properties
17374 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
17377 ;; This function borrows from Ganesh Swami's latex2png.el
17378 (defun org-create-formula-image-with-dvipng (string tofile options buffer)
17379 "This calls dvipng."
17380 (require 'org-latex)
17381 (let* ((tmpdir (if (featurep 'xemacs)
17383 temporary-file-directory))
17384 (texfilebase (make-temp-name
17385 (expand-file-name "orgtex" tmpdir)))
17386 (texfile (concat texfilebase ".tex"))
17387 (dvifile (concat texfilebase ".dvi"))
17388 (pngfile (concat texfilebase ".png"))
17389 (fnh (if (featurep 'xemacs)
17390 (font-height (face-font 'default))
17391 (face-attribute 'default :height nil)))
17392 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17393 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17394 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17396 (bg (or (plist-get options (if buffer :background :html-background))
17398 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
17399 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
17400 (with-temp-file texfile
17401 (insert (org-splice-latex-header
17402 org-format-latex-header
17403 org-export-latex-default-packages-alist
17404 org-export-latex-packages-alist t
17405 org-format-latex-header-extra))
17406 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
17407 (require 'org-latex)
17408 (org-export-latex-fix-inputenc))
17409 (let ((dir default-directory))
17410 (condition-case nil
17413 (call-process "latex" nil nil nil texfile))
17416 (if (not (file-exists-p dvifile))
17417 (progn (message "Failed to create dvi file from %s" texfile) nil)
17418 (condition-case nil
17419 (if (featurep 'xemacs)
17420 (call-process "dvipng" nil nil nil
17425 (call-process "dvipng" nil nil nil
17428 ;;"-x" scale "-y" scale
17433 (if (not (file-exists-p pngfile))
17434 (if org-format-latex-signal-error
17435 (error "Failed to create png file from %s" texfile)
17436 (message "Failed to create png file from %s" texfile)
17438 ;; Use the requested file name and clean up
17439 (copy-file pngfile tofile 'replace)
17440 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
17441 (if (file-exists-p (concat texfilebase e))
17442 (delete-file (concat texfilebase e))))
17445 (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
17446 (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
17447 "This calls convert, which is included into imagemagick."
17448 (require 'org-latex)
17449 (let* ((tmpdir (if (featurep 'xemacs)
17451 temporary-file-directory))
17452 (texfilebase (make-temp-name
17453 (expand-file-name "orgtex" tmpdir)))
17454 (texfile (concat texfilebase ".tex"))
17455 (pdffile (concat texfilebase ".pdf"))
17456 (pngfile (concat texfilebase ".png"))
17457 (fnh (if (featurep 'xemacs)
17458 (font-height (face-font 'default))
17459 (face-attribute 'default :height nil)))
17460 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17461 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17462 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17464 (bg (or (plist-get options (if buffer :background :html-background))
17466 (if (eq fg 'default) (setq fg (org-latex-color :foreground))
17467 (setq fg (org-latex-color-format fg)))
17468 (if (eq bg 'default) (setq bg (org-latex-color :background))
17469 (setq bg (org-latex-color-format
17470 (if (string= bg "Transparent")(setq bg "white")))))
17471 (with-temp-file texfile
17472 (insert (org-splice-latex-header
17473 org-format-latex-header
17474 org-export-latex-default-packages-alist
17475 org-export-latex-packages-alist t
17476 org-format-latex-header-extra))
17477 (insert "\n\\begin{document}\n"
17478 "\\definecolor{fg}{rgb}{" fg "}\n"
17479 "\\definecolor{bg}{rgb}{" bg "}\n"
17480 "\n\\pagecolor{bg}\n"
17484 "\n\\end{document}\n" )
17485 (require 'org-latex)
17486 (org-export-latex-fix-inputenc))
17487 (let ((dir default-directory) cmd cmds latex-frags-cmds)
17488 (condition-case nil
17491 (setq cmds org-latex-to-pdf-process)
17493 (setq latex-frags-cmds (pop cmds))
17494 (if (listp latex-frags-cmds)
17496 (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
17497 (while latex-frags-cmds
17498 (setq cmd (pop latex-frags-cmds))
17499 (while (string-match "%b" cmd)
17500 (setq cmd (replace-match
17502 (shell-quote-argument texfile))
17504 (while (string-match "%f" cmd)
17505 (setq cmd (replace-match
17507 (shell-quote-argument (file-name-nondirectory texfile)))
17509 (while (string-match "%o" cmd)
17510 (setq cmd (replace-match
17512 (shell-quote-argument (file-name-directory texfile)))
17514 (setq cmd (split-string cmd))
17515 (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
17518 (if (not (file-exists-p pdffile))
17519 (progn (message "Failed to create pdf file from %s" texfile) nil)
17520 (condition-case nil
17521 (if (featurep 'xemacs)
17522 (call-process "convert" nil nil nil
17528 ;; "-sharpen" "0x1.0"
17530 (call-process "convert" nil nil nil
17536 ; "-sharpen" "0x1.0"
17539 (if (not (file-exists-p pngfile))
17540 (if org-format-latex-signal-error
17541 (error "Failed to create png file from %s" texfile)
17542 (message "Failed to create png file from %s" texfile)
17544 ;; Use the requested file name and clean up
17545 (copy-file pngfile tofile 'replace)
17546 (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
17547 (if (file-exists-p (concat texfilebase e))
17548 (delete-file (concat texfilebase e))))
17551 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
17552 "Fill a LaTeX header template TPL.
17553 In the template, the following place holders will be recognized:
17555 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
17556 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
17557 [PACKAGES] \\usepackage statements for PKG
17558 [NO-PACKAGES] do not include PKG
17559 [EXTRA] the string EXTRA
17560 [NO-EXTRA] do not include EXTRA
17562 For backward compatibility, if both the positive and the negative place
17563 holder is missing, the positive one (without the \"NO-\") will be
17564 assumed to be present at the end of the template.
17565 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
17567 SNIPPETS-P indicates if this is run to create snippet images for HTML."
17568 (let (rpl (end ""))
17569 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
17570 (setq rpl (if (or (match-end 1) (not def-pkg))
17571 "" (org-latex-packages-to-string def-pkg snippets-p t))
17572 tpl (replace-match rpl t t tpl))
17573 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
17575 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
17576 (setq rpl (if (or (match-end 1) (not pkg))
17577 "" (org-latex-packages-to-string pkg snippets-p t))
17578 tpl (replace-match rpl t t tpl))
17581 (org-latex-packages-to-string pkg snippets-p)))))
17583 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
17584 (setq rpl (if (or (match-end 1) (not extra))
17585 "" (concat extra "\n"))
17586 tpl (replace-match rpl t t tpl))
17587 (if (and extra (string-match "\\S-" extra))
17588 (setq end (concat end "\n" extra))))
17590 (if (string-match "\\S-" end)
17591 (concat tpl "\n" end)
17594 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
17595 "Turn an alist of packages into a string with the \\usepackage macros."
17596 (setq pkg (mapconcat (lambda(p)
17599 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
17600 (format "%% Package %s omitted" (cadr p)))
17601 ((equal "" (car p))
17602 (format "\\usepackage{%s}" (cadr p)))
17604 (format "\\usepackage[%s]{%s}"
17605 (car p) (cadr p)))))
17608 (if newline (concat pkg "\n") pkg))
17610 (defun org-dvipng-color (attr)
17611 "Return a RGB color specification for dvipng."
17612 (apply 'format "rgb %s %s %s"
17613 (mapcar 'org-normalize-color
17614 (if (featurep 'xemacs)
17615 (color-rgb-components
17616 (face-property 'default
17617 (cond ((eq attr :foreground) 'foreground)
17618 ((eq attr :background) 'background))))
17619 (color-values (face-attribute 'default attr nil))))))
17621 (defun org-latex-color (attr)
17622 "Return a RGB color for the LaTeX color package."
17623 (apply 'format "%s,%s,%s"
17624 (mapcar 'org-normalize-color
17625 (if (featurep 'xemacs)
17626 (color-rgb-components
17627 (face-property 'default
17628 (cond ((eq attr :foreground) 'foreground)
17629 ((eq attr :background) 'background))))
17630 (color-values (face-attribute 'default attr nil))))))
17632 (defun org-latex-color-format (color-name)
17633 "Convert COLOR-NAME to a RGB color value."
17634 (apply 'format "%s,%s,%s"
17635 (mapcar 'org-normalize-color
17636 (color-values color-name))))
17638 (defun org-normalize-color (value)
17639 "Return string to be used as color value for an RGB component."
17640 (format "%g" (/ value 65535.0)))
17645 (defvar org-inline-image-overlays nil)
17646 (make-variable-buffer-local 'org-inline-image-overlays)
17648 (defun org-toggle-inline-images (&optional include-linked)
17649 "Toggle the display of inline images.
17650 INCLUDE-LINKED is passed to `org-display-inline-images'."
17652 (if org-inline-image-overlays
17654 (org-remove-inline-images)
17655 (message "Inline image display turned off"))
17656 (org-display-inline-images include-linked)
17657 (if org-inline-image-overlays
17658 (message "%d images displayed inline"
17659 (length org-inline-image-overlays))
17660 (message "No images to display inline"))))
17662 (defun org-display-inline-images (&optional include-linked refresh beg end)
17663 "Display inline images.
17664 Normally only links without a description part are inlined, because this
17665 is how it will work for export. When INCLUDE-LINKED is set, also links
17666 with a description part will be inlined. This can be nice for a quick
17667 look at those images, but it does not reflect what exported files will look
17669 When REFRESH is set, refresh existing images between BEG and END.
17670 This will create new image displays only if necessary.
17671 BEG and END default to the buffer boundaries."
17674 (org-remove-inline-images)
17675 (if (fboundp 'clear-image-cache) (clear-image-cache)))
17679 (setq beg (or beg (point-min)) end (or end (point-max)))
17681 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
17682 (substring (org-image-file-name-regexp) 0 -2)
17683 "\\)\\]" (if include-linked "" "\\]")))
17685 (while (re-search-forward re end t)
17686 (setq old (get-char-property-and-overlay (match-beginning 1)
17687 'org-image-overlay))
17688 (setq file (expand-file-name
17689 (concat (or (match-string 3) "") (match-string 4))))
17690 (when (file-exists-p file)
17691 (if (and (car-safe old) refresh)
17692 (image-refresh (overlay-get (cdr old) 'display))
17693 (setq img (save-match-data (create-image file)))
17695 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
17696 (overlay-put ov 'display img)
17697 (overlay-put ov 'face 'default)
17698 (overlay-put ov 'org-image-overlay t)
17699 (overlay-put ov 'modification-hooks
17700 (list 'org-display-inline-modification-hook))
17701 (push ov org-inline-image-overlays)))))))))
17703 (defun org-display-inline-modification-hook (ov after beg end &optional len)
17704 "Remove inline-display overlay if a corresponding region is modified."
17705 (let ((inhibit-modification-hooks t))
17706 (when (and ov after)
17707 (delete ov org-inline-image-overlays)
17708 (delete-overlay ov))))
17710 (defun org-remove-inline-images ()
17711 "Remove inline display of images."
17713 (mapc 'delete-overlay org-inline-image-overlays)
17714 (setq org-inline-image-overlays nil))
17718 ;; Outline functions from `outline-mode-prefix-map'
17719 ;; that can be remapped in Org:
17720 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
17721 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
17722 (define-key org-mode-map [remap outline-forward-same-level]
17723 'org-forward-same-level)
17724 (define-key org-mode-map [remap outline-backward-same-level]
17725 'org-backward-same-level)
17726 (define-key org-mode-map [remap show-branches]
17727 'org-kill-note-or-show-branches)
17728 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
17729 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
17730 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
17732 ;; Outline functions from `outline-mode-prefix-map' that can not
17733 ;; be remapped in Org:
17735 ;; - the column "key binding" shows whether the Outline function is still
17736 ;; available in Org mode on the same key that it has been bound to in
17738 ;; - "overridden": key used for a different functionality in Org mode
17739 ;; - else: key still bound to the same Outline function in Org mode
17741 ;; | Outline function | key binding | Org replacement |
17742 ;; |------------------------------------+-------------+-----------------------|
17743 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
17744 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
17745 ;; | `outline-up-heading' | `C-c C-u' | still same function |
17746 ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
17747 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
17748 ;; | `show-entry' | overridden | no replacement |
17749 ;; | `show-children' | `C-c C-i' | visibility cycling |
17750 ;; | `show-branches' | `C-c C-k' | still same function |
17751 ;; | `show-subtree' | overridden | visibility cycling |
17752 ;; | `show-all' | overridden | no replacement |
17753 ;; | `hide-subtree' | overridden | visibility cycling |
17754 ;; | `hide-body' | overridden | no replacement |
17755 ;; | `hide-entry' | overridden | visibility cycling |
17756 ;; | `hide-leaves' | overridden | no replacement |
17757 ;; | `hide-sublevels' | overridden | no replacement |
17758 ;; | `hide-other' | overridden | no replacement |
17760 ;; Make `C-c C-x' a prefix key
17761 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
17763 ;; TAB key with modifiers
17764 (org-defkey org-mode-map "\C-i" 'org-cycle)
17765 (org-defkey org-mode-map [(tab)] 'org-cycle)
17766 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
17767 (org-defkey org-mode-map "\M-\t" 'pcomplete)
17768 ;; The following line is necessary under Suse GNU/Linux
17769 (unless (featurep 'xemacs)
17770 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
17771 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
17772 (define-key org-mode-map [backtab] 'org-shifttab)
17774 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
17775 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
17776 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
17778 ;; Cursor keys with modifiers
17779 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
17780 (org-defkey org-mode-map [(meta right)] 'org-metaright)
17781 (org-defkey org-mode-map [(meta up)] 'org-metaup)
17782 (org-defkey org-mode-map [(meta down)] 'org-metadown)
17784 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
17785 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
17786 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
17787 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
17789 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
17790 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
17791 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
17792 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
17794 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
17795 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
17796 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
17797 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
17800 (define-key org-mode-map org-babel-key-prefix org-babel-map)
17801 (mapc (lambda (pair)
17802 (define-key org-babel-map (car pair) (cdr pair)))
17803 org-babel-key-bindings)
17805 ;;; Extra keys for tty access.
17806 ;; We only set them when really needed because otherwise the
17807 ;; menus don't show the simple keys
17809 (when (or org-use-extra-keys
17810 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
17811 (not window-system))
17812 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
17813 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
17814 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
17815 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
17816 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
17817 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
17818 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
17819 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
17820 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
17821 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
17822 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
17823 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
17824 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
17825 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
17826 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
17827 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
17828 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
17829 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
17830 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
17831 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
17832 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
17833 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
17834 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
17835 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
17836 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
17837 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
17838 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
17839 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
17841 ;; All the other keys
17843 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
17844 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
17845 (if (boundp 'narrow-map)
17846 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
17847 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
17848 (if (boundp 'narrow-map)
17849 (org-defkey narrow-map "b" 'org-narrow-to-block)
17850 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
17851 (if (boundp 'narrow-map)
17852 (org-defkey narrow-map "e" 'org-narrow-to-element)
17853 (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
17854 (org-defkey org-mode-map "\C-\M-t" 'org-element-transpose)
17855 (org-defkey org-mode-map "\M-}" 'org-element-forward)
17856 (org-defkey org-mode-map "\M-{" 'org-element-backward)
17857 (org-defkey org-mode-map "\C-c\C-^" 'org-element-up)
17858 (org-defkey org-mode-map "\C-c\C-_" 'org-element-down)
17859 (org-defkey org-mode-map "\C-\M-t" 'org-element-transpose)
17860 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
17861 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
17862 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
17863 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
17864 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
17865 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
17866 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
17867 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
17868 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
17869 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
17870 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
17871 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
17872 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
17873 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
17874 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
17875 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
17876 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
17877 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
17878 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
17879 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
17880 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
17881 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
17882 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
17883 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
17884 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
17885 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
17886 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
17887 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
17888 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
17889 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
17890 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
17891 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
17892 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
17893 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
17894 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
17895 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
17896 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
17897 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
17898 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
17899 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
17900 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
17901 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
17902 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
17903 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
17904 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
17905 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
17906 (org-defkey org-mode-map "\C-c^" 'org-sort)
17907 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
17908 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
17909 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
17910 (org-defkey org-mode-map "\C-m" 'org-return)
17911 (org-defkey org-mode-map "\C-j" 'org-return-indent)
17912 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
17913 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
17914 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
17915 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
17916 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
17917 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
17918 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
17919 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
17920 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
17921 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
17922 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
17923 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
17924 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
17925 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
17926 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
17927 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
17928 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
17929 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
17930 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
17931 (org-defkey org-mode-map "\C-c\C-@" 'org-element-mark-element)
17932 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
17933 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
17935 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
17936 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
17937 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
17938 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
17940 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
17941 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
17942 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
17943 (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
17944 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
17945 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
17946 (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
17947 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
17948 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
17949 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
17950 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
17951 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
17952 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
17953 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
17954 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
17955 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
17956 (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
17957 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
17958 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
17959 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
17960 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
17962 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
17963 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
17964 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
17965 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
17966 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
17968 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
17970 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
17972 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
17973 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
17975 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
17978 (when (featurep 'xemacs)
17979 (org-defkey org-mode-map 'button3 'popup-mode-menu))
17982 (defconst org-speed-commands-default
17984 ("Outline Navigation")
17985 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
17986 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
17987 ("f" . (org-speed-move-safe 'org-forward-same-level))
17988 ("b" . (org-speed-move-safe 'org-backward-same-level))
17989 ("u" . (org-speed-move-safe 'outline-up-heading))
17991 ("g" . (org-refile t))
17992 ("Outline Visibility")
17994 ("C" . org-shifttab)
17995 (" " . org-display-outline-path)
17996 (":" . org-columns)
17997 ("Outline Structure Editing")
17998 ("U" . org-shiftmetaup)
17999 ("D" . org-shiftmetadown)
18000 ("r" . org-metaright)
18001 ("l" . org-metaleft)
18002 ("R" . org-shiftmetaright)
18003 ("L" . org-shiftmetaleft)
18004 ("i" . (progn (forward-char 1) (call-interactively
18005 'org-insert-heading-respect-content)))
18008 ("a" . org-archive-subtree-default-with-confirmation)
18009 ("." . org-mark-subtree)
18010 ("#" . org-toggle-comment)
18012 ("I" . org-clock-in)
18013 ("O" . org-clock-out)
18014 ("Meta Data Editing")
18016 ("0" . (org-priority ?\ ))
18017 ("1" . (org-priority ?A))
18018 ("2" . (org-priority ?B))
18019 ("3" . (org-priority ?C))
18020 (";" . org-set-tags-command)
18021 ("e" . org-set-effort)
18022 ("E" . org-inc-effort)
18023 ("W" . (lambda(m) (interactive "sMinutes before warning: ")
18024 (org-entry-put (point) "APPT_WARNTIME" m)))
18025 ("Agenda Views etc")
18027 ("/" . org-sparse-tree)
18029 ("o" . org-open-at-point)
18030 ("?" . org-speed-command-help)
18031 ("<" . (org-agenda-set-restriction-lock 'subtree))
18032 (">" . (org-agenda-remove-restriction-lock))
18034 "The default speed commands.")
18036 (defun org-print-speed-command (e)
18037 (if (> (length (car e)) 1)
18042 (princ (make-string (length (car e)) ?-))
18046 (if (symbolp (cdr e))
18047 (princ (symbol-name (cdr e)))
18051 (defun org-speed-command-help ()
18052 "Show the available speed commands."
18054 (if (not org-use-speed-commands)
18055 (error "Speed commands are not activated, customize `org-use-speed-commands'")
18056 (with-output-to-temp-buffer "*Help*"
18057 (princ "User-defined Speed commands\n===========================\n")
18058 (mapc 'org-print-speed-command org-speed-commands-user)
18060 (princ "Built-in Speed commands\n=======================\n")
18061 (mapc 'org-print-speed-command org-speed-commands-default))
18062 (with-current-buffer "*Help*"
18063 (setq truncate-lines t))))
18065 (defun org-speed-move-safe (cmd)
18066 "Execute CMD, but make sure that the cursor always ends up in a headline.
18067 If not, return to the original position and throw an error."
18069 (let ((pos (point)))
18070 (call-interactively cmd)
18071 (unless (and (bolp) (org-at-heading-p))
18073 (error "Boundary reached while executing %s" cmd))))
18075 (defvar org-self-insert-command-undo-counter 0)
18077 (defvar org-table-auto-blank-field) ; defined in org-table.el
18078 (defvar org-speed-command nil)
18080 (defun org-speed-command-default-hook (keys)
18081 "Hook for activating single-letter speed commands.
18082 `org-speed-commands-default' specifies a minimal command set.
18083 Use `org-speed-commands-user' for further customization."
18084 (when (or (and (bolp) (looking-at org-outline-regexp))
18085 (and (functionp org-use-speed-commands)
18086 (funcall org-use-speed-commands)))
18087 (cdr (assoc keys (append org-speed-commands-user
18088 org-speed-commands-default)))))
18090 (defun org-babel-speed-command-hook (keys)
18091 "Hook for activating single-letter code block commands."
18092 (when (and (bolp) (looking-at org-babel-src-block-regexp))
18093 (cdr (assoc keys org-babel-key-bindings))))
18095 (defcustom org-speed-command-hook
18096 '(org-speed-command-default-hook org-babel-speed-command-hook)
18097 "Hook for activating speed commands at strategic locations.
18098 Hook functions are called in sequence until a valid handler is
18101 Each hook takes a single argument, a user-pressed command key
18102 which is also a `self-insert-command' from the global map.
18104 Within the hook, examine the cursor position and the command key
18105 and return nil or a valid handler as appropriate. Handler could
18106 be one of an interactive command, a function, or a form.
18108 Set `org-use-speed-commands' to non-nil value to enable this
18109 hook. The default setting is `org-speed-command-default-hook'."
18110 :group 'org-structure
18114 (defun org-self-insert-command (N)
18115 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
18116 If the cursor is in a table looking at whitespace, the whitespace is
18117 overwritten, and the table is not marked as requiring realignment."
18119 (org-check-before-invisible-edit 'insert)
18121 ((and org-use-speed-commands
18122 (setq org-speed-command
18123 (run-hook-with-args-until-success
18124 'org-speed-command-hook (this-command-keys))))
18126 ((commandp org-speed-command)
18127 (setq this-command org-speed-command)
18128 (call-interactively org-speed-command))
18129 ((functionp org-speed-command)
18130 (funcall org-speed-command))
18131 ((and org-speed-command (listp org-speed-command))
18132 (eval org-speed-command))
18133 (t (let (org-use-speed-commands)
18134 (call-interactively 'org-self-insert-command)))))
18138 ;; check if we blank the field, and if that triggers align
18139 (and (featurep 'org-table) org-table-auto-blank-field
18140 (member last-command
18141 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
18142 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
18143 ;; got extra space, this field does not determine column width
18144 (let (org-table-may-need-update) (org-table-blank-field))
18145 ;; no extra space, this field may determine column width
18146 (org-table-blank-field)))
18149 (looking-at "[^|\n]* |"))
18150 (let (org-table-may-need-update)
18151 (goto-char (1- (match-end 0)))
18152 (backward-delete-char 1)
18153 (goto-char (match-beginning 0))
18154 (self-insert-command N)))
18156 (setq org-table-may-need-update t)
18157 (self-insert-command N)
18158 (org-fix-tags-on-the-fly)
18159 (if org-self-insert-cluster-for-undo
18160 (if (not (eq last-command 'org-self-insert-command))
18161 (setq org-self-insert-command-undo-counter 1)
18162 (if (>= org-self-insert-command-undo-counter 20)
18163 (setq org-self-insert-command-undo-counter 1)
18164 (and (> org-self-insert-command-undo-counter 0)
18165 buffer-undo-list (listp buffer-undo-list)
18166 (not (cadr buffer-undo-list)) ; remove nil entry
18167 (setcdr buffer-undo-list (cddr buffer-undo-list)))
18168 (setq org-self-insert-command-undo-counter
18169 (1+ org-self-insert-command-undo-counter))))))))
18171 (defun org-check-before-invisible-edit (kind)
18172 "Check is editing if kind KIND would be dangerous with invisible text around.
18173 The detailed reaction depends on the user option `org-catch-invisible-edits'."
18174 ;; First, try to get out of here as quickly as possible, to reduce overhead
18175 (if (and org-catch-invisible-edits
18176 (or (not (boundp 'visible-mode)) (not visible-mode))
18177 (or (get-char-property (point) 'invisible)
18178 (get-char-property (max (point-min) (1- (point))) 'invisible)))
18179 ;; OK, we need to take a closer look
18180 (let* ((invisible-at-point (get-char-property (point) 'invisible))
18181 (invisible-before-point (if (bobp) nil (get-char-property
18182 (1- (point)) 'invisible)))
18183 (border-and-ok-direction
18185 ;; Check if we are acting predictably before invisible text
18186 (and invisible-at-point (not invisible-before-point)
18187 (memq kind '(insert delete-backward)))
18188 ;; Check if we are acting predictably after invisible text
18189 ;; This works not well, and I have turned it off. It seems
18190 ;; better to always show and stop after invisible text.
18191 ;; (and (not invisible-at-point) invisible-before-point
18192 ;; (memq kind '(insert delete)))
18195 (when (or (memq invisible-at-point '(outline org-hide-block))
18196 (memq invisible-before-point '(outline org-hide-block)))
18197 (if (eq org-catch-invisible-edits 'error)
18198 (error "Editing in invisible areas is prohibited - make visible first"))
18199 ;; Make the area visible
18201 (if invisible-before-point
18202 (goto-char (previous-single-char-property-change
18203 (point) 'invisible)))
18206 ((eq org-catch-invisible-edits 'show)
18207 ;; That's it, we do the edit after showing
18209 "Unfolding invisible region around point before editing")
18211 ((and (eq org-catch-invisible-edits 'smart)
18212 border-and-ok-direction)
18213 (message "Unfolding invisible region around point before editing"))
18215 ;; Don't do the edit, make the user repeat it in full visibility
18216 (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
18218 (defun org-fix-tags-on-the-fly ()
18219 (when (and (equal (char-after (point-at-bol)) ?*)
18220 (org-at-heading-p))
18221 (org-align-tags-here org-tags-column)))
18223 (defun org-delete-backward-char (N)
18224 "Like `delete-backward-char', insert whitespace at field end in tables.
18225 When deleting backwards, in tables this function will insert whitespace in
18226 front of the next \"|\" separator, to keep the table aligned. The table will
18227 still be marked for re-alignment if the field did fill the entire column,
18228 because, in this case the deletion might narrow the column."
18230 (org-check-before-invisible-edit 'delete-backward)
18231 (if (and (org-table-p)
18233 (string-match "|" (buffer-substring (point-at-bol) (point)))
18234 (looking-at ".*?|"))
18235 (let ((pos (point))
18236 (noalign (looking-at "[^|\n\r]* |"))
18237 (c org-table-may-need-update))
18238 (backward-delete-char N)
18239 (if (not overwrite-mode)
18241 (skip-chars-forward "^|")
18243 (goto-char (1- pos))))
18244 ;; noalign: if there were two spaces at the end, this field
18245 ;; does not determine the width of the column.
18246 (if noalign (setq org-table-may-need-update c)))
18247 (backward-delete-char N)
18248 (org-fix-tags-on-the-fly)))
18250 (defun org-delete-char (N)
18251 "Like `delete-char', but insert whitespace at field end in tables.
18252 When deleting characters, in tables this function will insert whitespace in
18253 front of the next \"|\" separator, to keep the table aligned. The table will
18254 still be marked for re-alignment if the field did fill the entire column,
18255 because, in this case the deletion might narrow the column."
18257 (org-check-before-invisible-edit 'delete)
18258 (if (and (org-table-p)
18260 (not (= (char-after) ?|))
18262 (if (looking-at ".*?|")
18263 (let ((pos (point))
18264 (noalign (looking-at "[^|\n\r]* |"))
18265 (c org-table-may-need-update))
18266 (replace-match (concat
18267 (substring (match-string 0) 1 -1)
18270 ;; noalign: if there were two spaces at the end, this field
18271 ;; does not determine the width of the column.
18272 (if noalign (setq org-table-may-need-update c)))
18275 (org-fix-tags-on-the-fly)))
18277 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
18278 (put 'org-self-insert-command 'delete-selection t)
18279 (put 'orgtbl-self-insert-command 'delete-selection t)
18280 (put 'org-delete-char 'delete-selection 'supersede)
18281 (put 'org-delete-backward-char 'delete-selection 'supersede)
18282 (put 'org-yank 'delete-selection 'yank)
18284 ;; Make `flyspell-mode' delay after some commands
18285 (put 'org-self-insert-command 'flyspell-delayed t)
18286 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
18287 (put 'org-delete-char 'flyspell-delayed t)
18288 (put 'org-delete-backward-char 'flyspell-delayed t)
18290 ;; Make pabbrev-mode expand after org-mode commands
18291 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
18292 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
18294 ;; How to do this: Measure non-white length of current string
18295 ;; If equal to column width, we should realign.
18297 (defun org-remap (map &rest commands)
18298 "In MAP, remap the functions given in COMMANDS.
18299 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
18302 (setq old (pop commands) new (pop commands))
18303 (if (fboundp 'command-remapping)
18304 (org-defkey map (vector 'remap old) new)
18305 (substitute-key-definition old new map global-map)))))
18307 (when (eq org-enable-table-editor 'optimized)
18308 ;; If the user wants maximum table support, we need to hijack
18309 ;; some standard editing functions
18310 (org-remap org-mode-map
18311 'self-insert-command 'org-self-insert-command
18312 'delete-char 'org-delete-char
18313 'delete-backward-char 'org-delete-backward-char)
18314 (org-defkey org-mode-map "|" 'org-force-self-insert))
18316 (defvar org-ctrl-c-ctrl-c-hook nil
18317 "Hook for functions attaching themselves to `C-c C-c'.
18319 This can be used to add additional functionality to the C-c C-c
18320 key which executes context-dependent commands. This hook is run
18321 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
18322 run after the last test.
18324 Each function will be called with no arguments. The function
18325 must check if the context is appropriate for it to act. If yes,
18326 it should do its thing and then return a non-nil value. If the
18327 context is wrong, just do nothing and return nil.")
18329 (defvar org-ctrl-c-ctrl-c-final-hook nil
18330 "Hook for functions attaching themselves to `C-c C-c'.
18332 This can be used to add additional functionality to the C-c C-c
18333 key which executes context-dependent commands. This hook is run
18334 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
18335 before the first test.
18337 Each function will be called with no arguments. The function
18338 must check if the context is appropriate for it to act. If yes,
18339 it should do its thing and then return a non-nil value. If the
18340 context is wrong, just do nothing and return nil.")
18342 (defvar org-tab-first-hook nil
18343 "Hook for functions to attach themselves to TAB.
18344 See `org-ctrl-c-ctrl-c-hook' for more information.
18345 This hook runs as the first action when TAB is pressed, even before
18346 `org-cycle' messes around with the `outline-regexp' to cater for
18347 inline tasks and plain list item folding.
18348 If any function in this hook returns t, any other actions that
18349 would have been caused by TAB (such as table field motion or visibility
18350 cycling) will not occur.")
18352 (defvar org-tab-after-check-for-table-hook nil
18353 "Hook for functions to attach themselves to TAB.
18354 See `org-ctrl-c-ctrl-c-hook' for more information.
18355 This hook runs after it has been established that the cursor is not in a
18356 table, but before checking if the cursor is in a headline or if global cycling
18358 If any function in this hook returns t, not other actions like visibility
18359 cycling will be done.")
18361 (defvar org-tab-after-check-for-cycling-hook nil
18362 "Hook for functions to attach themselves to TAB.
18363 See `org-ctrl-c-ctrl-c-hook' for more information.
18364 This hook runs after it has been established that not table field motion and
18365 not visibility should be done because of current context. This is probably
18366 the place where a package like yasnippets can hook in.")
18368 (defvar org-tab-before-tab-emulation-hook nil
18369 "Hook for functions to attach themselves to TAB.
18370 See `org-ctrl-c-ctrl-c-hook' for more information.
18371 This hook runs after every other options for TAB have been exhausted, but
18372 before indentation and \t insertion takes place.")
18374 (defvar org-metaleft-hook nil
18375 "Hook for functions attaching themselves to `M-left'.
18376 See `org-ctrl-c-ctrl-c-hook' for more information.")
18377 (defvar org-metaright-hook nil
18378 "Hook for functions attaching themselves to `M-right'.
18379 See `org-ctrl-c-ctrl-c-hook' for more information.")
18380 (defvar org-metaup-hook nil
18381 "Hook for functions attaching themselves to `M-up'.
18382 See `org-ctrl-c-ctrl-c-hook' for more information.")
18383 (defvar org-metadown-hook nil
18384 "Hook for functions attaching themselves to `M-down'.
18385 See `org-ctrl-c-ctrl-c-hook' for more information.")
18386 (defvar org-shiftmetaleft-hook nil
18387 "Hook for functions attaching themselves to `M-S-left'.
18388 See `org-ctrl-c-ctrl-c-hook' for more information.")
18389 (defvar org-shiftmetaright-hook nil
18390 "Hook for functions attaching themselves to `M-S-right'.
18391 See `org-ctrl-c-ctrl-c-hook' for more information.")
18392 (defvar org-shiftmetaup-hook nil
18393 "Hook for functions attaching themselves to `M-S-up'.
18394 See `org-ctrl-c-ctrl-c-hook' for more information.")
18395 (defvar org-shiftmetadown-hook nil
18396 "Hook for functions attaching themselves to `M-S-down'.
18397 See `org-ctrl-c-ctrl-c-hook' for more information.")
18398 (defvar org-metareturn-hook nil
18399 "Hook for functions attaching themselves to `M-RET'.
18400 See `org-ctrl-c-ctrl-c-hook' for more information.")
18401 (defvar org-shiftup-hook nil
18402 "Hook for functions attaching themselves to `S-up'.
18403 See `org-ctrl-c-ctrl-c-hook' for more information.")
18404 (defvar org-shiftup-final-hook nil
18405 "Hook for functions attaching themselves to `S-up'.
18406 This one runs after all other options except shift-select have been excluded.
18407 See `org-ctrl-c-ctrl-c-hook' for more information.")
18408 (defvar org-shiftdown-hook nil
18409 "Hook for functions attaching themselves to `S-down'.
18410 See `org-ctrl-c-ctrl-c-hook' for more information.")
18411 (defvar org-shiftdown-final-hook nil
18412 "Hook for functions attaching themselves to `S-down'.
18413 This one runs after all other options except shift-select have been excluded.
18414 See `org-ctrl-c-ctrl-c-hook' for more information.")
18415 (defvar org-shiftleft-hook nil
18416 "Hook for functions attaching themselves to `S-left'.
18417 See `org-ctrl-c-ctrl-c-hook' for more information.")
18418 (defvar org-shiftleft-final-hook nil
18419 "Hook for functions attaching themselves to `S-left'.
18420 This one runs after all other options except shift-select have been excluded.
18421 See `org-ctrl-c-ctrl-c-hook' for more information.")
18422 (defvar org-shiftright-hook nil
18423 "Hook for functions attaching themselves to `S-right'.
18424 See `org-ctrl-c-ctrl-c-hook' for more information.")
18425 (defvar org-shiftright-final-hook nil
18426 "Hook for functions attaching themselves to `S-right'.
18427 This one runs after all other options except shift-select have been excluded.
18428 See `org-ctrl-c-ctrl-c-hook' for more information.")
18430 (defun org-modifier-cursor-error ()
18431 "Throw an error, a modified cursor command was applied in wrong context."
18432 (error "This command is active in special context like tables, headlines or items"))
18434 (defun org-shiftselect-error ()
18435 "Throw an error because Shift-Cursor command was applied in wrong context."
18436 (if (and (boundp 'shift-select-mode) shift-select-mode)
18437 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
18438 (error "This command works only in special context like headlines or timestamps")))
18440 (defun org-call-for-shift-select (cmd)
18441 (let ((this-command-keys-shift-translated t))
18442 (call-interactively cmd)))
18444 (defun org-shifttab (&optional arg)
18445 "Global visibility cycling or move to previous table field.
18446 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
18448 See the individual commands for more information."
18451 ((org-at-table-p) (call-interactively 'org-table-previous-field))
18453 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
18454 (message "Content view to level: %d" arg)
18455 (org-content (prefix-numeric-value arg2))
18456 (setq org-cycle-global-status 'overview)))
18457 (t (call-interactively 'org-global-cycle))))
18459 (defun org-shiftmetaleft ()
18460 "Promote subtree or delete table column.
18461 Calls `org-promote-subtree', `org-outdent-item-tree', or
18462 `org-table-delete-column', depending on context. See the
18463 individual commands for more information."
18466 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
18467 ((org-at-table-p) (call-interactively 'org-table-delete-column))
18468 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
18469 ((if (not (org-region-active-p)) (org-at-item-p)
18470 (save-excursion (goto-char (region-beginning))
18472 (call-interactively 'org-outdent-item-tree))
18473 (t (org-modifier-cursor-error))))
18475 (defun org-shiftmetaright ()
18476 "Demote subtree or insert table column.
18477 Calls `org-demote-subtree', `org-indent-item-tree', or
18478 `org-table-insert-column', depending on context. See the
18479 individual commands for more information."
18482 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
18483 ((org-at-table-p) (call-interactively 'org-table-insert-column))
18484 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
18485 ((if (not (org-region-active-p)) (org-at-item-p)
18486 (save-excursion (goto-char (region-beginning))
18488 (call-interactively 'org-indent-item-tree))
18489 (t (org-modifier-cursor-error))))
18491 (defun org-shiftmetaup (&optional arg)
18492 "Move subtree up or kill table row.
18493 Calls `org-move-subtree-up' or `org-table-kill-row' or
18494 `org-move-item-up' or `org-timestamp-up', depending on context.
18495 See the individual commands for more information."
18498 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
18499 ((org-at-table-p) (call-interactively 'org-table-kill-row))
18500 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18501 ((org-at-item-p) (call-interactively 'org-move-item-up))
18502 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18503 (call-interactively 'org-timestamp-up)))
18504 (t (org-modifier-cursor-error))))
18506 (defun org-shiftmetadown (&optional arg)
18507 "Move subtree down or insert table row.
18508 Calls `org-move-subtree-down' or `org-table-insert-row' or
18509 `org-move-item-down' or `org-timestamp-up', depending on context.
18510 See the individual commands for more information."
18513 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
18514 ((org-at-table-p) (call-interactively 'org-table-insert-row))
18515 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18516 ((org-at-item-p) (call-interactively 'org-move-item-down))
18517 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18518 (call-interactively 'org-timestamp-down)))
18519 (t (org-modifier-cursor-error))))
18521 (defsubst org-hidden-tree-error ()
18523 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
18525 (defun org-metaleft (&optional arg)
18526 "Promote heading or move table column to left.
18527 Calls `org-do-promote' or `org-table-move-column', depending on context.
18528 With no specific context, calls the Emacs default `backward-word'.
18529 See the individual commands for more information."
18532 ((run-hook-with-args-until-success 'org-metaleft-hook))
18533 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
18534 ((org-with-limited-levels
18535 (or (org-at-heading-p)
18536 (and (org-region-active-p)
18538 (goto-char (region-beginning))
18539 (org-at-heading-p)))))
18540 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18541 (call-interactively 'org-do-promote))
18542 ;; At an inline task.
18543 ((org-at-heading-p)
18544 (call-interactively 'org-inlinetask-promote))
18545 ((or (org-at-item-p)
18546 (and (org-region-active-p)
18548 (goto-char (region-beginning))
18550 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18551 (call-interactively 'org-outdent-item))
18552 (t (call-interactively 'backward-word))))
18554 (defun org-metaright (&optional arg)
18555 "Demote a subtree, a list item or move table column to right.
18556 In front of a drawer or a block keyword, indent it correctly.
18557 With no specific context, calls the Emacs default `forward-word'.
18558 See the individual commands for more information."
18561 ((run-hook-with-args-until-success 'org-metaright-hook))
18562 ((org-at-table-p) (call-interactively 'org-table-move-column))
18563 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
18564 ((org-at-block-p) (call-interactively 'org-indent-block))
18565 ((org-with-limited-levels
18566 (or (org-at-heading-p)
18567 (and (org-region-active-p)
18569 (goto-char (region-beginning))
18570 (org-at-heading-p)))))
18571 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18572 (call-interactively 'org-do-demote))
18573 ;; At an inline task.
18574 ((org-at-heading-p)
18575 (call-interactively 'org-inlinetask-demote))
18576 ((or (org-at-item-p)
18577 (and (org-region-active-p)
18579 (goto-char (region-beginning))
18581 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18582 (call-interactively 'org-indent-item))
18583 (t (call-interactively 'forward-word))))
18585 (defun org-check-for-hidden (what)
18586 "Check if there are hidden headlines/items in the current visual line.
18587 WHAT can be either `headlines' or `items'. If the current line is
18588 an outline or item heading and it has a folded subtree below it,
18589 this function returns t, nil otherwise."
18591 ((eq what 'headlines) org-outline-regexp-bol)
18592 ((eq what 'items) (org-item-beginning-re))
18593 (t (error "This should not happen"))))
18597 (unless (org-region-active-p)
18598 (setq beg (point-at-bol))
18599 (beginning-of-line 2)
18600 (while (and (not (eobp)) ;; this is like `next-line'
18601 (get-char-property (1- (point)) 'invisible))
18602 (beginning-of-line 2))
18605 (goto-char (point-at-eol))
18606 (setq end (max end (point)))
18607 (while (re-search-forward re end t)
18608 (if (get-char-property (match-beginning 0) 'invisible)
18612 (defun org-metaup (&optional arg)
18613 "Move subtree up or move table row up.
18614 Calls `org-move-subtree-up' or `org-table-move-row' or
18615 `org-move-item-up', depending on context. See the individual commands
18616 for more information."
18619 ((run-hook-with-args-until-success 'org-metaup-hook))
18620 ((org-region-active-p)
18621 (let* ((a (min (region-beginning) (region-end)))
18622 (b (1- (max (region-beginning) (region-end))))
18623 (c (save-excursion (goto-char a)
18624 (move-beginning-of-line 0)))
18625 (d (save-excursion (goto-char a)
18626 (move-end-of-line 0) (point))))
18627 (transpose-regions a b c d)
18629 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
18630 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18631 ((org-at-item-p) (call-interactively 'org-move-item-up))
18632 (t (org-element-drag-backward))))
18634 (defun org-metadown (&optional arg)
18635 "Move subtree down or move table row down.
18636 Calls `org-move-subtree-down' or `org-table-move-row' or
18637 `org-move-item-down', depending on context. See the individual
18638 commands for more information."
18641 ((run-hook-with-args-until-success 'org-metadown-hook))
18642 ((org-region-active-p)
18643 (let* ((a (min (region-beginning) (region-end)))
18644 (b (max (region-beginning) (region-end)))
18645 (c (save-excursion (goto-char b)
18646 (move-beginning-of-line 1)))
18647 (d (save-excursion (goto-char b)
18648 (move-end-of-line 1) (1+ (point)))))
18649 (transpose-regions a b c d)
18651 ((org-at-table-p) (call-interactively 'org-table-move-row))
18652 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18653 ((org-at-item-p) (call-interactively 'org-move-item-down))
18654 (t (org-element-drag-forward))))
18656 (defun org-shiftup (&optional arg)
18657 "Increase item in timestamp or increase priority of current headline.
18658 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
18659 depending on context. See the individual commands for more information."
18662 ((run-hook-with-args-until-success 'org-shiftup-hook))
18663 ((and org-support-shift-select (org-region-active-p))
18664 (org-call-for-shift-select 'previous-line))
18665 ((org-at-timestamp-p t)
18666 (call-interactively (if org-edit-timestamp-down-means-later
18667 'org-timestamp-down 'org-timestamp-up)))
18668 ((and (not (eq org-support-shift-select 'always))
18669 org-enable-priority-commands
18670 (org-at-heading-p))
18671 (call-interactively 'org-priority-up))
18672 ((and (not org-support-shift-select) (org-at-item-p))
18673 (call-interactively 'org-previous-item))
18674 ((org-clocktable-try-shift 'up arg))
18675 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
18676 (org-support-shift-select
18677 (org-call-for-shift-select 'previous-line))
18678 (t (org-shiftselect-error))))
18680 (defun org-shiftdown (&optional arg)
18681 "Decrease item in timestamp or decrease priority of current headline.
18682 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
18683 depending on context. See the individual commands for more information."
18686 ((run-hook-with-args-until-success 'org-shiftdown-hook))
18687 ((and org-support-shift-select (org-region-active-p))
18688 (org-call-for-shift-select 'next-line))
18689 ((org-at-timestamp-p t)
18690 (call-interactively (if org-edit-timestamp-down-means-later
18691 'org-timestamp-up 'org-timestamp-down)))
18692 ((and (not (eq org-support-shift-select 'always))
18693 org-enable-priority-commands
18694 (org-at-heading-p))
18695 (call-interactively 'org-priority-down))
18696 ((and (not org-support-shift-select) (org-at-item-p))
18697 (call-interactively 'org-next-item))
18698 ((org-clocktable-try-shift 'down arg))
18699 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
18700 (org-support-shift-select
18701 (org-call-for-shift-select 'next-line))
18702 (t (org-shiftselect-error))))
18704 (defun org-shiftright (&optional arg)
18705 "Cycle the thing at point or in the current line, depending on context.
18706 Depending on context, this does one of the following:
18708 - switch a timestamp at point one day into the future
18709 - on a headline, switch to the next TODO keyword.
18710 - on an item, switch entire list to the next bullet type
18711 - on a property line, switch to the next allowed value
18712 - on a clocktable definition line, move time block into the future"
18715 ((run-hook-with-args-until-success 'org-shiftright-hook))
18716 ((and org-support-shift-select (org-region-active-p))
18717 (org-call-for-shift-select 'forward-char))
18718 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
18719 ((and (not (eq org-support-shift-select 'always))
18720 (org-at-heading-p))
18721 (let ((org-inhibit-logging
18722 (not org-treat-S-cursor-todo-selection-as-state-change))
18723 (org-inhibit-blocking
18724 (not org-treat-S-cursor-todo-selection-as-state-change)))
18725 (org-call-with-arg 'org-todo 'right)))
18726 ((or (and org-support-shift-select
18727 (not (eq org-support-shift-select 'always))
18728 (org-at-item-bullet-p))
18729 (and (not org-support-shift-select) (org-at-item-p)))
18730 (org-call-with-arg 'org-cycle-list-bullet nil))
18731 ((and (not (eq org-support-shift-select 'always))
18732 (org-at-property-p))
18733 (call-interactively 'org-property-next-allowed-value))
18734 ((org-clocktable-try-shift 'right arg))
18735 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
18736 (org-support-shift-select
18737 (org-call-for-shift-select 'forward-char))
18738 (t (org-shiftselect-error))))
18740 (defun org-shiftleft (&optional arg)
18741 "Cycle the thing at point or in the current line, depending on context.
18742 Depending on context, this does one of the following:
18744 - switch a timestamp at point one day into the past
18745 - on a headline, switch to the previous TODO keyword.
18746 - on an item, switch entire list to the previous bullet type
18747 - on a property line, switch to the previous allowed value
18748 - on a clocktable definition line, move time block into the past"
18751 ((run-hook-with-args-until-success 'org-shiftleft-hook))
18752 ((and org-support-shift-select (org-region-active-p))
18753 (org-call-for-shift-select 'backward-char))
18754 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
18755 ((and (not (eq org-support-shift-select 'always))
18756 (org-at-heading-p))
18757 (let ((org-inhibit-logging
18758 (not org-treat-S-cursor-todo-selection-as-state-change))
18759 (org-inhibit-blocking
18760 (not org-treat-S-cursor-todo-selection-as-state-change)))
18761 (org-call-with-arg 'org-todo 'left)))
18762 ((or (and org-support-shift-select
18763 (not (eq org-support-shift-select 'always))
18764 (org-at-item-bullet-p))
18765 (and (not org-support-shift-select) (org-at-item-p)))
18766 (org-call-with-arg 'org-cycle-list-bullet 'previous))
18767 ((and (not (eq org-support-shift-select 'always))
18768 (org-at-property-p))
18769 (call-interactively 'org-property-previous-allowed-value))
18770 ((org-clocktable-try-shift 'left arg))
18771 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
18772 (org-support-shift-select
18773 (org-call-for-shift-select 'backward-char))
18774 (t (org-shiftselect-error))))
18776 (defun org-shiftcontrolright ()
18777 "Switch to next TODO set."
18780 ((and org-support-shift-select (org-region-active-p))
18781 (org-call-for-shift-select 'forward-word))
18782 ((and (not (eq org-support-shift-select 'always))
18783 (org-at-heading-p))
18784 (org-call-with-arg 'org-todo 'nextset))
18785 (org-support-shift-select
18786 (org-call-for-shift-select 'forward-word))
18787 (t (org-shiftselect-error))))
18789 (defun org-shiftcontrolleft ()
18790 "Switch to previous TODO set."
18793 ((and org-support-shift-select (org-region-active-p))
18794 (org-call-for-shift-select 'backward-word))
18795 ((and (not (eq org-support-shift-select 'always))
18796 (org-at-heading-p))
18797 (org-call-with-arg 'org-todo 'previousset))
18798 (org-support-shift-select
18799 (org-call-for-shift-select 'backward-word))
18800 (t (org-shiftselect-error))))
18802 (defun org-shiftcontrolup ()
18803 "Change timestamps synchronously up in CLOCK log lines."
18805 (cond ((and (not org-support-shift-select)
18806 (org-at-clock-log-p)
18807 (org-at-timestamp-p t))
18808 (org-clock-timestamps-up))
18809 (t (org-shiftselect-error))))
18811 (defun org-shiftcontroldown ()
18812 "Change timestamps synchronously down in CLOCK log lines."
18814 (cond ((and (not org-support-shift-select)
18815 (org-at-clock-log-p)
18816 (org-at-timestamp-p t))
18817 (org-clock-timestamps-down))
18818 (t (org-shiftselect-error))))
18820 (defun org-ctrl-c-ret ()
18821 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
18824 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
18825 (t (call-interactively 'org-insert-heading))))
18827 (defun org-find-visible ()
18829 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18830 (get-char-property s 'invisible)))
18832 (defun org-find-invisible ()
18834 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18835 (not (get-char-property s 'invisible))))
18838 (defun org-copy-visible (beg end)
18839 "Copy the visible parts of the region."
18844 (narrow-to-region beg end)
18845 (setq s (goto-char (point-min)))
18846 (while (not (= (point) (point-max)))
18847 (goto-char (org-find-invisible))
18848 (push (buffer-substring s (point)) snippets)
18849 (setq s (goto-char (org-find-visible))))))
18850 (kill-new (apply 'concat (nreverse snippets)))))
18852 (defun org-copy-special ()
18853 "Copy region in table or copy current subtree.
18854 Calls `org-table-copy' or `org-copy-subtree', depending on context.
18855 See the individual commands for more information."
18857 (call-interactively
18858 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
18860 (defun org-cut-special ()
18861 "Cut region in table or cut current subtree.
18862 Calls `org-table-copy' or `org-cut-subtree', depending on context.
18863 See the individual commands for more information."
18865 (call-interactively
18866 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
18868 (defun org-paste-special (arg)
18869 "Paste rectangular region into table, or past subtree relative to level.
18870 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
18871 See the individual commands for more information."
18873 (if (org-at-table-p)
18874 (org-table-paste-rectangle)
18875 (org-paste-subtree arg)))
18877 (defun org-edit-special (&optional arg)
18878 "Call a special editor for the stuff at point.
18879 When at a table, call the formula editor with `org-table-edit-formulas'.
18880 When at the first line of an src example, call `org-edit-src-code'.
18881 When in an #+include line, visit the include file. Otherwise call
18882 `ffap' to visit the file at point."
18884 ;; possibly prep session before editing source
18886 (let* ((info (org-babel-get-src-block-info))
18887 (lang (nth 0 info))
18888 (params (nth 2 info))
18889 (session (cdr (assoc :session params))))
18890 (when (and info session) ;; we are in a source-code block with a session
18892 (intern (concat "org-babel-prep-session:" lang)) session params))))
18893 (cond ;; proceed with `org-edit-special'
18895 (beginning-of-line 1)
18896 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
18897 (find-file (org-trim (match-string 1))))
18898 ((org-edit-src-code))
18899 ((org-edit-fixed-width-region))
18900 ((org-at-table.el-p)
18901 (org-edit-src-code))
18902 ((or (org-at-table-p)
18904 (beginning-of-line 1)
18905 (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
18906 (call-interactively 'org-table-edit-formulas))
18907 (t (call-interactively 'ffap))))
18909 (defvar org-table-coordinate-overlays) ; defined in org-table.el
18910 (defun org-ctrl-c-ctrl-c (&optional arg)
18911 "Set tags in headline, or update according to changed information at point.
18913 This command does many different things, depending on context:
18915 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
18916 this is what we do.
18918 - If the cursor is on a statistics cookie, update it.
18920 - If the cursor is in a headline, prompt for tags and insert them
18921 into the current line, aligned to `org-tags-column'. When called
18922 with prefix arg, realign all tags in the current buffer.
18924 - If the cursor is in one of the special #+KEYWORD lines, this
18925 triggers scanning the buffer for these lines and updating the
18928 - If the cursor is inside a table, realign the table. This command
18929 works even if the automatic table editor has been turned off.
18931 - If the cursor is on a #+TBLFM line, re-apply the formulas to
18934 - If the cursor is at a footnote reference or definition, jump to
18935 the corresponding definition or references, respectively.
18937 - If the cursor is a the beginning of a dynamic block, update it.
18939 - If the current buffer is a capture buffer, close note and file it.
18941 - If the cursor is on a <<<target>>>, update radio targets and
18942 corresponding links in this buffer.
18944 - If the cursor is on a numbered item in a plain list, renumber the
18947 - If the cursor is on a checkbox, toggle it.
18949 - If the cursor is on a code block, evaluate it. The variable
18950 `org-confirm-babel-evaluate' can be used to control prompting
18951 before code block evaluation, by default every code block
18952 evaluation requires confirmation. Code block evaluation can be
18953 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
18955 (let ((org-enable-table-editor t))
18957 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
18958 org-occur-highlights
18959 org-latex-fragment-image-overlays)
18960 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
18961 (org-remove-occur-highlights)
18962 (org-remove-latex-fragment-image-overlays)
18963 (message "Temporary highlights/overlays removed from current buffer"))
18964 ((and (local-variable-p 'org-finish-function (current-buffer))
18965 (fboundp org-finish-function))
18966 (funcall org-finish-function))
18967 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
18968 ((org-in-regexp org-ts-regexp-both)
18969 (org-timestamp-change 0 'day))
18970 ((or (looking-at org-property-start-re)
18971 (org-at-property-p))
18972 (call-interactively 'org-property-action))
18973 ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
18974 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
18975 (or (org-at-heading-p) (org-at-item-p)))
18976 (call-interactively 'org-update-statistics-cookies))
18977 ((org-at-heading-p) (call-interactively 'org-set-tags))
18978 ((org-at-table.el-p)
18979 (message "Use C-c ' to edit table.el tables"))
18981 (org-table-maybe-eval-formula)
18983 (call-interactively 'org-table-recalculate)
18984 (org-table-maybe-recalculate-line))
18985 (call-interactively 'org-table-align)
18986 (orgtbl-send-table 'maybe))
18987 ((or (org-footnote-at-reference-p)
18988 (org-footnote-at-definition-p))
18989 (call-interactively 'org-footnote-action))
18990 ((org-at-item-checkbox-p)
18991 ;; Cursor at a checkbox: repair list and update checkboxes. Send
18992 ;; list only if at top item.
18993 (let* ((cbox (match-string 1))
18994 (struct (org-list-struct))
18995 (old-struct (copy-tree struct))
18996 (parents (org-list-parents-alist struct))
18997 (orderedp (org-entry-get nil "ORDERED"))
18998 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19000 ;; Use a light version of `org-toggle-checkbox' to avoid
19001 ;; computing list structure twice.
19002 (let ((new-box (cond
19003 ((equal arg '(16)) "[-]")
19004 ((equal arg '(4)) nil)
19005 ((equal "[X]" cbox) "[ ]")
19007 (if (and firstp arg)
19008 ;; If at first item of sub-list, remove check-box from
19009 ;; every item at the same level.
19011 (lambda (pos) (org-list-set-checkbox pos struct new-box))
19012 (org-list-get-all-items
19013 (point-at-bol) struct (org-list-prevs-alist struct)))
19014 (org-list-set-checkbox (point-at-bol) struct new-box)))
19015 ;; Replicate `org-list-write-struct', while grabbing a return
19016 ;; value from `org-list-struct-fix-box'.
19017 (org-list-struct-fix-ind struct parents 2)
19018 (org-list-struct-fix-item-end struct)
19019 (let ((prevs (org-list-prevs-alist struct)))
19020 (org-list-struct-fix-bul struct prevs)
19021 (org-list-struct-fix-ind struct parents)
19023 (org-list-struct-fix-box struct parents prevs orderedp)))
19024 (org-list-struct-apply-struct struct old-struct)
19025 (org-update-checkbox-count-maybe)
19028 "Checkboxes were removed due to unchecked box at line %d"
19029 (org-current-line block-item)))
19030 (when firstp (org-list-send-list 'maybe))))
19032 ;; Cursor at an item: repair list. Do checkbox related actions
19033 ;; only if function was called with an argument. Send list only
19035 (let* ((struct (org-list-struct))
19036 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19039 (setq old-struct (copy-tree struct))
19041 ;; If at first item of sub-list, add check-box to every
19042 ;; item at the same level.
19045 (unless (org-list-get-checkbox pos struct)
19046 (org-list-set-checkbox pos struct "[ ]")))
19047 (org-list-get-all-items
19048 (point-at-bol) struct (org-list-prevs-alist struct)))
19049 (org-list-set-checkbox (point-at-bol) struct "[ ]")))
19050 (org-list-write-struct
19051 struct (org-list-parents-alist struct) old-struct)
19052 (when arg (org-update-checkbox-count-maybe))
19053 (when firstp (org-list-send-list 'maybe))))
19054 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
19056 (beginning-of-line 1)
19057 (save-excursion (org-update-dblock)))
19059 (let ((case-fold-search t))
19060 (beginning-of-line 1)
19061 (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
19063 ((or (equal (match-string 1) "TBLFM")
19064 (equal (match-string 1) "tblfm"))
19065 ;; Recalculate the table before this line
19067 (beginning-of-line 1)
19068 (skip-chars-backward " \r\n\t")
19069 (if (org-at-table-p)
19070 (org-call-with-arg 'org-table-recalculate (or arg t)))))
19072 (let ((org-inhibit-startup-visibility-stuff t)
19073 (org-startup-align-all-tables nil))
19074 (when (boundp 'org-table-coordinate-overlays)
19075 (mapc 'delete-overlay org-table-coordinate-overlays)
19076 (setq org-table-coordinate-overlays nil))
19077 (org-save-outline-visibility 'use-markers (org-mode-restart)))
19078 (message "Local setup has been refreshed"))))
19079 ((org-clock-update-time-maybe))
19081 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
19082 (error "C-c C-c can do nothing useful at this location"))))))
19084 (defun org-mode-restart ()
19085 "Restart Org-mode, to scan again for special lines.
19086 Also updates the keyword regular expressions."
19089 (message "Org-mode restarted"))
19091 (defun org-kill-note-or-show-branches ()
19092 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
19094 (if (not org-finish-function)
19097 (call-interactively 'show-branches))
19098 (let ((org-note-abort t))
19099 (funcall org-finish-function))))
19101 (defun org-return (&optional indent)
19102 "Goto next table row or insert a newline.
19103 Calls `org-table-next-row' or `newline', depending on context.
19104 See the individual commands for more information."
19108 ((or (bobp) (org-in-src-block-p))
19109 (if indent (newline-and-indent) (newline)))
19111 (org-table-justify-field-maybe)
19112 (call-interactively 'org-table-next-row))
19113 ;; when `newline-and-indent' is called within a list, make sure
19114 ;; text moved stays inside the item.
19115 ((and (org-in-item-p) indent)
19116 (if (and (org-at-item-p) (>= (point) (match-end 0)))
19118 (save-match-data (newline))
19119 (org-indent-line-to (length (match-string 0))))
19120 (let ((ind (org-get-indentation)))
19122 (if (org-looking-back org-list-end-re)
19124 (org-indent-line-to ind)))))
19125 ((and org-return-follows-link
19126 (org-at-timestamp-p t)
19127 (not (eq org-ts-what 'after)))
19128 (org-follow-timestamp-link))
19129 ((and org-return-follows-link
19130 (let ((tprop (get-text-property (point) 'face)))
19131 (or (eq tprop 'org-link)
19132 (and (listp tprop) (memq 'org-link tprop)))))
19133 (call-interactively 'org-open-at-point))
19134 ((and (org-at-heading-p)
19136 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
19140 (t (if indent (newline-and-indent) (newline))))))
19142 (defun org-return-indent ()
19143 "Goto next table row or insert a newline and indent.
19144 Calls `org-table-next-row' or `newline-and-indent', depending on
19145 context. See the individual commands for more information."
19149 (defun org-ctrl-c-star ()
19150 "Compute table, or change heading status of lines.
19151 Calls `org-table-recalculate' or `org-toggle-heading',
19152 depending on context."
19156 (call-interactively 'org-table-recalculate))
19158 ;; Convert all lines in region to list items
19159 (call-interactively 'org-toggle-heading))))
19161 (defun org-ctrl-c-minus ()
19162 "Insert separator line in table or modify bullet status of line.
19163 Also turns a plain line or a region of lines into list items.
19164 Calls `org-table-insert-hline', `org-toggle-item', or
19165 `org-cycle-list-bullet', depending on context."
19169 (call-interactively 'org-table-insert-hline))
19170 ((org-region-active-p)
19171 (call-interactively 'org-toggle-item))
19173 (call-interactively 'org-cycle-list-bullet))
19175 (call-interactively 'org-toggle-item))))
19177 (defun org-toggle-item (arg)
19178 "Convert headings or normal lines to items, items to normal lines.
19179 If there is no active region, only the current line is considered.
19181 If the first non blank line in the region is an headline, convert
19182 all headlines to items, shifting text accordingly.
19184 If it is an item, convert all items to normal lines.
19186 If it is normal text, change region into an item. With a prefix
19187 argument ARG, change each line in region into an item."
19191 ;; Shift text in current section to IND, from point to END.
19192 ;; The function leaves point to END line.
19194 (let ((min-i 1000) (end (copy-marker end)))
19195 ;; First determine the minimum indentation (MIN-I) of
19199 (while (< (point) end)
19200 (let ((i (org-get-indentation)))
19202 ;; Skip blank lines and inline tasks.
19203 ((looking-at "^[ \t]*$"))
19204 ((looking-at org-outline-regexp-bol))
19205 ;; We can't find less than 0 indentation.
19206 ((zerop i) (throw 'exit (setq min-i 0)))
19207 ((< i min-i) (setq min-i i))))
19209 ;; Then indent each line so that a line indented to
19210 ;; MIN-I becomes indented to IND. Ignore blank lines
19211 ;; and inline tasks in the process.
19212 (let ((delta (- ind min-i)))
19213 (while (< (point) end)
19214 (unless (or (looking-at "^[ \t]*$")
19215 (looking-at org-outline-regexp-bol))
19216 (org-indent-line-to (+ (org-get-indentation) delta)))
19217 (forward-line)))))))
19220 ;; Return beginning of first non-blank line, starting from
19225 (skip-chars-forward " \r\t\n")
19228 ;; Determine boundaries of changes.
19229 (if (org-region-active-p)
19230 (setq beg (funcall skip-blanks (region-beginning))
19231 end (copy-marker (region-end)))
19232 (setq beg (funcall skip-blanks (point-at-bol))
19233 end (copy-marker (point-at-eol))))
19234 ;; Depending on the starting line, choose an action on the text
19235 ;; between BEG and END.
19236 (org-with-limited-levels
19240 ;; Case 1. Start at an item: de-itemize. Note that it only
19241 ;; happens when a region is active: `org-ctrl-c-minus'
19242 ;; would call `org-cycle-list-bullet' otherwise.
19244 (while (< (point) end)
19245 (when (org-at-item-p)
19246 (skip-chars-forward " \t")
19247 (delete-region (point) (match-end 0)))
19249 ;; Case 2. Start at an heading: convert to items.
19250 ((org-at-heading-p)
19251 (let* ((bul (org-list-bullet-string "-"))
19252 (bul-len (length bul))
19253 ;; Indentation of the first heading. It should be
19254 ;; relative to the indentation of its parent, if any.
19255 (start-ind (save-excursion
19257 ((not org-adapt-indentation) 0)
19258 ((not (outline-previous-heading)) 0)
19259 (t (length (match-string 0))))))
19260 ;; Level of first heading. Further headings will be
19261 ;; compared to it to determine hierarchy in the list.
19262 (ref-level (org-reduced-level (org-outline-level))))
19263 (while (< (point) end)
19264 (let* ((level (org-reduced-level (org-outline-level)))
19265 (delta (max 0 (- level ref-level))))
19266 ;; If current headline is less indented than the first
19267 ;; one, set it as reference, in order to preserve
19269 (when (< level ref-level) (setq ref-level level))
19270 (replace-match bul t t)
19271 (org-indent-line-to (+ start-ind (* delta bul-len)))
19272 ;; Ensure all text down to END (or SECTION-END) belongs
19273 ;; to the newly created item.
19274 (let ((section-end (save-excursion
19275 (or (outline-next-heading) (point)))))
19277 (funcall shift-text
19278 (+ start-ind (* (1+ delta) bul-len))
19279 (min end section-end)))))))
19280 ;; Case 3. Normal line with ARG: turn each non-item line into
19283 (while (< (point) end)
19284 (unless (or (org-at-heading-p) (org-at-item-p))
19285 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
19287 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
19289 ;; Case 4. Normal line without ARG: make the first line of
19290 ;; region an item, and shift indentation of others
19291 ;; lines to set them as item's body.
19292 (t (let* ((bul (org-list-bullet-string "-"))
19293 (bul-len (length bul))
19294 (ref-ind (org-get-indentation)))
19295 (skip-chars-forward " \t")
19298 (while (< (point) end)
19299 ;; Ensure that lines less indented than first one
19300 ;; still get included in item body.
19301 (funcall shift-text
19302 (+ ref-ind bul-len)
19303 (min end (save-excursion (or (outline-next-heading)
19305 (forward-line)))))))))
19307 (defun org-toggle-heading (&optional nstars)
19308 "Convert headings to normal text, or items or text to headings.
19309 If there is no active region, only the current line is considered.
19311 With a \\[universal-argument] prefix, convert the whole list at
19312 point into heading.
19316 - If the first non blank line is an headline, remove the stars
19317 from all headlines in the region.
19319 - If it is a normal line turn each and every normal line (i.e. not an
19320 heading or an item) in the region into a heading.
19322 - If it is a plain list item, turn all plain list items into headings.
19324 When converting a line into a heading, the number of stars is chosen
19325 such that the lines become children of the current entry. However,
19326 when a prefix argument is given, its value determines the number of
19331 ;; Return beginning of first non-blank line, starting from
19336 (while (org-at-comment-p) (forward-line))
19337 (skip-chars-forward " \r\t\n")
19340 ;; Determine boundaries of changes. If a universal prefix has
19341 ;; been given, put the list in a region. If region ends at a bol,
19342 ;; do not consider the last line to be in the region.
19344 (when (and current-prefix-arg (org-at-item-p))
19345 (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
19346 (org-element-mark-element))
19348 (if (org-region-active-p)
19349 (setq beg (funcall skip-blanks (region-beginning))
19350 end (copy-marker (save-excursion
19351 (goto-char (region-end))
19352 (if (bolp) (point) (point-at-eol)))))
19353 (setq beg (funcall skip-blanks (point-at-bol))
19354 end (copy-marker (point-at-eol))))
19355 ;; Ensure inline tasks don't count as headings.
19356 (org-with-limited-levels
19360 ;; Case 1. Started at an heading: de-star headings.
19361 ((org-at-heading-p)
19362 (while (< (point) end)
19363 (when (org-at-heading-p t)
19364 (looking-at org-outline-regexp) (replace-match "")
19367 ;; Case 2. Started at an item: change items into headlines.
19368 ;; One star will be added by `org-list-to-subtree'.
19370 (let* ((stars (make-string
19372 ;; subtract the star that will be added again by
19373 ;; `org-list-to-subtree'
19374 (1- (prefix-numeric-value current-prefix-arg))
19375 (or (org-current-level) 0))
19378 (cond (nstars "") ; stars from prefix only
19379 ((equal stars "") "") ; before first heading
19380 (org-odd-levels-only "*") ; inside heading, odd
19381 (t "")))) ; inside heading, oddeven
19382 (while (< (point) end)
19383 (when (org-at-item-p)
19384 ;; Pay attention to cases when region ends before list.
19385 (let* ((struct (org-list-struct))
19386 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
19388 (narrow-to-region (point) list-end)
19390 (org-list-to-subtree
19391 (org-list-parse-list t)
19392 '(:istart (concat stars add-stars (funcall get-stars depth))
19393 :icount (concat stars add-stars (funcall get-stars depth)))))))
19396 ;; Case 3. Started at normal text: make every line an heading,
19397 ;; skipping headlines and items.
19398 (t (let* ((stars (make-string
19400 (prefix-numeric-value current-prefix-arg)
19401 (or (org-current-level) 0))
19404 (cond (nstars "") ; stars from prefix only
19405 ((equal stars "") "*") ; before first heading
19406 (org-odd-levels-only "**") ; inside heading, odd
19407 (t "*"))) ; inside heading, oddeven
19408 (rpl (concat stars add-stars " ")))
19409 (while (< (point) end)
19410 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
19411 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
19412 (replace-match (concat rpl (match-string 2))) (setq toggled t))
19413 (forward-line)))))))
19414 (unless toggled (message "Cannot toggle heading from here"))))
19416 (defun org-meta-return (&optional arg)
19417 "Insert a new heading or wrap a region in a table.
19418 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
19419 See the individual commands for more information."
19422 ((run-hook-with-args-until-success 'org-metareturn-hook))
19423 ((or (org-at-drawer-p) (org-at-property-p))
19424 (newline-and-indent))
19426 (call-interactively 'org-table-wrap-region))
19427 (t (call-interactively 'org-insert-heading))))
19431 ;; Define the Org-mode menus
19432 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
19434 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
19435 ["Next Field" org-cycle (org-at-table-p)]
19436 ["Previous Field" org-shifttab (org-at-table-p)]
19437 ["Next Row" org-return (org-at-table-p)]
19439 ["Blank Field" org-table-blank-field (org-at-table-p)]
19440 ["Edit Field" org-table-edit-field (org-at-table-p)]
19441 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
19444 ["Move Column Left" org-metaleft (org-at-table-p)]
19445 ["Move Column Right" org-metaright (org-at-table-p)]
19446 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
19447 ["Insert Column" org-shiftmetaright (org-at-table-p)])
19449 ["Move Row Up" org-metaup (org-at-table-p)]
19450 ["Move Row Down" org-metadown (org-at-table-p)]
19451 ["Delete Row" org-shiftmetaup (org-at-table-p)]
19452 ["Insert Row" org-shiftmetadown (org-at-table-p)]
19453 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
19455 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
19457 ["Copy Rectangle" org-copy-special (org-at-table-p)]
19458 ["Cut Rectangle" org-cut-special (org-at-table-p)]
19459 ["Paste Rectangle" org-paste-special (org-at-table-p)]
19460 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
19463 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
19464 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
19465 ["Edit Formulas" org-edit-special (org-at-table-p)]
19467 ["Recalculate line" org-table-recalculate (org-at-table-p)]
19468 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
19469 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
19471 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
19473 ["Sum Column/Rectangle" org-table-sum
19474 (or (org-at-table-p) (org-region-active-p))]
19475 ["Which Column?" org-table-current-column (org-at-table-p)])
19477 org-table-toggle-formula-debugger
19478 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
19479 ["Show Col/Row Numbers"
19480 org-table-toggle-coordinate-overlays
19482 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
19484 ["Create" org-table-create (and (not (org-at-table-p))
19485 org-enable-table-editor)]
19486 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
19487 ["Import from File" org-table-import (not (org-at-table-p))]
19488 ["Export to File" org-table-export (org-at-table-p)]
19490 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
19492 (easy-menu-define org-org-menu org-mode-map "Org menu"
19495 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
19496 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
19497 ["Sparse Tree..." org-sparse-tree t]
19498 ["Reveal Context" org-reveal t]
19499 ["Show All" show-all t]
19501 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
19503 ["New Heading" org-insert-heading t]
19504 ("Navigate Headings"
19505 ["Up" outline-up-heading t]
19506 ["Next" outline-next-visible-heading t]
19507 ["Previous" outline-previous-visible-heading t]
19508 ["Next Same Level" outline-forward-same-level t]
19509 ["Previous Same Level" outline-backward-same-level t]
19511 ["Jump" org-goto t])
19513 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
19514 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
19516 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
19517 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
19518 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
19520 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
19522 ["Copy visible text" org-copy-visible t]
19524 ["Promote Heading" org-metaleft (not (org-at-table-p))]
19525 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
19526 ["Demote Heading" org-metaright (not (org-at-table-p))]
19527 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
19529 ["Sort Region/Children" org-sort (not (org-at-table-p))]
19531 ["Convert to odd levels" org-convert-to-odd-levels t]
19532 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
19534 ["Emphasis..." org-emphasize t]
19535 ["Edit Source Example" org-edit-special t]
19537 ["Footnote new/jump" org-footnote-action t]
19538 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
19540 ["Archive (default method)" org-archive-subtree-default t]
19542 ["Move Subtree to Archive file" org-advertized-archive-subtree t]
19543 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
19544 ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
19548 ["Store Link (Global)" org-store-link t]
19549 ["Find existing link to here" org-occur-link-in-agenda-files t]
19550 ["Insert Link" org-insert-link t]
19551 ["Follow Link" org-open-at-point t]
19553 ["Next link" org-next-link t]
19554 ["Previous link" org-previous-link t]
19556 ["Descriptive Links"
19557 org-toggle-link-display
19559 :selected org-descriptive-links
19562 org-toggle-link-display
19564 :selected (not org-descriptive-links)])
19567 ["TODO/DONE/-" org-todo t]
19569 ["Next keyword" org-shiftright (org-at-heading-p)]
19570 ["Previous keyword" org-shiftleft (org-at-heading-p)]
19571 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
19572 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
19573 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
19574 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
19575 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
19577 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
19578 :selected org-enforce-todo-dependencies :style toggle :active t]
19579 "Settings for tree at point"
19580 ["Do Children sequentially" org-toggle-ordered-property :style radio
19581 :selected (org-entry-get nil "ORDERED")
19582 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19583 ["Do Children parallel" org-toggle-ordered-property :style radio
19584 :selected (not (org-entry-get nil "ORDERED"))
19585 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19587 ["Set Priority" org-priority t]
19588 ["Priority Up" org-shiftup t]
19589 ["Priority Down" org-shiftdown t]
19591 ["Get news from all feeds" org-feed-update-all t]
19592 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
19593 ["Customize feeds" (customize-variable 'org-feed-alist) t])
19594 ("TAGS and Properties"
19595 ["Set Tags" org-set-tags-command t]
19596 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
19598 ["Set property" org-set-property t]
19599 ["Column view of properties" org-columns t]
19600 ["Insert Column View DBlock" org-insert-columns-dblock t])
19601 ("Dates and Scheduling"
19602 ["Timestamp" org-time-stamp t]
19603 ["Timestamp (inactive)" org-time-stamp-inactive t]
19605 ["1 Day Later" org-shiftright t]
19606 ["1 Day Earlier" org-shiftleft t]
19607 ["1 ... Later" org-shiftup t]
19608 ["1 ... Earlier" org-shiftdown t])
19609 ["Compute Time Range" org-evaluate-time-range t]
19610 ["Schedule Item" org-schedule t]
19611 ["Deadline" org-deadline t]
19613 ["Custom time format" org-toggle-time-stamp-overlays
19614 :style radio :selected org-display-custom-times]
19616 ["Goto Calendar" org-goto-calendar t]
19617 ["Date from Calendar" org-date-from-calendar t]
19619 ["Start/Restart Timer" org-timer-start t]
19620 ["Pause/Continue Timer" org-timer-pause-or-continue t]
19621 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
19622 ["Insert Timer String" org-timer t]
19623 ["Insert Timer Item" org-timer-item t])
19625 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
19626 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
19627 ["Clock out" org-clock-out t]
19628 ["Clock cancel" org-clock-cancel t]
19630 ["Mark as default task" org-clock-mark-default-task t]
19631 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
19632 ["Goto running clock" org-clock-goto t]
19634 ["Display times" org-clock-display t]
19635 ["Create clock table" org-clock-report t]
19637 ["Record DONE time"
19638 (progn (setq org-log-done (not org-log-done))
19639 (message "Switching to %s will %s record a timestamp"
19640 (car org-done-keywords)
19641 (if org-log-done "automatically" "not")))
19642 :style toggle :selected org-log-done])
19644 ["Agenda Command..." org-agenda t]
19645 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
19646 ("File List for Agenda")
19647 ("Special views current file"
19648 ["TODO Tree" org-show-todo-tree t]
19649 ["Check Deadlines" org-check-deadlines t]
19650 ["Timeline" org-timeline t]
19651 ["Tags/Property tree" org-match-sparse-tree t])
19653 ["Export/Publish..." org-export t]
19655 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
19656 :selected org-cdlatex-mode]
19657 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
19658 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
19659 ["Modify math symbol" org-cdlatex-math-modify
19660 (org-inside-LaTeX-fragment-p)]
19661 ["Insert citation" org-reftex-citation t]
19663 ["Template for BEAMER" (progn (require 'org-beamer)
19664 (org-insert-beamer-options-template)) t])
19667 ["Push Files and Views" org-mobile-push t]
19668 ["Get Captured and Flagged" org-mobile-pull t]
19669 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
19671 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
19674 ["Show Version" org-version t]
19675 ["Info Documentation" org-info t])
19677 ["Browse Org Group" org-customize t]
19679 ["Expand This Menu" org-create-customize-menu
19680 (fboundp 'customize-menu-create)])
19681 ["Send bug report" org-submit-bug-report t]
19684 ["Refresh setup current buffer" org-mode-restart t]
19685 ["Reload Org (after update)" org-reload t]
19686 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
19689 (defun org-info (&optional node)
19690 "Read documentation for Org-mode in the info system.
19691 With optional NODE, go directly to that node."
19693 (info (format "(org)%s" (or node ""))))
19696 (defun org-submit-bug-report ()
19697 "Submit a bug report on Org-mode via mail.
19699 Don't hesitate to report any problems or inaccurate documentation.
19701 If you don't have setup sending mail from (X)Emacs, please copy the
19702 output buffer into your mail program, as it gives us important
19703 information about your Org-mode version and configuration."
19705 (require 'reporter)
19706 (org-load-modules-maybe)
19707 (org-require-autoloaded-modules)
19708 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
19709 (reporter-submit-bug-report
19710 "emacs-orgmode@gnu.org"
19711 (org-version nil 'full)
19713 (save-window-excursion
19714 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
19715 (delete-other-windows)
19717 (insert "You are about to submit a bug report to the Org-mode mailing list.
19719 We would like to add your full Org-mode and Outline configuration to the
19720 bug report. This greatly simplifies the work of the maintainer and
19721 other experts on the mailing list.
19723 HOWEVER, some variables you have customized may contain private
19724 information. The names of customers, colleagues, or friends, might
19725 appear in the form of file names, tags, todo states, or search strings.
19726 If you answer yes to the prompt, you might want to check and remove
19727 such private information before sending the email.")
19728 (add-text-properties (point-min) (point-max) '(face org-warning))
19729 (when (yes-or-no-p "Include your Org-mode configuration ")
19733 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
19734 (or (and (symbol-value v)
19735 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
19737 (get v 'custom-type) (get v 'standard-value)
19738 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
19740 (kill-buffer (get-buffer "*Warn about privacy*"))
19743 "Remember to cover the basics, that is, what you expected to happen and
19744 what in fact did happen. You don't know how to make a good report? See
19746 http://orgmode.org/manual/Feedback.html#Feedback
19748 Your bug report will be posted to the Org-mode mailing list.
19749 ------------------------------------------------------------------------")
19751 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
19752 (replace-match "\\1Bug: \\3 [\\2]")))))
19755 (defun org-install-agenda-files-menu ()
19756 (let ((bl (buffer-list)))
19759 (set-buffer (pop bl))
19760 (if (derived-mode-p 'org-mode) (setq bl nil)))
19761 (when (derived-mode-p 'org-mode)
19763 '("Org") "File List for Agenda"
19766 ["Edit File List" (org-edit-agenda-file-list) t]
19767 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
19768 ["Remove Current File from List" org-remove-file t]
19769 ["Cycle through agenda files" org-cycle-agenda-files t]
19770 ["Occur in all agenda files" org-occur-in-agenda-files t]
19772 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
19777 (defun org-require-autoloaded-modules ()
19780 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
19781 org-docbook org-exp org-html org-icalendar
19783 org-publish org-remember org-table
19784 org-timer org-xoxo)))
19787 (defun org-reload (&optional uncompiled)
19788 "Reload all org lisp files.
19789 With prefix arg UNCOMPILED, load the uncompiled versions."
19791 (require 'find-func)
19792 (let* ((file-re "^org\\(-.*\\)?\\.el")
19793 (dir-org (file-name-directory (org-find-library-dir "org")))
19794 (dir-org-contrib (ignore-errors
19795 (file-name-directory
19796 (org-find-library-dir "org-contribdir"))))
19798 (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
19799 (append (list nil "comint" "eval" "exp" "keys"
19800 "lob" "ref" "table" "tangle")
19804 (when (cdr lang) (symbol-name (car lang))))
19805 org-babel-load-languages)))))
19807 (append babel-files
19808 (and dir-org-contrib
19809 (directory-files dir-org-contrib t file-re))
19810 (directory-files dir-org t file-re)))
19811 (remove-re (concat (if (featurep 'xemacs)
19812 "org-colview" "org-colview-xemacs")
19814 (setq files (mapcar 'file-name-sans-extension files))
19815 (setq files (mapcar
19816 (lambda (x) (if (string-match remove-re x) nil x))
19818 (setq files (delq nil files))
19821 (when (featurep (intern (file-name-nondirectory f)))
19822 (if (and (not uncompiled)
19823 (file-exists-p (concat f ".elc")))
19824 (load (concat f ".elc") nil nil 'nosuffix)
19825 (load (concat f ".el") nil nil 'nosuffix))))
19827 (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
19828 (org-version nil 'full 'message))
19831 (defun org-customize ()
19832 "Call the customize function with org as argument."
19834 (org-load-modules-maybe)
19835 (org-require-autoloaded-modules)
19836 (customize-browse 'org))
19838 (defun org-create-customize-menu ()
19839 "Create a full customization menu for Org-mode, insert it into the menu."
19841 (org-load-modules-maybe)
19842 (org-require-autoloaded-modules)
19843 (if (fboundp 'customize-menu-create)
19846 '("Org") "Customize"
19847 `(["Browse Org group" org-customize t]
19849 ,(customize-menu-create 'org)
19850 ["Set" Custom-set t]
19851 ["Save" Custom-save t]
19852 ["Reset to Current" Custom-reset-current t]
19853 ["Reset to Saved" Custom-reset-saved t]
19854 ["Reset to Standard Settings" Custom-reset-standard t]))
19855 (message "\"Org\"-menu now contains full customization menu"))
19856 (error "Cannot expand menu (outdated version of cus-edit.el)")))
19858 ;;;; Miscellaneous stuff
19860 ;;; Generally useful functions
19862 (defun org-get-at-bol (property)
19863 "Get text property PROPERTY at beginning of line."
19864 (get-text-property (point-at-bol) property))
19866 (defun org-find-text-property-in-string (prop s)
19867 "Return the first non-nil value of property PROP in string S."
19868 (or (get-text-property 0 prop s)
19869 (get-text-property (or (next-single-property-change 0 prop s) 0)
19872 (defun org-display-warning (message) ;; Copied from Emacs-Muse
19873 "Display the given MESSAGE as a warning."
19874 (if (fboundp 'display-warning)
19875 (display-warning 'org message
19876 (if (featurep 'xemacs) 'warning :warning))
19877 (let ((buf (get-buffer-create "*Org warnings*")))
19878 (with-current-buffer buf
19879 (goto-char (point-max))
19880 (insert "Warning (Org): " message)
19883 (display-buffer buf)
19886 (defun org-eval (form)
19887 "Eval FORM and return result."
19888 (condition-case error
19890 (error (format "%%![Error: %s]" error))))
19892 (defun org-in-clocktable-p ()
19893 "Check if the cursor is in a clocktable."
19894 (let ((pos (point)) start)
19897 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
19898 (setq start (match-beginning 0))
19899 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
19900 (>= (match-end 0) pos)
19903 (defun org-in-commented-line ()
19904 "Is point in a line starting with `#'?"
19905 (equal (char-after (point-at-bol)) ?#))
19907 (defun org-in-indented-comment-line ()
19908 "Is point in a line starting with `#' after some white space?"
19911 (goto-char (point-at-bol))
19912 (looking-at "[ \t]*#"))))
19914 (defun org-in-verbatim-emphasis ()
19916 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
19918 (defun org-goto-marker-or-bmk (marker &optional bookmark)
19919 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
19920 (if (and marker (marker-buffer marker)
19921 (buffer-live-p (marker-buffer marker)))
19923 (org-pop-to-buffer-same-window (marker-buffer marker))
19924 (if (or (> marker (point-max)) (< marker (point-min)))
19927 (org-show-context 'org-goto))
19929 (bookmark-jump bookmark)
19930 (error "Cannot find location"))))
19932 (defun org-quote-csv-field (s)
19933 "Quote field for inclusion in CSV material."
19934 (if (string-match "[\",]" s)
19935 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
19938 (defun org-force-self-insert (N)
19939 "Needed to enforce self-insert under remapping."
19941 (self-insert-command N))
19943 (defun org-string-width (s)
19944 "Compute width of string, ignoring invisible characters.
19945 This ignores character with invisibility property `org-link', and also
19946 characters with property `org-cwidth', because these will become invisible
19947 upon the next fontification round."
19949 (when (or (eq t buffer-invisibility-spec)
19950 (assq 'org-link buffer-invisibility-spec))
19951 (while (setq b (text-property-any 0 (length s)
19952 'invisible 'org-link s))
19953 (setq s (concat (substring s 0 b)
19954 (substring s (or (next-single-property-change
19955 b 'invisible s) (length s)))))))
19956 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
19957 (setq s (concat (substring s 0 b)
19958 (substring s (or (next-single-property-change
19959 b 'org-cwidth s) (length s))))))
19960 (setq l (string-width s) b -1)
19961 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
19962 (setq l (- l (get-text-property b 'org-dwidth-n s))))
19965 (defun org-shorten-string (s maxlength)
19966 "Shorten string S so tht it is no longer than MAXLENGTH characters.
19967 If the string is shorter or has length MAXLENGTH, just return the
19968 original string. If it is longer, the functions finds a space in the
19969 string, breaks this string off at that locations and adds three dots
19970 as ellipsis. Including the ellipsis, the string will not be longer
19971 than MAXLENGTH. If finding a good breaking point in the string does
19972 not work, the string is just chopped off in the middle of a word
19974 (if (<= (length s) maxlength)
19976 (let* ((n (max (- maxlength 4) 1))
19977 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
19978 (if (string-match re s)
19979 (concat (match-string 1 s) "...")
19980 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
19982 (defun org-get-indentation (&optional line)
19983 "Get the indentation of the current line, interpreting tabs.
19984 When LINE is given, assume it represents a line and compute its indentation."
19986 (if (string-match "^ *" (org-remove-tabs line))
19989 (beginning-of-line 1)
19990 (skip-chars-forward " \t")
19991 (current-column))))
19993 (defun org-get-string-indentation (s)
19994 "What indentation has S due to SPACE and TAB at the beginning of the string?"
19995 (let ((n -1) (i 0) (w tab-width) c)
19997 (while (< (setq n (1+ n)) (length s))
19998 (setq c (aref s n))
19999 (cond ((= c ?\ ) (setq i (1+ i)))
20000 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
20001 (t (throw 'exit t)))))
20004 (defun org-remove-tabs (s &optional width)
20005 "Replace tabulators in S with spaces.
20006 Assumes that s is a single line, starting in column 0."
20007 (setq width (or width tab-width))
20008 (while (string-match "\t" s)
20009 (setq s (replace-match
20011 (- (* width (/ (+ (match-beginning 0) width) width))
20012 (match-beginning 0)) ?\ )
20016 (defun org-fix-indentation (line ind)
20017 "Fix indentation in LINE.
20018 IND is a cons cell with target and minimum indentation.
20019 If the current indentation in LINE is smaller than the minimum,
20020 leave it alone. If it is larger than ind, set it to the target."
20021 (let* ((l (org-remove-tabs line))
20022 (i (org-get-indentation l))
20023 (i1 (car ind)) (i2 (cdr ind)))
20024 (if (>= i i2) (setq l (substring line i2)))
20026 (concat (make-string i1 ?\ ) l)
20029 (defun org-remove-indentation (code &optional n)
20030 "Remove the maximum common indentation from the lines in CODE.
20031 N may optionally be the number of spaces to remove."
20034 (org-do-remove-indentation n)
20037 (defun org-do-remove-indentation (&optional n)
20038 "Remove the maximum common indentation from the buffer."
20039 (untabify (point-min) (point-max))
20040 (let ((min 10000) re)
20043 (goto-char (point-min))
20044 (while (re-search-forward "^ *[^ \n]" nil t)
20045 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
20046 (unless (or (= min 0) (= min 10000))
20047 (setq re (format "^ \\{%d\\}" min))
20048 (goto-char (point-min))
20049 (while (re-search-forward re nil t)
20054 (defun org-fill-template (template alist)
20055 "Find each %key of ALIST in TEMPLATE and replace it."
20056 (let ((case-fold-search nil)
20058 (setq alist (sort (copy-sequence alist)
20059 (lambda (a b) (< (length (car a)) (length (car b))))))
20060 (while (setq entry (pop alist))
20062 (replace-regexp-in-string
20063 (concat "%" (regexp-quote (car entry)))
20064 (cdr entry) template t t)))
20067 (defun org-base-buffer (buffer)
20068 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
20071 (or (buffer-base-buffer buffer)
20074 (defun org-trim (s)
20075 "Remove whitespace at beginning and end of string."
20076 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
20077 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
20080 (defun org-wrap (string &optional width lines)
20081 "Wrap string to either a number of lines, or a width in characters.
20082 If WIDTH is non-nil, the string is wrapped to that width, however many lines
20083 that costs. If there is a word longer than WIDTH, the text is actually
20084 wrapped to the length of that word.
20085 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
20086 many lines, whatever width that takes.
20087 The return value is a list of lines, without newlines at the end."
20088 (let* ((words (org-split-string string "[ \t\n]+"))
20089 (maxword (apply 'max (mapcar 'org-string-width words)))
20092 (org-do-wrap words (max maxword width)))
20095 (setq ll (org-do-wrap words maxword))
20096 (if (<= (length ll) lines)
20099 (while (> (length ll) lines)
20101 (setq ll (org-do-wrap words w)))
20103 (t (error "Cannot wrap this")))))
20105 (defun org-do-wrap (words width)
20106 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
20109 (setq line (pop words))
20110 (while (and words (< (+ (length line) (length (car words))) width))
20111 (setq line (concat line " " (pop words))))
20112 (setq lines (push line lines)))
20115 (defun org-split-string (string &optional separators)
20116 "Splits STRING into substrings at SEPARATORS.
20117 No empty strings are returned if there are matches at the beginning
20118 and end of string."
20119 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
20123 (while (and (string-match rexp string
20125 (= start (match-beginning 0))
20126 (< start (length string)))
20128 (< (match-beginning 0) (length string)))
20130 (or (eq (match-beginning 0) 0)
20131 (and (eq (match-beginning 0) (match-end 0))
20132 (eq (match-beginning 0) start))
20134 (cons (substring string start (match-beginning 0))
20136 (setq start (match-end 0)))
20137 (or (eq start (length string))
20139 (cons (substring string start)
20143 (defun org-quote-vert (s)
20144 "Replace \"|\" with \"\\vert\"."
20145 (while (string-match "|" s)
20146 (setq s (replace-match "\\vert" t t s)))
20149 (defun org-uuidgen-p (s)
20150 "Is S an ID created by UUIDGEN?"
20151 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
20153 (defun org-in-src-block-p nil
20154 "Whether point is in a code source block."
20156 (when (setq ov (overlays-at (point)))
20157 (memq 'org-block-background
20158 (overlay-properties
20161 (defun org-context ()
20162 "Return a list of contexts of the current cursor position.
20163 If several contexts apply, all are returned.
20164 Each context entry is a list with a symbol naming the context, and
20165 two positions indicating start and end of the context. Possible
20168 :headline anywhere in a headline
20169 :headline-stars on the leading stars in a headline
20170 :todo-keyword on a TODO keyword (including DONE) in a headline
20171 :tags on the TAGS in a headline
20172 :priority on the priority cookie in a headline
20173 :item on the first line of a plain list item
20174 :item-bullet on the bullet/number of a plain list item
20175 :checkbox on the checkbox in a plain list item
20176 :table in an org-mode table
20177 :table-special on a special filed in a table
20178 :table-table in a table.el table
20179 :clocktable in a clocktable
20180 :src-block in a source block
20181 :link on a hyperlink
20182 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
20183 :target on a <<target>>
20184 :radio-target on a <<<radio-target>>>
20185 :latex-fragment on a LaTeX fragment
20186 :latex-preview on a LaTeX fragment with overlaid preview image
20188 This function expects the position to be visible because it uses font-lock
20189 faces as a help to recognize the following contexts: :table-special, :link,
20191 (let* ((f (get-text-property (point) 'face))
20192 (faces (if (listp f) f (list f)))
20193 (case-fold-search t)
20194 (p (point)) clist o)
20195 ;; First the large context
20197 ((org-at-heading-p t)
20198 (push (list :headline (point-at-bol) (point-at-eol)) clist)
20200 (beginning-of-line 1)
20201 (looking-at org-todo-line-tags-regexp))
20202 (push (org-point-in-group p 1 :headline-stars) clist)
20203 (push (org-point-in-group p 2 :todo-keyword) clist)
20204 (push (org-point-in-group p 4 :tags) clist))
20206 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
20207 (if (looking-at "\\[#[A-Z0-9]\\]")
20208 (push (org-point-in-group p 0 :priority) clist)))
20211 (push (org-point-in-group p 2 :item-bullet) clist)
20212 (push (list :item (point-at-bol)
20213 (save-excursion (org-end-of-item) (point)))
20215 (and (org-at-item-checkbox-p)
20216 (push (org-point-in-group p 0 :checkbox) clist)))
20219 (push (list :table (org-table-begin) (org-table-end)) clist)
20220 (if (memq 'org-formula faces)
20221 (push (list :table-special
20222 (previous-single-property-change p 'face)
20223 (next-single-property-change p 'face)) clist)))
20224 ((org-at-table-p 'any)
20225 (push (list :table-table) clist)))
20228 ;; New the "medium" contexts: clocktables, source blocks
20229 (cond ((org-in-clocktable-p)
20230 (push (list :clocktable
20231 (and (or (looking-at "#\\+BEGIN: clocktable")
20232 (search-backward "#+BEGIN: clocktable" nil t))
20233 (match-beginning 0))
20234 (and (re-search-forward "#\\+END:?" nil t)
20235 (match-end 0))) clist))
20236 ((org-in-src-block-p)
20237 (push (list :src-block
20238 (and (or (looking-at "#\\+BEGIN_SRC")
20239 (search-backward "#+BEGIN_SRC" nil t))
20240 (match-beginning 0))
20241 (and (search-forward "#+END_SRC" nil t)
20242 (match-beginning 0))) clist)))
20245 ;; Now the small context
20247 ((org-at-timestamp-p)
20248 (push (org-point-in-group p 0 :timestamp) clist))
20249 ((memq 'org-link faces)
20251 (previous-single-property-change p 'face)
20252 (next-single-property-change p 'face)) clist))
20253 ((memq 'org-special-keyword faces)
20254 (push (list :keyword
20255 (previous-single-property-change p 'face)
20256 (next-single-property-change p 'face)) clist))
20258 (push (org-point-in-group p 0 :target) clist)
20259 (goto-char (1- (match-beginning 0)))
20260 (if (looking-at org-radio-target-regexp)
20261 (push (org-point-in-group p 0 :radio-target) clist))
20263 ((setq o (car (delq nil
20266 (if (memq x org-latex-fragment-image-overlays) x))
20267 (overlays-at (point))))))
20268 (push (list :latex-fragment
20269 (overlay-start o) (overlay-end o)) clist)
20270 (push (list :latex-preview
20271 (overlay-start o) (overlay-end o)) clist))
20272 ((org-inside-LaTeX-fragment-p)
20273 ;; FIXME: positions wrong.
20274 (push (list :latex-fragment (point) (point)) clist)))
20276 (setq clist (nreverse (delq nil clist)))
20279 ;; FIXME: Compare with at-regexp-p Do we need both?
20280 (defun org-in-regexp (re &optional nlines visually)
20281 "Check if point is inside a match of regexp.
20282 Normally only the current line is checked, but you can include NLINES extra
20283 lines both before and after point into the search.
20284 If VISUALLY is set, require that the cursor is not after the match but
20285 really on, so that the block visually is on the match."
20287 (let ((pos (point))
20288 (eol (point-at-eol (+ 1 (or nlines 0))))
20289 (inc (if visually 1 0)))
20291 (beginning-of-line (- 1 (or nlines 0)))
20292 (while (re-search-forward re eol t)
20293 (if (and (<= (match-beginning 0) pos)
20294 (>= (+ inc (match-end 0)) pos))
20295 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
20297 (defun org-at-regexp-p (regexp)
20298 "Is point inside a match of REGEXP in the current line?"
20301 (let ((pos (point)) (end (point-at-eol)))
20302 (beginning-of-line 1)
20303 (while (re-search-forward regexp end t)
20304 (if (and (<= (match-beginning 0) pos)
20305 (>= (match-end 0) pos))
20309 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
20310 "Non-nil when point is between matches of START-RE and END-RE.
20312 Also return a non-nil value when point is on one of the matches.
20314 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
20315 buffer positions. Default values are the positions of headlines
20316 surrounding the point.
20318 The functions returns a cons cell whose car (resp. cdr) is the
20319 position before START-RE (resp. after END-RE)."
20321 (let ((pos (point))
20322 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
20323 (limit-down (or lim-down (save-excursion (outline-next-heading))))
20326 ;; Point is on a block when on START-RE or if START-RE can be
20327 ;; found before it...
20328 (and (or (org-at-regexp-p start-re)
20329 (re-search-backward start-re limit-up t))
20330 (setq beg (match-beginning 0))
20331 ;; ... and END-RE after it...
20332 (goto-char (match-end 0))
20333 (re-search-forward end-re limit-down t)
20334 (> (setq end (match-end 0)) pos)
20335 ;; ... without another START-RE in-between.
20336 (goto-char (match-beginning 0))
20337 (not (re-search-backward start-re (1+ beg) t))
20339 (cons beg end))))))
20341 (defun org-in-block-p (names)
20342 "Non-nil when point belongs to a block whose name belongs to NAMES.
20344 NAMES is a list of strings containing names of blocks.
20346 Return first block name matched, or nil. Beware that in case of
20347 nested blocks, the returned name may not belong to the closest
20351 (let ((case-fold-search t)
20352 (lim-up (save-excursion (outline-previous-heading)))
20353 (lim-down (save-excursion (outline-next-heading))))
20354 (mapc (lambda (name)
20355 (let ((n (regexp-quote name)))
20356 (when (org-between-regexps-p
20357 (concat "^[ \t]*#\\+begin_" n)
20358 (concat "^[ \t]*#\\+end_" n)
20364 (defun org-occur-in-agenda-files (regexp &optional nlines)
20365 "Call `multi-occur' with buffers for all agenda files."
20366 (interactive "sOrg-files matching: \np")
20367 (let* ((files (org-agenda-files))
20368 (tnames (mapcar 'file-truename files))
20369 (extra org-agenda-text-search-extra-files)
20371 (when (eq (car extra) 'agenda-archives)
20372 (setq extra (cdr extra))
20373 (setq files (org-add-archive-files files)))
20374 (while (setq f (pop extra))
20375 (unless (member (file-truename f) tnames)
20376 (add-to-list 'files f 'append)
20377 (add-to-list 'tnames (file-truename f) 'append)))
20379 (mapcar (lambda (x)
20380 (with-current-buffer
20381 (or (get-file-buffer x) (find-file-noselect x))
20387 (if (boundp 'occur-mode-find-occurrence-hook)
20389 (add-hook 'occur-mode-find-occurrence-hook
20391 (when (derived-mode-p 'org-mode)
20394 (defadvice occur-mode-goto-occurrence
20395 (after org-occur-reveal activate)
20396 (and (derived-mode-p 'org-mode) (org-reveal)))
20397 (defadvice occur-mode-goto-occurrence-other-window
20398 (after org-occur-reveal activate)
20399 (and (derived-mode-p 'org-mode) (org-reveal)))
20400 (defadvice occur-mode-display-occurrence
20401 (after org-occur-reveal activate)
20402 (when (derived-mode-p 'org-mode)
20403 (let ((pos (occur-mode-find-occurrence)))
20404 (with-current-buffer (marker-buffer pos)
20409 (defun org-occur-link-in-agenda-files ()
20410 "Create a link and search for it in the agendas.
20411 The link is not stored in `org-stored-links', it is just created
20412 for the search purpose."
20414 (let ((link (condition-case nil
20415 (org-store-link nil)
20416 (error "Unable to create a link to here"))))
20417 (org-occur-in-agenda-files (regexp-quote link))))
20419 (defun org-uniquify (list)
20420 "Remove duplicate elements from LIST."
20422 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
20425 (defun org-delete-all (elts list)
20426 "Remove all elements in ELTS from LIST."
20428 (setq list (delete (pop elts) list)))
20431 (defun org-count (cl-item cl-seq)
20432 "Count the number of occurrences of ITEM in SEQ.
20433 Taken from `count' in cl-seq.el with all keyword arguments removed."
20434 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
20435 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
20436 (while (< cl-start cl-end)
20437 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
20438 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
20439 (setq cl-start (1+ cl-start)))
20442 (defun org-remove-if (predicate seq)
20443 "Remove everything from SEQ that fulfills PREDICATE."
20447 (if (not (funcall predicate e)) (push e res)))
20450 (defun org-remove-if-not (predicate seq)
20451 "Remove everything from SEQ that does not fulfill PREDICATE."
20455 (if (funcall predicate e) (push e res)))
20458 (defun org-reduce (cl-func cl-seq &rest cl-keys)
20459 "Reduce two-argument FUNCTION across SEQ.
20460 Taken from `reduce' in cl-seq.el with all keyword arguments but
20461 \":initial-value\" removed."
20462 (let ((cl-accum (cond ((memq :initial-value cl-keys)
20463 (cadr (memq :initial-value cl-keys)))
20464 (cl-seq (pop cl-seq))
20465 (t (funcall cl-func)))))
20467 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
20470 (defun org-back-over-empty-lines ()
20471 "Move backwards over whitespace, to the beginning of the first empty line.
20472 Returns the number of empty lines passed."
20473 (let ((pos (point)))
20474 (if (cdr (assoc 'heading org-blank-before-new-entry))
20475 (skip-chars-backward " \t\n\r")
20477 (forward-line -1)))
20478 (beginning-of-line 2)
20479 (goto-char (min (point) pos))
20480 (count-lines (point) pos)))
20482 (defun org-skip-whitespace ()
20483 (skip-chars-forward " \t\n\r"))
20485 (defun org-point-in-group (point group &optional context)
20486 "Check if POINT is in match-group GROUP.
20487 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
20488 match. If the match group does not exist or point is not inside it,
20490 (and (match-beginning group)
20491 (>= point (match-beginning group))
20492 (<= point (match-end group))
20494 (list context (match-beginning group) (match-end group))
20497 (defun org-switch-to-buffer-other-window (&rest args)
20498 "Switch to buffer in a second window on the current frame.
20499 In particular, do not allow pop-up frames.
20500 Returns the newly created buffer."
20501 (let (pop-up-frames special-display-buffer-names special-display-regexps
20502 special-display-function)
20503 (apply 'switch-to-buffer-other-window args)))
20505 (defun org-combine-plists (&rest plists)
20506 "Create a single property list from all plists in PLISTS.
20507 The process starts by copying the first list, and then setting properties
20508 from the other lists. Settings in the last list are the most significant
20509 ones and overrule settings in the other lists."
20510 (let ((rtn (copy-sequence (pop plists)))
20513 (setq ls (pop plists))
20515 (setq p (pop ls) v (pop ls))
20516 (setq rtn (plist-put rtn p v))))
20519 (defun org-replace-escapes (string table)
20520 "Replace %-escapes in STRING with values in TABLE.
20521 TABLE is an association list with keys like \"%a\" and string values.
20522 The sequences in STRING may contain normal field width and padding information,
20523 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
20524 so values can contain further %-escapes if they are define later in TABLE."
20525 (let ((tbl (copy-alist table))
20526 (case-fold-search nil)
20529 (while (setq e (pop tbl))
20530 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
20531 (when (and (cdr e) (string-match re (cdr e)))
20532 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
20534 (add-text-properties 0 3 (list 'sref sref) safe)
20535 (setcdr e (replace-match safe t t (cdr e)))))
20536 (while (string-match re string)
20537 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
20539 (setq string (replace-match rpl t t string))))
20540 (while (setq pchg (next-property-change pchg string))
20541 (let ((sref (get-text-property pchg 'sref string)))
20542 (when (and sref (string-match "SREF" string pchg))
20543 (setq string (replace-match sref t t string)))))
20546 (defun org-sublist (list start end)
20547 "Return a section of LIST, from START to END.
20548 Counting starts at 1."
20549 (let (rtn (c start))
20550 (setq list (nthcdr (1- start) list))
20551 (while (and list (<= c end))
20552 (push (pop list) rtn)
20556 (defun org-find-base-buffer-visiting (file)
20557 "Like `find-buffer-visiting' but always return the base buffer and
20558 not an indirect buffer."
20559 (let ((buf (or (get-file-buffer file)
20560 (find-buffer-visiting file))))
20562 (or (buffer-base-buffer buf) buf)
20565 (defun org-image-file-name-regexp (&optional extensions)
20566 "Return regexp matching the file names of images.
20567 If EXTENSIONS is given, only match these."
20568 (if (and (not extensions) (fboundp 'image-file-name-regexp))
20569 (image-file-name-regexp)
20570 (let ((image-file-name-extensions
20572 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
20573 "xbm" "xpm" "pbm" "pgm" "ppm"))))
20575 (regexp-opt (nconc (mapcar 'upcase
20576 image-file-name-extensions)
20577 image-file-name-extensions)
20581 (defun org-file-image-p (file &optional extensions)
20582 "Return non-nil if FILE is an image."
20584 (string-match (org-image-file-name-regexp extensions) file)))
20586 (defun org-get-cursor-date ()
20587 "Return the date at cursor in as a time.
20588 This works in the calendar and in the agenda, anywhere else it just
20589 returns the current time."
20590 (let (date day defd)
20592 ((eq major-mode 'calendar-mode)
20593 (setq date (calendar-cursor-to-date)
20594 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
20595 ((eq major-mode 'org-agenda-mode)
20596 (setq day (get-text-property (point) 'day))
20598 (setq date (calendar-gregorian-from-absolute day)
20599 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
20601 (or defd (current-time))))
20603 (defvar org-agenda-action-marker (make-marker)
20604 "Marker pointing to the entry for the next agenda action.")
20606 (defun org-mark-entry-for-agenda-action ()
20607 "Mark the current entry as target of an agenda action.
20608 Agenda actions are actions executed from the agenda with the key `k',
20609 which make use of the date at the cursor."
20611 (move-marker org-agenda-action-marker
20612 (save-excursion (org-back-to-heading t) (point))
20615 "Entry marked for action; press `k' at desired date in agenda or calendar"))
20617 (defun org-mark-subtree ()
20618 "Mark the current subtree.
20619 This puts point at the start of the current subtree, and mark at the end.
20621 If point is in an inline task, mark that task instead."
20623 (let ((inline-task-p
20624 (and (featurep 'org-inlinetask)
20625 (org-inlinetask-in-task-p)))
20627 ;; Get beginning of subtree
20629 (inline-task-p (org-inlinetask-goto-beginning))
20630 ((org-at-heading-p) (beginning-of-line))
20631 (t (org-with-limited-levels (outline-previous-visible-heading 1))))
20635 (org-inlinetask-goto-end)
20636 (org-end-of-subtree))
20638 (push-mark (point) nil t)
20643 (defun org-indent-line ()
20644 "Indent line depending on context."
20646 (let* ((pos (point))
20647 (itemp (org-at-item-p))
20648 (case-fold-search t)
20649 (org-drawer-regexp (or org-drawer-regexp "\000"))
20650 (inline-task-p (and (featurep 'org-inlinetask)
20651 (org-inlinetask-in-task-p)))
20652 (inline-re (and inline-task-p
20653 (org-inlinetask-outline-regexp)))
20655 (beginning-of-line 1)
20658 ((looking-at org-outline-regexp) (setq column 0))
20660 ((looking-at "#\\+include:") (setq column 0))
20661 ;; Footnote definition
20662 ((looking-at org-footnote-definition-re) (setq column 0))
20663 ;; Literal examples
20664 ((looking-at "[ \t]*:\\( \\|$\\)")
20665 (setq column (org-get-indentation))) ; do nothing
20667 ((ignore-errors (goto-char (org-in-item-p)))
20668 (setq column (if itemp
20669 (org-get-indentation)
20670 (org-list-item-body-column (point))))
20673 ((and (looking-at "[ \t]*:END:")
20674 (save-excursion (re-search-backward org-drawer-regexp nil t)))
20676 (goto-char (1- (match-beginning 1)))
20677 (setq column (current-column))))
20679 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
20681 (re-search-backward
20682 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
20683 (setq column (org-get-indentation (match-string 0))))
20684 ((and (not (looking-at "[ \t]*#\\+begin_"))
20685 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
20687 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
20689 (cond ((equal (downcase (match-string 1)) "src")
20690 ;; src blocks: let `org-edit-src-exit' handle them
20691 (org-get-indentation))
20692 ((equal (downcase (match-string 1)) "example")
20693 (max (org-get-indentation)
20694 (org-get-indentation (match-string 0))))
20696 (org-get-indentation (match-string 0))))))
20697 ;; This line has nothing special, look at the previous relevant
20698 ;; line to compute indentation
20700 (beginning-of-line 0)
20701 (while (and (not (bobp))
20702 (not (looking-at org-drawer-regexp))
20703 ;; When point started in an inline task, do not move
20704 ;; above task starting line.
20705 (not (and inline-task-p (looking-at inline-re)))
20706 ;; Skip drawers, blocks, empty lines, verbatim,
20707 ;; comments, tables, footnotes definitions, lists,
20709 (or (and (looking-at "[ \t]*:END:")
20710 (re-search-backward org-drawer-regexp nil t))
20711 (and (looking-at "[ \t]*#\\+end_")
20712 (re-search-backward "[ \t]*#\\+begin_"nil t))
20713 (looking-at "[ \t]*[\n:#|]")
20714 (looking-at org-footnote-definition-re)
20715 (and (ignore-errors (goto-char (org-in-item-p)))
20717 (org-list-get-top-point (org-list-struct))))
20718 (and (not inline-task-p)
20719 (featurep 'org-inlinetask)
20720 (org-inlinetask-in-task-p)
20721 (or (org-inlinetask-goto-beginning) t))))
20722 (beginning-of-line 0))
20724 ;; There was an heading above.
20725 ((looking-at "\\*+[ \t]+")
20726 (if (not org-adapt-indentation)
20728 (goto-char (match-end 0))
20729 (setq column (current-column))))
20730 ;; A drawer had started and is unfinished
20731 ((looking-at org-drawer-regexp)
20732 (goto-char (1- (match-beginning 1)))
20733 (setq column (current-column)))
20734 ;; Else, nothing noticeable found: get indentation and go on.
20735 (t (setq column (org-get-indentation))))))
20736 ;; Now apply indentation and move cursor accordingly
20738 (if (<= (current-column) (current-indentation))
20739 (org-indent-line-to column)
20740 (save-excursion (org-indent-line-to column)))
20741 ;; Special polishing for properties, see `org-property-format'
20742 (setq column (current-column))
20743 (beginning-of-line 1)
20745 "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
20746 (replace-match (concat (match-string 1)
20747 (format org-property-format
20748 (match-string 2) (match-string 3)))
20750 (org-move-to-column column)
20751 (when (and orgstruct-is-++ (eq pos (point)))
20752 (org-let org-fb-vars
20753 '(indent-according-to-mode)))))
20755 (defun org-indent-drawer ()
20756 "Indent the drawer at point."
20759 (e (and (save-excursion (re-search-forward ":END:" nil t))
20764 (when (overlays-at (point))
20765 (member 'invisible (overlay-properties
20766 (car (overlays-at (point)))))))))
20767 (when folded (org-cycle))
20768 (indent-for-tab-command)
20769 (while (and (move-beginning-of-line 2) (< (point) e))
20770 (indent-for-tab-command))
20772 (when folded (org-cycle)))
20773 (message "Drawer at point indented"))
20775 (defun org-indent-block ()
20776 "Indent the block at point."
20779 (case-fold-search t)
20780 (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
20785 (when (overlays-at (point))
20786 (member 'invisible (overlay-properties
20787 (car (overlays-at (point)))))))))
20788 (when folded (org-cycle))
20789 (indent-for-tab-command)
20790 (while (and (move-beginning-of-line 2) (< (point) e))
20791 (indent-for-tab-command))
20793 (when folded (org-cycle)))
20794 (message "Block at point indented"))
20796 (defun org-indent-region (start end)
20800 (let ((line-end (org-current-line end)))
20802 (while (< (org-current-line) line-end)
20803 (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
20804 (t (call-interactively 'org-indent-line)))
20805 (move-beginning-of-line 2)))))
20810 ;; We use our own fill-paragraph and auto-fill functions. These
20811 ;; functions will shadow `fill-prefix' (computed internally with
20812 ;; `org-fill-context-prefix') and pass through to
20813 ;; `fill-region-as-paragraph' and `do-auto-fill' as appropriate.
20815 (defun org-set-autofill-regexps ()
20817 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
20818 ;; Prevent auto-fill from inserting unwanted new items.
20819 (when (boundp 'fill-nobreak-predicate)
20821 'fill-nobreak-predicate
20823 (append fill-nobreak-predicate
20824 '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
20825 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
20826 (org-set-local 'align-mode-rules-list
20827 '((org-in-buffer-settings
20828 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
20829 (modes . '(org-mode))))))
20831 (defun org-fill-item-nobreak-p ()
20832 "Non-nil when a line break at point would insert a new item."
20833 (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
20835 (defun org-fill-line-break-nobreak-p ()
20836 "Non-nil when a line break at point would create an Org line break."
20838 (skip-chars-backward "[ \t]")
20839 (skip-chars-backward "\\\\")
20840 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
20842 (defun org-fill-context-prefix (p)
20843 "Compute a fill prefix for the line at point P.
20844 Return fill prefix, as a string, or nil if current line isn't
20845 meant to be filled."
20848 (beginning-of-line)
20849 (let* ((element (org-element-at-point))
20850 (type (org-element-type element))
20853 (goto-char (org-element-property :begin element))
20854 (while (looking-at org-element--affiliated-re) (forward-line))
20856 (unless (< p post-affiliated)
20858 (comment (looking-at "[ \t]*# ?") (match-string 0))
20859 (footnote-definition "")
20861 (make-string (org-list-item-body-column
20862 (org-element-property :begin element))
20865 ;; Fill prefix is usually the same as the current line,
20866 ;; except if the paragraph is at the beginning of an item.
20867 (let ((parent (org-element-property :parent element)))
20868 (cond ((eq (org-element-type parent) 'item)
20869 (make-string (org-list-item-body-column
20870 (org-element-property :begin parent))
20872 ((looking-at "\\s-+") (match-string 0))
20874 ((comment-block verse-block)
20875 ;; Only fill contents if P is within block boundaries.
20876 (let* ((cbeg (save-excursion (goto-char post-affiliated)
20879 (cend (save-excursion
20880 (goto-char (org-element-property :end element))
20881 (skip-chars-backward " \r\t\n")
20882 (line-beginning-position))))
20883 (when (and (>= p cbeg) (< p cend))
20884 (if (looking-at "\\s-+") (match-string 0) "")))))))))
20886 (defun org-fill-paragraph (&optional justify)
20887 "Fill element at point, when applicable.
20889 This function only applies to comment blocks, comments, example
20890 blocks, paragraphs and verse blocks. Also, as a special case,
20891 re-align table when point is at one.
20893 If JUSTIFY is non-nil (interactively, with prefix argument),
20894 justify as well. If `sentence-end-double-space' is non-nil, then
20895 period followed by one space does not end a sentence, so don't
20896 break a line there. The variable `fill-column' controls the
20899 For convenience, when point is at a plain list, an item or
20900 a footnote definition, try to fill the first paragraph within."
20901 ;; Falls back on message-fill-paragraph when necessary
20902 (if (and (derived-mode-p 'message-mode)
20903 (or (not (message-in-body-p))
20904 (save-excursion (move-beginning-of-line 1)
20905 (looking-at "> "))))
20906 (message-fill-paragraph)
20908 ;; Move to end of line in order to get the first paragraph within
20909 ;; a plain list or a footnote definition.
20911 (let ((element (org-element-at-point)))
20912 ;; First check if point is in a blank line at the beginning of the
20913 ;; buffer. In that case, ignore filling.
20914 (if (< (point) (org-element-property :begin element)) t
20915 (case (org-element-type element)
20916 ;; Align Org tables, leave table.el tables as-is.
20917 (table-row (org-table-align) t)
20919 (when (eq (org-element-property :type element) 'org)
20922 ;; Elements that may contain `line-break' type objects.
20923 ((paragraph verse-block)
20924 (let ((beg (org-element-property :contents-begin element))
20925 (end (org-element-property :contents-end element))
20926 (type (org-element-type element)))
20927 ;; Do nothing if point is at an affiliated keyword or at
20928 ;; verse block markers.
20929 (if (or (< (point) beg)
20930 (and (eq type 'verse-block) (>= (point) end)))
20932 ;; At a verse block, first narrow to current "paragraph"
20933 ;; and set current element to that paragraph.
20935 (when (eq type 'verse-block)
20936 (narrow-to-region beg end)
20938 (let ((bol-pos (point-at-bol)))
20939 (re-search-backward
20940 org-element-paragraph-separate nil 'm)
20941 (unless (or (bobp) (= (point-at-bol) bol-pos))
20943 (setq element (org-element-paragraph-parser end)
20944 beg (org-element-property :contents-begin element)
20945 end (org-element-property
20946 :contents-end element)))))
20947 ;; Fill paragraph, taking line breaks into consideration.
20948 ;; For that, slice the paragraph using line breaks as
20949 ;; separators, and fill the parts in reverse order to
20950 ;; avoid messing with markers.
20955 (let ((fill-prefix (org-fill-context-prefix pos)))
20956 (fill-region-as-paragraph pos (point) justify))
20958 ;; Find the list of ending positions for line breaks
20959 ;; in the current paragraph. Add paragraph beginning
20960 ;; to include first slice.
20964 (org-element--parse-objects
20965 beg end nil org-element-all-objects)
20967 (lambda (lb) (org-element-property :end lb))))))))
20969 ;; Contents of `comment-block' type elements should be filled as
20972 (let ((fill-prefix (org-fill-context-prefix (point))))
20974 (fill-region-as-paragraph
20976 (goto-char (org-element-property :begin element))
20977 (while (looking-at org-element--affiliated-re) (forward-line))
20981 (goto-char (org-element-property :end element))
20982 (skip-chars-backward " \r\t\n")
20983 (line-beginning-position))
20985 ;; Fill comments, indented or not.
20987 (let ((fill-prefix (org-fill-context-prefix (point))))
20989 (fill-region-as-paragraph
20991 (goto-char (org-element-property :begin element))
20992 (while (looking-at org-element--affiliated-re) (forward-line))
20995 (goto-char (org-element-property :end element))
20996 (skip-chars-backward " \r\t\n")
20997 (line-end-position))))))
20998 ;; Ignore every other element.
20999 (otherwise t)))))))
21001 (defun org-auto-fill-function ()
21002 "Auto-fill function."
21003 ;; Check if auto-filling is meaningful before computing fill prefix.
21004 (let ((fc (current-fill-column)))
21005 (when (and fc (> (current-column) fc))
21006 (let ((fill-prefix (org-fill-context-prefix (point))))
21007 (when fill-prefix (do-auto-fill))))))
21012 ;; Since difference between comments and keywords is subtle, we cannot
21013 ;; trust `comment-only-p' when applying `comment-dwim'. Hence, both
21014 ;; `comment-region-function' and `uncomment-region-function' point to
21015 ;; `org-comment-or-uncomment-region', which can tell when region only
21016 ;; contains comments or not.
21018 (defun org-insert-comment ()
21019 "Insert an empty comment above current line.
21020 If the line is empty, insert comment at its beginning."
21021 (beginning-of-line)
21022 (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
21026 (defun org-comment-or-uncomment-region (beg end &rest ignore)
21027 "Comment or uncomment each non-blank line in the region.
21028 Uncomment each non-blank line between BEG and END if it only
21029 contains commented lines. Otherwise, comment them."
21032 (skip-chars-forward " \r\t\n" end)
21033 (beginning-of-line)
21035 ;; UNCOMMENTP is non-nil when every non blank line between
21036 ;; BEG and END is a comment.
21038 (while (progn (and (not (eobp))
21039 (let ((element (org-element-at-point)))
21040 (and (eq (org-element-type element) 'comment)
21041 (goto-char (org-element-property
21042 :end element)))))))
21044 ;; Remove or adding comment markers is going to change end
21045 ;; position so make it a marker.
21046 (end (copy-marker end)))
21047 (while (< (point) end)
21048 (unless (looking-at "\\s-*$")
21049 (if (not uncommentp) (progn (back-to-indentation) (insert "# "))
21050 ;; Only comments and blank lines in region: uncomment it.
21051 (looking-at "[ \t]*\\(# ?\\)")
21052 (replace-match "" nil nil nil 1)))
21054 (set-marker end nil))))
21059 (defun org-toggle-fixed-width-section (arg)
21060 "Toggle the fixed-width export.
21061 If there is no active region, the QUOTE keyword at the current headline is
21062 inserted or removed. When present, it causes the text between this headline
21063 and the next to be exported as fixed-width text, and unmodified.
21064 If there is an active region, this command adds or removes a colon as the
21065 first character of this line. If the first character of a line is a colon,
21066 this line is also exported in fixed-width font."
21069 (regionp (org-region-active-p))
21070 (beg (if regionp (region-beginning) (point)))
21071 (end (if regionp (region-end)))
21072 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
21073 (case-fold-search nil)
21074 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
21079 (setq cc (current-column))
21080 (beginning-of-line 1)
21081 (setq off (looking-at re))
21082 (while (> nlines 0)
21083 (setq nlines (1- nlines))
21084 (beginning-of-line 1)
21087 (org-move-to-column cc t)
21090 ((and off (looking-at re))
21091 (replace-match "" t t nil 1))
21092 ((not off) (org-move-to-column cc t) (insert ": ")))
21095 (org-back-to-heading)
21097 ((looking-at (format org-heading-keyword-regexp-format
21099 (goto-char (match-end 1))
21100 (looking-at (concat " +" org-quote-string))
21101 (replace-match "" t t)
21102 (when (eolp) (insert " ")))
21103 ((looking-at org-outline-regexp)
21104 (goto-char (match-end 0))
21105 (insert org-quote-string " ")))))))
21107 (defun org-reftex-citation ()
21108 "Use reftex-citation to insert a citation into the buffer.
21109 This looks for a line like
21111 #+BIBLIOGRAPHY: foo plain option:-d
21113 and derives from it that foo.bib is the bibliography file relevant
21114 for this document. It then installs the necessary environment for RefTeX
21115 to work in this buffer and calls `reftex-citation' to insert a citation
21118 Export of such citations to both LaTeX and HTML is handled by the contributed
21119 package org-exp-bibtex by Taru Karttunen."
21121 (let ((reftex-docstruct-symbol 'rds)
21122 (reftex-cite-format "\\cite{%l}")
21127 (let ((case-fold-search t)
21128 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
21129 (if (not (save-excursion
21130 (or (re-search-forward re nil t)
21131 (re-search-backward re nil t))))
21132 (error "No bibliography defined in file")
21133 (setq bib (concat (match-string 1) ".bib")
21134 rds (list (list 'bib bib)))))))
21135 (call-interactively 'reftex-citation)))
21137 ;;;; Functions extending outline functionality
21139 (defun org-beginning-of-line (&optional arg)
21140 "Go to the beginning of the current line. If that is invisible, continue
21141 to a visible line beginning. This makes the function of C-a more intuitive.
21142 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
21143 first attempt, and only move to after the tags when the cursor is already
21144 beyond the end of the headline."
21146 (let ((pos (point))
21147 (special (if (consp org-special-ctrl-a/e)
21148 (car org-special-ctrl-a/e)
21149 org-special-ctrl-a/e))
21151 (if (org-bound-and-true-p line-move-visual)
21152 (beginning-of-visual-line 1)
21153 (beginning-of-line 1))
21154 (if (and arg (fboundp 'move-beginning-of-line))
21155 (call-interactively 'move-beginning-of-line)
21159 (if (org-truely-invisible-p)
21160 (while (and (not (bobp)) (org-truely-invisible-p))
21162 (beginning-of-line 1))
21163 (forward-char 1))))
21166 ((and (looking-at org-complex-heading-regexp)
21167 (= (char-after (match-end 1)) ?\ ))
21168 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
21172 (cond ((> pos refpos) refpos)
21173 ((= pos (point)) refpos)
21175 (cond ((> pos (point)) (point))
21176 ((not (eq last-command this-command)) (point))
21179 ;; Being at an item and not looking at an the item means point
21180 ;; was previously moved to beginning of a visual line, which
21181 ;; doesn't contain the item. Therefore, do nothing special,
21183 (when (looking-at org-list-full-item-re)
21184 ;; Set special position at first white space character after
21185 ;; bullet, and check-box, if any.
21186 (let ((after-bullet
21187 (let ((box (match-end 3)))
21188 (if (not box) (match-end 1)
21189 (let ((after (char-after box)))
21190 (if (and after (= after ? )) (1+ box) box))))))
21191 ;; Special case: Move point to special position when
21192 ;; currently after it or at beginning of line.
21194 (when (or (> pos after-bullet) (= (point) pos))
21195 (goto-char after-bullet))
21196 ;; Reversed case: Move point to special position when
21197 ;; point was already at beginning of line and command is
21199 (when (and (= (point) pos) (eq last-command this-command))
21200 (goto-char after-bullet))))))))
21202 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21204 (defun org-end-of-line (&optional arg)
21205 "Go to the end of the line.
21206 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
21207 first attempt, and only move to after the tags when the cursor is already
21208 beyond the end of the headline."
21210 (let ((special (if (consp org-special-ctrl-a/e)
21211 (cdr org-special-ctrl-a/e)
21212 org-special-ctrl-a/e)))
21214 ((or (not special) arg
21215 (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
21216 (call-interactively
21217 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
21218 ((fboundp 'move-end-of-line) 'move-end-of-line)
21219 (t 'end-of-line))))
21220 ((org-at-heading-p)
21221 (let ((pos (point)))
21222 (beginning-of-line 1)
21223 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
21225 (if (or (< pos (match-beginning 1))
21226 (= pos (match-end 0)))
21227 (goto-char (match-beginning 1))
21228 (goto-char (match-end 0)))
21229 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
21230 (goto-char (match-end 0))
21231 (goto-char (match-beginning 1))))
21232 (call-interactively (if (fboundp 'move-end-of-line)
21236 (move-end-of-line 1)
21237 (when (overlays-at (1- (point))) (backward-char 1)))
21238 ;; At an item: Move before any hidden text.
21239 (t (call-interactively
21240 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
21241 ((fboundp 'move-end-of-line) 'move-end-of-line)
21242 (t 'end-of-line)))))
21244 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21246 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
21247 (define-key org-mode-map "\C-e" 'org-end-of-line)
21249 (defun org-backward-sentence (&optional arg)
21250 "Go to beginning of sentence, or beginning of table field.
21251 This will call `backward-sentence' or `org-table-beginning-of-field',
21252 depending on context."
21255 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
21256 (t (call-interactively 'backward-sentence))))
21258 (defun org-forward-sentence (&optional arg)
21259 "Go to end of sentence, or end of table field.
21260 This will call `forward-sentence' or `org-table-end-of-field',
21261 depending on context."
21264 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
21265 (t (call-interactively 'forward-sentence))))
21267 (define-key org-mode-map "\M-a" 'org-backward-sentence)
21268 (define-key org-mode-map "\M-e" 'org-forward-sentence)
21270 (defun org-kill-line (&optional arg)
21271 "Kill line, to tags or end of line."
21274 ((or (not org-special-ctrl-k)
21276 (not (org-at-heading-p)))
21277 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
21278 org-ctrl-k-protect-subtree)
21279 (if (or (eq org-ctrl-k-protect-subtree 'error)
21280 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
21281 (error "C-k aborted - would kill hidden subtree")))
21282 (call-interactively
21283 (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
21284 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
21285 (kill-region (point) (match-beginning 1))
21286 (org-set-tags nil t))
21287 (t (kill-region (point) (point-at-eol)))))
21289 (define-key org-mode-map "\C-k" 'org-kill-line)
21291 (defun org-yank (&optional arg)
21292 "Yank. If the kill is a subtree, treat it specially.
21293 This command will look at the current kill and check if is a single
21294 subtree, or a series of subtrees[1]. If it passes the test, and if the
21295 cursor is at the beginning of a line or after the stars of a currently
21296 empty headline, then the yank is handled specially. How exactly depends
21297 on the value of the following variables, both set by default.
21299 org-yank-folded-subtrees
21300 When set, the subtree(s) will be folded after insertion, but only
21301 if doing so would now swallow text after the yanked text.
21303 org-yank-adjusted-subtrees
21304 When set, the subtree will be promoted or demoted in order to
21305 fit into the local outline tree structure, which means that the level
21306 will be adjusted so that it becomes the smaller one of the two
21307 *visible* surrounding headings.
21309 Any prefix to this command will cause `yank' to be called directly with
21310 no special treatment. In particular, a simple \\[universal-argument] prefix \
21312 plainly yank the text as it is.
21314 \[1] The test checks if the first non-white line is a heading
21315 and if there are no other headings with fewer stars."
21317 (org-yank-generic 'yank arg))
21319 (defun org-yank-generic (command arg)
21320 "Perform some yank-like command.
21322 This function implements the behavior described in the `org-yank'
21323 documentation. However, it has been generalized to work for any
21324 interactive command with similar behavior."
21326 ;; pretend to be command COMMAND
21327 (setq this-command command)
21330 (call-interactively command)
21332 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
21333 (and (org-kill-is-subtree-p)
21335 (and (looking-at "[ \t]*$")
21338 (buffer-substring (point-at-bol) (point)))))))
21341 ((and subtreep org-yank-folded-subtrees)
21342 (let ((beg (point))
21344 (if (and subtreep org-yank-adjusted-subtrees)
21345 (org-paste-subtree nil nil 'for-yank)
21346 (call-interactively command))
21350 (when (and (bolp) subtreep
21351 (not (setq swallowp
21352 (org-yank-folding-would-swallow-text beg end))))
21353 (org-with-limited-levels
21354 (or (looking-at org-outline-regexp)
21355 (re-search-forward org-outline-regexp-bol end t))
21356 (while (and (< (point) end) (looking-at org-outline-regexp))
21358 (org-cycle-show-empty-lines 'folded)
21359 (condition-case nil
21360 (outline-forward-same-level 1)
21361 (error (goto-char end))))))
21364 "Inserted text not folded because that would swallow text"))
21367 (skip-chars-forward " \t\n\r")
21368 (beginning-of-line 1)
21369 (push-mark beg 'nomsg)))
21370 ((and subtreep org-yank-adjusted-subtrees)
21371 (let ((beg (point-at-bol)))
21372 (org-paste-subtree nil nil 'for-yank)
21373 (push-mark beg 'nomsg)))
21375 (call-interactively command))))))
21377 (defun org-yank-folding-would-swallow-text (beg end)
21378 "Would hide-subtree at BEG swallow any text after END?"
21380 (org-with-limited-levels
21383 (when (or (looking-at org-outline-regexp)
21384 (re-search-forward org-outline-regexp-bol end t))
21385 (setq level (org-outline-level)))
21387 (skip-chars-forward " \t\r\n\v\f")
21389 (and (bolp) (looking-at org-outline-regexp)
21390 (<= (org-outline-level) level)))
21391 nil ; Nothing would be swallowed
21392 t))))) ; something would swallow
21394 (define-key org-mode-map "\C-y" 'org-yank)
21396 (defun org-truely-invisible-p ()
21397 "Check if point is at a character currently not visible.
21398 This version does not only check the character property, but also
21400 ;; Early versions of noutline don't have `outline-invisible-p'.
21401 (if (org-bound-and-true-p visible-mode)
21403 (outline-invisible-p)))
21405 (defun org-invisible-p2 ()
21406 "Check if point is at a character currently not visible."
21408 (if (and (eolp) (not (bobp))) (backward-char 1))
21409 ;; Early versions of noutline don't have `outline-invisible-p'.
21410 (outline-invisible-p)))
21412 (defun org-back-to-heading (&optional invisible-ok)
21413 "Call `outline-back-to-heading', but provide a better error message."
21414 (condition-case nil
21415 (outline-back-to-heading invisible-ok)
21416 (error (error "Before first headline at position %d in buffer %s"
21417 (point) (current-buffer)))))
21419 (defun org-beginning-of-defun ()
21420 "Go to the beginning of the subtree, i.e. back to the heading."
21421 (org-back-to-heading))
21422 (defun org-end-of-defun ()
21423 "Go to the end of the subtree."
21424 (org-end-of-subtree nil t))
21426 (defun org-before-first-heading-p ()
21427 "Before first heading?"
21430 (null (re-search-backward org-outline-regexp-bol nil t))))
21432 (defun org-at-heading-p (&optional ignored)
21433 (outline-on-heading-p t))
21434 ;; Compatibility alias with Org versions < 7.8.03
21435 (defalias 'org-on-heading-p 'org-at-heading-p)
21437 (defun org-at-comment-p nil
21438 "Is cursor in a line starting with a # character?"
21440 (beginning-of-line)
21441 (looking-at "^#")))
21443 (defun org-at-drawer-p nil
21444 "Is cursor at a drawer keyword?"
21446 (move-beginning-of-line 1)
21447 (looking-at org-drawer-regexp)))
21449 (defun org-at-block-p nil
21450 "Is cursor at a block keyword?"
21452 (move-beginning-of-line 1)
21453 (looking-at org-block-regexp)))
21455 (defun org-point-at-end-of-empty-headline ()
21456 "If point is at the end of an empty headline, return t, else nil.
21457 If the heading only contains a TODO keyword, it is still still considered
21459 (and (looking-at "[ \t]*$")
21460 (when org-todo-line-regexp
21462 (beginning-of-line 1)
21463 (let ((case-fold-search nil))
21464 (looking-at org-todo-line-regexp)
21465 (string= (match-string 3) ""))))))
21467 (defun org-at-heading-or-item-p ()
21468 (or (org-at-heading-p) (org-at-item-p)))
21470 (defun org-at-target-p ()
21471 (or (org-in-regexp org-radio-target-regexp)
21472 (org-in-regexp org-target-regexp)))
21473 ;; Compatibility alias with Org versions < 7.8.03
21474 (defalias 'org-on-target-p 'org-at-target-p)
21476 (defun org-up-heading-all (arg)
21477 "Move to the heading line of which the present line is a subheading.
21478 This function considers both visible and invisible heading lines.
21479 With argument, move up ARG levels."
21480 (if (fboundp 'outline-up-heading-all)
21481 (outline-up-heading-all arg) ; emacs 21 version of outline.el
21482 (outline-up-heading arg t))) ; emacs 22 version of outline.el
21484 (defun org-up-heading-safe ()
21485 "Move to the heading line of which the present line is a subheading.
21486 This version will not throw an error. It will return the level of the
21487 headline found, or nil if no higher level is found.
21489 Also, this function will be a lot faster than `outline-up-heading',
21490 because it relies on stars being the outline starters. This can really
21491 make a significant difference in outlines with very many siblings."
21492 (let (start-level re)
21493 (org-back-to-heading t)
21494 (setq start-level (funcall outline-level))
21495 (if (equal start-level 1)
21497 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
21498 (if (re-search-backward re nil t)
21499 (funcall outline-level)))))
21501 (defun org-first-sibling-p ()
21502 "Is this heading the first child of its parents?"
21504 (let ((re org-outline-regexp-bol)
21506 (unless (org-at-heading-p t)
21507 (error "Not at a heading"))
21508 (setq level (funcall outline-level))
21510 (if (not (re-search-backward re nil t))
21512 (setq l (funcall outline-level))
21515 (defun org-goto-sibling (&optional previous)
21516 "Goto the next sibling, even if it is invisible.
21517 When PREVIOUS is set, go to the previous sibling instead. Returns t
21518 when a sibling was found. When none is found, return nil and don't
21520 (let ((fun (if previous 're-search-backward 're-search-forward))
21522 (re org-outline-regexp-bol)
21524 (when (condition-case nil (org-back-to-heading t) (error nil))
21525 (setq level (funcall outline-level))
21527 (or previous (forward-char 1))
21528 (while (funcall fun re nil t)
21529 (setq l (funcall outline-level))
21530 (when (< l level) (goto-char pos) (throw 'exit nil))
21531 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
21535 (defun org-show-siblings ()
21536 "Show all siblings of the current headline."
21538 (while (org-goto-sibling) (org-flag-heading nil)))
21540 (while (org-goto-sibling 'previous)
21541 (org-flag-heading nil))))
21543 (defun org-goto-first-child ()
21544 "Goto the first child, even if it is invisible.
21545 Return t when a child was found. Otherwise don't move point and
21547 (let (level (pos (point)) (re org-outline-regexp-bol))
21548 (when (condition-case nil (org-back-to-heading t) (error nil))
21549 (setq level (outline-level))
21551 (if (and (re-search-forward re nil t) (> (outline-level) level))
21552 (progn (goto-char (match-beginning 0)) t)
21553 (goto-char pos) nil))))
21555 (defun org-show-hidden-entry ()
21556 "Show an entry where even the heading is hidden."
21560 (defun org-flag-heading (flag &optional entry)
21561 "Flag the current heading. FLAG non-nil means make invisible.
21562 When ENTRY is non-nil, show the entire entry."
21564 (org-back-to-heading t)
21565 ;; Check if we should show the entire entry
21570 (and (outline-next-heading)
21571 (org-flag-heading nil))))
21572 (outline-flag-region (max (point-min) (1- (point)))
21573 (save-excursion (outline-end-of-heading) (point))
21576 (defun org-get-next-sibling ()
21577 "Move to next heading of the same level, and return point.
21578 If there is no such heading, return nil.
21579 This is like outline-next-sibling, but invisible headings are ok."
21580 (let ((level (funcall outline-level)))
21581 (outline-next-heading)
21582 (while (and (not (eobp)) (> (funcall outline-level) level))
21583 (outline-next-heading))
21584 (if (or (eobp) (< (funcall outline-level) level))
21588 (defun org-get-last-sibling ()
21589 "Move to previous heading of the same level, and return point.
21590 If there is no such heading, return nil."
21591 (let ((opoint (point))
21592 (level (funcall outline-level)))
21593 (outline-previous-heading)
21594 (when (and (/= (point) opoint) (outline-on-heading-p t))
21595 (while (and (> (funcall outline-level) level)
21597 (outline-previous-heading))
21598 (if (< (funcall outline-level) level)
21602 (defun org-end-of-subtree (&optional invisible-OK to-heading)
21603 ;; This contains an exact copy of the original function, but it uses
21604 ;; `org-back-to-heading', to make it work also in invisible
21605 ;; trees. And is uses an invisible-OK argument.
21606 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
21607 ;; Furthermore, when used inside Org, finding the end of a large subtree
21608 ;; with many children and grandchildren etc, this can be much faster
21609 ;; than the outline version.
21610 (org-back-to-heading invisible-OK)
21612 (level (funcall outline-level)))
21613 (if (and (derived-mode-p 'org-mode) (< level 1000))
21614 ;; A true heading (not a plain list item), in Org-mode
21615 ;; This means we can easily find the end by looking
21616 ;; only for the right number of stars. Using a regexp to do
21617 ;; this is so much faster than using a Lisp loop.
21618 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
21620 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
21621 ;; something else, do it the slow way
21622 (while (and (not (eobp))
21623 (or first (> (funcall outline-level) level)))
21625 (outline-next-heading)))
21627 (if (memq (preceding-char) '(?\n ?\^M))
21629 ;; Go to end of line before heading
21631 (if (memq (preceding-char) '(?\n ?\^M))
21632 ;; leave blank line before heading
21633 (forward-char -1))))))
21636 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
21637 "Use Org version in org-mode, for dramatic speed-up."
21638 (if (derived-mode-p 'org-mode)
21640 (org-end-of-subtree nil t)
21641 (unless (eobp) (backward-char 1)))
21644 (defun org-end-of-meta-data-and-drawers ()
21645 "Jump to the first text after meta data and drawers in the current entry.
21646 This will move over empty lines, lines with planning time stamps,
21647 clocking lines, and drawers."
21648 (org-back-to-heading t)
21649 (let ((end (save-excursion (outline-next-heading) (point)))
21650 (re (concat "\\(" org-drawer-regexp "\\)"
21651 "\\|" "[ \t]*" org-keyword-time-regexp)))
21653 (while (re-search-forward re end t)
21654 (if (not (match-end 1))
21655 ;; empty or planning line
21657 ;; a drawer, find the end
21658 (re-search-forward "^[ \t]*:END:" end 'move)
21660 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
21663 (defun org-forward-same-level (arg &optional invisible-ok)
21664 "Move forward to the arg'th subheading at same level as this one.
21665 Stop at the first and last subheadings of a superior heading.
21666 Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
21667 it wil also look at invisible ones."
21669 (org-back-to-heading invisible-ok)
21671 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21672 (re (format "^\\*\\{1,%d\\} " level))
21676 (while (and (re-search-forward re nil 'move)
21677 (setq l (- (match-end 0) (match-beginning 0) 1))
21680 (progn (backward-char 1) (outline-invisible-p)))
21681 (if (< l level) (setq arg 1)))
21682 (setq arg (1- arg)))
21683 (beginning-of-line 1)))
21685 (defun org-backward-same-level (arg &optional invisible-ok)
21686 "Move backward to the arg'th subheading at same level as this one.
21687 Stop at the first and last subheadings of a superior heading."
21689 (org-back-to-heading)
21691 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21692 (re (format "^\\*\\{1,%d\\} " level))
21695 (while (and (re-search-backward re nil 'move)
21696 (setq l (- (match-end 0) (match-beginning 0) 1))
21699 (outline-invisible-p))
21700 (if (< l level) (setq arg 1)))
21701 (setq arg (1- arg)))))
21703 (defun org-show-subtree ()
21704 "Show everything after this heading at deeper levels."
21706 (outline-flag-region
21709 (org-end-of-subtree t t))
21712 (defun org-show-entry ()
21713 "Show the body directly following this heading.
21714 Show the heading too, if it is currently invisible."
21717 (condition-case nil
21719 (org-back-to-heading t)
21720 (outline-flag-region
21721 (max (point-min) (1- (point)))
21723 (if (re-search-forward
21724 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
21725 (match-beginning 1)
21728 (org-cycle-hide-drawers 'children))
21731 (defun org-make-options-regexp (kwds &optional extra)
21732 "Make a regular expression for keyword lines."
21736 (mapconcat 'regexp-quote kwds "\\|")
21737 (if extra (concat "\\|" extra))
21741 ;; Make isearch reveal the necessary context
21742 (defun org-isearch-end ()
21743 "Reveal context after isearch exits."
21744 (when isearch-success ; only if search was successful
21745 (if (featurep 'xemacs)
21746 ;; Under XEmacs, the hook is run in the correct place,
21747 ;; we directly show the context.
21748 (org-show-context 'isearch)
21749 ;; In Emacs the hook runs *before* restoring the overlays.
21750 ;; So we have to use a one-time post-command-hook to do this.
21751 ;; (Emacs 22 has a special variable, see function `org-mode')
21752 (unless (and (boundp 'isearch-mode-end-hook-quit)
21753 isearch-mode-end-hook-quit)
21754 ;; Only when the isearch was not quitted.
21755 (org-add-hook 'post-command-hook 'org-isearch-post-command
21756 'append 'local)))))
21758 (defun org-isearch-post-command ()
21759 "Remove self from hook, and show context."
21760 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
21761 (org-show-context 'isearch))
21764 ;;;; Integration with and fixes for other packages
21768 (defvar org-imenu-markers nil
21769 "All markers currently used by Imenu.")
21770 (make-variable-buffer-local 'org-imenu-markers)
21772 (defun org-imenu-new-marker (&optional pos)
21773 "Return a new marker for use by Imenu, and remember the marker."
21774 (let ((m (make-marker)))
21775 (move-marker m (or pos (point)))
21776 (push m org-imenu-markers)
21779 (defun org-imenu-get-tree ()
21780 "Produce the index for Imenu."
21781 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
21782 (setq org-imenu-markers nil)
21783 (let* ((n org-imenu-depth)
21784 (re (concat "^" (org-get-limited-outline-regexp)))
21785 (subs (make-vector (1+ n) nil))
21791 (goto-char (point-max))
21792 (while (re-search-backward re nil t)
21793 (setq level (org-reduced-level (funcall outline-level)))
21794 (when (and (<= level n)
21795 (looking-at org-complex-heading-regexp))
21796 (setq head (org-link-display-format
21797 (org-match-string-no-properties 4))
21798 m (org-imenu-new-marker))
21799 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
21800 (if (>= level last-level)
21801 (push (cons head m) (aref subs level))
21802 (push (cons head (aref subs (1+ level))) (aref subs level))
21803 (loop for i from (1+ level) to n do (aset subs i nil)))
21804 (setq last-level level)))))
21807 (eval-after-load "imenu"
21809 (add-hook 'imenu-after-jump-hook
21811 (if (derived-mode-p 'org-mode)
21812 (org-show-context 'org-goto))))))
21814 (defun org-link-display-format (link)
21815 "Replace a link with either the description, or the link target
21816 if no description is present"
21818 (if (string-match org-bracket-link-analytic-regexp link)
21819 (replace-match (if (match-end 5)
21820 (match-string 5 link)
21821 (concat (match-string 1 link)
21822 (match-string 3 link)))
21826 (defun org-toggle-link-display ()
21827 "Toggle the literal or descriptive display of links."
21829 (if org-descriptive-links
21830 (progn (org-remove-from-invisibility-spec '(org-link))
21831 (org-restart-font-lock)
21832 (setq org-descriptive-links nil))
21833 (progn (add-to-invisibility-spec '(org-link))
21834 (org-restart-font-lock)
21835 (setq org-descriptive-links t))))
21837 ;; Speedbar support
21839 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
21840 "Overlay marking the agenda restriction line in speedbar.")
21841 (overlay-put org-speedbar-restriction-lock-overlay
21842 'face 'org-agenda-restriction-lock)
21843 (overlay-put org-speedbar-restriction-lock-overlay
21844 'help-echo "Agendas are currently limited to this item.")
21845 (org-detach-overlay org-speedbar-restriction-lock-overlay)
21847 (defun org-speedbar-set-agenda-restriction ()
21848 "Restrict future agenda commands to the location at point in speedbar.
21849 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
21851 (require 'org-agenda)
21852 (let (p m tp np dir txt)
21854 ((setq p (text-property-any (point-at-bol) (point-at-eol)
21856 (setq m (get-text-property p 'org-imenu-marker))
21857 (with-current-buffer (marker-buffer m)
21859 (org-agenda-set-restriction-lock 'subtree)))
21860 ((setq p (text-property-any (point-at-bol) (point-at-eol)
21861 'speedbar-function 'speedbar-find-file))
21862 (setq tp (previous-single-property-change
21863 (1+ p) 'speedbar-function)
21864 np (next-single-property-change
21865 tp 'speedbar-function)
21866 dir (speedbar-line-directory)
21867 txt (buffer-substring-no-properties (or tp (point-min))
21868 (or np (point-max))))
21869 (with-current-buffer (find-file-noselect
21870 (let ((default-directory dir))
21871 (expand-file-name txt)))
21872 (unless (derived-mode-p 'org-mode)
21873 (error "Cannot restrict to non-Org-mode file"))
21874 (org-agenda-set-restriction-lock 'file)))
21875 (t (error "Don't know how to restrict Org-mode's agenda")))
21876 (move-overlay org-speedbar-restriction-lock-overlay
21877 (point-at-bol) (point-at-eol))
21878 (setq current-prefix-arg nil)
21879 (org-agenda-maybe-redo)))
21881 (eval-after-load "speedbar"
21883 (speedbar-add-supported-extension ".org")
21884 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
21885 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
21886 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
21887 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
21888 (add-hook 'speedbar-visiting-tag-hook
21889 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
21891 ;;; Fixes and Hacks for problems with other packages
21893 ;; Make flyspell not check words in links, to not mess up our keymap
21894 (defun org-mode-flyspell-verify ()
21895 "Don't let flyspell put overlays at active buttons, or on
21896 {todo,all-time,additional-option-like}-keywords."
21897 (let ((pos (max (1- (point)) (point-min)))
21898 (word (thing-at-point 'word)))
21899 (and (not (get-text-property pos 'keymap))
21900 (not (get-text-property pos 'org-no-flyspell))
21901 (not (member word org-todo-keywords-1))
21902 (not (member word org-all-time-keywords))
21903 (not (member word org-options-keywords))
21904 (not (member word (mapcar 'car org-startup-options)))
21905 (not (member word org-additional-option-like-keywords-for-flyspell)))))
21907 (defun org-remove-flyspell-overlays-in (beg end)
21908 "Remove flyspell overlays in region."
21909 (and (org-bound-and-true-p flyspell-mode)
21910 (fboundp 'flyspell-delete-region-overlays)
21911 (flyspell-delete-region-overlays beg end))
21912 (add-text-properties beg end '(org-no-flyspell t)))
21914 ;; Make `bookmark-jump' shows the jump location if it was hidden.
21915 (eval-after-load "bookmark"
21916 '(if (boundp 'bookmark-after-jump-hook)
21917 ;; We can use the hook
21918 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
21919 ;; Hook not available, use advice
21920 (defadvice bookmark-jump (after org-make-visible activate)
21921 "Make the position visible."
21922 (org-bookmark-jump-unhide))))
21924 ;; Make sure saveplace shows the location if it was hidden
21925 (eval-after-load "saveplace"
21926 '(defadvice save-place-find-file-hook (after org-make-visible activate)
21927 "Make the position visible."
21928 (org-bookmark-jump-unhide)))
21930 ;; Make sure ecb shows the location if it was hidden
21931 (eval-after-load "ecb"
21932 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
21933 "Make hierarchy visible when jumping into location from ECB tree buffer."
21934 (if (derived-mode-p 'org-mode)
21935 (org-show-context))))
21937 (defun org-bookmark-jump-unhide ()
21938 "Unhide the current position, to show the bookmark location."
21939 (and (derived-mode-p 'org-mode)
21940 (or (outline-invisible-p)
21941 (save-excursion (goto-char (max (point-min) (1- (point))))
21942 (outline-invisible-p)))
21943 (org-show-context 'bookmark-jump)))
21945 ;; Make session.el ignore our circular variable
21946 (eval-after-load "session"
21947 '(add-to-list 'session-globals-exclude 'org-mark-ring))
21949 ;;;; Experimental code
21951 (defun org-closed-in-range ()
21952 "Sparse tree of items closed in a certain time range.
21953 Still experimental, may disappear in the future."
21955 ;; Get the time interval from the user.
21956 (let* ((time1 (org-float-time
21957 (org-read-date nil 'to-time nil "Starting date: ")))
21958 (time2 (org-float-time
21959 (org-read-date nil 'to-time nil "End date:")))
21960 ;; callback function
21961 (callback (lambda ()
21964 (apply 'encode-time
21965 (org-parse-time-string
21966 (match-string 1))))))
21967 ;; check if time in interval
21968 (and (>= time time1) (<= time time2))))))
21969 ;; make tree, check each match with the callback
21970 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
21976 (run-hooks 'org-load-hook)
21978 ;;; org.el ends here