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-insert-mode-line-in-empty-file nil
475 "Non-nil means insert the first line setting Org-mode in empty files.
476 When the function `org-mode' is called interactively in an empty file, this
477 normally means that the file name does not automatically trigger Org-mode.
478 To ensure that the file will always be in Org-mode in the future, a
479 line enforcing Org-mode will be inserted into the buffer, if this option
484 (defcustom org-replace-disputed-keys nil
485 "Non-nil means use alternative key bindings for some keys.
486 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
487 These keys are also used by other packages like shift-selection-mode'
488 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
489 If you want to use Org-mode together with one of these other modes,
490 or more generally if you would like to move some Org-mode commands to
491 other keys, set this variable and configure the keys with the variable
494 This option is only relevant at load-time of Org-mode, and must be set
495 *before* org.el is loaded. Changing it requires a restart of Emacs to
500 (defcustom org-use-extra-keys nil
501 "Non-nil means use extra key sequence definitions for certain commands.
502 This happens automatically if you run XEmacs or if `window-system'
503 is nil. This variable lets you do the same manually. You must
504 set it before loading org.
506 Example: on Carbon Emacs 22 running graphically, with an external
507 keyboard on a Powerbook, the default way of setting M-left might
508 not work for either Alt or ESC. Setting this variable will make
513 (if (fboundp 'defvaralias
)
514 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
516 (defcustom org-disputed-keys
517 '(([(shift up
)] .
[(meta p
)])
518 ([(shift down
)] .
[(meta n
)])
519 ([(shift left
)] .
[(meta -
)])
520 ([(shift right
)] .
[(meta +)])
521 ([(control shift right
)] .
[(meta shift
+)])
522 ([(control shift left
)] .
[(meta shift -
)]))
523 "Keys for which Org-mode and other modes compete.
524 This is an alist, cars are the default keys, second element specifies
525 the alternative to use when `org-replace-disputed-keys' is t.
527 Keys can be specified in any syntax supported by `define-key'.
528 The value of this option takes effect only at Org-mode's startup,
529 therefore you'll have to restart Emacs to apply it after changing."
534 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
535 Or return the original if not disputed.
536 Also apply the translations defined in `org-xemacs-key-equivalents'."
537 (when org-replace-disputed-keys
538 (let* ((nkey (key-description key
))
539 (x (org-find-if (lambda (x)
540 (equal (key-description (car x
)) nkey
))
542 (setq key
(if x
(cdr x
) key
))))
543 (when (featurep 'xemacs
)
544 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
547 (defun org-find-if (predicate seq
)
550 (if (funcall predicate
(car seq
))
551 (throw 'exit
(car seq
))
554 (defun org-defkey (keymap key def
)
555 "Define a key, possibly translated, as returned by `org-key'."
556 (define-key keymap
(org-key key
) def
))
558 (defcustom org-ellipsis nil
559 "The ellipsis to use in the Org-mode outline.
560 When nil, just use the standard three dots. When a string, use that instead,
561 When a face, use the standard 3 dots, but with the specified face.
562 The change affects only Org-mode (which will then use its own display table).
563 Changing this requires executing `M-x org-mode' in a buffer to become
566 :type
'(choice (const :tag
"Default" nil
)
567 (face :tag
"Face" :value org-warning
)
568 (string :tag
"String" :value
"...#")))
570 (defvar org-display-table nil
571 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
573 (defgroup org-keywords nil
574 "Keywords in Org-mode."
578 (defcustom org-deadline-string
"DEADLINE:"
579 "String to mark deadline entries.
580 A deadline is this string, followed by a time stamp. Should be a word,
581 terminated by a colon. You can insert a schedule keyword and
582 a timestamp with \\[org-deadline].
583 Changes become only effective after restarting Emacs."
587 (defcustom org-scheduled-string
"SCHEDULED:"
588 "String to mark scheduled TODO entries.
589 A schedule 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-schedule].
592 Changes become only effective after restarting Emacs."
596 (defcustom org-closed-string
"CLOSED:"
597 "String used as the prefix for timestamps logging closing a TODO entry."
601 (defcustom org-clock-string
"CLOCK:"
602 "String used as prefix for timestamps clocking work hours on an item."
606 (defcustom org-comment-string
"COMMENT"
607 "Entries starting with this keyword will never be exported.
608 An entry can be toggled between COMMENT and normal with
609 \\[org-toggle-comment].
610 Changes become only effective after restarting Emacs."
614 (defcustom org-quote-string
"QUOTE"
615 "Entries starting with this keyword will be exported in fixed-width font.
616 Quoting applies only to the text in the entry following the headline, and does
617 not extend beyond the next headline, even if that is lower level.
618 An entry can be toggled between QUOTE and normal with
619 \\[org-toggle-fixed-width-section]."
623 (defconst org-repeat-re
624 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
625 "Regular expression for specifying repeated events.
626 After a match, group 1 contains the repeat expression.")
628 (defgroup org-structure nil
629 "Options concerning the general structure of Org-mode files."
633 (defgroup org-reveal-location nil
634 "Options about how to make context of a location visible."
635 :tag
"Org Reveal Location"
636 :group
'org-structure
)
638 (defconst org-context-choice
640 (const :tag
"Always" t
)
641 (const :tag
"Never" nil
)
642 (repeat :greedy t
:tag
"Individual contexts"
644 (choice :tag
"Context"
651 (const bookmark-jump
)
655 "Contexts for the reveal options.")
657 (defcustom org-show-hierarchy-above
'((default . t
))
658 "Non-nil means show full hierarchy when revealing a location.
659 Org-mode often shows locations in an org-mode file which might have
660 been invisible before. When this is set, the hierarchy of headings
661 above the exposed location is shown.
662 Turning this off for example for sparse trees makes them very compact.
663 Instead of t, this can also be an alist specifying this option for different
664 contexts. Valid contexts are
665 agenda when exposing an entry from the agenda
666 org-goto when using the command `org-goto' on key C-c C-j
667 occur-tree when using the command `org-occur' on key C-c /
668 tags-tree when constructing a sparse tree based on tags matches
669 link-search when exposing search matches associated with a link
670 mark-goto when exposing the jump goal of a mark
671 bookmark-jump when exposing a bookmark location
672 isearch when exiting from an incremental search
673 default default for all contexts not set explicitly"
674 :group
'org-reveal-location
675 :type org-context-choice
)
677 (defcustom org-show-following-heading
'((default . nil
))
678 "Non-nil means show following heading when revealing a location.
679 Org-mode often shows locations in an org-mode file which might have
680 been invisible before. When this is set, the heading following the
682 Turning this off for example for sparse trees makes them very compact,
683 but makes it harder to edit the location of the match. In such a case,
684 use the command \\[org-reveal] to show more context.
685 Instead of t, this can also be an alist specifying this option for different
686 contexts. See `org-show-hierarchy-above' for valid contexts."
687 :group
'org-reveal-location
688 :type org-context-choice
)
690 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
691 "Non-nil means show all sibling heading when revealing a location.
692 Org-mode often shows locations in an org-mode file which might have
693 been invisible before. When this is set, the sibling of the current entry
694 heading are all made visible. If `org-show-hierarchy-above' is t,
695 the same happens on each level of the hierarchy above the current entry.
697 By default this is on for the isearch context, off for all other contexts.
698 Turning this off for example for sparse trees makes them very compact,
699 but makes it harder to edit the location of the match. In such a case,
700 use the command \\[org-reveal] to show more context.
701 Instead of t, this can also be an alist specifying this option for different
702 contexts. See `org-show-hierarchy-above' for valid contexts."
703 :group
'org-reveal-location
704 :type org-context-choice
)
706 (defcustom org-show-entry-below
'((default . nil
))
707 "Non-nil means show the entry below a headline when revealing a location.
708 Org-mode often shows locations in an org-mode file which might have
709 been invisible before. When this is set, the text below the headline that is
710 exposed is also shown.
712 By default this is off for all contexts.
713 Instead of t, this can also be an alist specifying this option for different
714 contexts. See `org-show-hierarchy-above' for valid contexts."
715 :group
'org-reveal-location
716 :type org-context-choice
)
718 (defcustom org-indirect-buffer-display
'other-window
719 "How should indirect tree buffers be displayed?
720 This applies to indirect buffers created with the commands
721 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
723 current-window Display in the current window
724 other-window Just display in another window.
725 dedicated-frame Create one new frame, and re-use it each time.
726 new-frame Make a new frame each time. Note that in this case
727 previously-made indirect buffers are kept, and you need to
728 kill these buffers yourself."
729 :group
'org-structure
730 :group
'org-agenda-windows
732 (const :tag
"In current window" current-window
)
733 (const :tag
"In current frame, other window" other-window
)
734 (const :tag
"Each time a new frame" new-frame
)
735 (const :tag
"One dedicated frame" dedicated-frame
)))
737 (defcustom org-use-speed-commands nil
738 "Non-nil means activate single letter commands at beginning of a headline.
739 This may also be a function to test for appropriate locations where speed
740 commands should be active."
741 :group
'org-structure
743 (const :tag
"Never" nil
)
744 (const :tag
"At beginning of headline stars" t
)
747 (defcustom org-speed-commands-user nil
748 "Alist of additional speed commands.
749 This list will be checked before `org-speed-commands-default'
750 when the variable `org-use-speed-commands' is non-nil
751 and when the cursor is at the beginning of a headline.
752 The car if each entry is a string with a single letter, which must
753 be assigned to `self-insert-command' in the global map.
754 The cdr is either a command to be called interactively, a function
755 to be called, or a form to be evaluated.
756 An entry that is just a list with a single string will be interpreted
757 as a descriptive headline that will be added when listing the speed
758 commands in the Help buffer using the `?' speed command."
759 :group
'org-structure
760 :type
'(repeat :value
("k" . ignore
)
761 (choice :value
("k" . ignore
)
762 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
763 (cons :tag
"Letter and Command"
764 (string :tag
"Command letter")
769 (defgroup org-cycle nil
770 "Options concerning visibility cycling in Org-mode."
772 :group
'org-structure
)
774 (defcustom org-cycle-skip-children-state-if-no-children t
775 "Non-nil means skip CHILDREN state in entries that don't have any."
779 (defcustom org-cycle-max-level nil
780 "Maximum level which should still be subject to visibility cycling.
781 Levels higher than this will, for cycling, be treated as text, not a headline.
782 When `org-odd-levels-only' is set, a value of N in this variable actually
783 means 2N-1 stars as the limiting headline.
784 When nil, cycle all levels.
785 Note that the limiting level of cycling is also influenced by
786 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
787 `org-inlinetask-min-level' is, cycling will be limited to levels one less
791 (const :tag
"No limit" nil
)
792 (integer :tag
"Maximum level")))
794 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK")
795 "Names of drawers. Drawers are not opened by cycling on the headline above.
796 Drawers only open with a TAB on the drawer line itself. A drawer looks like
801 The drawer \"PROPERTIES\" is special for capturing properties through
804 Drawers can be defined on the per-file basis with a line like:
806 #+DRAWERS: HIDDEN STATE PROPERTIES"
807 :group
'org-structure
809 :type
'(repeat (string :tag
"Drawer Name")))
811 (defcustom org-hide-block-startup nil
812 "Non-nil means entering Org-mode will fold all blocks.
813 This can also be set in on a per-file basis with
815 #+STARTUP: hideblocks
816 #+STARTUP: showblocks"
821 (defcustom org-cycle-global-at-bob nil
822 "Cycle globally if cursor is at beginning of buffer and not at a headline.
823 This makes it possible to do global cycling without having to use S-TAB or
824 \\[universal-argument] TAB. For this special case to work, the first line \
826 must not be a headline - it may be empty or some other text. When used in
827 this way, `org-cycle-hook' is disables temporarily, to make sure the
828 cursor stays at the beginning of the buffer.
829 When this option is nil, don't do anything special at the beginning
834 (defcustom org-cycle-level-after-item
/entry-creation t
835 "Non-nil means cycle entry level or item indentation in new empty entries.
837 When the cursor is at the end of an empty headline, i.e with only stars
838 and maybe a TODO keyword, TAB will then switch the entry to become a child,
839 and then all possible ancestor states, before returning to the original state.
840 This makes data entry extremely fast: M-RET to create a new headline,
841 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
843 When the cursor is at the end of an empty plain list item, one TAB will
844 make it a subitem, two or more tabs will back up to make this an item
845 higher up in the item hierarchy."
849 (defcustom org-cycle-emulate-tab t
850 "Where should `org-cycle' emulate TAB.
852 white Only in completely white lines
853 whitestart Only at the beginning of lines, before the first non-white char
854 t Everywhere except in headlines
855 exc-hl-bol Everywhere except at the start of a headline
856 If TAB is used in a place where it does not emulate TAB, the current subtree
857 visibility is cycled."
859 :type
'(choice (const :tag
"Never" nil
)
860 (const :tag
"Only in completely white lines" white
)
861 (const :tag
"Before first char in a line" whitestart
)
862 (const :tag
"Everywhere except in headlines" t
)
863 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
866 (defcustom org-cycle-separator-lines
2
867 "Number of empty lines needed to keep an empty line between collapsed trees.
868 If you leave an empty line between the end of a subtree and the following
869 headline, this empty line is hidden when the subtree is folded.
870 Org-mode will leave (exactly) one empty line visible if the number of
871 empty lines is equal or larger to the number given in this variable.
872 So the default 2 means at least 2 empty lines after the end of a subtree
873 are needed to produce free space between a collapsed subtree and the
876 If the number is negative, and the number of empty lines is at least -N,
877 all empty lines are shown.
879 Special case: when 0, never leave empty lines in collapsed view."
882 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
884 (defcustom org-pre-cycle-hook nil
885 "Hook that is run before visibility cycling is happening.
886 The function(s) in this hook must accept a single argument which indicates
887 the new state that will be set right after running this hook. The
888 argument is a symbol. Before a global state change, it can have the values
889 `overview', `content', or `all'. Before a local state change, it can have
890 the values `folded', `children', or `subtree'."
894 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
895 org-cycle-hide-drawers
896 org-cycle-show-empty-lines
897 org-optimize-window-after-visibility-change
)
898 "Hook that is run after `org-cycle' has changed the buffer visibility.
899 The function(s) in this hook must accept a single argument which indicates
900 the new state that was set by the most recent `org-cycle' command. The
901 argument is a symbol. After a global state change, it can have the values
902 `overview', `content', or `all'. After a local state change, it can have
903 the values `folded', `children', or `subtree'."
907 (defgroup org-edit-structure nil
908 "Options concerning structure editing in Org-mode."
909 :tag
"Org Edit Structure"
910 :group
'org-structure
)
912 (defcustom org-odd-levels-only nil
913 "Non-nil means skip even levels and only use odd levels for the outline.
914 This has the effect that two stars are being added/taken away in
915 promotion/demotion commands. It also influences how levels are
916 handled by the exporters.
917 Changing it requires restart of `font-lock-mode' to become effective
918 for fontification also in regions already fontified.
919 You may also set this on a per-file basis by adding one of the following
924 :group
'org-edit-structure
925 :group
'org-appearance
928 (defcustom org-adapt-indentation t
929 "Non-nil means adapt indentation to outline node level.
931 When this variable is set, Org assumes that you write outlines by
932 indenting text in each node to align with the headline (after the stars).
933 The following issues are influenced by this variable:
935 - When this is set and the *entire* text in an entry is indented, the
936 indentation is increased by one space in a demotion command, and
937 decreased by one in a promotion command. If any line in the entry
938 body starts with text at column 0, indentation is not changed at all.
940 - Property drawers and planning information is inserted indented when
941 this variable s set. When nil, they will not be indented.
943 - TAB indents a line relative to context. The lines below a headline
944 will be indented when this variable is set.
946 Note that this is all about true indentation, by adding and removing
947 space characters. See also `org-indent.el' which does level-dependent
948 indentation in a virtual way, i.e. at display time in Emacs."
949 :group
'org-edit-structure
952 (defcustom org-special-ctrl-a
/e nil
953 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
955 When t, `C-a' will bring back the cursor to the beginning of the
956 headline text, i.e. after the stars and after a possible TODO keyword.
957 In an item, this will be the position after the bullet.
958 When the cursor is already at that position, another `C-a' will bring
959 it to the beginning of the line.
961 `C-e' will jump to the end of the headline, ignoring the presence of tags
962 in the headline. A second `C-e' will then jump to the true end of the
963 line, after any tags. This also means that, when this variable is
964 non-nil, `C-e' also will never jump beyond the end of the heading of a
965 folded section, i.e. not after the ellipses.
967 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
968 going to the true line boundary first. Only a directly following, identical
969 keypress will bring the cursor to the special positions.
971 This may also be a cons cell where the behavior for `C-a' and `C-e' is
973 :group
'org-edit-structure
975 (const :tag
"off" nil
)
976 (const :tag
"on: after stars/bullet and before tags first" t
)
977 (const :tag
"reversed: true line boundary first" reversed
)
978 (cons :tag
"Set C-a and C-e separately"
979 (choice :tag
"Special C-a"
980 (const :tag
"off" nil
)
981 (const :tag
"on: after stars/bullet first" t
)
982 (const :tag
"reversed: before stars/bullet first" reversed
))
983 (choice :tag
"Special C-e"
984 (const :tag
"off" nil
)
985 (const :tag
"on: before tags first" t
)
986 (const :tag
"reversed: after tags first" reversed
)))))
987 (if (fboundp 'defvaralias
)
988 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
990 (defcustom org-special-ctrl-k nil
991 "Non-nil means `C-k' will behave specially in headlines.
992 When nil, `C-k' will call the default `kill-line' command.
993 When t, the following will happen while the cursor is in the headline:
995 - When the cursor is at the beginning of a headline, kill the entire
996 line and possible the folded subtree below the line.
997 - When in the middle of the headline text, kill the headline up to the tags.
998 - When after the headline text, kill the tags."
999 :group
'org-edit-structure
1002 (defcustom org-ctrl-k-protect-subtree nil
1003 "Non-nil means, do not delete a hidden subtree with C-k.
1004 When set to the symbol `error', simply throw an error when C-k is
1005 used to kill (part-of) a headline that has hidden text behind it.
1006 Any other non-nil value will result in a query to the user, if it is
1007 OK to kill that hidden subtree. When nil, kill without remorse."
1008 :group
'org-edit-structure
1010 (const :tag
"Do not protect hidden subtrees" nil
)
1011 (const :tag
"Protect hidden subtrees with a security query" t
)
1012 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1014 (defcustom org-yank-folded-subtrees t
1015 "Non-nil means when yanking subtrees, fold them.
1016 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1017 it starts with a heading and all other headings in it are either children
1018 or siblings, then fold all the subtrees. However, do this only if no
1019 text after the yank would be swallowed into a folded tree by this action."
1020 :group
'org-edit-structure
1023 (defcustom org-yank-adjusted-subtrees nil
1024 "Non-nil means when yanking subtrees, adjust the level.
1025 With this setting, `org-paste-subtree' is used to insert the subtree, see
1026 this function for details."
1027 :group
'org-edit-structure
1030 (defcustom org-M-RET-may-split-line
'((default . t
))
1031 "Non-nil means M-RET will split the line at the cursor position.
1032 When nil, it will go to the end of the line before making a
1034 You may also set this option in a different way for different
1035 contexts. Valid contexts are:
1037 headline when creating a new headline
1038 item when creating a new item
1039 table in a table field
1040 default the value to be used for all contexts not explicitly
1042 :group
'org-structure
1045 (const :tag
"Always" t
)
1046 (const :tag
"Never" nil
)
1047 (repeat :greedy t
:tag
"Individual contexts"
1049 (choice :tag
"Context"
1057 (defcustom org-insert-heading-respect-content nil
1058 "Non-nil means insert new headings after the current subtree.
1059 When nil, the new heading is created directly after the current line.
1060 The commands \\[org-insert-heading-respect-content] and
1061 \\[org-insert-todo-heading-respect-content] turn this variable on
1062 for the duration of the command."
1063 :group
'org-structure
1066 (defcustom org-blank-before-new-entry
'((heading . auto
)
1067 (plain-list-item . auto
))
1068 "Should `org-insert-heading' leave a blank line before new heading/item?
1069 The value is an alist, with `heading' and `plain-list-item' as car,
1070 and a boolean flag as cdr. For plain lists, if the variable
1071 `org-empty-line-terminates-plain-lists' is set, the setting here
1072 is ignored and no empty line is inserted, to keep the list in tact."
1073 :group
'org-edit-structure
1075 (cons (const heading
)
1076 (choice (const :tag
"Never" nil
)
1077 (const :tag
"Always" t
)
1078 (const :tag
"Auto" auto
)))
1079 (cons (const plain-list-item
)
1080 (choice (const :tag
"Never" nil
)
1081 (const :tag
"Always" t
)
1082 (const :tag
"Auto" auto
)))))
1084 (defcustom org-insert-heading-hook nil
1085 "Hook being run after inserting a new heading."
1086 :group
'org-edit-structure
1089 (defcustom org-enable-fixed-width-editor t
1090 "Non-nil means lines starting with \":\" are treated as fixed-width.
1091 This currently only means they are never auto-wrapped.
1092 When nil, such lines will be treated like ordinary lines.
1093 See also the QUOTE keyword."
1094 :group
'org-edit-structure
1097 (defcustom org-goto-auto-isearch t
1098 "Non-nil means typing characters in `org-goto' starts incremental search."
1099 :group
'org-edit-structure
1102 (defgroup org-sparse-trees nil
1103 "Options concerning sparse trees in Org-mode."
1104 :tag
"Org Sparse Trees"
1105 :group
'org-structure
)
1107 (defcustom org-highlight-sparse-tree-matches t
1108 "Non-nil means highlight all matches that define a sparse tree.
1109 The highlights will automatically disappear the next time the buffer is
1110 changed by an edit command."
1111 :group
'org-sparse-trees
1114 (defcustom org-remove-highlights-with-change t
1115 "Non-nil means any change to the buffer will remove temporary highlights.
1116 Such highlights are created by `org-occur' and `org-clock-display'.
1117 When nil, `C-c C-c needs to be used to get rid of the highlights.
1118 The highlights created by `org-preview-latex-fragment' always need
1119 `C-c C-c' to be removed."
1120 :group
'org-sparse-trees
1125 (defcustom org-occur-hook
'(org-first-headline-recenter)
1126 "Hook that is run after `org-occur' has constructed a sparse tree.
1127 This can be used to recenter the window to show as much of the structure
1129 :group
'org-sparse-trees
1132 (defgroup org-imenu-and-speedbar nil
1133 "Options concerning imenu and speedbar in Org-mode."
1134 :tag
"Org Imenu and Speedbar"
1135 :group
'org-structure
)
1137 (defcustom org-imenu-depth
2
1138 "The maximum level for Imenu access to Org-mode headlines.
1139 This also applied for speedbar access."
1140 :group
'org-imenu-and-speedbar
1143 (defgroup org-table nil
1144 "Options concerning tables in Org-mode."
1148 (defcustom org-enable-table-editor
'optimized
1149 "Non-nil means lines starting with \"|\" are handled by the table editor.
1150 When nil, such lines will be treated like ordinary lines.
1152 When equal to the symbol `optimized', the table editor will be optimized to
1154 - Automatic overwrite mode in front of whitespace in table fields.
1155 This makes the structure of the table stay in tact as long as the edited
1156 field does not exceed the column width.
1157 - Minimize the number of realigns. Normally, the table is aligned each time
1158 TAB or RET are pressed to move to another field. With optimization this
1159 happens only if changes to a field might have changed the column width.
1160 Optimization requires replacing the functions `self-insert-command',
1161 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1162 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1163 very good at guessing when a re-align will be necessary, but you can always
1164 force one with \\[org-ctrl-c-ctrl-c].
1166 If you would like to use the optimized version in Org-mode, but the
1167 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1169 This variable can be used to turn on and off the table editor during a session,
1170 but in order to toggle optimization, a restart is required.
1172 See also the variable `org-table-auto-blank-field'."
1175 (const :tag
"off" nil
)
1177 (const :tag
"on, optimized" optimized
)))
1179 (defcustom org-self-insert-cluster-for-undo t
1180 "Non-nil means cluster self-insert commands for undo when possible.
1181 If this is set, then, like in the Emacs command loop, 20 consecutive
1182 characters will be undone together.
1183 This is configurable, because there is some impact on typing performance."
1187 (defcustom org-table-tab-recognizes-table.el t
1188 "Non-nil means TAB will automatically notice a table.el table.
1189 When it sees such a table, it moves point into it and - if necessary -
1190 calls `table-recognize-table'."
1191 :group
'org-table-editing
1194 (defgroup org-link nil
1195 "Options concerning links in Org-mode."
1199 (defvar org-link-abbrev-alist-local nil
1200 "Buffer-local version of `org-link-abbrev-alist', which see.
1201 The value of this is taken from the #+LINK lines.")
1202 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1204 (defcustom org-link-abbrev-alist nil
1205 "Alist of link abbreviations.
1206 The car of each element is a string, to be replaced at the start of a link.
1207 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1208 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1210 [[linkkey:tag][description]]
1212 The 'linkkey' must be a word word, starting with a letter, followed
1213 by letters, numbers, '-' or '_'.
1215 If REPLACE is a string, the tag will simply be appended to create the link.
1216 If the string contains \"%s\", the tag will be inserted there. Alternatively,
1217 the placeholder \"%h\" will cause a url-encoded version of the tag to
1218 be inserted at that point (see the function `url-hexify-string').
1220 REPLACE may also be a function that will be called with the tag as the
1221 only argument to create the link, which should be returned as a string.
1223 See the manual for examples."
1227 (string :tag
"Protocol")
1229 (string :tag
"Format")
1232 (defcustom org-descriptive-links t
1233 "Non-nil means hide link part and only show description of bracket links.
1234 Bracket links are like [[link][description]]. This variable sets the initial
1235 state in new org-mode buffers. The setting can then be toggled on a
1236 per-buffer basis from the Org->Hyperlinks menu."
1240 (defcustom org-link-file-path-type
'adaptive
1241 "How the path name in file links should be stored.
1244 relative Relative to the current directory, i.e. the directory of the file
1245 into which the link is being inserted.
1246 absolute Absolute path, if possible with ~ for home directory.
1247 noabbrev Absolute path, no abbreviation of home directory.
1248 adaptive Use relative path for files in the current directory and sub-
1249 directories of it. For other files, use an absolute path."
1257 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1258 "Types of links that should be activated in Org-mode files.
1259 This is a list of symbols, each leading to the activation of a certain link
1260 type. In principle, it does not hurt to turn on most link types - there may
1261 be a small gain when turning off unused link types. The types are:
1263 bracket The recommended [[link][description]] or [[link]] links with hiding.
1264 angular Links in angular brackets that may contain whitespace like
1265 <bbdb:Carsten Dominik>.
1266 plain Plain links in normal text, no whitespace, like http://google.com.
1267 radio Text that is matched by a radio target, see manual for details.
1268 tag Tag settings in a headline (link to tag search).
1269 date Time stamps (link to calendar).
1270 footnote Footnote labels.
1272 Changing this variable requires a restart of Emacs to become effective."
1274 :type
'(set :greedy t
1275 (const :tag
"Double bracket links (new style)" bracket
)
1276 (const :tag
"Angular bracket links (old style)" angular
)
1277 (const :tag
"Plain text links" plain
)
1278 (const :tag
"Radio target matches" radio
)
1279 (const :tag
"Tags" tag
)
1280 (const :tag
"Timestamps" date
)
1281 (const :tag
"Footnotes" footnote
)))
1283 (defcustom org-make-link-description-function nil
1284 "Function to use to generate link descriptions from links.
1285 If nil the link location will be used. This function must take
1286 two parameters; the first is the link and the second the
1287 description `org-insert-link' has generated, and should return the
1288 description to use."
1292 (defgroup org-link-store nil
1293 "Options concerning storing links in Org-mode."
1294 :tag
"Org Store Link"
1297 (defcustom org-email-link-description-format
"Email %c: %.30s"
1298 "Format of the description part of a link to an email or usenet message.
1299 The following %-escapes will be replaced by corresponding information:
1301 %F full \"From\" field
1302 %f name, taken from \"From\" field, address if no name
1303 %T full \"To\" field
1304 %t first name in \"To\" field, address if no name
1305 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1306 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1310 You may use normal field width specification between the % and the letter.
1311 This is for example useful to limit the length of the subject.
1313 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1314 :group
'org-link-store
1317 (defcustom org-from-is-user-regexp
1319 (when (and user-mail-address
(not (string= user-mail-address
"")))
1320 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1321 (when (and user-full-name
(not (string= user-full-name
"")))
1322 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1323 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1324 "Regexp matched against the \"From:\" header of an email or usenet message.
1325 It should match if the message is from the user him/herself."
1326 :group
'org-link-store
1329 (defcustom org-link-to-org-use-id
'create-if-interactive-and-no-custom-id
1330 "Non-nil means storing a link to an Org file will use entry IDs.
1332 Note that before this variable is even considered, org-id must be loaded,
1333 so please customize `org-modules' and turn it on.
1335 The variable can have the following values:
1337 t Create an ID if needed to make a link to the current entry.
1339 create-if-interactive
1340 If `org-store-link' is called directly (interactively, as a user
1341 command), do create an ID to support the link. But when doing the
1342 job for remember, only use the ID if it already exists. The
1343 purpose of this setting is to avoid proliferation of unwanted
1344 IDs, just because you happen to be in an Org file when you
1345 call `org-remember' that automatically and preemptively
1346 creates a link. If you do want to get an ID link in a remember
1347 template to an entry not having an ID, create it first by
1348 explicitly creating a link to it, using `C-c C-l' first.
1350 create-if-interactive-and-no-custom-id
1351 Like create-if-interactive, but do not create an ID if there is
1352 a CUSTOM_ID property defined in the entry. This is the default.
1355 Use existing ID, do not create one.
1357 nil Never use an ID to make a link, instead link using a text search for
1359 :group
'org-link-store
1361 (const :tag
"Create ID to make link" t
)
1362 (const :tag
"Create if storing link interactively"
1363 create-if-interactive
)
1364 (const :tag
"Create if storing link interactively and no CUSTOM_ID is present"
1365 create-if-interactive-and-no-custom-id
)
1366 (const :tag
"Only use existing" use-existing
)
1367 (const :tag
"Do not use ID to create link" nil
)))
1369 (defcustom org-context-in-file-links t
1370 "Non-nil means file links from `org-store-link' contain context.
1371 A search string will be added to the file name with :: as separator and
1372 used to find the context when the link is activated by the command
1373 `org-open-at-point'.
1374 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1375 negates this setting for the duration of the command."
1376 :group
'org-link-store
1379 (defcustom org-keep-stored-link-after-insertion nil
1380 "Non-nil means keep link in list for entire session.
1382 The command `org-store-link' adds a link pointing to the current
1383 location to an internal list. These links accumulate during a session.
1384 The command `org-insert-link' can be used to insert links into any
1385 Org-mode file (offering completion for all stored links). When this
1386 option is nil, every link which has been inserted once using \\[org-insert-link]
1387 will be removed from the list, to make completing the unused links
1389 :group
'org-link-store
1392 (defgroup org-link-follow nil
1393 "Options concerning following links in Org-mode."
1394 :tag
"Org Follow Link"
1397 (defcustom org-link-translation-function nil
1398 "Function to translate links with different syntax to Org syntax.
1399 This can be used to translate links created for example by the Planner
1400 or emacs-wiki packages to Org syntax.
1401 The function must accept two parameters, a TYPE containing the link
1402 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1403 which is everything after the link protocol. It should return a cons
1404 with possibly modified values of type and path.
1405 Org contains a function for this, so if you set this variable to
1406 `org-translate-link-from-planner', you should be able follow many
1407 links created by planner."
1408 :group
'org-link-follow
1411 (defcustom org-follow-link-hook nil
1412 "Hook that is run after a link has been followed."
1413 :group
'org-link-follow
1416 (defcustom org-tab-follows-link nil
1417 "Non-nil means on links TAB will follow the link.
1418 Needs to be set before org.el is loaded.
1419 This really should not be used, it does not make sense, and the
1420 implementation is bad."
1421 :group
'org-link-follow
1424 (defcustom org-return-follows-link nil
1425 "Non-nil means on links RET will follow the link."
1426 :group
'org-link-follow
1429 (defcustom org-mouse-1-follows-link
1430 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1431 "Non-nil means mouse-1 on a link will follow the link.
1432 A longer mouse click will still set point. Does not work on XEmacs.
1433 Needs to be set before org.el is loaded."
1434 :group
'org-link-follow
1437 (defcustom org-mark-ring-length
4
1438 "Number of different positions to be recorded in the ring.
1439 Changing this requires a restart of Emacs to work correctly."
1440 :group
'org-link-follow
1443 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1444 "Non-nil means internal links in Org files must exactly match a headline.
1445 When nil, the link search tries to match a phrase will all words
1446 in the search text."
1447 :group
'org-link-follow
1449 (const :tag
"Use fuzy text search" nil
)
1450 (const :tag
"Match only exact headline" t
)
1451 (const :tag
"Match extact headline or query to create it"
1454 (defcustom org-link-frame-setup
1455 '((vm . vm-visit-folder-other-frame
)
1456 (gnus . org-gnus-no-new-news
)
1457 (file . find-file-other-window
)
1458 (wl . wl-other-frame
))
1459 "Setup the frame configuration for following links.
1460 When following a link with Emacs, it may often be useful to display
1461 this link in another window or frame. This variable can be used to
1462 set this up for the different types of links.
1465 `vm-visit-folder-other-frame'
1466 For Gnus, use any of
1469 `org-gnus-no-new-news'
1470 For FILE, use any of
1472 `find-file-other-window'
1473 `find-file-other-frame'
1474 For Wanderlust use any of
1477 For the calendar, use the variable `calendar-setup'.
1478 For BBDB, it is currently only possible to display the matches in
1480 :group
'org-link-follow
1484 (const vm-visit-folder
)
1485 (const vm-visit-folder-other-window
)
1486 (const vm-visit-folder-other-frame
)))
1490 (const gnus-other-frame
)
1491 (const org-gnus-no-new-news
)))
1495 (const find-file-other-window
)
1496 (const find-file-other-frame
)))
1500 (const wl-other-frame
)))))
1502 (defcustom org-display-internal-link-with-indirect-buffer nil
1503 "Non-nil means use indirect buffer to display infile links.
1504 Activating internal links (from one location in a file to another location
1505 in the same file) normally just jumps to the location. When the link is
1506 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1508 another window. When this option is set, the other window actually displays
1509 an indirect buffer clone of the current buffer, to avoid any visibility
1510 changes to the current buffer."
1511 :group
'org-link-follow
1514 (defcustom org-open-non-existing-files nil
1515 "Non-nil means `org-open-file' will open non-existing files.
1516 When nil, an error will be generated.
1517 This variable applies only to external applications because they
1518 might choke on non-existing files. If the link is to a file that
1519 will be opened in Emacs, the variable is ignored."
1520 :group
'org-link-follow
1523 (defcustom org-open-directory-means-index-dot-org nil
1524 "Non-nil means a link to a directory really means to index.org.
1525 When nil, following a directory link will run dired or open a finder/explorer
1526 window on that directory."
1527 :group
'org-link-follow
1530 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1531 "Function and arguments to call for following mailto links.
1532 This is a list with the first element being a Lisp function, and the
1533 remaining elements being arguments to the function. In string arguments,
1534 %a will be replaced by the address, and %s will be replaced by the subject
1535 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1536 :group
'org-link-follow
1538 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1539 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1540 (const :tag
"message-mail" (message-mail "%a" "%s"))
1541 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1543 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1544 "Non-nil means ask for confirmation before executing shell links.
1545 Shell links can be dangerous: just think about a link
1547 [[shell:rm -rf ~/*][Google Search]]
1549 This link would show up in your Org-mode document as \"Google Search\",
1550 but really it would remove your entire home directory.
1551 Therefore we advise against setting this variable to nil.
1552 Just change it to `y-or-n-p' if you want to confirm with a
1553 single keystroke rather than having to type \"yes\"."
1554 :group
'org-link-follow
1556 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1557 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1558 (const :tag
"no confirmation (dangerous)" nil
)))
1559 (put 'org-confirm-shell-link-function
1560 'safe-local-variable
1561 '(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1563 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1564 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1565 Elisp links can be dangerous: just think about a link
1567 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1569 This link would show up in your Org-mode document as \"Google Search\",
1570 but really it would remove your entire home directory.
1571 Therefore we advise against setting this variable to nil.
1572 Just change it to `y-or-n-p' if you want to confirm with a
1573 single keystroke rather than having to type \"yes\"."
1574 :group
'org-link-follow
1576 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1577 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1578 (const :tag
"no confirmation (dangerous)" nil
)))
1579 (put 'org-confirm-shell-link-function
1580 'safe-local-variable
1581 '(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1583 (defconst org-file-apps-defaults-gnu
1587 "Default file applications on a UNIX or GNU/Linux system.
1588 See `org-file-apps'.")
1590 (defconst org-file-apps-defaults-macosx
1593 (system .
"open %s")
1595 ("eps.gz" .
"gv %s")
1597 ("fig" .
"xfig %s"))
1598 "Default file applications on a MacOS X system.
1599 The system \"open\" is known as a default, but we use X11 applications
1600 for some files for which the OS does not have a good default.
1601 See `org-file-apps'.")
1603 (defconst org-file-apps-defaults-windowsnt
1607 (list (if (featurep 'xemacs
)
1608 'mswindows-shell-execute
1612 (list (if (featurep 'xemacs
)
1613 'mswindows-shell-execute
1616 "Default file applications on a Windows NT system.
1617 The system \"open\" is used for most files.
1618 See `org-file-apps'.")
1620 (defcustom org-file-apps
1623 ("\\.mm\\'" . default
)
1624 ("\\.x?html?\\'" . default
)
1625 ("\\.pdf\\'" . default
)
1627 "External applications for opening `file:path' items in a document.
1628 Org-mode uses system defaults for different file types, but
1629 you can use this variable to set the application for a given file
1630 extension. The entries in this list are cons cells where the car identifies
1631 files and the cdr the corresponding command. Possible values for the
1633 \"string\" A string as a file identifier can be interpreted in different
1634 ways, depending on its contents:
1636 - Alphanumeric characters only:
1637 Match links with this file extension.
1638 Example: (\"pdf\" . \"evince %s\")
1639 to open PDFs with evince.
1641 - Regular expression: Match links where the
1642 filename matches the regexp. If you want to
1643 use groups here, use shy groups.
1645 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1646 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1647 to open *.html and *.xhtml with firefox.
1649 - Regular expression which contains (non-shy) groups:
1650 Match links where the whole link, including \"::\", and
1651 anything after that, matches the regexp.
1652 In a custom command string, %1, %2, etc. are replaced with
1653 the parts of the link that were matched by the groups.
1654 For backwards compatibility, if a command string is given
1655 that does not use any of the group matches, this case is
1656 handled identically to the second one (i.e. match against
1658 In a custom lisp form, you can access the group matches with
1659 (match-string n link).
1661 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1662 to open [[file:document.pdf::5]] with evince at page 5.
1664 `directory' Matches a directory
1665 `remote' Matches a remote file, accessible through tramp or efs.
1666 Remote files most likely should be visited through Emacs
1667 because external applications cannot handle such paths.
1668 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1669 so all files Emacs knows how to handle. Using this with
1670 command `emacs' will open most files in Emacs. Beware that this
1671 will also open html files inside Emacs, unless you add
1672 (\"html\" . default) to the list as well.
1673 t Default for files not matched by any of the other options.
1674 `system' The system command to open files, like `open' on Windows
1675 and Mac OS X, and mailcap under GNU/Linux. This is the command
1676 that will be selected if you call `C-c C-o' with a double
1677 \\[universal-argument] \\[universal-argument] prefix.
1679 Possible values for the command are:
1680 `emacs' The file will be visited by the current Emacs process.
1681 `default' Use the default application for this file type, which is the
1682 association for t in the list, most likely in the system-specific
1684 This can be used to overrule an unwanted setting in the
1685 system-specific variable.
1686 `system' Use the system command for opening files, like \"open\".
1687 This command is specified by the entry whose car is `system'.
1688 Most likely, the system-specific version of this variable
1689 does define this command, but you can overrule/replace it
1691 string A command to be executed by a shell; %s will be replaced
1692 by the path to the file.
1693 sexp A Lisp form which will be evaluated. The file path will
1694 be available in the Lisp variable `file'.
1695 For more examples, see the system specific constants
1696 `org-file-apps-defaults-macosx'
1697 `org-file-apps-defaults-windowsnt'
1698 `org-file-apps-defaults-gnu'."
1699 :group
'org-link-follow
1701 (cons (choice :value
""
1702 (string :tag
"Extension")
1703 (const :tag
"System command to open files" system
)
1704 (const :tag
"Default for unrecognized files" t
)
1705 (const :tag
"Remote file" remote
)
1706 (const :tag
"Links to a directory" directory
)
1707 (const :tag
"Any files that have Emacs modes"
1710 (const :tag
"Visit with Emacs" emacs
)
1711 (const :tag
"Use default" default
)
1712 (const :tag
"Use the system command" system
)
1713 (string :tag
"Command")
1714 (sexp :tag
"Lisp form")))))
1718 (defgroup org-refile nil
1719 "Options concerning refiling entries in Org-mode."
1723 (defcustom org-directory
"~/org"
1724 "Directory with org files.
1725 This is just a default location to look for Org files. There is no need
1726 at all to put your files into this directory. It is only used in the
1727 following situations:
1729 1. When a remember template specifies a target file that is not an
1730 absolute path. The path will then be interpreted relative to
1732 2. When a remember note is filed away in an interactive way (when exiting the
1733 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1734 with `org-directory' as the default path."
1736 :group
'org-remember
1739 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1740 "Default target for storing notes.
1741 Used as a fall back file for org-remember.el and org-capture.el, for
1742 templates that do not specify a target file."
1744 :group
'org-remember
1746 (const :tag
"Default from remember-data-file" nil
)
1749 (defcustom org-goto-interface
'outline
1750 "The default interface to be used for `org-goto'.
1752 outline The interface shows an outline of the relevant file
1753 and the correct heading is found by moving through
1754 the outline or by searching with incremental search.
1755 outline-path-completion Headlines in the current buffer are offered via
1756 completion. This is the interface also used by
1757 the refile command."
1760 (const :tag
"Outline" outline
)
1761 (const :tag
"Outline-path-completion" outline-path-completion
)))
1763 (defcustom org-goto-max-level
5
1764 "Maximum target level when running `org-goto' with refile interface."
1768 (defcustom org-reverse-note-order nil
1769 "Non-nil means store new notes at the beginning of a file or entry.
1770 When nil, new notes will be filed to the end of a file or entry.
1771 This can also be a list with cons cells of regular expressions that
1772 are matched against file names, and values."
1773 :group
'org-remember
1776 (const :tag
"Reverse always" t
)
1777 (const :tag
"Reverse never" nil
)
1778 (repeat :tag
"By file name regexp"
1779 (cons regexp boolean
))))
1781 (defcustom org-log-refile nil
1782 "Information to record when a task is refiled.
1784 Possible values are:
1786 nil Don't add anything
1787 time Add a time stamp to the task
1788 note Prompt for a note and add it with template `org-log-note-headings'
1790 This option can also be set with on a per-file-basis with
1792 #+STARTUP: nologrefile
1793 #+STARTUP: logrefile
1794 #+STARTUP: lognoterefile
1796 You can have local logging settings for a subtree by setting the LOGGING
1797 property to one or more of these keywords.
1799 When bulk-refiling from the agenda, the value `note' is forbidden and
1800 will temporarily be changed to `time'."
1802 :group
'org-progress
1804 (const :tag
"No logging" nil
)
1805 (const :tag
"Record timestamp" time
)
1806 (const :tag
"Record timestamp with note." note
)))
1808 (defcustom org-refile-targets nil
1809 "Targets for refiling entries with \\[org-refile].
1810 This is list of cons cells. Each cell contains:
1811 - a specification of the files to be considered, either a list of files,
1812 or a symbol whose function or variable value will be used to retrieve
1813 a file name or a list of file names. If you use `org-agenda-files' for
1814 that, all agenda files will be scanned for targets. Nil means consider
1815 headings in the current buffer.
1816 - A specification of how to find candidate refile targets. This may be
1818 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1819 This tag has to be present in all target headlines, inheritance will
1821 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1823 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1824 headlines that are refiling targets.
1825 - a cons cell (:level . N). Any headline of level N is considered a target.
1826 Note that, when `org-odd-levels-only' is set, level corresponds to
1827 order in hierarchy, not to the number of stars.
1828 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1829 Note that, when `org-odd-levels-only' is set, level corresponds to
1830 order in hierarchy, not to the number of stars.
1832 You can set the variable `org-refile-target-verify-function' to a function
1833 to verify each headline found by the simple criteria above.
1835 When this variable is nil, all top-level headlines in the current buffer
1836 are used, equivalent to the value `((nil . (:level . 1))'."
1840 (choice :value org-agenda-files
1841 (const :tag
"All agenda files" org-agenda-files
)
1842 (const :tag
"Current buffer" nil
)
1843 (function) (variable) (file))
1844 (choice :tag
"Identify target headline by"
1845 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1846 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1847 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1848 (cons :tag
"Level number" (const :value
:level
) (integer))
1849 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1851 (defcustom org-refile-target-verify-function nil
1852 "Function to verify if the headline at point should be a refile target.
1853 The function will be called without arguments, with point at the
1854 beginning of the headline. It should return t and leave point
1855 where it is if the headline is a valid target for refiling.
1857 If the target should not be selected, the function must return nil.
1858 In addition to this, it may move point to a place from where the search
1859 should be continued. For example, the function may decide that the entire
1860 subtree of the current entry should be excluded and move point to the end
1865 (defcustom org-refile-use-cache nil
1866 "Non-nil means cache refile targets to speed up the process.
1867 The cache for a particular file will be updated automatically when
1868 the buffer has been killed, or when any of the marker used for flagging
1869 refile targets no longer points at a live buffer.
1870 If you have added new entries to a buffer that might themselves be targets,
1871 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
1872 find that easier, `C-u C-u C-u C-c C-w'."
1876 (defcustom org-refile-use-outline-path nil
1877 "Non-nil means provide refile targets as paths.
1878 So a level 3 headline will be available as level1/level2/level3.
1880 When the value is `file', also include the file name (without directory)
1881 into the path. In this case, you can also stop the completion after
1882 the file name, to get entries inserted as top level in the file.
1884 When `full-file-path', include the full file path."
1887 (const :tag
"Not" nil
)
1888 (const :tag
"Yes" t
)
1889 (const :tag
"Start with file name" file
)
1890 (const :tag
"Start with full file path" full-file-path
)))
1892 (defcustom org-outline-path-complete-in-steps t
1893 "Non-nil means complete the outline path in hierarchical steps.
1894 When Org-mode uses the refile interface to select an outline path
1895 \(see variable `org-refile-use-outline-path'), the completion of
1896 the path can be done is a single go, or if can be done in steps down
1897 the headline hierarchy. Going in steps is probably the best if you
1898 do not use a special completion package like `ido' or `icicles'.
1899 However, when using these packages, going in one step can be very
1900 fast, while still showing the whole path to the entry."
1904 (defcustom org-refile-allow-creating-parent-nodes nil
1905 "Non-nil means allow to create new nodes as refile targets.
1906 New nodes are then created by adding \"/new node name\" to the completion
1907 of an existing node. When the value of this variable is `confirm',
1908 new node creation must be confirmed by the user (recommended)
1909 When nil, the completion must match an existing entry.
1911 Note that, if the new heading is not seen by the criteria
1912 listed in `org-refile-targets', multiple instances of the same
1913 heading would be created by trying again to file under the new
1917 (const :tag
"Never" nil
)
1918 (const :tag
"Always" t
)
1919 (const :tag
"Prompt for confirmation" confirm
)))
1921 (defgroup org-todo nil
1922 "Options concerning TODO items in Org-mode."
1926 (defgroup org-progress nil
1927 "Options concerning Progress logging in Org-mode."
1931 (defvar org-todo-interpretation-widgets
1933 (:tag
"Sequence (cycling hits every state)" sequence
)
1934 (:tag
"Type (cycling directly to DONE)" type
))
1935 "The available interpretation symbols for customizing `org-todo-keywords'.
1936 Interested libraries should add to this list.")
1938 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1939 "List of TODO entry keyword sequences and their interpretation.
1940 \\<org-mode-map>This is a list of sequences.
1942 Each sequence starts with a symbol, either `sequence' or `type',
1943 indicating if the keywords should be interpreted as a sequence of
1944 action steps, or as different types of TODO items. The first
1945 keywords are states requiring action - these states will select a headline
1946 for inclusion into the global TODO list Org-mode produces. If one of
1947 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1948 signify that no further action is necessary. If \"|\" is not found,
1949 the last keyword is treated as the only DONE state of the sequence.
1951 The command \\[org-todo] cycles an entry through these states, and one
1952 additional state where no keyword is present. For details about this
1953 cycling, see the manual.
1955 TODO keywords and interpretation can also be set on a per-file basis with
1956 the special #+SEQ_TODO and #+TYP_TODO lines.
1958 Each keyword can optionally specify a character for fast state selection
1959 \(in combination with the variable `org-use-fast-todo-selection')
1960 and specifiers for state change logging, using the same syntax
1961 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1962 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1963 indicates to record a time stamp each time this state is selected.
1965 Each keyword may also specify if a timestamp or a note should be
1966 recorded when entering or leaving the state, by adding additional
1967 characters in the parenthesis after the keyword. This looks like this:
1968 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1969 record only the time of the state change. With X and Y being either
1970 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1971 Y when leaving the state if and only if the *target* state does not
1972 define X. You may omit any of the fast-selection key or X or /Y,
1973 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1975 For backward compatibility, this variable may also be just a list
1976 of keywords - in this case the interpretation (sequence or type) will be
1977 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1979 :group
'org-keywords
1981 (repeat :tag
"Old syntax, just keywords"
1982 (string :tag
"Keyword"))
1983 (repeat :tag
"New syntax"
1986 :tag
"Interpretation"
1987 ;;Quick and dirty way to see
1988 ;;`org-todo-interpretations'. This takes the
1989 ;;place of item arguments
1997 org-todo-interpretation-widgets
))
2000 (string :tag
"Keyword"))))))
2002 (defvar org-todo-keywords-1 nil
2003 "All TODO and DONE keywords active in a buffer.")
2004 (make-variable-buffer-local 'org-todo-keywords-1
)
2005 (defvar org-todo-keywords-for-agenda nil
)
2006 (defvar org-done-keywords-for-agenda nil
)
2007 (defvar org-drawers-for-agenda nil
)
2008 (defvar org-todo-keyword-alist-for-agenda nil
)
2009 (defvar org-tag-alist-for-agenda nil
)
2010 (defvar org-agenda-contributing-files nil
)
2011 (defvar org-not-done-keywords nil
)
2012 (make-variable-buffer-local 'org-not-done-keywords
)
2013 (defvar org-done-keywords nil
)
2014 (make-variable-buffer-local 'org-done-keywords
)
2015 (defvar org-todo-heads nil
)
2016 (make-variable-buffer-local 'org-todo-heads
)
2017 (defvar org-todo-sets nil
)
2018 (make-variable-buffer-local 'org-todo-sets
)
2019 (defvar org-todo-log-states nil
)
2020 (make-variable-buffer-local 'org-todo-log-states
)
2021 (defvar org-todo-kwd-alist nil
)
2022 (make-variable-buffer-local 'org-todo-kwd-alist
)
2023 (defvar org-todo-key-alist nil
)
2024 (make-variable-buffer-local 'org-todo-key-alist
)
2025 (defvar org-todo-key-trigger nil
)
2026 (make-variable-buffer-local 'org-todo-key-trigger
)
2028 (defcustom org-todo-interpretation
'sequence
2029 "Controls how TODO keywords are interpreted.
2030 This variable is in principle obsolete and is only used for
2031 backward compatibility, if the interpretation of todo keywords is
2032 not given already in `org-todo-keywords'. See that variable for
2035 :group
'org-keywords
2036 :type
'(choice (const sequence
)
2039 (defcustom org-use-fast-todo-selection t
2040 "Non-nil means use the fast todo selection scheme with C-c C-t.
2041 This variable describes if and under what circumstances the cycling
2042 mechanism for TODO keywords will be replaced by a single-key, direct
2045 When nil, fast selection is never used.
2047 When the symbol `prefix', it will be used when `org-todo' is called with
2048 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
2049 in an agenda buffer.
2051 When t, fast selection is used by default. In this case, the prefix
2052 argument forces cycling instead.
2054 In all cases, the special interface is only used if access keys have actually
2055 been assigned by the user, i.e. if keywords in the configuration are followed
2056 by a letter in parenthesis, like TODO(t)."
2059 (const :tag
"Never" nil
)
2060 (const :tag
"By default" t
)
2061 (const :tag
"Only with C-u C-c C-t" prefix
)))
2063 (defcustom org-provide-todo-statistics t
2064 "Non-nil means update todo statistics after insert and toggle.
2065 ALL-HEADLINES means update todo statistics by including headlines
2066 with no TODO keyword as well, counting them as not done.
2067 A list of TODO keywords means the same, but skip keywords that are
2070 When this is set, todo statistics is updated in the parent of the
2071 current entry each time a todo state is changed."
2074 (const :tag
"Yes, only for TODO entries" t
)
2075 (const :tag
"Yes, including all entries" 'all-headlines
)
2076 (repeat :tag
"Yes, for TODOs in this list"
2077 (string :tag
"TODO keyword"))
2078 (other :tag
"No TODO statistics" nil
)))
2080 (defcustom org-hierarchical-todo-statistics t
2081 "Non-nil means TODO statistics covers just direct children.
2082 When nil, all entries in the subtree are considered.
2083 This has only an effect if `org-provide-todo-statistics' is set.
2084 To set this to nil for only a single subtree, use a COOKIE_DATA
2085 property and include the word \"recursive\" into the value."
2089 (defcustom org-after-todo-state-change-hook nil
2090 "Hook which is run after the state of a TODO item was changed.
2091 The new state (a string with a TODO keyword, or nil) is available in the
2092 Lisp variable `state'."
2096 (defvar org-blocker-hook nil
2097 "Hook for functions that are allowed to block a state change.
2099 Each function gets as its single argument a property list, see
2100 `org-trigger-hook' for more information about this list.
2102 If any of the functions in this hook returns nil, the state change
2105 (defvar org-trigger-hook nil
2106 "Hook for functions that are triggered by a state change.
2108 Each function gets as its single argument a property list with at least
2109 the following elements:
2111 (:type type-of-change :position pos-at-entry-start
2112 :from old-state :to new-state)
2114 Depending on the type, more properties may be present.
2116 This mechanism is currently implemented for:
2120 :type todo-state-change
2121 :from previous state (keyword as a string), or nil, or a symbol
2122 'todo' or 'done', to indicate the general type of state.
2123 :to new state, like in :from")
2125 (defcustom org-enforce-todo-dependencies nil
2126 "Non-nil means undone TODO entries will block switching the parent to DONE.
2127 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2128 be blocked if any prior sibling is not yet done.
2129 Finally, if the parent is blocked because of ordered siblings of its own,
2130 the child will also be blocked.
2131 This variable needs to be set before org.el is loaded, and you need to
2132 restart Emacs after a change to make the change effective. The only way
2133 to change is while Emacs is running is through the customize interface."
2134 :set
(lambda (var val
)
2137 (add-hook 'org-blocker-hook
2138 'org-block-todo-from-children-or-siblings-or-parent
)
2139 (remove-hook 'org-blocker-hook
2140 'org-block-todo-from-children-or-siblings-or-parent
)))
2144 (defcustom org-enforce-todo-checkbox-dependencies nil
2145 "Non-nil means unchecked boxes will block switching the parent to DONE.
2146 When this is nil, checkboxes have no influence on switching TODO states.
2147 When non-nil, you first need to check off all check boxes before the TODO
2148 entry can be switched to DONE.
2149 This variable needs to be set before org.el is loaded, and you need to
2150 restart Emacs after a change to make the change effective. The only way
2151 to change is while Emacs is running is through the customize interface."
2152 :set
(lambda (var val
)
2155 (add-hook 'org-blocker-hook
2156 'org-block-todo-from-checkboxes
)
2157 (remove-hook 'org-blocker-hook
2158 'org-block-todo-from-checkboxes
)))
2162 (defcustom org-treat-insert-todo-heading-as-state-change nil
2163 "Non-nil means inserting a TODO heading is treated as state change.
2164 So when the command \\[org-insert-todo-heading] is used, state change
2165 logging will apply if appropriate. When nil, the new TODO item will
2166 be inserted directly, and no logging will take place."
2170 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2171 "Non-nil means switching TODO states with S-cursor counts as state change.
2172 This is the default behavior. However, setting this to nil allows a
2173 convenient way to select a TODO state and bypass any logging associated
2178 (defcustom org-todo-state-tags-triggers nil
2179 "Tag changes that should be triggered by TODO state changes.
2180 This is a list. Each entry is
2182 (state-change (tag . flag) .......)
2184 State-change can be a string with a state, and empty string to indicate the
2185 state that has no TODO keyword, or it can be one of the symbols `todo'
2186 or `done', meaning any not-done or done state, respectively."
2190 (cons (choice :tag
"When changing to"
2191 (const :tag
"Not-done state" todo
)
2192 (const :tag
"Done state" done
)
2193 (string :tag
"State"))
2195 (cons :tag
"Tag action"
2197 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2199 (defcustom org-log-done nil
2200 "Information to record when a task moves to the DONE state.
2202 Possible values are:
2204 nil Don't add anything, just change the keyword
2205 time Add a time stamp to the task
2206 note Prompt for a note and add it with template `org-log-note-headings'
2208 This option can also be set with on a per-file-basis with
2210 #+STARTUP: nologdone
2212 #+STARTUP: lognotedone
2214 You can have local logging settings for a subtree by setting the LOGGING
2215 property to one or more of these keywords."
2217 :group
'org-progress
2219 (const :tag
"No logging" nil
)
2220 (const :tag
"Record CLOSED timestamp" time
)
2221 (const :tag
"Record CLOSED timestamp with note." note
)))
2223 ;; Normalize old uses of org-log-done.
2225 ((eq org-log-done t
) (setq org-log-done
'time
))
2226 ((and (listp org-log-done
) (memq 'done org-log-done
))
2227 (setq org-log-done
'note
)))
2229 (defcustom org-log-reschedule nil
2230 "Information to record when the scheduling date of a tasks is modified.
2232 Possible values are:
2234 nil Don't add anything, just change the date
2235 time Add a time stamp to the task
2236 note Prompt for a note and add it with template `org-log-note-headings'
2238 This option can also be set with on a per-file-basis with
2240 #+STARTUP: nologreschedule
2241 #+STARTUP: logreschedule
2242 #+STARTUP: lognotereschedule"
2244 :group
'org-progress
2246 (const :tag
"No logging" nil
)
2247 (const :tag
"Record timestamp" time
)
2248 (const :tag
"Record timestamp with note." note
)))
2250 (defcustom org-log-redeadline nil
2251 "Information to record when the deadline date of a tasks is modified.
2253 Possible values are:
2255 nil Don't add anything, just change the date
2256 time Add a time stamp to the task
2257 note Prompt for a note and add it with template `org-log-note-headings'
2259 This option can also be set with on a per-file-basis with
2261 #+STARTUP: nologredeadline
2262 #+STARTUP: logredeadline
2263 #+STARTUP: lognoteredeadline
2265 You can have local logging settings for a subtree by setting the LOGGING
2266 property to one or more of these keywords."
2268 :group
'org-progress
2270 (const :tag
"No logging" nil
)
2271 (const :tag
"Record timestamp" time
)
2272 (const :tag
"Record timestamp with note." note
)))
2274 (defcustom org-log-note-clock-out nil
2275 "Non-nil means record a note when clocking out of an item.
2276 This can also be configured on a per-file basis by adding one of
2277 the following lines anywhere in the buffer:
2279 #+STARTUP: lognoteclock-out
2280 #+STARTUP: nolognoteclock-out"
2282 :group
'org-progress
2285 (defcustom org-log-done-with-time t
2286 "Non-nil means the CLOSED time stamp will contain date and time.
2287 When nil, only the date will be recorded."
2288 :group
'org-progress
2291 (defcustom org-log-note-headings
2292 '((done .
"CLOSING NOTE %t")
2293 (state .
"State %-12s from %-12S %t")
2294 (note .
"Note taken on %t")
2295 (reschedule .
"Rescheduled from %S on %t")
2296 (delschedule .
"Not scheduled, was %S on %t")
2297 (redeadline .
"New deadline from %S on %t")
2298 (deldeadline .
"Removed deadline, was %S on %t")
2299 (refile .
"Refiled on %t")
2301 "Headings for notes added to entries.
2302 The value is an alist, with the car being a symbol indicating the note
2303 context, and the cdr is the heading to be used. The heading may also be the
2305 %t in the heading will be replaced by a time stamp.
2306 %T will be an active time stamp instead the default inactive one
2307 %s will be replaced by the new TODO state, in double quotes.
2308 %S will be replaced by the old TODO state, in double quotes.
2309 %u will be replaced by the user name.
2310 %U will be replaced by the full user name.
2312 In fact, it is not a good idea to change the `state' entry, because
2313 agenda log mode depends on the format of these entries."
2315 :group
'org-progress
2316 :type
'(list :greedy t
2317 (cons (const :tag
"Heading when closing an item" done
) string
)
2319 "Heading when changing todo state (todo sequence only)"
2321 (cons (const :tag
"Heading when just taking a note" note
) string
)
2322 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2323 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2324 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2325 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2326 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2327 (cons (const :tag
"Heading when refiling" refile
) string
)))
2329 (unless (assq 'note org-log-note-headings
)
2330 (push '(note .
"%t") org-log-note-headings
))
2332 (defcustom org-log-into-drawer nil
2333 "Non-nil means insert state change notes and time stamps into a drawer.
2334 When nil, state changes notes will be inserted after the headline and
2335 any scheduling and clock lines, but not inside a drawer.
2337 The value of this variable should be the name of the drawer to use.
2338 LOGBOOK is proposed at the default drawer for this purpose, you can
2339 also set this to a string to define the drawer of your choice.
2341 A value of t is also allowed, representing \"LOGBOOK\".
2343 If this variable is set, `org-log-state-notes-insert-after-drawers'
2346 You can set the property LOG_INTO_DRAWER to overrule this setting for
2349 :group
'org-progress
2351 (const :tag
"Not into a drawer" nil
)
2352 (const :tag
"LOGBOOK" t
)
2353 (string :tag
"Other")))
2355 (if (fboundp 'defvaralias
)
2356 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2358 (defun org-log-into-drawer ()
2359 "Return the value of `org-log-into-drawer', but let properties overrule.
2360 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2361 used instead of the default value."
2362 (let ((p (ignore-errors (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit
))))
2364 ((or (not p
) (equal p
"nil")) org-log-into-drawer
)
2365 ((equal p
"t") "LOGBOOK")
2368 (defcustom org-log-state-notes-insert-after-drawers nil
2369 "Non-nil means insert state change notes after any drawers in entry.
2370 Only the drawers that *immediately* follow the headline and the
2371 deadline/scheduled line are skipped.
2372 When nil, insert notes right after the heading and perhaps the line
2373 with deadline/scheduling if present.
2375 This variable will have no effect if `org-log-into-drawer' is
2378 :group
'org-progress
2381 (defcustom org-log-states-order-reversed t
2382 "Non-nil means the latest state note will be directly after heading.
2383 When nil, the state change notes will be ordered according to time."
2385 :group
'org-progress
2388 (defcustom org-todo-repeat-to-state nil
2389 "The TODO state to which a repeater should return the repeating task.
2390 By default this is the first task in a TODO sequence, or the previous state
2391 in a TODO_TYP set. But you can specify another task here.
2392 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2394 :type
'(choice (const :tag
"Head of sequence" nil
)
2395 (string :tag
"Specific state")))
2397 (defcustom org-log-repeat
'time
2398 "Non-nil means record moving through the DONE state when triggering repeat.
2399 An auto-repeating task is immediately switched back to TODO when
2400 marked DONE. If you are not logging state changes (by adding \"@\"
2401 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2402 record a closing note, there will be no record of the task moving
2403 through DONE. This variable forces taking a note anyway.
2405 nil Don't force a record
2406 time Record a time stamp
2409 This option can also be set with on a per-file-basis with
2411 #+STARTUP: logrepeat
2412 #+STARTUP: lognoterepeat
2413 #+STARTUP: nologrepeat
2415 You can have local logging settings for a subtree by setting the LOGGING
2416 property to one or more of these keywords."
2418 :group
'org-progress
2420 (const :tag
"Don't force a record" nil
)
2421 (const :tag
"Force recording the DONE state" time
)
2422 (const :tag
"Force recording a note with the DONE state" note
)))
2425 (defgroup org-priorities nil
2426 "Priorities in Org-mode."
2427 :tag
"Org Priorities"
2430 (defcustom org-enable-priority-commands t
2431 "Non-nil means priority commands are active.
2432 When nil, these commands will be disabled, so that you never accidentally
2434 :group
'org-priorities
2437 (defcustom org-highest-priority ?A
2438 "The highest priority of TODO items. A character like ?A, ?B etc.
2439 Must have a smaller ASCII number than `org-lowest-priority'."
2440 :group
'org-priorities
2443 (defcustom org-lowest-priority ?C
2444 "The lowest priority of TODO items. A character like ?A, ?B etc.
2445 Must have a larger ASCII number than `org-highest-priority'."
2446 :group
'org-priorities
2449 (defcustom org-default-priority ?B
2450 "The default priority of TODO items.
2451 This is the priority an item get if no explicit priority is given."
2452 :group
'org-priorities
2455 (defcustom org-priority-start-cycle-with-default t
2456 "Non-nil means start with default priority when starting to cycle.
2457 When this is nil, the first step in the cycle will be (depending on the
2458 command used) one higher or lower that the default priority."
2459 :group
'org-priorities
2462 (defgroup org-time nil
2463 "Options concerning time stamps and deadlines in Org-mode."
2467 (defcustom org-insert-labeled-timestamps-at-point nil
2468 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2469 When nil, these labeled time stamps are forces into the second line of an
2470 entry, just after the headline. When scheduling from the global TODO list,
2471 the time stamp will always be forced into the second line."
2475 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2476 "Formats for `format-time-string' which are used for time stamps.
2477 It is not recommended to change this constant.")
2479 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2480 "Number of minutes to round time stamps to.
2481 These are two values, the first applies when first creating a time stamp.
2482 The second applies when changing it with the commands `S-up' and `S-down'.
2483 When changing the time stamp, this means that it will change in steps
2484 of N minutes, as given by the second value.
2486 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2487 numbers should be factors of 60, so for example 5, 10, 15.
2489 When this is larger than 1, you can still force an exact time stamp by using
2490 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2491 and by using a prefix arg to `S-up/down' to specify the exact number
2492 of minutes to shift."
2494 :get
'(lambda (var) ; Make sure both elements are there
2495 (if (integerp (default-value var
))
2496 (list (default-value var
) 5)
2497 (default-value var
)))
2499 (integer :tag
"when inserting times")
2500 (integer :tag
"when modifying times")))
2502 ;; Normalize old customizations of this variable.
2503 (when (integerp org-time-stamp-rounding-minutes
)
2504 (setq org-time-stamp-rounding-minutes
2505 (list org-time-stamp-rounding-minutes
2506 org-time-stamp-rounding-minutes
)))
2508 (defcustom org-display-custom-times nil
2509 "Non-nil means overlay custom formats over all time stamps.
2510 The formats are defined through the variable `org-time-stamp-custom-formats'.
2511 To turn this on on a per-file basis, insert anywhere in the file:
2512 #+STARTUP: customtime"
2516 (make-variable-buffer-local 'org-display-custom-times
)
2518 (defcustom org-time-stamp-custom-formats
2519 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2520 "Custom formats for time stamps. See `format-time-string' for the syntax.
2521 These are overlayed over the default ISO format if the variable
2522 `org-display-custom-times' is set. Time like %H:%M should be at the
2523 end of the second format. The custom formats are also honored by export
2524 commands, if custom time display is turned on at the time of export."
2528 (defun org-time-stamp-format (&optional long inactive
)
2529 "Get the right format for a time string."
2530 (let ((f (if long
(cdr org-time-stamp-formats
)
2531 (car org-time-stamp-formats
))))
2533 (concat "[" (substring f
1 -
1) "]")
2536 (defcustom org-time-clocksum-format
"%d:%02d"
2537 "The format string used when creating CLOCKSUM lines.
2538 This is also used when org-mode generates a time duration."
2542 (defcustom org-time-clocksum-use-fractional nil
2543 "If non-nil, \\[org-clock-display] uses fractional times.
2544 org-mode generates a time duration."
2548 (defcustom org-time-clocksum-fractional-format
"%.2f"
2549 "The format string used when creating CLOCKSUM lines, or when
2550 org-mode generates a time duration."
2554 (defcustom org-deadline-warning-days
14
2555 "No. of days before expiration during which a deadline becomes active.
2556 This variable governs the display in sparse trees and in the agenda.
2557 When 0 or negative, it means use this number (the absolute value of it)
2558 even if a deadline has a different individual lead time specified.
2560 Custom commands can set this variable in the options section."
2562 :group
'org-agenda-daily
/weekly
2565 (defcustom org-read-date-prefer-future t
2566 "Non-nil means assume future for incomplete date input from user.
2567 This affects the following situations:
2568 1. The user gives a month but not a year.
2569 For example, if it is April and you enter \"feb 2\", this will be read
2570 as Feb 2, *next* year. \"May 5\", however, will be this year.
2571 2. The user gives a day, but no month.
2572 For example, if today is the 15th, and you enter \"3\", Org-mode will
2573 read this as the third of *next* month. However, if you enter \"17\",
2574 it will be considered as *this* month.
2576 If you set this variable to the symbol `time', then also the following
2579 3. If the user gives a time, but no day. If the time is before now,
2580 to will be interpreted as tomorrow.
2582 Currently none of this works for ISO week specifications.
2584 When this option is nil, the current day, month and year will always be
2588 (const :tag
"Never" nil
)
2589 (const :tag
"Check month and day" t
)
2590 (const :tag
"Check month, day, and time" time
)))
2592 (defcustom org-read-date-display-live t
2593 "Non-nil means display current interpretation of date prompt live.
2594 This display will be in an overlay, in the minibuffer."
2598 (defcustom org-read-date-popup-calendar t
2599 "Non-nil means pop up a calendar when prompting for a date.
2600 In the calendar, the date can be selected with mouse-1. However, the
2601 minibuffer will also be active, and you can simply enter the date as well.
2602 When nil, only the minibuffer will be available."
2605 (if (fboundp 'defvaralias
)
2606 (defvaralias 'org-popup-calendar-for-date-prompt
2607 'org-read-date-popup-calendar
))
2609 (defcustom org-read-date-minibuffer-setup-hook nil
2610 "Hook to be used to set up keys for the date/time interface.
2611 Add key definitions to `minibuffer-local-map', which will be a temporary
2616 (defcustom org-extend-today-until
0
2617 "The hour when your day really ends. Must be an integer.
2618 This has influence for the following applications:
2619 - When switching the agenda to \"today\". It it is still earlier than
2620 the time given here, the day recognized as TODAY is actually yesterday.
2621 - When a date is read from the user and it is still before the time given
2622 here, the current date and time will be assumed to be yesterday, 23:59.
2623 Also, timestamps inserted in remember templates follow this rule.
2625 IMPORTANT: This is a feature whose implementation is and likely will
2626 remain incomplete. Really, it is only here because past midnight seems to
2627 be the favorite working time of John Wiegley :-)"
2631 (defcustom org-edit-timestamp-down-means-later nil
2632 "Non-nil means S-down will increase the time in a time stamp.
2633 When nil, S-up will increase."
2637 (defcustom org-calendar-follow-timestamp-change t
2638 "Non-nil means make the calendar window follow timestamp changes.
2639 When a timestamp is modified and the calendar window is visible, it will be
2640 moved to the new date."
2644 (defgroup org-tags nil
2645 "Options concerning tags in Org-mode."
2649 (defcustom org-tag-alist nil
2650 "List of tags allowed in Org-mode files.
2651 When this list is nil, Org-mode will base TAG input on what is already in the
2653 The value of this variable is an alist, the car of each entry must be a
2654 keyword as a string, the cdr may be a character that is used to select
2655 that tag through the fast-tag-selection interface.
2656 See the manual for details."
2660 (cons (string :tag
"Tag name")
2661 (character :tag
"Access char"))
2662 (list :tag
"Start radio group"
2664 (option (string :tag
"Group description")))
2665 (list :tag
"End radio group"
2667 (option (string :tag
"Group description")))
2668 (const :tag
"New line" (:newline
)))))
2670 (defcustom org-tag-persistent-alist nil
2671 "List of tags that will always appear in all Org-mode files.
2672 This is in addition to any in buffer settings or customizations
2674 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2675 The value of this variable is an alist, the car of each entry must be a
2676 keyword as a string, the cdr may be a character that is used to select
2677 that tag through the fast-tag-selection interface.
2678 See the manual for details.
2679 To disable these tags on a per-file basis, insert anywhere in the file:
2684 (cons (string :tag
"Tag name")
2685 (character :tag
"Access char"))
2686 (const :tag
"Start radio group" (:startgroup
))
2687 (const :tag
"End radio group" (:endgroup
))
2688 (const :tag
"New line" (:newline
)))))
2690 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2691 "If non-nil, always offer completion for all tags of all agenda files.
2692 Instead of customizing this variable directly, you might want to
2693 set it locally for remember buffers, because there no list of
2694 tags in that file can be created dynamically (there are none).
2696 (add-hook 'org-remember-mode-hook
2698 (set (make-local-variable
2699 'org-complete-tags-always-offer-all-agenda-tags)
2704 (defvar org-file-tags nil
2705 "List of tags that can be inherited by all entries in the file.
2706 The tags will be inherited if the variable `org-use-tag-inheritance'
2707 says they should be.
2708 This variable is populated from #+FILETAGS lines.")
2710 (defcustom org-use-fast-tag-selection
'auto
2711 "Non-nil means use fast tag selection scheme.
2712 This is a special interface to select and deselect tags with single keys.
2713 When nil, fast selection is never used.
2714 When the symbol `auto', fast selection is used if and only if selection
2715 characters for tags have been configured, either through the variable
2716 `org-tag-alist' or through a #+TAGS line in the buffer.
2717 When t, fast selection is always used and selection keys are assigned
2718 automatically if necessary."
2721 (const :tag
"Always" t
)
2722 (const :tag
"Never" nil
)
2723 (const :tag
"When selection characters are configured" 'auto
)))
2725 (defcustom org-fast-tag-selection-single-key nil
2726 "Non-nil means fast tag selection exits after first change.
2727 When nil, you have to press RET to exit it.
2728 During fast tag selection, you can toggle this flag with `C-c'.
2729 This variable can also have the value `expert'. In this case, the window
2730 displaying the tags menu is not even shown, until you press C-c again."
2733 (const :tag
"No" nil
)
2734 (const :tag
"Yes" t
)
2735 (const :tag
"Expert" expert
)))
2737 (defvar org-fast-tag-selection-include-todo nil
2738 "Non-nil means fast tags selection interface will also offer TODO states.
2739 This is an undocumented feature, you should not rely on it.")
2741 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2742 "The column to which tags should be indented in a headline.
2743 If this number is positive, it specifies the column. If it is negative,
2744 it means that the tags should be flushright to that column. For example,
2745 -80 works well for a normal 80 character screen."
2749 (defcustom org-auto-align-tags t
2750 "Non-nil means realign tags after pro/demotion of TODO state change.
2751 These operations change the length of a headline and therefore shift
2752 the tags around. With this options turned on, after each such operation
2753 the tags are again aligned to `org-tags-column'."
2757 (defcustom org-use-tag-inheritance t
2758 "Non-nil means tags in levels apply also for sublevels.
2759 When nil, only the tags directly given in a specific line apply there.
2760 This may also be a list of tags that should be inherited, or a regexp that
2761 matches tags that should be inherited. Additional control is possible
2762 with the variable `org-tags-exclude-from-inheritance' which gives an
2763 explicit list of tags to be excluded from inheritance., even if the value of
2764 `org-use-tag-inheritance' would select it for inheritance.
2766 If this option is t, a match early-on in a tree can lead to a large
2767 number of matches in the subtree when constructing the agenda or creating
2768 a sparse tree. If you only want to see the first match in a tree during
2769 a search, check out the variable `org-tags-match-list-sublevels'."
2772 (const :tag
"Not" nil
)
2773 (const :tag
"Always" t
)
2774 (repeat :tag
"Specific tags" (string :tag
"Tag"))
2775 (regexp :tag
"Tags matched by regexp")))
2777 (defcustom org-tags-exclude-from-inheritance nil
2778 "List of tags that should never be inherited.
2779 This is a way to exclude a few tags from inheritance. For way to do
2780 the opposite, to actively allow inheritance for selected tags,
2781 see the variable `org-use-tag-inheritance'."
2783 :type
'(repeat (string :tag
"Tag")))
2785 (defun org-tag-inherit-p (tag)
2786 "Check if TAG is one that should be inherited."
2788 ((member tag org-tags-exclude-from-inheritance
) nil
)
2789 ((eq org-use-tag-inheritance t
) t
)
2790 ((not org-use-tag-inheritance
) nil
)
2791 ((stringp org-use-tag-inheritance
)
2792 (string-match org-use-tag-inheritance tag
))
2793 ((listp org-use-tag-inheritance
)
2794 (member tag org-use-tag-inheritance
))
2795 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
2797 (defcustom org-tags-match-list-sublevels t
2798 "Non-nil means list also sublevels of headlines matching a search.
2799 This variable applies to tags/property searches, and also to stuck
2800 projects because this search is based on a tags match as well.
2802 When set to the symbol `indented', sublevels are indented with
2805 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2806 the sublevels of a headline matching a tag search often also match
2807 the same search. Listing all of them can create very long lists.
2808 Setting this variable to nil causes subtrees of a match to be skipped.
2810 This variable is semi-obsolete and probably should always be true. It
2811 is better to limit inheritance to certain tags using the variables
2812 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
2815 (const :tag
"No, don't list them" nil
)
2816 (const :tag
"Yes, do list them" t
)
2817 (const :tag
"List them, indented with leading dots" indented
)))
2819 (defcustom org-tags-sort-function nil
2820 "When set, tags are sorted using this function as a comparator."
2823 (const :tag
"No sorting" nil
)
2824 (const :tag
"Alphabetical" string
<)
2825 (const :tag
"Reverse alphabetical" string
>)
2826 (function :tag
"Custom function" nil
)))
2828 (defvar org-tags-history nil
2829 "History of minibuffer reads for tags.")
2830 (defvar org-last-tags-completion-table nil
2831 "The last used completion table for tags.")
2832 (defvar org-after-tags-change-hook nil
2833 "Hook that is run after the tags in a line have changed.")
2835 (defgroup org-properties nil
2836 "Options concerning properties in Org-mode."
2837 :tag
"Org Properties"
2840 (defcustom org-property-format
"%-10s %s"
2841 "How property key/value pairs should be formatted by `indent-line'.
2842 When `indent-line' hits a property definition, it will format the line
2843 according to this format, mainly to make sure that the values are
2844 lined-up with respect to each other."
2845 :group
'org-properties
2848 (defcustom org-use-property-inheritance nil
2849 "Non-nil means properties apply also for sublevels.
2851 This setting is chiefly used during property searches. Turning it on can
2852 cause significant overhead when doing a search, which is why it is not
2855 When nil, only the properties directly given in the current entry count.
2856 When t, every property is inherited. The value may also be a list of
2857 properties that should have inheritance, or a regular expression matching
2858 properties that should be inherited.
2860 However, note that some special properties use inheritance under special
2861 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2862 and the properties ending in \"_ALL\" when they are used as descriptor
2863 for valid values of a property.
2865 Note for programmers:
2866 When querying an entry with `org-entry-get', you can control if inheritance
2867 should be used. By default, `org-entry-get' looks only at the local
2868 properties. You can request inheritance by setting the inherit argument
2869 to t (to force inheritance) or to `selective' (to respect the setting
2871 :group
'org-properties
2873 (const :tag
"Not" nil
)
2874 (const :tag
"Always" t
)
2875 (repeat :tag
"Specific properties" (string :tag
"Property"))
2876 (regexp :tag
"Properties matched by regexp")))
2878 (defun org-property-inherit-p (property)
2879 "Check if PROPERTY is one that should be inherited."
2881 ((eq org-use-property-inheritance t
) t
)
2882 ((not org-use-property-inheritance
) nil
)
2883 ((stringp org-use-property-inheritance
)
2884 (string-match org-use-property-inheritance property
))
2885 ((listp org-use-property-inheritance
)
2886 (member property org-use-property-inheritance
))
2887 (t (error "Invalid setting of `org-use-property-inheritance'"))))
2889 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
2890 "The default column format, if no other format has been defined.
2891 This variable can be set on the per-file basis by inserting a line
2893 #+COLUMNS: %25ITEM ....."
2894 :group
'org-properties
2897 (defcustom org-columns-ellipses
".."
2898 "The ellipses to be used when a field in column view is truncated.
2899 When this is the empty string, as many characters as possible are shown,
2900 but then there will be no visual indication that the field has been truncated.
2901 When this is a string of length N, the last N characters of a truncated
2902 field are replaced by this string. If the column is narrower than the
2903 ellipses string, only part of the ellipses string will be shown."
2904 :group
'org-properties
2907 (defcustom org-columns-modify-value-for-display-function nil
2908 "Function that modifies values for display in column view.
2909 For example, it can be used to cut out a certain part from a time stamp.
2910 The function must take 2 arguments:
2912 column-title The title of the column (*not* the property name)
2913 value The value that should be modified.
2915 The function should return the value that should be displayed,
2916 or nil if the normal value should be used."
2917 :group
'org-properties
2920 (defcustom org-effort-property
"Effort"
2921 "The property that is being used to keep track of effort estimates.
2922 Effort estimates given in this property need to have the format H:MM."
2923 :group
'org-properties
2924 :group
'org-progress
2925 :type
'(string :tag
"Property"))
2927 (defconst org-global-properties-fixed
2928 '(("VISIBILITY_ALL" .
"folded children content all")
2929 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
2930 "List of property/value pairs that can be inherited by any entry.
2932 These are fixed values, for the preset properties. The user variable
2933 that can be used to add to this list is `org-global-properties'.
2935 The entries in this list are cons cells where the car is a property
2936 name and cdr is a string with the value. If the value represents
2937 multiple items like an \"_ALL\" property, separate the items by
2940 (defcustom org-global-properties nil
2941 "List of property/value pairs that can be inherited by any entry.
2943 This list will be combined with the constant `org-global-properties-fixed'.
2945 The entries in this list are cons cells where the car is a property
2946 name and cdr is a string with the value.
2948 You can set buffer-local values for the same purpose in the variable
2949 `org-file-properties' this by adding lines like
2951 #+PROPERTY: NAME VALUE"
2952 :group
'org-properties
2954 (cons (string :tag
"Property")
2955 (string :tag
"Value"))))
2957 (defvar org-file-properties nil
2958 "List of property/value pairs that can be inherited by any entry.
2959 Valid for the current buffer.
2960 This variable is populated from #+PROPERTY lines.")
2961 (make-variable-buffer-local 'org-file-properties
)
2963 (defgroup org-agenda nil
2964 "Options concerning agenda views in Org-mode."
2968 (defvar org-category nil
2969 "Variable used by org files to set a category for agenda display.
2970 Such files should use a file variable to set it, for example
2972 # -*- mode: org; org-category: \"ELisp\"
2974 or contain a special line
2978 If the file does not specify a category, then file's base name
2980 (make-variable-buffer-local 'org-category
)
2981 (put 'org-category
'safe-local-variable
'(lambda (x) (or (symbolp x
) (stringp x
))))
2983 (defcustom org-agenda-files nil
2984 "The files to be used for agenda display.
2985 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2986 \\[org-remove-file]. You can also use customize to edit the list.
2988 If an entry is a directory, all files in that directory that are matched by
2989 `org-agenda-file-regexp' will be part of the file list.
2991 If the value of the variable is not a list but a single file name, then
2992 the list of agenda files is actually stored and maintained in that file, one
2993 agenda file per line. In this file paths can be given relative to
2994 `org-directory'. Tilde expansion and environment variable substitution
2998 (repeat :tag
"List of files and directories" file
)
2999 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3001 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3002 "Regular expression to match files for `org-agenda-files'.
3003 If any element in the list in that variable contains a directory instead
3004 of a normal file, all files in that directory that are matched by this
3005 regular expression will be included."
3009 (defcustom org-agenda-text-search-extra-files nil
3010 "List of extra files to be searched by text search commands.
3011 These files will be search in addition to the agenda files by the
3012 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3013 Note that these files will only be searched for text search commands,
3014 not for the other agenda views like todo lists, tag searches or the weekly
3015 agenda. This variable is intended to list notes and possibly archive files
3016 that should also be searched by these two commands.
3017 In fact, if the first element in the list is the symbol `agenda-archives',
3018 than all archive files of all agenda files will be added to the search
3021 :type
'(set :greedy t
3022 (const :tag
"Agenda Archives" agenda-archives
)
3023 (repeat :inline t
(file))))
3025 (if (fboundp 'defvaralias
)
3026 (defvaralias 'org-agenda-multi-occur-extra-files
3027 'org-agenda-text-search-extra-files
))
3029 (defcustom org-agenda-skip-unavailable-files nil
3030 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3031 A nil value means to remove them, after a query, from the list."
3035 (defcustom org-calendar-to-agenda-key
[?c
]
3036 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3037 The command `org-calendar-goto-agenda' will be bound to this key. The
3038 default is the character `c' because then `c' can be used to switch back and
3039 forth between agenda and calendar."
3043 (defcustom org-calendar-agenda-action-key
[?k
]
3044 "The key to be installed in `calendar-mode-map' for agenda-action.
3045 The command `org-agenda-action' will be bound to this key. The
3046 default is the character `k' because we use the same key in the agenda."
3050 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3051 "The key to be installed in `calendar-mode-map' for adding diary entries.
3052 This option is irrelevant until `org-agenda-diary-file' has been configured
3053 to point to an Org-mode file. When that is the case, the command
3054 `org-agenda-diary-entry' will be bound to the key given here, by default
3055 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3056 if you want to continue doing this, you need to change this to a different
3061 (defcustom org-agenda-diary-file
'diary-file
3062 "File to which to add new entries with the `i' key in agenda and calendar.
3063 When this is the symbol `diary-file', the functionality in the Emacs
3064 calendar will be used to add entries to the `diary-file'. But when this
3065 points to a file, `org-agenda-diary-entry' will be used instead."
3068 (const :tag
"The standard Emacs diary file" diary-file
)
3069 (file :tag
"Special Org file diary entries")))
3071 (eval-after-load "calendar"
3073 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3074 'org-calendar-goto-agenda
)
3075 (org-defkey calendar-mode-map org-calendar-agenda-action-key
3077 (add-hook 'calendar-mode-hook
3079 (unless (eq org-agenda-diary-file
'diary-file
)
3080 (define-key calendar-mode-map
3081 org-calendar-insert-diary-entry-key
3082 'org-agenda-diary-entry
))))))
3084 (defgroup org-latex nil
3085 "Options for embedding LaTeX code into Org-mode."
3089 (defcustom org-format-latex-options
3090 '(:foreground default
:background default
:scale
1.0
3091 :html-foreground
"Black" :html-background
"Transparent"
3092 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3093 "Options for creating images from LaTeX fragments.
3094 This is a property list with the following properties:
3095 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3096 `default' means use the foreground of the default face.
3097 :background the background color, or \"Transparent\".
3098 `default' means use the background of the default face.
3099 :scale a scaling factor for the size of the images, to get more pixels
3100 :html-foreground, :html-background, :html-scale
3101 the same numbers for HTML export.
3102 :matchers a list indicating which matchers should be used to
3103 find LaTeX fragments. Valid members of this list are:
3104 \"begin\" find environments
3105 \"$1\" find single characters surrounded by $.$
3106 \"$\" find math expressions surrounded by $...$
3107 \"$$\" find math expressions surrounded by $$....$$
3108 \"\\(\" find math expressions surrounded by \\(...\\)
3109 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3113 (defcustom org-format-latex-signal-error t
3114 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3115 When nil, just push out a message."
3119 (defcustom org-format-latex-header
"\\documentclass{article}
3120 \\usepackage[usenames]{color}
3121 \\usepackage{amsmath}
3122 \\usepackage[mathscr]{eucal}
3123 \\pagestyle{empty} % do not remove
3126 % The settings below are copied from fullpage.sty
3127 \\setlength{\\textwidth}{\\paperwidth}
3128 \\addtolength{\\textwidth}{-3cm}
3129 \\setlength{\\oddsidemargin}{1.5cm}
3130 \\addtolength{\\oddsidemargin}{-2.54cm}
3131 \\setlength{\\evensidemargin}{\\oddsidemargin}
3132 \\setlength{\\textheight}{\\paperheight}
3133 \\addtolength{\\textheight}{-\\headheight}
3134 \\addtolength{\\textheight}{-\\headsep}
3135 \\addtolength{\\textheight}{-\\footskip}
3136 \\addtolength{\\textheight}{-3cm}
3137 \\setlength{\\topmargin}{1.5cm}
3138 \\addtolength{\\topmargin}{-2.54cm}"
3139 "The document header used for processing LaTeX fragments.
3140 It is imperative that this header make sure that no page number
3141 appears on the page. The package defined in the variables
3142 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
3143 will either replace the placeholder \"[PACKAGES]\" in this header, or they
3148 (defvar org-format-latex-header-extra nil
)
3150 (defun org-set-packages-alist (var val
)
3151 "Set the packages alist and make sure it has 3 elements per entry."
3152 (set var
(mapcar (lambda (x)
3153 (if (and (consp x
) (= (length x
) 2))
3154 (list (car x
) (nth 1 x
) t
)
3158 (defun org-get-packages-alist (var)
3160 "Get the packages alist and make sure it has 3 elements per entry."
3162 (if (and (consp x
) (= (length x
) 2))
3163 (list (car x
) (nth 1 x
) t
)
3165 (default-value var
)))
3167 ;; The following variables are defined here because is it also used
3168 ;; when formatting latex fragments. Originally it was part of the
3169 ;; LaTeX exporter, which is why the name includes "export".
3170 (defcustom org-export-latex-default-packages-alist
3171 '(("AUTO" "inputenc" t
)
3175 ("" "longtable" nil
)
3187 "Alist of default packages to be inserted in the header.
3188 Change this only if one of the packages here causes an incompatibility
3189 with another package you are using.
3190 The packages in this list are needed by one part or another of Org-mode
3191 to function properly.
3193 - inputenc, fontenc: for basic font and character selection
3194 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
3195 for interpreting the entities in `org-entities'. You can skip some of these
3196 packages if you don't use any of the symbols in it.
3197 - graphicx: for including images
3198 - float, wrapfig: for figure placement
3199 - longtable: for long tables
3200 - hyperref: for cross references
3202 Therefore you should not modify this variable unless you know what you
3203 are doing. The one reason to change it anyway is that you might be loading
3204 some other package that conflicts with one of the default packages.
3205 Each cell is of the format \( \"options\" \"package\" snippet-flag\).
3206 If SNIPPET-FLAG is t, the package also needs to be included when
3207 compiling LaTeX snippets into images for inclusion into HTML."
3208 :group
'org-export-latex
3209 :set
'org-set-packages-alist
3210 :get
'org-get-packages-alist
3213 (list :tag
"options/package pair"
3214 (string :tag
"options")
3215 (string :tag
"package")
3216 (boolean :tag
"Snippet"))
3217 (string :tag
"A line of LaTeX"))))
3219 (defcustom org-export-latex-packages-alist nil
3220 "Alist of packages to be inserted in every LaTeX header.
3221 These will be inserted after `org-export-latex-default-packages-alist'.
3222 Each cell is of the format \( \"options\" \"package\" snippet-flag \).
3223 SNIPPET-FLAG, when t, indicates that this package is also needed when
3224 turning LaTeX snippets into images for inclusion into HTML.
3225 Make sure that you only list packages here which:
3226 - you want in every file
3227 - do not conflict with the default packages in
3228 `org-export-latex-default-packages-alist'
3229 - do not conflict with the setup in `org-format-latex-header'."
3230 :group
'org-export-latex
3231 :set
'org-set-packages-alist
3232 :get
'org-get-packages-alist
3235 (list :tag
"options/package pair"
3236 (string :tag
"options")
3237 (string :tag
"package")
3238 (boolean :tag
"Snippet"))
3239 (string :tag
"A line of LaTeX"))))
3242 (defgroup org-appearance nil
3243 "Settings for Org-mode appearance."
3244 :tag
"Org Appearance"
3247 (defcustom org-level-color-stars-only nil
3248 "Non-nil means fontify only the stars in each headline.
3249 When nil, the entire headline is fontified.
3250 Changing it requires restart of `font-lock-mode' to become effective
3251 also in regions already fontified."
3252 :group
'org-appearance
3255 (defcustom org-hide-leading-stars nil
3256 "Non-nil means hide the first N-1 stars in a headline.
3257 This works by using the face `org-hide' for these stars. This
3258 face is white for a light background, and black for a dark
3259 background. You may have to customize the face `org-hide' to
3261 Changing it requires restart of `font-lock-mode' to become effective
3262 also in regions already fontified.
3263 You may also set this on a per-file basis by adding one of the following
3264 lines to the buffer:
3266 #+STARTUP: hidestars
3267 #+STARTUP: showstars"
3268 :group
'org-appearance
3271 (defcustom org-hidden-keywords nil
3272 "List of keywords that should be hidden when typed in the org buffer.
3273 For example, add #+TITLE to this list in order to make the
3274 document title appear in the buffer without the initial #+TITLE:
3276 :group
'org-appearance
3277 :type
'(set (const :tag
"#+AUTHOR" author
)
3278 (const :tag
"#+DATE" date
)
3279 (const :tag
"#+EMAIL" email
)
3280 (const :tag
"#+TITLE" title
)))
3282 (defcustom org-fontify-done-headline nil
3283 "Non-nil means change the face of a headline if it is marked DONE.
3284 Normally, only the TODO/DONE keyword indicates the state of a headline.
3285 When this is non-nil, the headline after the keyword is set to the
3286 `org-headline-done' as an additional indication."
3287 :group
'org-appearance
3290 (defcustom org-fontify-emphasized-text t
3291 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3292 Changing this variable requires a restart of Emacs to take effect."
3293 :group
'org-appearance
3296 (defcustom org-fontify-whole-heading-line nil
3297 "Non-nil means fontify the whole line for headings.
3298 This is useful when setting a background color for the
3300 :group
'org-appearance
3303 (defcustom org-highlight-latex-fragments-and-specials nil
3304 "Non-nil means fontify what is treated specially by the exporters."
3305 :group
'org-appearance
3308 (defcustom org-hide-emphasis-markers nil
3309 "Non-nil mean font-lock should hide the emphasis marker characters."
3310 :group
'org-appearance
3313 (defcustom org-pretty-entities nil
3314 "Non-nil means show entities as UTF8 characters.
3315 When nil, the \\name form remains in the buffer."
3316 :group
'org-appearance
3319 (defcustom org-pretty-entities-include-sub-superscripts t
3320 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3321 :group
'org-appearance
3324 (defvar org-emph-re nil
3325 "Regular expression for matching emphasis.
3326 After a match, the match groups contain these elements:
3327 0 The match of the full regular expression, including the characters
3328 before and after the proper match
3329 1 The character before the proper match, or empty at beginning of line
3330 2 The proper match, including the leading and trailing markers
3331 3 The leading marker like * or /, indicating the type of highlighting
3332 4 The text between the emphasis markers, not including the markers
3333 5 The character after the match, empty at the end of a line")
3334 (defvar org-verbatim-re nil
3335 "Regular expression for matching verbatim text.")
3336 (defvar org-emphasis-regexp-components
) ; defined just below
3337 (defvar org-emphasis-alist
) ; defined just below
3338 (defun org-set-emph-re (var val
)
3339 "Set variable and compute the emphasis regular expression."
3341 (when (and (boundp 'org-emphasis-alist
)
3342 (boundp 'org-emphasis-regexp-components
)
3343 org-emphasis-alist org-emphasis-regexp-components
)
3344 (let* ((e org-emphasis-regexp-components
)
3350 (body1 (concat body
"*?"))
3351 (markers (mapconcat 'car org-emphasis-alist
""))
3352 (vmarkers (mapconcat
3353 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3354 org-emphasis-alist
"")))
3355 ;; make sure special characters appear at the right position in the class
3356 (if (string-match "\\^" markers
)
3357 (setq markers
(concat (replace-match "" t t markers
) "^")))
3358 (if (string-match "-" markers
)
3359 (setq markers
(concat (replace-match "" t t markers
) "-")))
3360 (if (string-match "\\^" vmarkers
)
3361 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3362 (if (string-match "-" vmarkers
)
3363 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3365 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3366 (int-to-string nl
) "\\}")))
3369 (concat "\\([" pre
"]\\|^\\)"
3371 "\\([" markers
"]\\)"
3379 "\\([" post
"]\\|$\\)"))
3380 (setq org-verbatim-re
3381 (concat "\\([" pre
"]\\|^\\)"
3383 "\\([" vmarkers
"]\\)"
3391 "\\([" post
"]\\|$\\)")))))
3393 (defcustom org-emphasis-regexp-components
3394 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3395 "Components used to build the regular expression for emphasis.
3396 This is a list with 6 entries. Terminology: In an emphasis string
3397 like \" *strong word* \", we call the initial space PREMATCH, the final
3398 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3399 and \"trong wor\" is the body. The different components in this variable
3400 specify what is allowed/forbidden in each part:
3402 pre Chars allowed as prematch. Beginning of line will be allowed too.
3403 post Chars allowed as postmatch. End of line will be allowed too.
3404 border The chars *forbidden* as border characters.
3405 body-regexp A regexp like \".\" to match a body character. Don't use
3406 non-shy groups here, and don't allow newline here.
3407 newline The maximum number of newlines allowed in an emphasis exp.
3409 Use customize to modify this, or restart Emacs after changing it."
3410 :group
'org-appearance
3411 :set
'org-set-emph-re
3413 (sexp :tag
"Allowed chars in pre ")
3414 (sexp :tag
"Allowed chars in post ")
3415 (sexp :tag
"Forbidden chars in border ")
3416 (sexp :tag
"Regexp for body ")
3417 (integer :tag
"number of newlines allowed")
3418 (option (boolean :tag
"Please ignore this button"))))
3420 (defcustom org-emphasis-alist
3421 `(("*" bold
"<b>" "</b>")
3422 ("/" italic
"<i>" "</i>")
3423 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
3424 ("=" org-code
"<code>" "</code>" verbatim
)
3425 ("~" org-verbatim
"<code>" "</code>" verbatim
)
3426 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
3429 "Special syntax for emphasized text.
3430 Text starting and ending with a special character will be emphasized, for
3431 example *bold*, _underlined_ and /italic/. This variable sets the marker
3432 characters, the face to be used by font-lock for highlighting in Org-mode
3433 Emacs buffers, and the HTML tags to be used for this.
3434 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3435 For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
3436 Use customize to modify this, or restart Emacs after changing it."
3437 :group
'org-appearance
3438 :set
'org-set-emph-re
3441 (string :tag
"Marker character")
3443 (face :tag
"Font-lock-face")
3444 (plist :tag
"Face property list"))
3445 (string :tag
"HTML start tag")
3446 (string :tag
"HTML end tag")
3447 (option (const verbatim
)))))
3449 (defvar org-protecting-blocks
3450 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3451 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3452 This is needed for font-lock setup.")
3454 ;;; Miscellaneous options
3456 (defgroup org-completion nil
3457 "Completion in Org-mode."
3458 :tag
"Org Completion"
3461 (defcustom org-completion-use-ido nil
3462 "Non-nil means use ido completion wherever possible.
3463 Note that `ido-mode' must be active for this variable to be relevant.
3464 If you decide to turn this variable on, you might well want to turn off
3465 `org-outline-path-complete-in-steps'.
3466 See also `org-completion-use-iswitchb'."
3467 :group
'org-completion
3470 (defcustom org-completion-use-iswitchb nil
3471 "Non-nil means use iswitchb completion wherever possible.
3472 Note that `iswitchb-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 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3476 :group
'org-completion
3479 (defcustom org-completion-fallback-command
'hippie-expand
3480 "The expansion command called by \\[org-complete] in normal context.
3481 Normal means no org-mode-specific context."
3482 :group
'org-completion
3485 ;;; Functions and variables from their packages
3486 ;; Declared here to avoid compiler warnings
3489 (defvar outline-mode-menu-heading
)
3490 (defvar outline-mode-menu-show
)
3491 (defvar outline-mode-menu-hide
)
3492 (defvar zmacs-regions
) ; XEmacs regions
3495 (defvar mark-active
)
3498 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
3499 (declare-function calendar-forward-day
"cal-move" (arg))
3500 (declare-function calendar-goto-date
"cal-move" (date))
3501 (declare-function calendar-goto-today
"cal-move" ())
3502 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
3503 (defvar calc-embedded-close-formula
)
3504 (defvar calc-embedded-open-formula
)
3505 (declare-function cdlatex-tab
"ext:cdlatex" ())
3506 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
3507 (defvar font-lock-unfontify-region-function
)
3508 (declare-function iswitchb-read-buffer
"iswitchb"
3509 (prompt &optional default require-match start matches-set
))
3510 (defvar iswitchb-temp-buflist
)
3511 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
3512 (defvar org-agenda-tags-todo-honor-ignore-options
)
3513 (declare-function org-agenda-skip
"org-agenda" ())
3515 org-format-agenda-item
"org-agenda"
3516 (extra txt
&optional category tags dotime noprefix remove-re habitp
))
3517 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
3518 (declare-function org-agenda-change-all-lines
"org-agenda"
3519 (newhead hdmarker
&optional fixface just-this
))
3520 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
3521 (declare-function org-agenda-maybe-redo
"org-agenda" ())
3522 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
3524 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
3525 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3526 "org-agenda" (&optional end
))
3527 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
3528 (declare-function org-indent-mode
"org-indent" (&optional arg
))
3529 (declare-function parse-time-string
"parse-time" (string))
3530 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
3531 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
3532 (defvar remember-data-file
)
3533 (defvar texmathp-why
)
3534 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
3535 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
3537 (defvar w3m-current-url
)
3538 (defvar w3m-current-title
)
3540 (defvar org-latex-regexps
)
3542 ;;; Autoload and prepare some org modules
3544 ;; Some table stuff that needs to be defined here, because it is used
3545 ;; by the functions setting up org-mode or checking for table context.
3547 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
3548 "Detect an org-type or table-type table.")
3549 (defconst org-table-line-regexp
"^[ \t]*|"
3550 "Detect an org-type table line.")
3551 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
3552 "Detect an org-type table line.")
3553 (defconst org-table-hline-regexp
"^[ \t]*|-"
3554 "Detect an org-type table hline.")
3555 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
3556 "Detect a table-type table hline.")
3557 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
3558 "Detect the first line outside a table when searching from within it.
3559 This works for both table types.")
3561 ;; Autoload the functions in org-table.el that are needed by functions here.
3564 (org-autoload "org-table"
3565 '(org-table-align org-table-begin org-table-blank-field
3566 org-table-convert org-table-convert-region org-table-copy-down
3567 org-table-copy-region org-table-create
3568 org-table-create-or-convert-from-region
3569 org-table-create-with-table.el org-table-current-dline
3570 org-table-cut-region org-table-delete-column org-table-edit-field
3571 org-table-edit-formulas org-table-end org-table-eval-formula
3572 org-table-export org-table-field-info
3573 org-table-get-stored-formulas org-table-goto-column
3574 org-table-hline-and-move org-table-import org-table-insert-column
3575 org-table-insert-hline org-table-insert-row org-table-iterate
3576 org-table-justify-field-maybe org-table-kill-row
3577 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3578 org-table-move-column org-table-move-column-left
3579 org-table-move-column-right org-table-move-row
3580 org-table-move-row-down org-table-move-row-up
3581 org-table-next-field org-table-next-row org-table-paste-rectangle
3582 org-table-previous-field org-table-recalculate
3583 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3584 org-table-toggle-coordinate-overlays
3585 org-table-toggle-formula-debugger org-table-wrap-region
3586 orgtbl-mode turn-on-orgtbl org-table-to-lisp
3587 orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
3588 orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo
)))
3590 (defun org-at-table-p (&optional table-type
)
3591 "Return t if the cursor is inside an org-type table.
3592 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3593 (if org-enable-table-editor
3595 (beginning-of-line 1)
3596 (looking-at (if table-type org-table-any-line-regexp
3597 org-table-line-regexp
)))
3599 (defsubst org-table-p
() (org-at-table-p))
3601 (defun org-at-table.el-p
()
3602 "Return t if and only if we are at a table.el table."
3603 (and (org-at-table-p 'any
)
3605 (goto-char (org-table-begin 'any
))
3606 (looking-at org-table1-hline-regexp
))))
3607 (defun org-table-recognize-table.el
()
3608 "If there is a table.el table nearby, recognize it and move into it."
3609 (if org-table-tab-recognizes-table.el
3610 (if (org-at-table.el-p
)
3612 (beginning-of-line 1)
3613 (if (looking-at org-table-dataline-regexp
)
3615 (if (looking-at org-table1-hline-regexp
)
3617 (beginning-of-line 2)
3618 (if (looking-at org-table-any-border-regexp
)
3619 (beginning-of-line -
1)))))
3620 (if (re-search-forward "|" (org-table-end t
) t
)
3623 (if (table--at-cell-p (point))
3625 (message "recognizing table.el table...")
3626 (table-recognize-table)
3627 (message "recognizing table.el table...done")))
3628 (error "This should not happen"))
3633 (defun org-at-table-hline-p ()
3634 "Return t if the cursor is inside a hline in a table."
3635 (if org-enable-table-editor
3637 (beginning-of-line 1)
3638 (looking-at org-table-hline-regexp
))
3641 (defvar org-table-clean-did-remove-column nil
)
3643 (defun org-table-map-tables (function &optional quietly
)
3644 "Apply FUNCTION to the start of all tables in the buffer."
3648 (goto-char (point-min))
3649 (while (re-search-forward org-table-any-line-regexp nil t
)
3651 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
3652 (beginning-of-line 1)
3653 (when (looking-at org-table-line-regexp
)
3654 (save-excursion (funcall function
))
3655 (or (looking-at org-table-line-regexp
)
3657 (re-search-forward org-table-any-border-regexp nil
1))))
3658 (unless quietly
(message "Mapping tables: done")))
3660 ;; Declare and autoload functions from org-exp.el & Co
3662 (declare-function org-default-export-plist
"org-exp")
3663 (declare-function org-infile-export-plist
"org-exp")
3664 (declare-function org-get-current-options
"org-exp")
3666 (org-autoload "org-exp"
3667 '(org-export org-export-visible
3668 org-insert-export-options-template
3669 org-table-clean-before-export
))
3670 (org-autoload "org-ascii"
3671 '(org-export-as-ascii org-export-ascii-preprocess
3672 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3673 org-export-region-as-ascii
))
3674 (org-autoload "org-latex"
3675 '(org-export-as-latex-batch org-export-as-latex-to-buffer
3676 org-replace-region-by-latex org-export-region-as-latex
3677 org-export-as-latex org-export-as-pdf
3678 org-export-as-pdf-and-open
))
3679 (org-autoload "org-html"
3680 '(org-export-as-html-and-open
3681 org-export-as-html-batch org-export-as-html-to-buffer
3682 org-replace-region-by-html org-export-region-as-html
3683 org-export-as-html
))
3684 (org-autoload "org-docbook"
3685 '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
3686 org-replace-region-by-docbook org-export-region-as-docbook
3687 org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
3688 org-export-as-docbook
))
3689 (org-autoload "org-icalendar"
3690 '(org-export-icalendar-this-file
3691 org-export-icalendar-all-agenda-files
3692 org-export-icalendar-combine-agenda-files
))
3693 (org-autoload "org-xoxo" '(org-export-as-xoxo))
3694 (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning
)))
3696 ;; Declare and autoload functions from org-agenda.el
3699 (org-autoload "org-agenda"
3700 '(org-agenda org-agenda-list org-search-view
3701 org-todo-list org-tags-view org-agenda-list-stuck-projects
3702 org-diary org-agenda-to-appt
3703 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
)))
3705 ;; Autoload org-remember
3708 (org-autoload "org-remember"
3709 '(org-remember-insinuate org-remember-annotation
3710 org-remember-apply-template org-remember org-remember-handler
)))
3713 (org-autoload "org-capture"
3714 '(org-capture org-capture-insert-template-here
3715 org-capture-import-remember-templates
)))
3717 ;; Autoload org-clock.el
3720 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
3722 (declare-function org-clock-update-mode-line
"org-clock" ())
3723 (declare-function org-resolve-clocks
"org-clock"
3724 (&optional also-non-dangling-p prompt last-valid
))
3725 (defvar org-clock-start-time
)
3726 (defvar org-clock-marker
(make-marker)
3727 "Marker recording the last clock-in.")
3728 (defvar org-clock-hd-marker
(make-marker)
3729 "Marker recording the last clock-in, but the headline position.")
3730 (defvar org-clock-heading
""
3731 "The heading of the current clock entry.")
3732 (defun org-clock-is-active ()
3733 "Return non-nil if clock is currently running.
3734 The return value is actually the clock marker."
3735 (marker-buffer org-clock-marker
))
3740 '(org-clock-in org-clock-out org-clock-cancel
3741 org-clock-goto org-clock-sum org-clock-display
3742 org-clock-remove-overlays org-clock-report
3743 org-clocktable-shift org-dblock-write
:clocktable
3744 org-get-clocktable org-resolve-clocks
)))
3746 (defun org-clock-update-time-maybe ()
3747 "If this is a CLOCK line, update it and return t.
3748 Otherwise, return nil."
3751 (beginning-of-line 1)
3752 (skip-chars-forward " \t")
3753 (when (looking-at org-clock-string
)
3754 (let ((re (concat "[ \t]*" org-clock-string
3755 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
3756 "\\([ \t]*=>.*\\)?\\)?"))
3759 ((not (looking-at re
))
3761 ((not (match-end 2))
3762 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
3763 (> org-clock-marker
(point))
3764 (<= org-clock-marker
(point-at-eol)))
3765 ;; The clock is running here
3766 (setq org-clock-start-time
3768 (org-parse-time-string (match-string 1))))
3769 (org-clock-update-mode-line)))
3771 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
3773 (setq ts
(match-string 1)
3774 te
(match-string 3))
3775 (setq s
(- (org-float-time
3776 (apply 'encode-time
(org-parse-time-string te
)))
3778 (apply 'encode-time
(org-parse-time-string ts
))))
3781 h
(floor (/ s
3600))
3785 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
3788 (defun org-check-running-clock ()
3789 "Check if the current buffer contains the running clock.
3790 If yes, offer to stop it and to save the buffer with the changes."
3791 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
3792 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
3795 (when (y-or-n-p "Save changed buffer?")
3798 (defun org-clocktable-try-shift (dir n
)
3799 "Check if this line starts a clock table, if yes, shift the time block."
3800 (when (org-match-line "#\\+BEGIN: clocktable\\>")
3801 (org-clocktable-shift dir n
)))
3803 ;; Autoload org-timer.el
3808 '(org-timer-start org-timer org-timer-item
3809 org-timer-change-times-in-region
3811 org-timer-reset-timers
3812 org-timer-show-remaining-time
)))
3814 ;; Autoload org-feed.el
3819 '(org-feed-update org-feed-update-all org-feed-goto-inbox
)))
3822 ;; Autoload org-indent.el
3824 ;; Define the variable already here, to make sure we have it.
3825 (defvar org-indent-mode nil
3826 "Non-nil if Org-Indent mode is enabled.
3827 Use the command `org-indent-mode' to change this variable.")
3832 '(org-indent-mode)))
3834 ;; Autoload org-mobile.el
3839 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda
)))
3841 ;; Autoload archiving code
3842 ;; The stuff that is needed for cycling and tags has to be defined here.
3844 (defgroup org-archive nil
3845 "Options concerning archiving in Org-mode."
3847 :group
'org-structure
)
3849 (defcustom org-archive-location
"%s_archive::"
3850 "The location where subtrees should be archived.
3852 The value of this variable is a string, consisting of two parts,
3853 separated by a double-colon. The first part is a filename and
3854 the second part is a headline.
3856 When the filename is omitted, archiving happens in the same file.
3857 %s in the filename will be replaced by the current file
3858 name (without the directory part). Archiving to a different file
3859 is useful to keep archived entries from contributing to the
3862 The archived entries will be filed as subtrees of the specified
3863 headline. When the headline is omitted, the subtrees are simply
3864 filed away at the end of the file, as top-level entries. Also in
3865 the heading you can use %s to represent the file name, this can be
3866 useful when using the same archive for a number of different files.
3868 Here are a few examples:
3870 If the current file is Projects.org, archive in file
3871 Projects.org_archive, as top-level trees. This is the default.
3873 \"::* Archived Tasks\"
3874 Archive in the current file, under the top-level headline
3875 \"* Archived Tasks\".
3877 \"~/org/archive.org::\"
3878 Archive in file ~/org/archive.org (absolute path), as top-level trees.
3880 \"~/org/archive.org::From %s\"
3881 Archive in file ~/org/archive.org (absolute path), under headlines
3882 \"From FILENAME\" where file name is the current file name.
3884 \"basement::** Finished Tasks\"
3885 Archive in file ./basement (relative path), as level 3 trees
3886 below the level 2 heading \"** Finished Tasks\".
3888 You may set this option on a per-file basis by adding to the buffer a
3891 #+ARCHIVE: basement::** Finished Tasks
3893 You may also define it locally for a subtree by setting an ARCHIVE property
3894 in the entry. If such a property is found in an entry, or anywhere up
3895 the hierarchy, it will be used."
3899 (defcustom org-archive-tag
"ARCHIVE"
3900 "The tag that marks a subtree as archived.
3901 An archived subtree does not open during visibility cycling, and does
3902 not contribute to the agenda listings.
3903 After changing this, font-lock must be restarted in the relevant buffers to
3904 get the proper fontification."
3906 :group
'org-keywords
3909 (defcustom org-agenda-skip-archived-trees t
3910 "Non-nil means the agenda will skip any items located in archived trees.
3911 An archived tree is a tree marked with the tag ARCHIVE. The use of this
3912 variable is no longer recommended, you should leave it at the value t.
3913 Instead, use the key `v' to cycle the archives-mode in the agenda."
3915 :group
'org-agenda-skip
3918 (defcustom org-columns-skip-archived-trees t
3919 "Non-nil means ignore archived trees when creating column view."
3921 :group
'org-properties
3924 (defcustom org-cycle-open-archived-trees nil
3925 "Non-nil means `org-cycle' will open archived trees.
3926 An archived tree is a tree marked with the tag ARCHIVE.
3927 When nil, archived trees will stay folded. You can still open them with
3928 normal outline commands like `show-all', but not with the cycling commands."
3933 (defcustom org-sparse-tree-open-archived-trees nil
3934 "Non-nil means sparse tree construction shows matches in archived trees.
3935 When nil, matches in these trees are highlighted, but the trees are kept in
3938 :group
'org-sparse-trees
3941 (defun org-cycle-hide-archived-subtrees (state)
3942 "Re-hide all archived subtrees after a visibility state change."
3943 (when (and (not org-cycle-open-archived-trees
)
3944 (not (memq state
'(overview folded
))))
3946 (let* ((globalp (memq state
'(contents all
)))
3947 (beg (if globalp
(point-min) (point)))
3948 (end (if globalp
(point-max) (org-end-of-subtree t
))))
3949 (org-hide-archived-subtrees beg end
)
3951 (if (looking-at (concat ".*:" org-archive-tag
":"))
3952 (message "%s" (substitute-command-keys
3953 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
3955 (defun org-force-cycle-archived ()
3956 "Cycle subtree even if it is archived."
3958 (setq this-command
'org-cycle
)
3959 (let ((org-cycle-open-archived-trees t
))
3960 (call-interactively 'org-cycle
)))
3962 (defun org-hide-archived-subtrees (beg end
)
3963 "Re-hide all archived subtrees after a visibility state change."
3965 (let* ((re (concat ":" org-archive-tag
":")))
3967 (while (re-search-forward re end t
)
3968 (when (org-on-heading-p)
3969 (org-flag-subtree t
)
3970 (org-end-of-subtree t
))))))
3972 (defun org-flag-subtree (flag)
3974 (org-back-to-heading t
)
3975 (outline-end-of-heading)
3976 (outline-flag-region (point)
3977 (progn (org-end-of-subtree t
) (point))
3980 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
3983 (org-autoload "org-archive"
3984 '(org-add-archive-files org-archive-subtree
3985 org-archive-to-archive-sibling org-toggle-archive-tag
3986 org-archive-subtree-default
3987 org-archive-subtree-default-with-confirmation
)))
3989 ;; Autoload Column View Code
3991 (declare-function org-columns-number-to-string
"org-colview")
3992 (declare-function org-columns-get-format-and-top-level
"org-colview")
3993 (declare-function org-columns-compute
"org-colview")
3995 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
3996 '(org-columns-number-to-string org-columns-get-format-and-top-level
3997 org-columns-compute org-agenda-columns org-columns-remove-overlays
3998 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
4002 (declare-function org-id-store-link
"org-id")
4003 (declare-function org-id-locations-load
"org-id")
4004 (declare-function org-id-locations-save
"org-id")
4005 (defvar org-id-track-globally
)
4006 (org-autoload "org-id"
4007 '(org-id-get-create org-id-new org-id-copy org-id-get
4008 org-id-get-with-outline-path-completion
4009 org-id-get-with-outline-drilling org-id-store-link
4010 org-id-goto org-id-find org-id-store-link
))
4012 ;; Autoload Plotting Code
4014 (org-autoload "org-plot"
4015 '(org-plot/gnuplot
))
4017 ;;; Variables for pre-computed regular expressions, all buffer local
4019 (defvar org-drawer-regexp nil
4020 "Matches first line of a hidden block.")
4021 (make-variable-buffer-local 'org-drawer-regexp
)
4022 (defvar org-todo-regexp nil
4023 "Matches any of the TODO state keywords.")
4024 (make-variable-buffer-local 'org-todo-regexp
)
4025 (defvar org-not-done-regexp nil
4026 "Matches any of the TODO state keywords except the last one.")
4027 (make-variable-buffer-local 'org-not-done-regexp
)
4028 (defvar org-not-done-heading-regexp nil
4029 "Matches a TODO headline that is not done.")
4030 (make-variable-buffer-local 'org-not-done-regexp
)
4031 (defvar org-todo-line-regexp nil
4032 "Matches a headline and puts TODO state into group 2 if present.")
4033 (make-variable-buffer-local 'org-todo-line-regexp
)
4034 (defvar org-complex-heading-regexp nil
4035 "Matches a headline and puts everything into groups:
4037 group 2: The todo keyword, maybe
4038 group 3: Priority cookie
4039 group 4: True headline
4041 (make-variable-buffer-local 'org-complex-heading-regexp
)
4042 (defvar org-complex-heading-regexp-format nil
4043 "Printf format to make regexp to match an exact headline.
4044 This regexp will match the headline of any node which hase the exact
4045 headline text that is put into the format, but may have any TODO state,
4046 priority and tags.")
4047 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4048 (defvar org-todo-line-tags-regexp nil
4049 "Matches a headline and puts TODO state into group 2 if present.
4050 Also put tags into group 4 if tags are present.")
4051 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4052 (defvar org-nl-done-regexp nil
4053 "Matches newline followed by a headline with the DONE keyword.")
4054 (make-variable-buffer-local 'org-nl-done-regexp
)
4055 (defvar org-looking-at-done-regexp nil
4056 "Matches the DONE keyword a point.")
4057 (make-variable-buffer-local 'org-looking-at-done-regexp
)
4058 (defvar org-ds-keyword-length
12
4059 "Maximum length of the Deadline and SCHEDULED keywords.")
4060 (make-variable-buffer-local 'org-ds-keyword-length
)
4061 (defvar org-deadline-regexp nil
4062 "Matches the DEADLINE keyword.")
4063 (make-variable-buffer-local 'org-deadline-regexp
)
4064 (defvar org-deadline-time-regexp nil
4065 "Matches the DEADLINE keyword together with a time stamp.")
4066 (make-variable-buffer-local 'org-deadline-time-regexp
)
4067 (defvar org-deadline-line-regexp nil
4068 "Matches the DEADLINE keyword and the rest of the line.")
4069 (make-variable-buffer-local 'org-deadline-line-regexp
)
4070 (defvar org-scheduled-regexp nil
4071 "Matches the SCHEDULED keyword.")
4072 (make-variable-buffer-local 'org-scheduled-regexp
)
4073 (defvar org-scheduled-time-regexp nil
4074 "Matches the SCHEDULED keyword together with a time stamp.")
4075 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4076 (defvar org-closed-time-regexp nil
4077 "Matches the CLOSED keyword together with a time stamp.")
4078 (make-variable-buffer-local 'org-closed-time-regexp
)
4080 (defvar org-keyword-time-regexp nil
4081 "Matches any of the 4 keywords, together with the time stamp.")
4082 (make-variable-buffer-local 'org-keyword-time-regexp
)
4083 (defvar org-keyword-time-not-clock-regexp nil
4084 "Matches any of the 3 keywords, together with the time stamp.")
4085 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4086 (defvar org-maybe-keyword-time-regexp nil
4087 "Matches a timestamp, possibly preceded by a keyword.")
4088 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4089 (defvar org-planning-or-clock-line-re nil
4090 "Matches a line with planning or clock info.")
4091 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
4092 (defvar org-all-time-keywords nil
4093 "List of time keywords.")
4094 (make-variable-buffer-local 'org-all-time-keywords
)
4096 (defconst org-plain-time-of-day-regexp
4099 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4102 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4104 "Regular expression to match a plain time or time range.
4105 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4106 groups carry important information:
4108 1 the first time, range or not
4109 8 the second time, if it is a range.")
4111 (defconst org-plain-time-extension-regexp
4114 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4115 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4116 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4117 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4118 groups carry important information:
4121 9 minutes of duration")
4123 (defconst org-stamp-time-of-day-regexp
4125 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4126 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4128 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4129 "Regular expression to match a timestamp time or time range.
4130 After a match, the following groups carry important information:
4132 1 date plus weekday, for back referencing to make sure both times are on the same day
4133 2 the first time, range or not
4134 4 the second time, if it is a range.")
4136 (defconst org-startup-options
4137 '(("fold" org-startup-folded t
)
4138 ("overview" org-startup-folded t
)
4139 ("nofold" org-startup-folded nil
)
4140 ("showall" org-startup-folded nil
)
4141 ("showeverything" org-startup-folded showeverything
)
4142 ("content" org-startup-folded content
)
4143 ("indent" org-startup-indented t
)
4144 ("noindent" org-startup-indented nil
)
4145 ("hidestars" org-hide-leading-stars t
)
4146 ("showstars" org-hide-leading-stars nil
)
4147 ("odd" org-odd-levels-only t
)
4148 ("oddeven" org-odd-levels-only nil
)
4149 ("align" org-startup-align-all-tables t
)
4150 ("noalign" org-startup-align-all-tables nil
)
4151 ("customtime" org-display-custom-times t
)
4152 ("logdone" org-log-done time
)
4153 ("lognotedone" org-log-done note
)
4154 ("nologdone" org-log-done nil
)
4155 ("lognoteclock-out" org-log-note-clock-out t
)
4156 ("nolognoteclock-out" org-log-note-clock-out nil
)
4157 ("logrepeat" org-log-repeat state
)
4158 ("lognoterepeat" org-log-repeat note
)
4159 ("nologrepeat" org-log-repeat nil
)
4160 ("logreschedule" org-log-reschedule time
)
4161 ("lognotereschedule" org-log-reschedule note
)
4162 ("nologreschedule" org-log-reschedule nil
)
4163 ("logredeadline" org-log-redeadline time
)
4164 ("lognoteredeadline" org-log-redeadline note
)
4165 ("nologredeadline" org-log-redeadline nil
)
4166 ("logrefile" org-log-refile time
)
4167 ("lognoterefile" org-log-refile note
)
4168 ("nologrefile" org-log-refile nil
)
4169 ("fninline" org-footnote-define-inline t
)
4170 ("nofninline" org-footnote-define-inline nil
)
4171 ("fnlocal" org-footnote-section nil
)
4172 ("fnauto" org-footnote-auto-label t
)
4173 ("fnprompt" org-footnote-auto-label nil
)
4174 ("fnconfirm" org-footnote-auto-label confirm
)
4175 ("fnplain" org-footnote-auto-label plain
)
4176 ("fnadjust" org-footnote-auto-adjust t
)
4177 ("nofnadjust" org-footnote-auto-adjust nil
)
4178 ("constcgs" constants-unit-system cgs
)
4179 ("constSI" constants-unit-system SI
)
4180 ("noptag" org-tag-persistent-alist nil
)
4181 ("hideblocks" org-hide-block-startup t
)
4182 ("nohideblocks" org-hide-block-startup nil
)
4183 ("beamer" org-startup-with-beamer-mode t
)
4184 ("entitiespretty" org-pretty-entities t
)
4185 ("entitiesplain" org-pretty-entities nil
))
4186 "Variable associated with STARTUP options for org-mode.
4187 Each element is a list of three items: The startup options as written
4188 in the #+STARTUP line, the corresponding variable, and the value to
4189 set this variable to if the option is found. An optional forth element PUSH
4190 means to push this value onto the list in the variable.")
4192 (defun org-set-regexps-and-options ()
4193 "Precompute regular expressions for current buffer."
4195 (org-set-local 'org-todo-kwd-alist nil
)
4196 (org-set-local 'org-todo-key-alist nil
)
4197 (org-set-local 'org-todo-key-trigger nil
)
4198 (org-set-local 'org-todo-keywords-1 nil
)
4199 (org-set-local 'org-done-keywords nil
)
4200 (org-set-local 'org-todo-heads nil
)
4201 (org-set-local 'org-todo-sets nil
)
4202 (org-set-local 'org-todo-log-states nil
)
4203 (org-set-local 'org-file-properties nil
)
4204 (org-set-local 'org-file-tags nil
)
4205 (let ((re (org-make-options-regexp
4206 '("CATEGORY" "TODO" "COLUMNS"
4207 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
4208 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
4210 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4212 (scripts org-use-sub-superscripts
)
4213 kwds kws0 kwsa key log value cat arch tags const links hw dws
4214 tail sep kws1 prio props ftags drawers beamer-p
4215 ext-setup-or-nil setup-contents
(start 0))
4219 (goto-char (point-min))
4220 (while (or (and ext-setup-or-nil
4221 (string-match re ext-setup-or-nil start
)
4222 (setq start
(match-end 0)))
4223 (and (setq ext-setup-or-nil nil start
0)
4224 (re-search-forward re nil t
)))
4225 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4226 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4227 (if (stringp value
) (setq value
(org-trim value
)))
4229 ((equal key
"CATEGORY")
4231 ((member key
'("SEQ_TODO" "TODO"))
4232 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4233 ((equal key
"TYP_TODO")
4234 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4235 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4236 ;; general TODO-like setup
4237 (push (cons (intern (downcase (match-string 1 key
)))
4238 (org-split-string value splitre
)) kwds
))
4240 (setq tags
(append tags
(if tags
'("\\n") nil
)
4241 (org-split-string value splitre
))))
4242 ((equal key
"COLUMNS")
4243 (org-set-local 'org-columns-default-format value
))
4245 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4246 (push (cons (match-string 1 value
)
4247 (org-trim (match-string 2 value
)))
4249 ((equal key
"PRIORITIES")
4250 (setq prio
(org-split-string value
" +")))
4251 ((equal key
"PROPERTY")
4252 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4253 (push (cons (match-string 1 value
) (match-string 2 value
))
4255 ((equal key
"FILETAGS")
4256 (when (string-match "\\S-" value
)
4261 (mapcar (lambda (x) (org-split-string x
":"))
4262 (org-split-string value
)))))))
4263 ((equal key
"DRAWERS")
4264 (setq drawers
(org-split-string value splitre
)))
4265 ((equal key
"CONSTANTS")
4266 (setq const
(append const
(org-split-string value splitre
))))
4267 ((equal key
"STARTUP")
4268 (let ((opts (org-split-string value splitre
))
4270 (while (setq l
(pop opts
))
4271 (when (setq l
(assoc l org-startup-options
))
4272 (setq var
(nth 1 l
) val
(nth 2 l
))
4274 (set (make-local-variable var
) val
)
4275 (if (not (listp (symbol-value var
)))
4276 (set (make-local-variable var
) nil
))
4277 (set (make-local-variable var
) (symbol-value var
))
4278 (add-to-list var val
))))))
4279 ((equal key
"ARCHIVE")
4281 (remove-text-properties 0 (length arch
)
4282 '(face t fontified t
) arch
))
4283 ((equal key
"LATEX_CLASS")
4284 (setq beamer-p
(equal value
"beamer")))
4285 ((equal key
"OPTIONS")
4286 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4287 (setq scripts
(read (match-string 2 value
)))))
4288 ((equal key
"SETUPFILE")
4289 (setq setup-contents
(org-file-contents
4291 (org-remove-double-quotes value
))
4293 (if (not ext-setup-or-nil
)
4294 (setq ext-setup-or-nil setup-contents start
0)
4295 (setq ext-setup-or-nil
4296 (concat (substring ext-setup-or-nil
0 start
)
4297 "\n" setup-contents
"\n"
4298 (substring ext-setup-or-nil start
)))))
4300 (org-set-local 'org-use-sub-superscripts scripts
)
4302 (org-set-local 'org-category
(intern cat
))
4303 (push (cons "CATEGORY" cat
) props
))
4305 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4306 (setq prio
(mapcar 'string-to-char prio
))
4307 (org-set-local 'org-highest-priority
(nth 0 prio
))
4308 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4309 (org-set-local 'org-default-priority
(nth 2 prio
)))
4310 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4311 (and ftags
(org-set-local 'org-file-tags
4312 (mapcar 'org-add-prop-inherited ftags
)))
4313 (and drawers
(org-set-local 'org-drawers drawers
))
4314 (and arch
(org-set-local 'org-archive-location arch
))
4315 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4316 ;; Process the TODO keywords
4318 ;; Use the global values as if they had been given locally.
4319 (setq kwds
(default-value 'org-todo-keywords
))
4320 (if (stringp (car kwds
))
4321 (setq kwds
(list (cons org-todo-interpretation
4322 (default-value 'org-todo-keywords
)))))
4323 (setq kwds
(reverse kwds
)))
4324 (setq kwds
(nreverse kwds
))
4326 (while (setq kws
(pop kwds
))
4328 (run-hook-with-args-until-success
4329 'org-todo-setup-filter-hook kws
)
4331 (setq inter
(pop kws
) sep
(member "|" kws
)
4332 kws0
(delete "|" (copy-sequence kws
))
4337 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4339 (setq kw
(match-string 1 x
)
4340 key
(and (match-end 2) (match-string 2 x
))
4341 log
(org-extract-log-state-settings x
))
4342 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4343 (and log
(push log org-todo-log-states
))
4345 (error "Invalid TODO keyword %s" x
)))
4347 kwsa
(if kwsa
(append '((:startgroup
))
4351 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4352 tail
(list inter hw
(car dws
) (org-last dws
))))
4353 (add-to-list 'org-todo-heads hw
'append
)
4354 (push kws1 org-todo-sets
)
4355 (setq org-done-keywords
(append org-done-keywords dws nil
))
4356 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4357 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4358 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4359 (setq org-todo-sets
(nreverse org-todo-sets
)
4360 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4361 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4362 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4363 ;; Process the constants
4366 (while (setq e
(pop const
))
4367 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4368 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4369 (setq org-table-formula-constants-local cst
)))
4371 ;; Process the tags.
4374 (while (setq e
(pop tags
))
4376 ((equal e
"{") (push '(:startgroup
) tgs
))
4377 ((equal e
"}") (push '(:endgroup
) tgs
))
4378 ((equal e
"\\n") (push '(:newline
) tgs
))
4379 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4380 (push (cons (match-string 1 e
)
4381 (string-to-char (match-string 2 e
)))
4383 (t (push (list e
) tgs
))))
4384 (org-set-local 'org-tag-alist nil
)
4385 (while (setq e
(pop tgs
))
4386 (or (and (stringp (car e
))
4387 (assoc (car e
) org-tag-alist
))
4388 (push e org-tag-alist
)))))
4390 ;; Compute the regular expressions and other local variables
4391 (if (not org-done-keywords
)
4392 (setq org-done-keywords
(and org-todo-keywords-1
4393 (list (org-last org-todo-keywords-1
)))))
4394 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4395 (length org-scheduled-string
)
4396 (length org-clock-string
)
4397 (length org-closed-string
)))
4399 (concat "^[ \t]*:\\("
4400 (mapconcat 'regexp-quote org-drawers
"\\|")
4402 org-not-done-keywords
4403 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4405 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
4409 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4411 org-not-done-heading-regexp
4412 (concat "^\\(\\*+\\)[ \t]+\\("
4413 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4415 org-todo-line-regexp
4416 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4417 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4418 "\\)\\>\\)?[ \t]*\\(.*\\)")
4419 org-complex-heading-regexp
4420 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4421 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4422 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
4423 "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
4424 org-complex-heading-regexp-format
4425 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4426 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4428 "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
4429 "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
4431 "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
4432 "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?[ \t]*$")
4434 (concat "\n\\*+[ \t]+"
4435 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
4437 org-todo-line-tags-regexp
4438 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4439 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4441 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:[ \t]*\\)?$\\)"))
4442 org-looking-at-done-regexp
4444 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
4446 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4447 org-deadline-time-regexp
4448 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4449 org-deadline-line-regexp
4450 (concat "\\<\\(" org-deadline-string
"\\).*")
4451 org-scheduled-regexp
4452 (concat "\\<" org-scheduled-string
)
4453 org-scheduled-time-regexp
4454 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4455 org-closed-time-regexp
4456 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4457 org-keyword-time-regexp
4458 (concat "\\<\\(" org-scheduled-string
4459 "\\|" org-deadline-string
4460 "\\|" org-closed-string
4461 "\\|" org-clock-string
"\\)"
4462 " *[[<]\\([^]>]+\\)[]>]")
4463 org-keyword-time-not-clock-regexp
4464 (concat "\\<\\(" org-scheduled-string
4465 "\\|" org-deadline-string
4466 "\\|" org-closed-string
4468 " *[[<]\\([^]>]+\\)[]>]")
4469 org-maybe-keyword-time-regexp
4470 (concat "\\(\\<\\(" org-scheduled-string
4471 "\\|" org-deadline-string
4472 "\\|" org-closed-string
4473 "\\|" org-clock-string
"\\)\\)?"
4474 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4475 org-planning-or-clock-line-re
4476 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
4477 "\\|" org-deadline-string
4478 "\\|" org-closed-string
"\\|" org-clock-string
4480 org-all-time-keywords
4481 (mapcar (lambda (w) (substring w
0 -
1))
4482 (list org-scheduled-string org-deadline-string
4483 org-clock-string org-closed-string
))
4485 (org-compute-latex-and-specials-regexp)
4486 (org-set-font-lock-defaults))))
4488 (defun org-file-contents (file &optional noerror
)
4489 "Return the contents of FILE, as a string."
4491 (not (file-readable-p file
)))
4494 (message "Cannot read file \"%s\"" file
)
4497 (error "Cannot read file \"%s\"" file
))
4499 (insert-file-contents file
)
4502 (defun org-extract-log-state-settings (x)
4503 "Extract the log state setting from a TODO keyword string.
4504 This will extract info from a string like \"WAIT(w@/!)\"."
4505 (let (kw key log1 log2
)
4506 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4507 (setq kw
(match-string 1 x
)
4508 key
(and (match-end 2) (match-string 2 x
))
4509 log1
(and (match-end 3) (match-string 3 x
))
4510 log2
(and (match-end 4) (match-string 4 x
)))
4513 (and log1
(if (equal log1
"!") 'time
'note
))
4514 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4516 (defun org-remove-keyword-keys (list)
4517 "Remove a pair of parenthesis at the end of each string in LIST."
4519 (if (string-match "(.*)$" x
)
4520 (substring x
0 (match-beginning 0))
4524 (defun org-assign-fast-keys (alist)
4525 "Assign fast keys to a keyword-key alist.
4526 Respect keys that are already there."
4527 (let (new e
(alt ?
0))
4528 (while (setq e
(pop alist
))
4529 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
4530 (cdr e
)) ;; Key already assigned.
4532 (let ((clist (string-to-list (downcase (car e
))))
4533 (used (append new alist
)))
4534 (when (= (car clist
) ?
@)
4536 (while (and clist
(rassoc (car clist
) used
))
4539 (while (rassoc alt used
)
4541 (push (cons (car e
) (or (car clist
) alt
)) new
))))
4544 ;;; Some variables used in various places
4546 (defvar org-window-configuration nil
4547 "Used in various places to store a window configuration.")
4548 (defvar org-selected-window nil
4549 "Used in various places to store a window configuration.")
4550 (defvar org-finish-function nil
4551 "Function to be called when `C-c C-c' is used.
4552 This is for getting out of special buffers like remember.")
4555 ;; FIXME: Occasionally check by commenting these, to make sure
4556 ;; no other functions uses these, forgetting to let-bind them.
4561 ;; Defined somewhere in this file, but used before definition.
4562 (defvar org-entities
) ;; defined in org-entities.el
4563 (defvar org-struct-menu
)
4564 (defvar org-org-menu
)
4565 (defvar org-tbl-menu
)
4567 ;;;; Define the Org-mode
4569 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4570 (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"))
4573 ;; We use a before-change function to check if a table might need
4575 (defvar org-table-may-need-update t
4576 "Indicates that a table might need an update.
4577 This variable is set by `org-before-change-function'.
4578 `org-table-align' sets it back to nil.")
4579 (defun org-before-change-function (beg end
)
4580 "Every change indicates that a table might need an update."
4581 (setq org-table-may-need-update t
))
4582 (defvar org-mode-map
)
4583 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4584 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
4585 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4586 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
4587 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
4588 (defvar org-table-buffer-is-an nil
)
4589 (defconst org-outline-regexp
"\\*+ ")
4592 (define-derived-mode org-mode outline-mode
"Org"
4593 "Outline-based notes management and organizer, alias
4594 \"Carsten's outline-mode for keeping track of everything.\"
4596 Org-mode develops organizational tasks around a NOTES file which
4597 contains information about projects as plain text. Org-mode is
4598 implemented on top of outline-mode, which is ideal to keep the content
4599 of large files well structured. It supports ToDo items, deadlines and
4600 time stamps, which magically appear in the diary listing of the Emacs
4601 calendar. Tables are easily created with a built-in table editor.
4602 Plain text URL-like links connect to websites, emails (VM), Usenet
4603 messages (Gnus), BBDB entries, and any files related to the project.
4604 For printing and sharing of notes, an Org-mode file (or a part of it)
4605 can be exported as a structured ASCII or HTML file.
4607 The following commands are available:
4611 ;; Get rid of Outline menus, they are not needed
4612 ;; Need to do this here because define-derived-mode sets up
4613 ;; the keymap so late. Still, it is a waste to call this each time
4614 ;; we switch another buffer into org-mode.
4615 (if (featurep 'xemacs
)
4616 (when (boundp 'outline-mode-menu-heading
)
4617 ;; Assume this is Greg's port, it uses easymenu
4618 (easy-menu-remove outline-mode-menu-heading
)
4619 (easy-menu-remove outline-mode-menu-show
)
4620 (easy-menu-remove outline-mode-menu-hide
))
4621 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
4622 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
4623 (define-key org-mode-map
[menu-bar show
] 'undefined
))
4625 (org-load-modules-maybe)
4626 (easy-menu-add org-org-menu
)
4627 (easy-menu-add org-tbl-menu
)
4628 (org-install-agenda-files-menu)
4629 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
4630 (add-to-invisibility-spec '(org-cwidth))
4631 (add-to-invisibility-spec '(org-hide-block . t
))
4632 (when (featurep 'xemacs
)
4633 (org-set-local 'line-move-ignore-invisible t
))
4634 (org-set-local 'outline-regexp org-outline-regexp
)
4635 (org-set-local 'outline-level
'org-outline-level
)
4636 (when (and org-ellipsis
4637 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
4638 (fboundp 'make-glyph-code
))
4639 (unless org-display-table
4640 (setq org-display-table
(make-display-table)))
4641 (set-display-table-slot
4644 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
4646 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
4647 (setq buffer-display-table org-display-table
))
4648 (org-set-regexps-and-options)
4649 (when (and org-tag-faces
(not org-tags-special-faces-re
))
4650 ;; tag faces set outside customize.... force initialization.
4651 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
4653 (org-set-local 'calc-embedded-open-mode
"# ")
4654 (modify-syntax-entry ?
@ "w")
4655 (if org-startup-truncated
(setq truncate-lines t
))
4656 (org-set-local 'font-lock-unfontify-region-function
4657 'org-unfontify-region
)
4658 ;; Activate before-change-function
4659 (org-set-local 'org-table-may-need-update t
)
4660 (org-add-hook 'before-change-functions
'org-before-change-function nil
4662 ;; Check for running clock before killing a buffer
4663 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
4664 ;; Paragraphs and auto-filling
4665 (org-set-autofill-regexps)
4666 (setq indent-line-function
'org-indent-line-function
)
4667 (org-update-radio-target-regexp)
4668 ;; Beginning/end of defun
4669 (org-set-local 'beginning-of-defun-function
'org-beginning-of-defun
)
4670 (org-set-local 'end-of-defun-function
'org-end-of-defun
)
4671 ;; Make sure dependence stuff works reliably, even for users who set it
4673 (if org-enforce-todo-dependencies
4674 (add-hook 'org-blocker-hook
4675 'org-block-todo-from-children-or-siblings-or-parent
)
4676 (remove-hook 'org-blocker-hook
4677 'org-block-todo-from-children-or-siblings-or-parent
))
4678 (if org-enforce-todo-checkbox-dependencies
4679 (add-hook 'org-blocker-hook
4680 'org-block-todo-from-checkboxes
)
4681 (remove-hook 'org-blocker-hook
4682 'org-block-todo-from-checkboxes
))
4684 ;; Comment characters
4685 (org-set-local 'comment-start
"#")
4686 (org-set-local 'comment-padding
" ")
4688 ;; Align options lines
4690 'align-mode-rules-list
4691 '((org-in-buffer-settings
4692 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4693 (modes .
'(org-mode)))))
4696 (org-set-local 'imenu-create-index-function
4697 'org-imenu-get-tree
)
4699 ;; Make isearch reveal context
4700 (if (or (featurep 'xemacs
)
4701 (not (boundp 'outline-isearch-open-invisible-function
)))
4702 ;; Emacs 21 and XEmacs make use of the hook
4703 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
4704 ;; Emacs 22 deals with this through a special variable
4705 (org-set-local 'outline-isearch-open-invisible-function
4706 (lambda (&rest ignore
) (org-show-context 'isearch
))))
4708 ;; Turn on org-beamer-mode?
4709 (and org-startup-with-beamer-mode
(org-beamer-mode 1))
4711 ;; If empty file that did not turn on org-mode automatically, make it to.
4712 (if (and org-insert-mode-line-in-empty-file
4714 (= (point-min) (point-max)))
4715 (insert "# -*- mode: org -*-\n\n"))
4716 (unless org-inhibit-startup
4717 (when org-startup-align-all-tables
4718 (let ((bmp (buffer-modified-p)))
4719 (org-table-map-tables 'org-table-align
'quietly
)
4720 (set-buffer-modified-p bmp
)))
4721 (when org-startup-indented
4722 (require 'org-indent
)
4723 (org-indent-mode 1))
4724 (unless org-inhibit-startup-visibility-stuff
4725 (org-set-startup-visibility))))
4727 (when (fboundp 'abbrev-table-put
)
4728 (abbrev-table-put org-mode-abbrev-table
4729 :parents
(list text-mode-abbrev-table
)))
4731 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
4733 (defun org-current-time ()
4734 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
4735 (if (> (car org-time-stamp-rounding-minutes
) 1)
4736 (let ((r (car org-time-stamp-rounding-minutes
))
4737 (time (decode-time)))
4739 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
4743 ;;;; Font-Lock stuff, including the activators
4745 (defvar org-mouse-map
(make-sparse-keymap))
4746 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
4747 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
4748 (when org-mouse-1-follows-link
4749 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
4750 (when org-tab-follows-link
4751 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
4752 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
4754 (require 'font-lock
)
4756 (defconst org-non-link-chars
"]\t\n\r<>")
4757 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
4758 "shell" "elisp" "doi"))
4759 (defvar org-link-types-re nil
4760 "Matches a link that has a url-like prefix like \"http:\"")
4761 (defvar org-link-re-with-space nil
4762 "Matches a link with spaces, optional angular brackets around it.")
4763 (defvar org-link-re-with-space2 nil
4764 "Matches a link with spaces, optional angular brackets around it.")
4765 (defvar org-link-re-with-space3 nil
4766 "Matches a link with spaces, only for internal part in bracket links.")
4767 (defvar org-angle-link-re nil
4768 "Matches link with angular brackets, spaces are allowed.")
4769 (defvar org-plain-link-re nil
4770 "Matches plain link, without spaces.")
4771 (defvar org-bracket-link-regexp nil
4772 "Matches a link in double brackets.")
4773 (defvar org-bracket-link-analytic-regexp nil
4774 "Regular expression used to analyze links.
4775 Here is what the match groups contain after a match:
4781 (defvar org-bracket-link-analytic-regexp
++ nil
4782 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
4783 (defvar org-any-link-re nil
4784 "Regular expression matching any link.")
4786 (defcustom org-match-sexp-depth
3
4787 "Number of stacked braces for sub/superscript matching.
4788 This has to be set before loading org.el to be effective."
4789 :group
'org-export-translation
; ??????????????????????????/
4792 (defun org-create-multibrace-regexp (left right n
)
4793 "Create a regular expression which will match a balanced sexp.
4794 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
4795 as single character strings.
4796 The regexp returned will match the entire expression including the
4797 delimiters. It will also define a single group which contains the
4798 match except for the outermost delimiters. The maximum depth of
4799 stacked delimiters is N. Escaping delimiters is not possible."
4800 (let* ((nothing (concat "[^" left right
"]*?"))
4803 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
4806 re
(concat re or next
)
4807 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
4808 (concat left
"\\(" re
"\\)" right
)))
4810 (defvar org-match-substring-regexp
4812 "\\([^\\]\\)\\([_^]\\)\\("
4813 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
4815 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
4817 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
4818 "The regular expression matching a sub- or superscript.")
4820 (defvar org-match-substring-with-braces-regexp
4822 "\\([^\\]\\)\\([_^]\\)\\("
4823 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
4825 "The regular expression matching a sub- or superscript, forcing braces.")
4827 (defun org-make-link-regexps ()
4828 "Update the link regular expressions.
4829 This should be called after the variable `org-link-types' has changed."
4830 (setq org-link-types-re
4832 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
4833 org-link-re-with-space
4835 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
4836 "\\([^" org-non-link-chars
" ]"
4837 "[^" org-non-link-chars
"]*"
4838 "[^" org-non-link-chars
" ]\\)>?")
4839 org-link-re-with-space2
4841 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
4842 "\\([^" org-non-link-chars
" ]"
4844 "[^" org-non-link-chars
" ]\\)>?")
4845 org-link-re-with-space3
4847 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
4848 "\\([^" org-non-link-chars
" ]"
4852 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
4853 "\\([^" org-non-link-chars
" ]"
4854 "[^" org-non-link-chars
"]*"
4858 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
4859 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
4860 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
4861 org-bracket-link-regexp
4862 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
4863 org-bracket-link-analytic-regexp
4866 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
4869 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4871 org-bracket-link-analytic-regexp
++
4874 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
4877 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4880 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
4881 org-angle-link-re
"\\)\\|\\("
4882 org-plain-link-re
"\\)")))
4884 (org-make-link-regexps)
4886 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
4887 "Regular expression for fast time stamp matching.")
4888 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
4889 "Regular expression for fast time stamp matching.")
4890 (defconst org-ts-regexp0
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4891 "Regular expression matching time strings for analysis.
4892 This one does not require the space after the date, so it can be used
4893 on a string that terminates immediately after the date.")
4894 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4895 "Regular expression matching time strings for analysis.")
4896 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
4897 "Regular expression matching time stamps, with groups.")
4898 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
4899 "Regular expression matching time stamps (also [..]), with groups.")
4900 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
4901 "Regular expression matching a time stamp range.")
4902 (defconst org-tr-regexp-both
4903 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
4904 "Regular expression matching a time stamp range.")
4905 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
4906 org-ts-regexp
"\\)?")
4907 "Regular expression matching a time stamp or time stamp range.")
4908 (defconst org-tsr-regexp-both
(concat org-ts-regexp-both
"\\(--?-?"
4909 org-ts-regexp-both
"\\)?")
4910 "Regular expression matching a time stamp or time stamp range.
4911 The time stamps may be either active or inactive.")
4913 (defvar org-emph-face nil
)
4915 (defun org-do-emphasis-faces (limit)
4916 "Run through the buffer and add overlays to links."
4918 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
4919 (if (not (= (char-after (match-beginning 3))
4920 (char-after (match-beginning 4))))
4923 (setq a
(assoc (match-string 3) org-emphasis-alist
))
4924 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
4928 (org-remove-flyspell-overlays-in
4929 (match-beginning 0) (match-end 0)))
4930 (add-text-properties (match-beginning 2) (match-end 2)
4931 '(font-lock-multiline t org-emphasis t
))
4932 (when org-hide-emphasis-markers
4933 (add-text-properties (match-end 4) (match-beginning 5)
4934 '(invisible org-link
))
4935 (add-text-properties (match-beginning 3) (match-end 3)
4936 '(invisible org-link
)))))
4940 (defun org-emphasize (&optional char
)
4941 "Insert or change an emphasis, i.e. a font like bold or italic.
4942 If there is an active region, change that region to a new emphasis.
4943 If there is no region, just insert the marker characters and position
4944 the cursor between them.
4945 CHAR should be either the marker character, or the first character of the
4946 HTML tag associated with that emphasis. If CHAR is a space, the means
4947 to remove the emphasis of the selected region.
4948 If char is not given (for example in an interactive call) it
4949 will be prompted for."
4951 (let ((eal org-emphasis-alist
) e det
4952 (erc org-emphasis-regexp-components
)
4954 (string "") beg end move tag c s
)
4955 (if (org-region-active-p)
4956 (setq beg
(region-beginning) end
(region-end)
4957 string
(buffer-substring beg end
))
4960 (while (setq e
(pop eal
))
4961 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
4963 (push (cons c
(string-to-char (car e
))) det
)
4964 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
4965 (substring tag
1)))))
4966 (setq det
(nreverse det
))
4968 (message "%s" (concat "Emphasis marker or tag:" prompt
))
4969 (setq char
(read-char-exclusive)))
4970 (setq char
(or (cdr (assoc char det
)) char
))
4971 (if (equal char ?\
)
4972 (setq s
"" move nil
)
4973 (unless (assoc (char-to-string char
) org-emphasis-alist
)
4974 (error "No such emphasis marker: \"%c\"" char
))
4975 (setq s
(char-to-string char
)))
4976 (while (and (> (length string
) 1)
4977 (equal (substring string
0 1) (substring string -
1))
4978 (assoc (substring string
0 1) org-emphasis-alist
))
4979 (setq string
(substring string
1 -
1)))
4980 (setq string
(concat s string s
))
4981 (if beg
(delete-region beg end
))
4983 (string-match (concat "[" (nth 0 erc
) "\n]")
4984 (char-to-string (char-before (point)))))
4987 (string-match (concat "[" (nth 1 erc
) "\n]")
4988 (char-to-string (char-after (point)))))
4989 (insert " ") (backward-char 1))
4991 (and move
(backward-char 1))))
4993 (defconst org-nonsticky-props
4994 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text
))
4996 (defsubst org-rear-nonsticky-at
(pos)
4997 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
4999 (defun org-activate-plain-links (limit)
5000 "Run through the buffer and add overlays to links."
5003 (if (re-search-forward org-plain-link-re limit t
)
5005 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5006 (setq f
(get-text-property (match-beginning 0) 'face
))
5007 (if (or (eq f
'org-tag
)
5008 (and (listp f
) (memq 'org-tag f
)))
5010 (add-text-properties (match-beginning 0) (match-end 0)
5011 (list 'mouse-face
'highlight
5013 'keymap org-mouse-map
))
5014 (org-rear-nonsticky-at (match-end 0)))
5017 (defun org-activate-code (limit)
5018 (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t
)
5020 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5021 (remove-text-properties (match-beginning 0) (match-end 0)
5022 '(display t invisible t intangible t
))
5025 (defun org-fontify-meta-lines-and-blocks (limit)
5026 "Fontify #+ lines and blocks, in the correct ways."
5027 (let ((case-fold-search t
))
5028 (if (re-search-forward
5029 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
5031 (let ((beg (match-beginning 0))
5032 (beg1 (line-beginning-position 2))
5033 (dc1 (downcase (match-string 2)))
5034 (dc3 (downcase (match-string 3)))
5035 end end1 quoting block-type
)
5037 ((member dc1
'("html:" "ascii:" "latex:" "docbook:"))
5038 ;; a single line of backend-specific content
5039 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5040 (remove-text-properties (match-beginning 0) (match-end 0)
5041 '(display t invisible t intangible t
))
5042 (add-text-properties (match-beginning 1) (match-end 3)
5043 '(font-lock-fontified t face org-meta-line
))
5044 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5045 '(font-lock-fontified t face org-block
))
5046 ; for backend-specific code
5048 ((and (match-end 4) (equal dc3
"begin"))
5050 (setq block-type
(downcase (match-string 5))
5051 quoting
(member block-type org-protecting-blocks
))
5052 (when (re-search-forward
5053 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5054 nil t
) ;; on purpose, we look further than LIMIT
5055 (setq end
(match-end 0) end1
(1- (match-beginning 0)))
5057 (remove-text-properties beg end
5058 '(display t invisible t intangible t
)))
5059 (add-text-properties
5061 '(font-lock-fontified t font-lock-multiline t
))
5062 (add-text-properties beg beg1
'(face org-meta-line
))
5063 (add-text-properties end1
(+ end
1) '(face org-meta-line
))
5067 (add-text-properties beg1
(+ end1
1) '(face
5069 ; end of source block
5070 ((not org-fontify-quote-and-verse-blocks
))
5071 ((string= block-type
"quote")
5072 (add-text-properties beg1 end1
'(face org-quote
)))
5073 ((string= block-type
"verse")
5074 (add-text-properties beg1 end1
'(face org-verse
))))
5076 ((member dc1
'("title:" "author:" "email:" "date:"))
5077 (add-text-properties
5079 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5080 '(font-lock-fontified t invisible t
)
5081 '(font-lock-fontified t face org-document-info-keyword
)))
5082 (add-text-properties
5083 (match-beginning 6) (match-end 6)
5084 (if (string-equal dc1
"title:")
5085 '(font-lock-fontified t face org-document-title
)
5086 '(font-lock-fontified t face org-document-info
))))
5087 ((not (member (char-after beg
) '(?\ ?
\t)))
5088 ;; just any other in-buffer setting, but not indented
5089 (add-text-properties
5091 '(font-lock-fontified t face org-meta-line
))
5093 ((or (member dc1
'("begin:" "end:" "caption:" "label:"
5094 "orgtbl:" "tblfm:" "tblname:" "result:"
5095 "results:" "source:" "srcname:" "call:"))
5096 (and (match-end 4) (equal dc3
"attr")))
5097 (add-text-properties
5099 '(font-lock-fontified t face org-meta-line
))
5101 ((member dc3
'(" " ""))
5102 (add-text-properties
5104 '(font-lock-fontified t face font-lock-comment-face
)))
5107 (defun org-activate-angle-links (limit)
5108 "Run through the buffer and add overlays to links."
5109 (if (re-search-forward org-angle-link-re limit t
)
5111 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5112 (add-text-properties (match-beginning 0) (match-end 0)
5113 (list 'mouse-face
'highlight
5114 'keymap org-mouse-map
))
5115 (org-rear-nonsticky-at (match-end 0))
5118 (defun org-activate-footnote-links (limit)
5119 "Run through the buffer and add overlays to links."
5120 (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
5123 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5124 (add-text-properties (match-beginning 2) (match-end 2)
5125 (list 'mouse-face
'highlight
5126 'keymap org-mouse-map
5128 (if (= (point-at-bol) (match-beginning 2))
5129 "Footnote definition"
5130 "Footnote reference")
5132 (org-rear-nonsticky-at (match-end 2))
5135 (defun org-activate-bracket-links (limit)
5136 "Run through the buffer and add overlays to bracketed links."
5137 (if (re-search-forward org-bracket-link-regexp limit t
)
5138 (let* ((help (concat "LINK: "
5139 (org-match-string-no-properties 1)))
5140 ;; FIXME: above we should remove the escapes.
5141 ;; but that requires another match, protecting match data,
5142 ;; a lot of overhead for font-lock.
5143 (ip (org-maybe-intangible
5144 (list 'invisible
'org-link
5145 'keymap org-mouse-map
'mouse-face
'highlight
5146 'font-lock-multiline t
'help-echo help
)))
5147 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5148 'font-lock-multiline t
'help-echo help
)))
5149 ;; We need to remove the invisible property here. Table narrowing
5150 ;; may have made some of this invisible.
5151 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5152 (remove-text-properties (match-beginning 0) (match-end 0)
5156 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5157 (org-rear-nonsticky-at (match-beginning 3))
5158 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5159 (org-rear-nonsticky-at (match-end 3))
5160 (add-text-properties (match-end 3) (match-end 0) ip
)
5161 (org-rear-nonsticky-at (match-end 0)))
5162 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5163 (org-rear-nonsticky-at (match-beginning 1))
5164 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5165 (org-rear-nonsticky-at (match-end 1))
5166 (add-text-properties (match-end 1) (match-end 0) ip
)
5167 (org-rear-nonsticky-at (match-end 0)))
5170 (defun org-activate-dates (limit)
5171 "Run through the buffer and add overlays to dates."
5172 (if (re-search-forward org-tsr-regexp-both limit t
)
5174 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5175 (add-text-properties (match-beginning 0) (match-end 0)
5176 (list 'mouse-face
'highlight
5177 'keymap org-mouse-map
))
5178 (org-rear-nonsticky-at (match-end 0))
5179 (when org-display-custom-times
5181 (org-display-custom-time (match-beginning 3) (match-end 3)))
5182 (org-display-custom-time (match-beginning 1) (match-end 1)))
5185 (defvar org-target-link-regexp nil
5186 "Regular expression matching radio targets in plain text.")
5187 (make-variable-buffer-local 'org-target-link-regexp
)
5188 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5189 "Regular expression matching a link target.")
5190 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5191 "Regular expression matching a radio target.")
5192 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5193 "Regular expression matching any target.")
5195 (defun org-activate-target-links (limit)
5196 "Run through the buffer and add overlays to target matches."
5197 (when org-target-link-regexp
5198 (let ((case-fold-search t
))
5199 (if (re-search-forward org-target-link-regexp limit t
)
5201 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5202 (add-text-properties (match-beginning 0) (match-end 0)
5203 (list 'mouse-face
'highlight
5204 'keymap org-mouse-map
5205 'help-echo
"Radio target link"
5206 'org-linked-text t
))
5207 (org-rear-nonsticky-at (match-end 0))
5210 (defun org-update-radio-target-regexp ()
5211 "Find all radio targets in this file and update the regular expression."
5213 (when (memq 'radio org-activate-links
)
5214 (setq org-target-link-regexp
5215 (org-make-target-link-regexp (org-all-targets 'radio
)))
5216 (org-restart-font-lock)))
5218 (defun org-hide-wide-columns (limit)
5220 (setq s
(text-property-any (point) (or limit
(point-max))
5223 (setq e
(next-single-property-change s
'org-cwidth
))
5224 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5228 (defvar org-latex-and-specials-regexp nil
5229 "Regular expression for highlighting export special stuff.")
5230 (defvar org-match-substring-regexp
)
5231 (defvar org-match-substring-with-braces-regexp
)
5233 ;; This should be with the exporter code, but we also use if for font-locking
5234 (defconst org-export-html-special-string-regexps
5235 '(("\\\\-" .
"­")
5236 ("---\\([^-]\\)" .
"—\\1")
5237 ("--\\([^-]\\)" .
"–\\1")
5238 ("\\.\\.\\." .
"…"))
5239 "Regular expressions for special string conversion.")
5242 (defun org-compute-latex-and-specials-regexp ()
5243 "Compute regular expression for stuff treated specially by exporters."
5244 (if (not org-highlight-latex-fragments-and-specials
)
5245 (org-set-local 'org-latex-and-specials-regexp nil
)
5248 ((matchers (plist-get org-format-latex-options
:matchers
))
5249 (latexs (delq nil
(mapcar (lambda (x) (if (member (car x
) matchers
) x
))
5250 org-latex-regexps
)))
5251 (org-export-allow-BIND nil
)
5252 (options (org-combine-plists (org-default-export-plist)
5253 (org-infile-export-plist)))
5254 (org-export-with-sub-superscripts (plist-get options
:sub-superscript
))
5255 (org-export-with-LaTeX-fragments (plist-get options
:LaTeX-fragments
))
5256 (org-export-with-TeX-macros (plist-get options
:TeX-macros
))
5257 (org-export-html-expand (plist-get options
:expand-quoted-html
))
5258 (org-export-with-special-strings (plist-get options
:special-strings
))
5261 ((equal org-export-with-sub-superscripts
'{})
5262 (list org-match-substring-with-braces-regexp
))
5263 (org-export-with-sub-superscripts
5264 (list org-match-substring-regexp
))
5267 (if org-export-with-LaTeX-fragments
5268 (mapcar (lambda (x) (nth 1 x
)) latexs
)))
5270 (if org-export-with-TeX-macros
5271 (list (concat "\\\\"
5277 (append org-entities-user
5279 (if (boundp 'org-latex-entities
)
5281 (or (car-safe x
) x
))
5286 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5287 (re-special (if org-export-with-special-strings
5288 (mapcar (lambda (x) (car x
))
5289 org-export-html-special-string-regexps
)))
5293 (if org-export-html-expand
"@<[^>\n]+>")
5296 'org-latex-and-specials-regexp
5297 (mapconcat 'identity
(append re-latex re-sub re-macros re-special
5300 (defun org-do-latex-and-special-faces (limit)
5301 "Run through the buffer and add overlays to links."
5302 (when org-latex-and-specials-regexp
5304 (while (and (not rtn
) (re-search-forward org-latex-and-specials-regexp
5306 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5308 '(org-code org-verbatim underline
)))
5311 d
(cond ((member (char-after (1+ (match-beginning 0)))
5314 (font-lock-prepend-text-property
5315 (+ d
(match-beginning 0)) (match-end 0)
5316 'face
'org-latex-and-export-specials
)
5317 (add-text-properties (+ d
(match-beginning 0)) (match-end 0)
5318 '(font-lock-multiline t
)))))
5321 (defun org-restart-font-lock ()
5322 "Restart `font-lock-mode', to force refontification."
5323 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5325 (font-lock-mode 1)))
5327 (defun org-all-targets (&optional radio
)
5328 "Return a list of all targets in this file.
5329 With optional argument RADIO, only find radio targets."
5330 (let ((re (if radio org-radio-target-regexp org-target-regexp
))
5333 (goto-char (point-min))
5334 (while (re-search-forward re nil t
)
5335 (add-to-list 'rtn
(downcase (org-match-string-no-properties 1))))
5338 (defun org-make-target-link-regexp (targets)
5339 "Make regular expression matching all strings in TARGETS.
5340 The regular expression finds the targets also if there is a line break
5347 (while (string-match " +" x
)
5348 (setq x
(replace-match "\\s-+" t t x
)))
5354 (defun org-activate-tags (limit)
5355 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5357 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5358 (add-text-properties (match-beginning 1) (match-end 1)
5359 (list 'mouse-face
'highlight
5360 'keymap org-mouse-map
))
5361 (org-rear-nonsticky-at (match-end 1))
5364 (defun org-outline-level ()
5365 "Compute the outline level of the heading at point.
5366 This function assumes that the cursor is at the beginning of a line matched
5367 by `outline-regexp'. Otherwise it returns garbage.
5368 If this is called at a normal headline, the level is the number of stars.
5369 Use `org-reduced-level' to remove the effect of `org-odd-levels'.
5370 For plain list items, if they are matched by `outline-regexp', this returns
5371 1000 plus the line indentation."
5373 (looking-at outline-regexp
)
5374 (if (match-beginning 1)
5375 (+ (org-get-string-indentation (match-string 1)) 1000)
5376 (1- (- (match-end 0) (match-beginning 0))))))
5378 (defvar org-font-lock-keywords nil
)
5380 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5381 "Regular expression matching a property line.")
5383 (defvar org-font-lock-hook nil
5384 "Functions to be called for special font lock stuff.")
5386 (defvar org-font-lock-set-keywords-hook nil
5387 "Functions that can manipulate `org-font-lock-extra-keywords'.
5388 This is calles after `org-font-lock-extra-keywords' is defined, but before
5389 it is installed to be used by font lock. This can be useful if something
5390 needs to be inserted at a specific position in the font-lock sequence.")
5392 (defun org-font-lock-hook (limit)
5393 (run-hook-with-args 'org-font-lock-hook limit
))
5395 (defun org-set-font-lock-defaults ()
5396 (let* ((em org-fontify-emphasized-text
)
5397 (lk org-activate-links
)
5398 (org-font-lock-extra-keywords
5401 '(org-font-lock-hook)
5403 `(,(if org-fontify-whole-heading-line
5404 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5405 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5406 (1 (org-get-level-face 1))
5407 (2 (org-get-level-face 2))
5408 (3 (org-get-level-face 3)))
5410 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5413 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
5414 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
5415 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
5416 '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t
))
5418 (list org-drawer-regexp
'(0 'org-special-keyword t
))
5419 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
5421 (list org-property-re
5422 '(1 'org-special-keyword t
)
5423 '(3 'org-property-value t
))
5425 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
5426 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
5427 (if (memq 'plain lk
) '(org-activate-plain-links))
5428 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
5429 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
5430 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
5431 (if (memq 'footnote lk
) '(org-activate-footnote-links
5432 (2 'org-footnote t
)))
5433 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
5434 '(org-hide-wide-columns (0 nil append
))
5436 (list (concat "^\\*+[ \t]+" org-todo-regexp
"\\([ \t]\\|$\\)")
5437 '(1 (org-get-todo-face 1) t
))
5439 (if org-fontify-done-headline
5440 (list (concat "^[*]+ +\\<\\("
5441 (mapconcat 'regexp-quote org-done-keywords
"\\|")
5443 '(2 'org-headline-done t
))
5446 '(org-font-lock-add-priority-faces)
5448 '(org-font-lock-add-tag-faces)
5450 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
5451 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
5452 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
5453 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
5456 (if (featurep 'xemacs
)
5457 '(org-do-emphasis-faces (0 nil append
))
5458 '(org-do-emphasis-faces)))
5460 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
5461 1 'org-checkbox prepend
)
5462 (if (cdr (assq 'checkbox org-list-automatic-rules
))
5463 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5464 (0 (org-get-checkbox-statistics-face) t
)))
5465 ;; Description list items
5466 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\(.*? ::\\)"
5468 ;; ARCHIVEd headings
5469 (list (concat "^\\*+ \\(.*:" org-archive-tag
":.*\\)")
5470 '(1 'org-archived prepend
))
5472 '(org-do-latex-and-special-faces)
5473 '(org-fontify-entities)
5474 '(org-raise-scripts)
5476 '(org-activate-code (1 'org-code t
))
5478 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
5479 "\\|" org-quote-string
"\\)\\>")
5480 '(1 'org-special-keyword t
))
5481 '("^#.*" (0 'font-lock-comment-face t
))
5482 ;; Blocks and meta lines
5483 '(org-fontify-meta-lines-and-blocks)
5485 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
5486 (run-hooks 'org-font-lock-set-keywords-hook
)
5487 ;; Now set the full font-lock-keywords
5488 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
5489 (org-set-local 'font-lock-defaults
5490 '(org-font-lock-keywords t nil nil backward-paragraph
))
5491 (kill-local-variable 'font-lock-keywords
) nil
))
5493 (defun org-toggle-pretty-entities ()
5494 "Toggle the composition display of entities as UTF8 characters."
5496 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
5497 (org-restart-font-lock)
5498 (if org-pretty-entities
5499 (message "Entities are displayed as UTF8 characers")
5502 (decompose-region (point-min) (point-max))
5503 (message "Entities are displayed plain"))))
5505 (defun org-fontify-entities (limit)
5506 "Find an entity to fontify."
5508 (when org-pretty-entities
5510 (while (re-search-forward
5511 "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
5513 (if (and (not (org-in-indented-comment-line))
5514 (setq ee
(org-entity-get (match-string 1)))
5515 (= (length (nth 6 ee
)) 1))
5517 (add-text-properties
5518 (match-beginning 0) (match-end 1)
5519 (list 'font-lock-fontified t
))
5520 (compose-region (match-beginning 0) (match-end 1)
5526 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
5527 "Fontify string S like in Org-mode."
5530 (let ((org-odd-levels-only odd-levels
))
5532 (font-lock-fontify-buffer)
5538 (defun org-get-level-face (n)
5539 "Get the right face for match N in font-lock matching of headlines."
5540 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
5541 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
5542 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
5544 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
5546 (t (if org-level-color-stars-only nil org-f
))))
5548 (defun org-get-todo-face (kwd)
5549 "Get the right face for a TODO keyword KWD.
5550 If KWD is a number, get the corresponding match group."
5551 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5552 (or (org-face-from-face-or-color
5553 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
5554 (and (member kwd org-done-keywords
) 'org-done
)
5557 (defun org-face-from-face-or-color (context inherit face-or-color
)
5558 "Create a face list that inherits INHERIT, but sets the foreground color.
5559 When FACE-OR-COLOR is not a string, just return it."
5560 (if (stringp face-or-color
)
5561 (list :inherit inherit
5562 (cdr (assoc context org-faces-easy-properties
))
5566 (defun org-font-lock-add-tag-faces (limit)
5567 "Add the special tag faces."
5568 (when (and org-tag-faces org-tags-special-faces-re
)
5569 (while (re-search-forward org-tags-special-faces-re limit t
)
5570 (add-text-properties (match-beginning 1) (match-end 1)
5571 (list 'face
(org-get-tag-face 1)
5572 'font-lock-fontified t
))
5573 (backward-char 1))))
5575 (defun org-font-lock-add-priority-faces (limit)
5576 "Add the special priority faces."
5577 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
5578 (add-text-properties
5579 (match-beginning 0) (match-end 0)
5580 (list 'face
(or (org-face-from-face-or-color
5581 'priority
'org-special-keyword
5582 (cdr (assoc (char-after (match-beginning 1))
5583 org-priority-faces
)))
5584 'org-special-keyword
)
5585 'font-lock-fontified t
))))
5587 (defun org-get-tag-face (kwd)
5588 "Get the right face for a TODO keyword KWD.
5589 If KWD is a number, get the corresponding match group."
5590 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5591 (or (org-face-from-face-or-color
5592 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
5595 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
5596 "Remove fontification and activation overlays from links."
5597 (font-lock-default-unfontify-region beg end
)
5598 (let* ((buffer-undo-list t
)
5599 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
5600 (inhibit-modification-hooks t
)
5601 deactivate-mark buffer-file-name buffer-file-truename
)
5602 (decompose-region beg end
)
5603 (remove-text-properties
5606 ;; also remove line-prefix and wrap-prefix properties
5607 '(mouse-face t keymap t org-linked-text t
5608 invisible t intangible t
5609 line-prefix t wrap-prefix t
5610 org-no-flyspell t org-emphasis t
)
5611 '(mouse-face t keymap t org-linked-text t
5612 invisible t intangible t
5613 org-no-flyspell t org-emphasis t
)))
5614 (org-remove-font-lock-display-properties beg end
)))
5616 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
5617 ((raise 0.3) (height 0.7))
5620 "Display properties for showing superscripts and subscripts.")
5622 (defun org-remove-font-lock-display-properties (beg end
)
5623 "Remove specific display properties that have been added by font lock.
5624 The will remove the raise properties that are used to show superscripts
5628 (setq next
(next-single-property-change beg
'display nil end
)
5629 prop
(get-text-property beg
'display
))
5630 (if (member prop org-script-display
)
5631 (put-text-property beg next
'display nil
))
5634 (defun org-raise-scripts (limit)
5635 "Add raise properties to sub/superscripts."
5636 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
5637 (if (re-search-forward
5638 (if (eq org-use-sub-superscripts t
)
5639 org-match-substring-regexp
5640 org-match-substring-with-braces-regexp
)
5642 (let* ((pos (point)) table-p comment-p
5643 (mpos (match-beginning 3))
5644 (emph-p (get-text-property mpos
'org-emphasis
))
5645 (link-p (get-text-property mpos
'mouse-face
))
5646 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
5647 (goto-char (point-at-bol))
5648 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
5649 comment-p
(org-looking-at-p "[ \t]*#"))
5651 ;; FIXME: Should we go back one character here, for a_b^c
5652 ;; (goto-char (1- pos)) ;????????????????????
5653 (if (or comment-p emph-p link-p keyw-p
)
5655 (put-text-property (match-beginning 3) (match-end 0)
5657 (if (equal (char-after (match-beginning 2)) ?^
)
5658 (nth (if table-p
3 1) org-script-display
)
5659 (nth (if table-p
2 0) org-script-display
)))
5660 (add-text-properties (match-beginning 2) (match-end 2)
5662 'org-dwidth t
'org-dwidth-n
1))
5663 (if (and (eq (char-after (match-beginning 3)) ?
{)
5664 (eq (char-before (match-end 3)) ?
}))
5666 (add-text-properties
5667 (match-beginning 3) (1+ (match-beginning 3))
5668 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
5669 (add-text-properties
5670 (1- (match-end 3)) (match-end 3)
5671 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
5674 ;;;; Visibility cycling, including org-goto and indirect buffer
5678 (defvar org-cycle-global-status nil
)
5679 (make-variable-buffer-local 'org-cycle-global-status
)
5680 (defvar org-cycle-subtree-status nil
)
5681 (make-variable-buffer-local 'org-cycle-subtree-status
)
5685 (defvar org-inlinetask-min-level
)
5687 (defun org-cycle (&optional arg
)
5688 "TAB-action and visibility cycling for Org-mode.
5690 This is the command invoked in Org-mode by the TAB key. Its main purpose
5691 is outline visibility cycling, but it also invokes other actions
5692 in special contexts.
5694 - When this function is called with a prefix argument, rotate the entire
5695 buffer through 3 states (global cycling)
5696 1. OVERVIEW: Show only top-level headlines.
5697 2. CONTENTS: Show all headlines of all levels, but no body text.
5698 3. SHOW ALL: Show everything.
5699 When called with two `C-u C-u' prefixes, switch to the startup visibility,
5700 determined by the variable `org-startup-folded', and by any VISIBILITY
5701 properties in the buffer.
5702 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
5703 including any drawers.
5705 - When inside a table, re-align the table and move to the next field.
5707 - When point is at the beginning of a headline, rotate the subtree started
5708 by this line through 3 different states (local cycling)
5709 1. FOLDED: Only the main headline is shown.
5710 2. CHILDREN: The main headline and the direct children are shown.
5711 From this state, you can move to one of the children
5712 and zoom in further.
5713 3. SUBTREE: Show the entire subtree, including body text.
5714 If there is no subtree, switch directly from CHILDREN to FOLDED.
5716 - When point is at the beginning of an empty headline and the variable
5717 `org-cycle-level-after-item/entry-creation' is set, cycle the level
5718 of the headline by demoting and promoting it to likely levels. This
5719 speeds up creation document structure by pressing TAB once or several
5720 times right after creating a new headline.
5722 - When there is a numeric prefix, go up to a heading with level ARG, do
5723 a `show-subtree' and return to the previous cursor position. If ARG
5724 is negative, go up that many levels.
5726 - When point is not at the beginning of a headline, execute the global
5727 binding for TAB, which is re-indenting the line. See the option
5728 `org-cycle-emulate-tab' for details.
5730 - Special case: if point is at the beginning of the buffer and there is
5731 no headline in line 1, this function will act as if called with prefix arg.
5732 But only if also the variable `org-cycle-global-at-bob' is t."
5734 (org-load-modules-maybe)
5735 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
5736 (and org-cycle-level-after-item
/entry-creation
5737 (or (org-cycle-level)
5738 (org-cycle-item-indentation))))
5740 (or org-cycle-max-level
5741 (and (boundp 'org-inlinetask-min-level
)
5742 org-inlinetask-min-level
5743 (1- org-inlinetask-min-level
))))
5744 (nstars (and limit-level
5745 (if org-odd-levels-only
5746 (and limit-level
(1- (* limit-level
2)))
5750 ((not (org-mode-p)) outline-regexp
)
5751 ((or (eq org-cycle-include-plain-lists
'integrate
)
5752 (and org-cycle-include-plain-lists
(org-at-item-p)))
5754 (if nstars
(format "\\{1,%d\\}" nstars
) "+")
5755 " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
5756 (t (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ ")))))
5757 (bob-special (and org-cycle-global-at-bob
(bobp)
5758 (not (looking-at outline-regexp
))))
5761 (delq 'org-optimize-window-after-visibility-change
5762 (copy-sequence org-cycle-hook
))
5766 (if (or bob-special
(equal arg
'(4)))
5767 ;; special case: use global cycling
5773 (org-set-startup-visibility)
5774 (message "Startup visibility, plus VISIBILITY properties"))
5778 (message "Entire buffer visible, including drawers"))
5780 ((org-at-table-p 'any
)
5781 ;; Enter the table or move to the next field in the table
5782 (if (org-at-table.el-p
)
5783 (message "Use C-c ' to edit table.el tables")
5784 (if arg
(org-table-edit-field t
)
5785 (org-table-justify-field-maybe)
5786 (call-interactively 'org-table-next-field
))))
5788 ((run-hook-with-args-until-success
5789 'org-tab-after-check-for-table-hook
))
5791 ((eq arg t
) ;; Global cycling
5792 (org-cycle-internal-global))
5794 ((and org-drawers org-drawer-regexp
5796 (beginning-of-line 1)
5797 (looking-at org-drawer-regexp
)))
5798 ;; Toggle block visibility
5800 (not (get-char-property (match-end 0) 'invisible
))))
5803 ;; Show-subtree, ARG levels up from here.
5805 (org-back-to-heading)
5806 (outline-up-heading (if (< arg
0) (- arg
)
5807 (- (funcall outline-level
) arg
)))
5808 (org-show-subtree)))
5810 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp
))
5811 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
5813 (org-cycle-internal-local))
5815 ;; TAB emulation and template completion
5816 (buffer-read-only (org-back-to-heading))
5818 ((run-hook-with-args-until-success
5819 'org-tab-after-check-for-cycling-hook
))
5821 ((org-try-structure-completion))
5823 ((org-try-cdlatex-tab))
5825 ((run-hook-with-args-until-success
5826 'org-tab-before-tab-emulation-hook
))
5828 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
5830 (not (looking-at outline-regexp
))))
5831 (call-interactively (global-key-binding "\t")))
5833 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
5834 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
5835 (or (and (eq org-cycle-emulate-tab
'white
)
5836 (= (match-end 0) (point-at-eol)))
5837 (and (eq org-cycle-emulate-tab
'whitestart
)
5838 (>= (match-end 0) pos
))))
5840 (eq org-cycle-emulate-tab t
))
5841 (call-interactively (global-key-binding "\t")))
5844 (org-back-to-heading)
5847 (defun org-cycle-internal-global ()
5848 "Do the global cycling action."
5850 ((and (eq last-command this-command
)
5851 (eq org-cycle-global-status
'overview
))
5852 ;; We just created the overview - now do table of contents
5853 ;; This can be slow in very large buffers, so indicate action
5854 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
5855 (message "CONTENTS...")
5857 (message "CONTENTS...done")
5858 (setq org-cycle-global-status
'contents
)
5859 (run-hook-with-args 'org-cycle-hook
'contents
))
5861 ((and (eq last-command this-command
)
5862 (eq org-cycle-global-status
'contents
))
5863 ;; We just showed the table of contents - now show everything
5864 (run-hook-with-args 'org-pre-cycle-hook
'all
)
5866 (message "SHOW ALL")
5867 (setq org-cycle-global-status
'all
)
5868 (run-hook-with-args 'org-cycle-hook
'all
))
5871 ;; Default action: go to overview
5872 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
5874 (message "OVERVIEW")
5875 (setq org-cycle-global-status
'overview
)
5876 (run-hook-with-args 'org-cycle-hook
'overview
))))
5878 (defun org-cycle-internal-local ()
5879 "Do the local cycling action."
5880 (let ((goal-column 0) eoh eol eos level has-children children-skipped
)
5881 ;; First, some boundaries
5883 (org-back-to-heading)
5884 (setq level
(funcall outline-level
))
5886 (beginning-of-line 2)
5887 (if (or (featurep 'xemacs
) (<= emacs-major-version
21))
5888 ; XEmacs does not have `next-single-char-property-change'
5889 ; I'm not sure about Emacs 21.
5890 (while (and (not (eobp)) ;; this is like `next-line'
5891 (get-char-property (1- (point)) 'invisible
))
5892 (beginning-of-line 2))
5893 (while (and (not (eobp)) ;; this is like `next-line'
5894 (get-char-property (1- (point)) 'invisible
))
5895 (goto-char (next-single-char-property-change (point) 'invisible
))
5896 (and (eolp) (beginning-of-line 2))))
5898 (outline-end-of-heading) (setq eoh
(point))
5900 (outline-next-heading)
5901 (setq has-children
(and (org-at-heading-p t
)
5902 (> (funcall outline-level
) level
))))
5903 ;; if we're in a list, org-end-of-subtree is in fact org-end-of-item.
5905 (setq eos
(if (and (org-end-of-item) (bolp))
5908 (org-end-of-subtree t
)
5910 (skip-chars-forward " \t\n"))
5911 (setq eos
(if (eobp) (point) (1- (point))))))
5912 ;; Find out what to do next and set `this-command'
5915 ;; Nothing is hidden behind this heading
5916 (run-hook-with-args 'org-pre-cycle-hook
'empty
)
5917 (message "EMPTY ENTRY")
5918 (setq org-cycle-subtree-status nil
)
5921 (outline-next-heading)
5922 (if (org-invisible-p) (org-flag-heading nil
))))
5923 ((and (or (>= eol eos
)
5924 (not (string-match "\\S-" (buffer-substring eol eos
))))
5926 (not (setq children-skipped
5927 org-cycle-skip-children-state-if-no-children
))))
5928 ;; Entire subtree is hidden in one line: children view
5929 (run-hook-with-args 'org-pre-cycle-hook
'children
)
5932 (message "CHILDREN")
5935 (outline-next-heading)
5936 (if (org-invisible-p) (org-flag-heading nil
)))
5937 (setq org-cycle-subtree-status
'children
)
5938 (run-hook-with-args 'org-cycle-hook
'children
))
5939 ((or children-skipped
5940 (and (eq last-command this-command
)
5941 (eq org-cycle-subtree-status
'children
)))
5942 ;; We just showed the children, or no children are there,
5943 ;; now show everything.
5944 (run-hook-with-args 'org-pre-cycle-hook
'subtree
)
5945 (outline-flag-region eoh eos nil
)
5946 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
5947 (setq org-cycle-subtree-status
'subtree
)
5948 (run-hook-with-args 'org-cycle-hook
'subtree
))
5950 ;; Default action: hide the subtree.
5951 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
5952 (outline-flag-region eoh eos t
)
5954 (setq org-cycle-subtree-status
'folded
)
5955 (run-hook-with-args 'org-cycle-hook
'folded
)))))
5958 (defun org-global-cycle (&optional arg
)
5959 "Cycle the global visibility. For details see `org-cycle'.
5960 With \\[universal-argument] prefix arg, switch to startup visibility.
5961 With a numeric prefix, show all headlines up to that level."
5963 (let ((org-cycle-include-plain-lists
5964 (if (org-mode-p) org-cycle-include-plain-lists nil
)))
5968 (hide-sublevels arg
)
5969 (setq org-cycle-global-status
'contents
))
5971 (org-set-startup-visibility)
5972 (message "Startup visibility, plus VISIBILITY properties."))
5974 (org-cycle '(4))))))
5976 (defun org-set-startup-visibility ()
5977 "Set the visibility required by startup options and properties."
5979 ((eq org-startup-folded t
)
5981 ((eq org-startup-folded
'content
)
5982 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
5983 (org-cycle '(4)) (org-cycle '(4)))))
5984 (unless (eq org-startup-folded
'showeverything
)
5985 (if org-hide-block-startup
(org-hide-block-all))
5986 (org-set-visibility-according-to-property 'no-cleanup
)
5987 (org-cycle-hide-archived-subtrees 'all
)
5988 (org-cycle-hide-drawers 'all
)
5989 (org-cycle-show-empty-lines t
)))
5991 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
5992 "Switch subtree visibilities according to :VISIBILITY: property."
5994 (let (org-show-entry-below state
)
5996 (goto-char (point-max))
5997 (while (re-search-backward
5998 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6000 (setq state
(match-string 1))
6002 (org-back-to-heading t
)
6006 ((equal state
'("fold" "folded"))
6008 ((equal state
"children")
6009 (org-show-hidden-entry)
6011 ((equal state
"content")
6014 (org-narrow-to-subtree)
6016 ((member state
'("all" "showall"))
6019 (org-cycle-hide-archived-subtrees 'all
)
6020 (org-cycle-hide-drawers 'all
)
6021 (org-cycle-show-empty-lines 'all
)))))
6023 (defun org-overview ()
6024 "Switch to overview mode, showing only top-level headlines.
6025 Really, this shows all headlines with level equal or greater than the level
6026 of the first headline in the buffer. This is important, because if the
6027 first headline is not level one, then (hide-sublevels 1) gives confusing
6030 (let ((level (save-excursion
6031 (goto-char (point-min))
6032 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6034 (goto-char (match-beginning 0))
6035 (funcall outline-level
))))))
6036 (and level
(hide-sublevels level
))))
6038 (defun org-content (&optional arg
)
6039 "Show all headlines in the buffer, like a table of contents.
6040 With numerical argument N, show content up to level N."
6043 ;; Visit all headings and show their offspring
6044 (and (integerp arg
) (org-overview))
6045 (goto-char (point-max))
6047 (while (and (progn (condition-case nil
6048 (outline-previous-visible-heading 1)
6049 (error (goto-char (point-min))))
6051 (looking-at outline-regexp
))
6053 (show-children (1- arg
))
6055 (if (bobp) (throw 'exit nil
))))))
6058 (defun org-optimize-window-after-visibility-change (state)
6059 "Adjust the window after a change in outline visibility.
6060 This function is the default value of the hook `org-cycle-hook'."
6061 (when (get-buffer-window (current-buffer))
6063 ((eq state
'content
) nil
)
6064 ((eq state
'all
) nil
)
6065 ((eq state
'folded
) nil
)
6066 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6067 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6069 (defun org-remove-empty-overlays-at (pos)
6070 "Remove outline overlays that do not contain non-white stuff."
6073 (and (eq 'outline
(overlay-get o
'invisible
))
6074 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6076 (delete-overlay o
)))
6079 (defun org-clean-visibility-after-subtree-move ()
6080 "Fix visibility issues after moving a subtree."
6081 ;; First, find a reasonable region to look at:
6082 ;; Start two siblings above, end three below
6083 (let* ((beg (save-excursion
6084 (and (org-get-last-sibling)
6085 (org-get-last-sibling))
6087 (end (save-excursion
6088 (and (org-get-next-sibling)
6089 (org-get-next-sibling)
6090 (org-get-next-sibling))
6091 (if (org-at-heading-p)
6094 (level (looking-at "\\*+"))
6095 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6098 (narrow-to-region beg end
)
6100 ;; Properly fold already folded siblings
6101 (goto-char (point-min))
6102 (while (re-search-forward re nil t
)
6103 (if (and (not (org-invisible-p))
6105 (goto-char (point-at-eol)) (org-invisible-p)))
6107 (org-cycle-show-empty-lines 'overview
)
6108 (org-cycle-hide-drawers 'overview
)))))
6110 (defun org-cycle-show-empty-lines (state)
6111 "Show empty lines above all visible headlines.
6112 The region to be covered depends on STATE when called through
6113 `org-cycle-hook'. Lisp program can use t for STATE to get the
6114 entire buffer covered. Note that an empty line is only shown if there
6115 are at least `org-cycle-separator-lines' empty lines before the headline."
6116 (when (not (= org-cycle-separator-lines
0))
6118 (let* ((n (abs org-cycle-separator-lines
))
6120 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6121 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6122 (t (let ((ns (number-to-string (- n
2))))
6123 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6124 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6127 ((memq state
'(overview contents t
))
6128 (setq beg
(point-min) end
(point-max)))
6129 ((memq state
'(children folded
))
6130 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6131 (beginning-of-line 2)
6135 (while (re-search-forward re end t
)
6136 (unless (get-char-property (match-end 1) 'invisible
)
6137 (setq e
(match-end 1))
6138 (if (< org-cycle-separator-lines
0)
6139 (setq b
(save-excursion
6140 (goto-char (match-beginning 0))
6141 (org-back-over-empty-lines)
6143 (goto-char (max (point-min) (1- (point))))
6147 (setq b
(match-beginning 1)))
6148 (outline-flag-region b e nil
)))))))
6149 ;; Never hide empty lines at the end of the file.
6151 (goto-char (point-max))
6152 (outline-previous-heading)
6153 (outline-end-of-heading)
6154 (if (and (looking-at "[ \t\n]+")
6155 (= (match-end 0) (point-max)))
6156 (outline-flag-region (point) (match-end 0) nil
))))
6158 (defun org-show-empty-lines-in-parent ()
6159 "Move to the parent and re-show empty lines before visible headlines."
6161 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6162 (org-cycle-show-empty-lines context
))))
6164 (defun org-files-list ()
6165 "Return `org-agenda-files' list, plus all open org-mode files.
6166 This is useful for operations that need to scan all of a user's
6167 open and agenda-wise Org files."
6168 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6169 (dolist (buf (buffer-list))
6170 (with-current-buffer buf
6171 (if (and (eq major-mode
'org-mode
) (buffer-file-name))
6172 (let ((file (expand-file-name (buffer-file-name))))
6173 (unless (member file files
)
6174 (push file files
))))))
6177 (defsubst org-entry-beginning-position
()
6178 "Return the beginning position of the current entry."
6179 (save-excursion (outline-back-to-heading t
) (point)))
6181 (defsubst org-entry-end-position
()
6182 "Return the end position of the current entry."
6183 (save-excursion (outline-next-heading) (point)))
6185 (defun org-cycle-hide-drawers (state)
6186 "Re-hide all drawers after a visibility state change."
6187 (when (and (org-mode-p)
6188 (not (memq state
'(overview folded contents
))))
6190 (let* ((globalp (memq state
'(contents all
)))
6191 (beg (if globalp
(point-min) (point)))
6192 (end (if globalp
(point-max)
6193 (if (eq state
'children
)
6194 (save-excursion (outline-next-heading) (point))
6195 (org-end-of-subtree t
)))))
6197 (while (re-search-forward org-drawer-regexp end t
)
6198 (org-flag-drawer t
))))))
6200 (defun org-flag-drawer (flag)
6202 (beginning-of-line 1)
6203 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6204 (let ((b (match-end 0))
6205 (outline-regexp org-outline-regexp
))
6206 (if (re-search-forward
6208 (save-excursion (outline-next-heading) (point)) t
)
6209 (outline-flag-region b
(point-at-eol) flag
)
6210 (error ":END: line missing at position %s" b
))))))
6212 (defun org-subtree-end-visible-p ()
6213 "Is the end of the current subtree visible?"
6214 (pos-visible-in-window-p
6215 (save-excursion (org-end-of-subtree t
) (point))))
6217 (defun org-first-headline-recenter (&optional N
)
6218 "Move cursor to the first headline and recenter the headline.
6219 Optional argument N means put the headline into the Nth line of the window."
6220 (goto-char (point-min))
6221 (when (re-search-forward (concat "^\\(" outline-regexp
"\\)") nil t
)
6223 (recenter (prefix-numeric-value N
))))
6225 ;;; Saving and restoring visibility
6227 (defun org-outline-overlay-data (&optional use-markers
)
6228 "Return a list of the locations of all outline overlays.
6229 The are overlays with the `invisible' property value `outline'.
6230 The return values is a list of cons cells, with start and stop
6231 positions for each overlay.
6232 If USE-MARKERS is set, return the positions as markers."
6239 (when (eq (overlay-get o
'invisible
) 'outline
)
6240 (setq beg
(overlay-start o
)
6241 end
(overlay-end o
))
6242 (and beg end
(> end beg
)
6244 (cons (move-marker (make-marker) beg
)
6245 (move-marker (make-marker) end
))
6247 (overlays-in (point-min) (point-max))))))))
6249 (defun org-set-outline-overlay-data (data)
6250 "Create visibility overlays for all positions in DATA.
6251 DATA should have been made by `org-outline-overlay-data'."
6258 (setq o
(make-overlay (car c
) (cdr c
)))
6259 (overlay-put o
'invisible
'outline
))
6262 ;;; Folding of blocks
6264 (defconst org-block-regexp
6266 "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
6267 "Regular expression for hiding blocks.")
6269 (defvar org-hide-block-overlays nil
6270 "Overlays hiding blocks.")
6271 (make-variable-buffer-local 'org-hide-block-overlays
)
6273 (defun org-block-map (function &optional start end
)
6274 "Call FUNCTION at the head of all source blocks in the current buffer.
6275 Optional arguments START and END can be used to limit the range."
6276 (let ((start (or start
(point-min)))
6277 (end (or end
(point-max))))
6280 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6283 (goto-char (match-beginning 0))
6284 (funcall function
)))))))
6286 (defun org-hide-block-toggle-all ()
6287 "Toggle the visibility of all blocks in the current buffer."
6288 (org-block-map #'org-hide-block-toggle
))
6290 (defun org-hide-block-all ()
6291 "Fold all blocks in the current buffer."
6293 (org-show-block-all)
6294 (org-block-map #'org-hide-block-toggle-maybe
))
6296 (defun org-show-block-all ()
6297 "Unfold all blocks in the current buffer."
6299 (mapc 'delete-overlay org-hide-block-overlays
)
6300 (setq org-hide-block-overlays nil
))
6302 (defun org-hide-block-toggle-maybe ()
6303 "Toggle visibility of block at point."
6305 (let ((case-fold-search t
))
6307 (beginning-of-line 1)
6308 (looking-at org-block-regexp
))
6309 (progn (org-hide-block-toggle)
6310 t
) ;; to signal that we took action
6311 nil
))) ;; to signal that we did not
6313 (defun org-hide-block-toggle (&optional force
)
6314 "Toggle the visibility of the current block."
6318 (if (re-search-forward org-block-regexp nil t
)
6319 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
6320 (end (match-end 0)) ;; end of entire body
6322 (if (memq t
(mapcar (lambda (overlay)
6323 (eq (overlay-get overlay
'invisible
)
6325 (overlays-at start
)))
6326 (if (or (not force
) (eq force
'off
))
6328 (when (member ov org-hide-block-overlays
)
6329 (setq org-hide-block-overlays
6330 (delq ov org-hide-block-overlays
)))
6331 (when (eq (overlay-get ov
'invisible
)
6333 (delete-overlay ov
)))
6334 (overlays-at start
)))
6335 (setq ov
(make-overlay start end
))
6336 (overlay-put ov
'invisible
'org-hide-block
)
6337 ;; make the block accessible to isearch
6339 ov
'isearch-open-invisible
6341 (when (member ov org-hide-block-overlays
)
6342 (setq org-hide-block-overlays
6343 (delq ov org-hide-block-overlays
)))
6344 (when (eq (overlay-get ov
'invisible
)
6346 (delete-overlay ov
))))
6347 (push ov org-hide-block-overlays
)))
6348 (error "Not looking at a source block"))))
6350 ;; org-tab-after-check-for-cycling-hook
6351 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
6352 ;; Remove overlays when changing major mode
6353 (add-hook 'org-mode-hook
6354 (lambda () (org-add-hook 'change-major-mode-hook
6355 'org-show-block-all
'append
'local
)))
6359 (defvar org-goto-window-configuration nil
)
6360 (defvar org-goto-marker nil
)
6361 (defvar org-goto-map
6362 (let ((map (make-sparse-keymap)))
6363 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur
)) cmd
)
6364 (while (setq cmd
(pop cmds
))
6365 (substitute-key-definition cmd cmd map global-map
)))
6366 (suppress-keymap map
)
6367 (org-defkey map
"\C-m" 'org-goto-ret
)
6368 (org-defkey map
[(return)] 'org-goto-ret
)
6369 (org-defkey map
[(left)] 'org-goto-left
)
6370 (org-defkey map
[(right)] 'org-goto-right
)
6371 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
6372 (org-defkey map
"\C-i" 'org-cycle
)
6373 (org-defkey map
[(tab)] 'org-cycle
)
6374 (org-defkey map
[(down)] 'outline-next-visible-heading
)
6375 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
6376 (if org-goto-auto-isearch
6377 (if (fboundp 'define-key-after
)
6378 (define-key-after map
[t] 'org-goto-local-auto-isearch)
6380 (org-defkey map "q" 'org-goto-quit)
6381 (org-defkey map "n" 'outline-next-visible-heading)
6382 (org-defkey map "p" 'outline-previous-visible-heading)
6383 (org-defkey map "f" 'outline-forward-same-level)
6384 (org-defkey map "b" 'outline-backward-same-level)
6385 (org-defkey map "u" 'outline-up-heading))
6386 (org-defkey map "/" 'org-occur)
6387 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6388 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6389 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6390 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6391 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6394 (defconst org-goto-help
6395 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6396 RET=jump to location [Q]uit and return to previous location
6397 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6399 (defvar org-goto-start-pos) ; dynamically scoped parameter
6401 ;; FIXME: Docstring does not mention both interfaces
6402 (defun org-goto (&optional alternative-interface)
6403 "Look up a different location in the current file, keeping current visibility.
6405 When you want look-up or go to a different location in a document, the
6406 fastest way is often to fold the entire buffer and then dive into the tree.
6407 This method has the disadvantage, that the previous location will be folded,
6408 which may not be what you want.
6410 This command works around this by showing a copy of the current buffer
6411 in an indirect buffer, in overview mode. You can dive into the tree in
6412 that copy, use org-occur and incremental search to find a location.
6413 When pressing RET or `Q', the command returns to the original buffer in
6414 which the visibility is still unchanged. After RET is will also jump to
6415 the location selected in the indirect buffer and expose the
6416 the headline hierarchy above."
6418 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6419 (org-refile-use-outline-path t)
6420 (org-refile-target-verify-function nil)
6422 (if (not alternative-interface)
6424 (if (eq org-goto-interface 'outline)
6425 'outline-path-completion
6427 (org-goto-start-pos (point))
6429 (if (eq interface 'outline)
6430 (car (org-get-location (current-buffer) org-goto-help))
6431 (let ((pa (org-refile-get-location "Goto: ")))
6432 (org-refile-check-position pa)
6436 (org-mark-ring-push org-goto-start-pos)
6437 (goto-char selected-point)
6438 (if (or (org-invisible-p) (org-invisible-p2))
6439 (org-show-context 'org-goto)))
6442 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6443 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6444 (defvar org-goto-local-auto-isearch-map) ; defined below
6446 (defun org-get-location (buf help)
6447 "Let the user select a location in the Org-mode buffer BUF.
6448 This function uses a recursive edit. It returns the selected position
6450 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6451 (isearch-hide-immediately nil)
6452 (isearch-search-fun-function
6453 (lambda () 'org-goto-local-search-headings))
6454 (org-goto-selected-point org-goto-exit-command)
6456 (special-display-buffer-names nil)
6457 (special-display-regexps nil)
6458 (special-display-function nil))
6460 (save-window-excursion
6461 (delete-other-windows)
6462 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6465 (make-indirect-buffer (current-buffer) "*org-goto*")
6466 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6467 (with-output-to-temp-buffer "*Help*"
6469 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
6470 (setq buffer-read-only nil)
6471 (let ((org-startup-truncated t)
6472 (org-startup-folded nil)
6473 (org-startup-align-all-tables nil))
6476 (setq buffer-read-only t)
6477 (if (and (boundp 'org-goto-start-pos)
6478 (integer-or-marker-p org-goto-start-pos))
6479 (let ((org-show-hierarchy-above t)
6480 (org-show-siblings t)
6481 (org-show-following-heading t))
6482 (goto-char org-goto-start-pos)
6483 (and (org-invisible-p) (org-show-context)))
6484 (goto-char (point-min)))
6485 (let (org-special-ctrl-a/e) (org-beginning-of-line))
6486 (message "Select location and press RET")
6487 (use-local-map org-goto-map)
6490 (kill-buffer "*org-goto*")
6491 (cons org-goto-selected-point org-goto-exit-command)))
6493 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6494 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6495 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6496 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6498 (defun org-goto-local-search-headings (string bound noerror)
6499 "Search and make sure that any matches are in headlines."
6501 (while (if isearch-forward
6502 (search-forward string bound noerror)
6503 (search-backward string bound noerror))
6504 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6505 (and (member :headline context)
6506 (not (member :tags context))))
6507 (throw 'return (point))))))
6509 (defun org-goto-local-auto-isearch ()
6512 (goto-char (point-min))
6513 (let ((keys (this-command-keys)))
6514 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6516 (isearch-process-search-char (string-to-char keys)))))
6518 (defun org-goto-ret (&optional arg)
6519 "Finish `org-goto' by going to the new location."
6521 (setq org-goto-selected-point (point)
6522 org-goto-exit-command 'return)
6525 (defun org-goto-left ()
6526 "Finish `org-goto' by going to the new location."
6528 (if (org-on-heading-p)
6530 (beginning-of-line 1)
6531 (setq org-goto-selected-point (point)
6532 org-goto-exit-command 'left)
6534 (error "Not on a heading")))
6536 (defun org-goto-right ()
6537 "Finish `org-goto' by going to the new location."
6539 (if (org-on-heading-p)
6541 (setq org-goto-selected-point (point)
6542 org-goto-exit-command 'right)
6544 (error "Not on a heading")))
6546 (defun org-goto-quit ()
6547 "Finish `org-goto' without cursor motion."
6549 (setq org-goto-selected-point nil)
6550 (setq org-goto-exit-command 'quit)
6553 ;;; Indirect buffer display of subtrees
6555 (defvar org-indirect-dedicated-frame nil
6556 "This is the frame being used for indirect tree display.")
6557 (defvar org-last-indirect-buffer nil)
6559 (defun org-tree-to-indirect-buffer (&optional arg)
6560 "Create indirect buffer and narrow it to current subtree.
6561 With numerical prefix ARG, go up to this level and then take that tree.
6562 If ARG is negative, go up that many levels.
6563 If `org-indirect-buffer-display' is not `new-frame', the command removes the
6564 indirect buffer previously made with this command, to avoid proliferation of
6565 indirect buffers. However, when you call the command with a \
6566 \\[universal-argument] prefix, or
6567 when `org-indirect-buffer-display' is `new-frame', the last buffer
6568 is kept so that you can work with several indirect buffers at the same time.
6569 If `org-indirect-buffer-display' is `dedicated-frame', the \
6570 \\[universal-argument] prefix also
6571 requests that a new frame be made for the new buffer, so that the dedicated
6572 frame is not changed."
6574 (let ((cbuf (current-buffer))
6575 (cwin (selected-window))
6577 beg end level heading ibuf)
6579 (org-back-to-heading t)
6581 (setq level (org-outline-level))
6582 (if (< arg 0) (setq arg (+ level arg)))
6583 (while (> (setq level (org-outline-level)) arg)
6584 (outline-up-heading 1 t)))
6586 heading (org-get-heading))
6587 (org-end-of-subtree t t)
6588 (if (org-on-heading-p) (backward-char 1))
6590 (if (and (buffer-live-p org-last-indirect-buffer)
6591 (not (eq org-indirect-buffer-display 'new-frame))
6593 (kill-buffer org-last-indirect-buffer))
6594 (setq ibuf (org-get-indirect-buffer cbuf)
6595 org-last-indirect-buffer ibuf)
6597 ((or (eq org-indirect-buffer-display 'new-frame)
6598 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
6599 (select-frame (make-frame))
6600 (delete-other-windows)
6601 (switch-to-buffer ibuf)
6602 (org-set-frame-title heading))
6603 ((eq org-indirect-buffer-display 'dedicated-frame)
6605 (select-frame (or (and org-indirect-dedicated-frame
6606 (frame-live-p org-indirect-dedicated-frame)
6607 org-indirect-dedicated-frame)
6608 (setq org-indirect-dedicated-frame (make-frame)))))
6609 (delete-other-windows)
6610 (switch-to-buffer ibuf)
6611 (org-set-frame-title (concat "Indirect: " heading)))
6612 ((eq org-indirect-buffer-display 'current-window)
6613 (switch-to-buffer ibuf))
6614 ((eq org-indirect-buffer-display 'other-window)
6615 (pop-to-buffer ibuf))
6616 (t (error "Invalid value")))
6617 (if (featurep 'xemacs)
6618 (save-excursion (org-mode) (turn-on-font-lock)))
6619 (narrow-to-region beg end)
6622 (and (window-live-p cwin) (select-window cwin))))
6624 (defun org-get-indirect-buffer (&optional buffer)
6625 (setq buffer (or buffer (current-buffer)))
6626 (let ((n 1) (base (buffer-name buffer)) bname)
6627 (while (buffer-live-p
6628 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
6631 (make-indirect-buffer buffer bname 'clone)
6632 (error (make-indirect-buffer buffer bname)))))
6634 (defun org-set-frame-title (title)
6635 "Set the title of the current frame to the string TITLE."
6636 ;; FIXME: how to name a single frame in XEmacs???
6637 (unless (featurep 'xemacs)
6638 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
6640 ;;;; Structure editing
6642 ;;; Inserting headlines
6644 (defun org-previous-line-empty-p ()
6647 (or (beginning-of-line 0) t)
6649 (looking-at "[ \t]*$")))))
6651 (defun org-insert-heading (&optional force-heading invisible-ok)
6652 "Insert a new heading or item with same depth at point.
6653 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
6654 If point is at the beginning of a headline, insert a sibling before the
6655 current headline. If point is not at the beginning, do not split the line,
6656 but create the new headline after the current line.
6657 When INVISIBLE-OK is set, stop at invisible headlines when going back.
6658 This is important for non-interactive uses of the command."
6660 (if (or (= (buffer-size) 0)
6661 (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
6662 (org-on-heading-p))))
6663 (not (org-in-item-p))))
6665 (when (or force-heading (not (org-insert-item)))
6666 (let* ((empty-line-p nil)
6667 (head (save-excursion
6670 (org-back-to-heading invisible-ok)
6671 (setq empty-line-p (org-previous-line-empty-p))
6674 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
6675 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
6676 pos hide-previous previous-pos)
6678 ((and (org-on-heading-p) (bolp)
6680 (save-excursion (backward-char 1) (not (org-invisible-p)))))
6681 ;; insert before the current line
6682 (open-line (if blank 2 1)))
6684 (not org-insert-heading-respect-content)
6687 (backward-char 1) (not (org-invisible-p)))))
6688 ;; insert right here
6691 ;; somewhere in the line
6693 (setq previous-pos (point-at-bol))
6695 (setq hide-previous (org-invisible-p)))
6696 (and org-insert-heading-respect-content (org-show-subtree))
6698 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
6701 (goto-char (point-at-bol))
6702 (and (looking-at org-complex-heading-regexp)
6703 (> p (match-beginning 4)))))))
6706 (org-insert-heading-respect-content
6707 (org-end-of-subtree nil t)
6708 (or (bolp) (newline))
6709 (or (org-previous-line-empty-p)
6710 (and blank (newline)))
6716 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
6717 (setq tags (and (match-end 2) (match-string 2)))
6719 (delete-region (match-beginning 1) (match-end 1)))
6720 (setq pos (point-at-bol))
6721 (or split (end-of-line 1))
6722 (delete-horizontal-space)
6723 (if (string-match "\\`\\*+\\'"
6724 (buffer-substring (point-at-bol) (point)))
6726 (newline (if blank 2 1))
6732 (org-set-tags nil 'align))))
6734 (or split (end-of-line 1))
6735 (newline (if blank 2 1)))))))
6736 (insert head) (just-one-space)
6739 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
6740 (when (and org-insert-heading-respect-content hide-previous)
6742 (goto-char previous-pos)
6744 (run-hooks 'org-insert-heading-hook)))))
6746 (defun org-get-heading (&optional no-tags)
6747 "Return the heading of the current entry, without the stars."
6749 (org-back-to-heading t)
6752 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
6753 "\\*+[ \t]+\\([^\r\n]*\\)"))
6754 (match-string 1) "")))
6756 (defun org-heading-components ()
6757 "Return the components of the current heading.
6758 This is a list with the following elements:
6759 - the level as an integer
6760 - the reduced level, different if `org-odd-levels-only' is set.
6761 - the TODO keyword, or nil
6762 - the priority character, like ?A, or nil if no priority is given
6763 - the headline text itself, or the tags string if no headline text
6764 - the tags string, or nil."
6766 (org-back-to-heading t)
6767 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
6768 (list (length (match-string 1))
6769 (org-reduced-level (length (match-string 1)))
6770 (org-match-string-no-properties 2)
6771 (and (match-end 3) (aref (match-string 3) 2))
6772 (org-match-string-no-properties 4)
6773 (org-match-string-no-properties 5)))))
6775 (defun org-get-entry ()
6776 "Get the entry text, after heading, entire subtree."
6778 (org-back-to-heading t)
6779 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
6781 (defun org-insert-heading-after-current ()
6782 "Insert a new heading with same level as current, after current subtree."
6784 (org-back-to-heading)
6785 (org-insert-heading)
6786 (org-move-subtree-down)
6789 (defun org-insert-heading-respect-content ()
6791 (let ((org-insert-heading-respect-content t))
6792 (org-insert-heading t)))
6794 (defun org-insert-todo-heading-respect-content (&optional force-state)
6796 (let ((org-insert-heading-respect-content t))
6797 (org-insert-todo-heading force-state t)))
6799 (defun org-insert-todo-heading (arg &optional force-heading)
6800 "Insert a new heading with the same level and TODO state as current heading.
6801 If the heading has no TODO state, or if the state is DONE, use the first
6802 state (TODO by default). Also with prefix arg, force first state."
6804 (when (or force-heading (not (org-insert-item 'checkbox)))
6805 (org-insert-heading force-heading)
6807 (org-back-to-heading)
6808 (outline-previous-heading)
6809 (looking-at org-todo-line-regexp))
6813 (not (match-beginning 2))
6814 (member (match-string 2) org-done-keywords))
6815 (car org-todo-keywords-1)
6819 (run-hook-with-args-until-success
6820 'org-todo-get-default-hook new-mark-x nil)
6822 (beginning-of-line 1)
6823 (and (looking-at "\\*+ ") (goto-char (match-end 0))
6824 (if org-treat-insert-todo-heading-as-state-change
6826 (insert new-mark " "))))
6827 (when org-provide-todo-statistics
6828 (org-update-parent-todo-statistics))))
6830 (defun org-insert-subheading (arg)
6831 "Insert a new subheading and demote it.
6832 Works for outline headings and for plain lists alike."
6834 (org-insert-heading arg)
6836 ((org-on-heading-p) (org-do-demote))
6837 ((org-at-item-p) (org-indent-item 1))))
6839 (defun org-insert-todo-subheading (arg)
6840 "Insert a new subheading with TODO keyword or checkbox and demote it.
6841 Works for outline headings and for plain lists alike."
6843 (org-insert-todo-heading arg)
6845 ((org-on-heading-p) (org-do-demote))
6846 ((org-at-item-p) (org-indent-item 1))))
6848 ;;; Promotion and Demotion
6850 (defvar org-after-demote-entry-hook nil
6851 "Hook run after an entry has been demoted.
6852 The cursor will be at the beginning of the entry.
6853 When a subtree is being demoted, the hook will be called for each node.")
6855 (defvar org-after-promote-entry-hook nil
6856 "Hook run after an entry has been promoted.
6857 The cursor will be at the beginning of the entry.
6858 When a subtree is being promoted, the hook will be called for each node.")
6860 (defun org-promote-subtree ()
6861 "Promote the entire subtree.
6862 See also `org-promote'."
6865 (org-map-tree 'org-promote))
6866 (org-fix-position-after-promote))
6868 (defun org-demote-subtree ()
6869 "Demote the entire subtree. See `org-demote'.
6870 See also `org-promote'."
6873 (org-map-tree 'org-demote))
6874 (org-fix-position-after-promote))
6877 (defun org-do-promote ()
6878 "Promote the current heading higher up the tree.
6879 If the region is active in `transient-mark-mode', promote all headings
6883 (if (org-region-active-p)
6884 (org-map-region 'org-promote (region-beginning) (region-end))
6886 (org-fix-position-after-promote))
6888 (defun org-do-demote ()
6889 "Demote the current heading lower down the tree.
6890 If the region is active in `transient-mark-mode', demote all headings
6894 (if (org-region-active-p)
6895 (org-map-region 'org-demote (region-beginning) (region-end))
6897 (org-fix-position-after-promote))
6899 (defun org-fix-position-after-promote ()
6900 "Make sure that after pro/demotion cursor position is right."
6901 (let ((pos (point)))
6902 (when (save-excursion
6903 (beginning-of-line 1)
6904 (looking-at org-todo-line-regexp)
6905 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
6906 (cond ((eobp) (insert " "))
6907 ((eolp) (insert " "))
6908 ((equal (char-after) ?\ ) (forward-char 1))))))
6910 (defun org-current-level ()
6911 "Return the level of the current entry, or nil if before the first headline.
6912 The level is the number of stars at the beginning of the headline."
6916 (org-back-to-heading t)
6917 (funcall outline-level))
6920 (defun org-get-previous-line-level ()
6921 "Return the outline depth of the last headline before the current line.
6922 Returns 0 for the first headline in the buffer, and nil if before the
6924 (let ((current-level (org-current-level))
6925 (prev-level (when (> (line-number-at-pos) 1)
6927 (beginning-of-line 0)
6928 (org-current-level)))))
6929 (cond ((null current-level) nil) ; Before first headline
6930 ((null prev-level) 0) ; At first headline
6933 (defun org-reduced-level (l)
6934 "Compute the effective level of a heading.
6935 This takes into account the setting of `org-odd-levels-only'."
6936 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
6938 (defun org-level-increment ()
6939 "Return the number of stars that will be added or removed at a
6940 time to headlines when structure editing, based on the value of
6941 `org-odd-levels-only'."
6942 (if org-odd-levels-only 2 1))
6944 (defun org-get-valid-level (level &optional change)
6945 "Rectify a level change under the influence of `org-odd-levels-only'
6946 LEVEL is a current level, CHANGE is by how much the level should be
6947 modified. Even if CHANGE is nil, LEVEL may be returned modified because
6948 even level numbers will become the next higher odd number."
6949 (if org-odd-levels-only
6950 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
6951 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
6952 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
6953 (max 1 (+ level (or change 0)))))
6955 (if (boundp 'define-obsolete-function-alias)
6956 (if (or (featurep 'xemacs) (< emacs-major-version 23))
6957 (define-obsolete-function-alias 'org-get-legal-level
6958 'org-get-valid-level)
6959 (define-obsolete-function-alias 'org-get-legal-level
6960 'org-get-valid-level "23.1")))
6962 (defun org-promote ()
6963 "Promote the current heading higher up the tree.
6964 If the region is active in `transient-mark-mode', promote all headings
6966 (org-back-to-heading t)
6967 (let* ((level (save-match-data (funcall outline-level)))
6968 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
6969 (diff (abs (- level (length up-head) -1))))
6970 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
6971 (replace-match up-head nil t)
6972 ;; Fixup tag positioning
6973 (and org-auto-align-tags (org-set-tags nil t))
6974 (if org-adapt-indentation (org-fixup-indentation (- diff)))
6975 (run-hooks 'org-after-promote-entry-hook)))
6977 (defun org-demote ()
6978 "Demote the current heading lower down the tree.
6979 If the region is active in `transient-mark-mode', demote all headings
6981 (org-back-to-heading t)
6982 (let* ((level (save-match-data (funcall outline-level)))
6983 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
6984 (diff (abs (- level (length down-head) -1))))
6985 (replace-match down-head nil t)
6986 ;; Fixup tag positioning
6987 (and org-auto-align-tags (org-set-tags nil t))
6988 (if org-adapt-indentation (org-fixup-indentation diff))
6989 (run-hooks 'org-after-demote-entry-hook)))
6991 (defun org-cycle-level ()
6992 "Cycle the level of an empty headline through possible states.
6993 This goes first to child, then to parent, level, then up the hierarchy.
6994 After top level, it switches back to sibling level."
6996 (let ((org-adapt-indentation nil))
6997 (when (org-point-at-end-of-empty-headline)
6998 (setq this-command 'org-cycle-level) ; Only needed for caching
6999 (let ((cur-level (org-current-level))
7000 (prev-level (org-get-previous-line-level)))
7002 ;; If first headline in file, promote to top-level.
7004 (loop repeat (/ (- cur-level 1) (org-level-increment))
7005 do (org-do-promote)))
7006 ;; If same level as prev, demote one.
7007 ((= prev-level cur-level)
7009 ;; If parent is top-level, promote to top level if not already.
7011 (loop repeat (/ (- cur-level 1) (org-level-increment))
7012 do (org-do-promote)))
7013 ;; If top-level, return to prev-level.
7015 (loop repeat (/ (- prev-level 1) (org-level-increment))
7016 do (org-do-demote)))
7017 ;; If less than prev-level, promote one.
7018 ((< cur-level prev-level)
7020 ;; If deeper than prev-level, promote until higher than
7022 ((> cur-level prev-level)
7023 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7024 do (org-do-promote))))
7027 (defun org-map-tree (fun)
7028 "Call FUN for every heading underneath the current one."
7029 (org-back-to-heading)
7030 (let ((level (funcall outline-level)))
7034 (outline-next-heading)
7035 (> (funcall outline-level) level))
7039 (defun org-map-region (fun beg end)
7040 "Call FUN for every heading between BEG and END."
7041 (let ((org-ignore-region t))
7043 (setq end (copy-marker end))
7045 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
7049 (outline-next-heading)
7054 (defun org-fixup-indentation (diff)
7055 "Change the indentation in the current entry by DIFF.
7056 However, if any line in the current entry has no indentation, or if it
7057 would end up with no indentation after the change, nothing at all is done."
7059 (let ((end (save-excursion (outline-next-heading)
7061 (prohibit (if (> diff 0)
7063 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7065 (unless (save-excursion (end-of-line 1)
7066 (re-search-forward prohibit end t))
7067 (while (and (< (point) end)
7068 (re-search-forward "^[ \t]+" end t))
7069 (goto-char (match-end 0))
7070 (setq col (current-column))
7071 (if (< diff 0) (replace-match ""))
7072 (org-indent-to-column (+ diff col))))
7073 (move-marker end nil))))
7075 (defun org-convert-to-odd-levels ()
7076 "Convert an org-mode file with all levels allowed to one with odd levels.
7077 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7080 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7081 (let ((outline-regexp org-outline-regexp)
7082 (outline-level 'org-outline-level)
7083 (org-odd-levels-only nil) n)
7085 (goto-char (point-min))
7086 (while (re-search-forward "^\\*\\*+ " nil t)
7087 (setq n (- (length (match-string 0)) 2))
7088 (while (>= (setq n (1- n)) 0)
7090 (end-of-line 1))))))
7092 (defun org-convert-to-oddeven-levels ()
7093 "Convert an org-mode file with only odd levels to one with odd/even levels.
7094 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7095 file contains a section with an even level, conversion would
7096 destroy the structure of the file. An error is signaled in this
7099 (goto-char (point-min))
7100 ;; First check if there are no even levels
7101 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7102 (org-show-context t)
7103 (error "Not all levels are odd in this file. Conversion not possible"))
7104 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7105 (let ((outline-regexp org-outline-regexp)
7106 (outline-level 'org-outline-level)
7107 (org-odd-levels-only nil) n)
7109 (goto-char (point-min))
7110 (while (re-search-forward "^\\*\\*+ " nil t)
7111 (setq n (/ (1- (length (match-string 0))) 2))
7112 (while (>= (setq n (1- n)) 0)
7114 (end-of-line 1))))))
7116 (defun org-tr-level (n)
7117 "Make N odd if required."
7118 (if org-odd-levels-only (1+ (/ n 2)) n))
7120 ;;; Vertical tree motion, cutting and pasting of subtrees
7122 (defun org-move-subtree-up (&optional arg)
7123 "Move the current subtree up past ARG headlines of the same level."
7125 (org-move-subtree-down (- (prefix-numeric-value arg))))
7127 (defun org-move-subtree-down (&optional arg)
7128 "Move the current subtree down past ARG headlines of the same level."
7130 (setq arg (prefix-numeric-value arg))
7131 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7132 'org-get-last-sibling))
7133 (ins-point (make-marker))
7135 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7137 (org-back-to-heading)
7140 (setq ne-beg (org-back-over-empty-lines))
7143 (save-excursion (outline-end-of-heading)
7144 (setq folded (org-invisible-p)))
7145 (outline-end-of-subtree))
7146 (outline-next-heading)
7147 (setq ne-end (org-back-over-empty-lines))
7150 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7151 ;; include less whitespace
7154 (forward-line (- ne-beg ne-end))
7155 (setq beg (point))))
7156 ;; Find insertion point, with error handling
7158 (or (and (funcall movfunc) (looking-at outline-regexp))
7159 (progn (goto-char beg0)
7160 (error "Cannot move past superior level or buffer limit")))
7161 (setq cnt (1- cnt)))
7163 ;; Moving forward - still need to move over subtree
7164 (progn (org-end-of-subtree t t)
7166 (org-back-over-empty-lines)
7167 (or (bolp) (newline)))))
7168 (setq ne-ins (org-back-over-empty-lines))
7169 (move-marker ins-point (point))
7170 (setq txt (buffer-substring beg end))
7171 (org-save-markers-in-region beg end)
7172 (delete-region beg end)
7173 (org-remove-empty-overlays-at beg)
7174 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7175 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7176 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7177 (let ((bbb (point)))
7178 (insert-before-markers txt)
7179 (org-reinstall-markers-in-region bbb)
7180 (move-marker ins-point bbb))
7181 (or (bolp) (insert "\n"))
7182 (setq ins-end (point))
7183 (goto-char ins-point)
7184 (org-skip-whitespace)
7185 (when (and (< arg 0)
7186 (org-first-sibling-p)
7188 ;; Move whitespace back to beginning
7191 (let ((kill-whole-line t))
7192 (kill-line (- ne-ins ne-beg)) (point)))
7193 (insert (make-string (- ne-ins ne-beg) ?\n)))
7194 (move-marker ins-point nil)
7199 (org-cycle-hide-drawers 'children))
7200 (org-clean-visibility-after-subtree-move)))
7202 (defvar org-subtree-clip ""
7203 "Clipboard for cut and paste of subtrees.
7204 This is actually only a copy of the kill, because we use the normal kill
7205 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7207 (defvar org-subtree-clip-folded nil
7208 "Was the last copied subtree folded?
7209 This is used to fold the tree back after pasting.")
7211 (defun org-cut-subtree (&optional n)
7212 "Cut the current subtree into the clipboard.
7213 With prefix arg N, cut this many sequential subtrees.
7214 This is a short-hand for marking the subtree and then cutting it."
7216 (org-copy-subtree n 'cut))
7218 (defun org-copy-subtree (&optional n cut force-store-markers)
7219 "Cut the current subtree into the clipboard.
7220 With prefix arg N, cut this many sequential subtrees.
7221 This is a short-hand for marking the subtree and then copying it.
7222 If CUT is non-nil, actually cut the subtree.
7223 If FORCE-STORE-MARKERS is non-nil, store the relative locations
7224 of some markers in the region, even if CUT is non-nil. This is
7225 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
7227 (let (beg end folded (beg0 (point)))
7229 (org-back-to-heading nil) ; take what looks like a subtree
7230 (org-back-to-heading t)) ; take what is really there
7231 (org-back-over-empty-lines)
7233 (skip-chars-forward " \t\r\n")
7235 (save-excursion (outline-end-of-heading)
7236 (setq folded (org-invisible-p)))
7238 (org-forward-same-level (1- n) t)
7240 (org-end-of-subtree t t))
7241 (org-back-over-empty-lines)
7245 (setq org-subtree-clip-folded folded)
7246 (when (or cut force-store-markers)
7247 (org-save-markers-in-region beg end))
7248 (if cut (kill-region beg end) (copy-region-as-kill beg end))
7249 (setq org-subtree-clip (current-kill 0))
7250 (message "%s: Subtree(s) with %d characters"
7251 (if cut "Cut" "Copied")
7252 (length org-subtree-clip)))))
7254 (defun org-paste-subtree (&optional level tree for-yank)
7255 "Paste the clipboard as a subtree, with modification of headline level.
7256 The entire subtree is promoted or demoted in order to match a new headline
7259 If the cursor is at the beginning of a headline, the same level as
7260 that headline is used to paste the tree
7262 If not, the new level is derived from the *visible* headings
7263 before and after the insertion point, and taken to be the inferior headline
7264 level of the two. So if the previous visible heading is level 3 and the
7265 next is level 4 (or vice versa), level 4 will be used for insertion.
7266 This makes sure that the subtree remains an independent subtree and does
7267 not swallow low level entries.
7269 You can also force a different level, either by using a numeric prefix
7270 argument, or by inserting the heading marker by hand. For example, if the
7271 cursor is after \"*****\", then the tree will be shifted to level 5.
7273 If optional TREE is given, use this text instead of the kill ring.
7275 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
7276 move back over whitespace before inserting, and move point to the end of
7277 the inserted text when done."
7279 (setq tree (or tree (and kill-ring (current-kill 0))))
7280 (unless (org-kill-is-subtree-p tree)
7282 (substitute-command-keys
7283 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
7284 (let* ((visp (not (org-invisible-p)))
7286 (^re (concat "^\\(" outline-regexp "\\)"))
7287 (re (concat "\\(" outline-regexp "\\)"))
7288 (^re_ (concat "\\(\\*+\\)[ \t]*"))
7290 (old-level (if (string-match ^re txt)
7291 (- (match-end 0) (match-beginning 0) 1)
7293 (force-level (cond (level (prefix-numeric-value level))
7294 ((and (looking-at "[ \t]*$")
7296 ^re_ (buffer-substring
7297 (point-at-bol) (point))))
7298 (- (match-end 1) (match-beginning 1)))
7300 (looking-at org-outline-regexp))
7301 (- (match-end 0) (point) 1))
7303 (previous-level (save-excursion
7306 (outline-previous-visible-heading 1)
7308 (- (match-end 0) (match-beginning 0) 1)
7311 (next-level (save-excursion
7314 (or (looking-at outline-regexp)
7315 (outline-next-visible-heading 1))
7317 (- (match-end 0) (match-beginning 0) 1)
7320 (new-level (or force-level (max previous-level next-level)))
7321 (shift (if (or (= old-level -1)
7323 (= old-level new-level))
7325 (- new-level old-level)))
7326 (delta (if (> shift 0) -1 1))
7327 (func (if (> shift 0) 'org-demote 'org-promote))
7328 (org-odd-levels-only nil)
7330 ;; Remove the forced level indicator
7332 (delete-region (point-at-bol) (point)))
7334 (beginning-of-line 1)
7335 (unless for-yank (org-back-over-empty-lines))
7337 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
7338 (insert-before-markers txt)
7339 (unless (string-match "\n\\'" txt) (insert "\n"))
7340 (setq newend (point))
7341 (org-reinstall-markers-in-region beg)
7344 (skip-chars-forward " \t\n\r")
7346 (if (and (org-invisible-p) visp)
7347 (save-excursion (outline-show-heading)))
7348 ;; Shift if necessary
7351 (narrow-to-region beg end)
7352 (while (not (= shift 0))
7353 (org-map-region func (point-min) (point-max))
7354 (setq shift (+ delta shift)))
7355 (goto-char (point-min))
7356 (setq newend (point-max))))
7357 (when (or (interactive-p) for-yank)
7358 (message "Clipboard pasted as level %d subtree" new-level))
7359 (if (and (not for-yank) ; in this case, org-yank will decide about folding
7361 (eq org-subtree-clip (current-kill 0))
7362 org-subtree-clip-folded)
7363 ;; The tree was folded before it was killed/copied
7365 (and for-yank (goto-char newend))))
7367 (defun org-kill-is-subtree-p (&optional txt)
7368 "Check if the current kill is an outline subtree, or a set of trees.
7369 Returns nil if kill does not start with a headline, or if the first
7370 headline level is not the largest headline level in the tree.
7371 So this will actually accept several entries of equal levels as well,
7372 which is OK for `org-paste-subtree'.
7373 If optional TXT is given, check this string instead of the current kill."
7374 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
7375 (start-level (and kill
7376 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
7377 org-outline-regexp "\\)")
7379 (- (match-end 2) (match-beginning 2) 1)))
7380 (re (concat "^" org-outline-regexp))
7381 (start (1+ (or (match-beginning 2) -1))))
7382 (if (not start-level)
7384 nil) ;; does not even start with a heading
7386 (while (setq start (string-match re kill (1+ start)))
7387 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
7391 (defvar org-markers-to-move nil
7392 "Markers that should be moved with a cut-and-paste operation.
7393 Those markers are stored together with their positions relative to
7394 the start of the region.")
7396 (defun org-save-markers-in-region (beg end)
7397 "Check markers in region.
7398 If these markers are between BEG and END, record their position relative
7399 to BEG, so that after moving the block of text, we can put the markers back
7401 This function gets called just before an entry or tree gets cut from the
7402 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
7403 called immediately, to move the markers with the entries."
7404 (setq org-markers-to-move nil)
7405 (when (featurep 'org-clock)
7406 (org-clock-save-markers-for-cut-and-paste beg end))
7407 (when (featurep 'org-agenda)
7408 (org-agenda-save-markers-for-cut-and-paste beg end)))
7410 (defun org-check-and-save-marker (marker beg end)
7411 "Check if MARKER is between BEG and END.
7412 If yes, remember the marker and the distance to BEG."
7413 (when (and (marker-buffer marker)
7414 (equal (marker-buffer marker) (current-buffer)))
7415 (if (and (>= marker beg) (< marker end))
7416 (push (cons marker (- marker beg)) org-markers-to-move))))
7418 (defun org-reinstall-markers-in-region (beg)
7419 "Move all remembered markers to their position relative to BEG."
7421 (move-marker (car x) (+ beg (cdr x))))
7422 org-markers-to-move)
7423 (setq org-markers-to-move nil))
7425 (defun org-narrow-to-subtree ()
7426 "Narrow buffer to the current subtree."
7431 (progn (org-back-to-heading t) (point))
7432 (progn (org-end-of-subtree t t)
7433 (if (org-on-heading-p) (backward-char 1))
7437 (defvar org-property-drawer-re))
7439 (defun org-clone-subtree-with-time-shift (n &optional shift)
7440 "Clone the task (subtree) at point N times.
7441 The clones will be inserted as siblings.
7443 In interactive use, the user will be prompted for the number of
7444 clones to be produced, and for a time SHIFT, which may be a
7445 repeater as used in time stamps, for example `+3d'.
7447 When a valid repeater is given and the entry contains any time
7448 stamps, the clones will become a sequence in time, with time
7449 stamps in the subtree shifted for each clone produced. If SHIFT
7450 is nil or the empty string, time stamps will be left alone. The
7451 ID property of the original subtree is removed.
7453 If the original subtree did contain time stamps with a repeater,
7454 the following will happen:
7455 - the repeater will be removed in each clone
7456 - an additional clone will be produced, with the current, unshifted
7457 date(s) in the entry.
7458 - the original entry will be placed *after* all the clones, with
7460 - the start days in the repeater in the original entry will be shifted
7461 to past the last clone.
7462 I this way you can spell out a number of instances of a repeating task,
7463 and still retain the repeater to cover future instances of the task."
7464 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
7465 (let (beg end template task idprop
7466 shift-n shift-what doshift nmin nmax (n-no-remove -1))
7467 (if (not (and (integerp n) (> n 0)))
7468 (error "Invalid number of replications %s" n))
7469 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
7470 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
7472 (error "Invalid shift specification %s" shift))
7474 (setq shift-n (string-to-number (match-string 1 shift))
7475 shift-what (cdr (assoc (match-string 2 shift)
7476 '(("d" . day) ("w" . week)
7477 ("m" . month) ("y" . year))))))
7478 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
7479 (setq nmin 1 nmax n)
7480 (org-back-to-heading t)
7482 (setq idprop (org-entry-get nil "ID"))
7483 (org-end-of-subtree t t)
7484 (or (bolp) (insert "\n"))
7486 (setq template (buffer-substring beg end))
7488 (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
7489 (delete-region beg end)
7491 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
7493 (loop for n from nmin to nmax do
7498 (goto-char (point-min))
7499 (and idprop (if org-clone-delete-id
7500 (org-entry-delete nil "ID")
7501 (org-id-get-create t)))
7502 (while (re-search-forward org-property-drawer-re nil t)
7503 (org-remove-empty-drawer-at "PROPERTIES" (point)))
7504 (goto-char (point-min))
7506 (while (re-search-forward org-ts-regexp-both nil t)
7507 (org-timestamp-change (* n shift-n) shift-what))
7508 (unless (= n n-no-remove)
7509 (goto-char (point-min))
7510 (while (re-search-forward org-ts-regexp nil t)
7512 (goto-char (match-beginning 0))
7513 (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
7514 (delete-region (match-beginning 1) (match-end 1)))))))
7515 (setq task (buffer-string)))
7521 (defun org-sort (with-case)
7522 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
7523 Optional argument WITH-CASE means sort case-sensitively.
7524 With a double prefix argument, also remove duplicate entries."
7527 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
7528 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
7530 (org-call-with-arg 'org-sort-entries with-case))))
7532 (defun org-sort-remove-invisible (s)
7533 (remove-text-properties 0 (length s) org-rm-props s)
7534 (while (string-match org-bracket-link-regexp s)
7535 (setq s (replace-match (if (match-end 2)
7537 (match-string 1 s)) t t s)))
7540 (defvar org-priority-regexp) ; defined later in the file
7542 (defvar org-after-sorting-entries-or-items-hook nil
7543 "Hook that is run after a bunch of entries or items have been sorted.
7544 When children are sorted, the cursor is in the parent line when this
7545 hook gets called. When a region or a plain list is sorted, the cursor
7546 will be in the first entry of the sorted region/list.")
7548 (defun org-sort-entries
7549 (&optional with-case sorting-type getkey-func compare-func property)
7550 "Sort entries on a certain level of an outline tree.
7551 If there is an active region, the entries in the region are sorted.
7552 Else, if the cursor is before the first entry, sort the top-level items.
7553 Else, the children of the entry at point are sorted.
7555 Sorting can be alphabetically, numerically, by date/time as given by
7556 a time stamp, by a property or by priority.
7558 The command prompts for the sorting type unless it has been given to the
7559 function through the SORTING-TYPE argument, which needs to be a character,
7560 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
7561 precise meaning of each character:
7563 n Numerically, by converting the beginning of the entry/item to a number.
7564 a Alphabetically, ignoring the TODO keyword and the priority, if any.
7565 t By date/time, either the first active time stamp in the entry, or, if
7566 none exist, by the first inactive one.
7567 s By the scheduled date/time.
7568 d By deadline date/time.
7569 c By creation time, which is assumed to be the first inactive time stamp
7570 at the beginning of a line.
7571 p By priority according to the cookie.
7572 r By the value of a property.
7574 Capital letters will reverse the sort order.
7576 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
7577 called with point at the beginning of the record. It must return either
7578 a string or a number that should serve as the sorting key for that record.
7580 Comparing entries ignores case by default. However, with an optional argument
7581 WITH-CASE, the sorting considers case as well."
7583 (let ((case-func (if with-case 'identity 'downcase))
7584 start beg end stars re re2
7586 ;; Find beginning and end of region to sort
7588 ((org-region-active-p)
7589 ;; we will sort the region
7590 (setq end (region-end)
7592 (goto-char (region-beginning))
7593 (if (not (org-on-heading-p)) (outline-next-heading))
7594 (setq start (point)))
7595 ((or (org-on-heading-p)
7596 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
7597 ;; we will sort the children of the current headline
7598 (org-back-to-heading)
7600 end (progn (org-end-of-subtree t t)
7601 (or (bolp) (insert "\n"))
7602 (org-back-over-empty-lines)
7607 (outline-next-heading))
7609 ;; we will sort the top-level entries in this file
7610 (goto-char (point-min))
7611 (or (org-on-heading-p) (outline-next-heading))
7612 (setq start (point))
7613 (goto-char (point-max))
7614 (beginning-of-line 1)
7615 (when (looking-at ".*?\\S-")
7616 ;; File ends in a non-white line
7619 (setq end (point-max))
7620 (setq what "top-level")
7625 (if (>= beg end) (error "Nothing to sort"))
7627 (looking-at "\\(\\*+\\)")
7628 (setq stars (match-string 1)
7629 re (concat "^" (regexp-quote stars) " +")
7630 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
7631 txt (buffer-substring beg end))
7632 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
7633 (if (and (not (equal stars "*")) (string-match re2 txt))
7634 (error "Region to sort contains a level above the first entry"))
7636 (unless sorting-type
7638 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
7639 [t]ime
[s]cheduled [d]eadline [c]reated
7640 A/N/T/S/D/C/P/O/F means reversed:"
7642 (setq sorting-type (read-char-exclusive))
7644 (and (= (downcase sorting-type) ?f)
7646 (org-icompleting-read "Sort using function: "
7647 obarray 'fboundp t nil nil))
7648 (setq getkey-func (intern getkey-func)))
7650 (and (= (downcase sorting-type) ?r)
7652 (org-icompleting-read "Property: "
7653 (mapcar 'list (org-buffer-property-keys t))
7656 (message "Sorting entries...")
7659 (narrow-to-region start end)
7660 (let ((dcst (downcase sorting-type))
7661 (case-fold-search nil)
7662 (now (current-time)))
7664 (/= dcst sorting-type)
7665 ;; This function moves to the beginning character of the "record" to
7668 (if (re-search-forward re nil t)
7669 (goto-char (match-beginning 0))
7670 (goto-char (point-max))))
7671 ;; This function moves to the last character of the "record" being
7676 (outline-forward-same-level 1)
7678 (goto-char (point-max))))))
7679 ;; This function returns the value that gets sorted against.
7683 (if (looking-at org-complex-heading-regexp)
7684 (string-to-number (match-string 4))
7687 (if (looking-at org-complex-heading-regexp)
7688 (funcall case-func (match-string 4))
7691 (let ((end (save-excursion (outline-next-heading) (point))))
7692 (if (or (re-search-forward org-ts-regexp end t)
7693 (re-search-forward org-ts-regexp-both end t))
7694 (org-time-string-to-seconds (match-string 0))
7695 (org-float-time now))))
7697 (let ((end (save-excursion (outline-next-heading) (point))))
7698 (if (re-search-forward
7699 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
7701 (org-time-string-to-seconds (match-string 0))
7702 (org-float-time now))))
7704 (let ((end (save-excursion (outline-next-heading) (point))))
7705 (if (re-search-forward org-scheduled-time-regexp end t)
7706 (org-time-string-to-seconds (match-string 1))
7707 (org-float-time now))))
7709 (let ((end (save-excursion (outline-next-heading) (point))))
7710 (if (re-search-forward org-deadline-time-regexp end t)
7711 (org-time-string-to-seconds (match-string 1))
7712 (org-float-time now))))
7714 (if (re-search-forward org-priority-regexp (point-at-eol) t)
7715 (string-to-char (match-string 2))
7716 org-default-priority))
7718 (or (org-entry-get nil property) ""))
7720 (if (looking-at org-complex-heading-regexp)
7721 (- 9999 (length (member (match-string 2)
7722 org-todo-keywords-1)))))
7726 (setq tmp (funcall getkey-func))
7727 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
7729 (error "Invalid key function `%s'" getkey-func)))
7730 (t (error "Invalid sorting type `%c'" sorting-type))))
7733 ((= dcst ?a) 'string<)
7734 ((= dcst ?f) compare-func)
7735 ((member dcst '(?p ?t ?s ?d ?c)) '<)
7737 (run-hooks 'org-after-sorting-entries-or-items-hook)
7738 (message "Sorting entries...done")))
7740 (defun org-do-sort (table what &optional with-case sorting-type)
7741 "Sort TABLE of WHAT according to SORTING-TYPE.
7742 The user will be prompted for the SORTING-TYPE if the call to this
7743 function does not specify it. WHAT is only for the prompt, to indicate
7744 what is being sorted. The sorting key will be extracted from
7745 the car of the elements of the table.
7746 If WITH-CASE is non-nil, the sorting will be case-sensitive."
7747 (unless sorting-type
7749 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
7751 (setq sorting-type (read-char-exclusive)))
7752 (let ((dcst (downcase sorting-type))
7753 extractfun comparefun)
7754 ;; Define the appropriate functions
7757 (setq extractfun 'string-to-number
7758 comparefun (if (= dcst sorting-type) '< '>)))
7760 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
7761 (lambda(x) (downcase (org-sort-remove-invisible x))))
7762 comparefun (if (= dcst sorting-type)
7764 (lambda (a b) (and (not (string< a b))
7765 (not (string= a b)))))))
7769 (if (or (string-match org-ts-regexp x)
7770 (string-match org-ts-regexp-both x))
7772 (org-time-string-to-time (match-string 0 x)))
7774 comparefun (if (= dcst sorting-type) '< '>)))
7775 (t (error "Invalid sorting type `%c'" sorting-type)))
7777 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
7779 (lambda (a b) (funcall comparefun (car a) (car b))))))
7782 ;;; The orgstruct minor mode
7784 ;; Define a minor mode which can be used in other modes in order to
7785 ;; integrate the org-mode structure editing commands.
7787 ;; This is really a hack, because the org-mode structure commands use
7788 ;; keys which normally belong to the major mode. Here is how it
7789 ;; works: The minor mode defines all the keys necessary to operate the
7790 ;; structure commands, but wraps the commands into a function which
7791 ;; tests if the cursor is currently at a headline or a plain list
7792 ;; item. If that is the case, the structure command is used,
7793 ;; temporarily setting many Org-mode variables like regular
7794 ;; expressions for filling etc. However, when any of those keys is
7795 ;; used at a different location, function uses `key-binding' to look
7796 ;; up if the key has an associated command in another currently active
7797 ;; keymap (minor modes, major mode, global), and executes that
7798 ;; command. There might be problems if any of the keys is otherwise
7799 ;; used as a prefix key.
7801 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
7802 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
7803 ;; addresses this by checking explicitly for both bindings.
7805 (defvar orgstruct-mode-map (make-sparse-keymap)
7806 "Keymap for the minor `orgstruct-mode'.")
7808 (defvar org-local-vars nil
7809 "List of local variables, for use by `orgstruct-mode'.")
7812 (define-minor-mode orgstruct-mode
7813 "Toggle the minor mode `orgstruct-mode'.
7814 This mode is for using Org-mode structure commands in other
7815 modes. The following keys behave as if Org-mode were active, if
7816 the cursor is on a headline, or on a plain list item (both as
7817 defined by Org-mode).
7819 M-up Move entry/item up
7820 M-down Move entry/item down
7823 M-S-up Move entry/item up
7824 M-S-down Move entry/item down
7825 M-S-left Promote subtree
7826 M-S-right Demote subtree
7827 M-q Fill paragraph and items like in Org-mode
7829 C-c - Cycle list bullet
7830 TAB Cycle item visibility
7831 M-RET Insert new heading/item
7832 S-M-RET Insert new TODO heading / Checkbox item
7833 C-c C-c Set tags / toggle checkbox"
7834 nil " OrgStruct" nil
7835 (org-load-modules-maybe)
7836 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
7839 (defun turn-on-orgstruct ()
7840 "Unconditionally turn on `orgstruct-mode'."
7843 (defun orgstruct++-mode (&optional arg)
7844 "Toggle `orgstruct-mode', the enhanced version of it.
7845 In addition to setting orgstruct-mode, this also exports all indentation
7846 and autofilling variables from org-mode into the buffer. It will also
7847 recognize item context in multiline items.
7848 Note that turning off orgstruct-mode will *not* remove the
7849 indentation/paragraph settings. This can only be done by refreshing the
7850 major mode, for example with \\[normal-mode]."
7852 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
7860 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7861 (symbol-name (car x)))
7862 (setq var (car x) val (nth 1 x))
7863 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
7865 (org-set-local 'orgstruct-is-++ t))))
7867 (defvar orgstruct-is-++ nil
7868 "Is `orgstruct-mode' in ++ version in the current-buffer?")
7869 (make-variable-buffer-local 'orgstruct-is-++)
7872 (defun turn-on-orgstruct++ ()
7873 "Unconditionally turn on `orgstruct++-mode'."
7874 (orgstruct++-mode 1))
7876 (defun orgstruct-error ()
7877 "Error when there is no default binding for a structure key."
7879 (error "This key has no function outside structure elements"))
7881 (defun orgstruct-setup ()
7882 "Setup orgstruct keymaps."
7886 '([(meta up)] org-metaup)
7887 '([(meta down)] org-metadown)
7888 '([(meta left)] org-metaleft)
7889 '([(meta right)] org-metaright)
7890 '([(meta shift up)] org-shiftmetaup)
7891 '([(meta shift down)] org-shiftmetadown)
7892 '([(meta shift left)] org-shiftmetaleft)
7893 '([(meta shift right)] org-shiftmetaright)
7894 '([?\e (up)] org-metaup)
7895 '([?\e (down)] org-metadown)
7896 '([?\e (left)] org-metaleft)
7897 '([?\e (right)] org-metaright)
7898 '([?\e (shift up)] org-shiftmetaup)
7899 '([?\e (shift down)] org-shiftmetadown)
7900 '([?\e (shift left)] org-shiftmetaleft)
7901 '([?\e (shift right)] org-shiftmetaright)
7902 '([(shift up)] org-shiftup)
7903 '([(shift down)] org-shiftdown)
7904 '([(shift left)] org-shiftleft)
7905 '([(shift right)] org-shiftright)
7906 '("\C-c\C-c" org-ctrl-c-ctrl-c)
7907 '("\M-q" fill-paragraph)
7909 '("\C-c-" org-cycle-list-bullet)))
7911 (while (setq elt (pop bindings))
7912 (setq nfunc (1+ nfunc))
7913 (setq key (org-key (car elt))
7915 cmd (orgstruct-make-binding fun nfunc key))
7916 (org-defkey orgstruct-mode-map key cmd))
7918 ;; Special treatment needed for TAB and RET
7919 (org-defkey orgstruct-mode-map [(tab)]
7920 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
7921 (org-defkey orgstruct-mode-map "\C-i"
7922 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
7924 (org-defkey orgstruct-mode-map "\M-\C-m"
7925 (orgstruct-make-binding 'org-insert-heading 105
7926 "\M-\C-m" [(meta return)]))
7927 (org-defkey orgstruct-mode-map [(meta return)]
7928 (orgstruct-make-binding 'org-insert-heading 106
7929 [(meta return)] "\M-\C-m"))
7931 (org-defkey orgstruct-mode-map [(shift meta return)]
7932 (orgstruct-make-binding 'org-insert-todo-heading 107
7933 [(meta return)] "\M-\C-m"))
7935 (org-defkey orgstruct-mode-map "\e\C-m"
7936 (orgstruct-make-binding 'org-insert-heading 108
7937 "\e\C-m" [?\e (return)]))
7938 (org-defkey orgstruct-mode-map [?\e (return)]
7939 (orgstruct-make-binding 'org-insert-heading 109
7940 [?\e (return)] "\e\C-m"))
7941 (org-defkey orgstruct-mode-map [?\e (shift return)]
7942 (orgstruct-make-binding 'org-insert-todo-heading 110
7943 [?\e (return)] "\e\C-m"))
7945 (unless org-local-vars
7946 (setq org-local-vars (org-get-local-variables)))
7950 (defun orgstruct-make-binding (fun n &rest keys)
7951 "Create a function for binding in the structure minor mode.
7952 FUN is the command to call inside a table. N is used to create a unique
7953 command name. KEYS are keys that should be checked in for a command
7954 to execute outside of tables."
7957 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
7959 (concat "In Structure, run `" (symbol-name fun) "'.\n"
7960 "Outside of structure, run the binding of `"
7961 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
7965 `(org-context-p 'headline 'item
7966 (and orgstruct-is-++
7967 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
7969 (list 'org-run-like-in-org-mode (list 'quote fun))
7970 (list 'let '(orgstruct-mode)
7971 (list 'call-interactively
7974 (list 'key-binding k))
7976 '('orgstruct-error))))))))
7978 (defun org-context-p (&rest contexts)
7979 "Check if local context is any of CONTEXTS.
7980 Possible values in the list of contexts are `table', `headline', and `item'."
7981 (let ((pos (point)))
7982 (goto-char (point-at-bol))
7983 (prog1 (or (and (memq 'table contexts)
7984 (looking-at "[ \t]*|"))
7985 (and (memq 'headline contexts)
7986 ;;????????? (looking-at "\\*+"))
7987 (looking-at outline-regexp))
7988 (and (memq 'item contexts)
7989 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
7990 (and (memq 'item-body contexts)
7994 (defun org-get-local-variables ()
7995 "Return a list of all local variables in an org-mode buffer."
7997 (with-current-buffer (get-buffer-create "*Org tmp*")
8000 (setq varlist (buffer-local-variables)))
8001 (kill-buffer "*Org tmp*")
8008 (list (car x) (list 'quote (cdr x)))))
8010 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
8011 (symbol-name (car x)))
8016 (defun org-run-like-in-org-mode (cmd)
8017 "Run a command, pretending that the current buffer is in Org-mode.
8018 This will temporarily bind local variables that are typically bound in
8019 Org-mode to the values they have in Org-mode, and then interactively
8021 (org-load-modules-maybe)
8022 (unless org-local-vars
8023 (setq org-local-vars (org-get-local-variables)))
8024 (eval (list 'let org-local-vars
8025 (list 'call-interactively (list 'quote cmd)))))
8029 (defun org-get-category (&optional pos)
8030 "Get the category applying to position POS."
8031 (get-text-property (or pos (point)) 'org-category))
8033 (defun org-refresh-category-properties ()
8034 "Refresh category text properties in the buffer."
8035 (let ((def-cat (cond
8036 ((null org-category)
8037 (if buffer-file-name
8038 (file-name-sans-extension
8039 (file-name-nondirectory buffer-file-name))
8041 ((symbolp org-category) (symbol-name org-category))
8043 beg end cat pos optionp)
8048 (goto-char (point-min))
8049 (put-text-property (point) (point-max) 'org-category def-cat)
8050 (while (re-search-forward
8051 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8052 (setq pos (match-end 0)
8053 optionp (equal (char-after (match-beginning 0)) ?#)
8054 cat (org-trim (match-string 2)))
8056 (setq beg (point-at-bol) end (point-max))
8057 (org-back-to-heading t)
8058 (setq beg (point) end (org-end-of-subtree t t)))
8059 (put-text-property beg end 'org-category cat)
8060 (goto-char pos)))))))
8065 ;;; Link abbreviations
8067 (defun org-link-expand-abbrev (link)
8068 "Apply replacements as defined in `org-link-abbrev-alist."
8069 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
8070 (let* ((key (match-string 1 link))
8071 (as (or (assoc key org-link-abbrev-alist-local)
8072 (assoc key org-link-abbrev-alist)))
8073 (tag (and (match-end 2) (match-string 3 link)))
8079 ((symbolp rpl) (funcall rpl tag))
8080 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
8081 ((string-match "%h" rpl)
8082 (replace-match (url-hexify-string (or tag "")) t t rpl))
8083 (t (concat rpl tag)))))
8086 ;;; Storing and inserting links
8088 (defvar org-insert-link-history nil
8089 "Minibuffer history for links inserted with `org-insert-link'.")
8091 (defvar org-stored-links nil
8092 "Contains the links stored with `org-store-link'.")
8094 (defvar org-store-link-plist nil
8095 "Plist with info about the most recently link created with `org-store-link'.")
8097 (defvar org-link-protocols nil
8098 "Link protocols added to Org-mode using `org-add-link-type'.")
8100 (defvar org-store-link-functions nil
8101 "List of functions that are called to create and store a link.
8102 Each function will be called in turn until one returns a non-nil
8103 value. Each function should check if it is responsible for creating
8104 this link (for example by looking at the major mode).
8105 If not, it must exit and return nil.
8106 If yes, it should return a non-nil value after a calling
8107 `org-store-link-props' with a list of properties and values.
8108 Special properties are:
8110 :type The link prefix, like \"http\". This must be given.
8111 :link The link, like \"http://www.astro.uva.nl/~dominik\".
8112 This is obligatory as well.
8113 :description Optional default description for the second pair
8114 of brackets in an Org-mode link. The user can still change
8115 this when inserting this link into an Org-mode buffer.
8117 In addition to these, any additional properties can be specified
8118 and then used in remember templates.")
8120 (defun org-add-link-type (type &optional follow export)
8121 "Add TYPE to the list of `org-link-types'.
8122 Re-compute all regular expressions depending on `org-link-types'
8124 FOLLOW and EXPORT are two functions.
8126 FOLLOW should take the link path as the single argument and do whatever
8127 is necessary to follow the link, for example find a file or display
8130 EXPORT should format the link path for export to one of the export formats.
8131 It should be a function accepting three arguments:
8133 path the path of the link, the text after the prefix (like \"http:\")
8134 desc the description of the link, if any, nil if there was no description
8135 format the export format, a symbol like `html' or `latex'.
8137 The function may use the FORMAT information to return different values
8138 depending on the format. The return value will be put literally into
8140 Org-mode has a built-in default for exporting links. If you are happy with
8141 this default, there is no need to define an export function for the link
8142 type. For a simple example of an export function, see `org-bbdb.el'."
8143 (add-to-list 'org-link-types type t)
8144 (org-make-link-regexps)
8145 (if (assoc type org-link-protocols)
8146 (setcdr (assoc type org-link-protocols) (list follow export))
8147 (push (list type follow export) org-link-protocols)))
8149 (defvar org-agenda-buffer-name)
8152 (defun org-store-link (arg)
8153 "\\<org-mode-map>Store an org-link to the current location.
8154 This link is added to `org-stored-links' and can later be inserted
8155 into an org-buffer with \\[org-insert-link].
8157 For some link types, a prefix arg is interpreted:
8158 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
8159 For file links, arg negates `org-context-in-file-links'."
8161 (org-load-modules-maybe)
8162 (setq org-store-link-plist nil) ; reset
8163 (let ((outline-regexp (org-get-limited-outline-regexp))
8164 link cpltxt desc description search txt custom-id agenda-link)
8167 ((run-hook-with-args-until-success 'org-store-link-functions)
8168 (setq link (plist-get org-store-link-plist :link)
8169 desc (or (plist-get org-store-link-plist :description) link)))
8171 ((equal (buffer-name) "*Org Edit Src Example*")
8173 (while (or (not label)
8177 (goto-char (point-min))
8179 (regexp-quote (format org-coderef-label-format label))
8181 (when label (message "Label exists already") (sit-for 2))
8182 (setq label (read-string "Code line label: " label)))
8184 (setq link (format org-coderef-label-format label))
8185 (setq gc (- 79 (length link)))
8186 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
8188 (setq link (concat "(" label ")") desc nil)))
8190 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
8191 ;; We are in the agenda, link to referenced location
8192 (let ((m (or (get-text-property (point) 'org-hd-marker)
8193 (get-text-property (point) 'org-marker))))
8195 (org-with-point-at m
8198 (call-interactively 'org-store-link)
8199 (org-store-link nil)))))))
8201 ((eq major-mode 'calendar-mode)
8202 (let ((cd (calendar-cursor-to-date)))
8205 (car org-time-stamp-formats)
8207 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8209 (org-store-link-props :type "calendar" :date cd)))
8211 ((eq major-mode 'w3-mode)
8212 (setq cpltxt (if (and (buffer-name)
8213 (not (string-match "Untitled" (buffer-name))))
8216 link (org-make-link (url-view-url t)))
8217 (org-store-link-props :type "w3" :url (url-view-url t)))
8219 ((eq major-mode 'w3m-mode)
8220 (setq cpltxt (or w3m-current-title w3m-current-url)
8221 link (org-make-link w3m-current-url))
8222 (org-store-link-props :type "w3m" :url (url-view-url t)))
8224 ((setq search (run-hook-with-args-until-success
8225 'org-create-file-search-functions))
8226 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8228 (setq cpltxt (or description link)))
8230 ((eq major-mode 'image-mode)
8231 (setq cpltxt (concat "file:"
8232 (abbreviate-file-name buffer-file-name))
8233 link (org-make-link cpltxt))
8234 (org-store-link-props :type "image" :file buffer-file-name))
8236 ((eq major-mode 'dired-mode)
8237 ;; link to the file in the current line
8238 (let ((file (dired-get-filename nil t)))
8240 (abbreviate-file-name
8241 (expand-file-name (dired-get-filename nil t)))
8242 ;; otherwise, no file so use current directory.
8244 (setq cpltxt (concat "file:" file)
8245 link (org-make-link cpltxt))))
8247 ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
8248 (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
8250 ((org-in-regexp "<<\\(.*?\\)>>")
8253 (abbreviate-file-name
8254 (buffer-file-name (buffer-base-buffer)))
8255 "::" (match-string 1))
8256 link (org-make-link cpltxt)))
8257 ((and (featurep 'org-id)
8258 (or (eq org-link-to-org-use-id t)
8259 (and (eq org-link-to-org-use-id 'create-if-interactive)
8261 (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
8264 (and org-link-to-org-use-id
8266 (org-entry-get nil "ID")
8268 ;; We can make a link using the ID.
8269 (setq link (condition-case nil
8270 (prog1 (org-id-store-link)
8271 (setq desc (plist-get org-store-link-plist
8274 ;; probably before first headline, link to file only
8276 (abbreviate-file-name
8277 (buffer-file-name (buffer-base-buffer))))))))
8279 ;; Just link to current headline
8280 (setq cpltxt (concat "file:"
8281 (abbreviate-file-name
8282 (buffer-file-name (buffer-base-buffer)))))
8283 ;; Add a context search string
8284 (when (org-xor org-context-in-file-links arg)
8286 ((org-on-heading-p) nil)
8287 ((org-region-active-p)
8288 (buffer-substring (region-beginning) (region-end)))
8290 (when (or (null txt) (string-match "\\S-" txt))
8294 (org-make-org-heading-search-string txt)
8296 desc (or (nth 4 (ignore-errors
8297 (org-heading-components))) "NONE"))))
8298 (if (string-match "::\\'" cpltxt)
8299 (setq cpltxt (substring cpltxt 0 -2)))
8300 (setq link (org-make-link cpltxt)))))
8302 ((buffer-file-name (buffer-base-buffer))
8303 ;; Just link to this file here.
8304 (setq cpltxt (concat "file:"
8305 (abbreviate-file-name
8306 (buffer-file-name (buffer-base-buffer)))))
8307 ;; Add a context string
8308 (when (org-xor org-context-in-file-links arg)
8309 (setq txt (if (org-region-active-p)
8310 (buffer-substring (region-beginning) (region-end))
8311 (buffer-substring (point-at-bol) (point-at-eol))))
8312 ;; Only use search option if there is some text.
8313 (when (string-match "\\S-" txt)
8315 (concat cpltxt "::" (org-make-org-heading-search-string txt))
8317 (setq link (org-make-link cpltxt)))
8320 (error "Cannot link to a buffer which is not visiting a file"))
8322 (t (setq link nil)))
8324 (if (consp link) (setq cpltxt (car link) link (cdr link)))
8325 (setq link (or link cpltxt)
8326 desc (or desc cpltxt))
8327 (if (equal desc "NONE") (setq desc nil))
8329 (if (and (or (interactive-p) executing-kbd-macro) link)
8331 (setq org-stored-links
8332 (cons (list link desc) org-stored-links))
8333 (message "Stored: %s" (or desc link))
8335 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
8337 (setq org-stored-links
8338 (cons (list link desc) org-stored-links))))
8339 (or agenda-link (and link (org-make-link-string link desc))))))
8341 (defun org-store-link-props (&rest plist)
8342 "Store link properties, extract names and addresses."
8344 (when (setq x (plist-get plist :from))
8345 (setq adr (mail-extract-address-components x))
8346 (setq plist (plist-put plist :fromname (car adr)))
8347 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
8348 (when (setq x (plist-get plist :to))
8349 (setq adr (mail-extract-address-components x))
8350 (setq plist (plist-put plist :toname (car adr)))
8351 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
8352 (let ((from (plist-get plist :from))
8353 (to (plist-get plist :to)))
8354 (when (and from to org-from-is-user-regexp)
8356 (plist-put plist :fromto
8357 (if (string-match org-from-is-user-regexp from)
8359 (concat "from %f"))))))
8360 (setq org-store-link-plist plist))
8362 (defun org-add-link-props (&rest plist)
8363 "Add these properties to the link property list."
8366 (setq key (pop plist) value (pop plist))
8367 (setq org-store-link-plist
8368 (plist-put org-store-link-plist key value)))))
8370 (defun org-email-link-description (&optional fmt)
8371 "Return the description part of an email link.
8372 This takes information from `org-store-link-plist' and formats it
8373 according to FMT (default from `org-email-link-description-format')."
8374 (setq fmt (or fmt org-email-link-description-format))
8375 (let* ((p org-store-link-plist)
8376 (to (plist-get p :toaddress))
8377 (from (plist-get p :fromaddress))
8380 (cons "%c" (plist-get p :fromto))
8381 (cons "%F" (plist-get p :from))
8382 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
8383 (cons "%T" (plist-get p :to))
8384 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
8385 (cons "%s" (plist-get p :subject))
8386 (cons "%m" (plist-get p :message-id)))))
8387 (when (string-match "%c" fmt)
8388 ;; Check if the user wrote this message
8389 (if (and org-from-is-user-regexp from to
8390 (save-match-data (string-match org-from-is-user-regexp from)))
8391 (setq fmt (replace-match "to %t" t t fmt))
8392 (setq fmt (replace-match "from %f" t t fmt))))
8393 (org-replace-escapes fmt table)))
8395 (defun org-make-org-heading-search-string (&optional string heading)
8396 "Make search string for STRING or current headline."
8398 (let ((s (or string (org-get-heading))))
8399 (unless (and string (not heading))
8400 ;; We are using a headline, clean up garbage in there.
8401 (if (string-match org-todo-regexp s)
8402 (setq s (replace-match "" t t s)))
8403 (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
8404 (setq s (replace-match "" t t s)))
8405 (setq s (org-trim s))
8406 (if (string-match (concat "^\\(" org-quote-string "\\|"
8407 org-comment-string "\\)") s)
8408 (setq s (replace-match "" t t s)))
8409 (while (string-match org-ts-regexp s)
8410 (setq s (replace-match "" t t s))))
8411 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
8412 (setq s (replace-match " " t t s)))
8413 (or string (setq s (concat "*" s))) ; Add * for headlines
8414 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
8416 (defun org-make-link (&rest strings)
8417 "Concatenate STRINGS."
8418 (apply 'concat strings))
8420 (defun org-make-link-string (link &optional description)
8421 "Make a link with brackets, consisting of LINK and DESCRIPTION."
8422 (unless (string-match "\\S-" link)
8423 (error "Empty link"))
8424 (when (and description
8425 (stringp description)
8426 (not (string-match "\\S-" description)))
8427 (setq description nil))
8428 (when (stringp description)
8429 ;; Remove brackets from the description, they are fatal.
8430 (while (string-match "\\[" description)
8431 (setq description (replace-match "{" t t description)))
8432 (while (string-match "\\]" description)
8433 (setq description (replace-match "}" t t description))))
8434 (when (equal (org-link-escape link) description)
8435 ;; No description needed, it is identical
8436 (setq description nil))
8437 (when (and (not description)
8438 (not (equal link (org-link-escape link))))
8439 (setq description (org-extract-attributes link)))
8440 (concat "[[" (org-link-escape link) "]"
8441 (if description (concat "[" description "]") "")
8444 (defconst org-link-escape-chars
8448 (?\340 . "%E0") ; `a
8449 (?\342 . "%E2") ; ^a
8450 (?\347 . "%E7") ; ,c
8451 (?\350 . "%E8") ; `e
8452 (?\351 . "%E9") ; 'e
8453 (?\352 . "%EA") ; ^e
8454 (?\356 . "%EE") ; ^i
8455 (?\364 . "%F4") ; ^o
8456 (?\371 . "%F9") ; `u
8457 (?\373 . "%FB") ; ^u
8463 "Association list of escapes for some characters problematic in links.
8464 This is the list that is used for internal purposes.")
8466 (defvar org-url-encoding-use-url-hexify nil)
8468 (defconst org-link-escape-chars-browser
8469 '((?\ . "%20")) ; 32 for the SPC char
8470 "Association list of escapes for some characters problematic in links.
8471 This is the list that is used before handing over to the browser.")
8473 (defun org-link-escape (text &optional table)
8474 "Escape characters in TEXT that are problematic for links."
8475 (if (and org-url-encoding-use-url-hexify (not table))
8476 (url-hexify-string text)
8477 (setq table (or table org-link-escape-chars))
8479 (let ((re (mapconcat (lambda (x) (regexp-quote
8480 (char-to-string (car x))))
8482 (while (string-match re text)
8485 (cdr (assoc (string-to-char (match-string 0 text))
8490 (defun org-link-unescape (text &optional table)
8491 "Reverse the action of `org-link-escape'."
8492 (if (and org-url-encoding-use-url-hexify (not table))
8493 (url-unhex-string text)
8494 (setq table (or table org-link-escape-chars))
8496 (let ((case-fold-search t)
8497 (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
8499 (while (string-match re text)
8502 (char-to-string (car (rassoc (upcase (match-string 0 text))
8507 (defun org-xor (a b)
8511 (defun org-fixup-message-id-for-http (s)
8512 "Replace special characters in a message id, so it can be used in an http query."
8513 (when (string-match "%" s)
8514 (setq s (mapconcat (lambda (c)
8517 (char-to-string c)))
8519 (while (string-match "<" s)
8520 (setq s (replace-match "%3C" t t s)))
8521 (while (string-match ">" s)
8522 (setq s (replace-match "%3E" t t s)))
8523 (while (string-match "@" s)
8524 (setq s (replace-match "%40" t t s)))
8528 (defun org-insert-link-global ()
8529 "Insert a link like Org-mode does.
8530 This command can be called in any mode to insert a link in Org-mode syntax."
8532 (org-load-modules-maybe)
8533 (org-run-like-in-org-mode 'org-insert-link))
8535 (defun org-insert-link (&optional complete-file link-location)
8536 "Insert a link. At the prompt, enter the link.
8538 Completion can be used to insert any of the link protocol prefixes like
8541 The history can be used to select a link previously stored with
8542 `org-store-link'. When the empty string is entered (i.e. if you just
8543 press RET at the prompt), the link defaults to the most recently
8544 stored link. As SPC triggers completion in the minibuffer, you need to
8545 use M-SPC or C-q SPC to force the insertion of a space character.
8547 You will also be prompted for a description, and if one is given, it will
8548 be displayed in the buffer instead of the link.
8550 If there is already a link at point, this command will allow you to edit link
8551 and description parts.
8553 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
8554 be selected using completion. The path to the file will be relative to the
8555 current directory if the file is in the current directory or a subdirectory.
8556 Otherwise, the link will be the absolute path as completed in the minibuffer
8557 \(i.e. normally ~/path/to/file). You can configure this behavior using the
8558 option `org-link-file-path-type'.
8560 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
8561 the current directory or below.
8563 With three \\[universal-argument] prefixes, negate the meaning of
8564 `org-keep-stored-link-after-insertion'.
8566 If `org-make-link-description-function' is non-nil, this function will be
8567 called with the link target, and the result will be the default
8570 If the LINK-LOCATION parameter is non-nil, this value will be
8571 used as the link location instead of reading one interactively."
8573 (let* ((wcf (current-window-configuration))
8574 (region (if (org-region-active-p)
8575 (buffer-substring (region-beginning) (region-end))))
8576 (remove (and region (list (region-beginning) (region-end))))
8578 tmphist ; byte-compile incorrectly complains about this
8579 (link link-location)
8580 entry file all-prefixes)
8582 (link-location) ; specified by arg, just use it.
8583 ((org-in-regexp org-bracket-link-regexp 1)
8584 ;; We do have a link at point, and we are going to edit it.
8585 (setq remove (list (match-beginning 0) (match-end 0)))
8586 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
8587 (setq link (read-string "Link: "
8589 (org-match-string-no-properties 1)))))
8590 ((or (org-in-regexp org-angle-link-re)
8591 (org-in-regexp org-plain-link-re))
8592 ;; Convert to bracket link
8593 (setq remove (list (match-beginning 0) (match-end 0))
8594 link (read-string "Link: "
8595 (org-remove-angle-brackets (match-string 0)))))
8596 ((member complete-file '((4) (16)))
8597 ;; Completing read for file names.
8598 (setq link (org-file-complete-link complete-file)))
8600 ;; Read link, with completion for stored links.
8601 (with-output-to-temp-buffer "*Org Links*"
8602 (princ "Insert a link.
8603 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
8604 (when org-stored-links
8605 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
8608 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
8609 (reverse org-stored-links) "\n"))))
8610 (let ((cw (selected-window)))
8611 (select-window (get-buffer-window "*Org Links*" 'visible))
8612 (setq truncate-lines t)
8613 (unless (pos-visible-in-window-p (point-max))
8614 (org-fit-window-to-buffer))
8615 (and (window-live-p cw) (select-window cw)))
8616 ;; Fake a link history, containing the stored links.
8617 (setq tmphist (append (mapcar 'car org-stored-links)
8618 org-insert-link-history))
8619 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
8620 (mapcar 'car org-link-abbrev-alist)
8625 (let ((org-completion-use-ido nil)
8626 (org-completion-use-iswitchb nil))
8627 (org-completing-read
8630 (mapcar (lambda (x) (list (concat x ":")))
8632 (mapcar 'car org-stored-links))
8635 (car (car org-stored-links)))))
8636 (if (not (string-match "\\S-" link))
8637 (error "No link selected"))
8638 (if (or (member link all-prefixes)
8639 (and (equal ":" (substring link -1))
8640 (member (substring link 0 -1) all-prefixes)
8641 (setq link (substring link 0 -1))))
8642 (setq link (org-link-try-special-completion link))))
8643 (set-window-configuration wcf)
8644 (kill-buffer "*Org Links*"))
8645 (setq entry (assoc link org-stored-links))
8646 (or entry (push link org-insert-link-history))
8647 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
8648 (not org-keep-stored-link-after-insertion))
8649 (setq org-stored-links (delq (assoc link org-stored-links)
8651 (setq desc (or desc (nth 1 entry)))))
8653 (if (string-match org-plain-link-re link)
8654 ;; URL-like link, normalize the use of angular brackets.
8655 (setq link (org-make-link (org-remove-angle-brackets link))))
8657 ;; Check if we are linking to the current file with a search option
8658 ;; If yes, simplify the link by using only the search option.
8659 (when (and buffer-file-name
8660 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
8661 (let* ((path (match-string 1 link))
8662 (case-fold-search nil)
8663 (search (match-string 2 link)))
8665 (if (equal (file-truename buffer-file-name) (file-truename path))
8666 ;; We are linking to this same file, with a search option
8667 (setq link search)))))
8669 ;; Check if we can/should use a relative path. If yes, simplify the link
8670 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
8671 (let* ((type (match-string 1 link))
8672 (path (match-string 2 link))
8674 (case-fold-search nil))
8676 ((or (eq org-link-file-path-type 'absolute)
8677 (equal complete-file '(16)))
8678 (setq path (abbreviate-file-name (expand-file-name path))))
8679 ((eq org-link-file-path-type 'noabbrev)
8680 (setq path (expand-file-name path)))
8681 ((eq org-link-file-path-type 'relative)
8682 (setq path (file-relative-name path)))
8685 (if (string-match (concat "^" (regexp-quote
8687 (file-name-as-directory
8688 default-directory))))
8689 (expand-file-name path))
8690 ;; We are linking a file with relative path name.
8691 (setq path (substring (expand-file-name path)
8693 (setq path (abbreviate-file-name (expand-file-name path)))))))
8694 (setq link (concat type path))
8695 (if (equal desc origpath)
8698 (if org-make-link-description-function
8699 (setq desc (funcall org-make-link-description-function link desc)))
8701 (setq desc (read-string "Description: " desc))
8702 (unless (string-match "\\S-" desc) (setq desc nil))
8703 (if remove (apply 'delete-region remove))
8704 (insert (org-make-link-string link desc))))
8706 (defun org-link-try-special-completion (type)
8707 "If there is completion support for link type TYPE, offer it."
8708 (let ((fun (intern (concat "org-" type "-complete-link"))))
8711 (read-string "Link (no completion support): " (concat type ":")))))
8713 (defun org-file-complete-link (&optional arg)
8714 "Create a file link using completion."
8716 (setq file (read-file-name "File: "))
8717 (let ((pwd (file-name-as-directory (expand-file-name ".")))
8718 (pwd1 (file-name-as-directory (abbreviate-file-name
8719 (expand-file-name ".")))))
8722 (setq link (org-make-link
8724 (abbreviate-file-name (expand-file-name file)))))
8725 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
8726 (setq link (org-make-link "file:" (match-string 1 file))))
8727 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
8728 (expand-file-name file))
8729 (setq link (org-make-link
8730 "file:" (match-string 1 (expand-file-name file)))))
8731 (t (setq link (org-make-link "file:" file)))))
8734 (defun org-completing-read (&rest args)
8735 "Completing-read with SPACE being a normal character."
8736 (let ((minibuffer-local-completion-map
8737 (copy-keymap minibuffer-local-completion-map)))
8738 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
8739 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
8740 (apply 'org-icompleting-read args)))
8742 (defun org-completing-read-no-i (&rest args)
8743 (let (org-completion-use-ido org-completion-use-iswitchb)
8744 (apply 'org-completing-read args)))
8746 (defun org-iswitchb-completing-read (prompt choices &rest args)
8747 "Use iswitch as a completing-read replacement to choose from choices.
8748 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
8750 (let* ((iswitchb-use-virtual-buffers nil)
8751 (iswitchb-make-buflist-hook
8753 (setq iswitchb-temp-buflist choices))))
8754 (iswitchb-read-buffer prompt)))
8756 (defun org-icompleting-read (&rest args)
8757 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
8758 (org-without-partial-completion
8759 (if (and org-completion-use-ido
8760 (fboundp 'ido-completing-read)
8761 (boundp 'ido-mode) ido-mode
8762 (listp (second args)))
8763 (let ((ido-enter-matching-directory nil))
8764 (apply 'ido-completing-read (concat (car args))
8765 (if (consp (car (nth 1 args)))
8766 (mapcar (lambda (x) (car x)) (nth 1 args))
8769 (if (and org-completion-use-iswitchb
8770 (boundp 'iswitchb-mode) iswitchb-mode
8771 (listp (second args)))
8772 (apply 'org-iswitchb-completing-read (concat (car args))
8773 (if (consp (car (nth 1 args)))
8774 (mapcar (lambda (x) (car x)) (nth 1 args))
8777 (apply 'completing-read args)))))
8779 (defun org-extract-attributes (s)
8780 "Extract the attributes cookie from a string and set as text property."
8781 (let (a attr (start 0) key value)
8783 (when (string-match "{{\\([^}]+\\)}}$" s)
8784 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
8785 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
8786 (setq key (match-string 1 a) value (match-string 2 a)
8788 attr (plist-put attr (intern key) value))))
8789 (org-add-props s nil 'org-attr attr))
8792 (defun org-extract-attributes-from-string (tag)
8793 (let (key value attr)
8794 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
8795 (setq key (match-string 1 tag) value (match-string 2 tag)
8796 tag (replace-match "" t t tag)
8797 attr (plist-put attr (intern key) value)))
8800 (defun org-attributes-to-string (plist)
8801 "Format a property list into an HTML attribute list."
8802 (let ((s "") key value)
8804 (setq key (pop plist) value (pop plist))
8806 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
8809 ;;; Opening/following a link
8811 (defvar org-link-search-failed nil)
8813 (defvar org-open-link-functions nil
8814 "Hook for functions finding a plain text link.
8815 These functions must take a single argument, the link content.
8816 They will be called for links that look like [[link text][description]]
8817 when LINK TEXT does not have a protocol like \"http:\" and does not look
8818 like a filename (e.g. \"./blue.png\").
8820 These functions will be called *before* Org attempts to resolve the
8821 link by doing text searches in the current buffer - so if you want a
8822 link \"[[target]]\" to still find \"<<target>>\", your function should
8823 handle this as a special case.
8825 When the function does handle the link, it must return a non-nil value.
8826 If it decides that it is not responsible for this link, it must return
8827 nil to indicate that that Org-mode can continue with other options
8828 like exact and fuzzy text search.")
8830 (defun org-next-link ()
8831 "Move forward to the next link.
8832 If the link is in hidden text, expose it."
8834 (when (and org-link-search-failed (eq this-command last-command))
8835 (goto-char (point-min))
8836 (message "Link search wrapped back to beginning of buffer"))
8837 (setq org-link-search-failed nil)
8838 (let* ((pos (point))
8840 (a (assoc :link ct)))
8841 (if a (goto-char (nth 2 a)))
8842 (if (re-search-forward org-any-link-re nil t)
8844 (goto-char (match-beginning 0))
8845 (if (org-invisible-p) (org-show-context)))
8847 (setq org-link-search-failed t)
8848 (error "No further link found"))))
8850 (defun org-previous-link ()
8851 "Move backward to the previous link.
8852 If the link is in hidden text, expose it."
8854 (when (and org-link-search-failed (eq this-command last-command))
8855 (goto-char (point-max))
8856 (message "Link search wrapped back to end of buffer"))
8857 (setq org-link-search-failed nil)
8858 (let* ((pos (point))
8860 (a (assoc :link ct)))
8861 (if a (goto-char (nth 1 a)))
8862 (if (re-search-backward org-any-link-re nil t)
8864 (goto-char (match-beginning 0))
8865 (if (org-invisible-p) (org-show-context)))
8867 (setq org-link-search-failed t)
8868 (error "No further link found"))))
8870 (defun org-translate-link (s)
8871 "Translate a link string if a translation function has been defined."
8872 (if (and org-link-translation-function
8873 (fboundp org-link-translation-function)
8874 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
8876 (setq s (funcall org-link-translation-function
8877 (match-string 1) (match-string 2)))
8878 (concat (car s) ":" (cdr s)))
8881 (defun org-translate-link-from-planner (type path)
8882 "Translate a link from Emacs Planner syntax so that Org can follow it.
8883 This is still an experimental function, your mileage may vary."
8885 ((member type '("http" "https" "news" "ftp"))
8886 ;; standard Internet links are the same.
8888 ((and (equal type "irc") (string-match "^//" path))
8889 ;; Planner has two / at the beginning of an irc link, we have 1.
8890 ;; We should have zero, actually....
8891 (setq path (substring path 1)))
8892 ((and (equal type "lisp") (string-match "^/" path))
8893 ;; Planner has a slash, we do not.
8894 (setq type "elisp" path (substring path 1)))
8895 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
8896 ;; A typical message link. Planner has the id after the final slash,
8897 ;; we separate it with a hash mark
8898 (setq path (concat (match-string 1 path) "#"
8899 (org-remove-angle-brackets (match-string 2 path)))))
8903 (defun org-find-file-at-mouse (ev)
8904 "Open file link or URL at mouse."
8906 (mouse-set-point ev)
8907 (org-open-at-point 'in-emacs))
8909 (defun org-open-at-mouse (ev)
8910 "Open file link or URL at mouse."
8912 (mouse-set-point ev)
8913 (if (eq major-mode 'org-agenda-mode)
8914 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
8915 (org-open-at-point))
8917 (defvar org-window-config-before-follow-link nil
8918 "The window configuration before following a link.
8919 This is saved in case the need arises to restore it.")
8921 (defvar org-open-link-marker (make-marker)
8922 "Marker pointing to the location where `org-open-at-point; was called.")
8925 (defun org-open-at-point-global ()
8926 "Follow a link like Org-mode does.
8927 This command can be called in any mode to follow a link that has
8930 (org-run-like-in-org-mode 'org-open-at-point))
8933 (defun org-open-link-from-string (s &optional arg reference-buffer)
8934 "Open a link in the string S, as if it was in Org-mode."
8935 (interactive "sLink: \nP")
8936 (let ((reference-buffer (or reference-buffer (current-buffer))))
8938 (let ((org-inhibit-startup t))
8941 (goto-char (point-min))
8942 (when reference-buffer
8943 (setq org-link-abbrev-alist-local
8944 (with-current-buffer reference-buffer
8945 org-link-abbrev-alist-local)))
8946 (org-open-at-point arg reference-buffer)))))
8948 (defvar org-open-at-point-functions nil
8949 "Hook that is run when following a link at point.
8951 Functions in this hook must return t if they identify and follow
8952 a link at point. If they don't find anything interesting at point,
8953 they must return nil.")
8955 (defun org-open-at-point (&optional in-emacs reference-buffer)
8956 "Open link at or after point.
8957 If there is no link at point, this function will search forward up to
8958 the end of the current line.
8959 Normally, files will be opened by an appropriate application. If the
8960 optional argument IN-EMACS is non-nil, Emacs will visit the file.
8961 With a double prefix argument, try to open outside of Emacs, in the
8962 application the system uses for this file type."
8964 ;; if in a code block, then open the block's results
8965 (unless (call-interactively #'org-babel-open-src-block-result)
8966 (org-load-modules-maybe)
8967 (move-marker org-open-link-marker (point))
8968 (setq org-window-config-before-follow-link (current-window-configuration))
8969 (org-remove-occur-highlights nil nil t)
8971 ((and (org-on-heading-p)
8973 (concat org-plain-link-re "\\|"
8974 org-bracket-link-regexp "\\|"
8975 org-angle-link-re "\\|"
8976 "[ \t]:[^ \t\n]+:[ \t]*$")))
8977 (not (get-text-property (point) 'org-linked-text)))
8978 (or (org-offer-links-in-entry in-emacs)
8979 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
8980 ((run-hook-with-args-until-success 'org-open-at-point-functions))
8981 ((org-at-timestamp-p t) (org-follow-timestamp-link))
8982 ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
8983 (org-footnote-action))
8985 (let (type path link line search (pos (point)))
8988 (skip-chars-forward "^]\n\r")
8989 (when (org-in-regexp org-bracket-link-regexp 1)
8990 (setq link (org-extract-attributes
8991 (org-link-unescape (org-match-string-no-properties 1))))
8992 (while (string-match " *\n *" link)
8993 (setq link (replace-match " " t t link)))
8994 (setq link (org-link-expand-abbrev link))
8996 ((or (file-name-absolute-p link)
8997 (string-match "^\\.\\.?/" link))
8998 (setq type "file" path link))
8999 ((string-match org-link-re-with-space3 link)
9000 (setq type (match-string 1 link) path (match-string 2 link)))
9001 (t (setq type "thisfile" path link)))
9004 (when (get-text-property (point) 'org-linked-text)
9005 (setq type "thisfile"
9006 pos (if (get-text-property (1+ (point)) 'org-linked-text)
9007 (1+ (point)) (point))
9008 path (buffer-substring
9009 (previous-single-property-change pos 'org-linked-text)
9010 (next-single-property-change pos 'org-linked-text)))
9014 (when (or (org-in-regexp org-angle-link-re)
9015 (org-in-regexp org-plain-link-re))
9016 (setq type (match-string 1) path (match-string 2))
9019 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
9021 path (match-string 1))
9022 (while (string-match ":" path)
9023 (setq path (replace-match "+" t t path)))
9025 (when (org-in-regexp "<\\([^><\n]+\\)>")
9026 (setq type "tree-match"
9027 path (match-string 1))
9030 (error "No link found"))
9032 ;; switch back to reference buffer
9033 ;; needed when if called in a temporary buffer through
9034 ;; org-open-link-from-string
9035 (with-current-buffer (or reference-buffer (current-buffer))
9037 ;; Remove any trailing spaces in path
9038 (if (string-match " +\\'" path)
9039 (setq path (replace-match "" t t path)))
9040 (if (and org-link-translation-function
9041 (fboundp org-link-translation-function))
9042 ;; Check if we need to translate the link
9043 (let ((tmp (funcall org-link-translation-function type path)))
9044 (setq type (car tmp) path (cdr tmp))))
9048 ((assoc type org-link-protocols)
9049 (funcall (nth 1 (assoc type org-link-protocols)) path))
9051 ((equal type "mailto")
9052 (let ((cmd (car org-link-mailto-program))
9053 (args (cdr org-link-mailto-program)) args1
9054 (address path) (subject "") a)
9055 (if (string-match "\\(.*\\)::\\(.*\\)" path)
9056 (setq address (match-string 1 path)
9057 subject (org-link-escape (match-string 2 path))))
9060 ((not (stringp (car args))) (push (pop args) args1))
9061 (t (setq a (pop args))
9062 (if (string-match "%a" a)
9063 (setq a (replace-match address t t a)))
9064 (if (string-match "%s" a)
9065 (setq a (replace-match subject t t a)))
9067 (apply cmd (nreverse args1))))
9069 ((member type '("http" "https" "ftp" "news"))
9070 (browse-url (concat type ":" (org-link-escape
9071 path org-link-escape-chars-browser))))
9073 ((string= type "doi")
9074 (browse-url (concat "http://dx.doi.org/"
9076 path org-link-escape-chars-browser))))
9078 ((member type '("message"))
9079 (browse-url (concat type ":" path)))
9081 ((string= type "tags")
9082 (org-tags-view in-emacs path))
9084 ((string= type "tree-match")
9085 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
9087 ((string= type "file")
9088 (if (string-match "::\\([0-9]+\\)\\'" path)
9089 (setq line (string-to-number (match-string 1 path))
9090 path (substring path 0 (match-beginning 0)))
9091 (if (string-match "::\\(.+\\)\\'" path)
9092 (setq search (match-string 1 path)
9093 path (substring path 0 (match-beginning 0)))))
9094 (if (string-match "[*?{]" (file-name-nondirectory path))
9096 (org-open-file path in-emacs line search)))
9098 ((string= type "news")
9100 (org-gnus-follow-link path))
9102 ((string= type "shell")
9104 (if (or (not org-confirm-shell-link-function)
9105 (funcall org-confirm-shell-link-function
9106 (format "Execute \"%s\" in shell? "
9107 (org-add-props cmd nil
9108 'face 'org-warning))))
9110 (message "Executing %s" cmd)
9111 (shell-command cmd))
9114 ((string= type "elisp")
9116 (if (or (not org-confirm-elisp-link-function)
9117 (funcall org-confirm-elisp-link-function
9118 (format "Execute \"%s\" as elisp? "
9119 (org-add-props cmd nil
9120 'face 'org-warning))))
9121 (message "%s => %s" cmd
9122 (if (equal (string-to-char cmd) ?\()
9124 (call-interactively (read cmd))))
9127 ((and (string= type "thisfile")
9128 (run-hook-with-args-until-success
9129 'org-open-link-functions path)))
9131 ((string= type "thisfile")
9133 (switch-to-buffer-other-window
9134 (org-get-buffer-for-internal-link (current-buffer)))
9135 (org-mark-ring-push))
9136 (let ((cmd `(org-link-search
9138 ,(cond ((equal in-emacs '(4)) 'occur)
9139 ((equal in-emacs '(16)) 'org-occur)
9142 (condition-case nil (eval cmd)
9143 (error (progn (widen) (eval cmd))))))
9146 (browse-url-at-point)))))))
9147 (move-marker org-open-link-marker nil)
9148 (run-hook-with-args 'org-follow-link-hook)))
9150 (defun org-offer-links-in-entry (&optional nth zero)
9151 "Offer links in the current entry and follow the selected link.
9152 If there is only one link, follow it immediately as well.
9153 If NTH is an integer, immediately pick the NTH link found.
9154 If ZERO is a string, check also this string for a link, and if
9155 there is one, offer it as link number zero."
9156 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
9157 "\\(" org-angle-link-re "\\)\\|"
9158 "\\(" org-plain-link-re "\\)"))
9160 (in-emacs (if (integerp nth) nil nth))
9161 have-zero end links link c)
9162 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
9163 (push (match-string 0 zero) links)
9164 (setq cnt (1- cnt) have-zero t))
9166 (org-back-to-heading t)
9167 (setq end (save-excursion (outline-next-heading) (point)))
9168 (while (re-search-forward re end t)
9169 (push (match-string 0) links))
9170 (setq links (org-uniquify (reverse links))))
9174 (message "No links"))
9175 ((equal (length links) 1)
9176 (setq link (list (car links))))
9177 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
9178 (setq link (nth (if have-zero nth (1- nth)) links)))
9179 (t ; we have to select a link
9181 (save-window-excursion
9182 (delete-other-windows)
9183 (with-output-to-temp-buffer "*Select Link*"
9185 (if (not (string-match org-bracket-link-regexp l))
9186 (princ (format "[%c] %s\n" (incf cnt)
9187 (org-remove-angle-brackets l)))
9189 (princ (format "[%c] %s (%s)\n" (incf cnt)
9190 (match-string 3 l) (match-string 1 l)))
9191 (princ (format "[%c] %s\n" (incf cnt)
9192 (match-string 1 l))))))
9194 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
9195 (message "Select link to open, RET to open all:")
9196 (setq c (read-char-exclusive))
9197 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
9198 (when (equal c ?q) (error "Abort"))
9202 (if have-zero (setq nth (1+ nth)))
9203 (unless (and (integerp nth) (>= (length links) nth))
9204 (error "Invalid link selection"))
9205 (setq link (list (nth (1- nth) links))))))
9207 (let ((buf (current-buffer)))
9209 (org-open-link-from-string l in-emacs buf))
9213 ;; Add special file links that specify the way of opening
9215 (org-add-link-type "file+sys" 'org-open-file-with-system)
9216 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
9217 (defun org-open-file-with-system (path)
9218 "Open file at PATH using the system way of opening it."
9219 (org-open-file path 'system))
9220 (defun org-open-file-with-emacs (path)
9221 "Open file at PATH in Emacs."
9222 (org-open-file path 'emacs))
9223 (defun org-remove-file-link-modifiers ()
9224 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
9225 (goto-char (point-min))
9226 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
9228 (replace-match "file:" t t))))
9229 (eval-after-load "org-exp"
9230 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
9231 'org-remove-file-link-modifiers))
9235 (defun org-get-effort (&optional pom)
9236 "Get the effort estimate for the current entry."
9237 (org-entry-get pom org-effort-property))
9241 (defvar org-create-file-search-functions nil
9242 "List of functions to construct the right search string for a file link.
9243 These functions are called in turn with point at the location to
9244 which the link should point.
9246 A function in the hook should first test if it would like to
9247 handle this file type, for example by checking the `major-mode'
9248 or the file extension. If it decides not to handle this file, it
9249 should just return nil to give other functions a chance. If it
9250 does handle the file, it must return the search string to be used
9251 when following the link. The search string will be part of the
9252 file link, given after a double colon, and `org-open-at-point'
9253 will automatically search for it. If special measures must be
9254 taken to make the search successful, another function should be
9255 added to the companion hook `org-execute-file-search-functions',
9258 A function in this hook may also use `setq' to set the variable
9259 `description' to provide a suggestion for the descriptive text to
9260 be used for this link when it gets inserted into an Org-mode
9261 buffer with \\[org-insert-link].")
9263 (defvar org-execute-file-search-functions nil
9264 "List of functions to execute a file search triggered by a link.
9266 Functions added to this hook must accept a single argument, the
9267 search string that was part of the file link, the part after the
9268 double colon. The function must first check if it would like to
9269 handle this search, for example by checking the `major-mode' or
9270 the file extension. If it decides not to handle this search, it
9271 should just return nil to give other functions a chance. If it
9272 does handle the search, it must return a non-nil value to keep
9273 other functions from trying.
9275 Each function can access the current prefix argument through the
9276 variable `current-prefix-argument'. Note that a single prefix is
9277 used to force opening a link in Emacs, so it may be good to only
9278 use a numeric or double prefix to guide the search function.
9280 In case this is needed, a function in this hook can also restore
9281 the window configuration before `org-open-at-point' was called using:
9283 (set-window-configuration org-window-config-before-follow-link)")
9285 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
9286 (defun org-link-search (s &optional type avoid-pos)
9287 "Search for a link search option.
9288 If S is surrounded by forward slashes, it is interpreted as a
9289 regular expression. In org-mode files, this will create an `org-occur'
9290 sparse tree. In ordinary files, `occur' will be used to list matches.
9291 If the current buffer is in `dired-mode', grep will be used to search
9292 in all files. If AVOID-POS is given, ignore matches near that position."
9293 (let ((case-fold-search t)
9294 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
9295 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
9296 (append '(("") (" ") ("\t") ("\n"))
9300 (pre nil) (post nil)
9301 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
9303 ;; First check if there are any special search functions
9304 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
9305 ;; Now try the builtin stuff
9306 ((and (equal (string-to-char s0) ?#)
9309 (goto-char (point-min))
9312 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
9313 (setq type 'dedicated
9314 pos (match-beginning 0))))
9315 ;; There is an exact target for this
9317 (org-back-to-heading t)))
9319 (goto-char (point-min))
9322 (concat "<<" (regexp-quote s0) ">>") nil t)
9323 (setq type 'dedicated
9324 pos (match-beginning 0))))
9325 ;; There is an exact target for this
9327 ((and (string-match "^(\\(.*\\))$" s0)
9329 (goto-char (point-min))
9332 (concat "[^[]" (regexp-quote
9333 (format org-coderef-label-format
9334 (match-string 1 s0))))
9336 (setq type 'dedicated
9337 pos (1+ (match-beginning 0))))))
9338 ;; There is a coderef target for this
9340 ((string-match "^/\\(.*\\)/$" s)
9341 ;; A regular expression
9344 (org-occur (match-string 1 s)))
9345 ;;((eq major-mode 'dired-mode)
9346 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
9347 (t (org-do-occur (match-string 1 s)))))
9348 ((and (org-mode-p) org-link-search-must-match-exact-headline)
9349 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
9350 (goto-char (point-min))
9352 ((let (case-fold-search)
9353 (re-search-forward (format org-complex-heading-regexp-format
9356 ;; OK, found a match
9357 (goto-char (match-beginning 0)))
9358 ((and (not org-link-search-inhibit-query)
9359 (eq org-link-search-must-match-exact-headline 'query-to-create)
9360 (y-or-n-p "No match - create this as a new heading? "))
9361 (goto-char (point-max))
9362 (or (bolp) (newline))
9363 (insert "* " s "\n")
9364 (beginning-of-line 0))
9367 (error "No match"))))
9369 ;; A normal search string
9370 (when (equal (string-to-char s) ?*)
9371 ;; Anchor on headlines, post may include tags.
9372 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
9373 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
9375 (remove-text-properties
9377 '(face nil mouse-face nil keymap nil fontified nil) s)
9378 ;; Make a series of regular expressions to find a match
9379 (setq words (org-split-string s "[ \n\r\t]+")
9381 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
9382 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
9384 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
9385 re2a (concat "[ \t\r\n]" re2a_)
9386 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
9387 re4 (concat "[^a-zA-Z_]" re4_)
9389 re1 (concat pre re2 post)
9390 re3 (concat pre (if pre re4_ re4) post)
9391 re5 (concat pre ".*" re4)
9392 re2 (concat pre re2)
9393 re2a (concat pre (if pre re2a_ re2a))
9394 re4 (concat pre (if pre re4_ re4))
9395 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
9396 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
9400 ((eq type 'org-occur) (org-occur reall))
9401 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
9402 (t (goto-char (point-min))
9404 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
9405 (org-search-not-self 1 re1 nil t)
9406 (org-search-not-self 1 re2 nil t)
9407 (org-search-not-self 1 re2a nil t)
9408 (org-search-not-self 1 re3 nil t)
9409 (org-search-not-self 1 re4 nil t)
9410 (org-search-not-self 1 re5 nil t)
9412 (goto-char (match-beginning 1))
9414 (error "No match"))))))
9415 (and (org-mode-p) (org-show-context 'link-search))
9418 (defun org-search-not-self (group &rest args)
9419 "Execute `re-search-forward', but only accept matches that do not
9420 enclose the position of `org-open-link-marker'."
9421 (let ((m org-open-link-marker))
9423 (while (apply 're-search-forward args)
9424 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
9425 (goto-char (match-end group))
9426 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
9427 (> (match-beginning 0) (marker-position m))
9428 (< (match-end 0) (marker-position m)))
9430 (or (not (org-in-regexp
9431 org-bracket-link-analytic-regexp 1))
9432 (not (match-end 4)) ; no description
9433 (and (<= (match-beginning 4) (point))
9434 (>= (match-end 4) (point))))))
9435 (throw 'exit (point))))))))
9437 (defun org-get-buffer-for-internal-link (buffer)
9438 "Return a buffer to be used for displaying the link target of internal links."
9440 ((not org-display-internal-link-with-indirect-buffer)
9442 ((string-match "(Clone)$" (buffer-name buffer))
9443 (message "Buffer is already a clone, not making another one")
9444 ;; we also do not modify visibility in this case
9446 (t ; make a new indirect buffer for displaying the link
9447 (let* ((bn (buffer-name buffer))
9448 (ibn (concat bn "(Clone)"))
9449 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
9450 (with-current-buffer ib (org-overview))
9453 (defun org-do-occur (regexp &optional cleanup)
9454 "Call the Emacs command `occur'.
9455 If CLEANUP is non-nil, remove the printout of the regular expression
9456 in the *Occur* buffer. This is useful if the regex is long and not useful
9460 (let ((cwin (selected-window)) win beg end)
9461 (when (setq win (get-buffer-window "*Occur*"))
9462 (select-window win))
9463 (goto-char (point-min))
9464 (when (re-search-forward "match[a-z]+" nil t)
9465 (setq beg (match-end 0))
9466 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
9467 (setq end (1- (match-beginning 0)))))
9468 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
9469 (goto-char (point-min))
9470 (select-window cwin))))
9472 ;;; The mark ring for links jumps
9474 (defvar org-mark-ring nil
9475 "Mark ring for positions before jumps in Org-mode.")
9476 (defvar org-mark-ring-last-goto nil
9477 "Last position in the mark ring used to go back.")
9478 ;; Fill and close the ring
9479 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
9480 (loop for i from 1 to org-mark-ring-length do
9481 (push (make-marker) org-mark-ring))
9482 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
9485 (defun org-mark-ring-push (&optional pos buffer)
9486 "Put the current position or POS into the mark ring and rotate it."
9488 (setq pos (or pos (point)))
9489 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
9490 (move-marker (car org-mark-ring)
9492 (or buffer (current-buffer)))
9494 (substitute-command-keys
9495 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
9497 (defun org-mark-ring-goto (&optional n)
9498 "Jump to the previous position in the mark ring.
9499 With prefix arg N, jump back that many stored positions. When
9500 called several times in succession, walk through the entire ring.
9501 Org-mode commands jumping to a different position in the current file,
9502 or to another Org-mode file, automatically push the old position
9506 (if (eq last-command this-command)
9507 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
9508 (setq p org-mark-ring))
9509 (setq org-mark-ring-last-goto p)
9511 (switch-to-buffer (marker-buffer m))
9513 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
9515 (defun org-remove-angle-brackets (s)
9516 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
9517 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
9519 (defun org-add-angle-brackets (s)
9520 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
9521 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
9523 (defun org-remove-double-quotes (s)
9524 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
9525 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
9528 ;;; Following specific links
9530 (defun org-follow-timestamp-link ()
9532 ((org-at-date-range-p t)
9533 (let ((org-agenda-start-on-weekday)
9534 (t1 (match-string 1))
9535 (t2 (match-string 2)))
9536 (setq t1 (time-to-days (org-time-string-to-time t1))
9537 t2 (time-to-days (org-time-string-to-time t2)))
9538 (org-agenda-list nil t1 (1+ (- t2 t1)))))
9539 ((org-at-timestamp-p t)
9540 (org-agenda-list nil (time-to-days (org-time-string-to-time
9541 (substring (match-string 1) 0 10)))
9543 (t (error "This should not happen"))))
9546 ;;; Following file links
9547 (defvar org-wait nil)
9548 (defun org-open-file (path &optional in-emacs line search)
9549 "Open the file at PATH.
9550 First, this expands any special file name abbreviations. Then the
9551 configuration variable `org-file-apps' is checked if it contains an
9552 entry for this file type, and if yes, the corresponding command is launched.
9554 If no application is found, Emacs simply visits the file.
9556 With optional prefix argument IN-EMACS, Emacs will visit the file.
9557 With a double \\[universal-argument] \\[universal-argument] \
9558 prefix arg, Org tries to avoid opening in Emacs
9559 and to use an external application to visit the file.
9561 Optional LINE specifies a line to go to, optional SEARCH a string
9562 to search for. If LINE or SEARCH is given, the file will be
9563 opened in Emacs, unless an entry from org-file-apps that makes
9564 use of groups in a regexp matches.
9565 If the file does not exist, an error is thrown."
9566 (let* ((file (if (equal path "")
9568 (substitute-in-file-name (expand-file-name path))))
9569 (file-apps (append org-file-apps (org-default-apps)))
9570 (apps (org-remove-if
9571 'org-file-apps-entry-match-against-dlink-p file-apps))
9572 (apps-dlink (org-remove-if-not
9573 'org-file-apps-entry-match-against-dlink-p file-apps))
9574 (remp (and (assq 'remote apps) (org-file-remote-p file)))
9575 (dirp (if remp nil (file-directory-p file)))
9576 (file (if (and dirp org-open-directory-means-index-dot-org)
9577 (concat (file-name-as-directory file) "index.org")
9579 (a-m-a-p (assq 'auto-mode apps))
9580 (dfile (downcase file))
9581 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
9582 (link (cond ((and (eq line nil)
9586 (concat file "::" (number-to-string line)))
9588 (concat file "::" search))))
9589 (dlink (downcase link))
9590 (old-buffer (current-buffer))
9592 (old-mode major-mode)
9593 ext cmd link-match-data)
9594 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
9595 (setq ext (match-string 1 dfile))
9596 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
9597 (setq ext (match-string 1 dfile))))
9599 ((member in-emacs '((16) system))
9600 (setq cmd (cdr (assoc 'system apps))))
9601 (in-emacs (setq cmd 'emacs))
9603 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
9604 (and dirp (cdr (assoc 'directory apps)))
9605 ; first, try matching against apps-dlink
9606 ; if we get a match here, store the match data for later
9607 (let ((match (assoc-default dlink apps-dlink
9610 (progn (setq link-match-data (match-data))
9612 (progn (setq in-emacs (or in-emacs line search))
9613 nil))) ; if we have no match in apps-dlink,
9614 ; always open the file in emacs if line or search
9615 ; is given (for backwards compatibility)
9616 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
9618 (cdr (assoc ext apps))
9619 (cdr (assoc t apps))))))
9620 (when (eq cmd 'system)
9621 (setq cmd (cdr (assoc 'system apps))))
9622 (when (eq cmd 'default)
9623 (setq cmd (cdr (assoc t apps))))
9624 (when (eq cmd 'mailcap)
9626 (mailcap-parse-mailcaps)
9627 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
9628 (command (mailcap-mime-info mime-type)))
9629 (if (stringp command)
9631 (setq cmd 'emacs))))
9632 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
9633 (not (file-exists-p file))
9634 (not org-open-non-existing-files))
9635 (error "No such file: %s" file))
9637 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
9638 ;; Remove quotes around the file name - we'll use shell-quote-argument.
9639 (while (string-match "['\"]%s['\"]" cmd)
9640 (setq cmd (replace-match "%s" t t cmd)))
9641 (while (string-match "%s" cmd)
9642 (setq cmd (replace-match
9644 (shell-quote-argument
9645 (convert-standard-filename file)))
9648 ;; Replace "%1", "%2" etc. in command with group matches from regex
9650 (let ((match-index 1)
9651 (number-of-groups (- (/ (length link-match-data) 2) 1)))
9652 (set-match-data link-match-data)
9653 (while (<= match-index number-of-groups)
9654 (let ((regex (concat "%" (number-to-string match-index)))
9655 (replace-with (match-string match-index dlink)))
9656 (while (string-match regex cmd)
9657 (setq cmd (replace-match replace-with t t cmd))))
9658 (setq match-index (+ match-index 1)))))
9660 (save-window-excursion
9661 (start-process-shell-command cmd nil cmd)
9662 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
9666 (funcall (cdr (assq 'file org-link-frame-setup)) file)
9668 (if line (org-goto-line line)
9669 (if search (org-link-search search))))
9671 (let ((file (convert-standard-filename file)))
9673 (set-match-data link-match-data)
9675 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
9676 (and (org-mode-p) (eq old-mode 'org-mode)
9677 (or (not (equal old-buffer (current-buffer)))
9678 (not (equal old-pos (point))))
9679 (org-mark-ring-push old-pos old-buffer))))
9681 (defun org-file-apps-entry-match-against-dlink-p (entry)
9682 "This function returns non-nil if `entry' uses a regular
9683 expression which should be matched against the whole link by
9686 It assumes that is the case when the entry uses a regular
9687 expression which has at least one grouping construct and the
9688 action is either a lisp form or a command string containing
9689 '%1', i.e. using at least one subexpression match as a
9691 (let ((selector (car entry))
9692 (action (cdr entry)))
9693 (if (stringp selector)
9694 (and (> (regexp-opt-depth selector) 0)
9695 (or (and (stringp action)
9696 (string-match "%[0-9]" action))
9700 (defun org-default-apps ()
9701 "Return the default applications for this operating system."
9703 ((eq system-type 'darwin)
9704 org-file-apps-defaults-macosx)
9705 ((eq system-type 'windows-nt)
9706 org-file-apps-defaults-windowsnt)
9707 (t org-file-apps-defaults-gnu)))
9709 (defun org-apps-regexp-alist (list &optional add-auto-mode)
9710 "Convert extensions to regular expressions in the cars of LIST.
9711 Also, weed out any non-string entries, because the return value is used
9712 only for regexp matching.
9713 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
9714 point to the symbol `emacs', indicating that the file should
9715 be opened in Emacs."
9719 (if (not (stringp (car x)))
9721 (if (string-match "\\W" (car x))
9723 (cons (concat "\\." (car x) "\\'") (cdr x)))))
9726 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
9728 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
9729 (defun org-file-remote-p (file)
9730 "Test whether FILE specifies a location on a remote system.
9731 Return non-nil if the location is indeed remote.
9733 For example, the filename \"/user@host:/foo\" specifies a location
9734 on the system \"/user@host:\"."
9735 (cond ((fboundp 'file-remote-p)
9736 (file-remote-p file))
9737 ((fboundp 'tramp-handle-file-remote-p)
9738 (tramp-handle-file-remote-p file))
9739 ((and (boundp 'ange-ftp-name-format)
9740 (string-match (car ange-ftp-name-format) file))
9747 (defun org-get-org-file ()
9748 "Read a filename, with default directory `org-directory'."
9749 (let ((default (or org-default-notes-file remember-data-file)))
9750 (read-file-name (format "File name [%s]: " default)
9751 (file-name-as-directory org-directory)
9754 (defun org-notes-order-reversed-p ()
9755 "Check if the current file should receive notes in reversed order."
9757 ((not org-reverse-note-order) nil)
9758 ((eq t org-reverse-note-order) t)
9759 ((not (listp org-reverse-note-order)) nil)
9761 (let ((all org-reverse-note-order)
9763 (while (setq entry (pop all))
9764 (if (string-match (car entry) buffer-file-name)
9765 (throw 'exit (cdr entry))))
9768 (defvar org-refile-target-table nil
9769 "The list of refile targets, created by `org-refile'.")
9771 (defvar org-agenda-new-buffers nil
9772 "Buffers created to visit agenda files.")
9774 (defvar org-refile-cache nil
9775 "Cache for refile targets.")
9778 (defvar org-refile-markers nil
9779 "All the markers used for caching refile locations.")
9781 (defun org-refile-marker (pos)
9782 "Get a new refile marker, but only if caching is in use."
9783 (if (not org-refile-use-cache)
9785 (let ((m (make-marker)))
9787 (push m org-refile-markers)
9790 (defun org-refile-cache-clear ()
9791 "Clear the refile cache and disable all the markers."
9792 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
9793 (setq org-refile-markers nil)
9794 (setq org-refile-cache nil)
9795 (message "Refile cache has been cleared"))
9797 (defun org-refile-cache-check-set (set)
9798 "Check if all the markers in the cache still have live buffers."
9801 (while (and set (setq marker (nth 3 (pop set))))
9802 ;; if org-refile-use-outline-path is 'file, marker may be nil
9803 (when (and marker (null (marker-buffer marker)))
9804 (message "not found") (sit-for 3)
9808 (defun org-refile-cache-put (set &rest identifiers)
9809 "Push the refile targets SET into the cache, under IDENTIFIERS."
9810 (let* ((key (sha1 (prin1-to-string identifiers)))
9811 (entry (assoc key org-refile-cache)))
9814 (push (cons key set) org-refile-cache))))
9816 (defun org-refile-cache-get (&rest identifiers)
9817 "Retrieve the cached value for refile targets given by IDENTIFIERS."
9819 ((not org-refile-cache) nil)
9820 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
9822 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
9823 org-refile-cache))))
9824 (and set (org-refile-cache-check-set set) set)))))
9826 (defun org-get-refile-targets (&optional default-buffer)
9827 "Produce a table with refile targets."
9828 (let ((case-fold-search nil)
9829 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
9830 (entries (or org-refile-targets '((nil . (:level . 1)))))
9831 targets tgs txt re files f desc descre fast-path-p level pos0)
9832 (message "Getting targets...")
9833 (with-current-buffer (or default-buffer (current-buffer))
9834 (while (setq entry (pop entries))
9835 (setq files (car entry) desc (cdr entry))
9836 (setq fast-path-p nil)
9838 ((null files) (setq files (list (current-buffer))))
9839 ((eq files 'org-agenda-files)
9840 (setq files (org-agenda-files 'unrestricted)))
9841 ((and (symbolp files) (fboundp files))
9842 (setq files (funcall files)))
9843 ((and (symbolp files) (boundp files))
9844 (setq files (symbol-value files))))
9845 (if (stringp files) (setq files (list files)))
9847 ((eq (car desc) :tag)
9848 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
9849 ((eq (car desc) :todo)
9850 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
9851 ((eq (car desc) :regexp)
9852 (setq descre (cdr desc)))
9853 ((eq (car desc) :level)
9854 (setq descre (concat "^\\*\\{" (number-to-string
9855 (if org-odd-levels-only
9856 (1- (* 2 (cdr desc)))
9859 ((eq (car desc) :maxlevel)
9860 (setq fast-path-p t)
9861 (setq descre (concat "^\\*\\{1," (number-to-string
9862 (if org-odd-levels-only
9863 (1- (* 2 (cdr desc)))
9866 (t (error "Bad refiling target description %s" desc)))
9867 (while (setq f (pop files))
9868 (with-current-buffer
9869 (if (bufferp f) f (org-get-agenda-file-buffer f))
9871 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
9873 (if (bufferp f) (setq f (buffer-file-name
9874 (buffer-base-buffer f))))
9875 (setq f (and f (expand-file-name f)))
9876 (if (eq org-refile-use-outline-path 'file)
9877 (push (list (file-name-nondirectory f) f nil nil) tgs))
9881 (goto-char (point-min))
9882 (while (re-search-forward descre nil t)
9883 (goto-char (setq pos0 (point-at-bol)))
9885 (when org-refile-target-verify-function
9887 (or (funcall org-refile-target-verify-function)
9889 (when (looking-at org-complex-heading-regexp)
9890 (setq level (org-reduced-level
9891 (- (match-end 1) (match-beginning 1)))
9892 txt (org-link-display-format (match-string 4))
9893 re (format org-complex-heading-regexp-format
9894 (regexp-quote (match-string 4))))
9895 (when org-refile-use-outline-path
9896 (setq txt (mapconcat
9899 (if (eq org-refile-use-outline-path
9901 (list (file-name-nondirectory
9903 (buffer-base-buffer))))
9904 (if (eq org-refile-use-outline-path
9906 (list (buffer-file-name
9907 (buffer-base-buffer)))))
9908 (org-get-outline-path fast-path-p
9912 (push (list txt f re (org-refile-marker (point)))
9914 (when (= (point) pos0)
9915 ;; verification function has not moved point
9916 (goto-char (point-at-eol))))))))
9917 (when org-refile-use-cache
9918 (org-refile-cache-put tgs (buffer-file-name) descre))
9919 (setq targets (append tgs targets))
9921 (message "Getting targets...done")
9922 (nreverse targets)))
9924 (defun org-protect-slash (s)
9925 (while (string-match "/" s)
9926 (setq s (replace-match "\\" t t s)))
9929 (defvar org-olpa (make-vector 20 nil))
9931 (defun org-get-outline-path (&optional fastp level heading)
9932 "Return the outline path to the current entry, as a list.
9934 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
9935 routine which makes outline path derivations for an entire file,
9936 avoiding backtracing. Refile target collection makes use of that."
9940 (error "Outline path failure, more than 19 levels"))
9941 (loop for i from level upto 19 do
9942 (aset org-olpa i nil))
9944 (delq nil (append org-olpa nil))
9945 (aset org-olpa level heading)))
9946 (let (rtn case-fold-search)
9950 (while (org-up-heading-safe)
9951 (when (looking-at org-complex-heading-regexp)
9952 (push (org-match-string-no-properties 4) rtn)))
9955 (defun org-format-outline-path (path &optional width prefix)
9956 "Format the outline path PATH for display.
9957 Width is the maximum number of characters that is available.
9958 Prefix is a prefix to be included in the returned string,
9959 such as the file name."
9960 (setq width (or width 79))
9961 (if prefix (setq width (- width (length prefix))))
9964 (let* ((nsteps (length path))
9965 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
9966 (maxwidth (if (<= total-width width)
9967 10000 ;; everything fits
9968 ;; we need to shorten the level headings
9969 (/ (- width nsteps) nsteps)))
9970 (org-odd-levels-only nil)
9972 (total (1+ (length prefix))))
9973 (setq maxwidth (max maxwidth 10))
9978 (if (and (= n nsteps) (< maxwidth 10000))
9979 (setq maxwidth (- total-width total)))
9980 (if (< (length h) maxwidth)
9981 (progn (setq total (+ total (length h) 1)) h)
9982 (setq h (substring h 0 (- maxwidth 2))
9983 total (+ total maxwidth 1))
9984 (if (string-match "[ \t]+\\'" h)
9985 (setq h (substring h 0 (match-beginning 0))))
9986 (setq h (concat h "..")))
9987 (org-add-props h nil 'face
9988 (nth (% (1- n) org-n-level-faces)
9993 (defun org-display-outline-path (&optional file current)
9994 "Display the current outline path in the echo area."
9996 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
9997 (case-fold-search nil)
9998 (path (and (org-mode-p) (org-get-outline-path))))
9999 (if current (setq path (append path
10001 (org-back-to-heading t)
10002 (if (looking-at org-complex-heading-regexp)
10003 (list (match-string 4)))))))
10005 (org-format-outline-path
10008 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
10010 (defvar org-refile-history nil
10011 "History for refiling operations.")
10013 (defvar org-after-refile-insert-hook nil
10014 "Hook run after `org-refile' has inserted its stuff at the new location.
10015 Note that this is still *before* the stuff will be removed from
10016 the *old* location.")
10018 (defvar org-capture-last-stored-marker)
10019 (defun org-refile (&optional goto default-buffer rfloc)
10020 "Move the entry at point to another heading.
10021 The list of target headings is compiled using the information in
10022 `org-refile-targets', which see. This list is created before each use
10023 and will therefore always be up-to-date.
10025 At the target location, the entry is filed as a subitem of the target heading.
10026 Depending on `org-reverse-note-order', the new subitem will either be the
10027 first or the last subitem.
10029 If there is an active region, all entries in that region will be moved.
10030 However, the region must fulfill the requirement that the first heading
10031 is the first one sets the top-level of the moved text - at most siblings
10032 below it are allowed.
10034 With prefix arg GOTO, the command will only visit the target location,
10035 not actually move anything.
10036 With a double prefix arg \\[universal-argument] \\[universal-argument], \
10037 go to the location where the last refiling
10038 operation has put the subtree.
10039 With a prefix argument of `2', refile to the running clock.
10041 RFLOC can be a refile location obtained in a different way.
10043 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
10045 If you are using target caching (see `org-refile-use-cache'),
10046 You have to clear the target cache in order to find new targets.
10047 This can be done with a 0 prefix: `C-0 C-c C-w'"
10049 (if (member goto '(0 (64)))
10050 (org-refile-cache-clear)
10051 (let* ((cbuf (current-buffer))
10052 (regionp (org-region-active-p))
10053 (region-start (and regionp (region-beginning)))
10054 (region-end (and regionp (region-end)))
10055 (region-length (and regionp (- region-end region-start)))
10056 (filename (buffer-file-name (buffer-base-buffer cbuf)))
10057 pos it nbuf file re level reversed)
10058 (setq last-command nil)
10060 (goto-char region-start)
10061 (or (bolp) (goto-char (point-at-bol)))
10062 (setq region-start (point))
10063 (unless (org-kill-is-subtree-p
10064 (buffer-substring region-start region-end))
10065 (error "The region is not a (sequence of) subtree(s)")))
10066 (if (equal goto '(16))
10067 (org-refile-goto-last-stored)
10069 (and (equal goto 2)
10070 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
10072 (setq it (list (or org-clock-heading "running clock")
10074 (marker-buffer org-clock-hd-marker))
10076 (marker-position org-clock-hd-marker)))
10080 (org-refile-get-location
10081 (if goto "Goto: " "Refile to: ") default-buffer
10082 org-refile-allow-creating-parent-nodes)))))
10083 (setq file (nth 1 it)
10086 (if (and (not goto)
10088 (equal (buffer-file-name) file)
10090 (and (>= pos region-start)
10091 (<= pos region-end))
10092 (and (>= pos (point))
10093 (< pos (save-excursion
10094 (org-end-of-subtree t t))))))
10095 (error "Cannot refile to position inside the tree or region"))
10097 (setq nbuf (or (find-buffer-visiting file)
10098 (find-file-noselect file)))
10101 (switch-to-buffer nbuf)
10103 (org-show-context 'org-goto))
10106 (org-kill-new (buffer-substring region-start region-end))
10107 (org-save-markers-in-region region-start region-end))
10108 (org-copy-subtree 1 nil t))
10109 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
10110 (find-file-noselect file)))
10111 (setq reversed (org-notes-order-reversed-p))
10118 (looking-at outline-regexp)
10119 (setq level (org-get-valid-level (funcall outline-level) 1))
10122 (or (outline-next-heading) (point-max))
10123 (or (save-excursion (org-get-next-sibling))
10124 (org-end-of-subtree t t)
10128 (goto-char (point-max))
10129 (goto-char (point-min))
10130 (or (outline-next-heading) (goto-char (point-max)))))
10131 (if (not (bolp)) (newline))
10132 (org-paste-subtree level)
10133 (when org-log-refile
10134 (org-add-log-setup 'refile nil nil 'findpos
10136 (unless (eq org-log-refile 'note)
10137 (save-excursion (org-add-log-note))))
10138 (and org-auto-align-tags (org-set-tags nil t))
10139 (bookmark-set "org-refile-last-stored")
10140 ;; If we are refiling for capture, make sure that the
10141 ;; last-capture pointers point here
10142 (when (org-bound-and-true-p org-refile-for-capture)
10143 (bookmark-set "org-capture-last-stored-marker")
10144 (move-marker org-capture-last-stored-marker (point)))
10145 (if (fboundp 'deactivate-mark) (deactivate-mark))
10146 (run-hooks 'org-after-refile-insert-hook))))
10148 (delete-region (point) (+ (point) region-length))
10150 (when (featurep 'org-inlinetask)
10151 (org-inlinetask-remove-END-maybe))
10152 (setq org-markers-to-move nil)
10153 (message "Refiled to \"%s\" in file %s" (car it) file)))))))
10155 (defun org-refile-goto-last-stored ()
10156 "Go to the location where the last refile was stored."
10158 (bookmark-jump "org-refile-last-stored")
10159 (message "This is the location of the last refile"))
10161 (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
10162 "Prompt the user for a refile location, using PROMPT."
10163 (let ((org-refile-targets org-refile-targets)
10164 (org-refile-use-outline-path org-refile-use-outline-path))
10165 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
10166 (unless org-refile-target-table
10167 (error "No refile targets"))
10168 (let* ((cbuf (current-buffer))
10169 (partial-completion-mode nil)
10170 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
10171 (cfunc (if (and org-refile-use-outline-path
10172 org-outline-path-complete-in-steps)
10173 'org-olpath-completing-read
10174 'org-icompleting-read))
10175 (extra (if org-refile-use-outline-path "/" ""))
10176 (filename (and cfn (expand-file-name cfn)))
10179 (if (and (not (member org-refile-use-outline-path
10180 '(file full-file-path)))
10181 (not (equal filename (nth 1 x))))
10182 (cons (concat (car x) extra " ("
10183 (file-name-nondirectory (nth 1 x)) ")")
10185 (cons (concat (car x) extra) (cdr x))))
10186 org-refile-target-table))
10187 (completion-ignore-case t)
10188 pa answ parent-target child parent old-hist)
10189 (setq old-hist org-refile-history)
10190 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
10191 nil 'org-refile-history))
10192 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
10193 (org-refile-check-position pa)
10196 (when (or (not org-refile-history)
10197 (not (eq old-hist org-refile-history))
10198 (not (equal (car pa) (car org-refile-history))))
10199 (setq org-refile-history
10200 (cons (car pa) (if (assoc (car org-refile-history) tbl)
10202 (cdr org-refile-history))))
10203 (if (equal (car org-refile-history) (nth 1 org-refile-history))
10204 (pop org-refile-history)))
10206 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
10208 (setq parent (match-string 1 answ)
10209 child (match-string 2 answ))
10210 (setq parent-target (or (assoc parent tbl)
10211 (assoc (concat parent "/") tbl)))
10212 (when (and parent-target
10213 (or (eq new-nodes t)
10214 (and (eq new-nodes 'confirm)
10215 (y-or-n-p (format "Create new node \"%s\"? "
10217 (org-refile-new-child parent-target child)))
10218 (error "Invalid target location")))))
10220 (defun org-refile-check-position (refile-pointer)
10221 "Check if the refile pointer matches the readline to which it points."
10222 (let* ((file (nth 1 refile-pointer))
10223 (re (nth 2 refile-pointer))
10224 (pos (nth 3 refile-pointer))
10226 (when (org-string-nw-p re)
10227 (setq buffer (if (markerp pos)
10228 (marker-buffer pos)
10229 (or (find-buffer-visiting file)
10230 (find-file-noselect file))))
10231 (with-current-buffer buffer
10236 (beginning-of-line 1)
10237 (unless (org-looking-at-p re)
10238 (error "Invalid refile position, please rebuild the cache"))))))))
10240 (defun org-refile-new-child (parent-target child)
10241 "Use refile target PARENT-TARGET to add new CHILD below it."
10242 (unless parent-target
10243 (error "Cannot find parent for new node"))
10244 (let ((file (nth 1 parent-target))
10245 (pos (nth 3 parent-target))
10247 (with-current-buffer (or (find-buffer-visiting file)
10248 (find-file-noselect file))
10254 (goto-char (point-max))
10255 (if (not (bolp)) (newline)))
10256 (when (looking-at outline-regexp)
10257 (setq level (funcall outline-level))
10258 (org-end-of-subtree t t))
10259 (org-back-over-empty-lines)
10260 (insert "\n" (make-string
10261 (if pos (org-get-valid-level level 1) 1) ?*)
10263 (beginning-of-line 0)
10264 (list (concat (car parent-target) "/" child) file "" (point)))))))
10266 (defun org-olpath-completing-read (prompt collection &rest args)
10267 "Read an outline path like a file name."
10268 (let ((thetable collection)
10269 (org-completion-use-ido nil) ; does not work with ido.
10270 (org-completion-use-iswitchb nil)) ; or iswitchb
10272 'org-icompleting-read prompt
10273 (lambda (string predicate &optional flag)
10274 (let (rtn r f (l (length string)))
10278 (try-completion string thetable))
10281 (setq rtn (all-completions string thetable predicate))
10284 (setq r (substring x l))
10285 (if (string-match " ([^)]*)$" x)
10286 (setq f (match-string 0 x))
10288 (if (string-match "/" r)
10289 (concat string (substring r 0 (match-end 0)) f)
10294 (assoc string thetable)))
10298 ;;;; Dynamic blocks
10300 (defun org-find-dblock (name)
10301 "Find the first dynamic block with name NAME in the buffer.
10302 If not found, stay at current position and return nil."
10305 (goto-char (point-min))
10306 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
10308 (match-beginning 0))))
10309 (if pos (goto-char pos))
10312 (defconst org-dblock-start-re
10313 "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
10314 "Matches the start line of a dynamic block, with parameters.")
10316 (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
10317 "Matches the end of a dynamic block.")
10319 (defun org-create-dblock (plist)
10320 "Create a dynamic block section, with parameters taken from PLIST.
10321 PLIST must contain a :name entry which is used as name of the block."
10322 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
10325 (let ((col (current-column))
10326 (name (plist-get plist :name)))
10327 (insert "#+BEGIN: " name)
10329 (if (eq (car plist) :name)
10330 (setq plist (cddr plist))
10331 (insert " " (prin1-to-string (pop plist)))))
10332 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
10333 (beginning-of-line -2)))
10335 (defun org-prepare-dblock ()
10336 "Prepare dynamic block for refresh.
10337 This empties the block, puts the cursor at the insert position and returns
10338 the property list including an extra property :name with the block name."
10339 (unless (looking-at org-dblock-start-re)
10340 (error "Not at a dynamic block"))
10341 (let* ((begdel (1+ (match-end 0)))
10342 (name (org-no-properties (match-string 1)))
10343 (params (append (list :name name)
10344 (read (concat "(" (match-string 3) ")")))))
10346 (beginning-of-line 1)
10347 (skip-chars-forward " \t")
10348 (setq params (plist-put params :indentation-column (current-column))))
10349 (unless (re-search-forward org-dblock-end-re nil t)
10350 (error "Dynamic block not terminated"))
10353 (list :content (buffer-substring
10354 begdel (match-beginning 0)))))
10355 (delete-region begdel (match-beginning 0))
10360 (defun org-map-dblocks (&optional command)
10361 "Apply COMMAND to all dynamic blocks in the current buffer.
10362 If COMMAND is not given, use `org-update-dblock'."
10363 (let ((cmd (or command 'org-update-dblock)))
10365 (goto-char (point-min))
10366 (while (re-search-forward org-dblock-start-re nil t)
10367 (goto-char (match-beginning 0))
10369 (condition-case nil
10371 (error (message "Error during update of dynamic block"))))
10372 (unless (re-search-forward org-dblock-end-re nil t)
10373 (error "Dynamic block not terminated"))))))
10375 (defun org-dblock-update (&optional arg)
10376 "User command for updating dynamic blocks.
10377 Update the dynamic block at point. With prefix ARG, update all dynamic
10378 blocks in the buffer."
10381 (org-update-all-dblocks)
10382 (or (looking-at org-dblock-start-re)
10383 (org-beginning-of-dblock))
10384 (org-update-dblock)))
10386 (defun org-update-dblock ()
10387 "Update the dynamic block at point.
10388 This means to empty the block, parse for parameters and then call
10389 the correct writing function."
10390 (save-window-excursion
10391 (let* ((pos (point))
10392 (line (org-current-line))
10393 (params (org-prepare-dblock))
10394 (name (plist-get params :name))
10395 (indent (plist-get params :indentation-column))
10396 (cmd (intern (concat "org-dblock-write:" name))))
10397 (message "Updating dynamic block `%s' at line %d..." name line)
10398 (funcall cmd params)
10399 (message "Updating dynamic block `%s' at line %d...done" name line)
10401 (when (and indent (> indent 0))
10402 (setq indent (make-string indent ?\ ))
10404 (org-beginning-of-dblock)
10406 (while (not (looking-at org-dblock-end-re))
10408 (beginning-of-line 2))
10409 (when (looking-at org-dblock-end-re)
10410 (and (looking-at "[ \t]+")
10411 (replace-match ""))
10412 (insert indent)))))))
10414 (defun org-beginning-of-dblock ()
10415 "Find the beginning of the dynamic block at point.
10416 Error if there is no such block at point."
10417 (let ((pos (point))
10420 (if (and (re-search-backward org-dblock-start-re nil t)
10421 (setq beg (match-beginning 0))
10422 (re-search-forward org-dblock-end-re nil t)
10423 (> (match-end 0) pos))
10426 (error "Not in a dynamic block"))))
10428 (defun org-update-all-dblocks ()
10429 "Update all dynamic blocks in the buffer.
10430 This function can be used in a hook."
10432 (org-map-dblocks 'org-update-dblock)))
10437 (defconst org-additional-option-like-keywords
10438 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
10439 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
10440 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
10441 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
10443 "ORGTBL" "TBLFM:" "TBLNAME:"
10444 "BEGIN_EXAMPLE" "END_EXAMPLE"
10445 "BEGIN_QUOTE" "END_QUOTE"
10446 "BEGIN_VERSE" "END_VERSE"
10447 "BEGIN_CENTER" "END_CENTER"
10448 "BEGIN_SRC" "END_SRC"
10449 "CATEGORY" "COLUMNS"
10455 (defcustom org-structure-template-alist
10457 ("s" "#+begin_src ?\n\n#+end_src"
10458 "<src lang=\"?\">\n\n</src>")
10459 ("e" "#+begin_example\n?\n#+end_example"
10460 "<example>\n?\n</example>")
10461 ("q" "#+begin_quote\n?\n#+end_quote"
10462 "<quote>\n?\n</quote>")
10463 ("v" "#+begin_verse\n?\n#+end_verse"
10464 "<verse>\n?\n/verse>")
10465 ("c" "#+begin_center\n?\n#+end_center"
10466 "<center>\n?\n/center>")
10467 ("l" "#+begin_latex\n?\n#+end_latex"
10468 "<literal style=\"latex\">\n?\n</literal>")
10470 "<literal style=\"latex\">?</literal>")
10471 ("h" "#+begin_html\n?\n#+end_html"
10472 "<literal style=\"html\">\n?\n</literal>")
10474 "<literal style=\"html\">?</literal>")
10475 ("a" "#+begin_ascii\n?\n#+end_ascii")
10477 ("i" "#+include %file ?"
10478 "<include file=%file markup=\"?\">")
10480 "Structure completion elements.
10481 This is a list of abbreviation keys and values. The value gets inserted
10482 if you type `<' followed by the key and then press the completion key,
10483 usually `M-TAB'. %file will be replaced by a file name after prompting
10484 for the file using completion.
10485 There are two templates for each key, the first uses the original Org syntax,
10486 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
10487 the default when the /org-mtags.el/ module has been loaded. See also the
10488 variable `org-mtags-prefer-muse-templates'.
10489 This is an experimental feature, it is undecided if it is going to stay in."
10490 :group 'org-completion
10492 (string :tag "Key")
10493 (string :tag "Template")
10494 (string :tag "Muse Template")))
10496 (defun org-try-structure-completion ()
10497 "Try to complete a structure template before point.
10498 This looks for strings like \"<e\" on an otherwise empty line and
10500 (let ((l (buffer-substring (point-at-bol) (point)))
10502 (when (and (looking-at "[ \t]*$")
10503 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
10504 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
10505 (org-complete-expand-structure-template (+ -1 (point-at-bol)
10506 (match-beginning 1)) a)
10509 (defun org-complete-expand-structure-template (start cell)
10510 "Expand a structure template."
10511 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
10512 (rpl (nth (if musep 2 1) cell))
10514 (delete-region start (point))
10515 (when (string-match "\\`#\\+" rpl)
10518 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
10519 (setq ind (buffer-substring (point-at-bol) (point))))
10521 (setq start (point))
10522 (if (string-match "%file" rpl)
10523 (setq rpl (replace-match
10527 (abbreviate-file-name (read-file-name "Include file: ")))
10530 (setq rpl (mapconcat 'identity (split-string rpl "\n")
10531 (concat "\n" ind)))
10533 (if (re-search-backward "\\?" start t) (delete-char 1))))
10536 (defun org-complete (&optional arg)
10537 "Perform completion on word at point.
10538 At the beginning of a headline, this completes TODO keywords as given in
10539 `org-todo-keywords'.
10540 If the current word is preceded by a backslash, completes the TeX symbols
10541 that are supported for HTML support.
10542 If the current word is preceded by \"#+\", completes special words for
10543 setting file options.
10544 In the line after \"#+STARTUP:, complete valid keywords.\"
10545 At all other locations, this simply calls the value of
10546 `org-completion-fallback-command'."
10548 (org-without-partial-completion
10552 (beg1 (save-excursion
10553 (skip-chars-backward (org-re "[:alnum:]_@#%"))
10555 (beg (save-excursion
10556 (skip-chars-backward "a-zA-Z0-9_:$")
10558 (confirm (lambda (x) (stringp (car x))))
10559 (searchhead (equal (char-before beg) ?*))
10561 (when (and (member (char-before beg1) '(?. ?<))
10562 (setq a (assoc (buffer-substring beg1 (point))
10563 org-structure-template-alist)))
10564 (org-complete-expand-structure-template (1- beg1) a)
10566 (tag (and (equal (char-before beg1) ?:)
10567 (equal (char-after (point-at-bol)) ?*)))
10568 (prop (and (equal (char-before beg1) ?:)
10569 (not (equal (char-after (point-at-bol)) ?*))))
10570 (texp (equal (char-before beg) ?\\))
10571 (link (equal (char-before beg) ?\[))
10572 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
10575 (startup (string-match "^#\\+STARTUP:.*"
10576 (buffer-substring (point-at-bol) (point))))
10577 (completion-ignore-case opt)
10589 "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
10590 (cons (match-string 2 x)
10591 (match-string 1 x))))
10592 (org-split-string (org-get-current-options) "\n")))
10593 (mapcar 'list org-additional-option-like-keywords)))
10595 (setq type :startup)
10596 org-startup-options)
10597 (link (append org-link-abbrev-alist-local
10598 org-link-abbrev-alist))
10601 (append org-entities-user org-entities))
10602 ((string-match "\\`\\*+[ \t]+\\'"
10603 (buffer-substring (point-at-bol) beg))
10605 (mapcar 'list org-todo-keywords-1))
10607 (setq type :searchhead)
10609 (goto-char (point-min))
10610 (while (re-search-forward org-todo-line-regexp nil t)
10612 (org-make-org-heading-search-string
10613 (match-string 3) t))
10616 (tag (setq type :tag beg beg1)
10617 (or org-tag-alist (org-get-buffer-tags)))
10618 (prop (setq type :prop beg beg1)
10619 (mapcar 'list (org-buffer-property-keys nil t t)))
10621 (call-interactively org-completion-fallback-command)
10622 (throw 'exit nil)))))
10623 (pattern (buffer-substring-no-properties beg end))
10624 (completion (try-completion pattern table confirm)))
10625 (cond ((eq completion t)
10626 (if (not (assoc (upcase pattern) table))
10627 (message "Already complete")
10628 (if (and (equal type :opt)
10629 (not (member (car (assoc (upcase pattern) table))
10630 org-additional-option-like-keywords)))
10631 (insert (substring (cdr (assoc (upcase pattern) table))
10633 (if (memq type '(:tag :prop)) (insert ":")))))
10635 (message "Can't find completion for \"%s\"" pattern)
10637 ((not (string= pattern completion))
10638 (delete-region beg end)
10639 (if (string-match " +$" completion)
10640 (setq completion (replace-match "" t t completion)))
10641 (insert completion)
10642 (if (get-buffer-window "*Completions*")
10643 (delete-window (get-buffer-window "*Completions*")))
10644 (if (assoc completion table)
10645 (if (eq type :todo) (insert " ")
10646 (if (memq type '(:tag :prop)) (insert ":"))))
10647 (if (and (equal type :opt) (assoc completion table))
10648 (message "%s" (substitute-command-keys
10649 "Press \\[org-complete] again to insert example settings"))))
10651 (message "Making completion list...")
10652 (let ((list (sort (all-completions pattern table confirm)
10654 (with-output-to-temp-buffer "*Completions*"
10655 (condition-case nil
10656 ;; Protection needed for XEmacs and emacs 21
10657 (display-completion-list list pattern)
10658 (error (display-completion-list list)))))
10659 (message "Making completion list...%s" "done")))))))
10661 ;;;; TODO, DEADLINE, Comments
10663 (defun org-toggle-comment ()
10664 "Change the COMMENT state of an entry."
10667 (org-back-to-heading)
10668 (let (case-fold-search)
10669 (if (looking-at (concat outline-regexp
10670 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
10671 (replace-match "" t t nil 1)
10672 (if (looking-at outline-regexp)
10674 (goto-char (match-end 0))
10675 (insert org-comment-string " ")))))))
10677 (defvar org-last-todo-state-is-todo nil
10678 "This is non-nil when the last TODO state change led to a TODO state.
10679 If the last change removed the TODO tag or switched to DONE, then
10682 (defvar org-setting-tags nil) ; dynamically skipped
10684 (defun org-parse-local-options (string var)
10685 "Parse STRING for startup setting relevant for variable VAR."
10686 (let ((rtn (symbol-value var))
10689 (if (or (not string) (not (string-match "\\S-" string)))
10691 (setq opts (delq nil (mapcar (lambda (x)
10692 (setq e (assoc x org-startup-options))
10693 (if (eq (nth 1 e) var) e nil))
10694 (org-split-string string "[ \t]+"))))
10698 (while (setq e (pop opts))
10699 (if (not (nth 3 e))
10700 (setq rtn (nth 2 e))
10701 (if (not (listp rtn)) (setq rtn nil))
10702 (push (nth 2 e) rtn)))
10705 (defvar org-todo-setup-filter-hook nil
10706 "Hook for functions that pre-filter todo specs.
10707 Each function takes a todo spec and returns either nil or the spec
10708 transformed into canonical form." )
10710 (defvar org-todo-get-default-hook nil
10711 "Hook for functions that get a default item for todo.
10712 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
10713 nil or a string to be used for the todo mark." )
10715 (defvar org-agenda-headline-snapshot-before-repeat)
10717 (defun org-todo (&optional arg)
10718 "Change the TODO state of an item.
10719 The state of an item is given by a keyword at the start of the heading,
10721 *** TODO Write paper
10724 The different keywords are specified in the variable `org-todo-keywords'.
10725 By default the available states are \"TODO\" and \"DONE\".
10726 So for this example: when the item starts with TODO, it is changed to DONE.
10727 When it starts with DONE, the DONE is removed. And when neither TODO nor
10728 DONE are present, add TODO at the beginning of the heading.
10730 With \\[universal-argument] prefix arg, use completion to determine the new \
10732 With numeric prefix arg, switch to that state.
10733 With a double \\[universal-argument] prefix, switch to the next set of TODO \
10734 keywords (nextset).
10735 With a triple \\[universal-argument] prefix, circumvent any state blocking.
10737 For calling through lisp, arg is also interpreted in the following way:
10738 'none -> empty state
10739 \"\"(empty string) -> switch to empty state
10740 'done -> switch to DONE
10741 'nextset -> switch to the next set of keywords
10742 'previousset -> switch to the previous set of keywords
10743 \"WAITING\" -> switch to the specified keyword, but only if it
10744 really is a member of `org-todo-keywords'."
10746 (if (equal arg '(16)) (setq arg 'nextset))
10747 (let ((org-blocker-hook org-blocker-hook)
10748 (case-fold-search nil))
10749 (when (equal arg '(64))
10750 (setq arg nil org-blocker-hook nil))
10751 (when (and org-blocker-hook
10752 (or org-inhibit-blocking
10753 (org-entry-get nil "NOBLOCKING")))
10754 (setq org-blocker-hook nil))
10757 (org-back-to-heading t)
10758 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
10759 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
10761 (let* ((match-data (match-data))
10762 (startpos (point-at-bol))
10763 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
10764 (org-log-done org-log-done)
10765 (org-log-repeat org-log-repeat)
10766 (org-todo-log-states org-todo-log-states)
10767 (this (match-string 1))
10768 (hl-pos (match-beginning 0))
10769 (head (org-get-todo-sequence-head this))
10770 (ass (assoc head org-todo-kwd-alist))
10771 (interpret (nth 1 ass))
10772 (done-word (nth 3 ass))
10773 (final-done-word (nth 4 ass))
10774 (last-state (or this ""))
10775 (completion-ignore-case t)
10776 (member (member this org-todo-keywords-1))
10777 (tail (cdr member))
10779 ((and org-todo-key-trigger
10780 (or (and (equal arg '(4))
10781 (eq org-use-fast-todo-selection 'prefix))
10782 (and (not arg) org-use-fast-todo-selection
10783 (not (eq org-use-fast-todo-selection
10785 ;; Use fast selection
10786 (org-fast-todo-selection))
10787 ((and (equal arg '(4))
10788 (or (not org-use-fast-todo-selection)
10789 (not org-todo-key-trigger)))
10790 ;; Read a state with completion
10791 (org-icompleting-read
10792 "State: " (mapcar (lambda(x) (list x))
10793 org-todo-keywords-1)
10797 (if tail (car tail) nil)
10798 (car org-todo-keywords-1)))
10800 (if (equal member org-todo-keywords-1)
10803 (nth (- (length org-todo-keywords-1)
10805 org-todo-keywords-1)
10806 (org-last org-todo-keywords-1))))
10807 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
10808 (setq arg nil))) ; hack to fall back to cycling
10810 ;; user or caller requests a specific state
10812 ((equal arg "") nil)
10813 ((eq arg 'none) nil)
10814 ((eq arg 'done) (or done-word (car org-done-keywords)))
10816 (or (car (cdr (member head org-todo-heads)))
10817 (car org-todo-heads)))
10818 ((eq arg 'previousset)
10819 (let ((org-todo-heads (reverse org-todo-heads)))
10820 (or (car (cdr (member head org-todo-heads)))
10821 (car org-todo-heads))))
10822 ((car (member arg org-todo-keywords-1)))
10824 (error "State `%s' not valid in this file" arg))
10825 ((nth (1- (prefix-numeric-value arg))
10826 org-todo-keywords-1))))
10827 ((null member) (or head (car org-todo-keywords-1)))
10828 ((equal this final-done-word) nil) ;; -> make empty
10829 ((null tail) nil) ;; -> first entry
10830 ((memq interpret '(type priority))
10831 (if (eq this-command last-command)
10833 (if (> (length tail) 0)
10834 (or done-word (car org-done-keywords))
10839 (run-hook-with-args-until-success
10840 'org-todo-get-default-hook state last-state)
10842 (next (if state (concat " " state " ") " "))
10843 (change-plist (list :type 'todo-state-change :from this :to state
10844 :position startpos))
10846 (when org-blocker-hook
10847 (setq org-last-todo-state-is-todo
10848 (not (member this org-done-keywords)))
10849 (unless (save-excursion
10851 (run-hook-with-args-until-failure
10852 'org-blocker-hook change-plist)))
10853 (if (interactive-p)
10854 (error "TODO state change from %s to %s blocked" this state)
10856 (message "TODO state change from %s to %s blocked" this state)
10857 (throw 'exit nil))))
10858 (store-match-data match-data)
10859 (replace-match next t t)
10860 (unless (pos-visible-in-window-p hl-pos)
10861 (message "TODO state changed to %s" (org-trim next)))
10863 (setq head (org-get-todo-sequence-head state)
10864 ass (assoc head org-todo-kwd-alist)
10865 interpret (nth 1 ass)
10866 done-word (nth 3 ass)
10867 final-done-word (nth 4 ass)))
10868 (when (memq arg '(nextset previousset))
10869 (message "Keyword-Set %d/%d: %s"
10870 (- (length org-todo-sets) -1
10871 (length (memq (assoc state org-todo-sets) org-todo-sets)))
10872 (length org-todo-sets)
10873 (mapconcat 'identity (assoc state org-todo-sets) " ")))
10874 (setq org-last-todo-state-is-todo
10875 (not (member state org-done-keywords)))
10876 (setq now-done-p (and (member state org-done-keywords)
10877 (not (member this org-done-keywords))))
10878 (and logging (org-local-logging logging))
10879 (when (and (or org-todo-log-states org-log-done)
10880 (not (eq org-inhibit-logging t))
10881 (not (memq arg '(nextset previousset))))
10882 ;; we need to look at recording a time and note
10883 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
10884 (nth 2 (assoc this org-todo-log-states))))
10885 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
10886 (setq dolog 'time))
10888 (member state org-not-done-keywords)
10889 (not (member this org-not-done-keywords)))
10890 ;; This is now a todo state and was not one before
10891 ;; If there was a CLOSED time stamp, get rid of it.
10892 (org-add-planning-info nil nil 'closed))
10893 (when (and now-done-p org-log-done)
10894 ;; It is now done, and it was not done before
10895 (org-add-planning-info 'closed (org-current-time))
10896 (if (and (not dolog) (eq 'note org-log-done))
10897 (org-add-log-setup 'done state this 'findpos 'note)))
10898 (when (and state dolog)
10899 ;; This is a non-nil state, and we need to log it
10900 (org-add-log-setup 'state state this 'findpos dolog)))
10901 ;; Fixup tag positioning
10902 (org-todo-trigger-tag-changes state)
10903 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
10904 (when org-provide-todo-statistics
10905 (org-update-parent-todo-statistics))
10906 (run-hooks 'org-after-todo-state-change-hook)
10907 (if (and arg (not (member state org-done-keywords)))
10908 (setq head (org-get-todo-sequence-head state)))
10909 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
10910 ;; Do we need to trigger a repeat?
10912 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
10913 ;; This is for the agenda, take a snapshot of the headline.
10915 (setq org-agenda-headline-snapshot-before-repeat
10916 (org-get-heading))))
10917 (org-auto-repeat-maybe state))
10918 ;; Fixup cursor location if close to the keyword
10919 (if (and (outline-on-heading-p)
10921 (save-excursion (beginning-of-line 1)
10922 (looking-at org-todo-line-regexp))
10923 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
10925 (goto-char (or (match-end 2) (match-end 1)))
10926 (and (looking-at " ") (just-one-space))))
10927 (when org-trigger-hook
10929 (run-hook-with-args 'org-trigger-hook change-plist))))))))
10931 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
10932 "Block turning an entry into a TODO, using the hierarchy.
10933 This checks whether the current task should be blocked from state
10934 changes. Such blocking occurs when:
10936 1. The task has children which are not all in a completed state.
10938 2. A task has a parent with the property :ORDERED:, and there
10939 are siblings prior to the current task with incomplete
10942 3. The parent of the task is blocked because it has siblings that should
10943 be done first, or is child of a block grandparent TODO entry."
10945 (if (not org-enforce-todo-dependencies)
10946 t ; if locally turned off don't block
10948 ;; If this is not a todo state change, or if this entry is already DONE,
10950 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
10951 (member (plist-get change-plist :from)
10952 (cons 'done org-done-keywords))
10953 (member (plist-get change-plist :to)
10954 (cons 'todo org-not-done-keywords))
10955 (not (plist-get change-plist :to)))
10956 (throw 'dont-block t))
10957 ;; If this task has children, and any are undone, it's blocked
10959 (org-back-to-heading t)
10960 (let ((this-level (funcall outline-level)))
10961 (outline-next-heading)
10962 (let ((child-level (funcall outline-level)))
10963 (while (and (not (eobp))
10964 (> child-level this-level))
10965 ;; this todo has children, check whether they are all
10967 (if (and (not (org-entry-is-done-p))
10968 (org-entry-is-todo-p))
10969 (throw 'dont-block nil))
10970 (outline-next-heading)
10971 (setq child-level (funcall outline-level))))))
10972 ;; Otherwise, if the task's parent has the :ORDERED: property, and
10973 ;; any previous siblings are undone, it's blocked
10975 (org-back-to-heading t)
10976 (let* ((pos (point))
10977 (parent-pos (and (org-up-heading-safe) (point))))
10978 (if (not parent-pos) (throw 'dont-block t)) ; no parent
10979 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
10981 (re-search-forward org-not-done-heading-regexp pos t))
10982 (throw 'dont-block nil)) ; block, there is an older sibling not done.
10983 ;; Search further up the hierarchy, to see if an anchestor is blocked
10985 (goto-char parent-pos)
10986 (if (not (looking-at org-not-done-heading-regexp))
10987 (throw 'dont-block t)) ; do not block, parent is not a TODO
10989 (setq parent-pos (and (org-up-heading-safe) (point)))
10990 (if (not parent-pos) (throw 'dont-block t)) ; no parent
10991 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
10993 (re-search-forward org-not-done-heading-regexp pos t))
10994 (throw 'dont-block nil)))))))) ; block, older sibling not done.
10996 (defcustom org-track-ordered-property-with-tag nil
10997 "Should the ORDERED property also be shown as a tag?
10998 The ORDERED property decides if an entry should require subtasks to be
10999 completed in sequence. Since a property is not very visible, setting
11000 this option means that toggling the ORDERED property with the command
11001 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
11002 not relevant for the behavior, but it makes things more visible.
11004 Note that toggling the tag with tags commands will not change the property
11005 and therefore not influence behavior!
11007 This can be t, meaning the tag ORDERED should be used, It can also be a
11008 string to select a different tag for this task."
11011 (const :tag "No tracking" nil)
11012 (const :tag "Track with ORDERED tag" t)
11013 (string :tag "Use other tag")))
11015 (defun org-toggle-ordered-property ()
11016 "Toggle the ORDERED property of the current entry.
11017 For better visibility, you can track the value of this property with a tag.
11018 See variable `org-track-ordered-property-with-tag'."
11020 (let* ((t1 org-track-ordered-property-with-tag)
11021 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
11023 (org-back-to-heading)
11024 (if (org-entry-get nil "ORDERED")
11026 (org-delete-property "ORDERED")
11027 (and tag (org-toggle-tag tag 'off))
11028 (message "Subtasks can be completed in arbitrary order"))
11029 (org-entry-put nil "ORDERED" "t")
11030 (and tag (org-toggle-tag tag 'on))
11031 (message "Subtasks must be completed in sequence")))))
11033 (defvar org-blocked-by-checkboxes) ; dynamically scoped
11034 (defun org-block-todo-from-checkboxes (change-plist)
11035 "Block turning an entry into a TODO, using checkboxes.
11036 This checks whether the current task should be blocked from state
11037 changes because there are unchecked boxes in this entry."
11038 (if (not org-enforce-todo-checkbox-dependencies)
11039 t ; if locally turned off don't block
11041 ;; If this is not a todo state change, or if this entry is already DONE,
11043 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11044 (member (plist-get change-plist :from)
11045 (cons 'done org-done-keywords))
11046 (member (plist-get change-plist :to)
11047 (cons 'todo org-not-done-keywords))
11048 (not (plist-get change-plist :to)))
11049 (throw 'dont-block t))
11050 ;; If this task has checkboxes that are not checked, it's blocked
11052 (org-back-to-heading t)
11053 (let ((beg (point)) end)
11054 (outline-next-heading)
11057 (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
11060 (if (boundp 'org-blocked-by-checkboxes)
11061 (setq org-blocked-by-checkboxes t))
11062 (throw 'dont-block nil)))))
11063 t))) ; do not block
11065 (defun org-entry-blocked-p ()
11066 "Is the current entry blocked?"
11067 (if (org-entry-get nil "NOBLOCKING")
11068 nil ;; Never block this entry
11070 (run-hook-with-args-until-failure
11072 (list :type 'todo-state-change
11077 (defun org-update-statistics-cookies (all)
11078 "Update the statistics cookie, either from TODO or from checkboxes.
11079 This should be called with the cursor in a line with a statistics cookie."
11083 (org-update-checkbox-count 'all)
11084 (org-map-entries 'org-update-parent-todo-statistics))
11085 (if (not (org-on-heading-p))
11086 (org-update-checkbox-count)
11087 (let ((pos (move-marker (make-marker) (point)))
11089 (ignore-errors (org-back-to-heading t))
11090 (if (not (org-on-heading-p))
11091 (org-update-checkbox-count)
11092 (setq l1 (org-outline-level))
11093 (setq end (save-excursion
11094 (outline-next-heading)
11095 (if (org-on-heading-p) (setq l2 (org-outline-level)))
11097 (if (and (save-excursion
11099 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
11100 (not (save-excursion (re-search-forward
11101 ":COOKIE_DATA:.*\\<todo\\>" end t))))
11102 (org-update-checkbox-count)
11103 (if (and l2 (> l2 l1))
11106 (org-update-parent-todo-statistics))
11108 (beginning-of-line 1)
11109 (while (re-search-forward
11110 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
11112 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
11114 (move-marker pos nil)))))
11116 (defvar org-entry-property-inherited-from) ;; defined below
11117 (defun org-update-parent-todo-statistics ()
11118 "Update any statistics cookie in the parent of the current headline.
11119 When `org-hierarchical-todo-statistics' is nil, statistics will cover
11120 the entire subtree and this will travel up the hierarchy and update
11121 statistics everywhere."
11123 (let* ((lim 0) prop
11124 (recursive (or (not org-hierarchical-todo-statistics)
11127 (or (setq prop (org-entry-get
11128 nil "COOKIE_DATA" 'inherit)) ""))))
11129 (lim (or (and prop (marker-position
11130 org-entry-property-inherited-from))
11133 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
11134 level ltoggle l1 new ndel
11135 (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
11138 (beginning-of-line 1)
11139 (if (org-at-heading-p)
11140 (setq ltoggle (funcall outline-level))
11141 (error "This should not happen"))
11142 (while (and (setq level (org-up-heading-safe))
11143 (or recursive first)
11145 (setq first nil cookie-present nil)
11154 (while (re-search-forward box-re (point-at-eol) t)
11155 (setq cnt-all 0 cnt-done 0 cookie-present t)
11156 (setq is-percent (match-end 2))
11158 (unless (outline-next-heading) (throw 'exit nil))
11159 (while (and (looking-at org-complex-heading-regexp)
11160 (> (setq l1 (length (match-string 1))) level))
11161 (setq kwd (and (or recursive (= l1 ltoggle))
11163 (if (or (eq org-provide-todo-statistics 'all-headlines)
11164 (and (listp org-provide-todo-statistics)
11165 (or (member kwd org-provide-todo-statistics)
11166 (member kwd org-done-keywords))))
11167 (setq cnt-all (1+ cnt-all))
11168 (if (eq org-provide-todo-statistics t)
11169 (and kwd (setq cnt-all (1+ cnt-all)))))
11170 (and (member kwd org-done-keywords)
11171 (setq cnt-done (1+ cnt-done)))
11172 (outline-next-heading)))
11175 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
11176 (format "[%d/%d]" cnt-done cnt-all))
11177 ndel (- (match-end 0) (match-beginning 0)))
11178 (goto-char (match-beginning 0))
11180 (delete-region (point) (+ (point) ndel)))
11181 (when cookie-present
11182 (run-hook-with-args 'org-after-todo-statistics-hook
11183 cnt-done (- cnt-all cnt-done))))))
11184 (run-hooks 'org-todo-statistics-hook)))
11186 (defvar org-after-todo-statistics-hook nil
11187 "Hook that is called after a TODO statistics cookie has been updated.
11188 Each function is called with two arguments: the number of not-done entries
11189 and the number of done entries.
11191 For example, the following function, when added to this hook, will switch
11192 an entry to DONE when all children are done, and back to TODO when new
11193 entries are set to a TODO status. Note that this hook is only called
11194 when there is a statistics cookie in the headline!
11196 (defun org-summary-todo (n-done n-not-done)
11197 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
11198 (let (org-log-done org-log-states) ; turn off logging
11199 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
11202 (defvar org-todo-statistics-hook nil
11203 "Hook that is run whenever Org thinks TODO statistics should be updated.
11204 This hook runs even if there is no statistics cookie present, in which case
11205 `org-after-todo-statistics-hook' would not run.")
11207 (defun org-todo-trigger-tag-changes (state)
11208 "Apply the changes defined in `org-todo-state-tags-triggers'."
11209 (let ((l org-todo-state-tags-triggers)
11211 (when (or (not state) (equal state ""))
11212 (setq changes (append changes (cdr (assoc "" l)))))
11213 (when (and (stringp state) (> (length state) 0))
11214 (setq changes (append changes (cdr (assoc state l)))))
11215 (when (member state org-not-done-keywords)
11216 (setq changes (append changes (cdr (assoc 'todo l)))))
11217 (when (member state org-done-keywords)
11218 (setq changes (append changes (cdr (assoc 'done l)))))
11219 (dolist (c changes)
11220 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
11222 (defun org-local-logging (value)
11223 "Get logging settings from a property VALUE."
11225 ;; directly set the variables, they are already local.
11226 (setq org-log-done nil
11228 org-todo-log-states nil)
11229 (setq words (org-split-string value))
11230 (while (setq w (pop words))
11232 ((setq a (assoc w org-startup-options))
11233 (and (member (nth 1 a) '(org-log-done org-log-repeat))
11234 (set (nth 1 a) (nth 2 a))))
11235 ((setq a (org-extract-log-state-settings w))
11236 (and (member (car a) org-todo-keywords-1)
11237 (push a org-todo-log-states)))))))
11239 (defun org-get-todo-sequence-head (kwd)
11240 "Return the head of the TODO sequence to which KWD belongs.
11241 If KWD is not set, check if there is a text property remembering the
11246 (or (get-text-property (point-at-bol) 'org-todo-head)
11248 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
11249 nil (point-at-eol)))
11250 (get-text-property p 'org-todo-head))))
11251 ((not (member kwd org-todo-keywords-1))
11252 (car org-todo-keywords-1))
11253 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
11255 (defun org-fast-todo-selection ()
11256 "Fast TODO keyword selection with single keys.
11257 Returns the new TODO keyword, or nil if no state change should occur."
11258 (let* ((fulltable org-todo-key-alist)
11259 (done-keywords org-done-keywords) ;; needed for the faces.
11260 (maxlen (apply 'max (mapcar
11262 (if (stringp (car x)) (string-width (car x)) 0))
11265 (fwidth (+ maxlen 3 1 3))
11266 (ncol (/ (- (window-width) 4) fwidth))
11270 (save-window-excursion
11272 (set-buffer (get-buffer-create " *Org todo*"))
11273 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
11275 (org-set-local 'org-done-keywords done-keywords)
11276 (setq tbl fulltable cnt 0)
11277 (while (setq e (pop tbl))
11279 ((equal e '(:startgroup))
11280 (push '() groups) (setq ingroup t)
11281 (when (not (= cnt 0))
11285 ((equal e '(:endgroup))
11286 (setq ingroup nil cnt 0)
11288 ((equal e '(:newline))
11289 (when (not (= cnt 0))
11293 (while (equal (car tbl) '(:newline))
11295 (setq tbl (cdr tbl)))))
11297 (setq tg (car e) c (cdr e))
11298 (if ingroup (push tg (car groups)))
11299 (setq tg (org-add-props tg nil 'face
11300 (org-get-todo-face tg)))
11301 (if (and (= cnt 0) (not ingroup)) (insert " "))
11302 (insert "[" c "] " tg (make-string
11303 (- fwidth 4 (length tg)) ?\ ))
11304 (when (= (setq cnt (1+ cnt)) ncol)
11306 (if ingroup (insert " "))
11309 (goto-char (point-min))
11310 (if (not expert) (org-fit-window-to-buffer))
11311 (message "[a-z..]:Set [SPC]:clear")
11312 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
11315 (and (= c ?q) (not (rassoc c fulltable))))
11316 (setq quit-flag t))
11318 ((setq e (rassoc c fulltable) tg (car e))
11320 (t (setq quit-flag t)))))))
11322 (defun org-entry-is-todo-p ()
11323 (member (org-get-todo-state) org-not-done-keywords))
11325 (defun org-entry-is-done-p ()
11326 (member (org-get-todo-state) org-done-keywords))
11328 (defun org-get-todo-state ()
11330 (org-back-to-heading t)
11331 (and (looking-at org-todo-line-regexp)
11333 (match-string 2))))
11335 (defun org-at-date-range-p (&optional inactive-ok)
11336 "Is the cursor inside a date range?"
11340 (let ((pos (point)))
11341 (skip-chars-backward "^[<\r\n")
11342 (skip-chars-backward "<[")
11343 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
11344 (>= (match-end 0) pos)
11346 (skip-chars-backward "^<[\r\n")
11347 (skip-chars-backward "<[")
11348 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
11349 (>= (match-end 0) pos)
11353 (defun org-get-repeat (&optional tagline)
11354 "Check if there is a deadline/schedule with repeater in this entry."
11357 (org-back-to-heading t)
11358 (and (re-search-forward (if tagline
11359 (concat tagline "\\s-*" org-repeat-re)
11361 (org-entry-end-position) t)
11362 (match-string-no-properties 1)))))
11364 (defvar org-last-changed-timestamp)
11365 (defvar org-last-inserted-timestamp)
11366 (defvar org-log-post-message)
11367 (defvar org-log-note-purpose)
11368 (defvar org-log-note-how)
11369 (defvar org-log-note-extra)
11370 (defun org-auto-repeat-maybe (done-word)
11371 "Check if the current headline contains a repeated deadline/schedule.
11372 If yes, set TODO state back to what it was and change the base date
11373 of repeating deadline/scheduled time stamps to new date.
11374 This function is run automatically after each state change to a DONE state."
11375 ;; last-state is dynamically scoped into this function
11376 (let* ((repeat (org-get-repeat))
11377 (aa (assoc last-state org-todo-kwd-alist))
11378 (interpret (nth 1 aa))
11380 (whata '(("d" . day) ("m" . month) ("y" . year)))
11381 (msg "Entry repeats: ")
11383 (org-todo-log-states nil)
11384 (nshiftmax 10) (nshift 0)
11385 re type n what ts time to-state)
11387 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
11388 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
11389 org-todo-repeat-to-state))
11390 (unless (and to-state (member to-state org-todo-keywords-1))
11391 (setq to-state (if (eq interpret 'type) last-state head)))
11392 (org-todo to-state)
11393 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
11394 (org-entry-put nil "LAST_REPEAT" (format-time-string
11395 (org-time-stamp-format t t))))
11396 (when org-log-repeat
11397 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
11398 (memq 'org-add-log-note post-command-hook))
11399 ;; OK, we are already setup for some record
11400 (if (eq org-log-repeat 'note)
11401 ;; make sure we take a note, not only a time stamp
11402 (setq org-log-note-how 'note))
11403 ;; Set up for taking a record
11404 (org-add-log-setup 'state (or done-word (car org-done-keywords))
11406 'findpos org-log-repeat)))
11407 (org-back-to-heading t)
11408 (org-add-planning-info nil nil 'closed)
11409 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
11410 org-deadline-time-regexp "\\)\\|\\("
11411 org-ts-regexp "\\)"))
11412 (while (re-search-forward
11413 re (save-excursion (outline-next-heading) (point)) t)
11414 (setq type (if (match-end 1) org-scheduled-string
11415 (if (match-end 3) org-deadline-string "Plain:"))
11416 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
11417 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
11418 (setq n (string-to-number (match-string 2 ts))
11419 what (match-string 3 ts))
11420 (if (equal what "w") (setq n (* n 7) what "d"))
11421 ;; Preparation, see if we need to modify the start date for the change
11422 (when (match-end 1)
11423 (setq time (save-match-data (org-time-string-to-time ts)))
11425 ((equal (match-string 1 ts) ".")
11426 ;; Shift starting date to today
11427 (org-timestamp-change
11428 (- (time-to-days (current-time)) (time-to-days time))
11430 ((equal (match-string 1 ts) "+")
11431 (while (or (= nshift 0)
11432 (<= (time-to-days time) (time-to-days (current-time))))
11433 (when (= (incf nshift) nshiftmax)
11434 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
11436 (org-timestamp-change n (cdr (assoc what whata)))
11437 (org-at-timestamp-p t)
11438 (setq ts (match-string 1))
11439 (setq time (save-match-data (org-time-string-to-time ts))))
11440 (org-timestamp-change (- n) (cdr (assoc what whata)))
11441 ;; rematch, so that we have everything in place for the real shift
11442 (org-at-timestamp-p t)
11443 (setq ts (match-string 1))
11444 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
11445 (org-timestamp-change n (cdr (assoc what whata)))
11446 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
11447 (setq org-log-post-message msg)
11448 (message "%s" msg))))
11450 (defun org-show-todo-tree (arg)
11451 "Make a compact tree which shows all headlines marked with TODO.
11452 The tree will show the lines where the regexp matches, and all higher
11453 headlines above the match.
11454 With a \\[universal-argument] prefix, prompt for a regexp to match.
11455 With a numeric prefix N, construct a sparse tree for the Nth element
11456 of `org-todo-keywords-1'."
11458 (let ((case-fold-search nil)
11460 (cond ((null arg) org-not-done-regexp)
11462 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
11463 (mapcar 'list org-todo-keywords-1))))
11465 (mapconcat 'identity (org-split-string kwd "|") "\\|")
11467 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
11468 (regexp-quote (nth (1- (prefix-numeric-value arg))
11469 org-todo-keywords-1)))
11470 (t (error "Invalid prefix argument: %s" arg)))))
11471 (message "%d TODO entries found"
11472 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
11474 (defun org-deadline (&optional remove time)
11475 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
11476 With argument REMOVE, remove any deadline from the item.
11477 When TIME is set, it should be an internal time specification, and the
11478 scheduling will use the corresponding date."
11480 (let* ((old-date (org-entry-get nil "DEADLINE"))
11481 (repeater (and old-date
11482 (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
11483 (match-string 1 old-date))))
11486 (when (and old-date org-log-redeadline)
11487 (org-add-log-setup 'deldeadline nil old-date 'findpos
11488 org-log-redeadline))
11489 (org-remove-timestamp-with-keyword org-deadline-string)
11490 (message "Item no longer has a deadline."))
11491 (org-add-planning-info 'deadline time 'closed)
11492 (when (and old-date org-log-redeadline
11493 (not (equal old-date
11494 (substring org-last-inserted-timestamp 1 -1))))
11495 (org-add-log-setup 'redeadline nil old-date 'findpos
11496 org-log-redeadline))
11499 (org-back-to-heading t)
11500 (when (re-search-forward (concat org-deadline-string " "
11501 org-last-inserted-timestamp)
11503 (outline-next-heading) (point)) t)
11504 (goto-char (1- (match-end 0)))
11505 (insert " " repeater)
11506 (setq org-last-inserted-timestamp
11507 (concat (substring org-last-inserted-timestamp 0 -1)
11509 (substring org-last-inserted-timestamp -1))))))
11510 (message "Deadline on %s" org-last-inserted-timestamp))))
11512 (defun org-schedule (&optional remove time)
11513 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
11514 With argument REMOVE, remove any scheduling date from the item.
11515 When TIME is set, it should be an internal time specification, and the
11516 scheduling will use the corresponding date."
11518 (let* ((old-date (org-entry-get nil "SCHEDULED"))
11519 (repeater (and old-date
11520 (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
11521 (match-string 1 old-date))))
11524 (when (and old-date org-log-reschedule)
11525 (org-add-log-setup 'delschedule nil old-date 'findpos
11526 org-log-reschedule))
11527 (org-remove-timestamp-with-keyword org-scheduled-string)
11528 (message "Item is no longer scheduled."))
11529 (org-add-planning-info 'scheduled time 'closed)
11530 (when (and old-date org-log-reschedule
11531 (not (equal old-date
11532 (substring org-last-inserted-timestamp 1 -1))))
11533 (org-add-log-setup 'reschedule nil old-date 'findpos
11534 org-log-reschedule))
11537 (org-back-to-heading t)
11538 (when (re-search-forward (concat org-scheduled-string " "
11539 org-last-inserted-timestamp)
11541 (outline-next-heading) (point)) t)
11542 (goto-char (1- (match-end 0)))
11543 (insert " " repeater)
11544 (setq org-last-inserted-timestamp
11545 (concat (substring org-last-inserted-timestamp 0 -1)
11547 (substring org-last-inserted-timestamp -1))))))
11548 (message "Scheduled to %s" org-last-inserted-timestamp))))
11550 (defun org-get-scheduled-time (pom &optional inherit)
11551 "Get the scheduled time as a time tuple, of a format suitable
11552 for calling org-schedule with, or if there is no scheduling,
11554 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
11556 (apply 'encode-time (org-parse-time-string time)))))
11558 (defun org-get-deadline-time (pom &optional inherit)
11559 "Get the deadline as a time tuple, of a format suitable for
11560 calling org-deadline with, or if there is no scheduling, returns
11562 (let ((time (org-entry-get pom "DEADLINE" inherit)))
11564 (apply 'encode-time (org-parse-time-string time)))))
11566 (defun org-remove-timestamp-with-keyword (keyword)
11567 "Remove all time stamps with KEYWORD in the current entry."
11568 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
11571 (org-back-to-heading t)
11573 (outline-next-heading)
11574 (while (re-search-backward re beg t)
11576 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
11577 (equal (char-before) ?\ ))
11578 (backward-delete-char 1)
11579 (if (string-match "^[ \t]*$" (buffer-substring
11580 (point-at-bol) (point-at-eol)))
11581 (delete-region (point-at-bol)
11582 (min (point-max) (1+ (point-at-eol))))))))))
11584 (defun org-add-planning-info (what &optional time &rest remove)
11585 "Insert new timestamp with keyword in the line directly after the headline.
11586 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
11587 If non is given, the user is prompted for a date.
11588 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
11591 (let (org-time-was-given org-end-time-was-given ts
11592 end default-time default-input)
11595 (when (and (not time) (memq what '(scheduled deadline)))
11596 ;; Try to get a default date/time from existing timestamp
11598 (org-back-to-heading t)
11599 (setq end (save-excursion (outline-next-heading) (point)))
11600 (when (re-search-forward (if (eq what 'scheduled)
11601 org-scheduled-time-regexp
11602 org-deadline-time-regexp)
11604 (setq ts (match-string 1)
11606 (apply 'encode-time (org-parse-time-string ts))
11607 default-input (and ts (org-get-compact-tod ts))))))
11609 ;; If necessary, get the time from the user
11610 (setq time (or time (org-read-date nil 'to-time nil nil
11611 default-time default-input))))
11613 (when (and org-insert-labeled-timestamps-at-point
11614 (member what '(scheduled deadline)))
11616 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
11617 (org-insert-time-stamp time org-time-was-given
11618 nil nil nil (list org-end-time-was-given))
11622 (let (col list elt ts buffer-invisibility-spec)
11623 (org-back-to-heading t)
11624 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
11625 (goto-char (match-end 1))
11626 (setq col (current-column))
11627 (goto-char (match-end 0))
11628 (if (eobp) (insert "\n") (forward-char 1))
11629 (when (and (not what)
11632 org-keyword-time-not-clock-regexp))))
11633 ;; Nothing to add, nothing to remove...... :-)
11635 (if (and (not (looking-at outline-regexp))
11636 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
11638 (not (equal (match-string 1) org-clock-string)))
11639 (narrow-to-region (match-beginning 0) (match-end 0))
11640 (insert-before-markers "\n")
11642 (narrow-to-region (point) (point))
11643 (and org-adapt-indentation (org-indent-to-column col)))
11644 ;; Check if we have to remove something.
11645 (setq list (cons what remove))
11647 (setq elt (pop list))
11648 (goto-char (point-min))
11649 (when (or (and (eq elt 'scheduled)
11650 (re-search-forward org-scheduled-time-regexp nil t))
11651 (and (eq elt 'deadline)
11652 (re-search-forward org-deadline-time-regexp nil t))
11653 (and (eq elt 'closed)
11654 (re-search-forward org-closed-time-regexp nil t)))
11656 (if (looking-at "--+<[^>]+>") (replace-match ""))
11657 (skip-chars-backward " ")
11658 (if (looking-at " +") (replace-match ""))))
11659 (goto-char (point-max))
11660 (and org-adapt-indentation (bolp) (org-indent-to-column col))
11663 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
11664 (cond ((eq what 'scheduled) org-scheduled-string)
11665 ((eq what 'deadline) org-deadline-string)
11666 ((eq what 'closed) org-closed-string))
11668 (setq ts (org-insert-time-stamp
11670 (or org-time-was-given
11671 (and (eq what 'closed) org-log-done-with-time))
11673 nil nil (list org-end-time-was-given)))
11675 (goto-char (point-min))
11677 (if (and (looking-at "[ \t]*\n")
11678 (equal (char-before) ?\n))
11679 (delete-region (1- (point)) (point-at-eol)))
11682 (defvar org-log-note-marker (make-marker))
11683 (defvar org-log-note-purpose nil)
11684 (defvar org-log-note-state nil)
11685 (defvar org-log-note-previous-state nil)
11686 (defvar org-log-note-how nil)
11687 (defvar org-log-note-extra nil)
11688 (defvar org-log-note-window-configuration nil)
11689 (defvar org-log-note-return-to (make-marker))
11690 (defvar org-log-post-message nil
11691 "Message to be displayed after a log note has been stored.
11692 The auto-repeater uses this.")
11694 (defun org-add-note ()
11695 "Add a note to the current entry.
11696 This is done in the same way as adding a state change note."
11698 (org-add-log-setup 'note nil nil 'findpos nil))
11700 (defvar org-property-end-re)
11701 (defun org-add-log-setup (&optional purpose state prev-state
11703 "Set up the post command hook to take a note.
11704 If this is about to TODO state change, the new state is expected in STATE.
11705 When FINDPOS is non-nil, find the correct position for the note in
11706 the current entry. If not, assume that it can be inserted at point.
11707 HOW is an indicator what kind of note should be created.
11708 EXTRA is additional text that will be inserted into the notes buffer."
11709 (let* ((org-log-into-drawer (org-log-into-drawer))
11710 (drawer (cond ((stringp org-log-into-drawer)
11711 org-log-into-drawer)
11712 (org-log-into-drawer "LOGBOOK")
11717 (org-back-to-heading t)
11718 (narrow-to-region (point) (save-excursion
11719 (outline-next-heading) (point)))
11720 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
11721 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
11723 (goto-char (match-end 0))
11726 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
11729 (goto-char (match-end 0))
11730 (or org-log-states-order-reversed
11731 (and (re-search-forward org-property-end-re nil t)
11732 (goto-char (1- (match-beginning 0))))))
11733 (insert "\n:" drawer ":\n:END:")
11734 (beginning-of-line 0)
11735 (org-indent-line-function)
11736 (beginning-of-line 2)
11737 (org-indent-line-function)
11739 ((and org-log-state-notes-insert-after-drawers
11741 (forward-line) (looking-at org-drawer-regexp)))
11743 (while (looking-at org-drawer-regexp)
11744 (goto-char (match-end 0))
11745 (re-search-forward org-property-end-re (point-max) t)
11747 (forward-line -1)))
11748 (unless org-log-states-order-reversed
11749 (and (= (char-after) ?\n) (forward-char 1))
11750 (org-skip-over-state-notes)
11751 (skip-chars-backward " \t\n\r")))
11752 (move-marker org-log-note-marker (point))
11753 (setq org-log-note-purpose purpose
11754 org-log-note-state state
11755 org-log-note-previous-state prev-state
11756 org-log-note-how how
11757 org-log-note-extra extra)
11758 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
11760 (defun org-skip-over-state-notes ()
11761 "Skip past the list of State notes in an entry."
11762 (if (looking-at "\n[ \t]*- State") (forward-char 1))
11763 (while (looking-at "[ \t]*- State")
11764 (condition-case nil
11766 (error (org-end-of-item)))))
11768 (defun org-add-log-note (&optional purpose)
11769 "Pop up a window for taking a note, and add this note later at point."
11770 (remove-hook 'post-command-hook 'org-add-log-note)
11771 (setq org-log-note-window-configuration (current-window-configuration))
11772 (delete-other-windows)
11773 (move-marker org-log-note-return-to (point))
11774 (switch-to-buffer (marker-buffer org-log-note-marker))
11775 (goto-char org-log-note-marker)
11776 (org-switch-to-buffer-other-window "*Org Note*")
11778 (if (memq org-log-note-how '(time state))
11779 (let (current-prefix-arg) (org-store-log-note))
11780 (let ((org-inhibit-startup t)) (org-mode))
11781 (insert (format "# Insert note for %s.
11782 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
11784 ((eq org-log-note-purpose 'clock-out) "stopped clock")
11785 ((eq org-log-note-purpose 'done) "closed todo item")
11786 ((eq org-log-note-purpose 'state)
11787 (format "state change from \"%s\" to \"%s\""
11788 (or org-log-note-previous-state "")
11789 (or org-log-note-state "")))
11790 ((eq org-log-note-purpose 'reschedule)
11792 ((eq org-log-note-purpose 'delschedule)
11793 "no longer scheduled")
11794 ((eq org-log-note-purpose 'redeadline)
11795 "changing deadline")
11796 ((eq org-log-note-purpose 'deldeadline)
11797 "removing deadline")
11798 ((eq org-log-note-purpose 'refile)
11800 ((eq org-log-note-purpose 'note)
11802 (t (error "This should not happen")))))
11803 (if org-log-note-extra (insert org-log-note-extra))
11804 (org-set-local 'org-finish-function 'org-store-log-note)))
11806 (defvar org-note-abort nil) ; dynamically scoped
11807 (defun org-store-log-note ()
11808 "Finish taking a log note, and insert it to where it belongs."
11809 (let ((txt (buffer-string))
11810 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
11812 (kill-buffer (current-buffer))
11813 (while (string-match "\\`#.*\n[ \t\n]*" txt)
11814 (setq txt (replace-match "" t t txt)))
11815 (if (string-match "\\s-+\\'" txt)
11816 (setq txt (replace-match "" t t txt)))
11817 (setq lines (org-split-string txt "\n"))
11818 (when (and note (string-match "\\S-" note))
11820 (org-replace-escapes
11822 (list (cons "%u" (user-login-name))
11823 (cons "%U" user-full-name)
11824 (cons "%t" (format-time-string
11825 (org-time-stamp-format 'long 'inactive)
11827 (cons "%T" (format-time-string
11828 (org-time-stamp-format 'long nil)
11830 (cons "%s" (if org-log-note-state
11831 (concat "\"" org-log-note-state "\"")
11833 (cons "%S" (if org-log-note-previous-state
11834 (concat "\"" org-log-note-previous-state "\"")
11836 (if lines (setq note (concat note " \\\\")))
11838 (when (or current-prefix-arg org-note-abort)
11839 (when org-log-into-drawer
11840 (org-remove-empty-drawer-at
11841 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
11842 org-log-note-marker))
11845 (with-current-buffer (marker-buffer org-log-note-marker)
11847 (goto-char org-log-note-marker)
11848 (move-marker org-log-note-marker nil)
11850 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
11851 (org-indent-line-function)
11852 (insert "- " (pop lines))
11853 (beginning-of-line 1)
11854 (looking-at "[ \t]*")
11855 (setq ind (concat (match-string 0) " "))
11857 (while lines (insert "\n" ind (pop lines)))
11858 (message "Note stored")
11859 (org-back-to-heading t)
11860 (org-cycle-hide-drawers 'children)))))
11861 (set-window-configuration org-log-note-window-configuration)
11862 (with-current-buffer (marker-buffer org-log-note-return-to)
11863 (goto-char org-log-note-return-to))
11864 (move-marker org-log-note-return-to nil)
11865 (and org-log-post-message (message "%s" org-log-post-message)))
11867 (defun org-remove-empty-drawer-at (drawer pos)
11868 "Remove an empty drawer DRAWER at position POS.
11869 POS may also be a marker."
11870 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
11876 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
11877 (replace-match ""))))))
11879 (defun org-sparse-tree (&optional arg)
11880 "Create a sparse tree, prompt for the details.
11881 This command can create sparse trees. You first need to select the type
11882 of match used to create the tree:
11884 t Show all TODO entries.
11885 T Show entries with a specific TODO keyword.
11886 m Show entries selected by a tags/property match.
11887 p Enter a property name and its value (both with completion on existing
11888 names/values) and show entries with that property.
11889 / Show entries matching a regular expression (`r' can be used as well)
11890 d Show deadlines due within `org-deadline-warning-days'.
11891 b Show deadlines and scheduled items before a date.
11892 a Show deadlines and scheduled items after a date."
11894 (let (ans kwd value)
11895 (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty [d]eadlines\n [b]efore-date [a]fter-date")
11896 (setq ans (read-char-exclusive))
11899 (call-interactively 'org-check-deadlines))
11901 (call-interactively 'org-check-before-date))
11903 (call-interactively 'org-check-after-date))
11905 (org-show-todo-tree nil))
11907 (org-show-todo-tree '(4)))
11908 ((member ans '(?T ?m))
11909 (call-interactively 'org-match-sparse-tree))
11910 ((member ans '(?p ?P))
11911 (setq kwd (org-icompleting-read "Property: "
11912 (mapcar 'list (org-buffer-property-keys))))
11913 (setq value (org-icompleting-read "Value: "
11914 (mapcar 'list (org-property-values kwd))))
11915 (unless (string-match "\\`{.*}\\'" value)
11916 (setq value (concat "\"" value "\"")))
11917 (org-match-sparse-tree arg (concat kwd "=" value)))
11918 ((member ans '(?r ?R ?/))
11919 (call-interactively 'org-occur))
11920 (t (error "No such sparse tree command \"%c\"" ans)))))
11922 (defvar org-occur-highlights nil
11923 "List of overlays used for occur matches.")
11924 (make-variable-buffer-local 'org-occur-highlights)
11925 (defvar org-occur-parameters nil
11926 "Parameters of the active org-occur calls.
11927 This is a list, each call to org-occur pushes as cons cell,
11928 containing the regular expression and the callback, onto the list.
11929 The list can contain several entries if `org-occur' has been called
11930 several time with the KEEP-PREVIOUS argument. Otherwise, this list
11931 will only contain one set of parameters. When the highlights are
11932 removed (for example with `C-c C-c', or with the next edit (depending
11933 on `org-remove-highlights-with-change'), this variable is emptied
11935 (make-variable-buffer-local 'org-occur-parameters)
11937 (defun org-occur (regexp &optional keep-previous callback)
11938 "Make a compact tree which shows all matches of REGEXP.
11939 The tree will show the lines where the regexp matches, and all higher
11940 headlines above the match. It will also show the heading after the match,
11941 to make sure editing the matching entry is easy.
11942 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
11943 call to `org-occur' will be kept, to allow stacking of calls to this
11945 If CALLBACK is non-nil, it is a function which is called to confirm
11946 that the match should indeed be shown."
11947 (interactive "sRegexp: \nP")
11948 (when (equal regexp "")
11949 (error "Regexp cannot be empty"))
11950 (unless keep-previous
11951 (org-remove-occur-highlights nil nil t))
11952 (push (cons regexp callback) org-occur-parameters)
11955 (goto-char (point-min))
11956 (if (or (not keep-previous) ; do not want to keep
11957 (not org-occur-highlights)) ; no previous matches
11960 (while (re-search-forward regexp nil t)
11961 (when (or (not callback)
11962 (save-match-data (funcall callback)))
11963 (setq cnt (1+ cnt))
11964 (when org-highlight-sparse-tree-matches
11965 (org-highlight-new-match (match-beginning 0) (match-end 0)))
11966 (org-show-context 'occur-tree))))
11967 (when org-remove-highlights-with-change
11968 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
11970 (unless org-sparse-tree-open-archived-trees
11971 (org-hide-archived-subtrees (point-min) (point-max)))
11972 (run-hooks 'org-occur-hook)
11973 (if (interactive-p)
11974 (message "%d match(es) for regexp %s" cnt regexp))
11977 (defun org-show-context (&optional key)
11978 "Make sure point and context are visible.
11979 How much context is shown depends upon the variables
11980 `org-show-hierarchy-above', `org-show-following-heading'. and
11981 `org-show-siblings'."
11982 (let ((heading-p (org-on-heading-p t))
11983 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
11984 (following-p (org-get-alist-option org-show-following-heading key))
11985 (entry-p (org-get-alist-option org-show-entry-below key))
11986 (siblings-p (org-get-alist-option org-show-siblings key)))
11988 ;; Show heading or entry text
11989 (if (and heading-p (not entry-p))
11990 (org-flag-heading nil) ; only show the heading
11991 (and (or entry-p (org-invisible-p) (org-invisible-p2))
11992 (org-show-hidden-entry))) ; show entire entry
11994 ;; Show next sibling, or heading below text
11996 (and (if heading-p (org-goto-sibling) (outline-next-heading))
11997 (org-flag-heading nil))))
11998 (when siblings-p (org-show-siblings))
12000 ;; show all higher headings, possibly with siblings
12002 (while (and (condition-case nil
12003 (progn (org-up-heading-all 1) t)
12006 (org-flag-heading nil)
12007 (when siblings-p (org-show-siblings))))))))
12009 (defvar org-reveal-start-hook nil
12010 "Hook run before revealing a location.")
12012 (defun org-reveal (&optional siblings)
12013 "Show current entry, hierarchy above it, and the following headline.
12014 This can be used to show a consistent set of context around locations
12015 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
12016 not t for the search context.
12018 With optional argument SIBLINGS, on each level of the hierarchy all
12019 siblings are shown. This repairs the tree structure to what it would
12020 look like when opened with hierarchical calls to `org-cycle'.
12021 With double optional argument \\[universal-argument] \\[universal-argument], \
12022 go to the parent and show the
12025 (run-hooks 'org-reveal-start-hook)
12026 (let ((org-show-hierarchy-above t)
12027 (org-show-following-heading t)
12028 (org-show-siblings (if siblings t org-show-siblings)))
12029 (org-show-context nil))
12030 (when (equal siblings '(16))
12032 (when (org-up-heading-safe)
12034 (run-hook-with-args 'org-cycle-hook 'subtree)))))
12036 (defun org-highlight-new-match (beg end)
12037 "Highlight from BEG to END and mark the highlight is an occur headline."
12038 (let ((ov (make-overlay beg end)))
12039 (overlay-put ov 'face 'secondary-selection)
12040 (push ov org-occur-highlights)))
12042 (defun org-remove-occur-highlights (&optional beg end noremove)
12043 "Remove the occur highlights from the buffer.
12044 BEG and END are ignored. If NOREMOVE is nil, remove this function
12045 from the `before-change-functions' in the current buffer."
12047 (unless org-inhibit-highlight-removal
12048 (mapc 'delete-overlay org-occur-highlights)
12049 (setq org-occur-highlights nil)
12050 (setq org-occur-parameters nil)
12052 (remove-hook 'before-change-functions
12053 'org-remove-occur-highlights 'local))))
12057 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
12058 "Regular expression matching the priority indicator.")
12060 (defvar org-remove-priority-next-time nil)
12062 (defun org-priority-up ()
12063 "Increase the priority of the current item."
12065 (org-priority 'up))
12067 (defun org-priority-down ()
12068 "Decrease the priority of the current item."
12070 (org-priority 'down))
12072 (defun org-priority (&optional action)
12073 "Change the priority of an item by ARG.
12074 ACTION can be `set', `up', `down', or a character."
12076 (unless org-enable-priority-commands
12077 (error "Priority commands are disabled"))
12078 (setq action (or action 'set))
12079 (let (current new news have remove)
12081 (org-back-to-heading t)
12082 (if (looking-at org-priority-regexp)
12083 (setq current (string-to-char (match-string 2))
12085 (setq current org-default-priority))
12087 ((eq action 'remove)
12088 (setq remove t new ?\ ))
12089 ((or (eq action 'set)
12090 (if (featurep 'xemacs) (characterp action) (integerp action)))
12091 (if (not (eq action 'set))
12093 (message "Priority %c-%c, SPC to remove: "
12094 org-highest-priority org-lowest-priority)
12095 (setq new (read-char-exclusive)))
12096 (if (and (= (upcase org-highest-priority) org-highest-priority)
12097 (= (upcase org-lowest-priority) org-lowest-priority))
12098 (setq new (upcase new)))
12099 (cond ((equal new ?\ ) (setq remove t))
12100 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
12101 (error "Priority must be between `%c' and `%c'"
12102 org-highest-priority org-lowest-priority))))
12104 (if (and (not have) (eq last-command this-command))
12105 (setq new org-lowest-priority)
12106 (setq new (if (and org-priority-start-cycle-with-default (not have))
12107 org-default-priority (1- current)))))
12109 (if (and (not have) (eq last-command this-command))
12110 (setq new org-highest-priority)
12111 (setq new (if (and org-priority-start-cycle-with-default (not have))
12112 org-default-priority (1+ current)))))
12113 (t (error "Invalid action")))
12114 (if (or (< (upcase new) org-highest-priority)
12115 (> (upcase new) org-lowest-priority))
12117 (setq news (format "%c" new))
12120 (replace-match "" t t nil 1)
12121 (replace-match news t t nil 2))
12123 (error "No priority cookie found in line")
12124 (let ((case-fold-search nil))
12125 (looking-at org-todo-line-regexp))
12128 (goto-char (match-end 2))
12129 (insert " [#" news "]"))
12130 (goto-char (match-beginning 3))
12131 (insert "[#" news "] "))))
12132 (org-preserve-lc (org-set-tags nil 'align)))
12134 (message "Priority removed")
12135 (message "Priority of current item set to %s" news))))
12137 (defun org-get-priority (s)
12138 "Find priority cookie and return priority."
12140 (if (not (string-match org-priority-regexp s))
12141 (* 1000 (- org-lowest-priority org-default-priority))
12142 (* 1000 (- org-lowest-priority
12143 (string-to-char (match-string 2 s)))))))
12147 (defvar org-agenda-archives-mode)
12148 (defvar org-map-continue-from nil
12149 "Position from where mapping should continue.
12150 Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
12152 (defvar org-scanner-tags nil
12153 "The current tag list while the tags scanner is running.")
12154 (defvar org-trust-scanner-tags nil
12155 "Should `org-get-tags-at' use the tags fro the scanner.
12156 This is for internal dynamical scoping only.
12157 When this is non-nil, the function `org-get-tags-at' will return the value
12158 of `org-scanner-tags' instead of building the list by itself. This
12159 can lead to large speed-ups when the tags scanner is used in a file with
12160 many entries, and when the list of tags is retrieved, for example to
12161 obtain a list of properties. Building the tags list for each entry in such
12162 a file becomes an N^2 operation - but with this variable set, it scales
12165 (defun org-scan-tags (action matcher &optional todo-only)
12166 "Scan headline tags with inheritance and produce output ACTION.
12168 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
12169 or `agenda' to produce an entry list for an agenda view. It can also be
12170 a Lisp form or a function that should be called at each matched headline, in
12171 this case the return value is a list of all return values from these calls.
12173 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
12174 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
12175 only lines with a TODO keyword are included in the output."
12176 (require 'org-agenda)
12177 (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
12178 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
12180 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
12181 (props (list 'face 'default
12182 'done-face 'org-agenda-done
12183 'undone-face 'default
12184 'mouse-face 'highlight
12185 'org-not-done-regexp org-not-done-regexp
12186 'org-todo-regexp org-todo-regexp
12188 (format "mouse-2 or RET jump to org file %s"
12189 (abbreviate-file-name
12190 (or (buffer-file-name (buffer-base-buffer))
12191 (buffer-name (buffer-base-buffer)))))))
12192 (case-fold-search nil)
12193 (org-map-continue-from nil)
12194 lspos tags tags-list
12195 (tags-alist (list (cons 0 org-file-tags)))
12196 (llast 0) rtn rtn1 level category i txt
12197 todo marker entry priority)
12198 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
12199 (setq action (list 'lambda nil action)))
12201 (goto-char (point-min))
12202 (when (eq action 'sparse-tree)
12204 (org-remove-occur-highlights))
12205 (while (re-search-forward re nil t)
12207 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
12208 tags (if (match-end 4) (org-match-string-no-properties 4)))
12209 (goto-char (setq lspos (match-beginning 0)))
12210 (setq level (org-reduced-level (funcall outline-level))
12211 category (org-get-category))
12212 (setq i llast llast level)
12213 ;; remove tag lists from same and sublevels
12214 (while (>= i level)
12215 (when (setq entry (assoc i tags-alist))
12216 (setq tags-alist (delete entry tags-alist)))
12218 ;; add the next tags
12220 (setq tags (org-split-string tags ":")
12222 (cons (cons level tags) tags-alist)))
12223 ;; compile tags for current headline
12225 (if org-use-tag-inheritance
12226 (apply 'append (mapcar 'cdr (reverse tags-alist)))
12228 org-scanner-tags tags-list)
12229 (when org-use-tag-inheritance
12230 (setcdr (car tags-alist)
12231 (mapcar (lambda (x)
12232 (setq x (copy-sequence x))
12233 (org-add-prop-inherited x))
12234 (cdar tags-alist))))
12235 (when (and tags org-use-tag-inheritance
12236 (or (not (eq t org-use-tag-inheritance))
12237 org-tags-exclude-from-inheritance))
12238 ;; selective inheritance, remove uninherited ones
12239 (setcdr (car tags-alist)
12240 (org-remove-uniherited-tags (cdar tags-alist))))
12241 (when (and (or (not todo-only)
12242 (and (member todo org-not-done-keywords)
12243 (or (not org-agenda-tags-todo-honor-ignore-options)
12244 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
12245 (let ((case-fold-search t)) (eval matcher))
12247 (not (member org-archive-tag tags-list))
12248 ;; we have an archive tag, should we use this anyway?
12249 (or (not org-agenda-skip-archived-trees)
12250 (and (eq action 'agenda) org-agenda-archives-mode))))
12251 (unless (eq action 'sparse-tree) (org-agenda-skip))
12253 ;; select this headline
12256 ((eq action 'sparse-tree)
12257 (and org-highlight-sparse-tree-matches
12258 (org-get-heading) (match-end 0)
12259 (org-highlight-new-match
12260 (match-beginning 0) (match-beginning 1)))
12261 (org-show-context 'tags-tree))
12262 ((eq action 'agenda)
12263 (setq txt (org-format-agenda-item
12266 (if (eq org-tags-match-list-sublevels 'indented)
12267 (make-string (1- level) ?.) "")
12272 priority (org-get-priority txt))
12274 (setq marker (org-agenda-new-marker))
12275 (org-add-props txt props
12276 'org-marker marker 'org-hd-marker marker 'org-category category
12278 'priority priority 'type "tagsmatch")
12280 ((functionp action)
12281 (setq org-map-continue-from nil)
12283 (setq rtn1 (funcall action))
12285 (t (error "Invalid action")))
12287 ;; if we are to skip sublevels, jump to end of subtree
12288 (unless org-tags-match-list-sublevels
12289 (org-end-of-subtree t)
12290 (backward-char 1))))
12291 ;; Get the correct position from where to continue
12292 (if org-map-continue-from
12293 (goto-char org-map-continue-from)
12294 (and (= (point) lspos) (end-of-line 1)))))
12295 (when (and (eq action 'sparse-tree)
12296 (not org-sparse-tree-open-archived-trees))
12297 (org-hide-archived-subtrees (point-min) (point-max)))
12300 (defun org-remove-uniherited-tags (tags)
12301 "Remove all tags that are not inherited from the list TAGS."
12303 ((eq org-use-tag-inheritance t)
12304 (if org-tags-exclude-from-inheritance
12305 (org-delete-all org-tags-exclude-from-inheritance tags)
12307 ((not org-use-tag-inheritance) nil)
12308 ((stringp org-use-tag-inheritance)
12311 (if (and (string-match org-use-tag-inheritance x)
12312 (not (member x org-tags-exclude-from-inheritance)))
12315 ((listp org-use-tag-inheritance)
12318 (if (member x org-use-tag-inheritance) x nil))
12321 (defvar todo-only) ;; dynamically scoped
12323 (defun org-match-sparse-tree (&optional todo-only match)
12324 "Create a sparse tree according to tags string MATCH.
12325 MATCH can contain positive and negative selection of tags, like
12326 \"+WORK+URGENT-WITHBOSS\".
12327 If optional argument TODO-ONLY is non-nil, only select lines that are
12330 (org-prepare-agenda-buffers (list (current-buffer)))
12331 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
12333 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
12335 (defvar org-cached-props nil)
12336 (defun org-cached-entry-get (pom property)
12337 (if (or (eq t org-use-property-inheritance)
12338 (and (stringp org-use-property-inheritance)
12339 (string-match org-use-property-inheritance property))
12340 (and (listp org-use-property-inheritance)
12341 (member property org-use-property-inheritance)))
12342 ;; Caching is not possible, check it directly
12343 (org-entry-get pom property 'inherit)
12344 ;; Get all properties, so that we can do complicated checks easily
12345 (cdr (assoc property (or org-cached-props
12346 (setq org-cached-props
12347 (org-entry-properties pom)))))))
12349 (defun org-global-tags-completion-table (&optional files)
12350 "Return the list of all tags in all agenda buffer/files."
12357 (set-buffer (find-file-noselect file))
12358 (append (org-get-buffer-tags)
12359 (mapcar (lambda (x) (if (stringp (car-safe x))
12360 (list (car-safe x)) nil))
12362 (if (and files (car files))
12364 (org-agenda-files))))))))
12366 (defun org-make-tags-matcher (match)
12367 "Create the TAGS//TODO matcher form for the selection string MATCH."
12368 ;; todo-only is scoped dynamically into this function, and the function
12369 ;; may change it if the matcher asks for it.
12371 ;; Get a new match request, with completion
12372 (let ((org-last-tags-completion-table
12373 (org-global-tags-completion-table)))
12374 (setq match (org-completing-read-no-i
12375 "Match: " 'org-tags-completion-function nil nil nil
12376 'org-tags-history))))
12378 ;; Parse the string and create a lisp form
12379 (let ((match0 match)
12380 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
12382 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
12383 orterms term orlist re-p str-p level-p level-op time-p
12384 prop-p pn pv po cat-p gv rest)
12385 (if (string-match "/+" match)
12386 ;; match contains also a todo-matching request
12388 (setq tagsmatch (substring match 0 (match-beginning 0))
12389 todomatch (substring match (match-end 0)))
12390 (if (string-match "^!" todomatch)
12391 (setq todo-only t todomatch (substring todomatch 1)))
12392 (if (string-match "^\\s-*$" todomatch)
12393 (setq todomatch nil)))
12394 ;; only matching tags
12395 (setq tagsmatch match todomatch nil))
12397 ;; Make the tags matcher
12398 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
12399 (setq tagsmatcher t)
12400 (setq orterms (org-split-string tagsmatch "|") orlist nil)
12401 (while (setq term (pop orterms))
12402 (while (and (equal (substring term -1) "\\") orterms)
12403 (setq term (concat term "|" (pop orterms)))) ; repair bad split
12404 (while (string-match re term)
12405 (setq rest (substring term (match-end 0))
12406 minus (and (match-end 1)
12407 (equal (match-string 1 term) "-"))
12408 tag (save-match-data (replace-regexp-in-string
12410 (match-string 2 term)))
12411 re-p (equal (string-to-char tag) ?{)
12412 level-p (match-end 4)
12413 prop-p (match-end 5)
12415 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
12417 (setq level-op (org-op-to-function (match-string 3 term)))
12418 `(,level-op level ,(string-to-number
12419 (match-string 4 term))))
12421 (setq pn (match-string 5 term)
12422 po (match-string 6 term)
12423 pv (match-string 7 term)
12424 cat-p (equal pn "CATEGORY")
12425 re-p (equal (string-to-char pv) ?{)
12426 str-p (equal (string-to-char pv) ?\")
12427 time-p (save-match-data
12428 (string-match "^\"[[<].*[]>]\"$" pv))
12429 pv (if (or re-p str-p) (substring pv 1 -1) pv))
12430 (if time-p (setq pv (org-matcher-time pv)))
12431 (setq po (org-op-to-function po (if time-p 'time str-p)))
12433 ((equal pn "CATEGORY")
12434 (setq gv '(get-text-property (point) 'org-category)))
12438 (setq gv `(org-cached-entry-get nil ,pn))))
12441 `(not (string-match ,pv (or ,gv "")))
12442 `(string-match ,pv (or ,gv "")))
12444 `(,po (or ,gv "") ,pv)
12445 `(,po (string-to-number (or ,gv ""))
12446 ,(string-to-number pv) ))))
12447 (t `(member ,tag tags-list)))
12448 mm (if minus (list 'not mm) mm)
12450 (push mm tagsmatcher))
12451 (push (if (> (length tagsmatcher) 1)
12452 (cons 'and tagsmatcher)
12455 (setq tagsmatcher nil))
12456 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
12458 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
12459 ;; Make the todo matcher
12460 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
12461 (setq todomatcher t)
12462 (setq orterms (org-split-string todomatch "|") orlist nil)
12463 (while (setq term (pop orterms))
12464 (while (string-match re term)
12465 (setq minus (and (match-end 1)
12466 (equal (match-string 1 term) "-"))
12467 kwd (match-string 2 term)
12468 re-p (equal (string-to-char kwd) ?{)
12469 term (substring term (match-end 0))
12471 `(string-match ,(substring kwd 1 -1) todo)
12472 (list 'equal 'todo kwd))
12473 mm (if minus (list 'not mm) mm))
12474 (push mm todomatcher))
12475 (push (if (> (length todomatcher) 1)
12476 (cons 'and todomatcher)
12479 (setq todomatcher nil))
12480 (setq todomatcher (if (> (length orlist) 1)
12481 (cons 'or orlist) (car orlist))))
12483 ;; Return the string and lisp forms of the matcher
12484 (setq matcher (if todomatcher
12485 (list 'and tagsmatcher todomatcher)
12487 (cons match0 matcher)))
12489 (defun org-op-to-function (op &optional stringp)
12490 "Turn an operator into the appropriate function."
12493 ((equal op "<" ) '(< string< org-time<))
12494 ((equal op ">" ) '(> org-string> org-time>))
12495 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
12496 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
12497 ((member op '("=" "==")) '(= string= org-time=))
12498 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
12499 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
12501 (defun org<> (a b) (not (= a b)))
12502 (defun org-string<= (a b) (or (string= a b) (string< a b)))
12503 (defun org-string>= (a b) (not (string< a b)))
12504 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
12505 (defun org-string<> (a b) (not (string= a b)))
12506 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
12507 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
12508 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
12509 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
12510 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
12511 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
12513 "Convert S to a floating point time.
12514 If S is already a number, just return it. If it is a string, parse
12515 it as a time string and apply `float-time' to it. If S is nil, just return 0."
12519 (condition-case nil
12520 (float-time (apply 'encode-time (org-parse-time-string s)))
12524 (defun org-time-today ()
12525 "Time in seconds today at 0:00.
12526 Returns the float number of seconds since the beginning of the
12527 epoch to the beginning of today (00:00)."
12528 (float-time (apply 'encode-time
12529 (append '(0 0 0) (nthcdr 3 (decode-time))))))
12531 (defun org-matcher-time (s)
12532 "Interpret a time comparison value."
12535 ((string= s "<now>") (float-time))
12536 ((string= s "<today>") (org-time-today))
12537 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
12538 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
12539 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
12540 (+ (org-time-today)
12541 (* (string-to-number (match-string 1 s))
12542 (cdr (assoc (match-string 2 s)
12543 '(("d" . 86400.0) ("w" . 604800.0)
12544 ("m" . 2678400.0) ("y" . 31557600.0)))))))
12547 (defun org-match-any-p (re list)
12548 "Does re match any element of list?"
12549 (setq list (mapcar (lambda (x) (string-match re x)) list))
12552 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
12553 (defvar org-tags-overlay (make-overlay 1 1))
12554 (org-detach-overlay org-tags-overlay)
12556 (defun org-get-local-tags-at (&optional pos)
12557 "Get a list of tags defined in the current headline."
12558 (org-get-tags-at pos 'local))
12560 (defun org-get-local-tags ()
12561 "Get a list of tags defined in the current headline."
12562 (org-get-tags-at nil 'local))
12564 (defun org-get-tags-at (&optional pos local)
12565 "Get a list of all headline tags applicable at POS.
12566 POS defaults to point. If tags are inherited, the list contains
12567 the targets in the same sequence as the headlines appear, i.e.
12568 the tags of the current headline come last.
12569 When LOCAL is non-nil, only return tags from the current headline,
12570 ignore inherited ones."
12572 (if (and org-trust-scanner-tags
12573 (or (not pos) (equal pos (point)))
12576 (let (tags ltags lastpos parent)
12580 (goto-char (or pos (point)))
12583 (condition-case nil
12585 (org-back-to-heading t)
12586 (while (not (equal lastpos (point)))
12587 (setq lastpos (point))
12589 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
12590 (setq ltags (org-split-string
12591 (org-match-string-no-properties 1) ":"))
12593 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
12596 (org-remove-uniherited-tags ltags)
12599 (or org-use-tag-inheritance (throw 'done t))
12600 (if local (throw 'done t))
12601 (or (org-up-heading-safe) (error nil))
12604 (append (org-remove-uniherited-tags org-file-tags) tags)))))
12606 (defun org-add-prop-inherited (s)
12607 (add-text-properties 0 (length s) '(inherited t) s)
12610 (defun org-toggle-tag (tag &optional onoff)
12611 "Toggle the tag TAG for the current line.
12612 If ONOFF is `on' or `off', don't toggle but set to this state."
12615 (org-back-to-heading t)
12616 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
12619 (setq current (match-string 1))
12620 (replace-match ""))
12622 (setq current (nreverse (org-split-string current ":")))
12626 (or (member tag current) (push tag current)))
12628 (or (not (member tag current)) (setq current (delete tag current))))
12629 (t (if (member tag current)
12630 (setq current (delete tag current))
12632 (push tag current))))
12636 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
12637 (org-set-tags nil t))
12638 (delete-horizontal-space))
12639 (run-hooks 'org-after-tags-change-hook))
12642 (defun org-align-tags-here (to-col)
12643 ;; Assumes that this is a headline
12644 (let ((pos (point)) (col (current-column)) ncol tags-l p)
12645 (beginning-of-line 1)
12646 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
12647 (< pos (match-beginning 2)))
12649 (setq tags-l (- (match-end 2) (match-beginning 2)))
12650 (goto-char (match-beginning 1))
12652 (delete-region (point) (1+ (match-beginning 2)))
12653 (setq ncol (max (1+ (current-column))
12657 (- (abs to-col) tags-l))))
12659 (insert (make-string (- ncol (current-column)) ?\ ))
12660 (setq ncol (current-column))
12661 (when indent-tabs-mode (tabify p (point-at-eol)))
12662 (org-move-to-column (min ncol col) t))
12665 (defun org-set-tags-command (&optional arg just-align)
12666 "Call the set-tags command for the current entry."
12668 (if (org-on-heading-p)
12669 (org-set-tags arg just-align)
12671 (org-back-to-heading t)
12672 (org-set-tags arg just-align))))
12674 (defun org-set-tags-to (data)
12675 "Set the tags of the current entry to DATA, replacing the current tags.
12676 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
12677 If DATA is nil or the empty string, any tags will be removed."
12678 (interactive "sTags: ")
12682 ((equal data "") "")
12684 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
12687 (concat ":" (mapconcat 'identity data ":") ":"))
12691 (org-back-to-heading t)
12692 (when (looking-at org-complex-heading-regexp)
12695 (goto-char (match-beginning 5))
12697 (delete-region (point) (point-at-eol))
12698 (org-set-tags nil 'align))
12699 (goto-char (point-at-eol))
12701 (org-set-tags nil 'align)))
12702 (beginning-of-line 1)
12703 (if (looking-at ".*?\\([ \t]+\\)$")
12704 (delete-region (match-beginning 1) (match-end 1))))))
12706 (defun org-align-all-tags ()
12707 "Align the tags i all headings."
12710 (or (ignore-errors (org-back-to-heading t))
12711 (outline-next-heading))
12712 (if (org-on-heading-p)
12714 (message "No headings"))))
12716 (defvar org-indent-indentation-per-level)
12717 (defun org-set-tags (&optional arg just-align)
12718 "Set the tags for the current headline.
12719 With prefix ARG, realign all tags in headings in the current buffer."
12721 (let* ((re (concat "^" outline-regexp))
12722 (current (org-get-tags-string))
12723 (col (current-column))
12724 (org-setting-tags t)
12725 table current-tags inherited-tags ; computed below when needed
12726 tags p0 c0 c1 rpl di tc level)
12729 (goto-char (point-min))
12730 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
12731 (while (re-search-forward re nil t)
12732 (org-set-tags nil t)
12734 (message "All tags realigned to column %d" org-tags-column))
12736 (setq tags current)
12737 ;; Get a new set of tags from the user
12739 (setq table (append org-tag-persistent-alist
12740 (or org-tag-alist (org-get-buffer-tags))
12741 (and org-complete-tags-always-offer-all-agenda-tags
12742 (org-global-tags-completion-table (org-agenda-files))))
12743 org-last-tags-completion-table table
12744 current-tags (org-split-string current ":")
12745 inherited-tags (nreverse
12746 (nthcdr (length current-tags)
12747 (nreverse (org-get-tags-at))))
12749 (if (or (eq t org-use-fast-tag-selection)
12750 (and org-use-fast-tag-selection
12751 (delq nil (mapcar 'cdr table))))
12752 (org-fast-tag-selection
12753 current-tags inherited-tags table
12754 (if org-fast-tag-selection-include-todo org-todo-key-alist))
12755 (let ((org-add-colon-after-tag-completion t))
12757 (org-without-partial-completion
12758 (org-icompleting-read "Tags: " 'org-tags-completion-function
12759 nil nil current 'org-tags-history)))))))
12760 (while (string-match "[-+&]+" tags)
12761 ;; No boolean logic, just a list
12762 (setq tags (replace-match ":" t t tags))))
12764 (if org-tags-sort-function
12765 (setq tags (mapconcat 'identity
12766 (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
12767 org-tags-sort-function) ":")))
12769 (if (string-match "\\`[\t ]*\\'" tags)
12771 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
12772 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
12774 ;; Insert new tags at the correct column
12775 (beginning-of-line 1)
12776 (setq level (or (and (looking-at org-outline-regexp)
12777 (- (match-end 0) (point) 1))
12780 ((and (equal current "") (equal tags "")))
12781 ((re-search-forward
12782 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
12784 (if (equal tags "")
12786 (goto-char (match-beginning 0))
12787 (setq c0 (current-column)
12788 ;; compute offset for the case of org-indent-mode active
12789 di (if org-indent-mode
12790 (* (1- org-indent-indentation-per-level) (1- level))
12792 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
12793 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
12794 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
12795 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
12796 (replace-match rpl t t)
12797 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
12799 (t (error "Tags alignment failed")))
12800 (org-move-to-column col)
12802 (run-hooks 'org-after-tags-change-hook)))))
12804 (defun org-change-tag-in-region (beg end tag off)
12805 "Add or remove TAG for each entry in the region.
12806 This works in the agenda, and also in an org-mode buffer."
12808 (list (region-beginning) (region-end)
12809 (let ((org-last-tags-completion-table
12811 (org-get-buffer-tags)
12812 (org-global-tags-completion-table))))
12813 (org-icompleting-read
12814 "Tag: " 'org-tags-completion-function nil nil nil
12815 'org-tags-history))
12817 (message "[s]et or
[r]emove? ")
12818 (equal (read-char-exclusive) ?r))))
12819 (if (fboundp 'deactivate-mark) (deactivate-mark))
12820 (let ((agendap (equal major-mode 'org-agenda-mode))
12821 l1 l2 m buf pos newhead (cnt 0))
12823 (setq l2 (1- (org-current-line)))
12825 (setq l1 (org-current-line))
12826 (loop for l from l1 to l2 do
12828 (setq m (get-text-property (point) 'org-hd-marker))
12829 (when (or (and (org-mode-p) (org-on-heading-p))
12831 (setq buf (if agendap (marker-buffer m) (current-buffer))
12832 pos (if agendap m (point)))
12833 (with-current-buffer buf
12837 (setq cnt (1+ cnt))
12838 (org-toggle-tag tag (if off 'off 'on))
12839 (setq newhead (org-get-heading)))))
12840 (and agendap (org-agenda-change-all-lines newhead m))))
12841 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
12843 (defun org-tags-completion-function (string predicate &optional flag)
12844 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
12845 (confirm (lambda (x) (stringp (car x)))))
12846 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
12847 (setq s1 (match-string 1 string)
12848 s2 (match-string 2 string))
12849 (setq s1 "" s2 string))
12853 (setq rtn (try-completion s2 ctable confirm))
12856 (concat s1 s2 (substring rtn (length s2))
12857 (if (and org-add-colon-after-tag-completion
12858 (assoc rtn ctable))
12863 (all-completions s2 ctable confirm)
12867 (assoc s2 ctable)))
12870 (defun org-fast-tag-insert (kwd tags face &optional end)
12871 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
12872 (insert (format "%-12s" (concat kwd ":"))
12873 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
12876 (defun org-fast-tag-show-exit (flag)
12879 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
12880 (replace-match ""))
12883 (org-move-to-column (- (window-width) 19) t)
12884 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
12886 (defun org-set-current-tags-overlay (current prefix)
12887 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
12888 (if (featurep 'xemacs)
12889 (org-overlay-display org-tags-overlay (concat prefix s)
12890 'secondary-selection)
12891 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
12892 (org-overlay-display org-tags-overlay (concat prefix s)))))
12894 (defvar org-last-tag-selection-key nil)
12895 (defun org-fast-tag-selection (current inherited table &optional todo-table)
12896 "Fast tag selection with single keys.
12897 CURRENT is the current list of tags in the headline, INHERITED is the
12898 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
12899 possibly with grouping information. TODO-TABLE is a similar table with
12900 TODO keywords, should these have keys assigned to them.
12901 If the keys are nil, a-z are automatically assigned.
12902 Returns the new tags string, or nil to not change the current settings."
12903 (let* ((fulltable (append table todo-table))
12904 (maxlen (apply 'max (mapcar
12906 (if (stringp (car x)) (string-width (car x)) 0))
12908 (buf (current-buffer))
12909 (expert (eq org-fast-tag-selection-single-key 'expert))
12911 (fwidth (+ maxlen 3 1 3))
12912 (ncol (/ (- (window-width) 4) fwidth))
12915 tg cnt e c char c1 c2 ntable tbl rtn
12916 ov-start ov-end ov-prefix
12917 (exit-after-next org-fast-tag-selection-single-key)
12918 (done-keywords org-done-keywords)
12921 (beginning-of-line 1)
12923 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
12924 (setq ov-start (match-beginning 1)
12925 ov-end (match-end 1)
12927 (setq ov-start (1- (point-at-eol))
12928 ov-end (1+ ov-start))
12929 (skip-chars-forward "^\n\r")
12932 (buffer-substring (1- (point)) (point))
12933 (if (> (current-column) org-tags-column)
12935 (make-string (- org-tags-column (current-column)) ?\ ))))))
12936 (move-overlay org-tags-overlay ov-start ov-end)
12937 (save-window-excursion
12939 (set-buffer (get-buffer-create " *Org tags*"))
12940 (delete-other-windows)
12941 (split-window-vertically)
12942 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
12944 (org-set-local 'org-done-keywords done-keywords)
12945 (org-fast-tag-insert "Inherited" inherited i-face "\n")
12946 (org-fast-tag-insert "Current" current c-face "\n\n")
12947 (org-fast-tag-show-exit exit-after-next)
12948 (org-set-current-tags-overlay current ov-prefix)
12949 (setq tbl fulltable char ?a cnt 0)
12950 (while (setq e (pop tbl))
12952 ((equal (car e) :startgroup)
12953 (push '() groups) (setq ingroup t)
12954 (when (not (= cnt 0))
12957 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
12958 ((equal (car e) :endgroup)
12959 (setq ingroup nil cnt 0)
12960 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
12961 ((equal e '(:newline))
12962 (when (not (= cnt 0))
12966 (while (equal (car tbl) '(:newline))
12968 (setq tbl (cdr tbl)))))
12970 (setq tg (copy-sequence (car e)) c2 nil)
12973 ;; automatically assign a character.
12974 (setq c1 (string-to-char
12975 (downcase (substring
12976 tg (if (= (string-to-char tg) ?@) 1 0)))))
12977 (if (or (rassoc c1 ntable) (rassoc c1 table))
12978 (while (or (rassoc char ntable) (rassoc char table))
12979 (setq char (1+ char)))
12981 (setq c (or c2 char)))
12982 (if ingroup (push tg (car groups)))
12983 (setq tg (org-add-props tg nil 'face
12985 ((not (assoc tg table))
12986 (org-get-todo-face tg))
12987 ((member tg current) c-face)
12988 ((member tg inherited) i-face)
12990 (if (and (= cnt 0) (not ingroup)) (insert " "))
12991 (insert "[" c "] " tg (make-string
12992 (- fwidth 4 (length tg)) ?\ ))
12993 (push (cons tg c) ntable)
12994 (when (= (setq cnt (1+ cnt)) ncol)
12996 (if ingroup (insert " "))
12998 (setq ntable (nreverse ntable))
13000 (goto-char (point-min))
13001 (if (not expert) (org-fit-window-to-buffer))
13005 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
13006 (if (not groups) "no " "")
13007 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
13008 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13009 (setq org-last-tag-selection-key c)
13011 ((= c ?\r) (throw 'exit t))
13013 (setq groups (not groups))
13014 (goto-char (point-min))
13015 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
13018 (org-fast-tag-show-exit
13019 (setq exit-after-next (not exit-after-next)))
13021 (delete-other-windows)
13022 (split-window-vertically)
13023 (org-switch-to-buffer-other-window " *Org tags*")
13024 (org-fit-window-to-buffer)))
13026 (and (= c ?q) (not (rassoc c ntable))))
13027 (org-detach-overlay org-tags-overlay)
13028 (setq quit-flag t))
13031 (if exit-after-next (setq exit-after-next 'now)))
13033 (condition-case nil
13034 (setq tg (org-icompleting-read
13037 (with-current-buffer buf
13038 (org-get-buffer-tags)))))
13039 (quit (setq tg "")))
13040 (when (string-match "\\S-" tg)
13041 (add-to-list 'buffer-tags (list tg))
13042 (if (member tg current)
13043 (setq current (delete tg current))
13044 (push tg current)))
13045 (if exit-after-next (setq exit-after-next 'now)))
13046 ((setq e (rassoc c todo-table) tg (car e))
13047 (with-current-buffer buf
13048 (save-excursion (org-todo tg)))
13049 (if exit-after-next (setq exit-after-next 'now)))
13050 ((setq e (rassoc c ntable) tg (car e))
13051 (if (member tg current)
13052 (setq current (delete tg current))
13053 (loop for g in groups do
13056 (setq current (delete x current)))
13059 (if exit-after-next (setq exit-after-next 'now))))
13061 ;; Create a sorted list
13065 (assoc b (cdr (memq (assoc a ntable) ntable))))))
13066 (if (eq exit-after-next 'now) (throw 'exit t))
13067 (goto-char (point-min))
13068 (beginning-of-line 2)
13069 (delete-region (point) (point-at-eol))
13070 (org-fast-tag-insert "Current" current c-face)
13071 (org-set-current-tags-overlay current ov-prefix)
13072 (while (re-search-forward
13073 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
13074 (setq tg (match-string 1))
13075 (add-text-properties
13076 (match-beginning 1) (match-end 1)
13079 ((member tg current) c-face)
13080 ((member tg inherited) i-face)
13081 (t (get-text-property (match-beginning 1) 'face))))))
13082 (goto-char (point-min)))))
13083 (org-detach-overlay org-tags-overlay)
13085 (mapconcat 'identity current ":")
13088 (defun org-get-tags-string ()
13089 "Get the TAGS string in the current headline."
13090 (unless (org-on-heading-p t)
13091 (error "Not on a heading"))
13093 (beginning-of-line 1)
13094 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13095 (org-match-string-no-properties 1)
13098 (defun org-get-tags ()
13099 "Get the list of tags specified in the current headline."
13100 (org-split-string (org-get-tags-string) ":"))
13102 (defun org-get-buffer-tags ()
13103 "Get a table of all tags used in the buffer, for completion."
13106 (goto-char (point-min))
13107 (while (re-search-forward
13108 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
13109 (when (equal (char-after (point-at-bol 0)) ?*)
13110 (mapc (lambda (x) (add-to-list 'tags x))
13111 (org-split-string (org-match-string-no-properties 1) ":")))))
13112 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
13113 (mapcar 'list tags)))
13115 ;;;; The mapping API
13118 (defun org-map-entries (func &optional match scope &rest skip)
13119 "Call FUNC at each headline selected by MATCH in SCOPE.
13121 FUNC is a function or a lisp form. The function will be called without
13122 arguments, with the cursor positioned at the beginning of the headline.
13123 The return values of all calls to the function will be collected and
13124 returned as a list.
13126 The call to FUNC will be wrapped into a save-excursion form, so FUNC
13127 does not need to preserve point. After evaluation, the cursor will be
13128 moved to the end of the line (presumably of the headline of the
13129 processed entry) and search continues from there. Under some
13130 circumstances, this may not produce the wanted results. For example,
13131 if you have removed (e.g. archived) the current (sub)tree it could
13132 mean that the next entry will be skipped entirely. In such cases, you
13133 can specify the position from where search should continue by making
13134 FUNC set the variable `org-map-continue-from' to the desired buffer
13137 MATCH is a tags/property/todo match as it is used in the agenda tags view.
13138 Only headlines that are matched by this query will be considered during
13139 the iteration. When MATCH is nil or t, all headlines will be
13140 visited by the iteration.
13142 SCOPE determines the scope of this command. It can be any of:
13144 nil The current buffer, respecting the restriction if any
13145 tree The subtree started with the entry at point
13146 file The current buffer, without restriction
13148 The current buffer, and any archives associated with it
13149 agenda All agenda files
13150 agenda-with-archives
13151 All agenda files with any archive files associated with them
13153 If this is a list, all files in the list will be scanned
13155 The remaining args are treated as settings for the skipping facilities of
13156 the scanner. The following items can be given here:
13158 archive skip trees with the archive tag.
13159 comment skip trees with the COMMENT keyword
13160 function or Emacs Lisp form:
13161 will be used as value for `org-agenda-skip-function', so whenever
13162 the function returns t, FUNC will not be called for that
13163 entry and search will continue from the point where the
13164 function leaves it.
13166 If your function needs to retrieve the tags including inherited tags
13167 at the *current* entry, you can use the value of the variable
13168 `org-scanner-tags' which will be much faster than getting the value
13169 with `org-get-tags-at'. If your function gets properties with
13170 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
13171 to t around the call to `org-entry-properties' to get the same speedup.
13172 Note that if your function moves around to retrieve tags and properties at
13173 a *different* entry, you cannot use these techniques."
13174 (let* ((org-agenda-archives-mode nil) ; just to make sure
13175 (org-agenda-skip-archived-trees (memq 'archive skip))
13176 (org-agenda-skip-comment-trees (memq 'comment skip))
13177 (org-agenda-skip-function
13178 (car (org-delete-all '(comment archive) skip)))
13179 (org-tags-match-list-sublevels t)
13181 org-todo-keywords-for-agenda
13182 org-done-keywords-for-agenda
13183 org-todo-keyword-alist-for-agenda
13184 org-drawers-for-agenda
13185 org-tag-alist-for-agenda)
13188 ((eq match t) (setq matcher t))
13189 ((eq match nil) (setq matcher t))
13190 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
13194 (when (eq scope 'tree)
13195 (org-back-to-heading t)
13196 (org-narrow-to-subtree)
13201 (org-prepare-agenda-buffers
13202 (list (buffer-file-name (current-buffer))))
13203 (setq res (org-scan-tags func matcher)))
13204 ;; Get the right scope
13206 ((and scope (listp scope) (symbolp (car scope)))
13207 (setq scope (eval scope)))
13208 ((eq scope 'agenda)
13209 (setq scope (org-agenda-files t)))
13210 ((eq scope 'agenda-with-archives)
13211 (setq scope (org-agenda-files t))
13212 (setq scope (org-add-archive-files scope)))
13214 (setq scope (list (buffer-file-name))))
13215 ((eq scope 'file-with-archives)
13216 (setq scope (org-add-archive-files (list (buffer-file-name))))))
13217 (org-prepare-agenda-buffers scope)
13218 (while (setq file (pop scope))
13219 (with-current-buffer (org-find-base-buffer-visiting file)
13223 (goto-char (point-min))
13224 (setq res (append res (org-scan-tags func matcher))))))))))
13229 ;;; Setting and retrieving properties
13231 (defconst org-special-properties
13232 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
13233 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
13234 "The special properties valid in Org-mode.
13236 These are properties that are not defined in the property drawer,
13237 but in some other way.")
13239 (defconst org-default-properties
13240 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
13241 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
13242 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
13243 "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
13244 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
13245 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
13246 "Some properties that are used by Org-mode for various purposes.
13247 Being in this list makes sure that they are offered for completion.")
13249 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
13250 "Regular expression matching the first line of a property drawer.")
13252 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
13253 "Regular expression matching the last line of a property drawer.")
13255 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
13256 "Regular expression matching the first line of a property drawer.")
13258 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
13259 "Regular expression matching the first line of a property drawer.")
13261 (defconst org-property-drawer-re
13262 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
13263 org-property-end-re "\\)\n?")
13264 "Matches an entire property drawer.")
13266 (defconst org-clock-drawer-re
13267 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
13268 org-property-end-re "\\)\n?")
13269 "Matches an entire clock drawer.")
13271 (defun org-property-action ()
13272 "Do an action on properties."
13275 (org-at-property-p)
13276 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
13277 (setq c (read-char-exclusive))
13280 (call-interactively 'org-set-property))
13282 (call-interactively 'org-delete-property))
13284 (call-interactively 'org-delete-property-globally))
13286 (call-interactively 'org-compute-property-at-point))
13287 (t (error "No such property action %c" c)))))
13289 (defun org-set-effort (&optional value)
13290 "Set the effort property of the current entry.
13291 With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
13294 (if (equal value 0) (setq value 10))
13295 (let* ((completion-ignore-case t)
13296 (prop org-effort-property)
13297 (cur (org-entry-get nil prop))
13298 (allowed (org-property-get-allowed-values nil prop 'table))
13299 (existing (mapcar 'list (org-property-values prop)))
13302 ((stringp value) value)
13303 ((and allowed (integerp value))
13304 (or (car (nth (1- value) allowed))
13305 (car (org-last allowed))))
13307 (message "Select 1-9,0, [RET%s]: %s"
13308 (if cur (concat "=" cur) "")
13309 (mapconcat 'car allowed " "))
13310 (setq rpl (read-char-exclusive))
13311 (if (equal rpl ?\r)
13313 (setq rpl (- rpl ?0))
13314 (if (equal rpl 0) (setq rpl 10))
13315 (if (and (> rpl 0) (<= rpl (length allowed)))
13316 (car (nth (1- rpl) allowed))
13317 (org-completing-read "Effort: " allowed nil))))
13319 (let (org-completion-use-ido org-completion-use-iswitchb)
13320 (org-completing-read
13321 (concat "Effort " (if (and cur (string-match "\\S-" cur))
13322 (concat "[" cur "]") "")
13324 existing nil nil "" nil cur))))))
13325 (unless (equal (org-entry-get nil prop) val)
13326 (org-entry-put nil prop val))
13327 (message "%s is now %s" prop val)))
13329 (defun org-at-property-p ()
13330 "Is cursor inside a property drawer?"
13332 (beginning-of-line 1)
13333 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
13334 (save-match-data ;; Used by calling procedures
13336 (range (unless (org-before-first-heading-p)
13337 (org-get-property-block))))
13338 (and range (<= (car range) p) (< p (cdr range))))))))
13340 (defun org-get-property-block (&optional beg end force)
13341 "Return the (beg . end) range of the body of the property drawer.
13342 BEG and END can be beginning and end of subtree, if not given
13343 they will be found.
13344 If the drawer does not exist and FORCE is non-nil, create the drawer."
13347 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
13348 (end (or end (progn (outline-next-heading) (point)))))
13350 (if (re-search-forward org-property-start-re end t)
13351 (setq beg (1+ (match-end 0)))
13354 (org-insert-property-drawer)
13355 (setq end (progn (outline-next-heading) (point))))
13358 (if (re-search-forward org-property-start-re end t)
13359 (setq beg (1+ (match-end 0)))))
13360 (if (re-search-forward org-property-end-re end t)
13361 (setq end (match-beginning 0))
13362 (or force (throw 'exit nil))
13365 (org-indent-line-function)
13366 (insert ":END:\n"))
13369 (defun org-entry-properties (&optional pom which specific)
13370 "Get all properties of the entry at point-or-marker POM.
13371 This includes the TODO keyword, the tags, time strings for deadline,
13372 scheduled, and clocking, and any additional properties defined in the
13373 entry. The return value is an alist, keys may occur multiple times
13374 if the property key was used several times.
13375 POM may also be nil, in which case the current entry is used.
13376 If WHICH is nil or `all', get all properties. If WHICH is
13377 `special' or `standard', only get that subclass. If WHICH
13378 is a string only get exactly this property. Specific can be a string, the
13379 specific property we are interested in. Specifying it can speed
13380 things up because then unnecessary parsing is avoided."
13381 (setq which (or which 'all))
13382 (org-with-point-at pom
13383 (let ((clockstr (substring org-clock-string 0 -1))
13384 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
13385 (case-fold-search nil)
13386 beg end range props sum-props key key1 value string clocksum)
13388 (when (condition-case nil
13389 (and (org-mode-p) (org-back-to-heading t))
13392 (setq sum-props (get-text-property (point) 'org-summaries))
13393 (setq clocksum (get-text-property (point) :org-clock-minutes))
13394 (outline-next-heading)
13396 (when (memq which '(all special))
13397 ;; Get the special properties, like TODO and tags
13399 (when (and (or (not specific) (string= specific "TODO"))
13400 (looking-at org-todo-line-regexp) (match-end 2))
13401 (push (cons "TODO" (org-match-string-no-properties 2)) props))
13402 (when (and (or (not specific) (string= specific "PRIORITY"))
13403 (looking-at org-priority-regexp))
13404 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
13405 (when (and (or (not specific) (string= specific "TAGS"))
13406 (setq value (org-get-tags-string))
13407 (string-match "\\S-" value))
13408 (push (cons "TAGS" value) props))
13409 (when (and (or (not specific) (string= specific "ALLTAGS"))
13410 (setq value (org-get-tags-at)))
13411 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
13414 (when (or (not specific) (string= specific "BLOCKED"))
13415 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
13416 (when (or (not specific)
13418 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
13419 "TIMESTAMP" "TIMESTAMP_IA")))
13420 (while (re-search-forward org-maybe-keyword-time-regexp end t)
13421 (setq key (if (match-end 1)
13422 (substring (org-match-string-no-properties 1)
13424 string (if (equal key clockstr)
13428 (match-beginning 3) (goto-char
13430 (substring (org-match-string-no-properties 3)
13432 ;; Get the correct property name from the key. This is
13433 ;; necessary if the user has configured time keywords.
13434 (setq key1 (concat key ":"))
13438 (if (= (char-after (match-beginning 3)) ?\[)
13439 "TIMESTAMP_IA" "TIMESTAMP")))
13440 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
13441 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
13442 ((equal key1 org-closed-string) (setq key "CLOSED"))
13443 ((equal key1 org-clock-string) (setq key "CLOCK")))
13444 (when (or (equal key "CLOCK") (not (assoc key props)))
13445 (push (cons key string) props))))
13448 (when (memq which '(all standard))
13449 ;; Get the standard properties, like :PROP: ...
13450 (setq range (org-get-property-block beg end))
13452 (goto-char (car range))
13453 (while (re-search-forward
13454 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
13456 (setq key (org-match-string-no-properties 1)
13457 value (org-trim (or (org-match-string-no-properties 2) "")))
13458 (unless (member key excluded)
13459 (push (cons key (or value "")) props)))))
13461 (push (cons "CLOCKSUM"
13462 (org-columns-number-to-string (/ (float clocksum) 60.)
13465 (unless (assoc "CATEGORY" props)
13466 (setq value (or (org-get-category)
13467 (progn (org-refresh-category-properties)
13468 (org-get-category))))
13469 (push (cons "CATEGORY" value) props))
13470 (append sum-props (nreverse props)))))))
13472 (defun org-entry-get (pom property &optional inherit literal-nil)
13473 "Get value of PROPERTY for entry at point-or-marker POM.
13474 If INHERIT is non-nil and the entry does not have the property,
13475 then also check higher levels of the hierarchy.
13476 If INHERIT is the symbol `selective', use inheritance only if the setting
13477 in `org-use-property-inheritance' selects PROPERTY for inheritance.
13478 If the property is present but empty, the return value is the empty string.
13479 If the property is not present at all, nil is returned.
13481 If LITERAL-NIL is set, return the string value \"nil\" as a string,
13482 do not interpret it as the list atom nil. This is used for inheritance
13483 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
13484 (org-with-point-at pom
13485 (if (and inherit (if (eq inherit 'selective)
13486 (org-property-inherit-p property)
13488 (org-entry-get-with-inheritance property literal-nil)
13489 (if (member property org-special-properties)
13490 ;; We need a special property. Use `org-entry-properties' to
13491 ;; retrieve it, but specify the wanted property
13492 (cdr (assoc property (org-entry-properties nil 'special property)))
13493 (let ((range (org-get-property-block)))
13495 (goto-char (car range))
13497 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
13499 ;; Found the property, return it.
13502 (org-match-string-no-properties 1)
13503 (org-not-nil (org-match-string-no-properties 1)))
13506 (defun org-property-or-variable-value (var &optional inherit)
13507 "Check if there is a property fixing the value of VAR.
13508 If yes, return this value. If not, return the current value of the variable."
13509 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
13510 (if (and prop (stringp prop) (string-match "\\S-" prop))
13512 (symbol-value var))))
13514 (defun org-entry-delete (pom property)
13515 "Delete the property PROPERTY from entry at point-or-marker POM."
13516 (org-with-point-at pom
13517 (if (member property org-special-properties)
13518 nil ; cannot delete these properties.
13519 (let ((range (org-get-property-block)))
13521 (goto-char (car range))
13523 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
13526 (delete-region (match-beginning 0) (1+ (point-at-eol)))
13530 ;; Multi-values properties are properties that contain multiple values
13531 ;; These values are assumed to be single words, separated by whitespace.
13532 (defun org-entry-add-to-multivalued-property (pom property value)
13533 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
13534 (let* ((old (org-entry-get pom property))
13535 (values (and old (org-split-string old "[ \t]"))))
13536 (setq value (org-entry-protect-space value))
13537 (unless (member value values)
13538 (setq values (cons value values))
13539 (org-entry-put pom property
13540 (mapconcat 'identity values " ")))))
13542 (defun org-entry-remove-from-multivalued-property (pom property value)
13543 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
13544 (let* ((old (org-entry-get pom property))
13545 (values (and old (org-split-string old "[ \t]"))))
13546 (setq value (org-entry-protect-space value))
13547 (when (member value values)
13548 (setq values (delete value values))
13549 (org-entry-put pom property
13550 (mapconcat 'identity values " ")))))
13552 (defun org-entry-member-in-multivalued-property (pom property value)
13553 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
13554 (let* ((old (org-entry-get pom property))
13555 (values (and old (org-split-string old "[ \t]"))))
13556 (setq value (org-entry-protect-space value))
13557 (member value values)))
13559 (defun org-entry-get-multivalued-property (pom property)
13560 "Return a list of values in a multivalued property."
13561 (let* ((value (org-entry-get pom property))
13562 (values (and value (org-split-string value "[ \t]"))))
13563 (mapcar 'org-entry-restore-space values)))
13565 (defun org-entry-put-multivalued-property (pom property &rest values)
13566 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
13567 VALUES should be a list of strings. Spaces will be protected."
13568 (org-entry-put pom property
13569 (mapconcat 'org-entry-protect-space values " "))
13570 (let* ((value (org-entry-get pom property))
13571 (values (and value (org-split-string value "[ \t]"))))
13572 (mapcar 'org-entry-restore-space values)))
13574 (defun org-entry-protect-space (s)
13575 "Protect spaces and newline in string S."
13576 (while (string-match " " s)
13577 (setq s (replace-match "%20" t t s)))
13578 (while (string-match "\n" s)
13579 (setq s (replace-match "%0A" t t s)))
13582 (defun org-entry-restore-space (s)
13583 "Restore spaces and newline in string S."
13584 (while (string-match "%20" s)
13585 (setq s (replace-match " " t t s)))
13586 (while (string-match "%0A" s)
13587 (setq s (replace-match "\n" t t s)))
13590 (defvar org-entry-property-inherited-from (make-marker)
13591 "Marker pointing to the entry from where a property was inherited.
13592 Each call to `org-entry-get-with-inheritance' will set this marker to the
13593 location of the entry where the inheritance search matched. If there was
13594 no match, the marker will point nowhere.
13595 Note that also `org-entry-get' calls this function, if the INHERIT flag
13598 (defun org-entry-get-with-inheritance (property &optional literal-nil)
13599 "Get entry property, and search higher levels if not present.
13600 The search will stop at the first ancestor which has the property defined.
13601 If the value found is \"nil\", return nil to show that the property
13602 should be considered as undefined (this is the meaning of nil here).
13603 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
13604 (move-marker org-entry-property-inherited-from nil)
13611 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
13612 (org-back-to-heading t)
13613 (move-marker org-entry-property-inherited-from (point))
13615 (or (org-up-heading-safe) (throw 'ex nil)))))
13617 (cdr (assoc property org-file-properties))
13618 (cdr (assoc property org-global-properties))
13619 (cdr (assoc property org-global-properties-fixed))))
13620 (if literal-nil tmp (org-not-nil tmp)))))
13622 (defvar org-property-changed-functions nil
13623 "Hook called when the value of a property has changed.
13624 Each hook function should accept two arguments, the name of the property
13625 and the new value.")
13627 (defun org-entry-put (pom property value)
13628 "Set PROPERTY to VALUE for entry at point-or-marker POM."
13629 (org-with-point-at pom
13630 (org-back-to-heading t)
13631 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
13634 ((equal property "TODO")
13635 (when (and (stringp value) (string-match "\\S-" value)
13636 (not (member value org-todo-keywords-1)))
13637 (error "\"%s\" is not a valid TODO state" value))
13638 (if (or (not value)
13639 (not (string-match "\\S-" value)))
13640 (setq value 'none))
13642 (org-set-tags nil 'align))
13643 ((equal property "PRIORITY")
13644 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
13645 (string-to-char value) ?\ ))
13646 (org-set-tags nil 'align))
13647 ((equal property "SCHEDULED")
13648 (if (re-search-forward org-scheduled-time-regexp end t)
13650 ((eq value 'earlier) (org-timestamp-change -1 'day))
13651 ((eq value 'later) (org-timestamp-change 1 'day))
13652 (t (call-interactively 'org-schedule)))
13653 (call-interactively 'org-schedule)))
13654 ((equal property "DEADLINE")
13655 (if (re-search-forward org-deadline-time-regexp end t)
13657 ((eq value 'earlier) (org-timestamp-change -1 'day))
13658 ((eq value 'later) (org-timestamp-change 1 'day))
13659 (t (call-interactively 'org-deadline)))
13660 (call-interactively 'org-deadline)))
13661 ((member property org-special-properties)
13662 (error "The %s property can not yet be set with `org-entry-put'"
13664 (t ; a non-special property
13665 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
13666 (setq range (org-get-property-block beg end 'force))
13667 (goto-char (car range))
13668 (if (re-search-forward
13669 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
13671 (delete-region (match-beginning 1) (match-end 1))
13672 (goto-char (match-beginning 1)))
13673 (goto-char (cdr range))
13676 (org-indent-line-function)
13677 (insert ":" property ":"))
13678 (and value (insert " " value))
13679 (org-indent-line-function)))))
13680 (run-hook-with-args 'org-property-changed-functions property value)))
13682 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
13683 "Get all property keys in the current buffer.
13684 With INCLUDE-SPECIALS, also list the special properties that reflect things
13685 like tags and TODO state.
13686 With INCLUDE-DEFAULTS, also include properties that has special meaning
13687 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
13688 With INCLUDE-COLUMNS, also include property names given in COLUMN
13689 formats in the current buffer."
13690 (let (rtn range cfmt s p)
13694 (goto-char (point-min))
13695 (while (re-search-forward org-property-start-re nil t)
13696 (setq range (org-get-property-block))
13697 (goto-char (car range))
13698 (while (re-search-forward
13699 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
13701 (add-to-list 'rtn (org-match-string-no-properties 1)))
13702 (outline-next-heading))))
13704 (when include-specials
13705 (setq rtn (append org-special-properties rtn)))
13707 (when include-defaults
13708 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
13709 (add-to-list 'rtn org-effort-property))
13711 (when include-columns
13715 (goto-char (point-min))
13716 (while (re-search-forward
13717 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
13719 (setq cfmt (match-string 2) s 0)
13720 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
13722 (setq s (match-end 0)
13723 p (match-string 1 cfmt))
13724 (unless (or (equal p "ITEM")
13725 (member p org-special-properties))
13726 (add-to-list 'rtn (match-string 1 cfmt))))))))
13728 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
13730 (defun org-property-values (key)
13731 "Return a list of all values of property KEY."
13735 (goto-char (point-min))
13736 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
13738 (while (re-search-forward re nil t)
13739 (add-to-list 'values (org-trim (match-string 1))))
13740 (delete "" values)))))
13742 (defun org-insert-property-drawer ()
13743 "Insert a property drawer into the current entry."
13745 (org-back-to-heading t)
13746 (looking-at outline-regexp)
13747 (let ((indent (if org-adapt-indentation
13748 (- (match-end 0)(match-beginning 0))
13751 (re (concat "^[ \t]*" org-keyword-time-regexp))
13753 (outline-next-heading)
13756 (while (re-search-forward re end t))
13757 (setq hiddenp (org-invisible-p))
13759 (and (equal (char-after) ?\n) (forward-char 1))
13760 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
13761 (if (member (match-string 1) '("CLOCK:" ":END:"))
13762 ;; just skip this line
13763 (beginning-of-line 2)
13764 ;; Drawer start, find the end
13765 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
13766 (beginning-of-line 1)))
13767 (org-skip-over-state-notes)
13768 (skip-chars-backward " \t\n\r")
13769 (if (eq (char-before) ?*) (forward-char 1))
13770 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
13771 (beginning-of-line 0)
13772 (org-indent-to-column indent)
13773 (beginning-of-line 2)
13774 (org-indent-to-column indent)
13775 (beginning-of-line 0)
13778 (org-back-to-heading t)
13780 (org-flag-drawer t))))
13782 (defun org-set-property (property value)
13783 "In the current entry, set PROPERTY to VALUE.
13784 When called interactively, this will prompt for a property name, offering
13785 completion on existing and default properties. And then it will prompt
13786 for a value, offering completion either on allowed values (via an inherited
13787 xxx_ALL property) or on existing values in other instances of this property
13788 in the current file."
13790 (let* ((completion-ignore-case t)
13791 (keys (org-buffer-property-keys nil t t))
13792 (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
13793 (prop (if (member prop0 keys)
13795 (or (cdr (assoc (downcase prop0)
13796 (mapcar (lambda (x) (cons (downcase x) x))
13799 (cur (org-entry-get nil prop))
13800 (prompt (concat prop " value"
13801 (if (and cur (string-match "\\S-" cur))
13802 (concat " [" cur "]") "") ": "))
13803 (allowed (org-property-get-allowed-values nil prop 'table))
13804 (existing (mapcar 'list (org-property-values prop)))
13806 (org-completing-read prompt allowed nil
13807 (not (get-text-property 0 'org-unrestricted
13809 (let (org-completion-use-ido org-completion-use-iswitchb)
13810 (org-completing-read prompt existing nil nil "" nil cur)))))
13811 (list prop (if (equal val "") cur val))))
13812 (unless (equal (org-entry-get nil property) value)
13813 (org-entry-put nil property value)))
13815 (defun org-delete-property (property)
13816 "In the current entry, delete PROPERTY."
13818 (let* ((completion-ignore-case t)
13819 (prop (org-icompleting-read "Property: "
13820 (org-entry-properties nil 'standard))))
13822 (message "Property %s %s" property
13823 (if (org-entry-delete nil property)
13825 "was not present in the entry")))
13827 (defun org-delete-property-globally (property)
13828 "Remove PROPERTY globally, from all entries."
13830 (let* ((completion-ignore-case t)
13831 (prop (org-icompleting-read
13832 "Globally remove property: "
13833 (mapcar 'list (org-buffer-property-keys)))))
13838 (goto-char (point-min))
13840 (while (re-search-forward
13841 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
13843 (setq cnt (1+ cnt))
13844 (replace-match ""))
13845 (message "Property \"%s\" removed from %d entries" property cnt)))))
13847 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
13849 (defun org-compute-property-at-point ()
13850 "Compute the property at point.
13851 This looks for an enclosing column format, extracts the operator and
13852 then applies it to the property in the column format's scope."
13854 (unless (org-at-property-p)
13855 (error "Not at a property"))
13856 (let ((prop (org-match-string-no-properties 2)))
13857 (org-columns-get-format-and-top-level)
13858 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
13859 (error "No operator defined for property %s" prop))
13860 (org-columns-compute prop)))
13862 (defvar org-property-allowed-value-functions nil
13863 "Hook for functions supplying allowed values for a specific property.
13864 The functions must take a single argument, the name of the property, and
13865 return a flat list of allowed values. If \":ETC\" is one of
13866 the values, this means that these values are intended as defaults for
13867 completion, but that other values should be allowed too.
13868 The functions must return nil if they are not responsible for this
13871 (defun org-property-get-allowed-values (pom property &optional table)
13872 "Get allowed values for the property PROPERTY.
13873 When TABLE is non-nil, return an alist that can directly be used for
13877 ((equal property "TODO")
13878 (setq vals (org-with-point-at pom
13879 (append org-todo-keywords-1 '("")))))
13880 ((equal property "PRIORITY")
13881 (let ((n org-lowest-priority))
13882 (while (>= n org-highest-priority)
13883 (push (char-to-string n) vals)
13885 ((member property org-special-properties))
13886 ((setq vals (run-hook-with-args-until-success
13887 'org-property-allowed-value-functions property)))
13889 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
13890 (when (and vals (string-match "\\S-" vals))
13891 (setq vals (car (read-from-string (concat "(" vals ")"))))
13892 (setq vals (mapcar (lambda (x)
13893 (cond ((stringp x) x)
13894 ((numberp x) (number-to-string x))
13895 ((symbolp x) (symbol-name x))
13898 (when (member ":ETC" vals)
13899 (setq vals (remove ":ETC" vals))
13900 (org-add-props (car vals) '(org-unrestricted t)))
13901 (if table (mapcar 'list vals) vals)))
13903 (defun org-property-previous-allowed-value (&optional previous)
13904 "Switch to the next allowed value for this property."
13906 (org-property-next-allowed-value t))
13908 (defun org-property-next-allowed-value (&optional previous)
13909 "Switch to the next allowed value for this property."
13911 (unless (org-at-property-p)
13912 (error "Not at a property"))
13913 (let* ((key (match-string 2))
13914 (value (match-string 3))
13915 (allowed (or (org-property-get-allowed-values (point) key)
13916 (and (member value '("[ ]" "[-]" "[X]"))
13920 (error "Allowed values for this property have not been defined"))
13921 (if previous (setq allowed (reverse allowed)))
13922 (if (member value allowed)
13923 (setq nval (car (cdr (member value allowed)))))
13924 (setq nval (or nval (car allowed)))
13925 (if (equal nval value)
13926 (error "Only one allowed value for this property"))
13927 (org-at-property-p)
13928 (replace-match (concat " :" key ": " nval) t t)
13929 (org-indent-line-function)
13930 (beginning-of-line 1)
13931 (skip-chars-forward " \t")
13932 (run-hook-with-args 'org-property-changed-functions key nval)))
13934 (defun org-find-olp (path &optional this-buffer)
13935 "Return a marker pointing to the entry at outline path OLP.
13936 If anything goes wrong, throw an error.
13937 You can wrap this call to catch the error like this:
13939 (condition-case msg
13940 (org-mobile-locate-entry (match-string 4))
13941 (error (nth 1 msg)))
13943 The return value will then be either a string with the error message,
13944 or a marker if everything is OK.
13946 If THIS-BUFFER is set, the outline path does not contain a file,
13948 (let* ((file (if this-buffer buffer-file-name (pop path)))
13949 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
13953 limit re end found pos heading cnt)
13954 (unless buffer (error "File not found :%s" file))
13955 (with-current-buffer buffer
13959 (setq limit (point-max))
13960 (goto-char (point-min))
13961 (while (setq heading (pop path))
13962 (setq re (format org-complex-heading-regexp-format
13963 (regexp-quote heading)))
13964 (setq cnt 0 pos (point))
13965 (while (re-search-forward re end t)
13966 (setq level (- (match-end 1) (match-beginning 1)))
13967 (if (and (>= level lmin) (<= level lmax))
13968 (setq found (match-beginning 0) cnt (1+ cnt))))
13969 (when (= cnt 0) (error "Heading not found on level %d: %s"
13971 (when (> cnt 1) (error "Heading not unique on level %d: %s"
13974 (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
13975 (setq end (save-excursion (org-end-of-subtree t t))))
13976 (when (org-on-heading-p)
13977 (move-marker (make-marker) (point))))))))
13979 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
13980 "Find node HEADING in BUFFER.
13981 Return a marker to the heading if it was found, or nil if not.
13982 If POS-ONLY is set, return just the position instead of a marker.
13984 The heading text must match exact, but it may have a TODO keyword,
13985 a priority cookie and tags in the standard locations."
13986 (with-current-buffer (or buffer (current-buffer))
13990 (goto-char (point-min))
13991 (let (case-fold-search)
13992 (if (re-search-forward
13993 (format org-complex-heading-regexp-format
13994 (regexp-quote heading)) nil t)
13996 (match-beginning 0)
13997 (move-marker (make-marker) (match-beginning 0)))))))))
13999 (defun org-find-exact-heading-in-directory (heading &optional dir)
14000 "Find Org node headline HEADING in all .org files in directory DIR.
14001 When the target headline is found, return a marker to this location."
14002 (let ((files (directory-files (or dir default-directory)
14003 nil "\\`[^.#].*\\.org\\'"))
14004 file visiting m buffer)
14006 (while (setq file (pop files))
14007 (message "trying %s" file)
14008 (setq visiting (org-find-base-buffer-visiting file))
14009 (setq buffer (or visiting (find-file-noselect file)))
14010 (setq m (org-find-exact-headline-in-buffer
14012 (when (and (not m) (not visiting)) (kill-buffer buffer))
14013 (and m (throw 'found m))))))
14015 (defun org-find-entry-with-id (ident)
14016 "Locate the entry that contains the ID property with exact value IDENT.
14017 IDENT can be a string, a symbol or a number, this function will search for
14018 the string representation of it.
14019 Return the position where this entry starts, or nil if there is no such entry."
14020 (interactive "sID: ")
14022 ((stringp ident) ident)
14023 ((symbol-name ident) (symbol-name ident))
14024 ((numberp ident) (number-to-string ident))
14025 (t (error "IDENT %s must be a string, symbol or number" ident))))
14026 (case-fold-search nil))
14030 (goto-char (point-min))
14031 (when (re-search-forward
14032 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
14034 (org-back-to-heading t)
14039 (defvar org-last-changed-timestamp nil)
14040 (defvar org-last-inserted-timestamp nil
14041 "The last time stamp inserted with `org-insert-time-stamp'.")
14042 (defvar org-time-was-given) ; dynamically scoped parameter
14043 (defvar org-end-time-was-given) ; dynamically scoped parameter
14044 (defvar org-ts-what) ; dynamically scoped parameter
14046 (defun org-time-stamp (arg &optional inactive)
14047 "Prompt for a date/time and insert a time stamp.
14048 If the user specifies a time like HH:MM, or if this command is called
14049 with a prefix argument, the time stamp will contain date and time.
14050 Otherwise, only the date will be included. All parts of a date not
14051 specified by the user will be filled in from the current date/time.
14052 So if you press just return without typing anything, the time stamp
14053 will represent the current date/time. If there is already a timestamp
14054 at the cursor, it will be modified."
14058 ;; Default time is either today, or, when entering a range,
14059 ;; the range start.
14060 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
14062 (re-search-backward
14063 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
14064 (- (point) 20) t)))
14065 (apply 'encode-time (org-parse-time-string (match-string 1)))
14067 (default-input (and ts (org-get-compact-tod ts)))
14068 org-time-was-given org-end-time-was-given time)
14070 ((and (org-at-timestamp-p t)
14071 (memq last-command '(org-time-stamp org-time-stamp-inactive))
14072 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
14074 (setq time (let ((this-command this-command))
14075 (org-read-date arg 'totime nil nil
14076 default-time default-input)))
14077 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
14078 ((org-at-timestamp-p t)
14079 (setq time (let ((this-command this-command))
14080 (org-read-date arg 'totime nil nil default-time default-input)))
14081 (when (org-at-timestamp-p t) ; just to get the match data
14082 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
14084 (setq org-last-changed-timestamp
14085 (org-insert-time-stamp
14086 time (or org-time-was-given arg)
14087 inactive nil nil (list org-end-time-was-given))))
14088 (message "Timestamp updated"))
14090 (setq time (let ((this-command this-command))
14091 (org-read-date arg 'totime nil nil default-time default-input)))
14092 (org-insert-time-stamp time (or org-time-was-given arg) inactive
14093 nil nil (list org-end-time-was-given))))))
14095 ;; FIXME: can we use this for something else, like computing time differences?
14096 (defun org-get-compact-tod (s)
14097 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
14098 (let* ((t1 (match-string 1 s))
14099 (h1 (string-to-number (match-string 2 s)))
14100 (m1 (string-to-number (match-string 3 s)))
14101 (t2 (and (match-end 4) (match-string 5 s)))
14102 (h2 (and t2 (string-to-number (match-string 6 s))))
14103 (m2 (and t2 (string-to-number (match-string 7 s))))
14107 (setq dh (- h2 h1) dm (- m2 m1))
14108 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
14109 (concat t1 "+" (number-to-string dh)
14110 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
14112 (defun org-time-stamp-inactive (&optional arg)
14113 "Insert an inactive time stamp.
14114 An inactive time stamp is enclosed in square brackets instead of angle
14115 brackets. It is inactive in the sense that it does not trigger agenda entries,
14116 does not link to the calendar and cannot be changed with the S-cursor keys.
14117 So these are more for recording a certain time/date."
14119 (org-time-stamp arg 'inactive))
14121 (defvar org-date-ovl (make-overlay 1 1))
14122 (overlay-put org-date-ovl 'face 'org-warning)
14123 (org-detach-overlay org-date-ovl)
14125 (defvar org-ans1) ; dynamically scoped parameter
14126 (defvar org-ans2) ; dynamically scoped parameter
14128 (defvar org-plain-time-of-day-regexp) ; defined below
14130 (defvar org-overriding-default-time nil) ; dynamically scoped
14131 (defvar org-read-date-overlay nil)
14132 (defvar org-dcst nil) ; dynamically scoped
14133 (defvar org-read-date-history nil)
14134 (defvar org-read-date-final-answer nil)
14136 (defun org-read-date (&optional with-time to-time from-string prompt
14137 default-time default-input)
14138 "Read a date, possibly a time, and make things smooth for the user.
14139 The prompt will suggest to enter an ISO date, but you can also enter anything
14140 which will at least partially be understood by `parse-time-string'.
14141 Unrecognized parts of the date will default to the current day, month, year,
14142 hour and minute. If this command is called to replace a timestamp at point,
14143 of to enter the second timestamp of a range, the default time is taken
14144 from the existing stamp. Furthermore, the command prefers the future,
14145 so if you are giving a date where the year is not given, and the day-month
14146 combination is already past in the current year, it will assume you
14147 mean next year. For details, see the manual. A few examples:
14149 3-2-5 --> 2003-02-05
14150 feb 15 --> currentyear-02-15
14151 2/15 --> currentyear-02-15
14152 sep 12 9 --> 2009-09-12
14153 12:45 --> today 12:45
14154 22 sept 0:34 --> currentyear-09-22 0:34
14155 12 --> currentyear-currentmonth-12
14156 Fri --> nearest Friday (today or later)
14159 Furthermore you can specify a relative date by giving, as the *first* thing
14160 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
14161 change in days weeks, months, years.
14162 With a single plus or minus, the date is relative to today. With a double
14163 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
14164 +4d --> four days from today
14165 +4 --> same as above
14166 +2w --> two weeks from today
14167 ++5 --> five days from default date
14169 The function understands only English month and weekday abbreviations,
14170 but this can be configured with the variables `parse-time-months' and
14171 `parse-time-weekdays'.
14173 While prompting, a calendar is popped up - you can also select the
14174 date with the mouse (button 1). The calendar shows a period of three
14175 months. To scroll it to other months, use the keys `>' and `<'.
14176 If you don't like the calendar, turn it off with
14177 \(setq org-read-date-popup-calendar nil)
14179 With optional argument TO-TIME, the date will immediately be converted
14180 to an internal time.
14181 With an optional argument WITH-TIME, the prompt will suggest to also
14182 insert a time. Note that when WITH-TIME is not set, you can still
14183 enter a time, and this function will inform the calling routine about
14184 this change. The calling routine may then choose to change the format
14185 used to insert the time stamp into the buffer to include the time.
14186 With optional argument FROM-STRING, read from this string instead from
14187 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
14188 the time/date that is used for everything that is not specified by the
14190 (require 'parse-time)
14191 (let* ((org-time-stamp-rounding-minutes
14192 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
14193 (org-dcst org-display-custom-times)
14194 (ct (org-current-time))
14195 (def (or org-overriding-default-time default-time ct))
14196 (defdecode (decode-time def))
14198 (when (< (nth 2 defdecode) org-extend-today-until)
14199 (setcar (nthcdr 2 defdecode) -1)
14200 (setcar (nthcdr 1 defdecode) 59)
14201 (setq def (apply 'encode-time defdecode)
14202 defdecode (decode-time def)))))
14203 (calendar-frame-setup nil)
14204 (calendar-setup nil)
14205 (calendar-move-hook nil)
14206 (calendar-view-diary-initially-flag nil)
14207 (calendar-view-holidays-initially-flag nil)
14208 (timestr (format-time-string
14209 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
14210 (prompt (concat (if prompt (concat prompt " ") "")
14211 (format "Date+time [%s]: " timestr)))
14212 ans (org-ans0 "") org-ans1 org-ans2 final)
14215 (from-string (setq ans from-string))
14216 (org-read-date-popup-calendar
14218 (save-window-excursion
14220 (calendar-forward-day (- (time-to-days def)
14221 (calendar-absolute-from-gregorian
14222 (calendar-current-date))))
14223 (org-eval-in-calendar nil t)
14224 (let* ((old-map (current-local-map))
14225 (map (copy-keymap calendar-mode-map))
14226 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
14227 (org-defkey map (kbd "RET") 'org-calendar-select)
14228 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
14229 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
14230 (org-defkey minibuffer-local-map [(meta shift left)]
14231 (lambda () (interactive)
14232 (org-eval-in-calendar '(calendar-backward-month 1))))
14233 (org-defkey minibuffer-local-map [(meta shift right)]
14234 (lambda () (interactive)
14235 (org-eval-in-calendar '(calendar-forward-month 1))))
14236 (org-defkey minibuffer-local-map [(meta shift up)]
14237 (lambda () (interactive)
14238 (org-eval-in-calendar '(calendar-backward-year 1))))
14239 (org-defkey minibuffer-local-map [(meta shift down)]
14240 (lambda () (interactive)
14241 (org-eval-in-calendar '(calendar-forward-year 1))))
14242 (org-defkey minibuffer-local-map [?\e (shift left)]
14243 (lambda () (interactive)
14244 (org-eval-in-calendar '(calendar-backward-month 1))))
14245 (org-defkey minibuffer-local-map [?\e (shift right)]
14246 (lambda () (interactive)
14247 (org-eval-in-calendar '(calendar-forward-month 1))))
14248 (org-defkey minibuffer-local-map [?\e (shift up)]
14249 (lambda () (interactive)
14250 (org-eval-in-calendar '(calendar-backward-year 1))))
14251 (org-defkey minibuffer-local-map [?\e (shift down)]
14252 (lambda () (interactive)
14253 (org-eval-in-calendar '(calendar-forward-year 1))))
14254 (org-defkey minibuffer-local-map [(shift up)]
14255 (lambda () (interactive)
14256 (org-eval-in-calendar '(calendar-backward-week 1))))
14257 (org-defkey minibuffer-local-map [(shift down)]
14258 (lambda () (interactive)
14259 (org-eval-in-calendar '(calendar-forward-week 1))))
14260 (org-defkey minibuffer-local-map [(shift left)]
14261 (lambda () (interactive)
14262 (org-eval-in-calendar '(calendar-backward-day 1))))
14263 (org-defkey minibuffer-local-map [(shift right)]
14264 (lambda () (interactive)
14265 (org-eval-in-calendar '(calendar-forward-day 1))))
14266 (org-defkey minibuffer-local-map ">"
14267 (lambda () (interactive)
14268 (org-eval-in-calendar '(scroll-calendar-left 1))))
14269 (org-defkey minibuffer-local-map "<"
14270 (lambda () (interactive)
14271 (org-eval-in-calendar '(scroll-calendar-right 1))))
14272 (org-defkey minibuffer-local-map "\C-v"
14273 (lambda () (interactive)
14274 (org-eval-in-calendar
14275 '(calendar-scroll-left-three-months 1))))
14276 (org-defkey minibuffer-local-map "\M-v"
14277 (lambda () (interactive)
14278 (org-eval-in-calendar
14279 '(calendar-scroll-right-three-months 1))))
14280 (run-hooks 'org-read-date-minibuffer-setup-hook)
14283 (use-local-map map)
14284 (add-hook 'post-command-hook 'org-read-date-display)
14285 (setq org-ans0 (read-string prompt default-input
14286 'org-read-date-history nil))
14287 ;; org-ans0: from prompt
14288 ;; org-ans1: from mouse click
14289 ;; org-ans2: from calendar motion
14290 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
14291 (remove-hook 'post-command-hook 'org-read-date-display)
14292 (use-local-map old-map)
14293 (when org-read-date-overlay
14294 (delete-overlay org-read-date-overlay)
14295 (setq org-read-date-overlay nil)))))))
14297 (t ; Naked prompt only
14299 (setq ans (read-string prompt default-input
14300 'org-read-date-history timestr))
14301 (when org-read-date-overlay
14302 (delete-overlay org-read-date-overlay)
14303 (setq org-read-date-overlay nil)))))
14305 (setq final (org-read-date-analyze ans def defdecode))
14307 ;; One round trip to get rid of 34th of August and stuff like that....
14308 (setq final (decode-time (apply 'encode-time final)))
14310 (setq org-read-date-final-answer ans)
14313 (apply 'encode-time final)
14314 (if (and (boundp 'org-time-was-given) org-time-was-given)
14315 (format "%04d-%02d-%02d %02d:%02d"
14316 (nth 5 final) (nth 4 final) (nth 3 final)
14317 (nth 2 final) (nth 1 final))
14318 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
14323 (defvar org-read-date-analyze-futurep nil)
14324 (defun org-read-date-display ()
14325 "Display the current date prompt interpretation in the minibuffer."
14326 (when org-read-date-display-live
14327 (when org-read-date-overlay
14328 (delete-overlay org-read-date-overlay))
14331 (while (not (equal (buffer-substring
14332 (max (point-min) (- (point) 4)) (point))
14336 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
14337 " " (or org-ans1 org-ans2)))
14338 (org-end-time-was-given nil)
14339 (f (org-read-date-analyze ans def defdecode))
14341 org-time-stamp-custom-formats
14342 org-time-stamp-formats))
14343 (fmt (if (or with-time
14344 (and (boundp 'org-time-was-given) org-time-was-given))
14347 (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
14348 (when (and org-end-time-was-given
14349 (string-match org-plain-time-of-day-regexp txt))
14350 (setq txt (concat (substring txt 0 (match-end 0)) "-"
14351 org-end-time-was-given
14352 (substring txt (match-end 0)))))
14353 (when org-read-date-analyze-futurep
14354 (setq txt (concat txt " (=>F)")))
14355 (setq org-read-date-overlay
14356 (make-overlay (1- (point-at-eol)) (point-at-eol)))
14357 (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
14359 (defun org-read-date-analyze (ans def defdecode)
14360 "Analyze the combined answer of the date prompt."
14361 ;; FIXME: cleanup and comment
14362 (let ((nowdecode (decode-time (current-time)))
14363 delta deltan deltaw deltadef year month day
14364 hour minute second wday pm h2 m2 tl wday1
14365 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
14366 (setq org-read-date-analyze-futurep nil)
14367 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
14370 (when (setq delta (org-read-date-get-relative ans (current-time) def))
14371 (setq ans (replace-match "" t t ans)
14373 deltaw (nth 1 delta)
14374 deltadef (nth 2 delta)))
14376 ;; Check if there is an iso week date in there
14377 ;; If yes, store the info and postpone interpreting it until the rest
14378 ;; of the parsing is done
14379 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
14380 (setq iso-year (if (match-end 1)
14381 (org-small-year-to-year
14382 (string-to-number (match-string 1 ans))))
14383 iso-weekday (if (match-end 3)
14384 (string-to-number (match-string 3 ans)))
14385 iso-week (string-to-number (match-string 2 ans)))
14386 (setq ans (replace-match "" t t ans)))
14388 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
14389 (when (string-match
14390 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
14391 (setq year (if (match-end 2)
14392 (string-to-number (match-string 2 ans))
14393 (progn (setq kill-year t)
14394 (string-to-number (format-time-string "%Y"))))
14395 month (string-to-number (match-string 3 ans))
14396 day (string-to-number (match-string 4 ans)))
14397 (if (< year 100) (setq year (+ 2000 year)))
14398 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
14400 ;; Help matching american dates, like 5/30 or 5/30/7
14401 (when (string-match
14402 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
14403 (setq year (if (match-end 4)
14404 (string-to-number (match-string 4 ans))
14405 (progn (setq kill-year t)
14406 (string-to-number (format-time-string "%Y"))))
14407 month (string-to-number (match-string 1 ans))
14408 day (string-to-number (match-string 2 ans)))
14409 (if (< year 100) (setq year (+ 2000 year)))
14410 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
14412 ;; Help matching am/pm times, because `parse-time-string' does not do that.
14413 ;; If there is a time with am/pm, and *no* time without it, we convert
14414 ;; so that matching will be successful.
14415 (loop for i from 1 to 2 do ; twice, for end time as well
14416 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
14417 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
14418 (setq hour (string-to-number (match-string 1 ans))
14419 minute (if (match-end 3)
14420 (string-to-number (match-string 3 ans))
14423 (string-to-char (downcase (match-string 4 ans)))))
14424 (if (and (= hour 12) (not pm))
14426 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
14427 (setq ans (replace-match (format "%02d:%02d" hour minute)
14430 ;; Check if a time range is given as a duration
14431 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
14432 (setq hour (string-to-number (match-string 1 ans))
14433 h2 (+ hour (string-to-number (match-string 3 ans)))
14434 minute (string-to-number (match-string 2 ans))
14435 m2 (+ minute (if (match-end 5) (string-to-number
14436 (match-string 5 ans))0)))
14437 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
14438 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
14441 ;; Check if there is a time range
14442 (when (boundp 'org-end-time-was-given)
14443 (setq org-time-was-given nil)
14444 (when (and (string-match org-plain-time-of-day-regexp ans)
14446 (setq org-end-time-was-given (match-string 8 ans))
14447 (setq ans (concat (substring ans 0 (match-beginning 7))
14448 (substring ans (match-end 7))))))
14450 (setq tl (parse-time-string ans)
14451 day (or (nth 3 tl) (nth 3 defdecode))
14452 month (or (nth 4 tl)
14453 (if (and org-read-date-prefer-future
14454 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
14455 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
14456 (nth 4 defdecode)))
14457 year (or (and (not kill-year) (nth 5 tl))
14458 (if (and org-read-date-prefer-future
14459 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
14460 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
14461 (nth 5 defdecode)))
14462 hour (or (nth 2 tl) (nth 2 defdecode))
14463 minute (or (nth 1 tl) (nth 1 defdecode))
14464 second (or (nth 0 tl) 0)
14467 (when (and (eq org-read-date-prefer-future 'time)
14468 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
14469 (equal day (nth 3 nowdecode))
14470 (equal month (nth 4 nowdecode))
14471 (equal year (nth 5 nowdecode))
14473 (or (< (nth 2 tl) (nth 2 nowdecode))
14474 (and (= (nth 2 tl) (nth 2 nowdecode))
14476 (< (nth 1 tl) (nth 1 nowdecode)))))
14480 ;; Special date definitions below
14483 ;; There was an iso week
14486 (setq year (or iso-year year)
14487 day (or iso-weekday wday 1)
14488 wday nil ; to make sure that the trigger below does not match
14489 iso-date (calendar-gregorian-from-absolute
14490 (calendar-absolute-from-iso
14491 (list iso-week day year))))
14492 ; FIXME: Should we also push ISO weeks into the future?
14493 ; (when (and org-read-date-prefer-future
14495 ; (< (calendar-absolute-from-gregorian iso-date)
14496 ; (time-to-days (current-time))))
14497 ; (setq year (1+ year)
14498 ; iso-date (calendar-gregorian-from-absolute
14499 ; (calendar-absolute-from-iso
14500 ; (list iso-week day year)))))
14501 (setq month (car iso-date)
14502 year (nth 2 iso-date)
14503 day (nth 1 iso-date)))
14507 (let ((now (decode-time (current-time))))
14508 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
14509 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
14510 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
14511 ((equal deltaw "m") (setq month (+ month deltan)))
14512 ((equal deltaw "y") (setq year (+ year deltan)))))
14513 ((and wday (not (nth 3 tl)))
14515 ;; Weekday was given, but no day, so pick that day in the week
14516 ;; on or after the derived date.
14517 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
14518 (unless (equal wday wday1)
14519 (setq day (+ day (% (- wday wday1 -7) 7))))))
14520 (if (and (boundp 'org-time-was-given)
14522 (setq org-time-was-given t))
14523 (if (< year 100) (setq year (+ 2000 year)))
14524 (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
14525 (setq org-read-date-analyze-futurep futurep)
14526 (list second minute hour day month year)))
14528 (defvar parse-time-weekdays)
14530 (defun org-read-date-get-relative (s today default)
14531 "Check string S for special relative date string.
14532 TODAY and DEFAULT are internal times, for today and for a default.
14533 Return shift list (N what def-flag)
14534 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
14535 N is the number of WHATs to shift.
14536 DEF-FLAG is t when a double ++ or -- indicates shift relative to
14537 the DEFAULT date rather than TODAY."
14541 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
14543 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
14544 "\\([ \t]\\|$\\)") s)
14545 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
14546 (let* ((dir (if (> (match-end 1) (match-beginning 1))
14547 (string-to-char (substring (match-string 1 s) -1))
14549 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
14550 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
14551 (what (if (match-end 3) (match-string 3 s) "d"))
14552 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
14553 (date (if rel default today))
14554 (wday (nth 6 (decode-time date)))
14558 (setq delta (mod (+ 7 (- wday1 wday)) 7))
14559 (if (= dir ?-) (setq delta (- delta 7)))
14560 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
14561 (list delta "d" rel))
14562 (list (* n (if (= dir ?-) -1 1)) what rel)))))
14564 (defun org-order-calendar-date-args (arg1 arg2 arg3)
14565 "Turn a user-specified date into the internal representation.
14566 The internal representation needed by the calendar is (month day year).
14567 This is a wrapper to handle the brain-dead convention in calendar that
14568 user function argument order change dependent on argument order."
14569 (if (boundp 'calendar-date-style)
14571 ((eq calendar-date-style 'american)
14572 (list arg1 arg2 arg3))
14573 ((eq calendar-date-style 'european)
14574 (list arg2 arg1 arg3))
14575 ((eq calendar-date-style 'iso)
14576 (list arg2 arg3 arg1)))
14577 (if (org-bound-and-true-p european-calendar-style)
14578 (list arg2 arg1 arg3)
14579 (list arg1 arg2 arg3))))
14581 (defun org-eval-in-calendar (form &optional keepdate)
14582 "Eval FORM in the calendar window and return to current window.
14583 Also, store the cursor date in variable org-ans2."
14584 (let ((sf (selected-frame))
14585 (sw (selected-window)))
14586 (select-window (get-buffer-window "*Calendar*" t))
14588 (when (and (not keepdate) (calendar-cursor-to-date))
14589 (let* ((date (calendar-cursor-to-date))
14590 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
14591 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
14592 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
14594 (org-select-frame-set-input-focus sf)))
14596 (defun org-calendar-select ()
14597 "Return to `org-read-date' with the date currently selected.
14598 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
14600 (when (calendar-cursor-to-date)
14601 (let* ((date (calendar-cursor-to-date))
14602 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
14603 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
14604 (if (active-minibuffer-window) (exit-minibuffer))))
14606 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
14607 "Insert a date stamp for the date given by the internal TIME.
14608 WITH-HM means use the stamp format that includes the time of the day.
14609 INACTIVE means use square brackets instead of angular ones, so that the
14610 stamp will not contribute to the agenda.
14611 PRE and POST are optional strings to be inserted before and after the
14613 The command returns the inserted time stamp."
14614 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
14616 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
14617 (insert-before-markers (or pre ""))
14618 (when (listp extra)
14619 (setq extra (car extra))
14620 (if (and (stringp extra)
14621 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
14622 (setq extra (format "-%02d:%02d"
14623 (string-to-number (match-string 1 extra))
14624 (string-to-number (match-string 2 extra))))
14627 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
14628 (insert-before-markers (setq stamp (format-time-string fmt time)))
14629 (insert-before-markers (or post ""))
14630 (setq org-last-inserted-timestamp stamp)))
14632 (defun org-toggle-time-stamp-overlays ()
14633 "Toggle the use of custom time stamp formats."
14635 (setq org-display-custom-times (not org-display-custom-times))
14636 (unless org-display-custom-times
14637 (let ((p (point-min)) (bmp (buffer-modified-p)))
14638 (while (setq p (next-single-property-change p 'display))
14639 (if (and (get-text-property p 'display)
14640 (eq (get-text-property p 'face) 'org-date))
14641 (remove-text-properties
14642 p (setq p (next-single-property-change p 'display))
14644 (set-buffer-modified-p bmp)))
14645 (if (featurep 'xemacs)
14646 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
14647 (org-restart-font-lock)
14648 (setq org-table-may-need-update t)
14649 (if org-display-custom-times
14650 (message "Time stamps are overlayed with custom format")
14651 (message "Time stamp overlays removed")))
14653 (defun org-display-custom-time (beg end)
14654 "Overlay modified time stamp format over timestamp between BEG and END."
14655 (let* ((ts (buffer-substring beg end))
14656 t1 w1 with-hm tf time str w2 (off 0))
14658 (setq t1 (org-parse-time-string ts t))
14659 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
14660 (setq off (- (match-end 0) (match-beginning 0)))))
14661 (setq end (- end off))
14662 (setq w1 (- end beg)
14663 with-hm (and (nth 1 t1) (nth 2 t1))
14664 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
14665 time (org-fix-decoded-time t1)
14667 (format-time-string
14668 (substring tf 1 -1) (apply 'encode-time time))
14669 nil 'mouse-face 'highlight)
14671 (if (not (= w2 w1))
14672 (add-text-properties (1+ beg) (+ 2 beg)
14673 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
14674 (if (featurep 'xemacs)
14676 (put-text-property beg end 'invisible t)
14677 (put-text-property beg end 'end-glyph (make-glyph str)))
14678 (put-text-property beg end 'display str))))
14680 (defun org-translate-time (string)
14681 "Translate all timestamps in STRING to custom format.
14682 But do this only if the variable `org-display-custom-times' is set."
14683 (when org-display-custom-times
14686 (re org-ts-regexp-both)
14687 t1 with-hm inactive tf time str beg end)
14688 (while (setq start (string-match re string start))
14689 (setq beg (match-beginning 0)
14691 t1 (save-match-data
14692 (org-parse-time-string (substring string beg end) t))
14693 with-hm (and (nth 1 t1) (nth 2 t1))
14694 inactive (equal (substring string beg (1+ beg)) "[")
14695 tf (funcall (if with-hm 'cdr 'car)
14696 org-time-stamp-custom-formats)
14697 time (org-fix-decoded-time t1)
14698 str (format-time-string
14700 (if inactive "[" "<") (substring tf 1 -1)
14701 (if inactive "]" ">"))
14702 (apply 'encode-time time))
14703 string (replace-match str t t string)
14704 start (+ start (length str)))))))
14707 (defun org-fix-decoded-time (time)
14708 "Set 0 instead of nil for the first 6 elements of time.
14709 Don't touch the rest."
14711 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
14713 (defun org-days-to-time (timestamp-string)
14714 "Difference between TIMESTAMP-STRING and now in days."
14715 (- (time-to-days (org-time-string-to-time timestamp-string))
14716 (time-to-days (current-time))))
14718 (defun org-deadline-close (timestamp-string &optional ndays)
14719 "Is the time in TIMESTAMP-STRING close to the current date?"
14720 (setq ndays (or ndays (org-get-wdays timestamp-string)))
14721 (and (< (org-days-to-time timestamp-string) ndays)
14722 (not (org-entry-is-done-p))))
14724 (defun org-get-wdays (ts)
14725 "Get the deadline lead time appropriate for timestring TS."
14727 ((<= org-deadline-warning-days 0)
14728 ;; 0 or negative, enforce this value no matter what
14729 (- org-deadline-warning-days))
14730 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
14731 ;; lead time is specified.
14732 (floor (* (string-to-number (match-string 1 ts))
14733 (cdr (assoc (match-string 2 ts)
14734 '(("d" . 1) ("w" . 7)
14735 ("m" . 30.4) ("y" . 365.25)))))))
14736 ;; go for the default.
14737 (t org-deadline-warning-days)))
14739 (defun org-calendar-select-mouse (ev)
14740 "Return to `org-read-date' with the date currently selected.
14741 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
14743 (mouse-set-point ev)
14744 (when (calendar-cursor-to-date)
14745 (let* ((date (calendar-cursor-to-date))
14746 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
14747 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
14748 (if (active-minibuffer-window) (exit-minibuffer))))
14750 (defun org-check-deadlines (ndays)
14751 "Check if there are any deadlines due or past due.
14752 A deadline is considered due if it happens within `org-deadline-warning-days'
14753 days from today's date. If the deadline appears in an entry marked DONE,
14754 it is not shown. The prefix arg NDAYS can be used to test that many
14755 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
14757 (let* ((org-warn-days
14759 ((equal ndays '(4)) 100000)
14760 (ndays (prefix-numeric-value ndays))
14761 (t (abs org-deadline-warning-days))))
14762 (case-fold-search nil)
14763 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
14765 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
14767 (message "%d deadlines past-due or due within %d days"
14768 (org-occur regexp nil callback)
14771 (defun org-check-before-date (date)
14772 "Check if there are deadlines or scheduled entries before DATE."
14773 (interactive (list (org-read-date)))
14774 (let ((case-fold-search nil)
14775 (regexp (concat "\\<\\(" org-deadline-string
14776 "\\|" org-scheduled-string
14777 "\\) *<\\([^>]+\\)>"))
14779 (lambda () (time-less-p
14780 (org-time-string-to-time (match-string 2))
14781 (org-time-string-to-time date)))))
14782 (message "%d entries before %s"
14783 (org-occur regexp nil callback) date)))
14785 (defun org-check-after-date (date)
14786 "Check if there are deadlines or scheduled entries after DATE."
14787 (interactive (list (org-read-date)))
14788 (let ((case-fold-search nil)
14789 (regexp (concat "\\<\\(" org-deadline-string
14790 "\\|" org-scheduled-string
14791 "\\) *<\\([^>]+\\)>"))
14795 (org-time-string-to-time (match-string 2))
14796 (org-time-string-to-time date))))))
14797 (message "%d entries after %s"
14798 (org-occur regexp nil callback) date)))
14800 (defun org-evaluate-time-range (&optional to-buffer)
14801 "Evaluate a time range by computing the difference between start and end.
14802 Normally the result is just printed in the echo area, but with prefix arg
14803 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
14804 If the time range is actually in a table, the result is inserted into the
14806 For time difference computation, a year is assumed to be exactly 365
14807 days in order to avoid rounding problems."
14810 (org-clock-update-time-maybe)
14812 (unless (org-at-date-range-p t)
14813 (goto-char (point-at-bol))
14814 (re-search-forward org-tr-regexp-both (point-at-eol) t))
14815 (if (not (org-at-date-range-p t))
14816 (error "Not at a time-stamp range, and none found in current line")))
14817 (let* ((ts1 (match-string 1))
14818 (ts2 (match-string 2))
14819 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
14820 (match-end (match-end 0))
14821 (time1 (org-time-string-to-time ts1))
14822 (time2 (org-time-string-to-time ts2))
14823 (t1 (org-float-time time1))
14824 (t2 (org-float-time time2))
14825 (diff (abs (- t2 t1)))
14826 (negative (< (- t2 t1) 0))
14827 ;; (ys (floor (* 365 24 60 60)))
14830 (fy "%dy %dd %02d:%02d")
14832 (fd "%dd %02d:%02d")
14837 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
14839 d (floor (/ diff ds)) diff (mod diff ds)
14840 h (floor (/ diff hs)) diff (mod diff hs)
14841 m (floor (/ diff 60)))
14842 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
14844 d (floor (+ (/ diff ds) 0.5))
14846 (if (not to-buffer)
14847 (message "%s" (org-make-tdiff-string y d h m))
14848 (if (org-at-table-p)
14850 (goto-char match-end)
14852 (and (looking-at " *|") (goto-char (match-end 0))))
14853 (goto-char match-end))
14855 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
14856 (replace-match ""))
14857 (if negative (insert " -"))
14858 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
14859 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
14860 (insert " " (format fh h m))))
14861 (if align (org-table-align))
14862 (message "Time difference inserted")))))
14864 (defun org-make-tdiff-string (y d h m)
14867 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
14869 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
14871 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
14873 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
14875 (apply 'format fmt (nreverse l))))
14877 (defun org-time-string-to-time (s)
14878 (apply 'encode-time (org-parse-time-string s)))
14879 (defun org-time-string-to-seconds (s)
14880 (org-float-time (org-time-string-to-time s)))
14882 (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
14883 "Convert a time stamp to an absolute day number.
14884 If there is a specifier for a cyclic time stamp, get the closest date to
14886 PREFER and SHOW-ALL are passed through to `org-closest-date'.
14887 the variable date is bound by the calendar when this is called."
14889 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
14890 (if (org-diary-sexp-entry (match-string 1 s) "" date)
14893 ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
14894 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
14895 (time-to-days (current-time))) (match-string 0 s)
14897 (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
14899 (defun org-days-to-iso-week (days)
14900 "Return the iso week number."
14902 (car (calendar-iso-from-absolute days)))
14904 (defun org-small-year-to-year (year)
14905 "Convert 2-digit years into 4-digit years.
14906 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
14907 The year 2000 cannot be abbreviated. Any year larger than 99
14908 is returned unchanged."
14910 (setq year (+ 2000 year))
14912 (setq year (+ 1900 year))))
14915 (defun org-time-from-absolute (d)
14916 "Return the time corresponding to date D.
14917 D may be an absolute day number, or a calendar-type list (month day year)."
14918 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
14919 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
14921 (defun org-calendar-holiday ()
14922 "List of holidays, for Diary display in Org-mode."
14923 (require 'holidays)
14925 (if (fboundp 'calendar-check-holidays)
14926 'calendar-check-holidays 'check-calendar-holidays) date)))
14927 (if hl (mapconcat 'identity hl "; "))))
14929 (defun org-diary-sexp-entry (sexp entry date)
14930 "Process a SEXP diary ENTRY for DATE."
14931 (require 'diary-lib)
14932 (let ((result (if calendar-debug-sexp
14933 (let ((stack-trace-on-error t))
14934 (eval (car (read-from-string sexp))))
14935 (condition-case nil
14936 (eval (car (read-from-string sexp)))
14939 (message "Bad sexp at line %d in %s: %s"
14941 (buffer-file-name) sexp)
14943 (cond ((stringp result) result)
14944 ((and (consp result)
14945 (stringp (cdr result))) (cdr result))
14949 (defun org-diary-to-ical-string (frombuf)
14950 "Get iCalendar entries from diary entries in buffer FROMBUF.
14951 This uses the icalendar.el library."
14952 (let* ((tmpdir (if (featurep 'xemacs)
14954 temporary-file-directory))
14955 (tmpfile (make-temp-name
14956 (expand-file-name "orgics" tmpdir)))
14958 (with-current-buffer frombuf
14959 (icalendar-export-region (point-min) (point-max) tmpfile)
14960 (setq buf (find-buffer-visiting tmpfile))
14962 (goto-char (point-min))
14963 (if (re-search-forward "^BEGIN:VEVENT" nil t)
14964 (setq b (match-beginning 0)))
14965 (goto-char (point-max))
14966 (if (re-search-backward "^END:VEVENT" nil t)
14967 (setq e (match-end 0)))
14968 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
14970 (delete-file tmpfile)
14973 (defun org-closest-date (start current change prefer show-all)
14974 "Find the date closest to CURRENT that is consistent with START and CHANGE.
14975 When PREFER is `past' return a date that is either CURRENT or past.
14976 When PREFER is `future', return a date that is either CURRENT or future.
14977 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
14978 ;; Make the proper lists from the dates
14980 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
14981 dn dw sday cday n1 n2 n0
14982 d m y y1 y2 date1 date2 nmonths nm ny m2)
14984 (setq start (org-date-to-gregorian start)
14985 current (org-date-to-gregorian
14988 (time-to-days (current-time))))
14989 sday (calendar-absolute-from-gregorian start)
14990 cday (calendar-absolute-from-gregorian current))
14992 (if (<= cday sday) (throw 'exit sday))
14994 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
14995 (setq dn (string-to-number (match-string 1 change))
14996 dw (cdr (assoc (match-string 2 change) a1)))
14997 (error "Invalid change specifier: %s" change))
14998 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
15001 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
15004 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
15005 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
15006 (setq date1 (list m d y1)
15007 n1 (calendar-absolute-from-gregorian date1)
15008 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
15009 n2 (calendar-absolute-from-gregorian date2)))
15011 ;; approx number of month between the two dates
15012 (setq nmonths (floor (/ (- cday sday) 30.436875)))
15013 ;; How often does dn fit in there?
15014 (setq d (nth 1 start) m (car start) y (nth 2 start)
15015 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
15017 ny (floor (/ m 12))
15020 (while (> m 12) (setq m (- m 12) y (1+ y)))
15021 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
15022 (setq m2 (+ m dn) y2 y)
15023 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
15024 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
15025 (while (<= n2 cday)
15026 (setq n1 n2 m m2 y y2)
15027 (setq m2 (+ m dn) y2 y)
15028 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
15029 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
15030 ;; Make sure n1 is the earlier date
15031 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
15034 ((eq prefer 'past) (if (= cday n2) n2 n1))
15035 ((eq prefer 'future) (if (= cday n1) n1 n2))
15036 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
15038 ((eq prefer 'past) (if (= cday n2) n2 n1))
15039 ((eq prefer 'future) (if (= cday n1) n1 n2))
15040 (t (if (= cday n1) n1 n2)))))))
15042 (defun org-date-to-gregorian (date)
15043 "Turn any specification of DATE into a Gregorian date for the calendar."
15044 (cond ((integerp date) (calendar-gregorian-from-absolute date))
15045 ((and (listp date) (= (length date) 3)) date)
15047 (setq date (org-parse-time-string date))
15048 (list (nth 4 date) (nth 3 date) (nth 5 date)))
15050 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
15052 (defun org-parse-time-string (s &optional nodefault)
15053 "Parse the standard Org-mode time string.
15054 This should be a lot faster than the normal `parse-time-string'.
15055 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
15056 hour and minute fields will be nil if not given."
15057 (if (string-match org-ts-regexp0 s)
15059 (if (or (match-beginning 8) (not nodefault))
15060 (string-to-number (or (match-string 8 s) "0")))
15061 (if (or (match-beginning 7) (not nodefault))
15062 (string-to-number (or (match-string 7 s) "0")))
15063 (string-to-number (match-string 4 s))
15064 (string-to-number (match-string 3 s))
15065 (string-to-number (match-string 2 s))
15067 (error "Not a standard Org-mode time string: %s" s)))
15069 (defun org-timestamp-up (&optional arg)
15070 "Increase the date item at the cursor by one.
15071 If the cursor is on the year, change the year. If it is on the month or
15072 the day, change that.
15073 With prefix ARG, change by that many units."
15075 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
15077 (defun org-timestamp-down (&optional arg)
15078 "Decrease the date item at the cursor by one.
15079 If the cursor is on the year, change the year. If it is on the month or
15080 the day, change that.
15081 With prefix ARG, change by that many units."
15083 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
15085 (defun org-timestamp-up-day (&optional arg)
15086 "Increase the date in the time stamp by one day.
15087 With prefix ARG, change that many days."
15089 (if (and (not (org-at-timestamp-p t))
15090 (org-on-heading-p))
15092 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
15094 (defun org-timestamp-down-day (&optional arg)
15095 "Decrease the date in the time stamp by one day.
15096 With prefix ARG, change that many days."
15098 (if (and (not (org-at-timestamp-p t))
15099 (org-on-heading-p))
15101 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
15103 (defun org-at-timestamp-p (&optional inactive-ok)
15104 "Determine if the cursor is in or at a timestamp."
15106 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
15108 (ans (or (looking-at tsr)
15110 (skip-chars-backward "^[<\n\r\t")
15111 (if (> (point) (point-min)) (backward-char 1))
15112 (and (looking-at tsr)
15113 (> (- (match-end 0) pos) -1))))))
15115 (boundp 'org-ts-what)
15118 ((= pos (match-beginning 0)) 'bracket)
15119 ((= pos (1- (match-end 0))) 'bracket)
15120 ((org-pos-in-match-range pos 2) 'year)
15121 ((org-pos-in-match-range pos 3) 'month)
15122 ((org-pos-in-match-range pos 7) 'hour)
15123 ((org-pos-in-match-range pos 8) 'minute)
15124 ((or (org-pos-in-match-range pos 4)
15125 (org-pos-in-match-range pos 5)) 'day)
15126 ((and (> pos (or (match-end 8) (match-end 5)))
15127 (< pos (match-end 0)))
15128 (- pos (or (match-end 8) (match-end 5))))
15132 (defun org-toggle-timestamp-type ()
15133 "Toggle the type (<active> or [inactive]) of a time stamp."
15135 (when (org-at-timestamp-p t)
15136 (let ((beg (match-beginning 0)) (end (match-end 0))
15137 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
15140 (while (re-search-forward "[][<>]" end t)
15141 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
15143 (message "Timestamp is now %sactive"
15144 (if (equal (char-after beg) ?<) "" "in")))))
15146 (defun org-timestamp-change (n &optional what updown)
15147 "Change the date in the time stamp at point.
15148 The date will be changed by N times WHAT. WHAT can be `day', `month',
15149 `year', `minute', `second'. If WHAT is not given, the cursor position
15150 in the timestamp determines what will be changed."
15151 (let ((pos (point))
15153 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
15157 (if (not (org-at-timestamp-p t))
15158 (error "Not at a timestamp"))
15159 (if (and (not what) (eq org-ts-what 'bracket))
15160 (org-toggle-timestamp-type)
15161 (if (and (not what) (not (eq org-ts-what 'day))
15162 org-display-custom-times
15163 (get-text-property (point) 'display)
15164 (not (get-text-property (1- (point)) 'display)))
15165 (setq org-ts-what 'day))
15166 (setq org-ts-what (or what org-ts-what)
15167 inactive (= (char-after (match-beginning 0)) ?\[)
15168 ts (match-string 0))
15171 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
15173 (setq extra (match-string 1 ts)))
15174 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
15176 (setq time0 (org-parse-time-string ts))
15178 (eq org-ts-what 'minute)
15179 (not current-prefix-arg))
15180 ;; This looks like s-up and s-down. Change by one rounding step.
15181 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
15182 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
15183 (setcar (cdr time0) (+ (nth 1 time0)
15184 (if (> n 0) (- rem) (- dm rem))))))
15186 (encode-time (or (car time0) 0)
15187 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
15188 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
15189 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
15190 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
15191 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
15193 (when (and (member org-ts-what '(hour minute))
15195 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
15196 (setq extra (org-modify-ts-extra
15198 (if (eq org-ts-what 'hour) 2 5)
15200 (when (integerp org-ts-what)
15201 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
15202 (if (eq what 'calendar)
15203 (let ((cal-date (org-get-date-from-calendar)))
15204 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
15205 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
15206 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
15207 (setcar time0 (or (car time0) 0))
15208 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
15209 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
15210 (setq time (apply 'encode-time time0))))
15211 (setq org-last-changed-timestamp
15212 (org-insert-time-stamp time with-hm inactive nil nil extra))
15213 (org-clock-update-time-maybe)
15215 ;; Try to recenter the calendar window, if any
15216 (if (and org-calendar-follow-timestamp-change
15217 (get-buffer-window "*Calendar*" t)
15218 (memq org-ts-what '(day month year)))
15219 (org-recenter-calendar (time-to-days time))))))
15221 (defun org-modify-ts-extra (s pos n dm)
15222 "Change the different parts of the lead-time and repeat fields in timestamp."
15223 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
15225 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
15227 ((or (org-pos-in-match-range pos 2)
15228 (org-pos-in-match-range pos 3))
15229 (setq m (string-to-number (match-string 3 s))
15230 h (string-to-number (match-string 2 s)))
15231 (if (org-pos-in-match-range pos 2)
15233 (setq n (* dm (org-no-warnings (signum n))))
15234 (when (not (= 0 (setq rem (% m dm))))
15235 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
15237 (if (< m 0) (setq m (+ m 60) h (1- h)))
15238 (if (> m 59) (setq m (- m 60) h (1+ h)))
15239 (setq h (min 24 (max 0 h)))
15240 (setq ng 1 new (format "-%02d:%02d" h m)))
15241 ((org-pos-in-match-range pos 6)
15242 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
15243 ((org-pos-in-match-range pos 5)
15244 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
15246 ((org-pos-in-match-range pos 9)
15247 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
15248 ((org-pos-in-match-range pos 8)
15249 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
15253 (substring s 0 (match-beginning ng))
15255 (substring s (match-end ng))))))
15258 (defun org-recenter-calendar (date)
15259 "If the calendar is visible, recenter it to DATE."
15260 (let* ((win (selected-window))
15261 (cwin (get-buffer-window "*Calendar*" t))
15262 (calendar-move-hook nil))
15264 (select-window cwin)
15265 (calendar-goto-date (if (listp date) date
15266 (calendar-gregorian-from-absolute date)))
15267 (select-window win))))
15269 (defun org-goto-calendar (&optional arg)
15270 "Go to the Emacs calendar at the current date.
15271 If there is a time stamp in the current line, go to that date.
15272 A prefix ARG can be used to force the current date."
15274 (let ((tsr org-ts-regexp) diff
15275 (calendar-move-hook nil)
15276 (calendar-view-holidays-initially-flag nil)
15277 (calendar-view-diary-initially-flag nil))
15278 (if (or (org-at-timestamp-p)
15280 (beginning-of-line 1)
15281 (looking-at (concat ".*" tsr))))
15282 (let ((d1 (time-to-days (current-time)))
15284 (org-time-string-to-time (match-string 1)))))
15285 (setq diff (- d2 d1))))
15287 (calendar-goto-today)
15288 (if (and diff (not arg)) (calendar-forward-day diff))))
15290 (defun org-get-date-from-calendar ()
15291 "Return a list (month day year) of date at point in calendar."
15292 (with-current-buffer "*Calendar*"
15294 (calendar-cursor-to-date))))
15296 (defun org-date-from-calendar ()
15297 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
15298 If there is already a time stamp at the cursor position, update it."
15300 (if (org-at-timestamp-p t)
15301 (org-timestamp-change 0 'calendar)
15302 (let ((cal-date (org-get-date-from-calendar)))
15303 (org-insert-time-stamp
15304 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
15306 (defun org-minutes-to-hh:mm-string (m)
15307 "Compute H:MM from a number of minutes."
15308 (let ((h (/ m 60)))
15309 (setq m (- m (* 60 h)))
15310 (format org-time-clocksum-format h m)))
15312 (defun org-hh:mm-string-to-minutes (s)
15313 "Convert a string H:MM to a number of minutes.
15314 If the string is just a number, interpret it as minutes.
15315 In fact, the first hh:mm or number in the string will be taken,
15316 there can be extra stuff in the string.
15317 If no number is found, the return value is 0."
15319 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
15320 (+ (* (string-to-number (match-string 1 s)) 60)
15321 (string-to-number (match-string 2 s))))
15322 ((string-match "\\([0-9]+\\)" s)
15323 (string-to-number (match-string 1 s)))
15328 (defun org-save-all-org-buffers ()
15329 "Save all Org-mode buffers without user confirmation."
15331 (message "Saving all Org-mode buffers...")
15332 (save-some-buffers t 'org-mode-p)
15333 (when (featurep 'org-id) (org-id-locations-save))
15334 (message "Saving all Org-mode buffers... done"))
15336 (defun org-revert-all-org-buffers ()
15337 "Revert all Org-mode buffers.
15338 Prompt for confirmation when there are unsaved changes.
15339 Be sure you know what you are doing before letting this function
15340 overwrite your changes.
15342 This function is useful in a setup where one tracks org files
15343 with a version control system, to revert on one machine after pulling
15344 changes from another. I believe the procedure must be like this:
15346 1. M-x org-save-all-org-buffers
15347 2. Pull changes from the other machine, resolve conflicts
15348 3. M-x org-revert-all-org-buffers"
15350 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
15353 (save-window-excursion
15356 (when (and (with-current-buffer b (org-mode-p))
15357 (with-current-buffer b buffer-file-name))
15358 (switch-to-buffer b)
15359 (revert-buffer t 'no-confirm)))
15361 (when (and (featurep 'org-id) org-id-track-globally)
15362 (org-id-locations-load)))))
15367 (defun org-switchb (&optional arg)
15368 "Switch between Org buffers.
15369 With a prefix argument, restrict available to files.
15370 With two prefix arguments, restrict available buffers to agenda files.
15372 Defaults to `iswitchb' for buffer name completion.
15373 Set `org-completion-use-ido' to make it use ido instead."
15375 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
15376 ((equal arg '(16)) (org-buffer-list 'agenda))
15377 (t (org-buffer-list))))
15378 (org-completion-use-iswitchb org-completion-use-iswitchb)
15379 (org-completion-use-ido org-completion-use-ido))
15380 (unless (or org-completion-use-ido org-completion-use-iswitchb)
15381 (setq org-completion-use-iswitchb t))
15383 (org-icompleting-read "Org buffer: "
15384 (mapcar 'list (mapcar 'buffer-name blist))
15387 ;;; Define some older names previously used for this functionality
15389 (defalias 'org-ido-switchb 'org-switchb)
15391 (defalias 'org-iswitchb 'org-switchb)
15393 (defun org-buffer-list (&optional predicate exclude-tmp)
15394 "Return a list of Org buffers.
15395 PREDICATE can be `export', `files' or `agenda'.
15397 export restrict the list to Export buffers.
15398 files restrict the list to buffers visiting Org files.
15399 agenda restrict the list to buffers visiting agenda files.
15401 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
15403 (agenda-files (and (eq predicate 'agenda)
15404 (mapcar 'file-truename (org-agenda-files t))))
15407 ((eq predicate 'files)
15408 (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
15409 ((eq predicate 'export)
15410 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
15411 ((eq predicate 'agenda)
15413 (with-current-buffer b
15414 (and (eq major-mode 'org-mode)
15415 (setq bfn (buffer-file-name b))
15416 (member (file-truename bfn) agenda-files)))))
15417 (t (lambda (b) (with-current-buffer b
15418 (or (eq major-mode 'org-mode)
15419 (string-match "\*Org .*Export"
15420 (buffer-name b)))))))))
15424 (if (and (funcall filter b)
15425 (or (not exclude-tmp)
15426 (not (string-match "tmp" (buffer-name b)))))
15431 (defun org-agenda-files (&optional unrestricted archives)
15432 "Get the list of agenda files.
15433 Optional UNRESTRICTED means return the full list even if a restriction
15434 is currently in place.
15435 When ARCHIVES is t, include all archive files that are really being
15436 used by the agenda files. If ARCHIVE is `ifmode', do this only if
15437 `org-agenda-archives-mode' is t."
15440 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
15441 ((stringp org-agenda-files) (org-read-agenda-file-list))
15442 ((listp org-agenda-files) org-agenda-files)
15443 (t (error "Invalid value of `org-agenda-files'")))))
15444 (setq files (apply 'append
15445 (mapcar (lambda (f)
15446 (if (file-directory-p f)
15448 f t org-agenda-file-regexp)
15451 (when org-agenda-skip-unavailable-files
15452 (setq files (delq nil
15455 (and (file-readable-p file) file)))
15457 (when (or (eq archives t)
15458 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
15459 (setq files (org-add-archive-files files)))
15462 (defun org-agenda-file-p (&optional file)
15463 "Return non-nil, if FILE is an agenda file.
15464 If FILE is omitted, use the file associated with the current
15466 (member (or file (buffer-file-name))
15467 (org-agenda-files t)))
15469 (defun org-edit-agenda-file-list ()
15470 "Edit the list of agenda files.
15471 Depending on setup, this either uses customize to edit the variable
15472 `org-agenda-files', or it visits the file that is holding the list. In the
15473 latter case, the buffer is set up in a way that saving it automatically kills
15474 the buffer and restores the previous window configuration."
15476 (if (stringp org-agenda-files)
15477 (let ((cw (current-window-configuration)))
15478 (find-file org-agenda-files)
15479 (org-set-local 'org-window-configuration cw)
15480 (org-add-hook 'after-save-hook
15482 (set-window-configuration
15483 (prog1 org-window-configuration
15484 (kill-buffer (current-buffer))))
15485 (org-install-agenda-files-menu)
15486 (message "New agenda file list installed"))
15488 (message "%s" (substitute-command-keys
15489 "Edit list and finish with \\[save-buffer]")))
15490 (customize-variable 'org-agenda-files)))
15492 (defun org-store-new-agenda-file-list (list)
15493 "Set new value for the agenda file list and save it correctly."
15494 (if (stringp org-agenda-files)
15495 (let ((fe (org-read-agenda-file-list t)) b u)
15496 (while (setq b (find-buffer-visiting org-agenda-files))
15498 (with-temp-file org-agenda-files
15501 (lambda (f) ;; Keep un-expanded entries.
15502 (if (setq u (assoc f fe))
15507 (let ((org-mode-hook nil) (org-inhibit-startup t)
15508 (org-insert-mode-line-in-empty-file nil))
15509 (setq org-agenda-files list)
15510 (customize-save-variable 'org-agenda-files org-agenda-files))))
15512 (defun org-read-agenda-file-list (&optional pair-with-expansion)
15513 "Read the list of agenda files from a file.
15514 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
15515 filenames, used by `org-store-new-agenda-file-list' to write back
15516 un-expanded file names."
15517 (when (file-directory-p org-agenda-files)
15518 (error "`org-agenda-files' cannot be a single directory"))
15519 (when (stringp org-agenda-files)
15521 (insert-file-contents org-agenda-files)
15524 (let ((e (expand-file-name (substitute-in-file-name f)
15526 (if pair-with-expansion
15529 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
15532 (defun org-cycle-agenda-files ()
15533 "Cycle through the files in `org-agenda-files'.
15534 If the current buffer visits an agenda file, find the next one in the list.
15535 If the current buffer does not, find the first agenda file."
15537 (let* ((fs (org-agenda-files t))
15538 (files (append fs (list (car fs))))
15539 (tcf (if buffer-file-name (file-truename buffer-file-name)))
15541 (unless files (error "No agenda files"))
15543 (while (setq file (pop files))
15544 (if (equal (file-truename file) tcf)
15546 (find-file (car files))
15548 (find-file (car fs)))
15549 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
15551 (defun org-agenda-file-to-front (&optional to-end)
15552 "Move/add the current file to the top of the agenda file list.
15553 If the file is not present in the list, it is added to the front. If it is
15554 present, it is moved there. With optional argument TO-END, add/move to the
15557 (let ((org-agenda-skip-unavailable-files nil)
15558 (file-alist (mapcar (lambda (x)
15559 (cons (file-truename x) x))
15560 (org-agenda-files t)))
15561 (ctf (file-truename buffer-file-name))
15563 (setq x (assoc ctf file-alist) had x)
15565 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
15567 (setq file-alist (append (delq x file-alist) (list x)))
15568 (setq file-alist (cons x (delq x file-alist))))
15569 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
15570 (org-install-agenda-files-menu)
15571 (message "File %s to %s of agenda file list"
15572 (if had "moved" "added") (if to-end "end" "front"))))
15574 (defun org-remove-file (&optional file)
15575 "Remove current file from the list of files in variable `org-agenda-files'.
15576 These are the files which are being checked for agenda entries.
15577 Optional argument FILE means use this file instead of the current."
15579 (let* ((org-agenda-skip-unavailable-files nil)
15580 (file (or file buffer-file-name))
15581 (true-file (file-truename file))
15582 (afile (abbreviate-file-name file))
15583 (files (delq nil (mapcar
15585 (if (equal true-file
15588 (org-agenda-files t)))))
15589 (if (not (= (length files) (length (org-agenda-files t))))
15591 (org-store-new-agenda-file-list files)
15592 (org-install-agenda-files-menu)
15593 (message "Removed file: %s" afile))
15594 (message "File was not in list: %s (not removed)" afile))))
15596 (defun org-file-menu-entry (file)
15597 (vector file (list 'find-file file) t))
15599 (defun org-check-agenda-file (file)
15600 "Make sure FILE exists. If not, ask user what to do."
15601 (when (not (file-exists-p file))
15602 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
15603 (abbreviate-file-name file))
15604 (let ((r (downcase (read-char-exclusive))))
15607 (org-remove-file file)
15608 (throw 'nextfile t))
15609 (t (error "Abort"))))))
15611 (defun org-get-agenda-file-buffer (file)
15612 "Get a buffer visiting FILE. If the buffer needs to be created, add
15613 it to the list of buffers which might be released later."
15614 (let ((buf (org-find-base-buffer-visiting file)))
15616 buf ; just return it
15617 ;; Make a new buffer and remember it
15618 (setq buf (find-file-noselect file))
15619 (if buf (push buf org-agenda-new-buffers))
15622 (defun org-release-buffers (blist)
15623 "Release all buffers in list, asking the user for confirmation when needed.
15624 When a buffer is unmodified, it is just killed. When modified, it is saved
15625 \(if the user agrees) and then killed."
15627 (while (setq buf (pop blist))
15628 (setq file (buffer-file-name buf))
15629 (when (and (buffer-modified-p buf)
15631 (y-or-n-p (format "Save file %s? " file)))
15632 (with-current-buffer buf (save-buffer)))
15633 (kill-buffer buf))))
15635 (defun org-prepare-agenda-buffers (files)
15636 "Create buffers for all agenda files, protect archived trees and comments."
15638 (let ((pa '(:org-archived t))
15639 (pc '(:org-comment t))
15640 (pall '(:org-archived t :org-comment t))
15641 (inhibit-read-only t)
15642 (rea (concat ":" org-archive-tag ":"))
15646 (while (setq file (pop files))
15650 (org-check-agenda-file file)
15651 (set-buffer (org-get-agenda-file-buffer file)))
15653 (setq bmp (buffer-modified-p))
15654 (org-refresh-category-properties)
15655 (setq org-todo-keywords-for-agenda
15656 (append org-todo-keywords-for-agenda org-todo-keywords-1))
15657 (setq org-done-keywords-for-agenda
15658 (append org-done-keywords-for-agenda org-done-keywords))
15659 (setq org-todo-keyword-alist-for-agenda
15660 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
15661 (setq org-drawers-for-agenda
15662 (append org-drawers-for-agenda org-drawers))
15663 (setq org-tag-alist-for-agenda
15664 (append org-tag-alist-for-agenda org-tag-alist))
15667 (remove-text-properties (point-min) (point-max) pall)
15668 (when org-agenda-skip-archived-trees
15669 (goto-char (point-min))
15670 (while (re-search-forward rea nil t)
15671 (if (org-on-heading-p t)
15672 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
15673 (goto-char (point-min))
15674 (setq re (concat "^\\*+ +" org-comment-string "\\>"))
15675 (while (re-search-forward re nil t)
15676 (add-text-properties
15677 (match-beginning 0) (org-end-of-subtree t) pc)))
15678 (set-buffer-modified-p bmp)))))
15679 (setq org-todo-keywords-for-agenda
15680 (org-uniquify org-todo-keywords-for-agenda))
15681 (setq org-todo-keyword-alist-for-agenda
15682 (org-uniquify org-todo-keyword-alist-for-agenda)
15683 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
15685 ;;;; Embedded LaTeX
15687 (defvar org-cdlatex-mode-map (make-sparse-keymap)
15688 "Keymap for the minor `org-cdlatex-mode'.")
15690 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
15691 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
15692 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
15693 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
15694 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
15696 (defvar org-cdlatex-texmathp-advice-is-done nil
15697 "Flag remembering if we have applied the advice to texmathp already.")
15699 (define-minor-mode org-cdlatex-mode
15700 "Toggle the minor `org-cdlatex-mode'.
15701 This mode supports entering LaTeX environment and math in LaTeX fragments
15703 \\{org-cdlatex-mode-map}"
15705 (when org-cdlatex-mode (require 'cdlatex))
15706 (unless org-cdlatex-texmathp-advice-is-done
15707 (setq org-cdlatex-texmathp-advice-is-done t)
15708 (defadvice texmathp (around org-math-always-on activate)
15709 "Always return t in org-mode buffers.
15710 This is because we want to insert math symbols without dollars even outside
15711 the LaTeX math segments. If Orgmode thinks that point is actually inside
15712 an embedded LaTeX fragment, let texmathp do its job.
15713 \\[org-cdlatex-mode-map]"
15717 ((not (org-mode-p)) ad-do-it)
15718 ((eq this-command 'cdlatex-math-symbol)
15719 (setq ad-return-value t
15720 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
15722 (let ((p (org-inside-LaTeX-fragment-p)))
15723 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
15724 (setq ad-return-value t
15725 texmathp-why '("Org-mode embedded math" . 0))
15726 (if p ad-do-it)))))))))
15728 (defun turn-on-org-cdlatex ()
15729 "Unconditionally turn on `org-cdlatex-mode'."
15730 (org-cdlatex-mode 1))
15732 (defun org-inside-LaTeX-fragment-p ()
15733 "Test if point is inside a LaTeX fragment.
15734 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
15735 sequence appearing also before point.
15736 Even though the matchers for math are configurable, this function assumes
15737 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
15738 delimiters are skipped when they have been removed by customization.
15739 The return value is nil, or a cons cell with the delimiter and
15740 and the position of this delimiter.
15742 This function does a reasonably good job, but can locally be fooled by
15743 for example currency specifications. For example it will assume being in
15744 inline math after \"$22.34\". The LaTeX fragment formatter will only format
15745 fragments that are properly closed, but during editing, we have to live
15746 with the uncertainty caused by missing closing delimiters. This function
15747 looks only before point, not after."
15749 (let ((pos (point))
15750 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
15752 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
15754 dd-on str (start 0) m re)
15757 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
15758 re (nth 1 (assoc "$" org-latex-regexps)))
15759 (while (string-match re str start)
15761 ((= (match-end 0) (length str))
15762 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
15763 ((= (match-end 0) (- (length str) 5))
15765 (t (setq start (match-end 0))))))
15766 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
15768 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
15769 (and (match-beginning 2) (throw 'exit nil))
15771 (while (re-search-backward "\\$\\$" lim t)
15772 (setq dd-on (not dd-on)))
15774 (if dd-on (cons "$$" m))))))
15776 (defun org-inside-latex-macro-p ()
15777 "Is point inside a LaTeX macro or its arguments?"
15780 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
15782 (defun org-try-cdlatex-tab ()
15783 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
15784 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
15785 - inside a LaTeX fragment, or
15786 - after the first word in a line, where an abbreviation expansion could
15787 insert a LaTeX environment."
15788 (when org-cdlatex-mode
15791 (skip-chars-backward "a-zA-Z0-9*")
15792 (skip-chars-backward " \t")
15795 ((org-inside-LaTeX-fragment-p)
15799 (defun org-cdlatex-underscore-caret (&optional arg)
15800 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
15801 Revert to the normal definition outside of these fragments."
15803 (if (org-inside-LaTeX-fragment-p)
15804 (call-interactively 'cdlatex-sub-superscript)
15805 (let (org-cdlatex-mode)
15806 (call-interactively (key-binding (vector last-input-event))))))
15808 (defun org-cdlatex-math-modify (&optional arg)
15809 "Execute `cdlatex-math-modify' in LaTeX fragments.
15810 Revert to the normal definition outside of these fragments."
15812 (if (org-inside-LaTeX-fragment-p)
15813 (call-interactively 'cdlatex-math-modify)
15814 (let (org-cdlatex-mode)
15815 (call-interactively (key-binding (vector last-input-event))))))
15817 (defvar org-latex-fragment-image-overlays nil
15818 "List of overlays carrying the images of latex fragments.")
15819 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
15821 (defun org-remove-latex-fragment-image-overlays ()
15822 "Remove all overlays with LaTeX fragment images in current buffer."
15823 (mapc 'delete-overlay org-latex-fragment-image-overlays)
15824 (setq org-latex-fragment-image-overlays nil))
15826 (defun org-preview-latex-fragment (&optional subtree)
15827 "Preview the LaTeX fragment at point, or all locally or globally.
15828 If the cursor is in a LaTeX fragment, create the image and overlay
15829 it over the source code. If there is no fragment at point, display
15830 all fragments in the current text, from one headline to the next. With
15831 prefix SUBTREE, display all fragments in the current subtree. With a
15832 double prefix arg \\[universal-argument] \\[universal-argument], or when \
15833 the cursor is before the first headline,
15834 display all fragments in the buffer.
15835 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
15837 (org-remove-latex-fragment-image-overlays)
15840 (let (beg end at msg)
15842 ((or (equal subtree '(16))
15843 (not (save-excursion
15844 (re-search-backward (concat "^" outline-regexp) nil t))))
15845 (setq beg (point-min) end (point-max)
15846 msg "Creating images for buffer...%s"))
15847 ((equal subtree '(4))
15848 (org-back-to-heading)
15849 (setq beg (point) end (org-end-of-subtree t)
15850 msg "Creating images for subtree...%s"))
15852 (if (setq at (org-inside-LaTeX-fragment-p))
15853 (goto-char (max (point-min) (- (cdr at) 2)))
15854 (org-back-to-heading))
15855 (setq beg (point) end (progn (outline-next-heading) (point))
15856 msg (if at "Creating image...%s"
15857 "Creating images for entry...%s"))))
15859 (narrow-to-region beg end)
15862 (concat "ltxpng/" (file-name-sans-extension
15863 (file-name-nondirectory
15864 buffer-file-name)))
15865 default-directory 'overlays msg at 'forbuffer 'dvipng)
15866 (message msg "done. Use `C-c C-c' to remove images.")))))
15868 (defvar org-latex-regexps
15869 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
15870 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
15871 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
15872 ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
15873 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
15874 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
15875 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
15876 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
15877 "Regular expressions for matching embedded LaTeX.")
15879 (defvar org-export-have-math nil) ;; dynamic scoping
15880 (defun org-format-latex (prefix &optional dir overlays msg at
15881 forbuffer processing-type)
15882 "Replace LaTeX fragments with links to an image, and produce images.
15883 Some of the options can be changed using the variable
15884 `org-format-latex-options'."
15885 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
15886 (let* ((prefixnodir (file-name-nondirectory prefix))
15887 (absprefix (expand-file-name prefix dir))
15888 (todir (file-name-directory absprefix))
15889 (opt org-format-latex-options)
15890 (matchers (plist-get opt :matchers))
15891 (re-list org-latex-regexps)
15892 (org-format-latex-header-extra
15893 (plist-get (org-infile-export-plist) :latex-header-extra))
15894 (cnt 0) txt hash link beg end re e checkdir
15895 executables-checked string
15896 m n block linkfile movefile ov)
15897 ;; Check the different regular expressions
15898 (while (setq e (pop re-list))
15899 (setq m (car e) re (nth 1 e) n (nth 2 e)
15900 block (if (nth 3 e) "\n\n" ""))
15901 (when (member m matchers)
15902 (goto-char (point-min))
15903 (while (re-search-forward re nil t)
15904 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
15905 (not (get-text-property (match-beginning n)
15908 (not (eq (get-char-property (match-beginning n)
15910 'org-latex-overlay))))
15911 (setq org-export-have-math t)
15913 ((eq processing-type 'verbatim)
15914 ;; Leave the text verbatim, just protect it
15915 (add-text-properties (match-beginning n) (match-end n)
15916 '(org-protected t)))
15917 ((eq processing-type 'mathjax)
15918 ;; Prepare for MathJax processing
15919 (setq string (match-string n))
15922 (delete-region (match-beginning n) (match-end n))
15923 (goto-char (match-beginning n))
15924 (insert (org-add-props (concat "\\(" (substring string 1 -1)
15926 '(org-protected t))))
15927 (add-text-properties (match-beginning n) (match-end n)
15928 '(org-protected t))))
15929 ((or (eq processing-type 'dvipng) t)
15930 ;; Process to an image
15931 (setq txt (match-string n)
15932 beg (match-beginning n) end (match-end n)
15934 (let (print-length print-level) ; make sure full list is printed
15935 (setq hash (sha1 (prin1-to-string
15936 (list org-format-latex-header
15937 org-format-latex-header-extra
15938 org-export-latex-default-packages-alist
15939 org-export-latex-packages-alist
15940 org-format-latex-options
15942 linkfile (format "%s_%s.png" prefix hash)
15943 movefile (format "%s_%s.png" absprefix hash)))
15944 (setq link (concat block "[[file:" linkfile "]]" block))
15945 (if msg (message msg cnt))
15947 (unless checkdir ; make sure the directory exists
15949 (or (file-directory-p todir) (make-directory todir t)))
15951 (unless executables-checked
15952 (org-check-external-command
15953 "latex" "needed to convert LaTeX fragments to images")
15954 (org-check-external-command
15955 "dvipng" "needed to convert LaTeX fragments to images")
15956 (setq executables-checked t))
15958 (unless (file-exists-p movefile)
15959 (org-create-formula-image
15960 txt movefile opt forbuffer))
15964 (if (eq (overlay-get o 'org-overlay-type)
15965 'org-latex-overlay)
15966 (delete-overlay o)))
15967 (overlays-in beg end))
15968 (setq ov (make-overlay beg end))
15969 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
15970 (if (featurep 'xemacs)
15972 (overlay-put ov 'invisible t)
15975 (make-glyph (vector 'png :file movefile))))
15978 (list 'image :type 'png :file movefile :ascent 'center)))
15979 (push ov org-latex-fragment-image-overlays)
15981 (delete-region beg end)
15982 (insert (org-add-props link
15983 (list 'org-latex-src
15984 (replace-regexp-in-string
15985 "\"" "" txt)))))))))))))
15987 ;; This function borrows from Ganesh Swami's latex2png.el
15988 (defun org-create-formula-image (string tofile options buffer)
15989 "This calls dvipng."
15990 (require 'org-latex)
15991 (let* ((tmpdir (if (featurep 'xemacs)
15993 temporary-file-directory))
15994 (texfilebase (make-temp-name
15995 (expand-file-name "orgtex" tmpdir)))
15996 (texfile (concat texfilebase ".tex"))
15997 (dvifile (concat texfilebase ".dvi"))
15998 (pngfile (concat texfilebase ".png"))
15999 (fnh (if (featurep 'xemacs)
16000 (font-height (get-face-font 'default))
16001 (face-attribute 'default :height nil)))
16002 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
16003 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
16004 (fg (or (plist-get options (if buffer :foreground :html-foreground))
16006 (bg (or (plist-get options (if buffer :background :html-background))
16008 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
16009 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
16010 (with-temp-file texfile
16011 (insert (org-splice-latex-header
16012 org-format-latex-header
16013 org-export-latex-default-packages-alist
16014 org-export-latex-packages-alist t
16015 org-format-latex-header-extra))
16016 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
16017 (require 'org-latex)
16018 (org-export-latex-fix-inputenc))
16019 (let ((dir default-directory))
16020 (condition-case nil
16023 (call-process "latex" nil nil nil texfile))
16026 (if (not (file-exists-p dvifile))
16027 (progn (message "Failed to create dvi file from %s" texfile) nil)
16028 (condition-case nil
16029 (call-process "dvipng" nil nil nil
16032 ;;"-x" scale "-y" scale
16037 (if (not (file-exists-p pngfile))
16038 (if org-format-latex-signal-error
16039 (error "Failed to create png file from %s" texfile)
16040 (message "Failed to create png file from %s" texfile)
16042 ;; Use the requested file name and clean up
16043 (copy-file pngfile tofile 'replace)
16044 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
16045 (delete-file (concat texfilebase e)))
16048 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
16049 "Fill a LaTeX header template TPL.
16050 In the template, the following place holders will be recognized:
16052 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
16053 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
16054 [PACKAGES] \\usepackage statements for PKG
16055 [NO-PACKAGES] do not include PKG
16056 [EXTRA] the string EXTRA
16057 [NO-EXTRA] do not include EXTRA
16059 For backward compatibility, if both the positive and the negative place
16060 holder is missing, the positive one (without the \"NO-\") will be
16061 assumed to be present at the end of the template.
16062 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
16064 SNIPPETS-P indicates if this is run to create snippet images for HTML."
16065 (let (rpl (end ""))
16066 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
16067 (setq rpl (if (or (match-end 1) (not def-pkg))
16068 "" (org-latex-packages-to-string def-pkg snippets-p t))
16069 tpl (replace-match rpl t t tpl))
16070 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
16072 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
16073 (setq rpl (if (or (match-end 1) (not pkg))
16074 "" (org-latex-packages-to-string pkg snippets-p t))
16075 tpl (replace-match rpl t t tpl))
16078 (org-latex-packages-to-string pkg snippets-p)))))
16080 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
16081 (setq rpl (if (or (match-end 1) (not extra))
16082 "" (concat extra "\n"))
16083 tpl (replace-match rpl t t tpl))
16084 (if (and extra (string-match "\\S-" extra))
16085 (setq end (concat end "\n" extra))))
16087 (if (string-match "\\S-" end)
16088 (concat tpl "\n" end)
16091 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
16092 "Turn an alist of packages into a string with the \\usepackage macros."
16093 (setq pkg (mapconcat (lambda(p)
16096 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
16097 (format "%% Package %s omitted" (cadr p)))
16098 ((equal "" (car p))
16099 (format "\\usepackage{%s}" (cadr p)))
16101 (format "\\usepackage[%s]{%s}"
16102 (car p) (cadr p)))))
16105 (if newline (concat pkg "\n") pkg))
16107 (defun org-dvipng-color (attr)
16108 "Return an rgb color specification for dvipng."
16109 (apply 'format "rgb %s %s %s"
16110 (mapcar 'org-normalize-color
16111 (color-values (face-attribute 'default attr nil)))))
16113 (defun org-normalize-color (value)
16114 "Return string to be used as color value for an RGB component."
16115 (format "%g" (/ value 65535.0)))
16120 (defvar org-inline-image-overlays nil)
16121 (make-variable-buffer-local 'org-inline-image-overlays)
16123 (defun org-toggle-inline-images (&optional include-linked)
16124 "Toggle the display of inline images.
16125 INCLUDE-LINKED is passed to `org-display-inline-images'."
16127 (if org-inline-image-overlays
16129 (org-remove-inline-images)
16130 (message "Inline image display turned off"))
16131 (org-display-inline-images include-linked)
16132 (if org-inline-image-overlays
16133 (message "%d images displayed inline"
16134 (length org-inline-image-overlays))
16135 (message "No images to display inline"))))
16137 (defun org-display-inline-images (&optional include-linked refresh beg end)
16138 "Display inline images.
16139 Normally only links without a description part are inlined, because this
16140 is how it will work for export. When INCLUDE-LINKED is set, also links
16141 with a description part will be inlined. This can be nice for a quick
16142 look at those images, but it does not reflect what exported files will look
16144 When REFRESH is set, refresh existing images between BEG and END.
16145 This will create new image displays only if necessary.
16146 BEG and END default to the buffer boundaries."
16149 (org-remove-inline-images)
16150 (clear-image-cache))
16154 (setq beg (or beg (point-min)) end (or end (point-max)))
16155 (goto-char (point-min))
16156 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([-+~.:/\\_0-9a-zA-Z ]+"
16157 (substring (org-image-file-name-regexp) 0 -2)
16158 "\\)\\]" (if include-linked "" "\\]")))
16160 (while (re-search-forward re end t)
16161 (setq old (get-char-property-and-overlay (match-beginning 1)
16162 'org-image-overlay))
16163 (setq file (expand-file-name
16164 (concat (or (match-string 3) "") (match-string 4))))
16165 (when (file-exists-p file)
16166 (if (and (car-safe old) refresh)
16167 (image-refresh (overlay-get (cdr old) 'display))
16168 (setq img (create-image file))
16170 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
16171 (overlay-put ov 'display img)
16172 (overlay-put ov 'face 'default)
16173 (overlay-put ov 'org-image-overlay t)
16174 (overlay-put ov 'modification-hooks
16175 (list 'org-display-inline-modification-hook))
16176 (push ov org-inline-image-overlays)))))))))
16178 (defun org-display-inline-modification-hook (ov after beg end &optional len)
16179 "Remove inline-display overlay if a corresponding region is modified."
16180 (let ((inhibit-modification-hooks t))
16181 (when (and ov after)
16182 (delete ov org-inline-image-overlays)
16183 (delete-overlay ov))))
16185 (defun org-remove-inline-images ()
16186 "Remove inline display of images."
16188 (mapc 'delete-overlay org-inline-image-overlays)
16189 (setq org-inline-image-overlays nil))
16193 ;; Make `C-c C-x' a prefix key
16194 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
16196 ;; TAB key with modifiers
16197 (org-defkey org-mode-map "\C-i" 'org-cycle)
16198 (org-defkey org-mode-map [(tab)] 'org-cycle)
16199 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
16200 (org-defkey org-mode-map [(meta tab)] 'org-complete)
16201 (org-defkey org-mode-map "\M-\t" 'org-complete)
16202 (org-defkey org-mode-map "\M-\C-i" 'org-complete)
16203 ;; The following line is necessary under Suse GNU/Linux
16204 (unless (featurep 'xemacs)
16205 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
16206 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
16207 (define-key org-mode-map [backtab] 'org-shifttab)
16209 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
16210 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
16211 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
16213 ;; Cursor keys with modifiers
16214 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
16215 (org-defkey org-mode-map [(meta right)] 'org-metaright)
16216 (org-defkey org-mode-map [(meta up)] 'org-metaup)
16217 (org-defkey org-mode-map [(meta down)] 'org-metadown)
16219 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
16220 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
16221 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
16222 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
16224 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
16225 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
16226 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
16227 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
16229 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
16230 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
16233 (define-key org-mode-map org-babel-key-prefix org-babel-map)
16234 (mapc (lambda (pair)
16235 (define-key org-babel-map (car pair) (cdr pair)))
16236 org-babel-key-bindings)
16238 ;;; Extra keys for tty access.
16239 ;; We only set them when really needed because otherwise the
16240 ;; menus don't show the simple keys
16242 (when (or org-use-extra-keys
16243 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
16244 (not window-system))
16245 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
16246 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
16247 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
16248 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
16249 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
16250 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
16251 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
16252 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
16253 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
16254 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
16255 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
16256 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
16257 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
16258 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
16259 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
16260 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
16261 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
16262 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
16263 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
16264 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
16265 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
16266 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
16267 (org-defkey org-mode-map [?\e (tab)] 'org-complete)
16268 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
16269 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
16270 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
16271 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
16272 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
16274 ;; All the other keys
16276 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
16277 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
16278 (if (boundp 'narrow-map)
16279 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
16280 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
16281 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
16282 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
16283 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
16284 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
16285 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
16286 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
16287 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
16288 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
16289 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
16290 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
16291 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
16292 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
16293 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
16294 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
16295 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
16296 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
16297 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
16298 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
16299 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
16300 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
16301 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
16302 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
16303 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
16304 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
16305 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
16306 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
16307 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
16308 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
16309 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
16310 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
16311 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
16312 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
16313 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
16314 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
16315 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
16316 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
16317 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
16318 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
16319 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
16320 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
16321 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
16322 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
16323 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
16324 (org-defkey org-mode-map "\C-c^" 'org-sort)
16325 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
16326 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
16327 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
16328 (org-defkey org-mode-map "\C-m" 'org-return)
16329 (org-defkey org-mode-map "\C-j" 'org-return-indent)
16330 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
16331 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
16332 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
16333 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
16334 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
16335 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
16336 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
16337 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
16338 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
16339 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
16340 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
16341 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
16342 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
16343 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
16344 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
16345 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
16346 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
16347 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
16348 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
16349 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
16351 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
16352 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
16353 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
16354 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
16356 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
16357 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
16358 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
16359 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
16360 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
16361 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
16362 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
16363 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
16364 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
16365 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
16366 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
16367 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
16368 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
16369 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
16370 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
16371 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
16372 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
16374 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
16375 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
16376 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
16377 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
16379 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
16381 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
16383 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
16384 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
16386 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
16389 (when (featurep 'xemacs)
16390 (org-defkey org-mode-map 'button3 'popup-mode-menu))
16393 (defconst org-speed-commands-default
16395 ("Outline Navigation")
16396 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
16397 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
16398 ("f" . (org-speed-move-safe 'org-forward-same-level))
16399 ("b" . (org-speed-move-safe 'org-backward-same-level))
16400 ("u" . (org-speed-move-safe 'outline-up-heading))
16402 ("g" . (org-refile t))
16403 ("Outline Visibility")
16405 ("C" . org-shifttab)
16406 (" " . org-display-outline-path)
16407 ("Outline Structure Editing")
16408 ("U" . org-shiftmetaup)
16409 ("D" . org-shiftmetadown)
16410 ("r" . org-metaright)
16411 ("l" . org-metaleft)
16412 ("R" . org-shiftmetaright)
16413 ("L" . org-shiftmetaleft)
16414 ("i" . (progn (forward-char 1) (call-interactively
16415 'org-insert-heading-respect-content)))
16418 ("a" . org-archive-subtree-default-with-confirmation)
16419 ("." . outline-mark-subtree)
16421 ("I" . org-clock-in)
16422 ("O" . org-clock-out)
16423 ("Meta Data Editing")
16425 ("0" . (org-priority ?\ ))
16426 ("1" . (org-priority ?A))
16427 ("2" . (org-priority ?B))
16428 ("3" . (org-priority ?C))
16429 (";" . org-set-tags-command)
16430 ("e" . org-set-effort)
16431 ("Agenda Views etc")
16433 ("/" . org-sparse-tree)
16435 ("o" . org-open-at-point)
16436 ("?" . org-speed-command-help)
16437 ("<" . (org-agenda-set-restriction-lock 'subtree))
16438 (">" . (org-agenda-remove-restriction-lock))
16440 "The default speed commands.")
16442 (defun org-print-speed-command (e)
16443 (if (> (length (car e)) 1)
16448 (princ (make-string (length (car e)) ?-))
16452 (if (symbolp (cdr e))
16453 (princ (symbol-name (cdr e)))
16457 (defun org-speed-command-help ()
16458 "Show the available speed commands."
16460 (if (not org-use-speed-commands)
16461 (error "Speed commands are not activated, customize `org-use-speed-commands'")
16462 (with-output-to-temp-buffer "*Help*"
16463 (princ "User-defined Speed commands\n===========================\n")
16464 (mapc 'org-print-speed-command org-speed-commands-user)
16466 (princ "Built-in Speed commands\n=======================\n")
16467 (mapc 'org-print-speed-command org-speed-commands-default))
16468 (with-current-buffer "*Help*"
16469 (setq truncate-lines t))))
16471 (defun org-speed-move-safe (cmd)
16472 "Execute CMD, but make sure that the cursor always ends up in a headline.
16473 If not, return to the original position and throw an error."
16475 (let ((pos (point)))
16476 (call-interactively cmd)
16477 (unless (and (bolp) (org-on-heading-p))
16479 (error "Boundary reached while executing %s" cmd))))
16481 (defvar org-self-insert-command-undo-counter 0)
16483 (defvar org-table-auto-blank-field) ; defined in org-table.el
16484 (defvar org-speed-command nil)
16485 (defun org-self-insert-command (N)
16486 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
16487 If the cursor is in a table looking at whitespace, the whitespace is
16488 overwritten, and the table is not marked as requiring realignment."
16491 ((and org-use-speed-commands
16492 (or (and (bolp) (looking-at outline-regexp))
16493 (and (functionp org-use-speed-commands)
16494 (funcall org-use-speed-commands)))
16497 (or (cdr (assoc (this-command-keys) org-speed-commands-user))
16498 (cdr (assoc (this-command-keys) org-speed-commands-default)))))
16500 ((commandp org-speed-command)
16501 (setq this-command org-speed-command)
16502 (call-interactively org-speed-command))
16503 ((functionp org-speed-command)
16504 (funcall org-speed-command))
16505 ((and org-speed-command (listp org-speed-command))
16506 (eval org-speed-command))
16507 (t (let (org-use-speed-commands)
16508 (call-interactively 'org-self-insert-command)))))
16512 ;; check if we blank the field, and if that triggers align
16513 (and (featurep 'org-table) org-table-auto-blank-field
16514 (member last-command
16515 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
16516 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
16517 ;; got extra space, this field does not determine column width
16518 (let (org-table-may-need-update) (org-table-blank-field))
16519 ;; no extra space, this field may determine column width
16520 (org-table-blank-field)))
16523 (looking-at "[^|\n]* |"))
16524 (let (org-table-may-need-update)
16525 (goto-char (1- (match-end 0)))
16526 (delete-backward-char 1)
16527 (goto-char (match-beginning 0))
16528 (self-insert-command N)))
16530 (setq org-table-may-need-update t)
16531 (self-insert-command N)
16532 (org-fix-tags-on-the-fly)
16533 (if org-self-insert-cluster-for-undo
16534 (if (not (eq last-command 'org-self-insert-command))
16535 (setq org-self-insert-command-undo-counter 1)
16536 (if (>= org-self-insert-command-undo-counter 20)
16537 (setq org-self-insert-command-undo-counter 1)
16538 (and (> org-self-insert-command-undo-counter 0)
16540 (not (cadr buffer-undo-list)) ; remove nil entry
16541 (setcdr buffer-undo-list (cddr buffer-undo-list)))
16542 (setq org-self-insert-command-undo-counter
16543 (1+ org-self-insert-command-undo-counter))))))))
16545 (defun org-fix-tags-on-the-fly ()
16546 (when (and (equal (char-after (point-at-bol)) ?*)
16547 (org-on-heading-p))
16548 (org-align-tags-here org-tags-column)))
16550 (defun org-delete-backward-char (N)
16551 "Like `delete-backward-char', insert whitespace at field end in tables.
16552 When deleting backwards, in tables this function will insert whitespace in
16553 front of the next \"|\" separator, to keep the table aligned. The table will
16554 still be marked for re-alignment if the field did fill the entire column,
16555 because, in this case the deletion might narrow the column."
16557 (if (and (org-table-p)
16559 (string-match "|" (buffer-substring (point-at-bol) (point)))
16560 (looking-at ".*?|"))
16561 (let ((pos (point))
16562 (noalign (looking-at "[^|\n\r]* |"))
16563 (c org-table-may-need-update))
16564 (backward-delete-char N)
16565 (skip-chars-forward "^|")
16567 (goto-char (1- pos))
16568 ;; noalign: if there were two spaces at the end, this field
16569 ;; does not determine the width of the column.
16570 (if noalign (setq org-table-may-need-update c)))
16571 (backward-delete-char N)
16572 (org-fix-tags-on-the-fly)))
16574 (defun org-delete-char (N)
16575 "Like `delete-char', but insert whitespace at field end in tables.
16576 When deleting characters, in tables this function will insert whitespace in
16577 front of the next \"|\" separator, to keep the table aligned. The table will
16578 still be marked for re-alignment if the field did fill the entire column,
16579 because, in this case the deletion might narrow the column."
16581 (if (and (org-table-p)
16583 (not (= (char-after) ?|))
16585 (if (looking-at ".*?|")
16586 (let ((pos (point))
16587 (noalign (looking-at "[^|\n\r]* |"))
16588 (c org-table-may-need-update))
16589 (replace-match (concat
16590 (substring (match-string 0) 1 -1)
16593 ;; noalign: if there were two spaces at the end, this field
16594 ;; does not determine the width of the column.
16595 (if noalign (setq org-table-may-need-update c)))
16598 (org-fix-tags-on-the-fly)))
16600 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
16601 (put 'org-self-insert-command 'delete-selection t)
16602 (put 'orgtbl-self-insert-command 'delete-selection t)
16603 (put 'org-delete-char 'delete-selection 'supersede)
16604 (put 'org-delete-backward-char 'delete-selection 'supersede)
16605 (put 'org-yank 'delete-selection 'yank)
16607 ;; Make `flyspell-mode' delay after some commands
16608 (put 'org-self-insert-command 'flyspell-delayed t)
16609 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
16610 (put 'org-delete-char 'flyspell-delayed t)
16611 (put 'org-delete-backward-char 'flyspell-delayed t)
16613 ;; Make pabbrev-mode expand after org-mode commands
16614 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
16615 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
16617 ;; How to do this: Measure non-white length of current string
16618 ;; If equal to column width, we should realign.
16620 (defun org-remap (map &rest commands)
16621 "In MAP, remap the functions given in COMMANDS.
16622 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
16625 (setq old (pop commands) new (pop commands))
16626 (if (fboundp 'command-remapping)
16627 (org-defkey map (vector 'remap old) new)
16628 (substitute-key-definition old new map global-map)))))
16630 (when (eq org-enable-table-editor 'optimized)
16631 ;; If the user wants maximum table support, we need to hijack
16632 ;; some standard editing functions
16633 (org-remap org-mode-map
16634 'self-insert-command 'org-self-insert-command
16635 'delete-char 'org-delete-char
16636 'delete-backward-char 'org-delete-backward-char)
16637 (org-defkey org-mode-map "|" 'org-force-self-insert))
16639 (defvar org-ctrl-c-ctrl-c-hook nil
16640 "Hook for functions attaching themselves to `C-c C-c'.
16641 This can be used to add additional functionality to the C-c C-c key which
16642 executes context-dependent commands.
16643 Each function will be called with no arguments. The function must check
16644 if the context is appropriate for it to act. If yes, it should do its
16645 thing and then return a non-nil value. If the context is wrong,
16646 just do nothing and return nil.")
16648 (defvar org-tab-first-hook nil
16649 "Hook for functions to attach themselves to TAB.
16650 See `org-ctrl-c-ctrl-c-hook' for more information.
16651 This hook runs as the first action when TAB is pressed, even before
16652 `org-cycle' messes around with the `outline-regexp' to cater for
16653 inline tasks and plain list item folding.
16654 If any function in this hook returns t, any other actions that
16655 would have been caused by TAB (such as table field motion or visibility
16656 cycling) will not occur.")
16658 (defvar org-tab-after-check-for-table-hook nil
16659 "Hook for functions to attach themselves to TAB.
16660 See `org-ctrl-c-ctrl-c-hook' for more information.
16661 This hook runs after it has been established that the cursor is not in a
16662 table, but before checking if the cursor is in a headline or if global cycling
16664 If any function in this hook returns t, not other actions like visibility
16665 cycling will be done.")
16667 (defvar org-tab-after-check-for-cycling-hook nil
16668 "Hook for functions to attach themselves to TAB.
16669 See `org-ctrl-c-ctrl-c-hook' for more information.
16670 This hook runs after it has been established that not table field motion and
16671 not visibility should be done because of current context. This is probably
16672 the place where a package like yasnippets can hook in.")
16674 (defvar org-tab-before-tab-emulation-hook nil
16675 "Hook for functions to attach themselves to TAB.
16676 See `org-ctrl-c-ctrl-c-hook' for more information.
16677 This hook runs after every other options for TAB have been exhausted, but
16678 before indentation and \t insertion takes place.")
16680 (defvar org-metaleft-hook nil
16681 "Hook for functions attaching themselves to `M-left'.
16682 See `org-ctrl-c-ctrl-c-hook' for more information.")
16683 (defvar org-metaright-hook nil
16684 "Hook for functions attaching themselves to `M-right'.
16685 See `org-ctrl-c-ctrl-c-hook' for more information.")
16686 (defvar org-metaup-hook nil
16687 "Hook for functions attaching themselves to `M-up'.
16688 See `org-ctrl-c-ctrl-c-hook' for more information.")
16689 (defvar org-metadown-hook nil
16690 "Hook for functions attaching themselves to `M-down'.
16691 See `org-ctrl-c-ctrl-c-hook' for more information.")
16692 (defvar org-shiftmetaleft-hook nil
16693 "Hook for functions attaching themselves to `M-S-left'.
16694 See `org-ctrl-c-ctrl-c-hook' for more information.")
16695 (defvar org-shiftmetaright-hook nil
16696 "Hook for functions attaching themselves to `M-S-right'.
16697 See `org-ctrl-c-ctrl-c-hook' for more information.")
16698 (defvar org-shiftmetaup-hook nil
16699 "Hook for functions attaching themselves to `M-S-up'.
16700 See `org-ctrl-c-ctrl-c-hook' for more information.")
16701 (defvar org-shiftmetadown-hook nil
16702 "Hook for functions attaching themselves to `M-S-down'.
16703 See `org-ctrl-c-ctrl-c-hook' for more information.")
16704 (defvar org-metareturn-hook nil
16705 "Hook for functions attaching themselves to `M-RET'.
16706 See `org-ctrl-c-ctrl-c-hook' for more information.")
16707 (defvar org-shiftup-hook nil
16708 "Hook for functions attaching themselves to `S-up'.
16709 See `org-ctrl-c-ctrl-c-hook' for more information.")
16710 (defvar org-shiftup-final-hook nil
16711 "Hook for functions attaching themselves to `S-up'.
16712 This one runs after all other options except shift-select have been excluded.
16713 See `org-ctrl-c-ctrl-c-hook' for more information.")
16714 (defvar org-shiftdown-hook nil
16715 "Hook for functions attaching themselves to `S-down'.
16716 See `org-ctrl-c-ctrl-c-hook' for more information.")
16717 (defvar org-shiftdown-final-hook nil
16718 "Hook for functions attaching themselves to `S-down'.
16719 This one runs after all other options except shift-select have been excluded.
16720 See `org-ctrl-c-ctrl-c-hook' for more information.")
16721 (defvar org-shiftleft-hook nil
16722 "Hook for functions attaching themselves to `S-left'.
16723 See `org-ctrl-c-ctrl-c-hook' for more information.")
16724 (defvar org-shiftleft-final-hook nil
16725 "Hook for functions attaching themselves to `S-left'.
16726 This one runs after all other options except shift-select have been excluded.
16727 See `org-ctrl-c-ctrl-c-hook' for more information.")
16728 (defvar org-shiftright-hook nil
16729 "Hook for functions attaching themselves to `S-right'.
16730 See `org-ctrl-c-ctrl-c-hook' for more information.")
16731 (defvar org-shiftright-final-hook nil
16732 "Hook for functions attaching themselves to `S-right'.
16733 This one runs after all other options except shift-select have been excluded.
16734 See `org-ctrl-c-ctrl-c-hook' for more information.")
16736 (defun org-modifier-cursor-error ()
16737 "Throw an error, a modified cursor command was applied in wrong context."
16738 (error "This command is active in special context like tables, headlines or items"))
16740 (defun org-shiftselect-error ()
16741 "Throw an error because Shift-Cursor command was applied in wrong context."
16742 (if (and (boundp 'shift-select-mode) shift-select-mode)
16743 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
16744 (error "This command works only in special context like headlines or timestamps")))
16746 (defun org-call-for-shift-select (cmd)
16747 (let ((this-command-keys-shift-translated t))
16748 (call-interactively cmd)))
16750 (defun org-shifttab (&optional arg)
16751 "Global visibility cycling or move to previous table field.
16752 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
16754 See the individual commands for more information."
16757 ((org-at-table-p) (call-interactively 'org-table-previous-field))
16759 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
16760 (message "Content view to level: %d" arg)
16761 (org-content (prefix-numeric-value arg2))
16762 (setq org-cycle-global-status 'overview)))
16763 (t (call-interactively 'org-global-cycle))))
16765 (defun org-shiftmetaleft ()
16766 "Promote subtree or delete table column.
16767 Calls `org-promote-subtree', `org-outdent-item',
16768 or `org-table-delete-column', depending on context.
16769 See the individual commands for more information."
16772 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
16773 ((org-at-table-p) (call-interactively 'org-table-delete-column))
16774 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
16775 ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
16776 (t (org-modifier-cursor-error))))
16778 (defun org-shiftmetaright ()
16779 "Demote subtree or insert table column.
16780 Calls `org-demote-subtree', `org-indent-item',
16781 or `org-table-insert-column', depending on context.
16782 See the individual commands for more information."
16785 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
16786 ((org-at-table-p) (call-interactively 'org-table-insert-column))
16787 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
16788 ((org-at-item-p) (call-interactively 'org-indent-item-tree))
16789 (t (org-modifier-cursor-error))))
16791 (defun org-shiftmetaup (&optional arg)
16792 "Move subtree up or kill table row.
16793 Calls `org-move-subtree-up' or `org-table-kill-row' or
16794 `org-move-item-up' depending on context. See the individual commands
16795 for more information."
16798 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
16799 ((org-at-table-p) (call-interactively 'org-table-kill-row))
16800 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
16801 ((org-at-item-p) (call-interactively 'org-move-item-up))
16802 (t (org-modifier-cursor-error))))
16804 (defun org-shiftmetadown (&optional arg)
16805 "Move subtree down or insert table row.
16806 Calls `org-move-subtree-down' or `org-table-insert-row' or
16807 `org-move-item-down', depending on context. See the individual
16808 commands for more information."
16811 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
16812 ((org-at-table-p) (call-interactively 'org-table-insert-row))
16813 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
16814 ((org-at-item-p) (call-interactively 'org-move-item-down))
16815 (t (org-modifier-cursor-error))))
16817 (defsubst org-hidden-tree-error ()
16819 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
16821 (defun org-metaleft (&optional arg)
16822 "Promote heading or move table column to left.
16823 Calls `org-do-promote' or `org-table-move-column', depending on context.
16824 With no specific context, calls the Emacs default `backward-word'.
16825 See the individual commands for more information."
16828 ((run-hook-with-args-until-success 'org-metaleft-hook))
16829 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
16830 ((or (org-on-heading-p)
16831 (and (org-region-active-p)
16833 (goto-char (region-beginning))
16834 (org-on-heading-p))))
16835 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
16836 (call-interactively 'org-do-promote))
16837 ((or (org-at-item-p)
16838 (and (org-region-active-p)
16840 (goto-char (region-beginning))
16842 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
16843 (call-interactively 'org-outdent-item))
16844 (t (call-interactively 'backward-word))))
16846 (defun org-metaright (&optional arg)
16847 "Demote subtree or move table column to right.
16848 Calls `org-do-demote' or `org-table-move-column', depending on context.
16849 With no specific context, calls the Emacs default `forward-word'.
16850 See the individual commands for more information."
16853 ((run-hook-with-args-until-success 'org-metaright-hook))
16854 ((org-at-table-p) (call-interactively 'org-table-move-column))
16855 ((or (org-on-heading-p)
16856 (and (org-region-active-p)
16858 (goto-char (region-beginning))
16859 (org-on-heading-p))))
16860 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
16861 (call-interactively 'org-do-demote))
16862 ((or (org-at-item-p)
16863 (and (org-region-active-p)
16865 (goto-char (region-beginning))
16867 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
16868 (call-interactively 'org-indent-item))
16869 (t (call-interactively 'forward-word))))
16871 (defun org-check-for-hidden (what)
16872 "Check if there are hidden headlines/items in the current visual line.
16873 WHAT can be either `headlines' or `items'. If the current line is
16874 an outline or item heading and it has a folded subtree below it,
16875 this function returns t, nil otherwise."
16877 ((eq what 'headlines) (concat "^" org-outline-regexp))
16878 ((eq what 'items) (concat "^" (org-item-re t)))
16879 (t (error "This should not happen"))))
16883 (unless (org-region-active-p)
16884 (setq beg (point-at-bol))
16885 (beginning-of-line 2)
16886 (while (and (not (eobp)) ;; this is like `next-line'
16887 (get-char-property (1- (point)) 'invisible))
16888 (beginning-of-line 2))
16891 (goto-char (point-at-eol))
16892 (setq end (max end (point)))
16893 (while (re-search-forward re end t)
16894 (if (get-char-property (match-beginning 0) 'invisible)
16898 (defun org-metaup (&optional arg)
16899 "Move subtree up or move table row up.
16900 Calls `org-move-subtree-up' or `org-table-move-row' or
16901 `org-move-item-up', depending on context. See the individual commands
16902 for more information."
16905 ((run-hook-with-args-until-success 'org-metaup-hook))
16906 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
16907 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
16908 ((org-at-item-p) (call-interactively 'org-move-item-up))
16909 (t (transpose-lines 1) (beginning-of-line -1))))
16911 (defun org-metadown (&optional arg)
16912 "Move subtree down or move table row down.
16913 Calls `org-move-subtree-down' or `org-table-move-row' or
16914 `org-move-item-down', depending on context. See the individual
16915 commands for more information."
16918 ((run-hook-with-args-until-success 'org-metadown-hook))
16919 ((org-at-table-p) (call-interactively 'org-table-move-row))
16920 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
16921 ((org-at-item-p) (call-interactively 'org-move-item-down))
16922 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
16924 (defun org-shiftup (&optional arg)
16925 "Increase item in timestamp or increase priority of current headline.
16926 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
16927 depending on context. See the individual commands for more information."
16930 ((run-hook-with-args-until-success 'org-shiftup-hook))
16931 ((and org-support-shift-select (org-region-active-p))
16932 (org-call-for-shift-select 'previous-line))
16933 ((org-at-timestamp-p t)
16934 (call-interactively (if org-edit-timestamp-down-means-later
16935 'org-timestamp-down 'org-timestamp-up)))
16936 ((and (not (eq org-support-shift-select 'always))
16937 org-enable-priority-commands
16938 (org-on-heading-p))
16939 (call-interactively 'org-priority-up))
16940 ((and (not org-support-shift-select) (org-at-item-p))
16941 (call-interactively 'org-previous-item))
16942 ((org-clocktable-try-shift 'up arg))
16943 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
16944 (org-support-shift-select
16945 (org-call-for-shift-select 'previous-line))
16946 (t (org-shiftselect-error))))
16948 (defun org-shiftdown (&optional arg)
16949 "Decrease item in timestamp or decrease priority of current headline.
16950 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
16951 depending on context. See the individual commands for more information."
16954 ((run-hook-with-args-until-success 'org-shiftdown-hook))
16955 ((and org-support-shift-select (org-region-active-p))
16956 (org-call-for-shift-select 'next-line))
16957 ((org-at-timestamp-p t)
16958 (call-interactively (if org-edit-timestamp-down-means-later
16959 'org-timestamp-up 'org-timestamp-down)))
16960 ((and (not (eq org-support-shift-select 'always))
16961 org-enable-priority-commands
16962 (org-on-heading-p))
16963 (call-interactively 'org-priority-down))
16964 ((and (not org-support-shift-select) (org-at-item-p))
16965 (call-interactively 'org-next-item))
16966 ((org-clocktable-try-shift 'down arg))
16967 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
16968 (org-support-shift-select
16969 (org-call-for-shift-select 'next-line))
16970 (t (org-shiftselect-error))))
16972 (defun org-shiftright (&optional arg)
16973 "Cycle the thing at point or in the current line, depending on context.
16974 Depending on context, this does one of the following:
16976 - switch a timestamp at point one day into the future
16977 - on a headline, switch to the next TODO keyword.
16978 - on an item, switch entire list to the next bullet type
16979 - on a property line, switch to the next allowed value
16980 - on a clocktable definition line, move time block into the future"
16983 ((run-hook-with-args-until-success 'org-shiftright-hook))
16984 ((and org-support-shift-select (org-region-active-p))
16985 (org-call-for-shift-select 'forward-char))
16986 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
16987 ((and (not (eq org-support-shift-select 'always))
16988 (org-on-heading-p))
16989 (let ((org-inhibit-logging
16990 (not org-treat-S-cursor-todo-selection-as-state-change))
16991 (org-inhibit-blocking
16992 (not org-treat-S-cursor-todo-selection-as-state-change)))
16993 (org-call-with-arg 'org-todo 'right)))
16994 ((or (and org-support-shift-select
16995 (not (eq org-support-shift-select 'always))
16996 (org-at-item-bullet-p))
16997 (and (not org-support-shift-select) (org-at-item-p)))
16998 (org-call-with-arg 'org-cycle-list-bullet nil))
16999 ((and (not (eq org-support-shift-select 'always))
17000 (org-at-property-p))
17001 (call-interactively 'org-property-next-allowed-value))
17002 ((org-clocktable-try-shift 'right arg))
17003 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
17004 (org-support-shift-select
17005 (org-call-for-shift-select 'forward-char))
17006 (t (org-shiftselect-error))))
17008 (defun org-shiftleft (&optional arg)
17009 "Cycle the thing at point or in the current line, depending on context.
17010 Depending on context, this does one of the following:
17012 - switch a timestamp at point one day into the past
17013 - on a headline, switch to the previous TODO keyword.
17014 - on an item, switch entire list to the previous bullet type
17015 - on a property line, switch to the previous allowed value
17016 - on a clocktable definition line, move time block into the past"
17019 ((run-hook-with-args-until-success 'org-shiftleft-hook))
17020 ((and org-support-shift-select (org-region-active-p))
17021 (org-call-for-shift-select 'backward-char))
17022 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
17023 ((and (not (eq org-support-shift-select 'always))
17024 (org-on-heading-p))
17025 (let ((org-inhibit-logging
17026 (not org-treat-S-cursor-todo-selection-as-state-change))
17027 (org-inhibit-blocking
17028 (not org-treat-S-cursor-todo-selection-as-state-change)))
17029 (org-call-with-arg 'org-todo 'left)))
17030 ((or (and org-support-shift-select
17031 (not (eq org-support-shift-select 'always))
17032 (org-at-item-bullet-p))
17033 (and (not org-support-shift-select) (org-at-item-p)))
17034 (org-call-with-arg 'org-cycle-list-bullet 'previous))
17035 ((and (not (eq org-support-shift-select 'always))
17036 (org-at-property-p))
17037 (call-interactively 'org-property-previous-allowed-value))
17038 ((org-clocktable-try-shift 'left arg))
17039 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
17040 (org-support-shift-select
17041 (org-call-for-shift-select 'backward-char))
17042 (t (org-shiftselect-error))))
17044 (defun org-shiftcontrolright ()
17045 "Switch to next TODO set."
17048 ((and org-support-shift-select (org-region-active-p))
17049 (org-call-for-shift-select 'forward-word))
17050 ((and (not (eq org-support-shift-select 'always))
17051 (org-on-heading-p))
17052 (org-call-with-arg 'org-todo 'nextset))
17053 (org-support-shift-select
17054 (org-call-for-shift-select 'forward-word))
17055 (t (org-shiftselect-error))))
17057 (defun org-shiftcontrolleft ()
17058 "Switch to previous TODO set."
17061 ((and org-support-shift-select (org-region-active-p))
17062 (org-call-for-shift-select 'backward-word))
17063 ((and (not (eq org-support-shift-select 'always))
17064 (org-on-heading-p))
17065 (org-call-with-arg 'org-todo 'previousset))
17066 (org-support-shift-select
17067 (org-call-for-shift-select 'backward-word))
17068 (t (org-shiftselect-error))))
17070 (defun org-ctrl-c-ret ()
17071 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
17074 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
17075 (t (call-interactively 'org-insert-heading))))
17077 (defun org-copy-special ()
17078 "Copy region in table or copy current subtree.
17079 Calls `org-table-copy' or `org-copy-subtree', depending on context.
17080 See the individual commands for more information."
17082 (call-interactively
17083 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
17085 (defun org-cut-special ()
17086 "Cut region in table or cut current subtree.
17087 Calls `org-table-copy' or `org-cut-subtree', depending on context.
17088 See the individual commands for more information."
17090 (call-interactively
17091 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
17093 (defun org-paste-special (arg)
17094 "Paste rectangular region into table, or past subtree relative to level.
17095 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
17096 See the individual commands for more information."
17098 (if (org-at-table-p)
17099 (org-table-paste-rectangle)
17100 (org-paste-subtree arg)))
17102 (defun org-edit-special (&optional arg)
17103 "Call a special editor for the stuff at point.
17104 When at a table, call the formula editor with `org-table-edit-formulas'.
17105 When at the first line of an src example, call `org-edit-src-code'.
17106 When in an #+include line, visit the include file. Otherwise call
17107 `ffap' to visit the file at point."
17109 ;; possibly prep session before editing source
17111 (let* ((info (org-babel-get-src-block-info))
17112 (lang (nth 0 info))
17113 (params (nth 2 info))
17114 (session (cdr (assoc :session params))))
17115 (when (and info session) ;; we are in a source-code block with a session
17117 (intern (concat "org-babel-prep-session:" lang)) session params))))
17118 (cond ;; proceed with `org-edit-special'
17120 (beginning-of-line 1)
17121 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
17122 (find-file (org-trim (match-string 1))))
17123 ((org-edit-src-code))
17124 ((org-edit-fixed-width-region))
17125 ((org-at-table.el-p)
17126 (org-edit-src-code))
17128 (call-interactively 'org-table-edit-formulas))
17129 (t (call-interactively 'ffap))))
17132 (defun org-ctrl-c-ctrl-c (&optional arg)
17133 "Set tags in headline, or update according to changed information at point.
17135 This command does many different things, depending on context:
17137 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
17138 this is what we do.
17140 - If the cursor is on a statistics cookie, update it.
17142 - If the cursor is in a headline, prompt for tags and insert them
17143 into the current line, aligned to `org-tags-column'. When called
17144 with prefix arg, realign all tags in the current buffer.
17146 - If the cursor is in one of the special #+KEYWORD lines, this
17147 triggers scanning the buffer for these lines and updating the
17150 - If the cursor is inside a table, realign the table. This command
17151 works even if the automatic table editor has been turned off.
17153 - If the cursor is on a #+TBLFM line, re-apply the formulas to
17156 - If the cursor is at a footnote reference or definition, jump to
17157 the corresponding definition or references, respectively.
17159 - If the cursor is a the beginning of a dynamic block, update it.
17161 - If the current buffer is a capture buffer, close note and file it.
17163 - If the cursor is on a <<<target>>>, update radio targets and
17164 corresponding links in this buffer.
17166 - If the cursor is on a numbered item in a plain list, renumber the
17169 - If the cursor is on a checkbox, toggle it.
17171 - If the cursor is on a code block, evaluate it. The variable
17172 `org-confirm-babel-evaluate' can be used to control prompting
17173 before code block evaluation, by default every code block
17174 evaluation requires confirmation. Code block evaluation can be
17175 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
17177 (let ((org-enable-table-editor t))
17179 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
17180 org-occur-highlights
17181 org-latex-fragment-image-overlays)
17182 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
17183 (org-remove-occur-highlights)
17184 (org-remove-latex-fragment-image-overlays)
17185 (message "Temporary highlights/overlays removed from current buffer"))
17186 ((and (local-variable-p 'org-finish-function (current-buffer))
17187 (fboundp org-finish-function))
17188 (funcall org-finish-function))
17189 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
17190 ((or (looking-at org-property-start-re)
17191 (org-at-property-p))
17192 (call-interactively 'org-property-action))
17193 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
17194 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
17195 (or (org-on-heading-p) (org-at-item-p)))
17196 (call-interactively 'org-update-statistics-cookies))
17197 ((org-on-heading-p) (call-interactively 'org-set-tags))
17198 ((org-at-table.el-p)
17199 (message "Use C-c ' to edit table.el tables"))
17201 (org-table-maybe-eval-formula)
17203 (call-interactively 'org-table-recalculate)
17204 (org-table-maybe-recalculate-line))
17205 (call-interactively 'org-table-align))
17206 ((or (org-footnote-at-reference-p)
17207 (org-footnote-at-definition-p))
17208 (call-interactively 'org-footnote-action))
17209 ((org-at-item-checkbox-p)
17210 (call-interactively 'org-list-repair)
17211 (call-interactively 'org-toggle-checkbox)
17212 (org-list-send-list 'maybe))
17214 (call-interactively 'org-list-repair)
17215 (when arg (call-interactively 'org-toggle-checkbox))
17216 (org-list-send-list 'maybe))
17217 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
17219 (beginning-of-line 1)
17220 (save-excursion (org-update-dblock)))
17222 (beginning-of-line 1)
17223 (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
17225 ((equal (match-string 1) "TBLFM")
17226 ;; Recalculate the table before this line
17228 (beginning-of-line 1)
17229 (skip-chars-backward " \r\n\t")
17230 (if (org-at-table-p)
17231 (org-call-with-arg 'org-table-recalculate (or arg t)))))
17233 (let ((org-inhibit-startup-visibility-stuff t)
17234 (org-startup-align-all-tables nil))
17235 (org-save-outline-visibility 'use-markers (org-mode-restart)))
17236 (message "Local setup has been refreshed"))))
17237 ((org-clock-update-time-maybe))
17238 (t (error "C-c C-c can do nothing useful at this location")))))
17240 (defun org-mode-restart ()
17241 "Restart Org-mode, to scan again for special lines.
17242 Also updates the keyword regular expressions."
17245 (message "Org-mode restarted"))
17247 (defun org-kill-note-or-show-branches ()
17248 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
17250 (if (not org-finish-function)
17253 (call-interactively 'show-branches))
17254 (let ((org-note-abort t))
17255 (funcall org-finish-function))))
17257 (defun org-return (&optional indent)
17258 "Goto next table row or insert a newline.
17259 Calls `org-table-next-row' or `newline', depending on context.
17260 See the individual commands for more information."
17263 ((bobp) (if indent (newline-and-indent) (newline)))
17265 (org-table-justify-field-maybe)
17266 (call-interactively 'org-table-next-row))
17267 ((and org-return-follows-link
17268 (eq (get-text-property (point) 'face) 'org-link))
17269 (call-interactively 'org-open-at-point))
17270 ((and (org-at-heading-p)
17272 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
17276 (t (if indent (newline-and-indent) (newline)))))
17278 (defun org-return-indent ()
17279 "Goto next table row or insert a newline and indent.
17280 Calls `org-table-next-row' or `newline-and-indent', depending on
17281 context. See the individual commands for more information."
17285 (defun org-ctrl-c-star ()
17286 "Compute table, or change heading status of lines.
17287 Calls `org-table-recalculate' or `org-toggle-heading',
17288 depending on context."
17292 (call-interactively 'org-table-recalculate))
17294 ;; Convert all lines in region to list items
17295 (call-interactively 'org-toggle-heading))))
17297 (defun org-ctrl-c-minus ()
17298 "Insert separator line in table or modify bullet status of line.
17299 Also turns a plain line or a region of lines into list items.
17300 Calls `org-table-insert-hline', `org-toggle-item', or
17301 `org-cycle-list-bullet', depending on context."
17305 (call-interactively 'org-table-insert-hline))
17306 ((org-region-active-p)
17307 (call-interactively 'org-toggle-item))
17309 (call-interactively 'org-cycle-list-bullet))
17311 (call-interactively 'org-toggle-item))))
17313 (defun org-toggle-item ()
17314 "Convert headings or normal lines to items, items to normal lines.
17315 If there is no active region, only the current line is considered.
17317 If the first line in the region is a headline, convert all headlines to items.
17319 If the first line in the region is an item, convert all items to normal lines.
17321 If the first line is normal text, add an item bullet to each line."
17323 (let (l2 l beg end)
17324 (if (org-region-active-p)
17325 (setq beg (region-beginning) end (region-end))
17326 (setq beg (point-at-bol)
17327 end (min (1+ (point-at-eol)) (point-max))))
17330 (setq l2 (org-current-line))
17332 (beginning-of-line 1)
17333 (setq l (1- (org-current-line)))
17334 (if (org-at-item-p)
17335 ;; We already have items, de-itemize
17336 (while (< (setq l (1+ l)) l2)
17337 (when (org-at-item-p)
17338 (skip-chars-forward " \t")
17339 (delete-region (point) (match-end 0)))
17340 (beginning-of-line 2))
17341 (if (org-on-heading-p)
17342 ;; Headings, convert to items
17343 (while (< (setq l (1+ l)) l2)
17344 (if (looking-at org-outline-regexp)
17345 (replace-match (org-list-bullet-string "-") t t))
17346 (beginning-of-line 2))
17347 ;; normal lines, turn them into items
17348 (while (< (setq l (1+ l)) l2)
17349 (unless (org-at-item-p)
17350 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
17352 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
17353 (beginning-of-line 2)))))))
17355 (defun org-toggle-heading (&optional nstars)
17356 "Convert headings to normal text, or items or text to headings.
17357 If there is no active region, only the current line is considered.
17359 If the first line is a heading, remove the stars from all headlines
17362 If the first line is a plain list item, turn all plain list items
17365 If the first line is a normal line, turn each and every line in the
17366 region into a heading.
17368 When converting a line into a heading, the number of stars is chosen
17369 such that the lines become children of the current entry. However,
17370 when a prefix argument is given, its value determines the number of
17373 (let (l2 l itemp beg end)
17374 (if (org-region-active-p)
17375 (setq beg (region-beginning) end (region-end))
17376 (setq beg (point-at-bol)
17377 end (min (1+ (point-at-eol)) (point-max))))
17380 (setq l2 (org-current-line))
17382 (beginning-of-line 1)
17383 (setq l (1- (org-current-line)))
17384 (if (org-on-heading-p)
17385 ;; We already have headlines, de-star them
17386 (while (< (setq l (1+ l)) l2)
17387 (when (org-on-heading-p t)
17388 (and (looking-at outline-regexp) (replace-match "")))
17389 (beginning-of-line 2))
17390 (setq itemp (org-at-item-p))
17393 (make-string (prefix-numeric-value current-prefix-arg)
17396 (if (re-search-backward org-complex-heading-regexp nil t)
17397 (match-string 1) ""))))
17398 (add-stars (cond (nstars "")
17399 ((equal stars "") "*")
17400 (org-odd-levels-only "**")
17402 (rpl (concat stars add-stars " ")))
17403 (while (< (setq l (1+ l)) l2)
17405 (and (org-at-item-p) (replace-match rpl t t))
17406 (unless (org-on-heading-p)
17407 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
17408 (replace-match (concat rpl (match-string 2))))))
17409 (beginning-of-line 2)))))))
17411 (defun org-meta-return (&optional arg)
17412 "Insert a new heading or wrap a region in a table.
17413 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
17414 See the individual commands for more information."
17417 ((run-hook-with-args-until-success 'org-metareturn-hook))
17419 (call-interactively 'org-table-wrap-region))
17420 (t (call-interactively 'org-insert-heading))))
17424 ;; Define the Org-mode menus
17425 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
17427 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
17428 ["Next Field" org-cycle (org-at-table-p)]
17429 ["Previous Field" org-shifttab (org-at-table-p)]
17430 ["Next Row" org-return (org-at-table-p)]
17432 ["Blank Field" org-table-blank-field (org-at-table-p)]
17433 ["Edit Field" org-table-edit-field (org-at-table-p)]
17434 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
17437 ["Move Column Left" org-metaleft (org-at-table-p)]
17438 ["Move Column Right" org-metaright (org-at-table-p)]
17439 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
17440 ["Insert Column" org-shiftmetaright (org-at-table-p)])
17442 ["Move Row Up" org-metaup (org-at-table-p)]
17443 ["Move Row Down" org-metadown (org-at-table-p)]
17444 ["Delete Row" org-shiftmetaup (org-at-table-p)]
17445 ["Insert Row" org-shiftmetadown (org-at-table-p)]
17446 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
17448 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
17450 ["Copy Rectangle" org-copy-special (org-at-table-p)]
17451 ["Cut Rectangle" org-cut-special (org-at-table-p)]
17452 ["Paste Rectangle" org-paste-special (org-at-table-p)]
17453 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
17456 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
17457 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
17458 ["Edit Formulas" org-edit-special (org-at-table-p)]
17460 ["Recalculate line" org-table-recalculate (org-at-table-p)]
17461 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
17462 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
17464 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
17466 ["Sum Column/Rectangle" org-table-sum
17467 (or (org-at-table-p) (org-region-active-p))]
17468 ["Which Column?" org-table-current-column (org-at-table-p)])
17470 org-table-toggle-formula-debugger
17471 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
17472 ["Show Col/Row Numbers"
17473 org-table-toggle-coordinate-overlays
17475 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
17477 ["Create" org-table-create (and (not (org-at-table-p))
17478 org-enable-table-editor)]
17479 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
17480 ["Import from File" org-table-import (not (org-at-table-p))]
17481 ["Export to File" org-table-export (org-at-table-p)]
17483 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
17485 (easy-menu-define org-org-menu org-mode-map "Org menu"
17488 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
17489 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
17490 ["Sparse Tree..." org-sparse-tree t]
17491 ["Reveal Context" org-reveal t]
17492 ["Show All" show-all t]
17494 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
17496 ["New Heading" org-insert-heading t]
17497 ("Navigate Headings"
17498 ["Up" outline-up-heading t]
17499 ["Next" outline-next-visible-heading t]
17500 ["Previous" outline-previous-visible-heading t]
17501 ["Next Same Level" outline-forward-same-level t]
17502 ["Previous Same Level" outline-backward-same-level t]
17504 ["Jump" org-goto t])
17506 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
17507 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
17509 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
17510 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
17511 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
17513 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
17515 ["Promote Heading" org-metaleft (not (org-at-table-p))]
17516 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
17517 ["Demote Heading" org-metaright (not (org-at-table-p))]
17518 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
17520 ["Sort Region/Children" org-sort (not (org-at-table-p))]
17522 ["Convert to odd levels" org-convert-to-odd-levels t]
17523 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
17525 ["Emphasis..." org-emphasize t]
17526 ["Edit Source Example" org-edit-special t]
17528 ["Footnote new/jump" org-footnote-action t]
17529 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
17531 ["Archive (default method)" org-archive-subtree-default t]
17533 ["Move Subtree to Archive file" org-advertized-archive-subtree t]
17534 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
17535 ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
17539 ["Store Link (Global)" org-store-link t]
17540 ["Find existing link to here" org-occur-link-in-agenda-files t]
17541 ["Insert Link" org-insert-link t]
17542 ["Follow Link" org-open-at-point t]
17544 ["Next link" org-next-link t]
17545 ["Previous link" org-previous-link t]
17547 ["Descriptive Links"
17548 (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
17550 :selected (member '(org-link) buffer-invisibility-spec)]
17553 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
17555 :selected (not (member '(org-link) buffer-invisibility-spec))])
17558 ["TODO/DONE/-" org-todo t]
17560 ["Next keyword" org-shiftright (org-on-heading-p)]
17561 ["Previous keyword" org-shiftleft (org-on-heading-p)]
17562 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
17563 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
17564 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
17565 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
17566 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
17568 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
17569 :selected org-enforce-todo-dependencies :style toggle :active t]
17570 "Settings for tree at point"
17571 ["Do Children sequentially" org-toggle-ordered-property :style radio
17572 :selected (ignore-errors (org-entry-get nil "ORDERED"))
17573 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
17574 ["Do Children parallel" org-toggle-ordered-property :style radio
17575 :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
17576 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
17578 ["Set Priority" org-priority t]
17579 ["Priority Up" org-shiftup t]
17580 ["Priority Down" org-shiftdown t]
17582 ["Get news from all feeds" org-feed-update-all t]
17583 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
17584 ["Customize feeds" (customize-variable 'org-feed-alist) t])
17585 ("TAGS and Properties"
17586 ["Set Tags" org-set-tags-command t]
17587 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
17589 ["Set property" org-set-property t]
17590 ["Column view of properties" org-columns t]
17591 ["Insert Column View DBlock" org-insert-columns-dblock t])
17592 ("Dates and Scheduling"
17593 ["Timestamp" org-time-stamp t]
17594 ["Timestamp (inactive)" org-time-stamp-inactive t]
17596 ["1 Day Later" org-shiftright t]
17597 ["1 Day Earlier" org-shiftleft t]
17598 ["1 ... Later" org-shiftup t]
17599 ["1 ... Earlier" org-shiftdown t])
17600 ["Compute Time Range" org-evaluate-time-range t]
17601 ["Schedule Item" org-schedule t]
17602 ["Deadline" org-deadline t]
17604 ["Custom time format" org-toggle-time-stamp-overlays
17605 :style radio :selected org-display-custom-times]
17607 ["Goto Calendar" org-goto-calendar t]
17608 ["Date from Calendar" org-date-from-calendar t]
17610 ["Start/Restart Timer" org-timer-start t]
17611 ["Pause/Continue Timer" org-timer-pause-or-continue t]
17612 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
17613 ["Insert Timer String" org-timer t]
17614 ["Insert Timer Item" org-timer-item t])
17616 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
17617 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
17618 ["Clock out" org-clock-out t]
17619 ["Clock cancel" org-clock-cancel t]
17621 ["Mark as default task" org-clock-mark-default-task t]
17622 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
17623 ["Goto running clock" org-clock-goto t]
17625 ["Display times" org-clock-display t]
17626 ["Create clock table" org-clock-report t]
17628 ["Record DONE time"
17629 (progn (setq org-log-done (not org-log-done))
17630 (message "Switching to %s will %s record a timestamp"
17631 (car org-done-keywords)
17632 (if org-log-done "automatically" "not")))
17633 :style toggle :selected org-log-done])
17635 ["Agenda Command..." org-agenda t]
17636 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
17637 ("File List for Agenda")
17638 ("Special views current file"
17639 ["TODO Tree" org-show-todo-tree t]
17640 ["Check Deadlines" org-check-deadlines t]
17641 ["Timeline" org-timeline t]
17642 ["Tags/Property tree" org-match-sparse-tree t])
17644 ["Export/Publish..." org-export t]
17646 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
17647 :selected org-cdlatex-mode]
17648 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
17649 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
17650 ["Modify math symbol" org-cdlatex-math-modify
17651 (org-inside-LaTeX-fragment-p)]
17652 ["Insert citation" org-reftex-citation t]
17654 ["Template for BEAMER" org-insert-beamer-options-template t])
17657 ["Push Files and Views" org-mobile-push t]
17658 ["Get Captured and Flagged" org-mobile-pull t]
17659 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
17661 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
17664 ["Show Version" org-version t]
17665 ["Info Documentation" org-info t])
17667 ["Browse Org Group" org-customize t]
17669 ["Expand This Menu" org-create-customize-menu
17670 (fboundp 'customize-menu-create)])
17671 ["Send bug report" org-submit-bug-report t]
17674 ["Refresh setup current buffer" org-mode-restart t]
17675 ["Reload Org (after update)" org-reload t]
17676 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
17679 (defun org-info (&optional node)
17680 "Read documentation for Org-mode in the info system.
17681 With optional NODE, go directly to that node."
17683 (info (format "(org)%s" (or node ""))))
17686 (defun org-submit-bug-report ()
17687 "Submit a bug report on Org-mode via mail.
17689 Don't hesitate to report any problems or inaccurate documentation.
17691 If you don't have setup sending mail from (X)Emacs, please copy the
17692 output buffer into your mail program, as it gives us important
17693 information about your Org-mode version and configuration."
17695 (require 'reporter)
17696 (org-load-modules-maybe)
17697 (org-require-autoloaded-modules)
17698 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
17699 (reporter-submit-bug-report
17700 "emacs-orgmode@gnu.org"
17703 (save-window-excursion
17704 (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
17705 (delete-other-windows)
17707 (insert "You are about to submit a bug report to the Org-mode mailing list.
17709 We would like to add your full Org-mode and Outline configuration to the
17710 bug report. This greatly simplifies the work of the maintainer and
17711 other experts on the mailing list.
17713 HOWEVER, some variables you have customized may contain private
17714 information. The names of customers, colleagues, or friends, might
17715 appear in the form of file names, tags, todo states, or search strings.
17716 If you answer yes to the prompt, you might want to check and remove
17717 such private information before sending the email.")
17718 (add-text-properties (point-min) (point-max) '(face org-warning))
17719 (when (yes-or-no-p "Include your Org-mode configuration ")
17723 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
17724 (or (and (symbol-value v)
17725 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
17727 (get v 'custom-type) (get v 'standard-value)
17728 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
17730 (kill-buffer (get-buffer "*Warn about privacy*"))
17733 "Remember to cover the basics, that is, what you expected to happen and
17734 what in fact did happen. You don't know how to make a good report? See
17736 http://orgmode.org/manual/Feedback.html#Feedback
17738 Your bug report will be posted to the Org-mode mailing list.
17739 ------------------------------------------------------------------------")
17741 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
17742 (replace-match "\\1Bug: \\3 [\\2]")))))
17745 (defun org-install-agenda-files-menu ()
17746 (let ((bl (buffer-list)))
17749 (set-buffer (pop bl))
17750 (if (org-mode-p) (setq bl nil)))
17753 '("Org") "File List for Agenda"
17756 ["Edit File List" (org-edit-agenda-file-list) t]
17757 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
17758 ["Remove Current File from List" org-remove-file t]
17759 ["Cycle through agenda files" org-cycle-agenda-files t]
17760 ["Occur in all agenda files" org-occur-in-agenda-files t]
17762 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
17767 (defun org-require-autoloaded-modules ()
17770 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
17771 org-docbook org-exp org-html org-icalendar
17773 org-publish org-remember org-table
17774 org-timer org-xoxo)))
17777 (defun org-reload (&optional uncompiled)
17778 "Reload all org lisp files.
17779 With prefix arg UNCOMPILED, load the uncompiled versions."
17781 (require 'find-func)
17782 (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
17783 (dir-org (file-name-directory (org-find-library-name "org")))
17784 (dir-org-contrib (ignore-errors
17785 (file-name-directory
17786 (org-find-library-name "org-contribdir"))))
17788 (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
17789 (append (list nil "comint" "eval" "exp" "keys"
17790 "lob" "ref" "table" "tangle")
17794 (when (cdr lang) (symbol-name (car lang))))
17795 org-babel-load-languages)))))
17797 (append (directory-files dir-org t file-re)
17799 (and dir-org-contrib
17800 (directory-files dir-org-contrib t file-re))))
17801 (remove-re (concat (if (featurep 'xemacs)
17802 "org-colview" "org-colview-xemacs")
17804 (setq files (mapcar 'file-name-sans-extension files))
17805 (setq files (mapcar
17806 (lambda (x) (if (string-match remove-re x) nil x))
17808 (setq files (delq nil files))
17811 (when (featurep (intern (file-name-nondirectory f)))
17812 (if (and (not uncompiled)
17813 (file-exists-p (concat f ".elc")))
17814 (load (concat f ".elc") nil nil t)
17815 (load (concat f ".el") nil nil t))))
17820 (defun org-customize ()
17821 "Call the customize function with org as argument."
17823 (org-load-modules-maybe)
17824 (org-require-autoloaded-modules)
17825 (customize-browse 'org))
17827 (defun org-create-customize-menu ()
17828 "Create a full customization menu for Org-mode, insert it into the menu."
17830 (org-load-modules-maybe)
17831 (org-require-autoloaded-modules)
17832 (if (fboundp 'customize-menu-create)
17835 '("Org") "Customize"
17836 `(["Browse Org group" org-customize t]
17838 ,(customize-menu-create 'org)
17839 ["Set" Custom-set t]
17840 ["Save" Custom-save t]
17841 ["Reset to Current" Custom-reset-current t]
17842 ["Reset to Saved" Custom-reset-saved t]
17843 ["Reset to Standard Settings" Custom-reset-standard t]))
17844 (message "\"Org\"-menu now contains full customization menu"))
17845 (error "Cannot expand menu (outdated version of cus-edit.el)")))
17847 ;;;; Miscellaneous stuff
17849 ;;; Generally useful functions
17851 (defun org-get-at-bol (property)
17852 "Get text property PROPERTY at beginning of line."
17853 (get-text-property (point-at-bol) property))
17855 (defun org-find-text-property-in-string (prop s)
17856 "Return the first non-nil value of property PROP in string S."
17857 (or (get-text-property 0 prop s)
17858 (get-text-property (or (next-single-property-change 0 prop s) 0)
17861 (defun org-display-warning (message) ;; Copied from Emacs-Muse
17862 "Display the given MESSAGE as a warning."
17863 (if (fboundp 'display-warning)
17864 (display-warning 'org message
17865 (if (featurep 'xemacs) 'warning :warning))
17866 (let ((buf (get-buffer-create "*Org warnings*")))
17867 (with-current-buffer buf
17868 (goto-char (point-max))
17869 (insert "Warning (Org): " message)
17872 (display-buffer buf)
17875 (defun org-in-commented-line ()
17876 "Is point in a line starting with `#'?"
17877 (equal (char-after (point-at-bol)) ?#))
17879 (defun org-in-indented-comment-line ()
17880 "Is point in a line starting with `#' after some white space?"
17883 (goto-char (point-at-bol))
17884 (looking-at "[ \t]*#"))))
17886 (defun org-in-verbatim-emphasis ()
17888 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
17890 (defun org-goto-marker-or-bmk (marker &optional bookmark)
17891 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
17892 (if (and marker (marker-buffer marker)
17893 (buffer-live-p (marker-buffer marker)))
17895 (switch-to-buffer (marker-buffer marker))
17896 (if (or (> marker (point-max)) (< marker (point-min)))
17899 (org-show-context 'org-goto))
17901 (bookmark-jump bookmark)
17902 (error "Cannot find location"))))
17904 (defun org-quote-csv-field (s)
17905 "Quote field for inclusion in CSV material."
17906 (if (string-match "[\",]" s)
17907 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
17910 (defun org-plist-delete (plist property)
17911 "Delete PROPERTY from PLIST.
17912 This is in contrast to merely setting it to 0."
17915 (if (not (eq property (car plist)))
17916 (setq p (plist-put p (car plist) (nth 1 plist))))
17917 (setq plist (cddr plist)))
17920 (defun org-force-self-insert (N)
17921 "Needed to enforce self-insert under remapping."
17923 (self-insert-command N))
17925 (defun org-string-width (s)
17926 "Compute width of string, ignoring invisible characters.
17927 This ignores character with invisibility property `org-link', and also
17928 characters with property `org-cwidth', because these will become invisible
17929 upon the next fontification round."
17931 (when (or (eq t buffer-invisibility-spec)
17932 (assq 'org-link buffer-invisibility-spec))
17933 (while (setq b (text-property-any 0 (length s)
17934 'invisible 'org-link s))
17935 (setq s (concat (substring s 0 b)
17936 (substring s (or (next-single-property-change
17937 b 'invisible s) (length s)))))))
17938 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
17939 (setq s (concat (substring s 0 b)
17940 (substring s (or (next-single-property-change
17941 b 'org-cwidth s) (length s))))))
17942 (setq l (string-width s) b -1)
17943 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
17944 (setq l (- l (get-text-property b 'org-dwidth-n s))))
17947 (defun org-get-indentation (&optional line)
17948 "Get the indentation of the current line, interpreting tabs.
17949 When LINE is given, assume it represents a line and compute its indentation."
17951 (if (string-match "^ *" (org-remove-tabs line))
17954 (beginning-of-line 1)
17955 (skip-chars-forward " \t")
17956 (current-column))))
17958 (defun org-remove-tabs (s &optional width)
17959 "Replace tabulators in S with spaces.
17960 Assumes that s is a single line, starting in column 0."
17961 (setq width (or width tab-width))
17962 (while (string-match "\t" s)
17963 (setq s (replace-match
17965 (- (* width (/ (+ (match-beginning 0) width) width))
17966 (match-beginning 0)) ?\ )
17970 (defun org-fix-indentation (line ind)
17971 "Fix indentation in LINE.
17972 IND is a cons cell with target and minimum indentation.
17973 If the current indentation in LINE is smaller than the minimum,
17974 leave it alone. If it is larger than ind, set it to the target."
17975 (let* ((l (org-remove-tabs line))
17976 (i (org-get-indentation l))
17977 (i1 (car ind)) (i2 (cdr ind)))
17978 (if (>= i i2) (setq l (substring line i2)))
17980 (concat (make-string i1 ?\ ) l)
17983 (defun org-remove-indentation (code &optional n)
17984 "Remove the maximum common indentation from the lines in CODE.
17985 N may optionally be the number of spaces to remove."
17988 (org-do-remove-indentation n)
17991 (defun org-do-remove-indentation (&optional n)
17992 "Remove the maximum common indentation from the buffer."
17993 (untabify (point-min) (point-max))
17994 (let ((min 10000) re)
17997 (goto-char (point-min))
17998 (while (re-search-forward "^ *[^ \n]" nil t)
17999 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
18000 (unless (or (= min 0) (= min 10000))
18001 (setq re (format "^ \\{%d\\}" min))
18002 (goto-char (point-min))
18003 (while (re-search-forward re nil t)
18008 (defun org-fill-template (template alist)
18009 "Find each %key of ALIST in TEMPLATE and replace it."
18010 (let ((case-fold-search nil)
18012 (setq alist (sort (copy-sequence alist)
18013 (lambda (a b) (< (length (car a)) (length (car b))))))
18014 (while (setq entry (pop alist))
18016 (replace-regexp-in-string
18017 (concat "%" (regexp-quote (car entry)))
18018 (cdr entry) template t t)))
18021 (defun org-base-buffer (buffer)
18022 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
18025 (or (buffer-base-buffer buffer)
18028 (defun org-trim (s)
18029 "Remove whitespace at beginning and end of string."
18030 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
18031 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
18034 (defun org-wrap (string &optional width lines)
18035 "Wrap string to either a number of lines, or a width in characters.
18036 If WIDTH is non-nil, the string is wrapped to that width, however many lines
18037 that costs. If there is a word longer than WIDTH, the text is actually
18038 wrapped to the length of that word.
18039 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
18040 many lines, whatever width that takes.
18041 The return value is a list of lines, without newlines at the end."
18042 (let* ((words (org-split-string string "[ \t\n]+"))
18043 (maxword (apply 'max (mapcar 'org-string-width words)))
18046 (org-do-wrap words (max maxword width)))
18049 (setq ll (org-do-wrap words maxword))
18050 (if (<= (length ll) lines)
18053 (while (> (length ll) lines)
18055 (setq ll (org-do-wrap words w)))
18057 (t (error "Cannot wrap this")))))
18059 (defun org-do-wrap (words width)
18060 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
18063 (setq line (pop words))
18064 (while (and words (< (+ (length line) (length (car words))) width))
18065 (setq line (concat line " " (pop words))))
18066 (setq lines (push line lines)))
18069 (defun org-split-string (string &optional separators)
18070 "Splits STRING into substrings at SEPARATORS.
18071 No empty strings are returned if there are matches at the beginning
18072 and end of string."
18073 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
18077 (while (and (string-match rexp string
18079 (= start (match-beginning 0))
18080 (< start (length string)))
18082 (< (match-beginning 0) (length string)))
18084 (or (eq (match-beginning 0) 0)
18085 (and (eq (match-beginning 0) (match-end 0))
18086 (eq (match-beginning 0) start))
18088 (cons (substring string start (match-beginning 0))
18090 (setq start (match-end 0)))
18091 (or (eq start (length string))
18093 (cons (substring string start)
18097 (defun org-quote-vert (s)
18098 "Replace \"|\" with \"\\vert\"."
18099 (while (string-match "|" s)
18100 (setq s (replace-match "\\vert" t t s)))
18103 (defun org-uuidgen-p (s)
18104 "Is S an ID created by UUIDGEN?"
18105 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
18107 (defun org-context ()
18108 "Return a list of contexts of the current cursor position.
18109 If several contexts apply, all are returned.
18110 Each context entry is a list with a symbol naming the context, and
18111 two positions indicating start and end of the context. Possible
18114 :headline anywhere in a headline
18115 :headline-stars on the leading stars in a headline
18116 :todo-keyword on a TODO keyword (including DONE) in a headline
18117 :tags on the TAGS in a headline
18118 :priority on the priority cookie in a headline
18119 :item on the first line of a plain list item
18120 :item-bullet on the bullet/number of a plain list item
18121 :checkbox on the checkbox in a plain list item
18122 :table in an org-mode table
18123 :table-special on a special filed in a table
18124 :table-table in a table.el table
18125 :link on a hyperlink
18126 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
18127 :target on a <<target>>
18128 :radio-target on a <<<radio-target>>>
18129 :latex-fragment on a LaTeX fragment
18130 :latex-preview on a LaTeX fragment with overlayed preview image
18132 This function expects the position to be visible because it uses font-lock
18133 faces as a help to recognize the following contexts: :table-special, :link,
18135 (let* ((f (get-text-property (point) 'face))
18136 (faces (if (listp f) f (list f)))
18137 (p (point)) clist o)
18138 ;; First the large context
18140 ((org-on-heading-p t)
18141 (push (list :headline (point-at-bol) (point-at-eol)) clist)
18143 (beginning-of-line 1)
18144 (looking-at org-todo-line-tags-regexp))
18145 (push (org-point-in-group p 1 :headline-stars) clist)
18146 (push (org-point-in-group p 2 :todo-keyword) clist)
18147 (push (org-point-in-group p 4 :tags) clist))
18149 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
18150 (if (looking-at "\\[#[A-Z0-9]\\]")
18151 (push (org-point-in-group p 0 :priority) clist)))
18154 (push (org-point-in-group p 2 :item-bullet) clist)
18155 (push (list :item (point-at-bol)
18156 (save-excursion (org-end-of-item) (point)))
18158 (and (org-at-item-checkbox-p)
18159 (push (org-point-in-group p 0 :checkbox) clist)))
18162 (push (list :table (org-table-begin) (org-table-end)) clist)
18163 (if (memq 'org-formula faces)
18164 (push (list :table-special
18165 (previous-single-property-change p 'face)
18166 (next-single-property-change p 'face)) clist)))
18167 ((org-at-table-p 'any)
18168 (push (list :table-table) clist)))
18171 ;; Now the small context
18173 ((org-at-timestamp-p)
18174 (push (org-point-in-group p 0 :timestamp) clist))
18175 ((memq 'org-link faces)
18177 (previous-single-property-change p 'face)
18178 (next-single-property-change p 'face)) clist))
18179 ((memq 'org-special-keyword faces)
18180 (push (list :keyword
18181 (previous-single-property-change p 'face)
18182 (next-single-property-change p 'face)) clist))
18184 (push (org-point-in-group p 0 :target) clist)
18185 (goto-char (1- (match-beginning 0)))
18186 (if (looking-at org-radio-target-regexp)
18187 (push (org-point-in-group p 0 :radio-target) clist))
18189 ((setq o (car (delq nil
18192 (if (memq x org-latex-fragment-image-overlays) x))
18193 (overlays-at (point))))))
18194 (push (list :latex-fragment
18195 (overlay-start o) (overlay-end o)) clist)
18196 (push (list :latex-preview
18197 (overlay-start o) (overlay-end o)) clist))
18198 ((org-inside-LaTeX-fragment-p)
18199 ;; FIXME: positions wrong.
18200 (push (list :latex-fragment (point) (point)) clist)))
18202 (setq clist (nreverse (delq nil clist)))
18205 ;; FIXME: Compare with at-regexp-p Do we need both?
18206 (defun org-in-regexp (re &optional nlines visually)
18207 "Check if point is inside a match of regexp.
18208 Normally only the current line is checked, but you can include NLINES extra
18209 lines both before and after point into the search.
18210 If VISUALLY is set, require that the cursor is not after the match but
18211 really on, so that the block visually is on the match."
18213 (let ((pos (point))
18214 (eol (point-at-eol (+ 1 (or nlines 0))))
18215 (inc (if visually 1 0)))
18217 (beginning-of-line (- 1 (or nlines 0)))
18218 (while (re-search-forward re eol t)
18219 (if (and (<= (match-beginning 0) pos)
18220 (>= (+ inc (match-end 0)) pos))
18221 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
18223 (defun org-at-regexp-p (regexp)
18224 "Is point inside a match of REGEXP in the current line?"
18227 (let ((pos (point)) (end (point-at-eol)))
18228 (beginning-of-line 1)
18229 (while (re-search-forward regexp end t)
18230 (if (and (<= (match-beginning 0) pos)
18231 (>= (match-end 0) pos))
18235 (defun org-in-regexps-block-p (start-re end-re &optional bound)
18236 "Returns t if the current point is between matches of START-RE and END-RE.
18237 This will also return t if point is on one of the two matches or
18238 in an unfinished block. END-RE can be a string or a form
18239 returning a string.
18241 An optional third argument bounds the search for START-RE. It
18242 defaults to previous heading or `point-min'."
18243 (let ((pos (point))
18244 (limit (or bound (save-excursion (outline-previous-heading)))))
18246 ;; we're on a block when point is on start-re...
18247 (or (org-at-regexp-p start-re)
18248 ;; ... or start-re can be found above...
18249 (and (re-search-backward start-re limit t)
18250 ;; ... but no end-re between start-re and point.
18251 (not (re-search-forward (eval end-re) pos t)))))))
18253 (defun org-occur-in-agenda-files (regexp &optional nlines)
18254 "Call `multi-occur' with buffers for all agenda files."
18255 (interactive "sOrg-files matching: \np")
18256 (let* ((files (org-agenda-files))
18257 (tnames (mapcar 'file-truename files))
18258 (extra org-agenda-text-search-extra-files)
18260 (when (eq (car extra) 'agenda-archives)
18261 (setq extra (cdr extra))
18262 (setq files (org-add-archive-files files)))
18263 (while (setq f (pop extra))
18264 (unless (member (file-truename f) tnames)
18265 (add-to-list 'files f 'append)
18266 (add-to-list 'tnames (file-truename f) 'append)))
18268 (mapcar (lambda (x)
18269 (with-current-buffer
18270 (or (get-file-buffer x) (find-file-noselect x))
18276 (if (boundp 'occur-mode-find-occurrence-hook)
18278 (add-hook 'occur-mode-find-occurrence-hook
18283 (defadvice occur-mode-goto-occurrence
18284 (after org-occur-reveal activate)
18285 (and (org-mode-p) (org-reveal)))
18286 (defadvice occur-mode-goto-occurrence-other-window
18287 (after org-occur-reveal activate)
18288 (and (org-mode-p) (org-reveal)))
18289 (defadvice occur-mode-display-occurrence
18290 (after org-occur-reveal activate)
18292 (let ((pos (occur-mode-find-occurrence)))
18293 (with-current-buffer (marker-buffer pos)
18298 (defun org-occur-link-in-agenda-files ()
18299 "Create a link and search for it in the agendas.
18300 The link is not stored in `org-stored-links', it is just created
18301 for the search purpose."
18303 (let ((link (condition-case nil
18304 (org-store-link nil)
18305 (error "Unable to create a link to here"))))
18306 (org-occur-in-agenda-files (regexp-quote link))))
18308 (defun org-uniquify (list)
18309 "Remove duplicate elements from LIST."
18311 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
18314 (defun org-delete-all (elts list)
18315 "Remove all elements in ELTS from LIST."
18317 (setq list (delete (pop elts) list)))
18320 (defun org-count (cl-item cl-seq)
18321 "Count the number of occurrences of ITEM in SEQ.
18322 Taken from `count' in cl-seq.el with all keyword arguments removed."
18323 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
18324 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
18325 (while (< cl-start cl-end)
18326 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
18327 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
18328 (setq cl-start (1+ cl-start)))
18331 (defun org-remove-if (predicate seq)
18332 "Remove everything from SEQ that fulfills PREDICATE."
18336 (if (not (funcall predicate e)) (push e res)))
18339 (defun org-remove-if-not (predicate seq)
18340 "Remove everything from SEQ that does not fulfill PREDICATE."
18344 (if (funcall predicate e) (push e res)))
18347 (defun org-back-over-empty-lines ()
18348 "Move backwards over whitespace, to the beginning of the first empty line.
18349 Returns the number of empty lines passed."
18350 (let ((pos (point)))
18351 (skip-chars-backward " \t\n\r")
18352 (beginning-of-line 2)
18353 (goto-char (min (point) pos))
18354 (count-lines (point) pos)))
18356 (defun org-skip-whitespace ()
18357 (skip-chars-forward " \t\n\r"))
18359 (defun org-point-in-group (point group &optional context)
18360 "Check if POINT is in match-group GROUP.
18361 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
18362 match. If the match group does not exist or point is not inside it,
18364 (and (match-beginning group)
18365 (>= point (match-beginning group))
18366 (<= point (match-end group))
18368 (list context (match-beginning group) (match-end group))
18371 (defun org-switch-to-buffer-other-window (&rest args)
18372 "Switch to buffer in a second window on the current frame.
18373 In particular, do not allow pop-up frames.
18374 Returns the newly created buffer."
18375 (let (pop-up-frames special-display-buffer-names special-display-regexps
18376 special-display-function)
18377 (apply 'switch-to-buffer-other-window args)))
18379 (defun org-combine-plists (&rest plists)
18380 "Create a single property list from all plists in PLISTS.
18381 The process starts by copying the first list, and then setting properties
18382 from the other lists. Settings in the last list are the most significant
18383 ones and overrule settings in the other lists."
18384 (let ((rtn (copy-sequence (pop plists)))
18387 (setq ls (pop plists))
18389 (setq p (pop ls) v (pop ls))
18390 (setq rtn (plist-put rtn p v))))
18393 (defun org-move-line-down (arg)
18394 "Move the current line down. With prefix argument, move it past ARG lines."
18396 (let ((col (current-column))
18398 (beginning-of-line 1) (setq beg (point))
18399 (beginning-of-line 2) (setq end (point))
18400 (beginning-of-line (+ 1 arg))
18401 (setq pos (move-marker (make-marker) (point)))
18402 (insert (delete-and-extract-region beg end))
18404 (org-move-to-column col)))
18406 (defun org-move-line-up (arg)
18407 "Move the current line up. With prefix argument, move it past ARG lines."
18409 (let ((col (current-column))
18411 (beginning-of-line 1) (setq beg (point))
18412 (beginning-of-line 2) (setq end (point))
18413 (beginning-of-line (- arg))
18414 (setq pos (move-marker (make-marker) (point)))
18415 (insert (delete-and-extract-region beg end))
18417 (org-move-to-column col)))
18419 (defun org-replace-escapes (string table)
18420 "Replace %-escapes in STRING with values in TABLE.
18421 TABLE is an association list with keys like \"%a\" and string values.
18422 The sequences in STRING may contain normal field width and padding information,
18423 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
18424 so values can contain further %-escapes if they are define later in TABLE."
18425 (let ((tbl (copy-alist table))
18426 (case-fold-search nil)
18429 (while (setq e (pop tbl))
18430 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
18431 (when (and (cdr e) (string-match re (cdr e)))
18432 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
18434 (add-text-properties 0 3 (list 'sref sref) safe)
18435 (setcdr e (replace-match safe t t (cdr e)))))
18436 (while (string-match re string)
18437 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
18439 (setq string (replace-match rpl t t string))))
18440 (while (setq pchg (next-property-change pchg string))
18441 (let ((sref (get-text-property pchg 'sref string)))
18442 (when (and sref (string-match "SREF" string pchg))
18443 (setq string (replace-match sref t t string)))))
18446 (defun org-sublist (list start end)
18447 "Return a section of LIST, from START to END.
18448 Counting starts at 1."
18449 (let (rtn (c start))
18450 (setq list (nthcdr (1- start) list))
18451 (while (and list (<= c end))
18452 (push (pop list) rtn)
18456 (defun org-find-base-buffer-visiting (file)
18457 "Like `find-buffer-visiting' but always return the base buffer and
18458 not an indirect buffer."
18459 (let ((buf (or (get-file-buffer file)
18460 (find-buffer-visiting file))))
18462 (or (buffer-base-buffer buf) buf)
18465 (defun org-image-file-name-regexp (&optional extensions)
18466 "Return regexp matching the file names of images.
18467 If EXTENSIONS is given, only match these."
18468 (if (and (not extensions) (fboundp 'image-file-name-regexp))
18469 (image-file-name-regexp)
18470 (let ((image-file-name-extensions
18472 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
18473 "xbm" "xpm" "pbm" "pgm" "ppm"))))
18475 (regexp-opt (nconc (mapcar 'upcase
18476 image-file-name-extensions)
18477 image-file-name-extensions)
18481 (defun org-file-image-p (file &optional extensions)
18482 "Return non-nil if FILE is an image."
18484 (string-match (org-image-file-name-regexp extensions) file)))
18486 (defun org-get-cursor-date ()
18487 "Return the date at cursor in as a time.
18488 This works in the calendar and in the agenda, anywhere else it just
18489 returns the current time."
18490 (let (date day defd)
18492 ((eq major-mode 'calendar-mode)
18493 (setq date (calendar-cursor-to-date)
18494 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
18495 ((eq major-mode 'org-agenda-mode)
18496 (setq day (get-text-property (point) 'day))
18498 (setq date (calendar-gregorian-from-absolute day)
18499 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
18501 (or defd (current-time))))
18503 (defvar org-agenda-action-marker (make-marker)
18504 "Marker pointing to the entry for the next agenda action.")
18506 (defun org-mark-entry-for-agenda-action ()
18507 "Mark the current entry as target of an agenda action.
18508 Agenda actions are actions executed from the agenda with the key `k',
18509 which make use of the date at the cursor."
18511 (move-marker org-agenda-action-marker
18512 (save-excursion (org-back-to-heading t) (point))
18515 "Entry marked for action; press `k' at desired date in agenda or calendar"))
18517 ;;; Paragraph filling stuff.
18518 ;; We want this to be just right, so use the full arsenal.
18520 (defun org-indent-line-function ()
18521 "Indent line like previous, but further if previous was headline or item."
18523 (let* ((pos (point))
18524 (itemp (org-at-item-p))
18525 (case-fold-search t)
18526 (org-drawer-regexp (or org-drawer-regexp "\000"))
18527 column bpos bcol tpos tcol bullet btype bullet-type)
18528 ;; Find the previous relevant line
18529 (beginning-of-line 1)
18532 ((looking-at "#") (setq column 0))
18534 ((looking-at "\\*+ ") (setq column 0))
18536 ((and (looking-at "[ \t]*:END:")
18537 (save-excursion (re-search-backward org-drawer-regexp nil t)))
18539 (goto-char (1- (match-beginning 1)))
18540 (setq column (current-column))))
18542 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
18544 (re-search-backward
18545 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
18546 (setq column (org-get-indentation (match-string 0))))
18547 ((and (not (looking-at "[ \t]*#\\+begin_"))
18548 (org-in-regexps-block-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
18550 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
18552 (if (equal (downcase (match-string 1)) "src")
18553 ;; src blocks: let `org-edit-src-exit' handle them
18554 (org-get-indentation)
18555 (org-get-indentation (match-string 0)))))
18558 (org-beginning-of-item)
18559 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
18560 (setq bpos (match-beginning 1) tpos (match-end 0)
18561 bcol (progn (goto-char bpos) (current-column))
18562 tcol (progn (goto-char tpos) (current-column))
18563 bullet (match-string 1)
18564 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
18565 (if (> tcol (+ bcol org-description-max-indent))
18566 (setq tcol (+ bcol 5)))
18569 (beginning-of-line 1)
18571 (if (looking-at "\\S-")
18573 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
18574 (setq bullet (match-string 1)
18575 btype (if (string-match "[0-9]" bullet) "n" bullet))
18576 (setq column (if (equal btype bullet-type) bcol tcol)))
18577 (setq column (org-get-indentation)))))
18578 ;; This line has nothing special, look upside to get a clue about
18581 (beginning-of-line 0)
18582 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
18583 (not (looking-at "[ \t]*:END:"))
18584 (not (looking-at org-drawer-regexp)))
18585 (beginning-of-line 0))
18587 ;; There was an heading above.
18588 ((looking-at "\\*+[ \t]+")
18589 (if (not org-adapt-indentation)
18591 (goto-char (match-end 0))
18592 (setq column (current-column))))
18593 ;; A drawer had started and is unfinished: indent consequently.
18594 ((looking-at org-drawer-regexp)
18595 (goto-char (1- (match-beginning 1)))
18596 (setq column (current-column)))
18597 ;; The drawer had ended: indent like its :END: line.
18598 ((looking-at "\\([ \t]*\\):END:")
18599 (goto-char (match-end 1))
18600 (setq column (current-column)))
18601 ;; There was a list that since ended: indent relatively to
18602 ;; current heading.
18604 (outline-previous-heading)
18605 (if (and org-adapt-indentation
18606 (looking-at "\\*+[ \t]+"))
18608 (goto-char (match-end 0))
18609 (setq column (current-column)))
18611 ;; Else, nothing noticeable found: get indentation and go on.
18612 (t (setq column (org-get-indentation))))))
18614 (if (<= (current-column) (current-indentation))
18615 (org-indent-line-to column)
18616 (save-excursion (org-indent-line-to column)))
18617 (setq column (current-column))
18618 (beginning-of-line 1)
18620 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
18621 (replace-match (concat (match-string 1)
18622 (format org-property-format
18623 (match-string 2) (match-string 3)))
18625 (org-move-to-column column)))
18627 (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
18628 "Variable to store copy of `adaptive-fill-regexp'.
18629 Since `adaptive-fill-regexp' is set to never match, we need to
18630 store a backup of its value before entering `org-mode' so that
18631 the functionality can be provided as a fall-back.")
18633 (defun org-set-autofill-regexps ()
18635 ;; In the paragraph separator we include headlines, because filling
18636 ;; text in a line directly attached to a headline would otherwise
18637 ;; fill the headline as well.
18638 (org-set-local 'comment-start-skip "^#+[ \t]*")
18639 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
18640 ;; The paragraph starter includes hand-formatted lists.
18648 "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
18651 "\\\\\\(begin\\|end\\|[][]\\)"))
18652 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
18653 ;; But only if the user has not turned off tables or fixed-width regions
18655 'auto-fill-inhibit-regexp
18656 (concat "\\*+ \\|#\\+"
18657 "\\|[ \t]*" org-keyword-time-regexp
18658 (if (or org-enable-table-editor org-enable-fixed-width-editor)
18661 (if org-enable-table-editor "|" "")
18662 (if org-enable-fixed-width-editor ":" "")
18664 ;; We use our own fill-paragraph function, to make sure that tables
18665 ;; and fixed-width regions are not wrapped. That function will pass
18666 ;; through to `fill-paragraph' when appropriate.
18667 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
18668 ;; Adaptive filling: To get full control, first make sure that
18669 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
18670 (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
18671 (org-set-local 'org-adaptive-fill-regexp-backup
18672 adaptive-fill-regexp))
18673 (org-set-local 'adaptive-fill-regexp "\000")
18674 (org-set-local 'adaptive-fill-function
18675 'org-adaptive-fill-function)
18677 'align-mode-rules-list
18678 '((org-in-buffer-settings
18679 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
18680 (modes . '(org-mode))))))
18682 (defun org-fill-paragraph (&optional justify)
18683 "Re-align a table, pass through to fill-paragraph if no table."
18684 (let ((table-p (org-at-table-p))
18685 (table.el-p (org-at-table.el-p)))
18686 (cond ((and (equal (char-after (point-at-bol)) ?*)
18687 (save-excursion (goto-char (point-at-bol))
18688 (looking-at outline-regexp)))
18689 t) ; skip headlines
18690 (table.el-p t) ; skip table.el tables
18691 (table-p (org-table-align) t) ; align org-mode tables
18692 (t nil)))) ; call paragraph-fill
18694 ;; For reference, this is the default value of adaptive-fill-regexp
18695 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
18697 (defun org-adaptive-fill-function ()
18698 "Return a fill prefix for org-mode files.
18699 In particular, this makes sure hanging paragraphs for hand-formatted lists
18703 ((looking-at "#[ \t]+")
18704 (match-string-no-properties 0))
18705 ;; Description list
18706 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
18708 (if (> (match-end 1) (+ (match-beginning 1)
18709 org-description-max-indent))
18710 (goto-char (+ (match-beginning 1) 5))
18711 (goto-char (match-end 0)))
18712 (make-string (current-column) ?\ )))
18713 ;; Ordered or unordered list
18714 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
18716 (goto-char (match-end 0))
18717 (make-string (current-column) ?\ )))
18719 ((looking-at org-adaptive-fill-regexp-backup)
18720 (match-string-no-properties 0))))
18724 (defun org-toggle-fixed-width-section (arg)
18725 "Toggle the fixed-width export.
18726 If there is no active region, the QUOTE keyword at the current headline is
18727 inserted or removed. When present, it causes the text between this headline
18728 and the next to be exported as fixed-width text, and unmodified.
18729 If there is an active region, this command adds or removes a colon as the
18730 first character of this line. If the first character of a line is a colon,
18731 this line is also exported in fixed-width font."
18734 (regionp (org-region-active-p))
18735 (beg (if regionp (region-beginning) (point)))
18736 (end (if regionp (region-end)))
18737 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
18738 (case-fold-search nil)
18739 (re "[ \t]*\\(: \\)")
18744 (setq cc (current-column))
18745 (beginning-of-line 1)
18746 (setq off (looking-at re))
18747 (while (> nlines 0)
18748 (setq nlines (1- nlines))
18749 (beginning-of-line 1)
18752 (org-move-to-column cc t)
18755 ((and off (looking-at re))
18756 (replace-match "" t t nil 1))
18757 ((not off) (org-move-to-column cc t) (insert ": ")))
18760 (org-back-to-heading)
18761 (if (looking-at (concat outline-regexp
18762 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
18763 (replace-match "" t t nil 1)
18764 (if (looking-at outline-regexp)
18766 (goto-char (match-end 0))
18767 (insert org-quote-string " "))))))))
18769 (defun org-reftex-citation ()
18770 "Use reftex-citation to insert a citation into the buffer.
18771 This looks for a line like
18773 #+BIBLIOGRAPHY: foo plain option:-d
18775 and derives from it that foo.bib is the bibliography file relevant
18776 for this document. It then installs the necessary environment for RefTeX
18777 to work in this buffer and calls `reftex-citation' to insert a citation
18780 Export of such citations to both LaTeX and HTML is handled by the contributed
18781 package org-exp-bibtex by Taru Karttunen."
18783 (let ((reftex-docstruct-symbol 'rds)
18784 (reftex-cite-format "\\cite{%l}")
18789 (let ((case-fold-search t)
18790 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
18791 (if (not (save-excursion
18792 (or (re-search-forward re nil t)
18793 (re-search-backward re nil t))))
18794 (error "No bibliography defined in file")
18795 (setq bib (concat (match-string 1) ".bib")
18796 rds (list (list 'bib bib)))))))
18797 (call-interactively 'reftex-citation)))
18799 ;;;; Functions extending outline functionality
18801 (defun org-beginning-of-line (&optional arg)
18802 "Go to the beginning of the current line. If that is invisible, continue
18803 to a visible line beginning. This makes the function of C-a more intuitive.
18804 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
18805 first attempt, and only move to after the tags when the cursor is already
18806 beyond the end of the headline."
18808 (let ((pos (point))
18809 (special (if (consp org-special-ctrl-a/e)
18810 (car org-special-ctrl-a/e)
18811 org-special-ctrl-a/e))
18813 (if (org-bound-and-true-p line-move-visual)
18814 (beginning-of-visual-line 1)
18815 (beginning-of-line 1))
18816 (if (and arg (fboundp 'move-beginning-of-line))
18817 (call-interactively 'move-beginning-of-line)
18821 (if (org-truely-invisible-p)
18822 (while (and (not (bobp)) (org-truely-invisible-p))
18824 (beginning-of-line 1))
18825 (forward-char 1))))
18828 ((and (looking-at org-complex-heading-regexp)
18829 (= (char-after (match-end 1)) ?\ ))
18830 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
18834 (cond ((> pos refpos) refpos)
18835 ((= pos (point)) refpos)
18837 (cond ((> pos (point)) (point))
18838 ((not (eq last-command this-command)) (point))
18843 (cond ((> pos (match-end 4)) (match-end 4))
18844 ((= pos (point)) (match-end 4))
18846 (cond ((> pos (point)) (point))
18847 ((not (eq last-command this-command)) (point))
18848 (t (match-end 4))))))))
18850 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
18852 (defun org-end-of-line (&optional arg)
18853 "Go to the end of the line.
18854 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
18855 first attempt, and only move to after the tags when the cursor is already
18856 beyond the end of the headline."
18858 (let ((special (if (consp org-special-ctrl-a/e)
18859 (cdr org-special-ctrl-a/e)
18860 org-special-ctrl-a/e)))
18861 (if (or (not special)
18862 (not (org-on-heading-p))
18864 (call-interactively
18865 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
18866 ((fboundp 'move-end-of-line) 'move-end-of-line)
18868 (let ((pos (point)))
18869 (beginning-of-line 1)
18870 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
18872 (if (or (< pos (match-beginning 1))
18873 (= pos (match-end 0)))
18874 (goto-char (match-beginning 1))
18875 (goto-char (match-end 0)))
18876 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
18877 (goto-char (match-end 0))
18878 (goto-char (match-beginning 1))))
18879 (call-interactively (if (fboundp 'move-end-of-line)
18883 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
18885 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
18886 (define-key org-mode-map "\C-e" 'org-end-of-line)
18887 (define-key org-mode-map [home] 'org-beginning-of-line)
18888 (define-key org-mode-map [end] 'org-end-of-line)
18890 (defun org-backward-sentence (&optional arg)
18891 "Go to beginning of sentence, or beginning of table field.
18892 This will call `backward-sentence' or `org-table-beginning-of-field',
18893 depending on context."
18896 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
18897 (t (call-interactively 'backward-sentence))))
18899 (defun org-forward-sentence (&optional arg)
18900 "Go to end of sentence, or end of table field.
18901 This will call `forward-sentence' or `org-table-end-of-field',
18902 depending on context."
18905 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
18906 (t (call-interactively 'forward-sentence))))
18908 (define-key org-mode-map "\M-a" 'org-backward-sentence)
18909 (define-key org-mode-map "\M-e" 'org-forward-sentence)
18911 (defun org-kill-line (&optional arg)
18912 "Kill line, to tags or end of line."
18915 ((or (not org-special-ctrl-k)
18917 (not (org-on-heading-p)))
18918 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
18919 org-ctrl-k-protect-subtree)
18920 (if (or (eq org-ctrl-k-protect-subtree 'error)
18921 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
18922 (error "C-k aborted - would kill hidden subtree")))
18923 (call-interactively 'kill-line))
18924 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
18925 (kill-region (point) (match-beginning 1))
18926 (org-set-tags nil t))
18927 (t (kill-region (point) (point-at-eol)))))
18929 (define-key org-mode-map "\C-k" 'org-kill-line)
18931 (defun org-yank (&optional arg)
18932 "Yank. If the kill is a subtree, treat it specially.
18933 This command will look at the current kill and check if is a single
18934 subtree, or a series of subtrees[1]. If it passes the test, and if the
18935 cursor is at the beginning of a line or after the stars of a currently
18936 empty headline, then the yank is handled specially. How exactly depends
18937 on the value of the following variables, both set by default.
18939 org-yank-folded-subtrees
18940 When set, the subtree(s) will be folded after insertion, but only
18941 if doing so would now swallow text after the yanked text.
18943 org-yank-adjusted-subtrees
18944 When set, the subtree will be promoted or demoted in order to
18945 fit into the local outline tree structure, which means that the level
18946 will be adjusted so that it becomes the smaller one of the two
18947 *visible* surrounding headings.
18949 Any prefix to this command will cause `yank' to be called directly with
18950 no special treatment. In particular, a simple \\[universal-argument] prefix \
18952 plainly yank the text as it is.
18954 \[1] The test checks if the first non-white line is a heading
18955 and if there are no other headings with fewer stars."
18957 (org-yank-generic 'yank arg))
18959 (defun org-yank-generic (command arg)
18960 "Perform some yank-like command.
18962 This function implements the behavior described in the `org-yank'
18963 documentation. However, it has been generalized to work for any
18964 interactive command with similar behavior."
18966 ;; pretend to be command COMMAND
18967 (setq this-command command)
18970 (call-interactively command)
18972 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
18973 (and (org-kill-is-subtree-p)
18975 (and (looking-at "[ \t]*$")
18978 (buffer-substring (point-at-bol) (point)))))))
18981 ((and subtreep org-yank-folded-subtrees)
18982 (let ((beg (point))
18984 (if (and subtreep org-yank-adjusted-subtrees)
18985 (org-paste-subtree nil nil 'for-yank)
18986 (call-interactively command))
18990 (when (and (bolp) subtreep
18991 (not (setq swallowp
18992 (org-yank-folding-would-swallow-text beg end))))
18993 (or (looking-at outline-regexp)
18994 (re-search-forward (concat "^" outline-regexp) end t))
18995 (while (and (< (point) end) (looking-at outline-regexp))
18997 (org-cycle-show-empty-lines 'folded)
18998 (condition-case nil
18999 (outline-forward-same-level 1)
19000 (error (goto-char end)))))
19003 "Inserted text not folded because that would swallow text"))
19006 (skip-chars-forward " \t\n\r")
19007 (beginning-of-line 1)
19008 (push-mark beg 'nomsg)))
19009 ((and subtreep org-yank-adjusted-subtrees)
19010 (let ((beg (point-at-bol)))
19011 (org-paste-subtree nil nil 'for-yank)
19012 (push-mark beg 'nomsg)))
19014 (call-interactively command))))))
19016 (defun org-yank-folding-would-swallow-text (beg end)
19017 "Would hide-subtree at BEG swallow any text after END?"
19021 (when (or (looking-at outline-regexp)
19022 (re-search-forward (concat "^" outline-regexp) end t))
19023 (setq level (org-outline-level)))
19025 (skip-chars-forward " \t\r\n\v\f")
19027 (and (bolp) (looking-at org-outline-regexp)
19028 (<= (org-outline-level) level)))
19029 nil ; Nothing would be swallowed
19030 t)))) ; something would swallow
19032 (define-key org-mode-map "\C-y" 'org-yank)
19034 (defun org-invisible-p ()
19035 "Check if point is at a character currently not visible."
19036 ;; Early versions of noutline don't have `outline-invisible-p'.
19037 (if (fboundp 'outline-invisible-p)
19038 (outline-invisible-p)
19039 (get-char-property (point) 'invisible)))
19041 (defun org-truely-invisible-p ()
19042 "Check if point is at a character currently not visible.
19043 This version does not only check the character property, but also
19045 ;; Early versions of noutline don't have `outline-invisible-p'.
19046 (if (org-bound-and-true-p visible-mode)
19048 (if (fboundp 'outline-invisible-p)
19049 (outline-invisible-p)
19050 (get-char-property (point) 'invisible))))
19052 (defun org-invisible-p2 ()
19053 "Check if point is at a character currently not visible."
19055 (if (and (eolp) (not (bobp))) (backward-char 1))
19056 ;; Early versions of noutline don't have `outline-invisible-p'.
19057 (if (fboundp 'outline-invisible-p)
19058 (outline-invisible-p)
19059 (get-char-property (point) 'invisible))))
19061 (defun org-back-to-heading (&optional invisible-ok)
19062 "Call `outline-back-to-heading', but provide a better error message."
19063 (condition-case nil
19064 (outline-back-to-heading invisible-ok)
19065 (error (error "Before first headline at position %d in buffer %s"
19066 (point) (current-buffer)))))
19068 (defun org-beginning-of-defun ()
19069 "Go to the beginning of the subtree, i.e. back to the heading."
19070 (org-back-to-heading))
19071 (defun org-end-of-defun ()
19072 "Go to the end of the subtree."
19073 (org-end-of-subtree nil t))
19075 (defun org-before-first-heading-p ()
19076 "Before first heading?"
19078 (null (re-search-backward "^\\*+ " nil t))))
19080 (defun org-on-heading-p (&optional ignored)
19081 (outline-on-heading-p t))
19082 (defun org-at-heading-p (&optional ignored)
19083 (outline-on-heading-p t))
19085 (defun org-point-at-end-of-empty-headline ()
19086 "If point is at the end of an empty headline, return t, else nil.
19087 If the heading only contains a TODO keyword, it is still still considered
19089 (and (looking-at "[ \t]*$")
19091 (beginning-of-line 1)
19092 (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
19094 (defun org-at-heading-or-item-p ()
19095 (or (org-on-heading-p) (org-at-item-p)))
19097 (defun org-on-target-p ()
19098 (or (org-in-regexp org-radio-target-regexp)
19099 (org-in-regexp org-target-regexp)))
19101 (defun org-up-heading-all (arg)
19102 "Move to the heading line of which the present line is a subheading.
19103 This function considers both visible and invisible heading lines.
19104 With argument, move up ARG levels."
19105 (if (fboundp 'outline-up-heading-all)
19106 (outline-up-heading-all arg) ; emacs 21 version of outline.el
19107 (outline-up-heading arg t))) ; emacs 22 version of outline.el
19109 (defun org-up-heading-safe ()
19110 "Move to the heading line of which the present line is a subheading.
19111 This version will not throw an error. It will return the level of the
19112 headline found, or nil if no higher level is found.
19114 Also, this function will be a lot faster than `outline-up-heading',
19115 because it relies on stars being the outline starters. This can really
19116 make a significant difference in outlines with very many siblings."
19117 (let (start-level re)
19118 (org-back-to-heading t)
19119 (setq start-level (funcall outline-level))
19120 (if (equal start-level 1)
19122 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
19123 (if (re-search-backward re nil t)
19124 (funcall outline-level)))))
19126 (defun org-first-sibling-p ()
19127 "Is this heading the first child of its parents?"
19129 (let ((re (concat "^" outline-regexp))
19131 (unless (org-at-heading-p t)
19132 (error "Not at a heading"))
19133 (setq level (funcall outline-level))
19135 (if (not (re-search-backward re nil t))
19137 (setq l (funcall outline-level))
19140 (defun org-goto-sibling (&optional previous)
19141 "Goto the next sibling, even if it is invisible.
19142 When PREVIOUS is set, go to the previous sibling instead. Returns t
19143 when a sibling was found. When none is found, return nil and don't
19145 (let ((fun (if previous 're-search-backward 're-search-forward))
19147 (re (concat "^" outline-regexp))
19149 (when (condition-case nil (org-back-to-heading t) (error nil))
19150 (setq level (funcall outline-level))
19152 (or previous (forward-char 1))
19153 (while (funcall fun re nil t)
19154 (setq l (funcall outline-level))
19155 (when (< l level) (goto-char pos) (throw 'exit nil))
19156 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
19160 (defun org-show-siblings ()
19161 "Show all siblings of the current headline."
19163 (while (org-goto-sibling) (org-flag-heading nil)))
19165 (while (org-goto-sibling 'previous)
19166 (org-flag-heading nil))))
19168 (defun org-show-hidden-entry ()
19169 "Show an entry where even the heading is hidden."
19173 (defun org-flag-heading (flag &optional entry)
19174 "Flag the current heading. FLAG non-nil means make invisible.
19175 When ENTRY is non-nil, show the entire entry."
19177 (org-back-to-heading t)
19178 ;; Check if we should show the entire entry
19183 (and (outline-next-heading)
19184 (org-flag-heading nil))))
19185 (outline-flag-region (max (point-min) (1- (point)))
19186 (save-excursion (outline-end-of-heading) (point))
19189 (defun org-get-next-sibling ()
19190 "Move to next heading of the same level, and return point.
19191 If there is no such heading, return nil.
19192 This is like outline-next-sibling, but invisible headings are ok."
19193 (let ((level (funcall outline-level)))
19194 (outline-next-heading)
19195 (while (and (not (eobp)) (> (funcall outline-level) level))
19196 (outline-next-heading))
19197 (if (or (eobp) (< (funcall outline-level) level))
19201 (defun org-get-last-sibling ()
19202 "Move to previous heading of the same level, and return point.
19203 If there is no such heading, return nil."
19204 (let ((opoint (point))
19205 (level (funcall outline-level)))
19206 (outline-previous-heading)
19207 (when (and (/= (point) opoint) (outline-on-heading-p t))
19208 (while (and (> (funcall outline-level) level)
19210 (outline-previous-heading))
19211 (if (< (funcall outline-level) level)
19215 (defun org-end-of-subtree (&optional invisible-OK to-heading)
19216 ;; This contains an exact copy of the original function, but it uses
19217 ;; `org-back-to-heading', to make it work also in invisible
19218 ;; trees. And is uses an invisible-OK argument.
19219 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
19220 ;; Furthermore, when used inside Org, finding the end of a large subtree
19221 ;; with many children and grandchildren etc, this can be much faster
19222 ;; than the outline version.
19223 (org-back-to-heading invisible-OK)
19225 (level (funcall outline-level)))
19226 (if (and (org-mode-p) (< level 1000))
19227 ;; A true heading (not a plain list item), in Org-mode
19228 ;; This means we can easily find the end by looking
19229 ;; only for the right number of stars. Using a regexp to do
19230 ;; this is so much faster than using a Lisp loop.
19231 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
19233 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
19234 ;; something else, do it the slow way
19235 (while (and (not (eobp))
19236 (or first (> (funcall outline-level) level)))
19238 (outline-next-heading)))
19240 (if (memq (preceding-char) '(?\n ?\^M))
19242 ;; Go to end of line before heading
19244 (if (memq (preceding-char) '(?\n ?\^M))
19245 ;; leave blank line before heading
19246 (forward-char -1))))))
19249 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
19250 "Use Org version in org-mode, for dramatic speed-up."
19251 (if (eq major-mode 'org-mode)
19253 (org-end-of-subtree nil t)
19254 (unless (eobp) (backward-char 1)))
19257 (defun org-forward-same-level (arg &optional invisible-ok)
19258 "Move forward to the arg'th subheading at same level as this one.
19259 Stop at the first and last subheadings of a superior heading."
19261 (org-back-to-heading invisible-ok)
19263 (let* ((level (- (match-end 0) (match-beginning 0) 1))
19264 (re (format "^\\*\\{1,%d\\} " level))
19268 (while (and (re-search-forward re nil 'move)
19269 (setq l (- (match-end 0) (match-beginning 0) 1))
19272 (progn (backward-char 1) (org-invisible-p)))
19273 (if (< l level) (setq arg 1)))
19274 (setq arg (1- arg)))
19275 (beginning-of-line 1)))
19277 (defun org-backward-same-level (arg &optional invisible-ok)
19278 "Move backward to the arg'th subheading at same level as this one.
19279 Stop at the first and last subheadings of a superior heading."
19281 (org-back-to-heading)
19283 (let* ((level (- (match-end 0) (match-beginning 0) 1))
19284 (re (format "^\\*\\{1,%d\\} " level))
19287 (while (and (re-search-backward re nil 'move)
19288 (setq l (- (match-end 0) (match-beginning 0) 1))
19292 (if (< l level) (setq arg 1)))
19293 (setq arg (1- arg)))))
19295 (defun org-show-subtree ()
19296 "Show everything after this heading at deeper levels."
19297 (outline-flag-region
19300 (org-end-of-subtree t t))
19303 (defun org-show-entry ()
19304 "Show the body directly following this heading.
19305 Show the heading too, if it is currently invisible."
19308 (condition-case nil
19310 (org-back-to-heading t)
19311 (outline-flag-region
19312 (max (point-min) (1- (point)))
19314 (if (re-search-forward
19315 (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
19316 (match-beginning 1)
19319 (org-cycle-hide-drawers 'children))
19322 (defun org-make-options-regexp (kwds &optional extra)
19323 "Make a regular expression for keyword lines."
19327 (mapconcat 'regexp-quote kwds "\\|")
19328 (if extra (concat "\\|" extra))
19332 ;; Make isearch reveal the necessary context
19333 (defun org-isearch-end ()
19334 "Reveal context after isearch exits."
19335 (when isearch-success ; only if search was successful
19336 (if (featurep 'xemacs)
19337 ;; Under XEmacs, the hook is run in the correct place,
19338 ;; we directly show the context.
19339 (org-show-context 'isearch)
19340 ;; In Emacs the hook runs *before* restoring the overlays.
19341 ;; So we have to use a one-time post-command-hook to do this.
19342 ;; (Emacs 22 has a special variable, see function `org-mode')
19343 (unless (and (boundp 'isearch-mode-end-hook-quit)
19344 isearch-mode-end-hook-quit)
19345 ;; Only when the isearch was not quitted.
19346 (org-add-hook 'post-command-hook 'org-isearch-post-command
19347 'append 'local)))))
19349 (defun org-isearch-post-command ()
19350 "Remove self from hook, and show context."
19351 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
19352 (org-show-context 'isearch))
19355 ;;;; Integration with and fixes for other packages
19359 (defvar org-imenu-markers nil
19360 "All markers currently used by Imenu.")
19361 (make-variable-buffer-local 'org-imenu-markers)
19363 (defun org-imenu-new-marker (&optional pos)
19364 "Return a new marker for use by Imenu, and remember the marker."
19365 (let ((m (make-marker)))
19366 (move-marker m (or pos (point)))
19367 (push m org-imenu-markers)
19370 (defun org-imenu-get-tree ()
19371 "Produce the index for Imenu."
19372 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
19373 (setq org-imenu-markers nil)
19374 (let* ((n org-imenu-depth)
19375 (re (concat "^" outline-regexp))
19376 (subs (make-vector (1+ n) nil))
19382 (goto-char (point-max))
19383 (while (re-search-backward re nil t)
19384 (setq level (org-reduced-level (funcall outline-level)))
19386 (looking-at org-complex-heading-regexp)
19387 (setq head (org-link-display-format
19388 (org-match-string-no-properties 4))
19389 m (org-imenu-new-marker))
19390 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
19391 (if (>= level last-level)
19392 (push (cons head m) (aref subs level))
19393 (push (cons head (aref subs (1+ level))) (aref subs level))
19394 (loop for i from (1+ level) to n do (aset subs i nil)))
19395 (setq last-level level)))))
19398 (eval-after-load "imenu"
19400 (add-hook 'imenu-after-jump-hook
19402 (if (eq major-mode 'org-mode)
19403 (org-show-context 'org-goto))))))
19405 (defun org-link-display-format (link)
19406 "Replace a link with either the description, or the link target
19407 if no description is present"
19409 (if (string-match org-bracket-link-analytic-regexp link)
19410 (replace-match (if (match-end 5)
19411 (match-string 5 link)
19412 (concat (match-string 1 link)
19413 (match-string 3 link)))
19417 ;; Speedbar support
19419 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
19420 "Overlay marking the agenda restriction line in speedbar.")
19421 (overlay-put org-speedbar-restriction-lock-overlay
19422 'face 'org-agenda-restriction-lock)
19423 (overlay-put org-speedbar-restriction-lock-overlay
19424 'help-echo "Agendas are currently limited to this item.")
19425 (org-detach-overlay org-speedbar-restriction-lock-overlay)
19427 (defun org-speedbar-set-agenda-restriction ()
19428 "Restrict future agenda commands to the location at point in speedbar.
19429 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
19431 (require 'org-agenda)
19432 (let (p m tp np dir txt)
19434 ((setq p (text-property-any (point-at-bol) (point-at-eol)
19436 (setq m (get-text-property p 'org-imenu-marker))
19437 (with-current-buffer (marker-buffer m)
19439 (org-agenda-set-restriction-lock 'subtree)))
19440 ((setq p (text-property-any (point-at-bol) (point-at-eol)
19441 'speedbar-function 'speedbar-find-file))
19442 (setq tp (previous-single-property-change
19443 (1+ p) 'speedbar-function)
19444 np (next-single-property-change
19445 tp 'speedbar-function)
19446 dir (speedbar-line-directory)
19447 txt (buffer-substring-no-properties (or tp (point-min))
19448 (or np (point-max))))
19449 (with-current-buffer (find-file-noselect
19450 (let ((default-directory dir))
19451 (expand-file-name txt)))
19452 (unless (org-mode-p)
19453 (error "Cannot restrict to non-Org-mode file"))
19454 (org-agenda-set-restriction-lock 'file)))
19455 (t (error "Don't know how to restrict Org-mode's agenda")))
19456 (move-overlay org-speedbar-restriction-lock-overlay
19457 (point-at-bol) (point-at-eol))
19458 (setq current-prefix-arg nil)
19459 (org-agenda-maybe-redo)))
19461 (eval-after-load "speedbar"
19463 (speedbar-add-supported-extension ".org")
19464 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
19465 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
19466 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
19467 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
19468 (add-hook 'speedbar-visiting-tag-hook
19469 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
19471 ;;; Fixes and Hacks for problems with other packages
19473 ;; Make flyspell not check words in links, to not mess up our keymap
19474 (defun org-mode-flyspell-verify ()
19475 "Don't let flyspell put overlays at active buttons."
19476 (and (not (get-text-property (max (1- (point)) (point-min)) 'keymap))
19477 (not (get-text-property (max (1- (point)) (point-min)) 'org-no-flyspell))))
19479 (defun org-remove-flyspell-overlays-in (beg end)
19480 "Remove flyspell overlays in region."
19481 (and (org-bound-and-true-p flyspell-mode)
19482 (fboundp 'flyspell-delete-region-overlays)
19483 (flyspell-delete-region-overlays beg end))
19484 (add-text-properties beg end '(org-no-flyspell t)))
19486 ;; Make `bookmark-jump' shows the jump location if it was hidden.
19487 (eval-after-load "bookmark"
19488 '(if (boundp 'bookmark-after-jump-hook)
19489 ;; We can use the hook
19490 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
19491 ;; Hook not available, use advice
19492 (defadvice bookmark-jump (after org-make-visible activate)
19493 "Make the position visible."
19494 (org-bookmark-jump-unhide))))
19496 ;; Make sure saveplace shows the location if it was hidden
19497 (eval-after-load "saveplace"
19498 '(defadvice save-place-find-file-hook (after org-make-visible activate)
19499 "Make the position visible."
19500 (org-bookmark-jump-unhide)))
19502 ;; Make sure ecb shows the location if it was hidden
19503 (eval-after-load "ecb"
19504 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
19505 "Make hierarchy visible when jumping into location from ECB tree buffer."
19506 (if (eq major-mode 'org-mode)
19507 (org-show-context))))
19509 (defun org-bookmark-jump-unhide ()
19510 "Unhide the current position, to show the bookmark location."
19512 (or (org-invisible-p)
19513 (save-excursion (goto-char (max (point-min) (1- (point))))
19514 (org-invisible-p)))
19515 (org-show-context 'bookmark-jump)))
19517 ;; Make session.el ignore our circular variable
19518 (eval-after-load "session"
19519 '(add-to-list 'session-globals-exclude 'org-mark-ring))
19521 ;;;; Experimental code
19523 (defun org-closed-in-range ()
19524 "Sparse tree of items closed in a certain time range.
19525 Still experimental, may disappear in the future."
19527 ;; Get the time interval from the user.
19528 (let* ((time1 (org-float-time
19529 (org-read-date nil 'to-time nil "Starting date: ")))
19530 (time2 (org-float-time
19531 (org-read-date nil 'to-time nil "End date:")))
19532 ;; callback function
19533 (callback (lambda ()
19536 (apply 'encode-time
19537 (org-parse-time-string
19538 (match-string 1))))))
19539 ;; check if time in interval
19540 (and (>= time time1) (<= time time2))))))
19541 ;; make tree, check each match with the callback
19542 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
19548 (run-hooks 'org-load-hook)
19550 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
19552 ;;; org.el ends here