1 ;;; org.el --- Outline-based notes management and organizer
2 ;; Carstens outline-mode for keeping track of everything.
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; 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
78 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
79 (when (fboundp 'defvaralias
)
80 (unless (boundp 'calendar-view-holidays-initially-flag
)
81 (defvaralias 'calendar-view-holidays-initially-flag
82 'view-calendar-holidays-initially
))
83 (unless (boundp 'calendar-view-diary-initially-flag
)
84 (defvaralias 'calendar-view-diary-initially-flag
85 'view-diary-entries-initially
))
86 (unless (boundp 'diary-fancy-buffer
)
87 (defvaralias 'diary-fancy-buffer
'fancy-diary-buffer
)))
89 (require 'outline
) (require 'noutline
)
90 ;; Other stuff we need.
92 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
97 (require 'org-entities
)
102 (require 'org-footnote
)
113 ;; load languages based on value of `org-babel-load-languages'
114 (defvar org-babel-load-languages
)
116 (defun org-babel-do-load-languages (sym value
)
117 "Load the languages defined in `org-babel-load-languages'."
118 (set-default sym value
)
120 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
123 (require (intern (concat "ob-" lang
))))
125 (funcall 'fmakunbound
126 (intern (concat "org-babel-execute:" lang
)))
127 (funcall 'fmakunbound
128 (intern (concat "org-babel-expand-body:" lang
)))))))
129 org-babel-load-languages
))
131 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
132 "Languages which can be evaluated in Org-mode buffers.
133 This list can be used to load support for any of the languages
134 below, note that each language will depend on a different set of
135 system executables and/or Emacs modes. When a language is
136 \"loaded\", then code blocks in that language can be evaluated
137 with `org-babel-execute-src-block' bound by default to C-c
138 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
139 be set to remove code block evaluation from the C-c C-c
140 keybinding. By default only Emacs Lisp (which has no
141 requirements) is loaded."
143 :set
'org-babel-do-load-languages
144 :type
'(alist :tag
"Babel Languages"
149 (const :tag
"Asymptote" asymptote
)
150 (const :tag
"Clojure" clojure
)
151 (const :tag
"CSS" css
)
152 (const :tag
"Ditaa" ditaa
)
153 (const :tag
"Dot" dot
)
154 (const :tag
"Emacs Lisp" emacs-lisp
)
155 (const :tag
"Gnuplot" gnuplot
)
156 (const :tag
"Haskell" haskell
)
157 (const :tag
"Javascript" js
)
158 (const :tag
"Latex" latex
)
159 (const :tag
"Ledger" ledger
)
160 (const :tag
"Matlab" matlab
)
161 (const :tag
"Mscgen" mscgen
)
162 (const :tag
"Ocaml" ocaml
)
163 (const :tag
"Octave" octave
)
164 (const :tag
"Org" org
)
165 (const :tag
"Perl" perl
)
166 (const :tag
"PlantUML" plantuml
)
167 (const :tag
"Python" python
)
168 (const :tag
"Ruby" ruby
)
169 (const :tag
"Sass" sass
)
170 (const :tag
"Scheme" scheme
)
171 (const :tag
"Screen" screen
)
172 (const :tag
"Shell Script" sh
)
173 (const :tag
"Sql" sql
)
174 (const :tag
"Sqlite" sqlite
))
175 :value-type
(boolean :tag
"Activate" :value t
)))
177 ;;;; Customization variables
178 (defcustom org-clone-delete-id nil
179 "Remove ID property of clones of a subtree.
180 When non-nil, clones of a subtree don't inherit the ID property.
181 Otherwise they inherit the ID property with a new unique
188 (defconst org-version
"7.01trans"
189 "The version number of the file org.el.")
191 (defun org-version (&optional here
)
192 "Show the org-mode version in the echo area.
193 With prefix arg HERE, insert it at point."
195 (let* ((origin default-directory
)
196 (version org-version
)
198 (dir (concat (file-name-directory (locate-library "org")) "../" )))
199 (when (and (file-exists-p (expand-file-name ".git" dir
))
200 (executable-find "git"))
204 (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
205 (with-current-buffer "*Shell Command Output*"
206 (goto-char (point-min))
207 (setq git-version
(buffer-substring (point) (point-at-eol))))
208 (subst-char-in-string ?- ?. git-version t
)
209 (when (string-match "\\S-"
210 (shell-command-to-string
211 "git diff-index --name-only HEAD --"))
212 (setq git-version
(concat git-version
".dirty")))
213 (setq version
(concat version
" (" git-version
")"))))
215 (setq version
(format "Org-mode version %s" version
))
216 (if here
(insert version
))
219 ;;; Compatibility constants
221 ;;; The custom variables
224 "Outline-based notes management and organizer."
229 (defcustom org-mode-hook nil
230 "Mode hook for Org-mode, run after the mode was turned on."
234 (defcustom org-load-hook nil
235 "Hook that is run after org.el has been loaded."
239 (defvar org-modules
) ; defined below
240 (defvar org-modules-loaded nil
241 "Have the modules been loaded already?")
243 (defun org-load-modules-maybe (&optional force
)
244 "Load all extensions listed in `org-modules'."
245 (when (or force
(not org-modules-loaded
))
247 (condition-case nil
(require ext
)
248 (error (message "Problems while trying to load feature `%s'" ext
))))
250 (setq org-modules-loaded t
)))
252 (defun org-set-modules (var value
)
253 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
255 (when (featurep 'org
)
256 (org-load-modules-maybe 'force
)))
258 (when (org-bound-and-true-p org-modules
)
259 (let ((a (member 'org-infojs org-modules
)))
260 (and a
(setcar a
'org-jsinfo
))))
262 (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
)
263 "Modules that should always be loaded together with org.el.
264 If a description starts with <C>, the file is not part of Emacs
265 and loading it will require that you have downloaded and properly installed
266 the org-mode distribution.
268 You can also use this system to load external packages (i.e. neither Org
269 core modules, nor modules from the CONTRIB directory). Just add symbols
270 to the end of the list. If the package is called org-xyz.el, then you need
271 to add the symbol `xyz', and the package must have a call to
275 :set
'org-set-modules
278 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
279 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
280 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
281 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
282 (const :tag
" docview: Links to doc-view buffers" org-docview
)
283 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
284 (const :tag
" id: Global IDs for identifying entries" org-id
)
285 (const :tag
" info: Links to Info nodes" org-info
)
286 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
287 (const :tag
" habit: Track your consistency with habits" org-habit
)
288 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
289 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
290 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
291 (const :tag
" mew Links to Mew folders/messages" org-mew
)
292 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
293 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
294 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
295 (const :tag
" vm: Links to VM folders/messages" org-vm
)
296 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
297 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
298 (const :tag
" mouse: Additional mouse support" org-mouse
)
299 (const :tag
" TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler
)
301 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
302 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
303 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
304 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
305 (const :tag
"C collector: Collect properties into tables" org-collector
)
306 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
307 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
308 (const :tag
"C eval: Include command output as text" org-eval
)
309 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
310 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
311 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
312 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
313 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
315 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
317 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
318 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
319 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
320 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
321 (const :tag
"C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber
)
322 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
323 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
324 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
325 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
326 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
327 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
328 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
329 (const :tag
"C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks
)
330 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
331 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
332 (const :tag
"C track: Keep up with Org-mode development" org-track
)
333 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
334 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
335 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
337 (defcustom org-support-shift-select nil
338 "Non-nil means make shift-cursor commands select text when possible.
340 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
341 selecting a region, or enlarge regions started in this way.
342 In Org-mode, in special contexts, these same keys are used for other
343 purposes, important enough to compete with shift selection. Org tries
344 to balance these needs by supporting `shift-select-mode' outside these
345 special contexts, under control of this variable.
347 The default of this variable is nil, to avoid confusing behavior. Shifted
348 cursor keys will then execute Org commands in the following contexts:
349 - on a headline, changing TODO state (left/right) and priority (up/down)
350 - on a time stamp, changing the time
351 - in a plain list item, changing the bullet type
352 - in a property definition line, switching between allowed values
353 - in the BEGIN line of a clock table (changing the time block).
354 Outside these contexts, the commands will throw an error.
356 When this variable is t and the cursor is not in a special context,
357 Org-mode will support shift-selection for making and enlarging regions.
358 To make this more effective, the bullet cycling will no longer happen
359 anywhere in an item line, but only if the cursor is exactly on the bullet.
361 If you set this variable to the symbol `always', then the keys
362 will not be special in headlines, property lines, and item lines, to make
363 shift selection work there as well. If this is what you want, you can
364 use the following alternative commands: `C-c C-t' and `C-c ,' to
365 change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
366 TODO sets, `C-c -' to cycle item bullet types, and properties can be
367 edited by hand or in column view.
369 However, when the cursor is on a timestamp, shift-cursor commands
370 will still edit the time stamp - this is just too good to give up.
372 XEmacs user should have this variable set to nil, because shift-select-mode
376 (const :tag
"Never" nil
)
377 (const :tag
"When outside special context" t
)
378 (const :tag
"Everywhere except timestamps" always
)))
380 (defgroup org-startup nil
381 "Options concerning startup of Org-mode."
385 (defcustom org-startup-folded t
386 "Non-nil means entering Org-mode will switch to OVERVIEW.
387 This can also be configured on a per-file basis by adding one of
388 the following lines anywhere in the buffer:
390 #+STARTUP: fold (or `overview', this is equivalent)
391 #+STARTUP: nofold (or `showall', this is equivalent)
393 #+STARTUP: showeverything"
396 (const :tag
"nofold: show all" nil
)
397 (const :tag
"fold: overview" t
)
398 (const :tag
"content: all headlines" content
)
399 (const :tag
"show everything, even drawers" showeverything
)))
401 (defcustom org-startup-truncated t
402 "Non-nil means entering Org-mode will set `truncate-lines'.
403 This is useful since some lines containing links can be very long and
404 uninteresting. Also tables look terrible when wrapped."
408 (defcustom org-startup-indented nil
409 "Non-nil means turn on `org-indent-mode' on startup.
410 This can also be configured on a per-file basis by adding one of
411 the following lines anywhere in the buffer:
415 :group
'org-structure
417 (const :tag
"Not" nil
)
418 (const :tag
"Globally (slow on startup in large files)" t
)))
420 (defcustom org-use-sub-superscripts t
421 "Non-nil means interpret \"_\" and \"^\" for export.
422 When this option is turned on, you can use TeX-like syntax for sub- and
423 superscripts. Several characters after \"_\" or \"^\" will be
424 considered as a single item - so grouping with {} is normally not
425 needed. For example, the following things will be parsed as single
426 sub- or superscripts.
428 10^24 or 10^tau several digits will be considered 1 item.
429 10^-12 or 10^-tau a leading sign with digits or a word
430 x^2-y^3 will be read as x^2 - y^3, because items are
431 terminated by almost any nonword/nondigit char.
432 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
434 Still, ambiguity is possible - so when in doubt use {} to enclose the
435 sub/superscript. If you set this variable to the symbol `{}',
436 the braces are *required* in order to trigger interpretations as
437 sub/superscript. This can be helpful in documents that need \"_\"
438 frequently in plain text.
440 Not all export backends support this, but HTML does.
442 This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
444 :group
'org-export-translation
446 (const :tag
"Always interpret" t
)
447 (const :tag
"Only with braces" {})
448 (const :tag
"Never interpret" nil
)))
450 (if (fboundp 'defvaralias
)
451 (defvaralias 'org-export-with-sub-superscripts
'org-use-sub-superscripts
))
454 (defcustom org-startup-with-beamer-mode nil
455 "Non-nil means turn on `org-beamer-mode' on startup.
456 This can also be configured on a per-file basis by adding one of
457 the following lines anywhere in the buffer:
463 (defcustom org-startup-align-all-tables nil
464 "Non-nil means align all tables when visiting a file.
465 This is useful when the column width in tables is forced with <N> cookies
466 in table fields. Such tables will look correct only after the first re-align.
467 This can also be configured on a per-file basis by adding one of
468 the following lines anywhere in the buffer:
474 (defcustom org-startup-with-inline-images nil
475 "Non-nil means show inline images when loading a new Org file.
476 This can also be configured on a per-file basis by adding one of
477 the following lines anywhere in the buffer:
478 #+STARTUP: inlineimages
479 #+STARTUP: noinlineimages"
483 (defcustom org-insert-mode-line-in-empty-file nil
484 "Non-nil means insert the first line setting Org-mode in empty files.
485 When the function `org-mode' is called interactively in an empty file, this
486 normally means that the file name does not automatically trigger Org-mode.
487 To ensure that the file will always be in Org-mode in the future, a
488 line enforcing Org-mode will be inserted into the buffer, if this option
493 (defcustom org-replace-disputed-keys nil
494 "Non-nil means use alternative key bindings for some keys.
495 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
496 These keys are also used by other packages like shift-selection-mode'
497 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
498 If you want to use Org-mode together with one of these other modes,
499 or more generally if you would like to move some Org-mode commands to
500 other keys, set this variable and configure the keys with the variable
503 This option is only relevant at load-time of Org-mode, and must be set
504 *before* org.el is loaded. Changing it requires a restart of Emacs to
509 (defcustom org-use-extra-keys nil
510 "Non-nil means use extra key sequence definitions for certain commands.
511 This happens automatically if you run XEmacs or if `window-system'
512 is nil. This variable lets you do the same manually. You must
513 set it before loading org.
515 Example: on Carbon Emacs 22 running graphically, with an external
516 keyboard on a Powerbook, the default way of setting M-left might
517 not work for either Alt or ESC. Setting this variable will make
522 (if (fboundp 'defvaralias
)
523 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
525 (defcustom org-disputed-keys
526 '(([(shift up
)] .
[(meta p
)])
527 ([(shift down
)] .
[(meta n
)])
528 ([(shift left
)] .
[(meta -
)])
529 ([(shift right
)] .
[(meta +)])
530 ([(control shift right
)] .
[(meta shift
+)])
531 ([(control shift left
)] .
[(meta shift -
)]))
532 "Keys for which Org-mode and other modes compete.
533 This is an alist, cars are the default keys, second element specifies
534 the alternative to use when `org-replace-disputed-keys' is t.
536 Keys can be specified in any syntax supported by `define-key'.
537 The value of this option takes effect only at Org-mode's startup,
538 therefore you'll have to restart Emacs to apply it after changing."
543 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
544 Or return the original if not disputed.
545 Also apply the translations defined in `org-xemacs-key-equivalents'."
546 (when org-replace-disputed-keys
547 (let* ((nkey (key-description key
))
548 (x (org-find-if (lambda (x)
549 (equal (key-description (car x
)) nkey
))
551 (setq key
(if x
(cdr x
) key
))))
552 (when (featurep 'xemacs
)
553 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
556 (defun org-find-if (predicate seq
)
559 (if (funcall predicate
(car seq
))
560 (throw 'exit
(car seq
))
563 (defun org-defkey (keymap key def
)
564 "Define a key, possibly translated, as returned by `org-key'."
565 (define-key keymap
(org-key key
) def
))
567 (defcustom org-ellipsis nil
568 "The ellipsis to use in the Org-mode outline.
569 When nil, just use the standard three dots. When a string, use that instead,
570 When a face, use the standard 3 dots, but with the specified face.
571 The change affects only Org-mode (which will then use its own display table).
572 Changing this requires executing `M-x org-mode' in a buffer to become
575 :type
'(choice (const :tag
"Default" nil
)
576 (face :tag
"Face" :value org-warning
)
577 (string :tag
"String" :value
"...#")))
579 (defvar org-display-table nil
580 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
582 (defgroup org-keywords nil
583 "Keywords in Org-mode."
587 (defcustom org-deadline-string
"DEADLINE:"
588 "String to mark deadline entries.
589 A deadline is this string, followed by a time stamp. Should be a word,
590 terminated by a colon. You can insert a schedule keyword and
591 a timestamp with \\[org-deadline].
592 Changes become only effective after restarting Emacs."
596 (defcustom org-scheduled-string
"SCHEDULED:"
597 "String to mark scheduled TODO entries.
598 A schedule is this string, followed by a time stamp. Should be a word,
599 terminated by a colon. You can insert a schedule keyword and
600 a timestamp with \\[org-schedule].
601 Changes become only effective after restarting Emacs."
605 (defcustom org-closed-string
"CLOSED:"
606 "String used as the prefix for timestamps logging closing a TODO entry."
610 (defcustom org-clock-string
"CLOCK:"
611 "String used as prefix for timestamps clocking work hours on an item."
615 (defcustom org-comment-string
"COMMENT"
616 "Entries starting with this keyword will never be exported.
617 An entry can be toggled between COMMENT and normal with
618 \\[org-toggle-comment].
619 Changes become only effective after restarting Emacs."
623 (defcustom org-quote-string
"QUOTE"
624 "Entries starting with this keyword will be exported in fixed-width font.
625 Quoting applies only to the text in the entry following the headline, and does
626 not extend beyond the next headline, even if that is lower level.
627 An entry can be toggled between QUOTE and normal with
628 \\[org-toggle-fixed-width-section]."
632 (defconst org-repeat-re
633 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
634 "Regular expression for specifying repeated events.
635 After a match, group 1 contains the repeat expression.")
637 (defgroup org-structure nil
638 "Options concerning the general structure of Org-mode files."
642 (defgroup org-reveal-location nil
643 "Options about how to make context of a location visible."
644 :tag
"Org Reveal Location"
645 :group
'org-structure
)
647 (defconst org-context-choice
649 (const :tag
"Always" t
)
650 (const :tag
"Never" nil
)
651 (repeat :greedy t
:tag
"Individual contexts"
653 (choice :tag
"Context"
660 (const bookmark-jump
)
664 "Contexts for the reveal options.")
666 (defcustom org-show-hierarchy-above
'((default . t
))
667 "Non-nil means show full hierarchy when revealing a location.
668 Org-mode often shows locations in an org-mode file which might have
669 been invisible before. When this is set, the hierarchy of headings
670 above the exposed location is shown.
671 Turning this off for example for sparse trees makes them very compact.
672 Instead of t, this can also be an alist specifying this option for different
673 contexts. Valid contexts are
674 agenda when exposing an entry from the agenda
675 org-goto when using the command `org-goto' on key C-c C-j
676 occur-tree when using the command `org-occur' on key C-c /
677 tags-tree when constructing a sparse tree based on tags matches
678 link-search when exposing search matches associated with a link
679 mark-goto when exposing the jump goal of a mark
680 bookmark-jump when exposing a bookmark location
681 isearch when exiting from an incremental search
682 default default for all contexts not set explicitly"
683 :group
'org-reveal-location
684 :type org-context-choice
)
686 (defcustom org-show-following-heading
'((default . nil
))
687 "Non-nil means show following heading when revealing a location.
688 Org-mode often shows locations in an org-mode file which might have
689 been invisible before. When this is set, the heading following the
691 Turning this off for example for sparse trees makes them very compact,
692 but makes it harder to edit the location of the match. In such a case,
693 use the command \\[org-reveal] to show more context.
694 Instead of t, this can also be an alist specifying this option for different
695 contexts. See `org-show-hierarchy-above' for valid contexts."
696 :group
'org-reveal-location
697 :type org-context-choice
)
699 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
700 "Non-nil means show all sibling heading when revealing a location.
701 Org-mode often shows locations in an org-mode file which might have
702 been invisible before. When this is set, the sibling of the current entry
703 heading are all made visible. If `org-show-hierarchy-above' is t,
704 the same happens on each level of the hierarchy above the current entry.
706 By default this is on for the isearch context, off for all other contexts.
707 Turning this off for example for sparse trees makes them very compact,
708 but makes it harder to edit the location of the match. In such a case,
709 use the command \\[org-reveal] to show more context.
710 Instead of t, this can also be an alist specifying this option for different
711 contexts. See `org-show-hierarchy-above' for valid contexts."
712 :group
'org-reveal-location
713 :type org-context-choice
)
715 (defcustom org-show-entry-below
'((default . nil
))
716 "Non-nil means show the entry below a headline when revealing a location.
717 Org-mode often shows locations in an org-mode file which might have
718 been invisible before. When this is set, the text below the headline that is
719 exposed is also shown.
721 By default this is off for all contexts.
722 Instead of t, this can also be an alist specifying this option for different
723 contexts. See `org-show-hierarchy-above' for valid contexts."
724 :group
'org-reveal-location
725 :type org-context-choice
)
727 (defcustom org-indirect-buffer-display
'other-window
728 "How should indirect tree buffers be displayed?
729 This applies to indirect buffers created with the commands
730 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
732 current-window Display in the current window
733 other-window Just display in another window.
734 dedicated-frame Create one new frame, and re-use it each time.
735 new-frame Make a new frame each time. Note that in this case
736 previously-made indirect buffers are kept, and you need to
737 kill these buffers yourself."
738 :group
'org-structure
739 :group
'org-agenda-windows
741 (const :tag
"In current window" current-window
)
742 (const :tag
"In current frame, other window" other-window
)
743 (const :tag
"Each time a new frame" new-frame
)
744 (const :tag
"One dedicated frame" dedicated-frame
)))
746 (defcustom org-use-speed-commands nil
747 "Non-nil means activate single letter commands at beginning of a headline.
748 This may also be a function to test for appropriate locations where speed
749 commands should be active."
750 :group
'org-structure
752 (const :tag
"Never" nil
)
753 (const :tag
"At beginning of headline stars" t
)
756 (defcustom org-speed-commands-user nil
757 "Alist of additional speed commands.
758 This list will be checked before `org-speed-commands-default'
759 when the variable `org-use-speed-commands' is non-nil
760 and when the cursor is at the beginning of a headline.
761 The car if each entry is a string with a single letter, which must
762 be assigned to `self-insert-command' in the global map.
763 The cdr is either a command to be called interactively, a function
764 to be called, or a form to be evaluated.
765 An entry that is just a list with a single string will be interpreted
766 as a descriptive headline that will be added when listing the speed
767 commands in the Help buffer using the `?' speed command."
768 :group
'org-structure
769 :type
'(repeat :value
("k" . ignore
)
770 (choice :value
("k" . ignore
)
771 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
772 (cons :tag
"Letter and Command"
773 (string :tag
"Command letter")
778 (defgroup org-cycle nil
779 "Options concerning visibility cycling in Org-mode."
781 :group
'org-structure
)
783 (defcustom org-cycle-skip-children-state-if-no-children t
784 "Non-nil means skip CHILDREN state in entries that don't have any."
788 (defcustom org-cycle-max-level nil
789 "Maximum level which should still be subject to visibility cycling.
790 Levels higher than this will, for cycling, be treated as text, not a headline.
791 When `org-odd-levels-only' is set, a value of N in this variable actually
792 means 2N-1 stars as the limiting headline.
793 When nil, cycle all levels.
794 Note that the limiting level of cycling is also influenced by
795 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
796 `org-inlinetask-min-level' is, cycling will be limited to levels one less
800 (const :tag
"No limit" nil
)
801 (integer :tag
"Maximum level")))
803 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK")
804 "Names of drawers. Drawers are not opened by cycling on the headline above.
805 Drawers only open with a TAB on the drawer line itself. A drawer looks like
810 The drawer \"PROPERTIES\" is special for capturing properties through
813 Drawers can be defined on the per-file basis with a line like:
815 #+DRAWERS: HIDDEN STATE PROPERTIES"
816 :group
'org-structure
818 :type
'(repeat (string :tag
"Drawer Name")))
820 (defcustom org-hide-block-startup nil
821 "Non-nil means entering Org-mode will fold all blocks.
822 This can also be set in on a per-file basis with
824 #+STARTUP: hideblocks
825 #+STARTUP: showblocks"
830 (defcustom org-cycle-global-at-bob nil
831 "Cycle globally if cursor is at beginning of buffer and not at a headline.
832 This makes it possible to do global cycling without having to use S-TAB or
833 \\[universal-argument] TAB. For this special case to work, the first line \
835 must not be a headline - it may be empty or some other text. When used in
836 this way, `org-cycle-hook' is disables temporarily, to make sure the
837 cursor stays at the beginning of the buffer.
838 When this option is nil, don't do anything special at the beginning
843 (defcustom org-cycle-level-after-item
/entry-creation t
844 "Non-nil means cycle entry level or item indentation in new empty entries.
846 When the cursor is at the end of an empty headline, i.e with only stars
847 and maybe a TODO keyword, TAB will then switch the entry to become a child,
848 and then all possible ancestor states, before returning to the original state.
849 This makes data entry extremely fast: M-RET to create a new headline,
850 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
852 When the cursor is at the end of an empty plain list item, one TAB will
853 make it a subitem, two or more tabs will back up to make this an item
854 higher up in the item hierarchy."
858 (defcustom org-cycle-emulate-tab t
859 "Where should `org-cycle' emulate TAB.
861 white Only in completely white lines
862 whitestart Only at the beginning of lines, before the first non-white char
863 t Everywhere except in headlines
864 exc-hl-bol Everywhere except at the start of a headline
865 If TAB is used in a place where it does not emulate TAB, the current subtree
866 visibility is cycled."
868 :type
'(choice (const :tag
"Never" nil
)
869 (const :tag
"Only in completely white lines" white
)
870 (const :tag
"Before first char in a line" whitestart
)
871 (const :tag
"Everywhere except in headlines" t
)
872 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
875 (defcustom org-cycle-separator-lines
2
876 "Number of empty lines needed to keep an empty line between collapsed trees.
877 If you leave an empty line between the end of a subtree and the following
878 headline, this empty line is hidden when the subtree is folded.
879 Org-mode will leave (exactly) one empty line visible if the number of
880 empty lines is equal or larger to the number given in this variable.
881 So the default 2 means at least 2 empty lines after the end of a subtree
882 are needed to produce free space between a collapsed subtree and the
885 If the number is negative, and the number of empty lines is at least -N,
886 all empty lines are shown.
888 Special case: when 0, never leave empty lines in collapsed view."
891 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
893 (defcustom org-pre-cycle-hook nil
894 "Hook that is run before visibility cycling is happening.
895 The function(s) in this hook must accept a single argument which indicates
896 the new state that will be set right after running this hook. The
897 argument is a symbol. Before a global state change, it can have the values
898 `overview', `content', or `all'. Before a local state change, it can have
899 the values `folded', `children', or `subtree'."
903 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
904 org-cycle-hide-drawers
905 org-cycle-show-empty-lines
906 org-optimize-window-after-visibility-change
)
907 "Hook that is run after `org-cycle' has changed the buffer visibility.
908 The function(s) in this hook must accept a single argument which indicates
909 the new state that was set by the most recent `org-cycle' command. The
910 argument is a symbol. After a global state change, it can have the values
911 `overview', `content', or `all'. After a local state change, it can have
912 the values `folded', `children', or `subtree'."
916 (defgroup org-edit-structure nil
917 "Options concerning structure editing in Org-mode."
918 :tag
"Org Edit Structure"
919 :group
'org-structure
)
921 (defcustom org-odd-levels-only nil
922 "Non-nil means skip even levels and only use odd levels for the outline.
923 This has the effect that two stars are being added/taken away in
924 promotion/demotion commands. It also influences how levels are
925 handled by the exporters.
926 Changing it requires restart of `font-lock-mode' to become effective
927 for fontification also in regions already fontified.
928 You may also set this on a per-file basis by adding one of the following
933 :group
'org-edit-structure
934 :group
'org-appearance
937 (defcustom org-adapt-indentation t
938 "Non-nil means adapt indentation to outline node level.
940 When this variable is set, Org assumes that you write outlines by
941 indenting text in each node to align with the headline (after the stars).
942 The following issues are influenced by this variable:
944 - When this is set and the *entire* text in an entry is indented, the
945 indentation is increased by one space in a demotion command, and
946 decreased by one in a promotion command. If any line in the entry
947 body starts with text at column 0, indentation is not changed at all.
949 - Property drawers and planning information is inserted indented when
950 this variable s set. When nil, they will not be indented.
952 - TAB indents a line relative to context. The lines below a headline
953 will be indented when this variable is set.
955 Note that this is all about true indentation, by adding and removing
956 space characters. See also `org-indent.el' which does level-dependent
957 indentation in a virtual way, i.e. at display time in Emacs."
958 :group
'org-edit-structure
961 (defcustom org-special-ctrl-a
/e nil
962 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
964 When t, `C-a' will bring back the cursor to the beginning of the
965 headline text, i.e. after the stars and after a possible TODO keyword.
966 In an item, this will be the position after the bullet.
967 When the cursor is already at that position, another `C-a' will bring
968 it to the beginning of the line.
970 `C-e' will jump to the end of the headline, ignoring the presence of tags
971 in the headline. A second `C-e' will then jump to the true end of the
972 line, after any tags. This also means that, when this variable is
973 non-nil, `C-e' also will never jump beyond the end of the heading of a
974 folded section, i.e. not after the ellipses.
976 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
977 going to the true line boundary first. Only a directly following, identical
978 keypress will bring the cursor to the special positions.
980 This may also be a cons cell where the behavior for `C-a' and `C-e' is
982 :group
'org-edit-structure
984 (const :tag
"off" nil
)
985 (const :tag
"on: after stars/bullet and before tags first" t
)
986 (const :tag
"reversed: true line boundary first" reversed
)
987 (cons :tag
"Set C-a and C-e separately"
988 (choice :tag
"Special C-a"
989 (const :tag
"off" nil
)
990 (const :tag
"on: after stars/bullet first" t
)
991 (const :tag
"reversed: before stars/bullet first" reversed
))
992 (choice :tag
"Special C-e"
993 (const :tag
"off" nil
)
994 (const :tag
"on: before tags first" t
)
995 (const :tag
"reversed: after tags first" reversed
)))))
996 (if (fboundp 'defvaralias
)
997 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
999 (defcustom org-special-ctrl-k nil
1000 "Non-nil means `C-k' will behave specially in headlines.
1001 When nil, `C-k' will call the default `kill-line' command.
1002 When t, the following will happen while the cursor is in the headline:
1004 - When the cursor is at the beginning of a headline, kill the entire
1005 line and possible the folded subtree below the line.
1006 - When in the middle of the headline text, kill the headline up to the tags.
1007 - When after the headline text, kill the tags."
1008 :group
'org-edit-structure
1011 (defcustom org-ctrl-k-protect-subtree nil
1012 "Non-nil means, do not delete a hidden subtree with C-k.
1013 When set to the symbol `error', simply throw an error when C-k is
1014 used to kill (part-of) a headline that has hidden text behind it.
1015 Any other non-nil value will result in a query to the user, if it is
1016 OK to kill that hidden subtree. When nil, kill without remorse."
1017 :group
'org-edit-structure
1019 (const :tag
"Do not protect hidden subtrees" nil
)
1020 (const :tag
"Protect hidden subtrees with a security query" t
)
1021 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1023 (defcustom org-yank-folded-subtrees t
1024 "Non-nil means when yanking subtrees, fold them.
1025 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1026 it starts with a heading and all other headings in it are either children
1027 or siblings, then fold all the subtrees. However, do this only if no
1028 text after the yank would be swallowed into a folded tree by this action."
1029 :group
'org-edit-structure
1032 (defcustom org-yank-adjusted-subtrees nil
1033 "Non-nil means when yanking subtrees, adjust the level.
1034 With this setting, `org-paste-subtree' is used to insert the subtree, see
1035 this function for details."
1036 :group
'org-edit-structure
1039 (defcustom org-M-RET-may-split-line
'((default . t
))
1040 "Non-nil means M-RET will split the line at the cursor position.
1041 When nil, it will go to the end of the line before making a
1043 You may also set this option in a different way for different
1044 contexts. Valid contexts are:
1046 headline when creating a new headline
1047 item when creating a new item
1048 table in a table field
1049 default the value to be used for all contexts not explicitly
1051 :group
'org-structure
1054 (const :tag
"Always" t
)
1055 (const :tag
"Never" nil
)
1056 (repeat :greedy t
:tag
"Individual contexts"
1058 (choice :tag
"Context"
1066 (defcustom org-insert-heading-respect-content nil
1067 "Non-nil means insert new headings after the current subtree.
1068 When nil, the new heading is created directly after the current line.
1069 The commands \\[org-insert-heading-respect-content] and
1070 \\[org-insert-todo-heading-respect-content] turn this variable on
1071 for the duration of the command."
1072 :group
'org-structure
1075 (defcustom org-blank-before-new-entry
'((heading . auto
)
1076 (plain-list-item . auto
))
1077 "Should `org-insert-heading' leave a blank line before new heading/item?
1078 The value is an alist, with `heading' and `plain-list-item' as car,
1079 and a boolean flag as cdr. For plain lists, if the variable
1080 `org-empty-line-terminates-plain-lists' is set, the setting here
1081 is ignored and no empty line is inserted, to keep the list in tact."
1082 :group
'org-edit-structure
1084 (cons (const heading
)
1085 (choice (const :tag
"Never" nil
)
1086 (const :tag
"Always" t
)
1087 (const :tag
"Auto" auto
)))
1088 (cons (const plain-list-item
)
1089 (choice (const :tag
"Never" nil
)
1090 (const :tag
"Always" t
)
1091 (const :tag
"Auto" auto
)))))
1093 (defcustom org-insert-heading-hook nil
1094 "Hook being run after inserting a new heading."
1095 :group
'org-edit-structure
1098 (defcustom org-enable-fixed-width-editor t
1099 "Non-nil means lines starting with \":\" are treated as fixed-width.
1100 This currently only means they are never auto-wrapped.
1101 When nil, such lines will be treated like ordinary lines.
1102 See also the QUOTE keyword."
1103 :group
'org-edit-structure
1106 (defcustom org-goto-auto-isearch t
1107 "Non-nil means typing characters in `org-goto' starts incremental search."
1108 :group
'org-edit-structure
1111 (defgroup org-sparse-trees nil
1112 "Options concerning sparse trees in Org-mode."
1113 :tag
"Org Sparse Trees"
1114 :group
'org-structure
)
1116 (defcustom org-highlight-sparse-tree-matches t
1117 "Non-nil means highlight all matches that define a sparse tree.
1118 The highlights will automatically disappear the next time the buffer is
1119 changed by an edit command."
1120 :group
'org-sparse-trees
1123 (defcustom org-remove-highlights-with-change t
1124 "Non-nil means any change to the buffer will remove temporary highlights.
1125 Such highlights are created by `org-occur' and `org-clock-display'.
1126 When nil, `C-c C-c needs to be used to get rid of the highlights.
1127 The highlights created by `org-preview-latex-fragment' always need
1128 `C-c C-c' to be removed."
1129 :group
'org-sparse-trees
1134 (defcustom org-occur-hook
'(org-first-headline-recenter)
1135 "Hook that is run after `org-occur' has constructed a sparse tree.
1136 This can be used to recenter the window to show as much of the structure
1138 :group
'org-sparse-trees
1141 (defgroup org-imenu-and-speedbar nil
1142 "Options concerning imenu and speedbar in Org-mode."
1143 :tag
"Org Imenu and Speedbar"
1144 :group
'org-structure
)
1146 (defcustom org-imenu-depth
2
1147 "The maximum level for Imenu access to Org-mode headlines.
1148 This also applied for speedbar access."
1149 :group
'org-imenu-and-speedbar
1152 (defgroup org-table nil
1153 "Options concerning tables in Org-mode."
1157 (defcustom org-enable-table-editor
'optimized
1158 "Non-nil means lines starting with \"|\" are handled by the table editor.
1159 When nil, such lines will be treated like ordinary lines.
1161 When equal to the symbol `optimized', the table editor will be optimized to
1163 - Automatic overwrite mode in front of whitespace in table fields.
1164 This makes the structure of the table stay in tact as long as the edited
1165 field does not exceed the column width.
1166 - Minimize the number of realigns. Normally, the table is aligned each time
1167 TAB or RET are pressed to move to another field. With optimization this
1168 happens only if changes to a field might have changed the column width.
1169 Optimization requires replacing the functions `self-insert-command',
1170 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1171 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1172 very good at guessing when a re-align will be necessary, but you can always
1173 force one with \\[org-ctrl-c-ctrl-c].
1175 If you would like to use the optimized version in Org-mode, but the
1176 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1178 This variable can be used to turn on and off the table editor during a session,
1179 but in order to toggle optimization, a restart is required.
1181 See also the variable `org-table-auto-blank-field'."
1184 (const :tag
"off" nil
)
1186 (const :tag
"on, optimized" optimized
)))
1188 (defcustom org-self-insert-cluster-for-undo t
1189 "Non-nil means cluster self-insert commands for undo when possible.
1190 If this is set, then, like in the Emacs command loop, 20 consecutive
1191 characters will be undone together.
1192 This is configurable, because there is some impact on typing performance."
1196 (defcustom org-table-tab-recognizes-table.el t
1197 "Non-nil means TAB will automatically notice a table.el table.
1198 When it sees such a table, it moves point into it and - if necessary -
1199 calls `table-recognize-table'."
1200 :group
'org-table-editing
1203 (defgroup org-link nil
1204 "Options concerning links in Org-mode."
1208 (defvar org-link-abbrev-alist-local nil
1209 "Buffer-local version of `org-link-abbrev-alist', which see.
1210 The value of this is taken from the #+LINK lines.")
1211 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1213 (defcustom org-link-abbrev-alist nil
1214 "Alist of link abbreviations.
1215 The car of each element is a string, to be replaced at the start of a link.
1216 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1217 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1219 [[linkkey:tag][description]]
1221 The 'linkkey' must be a word word, starting with a letter, followed
1222 by letters, numbers, '-' or '_'.
1224 If REPLACE is a string, the tag will simply be appended to create the link.
1225 If the string contains \"%s\", the tag will be inserted there. Alternatively,
1226 the placeholder \"%h\" will cause a url-encoded version of the tag to
1227 be inserted at that point (see the function `url-hexify-string').
1229 REPLACE may also be a function that will be called with the tag as the
1230 only argument to create the link, which should be returned as a string.
1232 See the manual for examples."
1236 (string :tag
"Protocol")
1238 (string :tag
"Format")
1241 (defcustom org-descriptive-links t
1242 "Non-nil means hide link part and only show description of bracket links.
1243 Bracket links are like [[link][description]]. This variable sets the initial
1244 state in new org-mode buffers. The setting can then be toggled on a
1245 per-buffer basis from the Org->Hyperlinks menu."
1249 (defcustom org-link-file-path-type
'adaptive
1250 "How the path name in file links should be stored.
1253 relative Relative to the current directory, i.e. the directory of the file
1254 into which the link is being inserted.
1255 absolute Absolute path, if possible with ~ for home directory.
1256 noabbrev Absolute path, no abbreviation of home directory.
1257 adaptive Use relative path for files in the current directory and sub-
1258 directories of it. For other files, use an absolute path."
1266 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1267 "Types of links that should be activated in Org-mode files.
1268 This is a list of symbols, each leading to the activation of a certain link
1269 type. In principle, it does not hurt to turn on most link types - there may
1270 be a small gain when turning off unused link types. The types are:
1272 bracket The recommended [[link][description]] or [[link]] links with hiding.
1273 angular Links in angular brackets that may contain whitespace like
1274 <bbdb:Carsten Dominik>.
1275 plain Plain links in normal text, no whitespace, like http://google.com.
1276 radio Text that is matched by a radio target, see manual for details.
1277 tag Tag settings in a headline (link to tag search).
1278 date Time stamps (link to calendar).
1279 footnote Footnote labels.
1281 Changing this variable requires a restart of Emacs to become effective."
1283 :type
'(set :greedy t
1284 (const :tag
"Double bracket links (new style)" bracket
)
1285 (const :tag
"Angular bracket links (old style)" angular
)
1286 (const :tag
"Plain text links" plain
)
1287 (const :tag
"Radio target matches" radio
)
1288 (const :tag
"Tags" tag
)
1289 (const :tag
"Timestamps" date
)
1290 (const :tag
"Footnotes" footnote
)))
1292 (defcustom org-make-link-description-function nil
1293 "Function to use to generate link descriptions from links.
1294 If nil the link location will be used. This function must take
1295 two parameters; the first is the link and the second the
1296 description `org-insert-link' has generated, and should return the
1297 description to use."
1301 (defgroup org-link-store nil
1302 "Options concerning storing links in Org-mode."
1303 :tag
"Org Store Link"
1306 (defcustom org-email-link-description-format
"Email %c: %.30s"
1307 "Format of the description part of a link to an email or usenet message.
1308 The following %-escapes will be replaced by corresponding information:
1310 %F full \"From\" field
1311 %f name, taken from \"From\" field, address if no name
1312 %T full \"To\" field
1313 %t first name in \"To\" field, address if no name
1314 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1315 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1319 You may use normal field width specification between the % and the letter.
1320 This is for example useful to limit the length of the subject.
1322 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1323 :group
'org-link-store
1326 (defcustom org-from-is-user-regexp
1328 (when (and user-mail-address
(not (string= user-mail-address
"")))
1329 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1330 (when (and user-full-name
(not (string= user-full-name
"")))
1331 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1332 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1333 "Regexp matched against the \"From:\" header of an email or usenet message.
1334 It should match if the message is from the user him/herself."
1335 :group
'org-link-store
1338 (defcustom org-link-to-org-use-id
'create-if-interactive-and-no-custom-id
1339 "Non-nil means storing a link to an Org file will use entry IDs.
1341 Note that before this variable is even considered, org-id must be loaded,
1342 so please customize `org-modules' and turn it on.
1344 The variable can have the following values:
1346 t Create an ID if needed to make a link to the current entry.
1348 create-if-interactive
1349 If `org-store-link' is called directly (interactively, as a user
1350 command), do create an ID to support the link. But when doing the
1351 job for remember, only use the ID if it already exists. The
1352 purpose of this setting is to avoid proliferation of unwanted
1353 IDs, just because you happen to be in an Org file when you
1354 call `org-remember' that automatically and preemptively
1355 creates a link. If you do want to get an ID link in a remember
1356 template to an entry not having an ID, create it first by
1357 explicitly creating a link to it, using `C-c C-l' first.
1359 create-if-interactive-and-no-custom-id
1360 Like create-if-interactive, but do not create an ID if there is
1361 a CUSTOM_ID property defined in the entry. This is the default.
1364 Use existing ID, do not create one.
1366 nil Never use an ID to make a link, instead link using a text search for
1368 :group
'org-link-store
1370 (const :tag
"Create ID to make link" t
)
1371 (const :tag
"Create if storing link interactively"
1372 create-if-interactive
)
1373 (const :tag
"Create if storing link interactively and no CUSTOM_ID is present"
1374 create-if-interactive-and-no-custom-id
)
1375 (const :tag
"Only use existing" use-existing
)
1376 (const :tag
"Do not use ID to create link" nil
)))
1378 (defcustom org-context-in-file-links t
1379 "Non-nil means file links from `org-store-link' contain context.
1380 A search string will be added to the file name with :: as separator and
1381 used to find the context when the link is activated by the command
1382 `org-open-at-point'.
1383 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1384 negates this setting for the duration of the command."
1385 :group
'org-link-store
1388 (defcustom org-keep-stored-link-after-insertion nil
1389 "Non-nil means keep link in list for entire session.
1391 The command `org-store-link' adds a link pointing to the current
1392 location to an internal list. These links accumulate during a session.
1393 The command `org-insert-link' can be used to insert links into any
1394 Org-mode file (offering completion for all stored links). When this
1395 option is nil, every link which has been inserted once using \\[org-insert-link]
1396 will be removed from the list, to make completing the unused links
1398 :group
'org-link-store
1401 (defgroup org-link-follow nil
1402 "Options concerning following links in Org-mode."
1403 :tag
"Org Follow Link"
1406 (defcustom org-link-translation-function nil
1407 "Function to translate links with different syntax to Org syntax.
1408 This can be used to translate links created for example by the Planner
1409 or emacs-wiki packages to Org syntax.
1410 The function must accept two parameters, a TYPE containing the link
1411 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1412 which is everything after the link protocol. It should return a cons
1413 with possibly modified values of type and path.
1414 Org contains a function for this, so if you set this variable to
1415 `org-translate-link-from-planner', you should be able follow many
1416 links created by planner."
1417 :group
'org-link-follow
1420 (defcustom org-follow-link-hook nil
1421 "Hook that is run after a link has been followed."
1422 :group
'org-link-follow
1425 (defcustom org-tab-follows-link nil
1426 "Non-nil means on links TAB will follow the link.
1427 Needs to be set before org.el is loaded.
1428 This really should not be used, it does not make sense, and the
1429 implementation is bad."
1430 :group
'org-link-follow
1433 (defcustom org-return-follows-link nil
1434 "Non-nil means on links RET will follow the link."
1435 :group
'org-link-follow
1438 (defcustom org-mouse-1-follows-link
1439 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1440 "Non-nil means mouse-1 on a link will follow the link.
1441 A longer mouse click will still set point. Does not work on XEmacs.
1442 Needs to be set before org.el is loaded."
1443 :group
'org-link-follow
1446 (defcustom org-mark-ring-length
4
1447 "Number of different positions to be recorded in the ring.
1448 Changing this requires a restart of Emacs to work correctly."
1449 :group
'org-link-follow
1452 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1453 "Non-nil means internal links in Org files must exactly match a headline.
1454 When nil, the link search tries to match a phrase will all words
1455 in the search text."
1456 :group
'org-link-follow
1458 (const :tag
"Use fuzy text search" nil
)
1459 (const :tag
"Match only exact headline" t
)
1460 (const :tag
"Match extact headline or query to create it"
1463 (defcustom org-link-frame-setup
1464 '((vm . vm-visit-folder-other-frame
)
1465 (gnus . org-gnus-no-new-news
)
1466 (file . find-file-other-window
)
1467 (wl . wl-other-frame
))
1468 "Setup the frame configuration for following links.
1469 When following a link with Emacs, it may often be useful to display
1470 this link in another window or frame. This variable can be used to
1471 set this up for the different types of links.
1474 `vm-visit-folder-other-frame'
1475 For Gnus, use any of
1478 `org-gnus-no-new-news'
1479 For FILE, use any of
1481 `find-file-other-window'
1482 `find-file-other-frame'
1483 For Wanderlust use any of
1486 For the calendar, use the variable `calendar-setup'.
1487 For BBDB, it is currently only possible to display the matches in
1489 :group
'org-link-follow
1493 (const vm-visit-folder
)
1494 (const vm-visit-folder-other-window
)
1495 (const vm-visit-folder-other-frame
)))
1499 (const gnus-other-frame
)
1500 (const org-gnus-no-new-news
)))
1504 (const find-file-other-window
)
1505 (const find-file-other-frame
)))
1509 (const wl-other-frame
)))))
1511 (defcustom org-display-internal-link-with-indirect-buffer nil
1512 "Non-nil means use indirect buffer to display infile links.
1513 Activating internal links (from one location in a file to another location
1514 in the same file) normally just jumps to the location. When the link is
1515 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1517 another window. When this option is set, the other window actually displays
1518 an indirect buffer clone of the current buffer, to avoid any visibility
1519 changes to the current buffer."
1520 :group
'org-link-follow
1523 (defcustom org-open-non-existing-files nil
1524 "Non-nil means `org-open-file' will open non-existing files.
1525 When nil, an error will be generated.
1526 This variable applies only to external applications because they
1527 might choke on non-existing files. If the link is to a file that
1528 will be opened in Emacs, the variable is ignored."
1529 :group
'org-link-follow
1532 (defcustom org-open-directory-means-index-dot-org nil
1533 "Non-nil means a link to a directory really means to index.org.
1534 When nil, following a directory link will run dired or open a finder/explorer
1535 window on that directory."
1536 :group
'org-link-follow
1539 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1540 "Function and arguments to call for following mailto links.
1541 This is a list with the first element being a Lisp function, and the
1542 remaining elements being arguments to the function. In string arguments,
1543 %a will be replaced by the address, and %s will be replaced by the subject
1544 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1545 :group
'org-link-follow
1547 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1548 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1549 (const :tag
"message-mail" (message-mail "%a" "%s"))
1550 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1552 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1553 "Non-nil means ask for confirmation before executing shell links.
1554 Shell links can be dangerous: just think about a link
1556 [[shell:rm -rf ~/*][Google Search]]
1558 This link would show up in your Org-mode document as \"Google Search\",
1559 but really it would remove your entire home directory.
1560 Therefore we advise against setting this variable to nil.
1561 Just change it to `y-or-n-p' if you want to confirm with a
1562 single keystroke rather than having to type \"yes\"."
1563 :group
'org-link-follow
1565 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1566 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1567 (const :tag
"no confirmation (dangerous)" nil
)))
1568 (put 'org-confirm-shell-link-function
1569 'safe-local-variable
1570 '(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1572 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1573 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1574 Elisp links can be dangerous: just think about a link
1576 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1578 This link would show up in your Org-mode document as \"Google Search\",
1579 but really it would remove your entire home directory.
1580 Therefore we advise against setting this variable to nil.
1581 Just change it to `y-or-n-p' if you want to confirm with a
1582 single keystroke rather than having to type \"yes\"."
1583 :group
'org-link-follow
1585 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1586 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1587 (const :tag
"no confirmation (dangerous)" nil
)))
1588 (put 'org-confirm-shell-link-function
1589 'safe-local-variable
1590 '(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1592 (defconst org-file-apps-defaults-gnu
1596 "Default file applications on a UNIX or GNU/Linux system.
1597 See `org-file-apps'.")
1599 (defconst org-file-apps-defaults-macosx
1602 (system .
"open %s")
1604 ("eps.gz" .
"gv %s")
1606 ("fig" .
"xfig %s"))
1607 "Default file applications on a MacOS X system.
1608 The system \"open\" is known as a default, but we use X11 applications
1609 for some files for which the OS does not have a good default.
1610 See `org-file-apps'.")
1612 (defconst org-file-apps-defaults-windowsnt
1616 (list (if (featurep 'xemacs
)
1617 'mswindows-shell-execute
1621 (list (if (featurep 'xemacs
)
1622 'mswindows-shell-execute
1625 "Default file applications on a Windows NT system.
1626 The system \"open\" is used for most files.
1627 See `org-file-apps'.")
1629 (defcustom org-file-apps
1632 ("\\.mm\\'" . default
)
1633 ("\\.x?html?\\'" . default
)
1634 ("\\.pdf\\'" . default
)
1636 "External applications for opening `file:path' items in a document.
1637 Org-mode uses system defaults for different file types, but
1638 you can use this variable to set the application for a given file
1639 extension. The entries in this list are cons cells where the car identifies
1640 files and the cdr the corresponding command. Possible values for the
1642 \"string\" A string as a file identifier can be interpreted in different
1643 ways, depending on its contents:
1645 - Alphanumeric characters only:
1646 Match links with this file extension.
1647 Example: (\"pdf\" . \"evince %s\")
1648 to open PDFs with evince.
1650 - Regular expression: Match links where the
1651 filename matches the regexp. If you want to
1652 use groups here, use shy groups.
1654 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1655 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1656 to open *.html and *.xhtml with firefox.
1658 - Regular expression which contains (non-shy) groups:
1659 Match links where the whole link, including \"::\", and
1660 anything after that, matches the regexp.
1661 In a custom command string, %1, %2, etc. are replaced with
1662 the parts of the link that were matched by the groups.
1663 For backwards compatibility, if a command string is given
1664 that does not use any of the group matches, this case is
1665 handled identically to the second one (i.e. match against
1667 In a custom lisp form, you can access the group matches with
1668 (match-string n link).
1670 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1671 to open [[file:document.pdf::5]] with evince at page 5.
1673 `directory' Matches a directory
1674 `remote' Matches a remote file, accessible through tramp or efs.
1675 Remote files most likely should be visited through Emacs
1676 because external applications cannot handle such paths.
1677 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1678 so all files Emacs knows how to handle. Using this with
1679 command `emacs' will open most files in Emacs. Beware that this
1680 will also open html files inside Emacs, unless you add
1681 (\"html\" . default) to the list as well.
1682 t Default for files not matched by any of the other options.
1683 `system' The system command to open files, like `open' on Windows
1684 and Mac OS X, and mailcap under GNU/Linux. This is the command
1685 that will be selected if you call `C-c C-o' with a double
1686 \\[universal-argument] \\[universal-argument] prefix.
1688 Possible values for the command are:
1689 `emacs' The file will be visited by the current Emacs process.
1690 `default' Use the default application for this file type, which is the
1691 association for t in the list, most likely in the system-specific
1693 This can be used to overrule an unwanted setting in the
1694 system-specific variable.
1695 `system' Use the system command for opening files, like \"open\".
1696 This command is specified by the entry whose car is `system'.
1697 Most likely, the system-specific version of this variable
1698 does define this command, but you can overrule/replace it
1700 string A command to be executed by a shell; %s will be replaced
1701 by the path to the file.
1702 sexp A Lisp form which will be evaluated. The file path will
1703 be available in the Lisp variable `file'.
1704 For more examples, see the system specific constants
1705 `org-file-apps-defaults-macosx'
1706 `org-file-apps-defaults-windowsnt'
1707 `org-file-apps-defaults-gnu'."
1708 :group
'org-link-follow
1710 (cons (choice :value
""
1711 (string :tag
"Extension")
1712 (const :tag
"System command to open files" system
)
1713 (const :tag
"Default for unrecognized files" t
)
1714 (const :tag
"Remote file" remote
)
1715 (const :tag
"Links to a directory" directory
)
1716 (const :tag
"Any files that have Emacs modes"
1719 (const :tag
"Visit with Emacs" emacs
)
1720 (const :tag
"Use default" default
)
1721 (const :tag
"Use the system command" system
)
1722 (string :tag
"Command")
1723 (sexp :tag
"Lisp form")))))
1727 (defgroup org-refile nil
1728 "Options concerning refiling entries in Org-mode."
1732 (defcustom org-directory
"~/org"
1733 "Directory with org files.
1734 This is just a default location to look for Org files. There is no need
1735 at all to put your files into this directory. It is only used in the
1736 following situations:
1738 1. When a remember template specifies a target file that is not an
1739 absolute path. The path will then be interpreted relative to
1741 2. When a remember note is filed away in an interactive way (when exiting the
1742 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1743 with `org-directory' as the default path."
1745 :group
'org-remember
1748 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1749 "Default target for storing notes.
1750 Used as a fall back file for org-remember.el and org-capture.el, for
1751 templates that do not specify a target file."
1753 :group
'org-remember
1755 (const :tag
"Default from remember-data-file" nil
)
1758 (defcustom org-goto-interface
'outline
1759 "The default interface to be used for `org-goto'.
1761 outline The interface shows an outline of the relevant file
1762 and the correct heading is found by moving through
1763 the outline or by searching with incremental search.
1764 outline-path-completion Headlines in the current buffer are offered via
1765 completion. This is the interface also used by
1766 the refile command."
1769 (const :tag
"Outline" outline
)
1770 (const :tag
"Outline-path-completion" outline-path-completion
)))
1772 (defcustom org-goto-max-level
5
1773 "Maximum target level when running `org-goto' with refile interface."
1777 (defcustom org-reverse-note-order nil
1778 "Non-nil means store new notes at the beginning of a file or entry.
1779 When nil, new notes will be filed to the end of a file or entry.
1780 This can also be a list with cons cells of regular expressions that
1781 are matched against file names, and values."
1782 :group
'org-remember
1785 (const :tag
"Reverse always" t
)
1786 (const :tag
"Reverse never" nil
)
1787 (repeat :tag
"By file name regexp"
1788 (cons regexp boolean
))))
1790 (defcustom org-log-refile nil
1791 "Information to record when a task is refiled.
1793 Possible values are:
1795 nil Don't add anything
1796 time Add a time stamp to the task
1797 note Prompt for a note and add it with template `org-log-note-headings'
1799 This option can also be set with on a per-file-basis with
1801 #+STARTUP: nologrefile
1802 #+STARTUP: logrefile
1803 #+STARTUP: lognoterefile
1805 You can have local logging settings for a subtree by setting the LOGGING
1806 property to one or more of these keywords.
1808 When bulk-refiling from the agenda, the value `note' is forbidden and
1809 will temporarily be changed to `time'."
1811 :group
'org-progress
1813 (const :tag
"No logging" nil
)
1814 (const :tag
"Record timestamp" time
)
1815 (const :tag
"Record timestamp with note." note
)))
1817 (defcustom org-refile-targets nil
1818 "Targets for refiling entries with \\[org-refile].
1819 This is list of cons cells. Each cell contains:
1820 - a specification of the files to be considered, either a list of files,
1821 or a symbol whose function or variable value will be used to retrieve
1822 a file name or a list of file names. If you use `org-agenda-files' for
1823 that, all agenda files will be scanned for targets. Nil means consider
1824 headings in the current buffer.
1825 - A specification of how to find candidate refile targets. This may be
1827 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1828 This tag has to be present in all target headlines, inheritance will
1830 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1832 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1833 headlines that are refiling targets.
1834 - a cons cell (:level . N). Any headline of level N is considered a target.
1835 Note that, when `org-odd-levels-only' is set, level corresponds to
1836 order in hierarchy, not to the number of stars.
1837 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1838 Note that, when `org-odd-levels-only' is set, level corresponds to
1839 order in hierarchy, not to the number of stars.
1841 You can set the variable `org-refile-target-verify-function' to a function
1842 to verify each headline found by the simple criteria above.
1844 When this variable is nil, all top-level headlines in the current buffer
1845 are used, equivalent to the value `((nil . (:level . 1))'."
1849 (choice :value org-agenda-files
1850 (const :tag
"All agenda files" org-agenda-files
)
1851 (const :tag
"Current buffer" nil
)
1852 (function) (variable) (file))
1853 (choice :tag
"Identify target headline by"
1854 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1855 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1856 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1857 (cons :tag
"Level number" (const :value
:level
) (integer))
1858 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1860 (defcustom org-refile-target-verify-function nil
1861 "Function to verify if the headline at point should be a refile target.
1862 The function will be called without arguments, with point at the
1863 beginning of the headline. It should return t and leave point
1864 where it is if the headline is a valid target for refiling.
1866 If the target should not be selected, the function must return nil.
1867 In addition to this, it may move point to a place from where the search
1868 should be continued. For example, the function may decide that the entire
1869 subtree of the current entry should be excluded and move point to the end
1874 (defcustom org-refile-use-cache nil
1875 "Non-nil means cache refile targets to speed up the process.
1876 The cache for a particular file will be updated automatically when
1877 the buffer has been killed, or when any of the marker used for flagging
1878 refile targets no longer points at a live buffer.
1879 If you have added new entries to a buffer that might themselves be targets,
1880 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
1881 find that easier, `C-u C-u C-u C-c C-w'."
1885 (defcustom org-refile-use-outline-path nil
1886 "Non-nil means provide refile targets as paths.
1887 So a level 3 headline will be available as level1/level2/level3.
1889 When the value is `file', also include the file name (without directory)
1890 into the path. In this case, you can also stop the completion after
1891 the file name, to get entries inserted as top level in the file.
1893 When `full-file-path', include the full file path."
1896 (const :tag
"Not" nil
)
1897 (const :tag
"Yes" t
)
1898 (const :tag
"Start with file name" file
)
1899 (const :tag
"Start with full file path" full-file-path
)))
1901 (defcustom org-outline-path-complete-in-steps t
1902 "Non-nil means complete the outline path in hierarchical steps.
1903 When Org-mode uses the refile interface to select an outline path
1904 \(see variable `org-refile-use-outline-path'), the completion of
1905 the path can be done is a single go, or if can be done in steps down
1906 the headline hierarchy. Going in steps is probably the best if you
1907 do not use a special completion package like `ido' or `icicles'.
1908 However, when using these packages, going in one step can be very
1909 fast, while still showing the whole path to the entry."
1913 (defcustom org-refile-allow-creating-parent-nodes nil
1914 "Non-nil means allow to create new nodes as refile targets.
1915 New nodes are then created by adding \"/new node name\" to the completion
1916 of an existing node. When the value of this variable is `confirm',
1917 new node creation must be confirmed by the user (recommended)
1918 When nil, the completion must match an existing entry.
1920 Note that, if the new heading is not seen by the criteria
1921 listed in `org-refile-targets', multiple instances of the same
1922 heading would be created by trying again to file under the new
1926 (const :tag
"Never" nil
)
1927 (const :tag
"Always" t
)
1928 (const :tag
"Prompt for confirmation" confirm
)))
1930 (defgroup org-todo nil
1931 "Options concerning TODO items in Org-mode."
1935 (defgroup org-progress nil
1936 "Options concerning Progress logging in Org-mode."
1940 (defvar org-todo-interpretation-widgets
1942 (:tag
"Sequence (cycling hits every state)" sequence
)
1943 (:tag
"Type (cycling directly to DONE)" type
))
1944 "The available interpretation symbols for customizing `org-todo-keywords'.
1945 Interested libraries should add to this list.")
1947 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1948 "List of TODO entry keyword sequences and their interpretation.
1949 \\<org-mode-map>This is a list of sequences.
1951 Each sequence starts with a symbol, either `sequence' or `type',
1952 indicating if the keywords should be interpreted as a sequence of
1953 action steps, or as different types of TODO items. The first
1954 keywords are states requiring action - these states will select a headline
1955 for inclusion into the global TODO list Org-mode produces. If one of
1956 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1957 signify that no further action is necessary. If \"|\" is not found,
1958 the last keyword is treated as the only DONE state of the sequence.
1960 The command \\[org-todo] cycles an entry through these states, and one
1961 additional state where no keyword is present. For details about this
1962 cycling, see the manual.
1964 TODO keywords and interpretation can also be set on a per-file basis with
1965 the special #+SEQ_TODO and #+TYP_TODO lines.
1967 Each keyword can optionally specify a character for fast state selection
1968 \(in combination with the variable `org-use-fast-todo-selection')
1969 and specifiers for state change logging, using the same syntax
1970 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1971 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1972 indicates to record a time stamp each time this state is selected.
1974 Each keyword may also specify if a timestamp or a note should be
1975 recorded when entering or leaving the state, by adding additional
1976 characters in the parenthesis after the keyword. This looks like this:
1977 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1978 record only the time of the state change. With X and Y being either
1979 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1980 Y when leaving the state if and only if the *target* state does not
1981 define X. You may omit any of the fast-selection key or X or /Y,
1982 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1984 For backward compatibility, this variable may also be just a list
1985 of keywords - in this case the interpretation (sequence or type) will be
1986 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1988 :group
'org-keywords
1990 (repeat :tag
"Old syntax, just keywords"
1991 (string :tag
"Keyword"))
1992 (repeat :tag
"New syntax"
1995 :tag
"Interpretation"
1996 ;;Quick and dirty way to see
1997 ;;`org-todo-interpretations'. This takes the
1998 ;;place of item arguments
2006 org-todo-interpretation-widgets
))
2009 (string :tag
"Keyword"))))))
2011 (defvar org-todo-keywords-1 nil
2012 "All TODO and DONE keywords active in a buffer.")
2013 (make-variable-buffer-local 'org-todo-keywords-1
)
2014 (defvar org-todo-keywords-for-agenda nil
)
2015 (defvar org-done-keywords-for-agenda nil
)
2016 (defvar org-drawers-for-agenda nil
)
2017 (defvar org-todo-keyword-alist-for-agenda nil
)
2018 (defvar org-tag-alist-for-agenda nil
)
2019 (defvar org-agenda-contributing-files nil
)
2020 (defvar org-not-done-keywords nil
)
2021 (make-variable-buffer-local 'org-not-done-keywords
)
2022 (defvar org-done-keywords nil
)
2023 (make-variable-buffer-local 'org-done-keywords
)
2024 (defvar org-todo-heads nil
)
2025 (make-variable-buffer-local 'org-todo-heads
)
2026 (defvar org-todo-sets nil
)
2027 (make-variable-buffer-local 'org-todo-sets
)
2028 (defvar org-todo-log-states nil
)
2029 (make-variable-buffer-local 'org-todo-log-states
)
2030 (defvar org-todo-kwd-alist nil
)
2031 (make-variable-buffer-local 'org-todo-kwd-alist
)
2032 (defvar org-todo-key-alist nil
)
2033 (make-variable-buffer-local 'org-todo-key-alist
)
2034 (defvar org-todo-key-trigger nil
)
2035 (make-variable-buffer-local 'org-todo-key-trigger
)
2037 (defcustom org-todo-interpretation
'sequence
2038 "Controls how TODO keywords are interpreted.
2039 This variable is in principle obsolete and is only used for
2040 backward compatibility, if the interpretation of todo keywords is
2041 not given already in `org-todo-keywords'. See that variable for
2044 :group
'org-keywords
2045 :type
'(choice (const sequence
)
2048 (defcustom org-use-fast-todo-selection t
2049 "Non-nil means use the fast todo selection scheme with C-c C-t.
2050 This variable describes if and under what circumstances the cycling
2051 mechanism for TODO keywords will be replaced by a single-key, direct
2054 When nil, fast selection is never used.
2056 When the symbol `prefix', it will be used when `org-todo' is called with
2057 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
2058 in an agenda buffer.
2060 When t, fast selection is used by default. In this case, the prefix
2061 argument forces cycling instead.
2063 In all cases, the special interface is only used if access keys have actually
2064 been assigned by the user, i.e. if keywords in the configuration are followed
2065 by a letter in parenthesis, like TODO(t)."
2068 (const :tag
"Never" nil
)
2069 (const :tag
"By default" t
)
2070 (const :tag
"Only with C-u C-c C-t" prefix
)))
2072 (defcustom org-provide-todo-statistics t
2073 "Non-nil means update todo statistics after insert and toggle.
2074 ALL-HEADLINES means update todo statistics by including headlines
2075 with no TODO keyword as well, counting them as not done.
2076 A list of TODO keywords means the same, but skip keywords that are
2079 When this is set, todo statistics is updated in the parent of the
2080 current entry each time a todo state is changed."
2083 (const :tag
"Yes, only for TODO entries" t
)
2084 (const :tag
"Yes, including all entries" 'all-headlines
)
2085 (repeat :tag
"Yes, for TODOs in this list"
2086 (string :tag
"TODO keyword"))
2087 (other :tag
"No TODO statistics" nil
)))
2089 (defcustom org-hierarchical-todo-statistics t
2090 "Non-nil means TODO statistics covers just direct children.
2091 When nil, all entries in the subtree are considered.
2092 This has only an effect if `org-provide-todo-statistics' is set.
2093 To set this to nil for only a single subtree, use a COOKIE_DATA
2094 property and include the word \"recursive\" into the value."
2098 (defcustom org-after-todo-state-change-hook nil
2099 "Hook which is run after the state of a TODO item was changed.
2100 The new state (a string with a TODO keyword, or nil) is available in the
2101 Lisp variable `state'."
2105 (defvar org-blocker-hook nil
2106 "Hook for functions that are allowed to block a state change.
2108 Each function gets as its single argument a property list, see
2109 `org-trigger-hook' for more information about this list.
2111 If any of the functions in this hook returns nil, the state change
2114 (defvar org-trigger-hook nil
2115 "Hook for functions that are triggered by a state change.
2117 Each function gets as its single argument a property list with at least
2118 the following elements:
2120 (:type type-of-change :position pos-at-entry-start
2121 :from old-state :to new-state)
2123 Depending on the type, more properties may be present.
2125 This mechanism is currently implemented for:
2129 :type todo-state-change
2130 :from previous state (keyword as a string), or nil, or a symbol
2131 'todo' or 'done', to indicate the general type of state.
2132 :to new state, like in :from")
2134 (defcustom org-enforce-todo-dependencies nil
2135 "Non-nil means undone TODO entries will block switching the parent to DONE.
2136 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2137 be blocked if any prior sibling is not yet done.
2138 Finally, if the parent is blocked because of ordered siblings of its own,
2139 the child will also be blocked.
2140 This variable needs to be set before org.el is loaded, and you need to
2141 restart Emacs after a change to make the change effective. The only way
2142 to change is while Emacs is running is through the customize interface."
2143 :set
(lambda (var val
)
2146 (add-hook 'org-blocker-hook
2147 'org-block-todo-from-children-or-siblings-or-parent
)
2148 (remove-hook 'org-blocker-hook
2149 'org-block-todo-from-children-or-siblings-or-parent
)))
2153 (defcustom org-enforce-todo-checkbox-dependencies nil
2154 "Non-nil means unchecked boxes will block switching the parent to DONE.
2155 When this is nil, checkboxes have no influence on switching TODO states.
2156 When non-nil, you first need to check off all check boxes before the TODO
2157 entry can be switched to DONE.
2158 This variable needs to be set before org.el is loaded, and you need to
2159 restart Emacs after a change to make the change effective. The only way
2160 to change is while Emacs is running is through the customize interface."
2161 :set
(lambda (var val
)
2164 (add-hook 'org-blocker-hook
2165 'org-block-todo-from-checkboxes
)
2166 (remove-hook 'org-blocker-hook
2167 'org-block-todo-from-checkboxes
)))
2171 (defcustom org-treat-insert-todo-heading-as-state-change nil
2172 "Non-nil means inserting a TODO heading is treated as state change.
2173 So when the command \\[org-insert-todo-heading] is used, state change
2174 logging will apply if appropriate. When nil, the new TODO item will
2175 be inserted directly, and no logging will take place."
2179 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2180 "Non-nil means switching TODO states with S-cursor counts as state change.
2181 This is the default behavior. However, setting this to nil allows a
2182 convenient way to select a TODO state and bypass any logging associated
2187 (defcustom org-todo-state-tags-triggers nil
2188 "Tag changes that should be triggered by TODO state changes.
2189 This is a list. Each entry is
2191 (state-change (tag . flag) .......)
2193 State-change can be a string with a state, and empty string to indicate the
2194 state that has no TODO keyword, or it can be one of the symbols `todo'
2195 or `done', meaning any not-done or done state, respectively."
2199 (cons (choice :tag
"When changing to"
2200 (const :tag
"Not-done state" todo
)
2201 (const :tag
"Done state" done
)
2202 (string :tag
"State"))
2204 (cons :tag
"Tag action"
2206 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2208 (defcustom org-log-done nil
2209 "Information to record when a task moves to the DONE state.
2211 Possible values are:
2213 nil Don't add anything, just change the keyword
2214 time Add a time stamp to the task
2215 note Prompt for a note and add it with template `org-log-note-headings'
2217 This option can also be set with on a per-file-basis with
2219 #+STARTUP: nologdone
2221 #+STARTUP: lognotedone
2223 You can have local logging settings for a subtree by setting the LOGGING
2224 property to one or more of these keywords."
2226 :group
'org-progress
2228 (const :tag
"No logging" nil
)
2229 (const :tag
"Record CLOSED timestamp" time
)
2230 (const :tag
"Record CLOSED timestamp with note." note
)))
2232 ;; Normalize old uses of org-log-done.
2234 ((eq org-log-done t
) (setq org-log-done
'time
))
2235 ((and (listp org-log-done
) (memq 'done org-log-done
))
2236 (setq org-log-done
'note
)))
2238 (defcustom org-log-reschedule nil
2239 "Information to record when the scheduling date of a tasks is modified.
2241 Possible values are:
2243 nil Don't add anything, just change the date
2244 time Add a time stamp to the task
2245 note Prompt for a note and add it with template `org-log-note-headings'
2247 This option can also be set with on a per-file-basis with
2249 #+STARTUP: nologreschedule
2250 #+STARTUP: logreschedule
2251 #+STARTUP: lognotereschedule"
2253 :group
'org-progress
2255 (const :tag
"No logging" nil
)
2256 (const :tag
"Record timestamp" time
)
2257 (const :tag
"Record timestamp with note." note
)))
2259 (defcustom org-log-redeadline nil
2260 "Information to record when the deadline date of a tasks is modified.
2262 Possible values are:
2264 nil Don't add anything, just change the date
2265 time Add a time stamp to the task
2266 note Prompt for a note and add it with template `org-log-note-headings'
2268 This option can also be set with on a per-file-basis with
2270 #+STARTUP: nologredeadline
2271 #+STARTUP: logredeadline
2272 #+STARTUP: lognoteredeadline
2274 You can have local logging settings for a subtree by setting the LOGGING
2275 property to one or more of these keywords."
2277 :group
'org-progress
2279 (const :tag
"No logging" nil
)
2280 (const :tag
"Record timestamp" time
)
2281 (const :tag
"Record timestamp with note." note
)))
2283 (defcustom org-log-note-clock-out nil
2284 "Non-nil means record a note when clocking out of an item.
2285 This can also be configured on a per-file basis by adding one of
2286 the following lines anywhere in the buffer:
2288 #+STARTUP: lognoteclock-out
2289 #+STARTUP: nolognoteclock-out"
2291 :group
'org-progress
2294 (defcustom org-log-done-with-time t
2295 "Non-nil means the CLOSED time stamp will contain date and time.
2296 When nil, only the date will be recorded."
2297 :group
'org-progress
2300 (defcustom org-log-note-headings
2301 '((done .
"CLOSING NOTE %t")
2302 (state .
"State %-12s from %-12S %t")
2303 (note .
"Note taken on %t")
2304 (reschedule .
"Rescheduled from %S on %t")
2305 (delschedule .
"Not scheduled, was %S on %t")
2306 (redeadline .
"New deadline from %S on %t")
2307 (deldeadline .
"Removed deadline, was %S on %t")
2308 (refile .
"Refiled on %t")
2310 "Headings for notes added to entries.
2311 The value is an alist, with the car being a symbol indicating the note
2312 context, and the cdr is the heading to be used. The heading may also be the
2314 %t in the heading will be replaced by a time stamp.
2315 %T will be an active time stamp instead the default inactive one
2316 %s will be replaced by the new TODO state, in double quotes.
2317 %S will be replaced by the old TODO state, in double quotes.
2318 %u will be replaced by the user name.
2319 %U will be replaced by the full user name.
2321 In fact, it is not a good idea to change the `state' entry, because
2322 agenda log mode depends on the format of these entries."
2324 :group
'org-progress
2325 :type
'(list :greedy t
2326 (cons (const :tag
"Heading when closing an item" done
) string
)
2328 "Heading when changing todo state (todo sequence only)"
2330 (cons (const :tag
"Heading when just taking a note" note
) string
)
2331 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2332 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2333 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2334 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2335 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2336 (cons (const :tag
"Heading when refiling" refile
) string
)))
2338 (unless (assq 'note org-log-note-headings
)
2339 (push '(note .
"%t") org-log-note-headings
))
2341 (defcustom org-log-into-drawer nil
2342 "Non-nil means insert state change notes and time stamps into a drawer.
2343 When nil, state changes notes will be inserted after the headline and
2344 any scheduling and clock lines, but not inside a drawer.
2346 The value of this variable should be the name of the drawer to use.
2347 LOGBOOK is proposed at the default drawer for this purpose, you can
2348 also set this to a string to define the drawer of your choice.
2350 A value of t is also allowed, representing \"LOGBOOK\".
2352 If this variable is set, `org-log-state-notes-insert-after-drawers'
2355 You can set the property LOG_INTO_DRAWER to overrule this setting for
2358 :group
'org-progress
2360 (const :tag
"Not into a drawer" nil
)
2361 (const :tag
"LOGBOOK" t
)
2362 (string :tag
"Other")))
2364 (if (fboundp 'defvaralias
)
2365 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2367 (defun org-log-into-drawer ()
2368 "Return the value of `org-log-into-drawer', but let properties overrule.
2369 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2370 used instead of the default value."
2371 (let ((p (ignore-errors (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit
))))
2373 ((or (not p
) (equal p
"nil")) org-log-into-drawer
)
2374 ((equal p
"t") "LOGBOOK")
2377 (defcustom org-log-state-notes-insert-after-drawers nil
2378 "Non-nil means insert state change notes after any drawers in entry.
2379 Only the drawers that *immediately* follow the headline and the
2380 deadline/scheduled line are skipped.
2381 When nil, insert notes right after the heading and perhaps the line
2382 with deadline/scheduling if present.
2384 This variable will have no effect if `org-log-into-drawer' is
2387 :group
'org-progress
2390 (defcustom org-log-states-order-reversed t
2391 "Non-nil means the latest state note will be directly after heading.
2392 When nil, the state change notes will be ordered according to time."
2394 :group
'org-progress
2397 (defcustom org-todo-repeat-to-state nil
2398 "The TODO state to which a repeater should return the repeating task.
2399 By default this is the first task in a TODO sequence, or the previous state
2400 in a TODO_TYP set. But you can specify another task here.
2401 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2403 :type
'(choice (const :tag
"Head of sequence" nil
)
2404 (string :tag
"Specific state")))
2406 (defcustom org-log-repeat
'time
2407 "Non-nil means record moving through the DONE state when triggering repeat.
2408 An auto-repeating task is immediately switched back to TODO when
2409 marked DONE. If you are not logging state changes (by adding \"@\"
2410 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2411 record a closing note, there will be no record of the task moving
2412 through DONE. This variable forces taking a note anyway.
2414 nil Don't force a record
2415 time Record a time stamp
2418 This option can also be set with on a per-file-basis with
2420 #+STARTUP: logrepeat
2421 #+STARTUP: lognoterepeat
2422 #+STARTUP: nologrepeat
2424 You can have local logging settings for a subtree by setting the LOGGING
2425 property to one or more of these keywords."
2427 :group
'org-progress
2429 (const :tag
"Don't force a record" nil
)
2430 (const :tag
"Force recording the DONE state" time
)
2431 (const :tag
"Force recording a note with the DONE state" note
)))
2434 (defgroup org-priorities nil
2435 "Priorities in Org-mode."
2436 :tag
"Org Priorities"
2439 (defcustom org-enable-priority-commands t
2440 "Non-nil means priority commands are active.
2441 When nil, these commands will be disabled, so that you never accidentally
2443 :group
'org-priorities
2446 (defcustom org-highest-priority ?A
2447 "The highest priority of TODO items. A character like ?A, ?B etc.
2448 Must have a smaller ASCII number than `org-lowest-priority'."
2449 :group
'org-priorities
2452 (defcustom org-lowest-priority ?C
2453 "The lowest priority of TODO items. A character like ?A, ?B etc.
2454 Must have a larger ASCII number than `org-highest-priority'."
2455 :group
'org-priorities
2458 (defcustom org-default-priority ?B
2459 "The default priority of TODO items.
2460 This is the priority an item get if no explicit priority is given."
2461 :group
'org-priorities
2464 (defcustom org-priority-start-cycle-with-default t
2465 "Non-nil means start with default priority when starting to cycle.
2466 When this is nil, the first step in the cycle will be (depending on the
2467 command used) one higher or lower that the default priority."
2468 :group
'org-priorities
2471 (defgroup org-time nil
2472 "Options concerning time stamps and deadlines in Org-mode."
2476 (defcustom org-insert-labeled-timestamps-at-point nil
2477 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2478 When nil, these labeled time stamps are forces into the second line of an
2479 entry, just after the headline. When scheduling from the global TODO list,
2480 the time stamp will always be forced into the second line."
2484 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2485 "Formats for `format-time-string' which are used for time stamps.
2486 It is not recommended to change this constant.")
2488 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2489 "Number of minutes to round time stamps to.
2490 These are two values, the first applies when first creating a time stamp.
2491 The second applies when changing it with the commands `S-up' and `S-down'.
2492 When changing the time stamp, this means that it will change in steps
2493 of N minutes, as given by the second value.
2495 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2496 numbers should be factors of 60, so for example 5, 10, 15.
2498 When this is larger than 1, you can still force an exact time stamp by using
2499 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2500 and by using a prefix arg to `S-up/down' to specify the exact number
2501 of minutes to shift."
2503 :get
'(lambda (var) ; Make sure both elements are there
2504 (if (integerp (default-value var
))
2505 (list (default-value var
) 5)
2506 (default-value var
)))
2508 (integer :tag
"when inserting times")
2509 (integer :tag
"when modifying times")))
2511 ;; Normalize old customizations of this variable.
2512 (when (integerp org-time-stamp-rounding-minutes
)
2513 (setq org-time-stamp-rounding-minutes
2514 (list org-time-stamp-rounding-minutes
2515 org-time-stamp-rounding-minutes
)))
2517 (defcustom org-display-custom-times nil
2518 "Non-nil means overlay custom formats over all time stamps.
2519 The formats are defined through the variable `org-time-stamp-custom-formats'.
2520 To turn this on on a per-file basis, insert anywhere in the file:
2521 #+STARTUP: customtime"
2525 (make-variable-buffer-local 'org-display-custom-times
)
2527 (defcustom org-time-stamp-custom-formats
2528 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2529 "Custom formats for time stamps. See `format-time-string' for the syntax.
2530 These are overlayed over the default ISO format if the variable
2531 `org-display-custom-times' is set. Time like %H:%M should be at the
2532 end of the second format. The custom formats are also honored by export
2533 commands, if custom time display is turned on at the time of export."
2537 (defun org-time-stamp-format (&optional long inactive
)
2538 "Get the right format for a time string."
2539 (let ((f (if long
(cdr org-time-stamp-formats
)
2540 (car org-time-stamp-formats
))))
2542 (concat "[" (substring f
1 -
1) "]")
2545 (defcustom org-time-clocksum-format
"%d:%02d"
2546 "The format string used when creating CLOCKSUM lines.
2547 This is also used when org-mode generates a time duration."
2551 (defcustom org-time-clocksum-use-fractional nil
2552 "If non-nil, \\[org-clock-display] uses fractional times.
2553 org-mode generates a time duration."
2557 (defcustom org-time-clocksum-fractional-format
"%.2f"
2558 "The format string used when creating CLOCKSUM lines, or when
2559 org-mode generates a time duration."
2563 (defcustom org-deadline-warning-days
14
2564 "No. of days before expiration during which a deadline becomes active.
2565 This variable governs the display in sparse trees and in the agenda.
2566 When 0 or negative, it means use this number (the absolute value of it)
2567 even if a deadline has a different individual lead time specified.
2569 Custom commands can set this variable in the options section."
2571 :group
'org-agenda-daily
/weekly
2574 (defcustom org-read-date-prefer-future t
2575 "Non-nil means assume future for incomplete date input from user.
2576 This affects the following situations:
2577 1. The user gives a month but not a year.
2578 For example, if it is April and you enter \"feb 2\", this will be read
2579 as Feb 2, *next* year. \"May 5\", however, will be this year.
2580 2. The user gives a day, but no month.
2581 For example, if today is the 15th, and you enter \"3\", Org-mode will
2582 read this as the third of *next* month. However, if you enter \"17\",
2583 it will be considered as *this* month.
2585 If you set this variable to the symbol `time', then also the following
2588 3. If the user gives a time, but no day. If the time is before now,
2589 to will be interpreted as tomorrow.
2591 Currently none of this works for ISO week specifications.
2593 When this option is nil, the current day, month and year will always be
2597 (const :tag
"Never" nil
)
2598 (const :tag
"Check month and day" t
)
2599 (const :tag
"Check month, day, and time" time
)))
2601 (defcustom org-read-date-display-live t
2602 "Non-nil means display current interpretation of date prompt live.
2603 This display will be in an overlay, in the minibuffer."
2607 (defcustom org-read-date-popup-calendar t
2608 "Non-nil means pop up a calendar when prompting for a date.
2609 In the calendar, the date can be selected with mouse-1. However, the
2610 minibuffer will also be active, and you can simply enter the date as well.
2611 When nil, only the minibuffer will be available."
2614 (if (fboundp 'defvaralias
)
2615 (defvaralias 'org-popup-calendar-for-date-prompt
2616 'org-read-date-popup-calendar
))
2618 (defcustom org-read-date-minibuffer-setup-hook nil
2619 "Hook to be used to set up keys for the date/time interface.
2620 Add key definitions to `minibuffer-local-map', which will be a temporary
2625 (defcustom org-extend-today-until
0
2626 "The hour when your day really ends. Must be an integer.
2627 This has influence for the following applications:
2628 - When switching the agenda to \"today\". It it is still earlier than
2629 the time given here, the day recognized as TODAY is actually yesterday.
2630 - When a date is read from the user and it is still before the time given
2631 here, the current date and time will be assumed to be yesterday, 23:59.
2632 Also, timestamps inserted in remember templates follow this rule.
2634 IMPORTANT: This is a feature whose implementation is and likely will
2635 remain incomplete. Really, it is only here because past midnight seems to
2636 be the favorite working time of John Wiegley :-)"
2640 (defcustom org-edit-timestamp-down-means-later nil
2641 "Non-nil means S-down will increase the time in a time stamp.
2642 When nil, S-up will increase."
2646 (defcustom org-calendar-follow-timestamp-change t
2647 "Non-nil means make the calendar window follow timestamp changes.
2648 When a timestamp is modified and the calendar window is visible, it will be
2649 moved to the new date."
2653 (defgroup org-tags nil
2654 "Options concerning tags in Org-mode."
2658 (defcustom org-tag-alist nil
2659 "List of tags allowed in Org-mode files.
2660 When this list is nil, Org-mode will base TAG input on what is already in the
2662 The value of this variable is an alist, the car of each entry must be a
2663 keyword as a string, the cdr may be a character that is used to select
2664 that tag through the fast-tag-selection interface.
2665 See the manual for details."
2669 (cons (string :tag
"Tag name")
2670 (character :tag
"Access char"))
2671 (list :tag
"Start radio group"
2673 (option (string :tag
"Group description")))
2674 (list :tag
"End radio group"
2676 (option (string :tag
"Group description")))
2677 (const :tag
"New line" (:newline
)))))
2679 (defcustom org-tag-persistent-alist nil
2680 "List of tags that will always appear in all Org-mode files.
2681 This is in addition to any in buffer settings or customizations
2683 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2684 The value of this variable is an alist, the car of each entry must be a
2685 keyword as a string, the cdr may be a character that is used to select
2686 that tag through the fast-tag-selection interface.
2687 See the manual for details.
2688 To disable these tags on a per-file basis, insert anywhere in the file:
2693 (cons (string :tag
"Tag name")
2694 (character :tag
"Access char"))
2695 (const :tag
"Start radio group" (:startgroup
))
2696 (const :tag
"End radio group" (:endgroup
))
2697 (const :tag
"New line" (:newline
)))))
2699 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2700 "If non-nil, always offer completion for all tags of all agenda files.
2701 Instead of customizing this variable directly, you might want to
2702 set it locally for remember buffers, because there no list of
2703 tags in that file can be created dynamically (there are none).
2705 (add-hook 'org-remember-mode-hook
2707 (set (make-local-variable
2708 'org-complete-tags-always-offer-all-agenda-tags)
2713 (defvar org-file-tags nil
2714 "List of tags that can be inherited by all entries in the file.
2715 The tags will be inherited if the variable `org-use-tag-inheritance'
2716 says they should be.
2717 This variable is populated from #+FILETAGS lines.")
2719 (defcustom org-use-fast-tag-selection
'auto
2720 "Non-nil means use fast tag selection scheme.
2721 This is a special interface to select and deselect tags with single keys.
2722 When nil, fast selection is never used.
2723 When the symbol `auto', fast selection is used if and only if selection
2724 characters for tags have been configured, either through the variable
2725 `org-tag-alist' or through a #+TAGS line in the buffer.
2726 When t, fast selection is always used and selection keys are assigned
2727 automatically if necessary."
2730 (const :tag
"Always" t
)
2731 (const :tag
"Never" nil
)
2732 (const :tag
"When selection characters are configured" 'auto
)))
2734 (defcustom org-fast-tag-selection-single-key nil
2735 "Non-nil means fast tag selection exits after first change.
2736 When nil, you have to press RET to exit it.
2737 During fast tag selection, you can toggle this flag with `C-c'.
2738 This variable can also have the value `expert'. In this case, the window
2739 displaying the tags menu is not even shown, until you press C-c again."
2742 (const :tag
"No" nil
)
2743 (const :tag
"Yes" t
)
2744 (const :tag
"Expert" expert
)))
2746 (defvar org-fast-tag-selection-include-todo nil
2747 "Non-nil means fast tags selection interface will also offer TODO states.
2748 This is an undocumented feature, you should not rely on it.")
2750 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2751 "The column to which tags should be indented in a headline.
2752 If this number is positive, it specifies the column. If it is negative,
2753 it means that the tags should be flushright to that column. For example,
2754 -80 works well for a normal 80 character screen."
2758 (defcustom org-auto-align-tags t
2759 "Non-nil means realign tags after pro/demotion of TODO state change.
2760 These operations change the length of a headline and therefore shift
2761 the tags around. With this options turned on, after each such operation
2762 the tags are again aligned to `org-tags-column'."
2766 (defcustom org-use-tag-inheritance t
2767 "Non-nil means tags in levels apply also for sublevels.
2768 When nil, only the tags directly given in a specific line apply there.
2769 This may also be a list of tags that should be inherited, or a regexp that
2770 matches tags that should be inherited. Additional control is possible
2771 with the variable `org-tags-exclude-from-inheritance' which gives an
2772 explicit list of tags to be excluded from inheritance., even if the value of
2773 `org-use-tag-inheritance' would select it for inheritance.
2775 If this option is t, a match early-on in a tree can lead to a large
2776 number of matches in the subtree when constructing the agenda or creating
2777 a sparse tree. If you only want to see the first match in a tree during
2778 a search, check out the variable `org-tags-match-list-sublevels'."
2781 (const :tag
"Not" nil
)
2782 (const :tag
"Always" t
)
2783 (repeat :tag
"Specific tags" (string :tag
"Tag"))
2784 (regexp :tag
"Tags matched by regexp")))
2786 (defcustom org-tags-exclude-from-inheritance nil
2787 "List of tags that should never be inherited.
2788 This is a way to exclude a few tags from inheritance. For way to do
2789 the opposite, to actively allow inheritance for selected tags,
2790 see the variable `org-use-tag-inheritance'."
2792 :type
'(repeat (string :tag
"Tag")))
2794 (defun org-tag-inherit-p (tag)
2795 "Check if TAG is one that should be inherited."
2797 ((member tag org-tags-exclude-from-inheritance
) nil
)
2798 ((eq org-use-tag-inheritance t
) t
)
2799 ((not org-use-tag-inheritance
) nil
)
2800 ((stringp org-use-tag-inheritance
)
2801 (string-match org-use-tag-inheritance tag
))
2802 ((listp org-use-tag-inheritance
)
2803 (member tag org-use-tag-inheritance
))
2804 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
2806 (defcustom org-tags-match-list-sublevels t
2807 "Non-nil means list also sublevels of headlines matching a search.
2808 This variable applies to tags/property searches, and also to stuck
2809 projects because this search is based on a tags match as well.
2811 When set to the symbol `indented', sublevels are indented with
2814 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2815 the sublevels of a headline matching a tag search often also match
2816 the same search. Listing all of them can create very long lists.
2817 Setting this variable to nil causes subtrees of a match to be skipped.
2819 This variable is semi-obsolete and probably should always be true. It
2820 is better to limit inheritance to certain tags using the variables
2821 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
2824 (const :tag
"No, don't list them" nil
)
2825 (const :tag
"Yes, do list them" t
)
2826 (const :tag
"List them, indented with leading dots" indented
)))
2828 (defcustom org-tags-sort-function nil
2829 "When set, tags are sorted using this function as a comparator."
2832 (const :tag
"No sorting" nil
)
2833 (const :tag
"Alphabetical" string
<)
2834 (const :tag
"Reverse alphabetical" string
>)
2835 (function :tag
"Custom function" nil
)))
2837 (defvar org-tags-history nil
2838 "History of minibuffer reads for tags.")
2839 (defvar org-last-tags-completion-table nil
2840 "The last used completion table for tags.")
2841 (defvar org-after-tags-change-hook nil
2842 "Hook that is run after the tags in a line have changed.")
2844 (defgroup org-properties nil
2845 "Options concerning properties in Org-mode."
2846 :tag
"Org Properties"
2849 (defcustom org-property-format
"%-10s %s"
2850 "How property key/value pairs should be formatted by `indent-line'.
2851 When `indent-line' hits a property definition, it will format the line
2852 according to this format, mainly to make sure that the values are
2853 lined-up with respect to each other."
2854 :group
'org-properties
2857 (defcustom org-use-property-inheritance nil
2858 "Non-nil means properties apply also for sublevels.
2860 This setting is chiefly used during property searches. Turning it on can
2861 cause significant overhead when doing a search, which is why it is not
2864 When nil, only the properties directly given in the current entry count.
2865 When t, every property is inherited. The value may also be a list of
2866 properties that should have inheritance, or a regular expression matching
2867 properties that should be inherited.
2869 However, note that some special properties use inheritance under special
2870 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2871 and the properties ending in \"_ALL\" when they are used as descriptor
2872 for valid values of a property.
2874 Note for programmers:
2875 When querying an entry with `org-entry-get', you can control if inheritance
2876 should be used. By default, `org-entry-get' looks only at the local
2877 properties. You can request inheritance by setting the inherit argument
2878 to t (to force inheritance) or to `selective' (to respect the setting
2880 :group
'org-properties
2882 (const :tag
"Not" nil
)
2883 (const :tag
"Always" t
)
2884 (repeat :tag
"Specific properties" (string :tag
"Property"))
2885 (regexp :tag
"Properties matched by regexp")))
2887 (defun org-property-inherit-p (property)
2888 "Check if PROPERTY is one that should be inherited."
2890 ((eq org-use-property-inheritance t
) t
)
2891 ((not org-use-property-inheritance
) nil
)
2892 ((stringp org-use-property-inheritance
)
2893 (string-match org-use-property-inheritance property
))
2894 ((listp org-use-property-inheritance
)
2895 (member property org-use-property-inheritance
))
2896 (t (error "Invalid setting of `org-use-property-inheritance'"))))
2898 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
2899 "The default column format, if no other format has been defined.
2900 This variable can be set on the per-file basis by inserting a line
2902 #+COLUMNS: %25ITEM ....."
2903 :group
'org-properties
2906 (defcustom org-columns-ellipses
".."
2907 "The ellipses to be used when a field in column view is truncated.
2908 When this is the empty string, as many characters as possible are shown,
2909 but then there will be no visual indication that the field has been truncated.
2910 When this is a string of length N, the last N characters of a truncated
2911 field are replaced by this string. If the column is narrower than the
2912 ellipses string, only part of the ellipses string will be shown."
2913 :group
'org-properties
2916 (defcustom org-columns-modify-value-for-display-function nil
2917 "Function that modifies values for display in column view.
2918 For example, it can be used to cut out a certain part from a time stamp.
2919 The function must take 2 arguments:
2921 column-title The title of the column (*not* the property name)
2922 value The value that should be modified.
2924 The function should return the value that should be displayed,
2925 or nil if the normal value should be used."
2926 :group
'org-properties
2929 (defcustom org-effort-property
"Effort"
2930 "The property that is being used to keep track of effort estimates.
2931 Effort estimates given in this property need to have the format H:MM."
2932 :group
'org-properties
2933 :group
'org-progress
2934 :type
'(string :tag
"Property"))
2936 (defconst org-global-properties-fixed
2937 '(("VISIBILITY_ALL" .
"folded children content all")
2938 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
2939 "List of property/value pairs that can be inherited by any entry.
2941 These are fixed values, for the preset properties. The user variable
2942 that can be used to add to this list is `org-global-properties'.
2944 The entries in this list are cons cells where the car is a property
2945 name and cdr is a string with the value. If the value represents
2946 multiple items like an \"_ALL\" property, separate the items by
2949 (defcustom org-global-properties nil
2950 "List of property/value pairs that can be inherited by any entry.
2952 This list will be combined with the constant `org-global-properties-fixed'.
2954 The entries in this list are cons cells where the car is a property
2955 name and cdr is a string with the value.
2957 You can set buffer-local values for the same purpose in the variable
2958 `org-file-properties' this by adding lines like
2960 #+PROPERTY: NAME VALUE"
2961 :group
'org-properties
2963 (cons (string :tag
"Property")
2964 (string :tag
"Value"))))
2966 (defvar org-file-properties nil
2967 "List of property/value pairs that can be inherited by any entry.
2968 Valid for the current buffer.
2969 This variable is populated from #+PROPERTY lines.")
2970 (make-variable-buffer-local 'org-file-properties
)
2972 (defgroup org-agenda nil
2973 "Options concerning agenda views in Org-mode."
2977 (defvar org-category nil
2978 "Variable used by org files to set a category for agenda display.
2979 Such files should use a file variable to set it, for example
2981 # -*- mode: org; org-category: \"ELisp\"
2983 or contain a special line
2987 If the file does not specify a category, then file's base name
2989 (make-variable-buffer-local 'org-category
)
2990 (put 'org-category
'safe-local-variable
'(lambda (x) (or (symbolp x
) (stringp x
))))
2992 (defcustom org-agenda-files nil
2993 "The files to be used for agenda display.
2994 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2995 \\[org-remove-file]. You can also use customize to edit the list.
2997 If an entry is a directory, all files in that directory that are matched by
2998 `org-agenda-file-regexp' will be part of the file list.
3000 If the value of the variable is not a list but a single file name, then
3001 the list of agenda files is actually stored and maintained in that file, one
3002 agenda file per line. In this file paths can be given relative to
3003 `org-directory'. Tilde expansion and environment variable substitution
3007 (repeat :tag
"List of files and directories" file
)
3008 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3010 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3011 "Regular expression to match files for `org-agenda-files'.
3012 If any element in the list in that variable contains a directory instead
3013 of a normal file, all files in that directory that are matched by this
3014 regular expression will be included."
3018 (defcustom org-agenda-text-search-extra-files nil
3019 "List of extra files to be searched by text search commands.
3020 These files will be search in addition to the agenda files by the
3021 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3022 Note that these files will only be searched for text search commands,
3023 not for the other agenda views like todo lists, tag searches or the weekly
3024 agenda. This variable is intended to list notes and possibly archive files
3025 that should also be searched by these two commands.
3026 In fact, if the first element in the list is the symbol `agenda-archives',
3027 than all archive files of all agenda files will be added to the search
3030 :type
'(set :greedy t
3031 (const :tag
"Agenda Archives" agenda-archives
)
3032 (repeat :inline t
(file))))
3034 (if (fboundp 'defvaralias
)
3035 (defvaralias 'org-agenda-multi-occur-extra-files
3036 'org-agenda-text-search-extra-files
))
3038 (defcustom org-agenda-skip-unavailable-files nil
3039 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3040 A nil value means to remove them, after a query, from the list."
3044 (defcustom org-calendar-to-agenda-key
[?c
]
3045 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3046 The command `org-calendar-goto-agenda' will be bound to this key. The
3047 default is the character `c' because then `c' can be used to switch back and
3048 forth between agenda and calendar."
3052 (defcustom org-calendar-agenda-action-key
[?k
]
3053 "The key to be installed in `calendar-mode-map' for agenda-action.
3054 The command `org-agenda-action' will be bound to this key. The
3055 default is the character `k' because we use the same key in the agenda."
3059 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3060 "The key to be installed in `calendar-mode-map' for adding diary entries.
3061 This option is irrelevant until `org-agenda-diary-file' has been configured
3062 to point to an Org-mode file. When that is the case, the command
3063 `org-agenda-diary-entry' will be bound to the key given here, by default
3064 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3065 if you want to continue doing this, you need to change this to a different
3070 (defcustom org-agenda-diary-file
'diary-file
3071 "File to which to add new entries with the `i' key in agenda and calendar.
3072 When this is the symbol `diary-file', the functionality in the Emacs
3073 calendar will be used to add entries to the `diary-file'. But when this
3074 points to a file, `org-agenda-diary-entry' will be used instead."
3077 (const :tag
"The standard Emacs diary file" diary-file
)
3078 (file :tag
"Special Org file diary entries")))
3080 (eval-after-load "calendar"
3082 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3083 'org-calendar-goto-agenda
)
3084 (org-defkey calendar-mode-map org-calendar-agenda-action-key
3086 (add-hook 'calendar-mode-hook
3088 (unless (eq org-agenda-diary-file
'diary-file
)
3089 (define-key calendar-mode-map
3090 org-calendar-insert-diary-entry-key
3091 'org-agenda-diary-entry
))))))
3093 (defgroup org-latex nil
3094 "Options for embedding LaTeX code into Org-mode."
3098 (defcustom org-format-latex-options
3099 '(:foreground default
:background default
:scale
1.0
3100 :html-foreground
"Black" :html-background
"Transparent"
3101 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3102 "Options for creating images from LaTeX fragments.
3103 This is a property list with the following properties:
3104 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3105 `default' means use the foreground of the default face.
3106 :background the background color, or \"Transparent\".
3107 `default' means use the background of the default face.
3108 :scale a scaling factor for the size of the images, to get more pixels
3109 :html-foreground, :html-background, :html-scale
3110 the same numbers for HTML export.
3111 :matchers a list indicating which matchers should be used to
3112 find LaTeX fragments. Valid members of this list are:
3113 \"begin\" find environments
3114 \"$1\" find single characters surrounded by $.$
3115 \"$\" find math expressions surrounded by $...$
3116 \"$$\" find math expressions surrounded by $$....$$
3117 \"\\(\" find math expressions surrounded by \\(...\\)
3118 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3122 (defcustom org-format-latex-signal-error t
3123 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3124 When nil, just push out a message."
3128 (defcustom org-format-latex-header
"\\documentclass{article}
3129 \\usepackage[usenames]{color}
3130 \\usepackage{amsmath}
3131 \\usepackage[mathscr]{eucal}
3132 \\pagestyle{empty} % do not remove
3135 % The settings below are copied from fullpage.sty
3136 \\setlength{\\textwidth}{\\paperwidth}
3137 \\addtolength{\\textwidth}{-3cm}
3138 \\setlength{\\oddsidemargin}{1.5cm}
3139 \\addtolength{\\oddsidemargin}{-2.54cm}
3140 \\setlength{\\evensidemargin}{\\oddsidemargin}
3141 \\setlength{\\textheight}{\\paperheight}
3142 \\addtolength{\\textheight}{-\\headheight}
3143 \\addtolength{\\textheight}{-\\headsep}
3144 \\addtolength{\\textheight}{-\\footskip}
3145 \\addtolength{\\textheight}{-3cm}
3146 \\setlength{\\topmargin}{1.5cm}
3147 \\addtolength{\\topmargin}{-2.54cm}"
3148 "The document header used for processing LaTeX fragments.
3149 It is imperative that this header make sure that no page number
3150 appears on the page. The package defined in the variables
3151 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
3152 will either replace the placeholder \"[PACKAGES]\" in this header, or they
3157 (defvar org-format-latex-header-extra nil
)
3159 (defun org-set-packages-alist (var val
)
3160 "Set the packages alist and make sure it has 3 elements per entry."
3161 (set var
(mapcar (lambda (x)
3162 (if (and (consp x
) (= (length x
) 2))
3163 (list (car x
) (nth 1 x
) t
)
3167 (defun org-get-packages-alist (var)
3169 "Get the packages alist and make sure it has 3 elements per entry."
3171 (if (and (consp x
) (= (length x
) 2))
3172 (list (car x
) (nth 1 x
) t
)
3174 (default-value var
)))
3176 ;; The following variables are defined here because is it also used
3177 ;; when formatting latex fragments. Originally it was part of the
3178 ;; LaTeX exporter, which is why the name includes "export".
3179 (defcustom org-export-latex-default-packages-alist
3180 '(("AUTO" "inputenc" t
)
3184 ("" "longtable" nil
)
3196 "Alist of default packages to be inserted in the header.
3197 Change this only if one of the packages here causes an incompatibility
3198 with another package you are using.
3199 The packages in this list are needed by one part or another of Org-mode
3200 to function properly.
3202 - inputenc, fontenc: for basic font and character selection
3203 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
3204 for interpreting the entities in `org-entities'. You can skip some of these
3205 packages if you don't use any of the symbols in it.
3206 - graphicx: for including images
3207 - float, wrapfig: for figure placement
3208 - longtable: for long tables
3209 - hyperref: for cross references
3211 Therefore you should not modify this variable unless you know what you
3212 are doing. The one reason to change it anyway is that you might be loading
3213 some other package that conflicts with one of the default packages.
3214 Each cell is of the format \( \"options\" \"package\" snippet-flag\).
3215 If SNIPPET-FLAG is t, the package also needs to be included when
3216 compiling LaTeX snippets into images for inclusion into HTML."
3217 :group
'org-export-latex
3218 :set
'org-set-packages-alist
3219 :get
'org-get-packages-alist
3222 (list :tag
"options/package pair"
3223 (string :tag
"options")
3224 (string :tag
"package")
3225 (boolean :tag
"Snippet"))
3226 (string :tag
"A line of LaTeX"))))
3228 (defcustom org-export-latex-packages-alist nil
3229 "Alist of packages to be inserted in every LaTeX header.
3230 These will be inserted after `org-export-latex-default-packages-alist'.
3231 Each cell is of the format \( \"options\" \"package\" snippet-flag \).
3232 SNIPPET-FLAG, when t, indicates that this package is also needed when
3233 turning LaTeX snippets into images for inclusion into HTML.
3234 Make sure that you only list packages here which:
3235 - you want in every file
3236 - do not conflict with the default packages in
3237 `org-export-latex-default-packages-alist'
3238 - do not conflict with the setup in `org-format-latex-header'."
3239 :group
'org-export-latex
3240 :set
'org-set-packages-alist
3241 :get
'org-get-packages-alist
3244 (list :tag
"options/package pair"
3245 (string :tag
"options")
3246 (string :tag
"package")
3247 (boolean :tag
"Snippet"))
3248 (string :tag
"A line of LaTeX"))))
3251 (defgroup org-appearance nil
3252 "Settings for Org-mode appearance."
3253 :tag
"Org Appearance"
3256 (defcustom org-level-color-stars-only nil
3257 "Non-nil means fontify only the stars in each headline.
3258 When nil, the entire headline is fontified.
3259 Changing it requires restart of `font-lock-mode' to become effective
3260 also in regions already fontified."
3261 :group
'org-appearance
3264 (defcustom org-hide-leading-stars nil
3265 "Non-nil means hide the first N-1 stars in a headline.
3266 This works by using the face `org-hide' for these stars. This
3267 face is white for a light background, and black for a dark
3268 background. You may have to customize the face `org-hide' to
3270 Changing it requires restart of `font-lock-mode' to become effective
3271 also in regions already fontified.
3272 You may also set this on a per-file basis by adding one of the following
3273 lines to the buffer:
3275 #+STARTUP: hidestars
3276 #+STARTUP: showstars"
3277 :group
'org-appearance
3280 (defcustom org-hidden-keywords nil
3281 "List of keywords that should be hidden when typed in the org buffer.
3282 For example, add #+TITLE to this list in order to make the
3283 document title appear in the buffer without the initial #+TITLE:
3285 :group
'org-appearance
3286 :type
'(set (const :tag
"#+AUTHOR" author
)
3287 (const :tag
"#+DATE" date
)
3288 (const :tag
"#+EMAIL" email
)
3289 (const :tag
"#+TITLE" title
)))
3291 (defcustom org-fontify-done-headline nil
3292 "Non-nil means change the face of a headline if it is marked DONE.
3293 Normally, only the TODO/DONE keyword indicates the state of a headline.
3294 When this is non-nil, the headline after the keyword is set to the
3295 `org-headline-done' as an additional indication."
3296 :group
'org-appearance
3299 (defcustom org-fontify-emphasized-text t
3300 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3301 Changing this variable requires a restart of Emacs to take effect."
3302 :group
'org-appearance
3305 (defcustom org-fontify-whole-heading-line nil
3306 "Non-nil means fontify the whole line for headings.
3307 This is useful when setting a background color for the
3309 :group
'org-appearance
3312 (defcustom org-highlight-latex-fragments-and-specials nil
3313 "Non-nil means fontify what is treated specially by the exporters."
3314 :group
'org-appearance
3317 (defcustom org-hide-emphasis-markers nil
3318 "Non-nil mean font-lock should hide the emphasis marker characters."
3319 :group
'org-appearance
3322 (defcustom org-pretty-entities nil
3323 "Non-nil means show entities as UTF8 characters.
3324 When nil, the \\name form remains in the buffer."
3325 :group
'org-appearance
3328 (defcustom org-pretty-entities-include-sub-superscripts t
3329 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3330 :group
'org-appearance
3333 (defvar org-emph-re nil
3334 "Regular expression for matching emphasis.
3335 After a match, the match groups contain these elements:
3336 0 The match of the full regular expression, including the characters
3337 before and after the proper match
3338 1 The character before the proper match, or empty at beginning of line
3339 2 The proper match, including the leading and trailing markers
3340 3 The leading marker like * or /, indicating the type of highlighting
3341 4 The text between the emphasis markers, not including the markers
3342 5 The character after the match, empty at the end of a line")
3343 (defvar org-verbatim-re nil
3344 "Regular expression for matching verbatim text.")
3345 (defvar org-emphasis-regexp-components
) ; defined just below
3346 (defvar org-emphasis-alist
) ; defined just below
3347 (defun org-set-emph-re (var val
)
3348 "Set variable and compute the emphasis regular expression."
3350 (when (and (boundp 'org-emphasis-alist
)
3351 (boundp 'org-emphasis-regexp-components
)
3352 org-emphasis-alist org-emphasis-regexp-components
)
3353 (let* ((e org-emphasis-regexp-components
)
3359 (body1 (concat body
"*?"))
3360 (markers (mapconcat 'car org-emphasis-alist
""))
3361 (vmarkers (mapconcat
3362 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3363 org-emphasis-alist
"")))
3364 ;; make sure special characters appear at the right position in the class
3365 (if (string-match "\\^" markers
)
3366 (setq markers
(concat (replace-match "" t t markers
) "^")))
3367 (if (string-match "-" markers
)
3368 (setq markers
(concat (replace-match "" t t markers
) "-")))
3369 (if (string-match "\\^" vmarkers
)
3370 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3371 (if (string-match "-" vmarkers
)
3372 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3374 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3375 (int-to-string nl
) "\\}")))
3378 (concat "\\([" pre
"]\\|^\\)"
3380 "\\([" markers
"]\\)"
3388 "\\([" post
"]\\|$\\)"))
3389 (setq org-verbatim-re
3390 (concat "\\([" pre
"]\\|^\\)"
3392 "\\([" vmarkers
"]\\)"
3400 "\\([" post
"]\\|$\\)")))))
3402 (defcustom org-emphasis-regexp-components
3403 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3404 "Components used to build the regular expression for emphasis.
3405 This is a list with 6 entries. Terminology: In an emphasis string
3406 like \" *strong word* \", we call the initial space PREMATCH, the final
3407 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3408 and \"trong wor\" is the body. The different components in this variable
3409 specify what is allowed/forbidden in each part:
3411 pre Chars allowed as prematch. Beginning of line will be allowed too.
3412 post Chars allowed as postmatch. End of line will be allowed too.
3413 border The chars *forbidden* as border characters.
3414 body-regexp A regexp like \".\" to match a body character. Don't use
3415 non-shy groups here, and don't allow newline here.
3416 newline The maximum number of newlines allowed in an emphasis exp.
3418 Use customize to modify this, or restart Emacs after changing it."
3419 :group
'org-appearance
3420 :set
'org-set-emph-re
3422 (sexp :tag
"Allowed chars in pre ")
3423 (sexp :tag
"Allowed chars in post ")
3424 (sexp :tag
"Forbidden chars in border ")
3425 (sexp :tag
"Regexp for body ")
3426 (integer :tag
"number of newlines allowed")
3427 (option (boolean :tag
"Please ignore this button"))))
3429 (defcustom org-emphasis-alist
3430 `(("*" bold
"<b>" "</b>")
3431 ("/" italic
"<i>" "</i>")
3432 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
3433 ("=" org-code
"<code>" "</code>" verbatim
)
3434 ("~" org-verbatim
"<code>" "</code>" verbatim
)
3435 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
3438 "Special syntax for emphasized text.
3439 Text starting and ending with a special character will be emphasized, for
3440 example *bold*, _underlined_ and /italic/. This variable sets the marker
3441 characters, the face to be used by font-lock for highlighting in Org-mode
3442 Emacs buffers, and the HTML tags to be used for this.
3443 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3444 For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
3445 Use customize to modify this, or restart Emacs after changing it."
3446 :group
'org-appearance
3447 :set
'org-set-emph-re
3450 (string :tag
"Marker character")
3452 (face :tag
"Font-lock-face")
3453 (plist :tag
"Face property list"))
3454 (string :tag
"HTML start tag")
3455 (string :tag
"HTML end tag")
3456 (option (const verbatim
)))))
3458 (defvar org-protecting-blocks
3459 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3460 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3461 This is needed for font-lock setup.")
3463 ;;; Miscellaneous options
3465 (defgroup org-completion nil
3466 "Completion in Org-mode."
3467 :tag
"Org Completion"
3470 (defcustom org-completion-use-ido nil
3471 "Non-nil means use ido completion wherever possible.
3472 Note that `ido-mode' must be active for this variable to be relevant.
3473 If you decide to turn this variable on, you might well want to turn off
3474 `org-outline-path-complete-in-steps'.
3475 See also `org-completion-use-iswitchb'."
3476 :group
'org-completion
3479 (defcustom org-completion-use-iswitchb nil
3480 "Non-nil means use iswitchb completion wherever possible.
3481 Note that `iswitchb-mode' must be active for this variable to be relevant.
3482 If you decide to turn this variable on, you might well want to turn off
3483 `org-outline-path-complete-in-steps'.
3484 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3485 :group
'org-completion
3488 (defcustom org-completion-fallback-command
'hippie-expand
3489 "The expansion command called by \\[org-complete] in normal context.
3490 Normal means no org-mode-specific context."
3491 :group
'org-completion
3494 ;;; Functions and variables from their packages
3495 ;; Declared here to avoid compiler warnings
3498 (defvar outline-mode-menu-heading
)
3499 (defvar outline-mode-menu-show
)
3500 (defvar outline-mode-menu-hide
)
3501 (defvar zmacs-regions
) ; XEmacs regions
3504 (defvar mark-active
)
3507 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
3508 (declare-function calendar-forward-day
"cal-move" (arg))
3509 (declare-function calendar-goto-date
"cal-move" (date))
3510 (declare-function calendar-goto-today
"cal-move" ())
3511 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
3512 (defvar calc-embedded-close-formula
)
3513 (defvar calc-embedded-open-formula
)
3514 (declare-function cdlatex-tab
"ext:cdlatex" ())
3515 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
3516 (defvar font-lock-unfontify-region-function
)
3517 (declare-function iswitchb-read-buffer
"iswitchb"
3518 (prompt &optional default require-match start matches-set
))
3519 (defvar iswitchb-temp-buflist
)
3520 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
3521 (defvar org-agenda-tags-todo-honor-ignore-options
)
3522 (declare-function org-agenda-skip
"org-agenda" ())
3524 org-format-agenda-item
"org-agenda"
3525 (extra txt
&optional category tags dotime noprefix remove-re habitp
))
3526 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
3527 (declare-function org-agenda-change-all-lines
"org-agenda"
3528 (newhead hdmarker
&optional fixface just-this
))
3529 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
3530 (declare-function org-agenda-maybe-redo
"org-agenda" ())
3531 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
3533 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
3534 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3535 "org-agenda" (&optional end
))
3536 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
3537 (declare-function org-indent-mode
"org-indent" (&optional arg
))
3538 (declare-function parse-time-string
"parse-time" (string))
3539 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
3540 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
3541 (defvar remember-data-file
)
3542 (defvar texmathp-why
)
3543 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
3544 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
3546 (defvar w3m-current-url
)
3547 (defvar w3m-current-title
)
3549 (defvar org-latex-regexps
)
3551 ;;; Autoload and prepare some org modules
3553 ;; Some table stuff that needs to be defined here, because it is used
3554 ;; by the functions setting up org-mode or checking for table context.
3556 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
3557 "Detect an org-type or table-type table.")
3558 (defconst org-table-line-regexp
"^[ \t]*|"
3559 "Detect an org-type table line.")
3560 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
3561 "Detect an org-type table line.")
3562 (defconst org-table-hline-regexp
"^[ \t]*|-"
3563 "Detect an org-type table hline.")
3564 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
3565 "Detect a table-type table hline.")
3566 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
3567 "Detect the first line outside a table when searching from within it.
3568 This works for both table types.")
3570 ;; Autoload the functions in org-table.el that are needed by functions here.
3573 (org-autoload "org-table"
3574 '(org-table-align org-table-begin org-table-blank-field
3575 org-table-convert org-table-convert-region org-table-copy-down
3576 org-table-copy-region org-table-create
3577 org-table-create-or-convert-from-region
3578 org-table-create-with-table.el org-table-current-dline
3579 org-table-cut-region org-table-delete-column org-table-edit-field
3580 org-table-edit-formulas org-table-end org-table-eval-formula
3581 org-table-export org-table-field-info
3582 org-table-get-stored-formulas org-table-goto-column
3583 org-table-hline-and-move org-table-import org-table-insert-column
3584 org-table-insert-hline org-table-insert-row org-table-iterate
3585 org-table-justify-field-maybe org-table-kill-row
3586 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3587 org-table-move-column org-table-move-column-left
3588 org-table-move-column-right org-table-move-row
3589 org-table-move-row-down org-table-move-row-up
3590 org-table-next-field org-table-next-row org-table-paste-rectangle
3591 org-table-previous-field org-table-recalculate
3592 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3593 org-table-toggle-coordinate-overlays
3594 org-table-toggle-formula-debugger org-table-wrap-region
3595 orgtbl-mode turn-on-orgtbl org-table-to-lisp
3596 orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
3597 orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo
)))
3599 (defun org-at-table-p (&optional table-type
)
3600 "Return t if the cursor is inside an org-type table.
3601 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3602 (if org-enable-table-editor
3604 (beginning-of-line 1)
3605 (looking-at (if table-type org-table-any-line-regexp
3606 org-table-line-regexp
)))
3608 (defsubst org-table-p
() (org-at-table-p))
3610 (defun org-at-table.el-p
()
3611 "Return t if and only if we are at a table.el table."
3612 (and (org-at-table-p 'any
)
3614 (goto-char (org-table-begin 'any
))
3615 (looking-at org-table1-hline-regexp
))))
3616 (defun org-table-recognize-table.el
()
3617 "If there is a table.el table nearby, recognize it and move into it."
3618 (if org-table-tab-recognizes-table.el
3619 (if (org-at-table.el-p
)
3621 (beginning-of-line 1)
3622 (if (looking-at org-table-dataline-regexp
)
3624 (if (looking-at org-table1-hline-regexp
)
3626 (beginning-of-line 2)
3627 (if (looking-at org-table-any-border-regexp
)
3628 (beginning-of-line -
1)))))
3629 (if (re-search-forward "|" (org-table-end t
) t
)
3632 (if (table--at-cell-p (point))
3634 (message "recognizing table.el table...")
3635 (table-recognize-table)
3636 (message "recognizing table.el table...done")))
3637 (error "This should not happen"))
3642 (defun org-at-table-hline-p ()
3643 "Return t if the cursor is inside a hline in a table."
3644 (if org-enable-table-editor
3646 (beginning-of-line 1)
3647 (looking-at org-table-hline-regexp
))
3650 (defvar org-table-clean-did-remove-column nil
)
3652 (defun org-table-map-tables (function &optional quietly
)
3653 "Apply FUNCTION to the start of all tables in the buffer."
3657 (goto-char (point-min))
3658 (while (re-search-forward org-table-any-line-regexp nil t
)
3660 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
3661 (beginning-of-line 1)
3662 (when (looking-at org-table-line-regexp
)
3663 (save-excursion (funcall function
))
3664 (or (looking-at org-table-line-regexp
)
3666 (re-search-forward org-table-any-border-regexp nil
1))))
3667 (unless quietly
(message "Mapping tables: done")))
3669 ;; Declare and autoload functions from org-exp.el & Co
3671 (declare-function org-default-export-plist
"org-exp")
3672 (declare-function org-infile-export-plist
"org-exp")
3673 (declare-function org-get-current-options
"org-exp")
3675 (org-autoload "org-exp"
3676 '(org-export org-export-visible
3677 org-insert-export-options-template
3678 org-table-clean-before-export
))
3679 (org-autoload "org-ascii"
3680 '(org-export-as-ascii org-export-ascii-preprocess
3681 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3682 org-export-region-as-ascii
))
3683 (org-autoload "org-latex"
3684 '(org-export-as-latex-batch org-export-as-latex-to-buffer
3685 org-replace-region-by-latex org-export-region-as-latex
3686 org-export-as-latex org-export-as-pdf
3687 org-export-as-pdf-and-open
))
3688 (org-autoload "org-html"
3689 '(org-export-as-html-and-open
3690 org-export-as-html-batch org-export-as-html-to-buffer
3691 org-replace-region-by-html org-export-region-as-html
3692 org-export-as-html
))
3693 (org-autoload "org-docbook"
3694 '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
3695 org-replace-region-by-docbook org-export-region-as-docbook
3696 org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
3697 org-export-as-docbook
))
3698 (org-autoload "org-icalendar"
3699 '(org-export-icalendar-this-file
3700 org-export-icalendar-all-agenda-files
3701 org-export-icalendar-combine-agenda-files
))
3702 (org-autoload "org-xoxo" '(org-export-as-xoxo))
3703 (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning
)))
3705 ;; Declare and autoload functions from org-agenda.el
3708 (org-autoload "org-agenda"
3709 '(org-agenda org-agenda-list org-search-view
3710 org-todo-list org-tags-view org-agenda-list-stuck-projects
3711 org-diary org-agenda-to-appt
3712 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
)))
3714 ;; Autoload org-remember
3717 (org-autoload "org-remember"
3718 '(org-remember-insinuate org-remember-annotation
3719 org-remember-apply-template org-remember org-remember-handler
)))
3722 (org-autoload "org-capture"
3723 '(org-capture org-capture-insert-template-here
3724 org-capture-import-remember-templates
)))
3726 ;; Autoload org-clock.el
3729 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
3731 (declare-function org-clock-update-mode-line
"org-clock" ())
3732 (declare-function org-resolve-clocks
"org-clock"
3733 (&optional also-non-dangling-p prompt last-valid
))
3734 (defvar org-clock-start-time
)
3735 (defvar org-clock-marker
(make-marker)
3736 "Marker recording the last clock-in.")
3737 (defvar org-clock-hd-marker
(make-marker)
3738 "Marker recording the last clock-in, but the headline position.")
3739 (defvar org-clock-heading
""
3740 "The heading of the current clock entry.")
3741 (defun org-clock-is-active ()
3742 "Return non-nil if clock is currently running.
3743 The return value is actually the clock marker."
3744 (marker-buffer org-clock-marker
))
3749 '(org-clock-in org-clock-out org-clock-cancel
3750 org-clock-goto org-clock-sum org-clock-display
3751 org-clock-remove-overlays org-clock-report
3752 org-clocktable-shift org-dblock-write
:clocktable
3753 org-get-clocktable org-resolve-clocks
)))
3755 (defun org-clock-update-time-maybe ()
3756 "If this is a CLOCK line, update it and return t.
3757 Otherwise, return nil."
3760 (beginning-of-line 1)
3761 (skip-chars-forward " \t")
3762 (when (looking-at org-clock-string
)
3763 (let ((re (concat "[ \t]*" org-clock-string
3764 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
3765 "\\([ \t]*=>.*\\)?\\)?"))
3768 ((not (looking-at re
))
3770 ((not (match-end 2))
3771 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
3772 (> org-clock-marker
(point))
3773 (<= org-clock-marker
(point-at-eol)))
3774 ;; The clock is running here
3775 (setq org-clock-start-time
3777 (org-parse-time-string (match-string 1))))
3778 (org-clock-update-mode-line)))
3780 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
3782 (setq ts
(match-string 1)
3783 te
(match-string 3))
3784 (setq s
(- (org-float-time
3785 (apply 'encode-time
(org-parse-time-string te
)))
3787 (apply 'encode-time
(org-parse-time-string ts
))))
3790 h
(floor (/ s
3600))
3794 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
3797 (defun org-check-running-clock ()
3798 "Check if the current buffer contains the running clock.
3799 If yes, offer to stop it and to save the buffer with the changes."
3800 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
3801 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
3804 (when (y-or-n-p "Save changed buffer?")
3807 (defun org-clocktable-try-shift (dir n
)
3808 "Check if this line starts a clock table, if yes, shift the time block."
3809 (when (org-match-line "#\\+BEGIN: clocktable\\>")
3810 (org-clocktable-shift dir n
)))
3812 ;; Autoload org-timer.el
3817 '(org-timer-start org-timer org-timer-item
3818 org-timer-change-times-in-region
3820 org-timer-reset-timers
3821 org-timer-show-remaining-time
)))
3823 ;; Autoload org-feed.el
3828 '(org-feed-update org-feed-update-all org-feed-goto-inbox
)))
3831 ;; Autoload org-indent.el
3833 ;; Define the variable already here, to make sure we have it.
3834 (defvar org-indent-mode nil
3835 "Non-nil if Org-Indent mode is enabled.
3836 Use the command `org-indent-mode' to change this variable.")
3841 '(org-indent-mode)))
3843 ;; Autoload org-mobile.el
3848 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda
)))
3850 ;; Autoload archiving code
3851 ;; The stuff that is needed for cycling and tags has to be defined here.
3853 (defgroup org-archive nil
3854 "Options concerning archiving in Org-mode."
3856 :group
'org-structure
)
3858 (defcustom org-archive-location
"%s_archive::"
3859 "The location where subtrees should be archived.
3861 The value of this variable is a string, consisting of two parts,
3862 separated by a double-colon. The first part is a filename and
3863 the second part is a headline.
3865 When the filename is omitted, archiving happens in the same file.
3866 %s in the filename will be replaced by the current file
3867 name (without the directory part). Archiving to a different file
3868 is useful to keep archived entries from contributing to the
3871 The archived entries will be filed as subtrees of the specified
3872 headline. When the headline is omitted, the subtrees are simply
3873 filed away at the end of the file, as top-level entries. Also in
3874 the heading you can use %s to represent the file name, this can be
3875 useful when using the same archive for a number of different files.
3877 Here are a few examples:
3879 If the current file is Projects.org, archive in file
3880 Projects.org_archive, as top-level trees. This is the default.
3882 \"::* Archived Tasks\"
3883 Archive in the current file, under the top-level headline
3884 \"* Archived Tasks\".
3886 \"~/org/archive.org::\"
3887 Archive in file ~/org/archive.org (absolute path), as top-level trees.
3889 \"~/org/archive.org::From %s\"
3890 Archive in file ~/org/archive.org (absolute path), under headlines
3891 \"From FILENAME\" where file name is the current file name.
3893 \"basement::** Finished Tasks\"
3894 Archive in file ./basement (relative path), as level 3 trees
3895 below the level 2 heading \"** Finished Tasks\".
3897 You may set this option on a per-file basis by adding to the buffer a
3900 #+ARCHIVE: basement::** Finished Tasks
3902 You may also define it locally for a subtree by setting an ARCHIVE property
3903 in the entry. If such a property is found in an entry, or anywhere up
3904 the hierarchy, it will be used."
3908 (defcustom org-archive-tag
"ARCHIVE"
3909 "The tag that marks a subtree as archived.
3910 An archived subtree does not open during visibility cycling, and does
3911 not contribute to the agenda listings.
3912 After changing this, font-lock must be restarted in the relevant buffers to
3913 get the proper fontification."
3915 :group
'org-keywords
3918 (defcustom org-agenda-skip-archived-trees t
3919 "Non-nil means the agenda will skip any items located in archived trees.
3920 An archived tree is a tree marked with the tag ARCHIVE. The use of this
3921 variable is no longer recommended, you should leave it at the value t.
3922 Instead, use the key `v' to cycle the archives-mode in the agenda."
3924 :group
'org-agenda-skip
3927 (defcustom org-columns-skip-archived-trees t
3928 "Non-nil means ignore archived trees when creating column view."
3930 :group
'org-properties
3933 (defcustom org-cycle-open-archived-trees nil
3934 "Non-nil means `org-cycle' will open archived trees.
3935 An archived tree is a tree marked with the tag ARCHIVE.
3936 When nil, archived trees will stay folded. You can still open them with
3937 normal outline commands like `show-all', but not with the cycling commands."
3942 (defcustom org-sparse-tree-open-archived-trees nil
3943 "Non-nil means sparse tree construction shows matches in archived trees.
3944 When nil, matches in these trees are highlighted, but the trees are kept in
3947 :group
'org-sparse-trees
3950 (defun org-cycle-hide-archived-subtrees (state)
3951 "Re-hide all archived subtrees after a visibility state change."
3952 (when (and (not org-cycle-open-archived-trees
)
3953 (not (memq state
'(overview folded
))))
3955 (let* ((globalp (memq state
'(contents all
)))
3956 (beg (if globalp
(point-min) (point)))
3957 (end (if globalp
(point-max) (org-end-of-subtree t
))))
3958 (org-hide-archived-subtrees beg end
)
3960 (if (looking-at (concat ".*:" org-archive-tag
":"))
3961 (message "%s" (substitute-command-keys
3962 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
3964 (defun org-force-cycle-archived ()
3965 "Cycle subtree even if it is archived."
3967 (setq this-command
'org-cycle
)
3968 (let ((org-cycle-open-archived-trees t
))
3969 (call-interactively 'org-cycle
)))
3971 (defun org-hide-archived-subtrees (beg end
)
3972 "Re-hide all archived subtrees after a visibility state change."
3974 (let* ((re (concat ":" org-archive-tag
":")))
3976 (while (re-search-forward re end t
)
3977 (when (org-on-heading-p)
3978 (org-flag-subtree t
)
3979 (org-end-of-subtree t
))))))
3981 (defun org-flag-subtree (flag)
3983 (org-back-to-heading t
)
3984 (outline-end-of-heading)
3985 (outline-flag-region (point)
3986 (progn (org-end-of-subtree t
) (point))
3989 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
3992 (org-autoload "org-archive"
3993 '(org-add-archive-files org-archive-subtree
3994 org-archive-to-archive-sibling org-toggle-archive-tag
3995 org-archive-subtree-default
3996 org-archive-subtree-default-with-confirmation
)))
3998 ;; Autoload Column View Code
4000 (declare-function org-columns-number-to-string
"org-colview")
4001 (declare-function org-columns-get-format-and-top-level
"org-colview")
4002 (declare-function org-columns-compute
"org-colview")
4004 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4005 '(org-columns-number-to-string org-columns-get-format-and-top-level
4006 org-columns-compute org-agenda-columns org-columns-remove-overlays
4007 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
4011 (declare-function org-id-store-link
"org-id")
4012 (declare-function org-id-locations-load
"org-id")
4013 (declare-function org-id-locations-save
"org-id")
4014 (defvar org-id-track-globally
)
4015 (org-autoload "org-id"
4016 '(org-id-get-create org-id-new org-id-copy org-id-get
4017 org-id-get-with-outline-path-completion
4018 org-id-get-with-outline-drilling org-id-store-link
4019 org-id-goto org-id-find org-id-store-link
))
4021 ;; Autoload Plotting Code
4023 (org-autoload "org-plot"
4024 '(org-plot/gnuplot
))
4026 ;;; Variables for pre-computed regular expressions, all buffer local
4028 (defvar org-drawer-regexp nil
4029 "Matches first line of a hidden block.")
4030 (make-variable-buffer-local 'org-drawer-regexp
)
4031 (defvar org-todo-regexp nil
4032 "Matches any of the TODO state keywords.")
4033 (make-variable-buffer-local 'org-todo-regexp
)
4034 (defvar org-not-done-regexp nil
4035 "Matches any of the TODO state keywords except the last one.")
4036 (make-variable-buffer-local 'org-not-done-regexp
)
4037 (defvar org-not-done-heading-regexp nil
4038 "Matches a TODO headline that is not done.")
4039 (make-variable-buffer-local 'org-not-done-regexp
)
4040 (defvar org-todo-line-regexp nil
4041 "Matches a headline and puts TODO state into group 2 if present.")
4042 (make-variable-buffer-local 'org-todo-line-regexp
)
4043 (defvar org-complex-heading-regexp nil
4044 "Matches a headline and puts everything into groups:
4046 group 2: The todo keyword, maybe
4047 group 3: Priority cookie
4048 group 4: True headline
4050 (make-variable-buffer-local 'org-complex-heading-regexp
)
4051 (defvar org-complex-heading-regexp-format nil
4052 "Printf format to make regexp to match an exact headline.
4053 This regexp will match the headline of any node which hase the exact
4054 headline text that is put into the format, but may have any TODO state,
4055 priority and tags.")
4056 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4057 (defvar org-todo-line-tags-regexp nil
4058 "Matches a headline and puts TODO state into group 2 if present.
4059 Also put tags into group 4 if tags are present.")
4060 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4061 (defvar org-nl-done-regexp nil
4062 "Matches newline followed by a headline with the DONE keyword.")
4063 (make-variable-buffer-local 'org-nl-done-regexp
)
4064 (defvar org-looking-at-done-regexp nil
4065 "Matches the DONE keyword a point.")
4066 (make-variable-buffer-local 'org-looking-at-done-regexp
)
4067 (defvar org-ds-keyword-length
12
4068 "Maximum length of the Deadline and SCHEDULED keywords.")
4069 (make-variable-buffer-local 'org-ds-keyword-length
)
4070 (defvar org-deadline-regexp nil
4071 "Matches the DEADLINE keyword.")
4072 (make-variable-buffer-local 'org-deadline-regexp
)
4073 (defvar org-deadline-time-regexp nil
4074 "Matches the DEADLINE keyword together with a time stamp.")
4075 (make-variable-buffer-local 'org-deadline-time-regexp
)
4076 (defvar org-deadline-line-regexp nil
4077 "Matches the DEADLINE keyword and the rest of the line.")
4078 (make-variable-buffer-local 'org-deadline-line-regexp
)
4079 (defvar org-scheduled-regexp nil
4080 "Matches the SCHEDULED keyword.")
4081 (make-variable-buffer-local 'org-scheduled-regexp
)
4082 (defvar org-scheduled-time-regexp nil
4083 "Matches the SCHEDULED keyword together with a time stamp.")
4084 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4085 (defvar org-closed-time-regexp nil
4086 "Matches the CLOSED keyword together with a time stamp.")
4087 (make-variable-buffer-local 'org-closed-time-regexp
)
4089 (defvar org-keyword-time-regexp nil
4090 "Matches any of the 4 keywords, together with the time stamp.")
4091 (make-variable-buffer-local 'org-keyword-time-regexp
)
4092 (defvar org-keyword-time-not-clock-regexp nil
4093 "Matches any of the 3 keywords, together with the time stamp.")
4094 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4095 (defvar org-maybe-keyword-time-regexp nil
4096 "Matches a timestamp, possibly preceded by a keyword.")
4097 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4098 (defvar org-planning-or-clock-line-re nil
4099 "Matches a line with planning or clock info.")
4100 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
4101 (defvar org-all-time-keywords nil
4102 "List of time keywords.")
4103 (make-variable-buffer-local 'org-all-time-keywords
)
4105 (defconst org-plain-time-of-day-regexp
4108 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4111 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4113 "Regular expression to match a plain time or time range.
4114 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4115 groups carry important information:
4117 1 the first time, range or not
4118 8 the second time, if it is a range.")
4120 (defconst org-plain-time-extension-regexp
4123 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4124 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4125 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4126 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4127 groups carry important information:
4130 9 minutes of duration")
4132 (defconst org-stamp-time-of-day-regexp
4134 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4135 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4137 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4138 "Regular expression to match a timestamp time or time range.
4139 After a match, the following groups carry important information:
4141 1 date plus weekday, for back referencing to make sure both times are on the same day
4142 2 the first time, range or not
4143 4 the second time, if it is a range.")
4145 (defconst org-startup-options
4146 '(("fold" org-startup-folded t
)
4147 ("overview" org-startup-folded t
)
4148 ("nofold" org-startup-folded nil
)
4149 ("showall" org-startup-folded nil
)
4150 ("showeverything" org-startup-folded showeverything
)
4151 ("content" org-startup-folded content
)
4152 ("indent" org-startup-indented t
)
4153 ("noindent" org-startup-indented nil
)
4154 ("hidestars" org-hide-leading-stars t
)
4155 ("showstars" org-hide-leading-stars nil
)
4156 ("odd" org-odd-levels-only t
)
4157 ("oddeven" org-odd-levels-only nil
)
4158 ("align" org-startup-align-all-tables t
)
4159 ("noalign" org-startup-align-all-tables nil
)
4160 ("inlineimages" org-startup-with-inline-images t
)
4161 ("noinlineimages" org-startup-with-inline-images nil
)
4162 ("customtime" org-display-custom-times t
)
4163 ("logdone" org-log-done time
)
4164 ("lognotedone" org-log-done note
)
4165 ("nologdone" org-log-done nil
)
4166 ("lognoteclock-out" org-log-note-clock-out t
)
4167 ("nolognoteclock-out" org-log-note-clock-out nil
)
4168 ("logrepeat" org-log-repeat state
)
4169 ("lognoterepeat" org-log-repeat note
)
4170 ("nologrepeat" org-log-repeat nil
)
4171 ("logreschedule" org-log-reschedule time
)
4172 ("lognotereschedule" org-log-reschedule note
)
4173 ("nologreschedule" org-log-reschedule nil
)
4174 ("logredeadline" org-log-redeadline time
)
4175 ("lognoteredeadline" org-log-redeadline note
)
4176 ("nologredeadline" org-log-redeadline nil
)
4177 ("logrefile" org-log-refile time
)
4178 ("lognoterefile" org-log-refile note
)
4179 ("nologrefile" org-log-refile nil
)
4180 ("fninline" org-footnote-define-inline t
)
4181 ("nofninline" org-footnote-define-inline nil
)
4182 ("fnlocal" org-footnote-section nil
)
4183 ("fnauto" org-footnote-auto-label t
)
4184 ("fnprompt" org-footnote-auto-label nil
)
4185 ("fnconfirm" org-footnote-auto-label confirm
)
4186 ("fnplain" org-footnote-auto-label plain
)
4187 ("fnadjust" org-footnote-auto-adjust t
)
4188 ("nofnadjust" org-footnote-auto-adjust nil
)
4189 ("constcgs" constants-unit-system cgs
)
4190 ("constSI" constants-unit-system SI
)
4191 ("noptag" org-tag-persistent-alist nil
)
4192 ("hideblocks" org-hide-block-startup t
)
4193 ("nohideblocks" org-hide-block-startup nil
)
4194 ("beamer" org-startup-with-beamer-mode t
)
4195 ("entitiespretty" org-pretty-entities t
)
4196 ("entitiesplain" org-pretty-entities nil
))
4197 "Variable associated with STARTUP options for org-mode.
4198 Each element is a list of three items: The startup options as written
4199 in the #+STARTUP line, the corresponding variable, and the value to
4200 set this variable to if the option is found. An optional forth element PUSH
4201 means to push this value onto the list in the variable.")
4203 (defun org-set-regexps-and-options ()
4204 "Precompute regular expressions for current buffer."
4206 (org-set-local 'org-todo-kwd-alist nil
)
4207 (org-set-local 'org-todo-key-alist nil
)
4208 (org-set-local 'org-todo-key-trigger nil
)
4209 (org-set-local 'org-todo-keywords-1 nil
)
4210 (org-set-local 'org-done-keywords nil
)
4211 (org-set-local 'org-todo-heads nil
)
4212 (org-set-local 'org-todo-sets nil
)
4213 (org-set-local 'org-todo-log-states nil
)
4214 (org-set-local 'org-file-properties nil
)
4215 (org-set-local 'org-file-tags nil
)
4216 (let ((re (org-make-options-regexp
4217 '("CATEGORY" "TODO" "COLUMNS"
4218 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
4219 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
4221 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4223 (scripts org-use-sub-superscripts
)
4224 kwds kws0 kwsa key log value cat arch tags const links hw dws
4225 tail sep kws1 prio props ftags drawers beamer-p
4226 ext-setup-or-nil setup-contents
(start 0))
4230 (goto-char (point-min))
4231 (while (or (and ext-setup-or-nil
4232 (string-match re ext-setup-or-nil start
)
4233 (setq start
(match-end 0)))
4234 (and (setq ext-setup-or-nil nil start
0)
4235 (re-search-forward re nil t
)))
4236 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4237 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4238 (if (stringp value
) (setq value
(org-trim value
)))
4240 ((equal key
"CATEGORY")
4242 ((member key
'("SEQ_TODO" "TODO"))
4243 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4244 ((equal key
"TYP_TODO")
4245 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4246 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4247 ;; general TODO-like setup
4248 (push (cons (intern (downcase (match-string 1 key
)))
4249 (org-split-string value splitre
)) kwds
))
4251 (setq tags
(append tags
(if tags
'("\\n") nil
)
4252 (org-split-string value splitre
))))
4253 ((equal key
"COLUMNS")
4254 (org-set-local 'org-columns-default-format value
))
4256 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4257 (push (cons (match-string 1 value
)
4258 (org-trim (match-string 2 value
)))
4260 ((equal key
"PRIORITIES")
4261 (setq prio
(org-split-string value
" +")))
4262 ((equal key
"PROPERTY")
4263 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4264 (push (cons (match-string 1 value
) (match-string 2 value
))
4266 ((equal key
"FILETAGS")
4267 (when (string-match "\\S-" value
)
4272 (mapcar (lambda (x) (org-split-string x
":"))
4273 (org-split-string value
)))))))
4274 ((equal key
"DRAWERS")
4275 (setq drawers
(org-split-string value splitre
)))
4276 ((equal key
"CONSTANTS")
4277 (setq const
(append const
(org-split-string value splitre
))))
4278 ((equal key
"STARTUP")
4279 (let ((opts (org-split-string value splitre
))
4281 (while (setq l
(pop opts
))
4282 (when (setq l
(assoc l org-startup-options
))
4283 (setq var
(nth 1 l
) val
(nth 2 l
))
4285 (set (make-local-variable var
) val
)
4286 (if (not (listp (symbol-value var
)))
4287 (set (make-local-variable var
) nil
))
4288 (set (make-local-variable var
) (symbol-value var
))
4289 (add-to-list var val
))))))
4290 ((equal key
"ARCHIVE")
4292 (remove-text-properties 0 (length arch
)
4293 '(face t fontified t
) arch
))
4294 ((equal key
"LATEX_CLASS")
4295 (setq beamer-p
(equal value
"beamer")))
4296 ((equal key
"OPTIONS")
4297 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4298 (setq scripts
(read (match-string 2 value
)))))
4299 ((equal key
"SETUPFILE")
4300 (setq setup-contents
(org-file-contents
4302 (org-remove-double-quotes value
))
4304 (if (not ext-setup-or-nil
)
4305 (setq ext-setup-or-nil setup-contents start
0)
4306 (setq ext-setup-or-nil
4307 (concat (substring ext-setup-or-nil
0 start
)
4308 "\n" setup-contents
"\n"
4309 (substring ext-setup-or-nil start
)))))
4311 (org-set-local 'org-use-sub-superscripts scripts
)
4313 (org-set-local 'org-category
(intern cat
))
4314 (push (cons "CATEGORY" cat
) props
))
4316 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4317 (setq prio
(mapcar 'string-to-char prio
))
4318 (org-set-local 'org-highest-priority
(nth 0 prio
))
4319 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4320 (org-set-local 'org-default-priority
(nth 2 prio
)))
4321 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4322 (and ftags
(org-set-local 'org-file-tags
4323 (mapcar 'org-add-prop-inherited ftags
)))
4324 (and drawers
(org-set-local 'org-drawers drawers
))
4325 (and arch
(org-set-local 'org-archive-location arch
))
4326 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4327 ;; Process the TODO keywords
4329 ;; Use the global values as if they had been given locally.
4330 (setq kwds
(default-value 'org-todo-keywords
))
4331 (if (stringp (car kwds
))
4332 (setq kwds
(list (cons org-todo-interpretation
4333 (default-value 'org-todo-keywords
)))))
4334 (setq kwds
(reverse kwds
)))
4335 (setq kwds
(nreverse kwds
))
4337 (while (setq kws
(pop kwds
))
4339 (run-hook-with-args-until-success
4340 'org-todo-setup-filter-hook kws
)
4342 (setq inter
(pop kws
) sep
(member "|" kws
)
4343 kws0
(delete "|" (copy-sequence kws
))
4348 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4350 (setq kw
(match-string 1 x
)
4351 key
(and (match-end 2) (match-string 2 x
))
4352 log
(org-extract-log-state-settings x
))
4353 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4354 (and log
(push log org-todo-log-states
))
4356 (error "Invalid TODO keyword %s" x
)))
4358 kwsa
(if kwsa
(append '((:startgroup
))
4362 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4363 tail
(list inter hw
(car dws
) (org-last dws
))))
4364 (add-to-list 'org-todo-heads hw
'append
)
4365 (push kws1 org-todo-sets
)
4366 (setq org-done-keywords
(append org-done-keywords dws nil
))
4367 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4368 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4369 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4370 (setq org-todo-sets
(nreverse org-todo-sets
)
4371 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4372 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4373 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4374 ;; Process the constants
4377 (while (setq e
(pop const
))
4378 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4379 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4380 (setq org-table-formula-constants-local cst
)))
4382 ;; Process the tags.
4385 (while (setq e
(pop tags
))
4387 ((equal e
"{") (push '(:startgroup
) tgs
))
4388 ((equal e
"}") (push '(:endgroup
) tgs
))
4389 ((equal e
"\\n") (push '(:newline
) tgs
))
4390 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4391 (push (cons (match-string 1 e
)
4392 (string-to-char (match-string 2 e
)))
4394 (t (push (list e
) tgs
))))
4395 (org-set-local 'org-tag-alist nil
)
4396 (while (setq e
(pop tgs
))
4397 (or (and (stringp (car e
))
4398 (assoc (car e
) org-tag-alist
))
4399 (push e org-tag-alist
)))))
4401 ;; Compute the regular expressions and other local variables
4402 (if (not org-done-keywords
)
4403 (setq org-done-keywords
(and org-todo-keywords-1
4404 (list (org-last org-todo-keywords-1
)))))
4405 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4406 (length org-scheduled-string
)
4407 (length org-clock-string
)
4408 (length org-closed-string
)))
4410 (concat "^[ \t]*:\\("
4411 (mapconcat 'regexp-quote org-drawers
"\\|")
4413 org-not-done-keywords
4414 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4416 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
4420 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4422 org-not-done-heading-regexp
4423 (concat "^\\(\\*+\\)[ \t]+\\("
4424 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4426 org-todo-line-regexp
4427 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4428 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4429 "\\)\\>\\)?[ \t]*\\(.*\\)")
4430 org-complex-heading-regexp
4431 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4432 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4433 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
4434 "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
4435 org-complex-heading-regexp-format
4436 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4437 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4439 "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
4440 "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
4442 "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
4443 "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?[ \t]*$")
4445 (concat "\n\\*+[ \t]+"
4446 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
4448 org-todo-line-tags-regexp
4449 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4450 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4452 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:[ \t]*\\)?$\\)"))
4453 org-looking-at-done-regexp
4455 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
4457 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4458 org-deadline-time-regexp
4459 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4460 org-deadline-line-regexp
4461 (concat "\\<\\(" org-deadline-string
"\\).*")
4462 org-scheduled-regexp
4463 (concat "\\<" org-scheduled-string
)
4464 org-scheduled-time-regexp
4465 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4466 org-closed-time-regexp
4467 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4468 org-keyword-time-regexp
4469 (concat "\\<\\(" org-scheduled-string
4470 "\\|" org-deadline-string
4471 "\\|" org-closed-string
4472 "\\|" org-clock-string
"\\)"
4473 " *[[<]\\([^]>]+\\)[]>]")
4474 org-keyword-time-not-clock-regexp
4475 (concat "\\<\\(" org-scheduled-string
4476 "\\|" org-deadline-string
4477 "\\|" org-closed-string
4479 " *[[<]\\([^]>]+\\)[]>]")
4480 org-maybe-keyword-time-regexp
4481 (concat "\\(\\<\\(" org-scheduled-string
4482 "\\|" org-deadline-string
4483 "\\|" org-closed-string
4484 "\\|" org-clock-string
"\\)\\)?"
4485 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4486 org-planning-or-clock-line-re
4487 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
4488 "\\|" org-deadline-string
4489 "\\|" org-closed-string
"\\|" org-clock-string
4491 org-all-time-keywords
4492 (mapcar (lambda (w) (substring w
0 -
1))
4493 (list org-scheduled-string org-deadline-string
4494 org-clock-string org-closed-string
))
4496 (org-compute-latex-and-specials-regexp)
4497 (org-set-font-lock-defaults))))
4499 (defun org-file-contents (file &optional noerror
)
4500 "Return the contents of FILE, as a string."
4502 (not (file-readable-p file
)))
4505 (message "Cannot read file \"%s\"" file
)
4508 (error "Cannot read file \"%s\"" file
))
4510 (insert-file-contents file
)
4513 (defun org-extract-log-state-settings (x)
4514 "Extract the log state setting from a TODO keyword string.
4515 This will extract info from a string like \"WAIT(w@/!)\"."
4516 (let (kw key log1 log2
)
4517 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4518 (setq kw
(match-string 1 x
)
4519 key
(and (match-end 2) (match-string 2 x
))
4520 log1
(and (match-end 3) (match-string 3 x
))
4521 log2
(and (match-end 4) (match-string 4 x
)))
4524 (and log1
(if (equal log1
"!") 'time
'note
))
4525 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4527 (defun org-remove-keyword-keys (list)
4528 "Remove a pair of parenthesis at the end of each string in LIST."
4530 (if (string-match "(.*)$" x
)
4531 (substring x
0 (match-beginning 0))
4535 (defun org-assign-fast-keys (alist)
4536 "Assign fast keys to a keyword-key alist.
4537 Respect keys that are already there."
4538 (let (new e
(alt ?
0))
4539 (while (setq e
(pop alist
))
4540 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
4541 (cdr e
)) ;; Key already assigned.
4543 (let ((clist (string-to-list (downcase (car e
))))
4544 (used (append new alist
)))
4545 (when (= (car clist
) ?
@)
4547 (while (and clist
(rassoc (car clist
) used
))
4550 (while (rassoc alt used
)
4552 (push (cons (car e
) (or (car clist
) alt
)) new
))))
4555 ;;; Some variables used in various places
4557 (defvar org-window-configuration nil
4558 "Used in various places to store a window configuration.")
4559 (defvar org-selected-window nil
4560 "Used in various places to store a window configuration.")
4561 (defvar org-finish-function nil
4562 "Function to be called when `C-c C-c' is used.
4563 This is for getting out of special buffers like remember.")
4566 ;; FIXME: Occasionally check by commenting these, to make sure
4567 ;; no other functions uses these, forgetting to let-bind them.
4572 ;; Defined somewhere in this file, but used before definition.
4573 (defvar org-entities
) ;; defined in org-entities.el
4574 (defvar org-struct-menu
)
4575 (defvar org-org-menu
)
4576 (defvar org-tbl-menu
)
4578 ;;;; Define the Org-mode
4580 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4581 (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"))
4584 ;; We use a before-change function to check if a table might need
4586 (defvar org-table-may-need-update t
4587 "Indicates that a table might need an update.
4588 This variable is set by `org-before-change-function'.
4589 `org-table-align' sets it back to nil.")
4590 (defun org-before-change-function (beg end
)
4591 "Every change indicates that a table might need an update."
4592 (setq org-table-may-need-update t
))
4593 (defvar org-mode-map
)
4594 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4595 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
4596 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4597 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
4598 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
4599 (defvar org-table-buffer-is-an nil
)
4600 (defconst org-outline-regexp
"\\*+ ")
4603 (define-derived-mode org-mode outline-mode
"Org"
4604 "Outline-based notes management and organizer, alias
4605 \"Carsten's outline-mode for keeping track of everything.\"
4607 Org-mode develops organizational tasks around a NOTES file which
4608 contains information about projects as plain text. Org-mode is
4609 implemented on top of outline-mode, which is ideal to keep the content
4610 of large files well structured. It supports ToDo items, deadlines and
4611 time stamps, which magically appear in the diary listing of the Emacs
4612 calendar. Tables are easily created with a built-in table editor.
4613 Plain text URL-like links connect to websites, emails (VM), Usenet
4614 messages (Gnus), BBDB entries, and any files related to the project.
4615 For printing and sharing of notes, an Org-mode file (or a part of it)
4616 can be exported as a structured ASCII or HTML file.
4618 The following commands are available:
4622 ;; Get rid of Outline menus, they are not needed
4623 ;; Need to do this here because define-derived-mode sets up
4624 ;; the keymap so late. Still, it is a waste to call this each time
4625 ;; we switch another buffer into org-mode.
4626 (if (featurep 'xemacs
)
4627 (when (boundp 'outline-mode-menu-heading
)
4628 ;; Assume this is Greg's port, it uses easymenu
4629 (easy-menu-remove outline-mode-menu-heading
)
4630 (easy-menu-remove outline-mode-menu-show
)
4631 (easy-menu-remove outline-mode-menu-hide
))
4632 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
4633 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
4634 (define-key org-mode-map
[menu-bar show
] 'undefined
))
4636 (org-load-modules-maybe)
4637 (easy-menu-add org-org-menu
)
4638 (easy-menu-add org-tbl-menu
)
4639 (org-install-agenda-files-menu)
4640 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
4641 (add-to-invisibility-spec '(org-cwidth))
4642 (add-to-invisibility-spec '(org-hide-block . t
))
4643 (when (featurep 'xemacs
)
4644 (org-set-local 'line-move-ignore-invisible t
))
4645 (org-set-local 'outline-regexp org-outline-regexp
)
4646 (org-set-local 'outline-level
'org-outline-level
)
4647 (when (and org-ellipsis
4648 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
4649 (fboundp 'make-glyph-code
))
4650 (unless org-display-table
4651 (setq org-display-table
(make-display-table)))
4652 (set-display-table-slot
4655 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
4657 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
4658 (setq buffer-display-table org-display-table
))
4659 (org-set-regexps-and-options)
4660 (when (and org-tag-faces
(not org-tags-special-faces-re
))
4661 ;; tag faces set outside customize.... force initialization.
4662 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
4664 (org-set-local 'calc-embedded-open-mode
"# ")
4665 (modify-syntax-entry ?
@ "w")
4666 (if org-startup-truncated
(setq truncate-lines t
))
4667 (org-set-local 'font-lock-unfontify-region-function
4668 'org-unfontify-region
)
4669 ;; Activate before-change-function
4670 (org-set-local 'org-table-may-need-update t
)
4671 (org-add-hook 'before-change-functions
'org-before-change-function nil
4673 ;; Check for running clock before killing a buffer
4674 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
4675 ;; Paragraphs and auto-filling
4676 (org-set-autofill-regexps)
4677 (setq indent-line-function
'org-indent-line-function
)
4678 (org-update-radio-target-regexp)
4679 ;; Beginning/end of defun
4680 (org-set-local 'beginning-of-defun-function
'org-beginning-of-defun
)
4681 (org-set-local 'end-of-defun-function
'org-end-of-defun
)
4682 ;; Make sure dependence stuff works reliably, even for users who set it
4684 (if org-enforce-todo-dependencies
4685 (add-hook 'org-blocker-hook
4686 'org-block-todo-from-children-or-siblings-or-parent
)
4687 (remove-hook 'org-blocker-hook
4688 'org-block-todo-from-children-or-siblings-or-parent
))
4689 (if org-enforce-todo-checkbox-dependencies
4690 (add-hook 'org-blocker-hook
4691 'org-block-todo-from-checkboxes
)
4692 (remove-hook 'org-blocker-hook
4693 'org-block-todo-from-checkboxes
))
4695 ;; Comment characters
4696 (org-set-local 'comment-start
"#")
4697 (org-set-local 'comment-padding
" ")
4699 ;; Align options lines
4701 'align-mode-rules-list
4702 '((org-in-buffer-settings
4703 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4704 (modes .
'(org-mode)))))
4707 (org-set-local 'imenu-create-index-function
4708 'org-imenu-get-tree
)
4710 ;; Make isearch reveal context
4711 (if (or (featurep 'xemacs
)
4712 (not (boundp 'outline-isearch-open-invisible-function
)))
4713 ;; Emacs 21 and XEmacs make use of the hook
4714 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
4715 ;; Emacs 22 deals with this through a special variable
4716 (org-set-local 'outline-isearch-open-invisible-function
4717 (lambda (&rest ignore
) (org-show-context 'isearch
))))
4719 ;; Turn on org-beamer-mode?
4720 (and org-startup-with-beamer-mode
(org-beamer-mode 1))
4722 ;; If empty file that did not turn on org-mode automatically, make it to.
4723 (if (and org-insert-mode-line-in-empty-file
4725 (= (point-min) (point-max)))
4726 (insert "# -*- mode: org -*-\n\n"))
4727 (unless org-inhibit-startup
4728 (when org-startup-align-all-tables
4729 (let ((bmp (buffer-modified-p)))
4730 (org-table-map-tables 'org-table-align
'quietly
)
4731 (set-buffer-modified-p bmp
)))
4732 (when org-startup-with-inline-images
4733 (org-display-inline-images))
4734 (when org-startup-indented
4735 (require 'org-indent
)
4736 (org-indent-mode 1))
4737 (unless org-inhibit-startup-visibility-stuff
4738 (org-set-startup-visibility))))
4740 (when (fboundp 'abbrev-table-put
)
4741 (abbrev-table-put org-mode-abbrev-table
4742 :parents
(list text-mode-abbrev-table
)))
4744 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
4746 (defun org-current-time ()
4747 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
4748 (if (> (car org-time-stamp-rounding-minutes
) 1)
4749 (let ((r (car org-time-stamp-rounding-minutes
))
4750 (time (decode-time)))
4752 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
4756 ;;;; Font-Lock stuff, including the activators
4758 (defvar org-mouse-map
(make-sparse-keymap))
4759 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
4760 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
4761 (when org-mouse-1-follows-link
4762 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
4763 (when org-tab-follows-link
4764 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
4765 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
4767 (require 'font-lock
)
4769 (defconst org-non-link-chars
"]\t\n\r<>")
4770 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
4771 "shell" "elisp" "doi" "message"))
4772 (defvar org-link-types-re nil
4773 "Matches a link that has a url-like prefix like \"http:\"")
4774 (defvar org-link-re-with-space nil
4775 "Matches a link with spaces, optional angular brackets around it.")
4776 (defvar org-link-re-with-space2 nil
4777 "Matches a link with spaces, optional angular brackets around it.")
4778 (defvar org-link-re-with-space3 nil
4779 "Matches a link with spaces, only for internal part in bracket links.")
4780 (defvar org-angle-link-re nil
4781 "Matches link with angular brackets, spaces are allowed.")
4782 (defvar org-plain-link-re nil
4783 "Matches plain link, without spaces.")
4784 (defvar org-bracket-link-regexp nil
4785 "Matches a link in double brackets.")
4786 (defvar org-bracket-link-analytic-regexp nil
4787 "Regular expression used to analyze links.
4788 Here is what the match groups contain after a match:
4794 (defvar org-bracket-link-analytic-regexp
++ nil
4795 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
4796 (defvar org-any-link-re nil
4797 "Regular expression matching any link.")
4799 (defcustom org-match-sexp-depth
3
4800 "Number of stacked braces for sub/superscript matching.
4801 This has to be set before loading org.el to be effective."
4802 :group
'org-export-translation
; ??????????????????????????/
4805 (defun org-create-multibrace-regexp (left right n
)
4806 "Create a regular expression which will match a balanced sexp.
4807 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
4808 as single character strings.
4809 The regexp returned will match the entire expression including the
4810 delimiters. It will also define a single group which contains the
4811 match except for the outermost delimiters. The maximum depth of
4812 stacked delimiters is N. Escaping delimiters is not possible."
4813 (let* ((nothing (concat "[^" left right
"]*?"))
4816 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
4819 re
(concat re or next
)
4820 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
4821 (concat left
"\\(" re
"\\)" right
)))
4823 (defvar org-match-substring-regexp
4825 "\\([^\\]\\)\\([_^]\\)\\("
4826 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
4828 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
4830 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
4831 "The regular expression matching a sub- or superscript.")
4833 (defvar org-match-substring-with-braces-regexp
4835 "\\([^\\]\\)\\([_^]\\)\\("
4836 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
4838 "The regular expression matching a sub- or superscript, forcing braces.")
4840 (defun org-make-link-regexps ()
4841 "Update the link regular expressions.
4842 This should be called after the variable `org-link-types' has changed."
4843 (setq org-link-types-re
4845 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
4846 org-link-re-with-space
4848 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
4849 "\\([^" org-non-link-chars
" ]"
4850 "[^" org-non-link-chars
"]*"
4851 "[^" org-non-link-chars
" ]\\)>?")
4852 org-link-re-with-space2
4854 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
4855 "\\([^" org-non-link-chars
" ]"
4857 "[^" org-non-link-chars
" ]\\)>?")
4858 org-link-re-with-space3
4860 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
4861 "\\([^" org-non-link-chars
" ]"
4865 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
4866 "\\([^" org-non-link-chars
" ]"
4867 "[^" org-non-link-chars
"]*"
4871 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
4872 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
4873 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
4874 org-bracket-link-regexp
4875 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
4876 org-bracket-link-analytic-regexp
4879 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
4882 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4884 org-bracket-link-analytic-regexp
++
4887 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
4890 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4893 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
4894 org-angle-link-re
"\\)\\|\\("
4895 org-plain-link-re
"\\)")))
4897 (org-make-link-regexps)
4899 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
4900 "Regular expression for fast time stamp matching.")
4901 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
4902 "Regular expression for fast time stamp matching.")
4903 (defconst org-ts-regexp0
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4904 "Regular expression matching time strings for analysis.
4905 This one does not require the space after the date, so it can be used
4906 on a string that terminates immediately after the date.")
4907 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4908 "Regular expression matching time strings for analysis.")
4909 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
4910 "Regular expression matching time stamps, with groups.")
4911 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
4912 "Regular expression matching time stamps (also [..]), with groups.")
4913 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
4914 "Regular expression matching a time stamp range.")
4915 (defconst org-tr-regexp-both
4916 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
4917 "Regular expression matching a time stamp range.")
4918 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
4919 org-ts-regexp
"\\)?")
4920 "Regular expression matching a time stamp or time stamp range.")
4921 (defconst org-tsr-regexp-both
(concat org-ts-regexp-both
"\\(--?-?"
4922 org-ts-regexp-both
"\\)?")
4923 "Regular expression matching a time stamp or time stamp range.
4924 The time stamps may be either active or inactive.")
4926 (defvar org-emph-face nil
)
4928 (defun org-do-emphasis-faces (limit)
4929 "Run through the buffer and add overlays to links."
4931 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
4932 (if (not (= (char-after (match-beginning 3))
4933 (char-after (match-beginning 4))))
4936 (setq a
(assoc (match-string 3) org-emphasis-alist
))
4937 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
4941 (org-remove-flyspell-overlays-in
4942 (match-beginning 0) (match-end 0)))
4943 (add-text-properties (match-beginning 2) (match-end 2)
4944 '(font-lock-multiline t org-emphasis t
))
4945 (when org-hide-emphasis-markers
4946 (add-text-properties (match-end 4) (match-beginning 5)
4947 '(invisible org-link
))
4948 (add-text-properties (match-beginning 3) (match-end 3)
4949 '(invisible org-link
)))))
4953 (defun org-emphasize (&optional char
)
4954 "Insert or change an emphasis, i.e. a font like bold or italic.
4955 If there is an active region, change that region to a new emphasis.
4956 If there is no region, just insert the marker characters and position
4957 the cursor between them.
4958 CHAR should be either the marker character, or the first character of the
4959 HTML tag associated with that emphasis. If CHAR is a space, the means
4960 to remove the emphasis of the selected region.
4961 If char is not given (for example in an interactive call) it
4962 will be prompted for."
4964 (let ((eal org-emphasis-alist
) e det
4965 (erc org-emphasis-regexp-components
)
4967 (string "") beg end move tag c s
)
4968 (if (org-region-active-p)
4969 (setq beg
(region-beginning) end
(region-end)
4970 string
(buffer-substring beg end
))
4973 (while (setq e
(pop eal
))
4974 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
4976 (push (cons c
(string-to-char (car e
))) det
)
4977 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
4978 (substring tag
1)))))
4979 (setq det
(nreverse det
))
4981 (message "%s" (concat "Emphasis marker or tag:" prompt
))
4982 (setq char
(read-char-exclusive)))
4983 (setq char
(or (cdr (assoc char det
)) char
))
4984 (if (equal char ?\
)
4985 (setq s
"" move nil
)
4986 (unless (assoc (char-to-string char
) org-emphasis-alist
)
4987 (error "No such emphasis marker: \"%c\"" char
))
4988 (setq s
(char-to-string char
)))
4989 (while (and (> (length string
) 1)
4990 (equal (substring string
0 1) (substring string -
1))
4991 (assoc (substring string
0 1) org-emphasis-alist
))
4992 (setq string
(substring string
1 -
1)))
4993 (setq string
(concat s string s
))
4994 (if beg
(delete-region beg end
))
4996 (string-match (concat "[" (nth 0 erc
) "\n]")
4997 (char-to-string (char-before (point)))))
5000 (string-match (concat "[" (nth 1 erc
) "\n]")
5001 (char-to-string (char-after (point)))))
5002 (insert " ") (backward-char 1))
5004 (and move
(backward-char 1))))
5006 (defconst org-nonsticky-props
5007 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text
))
5009 (defsubst org-rear-nonsticky-at
(pos)
5010 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5012 (defun org-activate-plain-links (limit)
5013 "Run through the buffer and add overlays to links."
5016 (if (re-search-forward org-plain-link-re limit t
)
5018 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5019 (setq f
(get-text-property (match-beginning 0) 'face
))
5020 (if (or (eq f
'org-tag
)
5021 (and (listp f
) (memq 'org-tag f
)))
5023 (add-text-properties (match-beginning 0) (match-end 0)
5024 (list 'mouse-face
'highlight
5026 'keymap org-mouse-map
))
5027 (org-rear-nonsticky-at (match-end 0)))
5030 (defun org-activate-code (limit)
5031 (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t
)
5033 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5034 (remove-text-properties (match-beginning 0) (match-end 0)
5035 '(display t invisible t intangible t
))
5038 (defcustom org-src-fontify-natively nil
5039 "When non-nil, fontify code in code blocks."
5041 :group
'org-appearance
5044 (defun org-fontify-meta-lines-and-blocks (limit)
5045 "Fontify #+ lines and blocks, in the correct ways."
5046 (let ((case-fold-search t
))
5047 (if (re-search-forward
5048 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5050 (let ((beg (match-beginning 0))
5051 (block-start (match-end 0))
5053 (lang (match-string 7))
5054 (beg1 (line-beginning-position 2))
5055 (dc1 (downcase (match-string 2)))
5056 (dc3 (downcase (match-string 3)))
5057 end end1 quoting block-type
)
5059 ((member dc1
'("html:" "ascii:" "latex:" "docbook:"))
5060 ;; a single line of backend-specific content
5061 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5062 (remove-text-properties (match-beginning 0) (match-end 0)
5063 '(display t invisible t intangible t
))
5064 (add-text-properties (match-beginning 1) (match-end 3)
5065 '(font-lock-fontified t face org-meta-line
))
5066 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5067 '(font-lock-fontified t face org-block
))
5068 ; for backend-specific code
5070 ((and (match-end 4) (equal dc3
"begin"))
5072 (setq block-type
(downcase (match-string 5))
5073 quoting
(member block-type org-protecting-blocks
))
5074 (when (re-search-forward
5075 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5076 nil t
) ;; on purpose, we look further than LIMIT
5077 (setq end
(match-end 0) end1
(1- (match-beginning 0)))
5078 (setq block-end
(match-beginning 0))
5080 (remove-text-properties beg end
5081 '(display t invisible t intangible t
)))
5082 (add-text-properties
5084 '(font-lock-fontified t font-lock-multiline t
))
5085 (add-text-properties beg beg1
'(face org-meta-line
))
5086 (add-text-properties end1
(+ end
1) '(face org-meta-line
))
5089 ((and lang org-src-fontify-natively
)
5090 (org-src-font-lock-fontify-block lang block-start block-end
))
5092 (add-text-properties beg1
(+ end1
1) '(face
5094 ; end of source block
5095 ((not org-fontify-quote-and-verse-blocks
))
5096 ((string= block-type
"quote")
5097 (add-text-properties beg1 end1
'(face org-quote
)))
5098 ((string= block-type
"verse")
5099 (add-text-properties beg1 end1
'(face org-verse
))))
5101 ((member dc1
'("title:" "author:" "email:" "date:"))
5102 (add-text-properties
5104 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5105 '(font-lock-fontified t invisible t
)
5106 '(font-lock-fontified t face org-document-info-keyword
)))
5107 (add-text-properties
5108 (match-beginning 6) (match-end 6)
5109 (if (string-equal dc1
"title:")
5110 '(font-lock-fontified t face org-document-title
)
5111 '(font-lock-fontified t face org-document-info
))))
5112 ((not (member (char-after beg
) '(?\ ?
\t)))
5113 ;; just any other in-buffer setting, but not indented
5114 (add-text-properties
5116 '(font-lock-fontified t face org-meta-line
))
5118 ((or (member dc1
'("begin:" "end:" "caption:" "label:"
5119 "orgtbl:" "tblfm:" "tblname:" "result:"
5120 "results:" "source:" "srcname:" "call:"))
5121 (and (match-end 4) (equal dc3
"attr")))
5122 (add-text-properties
5124 '(font-lock-fontified t face org-meta-line
))
5126 ((member dc3
'(" " ""))
5127 (add-text-properties
5129 '(font-lock-fontified t face font-lock-comment-face
)))
5132 (defun org-activate-angle-links (limit)
5133 "Run through the buffer and add overlays to links."
5134 (if (re-search-forward org-angle-link-re limit t
)
5136 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5137 (add-text-properties (match-beginning 0) (match-end 0)
5138 (list 'mouse-face
'highlight
5139 'keymap org-mouse-map
))
5140 (org-rear-nonsticky-at (match-end 0))
5143 (defun org-activate-footnote-links (limit)
5144 "Run through the buffer and add overlays to links."
5145 (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
5148 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5149 (add-text-properties (match-beginning 2) (match-end 2)
5150 (list 'mouse-face
'highlight
5151 'keymap org-mouse-map
5153 (if (= (point-at-bol) (match-beginning 2))
5154 "Footnote definition"
5155 "Footnote reference")
5157 (org-rear-nonsticky-at (match-end 2))
5160 (defun org-activate-bracket-links (limit)
5161 "Run through the buffer and add overlays to bracketed links."
5162 (if (re-search-forward org-bracket-link-regexp limit t
)
5163 (let* ((help (concat "LINK: "
5164 (org-match-string-no-properties 1)))
5165 ;; FIXME: above we should remove the escapes.
5166 ;; but that requires another match, protecting match data,
5167 ;; a lot of overhead for font-lock.
5168 (ip (org-maybe-intangible
5169 (list 'invisible
'org-link
5170 'keymap org-mouse-map
'mouse-face
'highlight
5171 'font-lock-multiline t
'help-echo help
)))
5172 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5173 'font-lock-multiline t
'help-echo help
)))
5174 ;; We need to remove the invisible property here. Table narrowing
5175 ;; may have made some of this invisible.
5176 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5177 (remove-text-properties (match-beginning 0) (match-end 0)
5181 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5182 (org-rear-nonsticky-at (match-beginning 3))
5183 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5184 (org-rear-nonsticky-at (match-end 3))
5185 (add-text-properties (match-end 3) (match-end 0) ip
)
5186 (org-rear-nonsticky-at (match-end 0)))
5187 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5188 (org-rear-nonsticky-at (match-beginning 1))
5189 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5190 (org-rear-nonsticky-at (match-end 1))
5191 (add-text-properties (match-end 1) (match-end 0) ip
)
5192 (org-rear-nonsticky-at (match-end 0)))
5195 (defun org-activate-dates (limit)
5196 "Run through the buffer and add overlays to dates."
5197 (if (re-search-forward org-tsr-regexp-both limit t
)
5199 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5200 (add-text-properties (match-beginning 0) (match-end 0)
5201 (list 'mouse-face
'highlight
5202 'keymap org-mouse-map
))
5203 (org-rear-nonsticky-at (match-end 0))
5204 (when org-display-custom-times
5206 (org-display-custom-time (match-beginning 3) (match-end 3)))
5207 (org-display-custom-time (match-beginning 1) (match-end 1)))
5210 (defvar org-target-link-regexp nil
5211 "Regular expression matching radio targets in plain text.")
5212 (make-variable-buffer-local 'org-target-link-regexp
)
5213 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5214 "Regular expression matching a link target.")
5215 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5216 "Regular expression matching a radio target.")
5217 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5218 "Regular expression matching any target.")
5220 (defun org-activate-target-links (limit)
5221 "Run through the buffer and add overlays to target matches."
5222 (when org-target-link-regexp
5223 (let ((case-fold-search t
))
5224 (if (re-search-forward org-target-link-regexp limit t
)
5226 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5227 (add-text-properties (match-beginning 0) (match-end 0)
5228 (list 'mouse-face
'highlight
5229 'keymap org-mouse-map
5230 'help-echo
"Radio target link"
5231 'org-linked-text t
))
5232 (org-rear-nonsticky-at (match-end 0))
5235 (defun org-update-radio-target-regexp ()
5236 "Find all radio targets in this file and update the regular expression."
5238 (when (memq 'radio org-activate-links
)
5239 (setq org-target-link-regexp
5240 (org-make-target-link-regexp (org-all-targets 'radio
)))
5241 (org-restart-font-lock)))
5243 (defun org-hide-wide-columns (limit)
5245 (setq s
(text-property-any (point) (or limit
(point-max))
5248 (setq e
(next-single-property-change s
'org-cwidth
))
5249 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5253 (defvar org-latex-and-specials-regexp nil
5254 "Regular expression for highlighting export special stuff.")
5255 (defvar org-match-substring-regexp
)
5256 (defvar org-match-substring-with-braces-regexp
)
5258 ;; This should be with the exporter code, but we also use if for font-locking
5259 (defconst org-export-html-special-string-regexps
5260 '(("\\\\-" .
"­")
5261 ("---\\([^-]\\)" .
"—\\1")
5262 ("--\\([^-]\\)" .
"–\\1")
5263 ("\\.\\.\\." .
"…"))
5264 "Regular expressions for special string conversion.")
5267 (defun org-compute-latex-and-specials-regexp ()
5268 "Compute regular expression for stuff treated specially by exporters."
5269 (if (not org-highlight-latex-fragments-and-specials
)
5270 (org-set-local 'org-latex-and-specials-regexp nil
)
5273 ((matchers (plist-get org-format-latex-options
:matchers
))
5274 (latexs (delq nil
(mapcar (lambda (x) (if (member (car x
) matchers
) x
))
5275 org-latex-regexps
)))
5276 (org-export-allow-BIND nil
)
5277 (options (org-combine-plists (org-default-export-plist)
5278 (org-infile-export-plist)))
5279 (org-export-with-sub-superscripts (plist-get options
:sub-superscript
))
5280 (org-export-with-LaTeX-fragments (plist-get options
:LaTeX-fragments
))
5281 (org-export-with-TeX-macros (plist-get options
:TeX-macros
))
5282 (org-export-html-expand (plist-get options
:expand-quoted-html
))
5283 (org-export-with-special-strings (plist-get options
:special-strings
))
5286 ((equal org-export-with-sub-superscripts
'{})
5287 (list org-match-substring-with-braces-regexp
))
5288 (org-export-with-sub-superscripts
5289 (list org-match-substring-regexp
))
5292 (if org-export-with-LaTeX-fragments
5293 (mapcar (lambda (x) (nth 1 x
)) latexs
)))
5295 (if org-export-with-TeX-macros
5296 (list (concat "\\\\"
5302 (append org-entities-user
5304 (if (boundp 'org-latex-entities
)
5306 (or (car-safe x
) x
))
5311 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5312 (re-special (if org-export-with-special-strings
5313 (mapcar (lambda (x) (car x
))
5314 org-export-html-special-string-regexps
)))
5318 (if org-export-html-expand
"@<[^>\n]+>")
5321 'org-latex-and-specials-regexp
5322 (mapconcat 'identity
(append re-latex re-sub re-macros re-special
5325 (defun org-do-latex-and-special-faces (limit)
5326 "Run through the buffer and add overlays to links."
5327 (when org-latex-and-specials-regexp
5329 (while (and (not rtn
) (re-search-forward org-latex-and-specials-regexp
5331 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5333 '(org-code org-verbatim underline
)))
5336 d
(cond ((member (char-after (1+ (match-beginning 0)))
5339 (font-lock-prepend-text-property
5340 (+ d
(match-beginning 0)) (match-end 0)
5341 'face
'org-latex-and-export-specials
)
5342 (add-text-properties (+ d
(match-beginning 0)) (match-end 0)
5343 '(font-lock-multiline t
)))))
5346 (defun org-restart-font-lock ()
5347 "Restart `font-lock-mode', to force refontification."
5348 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5350 (font-lock-mode 1)))
5352 (defun org-all-targets (&optional radio
)
5353 "Return a list of all targets in this file.
5354 With optional argument RADIO, only find radio targets."
5355 (let ((re (if radio org-radio-target-regexp org-target-regexp
))
5358 (goto-char (point-min))
5359 (while (re-search-forward re nil t
)
5360 (add-to-list 'rtn
(downcase (org-match-string-no-properties 1))))
5363 (defun org-make-target-link-regexp (targets)
5364 "Make regular expression matching all strings in TARGETS.
5365 The regular expression finds the targets also if there is a line break
5372 (while (string-match " +" x
)
5373 (setq x
(replace-match "\\s-+" t t x
)))
5379 (defun org-activate-tags (limit)
5380 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5382 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5383 (add-text-properties (match-beginning 1) (match-end 1)
5384 (list 'mouse-face
'highlight
5385 'keymap org-mouse-map
))
5386 (org-rear-nonsticky-at (match-end 1))
5389 (defun org-outline-level ()
5390 "Compute the outline level of the heading at point.
5391 This function assumes that the cursor is at the beginning of a line matched
5392 by `outline-regexp'. Otherwise it returns garbage.
5393 If this is called at a normal headline, the level is the number of stars.
5394 Use `org-reduced-level' to remove the effect of `org-odd-levels'.
5395 For plain list items, if they are matched by `outline-regexp', this returns
5396 1000 plus the line indentation."
5398 (looking-at outline-regexp
)
5399 (if (match-beginning 1)
5400 (+ (org-get-string-indentation (match-string 1)) 1000)
5401 (1- (- (match-end 0) (match-beginning 0))))))
5403 (defvar org-font-lock-keywords nil
)
5405 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5406 "Regular expression matching a property line.")
5408 (defvar org-font-lock-hook nil
5409 "Functions to be called for special font lock stuff.")
5411 (defvar org-font-lock-set-keywords-hook nil
5412 "Functions that can manipulate `org-font-lock-extra-keywords'.
5413 This is calles after `org-font-lock-extra-keywords' is defined, but before
5414 it is installed to be used by font lock. This can be useful if something
5415 needs to be inserted at a specific position in the font-lock sequence.")
5417 (defun org-font-lock-hook (limit)
5418 (run-hook-with-args 'org-font-lock-hook limit
))
5420 (defun org-set-font-lock-defaults ()
5421 (let* ((em org-fontify-emphasized-text
)
5422 (lk org-activate-links
)
5423 (org-font-lock-extra-keywords
5426 '(org-font-lock-hook)
5428 `(,(if org-fontify-whole-heading-line
5429 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5430 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5431 (1 (org-get-level-face 1))
5432 (2 (org-get-level-face 2))
5433 (3 (org-get-level-face 3)))
5435 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5438 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
5439 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
5440 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
5441 '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t
))
5443 (list org-drawer-regexp
'(0 'org-special-keyword t
))
5444 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
5446 (list org-property-re
5447 '(1 'org-special-keyword t
)
5448 '(3 'org-property-value t
))
5450 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
5451 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
5452 (if (memq 'plain lk
) '(org-activate-plain-links))
5453 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
5454 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
5455 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
5456 (if (memq 'footnote lk
) '(org-activate-footnote-links
5457 (2 'org-footnote t
)))
5458 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
5459 '(org-hide-wide-columns (0 nil append
))
5461 (list (concat "^\\*+[ \t]+" org-todo-regexp
"\\([ \t]\\|$\\)")
5462 '(1 (org-get-todo-face 1) t
))
5464 (if org-fontify-done-headline
5465 (list (concat "^[*]+ +\\<\\("
5466 (mapconcat 'regexp-quote org-done-keywords
"\\|")
5468 '(2 'org-headline-done t
))
5471 '(org-font-lock-add-priority-faces)
5473 '(org-font-lock-add-tag-faces)
5475 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
5476 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
5477 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
5478 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
5481 (if (featurep 'xemacs
)
5482 '(org-do-emphasis-faces (0 nil append
))
5483 '(org-do-emphasis-faces)))
5485 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
5486 1 'org-checkbox prepend
)
5487 (if (cdr (assq 'checkbox org-list-automatic-rules
))
5488 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5489 (0 (org-get-checkbox-statistics-face) t
)))
5490 ;; Description list items
5491 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\(.*? ::\\)"
5493 ;; ARCHIVEd headings
5494 (list (concat "^\\*+ \\(.*:" org-archive-tag
":.*\\)")
5495 '(1 'org-archived prepend
))
5497 '(org-do-latex-and-special-faces)
5498 '(org-fontify-entities)
5499 '(org-raise-scripts)
5501 '(org-activate-code (1 'org-code t
))
5503 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
5504 "\\|" org-quote-string
"\\)\\>")
5505 '(1 'org-special-keyword t
))
5506 '("^#.*" (0 'font-lock-comment-face t
))
5507 ;; Blocks and meta lines
5508 '(org-fontify-meta-lines-and-blocks)
5510 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
5511 (run-hooks 'org-font-lock-set-keywords-hook
)
5512 ;; Now set the full font-lock-keywords
5513 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
5514 (org-set-local 'font-lock-defaults
5515 '(org-font-lock-keywords t nil nil backward-paragraph
))
5516 (kill-local-variable 'font-lock-keywords
) nil
))
5518 (defun org-toggle-pretty-entities ()
5519 "Toggle the composition display of entities as UTF8 characters."
5521 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
5522 (org-restart-font-lock)
5523 (if org-pretty-entities
5524 (message "Entities are displayed as UTF8 characers")
5527 (decompose-region (point-min) (point-max))
5528 (message "Entities are displayed plain"))))
5530 (defun org-fontify-entities (limit)
5531 "Find an entity to fontify."
5533 (when org-pretty-entities
5535 (while (re-search-forward
5536 "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
5538 (if (and (not (org-in-indented-comment-line))
5539 (setq ee
(org-entity-get (match-string 1)))
5540 (= (length (nth 6 ee
)) 1))
5542 (add-text-properties
5543 (match-beginning 0) (match-end 1)
5544 (list 'font-lock-fontified t
))
5545 (compose-region (match-beginning 0) (match-end 1)
5551 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
5552 "Fontify string S like in Org-mode."
5555 (let ((org-odd-levels-only odd-levels
))
5557 (font-lock-fontify-buffer)
5563 (defun org-get-level-face (n)
5564 "Get the right face for match N in font-lock matching of headlines."
5565 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
5566 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
5567 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
5569 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
5571 (t (if org-level-color-stars-only nil org-f
))))
5573 (defun org-get-todo-face (kwd)
5574 "Get the right face for a TODO keyword KWD.
5575 If KWD is a number, get the corresponding match group."
5576 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5577 (or (org-face-from-face-or-color
5578 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
5579 (and (member kwd org-done-keywords
) 'org-done
)
5582 (defun org-face-from-face-or-color (context inherit face-or-color
)
5583 "Create a face list that inherits INHERIT, but sets the foreground color.
5584 When FACE-OR-COLOR is not a string, just return it."
5585 (if (stringp face-or-color
)
5586 (list :inherit inherit
5587 (cdr (assoc context org-faces-easy-properties
))
5591 (defun org-font-lock-add-tag-faces (limit)
5592 "Add the special tag faces."
5593 (when (and org-tag-faces org-tags-special-faces-re
)
5594 (while (re-search-forward org-tags-special-faces-re limit t
)
5595 (add-text-properties (match-beginning 1) (match-end 1)
5596 (list 'face
(org-get-tag-face 1)
5597 'font-lock-fontified t
))
5598 (backward-char 1))))
5600 (defun org-font-lock-add-priority-faces (limit)
5601 "Add the special priority faces."
5602 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
5603 (add-text-properties
5604 (match-beginning 0) (match-end 0)
5605 (list 'face
(or (org-face-from-face-or-color
5606 'priority
'org-special-keyword
5607 (cdr (assoc (char-after (match-beginning 1))
5608 org-priority-faces
)))
5609 'org-special-keyword
)
5610 'font-lock-fontified t
))))
5612 (defun org-get-tag-face (kwd)
5613 "Get the right face for a TODO keyword KWD.
5614 If KWD is a number, get the corresponding match group."
5615 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5616 (or (org-face-from-face-or-color
5617 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
5620 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
5621 "Remove fontification and activation overlays from links."
5622 (font-lock-default-unfontify-region beg end
)
5623 (let* ((buffer-undo-list t
)
5624 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
5625 (inhibit-modification-hooks t
)
5626 deactivate-mark buffer-file-name buffer-file-truename
)
5627 (decompose-region beg end
)
5628 (remove-text-properties
5631 ;; also remove line-prefix and wrap-prefix properties
5632 '(mouse-face t keymap t org-linked-text t
5633 invisible t intangible t
5634 line-prefix t wrap-prefix t
5635 org-no-flyspell t org-emphasis t
)
5636 '(mouse-face t keymap t org-linked-text t
5637 invisible t intangible t
5638 org-no-flyspell t org-emphasis t
)))
5639 (org-remove-font-lock-display-properties beg end
)))
5641 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
5642 ((raise 0.3) (height 0.7))
5645 "Display properties for showing superscripts and subscripts.")
5647 (defun org-remove-font-lock-display-properties (beg end
)
5648 "Remove specific display properties that have been added by font lock.
5649 The will remove the raise properties that are used to show superscripts
5653 (setq next
(next-single-property-change beg
'display nil end
)
5654 prop
(get-text-property beg
'display
))
5655 (if (member prop org-script-display
)
5656 (put-text-property beg next
'display nil
))
5659 (defun org-raise-scripts (limit)
5660 "Add raise properties to sub/superscripts."
5661 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
5662 (if (re-search-forward
5663 (if (eq org-use-sub-superscripts t
)
5664 org-match-substring-regexp
5665 org-match-substring-with-braces-regexp
)
5667 (let* ((pos (point)) table-p comment-p
5668 (mpos (match-beginning 3))
5669 (emph-p (get-text-property mpos
'org-emphasis
))
5670 (link-p (get-text-property mpos
'mouse-face
))
5671 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
5672 (goto-char (point-at-bol))
5673 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
5674 comment-p
(org-looking-at-p "[ \t]*#"))
5676 ;; FIXME: Should we go back one character here, for a_b^c
5677 ;; (goto-char (1- pos)) ;????????????????????
5678 (if (or comment-p emph-p link-p keyw-p
)
5680 (put-text-property (match-beginning 3) (match-end 0)
5682 (if (equal (char-after (match-beginning 2)) ?^
)
5683 (nth (if table-p
3 1) org-script-display
)
5684 (nth (if table-p
2 0) org-script-display
)))
5685 (add-text-properties (match-beginning 2) (match-end 2)
5687 'org-dwidth t
'org-dwidth-n
1))
5688 (if (and (eq (char-after (match-beginning 3)) ?
{)
5689 (eq (char-before (match-end 3)) ?
}))
5691 (add-text-properties
5692 (match-beginning 3) (1+ (match-beginning 3))
5693 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
5694 (add-text-properties
5695 (1- (match-end 3)) (match-end 3)
5696 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
5699 ;;;; Visibility cycling, including org-goto and indirect buffer
5703 (defvar org-cycle-global-status nil
)
5704 (make-variable-buffer-local 'org-cycle-global-status
)
5705 (defvar org-cycle-subtree-status nil
)
5706 (make-variable-buffer-local 'org-cycle-subtree-status
)
5710 (defvar org-inlinetask-min-level
)
5712 (defun org-cycle (&optional arg
)
5713 "TAB-action and visibility cycling for Org-mode.
5715 This is the command invoked in Org-mode by the TAB key. Its main purpose
5716 is outline visibility cycling, but it also invokes other actions
5717 in special contexts.
5719 - When this function is called with a prefix argument, rotate the entire
5720 buffer through 3 states (global cycling)
5721 1. OVERVIEW: Show only top-level headlines.
5722 2. CONTENTS: Show all headlines of all levels, but no body text.
5723 3. SHOW ALL: Show everything.
5724 When called with two `C-u C-u' prefixes, switch to the startup visibility,
5725 determined by the variable `org-startup-folded', and by any VISIBILITY
5726 properties in the buffer.
5727 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
5728 including any drawers.
5730 - When inside a table, re-align the table and move to the next field.
5732 - When point is at the beginning of a headline, rotate the subtree started
5733 by this line through 3 different states (local cycling)
5734 1. FOLDED: Only the main headline is shown.
5735 2. CHILDREN: The main headline and the direct children are shown.
5736 From this state, you can move to one of the children
5737 and zoom in further.
5738 3. SUBTREE: Show the entire subtree, including body text.
5739 If there is no subtree, switch directly from CHILDREN to FOLDED.
5741 - When point is at the beginning of an empty headline and the variable
5742 `org-cycle-level-after-item/entry-creation' is set, cycle the level
5743 of the headline by demoting and promoting it to likely levels. This
5744 speeds up creation document structure by pressing TAB once or several
5745 times right after creating a new headline.
5747 - When there is a numeric prefix, go up to a heading with level ARG, do
5748 a `show-subtree' and return to the previous cursor position. If ARG
5749 is negative, go up that many levels.
5751 - When point is not at the beginning of a headline, execute the global
5752 binding for TAB, which is re-indenting the line. See the option
5753 `org-cycle-emulate-tab' for details.
5755 - Special case: if point is at the beginning of the buffer and there is
5756 no headline in line 1, this function will act as if called with prefix arg.
5757 But only if also the variable `org-cycle-global-at-bob' is t."
5759 (org-load-modules-maybe)
5760 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
5761 (and org-cycle-level-after-item
/entry-creation
5762 (or (org-cycle-level)
5763 (org-cycle-item-indentation))))
5765 (or org-cycle-max-level
5766 (and (boundp 'org-inlinetask-min-level
)
5767 org-inlinetask-min-level
5768 (1- org-inlinetask-min-level
))))
5769 (nstars (and limit-level
5770 (if org-odd-levels-only
5771 (and limit-level
(1- (* limit-level
2)))
5775 ((not (org-mode-p)) outline-regexp
)
5776 ((or (eq org-cycle-include-plain-lists
'integrate
)
5777 (and org-cycle-include-plain-lists
(org-at-item-p)))
5779 (if nstars
(format "\\{1,%d\\}" nstars
) "+")
5780 " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
5781 (t (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ ")))))
5782 (bob-special (and org-cycle-global-at-bob
(bobp)
5783 (not (looking-at outline-regexp
))))
5786 (delq 'org-optimize-window-after-visibility-change
5787 (copy-sequence org-cycle-hook
))
5791 (if (or bob-special
(equal arg
'(4)))
5792 ;; special case: use global cycling
5798 (org-set-startup-visibility)
5799 (message "Startup visibility, plus VISIBILITY properties"))
5803 (message "Entire buffer visible, including drawers"))
5805 ((org-at-table-p 'any
)
5806 ;; Enter the table or move to the next field in the table
5807 (if (org-at-table.el-p
)
5808 (message "Use C-c ' to edit table.el tables")
5809 (if arg
(org-table-edit-field t
)
5810 (org-table-justify-field-maybe)
5811 (call-interactively 'org-table-next-field
))))
5813 ((run-hook-with-args-until-success
5814 'org-tab-after-check-for-table-hook
))
5816 ((eq arg t
) ;; Global cycling
5817 (org-cycle-internal-global))
5819 ((and org-drawers org-drawer-regexp
5821 (beginning-of-line 1)
5822 (looking-at org-drawer-regexp
)))
5823 ;; Toggle block visibility
5825 (not (get-char-property (match-end 0) 'invisible
))))
5828 ;; Show-subtree, ARG levels up from here.
5830 (org-back-to-heading)
5831 (outline-up-heading (if (< arg
0) (- arg
)
5832 (- (funcall outline-level
) arg
)))
5833 (org-show-subtree)))
5835 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp
))
5836 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
5838 (org-cycle-internal-local))
5840 ;; TAB emulation and template completion
5841 (buffer-read-only (org-back-to-heading))
5843 ((run-hook-with-args-until-success
5844 'org-tab-after-check-for-cycling-hook
))
5846 ((org-try-structure-completion))
5848 ((org-try-cdlatex-tab))
5850 ((run-hook-with-args-until-success
5851 'org-tab-before-tab-emulation-hook
))
5853 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
5855 (not (looking-at outline-regexp
))))
5856 (call-interactively (global-key-binding "\t")))
5858 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
5859 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
5860 (or (and (eq org-cycle-emulate-tab
'white
)
5861 (= (match-end 0) (point-at-eol)))
5862 (and (eq org-cycle-emulate-tab
'whitestart
)
5863 (>= (match-end 0) pos
))))
5865 (eq org-cycle-emulate-tab t
))
5866 (call-interactively (global-key-binding "\t")))
5869 (org-back-to-heading)
5872 (defun org-cycle-internal-global ()
5873 "Do the global cycling action."
5875 ((and (eq last-command this-command
)
5876 (eq org-cycle-global-status
'overview
))
5877 ;; We just created the overview - now do table of contents
5878 ;; This can be slow in very large buffers, so indicate action
5879 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
5880 (message "CONTENTS...")
5882 (message "CONTENTS...done")
5883 (setq org-cycle-global-status
'contents
)
5884 (run-hook-with-args 'org-cycle-hook
'contents
))
5886 ((and (eq last-command this-command
)
5887 (eq org-cycle-global-status
'contents
))
5888 ;; We just showed the table of contents - now show everything
5889 (run-hook-with-args 'org-pre-cycle-hook
'all
)
5891 (message "SHOW ALL")
5892 (setq org-cycle-global-status
'all
)
5893 (run-hook-with-args 'org-cycle-hook
'all
))
5896 ;; Default action: go to overview
5897 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
5899 (message "OVERVIEW")
5900 (setq org-cycle-global-status
'overview
)
5901 (run-hook-with-args 'org-cycle-hook
'overview
))))
5903 (defun org-cycle-internal-local ()
5904 "Do the local cycling action."
5905 (let ((goal-column 0) eoh eol eos level has-children children-skipped
)
5906 ;; First, some boundaries
5908 (org-back-to-heading)
5909 (setq level
(funcall outline-level
))
5911 (beginning-of-line 2)
5912 (if (or (featurep 'xemacs
) (<= emacs-major-version
21))
5913 ; XEmacs does not have `next-single-char-property-change'
5914 ; I'm not sure about Emacs 21.
5915 (while (and (not (eobp)) ;; this is like `next-line'
5916 (get-char-property (1- (point)) 'invisible
))
5917 (beginning-of-line 2))
5918 (while (and (not (eobp)) ;; this is like `next-line'
5919 (get-char-property (1- (point)) 'invisible
))
5920 (goto-char (next-single-char-property-change (point) 'invisible
))
5921 (and (eolp) (beginning-of-line 2))))
5923 (outline-end-of-heading) (setq eoh
(point))
5925 (outline-next-heading)
5926 (setq has-children
(and (org-at-heading-p t
)
5927 (> (funcall outline-level
) level
))))
5928 ;; if we're in a list, org-end-of-subtree is in fact org-end-of-item.
5930 (setq eos
(if (and (org-end-of-item) (bolp))
5933 (org-end-of-subtree t
)
5935 (skip-chars-forward " \t\n"))
5936 (setq eos
(if (eobp) (point) (1- (point))))))
5937 ;; Find out what to do next and set `this-command'
5940 ;; Nothing is hidden behind this heading
5941 (run-hook-with-args 'org-pre-cycle-hook
'empty
)
5942 (message "EMPTY ENTRY")
5943 (setq org-cycle-subtree-status nil
)
5946 (outline-next-heading)
5947 (if (org-invisible-p) (org-flag-heading nil
))))
5948 ((and (or (>= eol eos
)
5949 (not (string-match "\\S-" (buffer-substring eol eos
))))
5951 (not (setq children-skipped
5952 org-cycle-skip-children-state-if-no-children
))))
5953 ;; Entire subtree is hidden in one line: children view
5954 (run-hook-with-args 'org-pre-cycle-hook
'children
)
5957 (message "CHILDREN")
5960 (outline-next-heading)
5961 (if (org-invisible-p) (org-flag-heading nil
)))
5962 (setq org-cycle-subtree-status
'children
)
5963 (run-hook-with-args 'org-cycle-hook
'children
))
5964 ((or children-skipped
5965 (and (eq last-command this-command
)
5966 (eq org-cycle-subtree-status
'children
)))
5967 ;; We just showed the children, or no children are there,
5968 ;; now show everything.
5969 (run-hook-with-args 'org-pre-cycle-hook
'subtree
)
5970 (outline-flag-region eoh eos nil
)
5971 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
5972 (setq org-cycle-subtree-status
'subtree
)
5973 (run-hook-with-args 'org-cycle-hook
'subtree
))
5975 ;; Default action: hide the subtree.
5976 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
5977 (outline-flag-region eoh eos t
)
5979 (setq org-cycle-subtree-status
'folded
)
5980 (run-hook-with-args 'org-cycle-hook
'folded
)))))
5983 (defun org-global-cycle (&optional arg
)
5984 "Cycle the global visibility. For details see `org-cycle'.
5985 With \\[universal-argument] prefix arg, switch to startup visibility.
5986 With a numeric prefix, show all headlines up to that level."
5988 (let ((org-cycle-include-plain-lists
5989 (if (org-mode-p) org-cycle-include-plain-lists nil
)))
5993 (hide-sublevels arg
)
5994 (setq org-cycle-global-status
'contents
))
5996 (org-set-startup-visibility)
5997 (message "Startup visibility, plus VISIBILITY properties."))
5999 (org-cycle '(4))))))
6001 (defun org-set-startup-visibility ()
6002 "Set the visibility required by startup options and properties."
6004 ((eq org-startup-folded t
)
6006 ((eq org-startup-folded
'content
)
6007 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6008 (org-cycle '(4)) (org-cycle '(4)))))
6009 (unless (eq org-startup-folded
'showeverything
)
6010 (if org-hide-block-startup
(org-hide-block-all))
6011 (org-set-visibility-according-to-property 'no-cleanup
)
6012 (org-cycle-hide-archived-subtrees 'all
)
6013 (org-cycle-hide-drawers 'all
)
6014 (org-cycle-show-empty-lines t
)))
6016 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6017 "Switch subtree visibilities according to :VISIBILITY: property."
6019 (let (org-show-entry-below state
)
6021 (goto-char (point-max))
6022 (while (re-search-backward
6023 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6025 (setq state
(match-string 1))
6027 (org-back-to-heading t
)
6031 ((equal state
'("fold" "folded"))
6033 ((equal state
"children")
6034 (org-show-hidden-entry)
6036 ((equal state
"content")
6039 (org-narrow-to-subtree)
6041 ((member state
'("all" "showall"))
6044 (org-cycle-hide-archived-subtrees 'all
)
6045 (org-cycle-hide-drawers 'all
)
6046 (org-cycle-show-empty-lines 'all
)))))
6048 (defun org-overview ()
6049 "Switch to overview mode, showing only top-level headlines.
6050 Really, this shows all headlines with level equal or greater than the level
6051 of the first headline in the buffer. This is important, because if the
6052 first headline is not level one, then (hide-sublevels 1) gives confusing
6055 (let ((level (save-excursion
6056 (goto-char (point-min))
6057 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6059 (goto-char (match-beginning 0))
6060 (funcall outline-level
))))))
6061 (and level
(hide-sublevels level
))))
6063 (defun org-content (&optional arg
)
6064 "Show all headlines in the buffer, like a table of contents.
6065 With numerical argument N, show content up to level N."
6068 ;; Visit all headings and show their offspring
6069 (and (integerp arg
) (org-overview))
6070 (goto-char (point-max))
6072 (while (and (progn (condition-case nil
6073 (outline-previous-visible-heading 1)
6074 (error (goto-char (point-min))))
6076 (looking-at outline-regexp
))
6078 (show-children (1- arg
))
6080 (if (bobp) (throw 'exit nil
))))))
6083 (defun org-optimize-window-after-visibility-change (state)
6084 "Adjust the window after a change in outline visibility.
6085 This function is the default value of the hook `org-cycle-hook'."
6086 (when (get-buffer-window (current-buffer))
6088 ((eq state
'content
) nil
)
6089 ((eq state
'all
) nil
)
6090 ((eq state
'folded
) nil
)
6091 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6092 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6094 (defun org-remove-empty-overlays-at (pos)
6095 "Remove outline overlays that do not contain non-white stuff."
6098 (and (eq 'outline
(overlay-get o
'invisible
))
6099 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6101 (delete-overlay o
)))
6104 (defun org-clean-visibility-after-subtree-move ()
6105 "Fix visibility issues after moving a subtree."
6106 ;; First, find a reasonable region to look at:
6107 ;; Start two siblings above, end three below
6108 (let* ((beg (save-excursion
6109 (and (org-get-last-sibling)
6110 (org-get-last-sibling))
6112 (end (save-excursion
6113 (and (org-get-next-sibling)
6114 (org-get-next-sibling)
6115 (org-get-next-sibling))
6116 (if (org-at-heading-p)
6119 (level (looking-at "\\*+"))
6120 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6123 (narrow-to-region beg end
)
6125 ;; Properly fold already folded siblings
6126 (goto-char (point-min))
6127 (while (re-search-forward re nil t
)
6128 (if (and (not (org-invisible-p))
6130 (goto-char (point-at-eol)) (org-invisible-p)))
6132 (org-cycle-show-empty-lines 'overview
)
6133 (org-cycle-hide-drawers 'overview
)))))
6135 (defun org-cycle-show-empty-lines (state)
6136 "Show empty lines above all visible headlines.
6137 The region to be covered depends on STATE when called through
6138 `org-cycle-hook'. Lisp program can use t for STATE to get the
6139 entire buffer covered. Note that an empty line is only shown if there
6140 are at least `org-cycle-separator-lines' empty lines before the headline."
6141 (when (not (= org-cycle-separator-lines
0))
6143 (let* ((n (abs org-cycle-separator-lines
))
6145 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6146 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6147 (t (let ((ns (number-to-string (- n
2))))
6148 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6149 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6152 ((memq state
'(overview contents t
))
6153 (setq beg
(point-min) end
(point-max)))
6154 ((memq state
'(children folded
))
6155 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6156 (beginning-of-line 2)
6160 (while (re-search-forward re end t
)
6161 (unless (get-char-property (match-end 1) 'invisible
)
6162 (setq e
(match-end 1))
6163 (if (< org-cycle-separator-lines
0)
6164 (setq b
(save-excursion
6165 (goto-char (match-beginning 0))
6166 (org-back-over-empty-lines)
6168 (goto-char (max (point-min) (1- (point))))
6172 (setq b
(match-beginning 1)))
6173 (outline-flag-region b e nil
)))))))
6174 ;; Never hide empty lines at the end of the file.
6176 (goto-char (point-max))
6177 (outline-previous-heading)
6178 (outline-end-of-heading)
6179 (if (and (looking-at "[ \t\n]+")
6180 (= (match-end 0) (point-max)))
6181 (outline-flag-region (point) (match-end 0) nil
))))
6183 (defun org-show-empty-lines-in-parent ()
6184 "Move to the parent and re-show empty lines before visible headlines."
6186 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6187 (org-cycle-show-empty-lines context
))))
6189 (defun org-files-list ()
6190 "Return `org-agenda-files' list, plus all open org-mode files.
6191 This is useful for operations that need to scan all of a user's
6192 open and agenda-wise Org files."
6193 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6194 (dolist (buf (buffer-list))
6195 (with-current-buffer buf
6196 (if (and (eq major-mode
'org-mode
) (buffer-file-name))
6197 (let ((file (expand-file-name (buffer-file-name))))
6198 (unless (member file files
)
6199 (push file files
))))))
6202 (defsubst org-entry-beginning-position
()
6203 "Return the beginning position of the current entry."
6204 (save-excursion (outline-back-to-heading t
) (point)))
6206 (defsubst org-entry-end-position
()
6207 "Return the end position of the current entry."
6208 (save-excursion (outline-next-heading) (point)))
6210 (defun org-cycle-hide-drawers (state)
6211 "Re-hide all drawers after a visibility state change."
6212 (when (and (org-mode-p)
6213 (not (memq state
'(overview folded contents
))))
6215 (let* ((globalp (memq state
'(contents all
)))
6216 (beg (if globalp
(point-min) (point)))
6217 (end (if globalp
(point-max)
6218 (if (eq state
'children
)
6219 (save-excursion (outline-next-heading) (point))
6220 (org-end-of-subtree t
)))))
6222 (while (re-search-forward org-drawer-regexp end t
)
6223 (org-flag-drawer t
))))))
6225 (defun org-flag-drawer (flag)
6227 (beginning-of-line 1)
6228 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6229 (let ((b (match-end 0))
6230 (outline-regexp org-outline-regexp
))
6231 (if (re-search-forward
6233 (save-excursion (outline-next-heading) (point)) t
)
6234 (outline-flag-region b
(point-at-eol) flag
)
6235 (error ":END: line missing at position %s" b
))))))
6237 (defun org-subtree-end-visible-p ()
6238 "Is the end of the current subtree visible?"
6239 (pos-visible-in-window-p
6240 (save-excursion (org-end-of-subtree t
) (point))))
6242 (defun org-first-headline-recenter (&optional N
)
6243 "Move cursor to the first headline and recenter the headline.
6244 Optional argument N means put the headline into the Nth line of the window."
6245 (goto-char (point-min))
6246 (when (re-search-forward (concat "^\\(" outline-regexp
"\\)") nil t
)
6248 (recenter (prefix-numeric-value N
))))
6250 ;;; Saving and restoring visibility
6252 (defun org-outline-overlay-data (&optional use-markers
)
6253 "Return a list of the locations of all outline overlays.
6254 These are overlays with the `invisible' property value `outline'.
6255 The return value is a list of cons cells, with start and stop
6256 positions for each overlay.
6257 If USE-MARKERS is set, return the positions as markers."
6264 (when (eq (overlay-get o
'invisible
) 'outline
)
6265 (setq beg
(overlay-start o
)
6266 end
(overlay-end o
))
6267 (and beg end
(> end beg
)
6269 (cons (move-marker (make-marker) beg
)
6270 (move-marker (make-marker) end
))
6272 (overlays-in (point-min) (point-max))))))))
6274 (defun org-set-outline-overlay-data (data)
6275 "Create visibility overlays for all positions in DATA.
6276 DATA should have been made by `org-outline-overlay-data'."
6283 (setq o
(make-overlay (car c
) (cdr c
)))
6284 (overlay-put o
'invisible
'outline
))
6287 ;;; Folding of blocks
6289 (defconst org-block-regexp
6291 "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
6292 "Regular expression for hiding blocks.")
6294 (defvar org-hide-block-overlays nil
6295 "Overlays hiding blocks.")
6296 (make-variable-buffer-local 'org-hide-block-overlays
)
6298 (defun org-block-map (function &optional start end
)
6299 "Call FUNCTION at the head of all source blocks in the current buffer.
6300 Optional arguments START and END can be used to limit the range."
6301 (let ((start (or start
(point-min)))
6302 (end (or end
(point-max))))
6305 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6308 (goto-char (match-beginning 0))
6309 (funcall function
)))))))
6311 (defun org-hide-block-toggle-all ()
6312 "Toggle the visibility of all blocks in the current buffer."
6313 (org-block-map #'org-hide-block-toggle
))
6315 (defun org-hide-block-all ()
6316 "Fold all blocks in the current buffer."
6318 (org-show-block-all)
6319 (org-block-map #'org-hide-block-toggle-maybe
))
6321 (defun org-show-block-all ()
6322 "Unfold all blocks in the current buffer."
6324 (mapc 'delete-overlay org-hide-block-overlays
)
6325 (setq org-hide-block-overlays nil
))
6327 (defun org-hide-block-toggle-maybe ()
6328 "Toggle visibility of block at point."
6330 (let ((case-fold-search t
))
6332 (beginning-of-line 1)
6333 (looking-at org-block-regexp
))
6334 (progn (org-hide-block-toggle)
6335 t
) ;; to signal that we took action
6336 nil
))) ;; to signal that we did not
6338 (defun org-hide-block-toggle (&optional force
)
6339 "Toggle the visibility of the current block."
6343 (if (re-search-forward org-block-regexp nil t
)
6344 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
6345 (end (match-end 0)) ;; end of entire body
6347 (if (memq t
(mapcar (lambda (overlay)
6348 (eq (overlay-get overlay
'invisible
)
6350 (overlays-at start
)))
6351 (if (or (not force
) (eq force
'off
))
6353 (when (member ov org-hide-block-overlays
)
6354 (setq org-hide-block-overlays
6355 (delq ov org-hide-block-overlays
)))
6356 (when (eq (overlay-get ov
'invisible
)
6358 (delete-overlay ov
)))
6359 (overlays-at start
)))
6360 (setq ov
(make-overlay start end
))
6361 (overlay-put ov
'invisible
'org-hide-block
)
6362 ;; make the block accessible to isearch
6364 ov
'isearch-open-invisible
6366 (when (member ov org-hide-block-overlays
)
6367 (setq org-hide-block-overlays
6368 (delq ov org-hide-block-overlays
)))
6369 (when (eq (overlay-get ov
'invisible
)
6371 (delete-overlay ov
))))
6372 (push ov org-hide-block-overlays
)))
6373 (error "Not looking at a source block"))))
6375 ;; org-tab-after-check-for-cycling-hook
6376 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
6377 ;; Remove overlays when changing major mode
6378 (add-hook 'org-mode-hook
6379 (lambda () (org-add-hook 'change-major-mode-hook
6380 'org-show-block-all
'append
'local
)))
6384 (defvar org-goto-window-configuration nil
)
6385 (defvar org-goto-marker nil
)
6386 (defvar org-goto-map
6387 (let ((map (make-sparse-keymap)))
6388 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur
)) cmd
)
6389 (while (setq cmd
(pop cmds
))
6390 (substitute-key-definition cmd cmd map global-map
)))
6391 (suppress-keymap map
)
6392 (org-defkey map
"\C-m" 'org-goto-ret
)
6393 (org-defkey map
[(return)] 'org-goto-ret
)
6394 (org-defkey map
[(left)] 'org-goto-left
)
6395 (org-defkey map
[(right)] 'org-goto-right
)
6396 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
6397 (org-defkey map
"\C-i" 'org-cycle
)
6398 (org-defkey map
[(tab)] 'org-cycle
)
6399 (org-defkey map
[(down)] 'outline-next-visible-heading
)
6400 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
6401 (if org-goto-auto-isearch
6402 (if (fboundp 'define-key-after
)
6403 (define-key-after map
[t] 'org-goto-local-auto-isearch)
6405 (org-defkey map "q" 'org-goto-quit)
6406 (org-defkey map "n" 'outline-next-visible-heading)
6407 (org-defkey map "p" 'outline-previous-visible-heading)
6408 (org-defkey map "f" 'outline-forward-same-level)
6409 (org-defkey map "b" 'outline-backward-same-level)
6410 (org-defkey map "u" 'outline-up-heading))
6411 (org-defkey map "/" 'org-occur)
6412 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6413 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6414 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6415 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6416 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6419 (defconst org-goto-help
6420 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6421 RET=jump to location [Q]uit and return to previous location
6422 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6424 (defvar org-goto-start-pos) ; dynamically scoped parameter
6426 ;; FIXME: Docstring does not mention both interfaces
6427 (defun org-goto (&optional alternative-interface)
6428 "Look up a different location in the current file, keeping current visibility.
6430 When you want look-up or go to a different location in a document, the
6431 fastest way is often to fold the entire buffer and then dive into the tree.
6432 This method has the disadvantage, that the previous location will be folded,
6433 which may not be what you want.
6435 This command works around this by showing a copy of the current buffer
6436 in an indirect buffer, in overview mode. You can dive into the tree in
6437 that copy, use org-occur and incremental search to find a location.
6438 When pressing RET or `Q', the command returns to the original buffer in
6439 which the visibility is still unchanged. After RET is will also jump to
6440 the location selected in the indirect buffer and expose the
6441 the headline hierarchy above."
6443 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6444 (org-refile-use-outline-path t)
6445 (org-refile-target-verify-function nil)
6447 (if (not alternative-interface)
6449 (if (eq org-goto-interface 'outline)
6450 'outline-path-completion
6452 (org-goto-start-pos (point))
6454 (if (eq interface 'outline)
6455 (car (org-get-location (current-buffer) org-goto-help))
6456 (let ((pa (org-refile-get-location "Goto: ")))
6457 (org-refile-check-position pa)
6461 (org-mark-ring-push org-goto-start-pos)
6462 (goto-char selected-point)
6463 (if (or (org-invisible-p) (org-invisible-p2))
6464 (org-show-context 'org-goto)))
6467 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6468 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6469 (defvar org-goto-local-auto-isearch-map) ; defined below
6471 (defun org-get-location (buf help)
6472 "Let the user select a location in the Org-mode buffer BUF.
6473 This function uses a recursive edit. It returns the selected position
6475 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6476 (isearch-hide-immediately nil)
6477 (isearch-search-fun-function
6478 (lambda () 'org-goto-local-search-headings))
6479 (org-goto-selected-point org-goto-exit-command)
6481 (special-display-buffer-names nil)
6482 (special-display-regexps nil)
6483 (special-display-function nil))
6485 (save-window-excursion
6486 (delete-other-windows)
6487 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6490 (make-indirect-buffer (current-buffer) "*org-goto*")
6491 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6492 (with-output-to-temp-buffer "*Help*"
6494 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
6495 (setq buffer-read-only nil)
6496 (let ((org-startup-truncated t)
6497 (org-startup-folded nil)
6498 (org-startup-align-all-tables nil))
6501 (setq buffer-read-only t)
6502 (if (and (boundp 'org-goto-start-pos)
6503 (integer-or-marker-p org-goto-start-pos))
6504 (let ((org-show-hierarchy-above t)
6505 (org-show-siblings t)
6506 (org-show-following-heading t))
6507 (goto-char org-goto-start-pos)
6508 (and (org-invisible-p) (org-show-context)))
6509 (goto-char (point-min)))
6510 (let (org-special-ctrl-a/e) (org-beginning-of-line))
6511 (message "Select location and press RET")
6512 (use-local-map org-goto-map)
6515 (kill-buffer "*org-goto*")
6516 (cons org-goto-selected-point org-goto-exit-command)))
6518 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6519 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6520 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6521 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6523 (defun org-goto-local-search-headings (string bound noerror)
6524 "Search and make sure that any matches are in headlines."
6526 (while (if isearch-forward
6527 (search-forward string bound noerror)
6528 (search-backward string bound noerror))
6529 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6530 (and (member :headline context)
6531 (not (member :tags context))))
6532 (throw 'return (point))))))
6534 (defun org-goto-local-auto-isearch ()
6537 (goto-char (point-min))
6538 (let ((keys (this-command-keys)))
6539 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6541 (isearch-process-search-char (string-to-char keys)))))
6543 (defun org-goto-ret (&optional arg)
6544 "Finish `org-goto' by going to the new location."
6546 (setq org-goto-selected-point (point)
6547 org-goto-exit-command 'return)
6550 (defun org-goto-left ()
6551 "Finish `org-goto' by going to the new location."
6553 (if (org-on-heading-p)
6555 (beginning-of-line 1)
6556 (setq org-goto-selected-point (point)
6557 org-goto-exit-command 'left)
6559 (error "Not on a heading")))
6561 (defun org-goto-right ()
6562 "Finish `org-goto' by going to the new location."
6564 (if (org-on-heading-p)
6566 (setq org-goto-selected-point (point)
6567 org-goto-exit-command 'right)
6569 (error "Not on a heading")))
6571 (defun org-goto-quit ()
6572 "Finish `org-goto' without cursor motion."
6574 (setq org-goto-selected-point nil)
6575 (setq org-goto-exit-command 'quit)
6578 ;;; Indirect buffer display of subtrees
6580 (defvar org-indirect-dedicated-frame nil
6581 "This is the frame being used for indirect tree display.")
6582 (defvar org-last-indirect-buffer nil)
6584 (defun org-tree-to-indirect-buffer (&optional arg)
6585 "Create indirect buffer and narrow it to current subtree.
6586 With numerical prefix ARG, go up to this level and then take that tree.
6587 If ARG is negative, go up that many levels.
6588 If `org-indirect-buffer-display' is not `new-frame', the command removes the
6589 indirect buffer previously made with this command, to avoid proliferation of
6590 indirect buffers. However, when you call the command with a \
6591 \\[universal-argument] prefix, or
6592 when `org-indirect-buffer-display' is `new-frame', the last buffer
6593 is kept so that you can work with several indirect buffers at the same time.
6594 If `org-indirect-buffer-display' is `dedicated-frame', the \
6595 \\[universal-argument] prefix also
6596 requests that a new frame be made for the new buffer, so that the dedicated
6597 frame is not changed."
6599 (let ((cbuf (current-buffer))
6600 (cwin (selected-window))
6602 beg end level heading ibuf)
6604 (org-back-to-heading t)
6606 (setq level (org-outline-level))
6607 (if (< arg 0) (setq arg (+ level arg)))
6608 (while (> (setq level (org-outline-level)) arg)
6609 (outline-up-heading 1 t)))
6611 heading (org-get-heading))
6612 (org-end-of-subtree t t)
6613 (if (org-on-heading-p) (backward-char 1))
6615 (if (and (buffer-live-p org-last-indirect-buffer)
6616 (not (eq org-indirect-buffer-display 'new-frame))
6618 (kill-buffer org-last-indirect-buffer))
6619 (setq ibuf (org-get-indirect-buffer cbuf)
6620 org-last-indirect-buffer ibuf)
6622 ((or (eq org-indirect-buffer-display 'new-frame)
6623 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
6624 (select-frame (make-frame))
6625 (delete-other-windows)
6626 (switch-to-buffer ibuf)
6627 (org-set-frame-title heading))
6628 ((eq org-indirect-buffer-display 'dedicated-frame)
6630 (select-frame (or (and org-indirect-dedicated-frame
6631 (frame-live-p org-indirect-dedicated-frame)
6632 org-indirect-dedicated-frame)
6633 (setq org-indirect-dedicated-frame (make-frame)))))
6634 (delete-other-windows)
6635 (switch-to-buffer ibuf)
6636 (org-set-frame-title (concat "Indirect: " heading)))
6637 ((eq org-indirect-buffer-display 'current-window)
6638 (switch-to-buffer ibuf))
6639 ((eq org-indirect-buffer-display 'other-window)
6640 (pop-to-buffer ibuf))
6641 (t (error "Invalid value")))
6642 (if (featurep 'xemacs)
6643 (save-excursion (org-mode) (turn-on-font-lock)))
6644 (narrow-to-region beg end)
6647 (and (window-live-p cwin) (select-window cwin))))
6649 (defun org-get-indirect-buffer (&optional buffer)
6650 (setq buffer (or buffer (current-buffer)))
6651 (let ((n 1) (base (buffer-name buffer)) bname)
6652 (while (buffer-live-p
6653 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
6656 (make-indirect-buffer buffer bname 'clone)
6657 (error (make-indirect-buffer buffer bname)))))
6659 (defun org-set-frame-title (title)
6660 "Set the title of the current frame to the string TITLE."
6661 ;; FIXME: how to name a single frame in XEmacs???
6662 (unless (featurep 'xemacs)
6663 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
6665 ;;;; Structure editing
6667 ;;; Inserting headlines
6669 (defun org-previous-line-empty-p ()
6672 (or (beginning-of-line 0) t)
6674 (looking-at "[ \t]*$")))))
6676 (defun org-insert-heading (&optional force-heading invisible-ok)
6677 "Insert a new heading or item with same depth at point.
6678 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
6679 If point is at the beginning of a headline, insert a sibling before the
6680 current headline. If point is not at the beginning, do not split the line,
6681 but create the new headline after the current line.
6682 When INVISIBLE-OK is set, stop at invisible headlines when going back.
6683 This is important for non-interactive uses of the command."
6685 (if (or (= (buffer-size) 0)
6686 (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
6687 (org-on-heading-p))))
6688 (not (org-in-item-p))))
6691 (run-hooks 'org-insert-heading-hook))
6692 (when (or force-heading (not (org-insert-item)))
6693 (let* ((empty-line-p nil)
6694 (head (save-excursion
6697 (org-back-to-heading invisible-ok)
6698 (setq empty-line-p (org-previous-line-empty-p))
6701 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
6702 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
6703 pos hide-previous previous-pos)
6705 ((and (org-on-heading-p) (bolp)
6707 (save-excursion (backward-char 1) (not (org-invisible-p)))))
6708 ;; insert before the current line
6709 (open-line (if blank 2 1)))
6711 (not org-insert-heading-respect-content)
6714 (backward-char 1) (not (org-invisible-p)))))
6715 ;; insert right here
6718 ;; somewhere in the line
6720 (setq previous-pos (point-at-bol))
6722 (setq hide-previous (org-invisible-p)))
6723 (and org-insert-heading-respect-content (org-show-subtree))
6725 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
6728 (goto-char (point-at-bol))
6729 (and (looking-at org-complex-heading-regexp)
6730 (> p (match-beginning 4)))))))
6733 (org-insert-heading-respect-content
6734 (org-end-of-subtree nil t)
6735 (or (bolp) (newline))
6736 (or (org-previous-line-empty-p)
6737 (and blank (newline)))
6743 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
6744 (setq tags (and (match-end 2) (match-string 2)))
6746 (delete-region (match-beginning 1) (match-end 1)))
6747 (setq pos (point-at-bol))
6748 (or split (end-of-line 1))
6749 (delete-horizontal-space)
6750 (if (string-match "\\`\\*+\\'"
6751 (buffer-substring (point-at-bol) (point)))
6753 (newline (if blank 2 1))
6759 (org-set-tags nil 'align))))
6761 (or split (end-of-line 1))
6762 (newline (if blank 2 1)))))))
6763 (insert head) (just-one-space)
6766 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
6767 (when (and org-insert-heading-respect-content hide-previous)
6769 (goto-char previous-pos)
6771 (run-hooks 'org-insert-heading-hook)))))
6773 (defun org-get-heading (&optional no-tags)
6774 "Return the heading of the current entry, without the stars."
6776 (org-back-to-heading t)
6779 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
6780 "\\*+[ \t]+\\([^\r\n]*\\)"))
6781 (match-string 1) "")))
6783 (defun org-heading-components ()
6784 "Return the components of the current heading.
6785 This is a list with the following elements:
6786 - the level as an integer
6787 - the reduced level, different if `org-odd-levels-only' is set.
6788 - the TODO keyword, or nil
6789 - the priority character, like ?A, or nil if no priority is given
6790 - the headline text itself, or the tags string if no headline text
6791 - the tags string, or nil."
6793 (org-back-to-heading t)
6794 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
6795 (list (length (match-string 1))
6796 (org-reduced-level (length (match-string 1)))
6797 (org-match-string-no-properties 2)
6798 (and (match-end 3) (aref (match-string 3) 2))
6799 (org-match-string-no-properties 4)
6800 (org-match-string-no-properties 5)))))
6802 (defun org-get-entry ()
6803 "Get the entry text, after heading, entire subtree."
6805 (org-back-to-heading t)
6806 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
6808 (defun org-insert-heading-after-current ()
6809 "Insert a new heading with same level as current, after current subtree."
6811 (org-back-to-heading)
6812 (org-insert-heading)
6813 (org-move-subtree-down)
6816 (defun org-insert-heading-respect-content ()
6818 (let ((org-insert-heading-respect-content t))
6819 (org-insert-heading t)))
6821 (defun org-insert-todo-heading-respect-content (&optional force-state)
6823 (let ((org-insert-heading-respect-content t))
6824 (org-insert-todo-heading force-state t)))
6826 (defun org-insert-todo-heading (arg &optional force-heading)
6827 "Insert a new heading with the same level and TODO state as current heading.
6828 If the heading has no TODO state, or if the state is DONE, use the first
6829 state (TODO by default). Also with prefix arg, force first state."
6831 (when (or force-heading (not (org-insert-item 'checkbox)))
6832 (org-insert-heading force-heading)
6834 (org-back-to-heading)
6835 (outline-previous-heading)
6836 (looking-at org-todo-line-regexp))
6840 (not (match-beginning 2))
6841 (member (match-string 2) org-done-keywords))
6842 (car org-todo-keywords-1)
6846 (run-hook-with-args-until-success
6847 'org-todo-get-default-hook new-mark-x nil)
6849 (beginning-of-line 1)
6850 (and (looking-at "\\*+ ") (goto-char (match-end 0))
6851 (if org-treat-insert-todo-heading-as-state-change
6853 (insert new-mark " "))))
6854 (when org-provide-todo-statistics
6855 (org-update-parent-todo-statistics))))
6857 (defun org-insert-subheading (arg)
6858 "Insert a new subheading and demote it.
6859 Works for outline headings and for plain lists alike."
6861 (org-insert-heading arg)
6863 ((org-on-heading-p) (org-do-demote))
6864 ((org-at-item-p) (org-indent-item))))
6866 (defun org-insert-todo-subheading (arg)
6867 "Insert a new subheading with TODO keyword or checkbox and demote it.
6868 Works for outline headings and for plain lists alike."
6870 (org-insert-todo-heading arg)
6872 ((org-on-heading-p) (org-do-demote))
6873 ((org-at-item-p) (org-indent-item))))
6875 ;;; Promotion and Demotion
6877 (defvar org-after-demote-entry-hook nil
6878 "Hook run after an entry has been demoted.
6879 The cursor will be at the beginning of the entry.
6880 When a subtree is being demoted, the hook will be called for each node.")
6882 (defvar org-after-promote-entry-hook nil
6883 "Hook run after an entry has been promoted.
6884 The cursor will be at the beginning of the entry.
6885 When a subtree is being promoted, the hook will be called for each node.")
6887 (defun org-promote-subtree ()
6888 "Promote the entire subtree.
6889 See also `org-promote'."
6892 (org-map-tree 'org-promote))
6893 (org-fix-position-after-promote))
6895 (defun org-demote-subtree ()
6896 "Demote the entire subtree. See `org-demote'.
6897 See also `org-promote'."
6900 (org-map-tree 'org-demote))
6901 (org-fix-position-after-promote))
6904 (defun org-do-promote ()
6905 "Promote the current heading higher up the tree.
6906 If the region is active in `transient-mark-mode', promote all headings
6910 (if (org-region-active-p)
6911 (org-map-region 'org-promote (region-beginning) (region-end))
6913 (org-fix-position-after-promote))
6915 (defun org-do-demote ()
6916 "Demote the current heading lower down the tree.
6917 If the region is active in `transient-mark-mode', demote all headings
6921 (if (org-region-active-p)
6922 (org-map-region 'org-demote (region-beginning) (region-end))
6924 (org-fix-position-after-promote))
6926 (defun org-fix-position-after-promote ()
6927 "Make sure that after pro/demotion cursor position is right."
6928 (let ((pos (point)))
6929 (when (save-excursion
6930 (beginning-of-line 1)
6931 (looking-at org-todo-line-regexp)
6932 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
6933 (cond ((eobp) (insert " "))
6934 ((eolp) (insert " "))
6935 ((equal (char-after) ?\ ) (forward-char 1))))))
6937 (defun org-current-level ()
6938 "Return the level of the current entry, or nil if before the first headline.
6939 The level is the number of stars at the beginning of the headline."
6943 (org-back-to-heading t)
6944 (funcall outline-level))
6947 (defun org-get-previous-line-level ()
6948 "Return the outline depth of the last headline before the current line.
6949 Returns 0 for the first headline in the buffer, and nil if before the
6951 (let ((current-level (org-current-level))
6952 (prev-level (when (> (line-number-at-pos) 1)
6954 (beginning-of-line 0)
6955 (org-current-level)))))
6956 (cond ((null current-level) nil) ; Before first headline
6957 ((null prev-level) 0) ; At first headline
6960 (defun org-reduced-level (l)
6961 "Compute the effective level of a heading.
6962 This takes into account the setting of `org-odd-levels-only'."
6963 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
6965 (defun org-level-increment ()
6966 "Return the number of stars that will be added or removed at a
6967 time to headlines when structure editing, based on the value of
6968 `org-odd-levels-only'."
6969 (if org-odd-levels-only 2 1))
6971 (defun org-get-valid-level (level &optional change)
6972 "Rectify a level change under the influence of `org-odd-levels-only'
6973 LEVEL is a current level, CHANGE is by how much the level should be
6974 modified. Even if CHANGE is nil, LEVEL may be returned modified because
6975 even level numbers will become the next higher odd number."
6976 (if org-odd-levels-only
6977 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
6978 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
6979 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
6980 (max 1 (+ level (or change 0)))))
6982 (if (boundp 'define-obsolete-function-alias)
6983 (if (or (featurep 'xemacs) (< emacs-major-version 23))
6984 (define-obsolete-function-alias 'org-get-legal-level
6985 'org-get-valid-level)
6986 (define-obsolete-function-alias 'org-get-legal-level
6987 'org-get-valid-level "23.1")))
6989 (defun org-promote ()
6990 "Promote the current heading higher up the tree.
6991 If the region is active in `transient-mark-mode', promote all headings
6993 (org-back-to-heading t)
6994 (let* ((level (save-match-data (funcall outline-level)))
6995 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
6996 (diff (abs (- level (length up-head) -1))))
6997 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
6998 (replace-match up-head nil t)
6999 ;; Fixup tag positioning
7000 (and org-auto-align-tags (org-set-tags nil t))
7001 (if org-adapt-indentation (org-fixup-indentation (- diff)))
7002 (run-hooks 'org-after-promote-entry-hook)))
7004 (defun org-demote ()
7005 "Demote the current heading lower down the tree.
7006 If the region is active in `transient-mark-mode', demote all headings
7008 (org-back-to-heading t)
7009 (let* ((level (save-match-data (funcall outline-level)))
7010 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7011 (diff (abs (- level (length down-head) -1))))
7012 (replace-match down-head nil t)
7013 ;; Fixup tag positioning
7014 (and org-auto-align-tags (org-set-tags nil t))
7015 (if org-adapt-indentation (org-fixup-indentation diff))
7016 (run-hooks 'org-after-demote-entry-hook)))
7018 (defun org-cycle-level ()
7019 "Cycle the level of an empty headline through possible states.
7020 This goes first to child, then to parent, level, then up the hierarchy.
7021 After top level, it switches back to sibling level."
7023 (let ((org-adapt-indentation nil))
7024 (when (org-point-at-end-of-empty-headline)
7025 (setq this-command 'org-cycle-level) ; Only needed for caching
7026 (let ((cur-level (org-current-level))
7027 (prev-level (org-get-previous-line-level)))
7029 ;; If first headline in file, promote to top-level.
7031 (loop repeat (/ (- cur-level 1) (org-level-increment))
7032 do (org-do-promote)))
7033 ;; If same level as prev, demote one.
7034 ((= prev-level cur-level)
7036 ;; If parent is top-level, promote to top level if not already.
7038 (loop repeat (/ (- cur-level 1) (org-level-increment))
7039 do (org-do-promote)))
7040 ;; If top-level, return to prev-level.
7042 (loop repeat (/ (- prev-level 1) (org-level-increment))
7043 do (org-do-demote)))
7044 ;; If less than prev-level, promote one.
7045 ((< cur-level prev-level)
7047 ;; If deeper than prev-level, promote until higher than
7049 ((> cur-level prev-level)
7050 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7051 do (org-do-promote))))
7054 (defun org-map-tree (fun)
7055 "Call FUN for every heading underneath the current one."
7056 (org-back-to-heading)
7057 (let ((level (funcall outline-level)))
7061 (outline-next-heading)
7062 (> (funcall outline-level) level))
7066 (defun org-map-region (fun beg end)
7067 "Call FUN for every heading between BEG and END."
7068 (let ((org-ignore-region t))
7070 (setq end (copy-marker end))
7072 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
7076 (outline-next-heading)
7081 (defun org-fixup-indentation (diff)
7082 "Change the indentation in the current entry by DIFF.
7083 However, if any line in the current entry has no indentation, or if it
7084 would end up with no indentation after the change, nothing at all is done."
7086 (let ((end (save-excursion (outline-next-heading)
7088 (prohibit (if (> diff 0)
7090 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7092 (unless (save-excursion (end-of-line 1)
7093 (re-search-forward prohibit end t))
7094 (while (and (< (point) end)
7095 (re-search-forward "^[ \t]+" end t))
7096 (goto-char (match-end 0))
7097 (setq col (current-column))
7098 (if (< diff 0) (replace-match ""))
7099 (org-indent-to-column (+ diff col))))
7100 (move-marker end nil))))
7102 (defun org-convert-to-odd-levels ()
7103 "Convert an org-mode file with all levels allowed to one with odd levels.
7104 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7107 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7108 (let ((outline-regexp org-outline-regexp)
7109 (outline-level 'org-outline-level)
7110 (org-odd-levels-only nil) n)
7112 (goto-char (point-min))
7113 (while (re-search-forward "^\\*\\*+ " nil t)
7114 (setq n (- (length (match-string 0)) 2))
7115 (while (>= (setq n (1- n)) 0)
7117 (end-of-line 1))))))
7119 (defun org-convert-to-oddeven-levels ()
7120 "Convert an org-mode file with only odd levels to one with odd/even levels.
7121 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7122 file contains a section with an even level, conversion would
7123 destroy the structure of the file. An error is signaled in this
7126 (goto-char (point-min))
7127 ;; First check if there are no even levels
7128 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7129 (org-show-context t)
7130 (error "Not all levels are odd in this file. Conversion not possible"))
7131 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7132 (let ((outline-regexp org-outline-regexp)
7133 (outline-level 'org-outline-level)
7134 (org-odd-levels-only nil) n)
7136 (goto-char (point-min))
7137 (while (re-search-forward "^\\*\\*+ " nil t)
7138 (setq n (/ (1- (length (match-string 0))) 2))
7139 (while (>= (setq n (1- n)) 0)
7141 (end-of-line 1))))))
7143 (defun org-tr-level (n)
7144 "Make N odd if required."
7145 (if org-odd-levels-only (1+ (/ n 2)) n))
7147 ;;; Vertical tree motion, cutting and pasting of subtrees
7149 (defun org-move-subtree-up (&optional arg)
7150 "Move the current subtree up past ARG headlines of the same level."
7152 (org-move-subtree-down (- (prefix-numeric-value arg))))
7154 (defun org-move-subtree-down (&optional arg)
7155 "Move the current subtree down past ARG headlines of the same level."
7157 (setq arg (prefix-numeric-value arg))
7158 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7159 'org-get-last-sibling))
7160 (ins-point (make-marker))
7162 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7164 (org-back-to-heading)
7167 (setq ne-beg (org-back-over-empty-lines))
7170 (save-excursion (outline-end-of-heading)
7171 (setq folded (org-invisible-p)))
7172 (outline-end-of-subtree))
7173 (outline-next-heading)
7174 (setq ne-end (org-back-over-empty-lines))
7177 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7178 ;; include less whitespace
7181 (forward-line (- ne-beg ne-end))
7182 (setq beg (point))))
7183 ;; Find insertion point, with error handling
7185 (or (and (funcall movfunc) (looking-at outline-regexp))
7186 (progn (goto-char beg0)
7187 (error "Cannot move past superior level or buffer limit")))
7188 (setq cnt (1- cnt)))
7190 ;; Moving forward - still need to move over subtree
7191 (progn (org-end-of-subtree t t)
7193 (org-back-over-empty-lines)
7194 (or (bolp) (newline)))))
7195 (setq ne-ins (org-back-over-empty-lines))
7196 (move-marker ins-point (point))
7197 (setq txt (buffer-substring beg end))
7198 (org-save-markers-in-region beg end)
7199 (delete-region beg end)
7200 (org-remove-empty-overlays-at beg)
7201 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7202 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7203 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7204 (let ((bbb (point)))
7205 (insert-before-markers txt)
7206 (org-reinstall-markers-in-region bbb)
7207 (move-marker ins-point bbb))
7208 (or (bolp) (insert "\n"))
7209 (setq ins-end (point))
7210 (goto-char ins-point)
7211 (org-skip-whitespace)
7212 (when (and (< arg 0)
7213 (org-first-sibling-p)
7215 ;; Move whitespace back to beginning
7218 (let ((kill-whole-line t))
7219 (kill-line (- ne-ins ne-beg)) (point)))
7220 (insert (make-string (- ne-ins ne-beg) ?\n)))
7221 (move-marker ins-point nil)
7226 (org-cycle-hide-drawers 'children))
7227 (org-clean-visibility-after-subtree-move)))
7229 (defvar org-subtree-clip ""
7230 "Clipboard for cut and paste of subtrees.
7231 This is actually only a copy of the kill, because we use the normal kill
7232 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7234 (defvar org-subtree-clip-folded nil
7235 "Was the last copied subtree folded?
7236 This is used to fold the tree back after pasting.")
7238 (defun org-cut-subtree (&optional n)
7239 "Cut the current subtree into the clipboard.
7240 With prefix arg N, cut this many sequential subtrees.
7241 This is a short-hand for marking the subtree and then cutting it."
7243 (org-copy-subtree n 'cut))
7245 (defun org-copy-subtree (&optional n cut force-store-markers)
7246 "Cut the current subtree into the clipboard.
7247 With prefix arg N, cut this many sequential subtrees.
7248 This is a short-hand for marking the subtree and then copying it.
7249 If CUT is non-nil, actually cut the subtree.
7250 If FORCE-STORE-MARKERS is non-nil, store the relative locations
7251 of some markers in the region, even if CUT is non-nil. This is
7252 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
7254 (let (beg end folded (beg0 (point)))
7256 (org-back-to-heading nil) ; take what looks like a subtree
7257 (org-back-to-heading t)) ; take what is really there
7258 (org-back-over-empty-lines)
7260 (skip-chars-forward " \t\r\n")
7262 (save-excursion (outline-end-of-heading)
7263 (setq folded (org-invisible-p)))
7265 (org-forward-same-level (1- n) t)
7267 (org-end-of-subtree t t))
7268 (org-back-over-empty-lines)
7272 (setq org-subtree-clip-folded folded)
7273 (when (or cut force-store-markers)
7274 (org-save-markers-in-region beg end))
7275 (if cut (kill-region beg end) (copy-region-as-kill beg end))
7276 (setq org-subtree-clip (current-kill 0))
7277 (message "%s: Subtree(s) with %d characters"
7278 (if cut "Cut" "Copied")
7279 (length org-subtree-clip)))))
7281 (defun org-paste-subtree (&optional level tree for-yank)
7282 "Paste the clipboard as a subtree, with modification of headline level.
7283 The entire subtree is promoted or demoted in order to match a new headline
7286 If the cursor is at the beginning of a headline, the same level as
7287 that headline is used to paste the tree
7289 If not, the new level is derived from the *visible* headings
7290 before and after the insertion point, and taken to be the inferior headline
7291 level of the two. So if the previous visible heading is level 3 and the
7292 next is level 4 (or vice versa), level 4 will be used for insertion.
7293 This makes sure that the subtree remains an independent subtree and does
7294 not swallow low level entries.
7296 You can also force a different level, either by using a numeric prefix
7297 argument, or by inserting the heading marker by hand. For example, if the
7298 cursor is after \"*****\", then the tree will be shifted to level 5.
7300 If optional TREE is given, use this text instead of the kill ring.
7302 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
7303 move back over whitespace before inserting, and move point to the end of
7304 the inserted text when done."
7306 (setq tree (or tree (and kill-ring (current-kill 0))))
7307 (unless (org-kill-is-subtree-p tree)
7309 (substitute-command-keys
7310 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
7311 (let* ((visp (not (org-invisible-p)))
7313 (^re (concat "^\\(" outline-regexp "\\)"))
7314 (re (concat "\\(" outline-regexp "\\)"))
7315 (^re_ (concat "\\(\\*+\\)[ \t]*"))
7317 (old-level (if (string-match ^re txt)
7318 (- (match-end 0) (match-beginning 0) 1)
7320 (force-level (cond (level (prefix-numeric-value level))
7321 ((and (looking-at "[ \t]*$")
7323 ^re_ (buffer-substring
7324 (point-at-bol) (point))))
7325 (- (match-end 1) (match-beginning 1)))
7327 (looking-at org-outline-regexp))
7328 (- (match-end 0) (point) 1))
7330 (previous-level (save-excursion
7333 (outline-previous-visible-heading 1)
7335 (- (match-end 0) (match-beginning 0) 1)
7338 (next-level (save-excursion
7341 (or (looking-at outline-regexp)
7342 (outline-next-visible-heading 1))
7344 (- (match-end 0) (match-beginning 0) 1)
7347 (new-level (or force-level (max previous-level next-level)))
7348 (shift (if (or (= old-level -1)
7350 (= old-level new-level))
7352 (- new-level old-level)))
7353 (delta (if (> shift 0) -1 1))
7354 (func (if (> shift 0) 'org-demote 'org-promote))
7355 (org-odd-levels-only nil)
7357 ;; Remove the forced level indicator
7359 (delete-region (point-at-bol) (point)))
7361 (beginning-of-line 1)
7362 (unless for-yank (org-back-over-empty-lines))
7364 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
7365 (insert-before-markers txt)
7366 (unless (string-match "\n\\'" txt) (insert "\n"))
7367 (setq newend (point))
7368 (org-reinstall-markers-in-region beg)
7371 (skip-chars-forward " \t\n\r")
7373 (if (and (org-invisible-p) visp)
7374 (save-excursion (outline-show-heading)))
7375 ;; Shift if necessary
7378 (narrow-to-region beg end)
7379 (while (not (= shift 0))
7380 (org-map-region func (point-min) (point-max))
7381 (setq shift (+ delta shift)))
7382 (goto-char (point-min))
7383 (setq newend (point-max))))
7384 (when (or (interactive-p) for-yank)
7385 (message "Clipboard pasted as level %d subtree" new-level))
7386 (if (and (not for-yank) ; in this case, org-yank will decide about folding
7388 (eq org-subtree-clip (current-kill 0))
7389 org-subtree-clip-folded)
7390 ;; The tree was folded before it was killed/copied
7392 (and for-yank (goto-char newend))))
7394 (defun org-kill-is-subtree-p (&optional txt)
7395 "Check if the current kill is an outline subtree, or a set of trees.
7396 Returns nil if kill does not start with a headline, or if the first
7397 headline level is not the largest headline level in the tree.
7398 So this will actually accept several entries of equal levels as well,
7399 which is OK for `org-paste-subtree'.
7400 If optional TXT is given, check this string instead of the current kill."
7401 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
7402 (start-level (and kill
7403 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
7404 org-outline-regexp "\\)")
7406 (- (match-end 2) (match-beginning 2) 1)))
7407 (re (concat "^" org-outline-regexp))
7408 (start (1+ (or (match-beginning 2) -1))))
7409 (if (not start-level)
7411 nil) ;; does not even start with a heading
7413 (while (setq start (string-match re kill (1+ start)))
7414 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
7418 (defvar org-markers-to-move nil
7419 "Markers that should be moved with a cut-and-paste operation.
7420 Those markers are stored together with their positions relative to
7421 the start of the region.")
7423 (defun org-save-markers-in-region (beg end)
7424 "Check markers in region.
7425 If these markers are between BEG and END, record their position relative
7426 to BEG, so that after moving the block of text, we can put the markers back
7428 This function gets called just before an entry or tree gets cut from the
7429 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
7430 called immediately, to move the markers with the entries."
7431 (setq org-markers-to-move nil)
7432 (when (featurep 'org-clock)
7433 (org-clock-save-markers-for-cut-and-paste beg end))
7434 (when (featurep 'org-agenda)
7435 (org-agenda-save-markers-for-cut-and-paste beg end)))
7437 (defun org-check-and-save-marker (marker beg end)
7438 "Check if MARKER is between BEG and END.
7439 If yes, remember the marker and the distance to BEG."
7440 (when (and (marker-buffer marker)
7441 (equal (marker-buffer marker) (current-buffer)))
7442 (if (and (>= marker beg) (< marker end))
7443 (push (cons marker (- marker beg)) org-markers-to-move))))
7445 (defun org-reinstall-markers-in-region (beg)
7446 "Move all remembered markers to their position relative to BEG."
7448 (move-marker (car x) (+ beg (cdr x))))
7449 org-markers-to-move)
7450 (setq org-markers-to-move nil))
7452 (defun org-narrow-to-subtree ()
7453 "Narrow buffer to the current subtree."
7458 (progn (org-back-to-heading t) (point))
7459 (progn (org-end-of-subtree t t)
7460 (if (org-on-heading-p) (backward-char 1))
7464 (defvar org-property-drawer-re))
7466 (defun org-clone-subtree-with-time-shift (n &optional shift)
7467 "Clone the task (subtree) at point N times.
7468 The clones will be inserted as siblings.
7470 In interactive use, the user will be prompted for the number of
7471 clones to be produced, and for a time SHIFT, which may be a
7472 repeater as used in time stamps, for example `+3d'.
7474 When a valid repeater is given and the entry contains any time
7475 stamps, the clones will become a sequence in time, with time
7476 stamps in the subtree shifted for each clone produced. If SHIFT
7477 is nil or the empty string, time stamps will be left alone. The
7478 ID property of the original subtree is removed.
7480 If the original subtree did contain time stamps with a repeater,
7481 the following will happen:
7482 - the repeater will be removed in each clone
7483 - an additional clone will be produced, with the current, unshifted
7484 date(s) in the entry.
7485 - the original entry will be placed *after* all the clones, with
7487 - the start days in the repeater in the original entry will be shifted
7488 to past the last clone.
7489 I this way you can spell out a number of instances of a repeating task,
7490 and still retain the repeater to cover future instances of the task."
7491 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
7492 (let (beg end template task idprop
7493 shift-n shift-what doshift nmin nmax (n-no-remove -1))
7494 (if (not (and (integerp n) (> n 0)))
7495 (error "Invalid number of replications %s" n))
7496 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
7497 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
7499 (error "Invalid shift specification %s" shift))
7501 (setq shift-n (string-to-number (match-string 1 shift))
7502 shift-what (cdr (assoc (match-string 2 shift)
7503 '(("d" . day) ("w" . week)
7504 ("m" . month) ("y" . year))))))
7505 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
7506 (setq nmin 1 nmax n)
7507 (org-back-to-heading t)
7509 (setq idprop (org-entry-get nil "ID"))
7510 (org-end-of-subtree t t)
7511 (or (bolp) (insert "\n"))
7513 (setq template (buffer-substring beg end))
7515 (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
7516 (delete-region beg end)
7518 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
7520 (loop for n from nmin to nmax do
7525 (goto-char (point-min))
7526 (and idprop (if org-clone-delete-id
7527 (org-entry-delete nil "ID")
7528 (org-id-get-create t)))
7529 (while (re-search-forward org-property-drawer-re nil t)
7530 (org-remove-empty-drawer-at "PROPERTIES" (point)))
7531 (goto-char (point-min))
7533 (while (re-search-forward org-ts-regexp-both nil t)
7534 (org-timestamp-change (* n shift-n) shift-what))
7535 (unless (= n n-no-remove)
7536 (goto-char (point-min))
7537 (while (re-search-forward org-ts-regexp nil t)
7539 (goto-char (match-beginning 0))
7540 (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
7541 (delete-region (match-beginning 1) (match-end 1)))))))
7542 (setq task (buffer-string)))
7548 (defun org-sort (with-case)
7549 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
7550 Optional argument WITH-CASE means sort case-sensitively.
7551 With a double prefix argument, also remove duplicate entries."
7554 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
7555 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
7557 (org-call-with-arg 'org-sort-entries with-case))))
7559 (defun org-sort-remove-invisible (s)
7560 (remove-text-properties 0 (length s) org-rm-props s)
7561 (while (string-match org-bracket-link-regexp s)
7562 (setq s (replace-match (if (match-end 2)
7564 (match-string 1 s)) t t s)))
7567 (defvar org-priority-regexp) ; defined later in the file
7569 (defvar org-after-sorting-entries-or-items-hook nil
7570 "Hook that is run after a bunch of entries or items have been sorted.
7571 When children are sorted, the cursor is in the parent line when this
7572 hook gets called. When a region or a plain list is sorted, the cursor
7573 will be in the first entry of the sorted region/list.")
7575 (defun org-sort-entries
7576 (&optional with-case sorting-type getkey-func compare-func property)
7577 "Sort entries on a certain level of an outline tree.
7578 If there is an active region, the entries in the region are sorted.
7579 Else, if the cursor is before the first entry, sort the top-level items.
7580 Else, the children of the entry at point are sorted.
7582 Sorting can be alphabetically, numerically, by date/time as given by
7583 a time stamp, by a property or by priority.
7585 The command prompts for the sorting type unless it has been given to the
7586 function through the SORTING-TYPE argument, which needs to be a character,
7587 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
7588 precise meaning of each character:
7590 n Numerically, by converting the beginning of the entry/item to a number.
7591 a Alphabetically, ignoring the TODO keyword and the priority, if any.
7592 t By date/time, either the first active time stamp in the entry, or, if
7593 none exist, by the first inactive one.
7594 s By the scheduled date/time.
7595 d By deadline date/time.
7596 c By creation time, which is assumed to be the first inactive time stamp
7597 at the beginning of a line.
7598 p By priority according to the cookie.
7599 r By the value of a property.
7601 Capital letters will reverse the sort order.
7603 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
7604 called with point at the beginning of the record. It must return either
7605 a string or a number that should serve as the sorting key for that record.
7607 Comparing entries ignores case by default. However, with an optional argument
7608 WITH-CASE, the sorting considers case as well."
7610 (let ((case-func (if with-case 'identity 'downcase))
7611 start beg end stars re re2
7613 ;; Find beginning and end of region to sort
7615 ((org-region-active-p)
7616 ;; we will sort the region
7617 (setq end (region-end)
7619 (goto-char (region-beginning))
7620 (if (not (org-on-heading-p)) (outline-next-heading))
7621 (setq start (point)))
7622 ((or (org-on-heading-p)
7623 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
7624 ;; we will sort the children of the current headline
7625 (org-back-to-heading)
7627 end (progn (org-end-of-subtree t t)
7628 (or (bolp) (insert "\n"))
7629 (org-back-over-empty-lines)
7634 (outline-next-heading))
7636 ;; we will sort the top-level entries in this file
7637 (goto-char (point-min))
7638 (or (org-on-heading-p) (outline-next-heading))
7639 (setq start (point))
7640 (goto-char (point-max))
7641 (beginning-of-line 1)
7642 (when (looking-at ".*?\\S-")
7643 ;; File ends in a non-white line
7646 (setq end (point-max))
7647 (setq what "top-level")
7652 (if (>= beg end) (error "Nothing to sort"))
7654 (looking-at "\\(\\*+\\)")
7655 (setq stars (match-string 1)
7656 re (concat "^" (regexp-quote stars) " +")
7657 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
7658 txt (buffer-substring beg end))
7659 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
7660 (if (and (not (equal stars "*")) (string-match re2 txt))
7661 (error "Region to sort contains a level above the first entry"))
7663 (unless sorting-type
7665 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
7666 [t]ime
[s]cheduled [d]eadline [c]reated
7667 A/N/T/S/D/C/P/O/F means reversed:"
7669 (setq sorting-type (read-char-exclusive))
7671 (and (= (downcase sorting-type) ?f)
7673 (org-icompleting-read "Sort using function: "
7674 obarray 'fboundp t nil nil))
7675 (setq getkey-func (intern getkey-func)))
7677 (and (= (downcase sorting-type) ?r)
7679 (org-icompleting-read "Property: "
7680 (mapcar 'list (org-buffer-property-keys t))
7683 (message "Sorting entries...")
7686 (narrow-to-region start end)
7687 (let ((dcst (downcase sorting-type))
7688 (case-fold-search nil)
7689 (now (current-time)))
7691 (/= dcst sorting-type)
7692 ;; This function moves to the beginning character of the "record" to
7695 (if (re-search-forward re nil t)
7696 (goto-char (match-beginning 0))
7697 (goto-char (point-max))))
7698 ;; This function moves to the last character of the "record" being
7703 (outline-forward-same-level 1)
7705 (goto-char (point-max))))))
7706 ;; This function returns the value that gets sorted against.
7710 (if (looking-at org-complex-heading-regexp)
7711 (string-to-number (match-string 4))
7714 (if (looking-at org-complex-heading-regexp)
7715 (funcall case-func (match-string 4))
7718 (let ((end (save-excursion (outline-next-heading) (point))))
7719 (if (or (re-search-forward org-ts-regexp end t)
7720 (re-search-forward org-ts-regexp-both end t))
7721 (org-time-string-to-seconds (match-string 0))
7722 (org-float-time now))))
7724 (let ((end (save-excursion (outline-next-heading) (point))))
7725 (if (re-search-forward
7726 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
7728 (org-time-string-to-seconds (match-string 0))
7729 (org-float-time now))))
7731 (let ((end (save-excursion (outline-next-heading) (point))))
7732 (if (re-search-forward org-scheduled-time-regexp end t)
7733 (org-time-string-to-seconds (match-string 1))
7734 (org-float-time now))))
7736 (let ((end (save-excursion (outline-next-heading) (point))))
7737 (if (re-search-forward org-deadline-time-regexp end t)
7738 (org-time-string-to-seconds (match-string 1))
7739 (org-float-time now))))
7741 (if (re-search-forward org-priority-regexp (point-at-eol) t)
7742 (string-to-char (match-string 2))
7743 org-default-priority))
7745 (or (org-entry-get nil property) ""))
7747 (if (looking-at org-complex-heading-regexp)
7748 (- 9999 (length (member (match-string 2)
7749 org-todo-keywords-1)))))
7753 (setq tmp (funcall getkey-func))
7754 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
7756 (error "Invalid key function `%s'" getkey-func)))
7757 (t (error "Invalid sorting type `%c'" sorting-type))))
7760 ((= dcst ?a) 'string<)
7761 ((= dcst ?f) compare-func)
7762 ((member dcst '(?p ?t ?s ?d ?c)) '<)
7764 (run-hooks 'org-after-sorting-entries-or-items-hook)
7765 (message "Sorting entries...done")))
7767 (defun org-do-sort (table what &optional with-case sorting-type)
7768 "Sort TABLE of WHAT according to SORTING-TYPE.
7769 The user will be prompted for the SORTING-TYPE if the call to this
7770 function does not specify it. WHAT is only for the prompt, to indicate
7771 what is being sorted. The sorting key will be extracted from
7772 the car of the elements of the table.
7773 If WITH-CASE is non-nil, the sorting will be case-sensitive."
7774 (unless sorting-type
7776 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
7778 (setq sorting-type (read-char-exclusive)))
7779 (let ((dcst (downcase sorting-type))
7780 extractfun comparefun)
7781 ;; Define the appropriate functions
7784 (setq extractfun 'string-to-number
7785 comparefun (if (= dcst sorting-type) '< '>)))
7787 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
7788 (lambda(x) (downcase (org-sort-remove-invisible x))))
7789 comparefun (if (= dcst sorting-type)
7791 (lambda (a b) (and (not (string< a b))
7792 (not (string= a b)))))))
7796 (if (or (string-match org-ts-regexp x)
7797 (string-match org-ts-regexp-both x))
7799 (org-time-string-to-time (match-string 0 x)))
7801 comparefun (if (= dcst sorting-type) '< '>)))
7802 (t (error "Invalid sorting type `%c'" sorting-type)))
7804 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
7806 (lambda (a b) (funcall comparefun (car a) (car b))))))
7809 ;;; The orgstruct minor mode
7811 ;; Define a minor mode which can be used in other modes in order to
7812 ;; integrate the org-mode structure editing commands.
7814 ;; This is really a hack, because the org-mode structure commands use
7815 ;; keys which normally belong to the major mode. Here is how it
7816 ;; works: The minor mode defines all the keys necessary to operate the
7817 ;; structure commands, but wraps the commands into a function which
7818 ;; tests if the cursor is currently at a headline or a plain list
7819 ;; item. If that is the case, the structure command is used,
7820 ;; temporarily setting many Org-mode variables like regular
7821 ;; expressions for filling etc. However, when any of those keys is
7822 ;; used at a different location, function uses `key-binding' to look
7823 ;; up if the key has an associated command in another currently active
7824 ;; keymap (minor modes, major mode, global), and executes that
7825 ;; command. There might be problems if any of the keys is otherwise
7826 ;; used as a prefix key.
7828 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
7829 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
7830 ;; addresses this by checking explicitly for both bindings.
7832 (defvar orgstruct-mode-map (make-sparse-keymap)
7833 "Keymap for the minor `orgstruct-mode'.")
7835 (defvar org-local-vars nil
7836 "List of local variables, for use by `orgstruct-mode'.")
7839 (define-minor-mode orgstruct-mode
7840 "Toggle the minor mode `orgstruct-mode'.
7841 This mode is for using Org-mode structure commands in other
7842 modes. The following keys behave as if Org-mode were active, if
7843 the cursor is on a headline, or on a plain list item (both as
7844 defined by Org-mode).
7846 M-up Move entry/item up
7847 M-down Move entry/item down
7850 M-S-up Move entry/item up
7851 M-S-down Move entry/item down
7852 M-S-left Promote subtree
7853 M-S-right Demote subtree
7854 M-q Fill paragraph and items like in Org-mode
7856 C-c - Cycle list bullet
7857 TAB Cycle item visibility
7858 M-RET Insert new heading/item
7859 S-M-RET Insert new TODO heading / Checkbox item
7860 C-c C-c Set tags / toggle checkbox"
7861 nil " OrgStruct" nil
7862 (org-load-modules-maybe)
7863 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
7866 (defun turn-on-orgstruct ()
7867 "Unconditionally turn on `orgstruct-mode'."
7870 (defun orgstruct++-mode (&optional arg)
7871 "Toggle `orgstruct-mode', the enhanced version of it.
7872 In addition to setting orgstruct-mode, this also exports all indentation
7873 and autofilling variables from org-mode into the buffer. It will also
7874 recognize item context in multiline items.
7875 Note that turning off orgstruct-mode will *not* remove the
7876 indentation/paragraph settings. This can only be done by refreshing the
7877 major mode, for example with \\[normal-mode]."
7879 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
7887 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7888 (symbol-name (car x)))
7889 (setq var (car x) val (nth 1 x))
7890 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
7892 (org-set-local 'orgstruct-is-++ t))))
7894 (defvar orgstruct-is-++ nil
7895 "Is `orgstruct-mode' in ++ version in the current-buffer?")
7896 (make-variable-buffer-local 'orgstruct-is-++)
7899 (defun turn-on-orgstruct++ ()
7900 "Unconditionally turn on `orgstruct++-mode'."
7901 (orgstruct++-mode 1))
7903 (defun orgstruct-error ()
7904 "Error when there is no default binding for a structure key."
7906 (error "This key has no function outside structure elements"))
7908 (defun orgstruct-setup ()
7909 "Setup orgstruct keymaps."
7913 '([(meta up)] org-metaup)
7914 '([(meta down)] org-metadown)
7915 '([(meta left)] org-metaleft)
7916 '([(meta right)] org-metaright)
7917 '([(meta shift up)] org-shiftmetaup)
7918 '([(meta shift down)] org-shiftmetadown)
7919 '([(meta shift left)] org-shiftmetaleft)
7920 '([(meta shift right)] org-shiftmetaright)
7921 '([?\e (up)] org-metaup)
7922 '([?\e (down)] org-metadown)
7923 '([?\e (left)] org-metaleft)
7924 '([?\e (right)] org-metaright)
7925 '([?\e (shift up)] org-shiftmetaup)
7926 '([?\e (shift down)] org-shiftmetadown)
7927 '([?\e (shift left)] org-shiftmetaleft)
7928 '([?\e (shift right)] org-shiftmetaright)
7929 '([(shift up)] org-shiftup)
7930 '([(shift down)] org-shiftdown)
7931 '([(shift left)] org-shiftleft)
7932 '([(shift right)] org-shiftright)
7933 '("\C-c\C-c" org-ctrl-c-ctrl-c)
7934 '("\M-q" fill-paragraph)
7936 '("\C-c-" org-cycle-list-bullet)))
7938 (while (setq elt (pop bindings))
7939 (setq nfunc (1+ nfunc))
7940 (setq key (org-key (car elt))
7942 cmd (orgstruct-make-binding fun nfunc key))
7943 (org-defkey orgstruct-mode-map key cmd))
7945 ;; Special treatment needed for TAB and RET
7946 (org-defkey orgstruct-mode-map [(tab)]
7947 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
7948 (org-defkey orgstruct-mode-map "\C-i"
7949 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
7951 (org-defkey orgstruct-mode-map "\M-\C-m"
7952 (orgstruct-make-binding 'org-insert-heading 105
7953 "\M-\C-m" [(meta return)]))
7954 (org-defkey orgstruct-mode-map [(meta return)]
7955 (orgstruct-make-binding 'org-insert-heading 106
7956 [(meta return)] "\M-\C-m"))
7958 (org-defkey orgstruct-mode-map [(shift meta return)]
7959 (orgstruct-make-binding 'org-insert-todo-heading 107
7960 [(meta return)] "\M-\C-m"))
7962 (org-defkey orgstruct-mode-map "\e\C-m"
7963 (orgstruct-make-binding 'org-insert-heading 108
7964 "\e\C-m" [?\e (return)]))
7965 (org-defkey orgstruct-mode-map [?\e (return)]
7966 (orgstruct-make-binding 'org-insert-heading 109
7967 [?\e (return)] "\e\C-m"))
7968 (org-defkey orgstruct-mode-map [?\e (shift return)]
7969 (orgstruct-make-binding 'org-insert-todo-heading 110
7970 [?\e (return)] "\e\C-m"))
7972 (unless org-local-vars
7973 (setq org-local-vars (org-get-local-variables)))
7977 (defun orgstruct-make-binding (fun n &rest keys)
7978 "Create a function for binding in the structure minor mode.
7979 FUN is the command to call inside a table. N is used to create a unique
7980 command name. KEYS are keys that should be checked in for a command
7981 to execute outside of tables."
7984 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
7986 (concat "In Structure, run `" (symbol-name fun) "'.\n"
7987 "Outside of structure, run the binding of `"
7988 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
7992 `(org-context-p 'headline 'item
7993 (and orgstruct-is-++
7994 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
7996 (list 'org-run-like-in-org-mode (list 'quote fun))
7997 (list 'let '(orgstruct-mode)
7998 (list 'call-interactively
8001 (list 'key-binding k))
8003 '('orgstruct-error))))))))
8005 (defun org-context-p (&rest contexts)
8006 "Check if local context is any of CONTEXTS.
8007 Possible values in the list of contexts are `table', `headline', and `item'."
8008 (let ((pos (point)))
8009 (goto-char (point-at-bol))
8010 (prog1 (or (and (memq 'table contexts)
8011 (looking-at "[ \t]*|"))
8012 (and (memq 'headline contexts)
8013 ;;????????? (looking-at "\\*+"))
8014 (looking-at outline-regexp))
8015 (and (memq 'item contexts)
8016 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
8017 (and (memq 'item-body contexts)
8021 (defun org-get-local-variables ()
8022 "Return a list of all local variables in an org-mode buffer."
8024 (with-current-buffer (get-buffer-create "*Org tmp*")
8027 (setq varlist (buffer-local-variables)))
8028 (kill-buffer "*Org tmp*")
8035 (list (car x) (list 'quote (cdr x)))))
8037 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
8038 (symbol-name (car x)))
8043 (defun org-run-like-in-org-mode (cmd)
8044 "Run a command, pretending that the current buffer is in Org-mode.
8045 This will temporarily bind local variables that are typically bound in
8046 Org-mode to the values they have in Org-mode, and then interactively
8048 (org-load-modules-maybe)
8049 (unless org-local-vars
8050 (setq org-local-vars (org-get-local-variables)))
8051 (eval (list 'let org-local-vars
8052 (list 'call-interactively (list 'quote cmd)))))
8056 (defun org-get-category (&optional pos)
8057 "Get the category applying to position POS."
8058 (get-text-property (or pos (point)) 'org-category))
8060 (defun org-refresh-category-properties ()
8061 "Refresh category text properties in the buffer."
8062 (let ((def-cat (cond
8063 ((null org-category)
8064 (if buffer-file-name
8065 (file-name-sans-extension
8066 (file-name-nondirectory buffer-file-name))
8068 ((symbolp org-category) (symbol-name org-category))
8070 beg end cat pos optionp)
8075 (goto-char (point-min))
8076 (put-text-property (point) (point-max) 'org-category def-cat)
8077 (while (re-search-forward
8078 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8079 (setq pos (match-end 0)
8080 optionp (equal (char-after (match-beginning 0)) ?#)
8081 cat (org-trim (match-string 2)))
8083 (setq beg (point-at-bol) end (point-max))
8084 (org-back-to-heading t)
8085 (setq beg (point) end (org-end-of-subtree t t)))
8086 (put-text-property beg end 'org-category cat)
8087 (goto-char pos)))))))
8092 ;;; Link abbreviations
8094 (defun org-link-expand-abbrev (link)
8095 "Apply replacements as defined in `org-link-abbrev-alist."
8096 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
8097 (let* ((key (match-string 1 link))
8098 (as (or (assoc key org-link-abbrev-alist-local)
8099 (assoc key org-link-abbrev-alist)))
8100 (tag (and (match-end 2) (match-string 3 link)))
8106 ((symbolp rpl) (funcall rpl tag))
8107 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
8108 ((string-match "%h" rpl)
8109 (replace-match (url-hexify-string (or tag "")) t t rpl))
8110 (t (concat rpl tag)))))
8113 ;;; Storing and inserting links
8115 (defvar org-insert-link-history nil
8116 "Minibuffer history for links inserted with `org-insert-link'.")
8118 (defvar org-stored-links nil
8119 "Contains the links stored with `org-store-link'.")
8121 (defvar org-store-link-plist nil
8122 "Plist with info about the most recently link created with `org-store-link'.")
8124 (defvar org-link-protocols nil
8125 "Link protocols added to Org-mode using `org-add-link-type'.")
8127 (defvar org-store-link-functions nil
8128 "List of functions that are called to create and store a link.
8129 Each function will be called in turn until one returns a non-nil
8130 value. Each function should check if it is responsible for creating
8131 this link (for example by looking at the major mode).
8132 If not, it must exit and return nil.
8133 If yes, it should return a non-nil value after a calling
8134 `org-store-link-props' with a list of properties and values.
8135 Special properties are:
8137 :type The link prefix, like \"http\". This must be given.
8138 :link The link, like \"http://www.astro.uva.nl/~dominik\".
8139 This is obligatory as well.
8140 :description Optional default description for the second pair
8141 of brackets in an Org-mode link. The user can still change
8142 this when inserting this link into an Org-mode buffer.
8144 In addition to these, any additional properties can be specified
8145 and then used in remember templates.")
8147 (defun org-add-link-type (type &optional follow export)
8148 "Add TYPE to the list of `org-link-types'.
8149 Re-compute all regular expressions depending on `org-link-types'
8151 FOLLOW and EXPORT are two functions.
8153 FOLLOW should take the link path as the single argument and do whatever
8154 is necessary to follow the link, for example find a file or display
8157 EXPORT should format the link path for export to one of the export formats.
8158 It should be a function accepting three arguments:
8160 path the path of the link, the text after the prefix (like \"http:\")
8161 desc the description of the link, if any, nil if there was no description
8162 format the export format, a symbol like `html' or `latex' or `ascii'..
8164 The function may use the FORMAT information to return different values
8165 depending on the format. The return value will be put literally into
8166 the exported file. If the return value is nil, this means Org should
8167 do what it normally does with links which do not have EXPORT defined.
8169 Org-mode has a built-in default for exporting links. If you are happy with
8170 this default, there is no need to define an export function for the link
8171 type. For a simple example of an export function, see `org-bbdb.el'."
8172 (add-to-list 'org-link-types type t)
8173 (org-make-link-regexps)
8174 (if (assoc type org-link-protocols)
8175 (setcdr (assoc type org-link-protocols) (list follow export))
8176 (push (list type follow export) org-link-protocols)))
8178 (defvar org-agenda-buffer-name)
8181 (defun org-store-link (arg)
8182 "\\<org-mode-map>Store an org-link to the current location.
8183 This link is added to `org-stored-links' and can later be inserted
8184 into an org-buffer with \\[org-insert-link].
8186 For some link types, a prefix arg is interpreted:
8187 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
8188 For file links, arg negates `org-context-in-file-links'."
8190 (org-load-modules-maybe)
8191 (setq org-store-link-plist nil) ; reset
8192 (let ((outline-regexp (org-get-limited-outline-regexp))
8193 link cpltxt desc description search txt custom-id agenda-link)
8196 ((run-hook-with-args-until-success 'org-store-link-functions)
8197 (setq link (plist-get org-store-link-plist :link)
8198 desc (or (plist-get org-store-link-plist :description) link)))
8200 ((equal (buffer-name) "*Org Edit Src Example*")
8202 (while (or (not label)
8206 (goto-char (point-min))
8208 (regexp-quote (format org-coderef-label-format label))
8210 (when label (message "Label exists already") (sit-for 2))
8211 (setq label (read-string "Code line label: " label)))
8213 (setq link (format org-coderef-label-format label))
8214 (setq gc (- 79 (length link)))
8215 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
8217 (setq link (concat "(" label ")") desc nil)))
8219 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
8220 ;; We are in the agenda, link to referenced location
8221 (let ((m (or (get-text-property (point) 'org-hd-marker)
8222 (get-text-property (point) 'org-marker))))
8224 (org-with-point-at m
8227 (call-interactively 'org-store-link)
8228 (org-store-link nil)))))))
8230 ((eq major-mode 'calendar-mode)
8231 (let ((cd (calendar-cursor-to-date)))
8234 (car org-time-stamp-formats)
8236 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8238 (org-store-link-props :type "calendar" :date cd)))
8240 ((eq major-mode 'w3-mode)
8241 (setq cpltxt (if (and (buffer-name)
8242 (not (string-match "Untitled" (buffer-name))))
8245 link (org-make-link (url-view-url t)))
8246 (org-store-link-props :type "w3" :url (url-view-url t)))
8248 ((eq major-mode 'w3m-mode)
8249 (setq cpltxt (or w3m-current-title w3m-current-url)
8250 link (org-make-link w3m-current-url))
8251 (org-store-link-props :type "w3m" :url (url-view-url t)))
8253 ((setq search (run-hook-with-args-until-success
8254 'org-create-file-search-functions))
8255 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8257 (setq cpltxt (or description link)))
8259 ((eq major-mode 'image-mode)
8260 (setq cpltxt (concat "file:"
8261 (abbreviate-file-name buffer-file-name))
8262 link (org-make-link cpltxt))
8263 (org-store-link-props :type "image" :file buffer-file-name))
8265 ((eq major-mode 'dired-mode)
8266 ;; link to the file in the current line
8267 (let ((file (dired-get-filename nil t)))
8269 (abbreviate-file-name
8270 (expand-file-name (dired-get-filename nil t)))
8271 ;; otherwise, no file so use current directory.
8273 (setq cpltxt (concat "file:" file)
8274 link (org-make-link cpltxt))))
8276 ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
8277 (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
8279 ((org-in-regexp "<<\\(.*?\\)>>")
8282 (abbreviate-file-name
8283 (buffer-file-name (buffer-base-buffer)))
8284 "::" (match-string 1))
8285 link (org-make-link cpltxt)))
8286 ((and (featurep 'org-id)
8287 (or (eq org-link-to-org-use-id t)
8288 (and (eq org-link-to-org-use-id 'create-if-interactive)
8290 (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
8293 (and org-link-to-org-use-id
8295 (org-entry-get nil "ID")
8297 ;; We can make a link using the ID.
8298 (setq link (condition-case nil
8299 (prog1 (org-id-store-link)
8300 (setq desc (plist-get org-store-link-plist
8303 ;; probably before first headline, link to file only
8305 (abbreviate-file-name
8306 (buffer-file-name (buffer-base-buffer))))))))
8308 ;; Just link to current headline
8309 (setq cpltxt (concat "file:"
8310 (abbreviate-file-name
8311 (buffer-file-name (buffer-base-buffer)))))
8312 ;; Add a context search string
8313 (when (org-xor org-context-in-file-links arg)
8315 ((org-on-heading-p) nil)
8316 ((org-region-active-p)
8317 (buffer-substring (region-beginning) (region-end)))
8319 (when (or (null txt) (string-match "\\S-" txt))
8323 (org-make-org-heading-search-string txt)
8325 desc (or (nth 4 (ignore-errors
8326 (org-heading-components))) "NONE"))))
8327 (if (string-match "::\\'" cpltxt)
8328 (setq cpltxt (substring cpltxt 0 -2)))
8329 (setq link (org-make-link cpltxt)))))
8331 ((buffer-file-name (buffer-base-buffer))
8332 ;; Just link to this file here.
8333 (setq cpltxt (concat "file:"
8334 (abbreviate-file-name
8335 (buffer-file-name (buffer-base-buffer)))))
8336 ;; Add a context string
8337 (when (org-xor org-context-in-file-links arg)
8338 (setq txt (if (org-region-active-p)
8339 (buffer-substring (region-beginning) (region-end))
8340 (buffer-substring (point-at-bol) (point-at-eol))))
8341 ;; Only use search option if there is some text.
8342 (when (string-match "\\S-" txt)
8344 (concat cpltxt "::" (org-make-org-heading-search-string txt))
8346 (setq link (org-make-link cpltxt)))
8349 (error "Cannot link to a buffer which is not visiting a file"))
8351 (t (setq link nil)))
8353 (if (consp link) (setq cpltxt (car link) link (cdr link)))
8354 (setq link (or link cpltxt)
8355 desc (or desc cpltxt))
8356 (if (equal desc "NONE") (setq desc nil))
8358 (if (and (or (interactive-p) executing-kbd-macro) link)
8360 (setq org-stored-links
8361 (cons (list link desc) org-stored-links))
8362 (message "Stored: %s" (or desc link))
8364 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
8366 (setq org-stored-links
8367 (cons (list link desc) org-stored-links))))
8368 (or agenda-link (and link (org-make-link-string link desc))))))
8370 (defun org-store-link-props (&rest plist)
8371 "Store link properties, extract names and addresses."
8373 (when (setq x (plist-get plist :from))
8374 (setq adr (mail-extract-address-components x))
8375 (setq plist (plist-put plist :fromname (car adr)))
8376 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
8377 (when (setq x (plist-get plist :to))
8378 (setq adr (mail-extract-address-components x))
8379 (setq plist (plist-put plist :toname (car adr)))
8380 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
8381 (let ((from (plist-get plist :from))
8382 (to (plist-get plist :to)))
8383 (when (and from to org-from-is-user-regexp)
8385 (plist-put plist :fromto
8386 (if (string-match org-from-is-user-regexp from)
8388 (concat "from %f"))))))
8389 (setq org-store-link-plist plist))
8391 (defun org-add-link-props (&rest plist)
8392 "Add these properties to the link property list."
8395 (setq key (pop plist) value (pop plist))
8396 (setq org-store-link-plist
8397 (plist-put org-store-link-plist key value)))))
8399 (defun org-email-link-description (&optional fmt)
8400 "Return the description part of an email link.
8401 This takes information from `org-store-link-plist' and formats it
8402 according to FMT (default from `org-email-link-description-format')."
8403 (setq fmt (or fmt org-email-link-description-format))
8404 (let* ((p org-store-link-plist)
8405 (to (plist-get p :toaddress))
8406 (from (plist-get p :fromaddress))
8409 (cons "%c" (plist-get p :fromto))
8410 (cons "%F" (plist-get p :from))
8411 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
8412 (cons "%T" (plist-get p :to))
8413 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
8414 (cons "%s" (plist-get p :subject))
8415 (cons "%m" (plist-get p :message-id)))))
8416 (when (string-match "%c" fmt)
8417 ;; Check if the user wrote this message
8418 (if (and org-from-is-user-regexp from to
8419 (save-match-data (string-match org-from-is-user-regexp from)))
8420 (setq fmt (replace-match "to %t" t t fmt))
8421 (setq fmt (replace-match "from %f" t t fmt))))
8422 (org-replace-escapes fmt table)))
8424 (defun org-make-org-heading-search-string (&optional string heading)
8425 "Make search string for STRING or current headline."
8427 (let ((s (or string (org-get-heading))))
8428 (unless (and string (not heading))
8429 ;; We are using a headline, clean up garbage in there.
8430 (if (string-match org-todo-regexp s)
8431 (setq s (replace-match "" t t s)))
8432 (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
8433 (setq s (replace-match "" t t s)))
8434 (setq s (org-trim s))
8435 (if (string-match (concat "^\\(" org-quote-string "\\|"
8436 org-comment-string "\\)") s)
8437 (setq s (replace-match "" t t s)))
8438 (while (string-match org-ts-regexp s)
8439 (setq s (replace-match "" t t s))))
8440 (or string (setq s (concat "*" s))) ; Add * for headlines
8441 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
8443 (defun org-make-link (&rest strings)
8444 "Concatenate STRINGS."
8445 (apply 'concat strings))
8447 (defun org-make-link-string (link &optional description)
8448 "Make a link with brackets, consisting of LINK and DESCRIPTION."
8449 (unless (string-match "\\S-" link)
8450 (error "Empty link"))
8451 (when (and description
8452 (stringp description)
8453 (not (string-match "\\S-" description)))
8454 (setq description nil))
8455 (when (stringp description)
8456 ;; Remove brackets from the description, they are fatal.
8457 (while (string-match "\\[" description)
8458 (setq description (replace-match "{" t t description)))
8459 (while (string-match "\\]" description)
8460 (setq description (replace-match "}" t t description))))
8461 (when (equal (org-link-escape link) description)
8462 ;; No description needed, it is identical
8463 (setq description nil))
8464 (when (and (not description)
8465 (not (equal link (org-link-escape link))))
8466 (setq description (org-extract-attributes link)))
8467 (setq link (if (string-match org-link-types-re link)
8468 (concat (match-string 1 link)
8469 (org-link-escape (substring link (match-end 1))))
8470 (org-link-escape link)))
8471 (concat "[[" link "]"
8472 (if description (concat "[" description "]") "")
8475 (defconst org-link-escape-chars
8479 (?\340 . "%E0") ; `a
8480 (?\342 . "%E2") ; ^a
8481 (?\347 . "%E7") ; ,c
8482 (?\350 . "%E8") ; `e
8483 (?\351 . "%E9") ; 'e
8484 (?\352 . "%EA") ; ^e
8485 (?\356 . "%EE") ; ^i
8486 (?\364 . "%F4") ; ^o
8487 (?\371 . "%F9") ; `u
8488 (?\373 . "%FB") ; ^u
8494 "Association list of escapes for some characters problematic in links.
8495 This is the list that is used for internal purposes.")
8497 (defvar org-url-encoding-use-url-hexify nil)
8499 (defconst org-link-escape-chars-browser
8500 '((?\ . "%20")) ; 32 for the SPC char
8501 "Association list of escapes for some characters problematic in links.
8502 This is the list that is used before handing over to the browser.")
8504 (defun org-link-escape (text &optional table)
8505 "Escape characters in TEXT that are problematic for links."
8506 (if (and org-url-encoding-use-url-hexify (not table))
8507 (url-hexify-string text)
8508 (setq table (or table org-link-escape-chars))
8510 (let ((re (mapconcat (lambda (x) (regexp-quote
8511 (char-to-string (car x))))
8513 (while (string-match re text)
8516 (cdr (assoc (string-to-char (match-string 0 text))
8521 (defun org-link-unescape (text &optional table)
8522 "Reverse the action of `org-link-escape'."
8523 (if (and org-url-encoding-use-url-hexify (not table))
8524 (url-unhex-string text)
8525 (setq table (or table org-link-escape-chars))
8527 (let ((case-fold-search t)
8528 (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
8530 (while (string-match re text)
8533 (char-to-string (car (rassoc (upcase (match-string 0 text))
8538 (defun org-xor (a b)
8542 (defun org-fixup-message-id-for-http (s)
8543 "Replace special characters in a message id, so it can be used in an http query."
8544 (when (string-match "%" s)
8545 (setq s (mapconcat (lambda (c)
8548 (char-to-string c)))
8550 (while (string-match "<" s)
8551 (setq s (replace-match "%3C" t t s)))
8552 (while (string-match ">" s)
8553 (setq s (replace-match "%3E" t t s)))
8554 (while (string-match "@" s)
8555 (setq s (replace-match "%40" t t s)))
8559 (defun org-insert-link-global ()
8560 "Insert a link like Org-mode does.
8561 This command can be called in any mode to insert a link in Org-mode syntax."
8563 (org-load-modules-maybe)
8564 (org-run-like-in-org-mode 'org-insert-link))
8566 (defun org-insert-link (&optional complete-file link-location)
8567 "Insert a link. At the prompt, enter the link.
8569 Completion can be used to insert any of the link protocol prefixes like
8572 The history can be used to select a link previously stored with
8573 `org-store-link'. When the empty string is entered (i.e. if you just
8574 press RET at the prompt), the link defaults to the most recently
8575 stored link. As SPC triggers completion in the minibuffer, you need to
8576 use M-SPC or C-q SPC to force the insertion of a space character.
8578 You will also be prompted for a description, and if one is given, it will
8579 be displayed in the buffer instead of the link.
8581 If there is already a link at point, this command will allow you to edit link
8582 and description parts.
8584 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
8585 be selected using completion. The path to the file will be relative to the
8586 current directory if the file is in the current directory or a subdirectory.
8587 Otherwise, the link will be the absolute path as completed in the minibuffer
8588 \(i.e. normally ~/path/to/file). You can configure this behavior using the
8589 option `org-link-file-path-type'.
8591 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
8592 the current directory or below.
8594 With three \\[universal-argument] prefixes, negate the meaning of
8595 `org-keep-stored-link-after-insertion'.
8597 If `org-make-link-description-function' is non-nil, this function will be
8598 called with the link target, and the result will be the default
8601 If the LINK-LOCATION parameter is non-nil, this value will be
8602 used as the link location instead of reading one interactively."
8604 (let* ((wcf (current-window-configuration))
8605 (region (if (org-region-active-p)
8606 (buffer-substring (region-beginning) (region-end))))
8607 (remove (and region (list (region-beginning) (region-end))))
8609 tmphist ; byte-compile incorrectly complains about this
8610 (link link-location)
8611 entry file all-prefixes)
8613 (link-location) ; specified by arg, just use it.
8614 ((org-in-regexp org-bracket-link-regexp 1)
8615 ;; We do have a link at point, and we are going to edit it.
8616 (setq remove (list (match-beginning 0) (match-end 0)))
8617 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
8618 (setq link (read-string "Link: "
8620 (org-match-string-no-properties 1)))))
8621 ((or (org-in-regexp org-angle-link-re)
8622 (org-in-regexp org-plain-link-re))
8623 ;; Convert to bracket link
8624 (setq remove (list (match-beginning 0) (match-end 0))
8625 link (read-string "Link: "
8626 (org-remove-angle-brackets (match-string 0)))))
8627 ((member complete-file '((4) (16)))
8628 ;; Completing read for file names.
8629 (setq link (org-file-complete-link complete-file)))
8631 ;; Read link, with completion for stored links.
8632 (with-output-to-temp-buffer "*Org Links*"
8633 (princ "Insert a link.
8634 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
8635 (when org-stored-links
8636 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
8639 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
8640 (reverse org-stored-links) "\n"))))
8641 (let ((cw (selected-window)))
8642 (select-window (get-buffer-window "*Org Links*" 'visible))
8643 (setq truncate-lines t)
8644 (unless (pos-visible-in-window-p (point-max))
8645 (org-fit-window-to-buffer))
8646 (and (window-live-p cw) (select-window cw)))
8647 ;; Fake a link history, containing the stored links.
8648 (setq tmphist (append (mapcar 'car org-stored-links)
8649 org-insert-link-history))
8650 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
8651 (mapcar 'car org-link-abbrev-alist)
8656 (let ((org-completion-use-ido nil)
8657 (org-completion-use-iswitchb nil))
8658 (org-completing-read
8661 (mapcar (lambda (x) (list (concat x ":")))
8663 (mapcar 'car org-stored-links))
8666 (car (car org-stored-links)))))
8667 (if (not (string-match "\\S-" link))
8668 (error "No link selected"))
8669 (if (or (member link all-prefixes)
8670 (and (equal ":" (substring link -1))
8671 (member (substring link 0 -1) all-prefixes)
8672 (setq link (substring link 0 -1))))
8673 (setq link (org-link-try-special-completion link))))
8674 (set-window-configuration wcf)
8675 (kill-buffer "*Org Links*"))
8676 (setq entry (assoc link org-stored-links))
8677 (or entry (push link org-insert-link-history))
8678 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
8679 (not org-keep-stored-link-after-insertion))
8680 (setq org-stored-links (delq (assoc link org-stored-links)
8682 (setq desc (or desc (nth 1 entry)))))
8684 (if (string-match org-plain-link-re link)
8685 ;; URL-like link, normalize the use of angular brackets.
8686 (setq link (org-make-link (org-remove-angle-brackets link))))
8688 ;; Check if we are linking to the current file with a search option
8689 ;; If yes, simplify the link by using only the search option.
8690 (when (and buffer-file-name
8691 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
8692 (let* ((path (match-string 1 link))
8693 (case-fold-search nil)
8694 (search (match-string 2 link)))
8696 (if (equal (file-truename buffer-file-name) (file-truename path))
8697 ;; We are linking to this same file, with a search option
8698 (setq link search)))))
8700 ;; Check if we can/should use a relative path. If yes, simplify the link
8701 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
8702 (let* ((type (match-string 1 link))
8703 (path (match-string 2 link))
8705 (case-fold-search nil))
8707 ((or (eq org-link-file-path-type 'absolute)
8708 (equal complete-file '(16)))
8709 (setq path (abbreviate-file-name (expand-file-name path))))
8710 ((eq org-link-file-path-type 'noabbrev)
8711 (setq path (expand-file-name path)))
8712 ((eq org-link-file-path-type 'relative)
8713 (setq path (file-relative-name path)))
8716 (if (string-match (concat "^" (regexp-quote
8718 (file-name-as-directory
8719 default-directory))))
8720 (expand-file-name path))
8721 ;; We are linking a file with relative path name.
8722 (setq path (substring (expand-file-name path)
8724 (setq path (abbreviate-file-name (expand-file-name path)))))))
8725 (setq link (concat type path))
8726 (if (equal desc origpath)
8729 (if org-make-link-description-function
8730 (setq desc (funcall org-make-link-description-function link desc)))
8732 (setq desc (read-string "Description: " desc))
8733 (unless (string-match "\\S-" desc) (setq desc nil))
8734 (if remove (apply 'delete-region remove))
8735 (insert (org-make-link-string link desc))))
8737 (defun org-link-try-special-completion (type)
8738 "If there is completion support for link type TYPE, offer it."
8739 (let ((fun (intern (concat "org-" type "-complete-link"))))
8742 (read-string "Link (no completion support): " (concat type ":")))))
8744 (defun org-file-complete-link (&optional arg)
8745 "Create a file link using completion."
8747 (setq file (read-file-name "File: "))
8748 (let ((pwd (file-name-as-directory (expand-file-name ".")))
8749 (pwd1 (file-name-as-directory (abbreviate-file-name
8750 (expand-file-name ".")))))
8753 (setq link (org-make-link
8755 (abbreviate-file-name (expand-file-name file)))))
8756 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
8757 (setq link (org-make-link "file:" (match-string 1 file))))
8758 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
8759 (expand-file-name file))
8760 (setq link (org-make-link
8761 "file:" (match-string 1 (expand-file-name file)))))
8762 (t (setq link (org-make-link "file:" file)))))
8765 (defun org-completing-read (&rest args)
8766 "Completing-read with SPACE being a normal character."
8767 (let ((minibuffer-local-completion-map
8768 (copy-keymap minibuffer-local-completion-map)))
8769 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
8770 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
8771 (apply 'org-icompleting-read args)))
8773 (defun org-completing-read-no-i (&rest args)
8774 (let (org-completion-use-ido org-completion-use-iswitchb)
8775 (apply 'org-completing-read args)))
8777 (defun org-iswitchb-completing-read (prompt choices &rest args)
8778 "Use iswitch as a completing-read replacement to choose from choices.
8779 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
8781 (let* ((iswitchb-use-virtual-buffers nil)
8782 (iswitchb-make-buflist-hook
8784 (setq iswitchb-temp-buflist choices))))
8785 (iswitchb-read-buffer prompt)))
8787 (defun org-icompleting-read (&rest args)
8788 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
8789 (org-without-partial-completion
8790 (if (and org-completion-use-ido
8791 (fboundp 'ido-completing-read)
8792 (boundp 'ido-mode) ido-mode
8793 (listp (second args)))
8794 (let ((ido-enter-matching-directory nil))
8795 (apply 'ido-completing-read (concat (car args))
8796 (if (consp (car (nth 1 args)))
8797 (mapcar (lambda (x) (car x)) (nth 1 args))
8800 (if (and org-completion-use-iswitchb
8801 (boundp 'iswitchb-mode) iswitchb-mode
8802 (listp (second args)))
8803 (apply 'org-iswitchb-completing-read (concat (car args))
8804 (if (consp (car (nth 1 args)))
8805 (mapcar (lambda (x) (car x)) (nth 1 args))
8808 (apply 'completing-read args)))))
8810 (defun org-extract-attributes (s)
8811 "Extract the attributes cookie from a string and set as text property."
8812 (let (a attr (start 0) key value)
8814 (when (string-match "{{\\([^}]+\\)}}$" s)
8815 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
8816 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
8817 (setq key (match-string 1 a) value (match-string 2 a)
8819 attr (plist-put attr (intern key) value))))
8820 (org-add-props s nil 'org-attr attr))
8823 (defun org-extract-attributes-from-string (tag)
8824 (let (key value attr)
8825 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
8826 (setq key (match-string 1 tag) value (match-string 2 tag)
8827 tag (replace-match "" t t tag)
8828 attr (plist-put attr (intern key) value)))
8831 (defun org-attributes-to-string (plist)
8832 "Format a property list into an HTML attribute list."
8833 (let ((s "") key value)
8835 (setq key (pop plist) value (pop plist))
8837 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
8840 ;;; Opening/following a link
8842 (defvar org-link-search-failed nil)
8844 (defvar org-open-link-functions nil
8845 "Hook for functions finding a plain text link.
8846 These functions must take a single argument, the link content.
8847 They will be called for links that look like [[link text][description]]
8848 when LINK TEXT does not have a protocol like \"http:\" and does not look
8849 like a filename (e.g. \"./blue.png\").
8851 These functions will be called *before* Org attempts to resolve the
8852 link by doing text searches in the current buffer - so if you want a
8853 link \"[[target]]\" to still find \"<<target>>\", your function should
8854 handle this as a special case.
8856 When the function does handle the link, it must return a non-nil value.
8857 If it decides that it is not responsible for this link, it must return
8858 nil to indicate that that Org-mode can continue with other options
8859 like exact and fuzzy text search.")
8861 (defun org-next-link ()
8862 "Move forward to the next link.
8863 If the link is in hidden text, expose it."
8865 (when (and org-link-search-failed (eq this-command last-command))
8866 (goto-char (point-min))
8867 (message "Link search wrapped back to beginning of buffer"))
8868 (setq org-link-search-failed nil)
8869 (let* ((pos (point))
8871 (a (assoc :link ct)))
8872 (if a (goto-char (nth 2 a)))
8873 (if (re-search-forward org-any-link-re nil t)
8875 (goto-char (match-beginning 0))
8876 (if (org-invisible-p) (org-show-context)))
8878 (setq org-link-search-failed t)
8879 (error "No further link found"))))
8881 (defun org-previous-link ()
8882 "Move backward to the previous link.
8883 If the link is in hidden text, expose it."
8885 (when (and org-link-search-failed (eq this-command last-command))
8886 (goto-char (point-max))
8887 (message "Link search wrapped back to end of buffer"))
8888 (setq org-link-search-failed nil)
8889 (let* ((pos (point))
8891 (a (assoc :link ct)))
8892 (if a (goto-char (nth 1 a)))
8893 (if (re-search-backward org-any-link-re nil t)
8895 (goto-char (match-beginning 0))
8896 (if (org-invisible-p) (org-show-context)))
8898 (setq org-link-search-failed t)
8899 (error "No further link found"))))
8901 (defun org-translate-link (s)
8902 "Translate a link string if a translation function has been defined."
8903 (if (and org-link-translation-function
8904 (fboundp org-link-translation-function)
8905 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
8907 (setq s (funcall org-link-translation-function
8908 (match-string 1) (match-string 2)))
8909 (concat (car s) ":" (cdr s)))
8912 (defun org-translate-link-from-planner (type path)
8913 "Translate a link from Emacs Planner syntax so that Org can follow it.
8914 This is still an experimental function, your mileage may vary."
8916 ((member type '("http" "https" "news" "ftp"))
8917 ;; standard Internet links are the same.
8919 ((and (equal type "irc") (string-match "^//" path))
8920 ;; Planner has two / at the beginning of an irc link, we have 1.
8921 ;; We should have zero, actually....
8922 (setq path (substring path 1)))
8923 ((and (equal type "lisp") (string-match "^/" path))
8924 ;; Planner has a slash, we do not.
8925 (setq type "elisp" path (substring path 1)))
8926 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
8927 ;; A typical message link. Planner has the id after the final slash,
8928 ;; we separate it with a hash mark
8929 (setq path (concat (match-string 1 path) "#"
8930 (org-remove-angle-brackets (match-string 2 path)))))
8934 (defun org-find-file-at-mouse (ev)
8935 "Open file link or URL at mouse."
8937 (mouse-set-point ev)
8938 (org-open-at-point 'in-emacs))
8940 (defun org-open-at-mouse (ev)
8941 "Open file link or URL at mouse."
8943 (mouse-set-point ev)
8944 (if (eq major-mode 'org-agenda-mode)
8945 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
8946 (org-open-at-point))
8948 (defvar org-window-config-before-follow-link nil
8949 "The window configuration before following a link.
8950 This is saved in case the need arises to restore it.")
8952 (defvar org-open-link-marker (make-marker)
8953 "Marker pointing to the location where `org-open-at-point; was called.")
8956 (defun org-open-at-point-global ()
8957 "Follow a link like Org-mode does.
8958 This command can be called in any mode to follow a link that has
8961 (org-run-like-in-org-mode 'org-open-at-point))
8964 (defun org-open-link-from-string (s &optional arg reference-buffer)
8965 "Open a link in the string S, as if it was in Org-mode."
8966 (interactive "sLink: \nP")
8967 (let ((reference-buffer (or reference-buffer (current-buffer))))
8969 (let ((org-inhibit-startup t))
8972 (goto-char (point-min))
8973 (when reference-buffer
8974 (setq org-link-abbrev-alist-local
8975 (with-current-buffer reference-buffer
8976 org-link-abbrev-alist-local)))
8977 (org-open-at-point arg reference-buffer)))))
8979 (defvar org-open-at-point-functions nil
8980 "Hook that is run when following a link at point.
8982 Functions in this hook must return t if they identify and follow
8983 a link at point. If they don't find anything interesting at point,
8984 they must return nil.")
8986 (defun org-open-at-point (&optional in-emacs reference-buffer)
8987 "Open link at or after point.
8988 If there is no link at point, this function will search forward up to
8989 the end of the current line.
8990 Normally, files will be opened by an appropriate application. If the
8991 optional argument IN-EMACS is non-nil, Emacs will visit the file.
8992 With a double prefix argument, try to open outside of Emacs, in the
8993 application the system uses for this file type."
8995 ;; if in a code block, then open the block's results
8996 (unless (call-interactively #'org-babel-open-src-block-result)
8997 (org-load-modules-maybe)
8998 (move-marker org-open-link-marker (point))
8999 (setq org-window-config-before-follow-link (current-window-configuration))
9000 (org-remove-occur-highlights nil nil t)
9002 ((and (org-on-heading-p)
9004 (concat org-plain-link-re "\\|"
9005 org-bracket-link-regexp "\\|"
9006 org-angle-link-re "\\|"
9007 "[ \t]:[^ \t\n]+:[ \t]*$")))
9008 (not (get-text-property (point) 'org-linked-text)))
9009 (or (org-offer-links-in-entry in-emacs)
9010 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
9011 ((run-hook-with-args-until-success 'org-open-at-point-functions))
9012 ((org-at-timestamp-p t) (org-follow-timestamp-link))
9013 ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
9014 (org-footnote-action))
9016 (let (type path link line search (pos (point)))
9019 (skip-chars-forward "^]\n\r")
9020 (when (org-in-regexp org-bracket-link-regexp 1)
9021 (setq link (org-extract-attributes
9022 (org-link-unescape (org-match-string-no-properties 1))))
9023 (while (string-match " *\n *" link)
9024 (setq link (replace-match " " t t link)))
9025 (setq link (org-link-expand-abbrev link))
9027 ((or (file-name-absolute-p link)
9028 (string-match "^\\.\\.?/" link))
9029 (setq type "file" path link))
9030 ((string-match org-link-re-with-space3 link)
9031 (setq type (match-string 1 link) path (match-string 2 link)))
9032 (t (setq type "thisfile" path link)))
9035 (when (get-text-property (point) 'org-linked-text)
9036 (setq type "thisfile"
9037 pos (if (get-text-property (1+ (point)) 'org-linked-text)
9038 (1+ (point)) (point))
9039 path (buffer-substring
9040 (previous-single-property-change pos 'org-linked-text)
9041 (next-single-property-change pos 'org-linked-text)))
9045 (when (or (org-in-regexp org-angle-link-re)
9046 (org-in-regexp org-plain-link-re))
9047 (setq type (match-string 1) path (match-string 2))
9050 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
9052 path (match-string 1))
9053 (while (string-match ":" path)
9054 (setq path (replace-match "+" t t path)))
9056 (when (org-in-regexp "<\\([^><\n]+\\)>")
9057 (setq type "tree-match"
9058 path (match-string 1))
9061 (error "No link found"))
9063 ;; switch back to reference buffer
9064 ;; needed when if called in a temporary buffer through
9065 ;; org-open-link-from-string
9066 (with-current-buffer (or reference-buffer (current-buffer))
9068 ;; Remove any trailing spaces in path
9069 (if (string-match " +\\'" path)
9070 (setq path (replace-match "" t t path)))
9071 (if (and org-link-translation-function
9072 (fboundp org-link-translation-function))
9073 ;; Check if we need to translate the link
9074 (let ((tmp (funcall org-link-translation-function type path)))
9075 (setq type (car tmp) path (cdr tmp))))
9079 ((assoc type org-link-protocols)
9080 (funcall (nth 1 (assoc type org-link-protocols)) path))
9082 ((equal type "mailto")
9083 (let ((cmd (car org-link-mailto-program))
9084 (args (cdr org-link-mailto-program)) args1
9085 (address path) (subject "") a)
9086 (if (string-match "\\(.*\\)::\\(.*\\)" path)
9087 (setq address (match-string 1 path)
9088 subject (org-link-escape (match-string 2 path))))
9091 ((not (stringp (car args))) (push (pop args) args1))
9092 (t (setq a (pop args))
9093 (if (string-match "%a" a)
9094 (setq a (replace-match address t t a)))
9095 (if (string-match "%s" a)
9096 (setq a (replace-match subject t t a)))
9098 (apply cmd (nreverse args1))))
9100 ((member type '("http" "https" "ftp" "news"))
9101 (browse-url (concat type ":" (org-link-escape
9102 path org-link-escape-chars-browser))))
9104 ((string= type "doi")
9105 (browse-url (concat "http://dx.doi.org/"
9107 path org-link-escape-chars-browser))))
9109 ((member type '("message"))
9110 (browse-url (concat type ":" path)))
9112 ((string= type "tags")
9113 (org-tags-view in-emacs path))
9115 ((string= type "tree-match")
9116 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
9118 ((string= type "file")
9119 (if (string-match "::\\([0-9]+\\)\\'" path)
9120 (setq line (string-to-number (match-string 1 path))
9121 path (substring path 0 (match-beginning 0)))
9122 (if (string-match "::\\(.+\\)\\'" path)
9123 (setq search (match-string 1 path)
9124 path (substring path 0 (match-beginning 0)))))
9125 (if (string-match "[*?{]" (file-name-nondirectory path))
9127 (org-open-file path in-emacs line search)))
9129 ((string= type "news")
9131 (org-gnus-follow-link path))
9133 ((string= type "shell")
9135 (if (or (not org-confirm-shell-link-function)
9136 (funcall org-confirm-shell-link-function
9137 (format "Execute \"%s\" in shell? "
9138 (org-add-props cmd nil
9139 'face 'org-warning))))
9141 (message "Executing %s" cmd)
9142 (shell-command cmd))
9145 ((string= type "elisp")
9147 (if (or (not org-confirm-elisp-link-function)
9148 (funcall org-confirm-elisp-link-function
9149 (format "Execute \"%s\" as elisp? "
9150 (org-add-props cmd nil
9151 'face 'org-warning))))
9152 (message "%s => %s" cmd
9153 (if (equal (string-to-char cmd) ?\()
9155 (call-interactively (read cmd))))
9158 ((and (string= type "thisfile")
9159 (run-hook-with-args-until-success
9160 'org-open-link-functions path)))
9162 ((string= type "thisfile")
9164 (switch-to-buffer-other-window
9165 (org-get-buffer-for-internal-link (current-buffer)))
9166 (org-mark-ring-push))
9167 (let ((cmd `(org-link-search
9169 ,(cond ((equal in-emacs '(4)) 'occur)
9170 ((equal in-emacs '(16)) 'org-occur)
9173 (condition-case nil (eval cmd)
9174 (error (progn (widen) (eval cmd))))))
9177 (browse-url-at-point)))))))
9178 (move-marker org-open-link-marker nil)
9179 (run-hook-with-args 'org-follow-link-hook)))
9181 (defun org-offer-links-in-entry (&optional nth zero)
9182 "Offer links in the current entry and follow the selected link.
9183 If there is only one link, follow it immediately as well.
9184 If NTH is an integer, immediately pick the NTH link found.
9185 If ZERO is a string, check also this string for a link, and if
9186 there is one, offer it as link number zero."
9187 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
9188 "\\(" org-angle-link-re "\\)\\|"
9189 "\\(" org-plain-link-re "\\)"))
9191 (in-emacs (if (integerp nth) nil nth))
9192 have-zero end links link c)
9193 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
9194 (push (match-string 0 zero) links)
9195 (setq cnt (1- cnt) have-zero t))
9197 (org-back-to-heading t)
9198 (setq end (save-excursion (outline-next-heading) (point)))
9199 (while (re-search-forward re end t)
9200 (push (match-string 0) links))
9201 (setq links (org-uniquify (reverse links))))
9205 (message "No links"))
9206 ((equal (length links) 1)
9207 (setq link (list (car links))))
9208 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
9209 (setq link (nth (if have-zero nth (1- nth)) links)))
9210 (t ; we have to select a link
9212 (save-window-excursion
9213 (delete-other-windows)
9214 (with-output-to-temp-buffer "*Select Link*"
9216 (if (not (string-match org-bracket-link-regexp l))
9217 (princ (format "[%c] %s\n" (incf cnt)
9218 (org-remove-angle-brackets l)))
9220 (princ (format "[%c] %s (%s)\n" (incf cnt)
9221 (match-string 3 l) (match-string 1 l)))
9222 (princ (format "[%c] %s\n" (incf cnt)
9223 (match-string 1 l))))))
9225 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
9226 (message "Select link to open, RET to open all:")
9227 (setq c (read-char-exclusive))
9228 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
9229 (when (equal c ?q) (error "Abort"))
9233 (if have-zero (setq nth (1+ nth)))
9234 (unless (and (integerp nth) (>= (length links) nth))
9235 (error "Invalid link selection"))
9236 (setq link (list (nth (1- nth) links))))))
9238 (let ((buf (current-buffer)))
9240 (org-open-link-from-string l in-emacs buf))
9244 ;; Add special file links that specify the way of opening
9246 (org-add-link-type "file+sys" 'org-open-file-with-system)
9247 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
9248 (defun org-open-file-with-system (path)
9249 "Open file at PATH using the system way of opening it."
9250 (org-open-file path 'system))
9251 (defun org-open-file-with-emacs (path)
9252 "Open file at PATH in Emacs."
9253 (org-open-file path 'emacs))
9254 (defun org-remove-file-link-modifiers ()
9255 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
9256 (goto-char (point-min))
9257 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
9259 (replace-match "file:" t t))))
9260 (eval-after-load "org-exp"
9261 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
9262 'org-remove-file-link-modifiers))
9266 (defun org-get-effort (&optional pom)
9267 "Get the effort estimate for the current entry."
9268 (org-entry-get pom org-effort-property))
9272 (defvar org-create-file-search-functions nil
9273 "List of functions to construct the right search string for a file link.
9274 These functions are called in turn with point at the location to
9275 which the link should point.
9277 A function in the hook should first test if it would like to
9278 handle this file type, for example by checking the `major-mode'
9279 or the file extension. If it decides not to handle this file, it
9280 should just return nil to give other functions a chance. If it
9281 does handle the file, it must return the search string to be used
9282 when following the link. The search string will be part of the
9283 file link, given after a double colon, and `org-open-at-point'
9284 will automatically search for it. If special measures must be
9285 taken to make the search successful, another function should be
9286 added to the companion hook `org-execute-file-search-functions',
9289 A function in this hook may also use `setq' to set the variable
9290 `description' to provide a suggestion for the descriptive text to
9291 be used for this link when it gets inserted into an Org-mode
9292 buffer with \\[org-insert-link].")
9294 (defvar org-execute-file-search-functions nil
9295 "List of functions to execute a file search triggered by a link.
9297 Functions added to this hook must accept a single argument, the
9298 search string that was part of the file link, the part after the
9299 double colon. The function must first check if it would like to
9300 handle this search, for example by checking the `major-mode' or
9301 the file extension. If it decides not to handle this search, it
9302 should just return nil to give other functions a chance. If it
9303 does handle the search, it must return a non-nil value to keep
9304 other functions from trying.
9306 Each function can access the current prefix argument through the
9307 variable `current-prefix-argument'. Note that a single prefix is
9308 used to force opening a link in Emacs, so it may be good to only
9309 use a numeric or double prefix to guide the search function.
9311 In case this is needed, a function in this hook can also restore
9312 the window configuration before `org-open-at-point' was called using:
9314 (set-window-configuration org-window-config-before-follow-link)")
9316 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
9317 (defun org-link-search (s &optional type avoid-pos)
9318 "Search for a link search option.
9319 If S is surrounded by forward slashes, it is interpreted as a
9320 regular expression. In org-mode files, this will create an `org-occur'
9321 sparse tree. In ordinary files, `occur' will be used to list matches.
9322 If the current buffer is in `dired-mode', grep will be used to search
9323 in all files. If AVOID-POS is given, ignore matches near that position."
9324 (let ((case-fold-search t)
9325 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
9326 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
9327 (append '(("") (" ") ("\t") ("\n"))
9331 (pre nil) (post nil)
9332 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
9334 ;; First check if there are any special search functions
9335 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
9336 ;; Now try the builtin stuff
9337 ((and (equal (string-to-char s0) ?#)
9340 (goto-char (point-min))
9343 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
9344 (setq type 'dedicated
9345 pos (match-beginning 0))))
9346 ;; There is an exact target for this
9348 (org-back-to-heading t)))
9350 (goto-char (point-min))
9353 (concat "<<" (regexp-quote s0) ">>") nil t)
9354 (setq type 'dedicated
9355 pos (match-beginning 0))))
9356 ;; There is an exact target for this
9358 ((and (string-match "^(\\(.*\\))$" s0)
9360 (goto-char (point-min))
9363 (concat "[^[]" (regexp-quote
9364 (format org-coderef-label-format
9365 (match-string 1 s0))))
9367 (setq type 'dedicated
9368 pos (1+ (match-beginning 0))))))
9369 ;; There is a coderef target for this
9371 ((string-match "^/\\(.*\\)/$" s)
9372 ;; A regular expression
9375 (org-occur (match-string 1 s)))
9376 ;;((eq major-mode 'dired-mode)
9377 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
9378 (t (org-do-occur (match-string 1 s)))))
9379 ((and (org-mode-p) org-link-search-must-match-exact-headline)
9380 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
9381 (goto-char (point-min))
9383 ((let (case-fold-search)
9384 (re-search-forward (format org-complex-heading-regexp-format
9387 ;; OK, found a match
9388 (goto-char (match-beginning 0)))
9389 ((and (not org-link-search-inhibit-query)
9390 (eq org-link-search-must-match-exact-headline 'query-to-create)
9391 (y-or-n-p "No match - create this as a new heading? "))
9392 (goto-char (point-max))
9393 (or (bolp) (newline))
9394 (insert "* " s "\n")
9395 (beginning-of-line 0))
9398 (error "No match"))))
9400 ;; A normal search string
9401 (when (equal (string-to-char s) ?*)
9402 ;; Anchor on headlines, post may include tags.
9403 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
9404 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
9406 (remove-text-properties
9408 '(face nil mouse-face nil keymap nil fontified nil) s)
9409 ;; Make a series of regular expressions to find a match
9410 (setq words (org-split-string s "[ \n\r\t]+")
9412 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
9413 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
9415 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
9416 re2a (concat "[ \t\r\n]" re2a_)
9417 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
9418 re4 (concat "[^a-zA-Z_]" re4_)
9420 re1 (concat pre re2 post)
9421 re3 (concat pre (if pre re4_ re4) post)
9422 re5 (concat pre ".*" re4)
9423 re2 (concat pre re2)
9424 re2a (concat pre (if pre re2a_ re2a))
9425 re4 (concat pre (if pre re4_ re4))
9426 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
9427 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
9431 ((eq type 'org-occur) (org-occur reall))
9432 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
9433 (t (goto-char (point-min))
9435 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
9436 (org-search-not-self 1 re1 nil t)
9437 (org-search-not-self 1 re2 nil t)
9438 (org-search-not-self 1 re2a nil t)
9439 (org-search-not-self 1 re3 nil t)
9440 (org-search-not-self 1 re4 nil t)
9441 (org-search-not-self 1 re5 nil t)
9443 (goto-char (match-beginning 1))
9445 (error "No match"))))))
9446 (and (org-mode-p) (org-show-context 'link-search))
9449 (defun org-search-not-self (group &rest args)
9450 "Execute `re-search-forward', but only accept matches that do not
9451 enclose the position of `org-open-link-marker'."
9452 (let ((m org-open-link-marker))
9454 (while (apply 're-search-forward args)
9455 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
9456 (goto-char (match-end group))
9457 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
9458 (> (match-beginning 0) (marker-position m))
9459 (< (match-end 0) (marker-position m)))
9461 (or (not (org-in-regexp
9462 org-bracket-link-analytic-regexp 1))
9463 (not (match-end 4)) ; no description
9464 (and (<= (match-beginning 4) (point))
9465 (>= (match-end 4) (point))))))
9466 (throw 'exit (point))))))))
9468 (defun org-get-buffer-for-internal-link (buffer)
9469 "Return a buffer to be used for displaying the link target of internal links."
9471 ((not org-display-internal-link-with-indirect-buffer)
9473 ((string-match "(Clone)$" (buffer-name buffer))
9474 (message "Buffer is already a clone, not making another one")
9475 ;; we also do not modify visibility in this case
9477 (t ; make a new indirect buffer for displaying the link
9478 (let* ((bn (buffer-name buffer))
9479 (ibn (concat bn "(Clone)"))
9480 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
9481 (with-current-buffer ib (org-overview))
9484 (defun org-do-occur (regexp &optional cleanup)
9485 "Call the Emacs command `occur'.
9486 If CLEANUP is non-nil, remove the printout of the regular expression
9487 in the *Occur* buffer. This is useful if the regex is long and not useful
9491 (let ((cwin (selected-window)) win beg end)
9492 (when (setq win (get-buffer-window "*Occur*"))
9493 (select-window win))
9494 (goto-char (point-min))
9495 (when (re-search-forward "match[a-z]+" nil t)
9496 (setq beg (match-end 0))
9497 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
9498 (setq end (1- (match-beginning 0)))))
9499 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
9500 (goto-char (point-min))
9501 (select-window cwin))))
9503 ;;; The mark ring for links jumps
9505 (defvar org-mark-ring nil
9506 "Mark ring for positions before jumps in Org-mode.")
9507 (defvar org-mark-ring-last-goto nil
9508 "Last position in the mark ring used to go back.")
9509 ;; Fill and close the ring
9510 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
9511 (loop for i from 1 to org-mark-ring-length do
9512 (push (make-marker) org-mark-ring))
9513 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
9516 (defun org-mark-ring-push (&optional pos buffer)
9517 "Put the current position or POS into the mark ring and rotate it."
9519 (setq pos (or pos (point)))
9520 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
9521 (move-marker (car org-mark-ring)
9523 (or buffer (current-buffer)))
9525 (substitute-command-keys
9526 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
9528 (defun org-mark-ring-goto (&optional n)
9529 "Jump to the previous position in the mark ring.
9530 With prefix arg N, jump back that many stored positions. When
9531 called several times in succession, walk through the entire ring.
9532 Org-mode commands jumping to a different position in the current file,
9533 or to another Org-mode file, automatically push the old position
9537 (if (eq last-command this-command)
9538 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
9539 (setq p org-mark-ring))
9540 (setq org-mark-ring-last-goto p)
9542 (switch-to-buffer (marker-buffer m))
9544 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
9546 (defun org-remove-angle-brackets (s)
9547 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
9548 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
9550 (defun org-add-angle-brackets (s)
9551 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
9552 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
9554 (defun org-remove-double-quotes (s)
9555 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
9556 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
9559 ;;; Following specific links
9561 (defun org-follow-timestamp-link ()
9563 ((org-at-date-range-p t)
9564 (let ((org-agenda-start-on-weekday)
9565 (t1 (match-string 1))
9566 (t2 (match-string 2)))
9567 (setq t1 (time-to-days (org-time-string-to-time t1))
9568 t2 (time-to-days (org-time-string-to-time t2)))
9569 (org-agenda-list nil t1 (1+ (- t2 t1)))))
9570 ((org-at-timestamp-p t)
9571 (org-agenda-list nil (time-to-days (org-time-string-to-time
9572 (substring (match-string 1) 0 10)))
9574 (t (error "This should not happen"))))
9577 ;;; Following file links
9578 (defvar org-wait nil)
9579 (defun org-open-file (path &optional in-emacs line search)
9580 "Open the file at PATH.
9581 First, this expands any special file name abbreviations. Then the
9582 configuration variable `org-file-apps' is checked if it contains an
9583 entry for this file type, and if yes, the corresponding command is launched.
9585 If no application is found, Emacs simply visits the file.
9587 With optional prefix argument IN-EMACS, Emacs will visit the file.
9588 With a double \\[universal-argument] \\[universal-argument] \
9589 prefix arg, Org tries to avoid opening in Emacs
9590 and to use an external application to visit the file.
9592 Optional LINE specifies a line to go to, optional SEARCH a string
9593 to search for. If LINE or SEARCH is given, the file will be
9594 opened in Emacs, unless an entry from org-file-apps that makes
9595 use of groups in a regexp matches.
9596 If the file does not exist, an error is thrown."
9597 (let* ((file (if (equal path "")
9599 (substitute-in-file-name (expand-file-name path))))
9600 (file-apps (append org-file-apps (org-default-apps)))
9601 (apps (org-remove-if
9602 'org-file-apps-entry-match-against-dlink-p file-apps))
9603 (apps-dlink (org-remove-if-not
9604 'org-file-apps-entry-match-against-dlink-p file-apps))
9605 (remp (and (assq 'remote apps) (org-file-remote-p file)))
9606 (dirp (if remp nil (file-directory-p file)))
9607 (file (if (and dirp org-open-directory-means-index-dot-org)
9608 (concat (file-name-as-directory file) "index.org")
9610 (a-m-a-p (assq 'auto-mode apps))
9611 (dfile (downcase file))
9612 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
9613 (link (cond ((and (eq line nil)
9617 (concat file "::" (number-to-string line)))
9619 (concat file "::" search))))
9620 (dlink (downcase link))
9621 (old-buffer (current-buffer))
9623 (old-mode major-mode)
9624 ext cmd link-match-data)
9625 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
9626 (setq ext (match-string 1 dfile))
9627 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
9628 (setq ext (match-string 1 dfile))))
9630 ((member in-emacs '((16) system))
9631 (setq cmd (cdr (assoc 'system apps))))
9632 (in-emacs (setq cmd 'emacs))
9634 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
9635 (and dirp (cdr (assoc 'directory apps)))
9636 ; first, try matching against apps-dlink
9637 ; if we get a match here, store the match data for later
9638 (let ((match (assoc-default dlink apps-dlink
9641 (progn (setq link-match-data (match-data))
9643 (progn (setq in-emacs (or in-emacs line search))
9644 nil))) ; if we have no match in apps-dlink,
9645 ; always open the file in emacs if line or search
9646 ; is given (for backwards compatibility)
9647 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
9649 (cdr (assoc ext apps))
9650 (cdr (assoc t apps))))))
9651 (when (eq cmd 'system)
9652 (setq cmd (cdr (assoc 'system apps))))
9653 (when (eq cmd 'default)
9654 (setq cmd (cdr (assoc t apps))))
9655 (when (eq cmd 'mailcap)
9657 (mailcap-parse-mailcaps)
9658 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
9659 (command (mailcap-mime-info mime-type)))
9660 (if (stringp command)
9662 (setq cmd 'emacs))))
9663 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
9664 (not (file-exists-p file))
9665 (not org-open-non-existing-files))
9666 (error "No such file: %s" file))
9668 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
9669 ;; Remove quotes around the file name - we'll use shell-quote-argument.
9670 (while (string-match "['\"]%s['\"]" cmd)
9671 (setq cmd (replace-match "%s" t t cmd)))
9672 (while (string-match "%s" cmd)
9673 (setq cmd (replace-match
9675 (shell-quote-argument
9676 (convert-standard-filename file)))
9679 ;; Replace "%1", "%2" etc. in command with group matches from regex
9681 (let ((match-index 1)
9682 (number-of-groups (- (/ (length link-match-data) 2) 1)))
9683 (set-match-data link-match-data)
9684 (while (<= match-index number-of-groups)
9685 (let ((regex (concat "%" (number-to-string match-index)))
9686 (replace-with (match-string match-index dlink)))
9687 (while (string-match regex cmd)
9688 (setq cmd (replace-match replace-with t t cmd))))
9689 (setq match-index (+ match-index 1)))))
9691 (save-window-excursion
9692 (start-process-shell-command cmd nil cmd)
9693 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
9697 (funcall (cdr (assq 'file org-link-frame-setup)) file)
9699 (if line (org-goto-line line)
9700 (if search (org-link-search search))))
9702 (let ((file (convert-standard-filename file)))
9704 (set-match-data link-match-data)
9706 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
9707 (and (org-mode-p) (eq old-mode 'org-mode)
9708 (or (not (equal old-buffer (current-buffer)))
9709 (not (equal old-pos (point))))
9710 (org-mark-ring-push old-pos old-buffer))))
9712 (defun org-file-apps-entry-match-against-dlink-p (entry)
9713 "This function returns non-nil if `entry' uses a regular
9714 expression which should be matched against the whole link by
9717 It assumes that is the case when the entry uses a regular
9718 expression which has at least one grouping construct and the
9719 action is either a lisp form or a command string containing
9720 '%1', i.e. using at least one subexpression match as a
9722 (let ((selector (car entry))
9723 (action (cdr entry)))
9724 (if (stringp selector)
9725 (and (> (regexp-opt-depth selector) 0)
9726 (or (and (stringp action)
9727 (string-match "%[0-9]" action))
9731 (defun org-default-apps ()
9732 "Return the default applications for this operating system."
9734 ((eq system-type 'darwin)
9735 org-file-apps-defaults-macosx)
9736 ((eq system-type 'windows-nt)
9737 org-file-apps-defaults-windowsnt)
9738 (t org-file-apps-defaults-gnu)))
9740 (defun org-apps-regexp-alist (list &optional add-auto-mode)
9741 "Convert extensions to regular expressions in the cars of LIST.
9742 Also, weed out any non-string entries, because the return value is used
9743 only for regexp matching.
9744 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
9745 point to the symbol `emacs', indicating that the file should
9746 be opened in Emacs."
9750 (if (not (stringp (car x)))
9752 (if (string-match "\\W" (car x))
9754 (cons (concat "\\." (car x) "\\'") (cdr x)))))
9757 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
9759 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
9760 (defun org-file-remote-p (file)
9761 "Test whether FILE specifies a location on a remote system.
9762 Return non-nil if the location is indeed remote.
9764 For example, the filename \"/user@host:/foo\" specifies a location
9765 on the system \"/user@host:\"."
9766 (cond ((fboundp 'file-remote-p)
9767 (file-remote-p file))
9768 ((fboundp 'tramp-handle-file-remote-p)
9769 (tramp-handle-file-remote-p file))
9770 ((and (boundp 'ange-ftp-name-format)
9771 (string-match (car ange-ftp-name-format) file))
9778 (defun org-get-org-file ()
9779 "Read a filename, with default directory `org-directory'."
9780 (let ((default (or org-default-notes-file remember-data-file)))
9781 (read-file-name (format "File name [%s]: " default)
9782 (file-name-as-directory org-directory)
9785 (defun org-notes-order-reversed-p ()
9786 "Check if the current file should receive notes in reversed order."
9788 ((not org-reverse-note-order) nil)
9789 ((eq t org-reverse-note-order) t)
9790 ((not (listp org-reverse-note-order)) nil)
9792 (let ((all org-reverse-note-order)
9794 (while (setq entry (pop all))
9795 (if (string-match (car entry) buffer-file-name)
9796 (throw 'exit (cdr entry))))
9799 (defvar org-refile-target-table nil
9800 "The list of refile targets, created by `org-refile'.")
9802 (defvar org-agenda-new-buffers nil
9803 "Buffers created to visit agenda files.")
9805 (defvar org-refile-cache nil
9806 "Cache for refile targets.")
9809 (defvar org-refile-markers nil
9810 "All the markers used for caching refile locations.")
9812 (defun org-refile-marker (pos)
9813 "Get a new refile marker, but only if caching is in use."
9814 (if (not org-refile-use-cache)
9816 (let ((m (make-marker)))
9818 (push m org-refile-markers)
9821 (defun org-refile-cache-clear ()
9822 "Clear the refile cache and disable all the markers."
9823 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
9824 (setq org-refile-markers nil)
9825 (setq org-refile-cache nil)
9826 (message "Refile cache has been cleared"))
9828 (defun org-refile-cache-check-set (set)
9829 "Check if all the markers in the cache still have live buffers."
9832 (while (and set (setq marker (nth 3 (pop set))))
9833 ;; if org-refile-use-outline-path is 'file, marker may be nil
9834 (when (and marker (null (marker-buffer marker)))
9835 (message "not found") (sit-for 3)
9839 (defun org-refile-cache-put (set &rest identifiers)
9840 "Push the refile targets SET into the cache, under IDENTIFIERS."
9841 (let* ((key (sha1 (prin1-to-string identifiers)))
9842 (entry (assoc key org-refile-cache)))
9845 (push (cons key set) org-refile-cache))))
9847 (defun org-refile-cache-get (&rest identifiers)
9848 "Retrieve the cached value for refile targets given by IDENTIFIERS."
9850 ((not org-refile-cache) nil)
9851 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
9853 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
9854 org-refile-cache))))
9855 (and set (org-refile-cache-check-set set) set)))))
9857 (defun org-get-refile-targets (&optional default-buffer)
9858 "Produce a table with refile targets."
9859 (let ((case-fold-search nil)
9860 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
9861 (entries (or org-refile-targets '((nil . (:level . 1)))))
9862 targets tgs txt re files f desc descre fast-path-p level pos0)
9863 (message "Getting targets...")
9864 (with-current-buffer (or default-buffer (current-buffer))
9865 (while (setq entry (pop entries))
9866 (setq files (car entry) desc (cdr entry))
9867 (setq fast-path-p nil)
9869 ((null files) (setq files (list (current-buffer))))
9870 ((eq files 'org-agenda-files)
9871 (setq files (org-agenda-files 'unrestricted)))
9872 ((and (symbolp files) (fboundp files))
9873 (setq files (funcall files)))
9874 ((and (symbolp files) (boundp files))
9875 (setq files (symbol-value files))))
9876 (if (stringp files) (setq files (list files)))
9878 ((eq (car desc) :tag)
9879 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
9880 ((eq (car desc) :todo)
9881 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
9882 ((eq (car desc) :regexp)
9883 (setq descre (cdr desc)))
9884 ((eq (car desc) :level)
9885 (setq descre (concat "^\\*\\{" (number-to-string
9886 (if org-odd-levels-only
9887 (1- (* 2 (cdr desc)))
9890 ((eq (car desc) :maxlevel)
9891 (setq fast-path-p t)
9892 (setq descre (concat "^\\*\\{1," (number-to-string
9893 (if org-odd-levels-only
9894 (1- (* 2 (cdr desc)))
9897 (t (error "Bad refiling target description %s" desc)))
9898 (while (setq f (pop files))
9899 (with-current-buffer
9900 (if (bufferp f) f (org-get-agenda-file-buffer f))
9902 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
9904 (if (bufferp f) (setq f (buffer-file-name
9905 (buffer-base-buffer f))))
9906 (setq f (and f (expand-file-name f)))
9907 (if (eq org-refile-use-outline-path 'file)
9908 (push (list (file-name-nondirectory f) f nil nil) tgs))
9912 (goto-char (point-min))
9913 (while (re-search-forward descre nil t)
9914 (goto-char (setq pos0 (point-at-bol)))
9916 (when org-refile-target-verify-function
9918 (or (funcall org-refile-target-verify-function)
9920 (when (looking-at org-complex-heading-regexp)
9921 (setq level (org-reduced-level
9922 (- (match-end 1) (match-beginning 1)))
9923 txt (org-link-display-format (match-string 4))
9924 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
9925 re (format org-complex-heading-regexp-format
9926 (regexp-quote (match-string 4))))
9927 (when org-refile-use-outline-path
9928 (setq txt (mapconcat
9931 (if (eq org-refile-use-outline-path
9933 (list (file-name-nondirectory
9935 (buffer-base-buffer))))
9936 (if (eq org-refile-use-outline-path
9938 (list (buffer-file-name
9939 (buffer-base-buffer)))))
9940 (org-get-outline-path fast-path-p
9944 (push (list txt f re (org-refile-marker (point)))
9946 (when (= (point) pos0)
9947 ;; verification function has not moved point
9948 (goto-char (point-at-eol))))))))
9949 (when org-refile-use-cache
9950 (org-refile-cache-put tgs (buffer-file-name) descre))
9951 (setq targets (append tgs targets))
9953 (message "Getting targets...done")
9954 (nreverse targets)))
9956 (defun org-protect-slash (s)
9957 (while (string-match "/" s)
9958 (setq s (replace-match "\\" t t s)))
9961 (defvar org-olpa (make-vector 20 nil))
9963 (defun org-get-outline-path (&optional fastp level heading)
9964 "Return the outline path to the current entry, as a list.
9966 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
9967 routine which makes outline path derivations for an entire file,
9968 avoiding backtracing. Refile target collection makes use of that."
9972 (error "Outline path failure, more than 19 levels"))
9973 (loop for i from level upto 19 do
9974 (aset org-olpa i nil))
9976 (delq nil (append org-olpa nil))
9977 (aset org-olpa level heading)))
9978 (let (rtn case-fold-search)
9982 (while (org-up-heading-safe)
9983 (when (looking-at org-complex-heading-regexp)
9984 (push (org-match-string-no-properties 4) rtn)))
9987 (defun org-format-outline-path (path &optional width prefix)
9988 "Format the outline path PATH for display.
9989 Width is the maximum number of characters that is available.
9990 Prefix is a prefix to be included in the returned string,
9991 such as the file name."
9992 (setq width (or width 79))
9993 (if prefix (setq width (- width (length prefix))))
9996 (let* ((nsteps (length path))
9997 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
9998 (maxwidth (if (<= total-width width)
9999 10000 ;; everything fits
10000 ;; we need to shorten the level headings
10001 (/ (- width nsteps) nsteps)))
10002 (org-odd-levels-only nil)
10004 (total (1+ (length prefix))))
10005 (setq maxwidth (max maxwidth 10))
10010 (if (and (= n nsteps) (< maxwidth 10000))
10011 (setq maxwidth (- total-width total)))
10012 (if (< (length h) maxwidth)
10013 (progn (setq total (+ total (length h) 1)) h)
10014 (setq h (substring h 0 (- maxwidth 2))
10015 total (+ total maxwidth 1))
10016 (if (string-match "[ \t]+\\'" h)
10017 (setq h (substring h 0 (match-beginning 0))))
10018 (setq h (concat h "..")))
10019 (org-add-props h nil 'face
10020 (nth (% (1- n) org-n-level-faces)
10025 (defun org-display-outline-path (&optional file current)
10026 "Display the current outline path in the echo area."
10028 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
10029 (case-fold-search nil)
10030 (path (and (org-mode-p) (org-get-outline-path))))
10031 (if current (setq path (append path
10033 (org-back-to-heading t)
10034 (if (looking-at org-complex-heading-regexp)
10035 (list (match-string 4)))))))
10037 (org-format-outline-path
10040 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
10042 (defvar org-refile-history nil
10043 "History for refiling operations.")
10045 (defvar org-after-refile-insert-hook nil
10046 "Hook run after `org-refile' has inserted its stuff at the new location.
10047 Note that this is still *before* the stuff will be removed from
10048 the *old* location.")
10050 (defvar org-capture-last-stored-marker)
10051 (defun org-refile (&optional goto default-buffer rfloc)
10052 "Move the entry at point to another heading.
10053 The list of target headings is compiled using the information in
10054 `org-refile-targets', which see. This list is created before each use
10055 and will therefore always be up-to-date.
10057 At the target location, the entry is filed as a subitem of the target heading.
10058 Depending on `org-reverse-note-order', the new subitem will either be the
10059 first or the last subitem.
10061 If there is an active region, all entries in that region will be moved.
10062 However, the region must fulfill the requirement that the first heading
10063 is the first one sets the top-level of the moved text - at most siblings
10064 below it are allowed.
10066 With prefix arg GOTO, the command will only visit the target location,
10067 not actually move anything.
10068 With a double prefix arg \\[universal-argument] \\[universal-argument], \
10069 go to the location where the last refiling
10070 operation has put the subtree.
10071 With a prefix argument of `2', refile to the running clock.
10073 RFLOC can be a refile location obtained in a different way.
10075 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
10077 If you are using target caching (see `org-refile-use-cache'),
10078 You have to clear the target cache in order to find new targets.
10079 This can be done with a 0 prefix: `C-0 C-c C-w'"
10081 (if (member goto '(0 (64)))
10082 (org-refile-cache-clear)
10083 (let* ((cbuf (current-buffer))
10084 (regionp (org-region-active-p))
10085 (region-start (and regionp (region-beginning)))
10086 (region-end (and regionp (region-end)))
10087 (region-length (and regionp (- region-end region-start)))
10088 (filename (buffer-file-name (buffer-base-buffer cbuf)))
10089 pos it nbuf file re level reversed)
10090 (setq last-command nil)
10092 (goto-char region-start)
10093 (or (bolp) (goto-char (point-at-bol)))
10094 (setq region-start (point))
10095 (unless (org-kill-is-subtree-p
10096 (buffer-substring region-start region-end))
10097 (error "The region is not a (sequence of) subtree(s)")))
10098 (if (equal goto '(16))
10099 (org-refile-goto-last-stored)
10101 (and (equal goto 2)
10102 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
10104 (setq it (list (or org-clock-heading "running clock")
10106 (marker-buffer org-clock-hd-marker))
10108 (marker-position org-clock-hd-marker)))
10112 (org-refile-get-location
10113 (if goto "Goto: " "Refile to: ") default-buffer
10114 org-refile-allow-creating-parent-nodes)))))
10115 (setq file (nth 1 it)
10118 (if (and (not goto)
10120 (equal (buffer-file-name) file)
10122 (and (>= pos region-start)
10123 (<= pos region-end))
10124 (and (>= pos (point))
10125 (< pos (save-excursion
10126 (org-end-of-subtree t t))))))
10127 (error "Cannot refile to position inside the tree or region"))
10129 (setq nbuf (or (find-buffer-visiting file)
10130 (find-file-noselect file)))
10133 (switch-to-buffer nbuf)
10135 (org-show-context 'org-goto))
10138 (org-kill-new (buffer-substring region-start region-end))
10139 (org-save-markers-in-region region-start region-end))
10140 (org-copy-subtree 1 nil t))
10141 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
10142 (find-file-noselect file)))
10143 (setq reversed (org-notes-order-reversed-p))
10150 (looking-at outline-regexp)
10151 (setq level (org-get-valid-level (funcall outline-level) 1))
10154 (or (outline-next-heading) (point-max))
10155 (or (save-excursion (org-get-next-sibling))
10156 (org-end-of-subtree t t)
10160 (goto-char (point-max))
10161 (goto-char (point-min))
10162 (or (outline-next-heading) (goto-char (point-max)))))
10163 (if (not (bolp)) (newline))
10164 (org-paste-subtree level)
10165 (when org-log-refile
10166 (org-add-log-setup 'refile nil nil 'findpos
10168 (unless (eq org-log-refile 'note)
10169 (save-excursion (org-add-log-note))))
10170 (and org-auto-align-tags (org-set-tags nil t))
10171 (bookmark-set "org-refile-last-stored")
10172 ;; If we are refiling for capture, make sure that the
10173 ;; last-capture pointers point here
10174 (when (org-bound-and-true-p org-refile-for-capture)
10175 (bookmark-set "org-capture-last-stored-marker")
10176 (move-marker org-capture-last-stored-marker (point)))
10177 (if (fboundp 'deactivate-mark) (deactivate-mark))
10178 (run-hooks 'org-after-refile-insert-hook))))
10180 (delete-region (point) (+ (point) region-length))
10182 (when (featurep 'org-inlinetask)
10183 (org-inlinetask-remove-END-maybe))
10184 (setq org-markers-to-move nil)
10185 (message "Refiled to \"%s\" in file %s" (car it) file)))))))
10187 (defun org-refile-goto-last-stored ()
10188 "Go to the location where the last refile was stored."
10190 (bookmark-jump "org-refile-last-stored")
10191 (message "This is the location of the last refile"))
10193 (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
10194 "Prompt the user for a refile location, using PROMPT."
10195 (let ((org-refile-targets org-refile-targets)
10196 (org-refile-use-outline-path org-refile-use-outline-path))
10197 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
10198 (unless org-refile-target-table
10199 (error "No refile targets"))
10200 (let* ((cbuf (current-buffer))
10201 (partial-completion-mode nil)
10202 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
10203 (cfunc (if (and org-refile-use-outline-path
10204 org-outline-path-complete-in-steps)
10205 'org-olpath-completing-read
10206 'org-icompleting-read))
10207 (extra (if org-refile-use-outline-path "/" ""))
10208 (filename (and cfn (expand-file-name cfn)))
10211 (if (and (not (member org-refile-use-outline-path
10212 '(file full-file-path)))
10213 (not (equal filename (nth 1 x))))
10214 (cons (concat (car x) extra " ("
10215 (file-name-nondirectory (nth 1 x)) ")")
10217 (cons (concat (car x) extra) (cdr x))))
10218 org-refile-target-table))
10219 (completion-ignore-case t)
10220 pa answ parent-target child parent old-hist)
10221 (setq old-hist org-refile-history)
10222 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
10223 nil 'org-refile-history))
10224 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
10225 (org-refile-check-position pa)
10228 (when (or (not org-refile-history)
10229 (not (eq old-hist org-refile-history))
10230 (not (equal (car pa) (car org-refile-history))))
10231 (setq org-refile-history
10232 (cons (car pa) (if (assoc (car org-refile-history) tbl)
10234 (cdr org-refile-history))))
10235 (if (equal (car org-refile-history) (nth 1 org-refile-history))
10236 (pop org-refile-history)))
10238 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
10240 (setq parent (match-string 1 answ)
10241 child (match-string 2 answ))
10242 (setq parent-target (or (assoc parent tbl)
10243 (assoc (concat parent "/") tbl)))
10244 (when (and parent-target
10245 (or (eq new-nodes t)
10246 (and (eq new-nodes 'confirm)
10247 (y-or-n-p (format "Create new node \"%s\"? "
10249 (org-refile-new-child parent-target child)))
10250 (error "Invalid target location")))))
10252 (defun org-refile-check-position (refile-pointer)
10253 "Check if the refile pointer matches the readline to which it points."
10254 (let* ((file (nth 1 refile-pointer))
10255 (re (nth 2 refile-pointer))
10256 (pos (nth 3 refile-pointer))
10258 (when (org-string-nw-p re)
10259 (setq buffer (if (markerp pos)
10260 (marker-buffer pos)
10261 (or (find-buffer-visiting file)
10262 (find-file-noselect file))))
10263 (with-current-buffer buffer
10268 (beginning-of-line 1)
10269 (unless (org-looking-at-p re)
10270 (error "Invalid refile position, please rebuild the cache"))))))))
10272 (defun org-refile-new-child (parent-target child)
10273 "Use refile target PARENT-TARGET to add new CHILD below it."
10274 (unless parent-target
10275 (error "Cannot find parent for new node"))
10276 (let ((file (nth 1 parent-target))
10277 (pos (nth 3 parent-target))
10279 (with-current-buffer (or (find-buffer-visiting file)
10280 (find-file-noselect file))
10286 (goto-char (point-max))
10287 (if (not (bolp)) (newline)))
10288 (when (looking-at outline-regexp)
10289 (setq level (funcall outline-level))
10290 (org-end-of-subtree t t))
10291 (org-back-over-empty-lines)
10292 (insert "\n" (make-string
10293 (if pos (org-get-valid-level level 1) 1) ?*)
10295 (beginning-of-line 0)
10296 (list (concat (car parent-target) "/" child) file "" (point)))))))
10298 (defun org-olpath-completing-read (prompt collection &rest args)
10299 "Read an outline path like a file name."
10300 (let ((thetable collection)
10301 (org-completion-use-ido nil) ; does not work with ido.
10302 (org-completion-use-iswitchb nil)) ; or iswitchb
10304 'org-icompleting-read prompt
10305 (lambda (string predicate &optional flag)
10306 (let (rtn r f (l (length string)))
10310 (try-completion string thetable))
10313 (setq rtn (all-completions string thetable predicate))
10316 (setq r (substring x l))
10317 (if (string-match " ([^)]*)$" x)
10318 (setq f (match-string 0 x))
10320 (if (string-match "/" r)
10321 (concat string (substring r 0 (match-end 0)) f)
10326 (assoc string thetable)))
10330 ;;;; Dynamic blocks
10332 (defun org-find-dblock (name)
10333 "Find the first dynamic block with name NAME in the buffer.
10334 If not found, stay at current position and return nil."
10337 (goto-char (point-min))
10338 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
10340 (match-beginning 0))))
10341 (if pos (goto-char pos))
10344 (defconst org-dblock-start-re
10345 "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
10346 "Matches the start line of a dynamic block, with parameters.")
10348 (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
10349 "Matches the end of a dynamic block.")
10351 (defun org-create-dblock (plist)
10352 "Create a dynamic block section, with parameters taken from PLIST.
10353 PLIST must contain a :name entry which is used as name of the block."
10354 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
10357 (let ((col (current-column))
10358 (name (plist-get plist :name)))
10359 (insert "#+BEGIN: " name)
10361 (if (eq (car plist) :name)
10362 (setq plist (cddr plist))
10363 (insert " " (prin1-to-string (pop plist)))))
10364 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
10365 (beginning-of-line -2)))
10367 (defun org-prepare-dblock ()
10368 "Prepare dynamic block for refresh.
10369 This empties the block, puts the cursor at the insert position and returns
10370 the property list including an extra property :name with the block name."
10371 (unless (looking-at org-dblock-start-re)
10372 (error "Not at a dynamic block"))
10373 (let* ((begdel (1+ (match-end 0)))
10374 (name (org-no-properties (match-string 1)))
10375 (params (append (list :name name)
10376 (read (concat "(" (match-string 3) ")")))))
10378 (beginning-of-line 1)
10379 (skip-chars-forward " \t")
10380 (setq params (plist-put params :indentation-column (current-column))))
10381 (unless (re-search-forward org-dblock-end-re nil t)
10382 (error "Dynamic block not terminated"))
10385 (list :content (buffer-substring
10386 begdel (match-beginning 0)))))
10387 (delete-region begdel (match-beginning 0))
10392 (defun org-map-dblocks (&optional command)
10393 "Apply COMMAND to all dynamic blocks in the current buffer.
10394 If COMMAND is not given, use `org-update-dblock'."
10395 (let ((cmd (or command 'org-update-dblock)))
10397 (goto-char (point-min))
10398 (while (re-search-forward org-dblock-start-re nil t)
10399 (goto-char (match-beginning 0))
10401 (condition-case nil
10403 (error (message "Error during update of dynamic block"))))
10404 (unless (re-search-forward org-dblock-end-re nil t)
10405 (error "Dynamic block not terminated"))))))
10407 (defun org-dblock-update (&optional arg)
10408 "User command for updating dynamic blocks.
10409 Update the dynamic block at point. With prefix ARG, update all dynamic
10410 blocks in the buffer."
10413 (org-update-all-dblocks)
10414 (or (looking-at org-dblock-start-re)
10415 (org-beginning-of-dblock))
10416 (org-update-dblock)))
10418 (defun org-update-dblock ()
10419 "Update the dynamic block at point.
10420 This means to empty the block, parse for parameters and then call
10421 the correct writing function."
10422 (save-window-excursion
10423 (let* ((pos (point))
10424 (line (org-current-line))
10425 (params (org-prepare-dblock))
10426 (name (plist-get params :name))
10427 (indent (plist-get params :indentation-column))
10428 (cmd (intern (concat "org-dblock-write:" name))))
10429 (message "Updating dynamic block `%s' at line %d..." name line)
10430 (funcall cmd params)
10431 (message "Updating dynamic block `%s' at line %d...done" name line)
10433 (when (and indent (> indent 0))
10434 (setq indent (make-string indent ?\ ))
10436 (org-beginning-of-dblock)
10438 (while (not (looking-at org-dblock-end-re))
10440 (beginning-of-line 2))
10441 (when (looking-at org-dblock-end-re)
10442 (and (looking-at "[ \t]+")
10443 (replace-match ""))
10444 (insert indent)))))))
10446 (defun org-beginning-of-dblock ()
10447 "Find the beginning of the dynamic block at point.
10448 Error if there is no such block at point."
10449 (let ((pos (point))
10452 (if (and (re-search-backward org-dblock-start-re nil t)
10453 (setq beg (match-beginning 0))
10454 (re-search-forward org-dblock-end-re nil t)
10455 (> (match-end 0) pos))
10458 (error "Not in a dynamic block"))))
10460 (defun org-update-all-dblocks ()
10461 "Update all dynamic blocks in the buffer.
10462 This function can be used in a hook."
10464 (org-map-dblocks 'org-update-dblock)))
10469 (defconst org-additional-option-like-keywords
10470 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
10471 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
10472 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
10473 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
10475 "ORGTBL" "TBLFM:" "TBLNAME:"
10476 "BEGIN_EXAMPLE" "END_EXAMPLE"
10477 "BEGIN_QUOTE" "END_QUOTE"
10478 "BEGIN_VERSE" "END_VERSE"
10479 "BEGIN_CENTER" "END_CENTER"
10480 "BEGIN_SRC" "END_SRC"
10481 "CATEGORY" "COLUMNS" "PROPERTY"
10487 (defcustom org-structure-template-alist
10489 ("s" "#+begin_src ?\n\n#+end_src"
10490 "<src lang=\"?\">\n\n</src>")
10491 ("e" "#+begin_example\n?\n#+end_example"
10492 "<example>\n?\n</example>")
10493 ("q" "#+begin_quote\n?\n#+end_quote"
10494 "<quote>\n?\n</quote>")
10495 ("v" "#+begin_verse\n?\n#+end_verse"
10496 "<verse>\n?\n/verse>")
10497 ("c" "#+begin_center\n?\n#+end_center"
10498 "<center>\n?\n/center>")
10499 ("l" "#+begin_latex\n?\n#+end_latex"
10500 "<literal style=\"latex\">\n?\n</literal>")
10502 "<literal style=\"latex\">?</literal>")
10503 ("h" "#+begin_html\n?\n#+end_html"
10504 "<literal style=\"html\">\n?\n</literal>")
10506 "<literal style=\"html\">?</literal>")
10507 ("a" "#+begin_ascii\n?\n#+end_ascii")
10509 ("i" "#+include %file ?"
10510 "<include file=%file markup=\"?\">")
10512 "Structure completion elements.
10513 This is a list of abbreviation keys and values. The value gets inserted
10514 if you type `<' followed by the key and then press the completion key,
10515 usually `M-TAB'. %file will be replaced by a file name after prompting
10516 for the file using completion.
10517 There are two templates for each key, the first uses the original Org syntax,
10518 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
10519 the default when the /org-mtags.el/ module has been loaded. See also the
10520 variable `org-mtags-prefer-muse-templates'.
10521 This is an experimental feature, it is undecided if it is going to stay in."
10522 :group 'org-completion
10524 (string :tag "Key")
10525 (string :tag "Template")
10526 (string :tag "Muse Template")))
10528 (defun org-try-structure-completion ()
10529 "Try to complete a structure template before point.
10530 This looks for strings like \"<e\" on an otherwise empty line and
10532 (let ((l (buffer-substring (point-at-bol) (point)))
10534 (when (and (looking-at "[ \t]*$")
10535 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
10536 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
10537 (org-complete-expand-structure-template (+ -1 (point-at-bol)
10538 (match-beginning 1)) a)
10541 (defun org-complete-expand-structure-template (start cell)
10542 "Expand a structure template."
10543 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
10544 (rpl (nth (if musep 2 1) cell))
10546 (delete-region start (point))
10547 (when (string-match "\\`#\\+" rpl)
10550 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
10551 (setq ind (buffer-substring (point-at-bol) (point))))
10553 (setq start (point))
10554 (if (string-match "%file" rpl)
10555 (setq rpl (replace-match
10559 (abbreviate-file-name (read-file-name "Include file: ")))
10562 (setq rpl (mapconcat 'identity (split-string rpl "\n")
10563 (concat "\n" ind)))
10565 (if (re-search-backward "\\?" start t) (delete-char 1))))
10568 (defun org-complete (&optional arg)
10569 "Perform completion on word at point.
10570 At the beginning of a headline, this completes TODO keywords as given in
10571 `org-todo-keywords'.
10572 If the current word is preceded by a backslash, completes the TeX symbols
10573 that are supported for HTML support.
10574 If the current word is preceded by \"#+\", completes special words for
10575 setting file options.
10576 In the line after \"#+STARTUP:, complete valid keywords.\"
10577 At all other locations, this simply calls the value of
10578 `org-completion-fallback-command'."
10580 (org-without-partial-completion
10584 (beg1 (save-excursion
10585 (skip-chars-backward (org-re "[:alnum:]_@#%"))
10587 (beg (save-excursion
10588 (skip-chars-backward "a-zA-Z0-9_:$")
10590 (confirm (lambda (x) (stringp (car x))))
10591 (searchhead (equal (char-before beg) ?*))
10593 (when (and (member (char-before beg1) '(?. ?<))
10594 (setq a (assoc (buffer-substring beg1 (point))
10595 org-structure-template-alist)))
10596 (org-complete-expand-structure-template (1- beg1) a)
10598 (tag (and (equal (char-before beg1) ?:)
10599 (equal (char-after (point-at-bol)) ?*)))
10600 (prop (or (and (equal (char-before beg1) ?:)
10601 (not (equal (char-after (point-at-bol)) ?*)))
10602 (string-match "^#\\+PROPERTY:.*"
10603 (buffer-substring (point-at-bol) (point)))))
10604 (texp (equal (char-before beg) ?\\))
10605 (link (equal (char-before beg) ?\[))
10606 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
10609 (startup (string-match "^#\\+STARTUP:.*"
10610 (buffer-substring (point-at-bol) (point))))
10611 (completion-ignore-case opt)
10623 "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
10624 (cons (match-string 2 x)
10625 (match-string 1 x))))
10626 (org-split-string (org-get-current-options) "\n")))
10627 (mapcar 'list org-additional-option-like-keywords)))
10629 (setq type :startup)
10630 org-startup-options)
10631 (link (append org-link-abbrev-alist-local
10632 org-link-abbrev-alist))
10635 (append org-entities-user org-entities))
10636 ((string-match "\\`\\*+[ \t]+\\'"
10637 (buffer-substring (point-at-bol) beg))
10639 (mapcar 'list org-todo-keywords-1))
10641 (setq type :searchhead)
10643 (goto-char (point-min))
10644 (while (re-search-forward org-todo-line-regexp nil t)
10646 (org-make-org-heading-search-string
10647 (match-string 3) t))
10650 (tag (setq type :tag beg beg1)
10651 (or org-tag-alist (org-get-buffer-tags)))
10652 (prop (setq type :prop beg beg1)
10653 (mapcar 'list (org-buffer-property-keys nil t t)))
10655 (call-interactively org-completion-fallback-command)
10656 (throw 'exit nil)))))
10657 (pattern (buffer-substring-no-properties beg end))
10658 (completion (try-completion pattern table confirm)))
10659 (cond ((eq completion t)
10660 (if (not (assoc (upcase pattern) table))
10661 (message "Already complete")
10662 (if (and (equal type :opt)
10663 (not (member (car (assoc (upcase pattern) table))
10664 org-additional-option-like-keywords)))
10665 (insert (substring (cdr (assoc (upcase pattern) table))
10667 (if (memq type '(:tag :prop)) (insert ":")))))
10669 (message "Can't find completion for \"%s\"" pattern)
10671 ((not (string= pattern completion))
10672 (delete-region beg end)
10673 (if (string-match " +$" completion)
10674 (setq completion (replace-match "" t t completion)))
10675 (insert completion)
10676 (if (get-buffer-window "*Completions*")
10677 (delete-window (get-buffer-window "*Completions*")))
10678 (if (assoc completion table)
10679 (if (eq type :todo) (insert " ")
10680 (if (and (memq type '(:tag :prop))
10681 (not (string-match "^#[ \t]*\\+property:"
10682 (org-current-line-string t))))
10684 (if (and (equal type :opt) (assoc completion table))
10685 (message "%s" (substitute-command-keys
10686 "Press \\[org-complete] again to insert example settings"))))
10688 (message "Making completion list...")
10689 (let ((list (sort (all-completions pattern table confirm)
10691 (with-output-to-temp-buffer "*Completions*"
10692 (condition-case nil
10693 ;; Protection needed for XEmacs and emacs 21
10694 (display-completion-list list pattern)
10695 (error (display-completion-list list)))))
10696 (message "Making completion list...%s" "done")))))))
10698 ;;;; TODO, DEADLINE, Comments
10700 (defun org-toggle-comment ()
10701 "Change the COMMENT state of an entry."
10704 (org-back-to-heading)
10705 (let (case-fold-search)
10706 (if (looking-at (concat outline-regexp
10707 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
10708 (replace-match "" t t nil 1)
10709 (if (looking-at outline-regexp)
10711 (goto-char (match-end 0))
10712 (insert org-comment-string " ")))))))
10714 (defvar org-last-todo-state-is-todo nil
10715 "This is non-nil when the last TODO state change led to a TODO state.
10716 If the last change removed the TODO tag or switched to DONE, then
10719 (defvar org-setting-tags nil) ; dynamically skipped
10721 (defvar org-todo-setup-filter-hook nil
10722 "Hook for functions that pre-filter todo specs.
10723 Each function takes a todo spec and returns either nil or the spec
10724 transformed into canonical form." )
10726 (defvar org-todo-get-default-hook nil
10727 "Hook for functions that get a default item for todo.
10728 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
10729 nil or a string to be used for the todo mark." )
10731 (defvar org-agenda-headline-snapshot-before-repeat)
10733 (defun org-todo (&optional arg)
10734 "Change the TODO state of an item.
10735 The state of an item is given by a keyword at the start of the heading,
10737 *** TODO Write paper
10740 The different keywords are specified in the variable `org-todo-keywords'.
10741 By default the available states are \"TODO\" and \"DONE\".
10742 So for this example: when the item starts with TODO, it is changed to DONE.
10743 When it starts with DONE, the DONE is removed. And when neither TODO nor
10744 DONE are present, add TODO at the beginning of the heading.
10746 With \\[universal-argument] prefix arg, use completion to determine the new \
10748 With numeric prefix arg, switch to that state.
10749 With a double \\[universal-argument] prefix, switch to the next set of TODO \
10750 keywords (nextset).
10751 With a triple \\[universal-argument] prefix, circumvent any state blocking.
10753 For calling through lisp, arg is also interpreted in the following way:
10754 'none -> empty state
10755 \"\"(empty string) -> switch to empty state
10756 'done -> switch to DONE
10757 'nextset -> switch to the next set of keywords
10758 'previousset -> switch to the previous set of keywords
10759 \"WAITING\" -> switch to the specified keyword, but only if it
10760 really is a member of `org-todo-keywords'."
10762 (if (equal arg '(16)) (setq arg 'nextset))
10763 (let ((org-blocker-hook org-blocker-hook)
10764 (case-fold-search nil))
10765 (when (equal arg '(64))
10766 (setq arg nil org-blocker-hook nil))
10767 (when (and org-blocker-hook
10768 (or org-inhibit-blocking
10769 (org-entry-get nil "NOBLOCKING")))
10770 (setq org-blocker-hook nil))
10773 (org-back-to-heading t)
10774 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
10775 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
10777 (let* ((match-data (match-data))
10778 (startpos (point-at-bol))
10779 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
10780 (org-log-done org-log-done)
10781 (org-log-repeat org-log-repeat)
10782 (org-todo-log-states org-todo-log-states)
10783 (this (match-string 1))
10784 (hl-pos (match-beginning 0))
10785 (head (org-get-todo-sequence-head this))
10786 (ass (assoc head org-todo-kwd-alist))
10787 (interpret (nth 1 ass))
10788 (done-word (nth 3 ass))
10789 (final-done-word (nth 4 ass))
10790 (last-state (or this ""))
10791 (completion-ignore-case t)
10792 (member (member this org-todo-keywords-1))
10793 (tail (cdr member))
10795 ((and org-todo-key-trigger
10796 (or (and (equal arg '(4))
10797 (eq org-use-fast-todo-selection 'prefix))
10798 (and (not arg) org-use-fast-todo-selection
10799 (not (eq org-use-fast-todo-selection
10801 ;; Use fast selection
10802 (org-fast-todo-selection))
10803 ((and (equal arg '(4))
10804 (or (not org-use-fast-todo-selection)
10805 (not org-todo-key-trigger)))
10806 ;; Read a state with completion
10807 (org-icompleting-read
10808 "State: " (mapcar (lambda(x) (list x))
10809 org-todo-keywords-1)
10813 (if tail (car tail) nil)
10814 (car org-todo-keywords-1)))
10816 (if (equal member org-todo-keywords-1)
10819 (nth (- (length org-todo-keywords-1)
10821 org-todo-keywords-1)
10822 (org-last org-todo-keywords-1))))
10823 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
10824 (setq arg nil))) ; hack to fall back to cycling
10826 ;; user or caller requests a specific state
10828 ((equal arg "") nil)
10829 ((eq arg 'none) nil)
10830 ((eq arg 'done) (or done-word (car org-done-keywords)))
10832 (or (car (cdr (member head org-todo-heads)))
10833 (car org-todo-heads)))
10834 ((eq arg 'previousset)
10835 (let ((org-todo-heads (reverse org-todo-heads)))
10836 (or (car (cdr (member head org-todo-heads)))
10837 (car org-todo-heads))))
10838 ((car (member arg org-todo-keywords-1)))
10840 (error "State `%s' not valid in this file" arg))
10841 ((nth (1- (prefix-numeric-value arg))
10842 org-todo-keywords-1))))
10843 ((null member) (or head (car org-todo-keywords-1)))
10844 ((equal this final-done-word) nil) ;; -> make empty
10845 ((null tail) nil) ;; -> first entry
10846 ((memq interpret '(type priority))
10847 (if (eq this-command last-command)
10849 (if (> (length tail) 0)
10850 (or done-word (car org-done-keywords))
10855 (run-hook-with-args-until-success
10856 'org-todo-get-default-hook state last-state)
10858 (next (if state (concat " " state " ") " "))
10859 (change-plist (list :type 'todo-state-change :from this :to state
10860 :position startpos))
10862 (when org-blocker-hook
10863 (setq org-last-todo-state-is-todo
10864 (not (member this org-done-keywords)))
10865 (unless (save-excursion
10867 (run-hook-with-args-until-failure
10868 'org-blocker-hook change-plist)))
10869 (if (interactive-p)
10870 (error "TODO state change from %s to %s blocked" this state)
10872 (message "TODO state change from %s to %s blocked" this state)
10873 (throw 'exit nil))))
10874 (store-match-data match-data)
10875 (replace-match next t t)
10876 (unless (pos-visible-in-window-p hl-pos)
10877 (message "TODO state changed to %s" (org-trim next)))
10879 (setq head (org-get-todo-sequence-head state)
10880 ass (assoc head org-todo-kwd-alist)
10881 interpret (nth 1 ass)
10882 done-word (nth 3 ass)
10883 final-done-word (nth 4 ass)))
10884 (when (memq arg '(nextset previousset))
10885 (message "Keyword-Set %d/%d: %s"
10886 (- (length org-todo-sets) -1
10887 (length (memq (assoc state org-todo-sets) org-todo-sets)))
10888 (length org-todo-sets)
10889 (mapconcat 'identity (assoc state org-todo-sets) " ")))
10890 (setq org-last-todo-state-is-todo
10891 (not (member state org-done-keywords)))
10892 (setq now-done-p (and (member state org-done-keywords)
10893 (not (member this org-done-keywords))))
10894 (and logging (org-local-logging logging))
10895 (when (and (or org-todo-log-states org-log-done)
10896 (not (eq org-inhibit-logging t))
10897 (not (memq arg '(nextset previousset))))
10898 ;; we need to look at recording a time and note
10899 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
10900 (nth 2 (assoc this org-todo-log-states))))
10901 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
10902 (setq dolog 'time))
10904 (member state org-not-done-keywords)
10905 (not (member this org-not-done-keywords)))
10906 ;; This is now a todo state and was not one before
10907 ;; If there was a CLOSED time stamp, get rid of it.
10908 (org-add-planning-info nil nil 'closed))
10909 (when (and now-done-p org-log-done)
10910 ;; It is now done, and it was not done before
10911 (org-add-planning-info 'closed (org-current-time))
10912 (if (and (not dolog) (eq 'note org-log-done))
10913 (org-add-log-setup 'done state this 'findpos 'note)))
10914 (when (and state dolog)
10915 ;; This is a non-nil state, and we need to log it
10916 (org-add-log-setup 'state state this 'findpos dolog)))
10917 ;; Fixup tag positioning
10918 (org-todo-trigger-tag-changes state)
10919 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
10920 (when org-provide-todo-statistics
10921 (org-update-parent-todo-statistics))
10922 (run-hooks 'org-after-todo-state-change-hook)
10923 (if (and arg (not (member state org-done-keywords)))
10924 (setq head (org-get-todo-sequence-head state)))
10925 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
10926 ;; Do we need to trigger a repeat?
10928 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
10929 ;; This is for the agenda, take a snapshot of the headline.
10931 (setq org-agenda-headline-snapshot-before-repeat
10932 (org-get-heading))))
10933 (org-auto-repeat-maybe state))
10934 ;; Fixup cursor location if close to the keyword
10935 (if (and (outline-on-heading-p)
10937 (save-excursion (beginning-of-line 1)
10938 (looking-at org-todo-line-regexp))
10939 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
10941 (goto-char (or (match-end 2) (match-end 1)))
10942 (and (looking-at " ") (just-one-space))))
10943 (when org-trigger-hook
10945 (run-hook-with-args 'org-trigger-hook change-plist))))))))
10947 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
10948 "Block turning an entry into a TODO, using the hierarchy.
10949 This checks whether the current task should be blocked from state
10950 changes. Such blocking occurs when:
10952 1. The task has children which are not all in a completed state.
10954 2. A task has a parent with the property :ORDERED:, and there
10955 are siblings prior to the current task with incomplete
10958 3. The parent of the task is blocked because it has siblings that should
10959 be done first, or is child of a block grandparent TODO entry."
10961 (if (not org-enforce-todo-dependencies)
10962 t ; if locally turned off don't block
10964 ;; If this is not a todo state change, or if this entry is already DONE,
10966 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
10967 (member (plist-get change-plist :from)
10968 (cons 'done org-done-keywords))
10969 (member (plist-get change-plist :to)
10970 (cons 'todo org-not-done-keywords))
10971 (not (plist-get change-plist :to)))
10972 (throw 'dont-block t))
10973 ;; If this task has children, and any are undone, it's blocked
10975 (org-back-to-heading t)
10976 (let ((this-level (funcall outline-level)))
10977 (outline-next-heading)
10978 (let ((child-level (funcall outline-level)))
10979 (while (and (not (eobp))
10980 (> child-level this-level))
10981 ;; this todo has children, check whether they are all
10983 (if (and (not (org-entry-is-done-p))
10984 (org-entry-is-todo-p))
10985 (throw 'dont-block nil))
10986 (outline-next-heading)
10987 (setq child-level (funcall outline-level))))))
10988 ;; Otherwise, if the task's parent has the :ORDERED: property, and
10989 ;; any previous siblings are undone, it's blocked
10991 (org-back-to-heading t)
10992 (let* ((pos (point))
10993 (parent-pos (and (org-up-heading-safe) (point))))
10994 (if (not parent-pos) (throw 'dont-block t)) ; no parent
10995 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
10997 (re-search-forward org-not-done-heading-regexp pos t))
10998 (throw 'dont-block nil)) ; block, there is an older sibling not done.
10999 ;; Search further up the hierarchy, to see if an anchestor is blocked
11001 (goto-char parent-pos)
11002 (if (not (looking-at org-not-done-heading-regexp))
11003 (throw 'dont-block t)) ; do not block, parent is not a TODO
11005 (setq parent-pos (and (org-up-heading-safe) (point)))
11006 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11007 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11009 (re-search-forward org-not-done-heading-regexp pos t))
11010 (throw 'dont-block nil)))))))) ; block, older sibling not done.
11012 (defcustom org-track-ordered-property-with-tag nil
11013 "Should the ORDERED property also be shown as a tag?
11014 The ORDERED property decides if an entry should require subtasks to be
11015 completed in sequence. Since a property is not very visible, setting
11016 this option means that toggling the ORDERED property with the command
11017 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
11018 not relevant for the behavior, but it makes things more visible.
11020 Note that toggling the tag with tags commands will not change the property
11021 and therefore not influence behavior!
11023 This can be t, meaning the tag ORDERED should be used, It can also be a
11024 string to select a different tag for this task."
11027 (const :tag "No tracking" nil)
11028 (const :tag "Track with ORDERED tag" t)
11029 (string :tag "Use other tag")))
11031 (defun org-toggle-ordered-property ()
11032 "Toggle the ORDERED property of the current entry.
11033 For better visibility, you can track the value of this property with a tag.
11034 See variable `org-track-ordered-property-with-tag'."
11036 (let* ((t1 org-track-ordered-property-with-tag)
11037 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
11039 (org-back-to-heading)
11040 (if (org-entry-get nil "ORDERED")
11042 (org-delete-property "ORDERED")
11043 (and tag (org-toggle-tag tag 'off))
11044 (message "Subtasks can be completed in arbitrary order"))
11045 (org-entry-put nil "ORDERED" "t")
11046 (and tag (org-toggle-tag tag 'on))
11047 (message "Subtasks must be completed in sequence")))))
11049 (defvar org-blocked-by-checkboxes) ; dynamically scoped
11050 (defun org-block-todo-from-checkboxes (change-plist)
11051 "Block turning an entry into a TODO, using checkboxes.
11052 This checks whether the current task should be blocked from state
11053 changes because there are unchecked boxes in this entry."
11054 (if (not org-enforce-todo-checkbox-dependencies)
11055 t ; if locally turned off don't block
11057 ;; If this is not a todo state change, or if this entry is already DONE,
11059 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11060 (member (plist-get change-plist :from)
11061 (cons 'done org-done-keywords))
11062 (member (plist-get change-plist :to)
11063 (cons 'todo org-not-done-keywords))
11064 (not (plist-get change-plist :to)))
11065 (throw 'dont-block t))
11066 ;; If this task has checkboxes that are not checked, it's blocked
11068 (org-back-to-heading t)
11069 (let ((beg (point)) end)
11070 (outline-next-heading)
11073 (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
11076 (if (boundp 'org-blocked-by-checkboxes)
11077 (setq org-blocked-by-checkboxes t))
11078 (throw 'dont-block nil)))))
11079 t))) ; do not block
11081 (defun org-entry-blocked-p ()
11082 "Is the current entry blocked?"
11083 (if (org-entry-get nil "NOBLOCKING")
11084 nil ;; Never block this entry
11086 (run-hook-with-args-until-failure
11088 (list :type 'todo-state-change
11093 (defun org-update-statistics-cookies (all)
11094 "Update the statistics cookie, either from TODO or from checkboxes.
11095 This should be called with the cursor in a line with a statistics cookie."
11099 (org-update-checkbox-count 'all)
11100 (org-map-entries 'org-update-parent-todo-statistics))
11101 (if (not (org-on-heading-p))
11102 (org-update-checkbox-count)
11103 (let ((pos (move-marker (make-marker) (point)))
11105 (ignore-errors (org-back-to-heading t))
11106 (if (not (org-on-heading-p))
11107 (org-update-checkbox-count)
11108 (setq l1 (org-outline-level))
11109 (setq end (save-excursion
11110 (outline-next-heading)
11111 (if (org-on-heading-p) (setq l2 (org-outline-level)))
11113 (if (and (save-excursion
11115 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
11116 (not (save-excursion (re-search-forward
11117 ":COOKIE_DATA:.*\\<todo\\>" end t))))
11118 (org-update-checkbox-count)
11119 (if (and l2 (> l2 l1))
11122 (org-update-parent-todo-statistics))
11124 (beginning-of-line 1)
11125 (while (re-search-forward
11126 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
11128 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
11130 (move-marker pos nil)))))
11132 (defvar org-entry-property-inherited-from) ;; defined below
11133 (defun org-update-parent-todo-statistics ()
11134 "Update any statistics cookie in the parent of the current headline.
11135 When `org-hierarchical-todo-statistics' is nil, statistics will cover
11136 the entire subtree and this will travel up the hierarchy and update
11137 statistics everywhere."
11139 (let* ((lim 0) prop
11140 (recursive (or (not org-hierarchical-todo-statistics)
11143 (or (setq prop (org-entry-get
11144 nil "COOKIE_DATA" 'inherit)) ""))))
11145 (lim (or (and prop (marker-position
11146 org-entry-property-inherited-from))
11149 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
11150 level ltoggle l1 new ndel
11151 (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
11154 (beginning-of-line 1)
11155 (if (org-at-heading-p)
11156 (setq ltoggle (funcall outline-level))
11157 (error "This should not happen"))
11158 (while (and (setq level (org-up-heading-safe))
11159 (or recursive first)
11161 (setq first nil cookie-present nil)
11170 (while (re-search-forward box-re (point-at-eol) t)
11171 (setq cnt-all 0 cnt-done 0 cookie-present t)
11172 (setq is-percent (match-end 2))
11174 (unless (outline-next-heading) (throw 'exit nil))
11175 (while (and (looking-at org-complex-heading-regexp)
11176 (> (setq l1 (length (match-string 1))) level))
11177 (setq kwd (and (or recursive (= l1 ltoggle))
11179 (if (or (eq org-provide-todo-statistics 'all-headlines)
11180 (and (listp org-provide-todo-statistics)
11181 (or (member kwd org-provide-todo-statistics)
11182 (member kwd org-done-keywords))))
11183 (setq cnt-all (1+ cnt-all))
11184 (if (eq org-provide-todo-statistics t)
11185 (and kwd (setq cnt-all (1+ cnt-all)))))
11186 (and (member kwd org-done-keywords)
11187 (setq cnt-done (1+ cnt-done)))
11188 (outline-next-heading)))
11191 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
11192 (format "[%d/%d]" cnt-done cnt-all))
11193 ndel (- (match-end 0) (match-beginning 0)))
11194 (goto-char (match-beginning 0))
11196 (delete-region (point) (+ (point) ndel)))
11197 (when cookie-present
11198 (run-hook-with-args 'org-after-todo-statistics-hook
11199 cnt-done (- cnt-all cnt-done))))))
11200 (run-hooks 'org-todo-statistics-hook)))
11202 (defvar org-after-todo-statistics-hook nil
11203 "Hook that is called after a TODO statistics cookie has been updated.
11204 Each function is called with two arguments: the number of not-done entries
11205 and the number of done entries.
11207 For example, the following function, when added to this hook, will switch
11208 an entry to DONE when all children are done, and back to TODO when new
11209 entries are set to a TODO status. Note that this hook is only called
11210 when there is a statistics cookie in the headline!
11212 (defun org-summary-todo (n-done n-not-done)
11213 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
11214 (let (org-log-done org-log-states) ; turn off logging
11215 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
11218 (defvar org-todo-statistics-hook nil
11219 "Hook that is run whenever Org thinks TODO statistics should be updated.
11220 This hook runs even if there is no statistics cookie present, in which case
11221 `org-after-todo-statistics-hook' would not run.")
11223 (defun org-todo-trigger-tag-changes (state)
11224 "Apply the changes defined in `org-todo-state-tags-triggers'."
11225 (let ((l org-todo-state-tags-triggers)
11227 (when (or (not state) (equal state ""))
11228 (setq changes (append changes (cdr (assoc "" l)))))
11229 (when (and (stringp state) (> (length state) 0))
11230 (setq changes (append changes (cdr (assoc state l)))))
11231 (when (member state org-not-done-keywords)
11232 (setq changes (append changes (cdr (assoc 'todo l)))))
11233 (when (member state org-done-keywords)
11234 (setq changes (append changes (cdr (assoc 'done l)))))
11235 (dolist (c changes)
11236 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
11238 (defun org-local-logging (value)
11239 "Get logging settings from a property VALUE."
11241 ;; directly set the variables, they are already local.
11242 (setq org-log-done nil
11244 org-todo-log-states nil)
11245 (setq words (org-split-string value))
11246 (while (setq w (pop words))
11248 ((setq a (assoc w org-startup-options))
11249 (and (member (nth 1 a) '(org-log-done org-log-repeat))
11250 (set (nth 1 a) (nth 2 a))))
11251 ((setq a (org-extract-log-state-settings w))
11252 (and (member (car a) org-todo-keywords-1)
11253 (push a org-todo-log-states)))))))
11255 (defun org-get-todo-sequence-head (kwd)
11256 "Return the head of the TODO sequence to which KWD belongs.
11257 If KWD is not set, check if there is a text property remembering the
11262 (or (get-text-property (point-at-bol) 'org-todo-head)
11264 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
11265 nil (point-at-eol)))
11266 (get-text-property p 'org-todo-head))))
11267 ((not (member kwd org-todo-keywords-1))
11268 (car org-todo-keywords-1))
11269 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
11271 (defun org-fast-todo-selection ()
11272 "Fast TODO keyword selection with single keys.
11273 Returns the new TODO keyword, or nil if no state change should occur."
11274 (let* ((fulltable org-todo-key-alist)
11275 (done-keywords org-done-keywords) ;; needed for the faces.
11276 (maxlen (apply 'max (mapcar
11278 (if (stringp (car x)) (string-width (car x)) 0))
11281 (fwidth (+ maxlen 3 1 3))
11282 (ncol (/ (- (window-width) 4) fwidth))
11286 (save-window-excursion
11288 (set-buffer (get-buffer-create " *Org todo*"))
11289 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
11291 (org-set-local 'org-done-keywords done-keywords)
11292 (setq tbl fulltable cnt 0)
11293 (while (setq e (pop tbl))
11295 ((equal e '(:startgroup))
11296 (push '() groups) (setq ingroup t)
11297 (when (not (= cnt 0))
11301 ((equal e '(:endgroup))
11302 (setq ingroup nil cnt 0)
11304 ((equal e '(:newline))
11305 (when (not (= cnt 0))
11309 (while (equal (car tbl) '(:newline))
11311 (setq tbl (cdr tbl)))))
11313 (setq tg (car e) c (cdr e))
11314 (if ingroup (push tg (car groups)))
11315 (setq tg (org-add-props tg nil 'face
11316 (org-get-todo-face tg)))
11317 (if (and (= cnt 0) (not ingroup)) (insert " "))
11318 (insert "[" c "] " tg (make-string
11319 (- fwidth 4 (length tg)) ?\ ))
11320 (when (= (setq cnt (1+ cnt)) ncol)
11322 (if ingroup (insert " "))
11325 (goto-char (point-min))
11326 (if (not expert) (org-fit-window-to-buffer))
11327 (message "[a-z..]:Set [SPC]:clear")
11328 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
11331 (and (= c ?q) (not (rassoc c fulltable))))
11332 (setq quit-flag t))
11334 ((setq e (rassoc c fulltable) tg (car e))
11336 (t (setq quit-flag t)))))))
11338 (defun org-entry-is-todo-p ()
11339 (member (org-get-todo-state) org-not-done-keywords))
11341 (defun org-entry-is-done-p ()
11342 (member (org-get-todo-state) org-done-keywords))
11344 (defun org-get-todo-state ()
11346 (org-back-to-heading t)
11347 (and (looking-at org-todo-line-regexp)
11349 (match-string 2))))
11351 (defun org-at-date-range-p (&optional inactive-ok)
11352 "Is the cursor inside a date range?"
11356 (let ((pos (point)))
11357 (skip-chars-backward "^[<\r\n")
11358 (skip-chars-backward "<[")
11359 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
11360 (>= (match-end 0) pos)
11362 (skip-chars-backward "^<[\r\n")
11363 (skip-chars-backward "<[")
11364 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
11365 (>= (match-end 0) pos)
11369 (defun org-get-repeat (&optional tagline)
11370 "Check if there is a deadline/schedule with repeater in this entry."
11373 (org-back-to-heading t)
11374 (and (re-search-forward (if tagline
11375 (concat tagline "\\s-*" org-repeat-re)
11377 (org-entry-end-position) t)
11378 (match-string-no-properties 1)))))
11380 (defvar org-last-changed-timestamp)
11381 (defvar org-last-inserted-timestamp)
11382 (defvar org-log-post-message)
11383 (defvar org-log-note-purpose)
11384 (defvar org-log-note-how)
11385 (defvar org-log-note-extra)
11386 (defun org-auto-repeat-maybe (done-word)
11387 "Check if the current headline contains a repeated deadline/schedule.
11388 If yes, set TODO state back to what it was and change the base date
11389 of repeating deadline/scheduled time stamps to new date.
11390 This function is run automatically after each state change to a DONE state."
11391 ;; last-state is dynamically scoped into this function
11392 (let* ((repeat (org-get-repeat))
11393 (aa (assoc last-state org-todo-kwd-alist))
11394 (interpret (nth 1 aa))
11396 (whata '(("d" . day) ("m" . month) ("y" . year)))
11397 (msg "Entry repeats: ")
11399 (org-todo-log-states nil)
11400 (nshiftmax 10) (nshift 0)
11401 re type n what ts time to-state)
11403 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
11404 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
11405 org-todo-repeat-to-state))
11406 (unless (and to-state (member to-state org-todo-keywords-1))
11407 (setq to-state (if (eq interpret 'type) last-state head)))
11408 (org-todo to-state)
11409 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
11410 (org-entry-put nil "LAST_REPEAT" (format-time-string
11411 (org-time-stamp-format t t))))
11412 (when org-log-repeat
11413 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
11414 (memq 'org-add-log-note post-command-hook))
11415 ;; OK, we are already setup for some record
11416 (if (eq org-log-repeat 'note)
11417 ;; make sure we take a note, not only a time stamp
11418 (setq org-log-note-how 'note))
11419 ;; Set up for taking a record
11420 (org-add-log-setup 'state (or done-word (car org-done-keywords))
11422 'findpos org-log-repeat)))
11423 (org-back-to-heading t)
11424 (org-add-planning-info nil nil 'closed)
11425 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
11426 org-deadline-time-regexp "\\)\\|\\("
11427 org-ts-regexp "\\)"))
11428 (while (re-search-forward
11429 re (save-excursion (outline-next-heading) (point)) t)
11430 (setq type (if (match-end 1) org-scheduled-string
11431 (if (match-end 3) org-deadline-string "Plain:"))
11432 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
11433 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
11434 (setq n (string-to-number (match-string 2 ts))
11435 what (match-string 3 ts))
11436 (if (equal what "w") (setq n (* n 7) what "d"))
11437 ;; Preparation, see if we need to modify the start date for the change
11438 (when (match-end 1)
11439 (setq time (save-match-data (org-time-string-to-time ts)))
11441 ((equal (match-string 1 ts) ".")
11442 ;; Shift starting date to today
11443 (org-timestamp-change
11444 (- (time-to-days (current-time)) (time-to-days time))
11446 ((equal (match-string 1 ts) "+")
11447 (while (or (= nshift 0)
11448 (<= (time-to-days time) (time-to-days (current-time))))
11449 (when (= (incf nshift) nshiftmax)
11450 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
11452 (org-timestamp-change n (cdr (assoc what whata)))
11453 (org-at-timestamp-p t)
11454 (setq ts (match-string 1))
11455 (setq time (save-match-data (org-time-string-to-time ts))))
11456 (org-timestamp-change (- n) (cdr (assoc what whata)))
11457 ;; rematch, so that we have everything in place for the real shift
11458 (org-at-timestamp-p t)
11459 (setq ts (match-string 1))
11460 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
11461 (org-timestamp-change n (cdr (assoc what whata)))
11462 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
11463 (setq org-log-post-message msg)
11464 (message "%s" msg))))
11466 (defun org-show-todo-tree (arg)
11467 "Make a compact tree which shows all headlines marked with TODO.
11468 The tree will show the lines where the regexp matches, and all higher
11469 headlines above the match.
11470 With a \\[universal-argument] prefix, prompt for a regexp to match.
11471 With a numeric prefix N, construct a sparse tree for the Nth element
11472 of `org-todo-keywords-1'."
11474 (let ((case-fold-search nil)
11476 (cond ((null arg) org-not-done-regexp)
11478 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
11479 (mapcar 'list org-todo-keywords-1))))
11481 (mapconcat 'identity (org-split-string kwd "|") "\\|")
11483 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
11484 (regexp-quote (nth (1- (prefix-numeric-value arg))
11485 org-todo-keywords-1)))
11486 (t (error "Invalid prefix argument: %s" arg)))))
11487 (message "%d TODO entries found"
11488 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
11490 (defun org-deadline (&optional remove time)
11491 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
11492 With argument REMOVE, remove any deadline from the item.
11493 When TIME is set, it should be an internal time specification, and the
11494 scheduling will use the corresponding date."
11496 (let* ((old-date (org-entry-get nil "DEADLINE"))
11497 (repeater (and old-date
11498 (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
11499 (match-string 1 old-date))))
11502 (when (and old-date org-log-redeadline)
11503 (org-add-log-setup 'deldeadline nil old-date 'findpos
11504 org-log-redeadline))
11505 (org-remove-timestamp-with-keyword org-deadline-string)
11506 (message "Item no longer has a deadline."))
11507 (org-add-planning-info 'deadline time 'closed)
11508 (when (and old-date org-log-redeadline
11509 (not (equal old-date
11510 (substring org-last-inserted-timestamp 1 -1))))
11511 (org-add-log-setup 'redeadline nil old-date 'findpos
11512 org-log-redeadline))
11515 (org-back-to-heading t)
11516 (when (re-search-forward (concat org-deadline-string " "
11517 org-last-inserted-timestamp)
11519 (outline-next-heading) (point)) t)
11520 (goto-char (1- (match-end 0)))
11521 (insert " " repeater)
11522 (setq org-last-inserted-timestamp
11523 (concat (substring org-last-inserted-timestamp 0 -1)
11525 (substring org-last-inserted-timestamp -1))))))
11526 (message "Deadline on %s" org-last-inserted-timestamp))))
11528 (defun org-schedule (&optional remove time)
11529 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
11530 With argument REMOVE, remove any scheduling date from the item.
11531 When TIME is set, it should be an internal time specification, and the
11532 scheduling will use the corresponding date."
11534 (let* ((old-date (org-entry-get nil "SCHEDULED"))
11535 (repeater (and old-date
11536 (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
11537 (match-string 1 old-date))))
11540 (when (and old-date org-log-reschedule)
11541 (org-add-log-setup 'delschedule nil old-date 'findpos
11542 org-log-reschedule))
11543 (org-remove-timestamp-with-keyword org-scheduled-string)
11544 (message "Item is no longer scheduled."))
11545 (org-add-planning-info 'scheduled time 'closed)
11546 (when (and old-date org-log-reschedule
11547 (not (equal old-date
11548 (substring org-last-inserted-timestamp 1 -1))))
11549 (org-add-log-setup 'reschedule nil old-date 'findpos
11550 org-log-reschedule))
11553 (org-back-to-heading t)
11554 (when (re-search-forward (concat org-scheduled-string " "
11555 org-last-inserted-timestamp)
11557 (outline-next-heading) (point)) t)
11558 (goto-char (1- (match-end 0)))
11559 (insert " " repeater)
11560 (setq org-last-inserted-timestamp
11561 (concat (substring org-last-inserted-timestamp 0 -1)
11563 (substring org-last-inserted-timestamp -1))))))
11564 (message "Scheduled to %s" org-last-inserted-timestamp))))
11566 (defun org-get-scheduled-time (pom &optional inherit)
11567 "Get the scheduled time as a time tuple, of a format suitable
11568 for calling org-schedule with, or if there is no scheduling,
11570 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
11572 (apply 'encode-time (org-parse-time-string time)))))
11574 (defun org-get-deadline-time (pom &optional inherit)
11575 "Get the deadline as a time tuple, of a format suitable for
11576 calling org-deadline with, or if there is no scheduling, returns
11578 (let ((time (org-entry-get pom "DEADLINE" inherit)))
11580 (apply 'encode-time (org-parse-time-string time)))))
11582 (defun org-remove-timestamp-with-keyword (keyword)
11583 "Remove all time stamps with KEYWORD in the current entry."
11584 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
11587 (org-back-to-heading t)
11589 (outline-next-heading)
11590 (while (re-search-backward re beg t)
11592 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
11593 (equal (char-before) ?\ ))
11594 (backward-delete-char 1)
11595 (if (string-match "^[ \t]*$" (buffer-substring
11596 (point-at-bol) (point-at-eol)))
11597 (delete-region (point-at-bol)
11598 (min (point-max) (1+ (point-at-eol))))))))))
11600 (defun org-add-planning-info (what &optional time &rest remove)
11601 "Insert new timestamp with keyword in the line directly after the headline.
11602 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
11603 If non is given, the user is prompted for a date.
11604 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
11607 (let (org-time-was-given org-end-time-was-given ts
11608 end default-time default-input)
11611 (when (and (not time) (memq what '(scheduled deadline)))
11612 ;; Try to get a default date/time from existing timestamp
11614 (org-back-to-heading t)
11615 (setq end (save-excursion (outline-next-heading) (point)))
11616 (when (re-search-forward (if (eq what 'scheduled)
11617 org-scheduled-time-regexp
11618 org-deadline-time-regexp)
11620 (setq ts (match-string 1)
11622 (apply 'encode-time (org-parse-time-string ts))
11623 default-input (and ts (org-get-compact-tod ts))))))
11625 ;; If necessary, get the time from the user
11626 (setq time (or time (org-read-date nil 'to-time nil nil
11627 default-time default-input))))
11629 (when (and org-insert-labeled-timestamps-at-point
11630 (member what '(scheduled deadline)))
11632 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
11633 (org-insert-time-stamp time org-time-was-given
11634 nil nil nil (list org-end-time-was-given))
11638 (let (col list elt ts buffer-invisibility-spec)
11639 (org-back-to-heading t)
11640 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
11641 (goto-char (match-end 1))
11642 (setq col (current-column))
11643 (goto-char (match-end 0))
11644 (if (eobp) (insert "\n") (forward-char 1))
11645 (when (and (not what)
11648 org-keyword-time-not-clock-regexp))))
11649 ;; Nothing to add, nothing to remove...... :-)
11651 (if (and (not (looking-at outline-regexp))
11652 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
11654 (not (equal (match-string 1) org-clock-string)))
11655 (narrow-to-region (match-beginning 0) (match-end 0))
11656 (insert-before-markers "\n")
11658 (narrow-to-region (point) (point))
11659 (and org-adapt-indentation (org-indent-to-column col)))
11660 ;; Check if we have to remove something.
11661 (setq list (cons what remove))
11663 (setq elt (pop list))
11664 (goto-char (point-min))
11665 (when (or (and (eq elt 'scheduled)
11666 (re-search-forward org-scheduled-time-regexp nil t))
11667 (and (eq elt 'deadline)
11668 (re-search-forward org-deadline-time-regexp nil t))
11669 (and (eq elt 'closed)
11670 (re-search-forward org-closed-time-regexp nil t)))
11672 (if (looking-at "--+<[^>]+>") (replace-match ""))
11673 (skip-chars-backward " ")
11674 (if (looking-at " +") (replace-match ""))))
11675 (goto-char (point-max))
11676 (and org-adapt-indentation (bolp) (org-indent-to-column col))
11679 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
11680 (cond ((eq what 'scheduled) org-scheduled-string)
11681 ((eq what 'deadline) org-deadline-string)
11682 ((eq what 'closed) org-closed-string))
11684 (setq ts (org-insert-time-stamp
11686 (or org-time-was-given
11687 (and (eq what 'closed) org-log-done-with-time))
11689 nil nil (list org-end-time-was-given)))
11691 (goto-char (point-min))
11693 (if (and (looking-at "[ \t]*\n")
11694 (equal (char-before) ?\n))
11695 (delete-region (1- (point)) (point-at-eol)))
11698 (defvar org-log-note-marker (make-marker))
11699 (defvar org-log-note-purpose nil)
11700 (defvar org-log-note-state nil)
11701 (defvar org-log-note-previous-state nil)
11702 (defvar org-log-note-how nil)
11703 (defvar org-log-note-extra nil)
11704 (defvar org-log-note-window-configuration nil)
11705 (defvar org-log-note-return-to (make-marker))
11706 (defvar org-log-post-message nil
11707 "Message to be displayed after a log note has been stored.
11708 The auto-repeater uses this.")
11710 (defun org-add-note ()
11711 "Add a note to the current entry.
11712 This is done in the same way as adding a state change note."
11714 (org-add-log-setup 'note nil nil 'findpos nil))
11716 (defvar org-property-end-re)
11717 (defun org-add-log-setup (&optional purpose state prev-state
11719 "Set up the post command hook to take a note.
11720 If this is about to TODO state change, the new state is expected in STATE.
11721 When FINDPOS is non-nil, find the correct position for the note in
11722 the current entry. If not, assume that it can be inserted at point.
11723 HOW is an indicator what kind of note should be created.
11724 EXTRA is additional text that will be inserted into the notes buffer."
11725 (let* ((org-log-into-drawer (org-log-into-drawer))
11726 (drawer (cond ((stringp org-log-into-drawer)
11727 org-log-into-drawer)
11728 (org-log-into-drawer "LOGBOOK")
11733 (org-back-to-heading t)
11734 (narrow-to-region (point) (save-excursion
11735 (outline-next-heading) (point)))
11736 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
11737 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
11739 (goto-char (match-end 0))
11742 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
11745 (goto-char (match-end 0))
11746 (or org-log-states-order-reversed
11747 (and (re-search-forward org-property-end-re nil t)
11748 (goto-char (1- (match-beginning 0))))))
11749 (insert "\n:" drawer ":\n:END:")
11750 (beginning-of-line 0)
11751 (org-indent-line-function)
11752 (beginning-of-line 2)
11753 (org-indent-line-function)
11755 ((and org-log-state-notes-insert-after-drawers
11757 (forward-line) (looking-at org-drawer-regexp)))
11759 (while (looking-at org-drawer-regexp)
11760 (goto-char (match-end 0))
11761 (re-search-forward org-property-end-re (point-max) t)
11763 (forward-line -1)))
11764 (unless org-log-states-order-reversed
11765 (and (= (char-after) ?\n) (forward-char 1))
11766 (org-skip-over-state-notes)
11767 (skip-chars-backward " \t\n\r")))
11768 (move-marker org-log-note-marker (point))
11769 (setq org-log-note-purpose purpose
11770 org-log-note-state state
11771 org-log-note-previous-state prev-state
11772 org-log-note-how how
11773 org-log-note-extra extra)
11774 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
11776 (defun org-skip-over-state-notes ()
11777 "Skip past the list of State notes in an entry."
11778 (if (looking-at "\n[ \t]*- State") (forward-char 1))
11779 (when (org-in-item-p)
11780 (let ((limit (org-list-bottom-point)))
11781 (while (looking-at "[ \t]*- State")
11782 (goto-char (or (org-get-next-item (point) limit)
11783 (org-get-end-of-item limit)))))))
11785 (defun org-add-log-note (&optional purpose)
11786 "Pop up a window for taking a note, and add this note later at point."
11787 (remove-hook 'post-command-hook 'org-add-log-note)
11788 (setq org-log-note-window-configuration (current-window-configuration))
11789 (delete-other-windows)
11790 (move-marker org-log-note-return-to (point))
11791 (switch-to-buffer (marker-buffer org-log-note-marker))
11792 (goto-char org-log-note-marker)
11793 (org-switch-to-buffer-other-window "*Org Note*")
11795 (if (memq org-log-note-how '(time state))
11796 (let (current-prefix-arg) (org-store-log-note))
11797 (let ((org-inhibit-startup t)) (org-mode))
11798 (insert (format "# Insert note for %s.
11799 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
11801 ((eq org-log-note-purpose 'clock-out) "stopped clock")
11802 ((eq org-log-note-purpose 'done) "closed todo item")
11803 ((eq org-log-note-purpose 'state)
11804 (format "state change from \"%s\" to \"%s\""
11805 (or org-log-note-previous-state "")
11806 (or org-log-note-state "")))
11807 ((eq org-log-note-purpose 'reschedule)
11809 ((eq org-log-note-purpose 'delschedule)
11810 "no longer scheduled")
11811 ((eq org-log-note-purpose 'redeadline)
11812 "changing deadline")
11813 ((eq org-log-note-purpose 'deldeadline)
11814 "removing deadline")
11815 ((eq org-log-note-purpose 'refile)
11817 ((eq org-log-note-purpose 'note)
11819 (t (error "This should not happen")))))
11820 (if org-log-note-extra (insert org-log-note-extra))
11821 (org-set-local 'org-finish-function 'org-store-log-note)))
11823 (defvar org-note-abort nil) ; dynamically scoped
11824 (defun org-store-log-note ()
11825 "Finish taking a log note, and insert it to where it belongs."
11826 (let ((txt (buffer-string))
11827 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
11829 (kill-buffer (current-buffer))
11830 (while (string-match "\\`#.*\n[ \t\n]*" txt)
11831 (setq txt (replace-match "" t t txt)))
11832 (if (string-match "\\s-+\\'" txt)
11833 (setq txt (replace-match "" t t txt)))
11834 (setq lines (org-split-string txt "\n"))
11835 (when (and note (string-match "\\S-" note))
11837 (org-replace-escapes
11839 (list (cons "%u" (user-login-name))
11840 (cons "%U" user-full-name)
11841 (cons "%t" (format-time-string
11842 (org-time-stamp-format 'long 'inactive)
11844 (cons "%T" (format-time-string
11845 (org-time-stamp-format 'long nil)
11847 (cons "%s" (if org-log-note-state
11848 (concat "\"" org-log-note-state "\"")
11850 (cons "%S" (if org-log-note-previous-state
11851 (concat "\"" org-log-note-previous-state "\"")
11853 (if lines (setq note (concat note " \\\\")))
11855 (when (or current-prefix-arg org-note-abort)
11856 (when org-log-into-drawer
11857 (org-remove-empty-drawer-at
11858 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
11859 org-log-note-marker))
11862 (with-current-buffer (marker-buffer org-log-note-marker)
11864 (goto-char org-log-note-marker)
11865 (move-marker org-log-note-marker nil)
11867 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
11868 (setq ind (save-excursion
11869 (if (org-in-item-p)
11871 (goto-char (org-list-top-point))
11872 (org-get-indentation))
11873 (skip-chars-backward " \r\t\n")
11875 ((and (org-at-heading-p)
11876 org-adapt-indentation)
11877 (1+ (org-current-level)))
11878 ((org-at-heading-p) 0)
11879 (t (org-get-indentation))))))
11880 (setq bul (org-list-bullet-string "-"))
11881 (org-indent-line-to ind)
11882 (insert bul (pop lines))
11883 (let ((ind-body (+ (length bul) ind)))
11886 (org-indent-line-to ind-body)
11887 (insert (pop lines))))
11888 (message "Note stored")
11889 (org-back-to-heading t)
11890 (org-cycle-hide-drawers 'children)))))
11891 (set-window-configuration org-log-note-window-configuration)
11892 (with-current-buffer (marker-buffer org-log-note-return-to)
11893 (goto-char org-log-note-return-to))
11894 (move-marker org-log-note-return-to nil)
11895 (and org-log-post-message (message "%s" org-log-post-message)))
11897 (defun org-remove-empty-drawer-at (drawer pos)
11898 "Remove an empty drawer DRAWER at position POS.
11899 POS may also be a marker."
11900 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
11906 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
11907 (replace-match ""))))))
11909 (defun org-sparse-tree (&optional arg)
11910 "Create a sparse tree, prompt for the details.
11911 This command can create sparse trees. You first need to select the type
11912 of match used to create the tree:
11914 t Show all TODO entries.
11915 T Show entries with a specific TODO keyword.
11916 m Show entries selected by a tags/property match.
11917 p Enter a property name and its value (both with completion on existing
11918 names/values) and show entries with that property.
11919 / Show entries matching a regular expression (`r' can be used as well)
11920 d Show deadlines due within `org-deadline-warning-days'.
11921 b Show deadlines and scheduled items before a date.
11922 a Show deadlines and scheduled items after a date."
11924 (let (ans kwd value)
11925 (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty [d]eadlines\n [b]efore-date [a]fter-date")
11926 (setq ans (read-char-exclusive))
11929 (call-interactively 'org-check-deadlines))
11931 (call-interactively 'org-check-before-date))
11933 (call-interactively 'org-check-after-date))
11935 (org-show-todo-tree nil))
11937 (org-show-todo-tree '(4)))
11938 ((member ans '(?T ?m))
11939 (call-interactively 'org-match-sparse-tree))
11940 ((member ans '(?p ?P))
11941 (setq kwd (org-icompleting-read "Property: "
11942 (mapcar 'list (org-buffer-property-keys))))
11943 (setq value (org-icompleting-read "Value: "
11944 (mapcar 'list (org-property-values kwd))))
11945 (unless (string-match "\\`{.*}\\'" value)
11946 (setq value (concat "\"" value "\"")))
11947 (org-match-sparse-tree arg (concat kwd "=" value)))
11948 ((member ans '(?r ?R ?/))
11949 (call-interactively 'org-occur))
11950 (t (error "No such sparse tree command \"%c\"" ans)))))
11952 (defvar org-occur-highlights nil
11953 "List of overlays used for occur matches.")
11954 (make-variable-buffer-local 'org-occur-highlights)
11955 (defvar org-occur-parameters nil
11956 "Parameters of the active org-occur calls.
11957 This is a list, each call to org-occur pushes as cons cell,
11958 containing the regular expression and the callback, onto the list.
11959 The list can contain several entries if `org-occur' has been called
11960 several time with the KEEP-PREVIOUS argument. Otherwise, this list
11961 will only contain one set of parameters. When the highlights are
11962 removed (for example with `C-c C-c', or with the next edit (depending
11963 on `org-remove-highlights-with-change'), this variable is emptied
11965 (make-variable-buffer-local 'org-occur-parameters)
11967 (defun org-occur (regexp &optional keep-previous callback)
11968 "Make a compact tree which shows all matches of REGEXP.
11969 The tree will show the lines where the regexp matches, and all higher
11970 headlines above the match. It will also show the heading after the match,
11971 to make sure editing the matching entry is easy.
11972 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
11973 call to `org-occur' will be kept, to allow stacking of calls to this
11975 If CALLBACK is non-nil, it is a function which is called to confirm
11976 that the match should indeed be shown."
11977 (interactive "sRegexp: \nP")
11978 (when (equal regexp "")
11979 (error "Regexp cannot be empty"))
11980 (unless keep-previous
11981 (org-remove-occur-highlights nil nil t))
11982 (push (cons regexp callback) org-occur-parameters)
11985 (goto-char (point-min))
11986 (if (or (not keep-previous) ; do not want to keep
11987 (not org-occur-highlights)) ; no previous matches
11990 (while (re-search-forward regexp nil t)
11991 (when (or (not callback)
11992 (save-match-data (funcall callback)))
11993 (setq cnt (1+ cnt))
11994 (when org-highlight-sparse-tree-matches
11995 (org-highlight-new-match (match-beginning 0) (match-end 0)))
11996 (org-show-context 'occur-tree))))
11997 (when org-remove-highlights-with-change
11998 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
12000 (unless org-sparse-tree-open-archived-trees
12001 (org-hide-archived-subtrees (point-min) (point-max)))
12002 (run-hooks 'org-occur-hook)
12003 (if (interactive-p)
12004 (message "%d match(es) for regexp %s" cnt regexp))
12007 (defun org-show-context (&optional key)
12008 "Make sure point and context are visible.
12009 How much context is shown depends upon the variables
12010 `org-show-hierarchy-above', `org-show-following-heading'. and
12011 `org-show-siblings'."
12012 (let ((heading-p (org-on-heading-p t))
12013 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
12014 (following-p (org-get-alist-option org-show-following-heading key))
12015 (entry-p (org-get-alist-option org-show-entry-below key))
12016 (siblings-p (org-get-alist-option org-show-siblings key)))
12018 ;; Show heading or entry text
12019 (if (and heading-p (not entry-p))
12020 (org-flag-heading nil) ; only show the heading
12021 (and (or entry-p (org-invisible-p) (org-invisible-p2))
12022 (org-show-hidden-entry))) ; show entire entry
12024 ;; Show next sibling, or heading below text
12026 (and (if heading-p (org-goto-sibling) (outline-next-heading))
12027 (org-flag-heading nil))))
12028 (when siblings-p (org-show-siblings))
12030 ;; show all higher headings, possibly with siblings
12032 (while (and (condition-case nil
12033 (progn (org-up-heading-all 1) t)
12036 (org-flag-heading nil)
12037 (when siblings-p (org-show-siblings))))))))
12039 (defvar org-reveal-start-hook nil
12040 "Hook run before revealing a location.")
12042 (defun org-reveal (&optional siblings)
12043 "Show current entry, hierarchy above it, and the following headline.
12044 This can be used to show a consistent set of context around locations
12045 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
12046 not t for the search context.
12048 With optional argument SIBLINGS, on each level of the hierarchy all
12049 siblings are shown. This repairs the tree structure to what it would
12050 look like when opened with hierarchical calls to `org-cycle'.
12051 With double optional argument \\[universal-argument] \\[universal-argument], \
12052 go to the parent and show the
12055 (run-hooks 'org-reveal-start-hook)
12056 (let ((org-show-hierarchy-above t)
12057 (org-show-following-heading t)
12058 (org-show-siblings (if siblings t org-show-siblings)))
12059 (org-show-context nil))
12060 (when (equal siblings '(16))
12062 (when (org-up-heading-safe)
12064 (run-hook-with-args 'org-cycle-hook 'subtree)))))
12066 (defun org-highlight-new-match (beg end)
12067 "Highlight from BEG to END and mark the highlight is an occur headline."
12068 (let ((ov (make-overlay beg end)))
12069 (overlay-put ov 'face 'secondary-selection)
12070 (push ov org-occur-highlights)))
12072 (defun org-remove-occur-highlights (&optional beg end noremove)
12073 "Remove the occur highlights from the buffer.
12074 BEG and END are ignored. If NOREMOVE is nil, remove this function
12075 from the `before-change-functions' in the current buffer."
12077 (unless org-inhibit-highlight-removal
12078 (mapc 'delete-overlay org-occur-highlights)
12079 (setq org-occur-highlights nil)
12080 (setq org-occur-parameters nil)
12082 (remove-hook 'before-change-functions
12083 'org-remove-occur-highlights 'local))))
12087 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
12088 "Regular expression matching the priority indicator.")
12090 (defvar org-remove-priority-next-time nil)
12092 (defun org-priority-up ()
12093 "Increase the priority of the current item."
12095 (org-priority 'up))
12097 (defun org-priority-down ()
12098 "Decrease the priority of the current item."
12100 (org-priority 'down))
12102 (defun org-priority (&optional action)
12103 "Change the priority of an item by ARG.
12104 ACTION can be `set', `up', `down', or a character."
12106 (unless org-enable-priority-commands
12107 (error "Priority commands are disabled"))
12108 (setq action (or action 'set))
12109 (let (current new news have remove)
12111 (org-back-to-heading t)
12112 (if (looking-at org-priority-regexp)
12113 (setq current (string-to-char (match-string 2))
12115 (setq current org-default-priority))
12117 ((eq action 'remove)
12118 (setq remove t new ?\ ))
12119 ((or (eq action 'set)
12120 (if (featurep 'xemacs) (characterp action) (integerp action)))
12121 (if (not (eq action 'set))
12123 (message "Priority %c-%c, SPC to remove: "
12124 org-highest-priority org-lowest-priority)
12126 (setq new (read-char-exclusive))))
12127 (if (and (= (upcase org-highest-priority) org-highest-priority)
12128 (= (upcase org-lowest-priority) org-lowest-priority))
12129 (setq new (upcase new)))
12130 (cond ((equal new ?\ ) (setq remove t))
12131 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
12132 (error "Priority must be between `%c' and `%c'"
12133 org-highest-priority org-lowest-priority))))
12135 (if (and (not have) (eq last-command this-command))
12136 (setq new org-lowest-priority)
12137 (setq new (if (and org-priority-start-cycle-with-default (not have))
12138 org-default-priority (1- current)))))
12140 (if (and (not have) (eq last-command this-command))
12141 (setq new org-highest-priority)
12142 (setq new (if (and org-priority-start-cycle-with-default (not have))
12143 org-default-priority (1+ current)))))
12144 (t (error "Invalid action")))
12145 (if (or (< (upcase new) org-highest-priority)
12146 (> (upcase new) org-lowest-priority))
12148 (setq news (format "%c" new))
12151 (replace-match "" t t nil 1)
12152 (replace-match news t t nil 2))
12154 (error "No priority cookie found in line")
12155 (let ((case-fold-search nil))
12156 (looking-at org-todo-line-regexp))
12159 (goto-char (match-end 2))
12160 (insert " [#" news "]"))
12161 (goto-char (match-beginning 3))
12162 (insert "[#" news "] "))))
12163 (org-preserve-lc (org-set-tags nil 'align)))
12165 (message "Priority removed")
12166 (message "Priority of current item set to %s" news))))
12168 (defun org-get-priority (s)
12169 "Find priority cookie and return priority."
12171 (if (not (string-match org-priority-regexp s))
12172 (* 1000 (- org-lowest-priority org-default-priority))
12173 (* 1000 (- org-lowest-priority
12174 (string-to-char (match-string 2 s)))))))
12178 (defvar org-agenda-archives-mode)
12179 (defvar org-map-continue-from nil
12180 "Position from where mapping should continue.
12181 Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
12183 (defvar org-scanner-tags nil
12184 "The current tag list while the tags scanner is running.")
12185 (defvar org-trust-scanner-tags nil
12186 "Should `org-get-tags-at' use the tags fro the scanner.
12187 This is for internal dynamical scoping only.
12188 When this is non-nil, the function `org-get-tags-at' will return the value
12189 of `org-scanner-tags' instead of building the list by itself. This
12190 can lead to large speed-ups when the tags scanner is used in a file with
12191 many entries, and when the list of tags is retrieved, for example to
12192 obtain a list of properties. Building the tags list for each entry in such
12193 a file becomes an N^2 operation - but with this variable set, it scales
12196 (defun org-scan-tags (action matcher &optional todo-only)
12197 "Scan headline tags with inheritance and produce output ACTION.
12199 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
12200 or `agenda' to produce an entry list for an agenda view. It can also be
12201 a Lisp form or a function that should be called at each matched headline, in
12202 this case the return value is a list of all return values from these calls.
12204 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
12205 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
12206 only lines with a TODO keyword are included in the output."
12207 (require 'org-agenda)
12208 (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
12209 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
12211 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
12212 (props (list 'face 'default
12213 'done-face 'org-agenda-done
12214 'undone-face 'default
12215 'mouse-face 'highlight
12216 'org-not-done-regexp org-not-done-regexp
12217 'org-todo-regexp org-todo-regexp
12219 (format "mouse-2 or RET jump to org file %s"
12220 (abbreviate-file-name
12221 (or (buffer-file-name (buffer-base-buffer))
12222 (buffer-name (buffer-base-buffer)))))))
12223 (case-fold-search nil)
12224 (org-map-continue-from nil)
12225 lspos tags tags-list
12226 (tags-alist (list (cons 0 org-file-tags)))
12227 (llast 0) rtn rtn1 level category i txt
12228 todo marker entry priority)
12229 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
12230 (setq action (list 'lambda nil action)))
12232 (goto-char (point-min))
12233 (when (eq action 'sparse-tree)
12235 (org-remove-occur-highlights))
12236 (while (re-search-forward re nil t)
12238 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
12239 tags (if (match-end 4) (org-match-string-no-properties 4)))
12240 (goto-char (setq lspos (match-beginning 0)))
12241 (setq level (org-reduced-level (funcall outline-level))
12242 category (org-get-category))
12243 (setq i llast llast level)
12244 ;; remove tag lists from same and sublevels
12245 (while (>= i level)
12246 (when (setq entry (assoc i tags-alist))
12247 (setq tags-alist (delete entry tags-alist)))
12249 ;; add the next tags
12251 (setq tags (org-split-string tags ":")
12253 (cons (cons level tags) tags-alist)))
12254 ;; compile tags for current headline
12256 (if org-use-tag-inheritance
12257 (apply 'append (mapcar 'cdr (reverse tags-alist)))
12259 org-scanner-tags tags-list)
12260 (when org-use-tag-inheritance
12261 (setcdr (car tags-alist)
12262 (mapcar (lambda (x)
12263 (setq x (copy-sequence x))
12264 (org-add-prop-inherited x))
12265 (cdar tags-alist))))
12266 (when (and tags org-use-tag-inheritance
12267 (or (not (eq t org-use-tag-inheritance))
12268 org-tags-exclude-from-inheritance))
12269 ;; selective inheritance, remove uninherited ones
12270 (setcdr (car tags-alist)
12271 (org-remove-uniherited-tags (cdar tags-alist))))
12272 (when (and (or (not todo-only)
12273 (and (member todo org-not-done-keywords)
12274 (or (not org-agenda-tags-todo-honor-ignore-options)
12275 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
12276 (let ((case-fold-search t)) (eval matcher))
12278 (not (member org-archive-tag tags-list))
12279 ;; we have an archive tag, should we use this anyway?
12280 (or (not org-agenda-skip-archived-trees)
12281 (and (eq action 'agenda) org-agenda-archives-mode))))
12282 (unless (eq action 'sparse-tree) (org-agenda-skip))
12284 ;; select this headline
12287 ((eq action 'sparse-tree)
12288 (and org-highlight-sparse-tree-matches
12289 (org-get-heading) (match-end 0)
12290 (org-highlight-new-match
12291 (match-beginning 0) (match-beginning 1)))
12292 (org-show-context 'tags-tree))
12293 ((eq action 'agenda)
12294 (setq txt (org-format-agenda-item
12297 (if (eq org-tags-match-list-sublevels 'indented)
12298 (make-string (1- level) ?.) "")
12303 priority (org-get-priority txt))
12305 (setq marker (org-agenda-new-marker))
12306 (org-add-props txt props
12307 'org-marker marker 'org-hd-marker marker 'org-category category
12309 'priority priority 'type "tagsmatch")
12311 ((functionp action)
12312 (setq org-map-continue-from nil)
12314 (setq rtn1 (funcall action))
12316 (t (error "Invalid action")))
12318 ;; if we are to skip sublevels, jump to end of subtree
12319 (unless org-tags-match-list-sublevels
12320 (org-end-of-subtree t)
12321 (backward-char 1))))
12322 ;; Get the correct position from where to continue
12323 (if org-map-continue-from
12324 (goto-char org-map-continue-from)
12325 (and (= (point) lspos) (end-of-line 1)))))
12326 (when (and (eq action 'sparse-tree)
12327 (not org-sparse-tree-open-archived-trees))
12328 (org-hide-archived-subtrees (point-min) (point-max)))
12331 (defun org-remove-uniherited-tags (tags)
12332 "Remove all tags that are not inherited from the list TAGS."
12334 ((eq org-use-tag-inheritance t)
12335 (if org-tags-exclude-from-inheritance
12336 (org-delete-all org-tags-exclude-from-inheritance tags)
12338 ((not org-use-tag-inheritance) nil)
12339 ((stringp org-use-tag-inheritance)
12342 (if (and (string-match org-use-tag-inheritance x)
12343 (not (member x org-tags-exclude-from-inheritance)))
12346 ((listp org-use-tag-inheritance)
12349 (if (member x org-use-tag-inheritance) x nil))
12352 (defvar todo-only) ;; dynamically scoped
12354 (defun org-match-sparse-tree (&optional todo-only match)
12355 "Create a sparse tree according to tags string MATCH.
12356 MATCH can contain positive and negative selection of tags, like
12357 \"+WORK+URGENT-WITHBOSS\".
12358 If optional argument TODO-ONLY is non-nil, only select lines that are
12361 (org-prepare-agenda-buffers (list (current-buffer)))
12362 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
12364 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
12366 (defvar org-cached-props nil)
12367 (defun org-cached-entry-get (pom property)
12368 (if (or (eq t org-use-property-inheritance)
12369 (and (stringp org-use-property-inheritance)
12370 (string-match org-use-property-inheritance property))
12371 (and (listp org-use-property-inheritance)
12372 (member property org-use-property-inheritance)))
12373 ;; Caching is not possible, check it directly
12374 (org-entry-get pom property 'inherit)
12375 ;; Get all properties, so that we can do complicated checks easily
12376 (cdr (assoc property (or org-cached-props
12377 (setq org-cached-props
12378 (org-entry-properties pom)))))))
12380 (defun org-global-tags-completion-table (&optional files)
12381 "Return the list of all tags in all agenda buffer/files."
12388 (set-buffer (find-file-noselect file))
12389 (append (org-get-buffer-tags)
12390 (mapcar (lambda (x) (if (stringp (car-safe x))
12391 (list (car-safe x)) nil))
12393 (if (and files (car files))
12395 (org-agenda-files))))))))
12397 (defun org-make-tags-matcher (match)
12398 "Create the TAGS//TODO matcher form for the selection string MATCH."
12399 ;; todo-only is scoped dynamically into this function, and the function
12400 ;; may change it if the matcher asks for it.
12402 ;; Get a new match request, with completion
12403 (let ((org-last-tags-completion-table
12404 (org-global-tags-completion-table)))
12405 (setq match (org-completing-read-no-i
12406 "Match: " 'org-tags-completion-function nil nil nil
12407 'org-tags-history))))
12409 ;; Parse the string and create a lisp form
12410 (let ((match0 match)
12411 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
12413 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
12414 orterms term orlist re-p str-p level-p level-op time-p
12415 prop-p pn pv po cat-p gv rest)
12416 (if (string-match "/+" match)
12417 ;; match contains also a todo-matching request
12419 (setq tagsmatch (substring match 0 (match-beginning 0))
12420 todomatch (substring match (match-end 0)))
12421 (if (string-match "^!" todomatch)
12422 (setq todo-only t todomatch (substring todomatch 1)))
12423 (if (string-match "^\\s-*$" todomatch)
12424 (setq todomatch nil)))
12425 ;; only matching tags
12426 (setq tagsmatch match todomatch nil))
12428 ;; Make the tags matcher
12429 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
12430 (setq tagsmatcher t)
12431 (setq orterms (org-split-string tagsmatch "|") orlist nil)
12432 (while (setq term (pop orterms))
12433 (while (and (equal (substring term -1) "\\") orterms)
12434 (setq term (concat term "|" (pop orterms)))) ; repair bad split
12435 (while (string-match re term)
12436 (setq rest (substring term (match-end 0))
12437 minus (and (match-end 1)
12438 (equal (match-string 1 term) "-"))
12439 tag (save-match-data (replace-regexp-in-string
12441 (match-string 2 term)))
12442 re-p (equal (string-to-char tag) ?{)
12443 level-p (match-end 4)
12444 prop-p (match-end 5)
12446 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
12448 (setq level-op (org-op-to-function (match-string 3 term)))
12449 `(,level-op level ,(string-to-number
12450 (match-string 4 term))))
12452 (setq pn (match-string 5 term)
12453 po (match-string 6 term)
12454 pv (match-string 7 term)
12455 cat-p (equal pn "CATEGORY")
12456 re-p (equal (string-to-char pv) ?{)
12457 str-p (equal (string-to-char pv) ?\")
12458 time-p (save-match-data
12459 (string-match "^\"[[<].*[]>]\"$" pv))
12460 pv (if (or re-p str-p) (substring pv 1 -1) pv))
12461 (if time-p (setq pv (org-matcher-time pv)))
12462 (setq po (org-op-to-function po (if time-p 'time str-p)))
12464 ((equal pn "CATEGORY")
12465 (setq gv '(get-text-property (point) 'org-category)))
12469 (setq gv `(org-cached-entry-get nil ,pn))))
12472 `(not (string-match ,pv (or ,gv "")))
12473 `(string-match ,pv (or ,gv "")))
12475 `(,po (or ,gv "") ,pv)
12476 `(,po (string-to-number (or ,gv ""))
12477 ,(string-to-number pv) ))))
12478 (t `(member ,tag tags-list)))
12479 mm (if minus (list 'not mm) mm)
12481 (push mm tagsmatcher))
12482 (push (if (> (length tagsmatcher) 1)
12483 (cons 'and tagsmatcher)
12486 (setq tagsmatcher nil))
12487 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
12489 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
12490 ;; Make the todo matcher
12491 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
12492 (setq todomatcher t)
12493 (setq orterms (org-split-string todomatch "|") orlist nil)
12494 (while (setq term (pop orterms))
12495 (while (string-match re term)
12496 (setq minus (and (match-end 1)
12497 (equal (match-string 1 term) "-"))
12498 kwd (match-string 2 term)
12499 re-p (equal (string-to-char kwd) ?{)
12500 term (substring term (match-end 0))
12502 `(string-match ,(substring kwd 1 -1) todo)
12503 (list 'equal 'todo kwd))
12504 mm (if minus (list 'not mm) mm))
12505 (push mm todomatcher))
12506 (push (if (> (length todomatcher) 1)
12507 (cons 'and todomatcher)
12510 (setq todomatcher nil))
12511 (setq todomatcher (if (> (length orlist) 1)
12512 (cons 'or orlist) (car orlist))))
12514 ;; Return the string and lisp forms of the matcher
12515 (setq matcher (if todomatcher
12516 (list 'and tagsmatcher todomatcher)
12518 (cons match0 matcher)))
12520 (defun org-op-to-function (op &optional stringp)
12521 "Turn an operator into the appropriate function."
12524 ((equal op "<" ) '(< string< org-time<))
12525 ((equal op ">" ) '(> org-string> org-time>))
12526 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
12527 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
12528 ((member op '("=" "==")) '(= string= org-time=))
12529 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
12530 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
12532 (defun org<> (a b) (not (= a b)))
12533 (defun org-string<= (a b) (or (string= a b) (string< a b)))
12534 (defun org-string>= (a b) (not (string< a b)))
12535 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
12536 (defun org-string<> (a b) (not (string= a b)))
12537 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
12538 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
12539 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
12540 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
12541 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
12542 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
12544 "Convert S to a floating point time.
12545 If S is already a number, just return it. If it is a string, parse
12546 it as a time string and apply `float-time' to it. If S is nil, just return 0."
12550 (condition-case nil
12551 (float-time (apply 'encode-time (org-parse-time-string s)))
12555 (defun org-time-today ()
12556 "Time in seconds today at 0:00.
12557 Returns the float number of seconds since the beginning of the
12558 epoch to the beginning of today (00:00)."
12559 (float-time (apply 'encode-time
12560 (append '(0 0 0) (nthcdr 3 (decode-time))))))
12562 (defun org-matcher-time (s)
12563 "Interpret a time comparison value."
12566 ((string= s "<now>") (float-time))
12567 ((string= s "<today>") (org-time-today))
12568 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
12569 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
12570 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
12571 (+ (org-time-today)
12572 (* (string-to-number (match-string 1 s))
12573 (cdr (assoc (match-string 2 s)
12574 '(("d" . 86400.0) ("w" . 604800.0)
12575 ("m" . 2678400.0) ("y" . 31557600.0)))))))
12578 (defun org-match-any-p (re list)
12579 "Does re match any element of list?"
12580 (setq list (mapcar (lambda (x) (string-match re x)) list))
12583 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
12584 (defvar org-tags-overlay (make-overlay 1 1))
12585 (org-detach-overlay org-tags-overlay)
12587 (defun org-get-local-tags-at (&optional pos)
12588 "Get a list of tags defined in the current headline."
12589 (org-get-tags-at pos 'local))
12591 (defun org-get-local-tags ()
12592 "Get a list of tags defined in the current headline."
12593 (org-get-tags-at nil 'local))
12595 (defun org-get-tags-at (&optional pos local)
12596 "Get a list of all headline tags applicable at POS.
12597 POS defaults to point. If tags are inherited, the list contains
12598 the targets in the same sequence as the headlines appear, i.e.
12599 the tags of the current headline come last.
12600 When LOCAL is non-nil, only return tags from the current headline,
12601 ignore inherited ones."
12603 (if (and org-trust-scanner-tags
12604 (or (not pos) (equal pos (point)))
12607 (let (tags ltags lastpos parent)
12611 (goto-char (or pos (point)))
12614 (condition-case nil
12616 (org-back-to-heading t)
12617 (while (not (equal lastpos (point)))
12618 (setq lastpos (point))
12620 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
12621 (setq ltags (org-split-string
12622 (org-match-string-no-properties 1) ":"))
12624 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
12627 (org-remove-uniherited-tags ltags)
12630 (or org-use-tag-inheritance (throw 'done t))
12631 (if local (throw 'done t))
12632 (or (org-up-heading-safe) (error nil))
12635 (append (org-remove-uniherited-tags org-file-tags) tags)))))
12637 (defun org-add-prop-inherited (s)
12638 (add-text-properties 0 (length s) '(inherited t) s)
12641 (defun org-toggle-tag (tag &optional onoff)
12642 "Toggle the tag TAG for the current line.
12643 If ONOFF is `on' or `off', don't toggle but set to this state."
12646 (org-back-to-heading t)
12647 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
12650 (setq current (match-string 1))
12651 (replace-match ""))
12653 (setq current (nreverse (org-split-string current ":")))
12657 (or (member tag current) (push tag current)))
12659 (or (not (member tag current)) (setq current (delete tag current))))
12660 (t (if (member tag current)
12661 (setq current (delete tag current))
12663 (push tag current))))
12667 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
12668 (org-set-tags nil t))
12669 (delete-horizontal-space))
12670 (run-hooks 'org-after-tags-change-hook))
12673 (defun org-align-tags-here (to-col)
12674 ;; Assumes that this is a headline
12675 (let ((pos (point)) (col (current-column)) ncol tags-l p)
12676 (beginning-of-line 1)
12677 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
12678 (< pos (match-beginning 2)))
12680 (setq tags-l (- (match-end 2) (match-beginning 2)))
12681 (goto-char (match-beginning 1))
12683 (delete-region (point) (1+ (match-beginning 2)))
12684 (setq ncol (max (1+ (current-column))
12688 (- (abs to-col) tags-l))))
12690 (insert (make-string (- ncol (current-column)) ?\ ))
12691 (setq ncol (current-column))
12692 (when indent-tabs-mode (tabify p (point-at-eol)))
12693 (org-move-to-column (min ncol col) t))
12696 (defun org-set-tags-command (&optional arg just-align)
12697 "Call the set-tags command for the current entry."
12699 (if (org-on-heading-p)
12700 (org-set-tags arg just-align)
12702 (org-back-to-heading t)
12703 (org-set-tags arg just-align))))
12705 (defun org-set-tags-to (data)
12706 "Set the tags of the current entry to DATA, replacing the current tags.
12707 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
12708 If DATA is nil or the empty string, any tags will be removed."
12709 (interactive "sTags: ")
12713 ((equal data "") "")
12715 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
12718 (concat ":" (mapconcat 'identity data ":") ":"))
12722 (org-back-to-heading t)
12723 (when (looking-at org-complex-heading-regexp)
12726 (goto-char (match-beginning 5))
12728 (delete-region (point) (point-at-eol))
12729 (org-set-tags nil 'align))
12730 (goto-char (point-at-eol))
12732 (org-set-tags nil 'align)))
12733 (beginning-of-line 1)
12734 (if (looking-at ".*?\\([ \t]+\\)$")
12735 (delete-region (match-beginning 1) (match-end 1))))))
12737 (defun org-align-all-tags ()
12738 "Align the tags i all headings."
12741 (or (ignore-errors (org-back-to-heading t))
12742 (outline-next-heading))
12743 (if (org-on-heading-p)
12745 (message "No headings"))))
12747 (defvar org-indent-indentation-per-level)
12748 (defun org-set-tags (&optional arg just-align)
12749 "Set the tags for the current headline.
12750 With prefix ARG, realign all tags in headings in the current buffer."
12752 (let* ((re (concat "^" outline-regexp))
12753 (current (org-get-tags-string))
12754 (col (current-column))
12755 (org-setting-tags t)
12756 table current-tags inherited-tags ; computed below when needed
12757 tags p0 c0 c1 rpl di tc level)
12760 (goto-char (point-min))
12761 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
12762 (while (re-search-forward re nil t)
12763 (org-set-tags nil t)
12765 (message "All tags realigned to column %d" org-tags-column))
12767 (setq tags current)
12768 ;; Get a new set of tags from the user
12770 (setq table (append org-tag-persistent-alist
12771 (or org-tag-alist (org-get-buffer-tags))
12772 (and org-complete-tags-always-offer-all-agenda-tags
12773 (org-global-tags-completion-table (org-agenda-files))))
12774 org-last-tags-completion-table table
12775 current-tags (org-split-string current ":")
12776 inherited-tags (nreverse
12777 (nthcdr (length current-tags)
12778 (nreverse (org-get-tags-at))))
12780 (if (or (eq t org-use-fast-tag-selection)
12781 (and org-use-fast-tag-selection
12782 (delq nil (mapcar 'cdr table))))
12783 (org-fast-tag-selection
12784 current-tags inherited-tags table
12785 (if org-fast-tag-selection-include-todo org-todo-key-alist))
12786 (let ((org-add-colon-after-tag-completion t))
12788 (org-without-partial-completion
12789 (org-icompleting-read "Tags: " 'org-tags-completion-function
12790 nil nil current 'org-tags-history)))))))
12791 (while (string-match "[-+&]+" tags)
12792 ;; No boolean logic, just a list
12793 (setq tags (replace-match ":" t t tags))))
12795 (if org-tags-sort-function
12796 (setq tags (mapconcat 'identity
12797 (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
12798 org-tags-sort-function) ":")))
12800 (if (string-match "\\`[\t ]*\\'" tags)
12802 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
12803 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
12805 ;; Insert new tags at the correct column
12806 (beginning-of-line 1)
12807 (setq level (or (and (looking-at org-outline-regexp)
12808 (- (match-end 0) (point) 1))
12811 ((and (equal current "") (equal tags "")))
12812 ((re-search-forward
12813 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
12815 (if (equal tags "")
12817 (goto-char (match-beginning 0))
12818 (setq c0 (current-column)
12819 ;; compute offset for the case of org-indent-mode active
12820 di (if org-indent-mode
12821 (* (1- org-indent-indentation-per-level) (1- level))
12823 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
12824 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
12825 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
12826 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
12827 (replace-match rpl t t)
12828 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
12830 (t (error "Tags alignment failed")))
12831 (org-move-to-column col)
12833 (run-hooks 'org-after-tags-change-hook)))))
12835 (defun org-change-tag-in-region (beg end tag off)
12836 "Add or remove TAG for each entry in the region.
12837 This works in the agenda, and also in an org-mode buffer."
12839 (list (region-beginning) (region-end)
12840 (let ((org-last-tags-completion-table
12842 (org-get-buffer-tags)
12843 (org-global-tags-completion-table))))
12844 (org-icompleting-read
12845 "Tag: " 'org-tags-completion-function nil nil nil
12846 'org-tags-history))
12848 (message "[s]et or
[r]emove? ")
12849 (equal (read-char-exclusive) ?r))))
12850 (if (fboundp 'deactivate-mark) (deactivate-mark))
12851 (let ((agendap (equal major-mode 'org-agenda-mode))
12852 l1 l2 m buf pos newhead (cnt 0))
12854 (setq l2 (1- (org-current-line)))
12856 (setq l1 (org-current-line))
12857 (loop for l from l1 to l2 do
12859 (setq m (get-text-property (point) 'org-hd-marker))
12860 (when (or (and (org-mode-p) (org-on-heading-p))
12862 (setq buf (if agendap (marker-buffer m) (current-buffer))
12863 pos (if agendap m (point)))
12864 (with-current-buffer buf
12868 (setq cnt (1+ cnt))
12869 (org-toggle-tag tag (if off 'off 'on))
12870 (setq newhead (org-get-heading)))))
12871 (and agendap (org-agenda-change-all-lines newhead m))))
12872 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
12874 (defun org-tags-completion-function (string predicate &optional flag)
12875 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
12876 (confirm (lambda (x) (stringp (car x)))))
12877 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
12878 (setq s1 (match-string 1 string)
12879 s2 (match-string 2 string))
12880 (setq s1 "" s2 string))
12884 (setq rtn (try-completion s2 ctable confirm))
12887 (concat s1 s2 (substring rtn (length s2))
12888 (if (and org-add-colon-after-tag-completion
12889 (assoc rtn ctable))
12894 (all-completions s2 ctable confirm)
12898 (assoc s2 ctable)))
12901 (defun org-fast-tag-insert (kwd tags face &optional end)
12902 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
12903 (insert (format "%-12s" (concat kwd ":"))
12904 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
12907 (defun org-fast-tag-show-exit (flag)
12910 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
12911 (replace-match ""))
12914 (org-move-to-column (- (window-width) 19) t)
12915 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
12917 (defun org-set-current-tags-overlay (current prefix)
12918 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
12919 (if (featurep 'xemacs)
12920 (org-overlay-display org-tags-overlay (concat prefix s)
12921 'secondary-selection)
12922 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
12923 (org-overlay-display org-tags-overlay (concat prefix s)))))
12925 (defvar org-last-tag-selection-key nil)
12926 (defun org-fast-tag-selection (current inherited table &optional todo-table)
12927 "Fast tag selection with single keys.
12928 CURRENT is the current list of tags in the headline, INHERITED is the
12929 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
12930 possibly with grouping information. TODO-TABLE is a similar table with
12931 TODO keywords, should these have keys assigned to them.
12932 If the keys are nil, a-z are automatically assigned.
12933 Returns the new tags string, or nil to not change the current settings."
12934 (let* ((fulltable (append table todo-table))
12935 (maxlen (apply 'max (mapcar
12937 (if (stringp (car x)) (string-width (car x)) 0))
12939 (buf (current-buffer))
12940 (expert (eq org-fast-tag-selection-single-key 'expert))
12942 (fwidth (+ maxlen 3 1 3))
12943 (ncol (/ (- (window-width) 4) fwidth))
12946 tg cnt e c char c1 c2 ntable tbl rtn
12947 ov-start ov-end ov-prefix
12948 (exit-after-next org-fast-tag-selection-single-key)
12949 (done-keywords org-done-keywords)
12952 (beginning-of-line 1)
12954 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
12955 (setq ov-start (match-beginning 1)
12956 ov-end (match-end 1)
12958 (setq ov-start (1- (point-at-eol))
12959 ov-end (1+ ov-start))
12960 (skip-chars-forward "^\n\r")
12963 (buffer-substring (1- (point)) (point))
12964 (if (> (current-column) org-tags-column)
12966 (make-string (- org-tags-column (current-column)) ?\ ))))))
12967 (move-overlay org-tags-overlay ov-start ov-end)
12968 (save-window-excursion
12970 (set-buffer (get-buffer-create " *Org tags*"))
12971 (delete-other-windows)
12972 (split-window-vertically)
12973 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
12975 (org-set-local 'org-done-keywords done-keywords)
12976 (org-fast-tag-insert "Inherited" inherited i-face "\n")
12977 (org-fast-tag-insert "Current" current c-face "\n\n")
12978 (org-fast-tag-show-exit exit-after-next)
12979 (org-set-current-tags-overlay current ov-prefix)
12980 (setq tbl fulltable char ?a cnt 0)
12981 (while (setq e (pop tbl))
12983 ((equal (car e) :startgroup)
12984 (push '() groups) (setq ingroup t)
12985 (when (not (= cnt 0))
12988 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
12989 ((equal (car e) :endgroup)
12990 (setq ingroup nil cnt 0)
12991 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
12992 ((equal e '(:newline))
12993 (when (not (= cnt 0))
12997 (while (equal (car tbl) '(:newline))
12999 (setq tbl (cdr tbl)))))
13001 (setq tg (copy-sequence (car e)) c2 nil)
13004 ;; automatically assign a character.
13005 (setq c1 (string-to-char
13006 (downcase (substring
13007 tg (if (= (string-to-char tg) ?@) 1 0)))))
13008 (if (or (rassoc c1 ntable) (rassoc c1 table))
13009 (while (or (rassoc char ntable) (rassoc char table))
13010 (setq char (1+ char)))
13012 (setq c (or c2 char)))
13013 (if ingroup (push tg (car groups)))
13014 (setq tg (org-add-props tg nil 'face
13016 ((not (assoc tg table))
13017 (org-get-todo-face tg))
13018 ((member tg current) c-face)
13019 ((member tg inherited) i-face)
13021 (if (and (= cnt 0) (not ingroup)) (insert " "))
13022 (insert "[" c "] " tg (make-string
13023 (- fwidth 4 (length tg)) ?\ ))
13024 (push (cons tg c) ntable)
13025 (when (= (setq cnt (1+ cnt)) ncol)
13027 (if ingroup (insert " "))
13029 (setq ntable (nreverse ntable))
13031 (goto-char (point-min))
13032 (if (not expert) (org-fit-window-to-buffer))
13036 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
13037 (if (not groups) "no " "")
13038 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
13039 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13040 (setq org-last-tag-selection-key c)
13042 ((= c ?\r) (throw 'exit t))
13044 (setq groups (not groups))
13045 (goto-char (point-min))
13046 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
13049 (org-fast-tag-show-exit
13050 (setq exit-after-next (not exit-after-next)))
13052 (delete-other-windows)
13053 (split-window-vertically)
13054 (org-switch-to-buffer-other-window " *Org tags*")
13055 (org-fit-window-to-buffer)))
13057 (and (= c ?q) (not (rassoc c ntable))))
13058 (org-detach-overlay org-tags-overlay)
13059 (setq quit-flag t))
13062 (if exit-after-next (setq exit-after-next 'now)))
13064 (condition-case nil
13065 (setq tg (org-icompleting-read
13068 (with-current-buffer buf
13069 (org-get-buffer-tags)))))
13070 (quit (setq tg "")))
13071 (when (string-match "\\S-" tg)
13072 (add-to-list 'buffer-tags (list tg))
13073 (if (member tg current)
13074 (setq current (delete tg current))
13075 (push tg current)))
13076 (if exit-after-next (setq exit-after-next 'now)))
13077 ((setq e (rassoc c todo-table) tg (car e))
13078 (with-current-buffer buf
13079 (save-excursion (org-todo tg)))
13080 (if exit-after-next (setq exit-after-next 'now)))
13081 ((setq e (rassoc c ntable) tg (car e))
13082 (if (member tg current)
13083 (setq current (delete tg current))
13084 (loop for g in groups do
13087 (setq current (delete x current)))
13090 (if exit-after-next (setq exit-after-next 'now))))
13092 ;; Create a sorted list
13096 (assoc b (cdr (memq (assoc a ntable) ntable))))))
13097 (if (eq exit-after-next 'now) (throw 'exit t))
13098 (goto-char (point-min))
13099 (beginning-of-line 2)
13100 (delete-region (point) (point-at-eol))
13101 (org-fast-tag-insert "Current" current c-face)
13102 (org-set-current-tags-overlay current ov-prefix)
13103 (while (re-search-forward
13104 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
13105 (setq tg (match-string 1))
13106 (add-text-properties
13107 (match-beginning 1) (match-end 1)
13110 ((member tg current) c-face)
13111 ((member tg inherited) i-face)
13112 (t (get-text-property (match-beginning 1) 'face))))))
13113 (goto-char (point-min)))))
13114 (org-detach-overlay org-tags-overlay)
13116 (mapconcat 'identity current ":")
13119 (defun org-get-tags-string ()
13120 "Get the TAGS string in the current headline."
13121 (unless (org-on-heading-p t)
13122 (error "Not on a heading"))
13124 (beginning-of-line 1)
13125 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13126 (org-match-string-no-properties 1)
13129 (defun org-get-tags ()
13130 "Get the list of tags specified in the current headline."
13131 (org-split-string (org-get-tags-string) ":"))
13133 (defun org-get-buffer-tags ()
13134 "Get a table of all tags used in the buffer, for completion."
13137 (goto-char (point-min))
13138 (while (re-search-forward
13139 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
13140 (when (equal (char-after (point-at-bol 0)) ?*)
13141 (mapc (lambda (x) (add-to-list 'tags x))
13142 (org-split-string (org-match-string-no-properties 1) ":")))))
13143 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
13144 (mapcar 'list tags)))
13146 ;;;; The mapping API
13149 (defun org-map-entries (func &optional match scope &rest skip)
13150 "Call FUNC at each headline selected by MATCH in SCOPE.
13152 FUNC is a function or a lisp form. The function will be called without
13153 arguments, with the cursor positioned at the beginning of the headline.
13154 The return values of all calls to the function will be collected and
13155 returned as a list.
13157 The call to FUNC will be wrapped into a save-excursion form, so FUNC
13158 does not need to preserve point. After evaluation, the cursor will be
13159 moved to the end of the line (presumably of the headline of the
13160 processed entry) and search continues from there. Under some
13161 circumstances, this may not produce the wanted results. For example,
13162 if you have removed (e.g. archived) the current (sub)tree it could
13163 mean that the next entry will be skipped entirely. In such cases, you
13164 can specify the position from where search should continue by making
13165 FUNC set the variable `org-map-continue-from' to the desired buffer
13168 MATCH is a tags/property/todo match as it is used in the agenda tags view.
13169 Only headlines that are matched by this query will be considered during
13170 the iteration. When MATCH is nil or t, all headlines will be
13171 visited by the iteration.
13173 SCOPE determines the scope of this command. It can be any of:
13175 nil The current buffer, respecting the restriction if any
13176 tree The subtree started with the entry at point
13177 file The current buffer, without restriction
13179 The current buffer, and any archives associated with it
13180 agenda All agenda files
13181 agenda-with-archives
13182 All agenda files with any archive files associated with them
13184 If this is a list, all files in the list will be scanned
13186 The remaining args are treated as settings for the skipping facilities of
13187 the scanner. The following items can be given here:
13189 archive skip trees with the archive tag.
13190 comment skip trees with the COMMENT keyword
13191 function or Emacs Lisp form:
13192 will be used as value for `org-agenda-skip-function', so whenever
13193 the function returns t, FUNC will not be called for that
13194 entry and search will continue from the point where the
13195 function leaves it.
13197 If your function needs to retrieve the tags including inherited tags
13198 at the *current* entry, you can use the value of the variable
13199 `org-scanner-tags' which will be much faster than getting the value
13200 with `org-get-tags-at'. If your function gets properties with
13201 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
13202 to t around the call to `org-entry-properties' to get the same speedup.
13203 Note that if your function moves around to retrieve tags and properties at
13204 a *different* entry, you cannot use these techniques."
13205 (let* ((org-agenda-archives-mode nil) ; just to make sure
13206 (org-agenda-skip-archived-trees (memq 'archive skip))
13207 (org-agenda-skip-comment-trees (memq 'comment skip))
13208 (org-agenda-skip-function
13209 (car (org-delete-all '(comment archive) skip)))
13210 (org-tags-match-list-sublevels t)
13212 org-todo-keywords-for-agenda
13213 org-done-keywords-for-agenda
13214 org-todo-keyword-alist-for-agenda
13215 org-drawers-for-agenda
13216 org-tag-alist-for-agenda)
13219 ((eq match t) (setq matcher t))
13220 ((eq match nil) (setq matcher t))
13221 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
13225 (when (eq scope 'tree)
13226 (org-back-to-heading t)
13227 (org-narrow-to-subtree)
13232 (org-prepare-agenda-buffers
13233 (list (buffer-file-name (current-buffer))))
13234 (setq res (org-scan-tags func matcher)))
13235 ;; Get the right scope
13237 ((and scope (listp scope) (symbolp (car scope)))
13238 (setq scope (eval scope)))
13239 ((eq scope 'agenda)
13240 (setq scope (org-agenda-files t)))
13241 ((eq scope 'agenda-with-archives)
13242 (setq scope (org-agenda-files t))
13243 (setq scope (org-add-archive-files scope)))
13245 (setq scope (list (buffer-file-name))))
13246 ((eq scope 'file-with-archives)
13247 (setq scope (org-add-archive-files (list (buffer-file-name))))))
13248 (org-prepare-agenda-buffers scope)
13249 (while (setq file (pop scope))
13250 (with-current-buffer (org-find-base-buffer-visiting file)
13254 (goto-char (point-min))
13255 (setq res (append res (org-scan-tags func matcher))))))))))
13260 ;;; Setting and retrieving properties
13262 (defconst org-special-properties
13263 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
13264 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
13265 "The special properties valid in Org-mode.
13267 These are properties that are not defined in the property drawer,
13268 but in some other way.")
13270 (defconst org-default-properties
13271 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
13272 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
13273 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
13274 "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
13275 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
13276 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
13277 "Some properties that are used by Org-mode for various purposes.
13278 Being in this list makes sure that they are offered for completion.")
13280 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
13281 "Regular expression matching the first line of a property drawer.")
13283 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
13284 "Regular expression matching the last line of a property drawer.")
13286 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
13287 "Regular expression matching the first line of a property drawer.")
13289 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
13290 "Regular expression matching the first line of a property drawer.")
13292 (defconst org-property-drawer-re
13293 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
13294 org-property-end-re "\\)\n?")
13295 "Matches an entire property drawer.")
13297 (defconst org-clock-drawer-re
13298 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
13299 org-property-end-re "\\)\n?")
13300 "Matches an entire clock drawer.")
13302 (defun org-property-action ()
13303 "Do an action on properties."
13306 (org-at-property-p)
13307 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
13308 (setq c (read-char-exclusive))
13311 (call-interactively 'org-set-property))
13313 (call-interactively 'org-delete-property))
13315 (call-interactively 'org-delete-property-globally))
13317 (call-interactively 'org-compute-property-at-point))
13318 (t (error "No such property action %c" c)))))
13320 (defun org-set-effort (&optional value)
13321 "Set the effort property of the current entry.
13322 With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
13325 (if (equal value 0) (setq value 10))
13326 (let* ((completion-ignore-case t)
13327 (prop org-effort-property)
13328 (cur (org-entry-get nil prop))
13329 (allowed (org-property-get-allowed-values nil prop 'table))
13330 (existing (mapcar 'list (org-property-values prop)))
13333 ((stringp value) value)
13334 ((and allowed (integerp value))
13335 (or (car (nth (1- value) allowed))
13336 (car (org-last allowed))))
13338 (message "Select 1-9,0, [RET%s]: %s"
13339 (if cur (concat "=" cur) "")
13340 (mapconcat 'car allowed " "))
13341 (setq rpl (read-char-exclusive))
13342 (if (equal rpl ?\r)
13344 (setq rpl (- rpl ?0))
13345 (if (equal rpl 0) (setq rpl 10))
13346 (if (and (> rpl 0) (<= rpl (length allowed)))
13347 (car (nth (1- rpl) allowed))
13348 (org-completing-read "Effort: " allowed nil))))
13350 (let (org-completion-use-ido org-completion-use-iswitchb)
13351 (org-completing-read
13352 (concat "Effort " (if (and cur (string-match "\\S-" cur))
13353 (concat "[" cur "]") "")
13355 existing nil nil "" nil cur))))))
13356 (unless (equal (org-entry-get nil prop) val)
13357 (org-entry-put nil prop val))
13358 (message "%s is now %s" prop val)))
13360 (defun org-at-property-p ()
13361 "Is cursor inside a property drawer?"
13363 (beginning-of-line 1)
13364 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
13365 (save-match-data ;; Used by calling procedures
13367 (range (unless (org-before-first-heading-p)
13368 (org-get-property-block))))
13369 (and range (<= (car range) p) (< p (cdr range))))))))
13371 (defun org-get-property-block (&optional beg end force)
13372 "Return the (beg . end) range of the body of the property drawer.
13373 BEG and END can be beginning and end of subtree, if not given
13374 they will be found.
13375 If the drawer does not exist and FORCE is non-nil, create the drawer."
13378 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
13379 (end (or end (progn (outline-next-heading) (point)))))
13381 (if (re-search-forward org-property-start-re end t)
13382 (setq beg (1+ (match-end 0)))
13385 (org-insert-property-drawer)
13386 (setq end (progn (outline-next-heading) (point))))
13389 (if (re-search-forward org-property-start-re end t)
13390 (setq beg (1+ (match-end 0)))))
13391 (if (re-search-forward org-property-end-re end t)
13392 (setq end (match-beginning 0))
13393 (or force (throw 'exit nil))
13396 (org-indent-line-function)
13397 (insert ":END:\n"))
13400 (defun org-entry-properties (&optional pom which specific)
13401 "Get all properties of the entry at point-or-marker POM.
13402 This includes the TODO keyword, the tags, time strings for deadline,
13403 scheduled, and clocking, and any additional properties defined in the
13404 entry. The return value is an alist, keys may occur multiple times
13405 if the property key was used several times.
13406 POM may also be nil, in which case the current entry is used.
13407 If WHICH is nil or `all', get all properties. If WHICH is
13408 `special' or `standard', only get that subclass. If WHICH
13409 is a string only get exactly this property. Specific can be a string, the
13410 specific property we are interested in. Specifying it can speed
13411 things up because then unnecessary parsing is avoided."
13412 (setq which (or which 'all))
13413 (org-with-point-at pom
13414 (let ((clockstr (substring org-clock-string 0 -1))
13415 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
13416 (case-fold-search nil)
13417 beg end range props sum-props key key1 value string clocksum)
13419 (when (condition-case nil
13420 (and (org-mode-p) (org-back-to-heading t))
13423 (setq sum-props (get-text-property (point) 'org-summaries))
13424 (setq clocksum (get-text-property (point) :org-clock-minutes))
13425 (outline-next-heading)
13427 (when (memq which '(all special))
13428 ;; Get the special properties, like TODO and tags
13430 (when (and (or (not specific) (string= specific "TODO"))
13431 (looking-at org-todo-line-regexp) (match-end 2))
13432 (push (cons "TODO" (org-match-string-no-properties 2)) props))
13433 (when (and (or (not specific) (string= specific "PRIORITY"))
13434 (looking-at org-priority-regexp))
13435 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
13436 (when (and (or (not specific) (string= specific "TAGS"))
13437 (setq value (org-get-tags-string))
13438 (string-match "\\S-" value))
13439 (push (cons "TAGS" value) props))
13440 (when (and (or (not specific) (string= specific "ALLTAGS"))
13441 (setq value (org-get-tags-at)))
13442 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
13445 (when (or (not specific) (string= specific "BLOCKED"))
13446 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
13447 (when (or (not specific)
13449 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
13450 "TIMESTAMP" "TIMESTAMP_IA")))
13451 (while (re-search-forward org-maybe-keyword-time-regexp end t)
13452 (setq key (if (match-end 1)
13453 (substring (org-match-string-no-properties 1)
13455 string (if (equal key clockstr)
13459 (match-beginning 3) (goto-char
13461 (substring (org-match-string-no-properties 3)
13463 ;; Get the correct property name from the key. This is
13464 ;; necessary if the user has configured time keywords.
13465 (setq key1 (concat key ":"))
13469 (if (= (char-after (match-beginning 3)) ?\[)
13470 "TIMESTAMP_IA" "TIMESTAMP")))
13471 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
13472 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
13473 ((equal key1 org-closed-string) (setq key "CLOSED"))
13474 ((equal key1 org-clock-string) (setq key "CLOCK")))
13475 (when (or (equal key "CLOCK") (not (assoc key props)))
13476 (push (cons key string) props))))
13479 (when (memq which '(all standard))
13480 ;; Get the standard properties, like :PROP: ...
13481 (setq range (org-get-property-block beg end))
13483 (goto-char (car range))
13484 (while (re-search-forward
13485 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
13487 (setq key (org-match-string-no-properties 1)
13488 value (org-trim (or (org-match-string-no-properties 2) "")))
13489 (unless (member key excluded)
13490 (push (cons key (or value "")) props)))))
13492 (push (cons "CLOCKSUM"
13493 (org-columns-number-to-string (/ (float clocksum) 60.)
13496 (unless (assoc "CATEGORY" props)
13497 (setq value (or (org-get-category)
13498 (progn (org-refresh-category-properties)
13499 (org-get-category))))
13500 (push (cons "CATEGORY" value) props))
13501 (append sum-props (nreverse props)))))))
13503 (defun org-entry-get (pom property &optional inherit literal-nil)
13504 "Get value of PROPERTY for entry at point-or-marker POM.
13505 If INHERIT is non-nil and the entry does not have the property,
13506 then also check higher levels of the hierarchy.
13507 If INHERIT is the symbol `selective', use inheritance only if the setting
13508 in `org-use-property-inheritance' selects PROPERTY for inheritance.
13509 If the property is present but empty, the return value is the empty string.
13510 If the property is not present at all, nil is returned.
13512 If LITERAL-NIL is set, return the string value \"nil\" as a string,
13513 do not interpret it as the list atom nil. This is used for inheritance
13514 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
13515 (org-with-point-at pom
13516 (if (and inherit (if (eq inherit 'selective)
13517 (org-property-inherit-p property)
13519 (org-entry-get-with-inheritance property literal-nil)
13520 (if (member property org-special-properties)
13521 ;; We need a special property. Use `org-entry-properties' to
13522 ;; retrieve it, but specify the wanted property
13523 (cdr (assoc property (org-entry-properties nil 'special property)))
13524 (let ((range (org-get-property-block)))
13526 (goto-char (car range))
13528 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
13530 ;; Found the property, return it.
13533 (org-match-string-no-properties 1)
13534 (org-not-nil (org-match-string-no-properties 1)))
13537 (defun org-property-or-variable-value (var &optional inherit)
13538 "Check if there is a property fixing the value of VAR.
13539 If yes, return this value. If not, return the current value of the variable."
13540 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
13541 (if (and prop (stringp prop) (string-match "\\S-" prop))
13543 (symbol-value var))))
13545 (defun org-entry-delete (pom property)
13546 "Delete the property PROPERTY from entry at point-or-marker POM."
13547 (org-with-point-at pom
13548 (if (member property org-special-properties)
13549 nil ; cannot delete these properties.
13550 (let ((range (org-get-property-block)))
13552 (goto-char (car range))
13554 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
13557 (delete-region (match-beginning 0) (1+ (point-at-eol)))
13561 ;; Multi-values properties are properties that contain multiple values
13562 ;; These values are assumed to be single words, separated by whitespace.
13563 (defun org-entry-add-to-multivalued-property (pom property value)
13564 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
13565 (let* ((old (org-entry-get pom property))
13566 (values (and old (org-split-string old "[ \t]"))))
13567 (setq value (org-entry-protect-space value))
13568 (unless (member value values)
13569 (setq values (cons value values))
13570 (org-entry-put pom property
13571 (mapconcat 'identity values " ")))))
13573 (defun org-entry-remove-from-multivalued-property (pom property value)
13574 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
13575 (let* ((old (org-entry-get pom property))
13576 (values (and old (org-split-string old "[ \t]"))))
13577 (setq value (org-entry-protect-space value))
13578 (when (member value values)
13579 (setq values (delete value values))
13580 (org-entry-put pom property
13581 (mapconcat 'identity values " ")))))
13583 (defun org-entry-member-in-multivalued-property (pom property value)
13584 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
13585 (let* ((old (org-entry-get pom property))
13586 (values (and old (org-split-string old "[ \t]"))))
13587 (setq value (org-entry-protect-space value))
13588 (member value values)))
13590 (defun org-entry-get-multivalued-property (pom property)
13591 "Return a list of values in a multivalued property."
13592 (let* ((value (org-entry-get pom property))
13593 (values (and value (org-split-string value "[ \t]"))))
13594 (mapcar 'org-entry-restore-space values)))
13596 (defun org-entry-put-multivalued-property (pom property &rest values)
13597 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
13598 VALUES should be a list of strings. Spaces will be protected."
13599 (org-entry-put pom property
13600 (mapconcat 'org-entry-protect-space values " "))
13601 (let* ((value (org-entry-get pom property))
13602 (values (and value (org-split-string value "[ \t]"))))
13603 (mapcar 'org-entry-restore-space values)))
13605 (defun org-entry-protect-space (s)
13606 "Protect spaces and newline in string S."
13607 (while (string-match " " s)
13608 (setq s (replace-match "%20" t t s)))
13609 (while (string-match "\n" s)
13610 (setq s (replace-match "%0A" t t s)))
13613 (defun org-entry-restore-space (s)
13614 "Restore spaces and newline in string S."
13615 (while (string-match "%20" s)
13616 (setq s (replace-match " " t t s)))
13617 (while (string-match "%0A" s)
13618 (setq s (replace-match "\n" t t s)))
13621 (defvar org-entry-property-inherited-from (make-marker)
13622 "Marker pointing to the entry from where a property was inherited.
13623 Each call to `org-entry-get-with-inheritance' will set this marker to the
13624 location of the entry where the inheritance search matched. If there was
13625 no match, the marker will point nowhere.
13626 Note that also `org-entry-get' calls this function, if the INHERIT flag
13629 (defun org-entry-get-with-inheritance (property &optional literal-nil)
13630 "Get entry property, and search higher levels if not present.
13631 The search will stop at the first ancestor which has the property defined.
13632 If the value found is \"nil\", return nil to show that the property
13633 should be considered as undefined (this is the meaning of nil here).
13634 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
13635 (move-marker org-entry-property-inherited-from nil)
13642 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
13643 (org-back-to-heading t)
13644 (move-marker org-entry-property-inherited-from (point))
13646 (or (org-up-heading-safe) (throw 'ex nil)))))
13648 (cdr (assoc property org-file-properties))
13649 (cdr (assoc property org-global-properties))
13650 (cdr (assoc property org-global-properties-fixed))))
13651 (if literal-nil tmp (org-not-nil tmp)))))
13653 (defvar org-property-changed-functions nil
13654 "Hook called when the value of a property has changed.
13655 Each hook function should accept two arguments, the name of the property
13656 and the new value.")
13658 (defun org-entry-put (pom property value)
13659 "Set PROPERTY to VALUE for entry at point-or-marker POM."
13660 (org-with-point-at pom
13661 (org-back-to-heading t)
13662 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
13665 ((equal property "TODO")
13666 (when (and (stringp value) (string-match "\\S-" value)
13667 (not (member value org-todo-keywords-1)))
13668 (error "\"%s\" is not a valid TODO state" value))
13669 (if (or (not value)
13670 (not (string-match "\\S-" value)))
13671 (setq value 'none))
13673 (org-set-tags nil 'align))
13674 ((equal property "PRIORITY")
13675 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
13676 (string-to-char value) ?\ ))
13677 (org-set-tags nil 'align))
13678 ((equal property "SCHEDULED")
13679 (if (re-search-forward org-scheduled-time-regexp end t)
13681 ((eq value 'earlier) (org-timestamp-change -1 'day))
13682 ((eq value 'later) (org-timestamp-change 1 'day))
13683 (t (call-interactively 'org-schedule)))
13684 (call-interactively 'org-schedule)))
13685 ((equal property "DEADLINE")
13686 (if (re-search-forward org-deadline-time-regexp end t)
13688 ((eq value 'earlier) (org-timestamp-change -1 'day))
13689 ((eq value 'later) (org-timestamp-change 1 'day))
13690 (t (call-interactively 'org-deadline)))
13691 (call-interactively 'org-deadline)))
13692 ((member property org-special-properties)
13693 (error "The %s property can not yet be set with `org-entry-put'"
13695 (t ; a non-special property
13696 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
13697 (setq range (org-get-property-block beg end 'force))
13698 (goto-char (car range))
13699 (if (re-search-forward
13700 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
13702 (delete-region (match-beginning 1) (match-end 1))
13703 (goto-char (match-beginning 1)))
13704 (goto-char (cdr range))
13707 (org-indent-line-function)
13708 (insert ":" property ":"))
13709 (and value (insert " " value))
13710 (org-indent-line-function)))))
13711 (run-hook-with-args 'org-property-changed-functions property value)))
13713 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
13714 "Get all property keys in the current buffer.
13715 With INCLUDE-SPECIALS, also list the special properties that reflect things
13716 like tags and TODO state.
13717 With INCLUDE-DEFAULTS, also include properties that has special meaning
13718 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
13719 With INCLUDE-COLUMNS, also include property names given in COLUMN
13720 formats in the current buffer."
13721 (let (rtn range cfmt s p)
13725 (goto-char (point-min))
13726 (while (re-search-forward org-property-start-re nil t)
13727 (setq range (org-get-property-block))
13728 (goto-char (car range))
13729 (while (re-search-forward
13730 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
13732 (add-to-list 'rtn (org-match-string-no-properties 1)))
13733 (outline-next-heading))))
13735 (when include-specials
13736 (setq rtn (append org-special-properties rtn)))
13738 (when include-defaults
13739 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
13740 (add-to-list 'rtn org-effort-property))
13742 (when include-columns
13746 (goto-char (point-min))
13747 (while (re-search-forward
13748 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
13750 (setq cfmt (match-string 2) s 0)
13751 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
13753 (setq s (match-end 0)
13754 p (match-string 1 cfmt))
13755 (unless (or (equal p "ITEM")
13756 (member p org-special-properties))
13757 (add-to-list 'rtn (match-string 1 cfmt))))))))
13759 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
13761 (defun org-property-values (key)
13762 "Return a list of all values of property KEY."
13766 (goto-char (point-min))
13767 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
13769 (while (re-search-forward re nil t)
13770 (add-to-list 'values (org-trim (match-string 1))))
13771 (delete "" values)))))
13773 (defun org-insert-property-drawer ()
13774 "Insert a property drawer into the current entry."
13776 (org-back-to-heading t)
13777 (looking-at outline-regexp)
13778 (let ((indent (if org-adapt-indentation
13779 (- (match-end 0)(match-beginning 0))
13782 (re (concat "^[ \t]*" org-keyword-time-regexp))
13784 (outline-next-heading)
13787 (while (re-search-forward re end t))
13788 (setq hiddenp (org-invisible-p))
13790 (and (equal (char-after) ?\n) (forward-char 1))
13791 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
13792 (if (member (match-string 1) '("CLOCK:" ":END:"))
13793 ;; just skip this line
13794 (beginning-of-line 2)
13795 ;; Drawer start, find the end
13796 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
13797 (beginning-of-line 1)))
13798 (org-skip-over-state-notes)
13799 (skip-chars-backward " \t\n\r")
13800 (if (eq (char-before) ?*) (forward-char 1))
13801 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
13802 (beginning-of-line 0)
13803 (org-indent-to-column indent)
13804 (beginning-of-line 2)
13805 (org-indent-to-column indent)
13806 (beginning-of-line 0)
13809 (org-back-to-heading t)
13811 (org-flag-drawer t))))
13813 (defun org-set-property (property value)
13814 "In the current entry, set PROPERTY to VALUE.
13815 When called interactively, this will prompt for a property name, offering
13816 completion on existing and default properties. And then it will prompt
13817 for a value, offering completion either on allowed values (via an inherited
13818 xxx_ALL property) or on existing values in other instances of this property
13819 in the current file."
13821 (let* ((completion-ignore-case t)
13822 (keys (org-buffer-property-keys nil t t))
13823 (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
13824 (prop (if (member prop0 keys)
13826 (or (cdr (assoc (downcase prop0)
13827 (mapcar (lambda (x) (cons (downcase x) x))
13830 (cur (org-entry-get nil prop))
13831 (prompt (concat prop " value"
13832 (if (and cur (string-match "\\S-" cur))
13833 (concat " [" cur "]") "") ": "))
13834 (allowed (org-property-get-allowed-values nil prop 'table))
13835 (existing (mapcar 'list (org-property-values prop)))
13837 (org-completing-read prompt allowed nil
13838 (not (get-text-property 0 'org-unrestricted
13840 (let (org-completion-use-ido org-completion-use-iswitchb)
13841 (org-completing-read prompt existing nil nil "" nil cur)))))
13842 (list prop (if (equal val "") cur val))))
13843 (unless (equal (org-entry-get nil property) value)
13844 (org-entry-put nil property value)))
13846 (defun org-delete-property (property)
13847 "In the current entry, delete PROPERTY."
13849 (let* ((completion-ignore-case t)
13850 (prop (org-icompleting-read "Property: "
13851 (org-entry-properties nil 'standard))))
13853 (message "Property %s %s" property
13854 (if (org-entry-delete nil property)
13856 "was not present in the entry")))
13858 (defun org-delete-property-globally (property)
13859 "Remove PROPERTY globally, from all entries."
13861 (let* ((completion-ignore-case t)
13862 (prop (org-icompleting-read
13863 "Globally remove property: "
13864 (mapcar 'list (org-buffer-property-keys)))))
13869 (goto-char (point-min))
13871 (while (re-search-forward
13872 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
13874 (setq cnt (1+ cnt))
13875 (replace-match ""))
13876 (message "Property \"%s\" removed from %d entries" property cnt)))))
13878 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
13880 (defun org-compute-property-at-point ()
13881 "Compute the property at point.
13882 This looks for an enclosing column format, extracts the operator and
13883 then applies it to the property in the column format's scope."
13885 (unless (org-at-property-p)
13886 (error "Not at a property"))
13887 (let ((prop (org-match-string-no-properties 2)))
13888 (org-columns-get-format-and-top-level)
13889 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
13890 (error "No operator defined for property %s" prop))
13891 (org-columns-compute prop)))
13893 (defvar org-property-allowed-value-functions nil
13894 "Hook for functions supplying allowed values for a specific property.
13895 The functions must take a single argument, the name of the property, and
13896 return a flat list of allowed values. If \":ETC\" is one of
13897 the values, this means that these values are intended as defaults for
13898 completion, but that other values should be allowed too.
13899 The functions must return nil if they are not responsible for this
13902 (defun org-property-get-allowed-values (pom property &optional table)
13903 "Get allowed values for the property PROPERTY.
13904 When TABLE is non-nil, return an alist that can directly be used for
13908 ((equal property "TODO")
13909 (setq vals (org-with-point-at pom
13910 (append org-todo-keywords-1 '("")))))
13911 ((equal property "PRIORITY")
13912 (let ((n org-lowest-priority))
13913 (while (>= n org-highest-priority)
13914 (push (char-to-string n) vals)
13916 ((member property org-special-properties))
13917 ((setq vals (run-hook-with-args-until-success
13918 'org-property-allowed-value-functions property)))
13920 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
13921 (when (and vals (string-match "\\S-" vals))
13922 (setq vals (car (read-from-string (concat "(" vals ")"))))
13923 (setq vals (mapcar (lambda (x)
13924 (cond ((stringp x) x)
13925 ((numberp x) (number-to-string x))
13926 ((symbolp x) (symbol-name x))
13929 (when (member ":ETC" vals)
13930 (setq vals (remove ":ETC" vals))
13931 (org-add-props (car vals) '(org-unrestricted t)))
13932 (if table (mapcar 'list vals) vals)))
13934 (defun org-property-previous-allowed-value (&optional previous)
13935 "Switch to the next allowed value for this property."
13937 (org-property-next-allowed-value t))
13939 (defun org-property-next-allowed-value (&optional previous)
13940 "Switch to the next allowed value for this property."
13942 (unless (org-at-property-p)
13943 (error "Not at a property"))
13944 (let* ((key (match-string 2))
13945 (value (match-string 3))
13946 (allowed (or (org-property-get-allowed-values (point) key)
13947 (and (member value '("[ ]" "[-]" "[X]"))
13951 (error "Allowed values for this property have not been defined"))
13952 (if previous (setq allowed (reverse allowed)))
13953 (if (member value allowed)
13954 (setq nval (car (cdr (member value allowed)))))
13955 (setq nval (or nval (car allowed)))
13956 (if (equal nval value)
13957 (error "Only one allowed value for this property"))
13958 (org-at-property-p)
13959 (replace-match (concat " :" key ": " nval) t t)
13960 (org-indent-line-function)
13961 (beginning-of-line 1)
13962 (skip-chars-forward " \t")
13963 (run-hook-with-args 'org-property-changed-functions key nval)))
13965 (defun org-find-olp (path &optional this-buffer)
13966 "Return a marker pointing to the entry at outline path OLP.
13967 If anything goes wrong, throw an error.
13968 You can wrap this call to catch the error like this:
13970 (condition-case msg
13971 (org-mobile-locate-entry (match-string 4))
13972 (error (nth 1 msg)))
13974 The return value will then be either a string with the error message,
13975 or a marker if everything is OK.
13977 If THIS-BUFFER is set, the outline path does not contain a file,
13979 (let* ((file (if this-buffer buffer-file-name (pop path)))
13980 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
13984 limit re end found pos heading cnt)
13985 (unless buffer (error "File not found :%s" file))
13986 (with-current-buffer buffer
13990 (setq limit (point-max))
13991 (goto-char (point-min))
13992 (while (setq heading (pop path))
13993 (setq re (format org-complex-heading-regexp-format
13994 (regexp-quote heading)))
13995 (setq cnt 0 pos (point))
13996 (while (re-search-forward re end t)
13997 (setq level (- (match-end 1) (match-beginning 1)))
13998 (if (and (>= level lmin) (<= level lmax))
13999 (setq found (match-beginning 0) cnt (1+ cnt))))
14000 (when (= cnt 0) (error "Heading not found on level %d: %s"
14002 (when (> cnt 1) (error "Heading not unique on level %d: %s"
14005 (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
14006 (setq end (save-excursion (org-end-of-subtree t t))))
14007 (when (org-on-heading-p)
14008 (move-marker (make-marker) (point))))))))
14010 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
14011 "Find node HEADING in BUFFER.
14012 Return a marker to the heading if it was found, or nil if not.
14013 If POS-ONLY is set, return just the position instead of a marker.
14015 The heading text must match exact, but it may have a TODO keyword,
14016 a priority cookie and tags in the standard locations."
14017 (with-current-buffer (or buffer (current-buffer))
14021 (goto-char (point-min))
14022 (let (case-fold-search)
14023 (if (re-search-forward
14024 (format org-complex-heading-regexp-format
14025 (regexp-quote heading)) nil t)
14027 (match-beginning 0)
14028 (move-marker (make-marker) (match-beginning 0)))))))))
14030 (defun org-find-exact-heading-in-directory (heading &optional dir)
14031 "Find Org node headline HEADING in all .org files in directory DIR.
14032 When the target headline is found, return a marker to this location."
14033 (let ((files (directory-files (or dir default-directory)
14034 nil "\\`[^.#].*\\.org\\'"))
14035 file visiting m buffer)
14037 (while (setq file (pop files))
14038 (message "trying %s" file)
14039 (setq visiting (org-find-base-buffer-visiting file))
14040 (setq buffer (or visiting (find-file-noselect file)))
14041 (setq m (org-find-exact-headline-in-buffer
14043 (when (and (not m) (not visiting)) (kill-buffer buffer))
14044 (and m (throw 'found m))))))
14046 (defun org-find-entry-with-id (ident)
14047 "Locate the entry that contains the ID property with exact value IDENT.
14048 IDENT can be a string, a symbol or a number, this function will search for
14049 the string representation of it.
14050 Return the position where this entry starts, or nil if there is no such entry."
14051 (interactive "sID: ")
14053 ((stringp ident) ident)
14054 ((symbol-name ident) (symbol-name ident))
14055 ((numberp ident) (number-to-string ident))
14056 (t (error "IDENT %s must be a string, symbol or number" ident))))
14057 (case-fold-search nil))
14061 (goto-char (point-min))
14062 (when (re-search-forward
14063 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
14065 (org-back-to-heading t)
14070 (defvar org-last-changed-timestamp nil)
14071 (defvar org-last-inserted-timestamp nil
14072 "The last time stamp inserted with `org-insert-time-stamp'.")
14073 (defvar org-time-was-given) ; dynamically scoped parameter
14074 (defvar org-end-time-was-given) ; dynamically scoped parameter
14075 (defvar org-ts-what) ; dynamically scoped parameter
14077 (defun org-time-stamp (arg &optional inactive)
14078 "Prompt for a date/time and insert a time stamp.
14079 If the user specifies a time like HH:MM, or if this command is called
14080 with a prefix argument, the time stamp will contain date and time.
14081 Otherwise, only the date will be included. All parts of a date not
14082 specified by the user will be filled in from the current date/time.
14083 So if you press just return without typing anything, the time stamp
14084 will represent the current date/time. If there is already a timestamp
14085 at the cursor, it will be modified."
14089 ;; Default time is either today, or, when entering a range,
14090 ;; the range start.
14091 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
14093 (re-search-backward
14094 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
14095 (- (point) 20) t)))
14096 (apply 'encode-time (org-parse-time-string (match-string 1)))
14098 (default-input (and ts (org-get-compact-tod ts)))
14099 org-time-was-given org-end-time-was-given time)
14101 ((and (org-at-timestamp-p t)
14102 (memq last-command '(org-time-stamp org-time-stamp-inactive))
14103 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
14105 (setq time (let ((this-command this-command))
14106 (org-read-date arg 'totime nil nil
14107 default-time default-input)))
14108 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
14109 ((org-at-timestamp-p t)
14110 (setq time (let ((this-command this-command))
14111 (org-read-date arg 'totime nil nil default-time default-input)))
14112 (when (org-at-timestamp-p t) ; just to get the match data
14113 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
14115 (setq org-last-changed-timestamp
14116 (org-insert-time-stamp
14117 time (or org-time-was-given arg)
14118 inactive nil nil (list org-end-time-was-given))))
14119 (message "Timestamp updated"))
14121 (setq time (let ((this-command this-command))
14122 (org-read-date arg 'totime nil nil default-time default-input)))
14123 (org-insert-time-stamp time (or org-time-was-given arg) inactive
14124 nil nil (list org-end-time-was-given))))))
14126 ;; FIXME: can we use this for something else, like computing time differences?
14127 (defun org-get-compact-tod (s)
14128 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
14129 (let* ((t1 (match-string 1 s))
14130 (h1 (string-to-number (match-string 2 s)))
14131 (m1 (string-to-number (match-string 3 s)))
14132 (t2 (and (match-end 4) (match-string 5 s)))
14133 (h2 (and t2 (string-to-number (match-string 6 s))))
14134 (m2 (and t2 (string-to-number (match-string 7 s))))
14138 (setq dh (- h2 h1) dm (- m2 m1))
14139 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
14140 (concat t1 "+" (number-to-string dh)
14141 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
14143 (defun org-time-stamp-inactive (&optional arg)
14144 "Insert an inactive time stamp.
14145 An inactive time stamp is enclosed in square brackets instead of angle
14146 brackets. It is inactive in the sense that it does not trigger agenda entries,
14147 does not link to the calendar and cannot be changed with the S-cursor keys.
14148 So these are more for recording a certain time/date."
14150 (org-time-stamp arg 'inactive))
14152 (defvar org-date-ovl (make-overlay 1 1))
14153 (overlay-put org-date-ovl 'face 'org-warning)
14154 (org-detach-overlay org-date-ovl)
14156 (defvar org-ans1) ; dynamically scoped parameter
14157 (defvar org-ans2) ; dynamically scoped parameter
14159 (defvar org-plain-time-of-day-regexp) ; defined below
14161 (defvar org-overriding-default-time nil) ; dynamically scoped
14162 (defvar org-read-date-overlay nil)
14163 (defvar org-dcst nil) ; dynamically scoped
14164 (defvar org-read-date-history nil)
14165 (defvar org-read-date-final-answer nil)
14167 (defun org-read-date (&optional with-time to-time from-string prompt
14168 default-time default-input)
14169 "Read a date, possibly a time, and make things smooth for the user.
14170 The prompt will suggest to enter an ISO date, but you can also enter anything
14171 which will at least partially be understood by `parse-time-string'.
14172 Unrecognized parts of the date will default to the current day, month, year,
14173 hour and minute. If this command is called to replace a timestamp at point,
14174 of to enter the second timestamp of a range, the default time is taken
14175 from the existing stamp. Furthermore, the command prefers the future,
14176 so if you are giving a date where the year is not given, and the day-month
14177 combination is already past in the current year, it will assume you
14178 mean next year. For details, see the manual. A few examples:
14180 3-2-5 --> 2003-02-05
14181 feb 15 --> currentyear-02-15
14182 2/15 --> currentyear-02-15
14183 sep 12 9 --> 2009-09-12
14184 12:45 --> today 12:45
14185 22 sept 0:34 --> currentyear-09-22 0:34
14186 12 --> currentyear-currentmonth-12
14187 Fri --> nearest Friday (today or later)
14190 Furthermore you can specify a relative date by giving, as the *first* thing
14191 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
14192 change in days weeks, months, years.
14193 With a single plus or minus, the date is relative to today. With a double
14194 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
14195 +4d --> four days from today
14196 +4 --> same as above
14197 +2w --> two weeks from today
14198 ++5 --> five days from default date
14200 The function understands only English month and weekday abbreviations,
14201 but this can be configured with the variables `parse-time-months' and
14202 `parse-time-weekdays'.
14204 While prompting, a calendar is popped up - you can also select the
14205 date with the mouse (button 1). The calendar shows a period of three
14206 months. To scroll it to other months, use the keys `>' and `<'.
14207 If you don't like the calendar, turn it off with
14208 \(setq org-read-date-popup-calendar nil)
14210 With optional argument TO-TIME, the date will immediately be converted
14211 to an internal time.
14212 With an optional argument WITH-TIME, the prompt will suggest to also
14213 insert a time. Note that when WITH-TIME is not set, you can still
14214 enter a time, and this function will inform the calling routine about
14215 this change. The calling routine may then choose to change the format
14216 used to insert the time stamp into the buffer to include the time.
14217 With optional argument FROM-STRING, read from this string instead from
14218 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
14219 the time/date that is used for everything that is not specified by the
14221 (require 'parse-time)
14222 (let* ((org-time-stamp-rounding-minutes
14223 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
14224 (org-dcst org-display-custom-times)
14225 (ct (org-current-time))
14226 (def (or org-overriding-default-time default-time ct))
14227 (defdecode (decode-time def))
14229 (when (< (nth 2 defdecode) org-extend-today-until)
14230 (setcar (nthcdr 2 defdecode) -1)
14231 (setcar (nthcdr 1 defdecode) 59)
14232 (setq def (apply 'encode-time defdecode)
14233 defdecode (decode-time def)))))
14234 (calendar-frame-setup nil)
14235 (calendar-setup nil)
14236 (calendar-move-hook nil)
14237 (calendar-view-diary-initially-flag nil)
14238 (calendar-view-holidays-initially-flag nil)
14239 (timestr (format-time-string
14240 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
14241 (prompt (concat (if prompt (concat prompt " ") "")
14242 (format "Date+time [%s]: " timestr)))
14243 ans (org-ans0 "") org-ans1 org-ans2 final)
14246 (from-string (setq ans from-string))
14247 (org-read-date-popup-calendar
14249 (save-window-excursion
14251 (calendar-forward-day (- (time-to-days def)
14252 (calendar-absolute-from-gregorian
14253 (calendar-current-date))))
14254 (org-eval-in-calendar nil t)
14255 (let* ((old-map (current-local-map))
14256 (map (copy-keymap calendar-mode-map))
14257 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
14258 (org-defkey map (kbd "RET") 'org-calendar-select)
14259 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
14260 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
14261 (org-defkey minibuffer-local-map [(meta shift left)]
14262 (lambda () (interactive)
14263 (org-eval-in-calendar '(calendar-backward-month 1))))
14264 (org-defkey minibuffer-local-map [(meta shift right)]
14265 (lambda () (interactive)
14266 (org-eval-in-calendar '(calendar-forward-month 1))))
14267 (org-defkey minibuffer-local-map [(meta shift up)]
14268 (lambda () (interactive)
14269 (org-eval-in-calendar '(calendar-backward-year 1))))
14270 (org-defkey minibuffer-local-map [(meta shift down)]
14271 (lambda () (interactive)
14272 (org-eval-in-calendar '(calendar-forward-year 1))))
14273 (org-defkey minibuffer-local-map [?\e (shift left)]
14274 (lambda () (interactive)
14275 (org-eval-in-calendar '(calendar-backward-month 1))))
14276 (org-defkey minibuffer-local-map [?\e (shift right)]
14277 (lambda () (interactive)
14278 (org-eval-in-calendar '(calendar-forward-month 1))))
14279 (org-defkey minibuffer-local-map [?\e (shift up)]
14280 (lambda () (interactive)
14281 (org-eval-in-calendar '(calendar-backward-year 1))))
14282 (org-defkey minibuffer-local-map [?\e (shift down)]
14283 (lambda () (interactive)
14284 (org-eval-in-calendar '(calendar-forward-year 1))))
14285 (org-defkey minibuffer-local-map [(shift up)]
14286 (lambda () (interactive)
14287 (org-eval-in-calendar '(calendar-backward-week 1))))
14288 (org-defkey minibuffer-local-map [(shift down)]
14289 (lambda () (interactive)
14290 (org-eval-in-calendar '(calendar-forward-week 1))))
14291 (org-defkey minibuffer-local-map [(shift left)]
14292 (lambda () (interactive)
14293 (org-eval-in-calendar '(calendar-backward-day 1))))
14294 (org-defkey minibuffer-local-map [(shift right)]
14295 (lambda () (interactive)
14296 (org-eval-in-calendar '(calendar-forward-day 1))))
14297 (org-defkey minibuffer-local-map ">"
14298 (lambda () (interactive)
14299 (org-eval-in-calendar '(scroll-calendar-left 1))))
14300 (org-defkey minibuffer-local-map "<"
14301 (lambda () (interactive)
14302 (org-eval-in-calendar '(scroll-calendar-right 1))))
14303 (org-defkey minibuffer-local-map "\C-v"
14304 (lambda () (interactive)
14305 (org-eval-in-calendar
14306 '(calendar-scroll-left-three-months 1))))
14307 (org-defkey minibuffer-local-map "\M-v"
14308 (lambda () (interactive)
14309 (org-eval-in-calendar
14310 '(calendar-scroll-right-three-months 1))))
14311 (run-hooks 'org-read-date-minibuffer-setup-hook)
14314 (use-local-map map)
14315 (add-hook 'post-command-hook 'org-read-date-display)
14316 (setq org-ans0 (read-string prompt default-input
14317 'org-read-date-history nil))
14318 ;; org-ans0: from prompt
14319 ;; org-ans1: from mouse click
14320 ;; org-ans2: from calendar motion
14321 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
14322 (remove-hook 'post-command-hook 'org-read-date-display)
14323 (use-local-map old-map)
14324 (when org-read-date-overlay
14325 (delete-overlay org-read-date-overlay)
14326 (setq org-read-date-overlay nil)))))))
14328 (t ; Naked prompt only
14330 (setq ans (read-string prompt default-input
14331 'org-read-date-history timestr))
14332 (when org-read-date-overlay
14333 (delete-overlay org-read-date-overlay)
14334 (setq org-read-date-overlay nil)))))
14336 (setq final (org-read-date-analyze ans def defdecode))
14338 ;; One round trip to get rid of 34th of August and stuff like that....
14339 (setq final (decode-time (apply 'encode-time final)))
14341 (setq org-read-date-final-answer ans)
14344 (apply 'encode-time final)
14345 (if (and (boundp 'org-time-was-given) org-time-was-given)
14346 (format "%04d-%02d-%02d %02d:%02d"
14347 (nth 5 final) (nth 4 final) (nth 3 final)
14348 (nth 2 final) (nth 1 final))
14349 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
14354 (defvar org-read-date-analyze-futurep nil)
14355 (defun org-read-date-display ()
14356 "Display the current date prompt interpretation in the minibuffer."
14357 (when org-read-date-display-live
14358 (when org-read-date-overlay
14359 (delete-overlay org-read-date-overlay))
14362 (while (not (equal (buffer-substring
14363 (max (point-min) (- (point) 4)) (point))
14367 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
14368 " " (or org-ans1 org-ans2)))
14369 (org-end-time-was-given nil)
14370 (f (org-read-date-analyze ans def defdecode))
14372 org-time-stamp-custom-formats
14373 org-time-stamp-formats))
14374 (fmt (if (or with-time
14375 (and (boundp 'org-time-was-given) org-time-was-given))
14378 (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
14379 (when (and org-end-time-was-given
14380 (string-match org-plain-time-of-day-regexp txt))
14381 (setq txt (concat (substring txt 0 (match-end 0)) "-"
14382 org-end-time-was-given
14383 (substring txt (match-end 0)))))
14384 (when org-read-date-analyze-futurep
14385 (setq txt (concat txt " (=>F)")))
14386 (setq org-read-date-overlay
14387 (make-overlay (1- (point-at-eol)) (point-at-eol)))
14388 (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
14390 (defun org-read-date-analyze (ans def defdecode)
14391 "Analyze the combined answer of the date prompt."
14392 ;; FIXME: cleanup and comment
14393 (let ((nowdecode (decode-time (current-time)))
14394 delta deltan deltaw deltadef year month day
14395 hour minute second wday pm h2 m2 tl wday1
14396 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
14397 (setq org-read-date-analyze-futurep nil)
14398 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
14401 (when (setq delta (org-read-date-get-relative ans (current-time) def))
14402 (setq ans (replace-match "" t t ans)
14404 deltaw (nth 1 delta)
14405 deltadef (nth 2 delta)))
14407 ;; Check if there is an iso week date in there
14408 ;; If yes, store the info and postpone interpreting it until the rest
14409 ;; of the parsing is done
14410 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
14411 (setq iso-year (if (match-end 1)
14412 (org-small-year-to-year
14413 (string-to-number (match-string 1 ans))))
14414 iso-weekday (if (match-end 3)
14415 (string-to-number (match-string 3 ans)))
14416 iso-week (string-to-number (match-string 2 ans)))
14417 (setq ans (replace-match "" t t ans)))
14419 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
14420 (when (string-match
14421 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
14422 (setq year (if (match-end 2)
14423 (string-to-number (match-string 2 ans))
14424 (progn (setq kill-year t)
14425 (string-to-number (format-time-string "%Y"))))
14426 month (string-to-number (match-string 3 ans))
14427 day (string-to-number (match-string 4 ans)))
14428 (if (< year 100) (setq year (+ 2000 year)))
14429 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
14431 ;; Help matching american dates, like 5/30 or 5/30/7
14432 (when (string-match
14433 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
14434 (setq year (if (match-end 4)
14435 (string-to-number (match-string 4 ans))
14436 (progn (setq kill-year t)
14437 (string-to-number (format-time-string "%Y"))))
14438 month (string-to-number (match-string 1 ans))
14439 day (string-to-number (match-string 2 ans)))
14440 (if (< year 100) (setq year (+ 2000 year)))
14441 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
14443 ;; Help matching am/pm times, because `parse-time-string' does not do that.
14444 ;; If there is a time with am/pm, and *no* time without it, we convert
14445 ;; so that matching will be successful.
14446 (loop for i from 1 to 2 do ; twice, for end time as well
14447 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
14448 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
14449 (setq hour (string-to-number (match-string 1 ans))
14450 minute (if (match-end 3)
14451 (string-to-number (match-string 3 ans))
14454 (string-to-char (downcase (match-string 4 ans)))))
14455 (if (and (= hour 12) (not pm))
14457 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
14458 (setq ans (replace-match (format "%02d:%02d" hour minute)
14461 ;; Check if a time range is given as a duration
14462 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
14463 (setq hour (string-to-number (match-string 1 ans))
14464 h2 (+ hour (string-to-number (match-string 3 ans)))
14465 minute (string-to-number (match-string 2 ans))
14466 m2 (+ minute (if (match-end 5) (string-to-number
14467 (match-string 5 ans))0)))
14468 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
14469 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
14472 ;; Check if there is a time range
14473 (when (boundp 'org-end-time-was-given)
14474 (setq org-time-was-given nil)
14475 (when (and (string-match org-plain-time-of-day-regexp ans)
14477 (setq org-end-time-was-given (match-string 8 ans))
14478 (setq ans (concat (substring ans 0 (match-beginning 7))
14479 (substring ans (match-end 7))))))
14481 (setq tl (parse-time-string ans)
14482 day (or (nth 3 tl) (nth 3 defdecode))
14483 month (or (nth 4 tl)
14484 (if (and org-read-date-prefer-future
14485 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
14486 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
14487 (nth 4 defdecode)))
14488 year (or (and (not kill-year) (nth 5 tl))
14489 (if (and org-read-date-prefer-future
14490 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
14491 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
14492 (nth 5 defdecode)))
14493 hour (or (nth 2 tl) (nth 2 defdecode))
14494 minute (or (nth 1 tl) (nth 1 defdecode))
14495 second (or (nth 0 tl) 0)
14498 (when (and (eq org-read-date-prefer-future 'time)
14499 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
14500 (equal day (nth 3 nowdecode))
14501 (equal month (nth 4 nowdecode))
14502 (equal year (nth 5 nowdecode))
14504 (or (< (nth 2 tl) (nth 2 nowdecode))
14505 (and (= (nth 2 tl) (nth 2 nowdecode))
14507 (< (nth 1 tl) (nth 1 nowdecode)))))
14511 ;; Special date definitions below
14514 ;; There was an iso week
14517 (setq year (or iso-year year)
14518 day (or iso-weekday wday 1)
14519 wday nil ; to make sure that the trigger below does not match
14520 iso-date (calendar-gregorian-from-absolute
14521 (calendar-absolute-from-iso
14522 (list iso-week day year))))
14523 ; FIXME: Should we also push ISO weeks into the future?
14524 ; (when (and org-read-date-prefer-future
14526 ; (< (calendar-absolute-from-gregorian iso-date)
14527 ; (time-to-days (current-time))))
14528 ; (setq year (1+ year)
14529 ; iso-date (calendar-gregorian-from-absolute
14530 ; (calendar-absolute-from-iso
14531 ; (list iso-week day year)))))
14532 (setq month (car iso-date)
14533 year (nth 2 iso-date)
14534 day (nth 1 iso-date)))
14538 (let ((now (decode-time (current-time))))
14539 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
14540 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
14541 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
14542 ((equal deltaw "m") (setq month (+ month deltan)))
14543 ((equal deltaw "y") (setq year (+ year deltan)))))
14544 ((and wday (not (nth 3 tl)))
14546 ;; Weekday was given, but no day, so pick that day in the week
14547 ;; on or after the derived date.
14548 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
14549 (unless (equal wday wday1)
14550 (setq day (+ day (% (- wday wday1 -7) 7))))))
14551 (if (and (boundp 'org-time-was-given)
14553 (setq org-time-was-given t))
14554 (if (< year 100) (setq year (+ 2000 year)))
14555 (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
14556 (setq org-read-date-analyze-futurep futurep)
14557 (list second minute hour day month year)))
14559 (defvar parse-time-weekdays)
14561 (defun org-read-date-get-relative (s today default)
14562 "Check string S for special relative date string.
14563 TODAY and DEFAULT are internal times, for today and for a default.
14564 Return shift list (N what def-flag)
14565 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
14566 N is the number of WHATs to shift.
14567 DEF-FLAG is t when a double ++ or -- indicates shift relative to
14568 the DEFAULT date rather than TODAY."
14572 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
14574 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
14575 "\\([ \t]\\|$\\)") s)
14576 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
14577 (let* ((dir (if (> (match-end 1) (match-beginning 1))
14578 (string-to-char (substring (match-string 1 s) -1))
14580 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
14581 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
14582 (what (if (match-end 3) (match-string 3 s) "d"))
14583 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
14584 (date (if rel default today))
14585 (wday (nth 6 (decode-time date)))
14589 (setq delta (mod (+ 7 (- wday1 wday)) 7))
14590 (if (= dir ?-) (setq delta (- delta 7)))
14591 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
14592 (list delta "d" rel))
14593 (list (* n (if (= dir ?-) -1 1)) what rel)))))
14595 (defun org-order-calendar-date-args (arg1 arg2 arg3)
14596 "Turn a user-specified date into the internal representation.
14597 The internal representation needed by the calendar is (month day year).
14598 This is a wrapper to handle the brain-dead convention in calendar that
14599 user function argument order change dependent on argument order."
14600 (if (boundp 'calendar-date-style)
14602 ((eq calendar-date-style 'american)
14603 (list arg1 arg2 arg3))
14604 ((eq calendar-date-style 'european)
14605 (list arg2 arg1 arg3))
14606 ((eq calendar-date-style 'iso)
14607 (list arg2 arg3 arg1)))
14608 (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
14609 (if (org-bound-and-true-p european-calendar-style)
14610 (list arg2 arg1 arg3)
14611 (list arg1 arg2 arg3)))))
14613 (defun org-eval-in-calendar (form &optional keepdate)
14614 "Eval FORM in the calendar window and return to current window.
14615 Also, store the cursor date in variable org-ans2."
14616 (let ((sf (selected-frame))
14617 (sw (selected-window)))
14618 (select-window (get-buffer-window "*Calendar*" t))
14620 (when (and (not keepdate) (calendar-cursor-to-date))
14621 (let* ((date (calendar-cursor-to-date))
14622 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
14623 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
14624 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
14626 (org-select-frame-set-input-focus sf)))
14628 (defun org-calendar-select ()
14629 "Return to `org-read-date' with the date currently selected.
14630 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
14632 (when (calendar-cursor-to-date)
14633 (let* ((date (calendar-cursor-to-date))
14634 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
14635 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
14636 (if (active-minibuffer-window) (exit-minibuffer))))
14638 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
14639 "Insert a date stamp for the date given by the internal TIME.
14640 WITH-HM means use the stamp format that includes the time of the day.
14641 INACTIVE means use square brackets instead of angular ones, so that the
14642 stamp will not contribute to the agenda.
14643 PRE and POST are optional strings to be inserted before and after the
14645 The command returns the inserted time stamp."
14646 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
14648 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
14649 (insert-before-markers (or pre ""))
14650 (when (listp extra)
14651 (setq extra (car extra))
14652 (if (and (stringp extra)
14653 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
14654 (setq extra (format "-%02d:%02d"
14655 (string-to-number (match-string 1 extra))
14656 (string-to-number (match-string 2 extra))))
14659 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
14660 (insert-before-markers (setq stamp (format-time-string fmt time)))
14661 (insert-before-markers (or post ""))
14662 (setq org-last-inserted-timestamp stamp)))
14664 (defun org-toggle-time-stamp-overlays ()
14665 "Toggle the use of custom time stamp formats."
14667 (setq org-display-custom-times (not org-display-custom-times))
14668 (unless org-display-custom-times
14669 (let ((p (point-min)) (bmp (buffer-modified-p)))
14670 (while (setq p (next-single-property-change p 'display))
14671 (if (and (get-text-property p 'display)
14672 (eq (get-text-property p 'face) 'org-date))
14673 (remove-text-properties
14674 p (setq p (next-single-property-change p 'display))
14676 (set-buffer-modified-p bmp)))
14677 (if (featurep 'xemacs)
14678 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
14679 (org-restart-font-lock)
14680 (setq org-table-may-need-update t)
14681 (if org-display-custom-times
14682 (message "Time stamps are overlayed with custom format")
14683 (message "Time stamp overlays removed")))
14685 (defun org-display-custom-time (beg end)
14686 "Overlay modified time stamp format over timestamp between BEG and END."
14687 (let* ((ts (buffer-substring beg end))
14688 t1 w1 with-hm tf time str w2 (off 0))
14690 (setq t1 (org-parse-time-string ts t))
14691 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
14692 (setq off (- (match-end 0) (match-beginning 0)))))
14693 (setq end (- end off))
14694 (setq w1 (- end beg)
14695 with-hm (and (nth 1 t1) (nth 2 t1))
14696 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
14697 time (org-fix-decoded-time t1)
14699 (format-time-string
14700 (substring tf 1 -1) (apply 'encode-time time))
14701 nil 'mouse-face 'highlight)
14703 (if (not (= w2 w1))
14704 (add-text-properties (1+ beg) (+ 2 beg)
14705 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
14706 (if (featurep 'xemacs)
14708 (put-text-property beg end 'invisible t)
14709 (put-text-property beg end 'end-glyph (make-glyph str)))
14710 (put-text-property beg end 'display str))))
14712 (defun org-translate-time (string)
14713 "Translate all timestamps in STRING to custom format.
14714 But do this only if the variable `org-display-custom-times' is set."
14715 (when org-display-custom-times
14718 (re org-ts-regexp-both)
14719 t1 with-hm inactive tf time str beg end)
14720 (while (setq start (string-match re string start))
14721 (setq beg (match-beginning 0)
14723 t1 (save-match-data
14724 (org-parse-time-string (substring string beg end) t))
14725 with-hm (and (nth 1 t1) (nth 2 t1))
14726 inactive (equal (substring string beg (1+ beg)) "[")
14727 tf (funcall (if with-hm 'cdr 'car)
14728 org-time-stamp-custom-formats)
14729 time (org-fix-decoded-time t1)
14730 str (format-time-string
14732 (if inactive "[" "<") (substring tf 1 -1)
14733 (if inactive "]" ">"))
14734 (apply 'encode-time time))
14735 string (replace-match str t t string)
14736 start (+ start (length str)))))))
14739 (defun org-fix-decoded-time (time)
14740 "Set 0 instead of nil for the first 6 elements of time.
14741 Don't touch the rest."
14743 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
14745 (defun org-days-to-time (timestamp-string)
14746 "Difference between TIMESTAMP-STRING and now in days."
14747 (- (time-to-days (org-time-string-to-time timestamp-string))
14748 (time-to-days (current-time))))
14750 (defun org-deadline-close (timestamp-string &optional ndays)
14751 "Is the time in TIMESTAMP-STRING close to the current date?"
14752 (setq ndays (or ndays (org-get-wdays timestamp-string)))
14753 (and (< (org-days-to-time timestamp-string) ndays)
14754 (not (org-entry-is-done-p))))
14756 (defun org-get-wdays (ts)
14757 "Get the deadline lead time appropriate for timestring TS."
14759 ((<= org-deadline-warning-days 0)
14760 ;; 0 or negative, enforce this value no matter what
14761 (- org-deadline-warning-days))
14762 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
14763 ;; lead time is specified.
14764 (floor (* (string-to-number (match-string 1 ts))
14765 (cdr (assoc (match-string 2 ts)
14766 '(("d" . 1) ("w" . 7)
14767 ("m" . 30.4) ("y" . 365.25)))))))
14768 ;; go for the default.
14769 (t org-deadline-warning-days)))
14771 (defun org-calendar-select-mouse (ev)
14772 "Return to `org-read-date' with the date currently selected.
14773 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
14775 (mouse-set-point ev)
14776 (when (calendar-cursor-to-date)
14777 (let* ((date (calendar-cursor-to-date))
14778 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
14779 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
14780 (if (active-minibuffer-window) (exit-minibuffer))))
14782 (defun org-check-deadlines (ndays)
14783 "Check if there are any deadlines due or past due.
14784 A deadline is considered due if it happens within `org-deadline-warning-days'
14785 days from today's date. If the deadline appears in an entry marked DONE,
14786 it is not shown. The prefix arg NDAYS can be used to test that many
14787 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
14789 (let* ((org-warn-days
14791 ((equal ndays '(4)) 100000)
14792 (ndays (prefix-numeric-value ndays))
14793 (t (abs org-deadline-warning-days))))
14794 (case-fold-search nil)
14795 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
14797 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
14799 (message "%d deadlines past-due or due within %d days"
14800 (org-occur regexp nil callback)
14803 (defun org-check-before-date (date)
14804 "Check if there are deadlines or scheduled entries before DATE."
14805 (interactive (list (org-read-date)))
14806 (let ((case-fold-search nil)
14807 (regexp (concat "\\<\\(" org-deadline-string
14808 "\\|" org-scheduled-string
14809 "\\) *<\\([^>]+\\)>"))
14811 (lambda () (time-less-p
14812 (org-time-string-to-time (match-string 2))
14813 (org-time-string-to-time date)))))
14814 (message "%d entries before %s"
14815 (org-occur regexp nil callback) date)))
14817 (defun org-check-after-date (date)
14818 "Check if there are deadlines or scheduled entries after DATE."
14819 (interactive (list (org-read-date)))
14820 (let ((case-fold-search nil)
14821 (regexp (concat "\\<\\(" org-deadline-string
14822 "\\|" org-scheduled-string
14823 "\\) *<\\([^>]+\\)>"))
14827 (org-time-string-to-time (match-string 2))
14828 (org-time-string-to-time date))))))
14829 (message "%d entries after %s"
14830 (org-occur regexp nil callback) date)))
14832 (defun org-evaluate-time-range (&optional to-buffer)
14833 "Evaluate a time range by computing the difference between start and end.
14834 Normally the result is just printed in the echo area, but with prefix arg
14835 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
14836 If the time range is actually in a table, the result is inserted into the
14838 For time difference computation, a year is assumed to be exactly 365
14839 days in order to avoid rounding problems."
14842 (org-clock-update-time-maybe)
14844 (unless (org-at-date-range-p t)
14845 (goto-char (point-at-bol))
14846 (re-search-forward org-tr-regexp-both (point-at-eol) t))
14847 (if (not (org-at-date-range-p t))
14848 (error "Not at a time-stamp range, and none found in current line")))
14849 (let* ((ts1 (match-string 1))
14850 (ts2 (match-string 2))
14851 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
14852 (match-end (match-end 0))
14853 (time1 (org-time-string-to-time ts1))
14854 (time2 (org-time-string-to-time ts2))
14855 (t1 (org-float-time time1))
14856 (t2 (org-float-time time2))
14857 (diff (abs (- t2 t1)))
14858 (negative (< (- t2 t1) 0))
14859 ;; (ys (floor (* 365 24 60 60)))
14862 (fy "%dy %dd %02d:%02d")
14864 (fd "%dd %02d:%02d")
14869 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
14871 d (floor (/ diff ds)) diff (mod diff ds)
14872 h (floor (/ diff hs)) diff (mod diff hs)
14873 m (floor (/ diff 60)))
14874 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
14876 d (floor (+ (/ diff ds) 0.5))
14878 (if (not to-buffer)
14879 (message "%s" (org-make-tdiff-string y d h m))
14880 (if (org-at-table-p)
14882 (goto-char match-end)
14884 (and (looking-at " *|") (goto-char (match-end 0))))
14885 (goto-char match-end))
14887 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
14888 (replace-match ""))
14889 (if negative (insert " -"))
14890 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
14891 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
14892 (insert " " (format fh h m))))
14893 (if align (org-table-align))
14894 (message "Time difference inserted")))))
14896 (defun org-make-tdiff-string (y d h m)
14899 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
14901 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
14903 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
14905 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
14907 (apply 'format fmt (nreverse l))))
14909 (defun org-time-string-to-time (s)
14910 (apply 'encode-time (org-parse-time-string s)))
14911 (defun org-time-string-to-seconds (s)
14912 (org-float-time (org-time-string-to-time s)))
14914 (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
14915 "Convert a time stamp to an absolute day number.
14916 If there is a specifier for a cyclic time stamp, get the closest date to
14918 PREFER and SHOW-ALL are passed through to `org-closest-date'.
14919 the variable date is bound by the calendar when this is called."
14921 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
14922 (if (org-diary-sexp-entry (match-string 1 s) "" date)
14925 ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
14926 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
14927 (time-to-days (current-time))) (match-string 0 s)
14929 (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
14931 (defun org-days-to-iso-week (days)
14932 "Return the iso week number."
14934 (car (calendar-iso-from-absolute days)))
14936 (defun org-small-year-to-year (year)
14937 "Convert 2-digit years into 4-digit years.
14938 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
14939 The year 2000 cannot be abbreviated. Any year larger than 99
14940 is returned unchanged."
14942 (setq year (+ 2000 year))
14944 (setq year (+ 1900 year))))
14947 (defun org-time-from-absolute (d)
14948 "Return the time corresponding to date D.
14949 D may be an absolute day number, or a calendar-type list (month day year)."
14950 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
14951 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
14953 (defun org-calendar-holiday ()
14954 "List of holidays, for Diary display in Org-mode."
14955 (require 'holidays)
14957 (if (fboundp 'calendar-check-holidays)
14958 'calendar-check-holidays 'check-calendar-holidays) date)))
14959 (if hl (mapconcat 'identity hl "; "))))
14961 (defun org-diary-sexp-entry (sexp entry date)
14962 "Process a SEXP diary ENTRY for DATE."
14963 (require 'diary-lib)
14964 (let ((result (if calendar-debug-sexp
14965 (let ((stack-trace-on-error t))
14966 (eval (car (read-from-string sexp))))
14967 (condition-case nil
14968 (eval (car (read-from-string sexp)))
14971 (message "Bad sexp at line %d in %s: %s"
14973 (buffer-file-name) sexp)
14975 (cond ((stringp result) result)
14976 ((and (consp result)
14977 (stringp (cdr result))) (cdr result))
14981 (defun org-diary-to-ical-string (frombuf)
14982 "Get iCalendar entries from diary entries in buffer FROMBUF.
14983 This uses the icalendar.el library."
14984 (let* ((tmpdir (if (featurep 'xemacs)
14986 temporary-file-directory))
14987 (tmpfile (make-temp-name
14988 (expand-file-name "orgics" tmpdir)))
14990 (with-current-buffer frombuf
14991 (icalendar-export-region (point-min) (point-max) tmpfile)
14992 (setq buf (find-buffer-visiting tmpfile))
14994 (goto-char (point-min))
14995 (if (re-search-forward "^BEGIN:VEVENT" nil t)
14996 (setq b (match-beginning 0)))
14997 (goto-char (point-max))
14998 (if (re-search-backward "^END:VEVENT" nil t)
14999 (setq e (match-end 0)))
15000 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
15002 (delete-file tmpfile)
15005 (defun org-closest-date (start current change prefer show-all)
15006 "Find the date closest to CURRENT that is consistent with START and CHANGE.
15007 When PREFER is `past' return a date that is either CURRENT or past.
15008 When PREFER is `future', return a date that is either CURRENT or future.
15009 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
15010 ;; Make the proper lists from the dates
15012 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
15013 dn dw sday cday n1 n2 n0
15014 d m y y1 y2 date1 date2 nmonths nm ny m2)
15016 (setq start (org-date-to-gregorian start)
15017 current (org-date-to-gregorian
15020 (time-to-days (current-time))))
15021 sday (calendar-absolute-from-gregorian start)
15022 cday (calendar-absolute-from-gregorian current))
15024 (if (<= cday sday) (throw 'exit sday))
15026 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
15027 (setq dn (string-to-number (match-string 1 change))
15028 dw (cdr (assoc (match-string 2 change) a1)))
15029 (error "Invalid change specifier: %s" change))
15030 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
15033 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
15036 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
15037 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
15038 (setq date1 (list m d y1)
15039 n1 (calendar-absolute-from-gregorian date1)
15040 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
15041 n2 (calendar-absolute-from-gregorian date2)))
15043 ;; approx number of month between the two dates
15044 (setq nmonths (floor (/ (- cday sday) 30.436875)))
15045 ;; How often does dn fit in there?
15046 (setq d (nth 1 start) m (car start) y (nth 2 start)
15047 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
15049 ny (floor (/ m 12))
15052 (while (> m 12) (setq m (- m 12) y (1+ y)))
15053 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
15054 (setq m2 (+ m dn) y2 y)
15055 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
15056 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
15057 (while (<= n2 cday)
15058 (setq n1 n2 m m2 y y2)
15059 (setq m2 (+ m dn) y2 y)
15060 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
15061 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
15062 ;; Make sure n1 is the earlier date
15063 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
15066 ((eq prefer 'past) (if (= cday n2) n2 n1))
15067 ((eq prefer 'future) (if (= cday n1) n1 n2))
15068 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
15070 ((eq prefer 'past) (if (= cday n2) n2 n1))
15071 ((eq prefer 'future) (if (= cday n1) n1 n2))
15072 (t (if (= cday n1) n1 n2)))))))
15074 (defun org-date-to-gregorian (date)
15075 "Turn any specification of DATE into a Gregorian date for the calendar."
15076 (cond ((integerp date) (calendar-gregorian-from-absolute date))
15077 ((and (listp date) (= (length date) 3)) date)
15079 (setq date (org-parse-time-string date))
15080 (list (nth 4 date) (nth 3 date) (nth 5 date)))
15082 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
15084 (defun org-parse-time-string (s &optional nodefault)
15085 "Parse the standard Org-mode time string.
15086 This should be a lot faster than the normal `parse-time-string'.
15087 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
15088 hour and minute fields will be nil if not given."
15089 (if (string-match org-ts-regexp0 s)
15091 (if (or (match-beginning 8) (not nodefault))
15092 (string-to-number (or (match-string 8 s) "0")))
15093 (if (or (match-beginning 7) (not nodefault))
15094 (string-to-number (or (match-string 7 s) "0")))
15095 (string-to-number (match-string 4 s))
15096 (string-to-number (match-string 3 s))
15097 (string-to-number (match-string 2 s))
15099 (error "Not a standard Org-mode time string: %s" s)))
15101 (defun org-timestamp-up (&optional arg)
15102 "Increase the date item at the cursor by one.
15103 If the cursor is on the year, change the year. If it is on the month or
15104 the day, change that.
15105 With prefix ARG, change by that many units."
15107 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
15109 (defun org-timestamp-down (&optional arg)
15110 "Decrease the date item at the cursor by one.
15111 If the cursor is on the year, change the year. If it is on the month or
15112 the day, change that.
15113 With prefix ARG, change by that many units."
15115 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
15117 (defun org-timestamp-up-day (&optional arg)
15118 "Increase the date in the time stamp by one day.
15119 With prefix ARG, change that many days."
15121 (if (and (not (org-at-timestamp-p t))
15122 (org-on-heading-p))
15124 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
15126 (defun org-timestamp-down-day (&optional arg)
15127 "Decrease the date in the time stamp by one day.
15128 With prefix ARG, change that many days."
15130 (if (and (not (org-at-timestamp-p t))
15131 (org-on-heading-p))
15133 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
15135 (defun org-at-timestamp-p (&optional inactive-ok)
15136 "Determine if the cursor is in or at a timestamp."
15138 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
15140 (ans (or (looking-at tsr)
15142 (skip-chars-backward "^[<\n\r\t")
15143 (if (> (point) (point-min)) (backward-char 1))
15144 (and (looking-at tsr)
15145 (> (- (match-end 0) pos) -1))))))
15147 (boundp 'org-ts-what)
15150 ((= pos (match-beginning 0)) 'bracket)
15151 ((= pos (1- (match-end 0))) 'bracket)
15152 ((org-pos-in-match-range pos 2) 'year)
15153 ((org-pos-in-match-range pos 3) 'month)
15154 ((org-pos-in-match-range pos 7) 'hour)
15155 ((org-pos-in-match-range pos 8) 'minute)
15156 ((or (org-pos-in-match-range pos 4)
15157 (org-pos-in-match-range pos 5)) 'day)
15158 ((and (> pos (or (match-end 8) (match-end 5)))
15159 (< pos (match-end 0)))
15160 (- pos (or (match-end 8) (match-end 5))))
15164 (defun org-toggle-timestamp-type ()
15165 "Toggle the type (<active> or [inactive]) of a time stamp."
15167 (when (org-at-timestamp-p t)
15168 (let ((beg (match-beginning 0)) (end (match-end 0))
15169 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
15172 (while (re-search-forward "[][<>]" end t)
15173 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
15175 (message "Timestamp is now %sactive"
15176 (if (equal (char-after beg) ?<) "" "in")))))
15178 (defun org-timestamp-change (n &optional what updown)
15179 "Change the date in the time stamp at point.
15180 The date will be changed by N times WHAT. WHAT can be `day', `month',
15181 `year', `minute', `second'. If WHAT is not given, the cursor position
15182 in the timestamp determines what will be changed."
15183 (let ((pos (point))
15185 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
15189 (if (not (org-at-timestamp-p t))
15190 (error "Not at a timestamp"))
15191 (if (and (not what) (eq org-ts-what 'bracket))
15192 (org-toggle-timestamp-type)
15193 (if (and (not what) (not (eq org-ts-what 'day))
15194 org-display-custom-times
15195 (get-text-property (point) 'display)
15196 (not (get-text-property (1- (point)) 'display)))
15197 (setq org-ts-what 'day))
15198 (setq org-ts-what (or what org-ts-what)
15199 inactive (= (char-after (match-beginning 0)) ?\[)
15200 ts (match-string 0))
15203 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
15205 (setq extra (match-string 1 ts)))
15206 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
15208 (setq time0 (org-parse-time-string ts))
15210 (eq org-ts-what 'minute)
15211 (not current-prefix-arg))
15212 ;; This looks like s-up and s-down. Change by one rounding step.
15213 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
15214 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
15215 (setcar (cdr time0) (+ (nth 1 time0)
15216 (if (> n 0) (- rem) (- dm rem))))))
15218 (encode-time (or (car time0) 0)
15219 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
15220 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
15221 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
15222 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
15223 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
15225 (when (and (member org-ts-what '(hour minute))
15227 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
15228 (setq extra (org-modify-ts-extra
15230 (if (eq org-ts-what 'hour) 2 5)
15232 (when (integerp org-ts-what)
15233 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
15234 (if (eq what 'calendar)
15235 (let ((cal-date (org-get-date-from-calendar)))
15236 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
15237 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
15238 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
15239 (setcar time0 (or (car time0) 0))
15240 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
15241 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
15242 (setq time (apply 'encode-time time0))))
15243 (setq org-last-changed-timestamp
15244 (org-insert-time-stamp time with-hm inactive nil nil extra))
15245 (org-clock-update-time-maybe)
15247 ;; Try to recenter the calendar window, if any
15248 (if (and org-calendar-follow-timestamp-change
15249 (get-buffer-window "*Calendar*" t)
15250 (memq org-ts-what '(day month year)))
15251 (org-recenter-calendar (time-to-days time))))))
15253 (defun org-modify-ts-extra (s pos n dm)
15254 "Change the different parts of the lead-time and repeat fields in timestamp."
15255 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
15257 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
15259 ((or (org-pos-in-match-range pos 2)
15260 (org-pos-in-match-range pos 3))
15261 (setq m (string-to-number (match-string 3 s))
15262 h (string-to-number (match-string 2 s)))
15263 (if (org-pos-in-match-range pos 2)
15265 (setq n (* dm (org-no-warnings (signum n))))
15266 (when (not (= 0 (setq rem (% m dm))))
15267 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
15269 (if (< m 0) (setq m (+ m 60) h (1- h)))
15270 (if (> m 59) (setq m (- m 60) h (1+ h)))
15271 (setq h (min 24 (max 0 h)))
15272 (setq ng 1 new (format "-%02d:%02d" h m)))
15273 ((org-pos-in-match-range pos 6)
15274 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
15275 ((org-pos-in-match-range pos 5)
15276 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
15278 ((org-pos-in-match-range pos 9)
15279 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
15280 ((org-pos-in-match-range pos 8)
15281 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
15285 (substring s 0 (match-beginning ng))
15287 (substring s (match-end ng))))))
15290 (defun org-recenter-calendar (date)
15291 "If the calendar is visible, recenter it to DATE."
15292 (let* ((win (selected-window))
15293 (cwin (get-buffer-window "*Calendar*" t))
15294 (calendar-move-hook nil))
15296 (select-window cwin)
15297 (calendar-goto-date (if (listp date) date
15298 (calendar-gregorian-from-absolute date)))
15299 (select-window win))))
15301 (defun org-goto-calendar (&optional arg)
15302 "Go to the Emacs calendar at the current date.
15303 If there is a time stamp in the current line, go to that date.
15304 A prefix ARG can be used to force the current date."
15306 (let ((tsr org-ts-regexp) diff
15307 (calendar-move-hook nil)
15308 (calendar-view-holidays-initially-flag nil)
15309 (calendar-view-diary-initially-flag nil))
15310 (if (or (org-at-timestamp-p)
15312 (beginning-of-line 1)
15313 (looking-at (concat ".*" tsr))))
15314 (let ((d1 (time-to-days (current-time)))
15316 (org-time-string-to-time (match-string 1)))))
15317 (setq diff (- d2 d1))))
15319 (calendar-goto-today)
15320 (if (and diff (not arg)) (calendar-forward-day diff))))
15322 (defun org-get-date-from-calendar ()
15323 "Return a list (month day year) of date at point in calendar."
15324 (with-current-buffer "*Calendar*"
15326 (calendar-cursor-to-date))))
15328 (defun org-date-from-calendar ()
15329 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
15330 If there is already a time stamp at the cursor position, update it."
15332 (if (org-at-timestamp-p t)
15333 (org-timestamp-change 0 'calendar)
15334 (let ((cal-date (org-get-date-from-calendar)))
15335 (org-insert-time-stamp
15336 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
15338 (defun org-minutes-to-hh:mm-string (m)
15339 "Compute H:MM from a number of minutes."
15340 (let ((h (/ m 60)))
15341 (setq m (- m (* 60 h)))
15342 (format org-time-clocksum-format h m)))
15344 (defun org-hh:mm-string-to-minutes (s)
15345 "Convert a string H:MM to a number of minutes.
15346 If the string is just a number, interpret it as minutes.
15347 In fact, the first hh:mm or number in the string will be taken,
15348 there can be extra stuff in the string.
15349 If no number is found, the return value is 0."
15351 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
15352 (+ (* (string-to-number (match-string 1 s)) 60)
15353 (string-to-number (match-string 2 s))))
15354 ((string-match "\\([0-9]+\\)" s)
15355 (string-to-number (match-string 1 s)))
15360 (defun org-save-all-org-buffers ()
15361 "Save all Org-mode buffers without user confirmation."
15363 (message "Saving all Org-mode buffers...")
15364 (save-some-buffers t 'org-mode-p)
15365 (when (featurep 'org-id) (org-id-locations-save))
15366 (message "Saving all Org-mode buffers... done"))
15368 (defun org-revert-all-org-buffers ()
15369 "Revert all Org-mode buffers.
15370 Prompt for confirmation when there are unsaved changes.
15371 Be sure you know what you are doing before letting this function
15372 overwrite your changes.
15374 This function is useful in a setup where one tracks org files
15375 with a version control system, to revert on one machine after pulling
15376 changes from another. I believe the procedure must be like this:
15378 1. M-x org-save-all-org-buffers
15379 2. Pull changes from the other machine, resolve conflicts
15380 3. M-x org-revert-all-org-buffers"
15382 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
15385 (save-window-excursion
15388 (when (and (with-current-buffer b (org-mode-p))
15389 (with-current-buffer b buffer-file-name))
15390 (switch-to-buffer b)
15391 (revert-buffer t 'no-confirm)))
15393 (when (and (featurep 'org-id) org-id-track-globally)
15394 (org-id-locations-load)))))
15399 (defun org-switchb (&optional arg)
15400 "Switch between Org buffers.
15401 With a prefix argument, restrict available to files.
15402 With two prefix arguments, restrict available buffers to agenda files.
15404 Defaults to `iswitchb' for buffer name completion.
15405 Set `org-completion-use-ido' to make it use ido instead."
15407 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
15408 ((equal arg '(16)) (org-buffer-list 'agenda))
15409 (t (org-buffer-list))))
15410 (org-completion-use-iswitchb org-completion-use-iswitchb)
15411 (org-completion-use-ido org-completion-use-ido))
15412 (unless (or org-completion-use-ido org-completion-use-iswitchb)
15413 (setq org-completion-use-iswitchb t))
15415 (org-icompleting-read "Org buffer: "
15416 (mapcar 'list (mapcar 'buffer-name blist))
15419 ;;; Define some older names previously used for this functionality
15421 (defalias 'org-ido-switchb 'org-switchb)
15423 (defalias 'org-iswitchb 'org-switchb)
15425 (defun org-buffer-list (&optional predicate exclude-tmp)
15426 "Return a list of Org buffers.
15427 PREDICATE can be `export', `files' or `agenda'.
15429 export restrict the list to Export buffers.
15430 files restrict the list to buffers visiting Org files.
15431 agenda restrict the list to buffers visiting agenda files.
15433 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
15435 (agenda-files (and (eq predicate 'agenda)
15436 (mapcar 'file-truename (org-agenda-files t))))
15439 ((eq predicate 'files)
15440 (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
15441 ((eq predicate 'export)
15442 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
15443 ((eq predicate 'agenda)
15445 (with-current-buffer b
15446 (and (eq major-mode 'org-mode)
15447 (setq bfn (buffer-file-name b))
15448 (member (file-truename bfn) agenda-files)))))
15449 (t (lambda (b) (with-current-buffer b
15450 (or (eq major-mode 'org-mode)
15451 (string-match "\*Org .*Export"
15452 (buffer-name b)))))))))
15456 (if (and (funcall filter b)
15457 (or (not exclude-tmp)
15458 (not (string-match "tmp" (buffer-name b)))))
15463 (defun org-agenda-files (&optional unrestricted archives)
15464 "Get the list of agenda files.
15465 Optional UNRESTRICTED means return the full list even if a restriction
15466 is currently in place.
15467 When ARCHIVES is t, include all archive files that are really being
15468 used by the agenda files. If ARCHIVE is `ifmode', do this only if
15469 `org-agenda-archives-mode' is t."
15472 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
15473 ((stringp org-agenda-files) (org-read-agenda-file-list))
15474 ((listp org-agenda-files) org-agenda-files)
15475 (t (error "Invalid value of `org-agenda-files'")))))
15476 (setq files (apply 'append
15477 (mapcar (lambda (f)
15478 (if (file-directory-p f)
15480 f t org-agenda-file-regexp)
15483 (when org-agenda-skip-unavailable-files
15484 (setq files (delq nil
15487 (and (file-readable-p file) file)))
15489 (when (or (eq archives t)
15490 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
15491 (setq files (org-add-archive-files files)))
15494 (defun org-agenda-file-p (&optional file)
15495 "Return non-nil, if FILE is an agenda file.
15496 If FILE is omitted, use the file associated with the current
15498 (member (or file (buffer-file-name))
15499 (org-agenda-files t)))
15501 (defun org-edit-agenda-file-list ()
15502 "Edit the list of agenda files.
15503 Depending on setup, this either uses customize to edit the variable
15504 `org-agenda-files', or it visits the file that is holding the list. In the
15505 latter case, the buffer is set up in a way that saving it automatically kills
15506 the buffer and restores the previous window configuration."
15508 (if (stringp org-agenda-files)
15509 (let ((cw (current-window-configuration)))
15510 (find-file org-agenda-files)
15511 (org-set-local 'org-window-configuration cw)
15512 (org-add-hook 'after-save-hook
15514 (set-window-configuration
15515 (prog1 org-window-configuration
15516 (kill-buffer (current-buffer))))
15517 (org-install-agenda-files-menu)
15518 (message "New agenda file list installed"))
15520 (message "%s" (substitute-command-keys
15521 "Edit list and finish with \\[save-buffer]")))
15522 (customize-variable 'org-agenda-files)))
15524 (defun org-store-new-agenda-file-list (list)
15525 "Set new value for the agenda file list and save it correctly."
15526 (if (stringp org-agenda-files)
15527 (let ((fe (org-read-agenda-file-list t)) b u)
15528 (while (setq b (find-buffer-visiting org-agenda-files))
15530 (with-temp-file org-agenda-files
15533 (lambda (f) ;; Keep un-expanded entries.
15534 (if (setq u (assoc f fe))
15539 (let ((org-mode-hook nil) (org-inhibit-startup t)
15540 (org-insert-mode-line-in-empty-file nil))
15541 (setq org-agenda-files list)
15542 (customize-save-variable 'org-agenda-files org-agenda-files))))
15544 (defun org-read-agenda-file-list (&optional pair-with-expansion)
15545 "Read the list of agenda files from a file.
15546 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
15547 filenames, used by `org-store-new-agenda-file-list' to write back
15548 un-expanded file names."
15549 (when (file-directory-p org-agenda-files)
15550 (error "`org-agenda-files' cannot be a single directory"))
15551 (when (stringp org-agenda-files)
15553 (insert-file-contents org-agenda-files)
15556 (let ((e (expand-file-name (substitute-in-file-name f)
15558 (if pair-with-expansion
15561 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
15564 (defun org-cycle-agenda-files ()
15565 "Cycle through the files in `org-agenda-files'.
15566 If the current buffer visits an agenda file, find the next one in the list.
15567 If the current buffer does not, find the first agenda file."
15569 (let* ((fs (org-agenda-files t))
15570 (files (append fs (list (car fs))))
15571 (tcf (if buffer-file-name (file-truename buffer-file-name)))
15573 (unless files (error "No agenda files"))
15575 (while (setq file (pop files))
15576 (if (equal (file-truename file) tcf)
15578 (find-file (car files))
15580 (find-file (car fs)))
15581 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
15583 (defun org-agenda-file-to-front (&optional to-end)
15584 "Move/add the current file to the top of the agenda file list.
15585 If the file is not present in the list, it is added to the front. If it is
15586 present, it is moved there. With optional argument TO-END, add/move to the
15589 (let ((org-agenda-skip-unavailable-files nil)
15590 (file-alist (mapcar (lambda (x)
15591 (cons (file-truename x) x))
15592 (org-agenda-files t)))
15593 (ctf (file-truename buffer-file-name))
15595 (setq x (assoc ctf file-alist) had x)
15597 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
15599 (setq file-alist (append (delq x file-alist) (list x)))
15600 (setq file-alist (cons x (delq x file-alist))))
15601 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
15602 (org-install-agenda-files-menu)
15603 (message "File %s to %s of agenda file list"
15604 (if had "moved" "added") (if to-end "end" "front"))))
15606 (defun org-remove-file (&optional file)
15607 "Remove current file from the list of files in variable `org-agenda-files'.
15608 These are the files which are being checked for agenda entries.
15609 Optional argument FILE means use this file instead of the current."
15611 (let* ((org-agenda-skip-unavailable-files nil)
15612 (file (or file buffer-file-name))
15613 (true-file (file-truename file))
15614 (afile (abbreviate-file-name file))
15615 (files (delq nil (mapcar
15617 (if (equal true-file
15620 (org-agenda-files t)))))
15621 (if (not (= (length files) (length (org-agenda-files t))))
15623 (org-store-new-agenda-file-list files)
15624 (org-install-agenda-files-menu)
15625 (message "Removed file: %s" afile))
15626 (message "File was not in list: %s (not removed)" afile))))
15628 (defun org-file-menu-entry (file)
15629 (vector file (list 'find-file file) t))
15631 (defun org-check-agenda-file (file)
15632 "Make sure FILE exists. If not, ask user what to do."
15633 (when (not (file-exists-p file))
15634 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
15635 (abbreviate-file-name file))
15636 (let ((r (downcase (read-char-exclusive))))
15639 (org-remove-file file)
15640 (throw 'nextfile t))
15641 (t (error "Abort"))))))
15643 (defun org-get-agenda-file-buffer (file)
15644 "Get a buffer visiting FILE. If the buffer needs to be created, add
15645 it to the list of buffers which might be released later."
15646 (let ((buf (org-find-base-buffer-visiting file)))
15648 buf ; just return it
15649 ;; Make a new buffer and remember it
15650 (setq buf (find-file-noselect file))
15651 (if buf (push buf org-agenda-new-buffers))
15654 (defun org-release-buffers (blist)
15655 "Release all buffers in list, asking the user for confirmation when needed.
15656 When a buffer is unmodified, it is just killed. When modified, it is saved
15657 \(if the user agrees) and then killed."
15659 (while (setq buf (pop blist))
15660 (setq file (buffer-file-name buf))
15661 (when (and (buffer-modified-p buf)
15663 (y-or-n-p (format "Save file %s? " file)))
15664 (with-current-buffer buf (save-buffer)))
15665 (kill-buffer buf))))
15667 (defun org-prepare-agenda-buffers (files)
15668 "Create buffers for all agenda files, protect archived trees and comments."
15670 (let ((pa '(:org-archived t))
15671 (pc '(:org-comment t))
15672 (pall '(:org-archived t :org-comment t))
15673 (inhibit-read-only t)
15674 (rea (concat ":" org-archive-tag ":"))
15678 (while (setq file (pop files))
15682 (org-check-agenda-file file)
15683 (set-buffer (org-get-agenda-file-buffer file)))
15685 (setq bmp (buffer-modified-p))
15686 (org-refresh-category-properties)
15687 (setq org-todo-keywords-for-agenda
15688 (append org-todo-keywords-for-agenda org-todo-keywords-1))
15689 (setq org-done-keywords-for-agenda
15690 (append org-done-keywords-for-agenda org-done-keywords))
15691 (setq org-todo-keyword-alist-for-agenda
15692 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
15693 (setq org-drawers-for-agenda
15694 (append org-drawers-for-agenda org-drawers))
15695 (setq org-tag-alist-for-agenda
15696 (append org-tag-alist-for-agenda org-tag-alist))
15699 (remove-text-properties (point-min) (point-max) pall)
15700 (when org-agenda-skip-archived-trees
15701 (goto-char (point-min))
15702 (while (re-search-forward rea nil t)
15703 (if (org-on-heading-p t)
15704 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
15705 (goto-char (point-min))
15706 (setq re (concat "^\\*+ +" org-comment-string "\\>"))
15707 (while (re-search-forward re nil t)
15708 (add-text-properties
15709 (match-beginning 0) (org-end-of-subtree t) pc)))
15710 (set-buffer-modified-p bmp)))))
15711 (setq org-todo-keywords-for-agenda
15712 (org-uniquify org-todo-keywords-for-agenda))
15713 (setq org-todo-keyword-alist-for-agenda
15714 (org-uniquify org-todo-keyword-alist-for-agenda)
15715 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
15717 ;;;; Embedded LaTeX
15719 (defvar org-cdlatex-mode-map (make-sparse-keymap)
15720 "Keymap for the minor `org-cdlatex-mode'.")
15722 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
15723 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
15724 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
15725 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
15726 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
15728 (defvar org-cdlatex-texmathp-advice-is-done nil
15729 "Flag remembering if we have applied the advice to texmathp already.")
15731 (define-minor-mode org-cdlatex-mode
15732 "Toggle the minor `org-cdlatex-mode'.
15733 This mode supports entering LaTeX environment and math in LaTeX fragments
15735 \\{org-cdlatex-mode-map}"
15737 (when org-cdlatex-mode (require 'cdlatex))
15738 (unless org-cdlatex-texmathp-advice-is-done
15739 (setq org-cdlatex-texmathp-advice-is-done t)
15740 (defadvice texmathp (around org-math-always-on activate)
15741 "Always return t in org-mode buffers.
15742 This is because we want to insert math symbols without dollars even outside
15743 the LaTeX math segments. If Orgmode thinks that point is actually inside
15744 an embedded LaTeX fragment, let texmathp do its job.
15745 \\[org-cdlatex-mode-map]"
15749 ((not (org-mode-p)) ad-do-it)
15750 ((eq this-command 'cdlatex-math-symbol)
15751 (setq ad-return-value t
15752 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
15754 (let ((p (org-inside-LaTeX-fragment-p)))
15755 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
15756 (setq ad-return-value t
15757 texmathp-why '("Org-mode embedded math" . 0))
15758 (if p ad-do-it)))))))))
15760 (defun turn-on-org-cdlatex ()
15761 "Unconditionally turn on `org-cdlatex-mode'."
15762 (org-cdlatex-mode 1))
15764 (defun org-inside-LaTeX-fragment-p ()
15765 "Test if point is inside a LaTeX fragment.
15766 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
15767 sequence appearing also before point.
15768 Even though the matchers for math are configurable, this function assumes
15769 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
15770 delimiters are skipped when they have been removed by customization.
15771 The return value is nil, or a cons cell with the delimiter and
15772 and the position of this delimiter.
15774 This function does a reasonably good job, but can locally be fooled by
15775 for example currency specifications. For example it will assume being in
15776 inline math after \"$22.34\". The LaTeX fragment formatter will only format
15777 fragments that are properly closed, but during editing, we have to live
15778 with the uncertainty caused by missing closing delimiters. This function
15779 looks only before point, not after."
15781 (let ((pos (point))
15782 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
15784 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
15786 dd-on str (start 0) m re)
15789 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
15790 re (nth 1 (assoc "$" org-latex-regexps)))
15791 (while (string-match re str start)
15793 ((= (match-end 0) (length str))
15794 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
15795 ((= (match-end 0) (- (length str) 5))
15797 (t (setq start (match-end 0))))))
15798 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
15800 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
15801 (and (match-beginning 2) (throw 'exit nil))
15803 (while (re-search-backward "\\$\\$" lim t)
15804 (setq dd-on (not dd-on)))
15806 (if dd-on (cons "$$" m))))))
15808 (defun org-inside-latex-macro-p ()
15809 "Is point inside a LaTeX macro or its arguments?"
15812 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
15814 (defun org-try-cdlatex-tab ()
15815 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
15816 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
15817 - inside a LaTeX fragment, or
15818 - after the first word in a line, where an abbreviation expansion could
15819 insert a LaTeX environment."
15820 (when org-cdlatex-mode
15823 (skip-chars-backward "a-zA-Z0-9*")
15824 (skip-chars-backward " \t")
15827 ((org-inside-LaTeX-fragment-p)
15831 (defun org-cdlatex-underscore-caret (&optional arg)
15832 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
15833 Revert to the normal definition outside of these fragments."
15835 (if (org-inside-LaTeX-fragment-p)
15836 (call-interactively 'cdlatex-sub-superscript)
15837 (let (org-cdlatex-mode)
15838 (call-interactively (key-binding (vector last-input-event))))))
15840 (defun org-cdlatex-math-modify (&optional arg)
15841 "Execute `cdlatex-math-modify' in LaTeX fragments.
15842 Revert to the normal definition outside of these fragments."
15844 (if (org-inside-LaTeX-fragment-p)
15845 (call-interactively 'cdlatex-math-modify)
15846 (let (org-cdlatex-mode)
15847 (call-interactively (key-binding (vector last-input-event))))))
15849 (defvar org-latex-fragment-image-overlays nil
15850 "List of overlays carrying the images of latex fragments.")
15851 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
15853 (defun org-remove-latex-fragment-image-overlays ()
15854 "Remove all overlays with LaTeX fragment images in current buffer."
15855 (mapc 'delete-overlay org-latex-fragment-image-overlays)
15856 (setq org-latex-fragment-image-overlays nil))
15858 (defun org-preview-latex-fragment (&optional subtree)
15859 "Preview the LaTeX fragment at point, or all locally or globally.
15860 If the cursor is in a LaTeX fragment, create the image and overlay
15861 it over the source code. If there is no fragment at point, display
15862 all fragments in the current text, from one headline to the next. With
15863 prefix SUBTREE, display all fragments in the current subtree. With a
15864 double prefix arg \\[universal-argument] \\[universal-argument], or when \
15865 the cursor is before the first headline,
15866 display all fragments in the buffer.
15867 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
15869 (org-remove-latex-fragment-image-overlays)
15872 (let (beg end at msg)
15874 ((or (equal subtree '(16))
15875 (not (save-excursion
15876 (re-search-backward (concat "^" outline-regexp) nil t))))
15877 (setq beg (point-min) end (point-max)
15878 msg "Creating images for buffer...%s"))
15879 ((equal subtree '(4))
15880 (org-back-to-heading)
15881 (setq beg (point) end (org-end-of-subtree t)
15882 msg "Creating images for subtree...%s"))
15884 (if (setq at (org-inside-LaTeX-fragment-p))
15885 (goto-char (max (point-min) (- (cdr at) 2)))
15886 (org-back-to-heading))
15887 (setq beg (point) end (progn (outline-next-heading) (point))
15888 msg (if at "Creating image...%s"
15889 "Creating images for entry...%s"))))
15891 (narrow-to-region beg end)
15894 (concat "ltxpng/" (file-name-sans-extension
15895 (file-name-nondirectory
15896 buffer-file-name)))
15897 default-directory 'overlays msg at 'forbuffer 'dvipng)
15898 (message msg "done. Use `C-c C-c' to remove images.")))))
15900 (defvar org-latex-regexps
15901 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
15902 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
15903 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
15904 ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
15905 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
15906 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
15907 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
15908 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
15909 "Regular expressions for matching embedded LaTeX.")
15911 (defvar org-export-have-math nil) ;; dynamic scoping
15912 (defun org-format-latex (prefix &optional dir overlays msg at
15913 forbuffer processing-type)
15914 "Replace LaTeX fragments with links to an image, and produce images.
15915 Some of the options can be changed using the variable
15916 `org-format-latex-options'."
15917 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
15918 (let* ((prefixnodir (file-name-nondirectory prefix))
15919 (absprefix (expand-file-name prefix dir))
15920 (todir (file-name-directory absprefix))
15921 (opt org-format-latex-options)
15922 (matchers (plist-get opt :matchers))
15923 (re-list org-latex-regexps)
15924 (org-format-latex-header-extra
15925 (plist-get (org-infile-export-plist) :latex-header-extra))
15926 (cnt 0) txt hash link beg end re e checkdir
15927 executables-checked string
15928 m n block linkfile movefile ov)
15929 ;; Check the different regular expressions
15930 (while (setq e (pop re-list))
15931 (setq m (car e) re (nth 1 e) n (nth 2 e)
15932 block (if (nth 3 e) "\n\n" ""))
15933 (when (member m matchers)
15934 (goto-char (point-min))
15935 (while (re-search-forward re nil t)
15936 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
15937 (not (get-text-property (match-beginning n)
15940 (not (eq (get-char-property (match-beginning n)
15942 'org-latex-overlay))))
15943 (setq org-export-have-math t)
15945 ((eq processing-type 'verbatim)
15946 ;; Leave the text verbatim, just protect it
15947 (add-text-properties (match-beginning n) (match-end n)
15948 '(org-protected t)))
15949 ((eq processing-type 'mathjax)
15950 ;; Prepare for MathJax processing
15951 (setq string (match-string n))
15954 (delete-region (match-beginning n) (match-end n))
15955 (goto-char (match-beginning n))
15956 (insert (org-add-props (concat "\\(" (substring string 1 -1)
15958 '(org-protected t))))
15959 (add-text-properties (match-beginning n) (match-end n)
15960 '(org-protected t))))
15961 ((or (eq processing-type 'dvipng) t)
15962 ;; Process to an image
15963 (setq txt (match-string n)
15964 beg (match-beginning n) end (match-end n)
15966 (let (print-length print-level) ; make sure full list is printed
15967 (setq hash (sha1 (prin1-to-string
15968 (list org-format-latex-header
15969 org-format-latex-header-extra
15970 org-export-latex-default-packages-alist
15971 org-export-latex-packages-alist
15972 org-format-latex-options
15974 linkfile (format "%s_%s.png" prefix hash)
15975 movefile (format "%s_%s.png" absprefix hash)))
15976 (setq link (concat block "[[file:" linkfile "]]" block))
15977 (if msg (message msg cnt))
15979 (unless checkdir ; make sure the directory exists
15981 (or (file-directory-p todir) (make-directory todir t)))
15983 (unless executables-checked
15984 (org-check-external-command
15985 "latex" "needed to convert LaTeX fragments to images")
15986 (org-check-external-command
15987 "dvipng" "needed to convert LaTeX fragments to images")
15988 (setq executables-checked t))
15990 (unless (file-exists-p movefile)
15991 (org-create-formula-image
15992 txt movefile opt forbuffer))
15996 (if (eq (overlay-get o 'org-overlay-type)
15997 'org-latex-overlay)
15998 (delete-overlay o)))
15999 (overlays-in beg end))
16000 (setq ov (make-overlay beg end))
16001 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
16002 (if (featurep 'xemacs)
16004 (overlay-put ov 'invisible t)
16007 (make-glyph (vector 'png :file movefile))))
16010 (list 'image :type 'png :file movefile :ascent 'center)))
16011 (push ov org-latex-fragment-image-overlays)
16013 (delete-region beg end)
16014 (insert (org-add-props link
16015 (list 'org-latex-src
16016 (replace-regexp-in-string
16017 "\"" "" txt)))))))))))))
16019 ;; This function borrows from Ganesh Swami's latex2png.el
16020 (defun org-create-formula-image (string tofile options buffer)
16021 "This calls dvipng."
16022 (require 'org-latex)
16023 (let* ((tmpdir (if (featurep 'xemacs)
16025 temporary-file-directory))
16026 (texfilebase (make-temp-name
16027 (expand-file-name "orgtex" tmpdir)))
16028 (texfile (concat texfilebase ".tex"))
16029 (dvifile (concat texfilebase ".dvi"))
16030 (pngfile (concat texfilebase ".png"))
16031 (fnh (if (featurep 'xemacs)
16032 (font-height (get-face-font 'default))
16033 (face-attribute 'default :height nil)))
16034 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
16035 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
16036 (fg (or (plist-get options (if buffer :foreground :html-foreground))
16038 (bg (or (plist-get options (if buffer :background :html-background))
16040 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
16041 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
16042 (with-temp-file texfile
16043 (insert (org-splice-latex-header
16044 org-format-latex-header
16045 org-export-latex-default-packages-alist
16046 org-export-latex-packages-alist t
16047 org-format-latex-header-extra))
16048 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
16049 (require 'org-latex)
16050 (org-export-latex-fix-inputenc))
16051 (let ((dir default-directory))
16052 (condition-case nil
16055 (call-process "latex" nil nil nil texfile))
16058 (if (not (file-exists-p dvifile))
16059 (progn (message "Failed to create dvi file from %s" texfile) nil)
16060 (condition-case nil
16061 (call-process "dvipng" nil nil nil
16064 ;;"-x" scale "-y" scale
16069 (if (not (file-exists-p pngfile))
16070 (if org-format-latex-signal-error
16071 (error "Failed to create png file from %s" texfile)
16072 (message "Failed to create png file from %s" texfile)
16074 ;; Use the requested file name and clean up
16075 (copy-file pngfile tofile 'replace)
16076 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
16077 (delete-file (concat texfilebase e)))
16080 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
16081 "Fill a LaTeX header template TPL.
16082 In the template, the following place holders will be recognized:
16084 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
16085 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
16086 [PACKAGES] \\usepackage statements for PKG
16087 [NO-PACKAGES] do not include PKG
16088 [EXTRA] the string EXTRA
16089 [NO-EXTRA] do not include EXTRA
16091 For backward compatibility, if both the positive and the negative place
16092 holder is missing, the positive one (without the \"NO-\") will be
16093 assumed to be present at the end of the template.
16094 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
16096 SNIPPETS-P indicates if this is run to create snippet images for HTML."
16097 (let (rpl (end ""))
16098 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
16099 (setq rpl (if (or (match-end 1) (not def-pkg))
16100 "" (org-latex-packages-to-string def-pkg snippets-p t))
16101 tpl (replace-match rpl t t tpl))
16102 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
16104 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
16105 (setq rpl (if (or (match-end 1) (not pkg))
16106 "" (org-latex-packages-to-string pkg snippets-p t))
16107 tpl (replace-match rpl t t tpl))
16110 (org-latex-packages-to-string pkg snippets-p)))))
16112 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
16113 (setq rpl (if (or (match-end 1) (not extra))
16114 "" (concat extra "\n"))
16115 tpl (replace-match rpl t t tpl))
16116 (if (and extra (string-match "\\S-" extra))
16117 (setq end (concat end "\n" extra))))
16119 (if (string-match "\\S-" end)
16120 (concat tpl "\n" end)
16123 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
16124 "Turn an alist of packages into a string with the \\usepackage macros."
16125 (setq pkg (mapconcat (lambda(p)
16128 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
16129 (format "%% Package %s omitted" (cadr p)))
16130 ((equal "" (car p))
16131 (format "\\usepackage{%s}" (cadr p)))
16133 (format "\\usepackage[%s]{%s}"
16134 (car p) (cadr p)))))
16137 (if newline (concat pkg "\n") pkg))
16139 (defun org-dvipng-color (attr)
16140 "Return an rgb color specification for dvipng."
16141 (apply 'format "rgb %s %s %s"
16142 (mapcar 'org-normalize-color
16143 (color-values (face-attribute 'default attr nil)))))
16145 (defun org-normalize-color (value)
16146 "Return string to be used as color value for an RGB component."
16147 (format "%g" (/ value 65535.0)))
16152 (defvar org-inline-image-overlays nil)
16153 (make-variable-buffer-local 'org-inline-image-overlays)
16155 (defun org-toggle-inline-images (&optional include-linked)
16156 "Toggle the display of inline images.
16157 INCLUDE-LINKED is passed to `org-display-inline-images'."
16159 (if org-inline-image-overlays
16161 (org-remove-inline-images)
16162 (message "Inline image display turned off"))
16163 (org-display-inline-images include-linked)
16164 (if org-inline-image-overlays
16165 (message "%d images displayed inline"
16166 (length org-inline-image-overlays))
16167 (message "No images to display inline"))))
16169 (defun org-display-inline-images (&optional include-linked refresh beg end)
16170 "Display inline images.
16171 Normally only links without a description part are inlined, because this
16172 is how it will work for export. When INCLUDE-LINKED is set, also links
16173 with a description part will be inlined. This can be nice for a quick
16174 look at those images, but it does not reflect what exported files will look
16176 When REFRESH is set, refresh existing images between BEG and END.
16177 This will create new image displays only if necessary.
16178 BEG and END default to the buffer boundaries."
16181 (org-remove-inline-images)
16182 (clear-image-cache))
16186 (setq beg (or beg (point-min)) end (or end (point-max)))
16187 (goto-char (point-min))
16188 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([-+~.:/\\_0-9a-zA-Z ]+"
16189 (substring (org-image-file-name-regexp) 0 -2)
16190 "\\)\\]" (if include-linked "" "\\]")))
16192 (while (re-search-forward re end t)
16193 (setq old (get-char-property-and-overlay (match-beginning 1)
16194 'org-image-overlay))
16195 (setq file (expand-file-name
16196 (concat (or (match-string 3) "") (match-string 4))))
16197 (when (file-exists-p file)
16198 (if (and (car-safe old) refresh)
16199 (image-refresh (overlay-get (cdr old) 'display))
16200 (setq img (create-image file))
16202 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
16203 (overlay-put ov 'display img)
16204 (overlay-put ov 'face 'default)
16205 (overlay-put ov 'org-image-overlay t)
16206 (overlay-put ov 'modification-hooks
16207 (list 'org-display-inline-modification-hook))
16208 (push ov org-inline-image-overlays)))))))))
16210 (defun org-display-inline-modification-hook (ov after beg end &optional len)
16211 "Remove inline-display overlay if a corresponding region is modified."
16212 (let ((inhibit-modification-hooks t))
16213 (when (and ov after)
16214 (delete ov org-inline-image-overlays)
16215 (delete-overlay ov))))
16217 (defun org-remove-inline-images ()
16218 "Remove inline display of images."
16220 (mapc 'delete-overlay org-inline-image-overlays)
16221 (setq org-inline-image-overlays nil))
16225 ;; Make `C-c C-x' a prefix key
16226 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
16228 ;; TAB key with modifiers
16229 (org-defkey org-mode-map "\C-i" 'org-cycle)
16230 (org-defkey org-mode-map [(tab)] 'org-cycle)
16231 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
16232 (org-defkey org-mode-map [(meta tab)] 'org-complete)
16233 (org-defkey org-mode-map "\M-\t" 'org-complete)
16234 (org-defkey org-mode-map "\M-\C-i" 'org-complete)
16235 ;; The following line is necessary under Suse GNU/Linux
16236 (unless (featurep 'xemacs)
16237 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
16238 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
16239 (define-key org-mode-map [backtab] 'org-shifttab)
16241 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
16242 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
16243 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
16245 ;; Cursor keys with modifiers
16246 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
16247 (org-defkey org-mode-map [(meta right)] 'org-metaright)
16248 (org-defkey org-mode-map [(meta up)] 'org-metaup)
16249 (org-defkey org-mode-map [(meta down)] 'org-metadown)
16251 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
16252 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
16253 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
16254 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
16256 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
16257 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
16258 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
16259 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
16261 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
16262 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
16265 (define-key org-mode-map org-babel-key-prefix org-babel-map)
16266 (mapc (lambda (pair)
16267 (define-key org-babel-map (car pair) (cdr pair)))
16268 org-babel-key-bindings)
16270 ;;; Extra keys for tty access.
16271 ;; We only set them when really needed because otherwise the
16272 ;; menus don't show the simple keys
16274 (when (or org-use-extra-keys
16275 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
16276 (not window-system))
16277 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
16278 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
16279 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
16280 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
16281 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
16282 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
16283 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
16284 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
16285 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
16286 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
16287 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
16288 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
16289 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
16290 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
16291 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
16292 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
16293 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
16294 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
16295 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
16296 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
16297 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
16298 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
16299 (org-defkey org-mode-map [?\e (tab)] 'org-complete)
16300 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
16301 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
16302 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
16303 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
16304 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
16306 ;; All the other keys
16308 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
16309 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
16310 (if (boundp 'narrow-map)
16311 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
16312 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
16313 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
16314 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
16315 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
16316 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
16317 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
16318 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
16319 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
16320 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
16321 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
16322 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
16323 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
16324 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
16325 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
16326 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
16327 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
16328 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
16329 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
16330 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
16331 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
16332 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
16333 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
16334 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
16335 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
16336 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
16337 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
16338 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
16339 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
16340 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
16341 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
16342 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
16343 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
16344 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
16345 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
16346 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
16347 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
16348 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
16349 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
16350 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
16351 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
16352 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
16353 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
16354 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
16355 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
16356 (org-defkey org-mode-map "\C-c^" 'org-sort)
16357 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
16358 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
16359 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
16360 (org-defkey org-mode-map "\C-m" 'org-return)
16361 (org-defkey org-mode-map "\C-j" 'org-return-indent)
16362 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
16363 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
16364 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
16365 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
16366 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
16367 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
16368 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
16369 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
16370 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
16371 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
16372 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
16373 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
16374 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
16375 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
16376 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
16377 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
16378 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
16379 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
16380 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
16381 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
16383 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
16384 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
16385 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
16386 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
16388 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
16389 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
16390 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
16391 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
16392 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
16393 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
16394 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
16395 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
16396 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
16397 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
16398 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
16399 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
16400 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
16401 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
16402 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
16403 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
16404 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
16405 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
16407 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
16408 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
16409 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
16410 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
16411 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
16413 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
16415 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
16417 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
16418 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
16420 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
16423 (when (featurep 'xemacs)
16424 (org-defkey org-mode-map 'button3 'popup-mode-menu))
16427 (defconst org-speed-commands-default
16429 ("Outline Navigation")
16430 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
16431 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
16432 ("f" . (org-speed-move-safe 'org-forward-same-level))
16433 ("b" . (org-speed-move-safe 'org-backward-same-level))
16434 ("u" . (org-speed-move-safe 'outline-up-heading))
16436 ("g" . (org-refile t))
16437 ("Outline Visibility")
16439 ("C" . org-shifttab)
16440 (" " . org-display-outline-path)
16441 ("Outline Structure Editing")
16442 ("U" . org-shiftmetaup)
16443 ("D" . org-shiftmetadown)
16444 ("r" . org-metaright)
16445 ("l" . org-metaleft)
16446 ("R" . org-shiftmetaright)
16447 ("L" . org-shiftmetaleft)
16448 ("i" . (progn (forward-char 1) (call-interactively
16449 'org-insert-heading-respect-content)))
16452 ("a" . org-archive-subtree-default-with-confirmation)
16453 ("." . outline-mark-subtree)
16455 ("I" . org-clock-in)
16456 ("O" . org-clock-out)
16457 ("Meta Data Editing")
16459 ("0" . (org-priority ?\ ))
16460 ("1" . (org-priority ?A))
16461 ("2" . (org-priority ?B))
16462 ("3" . (org-priority ?C))
16463 (";" . org-set-tags-command)
16464 ("e" . org-set-effort)
16465 ("Agenda Views etc")
16467 ("/" . org-sparse-tree)
16469 ("o" . org-open-at-point)
16470 ("?" . org-speed-command-help)
16471 ("<" . (org-agenda-set-restriction-lock 'subtree))
16472 (">" . (org-agenda-remove-restriction-lock))
16474 "The default speed commands.")
16476 (defun org-print-speed-command (e)
16477 (if (> (length (car e)) 1)
16482 (princ (make-string (length (car e)) ?-))
16486 (if (symbolp (cdr e))
16487 (princ (symbol-name (cdr e)))
16491 (defun org-speed-command-help ()
16492 "Show the available speed commands."
16494 (if (not org-use-speed-commands)
16495 (error "Speed commands are not activated, customize `org-use-speed-commands'")
16496 (with-output-to-temp-buffer "*Help*"
16497 (princ "User-defined Speed commands\n===========================\n")
16498 (mapc 'org-print-speed-command org-speed-commands-user)
16500 (princ "Built-in Speed commands\n=======================\n")
16501 (mapc 'org-print-speed-command org-speed-commands-default))
16502 (with-current-buffer "*Help*"
16503 (setq truncate-lines t))))
16505 (defun org-speed-move-safe (cmd)
16506 "Execute CMD, but make sure that the cursor always ends up in a headline.
16507 If not, return to the original position and throw an error."
16509 (let ((pos (point)))
16510 (call-interactively cmd)
16511 (unless (and (bolp) (org-on-heading-p))
16513 (error "Boundary reached while executing %s" cmd))))
16515 (defvar org-self-insert-command-undo-counter 0)
16517 (defvar org-table-auto-blank-field) ; defined in org-table.el
16518 (defvar org-speed-command nil)
16520 (defun org-speed-command-default-hook (keys)
16521 "Hook for activating single-letter speed commands.
16522 `org-speed-commands-default' specifies a minimal command set. Use
16523 `org-speed-commands-user' for further customization."
16524 (when (or (and (bolp) (looking-at outline-regexp))
16525 (and (functionp org-use-speed-commands)
16526 (funcall org-use-speed-commands)))
16527 (cdr (assoc keys (append org-speed-commands-user
16528 org-speed-commands-default)))))
16530 (defun org-babel-speed-command-hook (keys)
16531 "Hook for activating single-letter code block commands."
16532 (when (and (bolp) (looking-at org-babel-src-block-regexp))
16533 (cdr (assoc keys org-babel-key-bindings))))
16535 (defcustom org-speed-command-hook
16536 '(org-speed-command-default-hook org-babel-speed-command-hook)
16537 "Hook for activating speed commands at strategic locations.
16538 Hook functions are called in sequence until a valid handler is
16541 Each hook takes a single argument, a user-pressed command key
16542 which is also a `self-insert-command' from the global map.
16544 Within the hook, examine the cursor position and the command key
16545 and return nil or a valid handler as appropriate. Handler could
16546 be one of an interactive command, a function, or a form.
16548 Set `org-use-speed-commands' to non-nil value to enable this
16549 hook. The default setting is `org-speed-command-default-hook'."
16550 :group 'org-structure
16553 (defun org-self-insert-command (N)
16554 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
16555 If the cursor is in a table looking at whitespace, the whitespace is
16556 overwritten, and the table is not marked as requiring realignment."
16559 ((and org-use-speed-commands
16560 (setq org-speed-command
16561 (run-hook-with-args-until-success
16562 'org-speed-command-hook (this-command-keys))))
16564 ((commandp org-speed-command)
16565 (setq this-command org-speed-command)
16566 (call-interactively org-speed-command))
16567 ((functionp org-speed-command)
16568 (funcall org-speed-command))
16569 ((and org-speed-command (listp org-speed-command))
16570 (eval org-speed-command))
16571 (t (let (org-use-speed-commands)
16572 (call-interactively 'org-self-insert-command)))))
16576 ;; check if we blank the field, and if that triggers align
16577 (and (featurep 'org-table) org-table-auto-blank-field
16578 (member last-command
16579 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
16580 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
16581 ;; got extra space, this field does not determine column width
16582 (let (org-table-may-need-update) (org-table-blank-field))
16583 ;; no extra space, this field may determine column width
16584 (org-table-blank-field)))
16587 (looking-at "[^|\n]* |"))
16588 (let (org-table-may-need-update)
16589 (goto-char (1- (match-end 0)))
16590 (delete-backward-char 1)
16591 (goto-char (match-beginning 0))
16592 (self-insert-command N)))
16594 (setq org-table-may-need-update t)
16595 (self-insert-command N)
16596 (org-fix-tags-on-the-fly)
16597 (if org-self-insert-cluster-for-undo
16598 (if (not (eq last-command 'org-self-insert-command))
16599 (setq org-self-insert-command-undo-counter 1)
16600 (if (>= org-self-insert-command-undo-counter 20)
16601 (setq org-self-insert-command-undo-counter 1)
16602 (and (> org-self-insert-command-undo-counter 0)
16604 (not (cadr buffer-undo-list)) ; remove nil entry
16605 (setcdr buffer-undo-list (cddr buffer-undo-list)))
16606 (setq org-self-insert-command-undo-counter
16607 (1+ org-self-insert-command-undo-counter))))))))
16609 (defun org-fix-tags-on-the-fly ()
16610 (when (and (equal (char-after (point-at-bol)) ?*)
16611 (org-on-heading-p))
16612 (org-align-tags-here org-tags-column)))
16614 (defun org-delete-backward-char (N)
16615 "Like `delete-backward-char', insert whitespace at field end in tables.
16616 When deleting backwards, in tables this function will insert whitespace in
16617 front of the next \"|\" separator, to keep the table aligned. The table will
16618 still be marked for re-alignment if the field did fill the entire column,
16619 because, in this case the deletion might narrow the column."
16621 (if (and (org-table-p)
16623 (string-match "|" (buffer-substring (point-at-bol) (point)))
16624 (looking-at ".*?|"))
16625 (let ((pos (point))
16626 (noalign (looking-at "[^|\n\r]* |"))
16627 (c org-table-may-need-update))
16628 (backward-delete-char N)
16629 (if (not overwrite-mode)
16631 (skip-chars-forward "^|")
16633 (goto-char (1- pos))))
16634 ;; noalign: if there were two spaces at the end, this field
16635 ;; does not determine the width of the column.
16636 (if noalign (setq org-table-may-need-update c)))
16637 (backward-delete-char N)
16638 (org-fix-tags-on-the-fly)))
16640 (defun org-delete-char (N)
16641 "Like `delete-char', but insert whitespace at field end in tables.
16642 When deleting characters, in tables this function will insert whitespace in
16643 front of the next \"|\" separator, to keep the table aligned. The table will
16644 still be marked for re-alignment if the field did fill the entire column,
16645 because, in this case the deletion might narrow the column."
16647 (if (and (org-table-p)
16649 (not (= (char-after) ?|))
16651 (if (looking-at ".*?|")
16652 (let ((pos (point))
16653 (noalign (looking-at "[^|\n\r]* |"))
16654 (c org-table-may-need-update))
16655 (replace-match (concat
16656 (substring (match-string 0) 1 -1)
16659 ;; noalign: if there were two spaces at the end, this field
16660 ;; does not determine the width of the column.
16661 (if noalign (setq org-table-may-need-update c)))
16664 (org-fix-tags-on-the-fly)))
16666 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
16667 (put 'org-self-insert-command 'delete-selection t)
16668 (put 'orgtbl-self-insert-command 'delete-selection t)
16669 (put 'org-delete-char 'delete-selection 'supersede)
16670 (put 'org-delete-backward-char 'delete-selection 'supersede)
16671 (put 'org-yank 'delete-selection 'yank)
16673 ;; Make `flyspell-mode' delay after some commands
16674 (put 'org-self-insert-command 'flyspell-delayed t)
16675 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
16676 (put 'org-delete-char 'flyspell-delayed t)
16677 (put 'org-delete-backward-char 'flyspell-delayed t)
16679 ;; Make pabbrev-mode expand after org-mode commands
16680 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
16681 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
16683 ;; How to do this: Measure non-white length of current string
16684 ;; If equal to column width, we should realign.
16686 (defun org-remap (map &rest commands)
16687 "In MAP, remap the functions given in COMMANDS.
16688 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
16691 (setq old (pop commands) new (pop commands))
16692 (if (fboundp 'command-remapping)
16693 (org-defkey map (vector 'remap old) new)
16694 (substitute-key-definition old new map global-map)))))
16696 (when (eq org-enable-table-editor 'optimized)
16697 ;; If the user wants maximum table support, we need to hijack
16698 ;; some standard editing functions
16699 (org-remap org-mode-map
16700 'self-insert-command 'org-self-insert-command
16701 'delete-char 'org-delete-char
16702 'delete-backward-char 'org-delete-backward-char)
16703 (org-defkey org-mode-map "|" 'org-force-self-insert))
16705 (defvar org-ctrl-c-ctrl-c-hook nil
16706 "Hook for functions attaching themselves to `C-c C-c'.
16707 This can be used to add additional functionality to the C-c C-c key which
16708 executes context-dependent commands.
16709 Each function will be called with no arguments. The function must check
16710 if the context is appropriate for it to act. If yes, it should do its
16711 thing and then return a non-nil value. If the context is wrong,
16712 just do nothing and return nil.")
16714 (defvar org-tab-first-hook nil
16715 "Hook for functions to attach themselves to TAB.
16716 See `org-ctrl-c-ctrl-c-hook' for more information.
16717 This hook runs as the first action when TAB is pressed, even before
16718 `org-cycle' messes around with the `outline-regexp' to cater for
16719 inline tasks and plain list item folding.
16720 If any function in this hook returns t, any other actions that
16721 would have been caused by TAB (such as table field motion or visibility
16722 cycling) will not occur.")
16724 (defvar org-tab-after-check-for-table-hook nil
16725 "Hook for functions to attach themselves to TAB.
16726 See `org-ctrl-c-ctrl-c-hook' for more information.
16727 This hook runs after it has been established that the cursor is not in a
16728 table, but before checking if the cursor is in a headline or if global cycling
16730 If any function in this hook returns t, not other actions like visibility
16731 cycling will be done.")
16733 (defvar org-tab-after-check-for-cycling-hook nil
16734 "Hook for functions to attach themselves to TAB.
16735 See `org-ctrl-c-ctrl-c-hook' for more information.
16736 This hook runs after it has been established that not table field motion and
16737 not visibility should be done because of current context. This is probably
16738 the place where a package like yasnippets can hook in.")
16740 (defvar org-tab-before-tab-emulation-hook nil
16741 "Hook for functions to attach themselves to TAB.
16742 See `org-ctrl-c-ctrl-c-hook' for more information.
16743 This hook runs after every other options for TAB have been exhausted, but
16744 before indentation and \t insertion takes place.")
16746 (defvar org-metaleft-hook nil
16747 "Hook for functions attaching themselves to `M-left'.
16748 See `org-ctrl-c-ctrl-c-hook' for more information.")
16749 (defvar org-metaright-hook nil
16750 "Hook for functions attaching themselves to `M-right'.
16751 See `org-ctrl-c-ctrl-c-hook' for more information.")
16752 (defvar org-metaup-hook nil
16753 "Hook for functions attaching themselves to `M-up'.
16754 See `org-ctrl-c-ctrl-c-hook' for more information.")
16755 (defvar org-metadown-hook nil
16756 "Hook for functions attaching themselves to `M-down'.
16757 See `org-ctrl-c-ctrl-c-hook' for more information.")
16758 (defvar org-shiftmetaleft-hook nil
16759 "Hook for functions attaching themselves to `M-S-left'.
16760 See `org-ctrl-c-ctrl-c-hook' for more information.")
16761 (defvar org-shiftmetaright-hook nil
16762 "Hook for functions attaching themselves to `M-S-right'.
16763 See `org-ctrl-c-ctrl-c-hook' for more information.")
16764 (defvar org-shiftmetaup-hook nil
16765 "Hook for functions attaching themselves to `M-S-up'.
16766 See `org-ctrl-c-ctrl-c-hook' for more information.")
16767 (defvar org-shiftmetadown-hook nil
16768 "Hook for functions attaching themselves to `M-S-down'.
16769 See `org-ctrl-c-ctrl-c-hook' for more information.")
16770 (defvar org-metareturn-hook nil
16771 "Hook for functions attaching themselves to `M-RET'.
16772 See `org-ctrl-c-ctrl-c-hook' for more information.")
16773 (defvar org-shiftup-hook nil
16774 "Hook for functions attaching themselves to `S-up'.
16775 See `org-ctrl-c-ctrl-c-hook' for more information.")
16776 (defvar org-shiftup-final-hook nil
16777 "Hook for functions attaching themselves to `S-up'.
16778 This one runs after all other options except shift-select have been excluded.
16779 See `org-ctrl-c-ctrl-c-hook' for more information.")
16780 (defvar org-shiftdown-hook nil
16781 "Hook for functions attaching themselves to `S-down'.
16782 See `org-ctrl-c-ctrl-c-hook' for more information.")
16783 (defvar org-shiftdown-final-hook nil
16784 "Hook for functions attaching themselves to `S-down'.
16785 This one runs after all other options except shift-select have been excluded.
16786 See `org-ctrl-c-ctrl-c-hook' for more information.")
16787 (defvar org-shiftleft-hook nil
16788 "Hook for functions attaching themselves to `S-left'.
16789 See `org-ctrl-c-ctrl-c-hook' for more information.")
16790 (defvar org-shiftleft-final-hook nil
16791 "Hook for functions attaching themselves to `S-left'.
16792 This one runs after all other options except shift-select have been excluded.
16793 See `org-ctrl-c-ctrl-c-hook' for more information.")
16794 (defvar org-shiftright-hook nil
16795 "Hook for functions attaching themselves to `S-right'.
16796 See `org-ctrl-c-ctrl-c-hook' for more information.")
16797 (defvar org-shiftright-final-hook nil
16798 "Hook for functions attaching themselves to `S-right'.
16799 This one runs after all other options except shift-select have been excluded.
16800 See `org-ctrl-c-ctrl-c-hook' for more information.")
16802 (defun org-modifier-cursor-error ()
16803 "Throw an error, a modified cursor command was applied in wrong context."
16804 (error "This command is active in special context like tables, headlines or items"))
16806 (defun org-shiftselect-error ()
16807 "Throw an error because Shift-Cursor command was applied in wrong context."
16808 (if (and (boundp 'shift-select-mode) shift-select-mode)
16809 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
16810 (error "This command works only in special context like headlines or timestamps")))
16812 (defun org-call-for-shift-select (cmd)
16813 (let ((this-command-keys-shift-translated t))
16814 (call-interactively cmd)))
16816 (defun org-shifttab (&optional arg)
16817 "Global visibility cycling or move to previous table field.
16818 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
16820 See the individual commands for more information."
16823 ((org-at-table-p) (call-interactively 'org-table-previous-field))
16825 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
16826 (message "Content view to level: %d" arg)
16827 (org-content (prefix-numeric-value arg2))
16828 (setq org-cycle-global-status 'overview)))
16829 (t (call-interactively 'org-global-cycle))))
16831 (defun org-shiftmetaleft ()
16832 "Promote subtree or delete table column.
16833 Calls `org-promote-subtree', `org-outdent-item',
16834 or `org-table-delete-column', depending on context.
16835 See the individual commands for more information."
16838 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
16839 ((org-at-table-p) (call-interactively 'org-table-delete-column))
16840 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
16841 ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
16842 (t (org-modifier-cursor-error))))
16844 (defun org-shiftmetaright ()
16845 "Demote subtree or insert table column.
16846 Calls `org-demote-subtree', `org-indent-item',
16847 or `org-table-insert-column', depending on context.
16848 See the individual commands for more information."
16851 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
16852 ((org-at-table-p) (call-interactively 'org-table-insert-column))
16853 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
16854 ((org-at-item-p) (call-interactively 'org-indent-item-tree))
16855 (t (org-modifier-cursor-error))))
16857 (defun org-shiftmetaup (&optional arg)
16858 "Move subtree up or kill table row.
16859 Calls `org-move-subtree-up' or `org-table-kill-row' or
16860 `org-move-item-up' depending on context. See the individual commands
16861 for more information."
16864 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
16865 ((org-at-table-p) (call-interactively 'org-table-kill-row))
16866 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
16867 ((org-at-item-p) (call-interactively 'org-move-item-up))
16868 (t (org-modifier-cursor-error))))
16870 (defun org-shiftmetadown (&optional arg)
16871 "Move subtree down or insert table row.
16872 Calls `org-move-subtree-down' or `org-table-insert-row' or
16873 `org-move-item-down', depending on context. See the individual
16874 commands for more information."
16877 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
16878 ((org-at-table-p) (call-interactively 'org-table-insert-row))
16879 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
16880 ((org-at-item-p) (call-interactively 'org-move-item-down))
16881 (t (org-modifier-cursor-error))))
16883 (defsubst org-hidden-tree-error ()
16885 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
16887 (defun org-metaleft (&optional arg)
16888 "Promote heading or move table column to left.
16889 Calls `org-do-promote' or `org-table-move-column', depending on context.
16890 With no specific context, calls the Emacs default `backward-word'.
16891 See the individual commands for more information."
16894 ((run-hook-with-args-until-success 'org-metaleft-hook))
16895 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
16896 ((or (org-on-heading-p)
16897 (and (org-region-active-p)
16899 (goto-char (region-beginning))
16900 (org-on-heading-p))))
16901 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
16902 (call-interactively 'org-do-promote))
16903 ((or (org-at-item-p)
16904 (and (org-region-active-p)
16906 (goto-char (region-beginning))
16908 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
16909 (call-interactively 'org-outdent-item))
16910 (t (call-interactively 'backward-word))))
16912 (defun org-metaright (&optional arg)
16913 "Demote subtree or move table column to right.
16914 Calls `org-do-demote' or `org-table-move-column', depending on context.
16915 With no specific context, calls the Emacs default `forward-word'.
16916 See the individual commands for more information."
16919 ((run-hook-with-args-until-success 'org-metaright-hook))
16920 ((org-at-table-p) (call-interactively 'org-table-move-column))
16921 ((or (org-on-heading-p)
16922 (and (org-region-active-p)
16924 (goto-char (region-beginning))
16925 (org-on-heading-p))))
16926 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
16927 (call-interactively 'org-do-demote))
16928 ((or (org-at-item-p)
16929 (and (org-region-active-p)
16931 (goto-char (region-beginning))
16933 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
16934 (call-interactively 'org-indent-item))
16935 (t (call-interactively 'forward-word))))
16937 (defun org-check-for-hidden (what)
16938 "Check if there are hidden headlines/items in the current visual line.
16939 WHAT can be either `headlines' or `items'. If the current line is
16940 an outline or item heading and it has a folded subtree below it,
16941 this function returns t, nil otherwise."
16943 ((eq what 'headlines) (concat "^" org-outline-regexp))
16944 ((eq what 'items) (concat "^" (org-item-re t)))
16945 (t (error "This should not happen"))))
16949 (unless (org-region-active-p)
16950 (setq beg (point-at-bol))
16951 (beginning-of-line 2)
16952 (while (and (not (eobp)) ;; this is like `next-line'
16953 (get-char-property (1- (point)) 'invisible))
16954 (beginning-of-line 2))
16957 (goto-char (point-at-eol))
16958 (setq end (max end (point)))
16959 (while (re-search-forward re end t)
16960 (if (get-char-property (match-beginning 0) 'invisible)
16964 (defun org-metaup (&optional arg)
16965 "Move subtree up or move table row up.
16966 Calls `org-move-subtree-up' or `org-table-move-row' or
16967 `org-move-item-up', depending on context. See the individual commands
16968 for more information."
16971 ((run-hook-with-args-until-success 'org-metaup-hook))
16972 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
16973 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
16974 ((org-at-item-p) (call-interactively 'org-move-item-up))
16975 (t (transpose-lines 1) (beginning-of-line -1))))
16977 (defun org-metadown (&optional arg)
16978 "Move subtree down or move table row down.
16979 Calls `org-move-subtree-down' or `org-table-move-row' or
16980 `org-move-item-down', depending on context. See the individual
16981 commands for more information."
16984 ((run-hook-with-args-until-success 'org-metadown-hook))
16985 ((org-at-table-p) (call-interactively 'org-table-move-row))
16986 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
16987 ((org-at-item-p) (call-interactively 'org-move-item-down))
16988 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
16990 (defun org-shiftup (&optional arg)
16991 "Increase item in timestamp or increase priority of current headline.
16992 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
16993 depending on context. See the individual commands for more information."
16996 ((run-hook-with-args-until-success 'org-shiftup-hook))
16997 ((and org-support-shift-select (org-region-active-p))
16998 (org-call-for-shift-select 'previous-line))
16999 ((org-at-timestamp-p t)
17000 (call-interactively (if org-edit-timestamp-down-means-later
17001 'org-timestamp-down 'org-timestamp-up)))
17002 ((and (not (eq org-support-shift-select 'always))
17003 org-enable-priority-commands
17004 (org-on-heading-p))
17005 (call-interactively 'org-priority-up))
17006 ((and (not org-support-shift-select) (org-at-item-p))
17007 (call-interactively 'org-previous-item))
17008 ((org-clocktable-try-shift 'up arg))
17009 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
17010 (org-support-shift-select
17011 (org-call-for-shift-select 'previous-line))
17012 (t (org-shiftselect-error))))
17014 (defun org-shiftdown (&optional arg)
17015 "Decrease item in timestamp or decrease priority of current headline.
17016 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
17017 depending on context. See the individual commands for more information."
17020 ((run-hook-with-args-until-success 'org-shiftdown-hook))
17021 ((and org-support-shift-select (org-region-active-p))
17022 (org-call-for-shift-select 'next-line))
17023 ((org-at-timestamp-p t)
17024 (call-interactively (if org-edit-timestamp-down-means-later
17025 'org-timestamp-up 'org-timestamp-down)))
17026 ((and (not (eq org-support-shift-select 'always))
17027 org-enable-priority-commands
17028 (org-on-heading-p))
17029 (call-interactively 'org-priority-down))
17030 ((and (not org-support-shift-select) (org-at-item-p))
17031 (call-interactively 'org-next-item))
17032 ((org-clocktable-try-shift 'down arg))
17033 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
17034 (org-support-shift-select
17035 (org-call-for-shift-select 'next-line))
17036 (t (org-shiftselect-error))))
17038 (defun org-shiftright (&optional arg)
17039 "Cycle the thing at point or in the current line, depending on context.
17040 Depending on context, this does one of the following:
17042 - switch a timestamp at point one day into the future
17043 - on a headline, switch to the next TODO keyword.
17044 - on an item, switch entire list to the next bullet type
17045 - on a property line, switch to the next allowed value
17046 - on a clocktable definition line, move time block into the future"
17049 ((run-hook-with-args-until-success 'org-shiftright-hook))
17050 ((and org-support-shift-select (org-region-active-p))
17051 (org-call-for-shift-select 'forward-char))
17052 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
17053 ((and (not (eq org-support-shift-select 'always))
17054 (org-on-heading-p))
17055 (let ((org-inhibit-logging
17056 (not org-treat-S-cursor-todo-selection-as-state-change))
17057 (org-inhibit-blocking
17058 (not org-treat-S-cursor-todo-selection-as-state-change)))
17059 (org-call-with-arg 'org-todo 'right)))
17060 ((or (and org-support-shift-select
17061 (not (eq org-support-shift-select 'always))
17062 (org-at-item-bullet-p))
17063 (and (not org-support-shift-select) (org-at-item-p)))
17064 (org-call-with-arg 'org-cycle-list-bullet nil))
17065 ((and (not (eq org-support-shift-select 'always))
17066 (org-at-property-p))
17067 (call-interactively 'org-property-next-allowed-value))
17068 ((org-clocktable-try-shift 'right arg))
17069 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
17070 (org-support-shift-select
17071 (org-call-for-shift-select 'forward-char))
17072 (t (org-shiftselect-error))))
17074 (defun org-shiftleft (&optional arg)
17075 "Cycle the thing at point or in the current line, depending on context.
17076 Depending on context, this does one of the following:
17078 - switch a timestamp at point one day into the past
17079 - on a headline, switch to the previous TODO keyword.
17080 - on an item, switch entire list to the previous bullet type
17081 - on a property line, switch to the previous allowed value
17082 - on a clocktable definition line, move time block into the past"
17085 ((run-hook-with-args-until-success 'org-shiftleft-hook))
17086 ((and org-support-shift-select (org-region-active-p))
17087 (org-call-for-shift-select 'backward-char))
17088 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
17089 ((and (not (eq org-support-shift-select 'always))
17090 (org-on-heading-p))
17091 (let ((org-inhibit-logging
17092 (not org-treat-S-cursor-todo-selection-as-state-change))
17093 (org-inhibit-blocking
17094 (not org-treat-S-cursor-todo-selection-as-state-change)))
17095 (org-call-with-arg 'org-todo 'left)))
17096 ((or (and org-support-shift-select
17097 (not (eq org-support-shift-select 'always))
17098 (org-at-item-bullet-p))
17099 (and (not org-support-shift-select) (org-at-item-p)))
17100 (org-call-with-arg 'org-cycle-list-bullet 'previous))
17101 ((and (not (eq org-support-shift-select 'always))
17102 (org-at-property-p))
17103 (call-interactively 'org-property-previous-allowed-value))
17104 ((org-clocktable-try-shift 'left arg))
17105 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
17106 (org-support-shift-select
17107 (org-call-for-shift-select 'backward-char))
17108 (t (org-shiftselect-error))))
17110 (defun org-shiftcontrolright ()
17111 "Switch to next TODO set."
17114 ((and org-support-shift-select (org-region-active-p))
17115 (org-call-for-shift-select 'forward-word))
17116 ((and (not (eq org-support-shift-select 'always))
17117 (org-on-heading-p))
17118 (org-call-with-arg 'org-todo 'nextset))
17119 (org-support-shift-select
17120 (org-call-for-shift-select 'forward-word))
17121 (t (org-shiftselect-error))))
17123 (defun org-shiftcontrolleft ()
17124 "Switch to previous TODO set."
17127 ((and org-support-shift-select (org-region-active-p))
17128 (org-call-for-shift-select 'backward-word))
17129 ((and (not (eq org-support-shift-select 'always))
17130 (org-on-heading-p))
17131 (org-call-with-arg 'org-todo 'previousset))
17132 (org-support-shift-select
17133 (org-call-for-shift-select 'backward-word))
17134 (t (org-shiftselect-error))))
17136 (defun org-ctrl-c-ret ()
17137 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
17140 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
17141 (t (call-interactively 'org-insert-heading))))
17143 (defun org-copy-special ()
17144 "Copy region in table or copy current subtree.
17145 Calls `org-table-copy' or `org-copy-subtree', depending on context.
17146 See the individual commands for more information."
17148 (call-interactively
17149 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
17151 (defun org-cut-special ()
17152 "Cut region in table or cut current subtree.
17153 Calls `org-table-copy' or `org-cut-subtree', depending on context.
17154 See the individual commands for more information."
17156 (call-interactively
17157 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
17159 (defun org-paste-special (arg)
17160 "Paste rectangular region into table, or past subtree relative to level.
17161 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
17162 See the individual commands for more information."
17164 (if (org-at-table-p)
17165 (org-table-paste-rectangle)
17166 (org-paste-subtree arg)))
17168 (defun org-edit-special (&optional arg)
17169 "Call a special editor for the stuff at point.
17170 When at a table, call the formula editor with `org-table-edit-formulas'.
17171 When at the first line of an src example, call `org-edit-src-code'.
17172 When in an #+include line, visit the include file. Otherwise call
17173 `ffap' to visit the file at point."
17175 ;; possibly prep session before editing source
17177 (let* ((info (org-babel-get-src-block-info))
17178 (lang (nth 0 info))
17179 (params (nth 2 info))
17180 (session (cdr (assoc :session params))))
17181 (when (and info session) ;; we are in a source-code block with a session
17183 (intern (concat "org-babel-prep-session:" lang)) session params))))
17184 (cond ;; proceed with `org-edit-special'
17186 (beginning-of-line 1)
17187 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
17188 (find-file (org-trim (match-string 1))))
17189 ((org-edit-src-code))
17190 ((org-edit-fixed-width-region))
17191 ((org-at-table.el-p)
17192 (org-edit-src-code))
17194 (call-interactively 'org-table-edit-formulas))
17195 (t (call-interactively 'ffap))))
17198 (defun org-ctrl-c-ctrl-c (&optional arg)
17199 "Set tags in headline, or update according to changed information at point.
17201 This command does many different things, depending on context:
17203 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
17204 this is what we do.
17206 - If the cursor is on a statistics cookie, update it.
17208 - If the cursor is in a headline, prompt for tags and insert them
17209 into the current line, aligned to `org-tags-column'. When called
17210 with prefix arg, realign all tags in the current buffer.
17212 - If the cursor is in one of the special #+KEYWORD lines, this
17213 triggers scanning the buffer for these lines and updating the
17216 - If the cursor is inside a table, realign the table. This command
17217 works even if the automatic table editor has been turned off.
17219 - If the cursor is on a #+TBLFM line, re-apply the formulas to
17222 - If the cursor is at a footnote reference or definition, jump to
17223 the corresponding definition or references, respectively.
17225 - If the cursor is a the beginning of a dynamic block, update it.
17227 - If the current buffer is a capture buffer, close note and file it.
17229 - If the cursor is on a <<<target>>>, update radio targets and
17230 corresponding links in this buffer.
17232 - If the cursor is on a numbered item in a plain list, renumber the
17235 - If the cursor is on a checkbox, toggle it.
17237 - If the cursor is on a code block, evaluate it. The variable
17238 `org-confirm-babel-evaluate' can be used to control prompting
17239 before code block evaluation, by default every code block
17240 evaluation requires confirmation. Code block evaluation can be
17241 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
17243 (let ((org-enable-table-editor t))
17245 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
17246 org-occur-highlights
17247 org-latex-fragment-image-overlays)
17248 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
17249 (org-remove-occur-highlights)
17250 (org-remove-latex-fragment-image-overlays)
17251 (message "Temporary highlights/overlays removed from current buffer"))
17252 ((and (local-variable-p 'org-finish-function (current-buffer))
17253 (fboundp org-finish-function))
17254 (funcall org-finish-function))
17255 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
17256 ((or (looking-at org-property-start-re)
17257 (org-at-property-p))
17258 (call-interactively 'org-property-action))
17259 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
17260 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
17261 (or (org-on-heading-p) (org-at-item-p)))
17262 (call-interactively 'org-update-statistics-cookies))
17263 ((org-on-heading-p) (call-interactively 'org-set-tags))
17264 ((org-at-table.el-p)
17265 (message "Use C-c ' to edit table.el tables"))
17267 (org-table-maybe-eval-formula)
17269 (call-interactively 'org-table-recalculate)
17270 (org-table-maybe-recalculate-line))
17271 (call-interactively 'org-table-align))
17272 ((or (org-footnote-at-reference-p)
17273 (org-footnote-at-definition-p))
17274 (call-interactively 'org-footnote-action))
17275 ((org-at-item-checkbox-p)
17276 (call-interactively 'org-list-repair)
17277 (call-interactively 'org-toggle-checkbox)
17278 (org-list-send-list 'maybe))
17280 (call-interactively 'org-list-repair)
17281 (when arg (call-interactively 'org-toggle-checkbox))
17282 (org-list-send-list 'maybe))
17283 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
17285 (beginning-of-line 1)
17286 (save-excursion (org-update-dblock)))
17288 (beginning-of-line 1)
17289 (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
17291 ((equal (match-string 1) "TBLFM")
17292 ;; Recalculate the table before this line
17294 (beginning-of-line 1)
17295 (skip-chars-backward " \r\n\t")
17296 (if (org-at-table-p)
17297 (org-call-with-arg 'org-table-recalculate (or arg t)))))
17299 (let ((org-inhibit-startup-visibility-stuff t)
17300 (org-startup-align-all-tables nil))
17301 (org-save-outline-visibility 'use-markers (org-mode-restart)))
17302 (message "Local setup has been refreshed"))))
17303 ((org-clock-update-time-maybe))
17304 (t (error "C-c C-c can do nothing useful at this location")))))
17306 (defun org-mode-restart ()
17307 "Restart Org-mode, to scan again for special lines.
17308 Also updates the keyword regular expressions."
17311 (message "Org-mode restarted"))
17313 (defun org-kill-note-or-show-branches ()
17314 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
17316 (if (not org-finish-function)
17319 (call-interactively 'show-branches))
17320 (let ((org-note-abort t))
17321 (funcall org-finish-function))))
17323 (defun org-return (&optional indent)
17324 "Goto next table row or insert a newline.
17325 Calls `org-table-next-row' or `newline', depending on context.
17326 See the individual commands for more information."
17329 ((bobp) (if indent (newline-and-indent) (newline)))
17331 (org-table-justify-field-maybe)
17332 (call-interactively 'org-table-next-row))
17333 ((and org-return-follows-link
17334 (eq (get-text-property (point) 'face) 'org-link))
17335 (call-interactively 'org-open-at-point))
17336 ((and (org-at-heading-p)
17338 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
17342 (t (if indent (newline-and-indent) (newline)))))
17344 (defun org-return-indent ()
17345 "Goto next table row or insert a newline and indent.
17346 Calls `org-table-next-row' or `newline-and-indent', depending on
17347 context. See the individual commands for more information."
17351 (defun org-ctrl-c-star ()
17352 "Compute table, or change heading status of lines.
17353 Calls `org-table-recalculate' or `org-toggle-heading',
17354 depending on context."
17358 (call-interactively 'org-table-recalculate))
17360 ;; Convert all lines in region to list items
17361 (call-interactively 'org-toggle-heading))))
17363 (defun org-ctrl-c-minus ()
17364 "Insert separator line in table or modify bullet status of line.
17365 Also turns a plain line or a region of lines into list items.
17366 Calls `org-table-insert-hline', `org-toggle-item', or
17367 `org-cycle-list-bullet', depending on context."
17371 (call-interactively 'org-table-insert-hline))
17372 ((org-region-active-p)
17373 (call-interactively 'org-toggle-item))
17375 (call-interactively 'org-cycle-list-bullet))
17377 (call-interactively 'org-toggle-item))))
17379 (defun org-toggle-item ()
17380 "Convert headings or normal lines to items, items to normal lines.
17381 If there is no active region, only the current line is considered.
17383 If the first line in the region is a headline, convert all headlines to items.
17385 If the first line in the region is an item, convert all items to normal lines.
17387 If the first line is normal text, add an item bullet to each line."
17389 (let (l2 l beg end)
17390 (if (org-region-active-p)
17391 (setq beg (region-beginning) end (region-end))
17392 (setq beg (point-at-bol)
17393 end (min (1+ (point-at-eol)) (point-max))))
17396 (setq l2 (org-current-line))
17398 (beginning-of-line 1)
17399 (setq l (1- (org-current-line)))
17400 (if (org-at-item-p)
17401 ;; We already have items, de-itemize
17402 (while (< (setq l (1+ l)) l2)
17403 (when (org-at-item-p)
17404 (skip-chars-forward " \t")
17405 (delete-region (point) (match-end 0)))
17406 (beginning-of-line 2))
17407 (if (org-on-heading-p)
17408 ;; Headings, convert to items
17409 (while (< (setq l (1+ l)) l2)
17410 (if (looking-at org-outline-regexp)
17411 (replace-match (org-list-bullet-string "-") t t))
17412 (beginning-of-line 2))
17413 ;; normal lines, turn them into items
17414 (while (< (setq l (1+ l)) l2)
17415 (unless (org-at-item-p)
17416 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
17418 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
17419 (beginning-of-line 2)))))))
17421 (defun org-toggle-heading (&optional nstars)
17422 "Convert headings to normal text, or items or text to headings.
17423 If there is no active region, only the current line is considered.
17425 If the first line is a heading, remove the stars from all headlines
17428 If the first line is a plain list item, turn all plain list items
17431 If the first line is a normal line, turn each and every line in the
17432 region into a heading.
17434 When converting a line into a heading, the number of stars is chosen
17435 such that the lines become children of the current entry. However,
17436 when a prefix argument is given, its value determines the number of
17439 (let (l2 l itemp beg end)
17440 (if (org-region-active-p)
17441 (setq beg (region-beginning) end (region-end))
17442 (setq beg (point-at-bol)
17443 end (min (1+ (point-at-eol)) (point-max))))
17446 (setq l2 (org-current-line))
17448 (beginning-of-line 1)
17449 (setq l (1- (org-current-line)))
17450 (if (org-on-heading-p)
17451 ;; We already have headlines, de-star them
17452 (while (< (setq l (1+ l)) l2)
17453 (when (org-on-heading-p t)
17454 (and (looking-at outline-regexp) (replace-match "")))
17455 (beginning-of-line 2))
17456 (setq itemp (org-at-item-p))
17459 (make-string (prefix-numeric-value current-prefix-arg)
17462 (if (re-search-backward org-complex-heading-regexp nil t)
17463 (match-string 1) ""))))
17464 (add-stars (cond (nstars "")
17465 ((equal stars "") "*")
17466 (org-odd-levels-only "**")
17468 (rpl (concat stars add-stars " ")))
17469 (while (< (setq l (1+ l)) l2)
17471 (and (org-at-item-p) (replace-match rpl t t))
17472 (unless (org-on-heading-p)
17473 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
17474 (replace-match (concat rpl (match-string 2))))))
17475 (beginning-of-line 2)))))))
17477 (defun org-meta-return (&optional arg)
17478 "Insert a new heading or wrap a region in a table.
17479 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
17480 See the individual commands for more information."
17483 ((run-hook-with-args-until-success 'org-metareturn-hook))
17485 (call-interactively 'org-table-wrap-region))
17486 (t (call-interactively 'org-insert-heading))))
17490 ;; Define the Org-mode menus
17491 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
17493 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
17494 ["Next Field" org-cycle (org-at-table-p)]
17495 ["Previous Field" org-shifttab (org-at-table-p)]
17496 ["Next Row" org-return (org-at-table-p)]
17498 ["Blank Field" org-table-blank-field (org-at-table-p)]
17499 ["Edit Field" org-table-edit-field (org-at-table-p)]
17500 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
17503 ["Move Column Left" org-metaleft (org-at-table-p)]
17504 ["Move Column Right" org-metaright (org-at-table-p)]
17505 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
17506 ["Insert Column" org-shiftmetaright (org-at-table-p)])
17508 ["Move Row Up" org-metaup (org-at-table-p)]
17509 ["Move Row Down" org-metadown (org-at-table-p)]
17510 ["Delete Row" org-shiftmetaup (org-at-table-p)]
17511 ["Insert Row" org-shiftmetadown (org-at-table-p)]
17512 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
17514 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
17516 ["Copy Rectangle" org-copy-special (org-at-table-p)]
17517 ["Cut Rectangle" org-cut-special (org-at-table-p)]
17518 ["Paste Rectangle" org-paste-special (org-at-table-p)]
17519 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
17522 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
17523 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
17524 ["Edit Formulas" org-edit-special (org-at-table-p)]
17526 ["Recalculate line" org-table-recalculate (org-at-table-p)]
17527 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
17528 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
17530 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
17532 ["Sum Column/Rectangle" org-table-sum
17533 (or (org-at-table-p) (org-region-active-p))]
17534 ["Which Column?" org-table-current-column (org-at-table-p)])
17536 org-table-toggle-formula-debugger
17537 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
17538 ["Show Col/Row Numbers"
17539 org-table-toggle-coordinate-overlays
17541 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
17543 ["Create" org-table-create (and (not (org-at-table-p))
17544 org-enable-table-editor)]
17545 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
17546 ["Import from File" org-table-import (not (org-at-table-p))]
17547 ["Export to File" org-table-export (org-at-table-p)]
17549 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
17551 (easy-menu-define org-org-menu org-mode-map "Org menu"
17554 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
17555 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
17556 ["Sparse Tree..." org-sparse-tree t]
17557 ["Reveal Context" org-reveal t]
17558 ["Show All" show-all t]
17560 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
17562 ["New Heading" org-insert-heading t]
17563 ("Navigate Headings"
17564 ["Up" outline-up-heading t]
17565 ["Next" outline-next-visible-heading t]
17566 ["Previous" outline-previous-visible-heading t]
17567 ["Next Same Level" outline-forward-same-level t]
17568 ["Previous Same Level" outline-backward-same-level t]
17570 ["Jump" org-goto t])
17572 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
17573 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
17575 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
17576 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
17577 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
17579 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
17581 ["Promote Heading" org-metaleft (not (org-at-table-p))]
17582 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
17583 ["Demote Heading" org-metaright (not (org-at-table-p))]
17584 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
17586 ["Sort Region/Children" org-sort (not (org-at-table-p))]
17588 ["Convert to odd levels" org-convert-to-odd-levels t]
17589 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
17591 ["Emphasis..." org-emphasize t]
17592 ["Edit Source Example" org-edit-special t]
17594 ["Footnote new/jump" org-footnote-action t]
17595 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
17597 ["Archive (default method)" org-archive-subtree-default t]
17599 ["Move Subtree to Archive file" org-advertized-archive-subtree t]
17600 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
17601 ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
17605 ["Store Link (Global)" org-store-link t]
17606 ["Find existing link to here" org-occur-link-in-agenda-files t]
17607 ["Insert Link" org-insert-link t]
17608 ["Follow Link" org-open-at-point t]
17610 ["Next link" org-next-link t]
17611 ["Previous link" org-previous-link t]
17613 ["Descriptive Links"
17614 (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
17616 :selected (member '(org-link) buffer-invisibility-spec)]
17619 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
17621 :selected (not (member '(org-link) buffer-invisibility-spec))])
17624 ["TODO/DONE/-" org-todo t]
17626 ["Next keyword" org-shiftright (org-on-heading-p)]
17627 ["Previous keyword" org-shiftleft (org-on-heading-p)]
17628 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
17629 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
17630 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
17631 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
17632 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
17634 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
17635 :selected org-enforce-todo-dependencies :style toggle :active t]
17636 "Settings for tree at point"
17637 ["Do Children sequentially" org-toggle-ordered-property :style radio
17638 :selected (ignore-errors (org-entry-get nil "ORDERED"))
17639 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
17640 ["Do Children parallel" org-toggle-ordered-property :style radio
17641 :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
17642 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
17644 ["Set Priority" org-priority t]
17645 ["Priority Up" org-shiftup t]
17646 ["Priority Down" org-shiftdown t]
17648 ["Get news from all feeds" org-feed-update-all t]
17649 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
17650 ["Customize feeds" (customize-variable 'org-feed-alist) t])
17651 ("TAGS and Properties"
17652 ["Set Tags" org-set-tags-command t]
17653 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
17655 ["Set property" org-set-property t]
17656 ["Column view of properties" org-columns t]
17657 ["Insert Column View DBlock" org-insert-columns-dblock t])
17658 ("Dates and Scheduling"
17659 ["Timestamp" org-time-stamp t]
17660 ["Timestamp (inactive)" org-time-stamp-inactive t]
17662 ["1 Day Later" org-shiftright t]
17663 ["1 Day Earlier" org-shiftleft t]
17664 ["1 ... Later" org-shiftup t]
17665 ["1 ... Earlier" org-shiftdown t])
17666 ["Compute Time Range" org-evaluate-time-range t]
17667 ["Schedule Item" org-schedule t]
17668 ["Deadline" org-deadline t]
17670 ["Custom time format" org-toggle-time-stamp-overlays
17671 :style radio :selected org-display-custom-times]
17673 ["Goto Calendar" org-goto-calendar t]
17674 ["Date from Calendar" org-date-from-calendar t]
17676 ["Start/Restart Timer" org-timer-start t]
17677 ["Pause/Continue Timer" org-timer-pause-or-continue t]
17678 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
17679 ["Insert Timer String" org-timer t]
17680 ["Insert Timer Item" org-timer-item t])
17682 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
17683 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
17684 ["Clock out" org-clock-out t]
17685 ["Clock cancel" org-clock-cancel t]
17687 ["Mark as default task" org-clock-mark-default-task t]
17688 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
17689 ["Goto running clock" org-clock-goto t]
17691 ["Display times" org-clock-display t]
17692 ["Create clock table" org-clock-report t]
17694 ["Record DONE time"
17695 (progn (setq org-log-done (not org-log-done))
17696 (message "Switching to %s will %s record a timestamp"
17697 (car org-done-keywords)
17698 (if org-log-done "automatically" "not")))
17699 :style toggle :selected org-log-done])
17701 ["Agenda Command..." org-agenda t]
17702 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
17703 ("File List for Agenda")
17704 ("Special views current file"
17705 ["TODO Tree" org-show-todo-tree t]
17706 ["Check Deadlines" org-check-deadlines t]
17707 ["Timeline" org-timeline t]
17708 ["Tags/Property tree" org-match-sparse-tree t])
17710 ["Export/Publish..." org-export t]
17712 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
17713 :selected org-cdlatex-mode]
17714 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
17715 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
17716 ["Modify math symbol" org-cdlatex-math-modify
17717 (org-inside-LaTeX-fragment-p)]
17718 ["Insert citation" org-reftex-citation t]
17720 ["Template for BEAMER" org-insert-beamer-options-template t])
17723 ["Push Files and Views" org-mobile-push t]
17724 ["Get Captured and Flagged" org-mobile-pull t]
17725 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
17727 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
17730 ["Show Version" org-version t]
17731 ["Info Documentation" org-info t])
17733 ["Browse Org Group" org-customize t]
17735 ["Expand This Menu" org-create-customize-menu
17736 (fboundp 'customize-menu-create)])
17737 ["Send bug report" org-submit-bug-report t]
17740 ["Refresh setup current buffer" org-mode-restart t]
17741 ["Reload Org (after update)" org-reload t]
17742 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
17745 (defun org-info (&optional node)
17746 "Read documentation for Org-mode in the info system.
17747 With optional NODE, go directly to that node."
17749 (info (format "(org)%s" (or node ""))))
17752 (defun org-submit-bug-report ()
17753 "Submit a bug report on Org-mode via mail.
17755 Don't hesitate to report any problems or inaccurate documentation.
17757 If you don't have setup sending mail from (X)Emacs, please copy the
17758 output buffer into your mail program, as it gives us important
17759 information about your Org-mode version and configuration."
17761 (require 'reporter)
17762 (org-load-modules-maybe)
17763 (org-require-autoloaded-modules)
17764 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
17765 (reporter-submit-bug-report
17766 "emacs-orgmode@gnu.org"
17769 (save-window-excursion
17770 (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
17771 (delete-other-windows)
17773 (insert "You are about to submit a bug report to the Org-mode mailing list.
17775 We would like to add your full Org-mode and Outline configuration to the
17776 bug report. This greatly simplifies the work of the maintainer and
17777 other experts on the mailing list.
17779 HOWEVER, some variables you have customized may contain private
17780 information. The names of customers, colleagues, or friends, might
17781 appear in the form of file names, tags, todo states, or search strings.
17782 If you answer yes to the prompt, you might want to check and remove
17783 such private information before sending the email.")
17784 (add-text-properties (point-min) (point-max) '(face org-warning))
17785 (when (yes-or-no-p "Include your Org-mode configuration ")
17789 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
17790 (or (and (symbol-value v)
17791 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
17793 (get v 'custom-type) (get v 'standard-value)
17794 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
17796 (kill-buffer (get-buffer "*Warn about privacy*"))
17799 "Remember to cover the basics, that is, what you expected to happen and
17800 what in fact did happen. You don't know how to make a good report? See
17802 http://orgmode.org/manual/Feedback.html#Feedback
17804 Your bug report will be posted to the Org-mode mailing list.
17805 ------------------------------------------------------------------------")
17807 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
17808 (replace-match "\\1Bug: \\3 [\\2]")))))
17811 (defun org-install-agenda-files-menu ()
17812 (let ((bl (buffer-list)))
17815 (set-buffer (pop bl))
17816 (if (org-mode-p) (setq bl nil)))
17819 '("Org") "File List for Agenda"
17822 ["Edit File List" (org-edit-agenda-file-list) t]
17823 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
17824 ["Remove Current File from List" org-remove-file t]
17825 ["Cycle through agenda files" org-cycle-agenda-files t]
17826 ["Occur in all agenda files" org-occur-in-agenda-files t]
17828 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
17833 (defun org-require-autoloaded-modules ()
17836 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
17837 org-docbook org-exp org-html org-icalendar
17839 org-publish org-remember org-table
17840 org-timer org-xoxo)))
17843 (defun org-reload (&optional uncompiled)
17844 "Reload all org lisp files.
17845 With prefix arg UNCOMPILED, load the uncompiled versions."
17847 (require 'find-func)
17848 (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
17849 (dir-org (file-name-directory (org-find-library-name "org")))
17850 (dir-org-contrib (ignore-errors
17851 (file-name-directory
17852 (org-find-library-name "org-contribdir"))))
17854 (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
17855 (append (list nil "comint" "eval" "exp" "keys"
17856 "lob" "ref" "table" "tangle")
17860 (when (cdr lang) (symbol-name (car lang))))
17861 org-babel-load-languages)))))
17863 (append (directory-files dir-org t file-re)
17865 (and dir-org-contrib
17866 (directory-files dir-org-contrib t file-re))))
17867 (remove-re (concat (if (featurep 'xemacs)
17868 "org-colview" "org-colview-xemacs")
17870 (setq files (mapcar 'file-name-sans-extension files))
17871 (setq files (mapcar
17872 (lambda (x) (if (string-match remove-re x) nil x))
17874 (setq files (delq nil files))
17877 (when (featurep (intern (file-name-nondirectory f)))
17878 (if (and (not uncompiled)
17879 (file-exists-p (concat f ".elc")))
17880 (load (concat f ".elc") nil nil t)
17881 (load (concat f ".el") nil nil t))))
17886 (defun org-customize ()
17887 "Call the customize function with org as argument."
17889 (org-load-modules-maybe)
17890 (org-require-autoloaded-modules)
17891 (customize-browse 'org))
17893 (defun org-create-customize-menu ()
17894 "Create a full customization menu for Org-mode, insert it into the menu."
17896 (org-load-modules-maybe)
17897 (org-require-autoloaded-modules)
17898 (if (fboundp 'customize-menu-create)
17901 '("Org") "Customize"
17902 `(["Browse Org group" org-customize t]
17904 ,(customize-menu-create 'org)
17905 ["Set" Custom-set t]
17906 ["Save" Custom-save t]
17907 ["Reset to Current" Custom-reset-current t]
17908 ["Reset to Saved" Custom-reset-saved t]
17909 ["Reset to Standard Settings" Custom-reset-standard t]))
17910 (message "\"Org\"-menu now contains full customization menu"))
17911 (error "Cannot expand menu (outdated version of cus-edit.el)")))
17913 ;;;; Miscellaneous stuff
17915 ;;; Generally useful functions
17917 (defun org-get-at-bol (property)
17918 "Get text property PROPERTY at beginning of line."
17919 (get-text-property (point-at-bol) property))
17921 (defun org-find-text-property-in-string (prop s)
17922 "Return the first non-nil value of property PROP in string S."
17923 (or (get-text-property 0 prop s)
17924 (get-text-property (or (next-single-property-change 0 prop s) 0)
17927 (defun org-display-warning (message) ;; Copied from Emacs-Muse
17928 "Display the given MESSAGE as a warning."
17929 (if (fboundp 'display-warning)
17930 (display-warning 'org message
17931 (if (featurep 'xemacs) 'warning :warning))
17932 (let ((buf (get-buffer-create "*Org warnings*")))
17933 (with-current-buffer buf
17934 (goto-char (point-max))
17935 (insert "Warning (Org): " message)
17938 (display-buffer buf)
17941 (defun org-in-commented-line ()
17942 "Is point in a line starting with `#'?"
17943 (equal (char-after (point-at-bol)) ?#))
17945 (defun org-in-indented-comment-line ()
17946 "Is point in a line starting with `#' after some white space?"
17949 (goto-char (point-at-bol))
17950 (looking-at "[ \t]*#"))))
17952 (defun org-in-verbatim-emphasis ()
17954 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
17956 (defun org-goto-marker-or-bmk (marker &optional bookmark)
17957 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
17958 (if (and marker (marker-buffer marker)
17959 (buffer-live-p (marker-buffer marker)))
17961 (switch-to-buffer (marker-buffer marker))
17962 (if (or (> marker (point-max)) (< marker (point-min)))
17965 (org-show-context 'org-goto))
17967 (bookmark-jump bookmark)
17968 (error "Cannot find location"))))
17970 (defun org-quote-csv-field (s)
17971 "Quote field for inclusion in CSV material."
17972 (if (string-match "[\",]" s)
17973 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
17976 (defun org-plist-delete (plist property)
17977 "Delete PROPERTY from PLIST.
17978 This is in contrast to merely setting it to 0."
17981 (if (not (eq property (car plist)))
17982 (setq p (plist-put p (car plist) (nth 1 plist))))
17983 (setq plist (cddr plist)))
17986 (defun org-force-self-insert (N)
17987 "Needed to enforce self-insert under remapping."
17989 (self-insert-command N))
17991 (defun org-string-width (s)
17992 "Compute width of string, ignoring invisible characters.
17993 This ignores character with invisibility property `org-link', and also
17994 characters with property `org-cwidth', because these will become invisible
17995 upon the next fontification round."
17997 (when (or (eq t buffer-invisibility-spec)
17998 (assq 'org-link buffer-invisibility-spec))
17999 (while (setq b (text-property-any 0 (length s)
18000 'invisible 'org-link s))
18001 (setq s (concat (substring s 0 b)
18002 (substring s (or (next-single-property-change
18003 b 'invisible s) (length s)))))))
18004 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
18005 (setq s (concat (substring s 0 b)
18006 (substring s (or (next-single-property-change
18007 b 'org-cwidth s) (length s))))))
18008 (setq l (string-width s) b -1)
18009 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
18010 (setq l (- l (get-text-property b 'org-dwidth-n s))))
18013 (defun org-get-indentation (&optional line)
18014 "Get the indentation of the current line, interpreting tabs.
18015 When LINE is given, assume it represents a line and compute its indentation."
18017 (if (string-match "^ *" (org-remove-tabs line))
18020 (beginning-of-line 1)
18021 (skip-chars-forward " \t")
18022 (current-column))))
18024 (defun org-remove-tabs (s &optional width)
18025 "Replace tabulators in S with spaces.
18026 Assumes that s is a single line, starting in column 0."
18027 (setq width (or width tab-width))
18028 (while (string-match "\t" s)
18029 (setq s (replace-match
18031 (- (* width (/ (+ (match-beginning 0) width) width))
18032 (match-beginning 0)) ?\ )
18036 (defun org-fix-indentation (line ind)
18037 "Fix indentation in LINE.
18038 IND is a cons cell with target and minimum indentation.
18039 If the current indentation in LINE is smaller than the minimum,
18040 leave it alone. If it is larger than ind, set it to the target."
18041 (let* ((l (org-remove-tabs line))
18042 (i (org-get-indentation l))
18043 (i1 (car ind)) (i2 (cdr ind)))
18044 (if (>= i i2) (setq l (substring line i2)))
18046 (concat (make-string i1 ?\ ) l)
18049 (defun org-remove-indentation (code &optional n)
18050 "Remove the maximum common indentation from the lines in CODE.
18051 N may optionally be the number of spaces to remove."
18054 (org-do-remove-indentation n)
18057 (defun org-do-remove-indentation (&optional n)
18058 "Remove the maximum common indentation from the buffer."
18059 (untabify (point-min) (point-max))
18060 (let ((min 10000) re)
18063 (goto-char (point-min))
18064 (while (re-search-forward "^ *[^ \n]" nil t)
18065 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
18066 (unless (or (= min 0) (= min 10000))
18067 (setq re (format "^ \\{%d\\}" min))
18068 (goto-char (point-min))
18069 (while (re-search-forward re nil t)
18074 (defun org-fill-template (template alist)
18075 "Find each %key of ALIST in TEMPLATE and replace it."
18076 (let ((case-fold-search nil)
18078 (setq alist (sort (copy-sequence alist)
18079 (lambda (a b) (< (length (car a)) (length (car b))))))
18080 (while (setq entry (pop alist))
18082 (replace-regexp-in-string
18083 (concat "%" (regexp-quote (car entry)))
18084 (cdr entry) template t t)))
18087 (defun org-base-buffer (buffer)
18088 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
18091 (or (buffer-base-buffer buffer)
18094 (defun org-trim (s)
18095 "Remove whitespace at beginning and end of string."
18096 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
18097 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
18100 (defun org-wrap (string &optional width lines)
18101 "Wrap string to either a number of lines, or a width in characters.
18102 If WIDTH is non-nil, the string is wrapped to that width, however many lines
18103 that costs. If there is a word longer than WIDTH, the text is actually
18104 wrapped to the length of that word.
18105 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
18106 many lines, whatever width that takes.
18107 The return value is a list of lines, without newlines at the end."
18108 (let* ((words (org-split-string string "[ \t\n]+"))
18109 (maxword (apply 'max (mapcar 'org-string-width words)))
18112 (org-do-wrap words (max maxword width)))
18115 (setq ll (org-do-wrap words maxword))
18116 (if (<= (length ll) lines)
18119 (while (> (length ll) lines)
18121 (setq ll (org-do-wrap words w)))
18123 (t (error "Cannot wrap this")))))
18125 (defun org-do-wrap (words width)
18126 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
18129 (setq line (pop words))
18130 (while (and words (< (+ (length line) (length (car words))) width))
18131 (setq line (concat line " " (pop words))))
18132 (setq lines (push line lines)))
18135 (defun org-split-string (string &optional separators)
18136 "Splits STRING into substrings at SEPARATORS.
18137 No empty strings are returned if there are matches at the beginning
18138 and end of string."
18139 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
18143 (while (and (string-match rexp string
18145 (= start (match-beginning 0))
18146 (< start (length string)))
18148 (< (match-beginning 0) (length string)))
18150 (or (eq (match-beginning 0) 0)
18151 (and (eq (match-beginning 0) (match-end 0))
18152 (eq (match-beginning 0) start))
18154 (cons (substring string start (match-beginning 0))
18156 (setq start (match-end 0)))
18157 (or (eq start (length string))
18159 (cons (substring string start)
18163 (defun org-quote-vert (s)
18164 "Replace \"|\" with \"\\vert\"."
18165 (while (string-match "|" s)
18166 (setq s (replace-match "\\vert" t t s)))
18169 (defun org-uuidgen-p (s)
18170 "Is S an ID created by UUIDGEN?"
18171 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
18173 (defun org-context ()
18174 "Return a list of contexts of the current cursor position.
18175 If several contexts apply, all are returned.
18176 Each context entry is a list with a symbol naming the context, and
18177 two positions indicating start and end of the context. Possible
18180 :headline anywhere in a headline
18181 :headline-stars on the leading stars in a headline
18182 :todo-keyword on a TODO keyword (including DONE) in a headline
18183 :tags on the TAGS in a headline
18184 :priority on the priority cookie in a headline
18185 :item on the first line of a plain list item
18186 :item-bullet on the bullet/number of a plain list item
18187 :checkbox on the checkbox in a plain list item
18188 :table in an org-mode table
18189 :table-special on a special filed in a table
18190 :table-table in a table.el table
18191 :link on a hyperlink
18192 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
18193 :target on a <<target>>
18194 :radio-target on a <<<radio-target>>>
18195 :latex-fragment on a LaTeX fragment
18196 :latex-preview on a LaTeX fragment with overlayed preview image
18198 This function expects the position to be visible because it uses font-lock
18199 faces as a help to recognize the following contexts: :table-special, :link,
18201 (let* ((f (get-text-property (point) 'face))
18202 (faces (if (listp f) f (list f)))
18203 (p (point)) clist o)
18204 ;; First the large context
18206 ((org-on-heading-p t)
18207 (push (list :headline (point-at-bol) (point-at-eol)) clist)
18209 (beginning-of-line 1)
18210 (looking-at org-todo-line-tags-regexp))
18211 (push (org-point-in-group p 1 :headline-stars) clist)
18212 (push (org-point-in-group p 2 :todo-keyword) clist)
18213 (push (org-point-in-group p 4 :tags) clist))
18215 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
18216 (if (looking-at "\\[#[A-Z0-9]\\]")
18217 (push (org-point-in-group p 0 :priority) clist)))
18220 (push (org-point-in-group p 2 :item-bullet) clist)
18221 (push (list :item (point-at-bol)
18222 (save-excursion (org-end-of-item) (point)))
18224 (and (org-at-item-checkbox-p)
18225 (push (org-point-in-group p 0 :checkbox) clist)))
18228 (push (list :table (org-table-begin) (org-table-end)) clist)
18229 (if (memq 'org-formula faces)
18230 (push (list :table-special
18231 (previous-single-property-change p 'face)
18232 (next-single-property-change p 'face)) clist)))
18233 ((org-at-table-p 'any)
18234 (push (list :table-table) clist)))
18237 ;; Now the small context
18239 ((org-at-timestamp-p)
18240 (push (org-point-in-group p 0 :timestamp) clist))
18241 ((memq 'org-link faces)
18243 (previous-single-property-change p 'face)
18244 (next-single-property-change p 'face)) clist))
18245 ((memq 'org-special-keyword faces)
18246 (push (list :keyword
18247 (previous-single-property-change p 'face)
18248 (next-single-property-change p 'face)) clist))
18250 (push (org-point-in-group p 0 :target) clist)
18251 (goto-char (1- (match-beginning 0)))
18252 (if (looking-at org-radio-target-regexp)
18253 (push (org-point-in-group p 0 :radio-target) clist))
18255 ((setq o (car (delq nil
18258 (if (memq x org-latex-fragment-image-overlays) x))
18259 (overlays-at (point))))))
18260 (push (list :latex-fragment
18261 (overlay-start o) (overlay-end o)) clist)
18262 (push (list :latex-preview
18263 (overlay-start o) (overlay-end o)) clist))
18264 ((org-inside-LaTeX-fragment-p)
18265 ;; FIXME: positions wrong.
18266 (push (list :latex-fragment (point) (point)) clist)))
18268 (setq clist (nreverse (delq nil clist)))
18271 ;; FIXME: Compare with at-regexp-p Do we need both?
18272 (defun org-in-regexp (re &optional nlines visually)
18273 "Check if point is inside a match of regexp.
18274 Normally only the current line is checked, but you can include NLINES extra
18275 lines both before and after point into the search.
18276 If VISUALLY is set, require that the cursor is not after the match but
18277 really on, so that the block visually is on the match."
18279 (let ((pos (point))
18280 (eol (point-at-eol (+ 1 (or nlines 0))))
18281 (inc (if visually 1 0)))
18283 (beginning-of-line (- 1 (or nlines 0)))
18284 (while (re-search-forward re eol t)
18285 (if (and (<= (match-beginning 0) pos)
18286 (>= (+ inc (match-end 0)) pos))
18287 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
18289 (defun org-at-regexp-p (regexp)
18290 "Is point inside a match of REGEXP in the current line?"
18293 (let ((pos (point)) (end (point-at-eol)))
18294 (beginning-of-line 1)
18295 (while (re-search-forward regexp end t)
18296 (if (and (<= (match-beginning 0) pos)
18297 (>= (match-end 0) pos))
18301 (defun org-in-regexps-block-p (start-re end-re &optional bound)
18302 "Return t if the current point is between matches of START-RE and END-RE.
18303 This will also return t if point is on one of the two matches or
18304 in an unfinished block. END-RE can be a string or a form
18305 returning a string.
18307 An optional third argument bounds the search for START-RE. It
18308 defaults to previous heading or `point-min'."
18309 (let ((pos (point))
18310 (limit (or bound (save-excursion (outline-previous-heading)))))
18312 ;; we're on a block when point is on start-re...
18313 (or (org-at-regexp-p start-re)
18314 ;; ... or start-re can be found above...
18315 (and (re-search-backward start-re limit t)
18316 ;; ... but no end-re between start-re and point.
18317 (not (re-search-forward (eval end-re) pos t)))))))
18319 (defun org-occur-in-agenda-files (regexp &optional nlines)
18320 "Call `multi-occur' with buffers for all agenda files."
18321 (interactive "sOrg-files matching: \np")
18322 (let* ((files (org-agenda-files))
18323 (tnames (mapcar 'file-truename files))
18324 (extra org-agenda-text-search-extra-files)
18326 (when (eq (car extra) 'agenda-archives)
18327 (setq extra (cdr extra))
18328 (setq files (org-add-archive-files files)))
18329 (while (setq f (pop extra))
18330 (unless (member (file-truename f) tnames)
18331 (add-to-list 'files f 'append)
18332 (add-to-list 'tnames (file-truename f) 'append)))
18334 (mapcar (lambda (x)
18335 (with-current-buffer
18336 (or (get-file-buffer x) (find-file-noselect x))
18342 (if (boundp 'occur-mode-find-occurrence-hook)
18344 (add-hook 'occur-mode-find-occurrence-hook
18349 (defadvice occur-mode-goto-occurrence
18350 (after org-occur-reveal activate)
18351 (and (org-mode-p) (org-reveal)))
18352 (defadvice occur-mode-goto-occurrence-other-window
18353 (after org-occur-reveal activate)
18354 (and (org-mode-p) (org-reveal)))
18355 (defadvice occur-mode-display-occurrence
18356 (after org-occur-reveal activate)
18358 (let ((pos (occur-mode-find-occurrence)))
18359 (with-current-buffer (marker-buffer pos)
18364 (defun org-occur-link-in-agenda-files ()
18365 "Create a link and search for it in the agendas.
18366 The link is not stored in `org-stored-links', it is just created
18367 for the search purpose."
18369 (let ((link (condition-case nil
18370 (org-store-link nil)
18371 (error "Unable to create a link to here"))))
18372 (org-occur-in-agenda-files (regexp-quote link))))
18374 (defun org-uniquify (list)
18375 "Remove duplicate elements from LIST."
18377 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
18380 (defun org-delete-all (elts list)
18381 "Remove all elements in ELTS from LIST."
18383 (setq list (delete (pop elts) list)))
18386 (defun org-count (cl-item cl-seq)
18387 "Count the number of occurrences of ITEM in SEQ.
18388 Taken from `count' in cl-seq.el with all keyword arguments removed."
18389 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
18390 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
18391 (while (< cl-start cl-end)
18392 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
18393 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
18394 (setq cl-start (1+ cl-start)))
18397 (defun org-remove-if (predicate seq)
18398 "Remove everything from SEQ that fulfills PREDICATE."
18402 (if (not (funcall predicate e)) (push e res)))
18405 (defun org-remove-if-not (predicate seq)
18406 "Remove everything from SEQ that does not fulfill PREDICATE."
18410 (if (funcall predicate e) (push e res)))
18413 (defun org-back-over-empty-lines ()
18414 "Move backwards over whitespace, to the beginning of the first empty line.
18415 Returns the number of empty lines passed."
18416 (let ((pos (point)))
18417 (skip-chars-backward " \t\n\r")
18418 (beginning-of-line 2)
18419 (goto-char (min (point) pos))
18420 (count-lines (point) pos)))
18422 (defun org-skip-whitespace ()
18423 (skip-chars-forward " \t\n\r"))
18425 (defun org-point-in-group (point group &optional context)
18426 "Check if POINT is in match-group GROUP.
18427 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
18428 match. If the match group does not exist or point is not inside it,
18430 (and (match-beginning group)
18431 (>= point (match-beginning group))
18432 (<= point (match-end group))
18434 (list context (match-beginning group) (match-end group))
18437 (defun org-switch-to-buffer-other-window (&rest args)
18438 "Switch to buffer in a second window on the current frame.
18439 In particular, do not allow pop-up frames.
18440 Returns the newly created buffer."
18441 (let (pop-up-frames special-display-buffer-names special-display-regexps
18442 special-display-function)
18443 (apply 'switch-to-buffer-other-window args)))
18445 (defun org-combine-plists (&rest plists)
18446 "Create a single property list from all plists in PLISTS.
18447 The process starts by copying the first list, and then setting properties
18448 from the other lists. Settings in the last list are the most significant
18449 ones and overrule settings in the other lists."
18450 (let ((rtn (copy-sequence (pop plists)))
18453 (setq ls (pop plists))
18455 (setq p (pop ls) v (pop ls))
18456 (setq rtn (plist-put rtn p v))))
18459 (defun org-move-line-down (arg)
18460 "Move the current line down. With prefix argument, move it past ARG lines."
18462 (let ((col (current-column))
18464 (beginning-of-line 1) (setq beg (point))
18465 (beginning-of-line 2) (setq end (point))
18466 (beginning-of-line (+ 1 arg))
18467 (setq pos (move-marker (make-marker) (point)))
18468 (insert (delete-and-extract-region beg end))
18470 (org-move-to-column col)))
18472 (defun org-move-line-up (arg)
18473 "Move the current line up. With prefix argument, move it past ARG lines."
18475 (let ((col (current-column))
18477 (beginning-of-line 1) (setq beg (point))
18478 (beginning-of-line 2) (setq end (point))
18479 (beginning-of-line (- arg))
18480 (setq pos (move-marker (make-marker) (point)))
18481 (insert (delete-and-extract-region beg end))
18483 (org-move-to-column col)))
18485 (defun org-replace-escapes (string table)
18486 "Replace %-escapes in STRING with values in TABLE.
18487 TABLE is an association list with keys like \"%a\" and string values.
18488 The sequences in STRING may contain normal field width and padding information,
18489 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
18490 so values can contain further %-escapes if they are define later in TABLE."
18491 (let ((tbl (copy-alist table))
18492 (case-fold-search nil)
18495 (while (setq e (pop tbl))
18496 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
18497 (when (and (cdr e) (string-match re (cdr e)))
18498 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
18500 (add-text-properties 0 3 (list 'sref sref) safe)
18501 (setcdr e (replace-match safe t t (cdr e)))))
18502 (while (string-match re string)
18503 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
18505 (setq string (replace-match rpl t t string))))
18506 (while (setq pchg (next-property-change pchg string))
18507 (let ((sref (get-text-property pchg 'sref string)))
18508 (when (and sref (string-match "SREF" string pchg))
18509 (setq string (replace-match sref t t string)))))
18512 (defun org-sublist (list start end)
18513 "Return a section of LIST, from START to END.
18514 Counting starts at 1."
18515 (let (rtn (c start))
18516 (setq list (nthcdr (1- start) list))
18517 (while (and list (<= c end))
18518 (push (pop list) rtn)
18522 (defun org-find-base-buffer-visiting (file)
18523 "Like `find-buffer-visiting' but always return the base buffer and
18524 not an indirect buffer."
18525 (let ((buf (or (get-file-buffer file)
18526 (find-buffer-visiting file))))
18528 (or (buffer-base-buffer buf) buf)
18531 (defun org-image-file-name-regexp (&optional extensions)
18532 "Return regexp matching the file names of images.
18533 If EXTENSIONS is given, only match these."
18534 (if (and (not extensions) (fboundp 'image-file-name-regexp))
18535 (image-file-name-regexp)
18536 (let ((image-file-name-extensions
18538 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
18539 "xbm" "xpm" "pbm" "pgm" "ppm"))))
18541 (regexp-opt (nconc (mapcar 'upcase
18542 image-file-name-extensions)
18543 image-file-name-extensions)
18547 (defun org-file-image-p (file &optional extensions)
18548 "Return non-nil if FILE is an image."
18550 (string-match (org-image-file-name-regexp extensions) file)))
18552 (defun org-get-cursor-date ()
18553 "Return the date at cursor in as a time.
18554 This works in the calendar and in the agenda, anywhere else it just
18555 returns the current time."
18556 (let (date day defd)
18558 ((eq major-mode 'calendar-mode)
18559 (setq date (calendar-cursor-to-date)
18560 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
18561 ((eq major-mode 'org-agenda-mode)
18562 (setq day (get-text-property (point) 'day))
18564 (setq date (calendar-gregorian-from-absolute day)
18565 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
18567 (or defd (current-time))))
18569 (defvar org-agenda-action-marker (make-marker)
18570 "Marker pointing to the entry for the next agenda action.")
18572 (defun org-mark-entry-for-agenda-action ()
18573 "Mark the current entry as target of an agenda action.
18574 Agenda actions are actions executed from the agenda with the key `k',
18575 which make use of the date at the cursor."
18577 (move-marker org-agenda-action-marker
18578 (save-excursion (org-back-to-heading t) (point))
18581 "Entry marked for action; press `k' at desired date in agenda or calendar"))
18583 ;;; Paragraph filling stuff.
18584 ;; We want this to be just right, so use the full arsenal.
18586 (defun org-indent-line-function ()
18587 "Indent line like previous, but further if previous was headline or item."
18589 (let* ((pos (point))
18590 (itemp (org-at-item-p))
18591 (case-fold-search t)
18592 (org-drawer-regexp (or org-drawer-regexp "\000"))
18593 column bpos bcol tpos tcol bullet btype bullet-type)
18594 ;; Find the previous relevant line
18595 (beginning-of-line 1)
18598 ((looking-at "#") (setq column 0))
18600 ((looking-at "\\*+ ") (setq column 0))
18602 ((and (looking-at "[ \t]*:END:")
18603 (save-excursion (re-search-backward org-drawer-regexp nil t)))
18605 (goto-char (1- (match-beginning 1)))
18606 (setq column (current-column))))
18608 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
18610 (re-search-backward
18611 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
18612 (setq column (org-get-indentation (match-string 0))))
18613 ((and (not (looking-at "[ \t]*#\\+begin_"))
18614 (org-in-regexps-block-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
18616 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
18618 (if (equal (downcase (match-string 1)) "src")
18619 ;; src blocks: let `org-edit-src-exit' handle them
18620 (org-get-indentation)
18621 (org-get-indentation (match-string 0)))))
18624 (org-beginning-of-item)
18625 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\(:?\\[@\\(:?start:\\)?[0-9]+\\][ \t]*\\)?\\[[- X]\\][ \t]*\\|.*? :: \\)?")
18626 (setq bpos (match-beginning 1) tpos (match-end 0)
18627 bcol (progn (goto-char bpos) (current-column))
18628 tcol (progn (goto-char tpos) (current-column))
18629 bullet (match-string 1)
18630 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
18631 (if (> tcol (+ bcol org-description-max-indent))
18632 (setq tcol (+ bcol 5)))
18635 (beginning-of-line 1)
18637 (if (looking-at "\\S-")
18639 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
18640 (setq bullet (match-string 1)
18641 btype (if (string-match "[0-9]" bullet) "n" bullet))
18642 (setq column (if (equal btype bullet-type) bcol tcol)))
18643 (setq column (org-get-indentation)))))
18644 ;; This line has nothing special, look upside to get a clue about
18647 (beginning-of-line 0)
18648 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
18649 (not (looking-at "[ \t]*:END:"))
18650 (not (looking-at org-drawer-regexp)))
18651 (beginning-of-line 0))
18653 ;; There was an heading above.
18654 ((looking-at "\\*+[ \t]+")
18655 (if (not org-adapt-indentation)
18657 (goto-char (match-end 0))
18658 (setq column (current-column))))
18659 ;; A drawer had started and is unfinished: indent consequently.
18660 ((looking-at org-drawer-regexp)
18661 (goto-char (1- (match-beginning 1)))
18662 (setq column (current-column)))
18663 ;; The drawer had ended: indent like its :END: line.
18664 ((looking-at "\\([ \t]*\\):END:")
18665 (goto-char (match-end 1))
18666 (setq column (current-column)))
18667 ;; There was a list that since ended: indent relatively to
18668 ;; current heading.
18670 (outline-previous-heading)
18671 (if (and org-adapt-indentation
18672 (looking-at "\\*+[ \t]+"))
18674 (goto-char (match-end 0))
18675 (setq column (current-column)))
18677 ;; Else, nothing noticeable found: get indentation and go on.
18678 (t (setq column (org-get-indentation))))))
18680 (if (<= (current-column) (current-indentation))
18681 (org-indent-line-to column)
18682 (save-excursion (org-indent-line-to column)))
18683 (setq column (current-column))
18684 (beginning-of-line 1)
18686 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
18687 (replace-match (concat (match-string 1)
18688 (format org-property-format
18689 (match-string 2) (match-string 3)))
18691 (org-move-to-column column)))
18693 (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
18694 "Variable to store copy of `adaptive-fill-regexp'.
18695 Since `adaptive-fill-regexp' is set to never match, we need to
18696 store a backup of its value before entering `org-mode' so that
18697 the functionality can be provided as a fall-back.")
18699 (defun org-set-autofill-regexps ()
18701 ;; In the paragraph separator we include headlines, because filling
18702 ;; text in a line directly attached to a headline would otherwise
18703 ;; fill the headline as well.
18704 (org-set-local 'comment-start-skip "^#+[ \t]*")
18705 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
18706 ;; The paragraph starter includes hand-formatted lists.
18714 "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
18717 "\\\\\\(begin\\|end\\|[][]\\)"))
18718 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
18719 ;; But only if the user has not turned off tables or fixed-width regions
18721 'auto-fill-inhibit-regexp
18722 (concat "\\*+ \\|#\\+"
18723 "\\|[ \t]*" org-keyword-time-regexp
18724 (if (or org-enable-table-editor org-enable-fixed-width-editor)
18727 (if org-enable-table-editor "|" "")
18728 (if org-enable-fixed-width-editor ":" "")
18730 ;; We use our own fill-paragraph function, to make sure that tables
18731 ;; and fixed-width regions are not wrapped. That function will pass
18732 ;; through to `fill-paragraph' when appropriate.
18733 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
18734 ;; Adaptive filling: To get full control, first make sure that
18735 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
18736 (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
18737 (org-set-local 'org-adaptive-fill-regexp-backup
18738 adaptive-fill-regexp))
18739 (org-set-local 'adaptive-fill-regexp "\000")
18740 (org-set-local 'adaptive-fill-function
18741 'org-adaptive-fill-function)
18743 'align-mode-rules-list
18744 '((org-in-buffer-settings
18745 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
18746 (modes . '(org-mode))))))
18748 (defun org-fill-paragraph (&optional justify)
18749 "Re-align a table, pass through to fill-paragraph if no table."
18750 (let ((table-p (org-at-table-p))
18751 (table.el-p (org-at-table.el-p)))
18752 (cond ((and (equal (char-after (point-at-bol)) ?*)
18753 (save-excursion (goto-char (point-at-bol))
18754 (looking-at outline-regexp)))
18755 t) ; skip headlines
18756 (table.el-p t) ; skip table.el tables
18757 (table-p (org-table-align) t) ; align org-mode tables
18758 (t nil)))) ; call paragraph-fill
18760 ;; For reference, this is the default value of adaptive-fill-regexp
18761 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
18763 (defun org-adaptive-fill-function ()
18764 "Return a fill prefix for org-mode files.
18765 In particular, this makes sure hanging paragraphs for hand-formatted lists
18769 ((looking-at "#[ \t]+")
18770 (match-string-no-properties 0))
18771 ;; Description list
18772 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
18774 (if (> (match-end 1) (+ (match-beginning 1)
18775 org-description-max-indent))
18776 (goto-char (+ (match-beginning 1) 5))
18777 (goto-char (match-end 0)))
18778 (make-string (current-column) ?\ )))
18779 ;; Ordered or unordered list
18780 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
18782 (goto-char (match-end 0))
18783 (make-string (current-column) ?\ )))
18785 ((looking-at org-adaptive-fill-regexp-backup)
18786 (match-string-no-properties 0))))
18790 (defun org-toggle-fixed-width-section (arg)
18791 "Toggle the fixed-width export.
18792 If there is no active region, the QUOTE keyword at the current headline is
18793 inserted or removed. When present, it causes the text between this headline
18794 and the next to be exported as fixed-width text, and unmodified.
18795 If there is an active region, this command adds or removes a colon as the
18796 first character of this line. If the first character of a line is a colon,
18797 this line is also exported in fixed-width font."
18800 (regionp (org-region-active-p))
18801 (beg (if regionp (region-beginning) (point)))
18802 (end (if regionp (region-end)))
18803 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
18804 (case-fold-search nil)
18805 (re "[ \t]*\\(: \\)")
18810 (setq cc (current-column))
18811 (beginning-of-line 1)
18812 (setq off (looking-at re))
18813 (while (> nlines 0)
18814 (setq nlines (1- nlines))
18815 (beginning-of-line 1)
18818 (org-move-to-column cc t)
18821 ((and off (looking-at re))
18822 (replace-match "" t t nil 1))
18823 ((not off) (org-move-to-column cc t) (insert ": ")))
18826 (org-back-to-heading)
18827 (if (looking-at (concat outline-regexp
18828 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
18829 (replace-match "" t t nil 1)
18830 (if (looking-at outline-regexp)
18832 (goto-char (match-end 0))
18833 (insert org-quote-string " "))))))))
18835 (defun org-reftex-citation ()
18836 "Use reftex-citation to insert a citation into the buffer.
18837 This looks for a line like
18839 #+BIBLIOGRAPHY: foo plain option:-d
18841 and derives from it that foo.bib is the bibliography file relevant
18842 for this document. It then installs the necessary environment for RefTeX
18843 to work in this buffer and calls `reftex-citation' to insert a citation
18846 Export of such citations to both LaTeX and HTML is handled by the contributed
18847 package org-exp-bibtex by Taru Karttunen."
18849 (let ((reftex-docstruct-symbol 'rds)
18850 (reftex-cite-format "\\cite{%l}")
18855 (let ((case-fold-search t)
18856 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
18857 (if (not (save-excursion
18858 (or (re-search-forward re nil t)
18859 (re-search-backward re nil t))))
18860 (error "No bibliography defined in file")
18861 (setq bib (concat (match-string 1) ".bib")
18862 rds (list (list 'bib bib)))))))
18863 (call-interactively 'reftex-citation)))
18865 ;;;; Functions extending outline functionality
18867 (defun org-beginning-of-line (&optional arg)
18868 "Go to the beginning of the current line. If that is invisible, continue
18869 to a visible line beginning. This makes the function of C-a more intuitive.
18870 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
18871 first attempt, and only move to after the tags when the cursor is already
18872 beyond the end of the headline."
18874 (let ((pos (point))
18875 (special (if (consp org-special-ctrl-a/e)
18876 (car org-special-ctrl-a/e)
18877 org-special-ctrl-a/e))
18879 (if (org-bound-and-true-p line-move-visual)
18880 (beginning-of-visual-line 1)
18881 (beginning-of-line 1))
18882 (if (and arg (fboundp 'move-beginning-of-line))
18883 (call-interactively 'move-beginning-of-line)
18887 (if (org-truely-invisible-p)
18888 (while (and (not (bobp)) (org-truely-invisible-p))
18890 (beginning-of-line 1))
18891 (forward-char 1))))
18894 ((and (looking-at org-complex-heading-regexp)
18895 (= (char-after (match-end 1)) ?\ ))
18896 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
18900 (cond ((> pos refpos) refpos)
18901 ((= pos (point)) refpos)
18903 (cond ((> pos (point)) (point))
18904 ((not (eq last-command this-command)) (point))
18909 (cond ((> pos (match-end 4)) (match-end 4))
18910 ((= pos (point)) (match-end 4))
18912 (cond ((> pos (point)) (point))
18913 ((not (eq last-command this-command)) (point))
18914 (t (match-end 4))))))))
18916 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
18918 (defun org-end-of-line (&optional arg)
18919 "Go to the end of the line.
18920 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
18921 first attempt, and only move to after the tags when the cursor is already
18922 beyond the end of the headline."
18924 (let ((special (if (consp org-special-ctrl-a/e)
18925 (cdr org-special-ctrl-a/e)
18926 org-special-ctrl-a/e)))
18927 (if (or (not special)
18928 (not (org-on-heading-p))
18930 (call-interactively
18931 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
18932 ((fboundp 'move-end-of-line) 'move-end-of-line)
18934 (let ((pos (point)))
18935 (beginning-of-line 1)
18936 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
18938 (if (or (< pos (match-beginning 1))
18939 (= pos (match-end 0)))
18940 (goto-char (match-beginning 1))
18941 (goto-char (match-end 0)))
18942 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
18943 (goto-char (match-end 0))
18944 (goto-char (match-beginning 1))))
18945 (call-interactively (if (fboundp 'move-end-of-line)
18949 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
18951 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
18952 (define-key org-mode-map "\C-e" 'org-end-of-line)
18953 (define-key org-mode-map [home] 'org-beginning-of-line)
18954 (define-key org-mode-map [end] 'org-end-of-line)
18956 (defun org-backward-sentence (&optional arg)
18957 "Go to beginning of sentence, or beginning of table field.
18958 This will call `backward-sentence' or `org-table-beginning-of-field',
18959 depending on context."
18962 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
18963 (t (call-interactively 'backward-sentence))))
18965 (defun org-forward-sentence (&optional arg)
18966 "Go to end of sentence, or end of table field.
18967 This will call `forward-sentence' or `org-table-end-of-field',
18968 depending on context."
18971 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
18972 (t (call-interactively 'forward-sentence))))
18974 (define-key org-mode-map "\M-a" 'org-backward-sentence)
18975 (define-key org-mode-map "\M-e" 'org-forward-sentence)
18977 (defun org-kill-line (&optional arg)
18978 "Kill line, to tags or end of line."
18981 ((or (not org-special-ctrl-k)
18983 (not (org-on-heading-p)))
18984 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
18985 org-ctrl-k-protect-subtree)
18986 (if (or (eq org-ctrl-k-protect-subtree 'error)
18987 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
18988 (error "C-k aborted - would kill hidden subtree")))
18989 (call-interactively 'kill-line))
18990 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
18991 (kill-region (point) (match-beginning 1))
18992 (org-set-tags nil t))
18993 (t (kill-region (point) (point-at-eol)))))
18995 (define-key org-mode-map "\C-k" 'org-kill-line)
18997 (defun org-yank (&optional arg)
18998 "Yank. If the kill is a subtree, treat it specially.
18999 This command will look at the current kill and check if is a single
19000 subtree, or a series of subtrees[1]. If it passes the test, and if the
19001 cursor is at the beginning of a line or after the stars of a currently
19002 empty headline, then the yank is handled specially. How exactly depends
19003 on the value of the following variables, both set by default.
19005 org-yank-folded-subtrees
19006 When set, the subtree(s) will be folded after insertion, but only
19007 if doing so would now swallow text after the yanked text.
19009 org-yank-adjusted-subtrees
19010 When set, the subtree will be promoted or demoted in order to
19011 fit into the local outline tree structure, which means that the level
19012 will be adjusted so that it becomes the smaller one of the two
19013 *visible* surrounding headings.
19015 Any prefix to this command will cause `yank' to be called directly with
19016 no special treatment. In particular, a simple \\[universal-argument] prefix \
19018 plainly yank the text as it is.
19020 \[1] The test checks if the first non-white line is a heading
19021 and if there are no other headings with fewer stars."
19023 (org-yank-generic 'yank arg))
19025 (defun org-yank-generic (command arg)
19026 "Perform some yank-like command.
19028 This function implements the behavior described in the `org-yank'
19029 documentation. However, it has been generalized to work for any
19030 interactive command with similar behavior."
19032 ;; pretend to be command COMMAND
19033 (setq this-command command)
19036 (call-interactively command)
19038 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
19039 (and (org-kill-is-subtree-p)
19041 (and (looking-at "[ \t]*$")
19044 (buffer-substring (point-at-bol) (point)))))))
19047 ((and subtreep org-yank-folded-subtrees)
19048 (let ((beg (point))
19050 (if (and subtreep org-yank-adjusted-subtrees)
19051 (org-paste-subtree nil nil 'for-yank)
19052 (call-interactively command))
19056 (when (and (bolp) subtreep
19057 (not (setq swallowp
19058 (org-yank-folding-would-swallow-text beg end))))
19059 (or (looking-at outline-regexp)
19060 (re-search-forward (concat "^" outline-regexp) end t))
19061 (while (and (< (point) end) (looking-at outline-regexp))
19063 (org-cycle-show-empty-lines 'folded)
19064 (condition-case nil
19065 (outline-forward-same-level 1)
19066 (error (goto-char end)))))
19069 "Inserted text not folded because that would swallow text"))
19072 (skip-chars-forward " \t\n\r")
19073 (beginning-of-line 1)
19074 (push-mark beg 'nomsg)))
19075 ((and subtreep org-yank-adjusted-subtrees)
19076 (let ((beg (point-at-bol)))
19077 (org-paste-subtree nil nil 'for-yank)
19078 (push-mark beg 'nomsg)))
19080 (call-interactively command))))))
19082 (defun org-yank-folding-would-swallow-text (beg end)
19083 "Would hide-subtree at BEG swallow any text after END?"
19087 (when (or (looking-at outline-regexp)
19088 (re-search-forward (concat "^" outline-regexp) end t))
19089 (setq level (org-outline-level)))
19091 (skip-chars-forward " \t\r\n\v\f")
19093 (and (bolp) (looking-at org-outline-regexp)
19094 (<= (org-outline-level) level)))
19095 nil ; Nothing would be swallowed
19096 t)))) ; something would swallow
19098 (define-key org-mode-map "\C-y" 'org-yank)
19100 (defun org-invisible-p ()
19101 "Check if point is at a character currently not visible."
19102 ;; Early versions of noutline don't have `outline-invisible-p'.
19103 (if (fboundp 'outline-invisible-p)
19104 (outline-invisible-p)
19105 (get-char-property (point) 'invisible)))
19107 (defun org-truely-invisible-p ()
19108 "Check if point is at a character currently not visible.
19109 This version does not only check the character property, but also
19111 ;; Early versions of noutline don't have `outline-invisible-p'.
19112 (if (org-bound-and-true-p visible-mode)
19114 (if (fboundp 'outline-invisible-p)
19115 (outline-invisible-p)
19116 (get-char-property (point) 'invisible))))
19118 (defun org-invisible-p2 ()
19119 "Check if point is at a character currently not visible."
19121 (if (and (eolp) (not (bobp))) (backward-char 1))
19122 ;; Early versions of noutline don't have `outline-invisible-p'.
19123 (if (fboundp 'outline-invisible-p)
19124 (outline-invisible-p)
19125 (get-char-property (point) 'invisible))))
19127 (defun org-back-to-heading (&optional invisible-ok)
19128 "Call `outline-back-to-heading', but provide a better error message."
19129 (condition-case nil
19130 (outline-back-to-heading invisible-ok)
19131 (error (error "Before first headline at position %d in buffer %s"
19132 (point) (current-buffer)))))
19134 (defun org-beginning-of-defun ()
19135 "Go to the beginning of the subtree, i.e. back to the heading."
19136 (org-back-to-heading))
19137 (defun org-end-of-defun ()
19138 "Go to the end of the subtree."
19139 (org-end-of-subtree nil t))
19141 (defun org-before-first-heading-p ()
19142 "Before first heading?"
19144 (null (re-search-backward "^\\*+ " nil t))))
19146 (defun org-on-heading-p (&optional ignored)
19147 (outline-on-heading-p t))
19148 (defun org-at-heading-p (&optional ignored)
19149 (outline-on-heading-p t))
19151 (defun org-point-at-end-of-empty-headline ()
19152 "If point is at the end of an empty headline, return t, else nil.
19153 If the heading only contains a TODO keyword, it is still still considered
19155 (and (looking-at "[ \t]*$")
19157 (beginning-of-line 1)
19158 (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
19160 (defun org-at-heading-or-item-p ()
19161 (or (org-on-heading-p) (org-at-item-p)))
19163 (defun org-on-target-p ()
19164 (or (org-in-regexp org-radio-target-regexp)
19165 (org-in-regexp org-target-regexp)))
19167 (defun org-up-heading-all (arg)
19168 "Move to the heading line of which the present line is a subheading.
19169 This function considers both visible and invisible heading lines.
19170 With argument, move up ARG levels."
19171 (if (fboundp 'outline-up-heading-all)
19172 (outline-up-heading-all arg) ; emacs 21 version of outline.el
19173 (outline-up-heading arg t))) ; emacs 22 version of outline.el
19175 (defun org-up-heading-safe ()
19176 "Move to the heading line of which the present line is a subheading.
19177 This version will not throw an error. It will return the level of the
19178 headline found, or nil if no higher level is found.
19180 Also, this function will be a lot faster than `outline-up-heading',
19181 because it relies on stars being the outline starters. This can really
19182 make a significant difference in outlines with very many siblings."
19183 (let (start-level re)
19184 (org-back-to-heading t)
19185 (setq start-level (funcall outline-level))
19186 (if (equal start-level 1)
19188 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
19189 (if (re-search-backward re nil t)
19190 (funcall outline-level)))))
19192 (defun org-first-sibling-p ()
19193 "Is this heading the first child of its parents?"
19195 (let ((re (concat "^" outline-regexp))
19197 (unless (org-at-heading-p t)
19198 (error "Not at a heading"))
19199 (setq level (funcall outline-level))
19201 (if (not (re-search-backward re nil t))
19203 (setq l (funcall outline-level))
19206 (defun org-goto-sibling (&optional previous)
19207 "Goto the next sibling, even if it is invisible.
19208 When PREVIOUS is set, go to the previous sibling instead. Returns t
19209 when a sibling was found. When none is found, return nil and don't
19211 (let ((fun (if previous 're-search-backward 're-search-forward))
19213 (re (concat "^" outline-regexp))
19215 (when (condition-case nil (org-back-to-heading t) (error nil))
19216 (setq level (funcall outline-level))
19218 (or previous (forward-char 1))
19219 (while (funcall fun re nil t)
19220 (setq l (funcall outline-level))
19221 (when (< l level) (goto-char pos) (throw 'exit nil))
19222 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
19226 (defun org-show-siblings ()
19227 "Show all siblings of the current headline."
19229 (while (org-goto-sibling) (org-flag-heading nil)))
19231 (while (org-goto-sibling 'previous)
19232 (org-flag-heading nil))))
19234 (defun org-goto-first-child ()
19235 "Goto the first child, even if it is invisible.
19236 Return t when a child was found. Otherwise don't move point and
19238 (let (level (pos (point)) (re (concat "^" outline-regexp)))
19239 (when (condition-case nil (org-back-to-heading t) (error nil))
19240 (setq level (outline-level))
19242 (if (and (re-search-forward re nil t) (> (outline-level) level))
19243 (progn (goto-char (match-beginning 0)) t)
19244 (goto-char pos) nil))))
19246 (defun org-show-hidden-entry ()
19247 "Show an entry where even the heading is hidden."
19251 (defun org-flag-heading (flag &optional entry)
19252 "Flag the current heading. FLAG non-nil means make invisible.
19253 When ENTRY is non-nil, show the entire entry."
19255 (org-back-to-heading t)
19256 ;; Check if we should show the entire entry
19261 (and (outline-next-heading)
19262 (org-flag-heading nil))))
19263 (outline-flag-region (max (point-min) (1- (point)))
19264 (save-excursion (outline-end-of-heading) (point))
19267 (defun org-get-next-sibling ()
19268 "Move to next heading of the same level, and return point.
19269 If there is no such heading, return nil.
19270 This is like outline-next-sibling, but invisible headings are ok."
19271 (let ((level (funcall outline-level)))
19272 (outline-next-heading)
19273 (while (and (not (eobp)) (> (funcall outline-level) level))
19274 (outline-next-heading))
19275 (if (or (eobp) (< (funcall outline-level) level))
19279 (defun org-get-last-sibling ()
19280 "Move to previous heading of the same level, and return point.
19281 If there is no such heading, return nil."
19282 (let ((opoint (point))
19283 (level (funcall outline-level)))
19284 (outline-previous-heading)
19285 (when (and (/= (point) opoint) (outline-on-heading-p t))
19286 (while (and (> (funcall outline-level) level)
19288 (outline-previous-heading))
19289 (if (< (funcall outline-level) level)
19293 (defun org-end-of-subtree (&optional invisible-OK to-heading)
19294 ;; This contains an exact copy of the original function, but it uses
19295 ;; `org-back-to-heading', to make it work also in invisible
19296 ;; trees. And is uses an invisible-OK argument.
19297 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
19298 ;; Furthermore, when used inside Org, finding the end of a large subtree
19299 ;; with many children and grandchildren etc, this can be much faster
19300 ;; than the outline version.
19301 (org-back-to-heading invisible-OK)
19303 (level (funcall outline-level)))
19304 (if (and (org-mode-p) (< level 1000))
19305 ;; A true heading (not a plain list item), in Org-mode
19306 ;; This means we can easily find the end by looking
19307 ;; only for the right number of stars. Using a regexp to do
19308 ;; this is so much faster than using a Lisp loop.
19309 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
19311 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
19312 ;; something else, do it the slow way
19313 (while (and (not (eobp))
19314 (or first (> (funcall outline-level) level)))
19316 (outline-next-heading)))
19318 (if (memq (preceding-char) '(?\n ?\^M))
19320 ;; Go to end of line before heading
19322 (if (memq (preceding-char) '(?\n ?\^M))
19323 ;; leave blank line before heading
19324 (forward-char -1))))))
19327 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
19328 "Use Org version in org-mode, for dramatic speed-up."
19329 (if (eq major-mode 'org-mode)
19331 (org-end-of-subtree nil t)
19332 (unless (eobp) (backward-char 1)))
19335 (defun org-forward-same-level (arg &optional invisible-ok)
19336 "Move forward to the arg'th subheading at same level as this one.
19337 Stop at the first and last subheadings of a superior heading.
19338 Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
19339 it wil also look at invisible ones."
19341 (org-back-to-heading invisible-ok)
19343 (let* ((level (- (match-end 0) (match-beginning 0) 1))
19344 (re (format "^\\*\\{1,%d\\} " level))
19348 (while (and (re-search-forward re nil 'move)
19349 (setq l (- (match-end 0) (match-beginning 0) 1))
19352 (progn (backward-char 1) (org-invisible-p)))
19353 (if (< l level) (setq arg 1)))
19354 (setq arg (1- arg)))
19355 (beginning-of-line 1)))
19357 (defun org-backward-same-level (arg &optional invisible-ok)
19358 "Move backward to the arg'th subheading at same level as this one.
19359 Stop at the first and last subheadings of a superior heading."
19361 (org-back-to-heading)
19363 (let* ((level (- (match-end 0) (match-beginning 0) 1))
19364 (re (format "^\\*\\{1,%d\\} " level))
19367 (while (and (re-search-backward re nil 'move)
19368 (setq l (- (match-end 0) (match-beginning 0) 1))
19372 (if (< l level) (setq arg 1)))
19373 (setq arg (1- arg)))))
19375 (defun org-show-subtree ()
19376 "Show everything after this heading at deeper levels."
19377 (outline-flag-region
19380 (org-end-of-subtree t t))
19383 (defun org-show-entry ()
19384 "Show the body directly following this heading.
19385 Show the heading too, if it is currently invisible."
19388 (condition-case nil
19390 (org-back-to-heading t)
19391 (outline-flag-region
19392 (max (point-min) (1- (point)))
19394 (if (re-search-forward
19395 (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
19396 (match-beginning 1)
19399 (org-cycle-hide-drawers 'children))
19402 (defun org-make-options-regexp (kwds &optional extra)
19403 "Make a regular expression for keyword lines."
19407 (mapconcat 'regexp-quote kwds "\\|")
19408 (if extra (concat "\\|" extra))
19412 ;; Make isearch reveal the necessary context
19413 (defun org-isearch-end ()
19414 "Reveal context after isearch exits."
19415 (when isearch-success ; only if search was successful
19416 (if (featurep 'xemacs)
19417 ;; Under XEmacs, the hook is run in the correct place,
19418 ;; we directly show the context.
19419 (org-show-context 'isearch)
19420 ;; In Emacs the hook runs *before* restoring the overlays.
19421 ;; So we have to use a one-time post-command-hook to do this.
19422 ;; (Emacs 22 has a special variable, see function `org-mode')
19423 (unless (and (boundp 'isearch-mode-end-hook-quit)
19424 isearch-mode-end-hook-quit)
19425 ;; Only when the isearch was not quitted.
19426 (org-add-hook 'post-command-hook 'org-isearch-post-command
19427 'append 'local)))))
19429 (defun org-isearch-post-command ()
19430 "Remove self from hook, and show context."
19431 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
19432 (org-show-context 'isearch))
19435 ;;;; Integration with and fixes for other packages
19439 (defvar org-imenu-markers nil
19440 "All markers currently used by Imenu.")
19441 (make-variable-buffer-local 'org-imenu-markers)
19443 (defun org-imenu-new-marker (&optional pos)
19444 "Return a new marker for use by Imenu, and remember the marker."
19445 (let ((m (make-marker)))
19446 (move-marker m (or pos (point)))
19447 (push m org-imenu-markers)
19450 (defun org-imenu-get-tree ()
19451 "Produce the index for Imenu."
19452 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
19453 (setq org-imenu-markers nil)
19454 (let* ((n org-imenu-depth)
19455 (re (concat "^" outline-regexp))
19456 (subs (make-vector (1+ n) nil))
19462 (goto-char (point-max))
19463 (while (re-search-backward re nil t)
19464 (setq level (org-reduced-level (funcall outline-level)))
19466 (looking-at org-complex-heading-regexp)
19467 (setq head (org-link-display-format
19468 (org-match-string-no-properties 4))
19469 m (org-imenu-new-marker))
19470 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
19471 (if (>= level last-level)
19472 (push (cons head m) (aref subs level))
19473 (push (cons head (aref subs (1+ level))) (aref subs level))
19474 (loop for i from (1+ level) to n do (aset subs i nil)))
19475 (setq last-level level)))))
19478 (eval-after-load "imenu"
19480 (add-hook 'imenu-after-jump-hook
19482 (if (eq major-mode 'org-mode)
19483 (org-show-context 'org-goto))))))
19485 (defun org-link-display-format (link)
19486 "Replace a link with either the description, or the link target
19487 if no description is present"
19489 (if (string-match org-bracket-link-analytic-regexp link)
19490 (replace-match (if (match-end 5)
19491 (match-string 5 link)
19492 (concat (match-string 1 link)
19493 (match-string 3 link)))
19497 ;; Speedbar support
19499 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
19500 "Overlay marking the agenda restriction line in speedbar.")
19501 (overlay-put org-speedbar-restriction-lock-overlay
19502 'face 'org-agenda-restriction-lock)
19503 (overlay-put org-speedbar-restriction-lock-overlay
19504 'help-echo "Agendas are currently limited to this item.")
19505 (org-detach-overlay org-speedbar-restriction-lock-overlay)
19507 (defun org-speedbar-set-agenda-restriction ()
19508 "Restrict future agenda commands to the location at point in speedbar.
19509 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
19511 (require 'org-agenda)
19512 (let (p m tp np dir txt)
19514 ((setq p (text-property-any (point-at-bol) (point-at-eol)
19516 (setq m (get-text-property p 'org-imenu-marker))
19517 (with-current-buffer (marker-buffer m)
19519 (org-agenda-set-restriction-lock 'subtree)))
19520 ((setq p (text-property-any (point-at-bol) (point-at-eol)
19521 'speedbar-function 'speedbar-find-file))
19522 (setq tp (previous-single-property-change
19523 (1+ p) 'speedbar-function)
19524 np (next-single-property-change
19525 tp 'speedbar-function)
19526 dir (speedbar-line-directory)
19527 txt (buffer-substring-no-properties (or tp (point-min))
19528 (or np (point-max))))
19529 (with-current-buffer (find-file-noselect
19530 (let ((default-directory dir))
19531 (expand-file-name txt)))
19532 (unless (org-mode-p)
19533 (error "Cannot restrict to non-Org-mode file"))
19534 (org-agenda-set-restriction-lock 'file)))
19535 (t (error "Don't know how to restrict Org-mode's agenda")))
19536 (move-overlay org-speedbar-restriction-lock-overlay
19537 (point-at-bol) (point-at-eol))
19538 (setq current-prefix-arg nil)
19539 (org-agenda-maybe-redo)))
19541 (eval-after-load "speedbar"
19543 (speedbar-add-supported-extension ".org")
19544 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
19545 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
19546 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
19547 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
19548 (add-hook 'speedbar-visiting-tag-hook
19549 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
19551 ;;; Fixes and Hacks for problems with other packages
19553 ;; Make flyspell not check words in links, to not mess up our keymap
19554 (defun org-mode-flyspell-verify ()
19555 "Don't let flyspell put overlays at active buttons."
19556 (and (not (get-text-property (max (1- (point)) (point-min)) 'keymap))
19557 (not (get-text-property (max (1- (point)) (point-min)) 'org-no-flyspell))))
19559 (defun org-remove-flyspell-overlays-in (beg end)
19560 "Remove flyspell overlays in region."
19561 (and (org-bound-and-true-p flyspell-mode)
19562 (fboundp 'flyspell-delete-region-overlays)
19563 (flyspell-delete-region-overlays beg end))
19564 (add-text-properties beg end '(org-no-flyspell t)))
19566 ;; Make `bookmark-jump' shows the jump location if it was hidden.
19567 (eval-after-load "bookmark"
19568 '(if (boundp 'bookmark-after-jump-hook)
19569 ;; We can use the hook
19570 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
19571 ;; Hook not available, use advice
19572 (defadvice bookmark-jump (after org-make-visible activate)
19573 "Make the position visible."
19574 (org-bookmark-jump-unhide))))
19576 ;; Make sure saveplace shows the location if it was hidden
19577 (eval-after-load "saveplace"
19578 '(defadvice save-place-find-file-hook (after org-make-visible activate)
19579 "Make the position visible."
19580 (org-bookmark-jump-unhide)))
19582 ;; Make sure ecb shows the location if it was hidden
19583 (eval-after-load "ecb"
19584 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
19585 "Make hierarchy visible when jumping into location from ECB tree buffer."
19586 (if (eq major-mode 'org-mode)
19587 (org-show-context))))
19589 (defun org-bookmark-jump-unhide ()
19590 "Unhide the current position, to show the bookmark location."
19592 (or (org-invisible-p)
19593 (save-excursion (goto-char (max (point-min) (1- (point))))
19594 (org-invisible-p)))
19595 (org-show-context 'bookmark-jump)))
19597 ;; Make session.el ignore our circular variable
19598 (eval-after-load "session"
19599 '(add-to-list 'session-globals-exclude 'org-mark-ring))
19601 ;;;; Experimental code
19603 (defun org-closed-in-range ()
19604 "Sparse tree of items closed in a certain time range.
19605 Still experimental, may disappear in the future."
19607 ;; Get the time interval from the user.
19608 (let* ((time1 (org-float-time
19609 (org-read-date nil 'to-time nil "Starting date: ")))
19610 (time2 (org-float-time
19611 (org-read-date nil 'to-time nil "End date:")))
19612 ;; callback function
19613 (callback (lambda ()
19616 (apply 'encode-time
19617 (org-parse-time-string
19618 (match-string 1))))))
19619 ;; check if time in interval
19620 (and (>= time time1) (<= time time2))))))
19621 ;; make tree, check each match with the callback
19622 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
19628 (run-hooks 'org-load-hook)
19630 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
19632 ;;; org.el ends here