1 ;;; org.el --- Outline-based notes management and organizer
3 ;; Carstens outline-mode for keeping track of everything.
4 ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
8 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
30 ;; project planning with a fast and effective plain-text system.
32 ;; Org-mode develops organizational tasks around NOTES files that contain
33 ;; information about projects as plain text. Org-mode is implemented on
34 ;; top of outline-mode, which makes it possible to keep the content of
35 ;; large files well structured. Visibility cycling and structure editing
36 ;; help to work with the tree. Tables are easily created with a built-in
37 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
38 ;; and scheduling. It dynamically compiles entries into an agenda that
39 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
40 ;; Plain text URL-like links connect to websites, emails, Usenet
41 ;; messages, BBDB entries, and any files related to the projects. For
42 ;; printing and sharing of notes, an Org-mode file can be exported as a
43 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
44 ;; iCalendar file. It can also serve as a publishing tool for a set of
47 ;; Installation and Activation
48 ;; ---------------------------
49 ;; See the corresponding sections in the manual at
51 ;; http://orgmode.org/org.html#Installation
55 ;; The documentation of Org-mode can be found in the TeXInfo file. The
56 ;; distribution also contains a PDF version of it. At the homepage of
57 ;; Org-mode, you can read the same text online as HTML. There is also an
58 ;; excellent reference card made by Philip Rooke. This card can be found
59 ;; in the etc/ directory of Emacs 22.
61 ;; A list of recent changes can be found at
62 ;; http://orgmode.org/Changes.html
66 (defvar org-inhibit-highlight-removal nil
) ; dynamically scoped param
67 (defvar org-table-formula-constants-local nil
68 "Local version of `org-table-formula-constants'.")
69 (make-variable-buffer-local 'org-table-formula-constants-local
)
71 ;;;; Require other packages
79 (require 'format-spec
)
81 (load "org-loaddefs.el" t t
)
83 ;; `org-outline-regexp' ought to be a defconst but is let-binding in
84 ;; some places -- e.g. see the macro org-with-limited-levels.
86 ;; In Org buffers, the value of `outline-regexp' is that of
87 ;; `org-outline-regexp'. The only function still directly relying on
88 ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
89 ;; job when `orgstruct-mode' is active.
90 (defvar org-outline-regexp
"\\*+ "
91 "Regexp to match Org headlines.")
93 (defvar org-outline-regexp-bol
"^\\*+ "
94 "Regexp to match Org headlines.
95 This is similar to `org-outline-regexp' but additionally makes
96 sure that we are at the beginning of the line.")
98 (defvar org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
99 "Matches an headline, putting stars and text into groups.
100 Stars are put in group 1 and the trimmed body in group 2.")
102 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
103 (when (fboundp 'defvaralias
)
104 (unless (boundp 'calendar-view-holidays-initially-flag
)
105 (defvaralias 'calendar-view-holidays-initially-flag
106 'view-calendar-holidays-initially
))
107 (unless (boundp 'calendar-view-diary-initially-flag
)
108 (defvaralias 'calendar-view-diary-initially-flag
109 'view-diary-entries-initially
))
110 (unless (boundp 'diary-fancy-buffer
)
111 (defvaralias 'diary-fancy-buffer
'fancy-diary-buffer
)))
113 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
114 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
115 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
116 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
117 (declare-function org-at-clock-log-p
"org-clock" ())
118 (declare-function org-clock-timestamps-up
"org-clock" ())
119 (declare-function org-clock-timestamps-down
"org-clock" ())
120 (declare-function org-clock-sum-current-item
"org-clock" (&optional tstart
))
122 (declare-function orgtbl-mode
"org-table" (&optional arg
))
123 (declare-function org-clock-out
"org-clock" (&optional switch-to-state fail-quietly at-time
))
124 (declare-function org-beamer-mode
"org-beamer" ())
125 (declare-function org-table-edit-field
"org-table" (arg))
126 (declare-function org-table-justify-field-maybe
"org-table" (&optional new
))
127 (declare-function org-id-get-create
"org-id" (&optional force
))
128 (declare-function org-id-find-id-file
"org-id" (id))
129 (declare-function org-tags-view
"org-agenda" (&optional todo-only match
))
130 (declare-function org-agenda-list
"org-agenda" (&optional arg start-day span
))
131 (declare-function org-table-align
"org-table" ())
132 (declare-function org-table-paste-rectangle
"org-table" ())
133 (declare-function org-table-maybe-eval-formula
"org-table" ())
134 (declare-function org-table-maybe-recalculate-line
"org-table" ())
136 ;; load languages based on value of `org-babel-load-languages'
137 (defvar org-babel-load-languages
)
140 (defun org-babel-do-load-languages (sym value
)
141 "Load the languages defined in `org-babel-load-languages'."
142 (set-default sym value
)
144 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
147 (require (intern (concat "ob-" lang
))))
149 (funcall 'fmakunbound
150 (intern (concat "org-babel-execute:" lang
)))
151 (funcall 'fmakunbound
152 (intern (concat "org-babel-expand-body:" lang
)))))))
153 org-babel-load-languages
))
155 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
156 "Languages which can be evaluated in Org-mode buffers.
157 This list can be used to load support for any of the languages
158 below, note that each language will depend on a different set of
159 system executables and/or Emacs modes. When a language is
160 \"loaded\", then code blocks in that language can be evaluated
161 with `org-babel-execute-src-block' bound by default to C-c
162 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
163 be set to remove code block evaluation from the C-c C-c
164 keybinding. By default only Emacs Lisp (which has no
165 requirements) is loaded."
167 :set
'org-babel-do-load-languages
169 :type
'(alist :tag
"Babel Languages"
172 (const :tag
"Awk" awk
)
175 (const :tag
"Asymptote" asymptote
)
176 (const :tag
"Calc" calc
)
177 (const :tag
"Clojure" clojure
)
178 (const :tag
"CSS" css
)
179 (const :tag
"Ditaa" ditaa
)
180 (const :tag
"Dot" dot
)
181 (const :tag
"Emacs Lisp" emacs-lisp
)
182 (const :tag
"Fortran" fortran
)
183 (const :tag
"Gnuplot" gnuplot
)
184 (const :tag
"Haskell" haskell
)
186 (const :tag
"Java" java
)
187 (const :tag
"Javascript" js
)
188 (const :tag
"LaTeX" latex
)
189 (const :tag
"Ledger" ledger
)
190 (const :tag
"Lilypond" lilypond
)
191 (const :tag
"Lisp" lisp
)
192 (const :tag
"Maxima" maxima
)
193 (const :tag
"Matlab" matlab
)
194 (const :tag
"Mscgen" mscgen
)
195 (const :tag
"Ocaml" ocaml
)
196 (const :tag
"Octave" octave
)
197 (const :tag
"Org" org
)
198 (const :tag
"Perl" perl
)
199 (const :tag
"Pico Lisp" picolisp
)
200 (const :tag
"PlantUML" plantuml
)
201 (const :tag
"Python" python
)
202 (const :tag
"Ruby" ruby
)
203 (const :tag
"Sass" sass
)
204 (const :tag
"Scala" scala
)
205 (const :tag
"Scheme" scheme
)
206 (const :tag
"Screen" screen
)
207 (const :tag
"Shell Script" sh
)
208 (const :tag
"Shen" shen
)
209 (const :tag
"Sql" sql
)
210 (const :tag
"Sqlite" sqlite
))
211 :value-type
(boolean :tag
"Activate" :value t
)))
213 ;;;; Customization variables
214 (defcustom org-clone-delete-id nil
215 "Remove ID property of clones of a subtree.
216 When non-nil, clones of a subtree don't inherit the ID property.
217 Otherwise they inherit the ID property with a new unique
224 (require 'org-compat
)
228 (defun org-version (&optional here full message
)
229 "Show the org-mode version in the echo area.
230 With prefix argument HERE, insert it at point.
231 When FULL is non-nil, use a verbose version string.
232 When MESSAGE is non-nil, display a message with the version."
234 (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
235 (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs.el")))
237 (and (fboundp 'org-release
) (fboundp 'org-git-version
))
238 (load (concat org-dir
"org-version.el")
239 'noerror
'nomessage
'nosuffix
)))
240 (org-version (org-release))
241 (git-version (org-git-version))
242 (version (format "Org-mode version %s (%s @ %s)"
246 (if (string= org-dir org-install-dir
)
248 (concat "mixed installation! " org-install-dir
" and " org-dir
))
249 "org-loaddefs.el can not be found!")))
250 (_version (if full version org-version
)))
251 (if (org-called-interactively-p 'interactive
)
255 (if message
(message _version
))
258 (defconst org-version
(org-version))
260 ;;; Compatibility constants
262 ;;; The custom variables
265 "Outline-based notes management and organizer."
270 (defcustom org-mode-hook nil
271 "Mode hook for Org-mode, run after the mode was turned on."
275 (defcustom org-load-hook nil
276 "Hook that is run after org.el has been loaded."
280 (defcustom org-log-buffer-setup-hook nil
281 "Hook that is run after an Org log buffer is created."
286 (defvar org-modules
) ; defined below
287 (defvar org-modules-loaded nil
288 "Have the modules been loaded already?")
290 (defun org-load-modules-maybe (&optional force
)
291 "Load all extensions listed in `org-modules'."
292 (when (or force
(not org-modules-loaded
))
294 (condition-case nil
(require ext
)
295 (error (message "Problems while trying to load feature `%s'" ext
))))
297 (setq org-modules-loaded t
)))
299 (defun org-set-modules (var value
)
300 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
302 (when (featurep 'org
)
303 (org-load-modules-maybe 'force
)))
305 (when (org-bound-and-true-p org-modules
)
306 (let ((a (member 'org-infojs org-modules
)))
307 (and a
(setcar a
'org-jsinfo
))))
309 (defcustom org-modules
'(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl
)
310 "Modules that should always be loaded together with org.el.
311 If a description starts with <C>, the file is not part of Emacs
312 and loading it will require that you have downloaded and properly installed
313 the org-mode distribution.
315 You can also use this system to load external packages (i.e. neither Org
316 core modules, nor modules from the CONTRIB directory). Just add symbols
317 to the end of the list. If the package is called org-xyz.el, then you need
318 to add the symbol `xyz', and the package must have a call to
322 :set
'org-set-modules
325 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
326 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
327 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
328 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
329 (const :tag
" docview: Links to doc-view buffers" org-docview
)
330 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
331 (const :tag
" id: Global IDs for identifying entries" org-id
)
332 (const :tag
" info: Links to Info nodes" org-info
)
333 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
334 (const :tag
" habit: Track your consistency with habits" org-habit
)
335 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
336 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
337 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
338 (const :tag
" mew Links to Mew folders/messages" org-mew
)
339 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
340 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
341 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
342 (const :tag
" special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks
)
343 (const :tag
" vm: Links to VM folders/messages" org-vm
)
344 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
345 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
346 (const :tag
" mouse: Additional mouse support" org-mouse
)
347 (const :tag
" TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler
)
349 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
350 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
351 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
352 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
353 (const :tag
"C collector: Collect properties into tables" org-collector
)
354 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
355 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
356 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
357 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
358 (const :tag
"C eval: Include command output as text" org-eval
)
359 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
360 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
361 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
362 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
363 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
365 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
367 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
368 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
369 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
370 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
371 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
372 (const :tag
"C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber
)
373 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
374 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
375 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
376 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
377 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
378 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
379 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
380 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
381 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
382 (const :tag
"C track: Keep up with Org-mode development" org-track
)
383 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
384 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
385 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
387 (defcustom org-support-shift-select nil
388 "Non-nil means make shift-cursor commands select text when possible.
390 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
391 start selecting a region, or enlarge regions started in this way.
392 In Org-mode, in special contexts, these same keys are used for
393 other purposes, important enough to compete with shift selection.
394 Org tries to balance these needs by supporting `shift-select-mode'
395 outside these special contexts, under control of this variable.
397 The default of this variable is nil, to avoid confusing behavior. Shifted
398 cursor keys will then execute Org commands in the following contexts:
399 - on a headline, changing TODO state (left/right) and priority (up/down)
400 - on a time stamp, changing the time
401 - in a plain list item, changing the bullet type
402 - in a property definition line, switching between allowed values
403 - in the BEGIN line of a clock table (changing the time block).
404 Outside these contexts, the commands will throw an error.
406 When this variable is t and the cursor is not in a special
407 context, Org-mode will support shift-selection for making and
408 enlarging regions. To make this more effective, the bullet
409 cycling will no longer happen anywhere in an item line, but only
410 if the cursor is exactly on the bullet.
412 If you set this variable to the symbol `always', then the keys
413 will not be special in headlines, property lines, and item lines,
414 to make shift selection work there as well. If this is what you
415 want, you can use the following alternative commands: `C-c C-t'
416 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
417 can be used to switch TODO sets, `C-c -' to cycle item bullet
418 types, and properties can be edited by hand or in column view.
420 However, when the cursor is on a timestamp, shift-cursor commands
421 will still edit the time stamp - this is just too good to give up.
423 XEmacs user should have this variable set to nil, because
424 `shift-select-mode' is in Emacs 23 or later only."
427 (const :tag
"Never" nil
)
428 (const :tag
"When outside special context" t
)
429 (const :tag
"Everywhere except timestamps" always
)))
431 (defcustom org-loop-over-headlines-in-active-region nil
432 "Shall some commands act upon headlines in the active region?
434 When set to `t', some commands will be performed in all headlines
435 within the active region.
437 When set to `start-level', some commands will be performed in all
438 headlines within the active region, provided that these headlines
439 are of the same level than the first one.
441 When set to a string, those commands will be performed on the
442 matching headlines within the active region. Such string must be
443 a tags/property/todo match as it is used in the agenda tags view.
445 The list of commands is: `org-schedule', `org-deadline',
446 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
447 `org-archive-to-archive-sibling'. The archiving commands skip
448 already archived entries."
449 :type
'(choice (const :tag
"Don't loop" nil
)
450 (const :tag
"All headlines in active region" t
)
451 (const :tag
"In active region, headlines at the same level than the first one" 'start-level
)
452 (string :tag
"Tags/Property/Todo matcher"))
457 (defgroup org-startup nil
458 "Options concerning startup of Org-mode."
462 (defcustom org-startup-folded t
463 "Non-nil means entering Org-mode will switch to OVERVIEW.
464 This can also be configured on a per-file basis by adding one of
465 the following lines anywhere in the buffer:
467 #+STARTUP: fold (or `overview', this is equivalent)
468 #+STARTUP: nofold (or `showall', this is equivalent)
470 #+STARTUP: showeverything
472 By default, this option is ignored when Org opens agenda files
473 for the first time. If you want the agenda to honor the startup
474 option, set `org-agenda-inhibit-startup' to nil."
477 (const :tag
"nofold: show all" nil
)
478 (const :tag
"fold: overview" t
)
479 (const :tag
"content: all headlines" content
)
480 (const :tag
"show everything, even drawers" showeverything
)))
482 (defcustom org-startup-truncated t
483 "Non-nil means entering Org-mode will set `truncate-lines'.
484 This is useful since some lines containing links can be very long and
485 uninteresting. Also tables look terrible when wrapped."
489 (defcustom org-startup-indented nil
490 "Non-nil means turn on `org-indent-mode' on startup.
491 This can also be configured on a per-file basis by adding one of
492 the following lines anywhere in the buffer:
496 :group
'org-structure
498 (const :tag
"Not" nil
)
499 (const :tag
"Globally (slow on startup in large files)" t
)))
501 (defcustom org-use-sub-superscripts t
502 "Non-nil means interpret \"_\" and \"^\" for export.
503 When this option is turned on, you can use TeX-like syntax for sub- and
504 superscripts. Several characters after \"_\" or \"^\" will be
505 considered as a single item - so grouping with {} is normally not
506 needed. For example, the following things will be parsed as single
507 sub- or superscripts.
509 10^24 or 10^tau several digits will be considered 1 item.
510 10^-12 or 10^-tau a leading sign with digits or a word
511 x^2-y^3 will be read as x^2 - y^3, because items are
512 terminated by almost any nonword/nondigit char.
513 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
515 Still, ambiguity is possible - so when in doubt use {} to enclose the
516 sub/superscript. If you set this variable to the symbol `{}',
517 the braces are *required* in order to trigger interpretations as
518 sub/superscript. This can be helpful in documents that need \"_\"
519 frequently in plain text.
521 Not all export backends support this, but HTML does.
523 This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
525 :group
'org-export-translation
528 (const :tag
"Always interpret" t
)
529 (const :tag
"Only with braces" {})
530 (const :tag
"Never interpret" nil
)))
532 (if (fboundp 'defvaralias
)
533 (defvaralias 'org-export-with-sub-superscripts
'org-use-sub-superscripts
))
536 (defcustom org-startup-with-beamer-mode nil
537 "Non-nil means turn on `org-beamer-mode' on startup.
538 This can also be configured on a per-file basis by adding one of
539 the following lines anywhere in the buffer:
546 (defcustom org-startup-align-all-tables nil
547 "Non-nil means align all tables when visiting a file.
548 This is useful when the column width in tables is forced with <N> cookies
549 in table fields. Such tables will look correct only after the first re-align.
550 This can also be configured on a per-file basis by adding one of
551 the following lines anywhere in the buffer:
557 (defcustom org-startup-with-inline-images nil
558 "Non-nil means show inline images when loading a new Org file.
559 This can also be configured on a per-file basis by adding one of
560 the following lines anywhere in the buffer:
561 #+STARTUP: inlineimages
562 #+STARTUP: noinlineimages"
567 (defcustom org-insert-mode-line-in-empty-file nil
568 "Non-nil means insert the first line setting Org-mode in empty files.
569 When the function `org-mode' is called interactively in an empty file, this
570 normally means that the file name does not automatically trigger Org-mode.
571 To ensure that the file will always be in Org-mode in the future, a
572 line enforcing Org-mode will be inserted into the buffer, if this option
577 (defcustom org-replace-disputed-keys nil
578 "Non-nil means use alternative key bindings for some keys.
579 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
580 These keys are also used by other packages like shift-selection-mode'
581 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
582 If you want to use Org-mode together with one of these other modes,
583 or more generally if you would like to move some Org-mode commands to
584 other keys, set this variable and configure the keys with the variable
587 This option is only relevant at load-time of Org-mode, and must be set
588 *before* org.el is loaded. Changing it requires a restart of Emacs to
593 (defcustom org-use-extra-keys nil
594 "Non-nil means use extra key sequence definitions for certain commands.
595 This happens automatically if you run XEmacs or if `window-system'
596 is nil. This variable lets you do the same manually. You must
597 set it before loading org.
599 Example: on Carbon Emacs 22 running graphically, with an external
600 keyboard on a Powerbook, the default way of setting M-left might
601 not work for either Alt or ESC. Setting this variable will make
606 (if (fboundp 'defvaralias
)
607 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
609 (defcustom org-disputed-keys
610 '(([(shift up
)] .
[(meta p
)])
611 ([(shift down
)] .
[(meta n
)])
612 ([(shift left
)] .
[(meta -
)])
613 ([(shift right
)] .
[(meta +)])
614 ([(control shift right
)] .
[(meta shift
+)])
615 ([(control shift left
)] .
[(meta shift -
)]))
616 "Keys for which Org-mode and other modes compete.
617 This is an alist, cars are the default keys, second element specifies
618 the alternative to use when `org-replace-disputed-keys' is t.
620 Keys can be specified in any syntax supported by `define-key'.
621 The value of this option takes effect only at Org-mode's startup,
622 therefore you'll have to restart Emacs to apply it after changing."
627 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
628 Or return the original if not disputed.
629 Also apply the translations defined in `org-xemacs-key-equivalents'."
630 (when org-replace-disputed-keys
631 (let* ((nkey (key-description key
))
632 (x (org-find-if (lambda (x)
633 (equal (key-description (car x
)) nkey
))
635 (setq key
(if x
(cdr x
) key
))))
636 (when (featurep 'xemacs
)
637 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
640 (defun org-find-if (predicate seq
)
643 (if (funcall predicate
(car seq
))
644 (throw 'exit
(car seq
))
647 (defun org-defkey (keymap key def
)
648 "Define a key, possibly translated, as returned by `org-key'."
649 (define-key keymap
(org-key key
) def
))
651 (defcustom org-ellipsis nil
652 "The ellipsis to use in the Org-mode outline.
653 When nil, just use the standard three dots. When a string, use that instead,
654 When a face, use the standard 3 dots, but with the specified face.
655 The change affects only Org-mode (which will then use its own display table).
656 Changing this requires executing `M-x org-mode' in a buffer to become
659 :type
'(choice (const :tag
"Default" nil
)
660 (face :tag
"Face" :value org-warning
)
661 (string :tag
"String" :value
"...#")))
663 (defvar org-display-table nil
664 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
666 (defgroup org-keywords nil
667 "Keywords in Org-mode."
671 (defcustom org-deadline-string
"DEADLINE:"
672 "String to mark deadline entries.
673 A deadline is this string, followed by a time stamp. Should be a word,
674 terminated by a colon. You can insert a schedule keyword and
675 a timestamp with \\[org-deadline].
676 Changes become only effective after restarting Emacs."
680 (defcustom org-scheduled-string
"SCHEDULED:"
681 "String to mark scheduled TODO entries.
682 A schedule is this string, followed by a time stamp. Should be a word,
683 terminated by a colon. You can insert a schedule keyword and
684 a timestamp with \\[org-schedule].
685 Changes become only effective after restarting Emacs."
689 (defcustom org-closed-string
"CLOSED:"
690 "String used as the prefix for timestamps logging closing a TODO entry."
694 (defcustom org-clock-string
"CLOCK:"
695 "String used as prefix for timestamps clocking work hours on an item."
699 (defconst org-planning-or-clock-line-re
(concat "^[ \t]*\\("
700 org-scheduled-string
"\\|"
701 org-deadline-string
"\\|"
702 org-closed-string
"\\|"
703 org-clock-string
"\\)")
704 "Matches a line with planning or clock info.")
706 (defcustom org-comment-string
"COMMENT"
707 "Entries starting with this keyword will never be exported.
708 An entry can be toggled between COMMENT and normal with
709 \\[org-toggle-comment].
710 Changes become only effective after restarting Emacs."
714 (defcustom org-quote-string
"QUOTE"
715 "Entries starting with this keyword will be exported in fixed-width font.
716 Quoting applies only to the text in the entry following the headline, and does
717 not extend beyond the next headline, even if that is lower level.
718 An entry can be toggled between QUOTE and normal with
719 \\[org-toggle-fixed-width-section]."
723 (defconst org-repeat-re
724 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
725 "Regular expression for specifying repeated events.
726 After a match, group 1 contains the repeat expression.")
728 (defgroup org-structure nil
729 "Options concerning the general structure of Org-mode files."
733 (defgroup org-reveal-location nil
734 "Options about how to make context of a location visible."
735 :tag
"Org Reveal Location"
736 :group
'org-structure
)
738 (defconst org-context-choice
740 (const :tag
"Always" t
)
741 (const :tag
"Never" nil
)
742 (repeat :greedy t
:tag
"Individual contexts"
744 (choice :tag
"Context"
751 (const bookmark-jump
)
755 "Contexts for the reveal options.")
757 (defcustom org-show-hierarchy-above
'((default . t
))
758 "Non-nil means show full hierarchy when revealing a location.
759 Org-mode often shows locations in an org-mode file which might have
760 been invisible before. When this is set, the hierarchy of headings
761 above the exposed location is shown.
762 Turning this off for example for sparse trees makes them very compact.
763 Instead of t, this can also be an alist specifying this option for different
764 contexts. Valid contexts are
765 agenda when exposing an entry from the agenda
766 org-goto when using the command `org-goto' on key C-c C-j
767 occur-tree when using the command `org-occur' on key C-c /
768 tags-tree when constructing a sparse tree based on tags matches
769 link-search when exposing search matches associated with a link
770 mark-goto when exposing the jump goal of a mark
771 bookmark-jump when exposing a bookmark location
772 isearch when exiting from an incremental search
773 default default for all contexts not set explicitly"
774 :group
'org-reveal-location
775 :type org-context-choice
)
777 (defcustom org-show-following-heading
'((default . nil
))
778 "Non-nil means show following heading when revealing a location.
779 Org-mode often shows locations in an org-mode file which might have
780 been invisible before. When this is set, the heading following the
782 Turning this off for example for sparse trees makes them very compact,
783 but makes it harder to edit the location of the match. In such a case,
784 use the command \\[org-reveal] to show more context.
785 Instead of t, this can also be an alist specifying this option for different
786 contexts. See `org-show-hierarchy-above' for valid contexts."
787 :group
'org-reveal-location
788 :type org-context-choice
)
790 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
791 "Non-nil means show all sibling heading when revealing a location.
792 Org-mode often shows locations in an org-mode file which might have
793 been invisible before. When this is set, the sibling of the current entry
794 heading are all made visible. If `org-show-hierarchy-above' is t,
795 the same happens on each level of the hierarchy above the current entry.
797 By default this is on for the isearch context, off for all other contexts.
798 Turning this off for example for sparse trees makes them very compact,
799 but makes it harder to edit the location of the match. In such a case,
800 use the command \\[org-reveal] to show more context.
801 Instead of t, this can also be an alist specifying this option for different
802 contexts. See `org-show-hierarchy-above' for valid contexts."
803 :group
'org-reveal-location
804 :type org-context-choice
)
806 (defcustom org-show-entry-below
'((default . nil
))
807 "Non-nil means show the entry below a headline when revealing a location.
808 Org-mode often shows locations in an org-mode file which might have
809 been invisible before. When this is set, the text below the headline that is
810 exposed is also shown.
812 By default this is off for all contexts.
813 Instead of t, this can also be an alist specifying this option for different
814 contexts. See `org-show-hierarchy-above' for valid contexts."
815 :group
'org-reveal-location
816 :type org-context-choice
)
818 (defcustom org-indirect-buffer-display
'other-window
819 "How should indirect tree buffers be displayed?
820 This applies to indirect buffers created with the commands
821 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
823 current-window Display in the current window
824 other-window Just display in another window.
825 dedicated-frame Create one new frame, and re-use it each time.
826 new-frame Make a new frame each time. Note that in this case
827 previously-made indirect buffers are kept, and you need to
828 kill these buffers yourself."
829 :group
'org-structure
830 :group
'org-agenda-windows
832 (const :tag
"In current window" current-window
)
833 (const :tag
"In current frame, other window" other-window
)
834 (const :tag
"Each time a new frame" new-frame
)
835 (const :tag
"One dedicated frame" dedicated-frame
)))
837 (defcustom org-use-speed-commands nil
838 "Non-nil means activate single letter commands at beginning of a headline.
839 This may also be a function to test for appropriate locations where speed
840 commands should be active."
841 :group
'org-structure
843 (const :tag
"Never" nil
)
844 (const :tag
"At beginning of headline stars" t
)
847 (defcustom org-speed-commands-user nil
848 "Alist of additional speed commands.
849 This list will be checked before `org-speed-commands-default'
850 when the variable `org-use-speed-commands' is non-nil
851 and when the cursor is at the beginning of a headline.
852 The car if each entry is a string with a single letter, which must
853 be assigned to `self-insert-command' in the global map.
854 The cdr is either a command to be called interactively, a function
855 to be called, or a form to be evaluated.
856 An entry that is just a list with a single string will be interpreted
857 as a descriptive headline that will be added when listing the speed
858 commands in the Help buffer using the `?' speed command."
859 :group
'org-structure
860 :type
'(repeat :value
("k" . ignore
)
861 (choice :value
("k" . ignore
)
862 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
863 (cons :tag
"Letter and Command"
864 (string :tag
"Command letter")
869 (defgroup org-cycle nil
870 "Options concerning visibility cycling in Org-mode."
872 :group
'org-structure
)
874 (defcustom org-cycle-skip-children-state-if-no-children t
875 "Non-nil means skip CHILDREN state in entries that don't have any."
879 (defcustom org-cycle-max-level nil
880 "Maximum level which should still be subject to visibility cycling.
881 Levels higher than this will, for cycling, be treated as text, not a headline.
882 When `org-odd-levels-only' is set, a value of N in this variable actually
883 means 2N-1 stars as the limiting headline.
884 When nil, cycle all levels.
885 Note that the limiting level of cycling is also influenced by
886 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
887 `org-inlinetask-min-level' is, cycling will be limited to levels one less
891 (const :tag
"No limit" nil
)
892 (integer :tag
"Maximum level")))
894 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
895 "Names of drawers. Drawers are not opened by cycling on the headline above.
896 Drawers only open with a TAB on the drawer line itself. A drawer looks like
901 The drawer \"PROPERTIES\" is special for capturing properties through
904 Drawers can be defined on the per-file basis with a line like:
906 #+DRAWERS: HIDDEN STATE PROPERTIES"
907 :group
'org-structure
909 :type
'(repeat (string :tag
"Drawer Name")))
911 (defcustom org-hide-block-startup nil
912 "Non-nil means entering Org-mode will fold all blocks.
913 This can also be set in on a per-file basis with
915 #+STARTUP: hideblocks
916 #+STARTUP: showblocks"
921 (defcustom org-cycle-global-at-bob nil
922 "Cycle globally if cursor is at beginning of buffer and not at a headline.
923 This makes it possible to do global cycling without having to use S-TAB or
924 \\[universal-argument] TAB. For this special case to work, the first line
925 of the buffer must not be a headline -- it may be empty or some other text.
926 When used in this way, `org-cycle-hook' is disabled temporarily to make
927 sure the cursor stays at the beginning of the buffer. When this option is
928 nil, don't do anything special at the beginning of the buffer."
932 (defcustom org-cycle-level-after-item
/entry-creation t
933 "Non-nil means cycle entry level or item indentation in new empty entries.
935 When the cursor is at the end of an empty headline, i.e., with only stars
936 and maybe a TODO keyword, TAB will then switch the entry to become a child,
937 and then all possible ancestor states, before returning to the original state.
938 This makes data entry extremely fast: M-RET to create a new headline,
939 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
941 When the cursor is at the end of an empty plain list item, one TAB will
942 make it a subitem, two or more tabs will back up to make this an item
943 higher up in the item hierarchy."
947 (defcustom org-cycle-emulate-tab t
948 "Where should `org-cycle' emulate TAB.
950 white Only in completely white lines
951 whitestart Only at the beginning of lines, before the first non-white char
952 t Everywhere except in headlines
953 exc-hl-bol Everywhere except at the start of a headline
954 If TAB is used in a place where it does not emulate TAB, the current subtree
955 visibility is cycled."
957 :type
'(choice (const :tag
"Never" nil
)
958 (const :tag
"Only in completely white lines" white
)
959 (const :tag
"Before first char in a line" whitestart
)
960 (const :tag
"Everywhere except in headlines" t
)
961 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
964 (defcustom org-cycle-separator-lines
2
965 "Number of empty lines needed to keep an empty line between collapsed trees.
966 If you leave an empty line between the end of a subtree and the following
967 headline, this empty line is hidden when the subtree is folded.
968 Org-mode will leave (exactly) one empty line visible if the number of
969 empty lines is equal or larger to the number given in this variable.
970 So the default 2 means at least 2 empty lines after the end of a subtree
971 are needed to produce free space between a collapsed subtree and the
974 If the number is negative, and the number of empty lines is at least -N,
975 all empty lines are shown.
977 Special case: when 0, never leave empty lines in collapsed view."
980 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
982 (defcustom org-pre-cycle-hook nil
983 "Hook that is run before visibility cycling is happening.
984 The function(s) in this hook must accept a single argument which indicates
985 the new state that will be set right after running this hook. The
986 argument is a symbol. Before a global state change, it can have the values
987 `overview', `content', or `all'. Before a local state change, it can have
988 the values `folded', `children', or `subtree'."
992 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
993 org-cycle-hide-drawers
994 org-cycle-show-empty-lines
995 org-optimize-window-after-visibility-change
)
996 "Hook that is run after `org-cycle' has changed the buffer visibility.
997 The function(s) in this hook must accept a single argument which indicates
998 the new state that was set by the most recent `org-cycle' command. The
999 argument is a symbol. After a global state change, it can have the values
1000 `overview', `contents', or `all'. After a local state change, it can have
1001 the values `folded', `children', or `subtree'."
1005 (defgroup org-edit-structure nil
1006 "Options concerning structure editing in Org-mode."
1007 :tag
"Org Edit Structure"
1008 :group
'org-structure
)
1010 (defcustom org-odd-levels-only nil
1011 "Non-nil means skip even levels and only use odd levels for the outline.
1012 This has the effect that two stars are being added/taken away in
1013 promotion/demotion commands. It also influences how levels are
1014 handled by the exporters.
1015 Changing it requires restart of `font-lock-mode' to become effective
1016 for fontification also in regions already fontified.
1017 You may also set this on a per-file basis by adding one of the following
1018 lines to the buffer:
1022 :group
'org-edit-structure
1023 :group
'org-appearance
1026 (defcustom org-adapt-indentation t
1027 "Non-nil means adapt indentation to outline node level.
1029 When this variable is set, Org assumes that you write outlines by
1030 indenting text in each node to align with the headline (after the stars).
1031 The following issues are influenced by this variable:
1033 - When this is set and the *entire* text in an entry is indented, the
1034 indentation is increased by one space in a demotion command, and
1035 decreased by one in a promotion command. If any line in the entry
1036 body starts with text at column 0, indentation is not changed at all.
1038 - Property drawers and planning information is inserted indented when
1039 this variable s set. When nil, they will not be indented.
1041 - TAB indents a line relative to context. The lines below a headline
1042 will be indented when this variable is set.
1044 Note that this is all about true indentation, by adding and removing
1045 space characters. See also `org-indent.el' which does level-dependent
1046 indentation in a virtual way, i.e. at display time in Emacs."
1047 :group
'org-edit-structure
1050 (defcustom org-special-ctrl-a
/e nil
1051 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1053 When t, `C-a' will bring back the cursor to the beginning of the
1054 headline text, i.e. after the stars and after a possible TODO
1055 keyword. In an item, this will be the position after bullet and
1056 check-box, if any. When the cursor is already at that position,
1057 another `C-a' will bring it to the beginning of the line.
1059 `C-e' will jump to the end of the headline, ignoring the presence
1060 of tags in the headline. A second `C-e' will then jump to the
1061 true end of the line, after any tags. This also means that, when
1062 this variable is non-nil, `C-e' also will never jump beyond the
1063 end of the heading of a folded section, i.e. not after the
1066 When set to the symbol `reversed', the first `C-a' or `C-e' works
1067 normally, going to the true line boundary first. Only a directly
1068 following, identical keypress will bring the cursor to the
1071 This may also be a cons cell where the behavior for `C-a' and
1072 `C-e' is set separately."
1073 :group
'org-edit-structure
1075 (const :tag
"off" nil
)
1076 (const :tag
"on: after stars/bullet and before tags first" t
)
1077 (const :tag
"reversed: true line boundary first" reversed
)
1078 (cons :tag
"Set C-a and C-e separately"
1079 (choice :tag
"Special C-a"
1080 (const :tag
"off" nil
)
1081 (const :tag
"on: after stars/bullet first" t
)
1082 (const :tag
"reversed: before stars/bullet first" reversed
))
1083 (choice :tag
"Special C-e"
1084 (const :tag
"off" nil
)
1085 (const :tag
"on: before tags first" t
)
1086 (const :tag
"reversed: after tags first" reversed
)))))
1087 (if (fboundp 'defvaralias
)
1088 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
1090 (defcustom org-special-ctrl-k nil
1091 "Non-nil means `C-k' will behave specially in headlines.
1092 When nil, `C-k' will call the default `kill-line' command.
1093 When t, the following will happen while the cursor is in the headline:
1095 - When the cursor is at the beginning of a headline, kill the entire
1096 line and possible the folded subtree below the line.
1097 - When in the middle of the headline text, kill the headline up to the tags.
1098 - When after the headline text, kill the tags."
1099 :group
'org-edit-structure
1102 (defcustom org-ctrl-k-protect-subtree nil
1103 "Non-nil means, do not delete a hidden subtree with C-k.
1104 When set to the symbol `error', simply throw an error when C-k is
1105 used to kill (part-of) a headline that has hidden text behind it.
1106 Any other non-nil value will result in a query to the user, if it is
1107 OK to kill that hidden subtree. When nil, kill without remorse."
1108 :group
'org-edit-structure
1111 (const :tag
"Do not protect hidden subtrees" nil
)
1112 (const :tag
"Protect hidden subtrees with a security query" t
)
1113 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1115 (defcustom org-catch-invisible-edits nil
1116 "Check if in invisible region before inserting or deleting a character.
1119 nil Do not check, so just do invisible edits.
1120 error Throw an error and do nothing.
1121 show Make point visible, and do the requested edit.
1122 show-and-error Make point visible, then throw an error and abort the edit.
1123 smart Make point visible, and do insertion/deletion if it is
1124 adjacent to visible text and the change feels predictable.
1125 Never delete a previously invisible character or add in the
1126 middle or right after an invisible region. Basically, this
1127 allows insertion and backward-delete right before ellipses.
1128 FIXME: maybe in this case we should not even show?"
1129 :group
'org-edit-structure
1132 (const :tag
"Do not check" nil
)
1133 (const :tag
"Throw error when trying to edit" error
)
1134 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1135 (const :tag
"Show invisible part and do the edit" show
)
1136 (const :tag
"Be smart and do the right thing" smart
)))
1138 (defcustom org-yank-folded-subtrees t
1139 "Non-nil means when yanking subtrees, fold them.
1140 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1141 it starts with a heading and all other headings in it are either children
1142 or siblings, then fold all the subtrees. However, do this only if no
1143 text after the yank would be swallowed into a folded tree by this action."
1144 :group
'org-edit-structure
1147 (defcustom org-yank-adjusted-subtrees nil
1148 "Non-nil means when yanking subtrees, adjust the level.
1149 With this setting, `org-paste-subtree' is used to insert the subtree, see
1150 this function for details."
1151 :group
'org-edit-structure
1154 (defcustom org-M-RET-may-split-line
'((default . t
))
1155 "Non-nil means M-RET will split the line at the cursor position.
1156 When nil, it will go to the end of the line before making a
1158 You may also set this option in a different way for different
1159 contexts. Valid contexts are:
1161 headline when creating a new headline
1162 item when creating a new item
1163 table in a table field
1164 default the value to be used for all contexts not explicitly
1166 :group
'org-structure
1169 (const :tag
"Always" t
)
1170 (const :tag
"Never" nil
)
1171 (repeat :greedy t
:tag
"Individual contexts"
1173 (choice :tag
"Context"
1181 (defcustom org-insert-heading-respect-content nil
1182 "Non-nil means insert new headings after the current subtree.
1183 When nil, the new heading is created directly after the current line.
1184 The commands \\[org-insert-heading-respect-content] and
1185 \\[org-insert-todo-heading-respect-content] turn this variable on
1186 for the duration of the command."
1187 :group
'org-structure
1190 (defcustom org-blank-before-new-entry
'((heading . auto
)
1191 (plain-list-item . auto
))
1192 "Should `org-insert-heading' leave a blank line before new heading/item?
1193 The value is an alist, with `heading' and `plain-list-item' as CAR,
1194 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1195 which case Org will look at the surrounding headings/items and try to
1196 make an intelligent decision whether to insert a blank line or not.
1198 For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
1199 set, the setting here is ignored and no empty line is inserted, to avoid
1200 breaking the list structure."
1201 :group
'org-edit-structure
1203 (cons (const heading
)
1204 (choice (const :tag
"Never" nil
)
1205 (const :tag
"Always" t
)
1206 (const :tag
"Auto" auto
)))
1207 (cons (const plain-list-item
)
1208 (choice (const :tag
"Never" nil
)
1209 (const :tag
"Always" t
)
1210 (const :tag
"Auto" auto
)))))
1212 (defcustom org-insert-heading-hook nil
1213 "Hook being run after inserting a new heading."
1214 :group
'org-edit-structure
1217 (defcustom org-enable-fixed-width-editor t
1218 "Non-nil means lines starting with \":\" are treated as fixed-width.
1219 This currently only means they are never auto-wrapped.
1220 When nil, such lines will be treated like ordinary lines.
1221 See also the QUOTE keyword."
1222 :group
'org-edit-structure
1225 (defcustom org-goto-auto-isearch t
1226 "Non-nil means typing characters in `org-goto' starts incremental search.
1227 When nil, you can use these keybindings to navigate the buffer:
1229 q Quit the org-goto interface
1230 n Go to the next visible heading
1231 p Go to the previous visible heading
1232 f Go one heading forward on same level
1233 b Go one heading backward on same level
1234 u Go one heading up"
1235 :group
'org-edit-structure
1238 (defgroup org-sparse-trees nil
1239 "Options concerning sparse trees in Org-mode."
1240 :tag
"Org Sparse Trees"
1241 :group
'org-structure
)
1243 (defcustom org-highlight-sparse-tree-matches t
1244 "Non-nil means highlight all matches that define a sparse tree.
1245 The highlights will automatically disappear the next time the buffer is
1246 changed by an edit command."
1247 :group
'org-sparse-trees
1250 (defcustom org-remove-highlights-with-change t
1251 "Non-nil means any change to the buffer will remove temporary highlights.
1252 Such highlights are created by `org-occur' and `org-clock-display'.
1253 When nil, `C-c C-c needs to be used to get rid of the highlights.
1254 The highlights created by `org-preview-latex-fragment' always need
1255 `C-c C-c' to be removed."
1256 :group
'org-sparse-trees
1261 (defcustom org-occur-hook
'(org-first-headline-recenter)
1262 "Hook that is run after `org-occur' has constructed a sparse tree.
1263 This can be used to recenter the window to show as much of the structure
1265 :group
'org-sparse-trees
1268 (defgroup org-imenu-and-speedbar nil
1269 "Options concerning imenu and speedbar in Org-mode."
1270 :tag
"Org Imenu and Speedbar"
1271 :group
'org-structure
)
1273 (defcustom org-imenu-depth
2
1274 "The maximum level for Imenu access to Org-mode headlines.
1275 This also applied for speedbar access."
1276 :group
'org-imenu-and-speedbar
1279 (defgroup org-table nil
1280 "Options concerning tables in Org-mode."
1284 (defcustom org-enable-table-editor
'optimized
1285 "Non-nil means lines starting with \"|\" are handled by the table editor.
1286 When nil, such lines will be treated like ordinary lines.
1288 When equal to the symbol `optimized', the table editor will be optimized to
1290 - Automatic overwrite mode in front of whitespace in table fields.
1291 This makes the structure of the table stay in tact as long as the edited
1292 field does not exceed the column width.
1293 - Minimize the number of realigns. Normally, the table is aligned each time
1294 TAB or RET are pressed to move to another field. With optimization this
1295 happens only if changes to a field might have changed the column width.
1296 Optimization requires replacing the functions `self-insert-command',
1297 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1298 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1299 very good at guessing when a re-align will be necessary, but you can always
1300 force one with \\[org-ctrl-c-ctrl-c].
1302 If you would like to use the optimized version in Org-mode, but the
1303 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1305 This variable can be used to turn on and off the table editor during a session,
1306 but in order to toggle optimization, a restart is required.
1308 See also the variable `org-table-auto-blank-field'."
1311 (const :tag
"off" nil
)
1313 (const :tag
"on, optimized" optimized
)))
1315 (defcustom org-self-insert-cluster-for-undo
(or (featurep 'xemacs
)
1316 (version<= emacs-version
"24.1"))
1317 "Non-nil means cluster self-insert commands for undo when possible.
1318 If this is set, then, like in the Emacs command loop, 20 consecutive
1319 characters will be undone together.
1320 This is configurable, because there is some impact on typing performance."
1324 (defcustom org-table-tab-recognizes-table.el t
1325 "Non-nil means TAB will automatically notice a table.el table.
1326 When it sees such a table, it moves point into it and - if necessary -
1327 calls `table-recognize-table'."
1328 :group
'org-table-editing
1331 (defgroup org-link nil
1332 "Options concerning links in Org-mode."
1336 (defvar org-link-abbrev-alist-local nil
1337 "Buffer-local version of `org-link-abbrev-alist', which see.
1338 The value of this is taken from the #+LINK lines.")
1339 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1341 (defcustom org-link-abbrev-alist nil
1342 "Alist of link abbreviations.
1343 The car of each element is a string, to be replaced at the start of a link.
1344 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1345 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1347 [[linkkey:tag][description]]
1349 The 'linkkey' must be a word word, starting with a letter, followed
1350 by letters, numbers, '-' or '_'.
1352 If REPLACE is a string, the tag will simply be appended to create the link.
1353 If the string contains \"%s\", the tag will be inserted there. If the string
1354 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1355 at that point (see the function `url-hexify-string'). If the string contains
1356 the specifier \"%(my-function)\", then the custom function `my-function' will
1357 be invoked: this function takes the tag as its only argument and must return
1360 REPLACE may also be a function that will be called with the tag as the
1361 only argument to create the link, which should be returned as a string.
1363 See the manual for examples."
1367 (string :tag
"Protocol")
1369 (string :tag
"Format")
1372 (defcustom org-descriptive-links t
1373 "Non-nil means Org will display descriptive links.
1374 E.g. [[http://orgmode.org][Org website]] will be displayed as
1375 \"Org Website\", hiding the link itself and just displaying its
1376 description. When set to `nil', Org will display the full links
1379 You can interactively set the value of this variable by calling
1380 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1384 (defcustom org-link-file-path-type
'adaptive
1385 "How the path name in file links should be stored.
1388 relative Relative to the current directory, i.e. the directory of the file
1389 into which the link is being inserted.
1390 absolute Absolute path, if possible with ~ for home directory.
1391 noabbrev Absolute path, no abbreviation of home directory.
1392 adaptive Use relative path for files in the current directory and sub-
1393 directories of it. For other files, use an absolute path."
1401 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1402 "Types of links that should be activated in Org-mode files.
1403 This is a list of symbols, each leading to the activation of a certain link
1404 type. In principle, it does not hurt to turn on most link types - there may
1405 be a small gain when turning off unused link types. The types are:
1407 bracket The recommended [[link][description]] or [[link]] links with hiding.
1408 angle Links in angular brackets that may contain whitespace like
1409 <bbdb:Carsten Dominik>.
1410 plain Plain links in normal text, no whitespace, like http://google.com.
1411 radio Text that is matched by a radio target, see manual for details.
1412 tag Tag settings in a headline (link to tag search).
1413 date Time stamps (link to calendar).
1414 footnote Footnote labels.
1416 Changing this variable requires a restart of Emacs to become effective."
1418 :type
'(set :greedy t
1419 (const :tag
"Double bracket links" bracket
)
1420 (const :tag
"Angular bracket links" angle
)
1421 (const :tag
"Plain text links" plain
)
1422 (const :tag
"Radio target matches" radio
)
1423 (const :tag
"Tags" tag
)
1424 (const :tag
"Timestamps" date
)
1425 (const :tag
"Footnotes" footnote
)))
1427 (defcustom org-make-link-description-function nil
1428 "Function to use for generating link descriptions from links.
1429 When nil, the link location will be used. This function must take
1430 two parameters: the first one is the link, the second one is the
1431 description generated by `org-insert-link'. The function should
1432 return the description to use."
1436 (defgroup org-link-store nil
1437 "Options concerning storing links in Org-mode."
1438 :tag
"Org Store Link"
1441 (defcustom org-url-hexify-p t
1442 "When non-nil, hexify URL when creating a link."
1445 :group
'org-link-store
)
1447 (defcustom org-email-link-description-format
"Email %c: %.30s"
1448 "Format of the description part of a link to an email or usenet message.
1449 The following %-escapes will be replaced by corresponding information:
1451 %F full \"From\" field
1452 %f name, taken from \"From\" field, address if no name
1453 %T full \"To\" field
1454 %t first name in \"To\" field, address if no name
1455 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1456 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1461 You may use normal field width specification between the % and the letter.
1462 This is for example useful to limit the length of the subject.
1464 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1465 :group
'org-link-store
1468 (defcustom org-from-is-user-regexp
1470 (when (and user-mail-address
(not (string= user-mail-address
"")))
1471 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1472 (when (and user-full-name
(not (string= user-full-name
"")))
1473 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1474 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1475 "Regexp matched against the \"From:\" header of an email or usenet message.
1476 It should match if the message is from the user him/herself."
1477 :group
'org-link-store
1480 (defcustom org-context-in-file-links t
1481 "Non-nil means file links from `org-store-link' contain context.
1482 A search string will be added to the file name with :: as separator and
1483 used to find the context when the link is activated by the command
1484 `org-open-at-point'. When this option is t, the entire active region
1485 will be placed in the search string of the file link. If set to a
1486 positive integer, only the first n lines of context will be stored.
1488 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1489 negates this setting for the duration of the command."
1490 :group
'org-link-store
1491 :type
'(choice boolean integer
))
1493 (defcustom org-keep-stored-link-after-insertion nil
1494 "Non-nil means keep link in list for entire session.
1496 The command `org-store-link' adds a link pointing to the current
1497 location to an internal list. These links accumulate during a session.
1498 The command `org-insert-link' can be used to insert links into any
1499 Org-mode file (offering completion for all stored links). When this
1500 option is nil, every link which has been inserted once using \\[org-insert-link]
1501 will be removed from the list, to make completing the unused links
1503 :group
'org-link-store
1506 (defgroup org-link-follow nil
1507 "Options concerning following links in Org-mode."
1508 :tag
"Org Follow Link"
1511 (defcustom org-link-translation-function nil
1512 "Function to translate links with different syntax to Org syntax.
1513 This can be used to translate links created for example by the Planner
1514 or emacs-wiki packages to Org syntax.
1515 The function must accept two parameters, a TYPE containing the link
1516 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1517 which is everything after the link protocol. It should return a cons
1518 with possibly modified values of type and path.
1519 Org contains a function for this, so if you set this variable to
1520 `org-translate-link-from-planner', you should be able follow many
1521 links created by planner."
1522 :group
'org-link-follow
1525 (defcustom org-follow-link-hook nil
1526 "Hook that is run after a link has been followed."
1527 :group
'org-link-follow
1530 (defcustom org-tab-follows-link nil
1531 "Non-nil means on links TAB will follow the link.
1532 Needs to be set before org.el is loaded.
1533 This really should not be used, it does not make sense, and the
1534 implementation is bad."
1535 :group
'org-link-follow
1538 (defcustom org-return-follows-link nil
1539 "Non-nil means on links RET will follow the link."
1540 :group
'org-link-follow
1543 (defcustom org-mouse-1-follows-link
1544 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1545 "Non-nil means mouse-1 on a link will follow the link.
1546 A longer mouse click will still set point. Does not work on XEmacs.
1547 Needs to be set before org.el is loaded."
1548 :group
'org-link-follow
1551 (defcustom org-mark-ring-length
4
1552 "Number of different positions to be recorded in the ring.
1553 Changing this requires a restart of Emacs to work correctly."
1554 :group
'org-link-follow
1557 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1558 "Non-nil means internal links in Org files must exactly match a headline.
1559 When nil, the link search tries to match a phrase with all words
1560 in the search text."
1561 :group
'org-link-follow
1564 (const :tag
"Use fuzzy text search" nil
)
1565 (const :tag
"Match only exact headline" t
)
1566 (const :tag
"Match exact headline or query to create it"
1569 (defcustom org-link-frame-setup
1570 '((vm . vm-visit-folder-other-frame
)
1571 (vm-imap . vm-visit-imap-folder-other-frame
)
1572 (gnus . org-gnus-no-new-news
)
1573 (file . find-file-other-window
)
1574 (wl . wl-other-frame
))
1575 "Setup the frame configuration for following links.
1576 When following a link with Emacs, it may often be useful to display
1577 this link in another window or frame. This variable can be used to
1578 set this up for the different types of links.
1581 `vm-visit-folder-other-window'
1582 `vm-visit-folder-other-frame'
1583 For Gnus, use any of
1586 `org-gnus-no-new-news'
1587 For FILE, use any of
1589 `find-file-other-window'
1590 `find-file-other-frame'
1591 For Wanderlust use any of
1594 For the calendar, use the variable `calendar-setup'.
1595 For BBDB, it is currently only possible to display the matches in
1597 :group
'org-link-follow
1601 (const vm-visit-folder
)
1602 (const vm-visit-folder-other-window
)
1603 (const vm-visit-folder-other-frame
)))
1607 (const gnus-other-frame
)
1608 (const org-gnus-no-new-news
)))
1612 (const find-file-other-window
)
1613 (const find-file-other-frame
)))
1617 (const wl-other-frame
)))))
1619 (defcustom org-display-internal-link-with-indirect-buffer nil
1620 "Non-nil means use indirect buffer to display infile links.
1621 Activating internal links (from one location in a file to another location
1622 in the same file) normally just jumps to the location. When the link is
1623 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1625 another window. When this option is set, the other window actually displays
1626 an indirect buffer clone of the current buffer, to avoid any visibility
1627 changes to the current buffer."
1628 :group
'org-link-follow
1631 (defcustom org-open-non-existing-files nil
1632 "Non-nil means `org-open-file' will open non-existing files.
1633 When nil, an error will be generated.
1634 This variable applies only to external applications because they
1635 might choke on non-existing files. If the link is to a file that
1636 will be opened in Emacs, the variable is ignored."
1637 :group
'org-link-follow
1640 (defcustom org-open-directory-means-index-dot-org nil
1641 "Non-nil means a link to a directory really means to index.org.
1642 When nil, following a directory link will run dired or open a finder/explorer
1643 window on that directory."
1644 :group
'org-link-follow
1647 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1648 "Function and arguments to call for following mailto links.
1649 This is a list with the first element being a Lisp function, and the
1650 remaining elements being arguments to the function. In string arguments,
1651 %a will be replaced by the address, and %s will be replaced by the subject
1652 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1653 :group
'org-link-follow
1655 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1656 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1657 (const :tag
"message-mail" (message-mail "%a" "%s"))
1658 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1660 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1661 "Non-nil means ask for confirmation before executing shell links.
1662 Shell links can be dangerous: just think about a link
1664 [[shell:rm -rf ~/*][Google Search]]
1666 This link would show up in your Org-mode document as \"Google Search\",
1667 but really it would remove your entire home directory.
1668 Therefore we advise against setting this variable to nil.
1669 Just change it to `y-or-n-p' if you want to confirm with a
1670 single keystroke rather than having to type \"yes\"."
1671 :group
'org-link-follow
1673 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1674 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1675 (const :tag
"no confirmation (dangerous)" nil
)))
1676 (put 'org-confirm-shell-link-function
1677 'safe-local-variable
1678 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1680 (defcustom org-confirm-shell-link-not-regexp
""
1681 "A regexp to skip confirmation for shell links."
1682 :group
'org-link-follow
1686 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1687 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1688 Elisp links can be dangerous: just think about a link
1690 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1692 This link would show up in your Org-mode document as \"Google Search\",
1693 but really it would remove your entire home directory.
1694 Therefore we advise against setting this variable to nil.
1695 Just change it to `y-or-n-p' if you want to confirm with a
1696 single keystroke rather than having to type \"yes\"."
1697 :group
'org-link-follow
1699 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1700 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1701 (const :tag
"no confirmation (dangerous)" nil
)))
1702 (put 'org-confirm-shell-link-function
1703 'safe-local-variable
1704 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1706 (defcustom org-confirm-elisp-link-not-regexp
""
1707 "A regexp to skip confirmation for Elisp links."
1708 :group
'org-link-follow
1712 (defconst org-file-apps-defaults-gnu
1716 "Default file applications on a UNIX or GNU/Linux system.
1717 See `org-file-apps'.")
1719 (defconst org-file-apps-defaults-macosx
1722 (system .
"open %s")
1724 ("eps.gz" .
"gv %s")
1726 ("fig" .
"xfig %s"))
1727 "Default file applications on a MacOS X system.
1728 The system \"open\" is known as a default, but we use X11 applications
1729 for some files for which the OS does not have a good default.
1730 See `org-file-apps'.")
1732 (defconst org-file-apps-defaults-windowsnt
1736 (list (if (featurep 'xemacs
)
1737 'mswindows-shell-execute
1741 (list (if (featurep 'xemacs
)
1742 'mswindows-shell-execute
1745 "Default file applications on a Windows NT system.
1746 The system \"open\" is used for most files.
1747 See `org-file-apps'.")
1749 (defcustom org-file-apps
1752 ("\\.mm\\'" . default
)
1753 ("\\.x?html?\\'" . default
)
1754 ("\\.pdf\\'" . default
)
1756 "External applications for opening `file:path' items in a document.
1757 Org-mode uses system defaults for different file types, but
1758 you can use this variable to set the application for a given file
1759 extension. The entries in this list are cons cells where the car identifies
1760 files and the cdr the corresponding command. Possible values for the
1762 \"string\" A string as a file identifier can be interpreted in different
1763 ways, depending on its contents:
1765 - Alphanumeric characters only:
1766 Match links with this file extension.
1767 Example: (\"pdf\" . \"evince %s\")
1768 to open PDFs with evince.
1770 - Regular expression: Match links where the
1771 filename matches the regexp. If you want to
1772 use groups here, use shy groups.
1774 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1775 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1776 to open *.html and *.xhtml with firefox.
1778 - Regular expression which contains (non-shy) groups:
1779 Match links where the whole link, including \"::\", and
1780 anything after that, matches the regexp.
1781 In a custom command string, %1, %2, etc. are replaced with
1782 the parts of the link that were matched by the groups.
1783 For backwards compatibility, if a command string is given
1784 that does not use any of the group matches, this case is
1785 handled identically to the second one (i.e. match against
1787 In a custom lisp form, you can access the group matches with
1788 (match-string n link).
1790 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1791 to open [[file:document.pdf::5]] with evince at page 5.
1793 `directory' Matches a directory
1794 `remote' Matches a remote file, accessible through tramp or efs.
1795 Remote files most likely should be visited through Emacs
1796 because external applications cannot handle such paths.
1797 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1798 so all files Emacs knows how to handle. Using this with
1799 command `emacs' will open most files in Emacs. Beware that this
1800 will also open html files inside Emacs, unless you add
1801 (\"html\" . default) to the list as well.
1802 t Default for files not matched by any of the other options.
1803 `system' The system command to open files, like `open' on Windows
1804 and Mac OS X, and mailcap under GNU/Linux. This is the command
1805 that will be selected if you call `C-c C-o' with a double
1806 \\[universal-argument] \\[universal-argument] prefix.
1808 Possible values for the command are:
1809 `emacs' The file will be visited by the current Emacs process.
1810 `default' Use the default application for this file type, which is the
1811 association for t in the list, most likely in the system-specific
1813 This can be used to overrule an unwanted setting in the
1814 system-specific variable.
1815 `system' Use the system command for opening files, like \"open\".
1816 This command is specified by the entry whose car is `system'.
1817 Most likely, the system-specific version of this variable
1818 does define this command, but you can overrule/replace it
1820 string A command to be executed by a shell; %s will be replaced
1821 by the path to the file.
1822 sexp A Lisp form which will be evaluated. The file path will
1823 be available in the Lisp variable `file'.
1824 For more examples, see the system specific constants
1825 `org-file-apps-defaults-macosx'
1826 `org-file-apps-defaults-windowsnt'
1827 `org-file-apps-defaults-gnu'."
1828 :group
'org-link-follow
1830 (cons (choice :value
""
1831 (string :tag
"Extension")
1832 (const :tag
"System command to open files" system
)
1833 (const :tag
"Default for unrecognized files" t
)
1834 (const :tag
"Remote file" remote
)
1835 (const :tag
"Links to a directory" directory
)
1836 (const :tag
"Any files that have Emacs modes"
1839 (const :tag
"Visit with Emacs" emacs
)
1840 (const :tag
"Use default" default
)
1841 (const :tag
"Use the system command" system
)
1842 (string :tag
"Command")
1843 (sexp :tag
"Lisp form")))))
1845 (defcustom org-doi-server-url
"http://dx.doi.org/"
1846 "The URL of the DOI server."
1849 :group
'org-link-follow
)
1851 (defgroup org-refile nil
1852 "Options concerning refiling entries in Org-mode."
1856 (defcustom org-directory
"~/org"
1857 "Directory with org files.
1858 This is just a default location to look for Org files. There is no need
1859 at all to put your files into this directory. It is only used in the
1860 following situations:
1862 1. When a capture template specifies a target file that is not an
1863 absolute path. The path will then be interpreted relative to
1865 2. When a capture note is filed away in an interactive way (when exiting the
1866 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1867 with `org-directory' as the default path."
1869 :group
'org-remember
1873 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1874 "Default target for storing notes.
1875 Used as a fall back file for org-remember.el and org-capture.el, for
1876 templates that do not specify a target file."
1878 :group
'org-remember
1881 (const :tag
"Default from remember-data-file" nil
)
1884 (defcustom org-goto-interface
'outline
1885 "The default interface to be used for `org-goto'.
1887 outline The interface shows an outline of the relevant file
1888 and the correct heading is found by moving through
1889 the outline or by searching with incremental search.
1890 outline-path-completion Headlines in the current buffer are offered via
1891 completion. This is the interface also used by
1892 the refile command."
1895 (const :tag
"Outline" outline
)
1896 (const :tag
"Outline-path-completion" outline-path-completion
)))
1898 (defcustom org-goto-max-level
5
1899 "Maximum target level when running `org-goto' with refile interface."
1903 (defcustom org-reverse-note-order nil
1904 "Non-nil means store new notes at the beginning of a file or entry.
1905 When nil, new notes will be filed to the end of a file or entry.
1906 This can also be a list with cons cells of regular expressions that
1907 are matched against file names, and values."
1908 :group
'org-remember
1912 (const :tag
"Reverse always" t
)
1913 (const :tag
"Reverse never" nil
)
1914 (repeat :tag
"By file name regexp"
1915 (cons regexp boolean
))))
1917 (defcustom org-log-refile nil
1918 "Information to record when a task is refiled.
1920 Possible values are:
1922 nil Don't add anything
1923 time Add a time stamp to the task
1924 note Prompt for a note and add it with template `org-log-note-headings'
1926 This option can also be set with on a per-file-basis with
1928 #+STARTUP: nologrefile
1929 #+STARTUP: logrefile
1930 #+STARTUP: lognoterefile
1932 You can have local logging settings for a subtree by setting the LOGGING
1933 property to one or more of these keywords.
1935 When bulk-refiling from the agenda, the value `note' is forbidden and
1936 will temporarily be changed to `time'."
1938 :group
'org-progress
1941 (const :tag
"No logging" nil
)
1942 (const :tag
"Record timestamp" time
)
1943 (const :tag
"Record timestamp with note." note
)))
1945 (defcustom org-refile-targets nil
1946 "Targets for refiling entries with \\[org-refile].
1947 This is a list of cons cells. Each cell contains:
1948 - a specification of the files to be considered, either a list of files,
1949 or a symbol whose function or variable value will be used to retrieve
1950 a file name or a list of file names. If you use `org-agenda-files' for
1951 that, all agenda files will be scanned for targets. Nil means consider
1952 headings in the current buffer.
1953 - A specification of how to find candidate refile targets. This may be
1955 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1956 This tag has to be present in all target headlines, inheritance will
1958 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1960 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1961 headlines that are refiling targets.
1962 - a cons cell (:level . N). Any headline of level N is considered a target.
1963 Note that, when `org-odd-levels-only' is set, level corresponds to
1964 order in hierarchy, not to the number of stars.
1965 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1966 Note that, when `org-odd-levels-only' is set, level corresponds to
1967 order in hierarchy, not to the number of stars.
1969 Each element of this list generates a set of possible targets.
1970 The union of these sets is presented (with completion) to
1971 the user by `org-refile'.
1973 You can set the variable `org-refile-target-verify-function' to a function
1974 to verify each headline found by the simple criteria above.
1976 When this variable is nil, all top-level headlines in the current buffer
1977 are used, equivalent to the value `((nil . (:level . 1))'."
1981 (choice :value org-agenda-files
1982 (const :tag
"All agenda files" org-agenda-files
)
1983 (const :tag
"Current buffer" nil
)
1984 (function) (variable) (file))
1985 (choice :tag
"Identify target headline by"
1986 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1987 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1988 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1989 (cons :tag
"Level number" (const :value
:level
) (integer))
1990 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1992 (defcustom org-refile-target-verify-function nil
1993 "Function to verify if the headline at point should be a refile target.
1994 The function will be called without arguments, with point at the
1995 beginning of the headline. It should return t and leave point
1996 where it is if the headline is a valid target for refiling.
1998 If the target should not be selected, the function must return nil.
1999 In addition to this, it may move point to a place from where the search
2000 should be continued. For example, the function may decide that the entire
2001 subtree of the current entry should be excluded and move point to the end
2006 (defcustom org-refile-use-cache nil
2007 "Non-nil means cache refile targets to speed up the process.
2008 The cache for a particular file will be updated automatically when
2009 the buffer has been killed, or when any of the marker used for flagging
2010 refile targets no longer points at a live buffer.
2011 If you have added new entries to a buffer that might themselves be targets,
2012 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
2013 find that easier, `C-u C-u C-u C-c C-w'."
2018 (defcustom org-refile-use-outline-path nil
2019 "Non-nil means provide refile targets as paths.
2020 So a level 3 headline will be available as level1/level2/level3.
2022 When the value is `file', also include the file name (without directory)
2023 into the path. In this case, you can also stop the completion after
2024 the file name, to get entries inserted as top level in the file.
2026 When `full-file-path', include the full file path."
2029 (const :tag
"Not" nil
)
2030 (const :tag
"Yes" t
)
2031 (const :tag
"Start with file name" file
)
2032 (const :tag
"Start with full file path" full-file-path
)))
2034 (defcustom org-outline-path-complete-in-steps t
2035 "Non-nil means complete the outline path in hierarchical steps.
2036 When Org-mode uses the refile interface to select an outline path
2037 \(see variable `org-refile-use-outline-path'), the completion of
2038 the path can be done is a single go, or if can be done in steps down
2039 the headline hierarchy. Going in steps is probably the best if you
2040 do not use a special completion package like `ido' or `icicles'.
2041 However, when using these packages, going in one step can be very
2042 fast, while still showing the whole path to the entry."
2046 (defcustom org-refile-allow-creating-parent-nodes nil
2047 "Non-nil means allow to create new nodes as refile targets.
2048 New nodes are then created by adding \"/new node name\" to the completion
2049 of an existing node. When the value of this variable is `confirm',
2050 new node creation must be confirmed by the user (recommended)
2051 When nil, the completion must match an existing entry.
2053 Note that, if the new heading is not seen by the criteria
2054 listed in `org-refile-targets', multiple instances of the same
2055 heading would be created by trying again to file under the new
2059 (const :tag
"Never" nil
)
2060 (const :tag
"Always" t
)
2061 (const :tag
"Prompt for confirmation" confirm
)))
2063 (defcustom org-refile-active-region-within-subtree nil
2064 "Non-nil means also refile active region within a subtree.
2066 By default `org-refile' doesn't allow refiling regions if they
2067 don't contain a set of subtrees, but it might be convenient to
2068 do so sometimes: in that case, the first line of the region is
2069 converted to a headline before refiling."
2074 (defgroup org-todo nil
2075 "Options concerning TODO items in Org-mode."
2079 (defgroup org-progress nil
2080 "Options concerning Progress logging in Org-mode."
2084 (defvar org-todo-interpretation-widgets
2085 '((:tag
"Sequence (cycling hits every state)" sequence
)
2086 (:tag
"Type (cycling directly to DONE)" type
))
2087 "The available interpretation symbols for customizing `org-todo-keywords'.
2088 Interested libraries should add to this list.")
2090 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2091 "List of TODO entry keyword sequences and their interpretation.
2092 \\<org-mode-map>This is a list of sequences.
2094 Each sequence starts with a symbol, either `sequence' or `type',
2095 indicating if the keywords should be interpreted as a sequence of
2096 action steps, or as different types of TODO items. The first
2097 keywords are states requiring action - these states will select a headline
2098 for inclusion into the global TODO list Org-mode produces. If one of
2099 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2100 signify that no further action is necessary. If \"|\" is not found,
2101 the last keyword is treated as the only DONE state of the sequence.
2103 The command \\[org-todo] cycles an entry through these states, and one
2104 additional state where no keyword is present. For details about this
2105 cycling, see the manual.
2107 TODO keywords and interpretation can also be set on a per-file basis with
2108 the special #+SEQ_TODO and #+TYP_TODO lines.
2110 Each keyword can optionally specify a character for fast state selection
2111 \(in combination with the variable `org-use-fast-todo-selection')
2112 and specifiers for state change logging, using the same syntax that
2113 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2114 the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2115 indicates to record a time stamp each time this state is selected.
2117 Each keyword may also specify if a timestamp or a note should be
2118 recorded when entering or leaving the state, by adding additional
2119 characters in the parenthesis after the keyword. This looks like this:
2120 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2121 record only the time of the state change. With X and Y being either
2122 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2123 Y when leaving the state if and only if the *target* state does not
2124 define X. You may omit any of the fast-selection key or X or /Y,
2125 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2127 For backward compatibility, this variable may also be just a list
2128 of keywords. In this case the interpretation (sequence or type) will be
2129 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2131 :group
'org-keywords
2133 (repeat :tag
"Old syntax, just keywords"
2134 (string :tag
"Keyword"))
2135 (repeat :tag
"New syntax"
2138 :tag
"Interpretation"
2139 ;;Quick and dirty way to see
2140 ;;`org-todo-interpretations'. This takes the
2141 ;;place of item arguments
2149 org-todo-interpretation-widgets
))
2152 (string :tag
"Keyword"))))))
2154 (defvar org-todo-keywords-1 nil
2155 "All TODO and DONE keywords active in a buffer.")
2156 (make-variable-buffer-local 'org-todo-keywords-1
)
2157 (defvar org-todo-keywords-for-agenda nil
)
2158 (defvar org-done-keywords-for-agenda nil
)
2159 (defvar org-drawers-for-agenda nil
)
2160 (defvar org-todo-keyword-alist-for-agenda nil
)
2161 (defvar org-tag-alist-for-agenda nil
)
2162 (defvar org-agenda-contributing-files nil
)
2163 (defvar org-not-done-keywords nil
)
2164 (make-variable-buffer-local 'org-not-done-keywords
)
2165 (defvar org-done-keywords nil
)
2166 (make-variable-buffer-local 'org-done-keywords
)
2167 (defvar org-todo-heads nil
)
2168 (make-variable-buffer-local 'org-todo-heads
)
2169 (defvar org-todo-sets nil
)
2170 (make-variable-buffer-local 'org-todo-sets
)
2171 (defvar org-todo-log-states nil
)
2172 (make-variable-buffer-local 'org-todo-log-states
)
2173 (defvar org-todo-kwd-alist nil
)
2174 (make-variable-buffer-local 'org-todo-kwd-alist
)
2175 (defvar org-todo-key-alist nil
)
2176 (make-variable-buffer-local 'org-todo-key-alist
)
2177 (defvar org-todo-key-trigger nil
)
2178 (make-variable-buffer-local 'org-todo-key-trigger
)
2180 (defcustom org-todo-interpretation
'sequence
2181 "Controls how TODO keywords are interpreted.
2182 This variable is in principle obsolete and is only used for
2183 backward compatibility, if the interpretation of todo keywords is
2184 not given already in `org-todo-keywords'. See that variable for
2187 :group
'org-keywords
2188 :type
'(choice (const sequence
)
2191 (defcustom org-use-fast-todo-selection t
2192 "Non-nil means use the fast todo selection scheme with C-c C-t.
2193 This variable describes if and under what circumstances the cycling
2194 mechanism for TODO keywords will be replaced by a single-key, direct
2197 When nil, fast selection is never used.
2199 When the symbol `prefix', it will be used when `org-todo' is called
2200 with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
2201 `C-u t' in an agenda buffer.
2203 When t, fast selection is used by default. In this case, the prefix
2204 argument forces cycling instead.
2206 In all cases, the special interface is only used if access keys have
2207 actually been assigned by the user, i.e. if keywords in the configuration
2208 are followed by a letter in parenthesis, like TODO(t)."
2211 (const :tag
"Never" nil
)
2212 (const :tag
"By default" t
)
2213 (const :tag
"Only with C-u C-c C-t" prefix
)))
2215 (defcustom org-provide-todo-statistics t
2216 "Non-nil means update todo statistics after insert and toggle.
2217 ALL-HEADLINES means update todo statistics by including headlines
2218 with no TODO keyword as well, counting them as not done.
2219 A list of TODO keywords means the same, but skip keywords that are
2222 When this is set, todo statistics is updated in the parent of the
2223 current entry each time a todo state is changed."
2226 (const :tag
"Yes, only for TODO entries" t
)
2227 (const :tag
"Yes, including all entries" 'all-headlines
)
2228 (repeat :tag
"Yes, for TODOs in this list"
2229 (string :tag
"TODO keyword"))
2230 (other :tag
"No TODO statistics" nil
)))
2232 (defcustom org-hierarchical-todo-statistics t
2233 "Non-nil means TODO statistics covers just direct children.
2234 When nil, all entries in the subtree are considered.
2235 This has only an effect if `org-provide-todo-statistics' is set.
2236 To set this to nil for only a single subtree, use a COOKIE_DATA
2237 property and include the word \"recursive\" into the value."
2241 (defcustom org-after-todo-state-change-hook nil
2242 "Hook which is run after the state of a TODO item was changed.
2243 The new state (a string with a TODO keyword, or nil) is available in the
2244 Lisp variable `org-state'."
2248 (defvar org-blocker-hook nil
2249 "Hook for functions that are allowed to block a state change.
2251 Functions in this hook should not modify the buffer.
2252 Each function gets as its single argument a property list,
2253 see `org-trigger-hook' for more information about this list.
2255 If any of the functions in this hook returns nil, the state change
2258 (defvar org-trigger-hook nil
2259 "Hook for functions that are triggered by a state change.
2261 Each function gets as its single argument a property list with at
2262 least the following elements:
2264 (:type type-of-change :position pos-at-entry-start
2265 :from old-state :to new-state)
2267 Depending on the type, more properties may be present.
2269 This mechanism is currently implemented for:
2273 :type todo-state-change
2274 :from previous state (keyword as a string), or nil, or a symbol
2275 'todo' or 'done', to indicate the general type of state.
2276 :to new state, like in :from")
2278 (defcustom org-enforce-todo-dependencies nil
2279 "Non-nil means undone TODO entries will block switching the parent to DONE.
2280 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2281 be blocked if any prior sibling is not yet done.
2282 Finally, if the parent is blocked because of ordered siblings of its own,
2283 the child will also be blocked."
2284 :set
(lambda (var val
)
2287 (add-hook 'org-blocker-hook
2288 'org-block-todo-from-children-or-siblings-or-parent
)
2289 (remove-hook 'org-blocker-hook
2290 'org-block-todo-from-children-or-siblings-or-parent
)))
2294 (defcustom org-enforce-todo-checkbox-dependencies nil
2295 "Non-nil means unchecked boxes will block switching the parent to DONE.
2296 When this is nil, checkboxes have no influence on switching TODO states.
2297 When non-nil, you first need to check off all check boxes before the TODO
2298 entry can be switched to DONE.
2299 This variable needs to be set before org.el is loaded, and you need to
2300 restart Emacs after a change to make the change effective. The only way
2301 to change is while Emacs is running is through the customize interface."
2302 :set
(lambda (var val
)
2305 (add-hook 'org-blocker-hook
2306 'org-block-todo-from-checkboxes
)
2307 (remove-hook 'org-blocker-hook
2308 'org-block-todo-from-checkboxes
)))
2312 (defcustom org-treat-insert-todo-heading-as-state-change nil
2313 "Non-nil means inserting a TODO heading is treated as state change.
2314 So when the command \\[org-insert-todo-heading] is used, state change
2315 logging will apply if appropriate. When nil, the new TODO item will
2316 be inserted directly, and no logging will take place."
2320 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2321 "Non-nil means switching TODO states with S-cursor counts as state change.
2322 This is the default behavior. However, setting this to nil allows a
2323 convenient way to select a TODO state and bypass any logging associated
2328 (defcustom org-todo-state-tags-triggers nil
2329 "Tag changes that should be triggered by TODO state changes.
2330 This is a list. Each entry is
2332 (state-change (tag . flag) .......)
2334 State-change can be a string with a state, and empty string to indicate the
2335 state that has no TODO keyword, or it can be one of the symbols `todo'
2336 or `done', meaning any not-done or done state, respectively."
2340 (cons (choice :tag
"When changing to"
2341 (const :tag
"Not-done state" todo
)
2342 (const :tag
"Done state" done
)
2343 (string :tag
"State"))
2345 (cons :tag
"Tag action"
2347 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2349 (defcustom org-log-done nil
2350 "Information to record when a task moves to the DONE state.
2352 Possible values are:
2354 nil Don't add anything, just change the keyword
2355 time Add a time stamp to the task
2356 note Prompt for a note and add it with template `org-log-note-headings'
2358 This option can also be set with on a per-file-basis with
2360 #+STARTUP: nologdone
2362 #+STARTUP: lognotedone
2364 You can have local logging settings for a subtree by setting the LOGGING
2365 property to one or more of these keywords."
2367 :group
'org-progress
2369 (const :tag
"No logging" nil
)
2370 (const :tag
"Record CLOSED timestamp" time
)
2371 (const :tag
"Record CLOSED timestamp with note." note
)))
2373 ;; Normalize old uses of org-log-done.
2375 ((eq org-log-done t
) (setq org-log-done
'time
))
2376 ((and (listp org-log-done
) (memq 'done org-log-done
))
2377 (setq org-log-done
'note
)))
2379 (defcustom org-log-reschedule nil
2380 "Information to record when the scheduling date of a tasks is modified.
2382 Possible values are:
2384 nil Don't add anything, just change the date
2385 time Add a time stamp to the task
2386 note Prompt for a note and add it with template `org-log-note-headings'
2388 This option can also be set with on a per-file-basis with
2390 #+STARTUP: nologreschedule
2391 #+STARTUP: logreschedule
2392 #+STARTUP: lognotereschedule"
2394 :group
'org-progress
2396 (const :tag
"No logging" nil
)
2397 (const :tag
"Record timestamp" time
)
2398 (const :tag
"Record timestamp with note." note
)))
2400 (defcustom org-log-redeadline nil
2401 "Information to record when the deadline date of a tasks is modified.
2403 Possible values are:
2405 nil Don't add anything, just change the date
2406 time Add a time stamp to the task
2407 note Prompt for a note and add it with template `org-log-note-headings'
2409 This option can also be set with on a per-file-basis with
2411 #+STARTUP: nologredeadline
2412 #+STARTUP: logredeadline
2413 #+STARTUP: lognoteredeadline
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
"No logging" nil
)
2421 (const :tag
"Record timestamp" time
)
2422 (const :tag
"Record timestamp with note." note
)))
2424 (defcustom org-log-note-clock-out nil
2425 "Non-nil means record a note when clocking out of an item.
2426 This can also be configured on a per-file basis by adding one of
2427 the following lines anywhere in the buffer:
2429 #+STARTUP: lognoteclock-out
2430 #+STARTUP: nolognoteclock-out"
2432 :group
'org-progress
2435 (defcustom org-log-done-with-time t
2436 "Non-nil means the CLOSED time stamp will contain date and time.
2437 When nil, only the date will be recorded."
2438 :group
'org-progress
2441 (defcustom org-log-note-headings
2442 '((done .
"CLOSING NOTE %t")
2443 (state .
"State %-12s from %-12S %t")
2444 (note .
"Note taken on %t")
2445 (reschedule .
"Rescheduled from %S on %t")
2446 (delschedule .
"Not scheduled, was %S on %t")
2447 (redeadline .
"New deadline from %S on %t")
2448 (deldeadline .
"Removed deadline, was %S on %t")
2449 (refile .
"Refiled on %t")
2451 "Headings for notes added to entries.
2452 The value is an alist, with the car being a symbol indicating the note
2453 context, and the cdr is the heading to be used. The heading may also be the
2455 %t in the heading will be replaced by a time stamp.
2456 %T will be an active time stamp instead the default inactive one
2457 %d will be replaced by a short-format time stamp.
2458 %D will be replaced by an active short-format time stamp.
2459 %s will be replaced by the new TODO state, in double quotes.
2460 %S will be replaced by the old TODO state, in double quotes.
2461 %u will be replaced by the user name.
2462 %U will be replaced by the full user name.
2464 In fact, it is not a good idea to change the `state' entry, because
2465 agenda log mode depends on the format of these entries."
2467 :group
'org-progress
2468 :type
'(list :greedy t
2469 (cons (const :tag
"Heading when closing an item" done
) string
)
2471 "Heading when changing todo state (todo sequence only)"
2473 (cons (const :tag
"Heading when just taking a note" note
) string
)
2474 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2475 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2476 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2477 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2478 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2479 (cons (const :tag
"Heading when refiling" refile
) string
)))
2481 (unless (assq 'note org-log-note-headings
)
2482 (push '(note .
"%t") org-log-note-headings
))
2484 (defcustom org-log-into-drawer nil
2485 "Non-nil means insert state change notes and time stamps into a drawer.
2486 When nil, state changes notes will be inserted after the headline and
2487 any scheduling and clock lines, but not inside a drawer.
2489 The value of this variable should be the name of the drawer to use.
2490 LOGBOOK is proposed as the default drawer for this purpose, you can
2491 also set this to a string to define the drawer of your choice.
2493 A value of t is also allowed, representing \"LOGBOOK\".
2495 If this variable is set, `org-log-state-notes-insert-after-drawers'
2498 You can set the property LOG_INTO_DRAWER to overrule this setting for
2501 :group
'org-progress
2503 (const :tag
"Not into a drawer" nil
)
2504 (const :tag
"LOGBOOK" t
)
2505 (string :tag
"Other")))
2507 (if (fboundp 'defvaralias
)
2508 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2510 (defun org-log-into-drawer ()
2511 "Return the value of `org-log-into-drawer', but let properties overrule.
2512 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2513 used instead of the default value."
2514 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit t
)))
2516 ((not p
) org-log-into-drawer
)
2517 ((equal p
"nil") nil
)
2518 ((equal p
"t") "LOGBOOK")
2521 (defcustom org-log-state-notes-insert-after-drawers nil
2522 "Non-nil means insert state change notes after any drawers in entry.
2523 Only the drawers that *immediately* follow the headline and the
2524 deadline/scheduled line are skipped.
2525 When nil, insert notes right after the heading and perhaps the line
2526 with deadline/scheduling if present.
2528 This variable will have no effect if `org-log-into-drawer' is
2531 :group
'org-progress
2534 (defcustom org-log-states-order-reversed t
2535 "Non-nil means the latest state note will be directly after heading.
2536 When nil, the state change notes will be ordered according to time."
2538 :group
'org-progress
2541 (defcustom org-todo-repeat-to-state nil
2542 "The TODO state to which a repeater should return the repeating task.
2543 By default this is the first task in a TODO sequence, or the previous state
2544 in a TODO_TYP set. But you can specify another task here.
2545 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2548 :type
'(choice (const :tag
"Head of sequence" nil
)
2549 (string :tag
"Specific state")))
2551 (defcustom org-log-repeat
'time
2552 "Non-nil means record moving through the DONE state when triggering repeat.
2553 An auto-repeating task is immediately switched back to TODO when
2554 marked DONE. If you are not logging state changes (by adding \"@\"
2555 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2556 record a closing note, there will be no record of the task moving
2557 through DONE. This variable forces taking a note anyway.
2559 nil Don't force a record
2560 time Record a time stamp
2561 note Prompt for a note and add it with template `org-log-note-headings'
2563 This option can also be set with on a per-file-basis with
2565 #+STARTUP: nologrepeat
2566 #+STARTUP: logrepeat
2567 #+STARTUP: lognoterepeat
2569 You can have local logging settings for a subtree by setting the LOGGING
2570 property to one or more of these keywords."
2572 :group
'org-progress
2574 (const :tag
"Don't force a record" nil
)
2575 (const :tag
"Force recording the DONE state" time
)
2576 (const :tag
"Force recording a note with the DONE state" note
)))
2579 (defgroup org-priorities nil
2580 "Priorities in Org-mode."
2581 :tag
"Org Priorities"
2584 (defcustom org-enable-priority-commands t
2585 "Non-nil means priority commands are active.
2586 When nil, these commands will be disabled, so that you never accidentally
2588 :group
'org-priorities
2591 (defcustom org-highest-priority ?A
2592 "The highest priority of TODO items. A character like ?A, ?B etc.
2593 Must have a smaller ASCII number than `org-lowest-priority'."
2594 :group
'org-priorities
2597 (defcustom org-lowest-priority ?C
2598 "The lowest priority of TODO items. A character like ?A, ?B etc.
2599 Must have a larger ASCII number than `org-highest-priority'."
2600 :group
'org-priorities
2603 (defcustom org-default-priority ?B
2604 "The default priority of TODO items.
2605 This is the priority an item gets if no explicit priority is given.
2606 When starting to cycle on an empty priority the first step in the cycle
2607 depends on `org-priority-start-cycle-with-default'. The resulting first
2608 step priority must not exceed the range from `org-highest-priority' to
2609 `org-lowest-priority' which means that `org-default-priority' has to be
2610 in this range exclusive or inclusive the range boundaries. Else the
2611 first step refuses to set the default and the second will fall back
2612 to (depending on the command used) the highest or lowest priority."
2613 :group
'org-priorities
2616 (defcustom org-priority-start-cycle-with-default t
2617 "Non-nil means start with default priority when starting to cycle.
2618 When this is nil, the first step in the cycle will be (depending on the
2619 command used) one higher or lower than the default priority.
2620 See also `org-default-priority'."
2621 :group
'org-priorities
2624 (defcustom org-get-priority-function nil
2625 "Function to extract the priority from a string.
2626 The string is normally the headline. If this is nil Org computes the
2627 priority from the priority cookie like [#A] in the headline. It returns
2628 an integer, increasing by 1000 for each priority level.
2629 The user can set a different function here, which should take a string
2630 as an argument and return the numeric priority."
2631 :group
'org-priorities
2635 (defgroup org-time nil
2636 "Options concerning time stamps and deadlines in Org-mode."
2640 (defcustom org-insert-labeled-timestamps-at-point nil
2641 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2642 When nil, these labeled time stamps are forces into the second line of an
2643 entry, just after the headline. When scheduling from the global TODO list,
2644 the time stamp will always be forced into the second line."
2648 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2649 "Formats for `format-time-string' which are used for time stamps.
2650 It is not recommended to change this constant.")
2652 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2653 "Number of minutes to round time stamps to.
2654 These are two values, the first applies when first creating a time stamp.
2655 The second applies when changing it with the commands `S-up' and `S-down'.
2656 When changing the time stamp, this means that it will change in steps
2657 of N minutes, as given by the second value.
2659 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2660 numbers should be factors of 60, so for example 5, 10, 15.
2662 When this is larger than 1, you can still force an exact time stamp by using
2663 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2664 and by using a prefix arg to `S-up/down' to specify the exact number
2665 of minutes to shift."
2667 :get
#'(lambda (var) ; Make sure both elements are there
2668 (if (integerp (default-value var
))
2669 (list (default-value var
) 5)
2670 (default-value var
)))
2672 (integer :tag
"when inserting times")
2673 (integer :tag
"when modifying times")))
2675 ;; Normalize old customizations of this variable.
2676 (when (integerp org-time-stamp-rounding-minutes
)
2677 (setq org-time-stamp-rounding-minutes
2678 (list org-time-stamp-rounding-minutes
2679 org-time-stamp-rounding-minutes
)))
2681 (defcustom org-display-custom-times nil
2682 "Non-nil means overlay custom formats over all time stamps.
2683 The formats are defined through the variable `org-time-stamp-custom-formats'.
2684 To turn this on on a per-file basis, insert anywhere in the file:
2685 #+STARTUP: customtime"
2689 (make-variable-buffer-local 'org-display-custom-times
)
2691 (defcustom org-time-stamp-custom-formats
2692 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2693 "Custom formats for time stamps. See `format-time-string' for the syntax.
2694 These are overlaid over the default ISO format if the variable
2695 `org-display-custom-times' is set. Time like %H:%M should be at the
2696 end of the second format. The custom formats are also honored by export
2697 commands, if custom time display is turned on at the time of export."
2701 (defun org-time-stamp-format (&optional long inactive
)
2702 "Get the right format for a time string."
2703 (let ((f (if long
(cdr org-time-stamp-formats
)
2704 (car org-time-stamp-formats
))))
2706 (concat "[" (substring f
1 -
1) "]")
2709 (defcustom org-time-clocksum-format
"%d:%02d"
2710 "The format string used when creating CLOCKSUM lines.
2711 This is also used when org-mode generates a time duration."
2715 (defcustom org-time-clocksum-use-fractional nil
2716 "If non-nil, \\[org-clock-display] uses fractional times.
2717 org-mode generates a time duration."
2721 (defcustom org-time-clocksum-fractional-format
"%.2f"
2722 "The format string used when creating CLOCKSUM lines, or when
2723 org-mode generates a time duration."
2727 (defcustom org-deadline-warning-days
14
2728 "No. of days before expiration during which a deadline becomes active.
2729 This variable governs the display in sparse trees and in the agenda.
2730 When 0 or negative, it means use this number (the absolute value of it)
2731 even if a deadline has a different individual lead time specified.
2733 Custom commands can set this variable in the options section."
2735 :group
'org-agenda-daily
/weekly
2738 (defcustom org-read-date-prefer-future t
2739 "Non-nil means assume future for incomplete date input from user.
2740 This affects the following situations:
2741 1. The user gives a month but not a year.
2742 For example, if it is April and you enter \"feb 2\", this will be read
2743 as Feb 2, *next* year. \"May 5\", however, will be this year.
2744 2. The user gives a day, but no month.
2745 For example, if today is the 15th, and you enter \"3\", Org-mode will
2746 read this as the third of *next* month. However, if you enter \"17\",
2747 it will be considered as *this* month.
2749 If you set this variable to the symbol `time', then also the following
2752 3. If the user gives a time.
2753 If the time is before now, it will be interpreted as tomorrow.
2755 Currently none of this works for ISO week specifications.
2757 When this option is nil, the current day, month and year will always be
2760 See also `org-agenda-jump-prefer-future'."
2763 (const :tag
"Never" nil
)
2764 (const :tag
"Check month and day" t
)
2765 (const :tag
"Check month, day, and time" time
)))
2767 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
2768 "Should the agenda jump command prefer the future for incomplete dates?
2769 The default is to do the same as configured in `org-read-date-prefer-future'.
2770 But you can also set a deviating value here.
2771 This may t or nil, or the symbol `org-read-date-prefer-future'."
2776 (const :tag
"Use org-read-date-prefer-future"
2777 org-read-date-prefer-future
)
2778 (const :tag
"Never" nil
)
2779 (const :tag
"Always" t
)))
2781 (defcustom org-read-date-force-compatible-dates t
2782 "Should date/time prompt force dates that are guaranteed to work in Emacs?
2784 Depending on the system Emacs is running on, certain dates cannot
2785 be represented with the type used internally to represent time.
2786 Dates between 1970-1-1 and 2038-1-1 can always be represented
2787 correctly. Some systems allow for earlier dates, some for later,
2788 some for both. One way to find out it to insert any date into an
2789 Org buffer, putting the cursor on the year and hitting S-up and
2790 S-down to test the range.
2792 When this variable is set to t, the date/time prompt will not let
2793 you specify dates outside the 1970-2037 range, so it is certain that
2794 these dates will work in whatever version of Emacs you are
2795 running, and also that you can move a file from one Emacs implementation
2796 to another. WHenever Org is forcing the year for you, it will display
2799 When this variable is nil, Org will check if the date is
2800 representable in the specific Emacs implementation you are using.
2801 If not, it will force a year, usually the current year, and beep
2802 to remind you. Currently this setting is not recommended because
2803 the likelihood that you will open your Org files in an Emacs that
2804 has limited date range is not negligible.
2806 A workaround for this problem is to use diary sexp dates for time
2807 stamps outside of this range."
2812 (defcustom org-read-date-display-live t
2813 "Non-nil means display current interpretation of date prompt live.
2814 This display will be in an overlay, in the minibuffer."
2818 (defcustom org-read-date-popup-calendar t
2819 "Non-nil means pop up a calendar when prompting for a date.
2820 In the calendar, the date can be selected with mouse-1. However, the
2821 minibuffer will also be active, and you can simply enter the date as well.
2822 When nil, only the minibuffer will be available."
2825 (if (fboundp 'defvaralias
)
2826 (defvaralias 'org-popup-calendar-for-date-prompt
2827 'org-read-date-popup-calendar
))
2829 (defcustom org-read-date-minibuffer-setup-hook nil
2830 "Hook to be used to set up keys for the date/time interface.
2831 Add key definitions to `minibuffer-local-map', which will be a temporary
2836 (defcustom org-extend-today-until
0
2837 "The hour when your day really ends. Must be an integer.
2838 This has influence for the following applications:
2839 - When switching the agenda to \"today\". It it is still earlier than
2840 the time given here, the day recognized as TODAY is actually yesterday.
2841 - When a date is read from the user and it is still before the time given
2842 here, the current date and time will be assumed to be yesterday, 23:59.
2843 Also, timestamps inserted in capture templates follow this rule.
2845 IMPORTANT: This is a feature whose implementation is and likely will
2846 remain incomplete. Really, it is only here because past midnight seems to
2847 be the favorite working time of John Wiegley :-)"
2851 (defcustom org-use-effective-time nil
2852 "If non-nil, consider `org-extend-today-until' when creating timestamps.
2853 For example, if `org-extend-today-until' is 8, and it's 4am, then the
2854 \"effective time\" of any timestamps between midnight and 8am will be
2855 23:59 of the previous day."
2860 (defcustom org-edit-timestamp-down-means-later nil
2861 "Non-nil means S-down will increase the time in a time stamp.
2862 When nil, S-up will increase."
2866 (defcustom org-calendar-follow-timestamp-change t
2867 "Non-nil means make the calendar window follow timestamp changes.
2868 When a timestamp is modified and the calendar window is visible, it will be
2869 moved to the new date."
2873 (defgroup org-tags nil
2874 "Options concerning tags in Org-mode."
2878 (defcustom org-tag-alist nil
2879 "List of tags allowed in Org-mode files.
2880 When this list is nil, Org-mode will base TAG input on what is already in the
2882 The value of this variable is an alist, the car of each entry must be a
2883 keyword as a string, the cdr may be a character that is used to select
2884 that tag through the fast-tag-selection interface.
2885 See the manual for details."
2889 (cons (string :tag
"Tag name")
2890 (character :tag
"Access char"))
2891 (list :tag
"Start radio group"
2893 (option (string :tag
"Group description")))
2894 (list :tag
"End radio group"
2896 (option (string :tag
"Group description")))
2897 (const :tag
"New line" (:newline
)))))
2899 (defcustom org-tag-persistent-alist nil
2900 "List of tags that will always appear in all Org-mode files.
2901 This is in addition to any in buffer settings or customizations
2903 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2904 The value of this variable is an alist, the car of each entry must be a
2905 keyword as a string, the cdr may be a character that is used to select
2906 that tag through the fast-tag-selection interface.
2907 See the manual for details.
2908 To disable these tags on a per-file basis, insert anywhere in the file:
2913 (cons (string :tag
"Tag name")
2914 (character :tag
"Access char"))
2915 (const :tag
"Start radio group" (:startgroup
))
2916 (const :tag
"End radio group" (:endgroup
))
2917 (const :tag
"New line" (:newline
)))))
2919 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2920 "If non-nil, always offer completion for all tags of all agenda files.
2921 Instead of customizing this variable directly, you might want to
2922 set it locally for capture buffers, because there no list of
2923 tags in that file can be created dynamically (there are none).
2925 (add-hook 'org-capture-mode-hook
2927 (set (make-local-variable
2928 'org-complete-tags-always-offer-all-agenda-tags)
2934 (defvar org-file-tags nil
2935 "List of tags that can be inherited by all entries in the file.
2936 The tags will be inherited if the variable `org-use-tag-inheritance'
2937 says they should be.
2938 This variable is populated from #+FILETAGS lines.")
2940 (defcustom org-use-fast-tag-selection
'auto
2941 "Non-nil means use fast tag selection scheme.
2942 This is a special interface to select and deselect tags with single keys.
2943 When nil, fast selection is never used.
2944 When the symbol `auto', fast selection is used if and only if selection
2945 characters for tags have been configured, either through the variable
2946 `org-tag-alist' or through a #+TAGS line in the buffer.
2947 When t, fast selection is always used and selection keys are assigned
2948 automatically if necessary."
2951 (const :tag
"Always" t
)
2952 (const :tag
"Never" nil
)
2953 (const :tag
"When selection characters are configured" 'auto
)))
2955 (defcustom org-fast-tag-selection-single-key nil
2956 "Non-nil means fast tag selection exits after first change.
2957 When nil, you have to press RET to exit it.
2958 During fast tag selection, you can toggle this flag with `C-c'.
2959 This variable can also have the value `expert'. In this case, the window
2960 displaying the tags menu is not even shown, until you press C-c again."
2963 (const :tag
"No" nil
)
2964 (const :tag
"Yes" t
)
2965 (const :tag
"Expert" expert
)))
2967 (defvar org-fast-tag-selection-include-todo nil
2968 "Non-nil means fast tags selection interface will also offer TODO states.
2969 This is an undocumented feature, you should not rely on it.")
2971 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2972 "The column to which tags should be indented in a headline.
2973 If this number is positive, it specifies the column. If it is negative,
2974 it means that the tags should be flushright to that column. For example,
2975 -80 works well for a normal 80 character screen.
2976 When 0, place tags directly after headline text, with only one space in
2981 (defcustom org-auto-align-tags t
2982 "Non-nil keeps tags aligned when modifying headlines.
2983 Some operations (i.e. demoting) change the length of a headline and
2984 therefore shift the tags around. With this option turned on, after
2985 each such operation the tags are again aligned to `org-tags-column'."
2989 (defcustom org-use-tag-inheritance t
2990 "Non-nil means tags in levels apply also for sublevels.
2991 When nil, only the tags directly given in a specific line apply there.
2992 This may also be a list of tags that should be inherited, or a regexp that
2993 matches tags that should be inherited. Additional control is possible
2994 with the variable `org-tags-exclude-from-inheritance' which gives an
2995 explicit list of tags to be excluded from inheritance, even if the value of
2996 `org-use-tag-inheritance' would select it for inheritance.
2998 If this option is t, a match early-on in a tree can lead to a large
2999 number of matches in the subtree when constructing the agenda or creating
3000 a sparse tree. If you only want to see the first match in a tree during
3001 a search, check out the variable `org-tags-match-list-sublevels'."
3004 (const :tag
"Not" nil
)
3005 (const :tag
"Always" t
)
3006 (repeat :tag
"Specific tags" (string :tag
"Tag"))
3007 (regexp :tag
"Tags matched by regexp")))
3009 (defcustom org-tags-exclude-from-inheritance nil
3010 "List of tags that should never be inherited.
3011 This is a way to exclude a few tags from inheritance. For way to do
3012 the opposite, to actively allow inheritance for selected tags,
3013 see the variable `org-use-tag-inheritance'."
3015 :type
'(repeat (string :tag
"Tag")))
3017 (defun org-tag-inherit-p (tag)
3018 "Check if TAG is one that should be inherited."
3020 ((member tag org-tags-exclude-from-inheritance
) nil
)
3021 ((eq org-use-tag-inheritance t
) t
)
3022 ((not org-use-tag-inheritance
) nil
)
3023 ((stringp org-use-tag-inheritance
)
3024 (string-match org-use-tag-inheritance tag
))
3025 ((listp org-use-tag-inheritance
)
3026 (member tag org-use-tag-inheritance
))
3027 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3029 (defcustom org-tags-match-list-sublevels t
3030 "Non-nil means list also sublevels of headlines matching a search.
3031 This variable applies to tags/property searches, and also to stuck
3032 projects because this search is based on a tags match as well.
3034 When set to the symbol `indented', sublevels are indented with
3037 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3038 the sublevels of a headline matching a tag search often also match
3039 the same search. Listing all of them can create very long lists.
3040 Setting this variable to nil causes subtrees of a match to be skipped.
3042 This variable is semi-obsolete and probably should always be true. It
3043 is better to limit inheritance to certain tags using the variables
3044 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3047 (const :tag
"No, don't list them" nil
)
3048 (const :tag
"Yes, do list them" t
)
3049 (const :tag
"List them, indented with leading dots" indented
)))
3051 (defcustom org-tags-sort-function nil
3052 "When set, tags are sorted using this function as a comparator."
3055 (const :tag
"No sorting" nil
)
3056 (const :tag
"Alphabetical" string
<)
3057 (const :tag
"Reverse alphabetical" string
>)
3058 (function :tag
"Custom function" nil
)))
3060 (defvar org-tags-history nil
3061 "History of minibuffer reads for tags.")
3062 (defvar org-last-tags-completion-table nil
3063 "The last used completion table for tags.")
3064 (defvar org-after-tags-change-hook nil
3065 "Hook that is run after the tags in a line have changed.")
3067 (defgroup org-properties nil
3068 "Options concerning properties in Org-mode."
3069 :tag
"Org Properties"
3072 (defcustom org-property-format
"%-10s %s"
3073 "How property key/value pairs should be formatted by `indent-line'.
3074 When `indent-line' hits a property definition, it will format the line
3075 according to this format, mainly to make sure that the values are
3076 lined-up with respect to each other."
3077 :group
'org-properties
3080 (defcustom org-properties-postprocess-alist nil
3081 "Alist of properties and functions to adjust inserted values.
3082 Elements of this alist must be of the form
3084 ([string] [function])
3086 where [string] must be a property name and [function] must be a
3087 lambda expression: this lambda expression must take one argument,
3088 the value to adjust, and return the new value as a string.
3090 For example, this element will allow the property \"Remaining\"
3091 to be updated wrt the relation between the \"Effort\" property
3092 and the clock summary:
3094 ((\"Remaining\" (lambda(value)
3095 (let ((clocksum (org-clock-sum-current-item))
3096 (effort (org-duration-string-to-minutes
3097 (org-entry-get (point) \"Effort\"))))
3098 (org-minutes-to-hh:mm-string (- effort clocksum))))))"
3099 :group
'org-properties
3101 :type
'(alist :key-type
(string :tag
"Property")
3102 :value-type
(function :tag
"Function")))
3104 (defcustom org-use-property-inheritance nil
3105 "Non-nil means properties apply also for sublevels.
3107 This setting is chiefly used during property searches. Turning it on can
3108 cause significant overhead when doing a search, which is why it is not
3111 When nil, only the properties directly given in the current entry count.
3112 When t, every property is inherited. The value may also be a list of
3113 properties that should have inheritance, or a regular expression matching
3114 properties that should be inherited.
3116 However, note that some special properties use inheritance under special
3117 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3118 and the properties ending in \"_ALL\" when they are used as descriptor
3119 for valid values of a property.
3121 Note for programmers:
3122 When querying an entry with `org-entry-get', you can control if inheritance
3123 should be used. By default, `org-entry-get' looks only at the local
3124 properties. You can request inheritance by setting the inherit argument
3125 to t (to force inheritance) or to `selective' (to respect the setting
3127 :group
'org-properties
3129 (const :tag
"Not" nil
)
3130 (const :tag
"Always" t
)
3131 (repeat :tag
"Specific properties" (string :tag
"Property"))
3132 (regexp :tag
"Properties matched by regexp")))
3134 (defun org-property-inherit-p (property)
3135 "Check if PROPERTY is one that should be inherited."
3137 ((eq org-use-property-inheritance t
) t
)
3138 ((not org-use-property-inheritance
) nil
)
3139 ((stringp org-use-property-inheritance
)
3140 (string-match org-use-property-inheritance property
))
3141 ((listp org-use-property-inheritance
)
3142 (member property org-use-property-inheritance
))
3143 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3145 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3146 "The default column format, if no other format has been defined.
3147 This variable can be set on the per-file basis by inserting a line
3149 #+COLUMNS: %25ITEM ....."
3150 :group
'org-properties
3153 (defcustom org-columns-ellipses
".."
3154 "The ellipses to be used when a field in column view is truncated.
3155 When this is the empty string, as many characters as possible are shown,
3156 but then there will be no visual indication that the field has been truncated.
3157 When this is a string of length N, the last N characters of a truncated
3158 field are replaced by this string. If the column is narrower than the
3159 ellipses string, only part of the ellipses string will be shown."
3160 :group
'org-properties
3163 (defcustom org-columns-modify-value-for-display-function nil
3164 "Function that modifies values for display in column view.
3165 For example, it can be used to cut out a certain part from a time stamp.
3166 The function must take 2 arguments:
3168 column-title The title of the column (*not* the property name)
3169 value The value that should be modified.
3171 The function should return the value that should be displayed,
3172 or nil if the normal value should be used."
3173 :group
'org-properties
3176 (defcustom org-effort-property
"Effort"
3177 "The property that is being used to keep track of effort estimates.
3178 Effort estimates given in this property need to have the format H:MM."
3179 :group
'org-properties
3180 :group
'org-progress
3181 :type
'(string :tag
"Property"))
3183 (defconst org-global-properties-fixed
3184 '(("VISIBILITY_ALL" .
"folded children content all")
3185 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3186 "List of property/value pairs that can be inherited by any entry.
3188 These are fixed values, for the preset properties. The user variable
3189 that can be used to add to this list is `org-global-properties'.
3191 The entries in this list are cons cells where the car is a property
3192 name and cdr is a string with the value. If the value represents
3193 multiple items like an \"_ALL\" property, separate the items by
3196 (defcustom org-global-properties nil
3197 "List of property/value pairs that can be inherited by any entry.
3199 This list will be combined with the constant `org-global-properties-fixed'.
3201 The entries in this list are cons cells where the car is a property
3202 name and cdr is a string with the value.
3204 You can set buffer-local values for the same purpose in the variable
3205 `org-file-properties' this by adding lines like
3207 #+PROPERTY: NAME VALUE"
3208 :group
'org-properties
3210 (cons (string :tag
"Property")
3211 (string :tag
"Value"))))
3213 (defvar org-file-properties nil
3214 "List of property/value pairs that can be inherited by any entry.
3215 Valid for the current buffer.
3216 This variable is populated from #+PROPERTY lines.")
3217 (make-variable-buffer-local 'org-file-properties
)
3219 (defgroup org-agenda nil
3220 "Options concerning agenda views in Org-mode."
3224 (defvar org-category nil
3225 "Variable used by org files to set a category for agenda display.
3226 Such files should use a file variable to set it, for example
3228 # -*- mode: org; org-category: \"ELisp\"
3230 or contain a special line
3234 If the file does not specify a category, then file's base name
3236 (make-variable-buffer-local 'org-category
)
3237 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3239 (defcustom org-agenda-files nil
3240 "The files to be used for agenda display.
3241 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3242 \\[org-remove-file]. You can also use customize to edit the list.
3244 If an entry is a directory, all files in that directory that are matched by
3245 `org-agenda-file-regexp' will be part of the file list.
3247 If the value of the variable is not a list but a single file name, then
3248 the list of agenda files is actually stored and maintained in that file, one
3249 agenda file per line. In this file paths can be given relative to
3250 `org-directory'. Tilde expansion and environment variable substitution
3254 (repeat :tag
"List of files and directories" file
)
3255 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3257 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3258 "Regular expression to match files for `org-agenda-files'.
3259 If any element in the list in that variable contains a directory instead
3260 of a normal file, all files in that directory that are matched by this
3261 regular expression will be included."
3265 (defcustom org-agenda-text-search-extra-files nil
3266 "List of extra files to be searched by text search commands.
3267 These files will be search in addition to the agenda files by the
3268 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3269 Note that these files will only be searched for text search commands,
3270 not for the other agenda views like todo lists, tag searches or the weekly
3271 agenda. This variable is intended to list notes and possibly archive files
3272 that should also be searched by these two commands.
3273 In fact, if the first element in the list is the symbol `agenda-archives',
3274 than all archive files of all agenda files will be added to the search
3277 :type
'(set :greedy t
3278 (const :tag
"Agenda Archives" agenda-archives
)
3279 (repeat :inline t
(file))))
3281 (if (fboundp 'defvaralias
)
3282 (defvaralias 'org-agenda-multi-occur-extra-files
3283 'org-agenda-text-search-extra-files
))
3285 (defcustom org-agenda-skip-unavailable-files nil
3286 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3287 A nil value means to remove them, after a query, from the list."
3291 (defcustom org-calendar-to-agenda-key
[?c
]
3292 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3293 The command `org-calendar-goto-agenda' will be bound to this key. The
3294 default is the character `c' because then `c' can be used to switch back and
3295 forth between agenda and calendar."
3299 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3300 "The key to be installed in `calendar-mode-map' for adding diary entries.
3301 This option is irrelevant until `org-agenda-diary-file' has been configured
3302 to point to an Org-mode file. When that is the case, the command
3303 `org-agenda-diary-entry' will be bound to the key given here, by default
3304 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3305 if you want to continue doing this, you need to change this to a different
3310 (defcustom org-agenda-diary-file
'diary-file
3311 "File to which to add new entries with the `i' key in agenda and calendar.
3312 When this is the symbol `diary-file', the functionality in the Emacs
3313 calendar will be used to add entries to the `diary-file'. But when this
3314 points to a file, `org-agenda-diary-entry' will be used instead."
3317 (const :tag
"The standard Emacs diary file" diary-file
)
3318 (file :tag
"Special Org file diary entries")))
3320 (eval-after-load "calendar"
3322 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3323 'org-calendar-goto-agenda
)
3324 (add-hook 'calendar-mode-hook
3326 (unless (eq org-agenda-diary-file
'diary-file
)
3327 (define-key calendar-mode-map
3328 org-calendar-insert-diary-entry-key
3329 'org-agenda-diary-entry
))))))
3331 (defgroup org-latex nil
3332 "Options for embedding LaTeX code into Org-mode."
3336 (defcustom org-format-latex-options
3337 '(:foreground default
:background default
:scale
1.0
3338 :html-foreground
"Black" :html-background
"Transparent"
3339 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3340 "Options for creating images from LaTeX fragments.
3341 This is a property list with the following properties:
3342 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3343 `default' means use the foreground of the default face.
3344 :background the background color, or \"Transparent\".
3345 `default' means use the background of the default face.
3346 :scale a scaling factor for the size of the images, to get more pixels
3347 :html-foreground, :html-background, :html-scale
3348 the same numbers for HTML export.
3349 :matchers a list indicating which matchers should be used to
3350 find LaTeX fragments. Valid members of this list are:
3351 \"begin\" find environments
3352 \"$1\" find single characters surrounded by $.$
3353 \"$\" find math expressions surrounded by $...$
3354 \"$$\" find math expressions surrounded by $$....$$
3355 \"\\(\" find math expressions surrounded by \\(...\\)
3356 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3360 (defcustom org-format-latex-signal-error t
3361 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3362 When nil, just push out a message."
3367 (defcustom org-latex-to-mathml-jar-file nil
3368 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3369 Use this to specify additional executable file say a jar file.
3371 When using MathToWeb as the converter, specify the full-path to
3372 your mathtoweb.jar file."
3376 (const :tag
"None" nil
)
3377 (file :tag
"JAR file" :must-match t
)))
3379 (defcustom org-latex-to-mathml-convert-command nil
3380 "Command to convert LaTeX fragments to MathML.
3381 Replace format-specifiers in the command as noted below and use
3382 `shell-command' to convert LaTeX to MathML.
3383 %j: Executable file in fully expanded form as specified by
3384 `org-latex-to-mathml-jar-file'.
3385 %I: Input LaTeX file in fully expanded form
3386 %o: Output MathML file
3387 This command is used by `org-create-math-formula'.
3389 When using MathToWeb as the converter, set this to
3390 \"java -jar %j -unicode -force -df %o %I\"."
3394 (const :tag
"None" nil
)
3395 (string :tag
"\nShell command")))
3397 (defcustom org-latex-create-formula-image-program
'dvipng
3398 "Program to convert LaTeX fragments with.
3400 dvipng Process the LaTeX fragments to dvi file, then convert
3401 dvi files to png files using dvipng.
3402 This will also include processing of non-math environments.
3403 imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
3404 to convert pdf files to png files"
3408 (const :tag
"dvipng" dvipng
)
3409 (const :tag
"imagemagick" imagemagick
)))
3411 (defcustom org-latex-preview-ltxpng-directory
"ltxpng/"
3412 "Path to store latex preview images. A relative path here creates many
3413 directories relative to the processed org files paths. An absolute path
3414 puts all preview images at the same place."
3419 (defun org-format-latex-mathml-available-p ()
3420 "Return t if `org-latex-to-mathml-convert-command' is usable."
3422 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3423 org-latex-to-mathml-convert-command
)
3424 (let ((executable (car (split-string
3425 org-latex-to-mathml-convert-command
))))
3426 (when (executable-find executable
)
3428 "%j" org-latex-to-mathml-convert-command
)
3429 (file-readable-p org-latex-to-mathml-jar-file
)
3432 (defcustom org-format-latex-header
"\\documentclass{article}
3433 \\usepackage[usenames]{color}
3434 \\usepackage{amsmath}
3435 \\usepackage[mathscr]{eucal}
3436 \\pagestyle{empty} % do not remove
3439 % The settings below are copied from fullpage.sty
3440 \\setlength{\\textwidth}{\\paperwidth}
3441 \\addtolength{\\textwidth}{-3cm}
3442 \\setlength{\\oddsidemargin}{1.5cm}
3443 \\addtolength{\\oddsidemargin}{-2.54cm}
3444 \\setlength{\\evensidemargin}{\\oddsidemargin}
3445 \\setlength{\\textheight}{\\paperheight}
3446 \\addtolength{\\textheight}{-\\headheight}
3447 \\addtolength{\\textheight}{-\\headsep}
3448 \\addtolength{\\textheight}{-\\footskip}
3449 \\addtolength{\\textheight}{-3cm}
3450 \\setlength{\\topmargin}{1.5cm}
3451 \\addtolength{\\topmargin}{-2.54cm}"
3452 "The document header used for processing LaTeX fragments.
3453 It is imperative that this header make sure that no page number
3454 appears on the page. The package defined in the variables
3455 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
3456 will either replace the placeholder \"[PACKAGES]\" in this header, or they
3461 (defvar org-format-latex-header-extra nil
)
3463 (defun org-set-packages-alist (var val
)
3464 "Set the packages alist and make sure it has 3 elements per entry."
3465 (set var
(mapcar (lambda (x)
3466 (if (and (consp x
) (= (length x
) 2))
3467 (list (car x
) (nth 1 x
) t
)
3471 (defun org-get-packages-alist (var)
3473 "Get the packages alist and make sure it has 3 elements per entry."
3475 (if (and (consp x
) (= (length x
) 2))
3476 (list (car x
) (nth 1 x
) t
)
3478 (default-value var
)))
3480 ;; The following variables are defined here because is it also used
3481 ;; when formatting latex fragments. Originally it was part of the
3482 ;; LaTeX exporter, which is why the name includes "export".
3483 (defcustom org-export-latex-default-packages-alist
3484 '(("AUTO" "inputenc" t
)
3488 ("" "longtable" nil
)
3500 "Alist of default packages to be inserted in the header.
3501 Change this only if one of the packages here causes an incompatibility
3502 with another package you are using.
3503 The packages in this list are needed by one part or another of Org-mode
3504 to function properly.
3506 - inputenc, fontenc: for basic font and character selection
3507 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
3508 for interpreting the entities in `org-entities'. You can skip some of these
3509 packages if you don't use any of the symbols in it.
3510 - graphicx: for including images
3511 - float, wrapfig: for figure placement
3512 - longtable: for long tables
3513 - hyperref: for cross references
3515 Therefore you should not modify this variable unless you know what you
3516 are doing. The one reason to change it anyway is that you might be loading
3517 some other package that conflicts with one of the default packages.
3518 Each cell is of the format \( \"options\" \"package\" snippet-flag\).
3519 If SNIPPET-FLAG is t, the package also needs to be included when
3520 compiling LaTeX snippets into images for inclusion into HTML."
3521 :group
'org-export-latex
3522 :set
'org-set-packages-alist
3523 :get
'org-get-packages-alist
3527 (list :tag
"options/package pair"
3528 (string :tag
"options")
3529 (string :tag
"package")
3530 (boolean :tag
"Snippet"))
3531 (string :tag
"A line of LaTeX"))))
3533 (defcustom org-export-latex-packages-alist nil
3534 "Alist of packages to be inserted in every LaTeX header.
3535 These will be inserted after `org-export-latex-default-packages-alist'.
3536 Each cell is of the format \( \"options\" \"package\" snippet-flag \).
3537 SNIPPET-FLAG, when t, indicates that this package is also needed when
3538 turning LaTeX snippets into images for inclusion into HTML.
3539 Make sure that you only list packages here which:
3540 - you want in every file
3541 - do not conflict with the default packages in
3542 `org-export-latex-default-packages-alist'
3543 - do not conflict with the setup in `org-format-latex-header'."
3544 :group
'org-export-latex
3545 :set
'org-set-packages-alist
3546 :get
'org-get-packages-alist
3549 (list :tag
"options/package pair"
3550 (string :tag
"options")
3551 (string :tag
"package")
3552 (boolean :tag
"Snippet"))
3553 (string :tag
"A line of LaTeX"))))
3556 (defgroup org-appearance nil
3557 "Settings for Org-mode appearance."
3558 :tag
"Org Appearance"
3561 (defcustom org-level-color-stars-only nil
3562 "Non-nil means fontify only the stars in each headline.
3563 When nil, the entire headline is fontified.
3564 Changing it requires restart of `font-lock-mode' to become effective
3565 also in regions already fontified."
3566 :group
'org-appearance
3569 (defcustom org-hide-leading-stars nil
3570 "Non-nil means hide the first N-1 stars in a headline.
3571 This works by using the face `org-hide' for these stars. This
3572 face is white for a light background, and black for a dark
3573 background. You may have to customize the face `org-hide' to
3575 Changing it requires restart of `font-lock-mode' to become effective
3576 also in regions already fontified.
3577 You may also set this on a per-file basis by adding one of the following
3578 lines to the buffer:
3580 #+STARTUP: hidestars
3581 #+STARTUP: showstars"
3582 :group
'org-appearance
3585 (defcustom org-hidden-keywords nil
3586 "List of symbols corresponding to keywords to be hidden the org buffer.
3587 For example, a value '(title) for this list will make the document's title
3588 appear in the buffer without the initial #+TITLE: keyword."
3589 :group
'org-appearance
3591 :type
'(set (const :tag
"#+AUTHOR" author
)
3592 (const :tag
"#+DATE" date
)
3593 (const :tag
"#+EMAIL" email
)
3594 (const :tag
"#+TITLE" title
)))
3596 (defcustom org-custom-properties nil
3597 "List of properties (as strings) with a special meaning.
3598 The default use of these custom properties is to let the user
3599 hide them with `org-toggle-custom-properties-visibility'."
3600 :group
'org-properties
3601 :group
'org-appearance
3603 :type
'(repeat (string :tag
"Property Name")))
3605 (defcustom org-fontify-done-headline nil
3606 "Non-nil means change the face of a headline if it is marked DONE.
3607 Normally, only the TODO/DONE keyword indicates the state of a headline.
3608 When this is non-nil, the headline after the keyword is set to the
3609 `org-headline-done' as an additional indication."
3610 :group
'org-appearance
3613 (defcustom org-fontify-emphasized-text t
3614 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3615 Changing this variable requires a restart of Emacs to take effect."
3616 :group
'org-appearance
3619 (defcustom org-fontify-whole-heading-line nil
3620 "Non-nil means fontify the whole line for headings.
3621 This is useful when setting a background color for the
3623 :group
'org-appearance
3626 (defcustom org-highlight-latex-fragments-and-specials nil
3627 "Non-nil means fontify what is treated specially by the exporters."
3628 :group
'org-appearance
3631 (defcustom org-hide-emphasis-markers nil
3632 "Non-nil mean font-lock should hide the emphasis marker characters."
3633 :group
'org-appearance
3636 (defcustom org-pretty-entities nil
3637 "Non-nil means show entities as UTF8 characters.
3638 When nil, the \\name form remains in the buffer."
3639 :group
'org-appearance
3643 (defcustom org-pretty-entities-include-sub-superscripts t
3644 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3645 :group
'org-appearance
3649 (defvar org-emph-re nil
3650 "Regular expression for matching emphasis.
3651 After a match, the match groups contain these elements:
3652 0 The match of the full regular expression, including the characters
3653 before and after the proper match
3654 1 The character before the proper match, or empty at beginning of line
3655 2 The proper match, including the leading and trailing markers
3656 3 The leading marker like * or /, indicating the type of highlighting
3657 4 The text between the emphasis markers, not including the markers
3658 5 The character after the match, empty at the end of a line")
3659 (defvar org-verbatim-re nil
3660 "Regular expression for matching verbatim text.")
3661 (defvar org-emphasis-regexp-components
) ; defined just below
3662 (defvar org-emphasis-alist
) ; defined just below
3663 (defun org-set-emph-re (var val
)
3664 "Set variable and compute the emphasis regular expression."
3666 (when (and (boundp 'org-emphasis-alist
)
3667 (boundp 'org-emphasis-regexp-components
)
3668 org-emphasis-alist org-emphasis-regexp-components
)
3669 (let* ((e org-emphasis-regexp-components
)
3675 (body1 (concat body
"*?"))
3676 (markers (mapconcat 'car org-emphasis-alist
""))
3677 (vmarkers (mapconcat
3678 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3679 org-emphasis-alist
"")))
3680 ;; make sure special characters appear at the right position in the class
3681 (if (string-match "\\^" markers
)
3682 (setq markers
(concat (replace-match "" t t markers
) "^")))
3683 (if (string-match "-" markers
)
3684 (setq markers
(concat (replace-match "" t t markers
) "-")))
3685 (if (string-match "\\^" vmarkers
)
3686 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3687 (if (string-match "-" vmarkers
)
3688 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3690 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3691 (int-to-string nl
) "\\}")))
3694 (concat "\\([" pre
"]\\|^\\)"
3696 "\\([" markers
"]\\)"
3704 "\\([" post
"]\\|$\\)"))
3705 (setq org-verbatim-re
3706 (concat "\\([" pre
"]\\|^\\)"
3708 "\\([" vmarkers
"]\\)"
3716 "\\([" post
"]\\|$\\)")))))
3718 (defcustom org-emphasis-regexp-components
3719 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3720 "Components used to build the regular expression for emphasis.
3721 This is a list with five entries. Terminology: In an emphasis string
3722 like \" *strong word* \", we call the initial space PREMATCH, the final
3723 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3724 and \"trong wor\" is the body. The different components in this variable
3725 specify what is allowed/forbidden in each part:
3727 pre Chars allowed as prematch. Beginning of line will be allowed too.
3728 post Chars allowed as postmatch. End of line will be allowed too.
3729 border The chars *forbidden* as border characters.
3730 body-regexp A regexp like \".\" to match a body character. Don't use
3731 non-shy groups here, and don't allow newline here.
3732 newline The maximum number of newlines allowed in an emphasis exp.
3734 Use customize to modify this, or restart Emacs after changing it."
3735 :group
'org-appearance
3736 :set
'org-set-emph-re
3738 (sexp :tag
"Allowed chars in pre ")
3739 (sexp :tag
"Allowed chars in post ")
3740 (sexp :tag
"Forbidden chars in border ")
3741 (sexp :tag
"Regexp for body ")
3742 (integer :tag
"number of newlines allowed")
3743 (option (boolean :tag
"Please ignore this button"))))
3745 (defcustom org-emphasis-alist
3746 `(("*" bold
"<b>" "</b>")
3747 ("/" italic
"<i>" "</i>")
3748 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
3749 ("=" org-code
"<code>" "</code>" verbatim
)
3750 ("~" org-verbatim
"<code>" "</code>" verbatim
)
3751 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
3754 "Special syntax for emphasized text.
3755 Text starting and ending with a special character will be emphasized, for
3756 example *bold*, _underlined_ and /italic/. This variable sets the marker
3757 characters, the face to be used by font-lock for highlighting in Org-mode
3758 Emacs buffers, and the HTML tags to be used for this.
3759 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3760 For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
3761 Use customize to modify this, or restart Emacs after changing it."
3762 :group
'org-appearance
3763 :set
'org-set-emph-re
3766 (string :tag
"Marker character")
3768 (face :tag
"Font-lock-face")
3769 (plist :tag
"Face property list"))
3770 (string :tag
"HTML start tag")
3771 (string :tag
"HTML end tag")
3772 (option (const verbatim
)))))
3774 (defvar org-protecting-blocks
3775 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3776 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3777 This is needed for font-lock setup.")
3779 ;;; Miscellaneous options
3781 (defgroup org-completion nil
3782 "Completion in Org-mode."
3783 :tag
"Org Completion"
3786 (defcustom org-completion-use-ido nil
3787 "Non-nil means use ido completion wherever possible.
3788 Note that `ido-mode' must be active for this variable to be relevant.
3789 If you decide to turn this variable on, you might well want to turn off
3790 `org-outline-path-complete-in-steps'.
3791 See also `org-completion-use-iswitchb'."
3792 :group
'org-completion
3795 (defcustom org-completion-use-iswitchb nil
3796 "Non-nil means use iswitchb completion wherever possible.
3797 Note that `iswitchb-mode' must be active for this variable to be relevant.
3798 If you decide to turn this variable on, you might well want to turn off
3799 `org-outline-path-complete-in-steps'.
3800 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3801 :group
'org-completion
3804 (defcustom org-completion-fallback-command
'hippie-expand
3805 "The expansion command called by \\[pcomplete] in normal context.
3806 Normal means, no org-mode-specific context."
3807 :group
'org-completion
3810 ;;; Functions and variables from their packages
3811 ;; Declared here to avoid compiler warnings
3814 (defvar outline-mode-menu-heading
)
3815 (defvar outline-mode-menu-show
)
3816 (defvar outline-mode-menu-hide
)
3817 (defvar zmacs-regions
) ; XEmacs regions
3820 (defvar mark-active
)
3823 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
3824 (declare-function calendar-forward-day
"cal-move" (arg))
3825 (declare-function calendar-goto-date
"cal-move" (date))
3826 (declare-function calendar-goto-today
"cal-move" ())
3827 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
3828 (defvar calc-embedded-close-formula
)
3829 (defvar calc-embedded-open-formula
)
3830 (declare-function cdlatex-tab
"ext:cdlatex" ())
3831 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
3832 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
3833 (defvar font-lock-unfontify-region-function
)
3834 (declare-function iswitchb-read-buffer
"iswitchb"
3835 (prompt &optional default require-match start matches-set
))
3836 (defvar iswitchb-temp-buflist
)
3837 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
3838 (defvar org-agenda-tags-todo-honor-ignore-options
)
3839 (declare-function org-agenda-skip
"org-agenda" ())
3841 org-agenda-format-item
"org-agenda"
3842 (extra txt
&optional category tags dotime noprefix remove-re habitp
))
3843 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
3844 (declare-function org-agenda-change-all-lines
"org-agenda"
3845 (newhead hdmarker
&optional fixface just-this
))
3846 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
3847 (declare-function org-agenda-maybe-redo
"org-agenda" ())
3848 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
3850 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
3851 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3852 "org-agenda" (&optional end
))
3853 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
3854 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
3855 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
3856 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
3857 (declare-function org-indent-mode
"org-indent" (&optional arg
))
3858 (declare-function parse-time-string
"parse-time" (string))
3859 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
3860 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
3861 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
3862 (defvar remember-data-file
)
3863 (defvar texmathp-why
)
3864 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
3865 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
3867 (defvar w3m-current-url
)
3868 (defvar w3m-current-title
)
3870 (defvar org-latex-regexps
)
3872 ;;; Autoload and prepare some org modules
3874 ;; Some table stuff that needs to be defined here, because it is used
3875 ;; by the functions setting up org-mode or checking for table context.
3877 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
3878 "Detect an org-type or table-type table.")
3879 (defconst org-table-line-regexp
"^[ \t]*|"
3880 "Detect an org-type table line.")
3881 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
3882 "Detect an org-type table line.")
3883 (defconst org-table-hline-regexp
"^[ \t]*|-"
3884 "Detect an org-type table hline.")
3885 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
3886 "Detect a table-type table hline.")
3887 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
3888 "Detect the first line outside a table when searching from within it.
3889 This works for both table types.")
3891 ;; Autoload the functions in org-table.el that are needed by functions here.
3894 (org-autoload "org-table"
3895 '(org-table-begin org-table-blank-field org-table-end
)))
3898 (defun turn-on-orgtbl ()
3899 "Unconditionally turn on `orgtbl-mode'."
3900 (require 'org-table
)
3903 (defun org-at-table-p (&optional table-type
)
3904 "Return t if the cursor is inside an org-type table.
3905 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3906 (if org-enable-table-editor
3908 (beginning-of-line 1)
3909 (looking-at (if table-type org-table-any-line-regexp
3910 org-table-line-regexp
)))
3912 (defsubst org-table-p
() (org-at-table-p))
3914 (defun org-at-table.el-p
()
3915 "Return t if and only if we are at a table.el table."
3916 (and (org-at-table-p 'any
)
3918 (goto-char (org-table-begin 'any
))
3919 (looking-at org-table1-hline-regexp
))))
3920 (defun org-table-recognize-table.el
()
3921 "If there is a table.el table nearby, recognize it and move into it."
3922 (if org-table-tab-recognizes-table.el
3923 (if (org-at-table.el-p
)
3925 (beginning-of-line 1)
3926 (if (looking-at org-table-dataline-regexp
)
3928 (if (looking-at org-table1-hline-regexp
)
3930 (beginning-of-line 2)
3931 (if (looking-at org-table-any-border-regexp
)
3932 (beginning-of-line -
1)))))
3933 (if (re-search-forward "|" (org-table-end t
) t
)
3936 (if (table--at-cell-p (point))
3938 (message "recognizing table.el table...")
3939 (table-recognize-table)
3940 (message "recognizing table.el table...done")))
3941 (error "This should not happen"))
3946 (defun org-at-table-hline-p ()
3947 "Return t if the cursor is inside a hline in a table."
3948 (if org-enable-table-editor
3950 (beginning-of-line 1)
3951 (looking-at org-table-hline-regexp
))
3954 (defvar org-table-clean-did-remove-column nil
)
3956 (defun org-table-map-tables (function &optional quietly
)
3957 "Apply FUNCTION to the start of all tables in the buffer."
3961 (goto-char (point-min))
3962 (while (re-search-forward org-table-any-line-regexp nil t
)
3964 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
3965 (beginning-of-line 1)
3966 (when (and (looking-at org-table-line-regexp
)
3967 ;; Exclude tables in src/example/verbatim/clocktable blocks
3968 (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
3969 (save-excursion (funcall function
))
3970 (or (looking-at org-table-line-regexp
)
3972 (re-search-forward org-table-any-border-regexp nil
1))))
3973 (unless quietly
(message "Mapping tables: done")))
3975 ;; Declare and autoload functions from org-exp.el & Co
3977 (declare-function org-default-export-plist
"org-exp")
3978 (declare-function org-infile-export-plist
"org-exp")
3979 (declare-function org-get-current-options
"org-exp")
3981 ;; Declare and autoload functions from org-agenda.el
3984 (org-autoload "org-agenda"
3985 '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
3987 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock" (beg end
))
3988 (declare-function org-clock-update-mode-line
"org-clock" ())
3989 (declare-function org-resolve-clocks
"org-clock"
3990 (&optional also-non-dangling-p prompt last-valid
))
3991 (defvar org-clock-start-time
)
3992 (defvar org-clock-marker
(make-marker)
3993 "Marker recording the last clock-in.")
3994 (defvar org-clock-hd-marker
(make-marker)
3995 "Marker recording the last clock-in, but the headline position.")
3996 (defvar org-clock-heading
""
3997 "The heading of the current clock entry.")
3998 (defun org-clock-is-active ()
3999 "Return non-nil if clock is currently running.
4000 The return value is actually the clock marker."
4001 (marker-buffer org-clock-marker
))
4004 (org-autoload "org-clock" '(org-clock-remove-overlays
4005 org-clock-update-time-maybe
4006 org-clocktable-shift
)))
4008 (defun org-check-running-clock ()
4009 "Check if the current buffer contains the running clock.
4010 If yes, offer to stop it and to save the buffer with the changes."
4011 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4012 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4015 (when (y-or-n-p "Save changed buffer?")
4018 (defun org-clocktable-try-shift (dir n
)
4019 "Check if this line starts a clock table, if yes, shift the time block."
4020 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4021 (org-clocktable-shift dir n
)))
4024 (defun org-clock-persistence-insinuate ()
4025 "Set up hooks for clock persistence."
4026 (require 'org-clock
)
4027 (add-hook 'org-mode-hook
'org-clock-load
)
4028 (add-hook 'kill-emacs-hook
'org-clock-save
))
4030 ;; Define the variable already here, to make sure we have it.
4031 (defvar org-indent-mode nil
4032 "Non-nil if Org-Indent mode is enabled.
4033 Use the command `org-indent-mode' to change this variable.")
4035 ;; Autoload archiving code
4036 ;; The stuff that is needed for cycling and tags has to be defined here.
4038 (defgroup org-archive nil
4039 "Options concerning archiving in Org-mode."
4041 :group
'org-structure
)
4043 (defcustom org-archive-location
"%s_archive::"
4044 "The location where subtrees should be archived.
4046 The value of this variable is a string, consisting of two parts,
4047 separated by a double-colon. The first part is a filename and
4048 the second part is a headline.
4050 When the filename is omitted, archiving happens in the same file.
4051 %s in the filename will be replaced by the current file
4052 name (without the directory part). Archiving to a different file
4053 is useful to keep archived entries from contributing to the
4056 The archived entries will be filed as subtrees of the specified
4057 headline. When the headline is omitted, the subtrees are simply
4058 filed away at the end of the file, as top-level entries. Also in
4059 the heading you can use %s to represent the file name, this can be
4060 useful when using the same archive for a number of different files.
4062 Here are a few examples:
4064 If the current file is Projects.org, archive in file
4065 Projects.org_archive, as top-level trees. This is the default.
4067 \"::* Archived Tasks\"
4068 Archive in the current file, under the top-level headline
4069 \"* Archived Tasks\".
4071 \"~/org/archive.org::\"
4072 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4074 \"~/org/archive.org::* From %s\"
4075 Archive in file ~/org/archive.org (absolute path), under headlines
4076 \"From FILENAME\" where file name is the current file name.
4078 \"~/org/datetree.org::datetree/* Finished Tasks\"
4079 The \"datetree/\" string is special, signifying to archive
4080 items to the datetree. Items are placed in either the CLOSED
4081 date of the item, or the current date if there is no CLOSED date.
4082 The heading will be a subentry to the current date. There doesn't
4083 need to be a heading, but there always needs to be a slash after
4084 datetree. For example, to store archived items directly in the
4085 datetree, use \"~/org/datetree.org::datetree/\".
4087 \"basement::** Finished Tasks\"
4088 Archive in file ./basement (relative path), as level 3 trees
4089 below the level 2 heading \"** Finished Tasks\".
4091 You may set this option on a per-file basis by adding to the buffer a
4094 #+ARCHIVE: basement::** Finished Tasks
4096 You may also define it locally for a subtree by setting an ARCHIVE property
4097 in the entry. If such a property is found in an entry, or anywhere up
4098 the hierarchy, it will be used."
4102 (defcustom org-archive-tag
"ARCHIVE"
4103 "The tag that marks a subtree as archived.
4104 An archived subtree does not open during visibility cycling, and does
4105 not contribute to the agenda listings.
4106 After changing this, font-lock must be restarted in the relevant buffers to
4107 get the proper fontification."
4109 :group
'org-keywords
4112 (defcustom org-agenda-skip-archived-trees t
4113 "Non-nil means the agenda will skip any items located in archived trees.
4114 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4115 variable is no longer recommended, you should leave it at the value t.
4116 Instead, use the key `v' to cycle the archives-mode in the agenda."
4118 :group
'org-agenda-skip
4121 (defcustom org-columns-skip-archived-trees t
4122 "Non-nil means ignore archived trees when creating column view."
4124 :group
'org-properties
4127 (defcustom org-cycle-open-archived-trees nil
4128 "Non-nil means `org-cycle' will open archived trees.
4129 An archived tree is a tree marked with the tag ARCHIVE.
4130 When nil, archived trees will stay folded. You can still open them with
4131 normal outline commands like `show-all', but not with the cycling commands."
4136 (defcustom org-sparse-tree-open-archived-trees nil
4137 "Non-nil means sparse tree construction shows matches in archived trees.
4138 When nil, matches in these trees are highlighted, but the trees are kept in
4141 :group
'org-sparse-trees
4144 (defcustom org-sparse-tree-default-date-type
'scheduled-or-deadline
4145 "The default date type when building a sparse tree.
4146 When this is nil, a date is a scheduled or a deadline timestamp.
4147 Otherwise, these types are allowed:
4150 active: only active timestamps (<...>)
4151 inactive: only inactive timestamps (<...)
4152 scheduled: only scheduled timestamps
4153 deadline: only deadline timestamps"
4154 :type
'(choice (const :tag
"Scheduled or deadline" 'scheduled-or-deadline
)
4155 (const :tag
"All timestamps" all
)
4156 (const :tag
"Only active timestamps" active
)
4157 (const :tag
"Only inactive timestamps" inactive
)
4158 (const :tag
"Only scheduled timestamps" scheduled
)
4159 (const :tag
"Only deadline timestamps" deadline
))
4161 :group
'org-sparse-trees
)
4163 (defun org-cycle-hide-archived-subtrees (state)
4164 "Re-hide all archived subtrees after a visibility state change."
4165 (when (and (not org-cycle-open-archived-trees
)
4166 (not (memq state
'(overview folded
))))
4168 (let* ((globalp (memq state
'(contents all
)))
4169 (beg (if globalp
(point-min) (point)))
4170 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4171 (org-hide-archived-subtrees beg end
)
4173 (if (looking-at (concat ".*:" org-archive-tag
":"))
4174 (message "%s" (substitute-command-keys
4175 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4177 (defun org-force-cycle-archived ()
4178 "Cycle subtree even if it is archived."
4180 (setq this-command
'org-cycle
)
4181 (let ((org-cycle-open-archived-trees t
))
4182 (call-interactively 'org-cycle
)))
4184 (defun org-hide-archived-subtrees (beg end
)
4185 "Re-hide all archived subtrees after a visibility state change."
4187 (let* ((re (concat ":" org-archive-tag
":")))
4189 (while (re-search-forward re end t
)
4190 (when (org-at-heading-p)
4191 (org-flag-subtree t
)
4192 (org-end-of-subtree t
))))))
4194 (declare-function outline-end-of-heading
"outline" ())
4195 (declare-function outline-flag-region
"outline" (from to flag
))
4196 (defun org-flag-subtree (flag)
4198 (org-back-to-heading t
)
4199 (outline-end-of-heading)
4200 (outline-flag-region (point)
4201 (progn (org-end-of-subtree t
) (point))
4204 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4207 (org-autoload "org-archive"
4208 '(org-add-archive-files)))
4210 ;; Autoload Column View Code
4212 (declare-function org-columns-number-to-string
"org-colview" (n fmt
&optional printf
))
4213 (declare-function org-columns-get-format-and-top-level
"org-colview" ())
4214 (declare-function org-columns-compute
"org-colview" (property))
4216 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4217 '(org-columns-number-to-string
4218 org-columns-get-format-and-top-level
4220 org-columns-remove-overlays
))
4224 (declare-function org-id-store-link
"org-id")
4225 (declare-function org-id-locations-load
"org-id")
4226 (declare-function org-id-locations-save
"org-id")
4227 (defvar org-id-track-globally
)
4228 (org-autoload "org-id"
4231 org-id-get-with-outline-path-completion
4232 org-id-get-with-outline-drilling
))
4234 ;;; Variables for pre-computed regular expressions, all buffer local
4236 (defvar org-drawer-regexp
"^[ \t]*:PROPERTIES:[ \t]*$"
4237 "Matches first line of a hidden block.")
4238 (make-variable-buffer-local 'org-drawer-regexp
)
4239 (defvar org-todo-regexp nil
4240 "Matches any of the TODO state keywords.")
4241 (make-variable-buffer-local 'org-todo-regexp
)
4242 (defvar org-not-done-regexp nil
4243 "Matches any of the TODO state keywords except the last one.")
4244 (make-variable-buffer-local 'org-not-done-regexp
)
4245 (defvar org-not-done-heading-regexp nil
4246 "Matches a TODO headline that is not done.")
4247 (make-variable-buffer-local 'org-not-done-regexp
)
4248 (defvar org-todo-line-regexp nil
4249 "Matches a headline and puts TODO state into group 2 if present.")
4250 (make-variable-buffer-local 'org-todo-line-regexp
)
4251 (defvar org-complex-heading-regexp nil
4252 "Matches a headline and puts everything into groups:
4254 group 2: The todo keyword, maybe
4255 group 3: Priority cookie
4256 group 4: True headline
4258 (make-variable-buffer-local 'org-complex-heading-regexp
)
4259 (defvar org-complex-heading-regexp-format nil
4260 "Printf format to make regexp to match an exact headline.
4261 This regexp will match the headline of any node which has the
4262 exact headline text that is put into the format, but may have any
4263 TODO state, priority and tags.")
4264 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4265 (defvar org-todo-line-tags-regexp nil
4266 "Matches a headline and puts TODO state into group 2 if present.
4267 Also put tags into group 4 if tags are present.")
4268 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4269 (defvar org-ds-keyword-length
12
4270 "Maximum length of the DEADLINE and SCHEDULED keywords.")
4271 (make-variable-buffer-local 'org-ds-keyword-length
)
4272 (defvar org-deadline-regexp nil
4273 "Matches the DEADLINE keyword.")
4274 (make-variable-buffer-local 'org-deadline-regexp
)
4275 (defvar org-deadline-time-regexp nil
4276 "Matches the DEADLINE keyword together with a time stamp.")
4277 (make-variable-buffer-local 'org-deadline-time-regexp
)
4278 (defvar org-deadline-line-regexp nil
4279 "Matches the DEADLINE keyword and the rest of the line.")
4280 (make-variable-buffer-local 'org-deadline-line-regexp
)
4281 (defvar org-scheduled-regexp nil
4282 "Matches the SCHEDULED keyword.")
4283 (make-variable-buffer-local 'org-scheduled-regexp
)
4284 (defvar org-scheduled-time-regexp nil
4285 "Matches the SCHEDULED keyword together with a time stamp.")
4286 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4287 (defvar org-closed-time-regexp nil
4288 "Matches the CLOSED keyword together with a time stamp.")
4289 (make-variable-buffer-local 'org-closed-time-regexp
)
4291 (defvar org-keyword-time-regexp nil
4292 "Matches any of the 4 keywords, together with the time stamp.")
4293 (make-variable-buffer-local 'org-keyword-time-regexp
)
4294 (defvar org-keyword-time-not-clock-regexp nil
4295 "Matches any of the 3 keywords, together with the time stamp.")
4296 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4297 (defvar org-maybe-keyword-time-regexp nil
4298 "Matches a timestamp, possibly preceded by a keyword.")
4299 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4300 (defvar org-all-time-keywords nil
4301 "List of time keywords.")
4302 (make-variable-buffer-local 'org-all-time-keywords
)
4304 (defconst org-plain-time-of-day-regexp
4307 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4310 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4312 "Regular expression to match a plain time or time range.
4313 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4314 groups carry important information:
4316 1 the first time, range or not
4317 8 the second time, if it is a range.")
4319 (defconst org-plain-time-extension-regexp
4322 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4323 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4324 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4325 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4326 groups carry important information:
4329 9 minutes of duration")
4331 (defconst org-stamp-time-of-day-regexp
4333 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4334 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4336 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4337 "Regular expression to match a timestamp time or time range.
4338 After a match, the following groups carry important information:
4340 1 date plus weekday, for back referencing to make sure both times are on the same day
4341 2 the first time, range or not
4342 4 the second time, if it is a range.")
4344 (defconst org-startup-options
4345 '(("fold" org-startup-folded t
)
4346 ("overview" org-startup-folded t
)
4347 ("nofold" org-startup-folded nil
)
4348 ("showall" org-startup-folded nil
)
4349 ("showeverything" org-startup-folded showeverything
)
4350 ("content" org-startup-folded content
)
4351 ("indent" org-startup-indented t
)
4352 ("noindent" org-startup-indented nil
)
4353 ("hidestars" org-hide-leading-stars t
)
4354 ("showstars" org-hide-leading-stars nil
)
4355 ("odd" org-odd-levels-only t
)
4356 ("oddeven" org-odd-levels-only nil
)
4357 ("align" org-startup-align-all-tables t
)
4358 ("noalign" org-startup-align-all-tables nil
)
4359 ("inlineimages" org-startup-with-inline-images t
)
4360 ("noinlineimages" org-startup-with-inline-images nil
)
4361 ("customtime" org-display-custom-times t
)
4362 ("logdone" org-log-done time
)
4363 ("lognotedone" org-log-done note
)
4364 ("nologdone" org-log-done nil
)
4365 ("lognoteclock-out" org-log-note-clock-out t
)
4366 ("nolognoteclock-out" org-log-note-clock-out nil
)
4367 ("logrepeat" org-log-repeat state
)
4368 ("lognoterepeat" org-log-repeat note
)
4369 ("nologrepeat" org-log-repeat nil
)
4370 ("logreschedule" org-log-reschedule time
)
4371 ("lognotereschedule" org-log-reschedule note
)
4372 ("nologreschedule" org-log-reschedule nil
)
4373 ("logredeadline" org-log-redeadline time
)
4374 ("lognoteredeadline" org-log-redeadline note
)
4375 ("nologredeadline" org-log-redeadline nil
)
4376 ("logrefile" org-log-refile time
)
4377 ("lognoterefile" org-log-refile note
)
4378 ("nologrefile" org-log-refile nil
)
4379 ("fninline" org-footnote-define-inline t
)
4380 ("nofninline" org-footnote-define-inline nil
)
4381 ("fnlocal" org-footnote-section nil
)
4382 ("fnauto" org-footnote-auto-label t
)
4383 ("fnprompt" org-footnote-auto-label nil
)
4384 ("fnconfirm" org-footnote-auto-label confirm
)
4385 ("fnplain" org-footnote-auto-label plain
)
4386 ("fnadjust" org-footnote-auto-adjust t
)
4387 ("nofnadjust" org-footnote-auto-adjust nil
)
4388 ("constcgs" constants-unit-system cgs
)
4389 ("constSI" constants-unit-system SI
)
4390 ("noptag" org-tag-persistent-alist nil
)
4391 ("hideblocks" org-hide-block-startup t
)
4392 ("nohideblocks" org-hide-block-startup nil
)
4393 ("beamer" org-startup-with-beamer-mode t
)
4394 ("entitiespretty" org-pretty-entities t
)
4395 ("entitiesplain" org-pretty-entities nil
))
4396 "Variable associated with STARTUP options for org-mode.
4397 Each element is a list of three items: the startup options (as written
4398 in the #+STARTUP line), the corresponding variable, and the value to set
4399 this variable to if the option is found. An optional forth element PUSH
4400 means to push this value onto the list in the variable.")
4402 (defun org-update-property-plist (key val props
)
4403 "Update PROPS with KEY and VAL."
4404 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4405 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4406 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4407 (previous (cdr (assoc key props
))))
4409 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4410 (cons (cons key val
) remainder
))))
4412 (defconst org-block-regexp
4413 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4414 "Regular expression for hiding blocks.")
4415 (defconst org-heading-keyword-regexp-format
4416 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4417 "Printf format for a regexp matching an headline with some keyword.
4418 This regexp will match the headline of any node which has the
4419 exact keyword that is put into the format. The keyword isn't in
4420 any group by default, but the stars and the body are.")
4421 (defconst org-heading-keyword-maybe-regexp-format
4422 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4423 "Printf format for a regexp matching an headline, possibly with some keyword.
4424 This regexp can match any headline with the specified keyword, or
4425 without a keyword. The keyword isn't in any group by default,
4426 but the stars and the body are.")
4428 (defun org-set-regexps-and-options ()
4429 "Precompute regular expressions for current buffer."
4430 (when (derived-mode-p 'org-mode
)
4431 (org-set-local 'org-todo-kwd-alist nil
)
4432 (org-set-local 'org-todo-key-alist nil
)
4433 (org-set-local 'org-todo-key-trigger nil
)
4434 (org-set-local 'org-todo-keywords-1 nil
)
4435 (org-set-local 'org-done-keywords nil
)
4436 (org-set-local 'org-todo-heads nil
)
4437 (org-set-local 'org-todo-sets nil
)
4438 (org-set-local 'org-todo-log-states nil
)
4439 (org-set-local 'org-file-properties nil
)
4440 (org-set-local 'org-file-tags nil
)
4441 (let ((re (org-make-options-regexp
4442 '("CATEGORY" "TODO" "COLUMNS"
4443 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
4444 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
4446 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4448 (scripts org-use-sub-superscripts
)
4449 kwds kws0 kwsa key log value cat arch tags const links hw dws
4450 tail sep kws1 prio props ftags drawers beamer-p
4451 ext-setup-or-nil setup-contents
(start 0))
4455 (goto-char (point-min))
4456 (while (or (and ext-setup-or-nil
4457 (string-match re ext-setup-or-nil start
)
4458 (setq start
(match-end 0)))
4459 (and (setq ext-setup-or-nil nil start
0)
4460 (re-search-forward re nil t
)))
4461 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4462 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4463 (if (stringp value
) (setq value
(org-trim value
)))
4465 ((equal key
"CATEGORY")
4467 ((member key
'("SEQ_TODO" "TODO"))
4468 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4469 ((equal key
"TYP_TODO")
4470 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4471 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4472 ;; general TODO-like setup
4473 (push (cons (intern (downcase (match-string 1 key
)))
4474 (org-split-string value splitre
)) kwds
))
4476 (setq tags
(append tags
(if tags
'("\\n") nil
)
4477 (org-split-string value splitre
))))
4478 ((equal key
"COLUMNS")
4479 (org-set-local 'org-columns-default-format value
))
4481 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4482 (push (cons (match-string 1 value
)
4483 (org-trim (match-string 2 value
)))
4485 ((equal key
"PRIORITIES")
4486 (setq prio
(org-split-string value
" +")))
4487 ((equal key
"PROPERTY")
4488 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4489 (setq props
(org-update-property-plist (match-string 1 value
)
4490 (match-string 2 value
)
4492 ((equal key
"FILETAGS")
4493 (when (string-match "\\S-" value
)
4498 (mapcar (lambda (x) (org-split-string x
":"))
4499 (org-split-string value
)))))))
4500 ((equal key
"DRAWERS")
4501 (setq drawers
(delete-dups (append org-drawers
(org-split-string value splitre
)))))
4502 ((equal key
"CONSTANTS")
4503 (setq const
(append const
(org-split-string value splitre
))))
4504 ((equal key
"STARTUP")
4505 (let ((opts (org-split-string value splitre
))
4507 (while (setq l
(pop opts
))
4508 (when (setq l
(assoc l org-startup-options
))
4509 (setq var
(nth 1 l
) val
(nth 2 l
))
4511 (set (make-local-variable var
) val
)
4512 (if (not (listp (symbol-value var
)))
4513 (set (make-local-variable var
) nil
))
4514 (set (make-local-variable var
) (symbol-value var
))
4515 (add-to-list var val
))))))
4516 ((equal key
"ARCHIVE")
4518 (remove-text-properties 0 (length arch
)
4519 '(face t fontified t
) arch
))
4520 ((equal key
"LATEX_CLASS")
4521 (setq beamer-p
(equal value
"beamer")))
4522 ((equal key
"OPTIONS")
4523 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4524 (setq scripts
(read (match-string 2 value
)))))
4525 ((equal key
"SETUPFILE")
4526 (setq setup-contents
(org-file-contents
4528 (org-remove-double-quotes value
))
4530 (if (not ext-setup-or-nil
)
4531 (setq ext-setup-or-nil setup-contents start
0)
4532 (setq ext-setup-or-nil
4533 (concat (substring ext-setup-or-nil
0 start
)
4534 "\n" setup-contents
"\n"
4535 (substring ext-setup-or-nil start
)))))))
4536 ;; search for property blocks
4537 (goto-char (point-min))
4538 (while (re-search-forward org-block-regexp nil t
)
4539 (when (equal "PROPERTY" (upcase (match-string 1)))
4540 (setq value
(replace-regexp-in-string
4541 "[\n\r]" " " (match-string 4)))
4542 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4543 (setq props
(org-update-property-plist (match-string 1 value
)
4544 (match-string 2 value
)
4546 (org-set-local 'org-use-sub-superscripts scripts
)
4548 (org-set-local 'org-category
(intern cat
))
4549 (push (cons "CATEGORY" cat
) props
))
4551 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4552 (setq prio
(mapcar 'string-to-char prio
))
4553 (org-set-local 'org-highest-priority
(nth 0 prio
))
4554 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4555 (org-set-local 'org-default-priority
(nth 2 prio
)))
4556 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4557 (and ftags
(org-set-local 'org-file-tags
4558 (mapcar 'org-add-prop-inherited ftags
)))
4559 (and drawers
(org-set-local 'org-drawers drawers
))
4560 (and arch
(org-set-local 'org-archive-location arch
))
4561 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4562 ;; Process the TODO keywords
4564 ;; Use the global values as if they had been given locally.
4565 (setq kwds
(default-value 'org-todo-keywords
))
4566 (if (stringp (car kwds
))
4567 (setq kwds
(list (cons org-todo-interpretation
4568 (default-value 'org-todo-keywords
)))))
4569 (setq kwds
(reverse kwds
)))
4570 (setq kwds
(nreverse kwds
))
4572 (while (setq kws
(pop kwds
))
4574 (run-hook-with-args-until-success
4575 'org-todo-setup-filter-hook kws
)
4577 (setq inter
(pop kws
) sep
(member "|" kws
)
4578 kws0
(delete "|" (copy-sequence kws
))
4583 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4585 (setq kw
(match-string 1 x
)
4586 key
(and (match-end 2) (match-string 2 x
))
4587 log
(org-extract-log-state-settings x
))
4588 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4589 (and log
(push log org-todo-log-states
))
4591 (error "Invalid TODO keyword %s" x
)))
4593 kwsa
(if kwsa
(append '((:startgroup
))
4597 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4598 tail
(list inter hw
(car dws
) (org-last dws
))))
4599 (add-to-list 'org-todo-heads hw
'append
)
4600 (push kws1 org-todo-sets
)
4601 (setq org-done-keywords
(append org-done-keywords dws nil
))
4602 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4603 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4604 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4605 (setq org-todo-sets
(nreverse org-todo-sets
)
4606 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4607 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4608 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4609 ;; Process the constants
4612 (while (setq e
(pop const
))
4613 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4614 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4615 (setq org-table-formula-constants-local cst
)))
4617 ;; Process the tags.
4620 (while (setq e
(pop tags
))
4622 ((equal e
"{") (push '(:startgroup
) tgs
))
4623 ((equal e
"}") (push '(:endgroup
) tgs
))
4624 ((equal e
"\\n") (push '(:newline
) tgs
))
4625 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4626 (push (cons (match-string 1 e
)
4627 (string-to-char (match-string 2 e
)))
4629 (t (push (list e
) tgs
))))
4630 (org-set-local 'org-tag-alist nil
)
4631 (while (setq e
(pop tgs
))
4632 (or (and (stringp (car e
))
4633 (assoc (car e
) org-tag-alist
))
4634 (push e org-tag-alist
)))))
4636 ;; Compute the regular expressions and other local variables.
4637 ;; Using `org-outline-regexp-bol' would complicate them much,
4638 ;; because of the fixed white space at the end of that string.
4639 (if (not org-done-keywords
)
4640 (setq org-done-keywords
(and org-todo-keywords-1
4641 (list (org-last org-todo-keywords-1
)))))
4642 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4643 (length org-scheduled-string
)
4644 (length org-clock-string
)
4645 (length org-closed-string
)))
4647 (concat "^[ \t]*:\\("
4648 (mapconcat 'regexp-quote org-drawers
"\\|")
4650 org-not-done-keywords
4651 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4654 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4658 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4660 org-not-done-heading-regexp
4661 (format org-heading-keyword-regexp-format org-not-done-regexp
)
4662 org-todo-line-regexp
4663 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
4664 org-complex-heading-regexp
4665 (concat "^\\(\\*+\\)"
4666 "\\(?: +" org-todo-regexp
"\\)?"
4667 "\\(?: +\\(\\[#.\\]\\)\\)?"
4668 "\\(?: +\\(.*?\\)\\)??"
4669 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
4671 org-complex-heading-regexp-format
4672 (concat "^\\(\\*+\\)"
4673 "\\(?: +" org-todo-regexp
"\\)?"
4674 "\\(?: +\\(\\[#.\\]\\)\\)?"
4676 ;; Stats cookies can be stuck to body.
4677 "\\(?:\\[[0-9%%/]+\\] *\\)?"
4679 "\\(?: *\\[[0-9%%/]+\\]\\)?"
4681 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
4683 org-todo-line-tags-regexp
4684 (concat "^\\(\\*+\\)"
4685 "\\(?: +" org-todo-regexp
"\\)?"
4686 "\\(?: +\\(.*?\\)\\)??"
4687 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
4689 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4690 org-deadline-time-regexp
4691 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4692 org-deadline-line-regexp
4693 (concat "\\<\\(" org-deadline-string
"\\).*")
4694 org-scheduled-regexp
4695 (concat "\\<" org-scheduled-string
)
4696 org-scheduled-time-regexp
4697 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4698 org-closed-time-regexp
4699 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4700 org-keyword-time-regexp
4701 (concat "\\<\\(" org-scheduled-string
4702 "\\|" org-deadline-string
4703 "\\|" org-closed-string
4704 "\\|" org-clock-string
"\\)"
4705 " *[[<]\\([^]>]+\\)[]>]")
4706 org-keyword-time-not-clock-regexp
4707 (concat "\\<\\(" org-scheduled-string
4708 "\\|" org-deadline-string
4709 "\\|" org-closed-string
4711 " *[[<]\\([^]>]+\\)[]>]")
4712 org-maybe-keyword-time-regexp
4713 (concat "\\(\\<\\(" org-scheduled-string
4714 "\\|" org-deadline-string
4715 "\\|" org-closed-string
4716 "\\|" org-clock-string
"\\)\\)?"
4717 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4718 org-all-time-keywords
4719 (mapcar (lambda (w) (substring w
0 -
1))
4720 (list org-scheduled-string org-deadline-string
4721 org-clock-string org-closed-string
))
4723 (org-compute-latex-and-specials-regexp)
4724 (org-set-font-lock-defaults))))
4726 (defun org-file-contents (file &optional noerror
)
4727 "Return the contents of FILE, as a string."
4729 (not (file-readable-p file
)))
4732 (message "Cannot read file \"%s\"" file
)
4735 (error "Cannot read file \"%s\"" file
))
4737 (insert-file-contents file
)
4740 (defun org-extract-log-state-settings (x)
4741 "Extract the log state setting from a TODO keyword string.
4742 This will extract info from a string like \"WAIT(w@/!)\"."
4743 (let (kw key log1 log2
)
4744 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4745 (setq kw
(match-string 1 x
)
4746 key
(and (match-end 2) (match-string 2 x
))
4747 log1
(and (match-end 3) (match-string 3 x
))
4748 log2
(and (match-end 4) (match-string 4 x
)))
4751 (and log1
(if (equal log1
"!") 'time
'note
))
4752 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4754 (defun org-remove-keyword-keys (list)
4755 "Remove a pair of parenthesis at the end of each string in LIST."
4757 (if (string-match "(.*)$" x
)
4758 (substring x
0 (match-beginning 0))
4762 (defun org-assign-fast-keys (alist)
4763 "Assign fast keys to a keyword-key alist.
4764 Respect keys that are already there."
4765 (let (new e
(alt ?
0))
4766 (while (setq e
(pop alist
))
4767 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
4768 (cdr e
)) ;; Key already assigned.
4770 (let ((clist (string-to-list (downcase (car e
))))
4771 (used (append new alist
)))
4772 (when (= (car clist
) ?
@)
4774 (while (and clist
(rassoc (car clist
) used
))
4777 (while (rassoc alt used
)
4779 (push (cons (car e
) (or (car clist
) alt
)) new
))))
4782 ;;; Some variables used in various places
4784 (defvar org-window-configuration nil
4785 "Used in various places to store a window configuration.")
4786 (defvar org-selected-window nil
4787 "Used in various places to store a window configuration.")
4788 (defvar org-finish-function nil
4789 "Function to be called when `C-c C-c' is used.
4790 This is for getting out of special buffers like capture.")
4793 ;; FIXME: Occasionally check by commenting these, to make sure
4794 ;; no other functions uses these, forgetting to let-bind them.
4795 (org-no-warnings (defvar entry
)) ;; unprefixed, from calendar.el
4796 (defvar org-last-state
)
4797 (org-no-warnings (defvar date
)) ;; unprefixed, from calendar.el
4799 ;; Defined somewhere in this file, but used before definition.
4800 (defvar org-entities
) ;; defined in org-entities.el
4801 (defvar org-struct-menu
)
4802 (defvar org-org-menu
)
4803 (defvar org-tbl-menu
)
4805 ;;;; Define the Org-mode
4807 ;; We use a before-change function to check if a table might need
4809 (defvar org-table-may-need-update t
4810 "Indicates that a table might need an update.
4811 This variable is set by `org-before-change-function'.
4812 `org-table-align' sets it back to nil.")
4813 (defun org-before-change-function (beg end
)
4814 "Every change indicates that a table might need an update."
4815 (setq org-table-may-need-update t
))
4816 (defvar org-mode-map
)
4817 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4818 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
4819 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4820 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
4821 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
4822 (defvar org-table-buffer-is-an nil
)
4824 (defvar bidi-paragraph-direction
)
4825 (defvar buffer-face-mode-face
)
4828 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4829 (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"))
4830 (require 'noutline
"noutline" 'noerror
) ;; stock XEmacs does not have it
4832 ;; Other stuff we need.
4833 (require 'time-date
)
4834 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
4839 (require 'org-entities
)
4840 ;; (require 'org-compat) moved higher up in the file before it is first used
4841 (require 'org-faces
)
4843 (require 'org-pcomplete
)
4845 (require 'org-footnote
)
4852 (require 'ob-tangle
)
4853 (require 'ob-comint
)
4857 (define-derived-mode org-mode outline-mode
"Org"
4858 "Outline-based notes management and organizer, alias
4859 \"Carsten's outline-mode for keeping track of everything.\"
4861 Org-mode develops organizational tasks around a NOTES file which
4862 contains information about projects as plain text. Org-mode is
4863 implemented on top of outline-mode, which is ideal to keep the content
4864 of large files well structured. It supports ToDo items, deadlines and
4865 time stamps, which magically appear in the diary listing of the Emacs
4866 calendar. Tables are easily created with a built-in table editor.
4867 Plain text URL-like links connect to websites, emails (VM), Usenet
4868 messages (Gnus), BBDB entries, and any files related to the project.
4869 For printing and sharing of notes, an Org-mode file (or a part of it)
4870 can be exported as a structured ASCII or HTML file.
4872 The following commands are available:
4876 ;; Get rid of Outline menus, they are not needed
4877 ;; Need to do this here because define-derived-mode sets up
4878 ;; the keymap so late. Still, it is a waste to call this each time
4879 ;; we switch another buffer into org-mode.
4880 (if (featurep 'xemacs
)
4881 (when (boundp 'outline-mode-menu-heading
)
4882 ;; Assume this is Greg's port, it uses easymenu
4883 (easy-menu-remove outline-mode-menu-heading
)
4884 (easy-menu-remove outline-mode-menu-show
)
4885 (easy-menu-remove outline-mode-menu-hide
))
4886 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
4887 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
4888 (define-key org-mode-map
[menu-bar show
] 'undefined
))
4890 (org-load-modules-maybe)
4891 (easy-menu-add org-org-menu
)
4892 (easy-menu-add org-tbl-menu
)
4893 (org-install-agenda-files-menu)
4894 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
4895 (add-to-invisibility-spec '(org-cwidth))
4896 (add-to-invisibility-spec '(org-hide-block . t
))
4897 (when (featurep 'xemacs
)
4898 (org-set-local 'line-move-ignore-invisible t
))
4899 (org-set-local 'outline-regexp org-outline-regexp
)
4900 (org-set-local 'outline-level
'org-outline-level
)
4901 (setq bidi-paragraph-direction
'left-to-right
)
4902 (when (and org-ellipsis
4903 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
4904 (fboundp 'make-glyph-code
))
4905 (unless org-display-table
4906 (setq org-display-table
(make-display-table)))
4907 (set-display-table-slot
4910 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
4912 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
4913 (setq buffer-display-table org-display-table
))
4914 (org-set-regexps-and-options)
4915 (when (and org-tag-faces
(not org-tags-special-faces-re
))
4916 ;; tag faces set outside customize.... force initialization.
4917 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
4919 (org-set-local 'calc-embedded-open-mode
"# ")
4920 (modify-syntax-entry ?
@ "w")
4921 (if org-startup-truncated
(setq truncate-lines t
))
4922 (when org-startup-indented
(require 'org-indent
) (org-indent-mode 1))
4923 (org-set-local 'font-lock-unfontify-region-function
4924 'org-unfontify-region
)
4925 ;; Activate before-change-function
4926 (org-set-local 'org-table-may-need-update t
)
4927 (org-add-hook 'before-change-functions
'org-before-change-function nil
4929 ;; Check for running clock before killing a buffer
4930 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
4932 (org-set-local 'indent-line-function
'org-indent-line
)
4933 (org-set-local 'indent-region-function
'org-indent-region
)
4934 ;; Initialize radio targets.
4935 (org-update-radio-target-regexp)
4936 ;; Filling and auto-filling.
4939 (org-setup-comments-handling)
4940 ;; Beginning/end of defun
4941 (org-set-local 'beginning-of-defun-function
'org-back-to-heading
)
4942 (org-set-local 'end-of-defun-function
(lambda () (interactive) (org-end-of-subtree nil t
)))
4943 ;; Next error for sparse trees
4944 (org-set-local 'next-error-function
'org-occur-next-match
)
4945 ;; Make sure dependence stuff works reliably, even for users who set it
4947 (if org-enforce-todo-dependencies
4948 (add-hook 'org-blocker-hook
4949 'org-block-todo-from-children-or-siblings-or-parent
)
4950 (remove-hook 'org-blocker-hook
4951 'org-block-todo-from-children-or-siblings-or-parent
))
4952 (if org-enforce-todo-checkbox-dependencies
4953 (add-hook 'org-blocker-hook
4954 'org-block-todo-from-checkboxes
)
4955 (remove-hook 'org-blocker-hook
4956 'org-block-todo-from-checkboxes
))
4958 ;; Align options lines
4960 'align-mode-rules-list
4961 '((org-in-buffer-settings
4962 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4963 (modes .
'(org-mode)))))
4966 (org-set-local 'imenu-create-index-function
4967 'org-imenu-get-tree
)
4969 ;; Make isearch reveal context
4970 (if (or (featurep 'xemacs
)
4971 (not (boundp 'outline-isearch-open-invisible-function
)))
4972 ;; Emacs 21 and XEmacs make use of the hook
4973 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
4974 ;; Emacs 22 deals with this through a special variable
4975 (org-set-local 'outline-isearch-open-invisible-function
4976 (lambda (&rest ignore
) (org-show-context 'isearch
))))
4978 ;; Setup the pcomplete hooks
4979 (set (make-local-variable 'pcomplete-command-completion-function
)
4980 'org-pcomplete-initial
)
4981 (set (make-local-variable 'pcomplete-command-name-function
)
4982 'org-command-at-point
)
4983 (set (make-local-variable 'pcomplete-default-completion-function
)
4985 (set (make-local-variable 'pcomplete-parse-arguments-function
)
4986 'org-parse-arguments
)
4987 (set (make-local-variable 'pcomplete-termination-string
) "")
4988 (when (>= emacs-major-version
23)
4989 (set (make-local-variable 'buffer-face-mode-face
) 'org-default
))
4991 ;; If empty file that did not turn on org-mode automatically, make it to.
4992 (if (and org-insert-mode-line-in-empty-file
4993 (org-called-interactively-p 'any
)
4994 (= (point-min) (point-max)))
4995 (insert "# -*- mode: org -*-\n\n"))
4996 (unless org-inhibit-startup
4997 (and org-startup-with-beamer-mode
(org-beamer-mode))
4998 (when org-startup-align-all-tables
4999 (let ((bmp (buffer-modified-p)))
5000 (org-table-map-tables 'org-table-align
'quietly
)
5001 (set-buffer-modified-p bmp
)))
5002 (when org-startup-with-inline-images
5003 (org-display-inline-images))
5004 (unless org-inhibit-startup-visibility-stuff
5005 (org-set-startup-visibility)))
5006 ;; Try to set org-hide correctly
5007 (set-face-foreground 'org-hide
(org-find-invisible-foreground)))
5009 (when (fboundp 'abbrev-table-put
)
5010 (abbrev-table-put org-mode-abbrev-table
5011 :parents
(list text-mode-abbrev-table
)))
5013 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5016 (defun org-find-invisible-foreground ()
5017 (let ((candidates (remove
5020 (list (face-background 'default
)
5021 (face-background 'org-default
))
5024 (when (boundp alist
)
5025 (cdr (assoc 'background-color
(symbol-value alist
)))))
5026 '(default-frame-alist initial-frame-alist window-system-default-frame-alist
))
5027 (list (face-foreground 'org-hide
))))))
5028 (car (remove nil candidates
))))
5030 (defun org-current-time ()
5031 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
5032 (if (> (car org-time-stamp-rounding-minutes
) 1)
5033 (let ((r (car org-time-stamp-rounding-minutes
))
5034 (time (decode-time)))
5036 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5041 "Return today date, considering `org-extend-today-until'."
5043 (time-subtract (current-time)
5044 (list 0 (* 3600 org-extend-today-until
) 0))))
5046 ;;;; Font-Lock stuff, including the activators
5048 (defvar org-mouse-map
(make-sparse-keymap))
5049 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5050 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5051 (when org-mouse-1-follows-link
5052 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5053 (when org-tab-follows-link
5054 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5055 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5057 (require 'font-lock
)
5059 (defconst org-non-link-chars
"]\t\n\r<>")
5060 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5061 "shell" "elisp" "doi" "message"))
5062 (defvar org-link-types-re nil
5063 "Matches a link that has a url-like prefix like \"http:\"")
5064 (defvar org-link-re-with-space nil
5065 "Matches a link with spaces, optional angular brackets around it.")
5066 (defvar org-link-re-with-space2 nil
5067 "Matches a link with spaces, optional angular brackets around it.")
5068 (defvar org-link-re-with-space3 nil
5069 "Matches a link with spaces, only for internal part in bracket links.")
5070 (defvar org-angle-link-re nil
5071 "Matches link with angular brackets, spaces are allowed.")
5072 (defvar org-plain-link-re nil
5073 "Matches plain link, without spaces.")
5074 (defvar org-bracket-link-regexp nil
5075 "Matches a link in double brackets.")
5076 (defvar org-bracket-link-analytic-regexp nil
5077 "Regular expression used to analyze links.
5078 Here is what the match groups contain after a match:
5084 (defvar org-bracket-link-analytic-regexp
++ nil
5085 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5086 (defvar org-any-link-re nil
5087 "Regular expression matching any link.")
5089 (defcustom org-match-sexp-depth
3
5090 "Number of stacked braces for sub/superscript matching.
5091 This has to be set before loading org.el to be effective."
5092 :group
'org-export-translation
; ??????????????????????????/
5095 (defun org-create-multibrace-regexp (left right n
)
5096 "Create a regular expression which will match a balanced sexp.
5097 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5098 as single character strings.
5099 The regexp returned will match the entire expression including the
5100 delimiters. It will also define a single group which contains the
5101 match except for the outermost delimiters. The maximum depth of
5102 stacked delimiters is N. Escaping delimiters is not possible."
5103 (let* ((nothing (concat "[^" left right
"]*?"))
5106 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5109 re
(concat re or next
)
5110 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5111 (concat left
"\\(" re
"\\)" right
)))
5113 (defvar org-match-substring-regexp
5115 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5116 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5118 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5120 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5121 "The regular expression matching a sub- or superscript.")
5123 (defvar org-match-substring-with-braces-regexp
5125 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5126 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5128 "The regular expression matching a sub- or superscript, forcing braces.")
5130 (defun org-make-link-regexps ()
5131 "Update the link regular expressions.
5132 This should be called after the variable `org-link-types' has changed."
5133 (setq org-link-types-re
5135 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
5136 org-link-re-with-space
5138 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5139 "\\([^" org-non-link-chars
" ]"
5140 "[^" org-non-link-chars
"]*"
5141 "[^" org-non-link-chars
" ]\\)>?")
5142 org-link-re-with-space2
5144 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5145 "\\([^" org-non-link-chars
" ]"
5147 "[^" org-non-link-chars
" ]\\)>?")
5148 org-link-re-with-space3
5150 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5151 "\\([^" org-non-link-chars
" ]"
5155 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5156 "\\([^" org-non-link-chars
" ]"
5157 "[^" org-non-link-chars
"]*"
5161 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5162 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5163 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5164 org-bracket-link-regexp
5165 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5166 org-bracket-link-analytic-regexp
5169 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
5172 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5174 org-bracket-link-analytic-regexp
++
5177 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
5180 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5183 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5184 org-angle-link-re
"\\)\\|\\("
5185 org-plain-link-re
"\\)")))
5187 (org-make-link-regexps)
5189 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5190 "Regular expression for fast time stamp matching.")
5191 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5192 "Regular expression for fast time stamp matching.")
5193 (defconst org-ts-regexp0
5194 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5195 "Regular expression matching time strings for analysis.
5196 This one does not require the space after the date, so it can be used
5197 on a string that terminates immediately after the date.")
5198 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5199 "Regular expression matching time strings for analysis.")
5200 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5201 "Regular expression matching time stamps, with groups.")
5202 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5203 "Regular expression matching time stamps (also [..]), with groups.")
5204 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5205 "Regular expression matching a time stamp range.")
5206 (defconst org-tr-regexp-both
5207 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5208 "Regular expression matching a time stamp range.")
5209 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5210 org-ts-regexp
"\\)?")
5211 "Regular expression matching a time stamp or time stamp range.")
5212 (defconst org-tsr-regexp-both
5213 (concat "[^][]" ;; Don't activate dates in links
5214 org-ts-regexp-both
"\\(--?-?"
5215 org-ts-regexp-both
"\\)?")
5216 "Regular expression matching a time stamp or time stamp range.
5217 The time stamps may be either active or inactive.")
5219 (defvar org-emph-face nil
)
5221 (defun org-do-emphasis-faces (limit)
5222 "Run through the buffer and add overlays to emphasized strings."
5224 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5225 (if (not (= (char-after (match-beginning 3))
5226 (char-after (match-beginning 4))))
5229 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5230 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5234 (org-remove-flyspell-overlays-in
5235 (match-beginning 0) (match-end 0)))
5236 (add-text-properties (match-beginning 2) (match-end 2)
5237 '(font-lock-multiline t org-emphasis t
))
5238 (when org-hide-emphasis-markers
5239 (add-text-properties (match-end 4) (match-beginning 5)
5240 '(invisible org-link
))
5241 (add-text-properties (match-beginning 3) (match-end 3)
5242 '(invisible org-link
)))))
5246 (defun org-emphasize (&optional char
)
5247 "Insert or change an emphasis, i.e. a font like bold or italic.
5248 If there is an active region, change that region to a new emphasis.
5249 If there is no region, just insert the marker characters and position
5250 the cursor between them.
5251 CHAR should be either the marker character, or the first character of the
5252 HTML tag associated with that emphasis. If CHAR is a space, the means
5253 to remove the emphasis of the selected region.
5254 If char is not given (for example in an interactive call) it
5255 will be prompted for."
5257 (let ((eal org-emphasis-alist
) e det
5258 (erc org-emphasis-regexp-components
)
5260 (string "") beg end move tag c s
)
5261 (if (org-region-active-p)
5262 (setq beg
(region-beginning) end
(region-end)
5263 string
(buffer-substring beg end
))
5266 (while (setq e
(pop eal
))
5267 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
5269 (push (cons c
(string-to-char (car e
))) det
)
5270 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
5271 (substring tag
1)))))
5272 (setq det
(nreverse det
))
5274 (message "%s" (concat "Emphasis marker or tag:" prompt
))
5275 (setq char
(read-char-exclusive)))
5276 (setq char
(or (cdr (assoc char det
)) char
))
5277 (if (equal char ?\
)
5278 (setq s
"" move nil
)
5279 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5280 (error "No such emphasis marker: \"%c\"" char
))
5281 (setq s
(char-to-string char
)))
5282 (while (and (> (length string
) 1)
5283 (equal (substring string
0 1) (substring string -
1))
5284 (assoc (substring string
0 1) org-emphasis-alist
))
5285 (setq string
(substring string
1 -
1)))
5286 (setq string
(concat s string s
))
5287 (if beg
(delete-region beg end
))
5289 (string-match (concat "[" (nth 0 erc
) "\n]")
5290 (char-to-string (char-before (point)))))
5293 (string-match (concat "[" (nth 1 erc
) "\n]")
5294 (char-to-string (char-after (point)))))
5295 (insert " ") (backward-char 1))
5297 (and move
(backward-char 1))))
5299 (defconst org-nonsticky-props
5300 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link
))
5302 (defsubst org-rear-nonsticky-at
(pos)
5303 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5305 (defun org-activate-plain-links (limit)
5306 "Run through the buffer and add overlays to links."
5309 (when (and (re-search-forward (concat org-plain-link-re
) limit t
)
5310 (not (org-in-src-block-p)))
5311 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5312 (setq f
(get-text-property (match-beginning 0) 'face
))
5313 (unless (or (org-in-src-block-p)
5315 (and (listp f
) (memq 'org-tag f
)))
5316 (add-text-properties (match-beginning 0) (match-end 0)
5317 (list 'mouse-face
'highlight
5319 'keymap org-mouse-map
))
5320 (org-rear-nonsticky-at (match-end 0)))
5323 (defun org-activate-code (limit)
5324 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5326 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5327 (remove-text-properties (match-beginning 0) (match-end 0)
5328 '(display t invisible t intangible t
))
5331 (defcustom org-src-fontify-natively nil
5332 "When non-nil, fontify code in code blocks."
5335 :group
'org-appearance
5338 (defcustom org-allow-promoting-top-level-subtree nil
5339 "When non-nil, allow promoting a top level subtree.
5340 The leading star of the top level headline will be replaced
5344 :group
'org-appearance
)
5346 (defun org-fontify-meta-lines-and-blocks (limit)
5348 (org-fontify-meta-lines-and-blocks-1 limit
)
5349 (error (message "org-mode fontification error"))))
5351 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5352 "Fontify #+ lines and blocks, in the correct ways."
5353 (let ((case-fold-search t
))
5354 (if (re-search-forward
5355 "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5357 (let ((beg (match-beginning 0))
5358 (block-start (match-end 0))
5360 (lang (match-string 7))
5361 (beg1 (line-beginning-position 2))
5362 (dc1 (downcase (match-string 2)))
5363 (dc3 (downcase (match-string 3)))
5364 end end1 quoting block-type ovl
)
5366 ((member dc1
'("+html:" "+ascii:" "+latex:" "+docbook:"))
5367 ;; a single line of backend-specific content
5368 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5369 (remove-text-properties (match-beginning 0) (match-end 0)
5370 '(display t invisible t intangible t
))
5371 (add-text-properties (match-beginning 1) (match-end 3)
5372 '(font-lock-fontified t face org-meta-line
))
5373 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5374 '(font-lock-fontified t face org-block
))
5375 ; for backend-specific code
5377 ((and (match-end 4) (equal dc3
"+begin"))
5379 (setq block-type
(downcase (match-string 5))
5380 quoting
(member block-type org-protecting-blocks
))
5381 (when (re-search-forward
5382 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5383 nil t
) ;; on purpose, we look further than LIMIT
5384 (setq end
(min (point-max) (match-end 0))
5385 end1
(min (point-max) (1- (match-beginning 0))))
5386 (setq block-end
(match-beginning 0))
5388 (remove-text-properties beg end
5389 '(display t invisible t intangible t
)))
5390 (add-text-properties
5392 '(font-lock-fontified t font-lock-multiline t
))
5393 (add-text-properties beg beg1
'(face org-meta-line
))
5394 (add-text-properties end1
(min (point-max) (1+ end
))
5395 '(face org-meta-line
)) ; for end_src
5397 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5398 (org-src-font-lock-fontify-block lang block-start block-end
)
5399 ;; remove old background overlays
5401 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5402 (delete-overlay ov
)))
5403 (overlays-at (/ (+ beg1 block-end
) 2)))
5404 ;; add a background overlay
5405 (setq ovl
(make-overlay beg1 block-end
))
5406 (overlay-put ovl
'face
'org-block-background
)
5407 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5409 (add-text-properties beg1
(min (point-max) (1+ end1
))
5410 '(face org-block
))) ; end of source block
5411 ((not org-fontify-quote-and-verse-blocks
))
5412 ((string= block-type
"quote")
5413 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-quote
)))
5414 ((string= block-type
"verse")
5415 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-verse
))))
5416 (add-text-properties beg beg1
'(face org-block-begin-line
))
5417 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5418 '(face org-block-end-line
))
5420 ((member dc1
'("+title:" "+author:" "+email:" "+date:"))
5421 (add-text-properties
5423 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5424 '(font-lock-fontified t invisible t
)
5425 '(font-lock-fontified t face org-document-info-keyword
)))
5426 (add-text-properties
5427 (match-beginning 6) (min (point-max) (1+ (match-end 6)))
5428 (if (string-equal dc1
"+title:")
5429 '(font-lock-fontified t face org-document-title
)
5430 '(font-lock-fontified t face org-document-info
))))
5431 ((or (equal dc1
"+results")
5432 (member dc1
'("+begin:" "+end:" "+caption:" "+label:"
5433 "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
5434 "+call:" "+header:" "+headers:" "+name:"))
5435 (and (match-end 4) (equal dc3
"+attr")))
5436 (add-text-properties
5438 '(font-lock-fontified t face org-meta-line
))
5440 ((member dc3
'(" " ""))
5441 (add-text-properties
5443 '(font-lock-fontified t face font-lock-comment-face
)))
5444 ((not (member (char-after beg
) '(?\ ?
\t)))
5445 ;; just any other in-buffer setting, but not indented
5446 (add-text-properties
5448 '(font-lock-fontified t face org-meta-line
))
5452 (defun org-activate-angle-links (limit)
5453 "Run through the buffer and add overlays to links."
5454 (if (and (re-search-forward org-angle-link-re limit t
)
5455 (not (org-in-src-block-p)))
5457 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5458 (add-text-properties (match-beginning 0) (match-end 0)
5459 (list 'mouse-face
'highlight
5460 'keymap org-mouse-map
))
5461 (org-rear-nonsticky-at (match-end 0))
5464 (defun org-activate-footnote-links (limit)
5465 "Run through the buffer and add overlays to footnotes."
5466 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5468 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5469 (org-remove-flyspell-overlays-in beg end
)
5470 (add-text-properties beg end
5471 (list 'mouse-face
'highlight
5472 'keymap org-mouse-map
5474 (if (= (point-at-bol) beg
)
5475 "Footnote definition"
5476 "Footnote reference")
5477 'font-lock-fontified t
5478 'font-lock-multiline t
5479 'face
'org-footnote
))))))
5481 (defun org-activate-bracket-links (limit)
5482 "Run through the buffer and add overlays to bracketed links."
5483 (if (and (re-search-forward org-bracket-link-regexp limit t
)
5484 (not (org-in-src-block-p)))
5485 (let* ((help (concat "LINK: "
5486 (org-match-string-no-properties 1)))
5487 ;; FIXME: above we should remove the escapes.
5488 ;; but that requires another match, protecting match data,
5489 ;; a lot of overhead for font-lock.
5490 (ip (org-maybe-intangible
5491 (list 'invisible
'org-link
5492 'keymap org-mouse-map
'mouse-face
'highlight
5493 'font-lock-multiline t
'help-echo help
)))
5494 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5495 'font-lock-multiline t
'help-echo help
)))
5496 ;; We need to remove the invisible property here. Table narrowing
5497 ;; may have made some of this invisible.
5498 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5499 (remove-text-properties (match-beginning 0) (match-end 0)
5503 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5504 (org-rear-nonsticky-at (match-beginning 3))
5505 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5506 (org-rear-nonsticky-at (match-end 3))
5507 (add-text-properties (match-end 3) (match-end 0) ip
)
5508 (org-rear-nonsticky-at (match-end 0)))
5509 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5510 (org-rear-nonsticky-at (match-beginning 1))
5511 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5512 (org-rear-nonsticky-at (match-end 1))
5513 (add-text-properties (match-end 1) (match-end 0) ip
)
5514 (org-rear-nonsticky-at (match-end 0)))
5517 (defun org-activate-dates (limit)
5518 "Run through the buffer and add overlays to dates."
5519 (if (re-search-forward org-tsr-regexp-both limit t
)
5521 (org-remove-flyspell-overlays-in (1+ (match-beginning 0)) (match-end 0))
5522 (add-text-properties (1+ (match-beginning 0)) (match-end 0)
5523 (list 'mouse-face
'highlight
5524 'keymap org-mouse-map
))
5525 (org-rear-nonsticky-at (match-end 0))
5526 (when org-display-custom-times
5528 (org-display-custom-time (match-beginning 3) (match-end 3)))
5529 (org-display-custom-time (match-beginning 1) (match-end 1)))
5532 (defvar org-target-link-regexp nil
5533 "Regular expression matching radio targets in plain text.")
5534 (make-variable-buffer-local 'org-target-link-regexp
)
5535 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5536 "Regular expression matching a link target.")
5537 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5538 "Regular expression matching a radio target.")
5539 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5540 "Regular expression matching any target.")
5542 (defun org-activate-target-links (limit)
5543 "Run through the buffer and add overlays to target matches."
5544 (when org-target-link-regexp
5545 (let ((case-fold-search t
))
5546 (if (re-search-forward org-target-link-regexp limit t
)
5548 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5549 (add-text-properties (match-beginning 0) (match-end 0)
5550 (list 'mouse-face
'highlight
5551 'keymap org-mouse-map
5552 'help-echo
"Radio target link"
5553 'org-linked-text t
))
5554 (org-rear-nonsticky-at (match-end 0))
5557 (defun org-update-radio-target-regexp ()
5558 "Find all radio targets in this file and update the regular expression."
5560 (when (memq 'radio org-activate-links
)
5561 (setq org-target-link-regexp
5562 (org-make-target-link-regexp (org-all-targets 'radio
)))
5563 (org-restart-font-lock)))
5565 (defun org-hide-wide-columns (limit)
5567 (setq s
(text-property-any (point) (or limit
(point-max))
5570 (setq e
(next-single-property-change s
'org-cwidth
))
5571 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5575 (defvar org-latex-and-specials-regexp nil
5576 "Regular expression for highlighting export special stuff.")
5577 (defvar org-match-substring-regexp
)
5578 (defvar org-match-substring-with-braces-regexp
)
5580 ;; This should be with the exporter code, but we also use if for font-locking
5581 (defconst org-export-html-special-string-regexps
5582 '(("\\\\-" .
"­")
5583 ("---\\([^-]\\)" .
"—\\1")
5584 ("--\\([^-]\\)" .
"–\\1")
5585 ("\\.\\.\\." .
"…"))
5586 "Regular expressions for special string conversion.")
5589 (defun org-compute-latex-and-specials-regexp ()
5590 "Compute regular expression for stuff treated specially by exporters."
5591 (if (not org-highlight-latex-fragments-and-specials
)
5592 (org-set-local 'org-latex-and-specials-regexp nil
)
5595 ((matchers (plist-get org-format-latex-options
:matchers
))
5596 (latexs (delq nil
(mapcar (lambda (x) (if (member (car x
) matchers
) x
))
5597 org-latex-regexps
)))
5598 (org-export-allow-BIND nil
)
5599 (options (org-combine-plists (org-default-export-plist)
5600 (org-infile-export-plist)))
5601 (org-export-with-sub-superscripts (plist-get options
:sub-superscript
))
5602 (org-export-with-LaTeX-fragments (plist-get options
:LaTeX-fragments
))
5603 (org-export-with-TeX-macros (plist-get options
:TeX-macros
))
5604 (org-export-html-expand (plist-get options
:expand-quoted-html
))
5605 (org-export-with-special-strings (plist-get options
:special-strings
))
5608 ((equal org-export-with-sub-superscripts
'{})
5609 (list org-match-substring-with-braces-regexp
))
5610 (org-export-with-sub-superscripts
5611 (list org-match-substring-regexp
))))
5613 (if org-export-with-LaTeX-fragments
5614 (mapcar (lambda (x) (nth 1 x
)) latexs
)))
5616 (if org-export-with-TeX-macros
5617 (list (concat "\\\\"
5623 (append org-entities-user
5625 (if (boundp 'org-latex-entities
)
5627 (or (car-safe x
) x
))
5632 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5633 (re-special (if org-export-with-special-strings
5634 (mapcar (lambda (x) (car x
))
5635 org-export-html-special-string-regexps
)))
5639 (if org-export-html-expand
"@<[^>\n]+>")
5642 'org-latex-and-specials-regexp
5643 (mapconcat 'identity
(append re-latex re-sub re-macros re-special
5646 (defun org-do-latex-and-special-faces (limit)
5647 "Run through the buffer and add overlays to links."
5648 (when org-latex-and-specials-regexp
5650 (while (and (not rtn
) (re-search-forward org-latex-and-specials-regexp
5652 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5654 '(org-code org-verbatim underline
)))
5657 d
(cond ((member (char-after (1+ (match-beginning 0)))
5660 (font-lock-prepend-text-property
5661 (+ d
(match-beginning 0)) (match-end 0)
5662 'face
'org-latex-and-export-specials
)
5663 (add-text-properties (+ d
(match-beginning 0)) (match-end 0)
5664 '(font-lock-multiline t
)))))
5667 (defun org-restart-font-lock ()
5668 "Restart `font-lock-mode', to force refontification."
5669 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5671 (font-lock-mode 1)))
5673 (defun org-all-targets (&optional radio
)
5674 "Return a list of all targets in this file.
5675 With optional argument RADIO, only find radio targets."
5676 (let ((re (if radio org-radio-target-regexp org-target-regexp
))
5679 (goto-char (point-min))
5680 (while (re-search-forward re nil t
)
5681 (add-to-list 'rtn
(downcase (org-match-string-no-properties 1))))
5684 (defun org-make-target-link-regexp (targets)
5685 "Make regular expression matching all strings in TARGETS.
5686 The regular expression finds the targets also if there is a line break
5693 (setq x
(regexp-quote x
))
5694 (while (string-match " +" x
)
5695 (setq x
(replace-match "\\s-+" t t x
)))
5701 (defun org-activate-tags (limit)
5702 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5704 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5705 (add-text-properties (match-beginning 1) (match-end 1)
5706 (list 'mouse-face
'highlight
5707 'keymap org-mouse-map
))
5708 (org-rear-nonsticky-at (match-end 1))
5711 (defun org-outline-level ()
5712 "Compute the outline level of the heading at point.
5713 This function assumes that the cursor is at the beginning of a line matched
5714 by `outline-regexp'. Otherwise it returns garbage.
5715 If this is called at a normal headline, the level is the number of stars.
5716 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
5718 (looking-at org-outline-regexp
)
5719 (1- (- (match-end 0) (match-beginning 0)))))
5721 (defvar org-font-lock-keywords nil
)
5723 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5724 "Regular expression matching a property line.")
5726 (defvar org-font-lock-hook nil
5727 "Functions to be called for special font lock stuff.")
5729 (defvar org-font-lock-set-keywords-hook nil
5730 "Functions that can manipulate `org-font-lock-extra-keywords'.
5731 This is called after `org-font-lock-extra-keywords' is defined, but before
5732 it is installed to be used by font lock. This can be useful if something
5733 needs to be inserted at a specific position in the font-lock sequence.")
5735 (defun org-font-lock-hook (limit)
5736 "Run `org-font-lock-hook' within LIMIT."
5737 (run-hook-with-args 'org-font-lock-hook limit
))
5739 (defun org-set-font-lock-defaults ()
5740 "Set font lock defaults for the current buffer."
5741 (let* ((em org-fontify-emphasized-text
)
5742 (lk org-activate-links
)
5743 (org-font-lock-extra-keywords
5746 '(org-font-lock-hook)
5748 `(,(if org-fontify-whole-heading-line
5749 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5750 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5751 (1 (org-get-level-face 1))
5752 (2 (org-get-level-face 2))
5753 (3 (org-get-level-face 3)))
5755 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5758 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
5759 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
5760 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
5761 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
5763 (list org-drawer-regexp
'(0 'org-special-keyword t
))
5764 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
5766 (list org-property-re
5767 '(1 'org-special-keyword t
)
5768 '(3 'org-property-value t
))
5770 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
5771 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
5772 (if (memq 'plain lk
) '(org-activate-plain-links))
5773 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
5774 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
5775 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
5776 (if (memq 'footnote lk
) '(org-activate-footnote-links))
5777 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
5778 '(org-hide-wide-columns (0 nil append
))
5780 (list (format org-heading-keyword-regexp-format
5782 '(2 (org-get-todo-face 2) t
))
5784 (if org-fontify-done-headline
5785 (list (format org-heading-keyword-regexp-format
5788 (mapconcat 'regexp-quote org-done-keywords
"\\|")
5790 '(2 'org-headline-done t
))
5793 '(org-font-lock-add-priority-faces)
5795 '(org-font-lock-add-tag-faces)
5797 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
5798 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
5799 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
5800 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
5803 (if (featurep 'xemacs
)
5804 '(org-do-emphasis-faces (0 nil append
))
5805 '(org-do-emphasis-faces)))
5807 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
5808 1 'org-checkbox prepend
)
5809 (if (cdr (assq 'checkbox org-list-automatic-rules
))
5810 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5811 (0 (org-get-checkbox-statistics-face) t
)))
5812 ;; Description list items
5813 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
5814 1 'org-list-dt prepend
)
5815 ;; ARCHIVEd headings
5817 org-outline-regexp-bol
5818 "\\(.*:" org-archive-tag
":.*\\)")
5819 '(1 'org-archived prepend
))
5821 '(org-do-latex-and-special-faces)
5822 '(org-fontify-entities)
5823 '(org-raise-scripts)
5825 '(org-activate-code (1 'org-code t
))
5827 (list (format org-heading-keyword-regexp-format
5829 org-comment-string
"\\|" org-quote-string
5831 '(2 'org-special-keyword t
))
5832 ;; Blocks and meta lines
5833 '(org-fontify-meta-lines-and-blocks)
5835 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
5836 (run-hooks 'org-font-lock-set-keywords-hook
)
5837 ;; Now set the full font-lock-keywords
5838 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
5839 (org-set-local 'font-lock-defaults
5840 '(org-font-lock-keywords t nil nil backward-paragraph
))
5841 (kill-local-variable 'font-lock-keywords
) nil
))
5843 (defun org-toggle-pretty-entities ()
5844 "Toggle the composition display of entities as UTF8 characters."
5846 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
5847 (org-restart-font-lock)
5848 (if org-pretty-entities
5849 (message "Entities are displayed as UTF8 characters")
5852 (org-decompose-region (point-min) (point-max))
5853 (message "Entities are displayed plain"))))
5855 (defvar org-custom-properties-overlays nil
5856 "List of overlays used for custom properties.")
5857 (make-variable-buffer-local 'org-custom-properties-overlays
)
5859 (defun org-toggle-custom-properties-visibility ()
5860 "Display or hide properties in `org-custom-properties'."
5862 (if org-custom-properties-overlays
5863 (progn (mapc 'delete-overlay org-custom-properties-overlays
)
5864 (setq org-custom-properties-overlays nil
))
5865 (unless (not org-custom-properties
)
5869 (goto-char (point-min))
5870 (while (re-search-forward org-property-re nil t
)
5872 (when (equal p
(substring (match-string 1) 1 -
1))
5873 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
5874 (overlay-put o
'invisible t
)
5875 (overlay-put o
'org-custom-property t
)
5876 (push o org-custom-properties-overlays
))))
5877 org-custom-properties
)))))))
5879 (defun org-fontify-entities (limit)
5880 "Find an entity to fontify."
5882 (when org-pretty-entities
5884 (while (re-search-forward
5885 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
5887 (if (and (not (org-in-indented-comment-line))
5888 (setq ee
(org-entity-get (match-string 1)))
5889 (= (length (nth 6 ee
)) 1))
5891 ((end (if (equal (match-string 2) "{}")
5894 (add-text-properties
5895 (match-beginning 0) end
5896 (list 'font-lock-fontified t
))
5897 (compose-region (match-beginning 0) end
5903 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
5904 "Fontify string S like in Org-mode."
5907 (let ((org-odd-levels-only odd-levels
))
5909 (font-lock-fontify-buffer)
5915 (defun org-get-level-face (n)
5916 "Get the right face for match N in font-lock matching of headlines."
5917 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
5918 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
5919 (if org-cycle-level-faces
5920 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
5921 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
5923 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
5925 (t (if org-level-color-stars-only nil org-f
))))
5928 (defun org-get-todo-face (kwd)
5929 "Get the right face for a TODO keyword KWD.
5930 If KWD is a number, get the corresponding match group."
5931 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5932 (or (org-face-from-face-or-color
5933 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
5934 (and (member kwd org-done-keywords
) 'org-done
)
5937 (defun org-face-from-face-or-color (context inherit face-or-color
)
5938 "Create a face list that inherits INHERIT, but sets the foreground color.
5939 When FACE-OR-COLOR is not a string, just return it."
5940 (if (stringp face-or-color
)
5941 (list :inherit inherit
5942 (cdr (assoc context org-faces-easy-properties
))
5946 (defun org-font-lock-add-tag-faces (limit)
5947 "Add the special tag faces."
5948 (when (and org-tag-faces org-tags-special-faces-re
)
5949 (while (re-search-forward org-tags-special-faces-re limit t
)
5950 (add-text-properties (match-beginning 1) (match-end 1)
5951 (list 'face
(org-get-tag-face 1)
5952 'font-lock-fontified t
))
5953 (backward-char 1))))
5955 (defun org-font-lock-add-priority-faces (limit)
5956 "Add the special priority faces."
5957 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
5958 (when (save-match-data (org-at-heading-p))
5959 (add-text-properties
5960 (match-beginning 0) (match-end 0)
5961 (list 'face
(or (org-face-from-face-or-color
5962 'priority
'org-special-keyword
5963 (cdr (assoc (char-after (match-beginning 1))
5964 org-priority-faces
)))
5965 'org-special-keyword
)
5966 'font-lock-fontified t
)))))
5968 (defun org-get-tag-face (kwd)
5969 "Get the right face for a TODO keyword KWD.
5970 If KWD is a number, get the corresponding match group."
5971 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5972 (or (org-face-from-face-or-color
5973 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
5976 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
5977 "Remove fontification and activation overlays from links."
5978 (font-lock-default-unfontify-region beg end
)
5979 (let* ((buffer-undo-list t
)
5980 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
5981 (inhibit-modification-hooks t
)
5982 deactivate-mark buffer-file-name buffer-file-truename
)
5983 (org-decompose-region beg end
)
5984 (remove-text-properties beg end
5985 '(mouse-face t keymap t org-linked-text t
5986 invisible t intangible t
5987 org-no-flyspell t org-emphasis t
))
5988 (org-remove-font-lock-display-properties beg end
)))
5990 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
5991 ((raise 0.3) (height 0.7))
5994 "Display properties for showing superscripts and subscripts.")
5996 (defun org-remove-font-lock-display-properties (beg end
)
5997 "Remove specific display properties that have been added by font lock.
5998 The will remove the raise properties that are used to show superscripts
6002 (setq next
(next-single-property-change beg
'display nil end
)
6003 prop
(get-text-property beg
'display
))
6004 (if (member prop org-script-display
)
6005 (put-text-property beg next
'display nil
))
6008 (defun org-raise-scripts (limit)
6009 "Add raise properties to sub/superscripts."
6010 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
6011 (if (re-search-forward
6012 (if (eq org-use-sub-superscripts t
)
6013 org-match-substring-regexp
6014 org-match-substring-with-braces-regexp
)
6016 (let* ((pos (point)) table-p comment-p
6017 (mpos (match-beginning 3))
6018 (emph-p (get-text-property mpos
'org-emphasis
))
6019 (link-p (get-text-property mpos
'mouse-face
))
6020 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6021 (goto-char (point-at-bol))
6022 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
6023 comment-p
(org-looking-at-p "[ \t]*#"))
6025 ;; FIXME: Should we go back one character here, for a_b^c
6026 ;; (goto-char (1- pos)) ;????????????????????
6027 (if (or comment-p emph-p link-p keyw-p
)
6029 (put-text-property (match-beginning 3) (match-end 0)
6031 (if (equal (char-after (match-beginning 2)) ?^
)
6032 (nth (if table-p
3 1) org-script-display
)
6033 (nth (if table-p
2 0) org-script-display
)))
6034 (add-text-properties (match-beginning 2) (match-end 2)
6036 'org-dwidth t
'org-dwidth-n
1))
6037 (if (and (eq (char-after (match-beginning 3)) ?
{)
6038 (eq (char-before (match-end 3)) ?
}))
6040 (add-text-properties
6041 (match-beginning 3) (1+ (match-beginning 3))
6042 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6043 (add-text-properties
6044 (1- (match-end 3)) (match-end 3)
6045 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6048 ;;;; Visibility cycling, including org-goto and indirect buffer
6052 (defvar org-cycle-global-status nil
)
6053 (make-variable-buffer-local 'org-cycle-global-status
)
6054 (defvar org-cycle-subtree-status nil
)
6055 (make-variable-buffer-local 'org-cycle-subtree-status
)
6057 (defvar org-inlinetask-min-level
)
6060 (defun org-cycle (&optional arg
)
6061 "TAB-action and visibility cycling for Org-mode.
6063 This is the command invoked in Org-mode by the TAB key. Its main purpose
6064 is outline visibility cycling, but it also invokes other actions
6065 in special contexts.
6067 - When this function is called with a prefix argument, rotate the entire
6068 buffer through 3 states (global cycling)
6069 1. OVERVIEW: Show only top-level headlines.
6070 2. CONTENTS: Show all headlines of all levels, but no body text.
6071 3. SHOW ALL: Show everything.
6072 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6073 determined by the variable `org-startup-folded', and by any VISIBILITY
6074 properties in the buffer.
6075 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6076 including any drawers.
6078 - When inside a table, re-align the table and move to the next field.
6080 - When point is at the beginning of a headline, rotate the subtree started
6081 by this line through 3 different states (local cycling)
6082 1. FOLDED: Only the main headline is shown.
6083 2. CHILDREN: The main headline and the direct children are shown.
6084 From this state, you can move to one of the children
6085 and zoom in further.
6086 3. SUBTREE: Show the entire subtree, including body text.
6087 If there is no subtree, switch directly from CHILDREN to FOLDED.
6089 - When point is at the beginning of an empty headline and the variable
6090 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6091 of the headline by demoting and promoting it to likely levels. This
6092 speeds up creation document structure by pressing TAB once or several
6093 times right after creating a new headline.
6095 - When there is a numeric prefix, go up to a heading with level ARG, do
6096 a `show-subtree' and return to the previous cursor position. If ARG
6097 is negative, go up that many levels.
6099 - When point is not at the beginning of a headline, execute the global
6100 binding for TAB, which is re-indenting the line. See the option
6101 `org-cycle-emulate-tab' for details.
6103 - Special case: if point is at the beginning of the buffer and there is
6104 no headline in line 1, this function will act as if called with prefix arg
6105 (C-u TAB, same as S-TAB) also when called without prefix arg.
6106 But only if also the variable `org-cycle-global-at-bob' is t."
6108 (org-load-modules-maybe)
6109 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6110 (and org-cycle-level-after-item
/entry-creation
6111 (or (org-cycle-level)
6112 (org-cycle-item-indentation))))
6114 (or org-cycle-max-level
6115 (and (boundp 'org-inlinetask-min-level
)
6116 org-inlinetask-min-level
6117 (1- org-inlinetask-min-level
))))
6118 (nstars (and limit-level
6119 (if org-odd-levels-only
6120 (and limit-level
(1- (* limit-level
2)))
6123 (if (not (derived-mode-p 'org-mode
))
6125 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6126 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6127 (not (looking-at org-outline-regexp
))))
6130 (delq 'org-optimize-window-after-visibility-change
6131 (copy-sequence org-cycle-hook
))
6135 (if (or bob-special
(equal arg
'(4)))
6136 ;; special case: use global cycling
6142 (setq last-command
'dummy
)
6143 (org-set-startup-visibility)
6144 (message "Startup visibility, plus VISIBILITY properties"))
6148 (message "Entire buffer visible, including drawers"))
6150 ;; Table: enter it or move to the next field.
6151 ((org-at-table-p 'any
)
6152 (if (org-at-table.el-p
)
6153 (message "Use C-c ' to edit table.el tables")
6154 (if arg
(org-table-edit-field t
)
6155 (org-table-justify-field-maybe)
6156 (call-interactively 'org-table-next-field
))))
6158 ((run-hook-with-args-until-success
6159 'org-tab-after-check-for-table-hook
))
6161 ;; Global cycling: delegate to `org-cycle-internal-global'.
6162 ((eq arg t
) (org-cycle-internal-global))
6164 ;; Drawers: delegate to `org-flag-drawer'.
6165 ((and org-drawers org-drawer-regexp
6167 (beginning-of-line 1)
6168 (looking-at org-drawer-regexp
)))
6169 (org-flag-drawer ; toggle block visibility
6170 (not (get-char-property (match-end 0) 'invisible
))))
6172 ;; Show-subtree, ARG levels up from here.
6175 (org-back-to-heading)
6176 (outline-up-heading (if (< arg
0) (- arg
)
6177 (- (funcall outline-level
) arg
)))
6178 (org-show-subtree)))
6180 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6181 ((and (featurep 'org-inlinetask
)
6182 (org-inlinetask-at-task-p)
6183 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6184 (org-inlinetask-toggle-visibility))
6186 ((org-try-cdlatex-tab))
6188 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6189 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6190 (save-excursion (beginning-of-line 1)
6191 (looking-at org-outline-regexp
)))
6192 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6193 (org-cycle-internal-local))
6195 ;; From there: TAB emulation and template completion.
6196 (buffer-read-only (org-back-to-heading))
6198 ((run-hook-with-args-until-success
6199 'org-tab-after-check-for-cycling-hook
))
6201 ((org-try-structure-completion))
6203 ((run-hook-with-args-until-success
6204 'org-tab-before-tab-emulation-hook
))
6206 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6208 (not (looking-at org-outline-regexp
))))
6209 (call-interactively (global-key-binding "\t")))
6211 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6212 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6213 (or (and (eq org-cycle-emulate-tab
'white
)
6214 (= (match-end 0) (point-at-eol)))
6215 (and (eq org-cycle-emulate-tab
'whitestart
)
6216 (>= (match-end 0) pos
))))
6218 (eq org-cycle-emulate-tab t
))
6219 (call-interactively (global-key-binding "\t")))
6222 (org-back-to-heading)
6225 (defun org-cycle-internal-global ()
6226 "Do the global cycling action."
6227 ;; Hack to avoid display of messages for .org attachments in Gnus
6228 (let ((ga (string-match "\\*fontification" (buffer-name))))
6230 ((and (eq last-command this-command
)
6231 (eq org-cycle-global-status
'overview
))
6232 ;; We just created the overview - now do table of contents
6233 ;; This can be slow in very large buffers, so indicate action
6234 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6235 (unless ga
(message "CONTENTS..."))
6237 (unless ga
(message "CONTENTS...done"))
6238 (setq org-cycle-global-status
'contents
)
6239 (run-hook-with-args 'org-cycle-hook
'contents
))
6241 ((and (eq last-command this-command
)
6242 (eq org-cycle-global-status
'contents
))
6243 ;; We just showed the table of contents - now show everything
6244 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6246 (unless ga
(message "SHOW ALL"))
6247 (setq org-cycle-global-status
'all
)
6248 (run-hook-with-args 'org-cycle-hook
'all
))
6251 ;; Default action: go to overview
6252 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6254 (unless ga
(message "OVERVIEW"))
6255 (setq org-cycle-global-status
'overview
)
6256 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6258 (defun org-cycle-internal-local ()
6259 "Do the local cycling action."
6260 (let ((goal-column 0) eoh eol eos has-children children-skipped struct
)
6261 ;; First, determine end of headline (EOH), end of subtree or item
6262 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6267 (setq struct
(org-list-struct))
6268 (setq eoh
(point-at-eol))
6269 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6270 (setq has-children
(org-list-has-child-p (point) struct
)))
6271 (org-back-to-heading)
6272 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6273 (setq eos
(save-excursion (1- (org-end-of-subtree t t
))))
6276 (let ((level (funcall outline-level
)))
6277 (outline-next-heading)
6278 (and (org-at-heading-p t
)
6279 (> (funcall outline-level
) level
))))
6281 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6282 ;; Determine end invisible part of buffer (EOL)
6283 (beginning-of-line 2)
6284 ;; XEmacs doesn't have `next-single-char-property-change'
6285 (if (featurep 'xemacs
)
6286 (while (and (not (eobp)) ;; this is like `next-line'
6287 (get-char-property (1- (point)) 'invisible
))
6288 (beginning-of-line 2))
6289 (while (and (not (eobp)) ;; this is like `next-line'
6290 (get-char-property (1- (point)) 'invisible
))
6291 (goto-char (next-single-char-property-change (point) 'invisible
))
6292 (and (eolp) (beginning-of-line 2))))
6294 ;; Find out what to do next and set `this-command'
6297 ;; Nothing is hidden behind this heading
6298 (unless (org-before-first-heading-p)
6299 (run-hook-with-args 'org-pre-cycle-hook
'empty
))
6300 (message "EMPTY ENTRY")
6301 (setq org-cycle-subtree-status nil
)
6304 (outline-next-heading)
6305 (if (outline-invisible-p) (org-flag-heading nil
))))
6306 ((and (or (>= eol eos
)
6307 (not (string-match "\\S-" (buffer-substring eol eos
))))
6309 (not (setq children-skipped
6310 org-cycle-skip-children-state-if-no-children
))))
6311 ;; Entire subtree is hidden in one line: children view
6312 (unless (org-before-first-heading-p)
6313 (run-hook-with-args 'org-pre-cycle-hook
'children
))
6315 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6317 (org-with-limited-levels (show-children))
6318 ;; FIXME: This slows down the func way too much.
6319 ;; How keep drawers hidden in subtree anyway?
6320 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6321 ;; (org-cycle-hide-drawers 'subtree))
6323 ;; Fold every list in subtree to top-level items.
6324 (when (eq org-cycle-include-plain-lists
'integrate
)
6326 (org-back-to-heading)
6327 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6328 (beginning-of-line 1)
6329 (let* ((struct (org-list-struct))
6330 (prevs (org-list-prevs-alist struct
))
6331 (end (org-list-get-bottom-point struct
)))
6332 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6333 (org-list-get-all-items (point) struct prevs
))
6334 (goto-char end
))))))
6335 (message "CHILDREN")
6338 (outline-next-heading)
6339 (if (outline-invisible-p) (org-flag-heading nil
)))
6340 (setq org-cycle-subtree-status
'children
)
6341 (unless (org-before-first-heading-p)
6342 (run-hook-with-args 'org-cycle-hook
'children
)))
6343 ((or children-skipped
6344 (and (eq last-command this-command
)
6345 (eq org-cycle-subtree-status
'children
)))
6346 ;; We just showed the children, or no children are there,
6347 ;; now show everything.
6348 (unless (org-before-first-heading-p)
6349 (run-hook-with-args 'org-pre-cycle-hook
'subtree
))
6350 (outline-flag-region eoh eos nil
)
6351 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6352 (setq org-cycle-subtree-status
'subtree
)
6353 (unless (org-before-first-heading-p)
6354 (run-hook-with-args 'org-cycle-hook
'subtree
)))
6356 ;; Default action: hide the subtree.
6357 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6358 (outline-flag-region eoh eos t
)
6360 (setq org-cycle-subtree-status
'folded
)
6361 (unless (org-before-first-heading-p)
6362 (run-hook-with-args 'org-cycle-hook
'folded
))))))
6365 (defun org-global-cycle (&optional arg
)
6366 "Cycle the global visibility. For details see `org-cycle'.
6367 With \\[universal-argument] prefix arg, switch to startup visibility.
6368 With a numeric prefix, show all headlines up to that level."
6370 (let ((org-cycle-include-plain-lists
6371 (if (derived-mode-p 'org-mode
) org-cycle-include-plain-lists nil
)))
6375 (hide-sublevels arg
)
6376 (setq org-cycle-global-status
'contents
))
6378 (org-set-startup-visibility)
6379 (message "Startup visibility, plus VISIBILITY properties."))
6381 (org-cycle '(4))))))
6383 (defun org-set-startup-visibility ()
6384 "Set the visibility required by startup options and properties."
6386 ((eq org-startup-folded t
)
6388 ((eq org-startup-folded
'content
)
6389 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6390 (org-cycle '(4)) (org-cycle '(4)))))
6391 (unless (eq org-startup-folded
'showeverything
)
6392 (if org-hide-block-startup
(org-hide-block-all))
6393 (org-set-visibility-according-to-property 'no-cleanup
)
6394 (org-cycle-hide-archived-subtrees 'all
)
6395 (org-cycle-hide-drawers 'all
)
6396 (org-cycle-show-empty-lines t
)))
6398 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6399 "Switch subtree visibilities according to :VISIBILITY: property."
6401 (let (org-show-entry-below state
)
6403 (goto-char (point-min))
6404 (while (re-search-forward
6405 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6407 (setq state
(match-string 1))
6409 (org-back-to-heading t
)
6413 ((equal state
'("fold" "folded"))
6415 ((equal state
"children")
6416 (org-show-hidden-entry)
6418 ((equal state
"content")
6421 (org-narrow-to-subtree)
6423 ((member state
'("all" "showall"))
6426 (org-cycle-hide-archived-subtrees 'all
)
6427 (org-cycle-hide-drawers 'all
)
6428 (org-cycle-show-empty-lines 'all
)))))
6430 ;; This function uses outline-regexp instead of the more fundamental
6431 ;; org-outline-regexp so that org-cycle-global works outside of Org
6432 ;; buffers, where outline-regexp is needed.
6433 (defun org-overview ()
6434 "Switch to overview mode, showing only top-level headlines.
6435 Really, this shows all headlines with level equal or greater than the level
6436 of the first headline in the buffer. This is important, because if the
6437 first headline is not level one, then (hide-sublevels 1) gives confusing
6440 (let ((level (save-excursion
6441 (goto-char (point-min))
6442 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6444 (goto-char (match-beginning 0))
6445 (funcall outline-level
))))))
6446 (and level
(hide-sublevels level
))))
6448 (defun org-content (&optional arg
)
6449 "Show all headlines in the buffer, like a table of contents.
6450 With numerical argument N, show content up to level N."
6453 ;; Visit all headings and show their offspring
6454 (and (integerp arg
) (org-overview))
6455 (goto-char (point-max))
6457 (while (and (progn (condition-case nil
6458 (outline-previous-visible-heading 1)
6459 (error (goto-char (point-min))))
6461 (looking-at org-outline-regexp
))
6463 (show-children (1- arg
))
6465 (if (bobp) (throw 'exit nil
))))))
6468 (defun org-optimize-window-after-visibility-change (state)
6469 "Adjust the window after a change in outline visibility.
6470 This function is the default value of the hook `org-cycle-hook'."
6471 (when (get-buffer-window (current-buffer))
6473 ((eq state
'content
) nil
)
6474 ((eq state
'all
) nil
)
6475 ((eq state
'folded
) nil
)
6476 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6477 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6479 (defun org-remove-empty-overlays-at (pos)
6480 "Remove outline overlays that do not contain non-white stuff."
6483 (and (eq 'outline
(overlay-get o
'invisible
))
6484 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6486 (delete-overlay o
)))
6489 (defun org-clean-visibility-after-subtree-move ()
6490 "Fix visibility issues after moving a subtree."
6491 ;; First, find a reasonable region to look at:
6492 ;; Start two siblings above, end three below
6493 (let* ((beg (save-excursion
6494 (and (org-get-last-sibling)
6495 (org-get-last-sibling))
6497 (end (save-excursion
6498 (and (org-get-next-sibling)
6499 (org-get-next-sibling)
6500 (org-get-next-sibling))
6501 (if (org-at-heading-p)
6504 (level (looking-at "\\*+"))
6505 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6508 (narrow-to-region beg end
)
6510 ;; Properly fold already folded siblings
6511 (goto-char (point-min))
6512 (while (re-search-forward re nil t
)
6513 (if (and (not (outline-invisible-p))
6515 (goto-char (point-at-eol)) (outline-invisible-p)))
6517 (org-cycle-show-empty-lines 'overview
)
6518 (org-cycle-hide-drawers 'overview
)))))
6520 (defun org-cycle-show-empty-lines (state)
6521 "Show empty lines above all visible headlines.
6522 The region to be covered depends on STATE when called through
6523 `org-cycle-hook'. Lisp program can use t for STATE to get the
6524 entire buffer covered. Note that an empty line is only shown if there
6525 are at least `org-cycle-separator-lines' empty lines before the headline."
6526 (when (not (= org-cycle-separator-lines
0))
6528 (let* ((n (abs org-cycle-separator-lines
))
6530 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6531 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6532 (t (let ((ns (number-to-string (- n
2))))
6533 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6534 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6537 ((memq state
'(overview contents t
))
6538 (setq beg
(point-min) end
(point-max)))
6539 ((memq state
'(children folded
))
6540 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6541 (beginning-of-line 2)
6545 (while (re-search-forward re end t
)
6546 (unless (get-char-property (match-end 1) 'invisible
)
6547 (setq e
(match-end 1))
6548 (if (< org-cycle-separator-lines
0)
6549 (setq b
(save-excursion
6550 (goto-char (match-beginning 0))
6551 (org-back-over-empty-lines)
6553 (goto-char (max (point-min) (1- (point))))
6557 (setq b
(match-beginning 1)))
6558 (outline-flag-region b e nil
)))))))
6559 ;; Never hide empty lines at the end of the file.
6561 (goto-char (point-max))
6562 (outline-previous-heading)
6563 (outline-end-of-heading)
6564 (if (and (looking-at "[ \t\n]+")
6565 (= (match-end 0) (point-max)))
6566 (outline-flag-region (point) (match-end 0) nil
))))
6568 (defun org-show-empty-lines-in-parent ()
6569 "Move to the parent and re-show empty lines before visible headlines."
6571 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6572 (org-cycle-show-empty-lines context
))))
6574 (defun org-files-list ()
6575 "Return `org-agenda-files' list, plus all open org-mode files.
6576 This is useful for operations that need to scan all of a user's
6577 open and agenda-wise Org files."
6578 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6579 (dolist (buf (buffer-list))
6580 (with-current-buffer buf
6581 (if (and (derived-mode-p 'org-mode
) (buffer-file-name))
6582 (let ((file (expand-file-name (buffer-file-name))))
6583 (unless (member file files
)
6584 (push file files
))))))
6587 (defsubst org-entry-beginning-position
()
6588 "Return the beginning position of the current entry."
6589 (save-excursion (outline-back-to-heading t
) (point)))
6591 (defsubst org-entry-end-position
()
6592 "Return the end position of the current entry."
6593 (save-excursion (outline-next-heading) (point)))
6595 (defun org-cycle-hide-drawers (state)
6596 "Re-hide all drawers after a visibility state change."
6597 (when (and (derived-mode-p 'org-mode
)
6598 (not (memq state
'(overview folded contents
))))
6600 (let* ((globalp (memq state
'(contents all
)))
6601 (beg (if globalp
(point-min) (point)))
6602 (end (if globalp
(point-max)
6603 (if (eq state
'children
)
6604 (save-excursion (outline-next-heading) (point))
6605 (org-end-of-subtree t
)))))
6607 (while (re-search-forward org-drawer-regexp end t
)
6608 (org-flag-drawer t
))))))
6610 (defun org-flag-drawer (flag)
6611 "When FLAG is non-nil, hide the drawer we are within.
6612 Otherwise make it visible."
6614 (beginning-of-line 1)
6615 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6616 (let ((b (match-end 0)))
6617 (if (re-search-forward
6619 (save-excursion (outline-next-heading) (point)) t
)
6620 (outline-flag-region b
(point-at-eol) flag
)
6621 (error ":END: line missing at position %s" b
))))))
6623 (defun org-subtree-end-visible-p ()
6624 "Is the end of the current subtree visible?"
6625 (pos-visible-in-window-p
6626 (save-excursion (org-end-of-subtree t
) (point))))
6628 (defun org-first-headline-recenter (&optional N
)
6629 "Move cursor to the first headline and recenter the headline.
6630 Optional argument N means put the headline into the Nth line of the window."
6631 (goto-char (point-min))
6632 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
6634 (recenter (prefix-numeric-value N
))))
6636 ;;; Saving and restoring visibility
6638 (defun org-outline-overlay-data (&optional use-markers
)
6639 "Return a list of the locations of all outline overlays.
6640 These are overlays with the `invisible' property value `outline'.
6641 The return value is a list of cons cells, with start and stop
6642 positions for each overlay.
6643 If USE-MARKERS is set, return the positions as markers."
6650 (when (eq (overlay-get o
'invisible
) 'outline
)
6651 (setq beg
(overlay-start o
)
6652 end
(overlay-end o
))
6653 (and beg end
(> end beg
)
6655 (cons (move-marker (make-marker) beg
)
6656 (move-marker (make-marker) end
))
6658 (overlays-in (point-min) (point-max))))))))
6660 (defun org-set-outline-overlay-data (data)
6661 "Create visibility overlays for all positions in DATA.
6662 DATA should have been made by `org-outline-overlay-data'."
6669 (outline-flag-region (car c
) (cdr c
) t
))
6672 ;;; Folding of blocks
6674 (defvar org-hide-block-overlays nil
6675 "Overlays hiding blocks.")
6676 (make-variable-buffer-local 'org-hide-block-overlays
)
6678 (defun org-block-map (function &optional start end
)
6679 "Call FUNCTION at the head of all source blocks in the current buffer.
6680 Optional arguments START and END can be used to limit the range."
6681 (let ((start (or start
(point-min)))
6682 (end (or end
(point-max))))
6685 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6688 (goto-char (match-beginning 0))
6689 (funcall function
)))))))
6691 (defun org-hide-block-toggle-all ()
6692 "Toggle the visibility of all blocks in the current buffer."
6693 (org-block-map #'org-hide-block-toggle
))
6695 (defun org-hide-block-all ()
6696 "Fold all blocks in the current buffer."
6698 (org-show-block-all)
6699 (org-block-map #'org-hide-block-toggle-maybe
))
6701 (defun org-show-block-all ()
6702 "Unfold all blocks in the current buffer."
6704 (mapc 'delete-overlay org-hide-block-overlays
)
6705 (setq org-hide-block-overlays nil
))
6707 (defun org-hide-block-toggle-maybe ()
6708 "Toggle visibility of block at point."
6710 (let ((case-fold-search t
))
6712 (beginning-of-line 1)
6713 (looking-at org-block-regexp
))
6714 (progn (org-hide-block-toggle)
6715 t
) ;; to signal that we took action
6716 nil
))) ;; to signal that we did not
6718 (defun org-hide-block-toggle (&optional force
)
6719 "Toggle the visibility of the current block."
6723 (if (re-search-forward org-block-regexp nil t
)
6724 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
6725 (end (match-end 0)) ;; end of entire body
6727 (if (memq t
(mapcar (lambda (overlay)
6728 (eq (overlay-get overlay
'invisible
)
6730 (overlays-at start
)))
6731 (if (or (not force
) (eq force
'off
))
6733 (when (member ov org-hide-block-overlays
)
6734 (setq org-hide-block-overlays
6735 (delq ov org-hide-block-overlays
)))
6736 (when (eq (overlay-get ov
'invisible
)
6738 (delete-overlay ov
)))
6739 (overlays-at start
)))
6740 (setq ov
(make-overlay start end
))
6741 (overlay-put ov
'invisible
'org-hide-block
)
6742 ;; make the block accessible to isearch
6744 ov
'isearch-open-invisible
6746 (when (member ov org-hide-block-overlays
)
6747 (setq org-hide-block-overlays
6748 (delq ov org-hide-block-overlays
)))
6749 (when (eq (overlay-get ov
'invisible
)
6751 (delete-overlay ov
))))
6752 (push ov org-hide-block-overlays
)))
6753 (error "Not looking at a source block"))))
6755 ;; org-tab-after-check-for-cycling-hook
6756 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
6757 ;; Remove overlays when changing major mode
6758 (add-hook 'org-mode-hook
6759 (lambda () (org-add-hook 'change-major-mode-hook
6760 'org-show-block-all
'append
'local
)))
6764 (defvar org-goto-window-configuration nil
)
6765 (defvar org-goto-marker nil
)
6766 (defvar org-goto-map
)
6767 (defun org-goto-map ()
6768 "Set the keymap `org-goto'."
6770 (let ((map (make-sparse-keymap)))
6771 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
6772 mouse-drag-region universal-argument org-occur
))
6774 (while (setq cmd
(pop cmds
))
6775 (substitute-key-definition cmd cmd map global-map
)))
6776 (suppress-keymap map
)
6777 (org-defkey map
"\C-m" 'org-goto-ret
)
6778 (org-defkey map
[(return)] 'org-goto-ret
)
6779 (org-defkey map
[(left)] 'org-goto-left
)
6780 (org-defkey map
[(right)] 'org-goto-right
)
6781 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
6782 (org-defkey map
"\C-i" 'org-cycle
)
6783 (org-defkey map
[(tab)] 'org-cycle
)
6784 (org-defkey map
[(down)] 'outline-next-visible-heading
)
6785 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
6786 (if org-goto-auto-isearch
6787 (if (fboundp 'define-key-after
)
6788 (define-key-after map
[t] 'org-goto-local-auto-isearch)
6790 (org-defkey map "q" 'org-goto-quit)
6791 (org-defkey map "n" 'outline-next-visible-heading)
6792 (org-defkey map "p" 'outline-previous-visible-heading)
6793 (org-defkey map "f" 'outline-forward-same-level)
6794 (org-defkey map "b" 'outline-backward-same-level)
6795 (org-defkey map "u" 'outline-up-heading))
6796 (org-defkey map "/" 'org-occur)
6797 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6798 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6799 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6800 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6801 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6804 (defconst org-goto-help
6805 "Browse buffer copy, to find location or copy text.%s
6806 RET=jump to location C-g=quit and return to previous location
6807 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6809 (defvar org-goto-start-pos) ; dynamically scoped parameter
6811 ;; FIXME: Docstring does not mention both interfaces
6812 (defun org-goto (&optional alternative-interface)
6813 "Look up a different location in the current file, keeping current visibility.
6815 When you want look-up or go to a different location in a
6816 document, the fastest way is often to fold the entire buffer and
6817 then dive into the tree. This method has the disadvantage, that
6818 the previous location will be folded, which may not be what you
6821 This command works around this by showing a copy of the current
6822 buffer in an indirect buffer, in overview mode. You can dive
6823 into the tree in that copy, use org-occur and incremental search
6824 to find a location. When pressing RET or `Q', the command
6825 returns to the original buffer in which the visibility is still
6826 unchanged. After RET it will also jump to the location selected
6827 in the indirect buffer and expose the headline hierarchy above.
6829 With a prefix argument, use the alternative interface: e.g. if
6830 `org-goto-interface' is 'outline use 'outline-path-completion."
6833 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6834 (org-refile-use-outline-path t)
6835 (org-refile-target-verify-function nil)
6837 (if (not alternative-interface)
6839 (if (eq org-goto-interface 'outline)
6840 'outline-path-completion
6842 (org-goto-start-pos (point))
6844 (if (eq interface 'outline)
6845 (car (org-get-location (current-buffer) org-goto-help))
6846 (let ((pa (org-refile-get-location "Goto" nil nil t)))
6847 (org-refile-check-position pa)
6851 (org-mark-ring-push org-goto-start-pos)
6852 (goto-char selected-point)
6853 (if (or (outline-invisible-p) (org-invisible-p2))
6854 (org-show-context 'org-goto)))
6857 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6858 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6859 (defvar org-goto-local-auto-isearch-map) ; defined below
6861 (defun org-get-location (buf help)
6862 "Let the user select a location in the Org-mode buffer BUF.
6863 This function uses a recursive edit. It returns the selected position
6866 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6867 (isearch-hide-immediately nil)
6868 (isearch-search-fun-function
6869 (lambda () 'org-goto-local-search-headings))
6870 (org-goto-selected-point org-goto-exit-command))
6872 (save-window-excursion
6873 (delete-other-windows)
6874 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6875 (org-pop-to-buffer-same-window
6877 (make-indirect-buffer (current-buffer) "*org-goto*")
6878 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6879 (with-output-to-temp-buffer "*Org Help*"
6880 (princ (format help (if org-goto-auto-isearch
6881 " Just type for auto-isearch."
6882 " n/p/f/b/u to navigate, q to quit."))))
6883 (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
6884 (setq buffer-read-only nil)
6885 (let ((org-startup-truncated t)
6886 (org-startup-folded nil)
6887 (org-startup-align-all-tables nil))
6890 (setq buffer-read-only t)
6891 (if (and (boundp 'org-goto-start-pos)
6892 (integer-or-marker-p org-goto-start-pos))
6893 (let ((org-show-hierarchy-above t)
6894 (org-show-siblings t)
6895 (org-show-following-heading t))
6896 (goto-char org-goto-start-pos)
6897 (and (outline-invisible-p) (org-show-context)))
6898 (goto-char (point-min)))
6899 (let (org-special-ctrl-a/e) (org-beginning-of-line))
6900 (message "Select location and press RET")
6901 (use-local-map org-goto-map)
6903 (kill-buffer "*org-goto*")
6904 (cons org-goto-selected-point org-goto-exit-command))))
6906 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6907 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6908 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6909 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6911 (defun org-goto-local-search-headings (string bound noerror)
6912 "Search and make sure that any matches are in headlines."
6914 (while (if isearch-forward
6915 (search-forward string bound noerror)
6916 (search-backward string bound noerror))
6917 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6918 (and (member :headline context)
6919 (not (member :tags context))))
6920 (throw 'return (point))))))
6922 (defun org-goto-local-auto-isearch ()
6925 (goto-char (point-min))
6926 (let ((keys (this-command-keys)))
6927 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6929 (isearch-process-search-char (string-to-char keys)))))
6931 (defun org-goto-ret (&optional arg)
6932 "Finish `org-goto' by going to the new location."
6934 (setq org-goto-selected-point (point)
6935 org-goto-exit-command 'return)
6938 (defun org-goto-left ()
6939 "Finish `org-goto' by going to the new location."
6941 (if (org-at-heading-p)
6943 (beginning-of-line 1)
6944 (setq org-goto-selected-point (point)
6945 org-goto-exit-command 'left)
6947 (error "Not on a heading")))
6949 (defun org-goto-right ()
6950 "Finish `org-goto' by going to the new location."
6952 (if (org-at-heading-p)
6954 (setq org-goto-selected-point (point)
6955 org-goto-exit-command 'right)
6957 (error "Not on a heading")))
6959 (defun org-goto-quit ()
6960 "Finish `org-goto' without cursor motion."
6962 (setq org-goto-selected-point nil)
6963 (setq org-goto-exit-command 'quit)
6966 ;;; Indirect buffer display of subtrees
6968 (defvar org-indirect-dedicated-frame nil
6969 "This is the frame being used for indirect tree display.")
6970 (defvar org-last-indirect-buffer nil)
6972 (defun org-tree-to-indirect-buffer (&optional arg)
6973 "Create indirect buffer and narrow it to current subtree.
6974 With a numerical prefix ARG, go up to this level and then take that tree.
6975 If ARG is negative, go up that many levels.
6977 If `org-indirect-buffer-display' is not `new-frame', the command removes the
6978 indirect buffer previously made with this command, to avoid proliferation of
6979 indirect buffers. However, when you call the command with a \
6980 \\[universal-argument] prefix, or
6981 when `org-indirect-buffer-display' is `new-frame', the last buffer
6982 is kept so that you can work with several indirect buffers at the same time.
6983 If `org-indirect-buffer-display' is `dedicated-frame', the \
6984 \\[universal-argument] prefix also
6985 requests that a new frame be made for the new buffer, so that the dedicated
6986 frame is not changed."
6988 (let ((cbuf (current-buffer))
6989 (cwin (selected-window))
6991 beg end level heading ibuf)
6993 (org-back-to-heading t)
6995 (setq level (org-outline-level))
6996 (if (< arg 0) (setq arg (+ level arg)))
6997 (while (> (setq level (org-outline-level)) arg)
6998 (org-up-heading-safe)))
7000 heading (org-get-heading))
7001 (org-end-of-subtree t t)
7002 (if (org-at-heading-p) (backward-char 1))
7004 (if (and (buffer-live-p org-last-indirect-buffer)
7005 (not (eq org-indirect-buffer-display 'new-frame))
7007 (kill-buffer org-last-indirect-buffer))
7008 (setq ibuf (org-get-indirect-buffer cbuf)
7009 org-last-indirect-buffer ibuf)
7011 ((or (eq org-indirect-buffer-display 'new-frame)
7012 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7013 (select-frame (make-frame))
7014 (delete-other-windows)
7015 (org-pop-to-buffer-same-window ibuf)
7016 (org-set-frame-title heading))
7017 ((eq org-indirect-buffer-display 'dedicated-frame)
7019 (select-frame (or (and org-indirect-dedicated-frame
7020 (frame-live-p org-indirect-dedicated-frame)
7021 org-indirect-dedicated-frame)
7022 (setq org-indirect-dedicated-frame (make-frame)))))
7023 (delete-other-windows)
7024 (org-pop-to-buffer-same-window ibuf)
7025 (org-set-frame-title (concat "Indirect: " heading)))
7026 ((eq org-indirect-buffer-display 'current-window)
7027 (org-pop-to-buffer-same-window ibuf))
7028 ((eq org-indirect-buffer-display 'other-window)
7029 (pop-to-buffer ibuf))
7030 (t (error "Invalid value")))
7031 (if (featurep 'xemacs)
7032 (save-excursion (org-mode) (turn-on-font-lock)))
7033 (narrow-to-region beg end)
7036 (run-hook-with-args 'org-cycle-hook 'all)
7037 (and (window-live-p cwin) (select-window cwin))))
7039 (defun org-get-indirect-buffer (&optional buffer)
7040 (setq buffer (or buffer (current-buffer)))
7041 (let ((n 1) (base (buffer-name buffer)) bname)
7042 (while (buffer-live-p
7043 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7046 (make-indirect-buffer buffer bname 'clone)
7047 (error (make-indirect-buffer buffer bname)))))
7049 (defun org-set-frame-title (title)
7050 "Set the title of the current frame to the string TITLE."
7051 ;; FIXME: how to name a single frame in XEmacs???
7052 (unless (featurep 'xemacs)
7053 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7055 ;;;; Structure editing
7057 ;;; Inserting headlines
7059 (defun org-previous-line-empty-p ()
7062 (or (beginning-of-line 0) t)
7064 (looking-at "[ \t]*$")))))
7066 (defun org-insert-heading (&optional force-heading invisible-ok)
7067 "Insert a new heading or item with same depth at point.
7068 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
7069 If point is at the beginning of a headline, insert a sibling before the
7070 current headline. If point is not at the beginning, split the line,
7071 create the new headline with the text in the current line after point
7072 \(but see also the variable `org-M-RET-may-split-line').
7074 When INVISIBLE-OK is set, stop at invisible headlines when going back.
7075 This is important for non-interactive uses of the command."
7077 (if (or (= (buffer-size) 0)
7078 (and (not (save-excursion
7079 (and (ignore-errors (org-back-to-heading invisible-ok))
7080 (org-at-heading-p))))
7081 (or force-heading (not (org-in-item-p)))))
7084 (run-hooks 'org-insert-heading-hook))
7085 (when (or force-heading (not (org-insert-item)))
7086 (let* ((empty-line-p nil)
7088 (on-heading (org-at-heading-p))
7089 (head (save-excursion
7092 (org-back-to-heading invisible-ok)
7093 (when (and (not on-heading)
7094 (featurep 'org-inlinetask)
7095 (integerp org-inlinetask-min-level)
7096 (>= (length (match-string 0))
7097 org-inlinetask-min-level))
7098 ;; Find a heading level before the inline task
7099 (while (and (setq level (org-up-heading-safe))
7100 (>= level org-inlinetask-min-level)))
7101 (if (org-at-heading-p)
7102 (org-back-to-heading invisible-ok)
7103 (error "This should not happen")))
7104 (setq empty-line-p (org-previous-line-empty-p))
7107 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7108 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7109 pos hide-previous previous-pos)
7111 ((and (org-at-heading-p) (bolp)
7113 (save-excursion (backward-char 1) (not (outline-invisible-p)))))
7114 ;; insert before the current line
7115 (open-line (if blank 2 1)))
7117 (not org-insert-heading-respect-content)
7120 (backward-char 1) (not (outline-invisible-p)))))
7121 ;; insert right here
7124 ;; somewhere in the line
7126 (setq previous-pos (point-at-bol))
7128 (setq hide-previous (outline-invisible-p)))
7129 (and org-insert-heading-respect-content (org-show-subtree))
7131 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
7134 (goto-char (point-at-bol))
7135 (and (looking-at org-complex-heading-regexp)
7137 (> p (match-beginning 4)))))))
7140 (org-insert-heading-respect-content
7141 (org-end-of-subtree nil t)
7142 (when (featurep 'org-inlinetask)
7143 (while (and (not (eobp))
7144 (looking-at "\\(\\*+\\)[ \t]+")
7145 (>= (length (match-string 1))
7146 org-inlinetask-min-level))
7147 (org-end-of-subtree nil t)))
7148 (or (bolp) (newline))
7149 (or (org-previous-line-empty-p)
7150 (and blank (newline)))
7156 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
7157 (setq tags (and (match-end 2) (match-string 2)))
7159 (delete-region (match-beginning 1) (match-end 1)))
7160 (setq pos (point-at-bol))
7161 (or split (end-of-line 1))
7162 (delete-horizontal-space)
7163 (if (string-match "\\`\\*+\\'"
7164 (buffer-substring (point-at-bol) (point)))
7166 (newline (if blank 2 1))
7172 (org-set-tags nil 'align))))
7174 (or split (end-of-line 1))
7175 (newline (if blank 2 1)))))))
7176 (insert head) (just-one-space)
7179 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
7180 (when (and org-insert-heading-respect-content hide-previous)
7182 (goto-char previous-pos)
7184 (run-hooks 'org-insert-heading-hook)))))
7186 (defun org-get-heading (&optional no-tags no-todo)
7187 "Return the heading of the current entry, without the stars.
7188 When NO-TAGS is non-nil, don't include tags.
7189 When NO-TODO is non-nil, don't include TODO keywords."
7191 (org-back-to-heading t)
7193 ((and no-tags no-todo)
7194 (looking-at org-complex-heading-regexp)
7197 (looking-at (concat org-outline-regexp
7199 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7202 (looking-at org-todo-line-regexp)
7204 (t (looking-at org-heading-regexp)
7205 (match-string 2)))))
7207 (defun org-heading-components ()
7208 "Return the components of the current heading.
7209 This is a list with the following elements:
7210 - the level as an integer
7211 - the reduced level, different if `org-odd-levels-only' is set.
7212 - the TODO keyword, or nil
7213 - the priority character, like ?A, or nil if no priority is given
7214 - the headline text itself, or the tags string if no headline text
7215 - the tags string, or nil."
7217 (org-back-to-heading t)
7218 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
7219 (list (length (match-string 1))
7220 (org-reduced-level (length (match-string 1)))
7221 (org-match-string-no-properties 2)
7222 (and (match-end 3) (aref (match-string 3) 2))
7223 (org-match-string-no-properties 4)
7224 (org-match-string-no-properties 5)))))
7226 (defun org-get-entry ()
7227 "Get the entry text, after heading, entire subtree."
7229 (org-back-to-heading t)
7230 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7232 (defun org-insert-heading-after-current ()
7233 "Insert a new heading with same level as current, after current subtree."
7235 (org-back-to-heading)
7236 (org-insert-heading)
7237 (org-move-subtree-down)
7240 (defun org-insert-heading-respect-content (invisible-ok)
7241 "Insert heading with `org-insert-heading-respect-content' set to t."
7243 (let ((org-insert-heading-respect-content t))
7244 (org-insert-heading t invisible-ok)))
7246 (defun org-insert-todo-heading-respect-content (&optional force-state)
7247 "Insert TODO heading with `org-insert-heading-respect-content' set to t."
7249 (let ((org-insert-heading-respect-content t))
7250 (org-insert-todo-heading force-state t)))
7252 (defun org-insert-todo-heading (arg &optional force-heading)
7253 "Insert a new heading with the same level and TODO state as current heading.
7254 If the heading has no TODO state, or if the state is DONE, use the first
7255 state (TODO by default). Also with prefix arg, force first state."
7257 (when (or force-heading (not (org-insert-item 'checkbox)))
7258 (org-insert-heading force-heading)
7260 (org-back-to-heading)
7261 (outline-previous-heading)
7262 (looking-at org-todo-line-regexp))
7266 (not (match-beginning 2))
7267 (member (match-string 2) org-done-keywords))
7268 (car org-todo-keywords-1)
7272 (run-hook-with-args-until-success
7273 'org-todo-get-default-hook new-mark-x nil)
7275 (beginning-of-line 1)
7276 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7277 (if org-treat-insert-todo-heading-as-state-change
7279 (insert new-mark " "))))
7280 (when org-provide-todo-statistics
7281 (org-update-parent-todo-statistics))))
7283 (defun org-insert-subheading (arg)
7284 "Insert a new subheading and demote it.
7285 Works for outline headings and for plain lists alike."
7287 (org-insert-heading arg)
7289 ((org-at-heading-p) (org-do-demote))
7290 ((org-at-item-p) (org-indent-item))))
7292 (defun org-insert-todo-subheading (arg)
7293 "Insert a new subheading with TODO keyword or checkbox and demote it.
7294 Works for outline headings and for plain lists alike."
7296 (org-insert-todo-heading arg)
7298 ((org-at-heading-p) (org-do-demote))
7299 ((org-at-item-p) (org-indent-item))))
7301 ;;; Promotion and Demotion
7303 (defvar org-after-demote-entry-hook nil
7304 "Hook run after an entry has been demoted.
7305 The cursor will be at the beginning of the entry.
7306 When a subtree is being demoted, the hook will be called for each node.")
7308 (defvar org-after-promote-entry-hook nil
7309 "Hook run after an entry has been promoted.
7310 The cursor will be at the beginning of the entry.
7311 When a subtree is being promoted, the hook will be called for each node.")
7313 (defun org-promote-subtree ()
7314 "Promote the entire subtree.
7315 See also `org-promote'."
7318 (org-with-limited-levels (org-map-tree 'org-promote)))
7319 (org-fix-position-after-promote))
7321 (defun org-demote-subtree ()
7322 "Demote the entire subtree. See `org-demote'.
7323 See also `org-promote'."
7326 (org-with-limited-levels (org-map-tree 'org-demote)))
7327 (org-fix-position-after-promote))
7330 (defun org-do-promote ()
7331 "Promote the current heading higher up the tree.
7332 If the region is active in `transient-mark-mode', promote all headings
7336 (if (org-region-active-p)
7337 (org-map-region 'org-promote (region-beginning) (region-end))
7339 (org-fix-position-after-promote))
7341 (defun org-do-demote ()
7342 "Demote the current heading lower down the tree.
7343 If the region is active in `transient-mark-mode', demote all headings
7347 (if (org-region-active-p)
7348 (org-map-region 'org-demote (region-beginning) (region-end))
7350 (org-fix-position-after-promote))
7352 (defun org-fix-position-after-promote ()
7353 "Make sure that after pro/demotion cursor position is right."
7354 (let ((pos (point)))
7355 (when (save-excursion
7356 (beginning-of-line 1)
7357 (looking-at org-todo-line-regexp)
7358 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7359 (cond ((eobp) (insert " "))
7360 ((eolp) (insert " "))
7361 ((equal (char-after) ?\ ) (forward-char 1))))))
7363 (defun org-current-level ()
7364 "Return the level of the current entry, or nil if before the first headline.
7365 The level is the number of stars at the beginning of the headline."
7367 (org-with-limited-levels
7368 (if (ignore-errors (org-back-to-heading t))
7369 (funcall outline-level)))))
7371 (defun org-get-previous-line-level ()
7372 "Return the outline depth of the last headline before the current line.
7373 Returns 0 for the first headline in the buffer, and nil if before the
7375 (let ((current-level (org-current-level))
7376 (prev-level (when (> (line-number-at-pos) 1)
7378 (beginning-of-line 0)
7379 (org-current-level)))))
7380 (cond ((null current-level) nil) ; Before first headline
7381 ((null prev-level) 0) ; At first headline
7384 (defun org-reduced-level (l)
7385 "Compute the effective level of a heading.
7386 This takes into account the setting of `org-odd-levels-only'."
7389 (org-odd-levels-only (1+ (floor (/ l 2))))
7392 (defun org-level-increment ()
7393 "Return the number of stars that will be added or removed at a
7394 time to headlines when structure editing, based on the value of
7395 `org-odd-levels-only'."
7396 (if org-odd-levels-only 2 1))
7398 (defun org-get-valid-level (level &optional change)
7399 "Rectify a level change under the influence of `org-odd-levels-only'
7400 LEVEL is a current level, CHANGE is by how much the level should be
7401 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7402 even level numbers will become the next higher odd number."
7403 (if org-odd-levels-only
7404 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7405 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7406 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7407 (max 1 (+ level (or change 0)))))
7409 (if (boundp 'define-obsolete-function-alias)
7410 (if (or (featurep 'xemacs) (< emacs-major-version 23))
7411 (define-obsolete-function-alias 'org-get-legal-level
7412 'org-get-valid-level)
7413 (define-obsolete-function-alias 'org-get-legal-level
7414 'org-get-valid-level "23.1")))
7416 (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
7417 ;; ̀org-with-limited-levels'
7418 (defun org-promote ()
7419 "Promote the current heading higher up the tree.
7420 If the region is active in `transient-mark-mode', promote all headings
7422 (org-back-to-heading t)
7423 (let* ((level (save-match-data (funcall outline-level)))
7424 (after-change-functions (remove 'flyspell-after-change-function
7425 after-change-functions))
7426 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7427 (diff (abs (- level (length up-head) -1))))
7428 (cond ((and (= level 1) org-called-with-limited-levels
7429 org-allow-promoting-top-level-subtree)
7430 (replace-match "# " nil t))
7432 (error "Cannot promote to level 0. UNDO to recover if necessary"))
7433 (t (replace-match up-head nil t)))
7434 ;; Fixup tag positioning
7436 (and org-auto-align-tags (org-set-tags nil t))
7437 (if org-adapt-indentation (org-fixup-indentation (- diff))))
7438 (run-hooks 'org-after-promote-entry-hook)))
7440 (defun org-demote ()
7441 "Demote the current heading lower down the tree.
7442 If the region is active in `transient-mark-mode', demote all headings
7444 (org-back-to-heading t)
7445 (let* ((level (save-match-data (funcall outline-level)))
7446 (after-change-functions (remove 'flyspell-after-change-function
7447 after-change-functions))
7448 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7449 (diff (abs (- level (length down-head) -1))))
7450 (replace-match down-head nil t)
7451 ;; Fixup tag positioning
7452 (and org-auto-align-tags (org-set-tags nil t))
7453 (if org-adapt-indentation (org-fixup-indentation diff))
7454 (run-hooks 'org-after-demote-entry-hook)))
7456 (defun org-cycle-level ()
7457 "Cycle the level of an empty headline through possible states.
7458 This goes first to child, then to parent, level, then up the hierarchy.
7459 After top level, it switches back to sibling level."
7461 (let ((org-adapt-indentation nil))
7462 (when (org-point-at-end-of-empty-headline)
7463 (setq this-command 'org-cycle-level) ; Only needed for caching
7464 (let ((cur-level (org-current-level))
7465 (prev-level (org-get-previous-line-level)))
7467 ;; If first headline in file, promote to top-level.
7469 (loop repeat (/ (- cur-level 1) (org-level-increment))
7470 do (org-do-promote)))
7471 ;; If same level as prev, demote one.
7472 ((= prev-level cur-level)
7474 ;; If parent is top-level, promote to top level if not already.
7476 (loop repeat (/ (- cur-level 1) (org-level-increment))
7477 do (org-do-promote)))
7478 ;; If top-level, return to prev-level.
7480 (loop repeat (/ (- prev-level 1) (org-level-increment))
7481 do (org-do-demote)))
7482 ;; If less than prev-level, promote one.
7483 ((< cur-level prev-level)
7485 ;; If deeper than prev-level, promote until higher than
7487 ((> cur-level prev-level)
7488 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7489 do (org-do-promote))))
7492 (defun org-map-tree (fun)
7493 "Call FUN for every heading underneath the current one."
7494 (org-back-to-heading)
7495 (let ((level (funcall outline-level)))
7499 (outline-next-heading)
7500 (> (funcall outline-level) level))
7504 (defun org-map-region (fun beg end)
7505 "Call FUN for every heading between BEG and END."
7506 (let ((org-ignore-region t))
7508 (setq end (copy-marker end))
7510 (if (and (re-search-forward org-outline-regexp-bol nil t)
7514 (outline-next-heading)
7519 (defvar org-property-end-re) ; silence byte-compiler
7520 (defun org-fixup-indentation (diff)
7521 "Change the indentation in the current entry by DIFF.
7522 However, if any line in the current entry has no indentation, or if it
7523 would end up with no indentation after the change, nothing at all is done."
7525 (let ((end (save-excursion (outline-next-heading)
7527 (prohibit (if (> diff 0)
7529 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7531 (unless (save-excursion (end-of-line 1)
7532 (re-search-forward prohibit end t))
7533 (while (and (< (point) end)
7534 (re-search-forward "^[ \t]+" end t))
7535 (goto-char (match-end 0))
7536 (setq col (current-column))
7537 (if (< diff 0) (replace-match ""))
7538 (org-indent-to-column (+ diff col))))
7539 (move-marker end nil))))
7541 (defun org-convert-to-odd-levels ()
7542 "Convert an org-mode file with all levels allowed to one with odd levels.
7543 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7546 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7547 (let ((outline-level 'org-outline-level)
7548 (org-odd-levels-only nil) n)
7550 (goto-char (point-min))
7551 (while (re-search-forward "^\\*\\*+ " nil t)
7552 (setq n (- (length (match-string 0)) 2))
7553 (while (>= (setq n (1- n)) 0)
7555 (end-of-line 1))))))
7557 (defun org-convert-to-oddeven-levels ()
7558 "Convert an org-mode file with only odd levels to one with odd/even levels.
7559 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7560 file contains a section with an even level, conversion would
7561 destroy the structure of the file. An error is signaled in this
7564 (goto-char (point-min))
7565 ;; First check if there are no even levels
7566 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7567 (org-show-context t)
7568 (error "Not all levels are odd in this file. Conversion not possible"))
7569 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7570 (let ((outline-regexp org-outline-regexp)
7571 (outline-level 'org-outline-level)
7572 (org-odd-levels-only nil) n)
7574 (goto-char (point-min))
7575 (while (re-search-forward "^\\*\\*+ " nil t)
7576 (setq n (/ (1- (length (match-string 0))) 2))
7577 (while (>= (setq n (1- n)) 0)
7579 (end-of-line 1))))))
7581 (defun org-tr-level (n)
7582 "Make N odd if required."
7583 (if org-odd-levels-only (1+ (/ n 2)) n))
7585 ;;; Vertical tree motion, cutting and pasting of subtrees
7587 (defun org-move-subtree-up (&optional arg)
7588 "Move the current subtree up past ARG headlines of the same level."
7590 (org-move-subtree-down (- (prefix-numeric-value arg))))
7592 (defun org-move-subtree-down (&optional arg)
7593 "Move the current subtree down past ARG headlines of the same level."
7595 (setq arg (prefix-numeric-value arg))
7596 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7597 'org-get-last-sibling))
7598 (ins-point (make-marker))
7600 (col (current-column))
7601 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7603 (org-back-to-heading)
7606 (setq ne-beg (org-back-over-empty-lines))
7609 (save-excursion (outline-end-of-heading)
7610 (setq folded (outline-invisible-p)))
7611 (outline-end-of-subtree))
7612 (outline-next-heading)
7613 (setq ne-end (org-back-over-empty-lines))
7616 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7617 ;; include less whitespace
7620 (forward-line (- ne-beg ne-end))
7621 (setq beg (point))))
7622 ;; Find insertion point, with error handling
7624 (or (and (funcall movfunc) (looking-at org-outline-regexp))
7625 (progn (goto-char beg0)
7626 (error "Cannot move past superior level or buffer limit")))
7627 (setq cnt (1- cnt)))
7629 ;; Moving forward - still need to move over subtree
7630 (progn (org-end-of-subtree t t)
7632 (org-back-over-empty-lines)
7633 (or (bolp) (newline)))))
7634 (setq ne-ins (org-back-over-empty-lines))
7635 (move-marker ins-point (point))
7636 (setq txt (buffer-substring beg end))
7637 (org-save-markers-in-region beg end)
7638 (delete-region beg end)
7639 (org-remove-empty-overlays-at beg)
7640 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7641 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7642 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7643 (let ((bbb (point)))
7644 (insert-before-markers txt)
7645 (org-reinstall-markers-in-region bbb)
7646 (move-marker ins-point bbb))
7647 (or (bolp) (insert "\n"))
7648 (setq ins-end (point))
7649 (goto-char ins-point)
7650 (org-skip-whitespace)
7651 (when (and (< arg 0)
7652 (org-first-sibling-p)
7654 ;; Move whitespace back to beginning
7657 (let ((kill-whole-line t))
7658 (kill-line (- ne-ins ne-beg)) (point)))
7659 (insert (make-string (- ne-ins ne-beg) ?\n)))
7660 (move-marker ins-point nil)
7665 (org-cycle-hide-drawers 'children))
7666 (org-clean-visibility-after-subtree-move)
7667 ;; move back to the initial column we were at
7668 (move-to-column col)))
7670 (defvar org-subtree-clip ""
7671 "Clipboard for cut and paste of subtrees.
7672 This is actually only a copy of the kill, because we use the normal kill
7673 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7675 (defvar org-subtree-clip-folded nil
7676 "Was the last copied subtree folded?
7677 This is used to fold the tree back after pasting.")
7679 (defun org-cut-subtree (&optional n)
7680 "Cut the current subtree into the clipboard.
7681 With prefix arg N, cut this many sequential subtrees.
7682 This is a short-hand for marking the subtree and then cutting it."
7684 (org-copy-subtree n 'cut))
7686 (defun org-copy-subtree (&optional n cut force-store-markers)
7687 "Cut the current subtree into the clipboard.
7688 With prefix arg N, cut this many sequential subtrees.
7689 This is a short-hand for marking the subtree and then copying it.
7690 If CUT is non-nil, actually cut the subtree.
7691 If FORCE-STORE-MARKERS is non-nil, store the relative locations
7692 of some markers in the region, even if CUT is non-nil. This is
7693 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
7695 (let (beg end folded (beg0 (point)))
7696 (if (org-called-interactively-p 'any)
7697 (org-back-to-heading nil) ; take what looks like a subtree
7698 (org-back-to-heading t)) ; take what is really there
7700 (skip-chars-forward " \t\r\n")
7702 (save-excursion (outline-end-of-heading)
7703 (setq folded (outline-invisible-p)))
7705 (org-forward-heading-same-level (1- n) t)
7707 (org-end-of-subtree t t))
7711 (setq org-subtree-clip-folded folded)
7712 (when (or cut force-store-markers)
7713 (org-save-markers-in-region beg end))
7714 (if cut (kill-region beg end) (copy-region-as-kill beg end))
7715 (setq org-subtree-clip (current-kill 0))
7716 (message "%s: Subtree(s) with %d characters"
7717 (if cut "Cut" "Copied")
7718 (length org-subtree-clip)))))
7720 (defun org-paste-subtree (&optional level tree for-yank)
7721 "Paste the clipboard as a subtree, with modification of headline level.
7722 The entire subtree is promoted or demoted in order to match a new headline
7725 If the cursor is at the beginning of a headline, the same level as
7726 that headline is used to paste the tree
7728 If not, the new level is derived from the *visible* headings
7729 before and after the insertion point, and taken to be the inferior headline
7730 level of the two. So if the previous visible heading is level 3 and the
7731 next is level 4 (or vice versa), level 4 will be used for insertion.
7732 This makes sure that the subtree remains an independent subtree and does
7733 not swallow low level entries.
7735 You can also force a different level, either by using a numeric prefix
7736 argument, or by inserting the heading marker by hand. For example, if the
7737 cursor is after \"*****\", then the tree will be shifted to level 5.
7739 If optional TREE is given, use this text instead of the kill ring.
7741 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
7742 move back over whitespace before inserting, and move point to the end of
7743 the inserted text when done."
7745 (setq tree (or tree (and kill-ring (current-kill 0))))
7746 (unless (org-kill-is-subtree-p tree)
7748 (substitute-command-keys
7749 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
7750 (org-with-limited-levels
7751 (let* ((visp (not (outline-invisible-p)))
7753 (^re_ "\\(\\*+\\)[ \t]*")
7754 (old-level (if (string-match org-outline-regexp-bol txt)
7755 (- (match-end 0) (match-beginning 0) 1)
7757 (force-level (cond (level (prefix-numeric-value level))
7758 ((and (looking-at "[ \t]*$")
7760 "^\\*+$" (buffer-substring
7761 (point-at-bol) (point))))
7762 (- (match-end 1) (match-beginning 1)))
7764 (looking-at org-outline-regexp))
7765 (- (match-end 0) (point) 1))))
7766 (previous-level (save-excursion
7769 (outline-previous-visible-heading 1)
7770 (if (looking-at ^re_)
7771 (- (match-end 0) (match-beginning 0) 1)
7774 (next-level (save-excursion
7777 (or (looking-at org-outline-regexp)
7778 (outline-next-visible-heading 1))
7779 (if (looking-at ^re_)
7780 (- (match-end 0) (match-beginning 0) 1)
7783 (new-level (or force-level (max previous-level next-level)))
7784 (shift (if (or (= old-level -1)
7786 (= old-level new-level))
7788 (- new-level old-level)))
7789 (delta (if (> shift 0) -1 1))
7790 (func (if (> shift 0) 'org-demote 'org-promote))
7791 (org-odd-levels-only nil)
7793 ;; Remove the forced level indicator
7795 (delete-region (point-at-bol) (point)))
7797 (beginning-of-line (if (bolp) 1 2))
7799 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
7800 (insert-before-markers txt)
7801 (unless (string-match "\n\\'" txt) (insert "\n"))
7802 (setq newend (point))
7803 (org-reinstall-markers-in-region beg)
7806 (skip-chars-forward " \t\n\r")
7808 (if (and (outline-invisible-p) visp)
7809 (save-excursion (outline-show-heading)))
7810 ;; Shift if necessary
7813 (narrow-to-region beg end)
7814 (while (not (= shift 0))
7815 (org-map-region func (point-min) (point-max))
7816 (setq shift (+ delta shift)))
7817 (goto-char (point-min))
7818 (setq newend (point-max))))
7819 (when (or (org-called-interactively-p 'interactive) for-yank)
7820 (message "Clipboard pasted as level %d subtree" new-level))
7821 (if (and (not for-yank) ; in this case, org-yank will decide about folding
7823 (eq org-subtree-clip (current-kill 0))
7824 org-subtree-clip-folded)
7825 ;; The tree was folded before it was killed/copied
7827 (and for-yank (goto-char newend)))))
7829 (defun org-kill-is-subtree-p (&optional txt)
7830 "Check if the current kill is an outline subtree, or a set of trees.
7831 Returns nil if kill does not start with a headline, or if the first
7832 headline level is not the largest headline level in the tree.
7833 So this will actually accept several entries of equal levels as well,
7834 which is OK for `org-paste-subtree'.
7835 If optional TXT is given, check this string instead of the current kill."
7836 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
7837 (re (org-get-limited-outline-regexp))
7838 (^re (concat "^" re))
7839 (start-level (and kill
7841 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
7843 (- (match-end 2) (match-beginning 2) 1)))
7844 (start (1+ (or (match-beginning 2) -1))))
7845 (if (not start-level)
7847 nil) ;; does not even start with a heading
7849 (while (setq start (string-match ^re kill (1+ start)))
7850 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
7854 (defvar org-markers-to-move nil
7855 "Markers that should be moved with a cut-and-paste operation.
7856 Those markers are stored together with their positions relative to
7857 the start of the region.")
7859 (defun org-save-markers-in-region (beg end)
7860 "Check markers in region.
7861 If these markers are between BEG and END, record their position relative
7862 to BEG, so that after moving the block of text, we can put the markers back
7864 This function gets called just before an entry or tree gets cut from the
7865 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
7866 called immediately, to move the markers with the entries."
7867 (setq org-markers-to-move nil)
7868 (when (featurep 'org-clock)
7869 (org-clock-save-markers-for-cut-and-paste beg end))
7870 (when (featurep 'org-agenda)
7871 (org-agenda-save-markers-for-cut-and-paste beg end)))
7873 (defun org-check-and-save-marker (marker beg end)
7874 "Check if MARKER is between BEG and END.
7875 If yes, remember the marker and the distance to BEG."
7876 (when (and (marker-buffer marker)
7877 (equal (marker-buffer marker) (current-buffer)))
7878 (if (and (>= marker beg) (< marker end))
7879 (push (cons marker (- marker beg)) org-markers-to-move))))
7881 (defun org-reinstall-markers-in-region (beg)
7882 "Move all remembered markers to their position relative to BEG."
7884 (move-marker (car x) (+ beg (cdr x))))
7885 org-markers-to-move)
7886 (setq org-markers-to-move nil))
7888 (defun org-narrow-to-subtree ()
7889 "Narrow buffer to the current subtree."
7893 (org-with-limited-levels
7895 (progn (org-back-to-heading t) (point))
7896 (progn (org-end-of-subtree t t)
7897 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
7900 (defun org-narrow-to-block ()
7901 "Narrow buffer to the current block."
7903 (let* ((case-fold-search t)
7904 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
7905 "^[ \t]*#\\+end_.*")))
7907 (narrow-to-region (car blockp) (cdr blockp))
7908 (error "Not in a block"))))
7911 (defvar org-property-drawer-re))
7913 (defvar org-property-start-re) ;; defined below
7914 (defun org-clone-subtree-with-time-shift (n &optional shift)
7915 "Clone the task (subtree) at point N times.
7916 The clones will be inserted as siblings.
7918 In interactive use, the user will be prompted for the number of
7919 clones to be produced, and for a time SHIFT, which may be a
7920 repeater as used in time stamps, for example `+3d'.
7922 When a valid repeater is given and the entry contains any time
7923 stamps, the clones will become a sequence in time, with time
7924 stamps in the subtree shifted for each clone produced. If SHIFT
7925 is nil or the empty string, time stamps will be left alone. The
7926 ID property of the original subtree is removed.
7928 If the original subtree did contain time stamps with a repeater,
7929 the following will happen:
7930 - the repeater will be removed in each clone
7931 - an additional clone will be produced, with the current, unshifted
7932 date(s) in the entry.
7933 - the original entry will be placed *after* all the clones, with
7935 - the start days in the repeater in the original entry will be shifted
7936 to past the last clone.
7937 In this way you can spell out a number of instances of a repeating task,
7938 and still retain the repeater to cover future instances of the task."
7939 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
7940 (let (beg end template task idprop
7941 shift-n shift-what doshift nmin nmax (n-no-remove -1)
7942 (drawer-re org-drawer-regexp))
7943 (if (not (and (integerp n) (> n 0)))
7944 (error "Invalid number of replications %s" n))
7945 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
7946 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
7948 (error "Invalid shift specification %s" shift))
7950 (setq shift-n (string-to-number (match-string 1 shift))
7951 shift-what (cdr (assoc (match-string 2 shift)
7952 '(("d" . day) ("w" . week)
7953 ("m" . month) ("y" . year))))))
7954 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
7955 (setq nmin 1 nmax n)
7956 (org-back-to-heading t)
7958 (setq idprop (org-entry-get nil "ID"))
7959 (org-end-of-subtree t t)
7960 (or (bolp) (insert "\n"))
7962 (setq template (buffer-substring beg end))
7964 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
7965 (delete-region beg end)
7967 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
7969 (loop for n from nmin to nmax do
7974 (goto-char (point-min))
7976 (and idprop (if org-clone-delete-id
7977 (org-entry-delete nil "ID")
7978 (org-id-get-create t)))
7980 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
7982 (goto-char (point-min))
7983 (while (re-search-forward drawer-re nil t)
7985 (org-remove-empty-drawer-at d (point))) org-drawers)))
7986 (goto-char (point-min))
7988 (while (re-search-forward org-ts-regexp-both nil t)
7989 (org-timestamp-change (* n shift-n) shift-what))
7990 (unless (= n n-no-remove)
7991 (goto-char (point-min))
7992 (while (re-search-forward org-ts-regexp nil t)
7994 (goto-char (match-beginning 0))
7995 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
7996 (delete-region (match-beginning 1) (match-end 1)))))))
7997 (setq task (buffer-string)))
8003 (defun org-sort (with-case)
8004 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
8005 Optional argument WITH-CASE means sort case-sensitively."
8008 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
8009 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
8011 (org-call-with-arg 'org-sort-entries with-case))))
8013 (defun org-sort-remove-invisible (s)
8014 (remove-text-properties 0 (length s) org-rm-props s)
8015 (while (string-match org-bracket-link-regexp s)
8016 (setq s (replace-match (if (match-end 2)
8018 (match-string 1 s)) t t s)))
8021 (defvar org-priority-regexp) ; defined later in the file
8023 (defvar org-after-sorting-entries-or-items-hook nil
8024 "Hook that is run after a bunch of entries or items have been sorted.
8025 When children are sorted, the cursor is in the parent line when this
8026 hook gets called. When a region or a plain list is sorted, the cursor
8027 will be in the first entry of the sorted region/list.")
8029 (defun org-sort-entries
8030 (&optional with-case sorting-type getkey-func compare-func property)
8031 "Sort entries on a certain level of an outline tree.
8032 If there is an active region, the entries in the region are sorted.
8033 Else, if the cursor is before the first entry, sort the top-level items.
8034 Else, the children of the entry at point are sorted.
8036 Sorting can be alphabetically, numerically, by date/time as given by
8037 a time stamp, by a property or by priority.
8039 The command prompts for the sorting type unless it has been given to the
8040 function through the SORTING-TYPE argument, which needs to be a character,
8041 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
8042 precise meaning of each character:
8044 n Numerically, by converting the beginning of the entry/item to a number.
8045 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8046 o By order of TODO keywords.
8047 t By date/time, either the first active time stamp in the entry, or, if
8048 none exist, by the first inactive one.
8049 s By the scheduled date/time.
8050 d By deadline date/time.
8051 c By creation time, which is assumed to be the first inactive time stamp
8052 at the beginning of a line.
8053 p By priority according to the cookie.
8054 r By the value of a property.
8056 Capital letters will reverse the sort order.
8058 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8059 called with point at the beginning of the record. It must return either
8060 a string or a number that should serve as the sorting key for that record.
8062 Comparing entries ignores case by default. However, with an optional argument
8063 WITH-CASE, the sorting considers case as well."
8065 (let ((case-func (if with-case 'identity 'downcase))
8067 ;; The clock marker is lost when using `sort-subr', let's
8068 ;; store the clocking string.
8069 (when (equal (marker-buffer org-clock-marker) (current-buffer))
8071 (goto-char org-clock-marker)
8072 (looking-back "^.*") (match-string-no-properties 0))))
8073 start beg end stars re re2
8075 ;; Find beginning and end of region to sort
8077 ((org-region-active-p)
8078 ;; we will sort the region
8079 (setq end (region-end)
8081 (goto-char (region-beginning))
8082 (if (not (org-at-heading-p)) (outline-next-heading))
8083 (setq start (point)))
8084 ((or (org-at-heading-p)
8085 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8086 ;; we will sort the children of the current headline
8087 (org-back-to-heading)
8089 end (progn (org-end-of-subtree t t)
8090 (or (bolp) (insert "\n"))
8091 (org-back-over-empty-lines)
8096 (outline-next-heading))
8098 ;; we will sort the top-level entries in this file
8099 (goto-char (point-min))
8100 (or (org-at-heading-p) (outline-next-heading))
8101 (setq start (point))
8102 (goto-char (point-max))
8103 (beginning-of-line 1)
8104 (when (looking-at ".*?\\S-")
8105 ;; File ends in a non-white line
8108 (setq end (point-max))
8109 (setq what "top-level")
8114 (if (>= beg end) (error "Nothing to sort"))
8116 (looking-at "\\(\\*+\\)")
8117 (setq stars (match-string 1)
8118 re (concat "^" (regexp-quote stars) " +")
8119 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8120 txt (buffer-substring beg end))
8121 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8122 (if (and (not (equal stars "*")) (string-match re2 txt))
8123 (error "Region to sort contains a level above the first entry"))
8125 (unless sorting-type
8127 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8128 [t]ime
[s]cheduled [d]eadline [c]reated
8129 A/N/P/R/O/F/T/S/D/C means reversed:"
8131 (setq sorting-type (read-char-exclusive))
8133 (and (= (downcase sorting-type) ?f)
8135 (org-icompleting-read "Sort using function: "
8136 obarray 'fboundp t nil nil))
8137 (setq getkey-func (intern getkey-func)))
8139 (and (= (downcase sorting-type) ?r)
8141 (org-icompleting-read "Property: "
8142 (mapcar 'list (org-buffer-property-keys t))
8145 (message "Sorting entries...")
8148 (narrow-to-region start end)
8149 (let ((dcst (downcase sorting-type))
8150 (case-fold-search nil)
8151 (now (current-time)))
8153 (/= dcst sorting-type)
8154 ;; This function moves to the beginning character of the "record" to
8157 (if (re-search-forward re nil t)
8158 (goto-char (match-beginning 0))
8159 (goto-char (point-max))))
8160 ;; This function moves to the last character of the "record" being
8165 (outline-forward-same-level 1)
8167 (goto-char (point-max))))))
8168 ;; This function returns the value that gets sorted against.
8172 (if (looking-at org-complex-heading-regexp)
8173 (string-to-number (match-string 4))
8176 (if (looking-at org-complex-heading-regexp)
8177 (funcall case-func (match-string 4))
8180 (let ((end (save-excursion (outline-next-heading) (point))))
8181 (if (or (re-search-forward org-ts-regexp end t)
8182 (re-search-forward org-ts-regexp-both end t))
8183 (org-time-string-to-seconds (match-string 0))
8184 (org-float-time now))))
8186 (let ((end (save-excursion (outline-next-heading) (point))))
8187 (if (re-search-forward
8188 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8190 (org-time-string-to-seconds (match-string 0))
8191 (org-float-time now))))
8193 (let ((end (save-excursion (outline-next-heading) (point))))
8194 (if (re-search-forward org-scheduled-time-regexp end t)
8195 (org-time-string-to-seconds (match-string 1))
8196 (org-float-time now))))
8198 (let ((end (save-excursion (outline-next-heading) (point))))
8199 (if (re-search-forward org-deadline-time-regexp end t)
8200 (org-time-string-to-seconds (match-string 1))
8201 (org-float-time now))))
8203 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8204 (string-to-char (match-string 2))
8205 org-default-priority))
8207 (or (org-entry-get nil property) ""))
8209 (if (looking-at org-complex-heading-regexp)
8210 (- 9999 (length (member (match-string 2)
8211 org-todo-keywords-1)))))
8215 (setq tmp (funcall getkey-func))
8216 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8218 (error "Invalid key function `%s'" getkey-func)))
8219 (t (error "Invalid sorting type `%c'" sorting-type))))
8222 ((= dcst ?a) 'string<)
8223 ((= dcst ?f) compare-func)
8224 ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
8225 (run-hooks 'org-after-sorting-entries-or-items-hook)
8226 ;; Reset the clock marker if needed
8230 (search-forward cmstr nil t)
8231 (move-marker org-clock-marker (point))))
8232 (message "Sorting entries...done")))
8234 (defun org-do-sort (table what &optional with-case sorting-type)
8235 "Sort TABLE of WHAT according to SORTING-TYPE.
8236 The user will be prompted for the SORTING-TYPE if the call to this
8237 function does not specify it. WHAT is only for the prompt, to indicate
8238 what is being sorted. The sorting key will be extracted from
8239 the car of the elements of the table.
8240 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8241 (unless sorting-type
8243 "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
8245 (setq sorting-type (read-char-exclusive)))
8246 (let ((dcst (downcase sorting-type))
8247 extractfun comparefun)
8248 ;; Define the appropriate functions
8251 (setq extractfun 'string-to-number
8252 comparefun (if (= dcst sorting-type) '< '>)))
8254 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8255 (lambda(x) (downcase (org-sort-remove-invisible x))))
8256 comparefun (if (= dcst sorting-type)
8258 (lambda (a b) (and (not (string< a b))
8259 (not (string= a b)))))))
8263 (if (or (string-match org-ts-regexp x)
8264 (string-match org-ts-regexp-both x))
8266 (org-time-string-to-time (match-string 0 x)))
8268 comparefun (if (= dcst sorting-type) '< '>)))
8269 (t (error "Invalid sorting type `%c'" sorting-type)))
8271 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8273 (lambda (a b) (funcall comparefun (car a) (car b))))))
8276 ;;; The orgstruct minor mode
8278 ;; Define a minor mode which can be used in other modes in order to
8279 ;; integrate the org-mode structure editing commands.
8281 ;; This is really a hack, because the org-mode structure commands use
8282 ;; keys which normally belong to the major mode. Here is how it
8283 ;; works: The minor mode defines all the keys necessary to operate the
8284 ;; structure commands, but wraps the commands into a function which
8285 ;; tests if the cursor is currently at a headline or a plain list
8286 ;; item. If that is the case, the structure command is used,
8287 ;; temporarily setting many Org-mode variables like regular
8288 ;; expressions for filling etc. However, when any of those keys is
8289 ;; used at a different location, function uses `key-binding' to look
8290 ;; up if the key has an associated command in another currently active
8291 ;; keymap (minor modes, major mode, global), and executes that
8292 ;; command. There might be problems if any of the keys is otherwise
8293 ;; used as a prefix key.
8295 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
8296 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
8297 ;; addresses this by checking explicitly for both bindings.
8299 (defvar orgstruct-mode-map (make-sparse-keymap)
8300 "Keymap for the minor `orgstruct-mode'.")
8302 (defvar org-local-vars nil
8303 "List of local variables, for use by `orgstruct-mode'.")
8306 (define-minor-mode orgstruct-mode
8307 "Toggle the minor mode `orgstruct-mode'.
8308 This mode is for using Org-mode structure commands in other
8309 modes. The following keys behave as if Org-mode were active, if
8310 the cursor is on a headline, or on a plain list item (both as
8311 defined by Org-mode).
8313 M-up Move entry/item up
8314 M-down Move entry/item down
8317 M-S-up Move entry/item up
8318 M-S-down Move entry/item down
8319 M-S-left Promote subtree
8320 M-S-right Demote subtree
8321 M-q Fill paragraph and items like in Org-mode
8323 C-c - Cycle list bullet
8324 TAB Cycle item visibility
8325 M-RET Insert new heading/item
8326 S-M-RET Insert new TODO heading / Checkbox item
8327 C-c C-c Set tags / toggle checkbox"
8328 nil " OrgStruct" nil
8329 (org-load-modules-maybe)
8330 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
8333 (defun turn-on-orgstruct ()
8334 "Unconditionally turn on `orgstruct-mode'."
8337 (defvar org-fb-vars nil)
8338 (make-variable-buffer-local 'org-fb-vars)
8339 (defun orgstruct++-mode (&optional arg)
8340 "Toggle `orgstruct-mode', the enhanced version of it.
8341 In addition to setting orgstruct-mode, this also exports all
8342 indentation and autofilling variables from org-mode into the
8343 buffer. It will also recognize item context in multiline items."
8345 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8347 (progn (orgstruct-mode -1)
8349 (org-set-local (car v)
8350 (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
8353 (setq org-fb-vars nil)
8358 "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
8359 (symbol-name (car x)))
8360 (setq var (car x) val (nth 1 x))
8361 (push (list var `(quote ,(eval var))) org-fb-vars)
8362 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8364 (org-set-local 'orgstruct-is-++ t))))
8366 (defvar orgstruct-is-++ nil
8367 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8368 (make-variable-buffer-local 'orgstruct-is-++)
8371 (defun turn-on-orgstruct++ ()
8372 "Unconditionally turn on `orgstruct++-mode'."
8373 (orgstruct++-mode 1))
8375 (defun orgstruct-error ()
8376 "Error when there is no default binding for a structure key."
8378 (error "This key has no function outside structure elements"))
8380 (defun orgstruct-setup ()
8381 "Setup orgstruct keymaps."
8385 '([(meta up)] org-metaup)
8386 '([(meta down)] org-metadown)
8387 '([(meta left)] org-metaleft)
8388 '([(meta right)] org-metaright)
8389 '([(meta shift up)] org-shiftmetaup)
8390 '([(meta shift down)] org-shiftmetadown)
8391 '([(meta shift left)] org-shiftmetaleft)
8392 '([(meta shift right)] org-shiftmetaright)
8393 '([?\e (up)] org-metaup)
8394 '([?\e (down)] org-metadown)
8395 '([?\e (left)] org-metaleft)
8396 '([?\e (right)] org-metaright)
8397 '([?\e (shift up)] org-shiftmetaup)
8398 '([?\e (shift down)] org-shiftmetadown)
8399 '([?\e (shift left)] org-shiftmetaleft)
8400 '([?\e (shift right)] org-shiftmetaright)
8401 '([(shift up)] org-shiftup)
8402 '([(shift down)] org-shiftdown)
8403 '([(shift left)] org-shiftleft)
8404 '([(shift right)] org-shiftright)
8405 '("\C-c\C-c" org-ctrl-c-ctrl-c)
8406 '("\M-q" fill-paragraph)
8408 '("\C-c-" org-cycle-list-bullet)))
8410 (while (setq elt (pop bindings))
8411 (setq nfunc (1+ nfunc))
8412 (setq key (org-key (car elt))
8414 cmd (orgstruct-make-binding fun nfunc key))
8415 (org-defkey orgstruct-mode-map key cmd))
8417 ;; Prevent an error for users who forgot to make autoloads
8418 (require 'org-element)
8420 ;; Special treatment needed for TAB and RET
8421 (org-defkey orgstruct-mode-map [(tab)]
8422 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
8423 (org-defkey orgstruct-mode-map "\C-i"
8424 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
8426 (org-defkey orgstruct-mode-map "\M-\C-m"
8427 (orgstruct-make-binding 'org-insert-heading 105
8428 "\M-\C-m" [(meta return)]))
8429 (org-defkey orgstruct-mode-map [(meta return)]
8430 (orgstruct-make-binding 'org-insert-heading 106
8431 [(meta return)] "\M-\C-m"))
8433 (org-defkey orgstruct-mode-map [(shift meta return)]
8434 (orgstruct-make-binding 'org-insert-todo-heading 107
8435 [(meta return)] "\M-\C-m"))
8437 (org-defkey orgstruct-mode-map "\e\C-m"
8438 (orgstruct-make-binding 'org-insert-heading 108
8439 "\e\C-m" [?\e (return)]))
8440 (org-defkey orgstruct-mode-map [?\e (return)]
8441 (orgstruct-make-binding 'org-insert-heading 109
8442 [?\e (return)] "\e\C-m"))
8443 (org-defkey orgstruct-mode-map [?\e (shift return)]
8444 (orgstruct-make-binding 'org-insert-todo-heading 110
8445 [?\e (return)] "\e\C-m"))
8447 (unless org-local-vars
8448 (setq org-local-vars (org-get-local-variables)))
8452 (defun orgstruct-make-binding (fun n &rest keys)
8453 "Create a function for binding in the structure minor mode.
8454 FUN is the command to call inside a table. N is used to create a unique
8455 command name. KEYS are keys that should be checked in for a command
8456 to execute outside of tables."
8459 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
8461 (concat "In Structure, run `" (symbol-name fun) "'.\n"
8462 "Outside of structure, run the binding of `"
8463 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
8467 `(org-context-p 'headline 'item
8468 (and orgstruct-is-++
8469 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
8471 (list 'org-run-like-in-org-mode (list 'quote fun))
8472 (list 'let '(orgstruct-mode)
8473 (list 'call-interactively
8476 (list 'key-binding k))
8478 '('orgstruct-error))))))))
8480 (defun org-contextualize-keys (alist contexts)
8481 "Return valid elements in ALIST depending on CONTEXTS.
8483 `org-agenda-custom-commands' or `org-capture-templates' are the
8484 values used for ALIST, and `org-agenda-custom-commands-contexts'
8485 or `org-capture-templates-contexts' are the associated contexts
8488 ;; normalize contexts
8490 (lambda(c) (cond ((listp (cadr c))
8491 (list (car c) (car c) (cadr c)))
8492 ((string= "" (cadr c))
8493 (list (car c) (car c) (caddr c)))
8496 ;; loop over all commands or templates
8497 (while (setq c (pop a))
8500 ((not (assoc (car c) contexts))
8502 ((and (assoc (car c) contexts)
8503 (setq vrules (org-contextualize-validate-key
8506 (when (not (equal (car vr) (cadr vr)))
8507 (setq repl vr))) vrules)
8508 (if (not repl) (push c r)
8509 (push (cadr repl) s)
8512 (cdr (or (assoc (cadr repl) alist)
8513 (error "Undefined key `%s' as contextual replacement for `%s'"
8514 (cadr repl) (car c)))))
8516 ;; Return limited ALIST, possibly with keys modified, and deduplicated
8521 (let ((tpl (car x)))
8525 (equal y tpl)) s))) x)))
8528 (defun org-contextualize-validate-key (key contexts)
8529 "Check CONTEXTS for agenda or capture KEY."
8531 (while (setq r (pop contexts))
8535 (and (equal key (car r))
8536 (if (functionp rr) (funcall rr)
8537 (or (and (eq (car rr) 'in-file)
8539 (string-match (cdr rr) (buffer-file-name)))
8540 (and (eq (car rr) 'in-mode)
8541 (string-match (cdr rr) (symbol-name major-mode)))
8542 (when (and (eq (car rr) 'not-in-file)
8544 (not (string-match (cdr rr) (buffer-file-name))))
8545 (when (eq (car rr) 'not-in-mode)
8546 (not (string-match (cdr rr) (symbol-name major-mode)))))))
8549 (delete-dups (delq nil res))))
8551 (defun org-context-p (&rest contexts)
8552 "Check if local context is any of CONTEXTS.
8553 Possible values in the list of contexts are `table', `headline', and `item'."
8554 (let ((pos (point)))
8555 (goto-char (point-at-bol))
8556 (prog1 (or (and (memq 'table contexts)
8557 (looking-at "[ \t]*|"))
8558 (and (memq 'headline contexts)
8559 (looking-at org-outline-regexp))
8560 (and (memq 'item contexts)
8561 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
8562 (and (memq 'item-body contexts)
8566 (defun org-get-local-variables ()
8567 "Return a list of all local variables in an Org mode buffer."
8569 (with-current-buffer (get-buffer-create "*Org tmp*")
8572 (setq varlist (buffer-local-variables)))
8573 (kill-buffer "*Org tmp*")
8580 (list (car x) (list 'quote (cdr x)))))
8582 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
8583 (symbol-name (car x)))
8587 (defun org-clone-local-variables (from-buffer &optional regexp)
8588 "Clone local variables from FROM-BUFFER.
8589 Optional argument REGEXP selects variables to clone."
8592 (and (symbolp (car pair))
8594 (string-match regexp (symbol-name (car pair))))
8595 (set (make-local-variable (car pair))
8597 (buffer-local-variables from-buffer)))
8600 (defun org-run-like-in-org-mode (cmd)
8601 "Run a command, pretending that the current buffer is in Org-mode.
8602 This will temporarily bind local variables that are typically bound in
8603 Org-mode to the values they have in Org-mode, and then interactively
8605 (org-load-modules-maybe)
8606 (unless org-local-vars
8607 (setq org-local-vars (org-get-local-variables)))
8608 (eval (list 'let org-local-vars
8609 (list 'call-interactively (list 'quote cmd)))))
8613 (defun org-get-category (&optional pos force-refresh)
8614 "Get the category applying to position POS."
8616 (if force-refresh (org-refresh-category-properties))
8617 (let ((pos (or pos (point))))
8618 (or (get-text-property pos 'org-category)
8619 (progn (org-refresh-category-properties)
8620 (get-text-property pos 'org-category))))))
8622 (defun org-refresh-category-properties ()
8623 "Refresh category text properties in the buffer."
8624 (let ((case-fold-search t)
8625 (inhibit-read-only t)
8627 ((null org-category)
8628 (if buffer-file-name
8629 (file-name-sans-extension
8630 (file-name-nondirectory buffer-file-name))
8632 ((symbolp org-category) (symbol-name org-category))
8634 beg end cat pos optionp)
8639 (goto-char (point-min))
8640 (put-text-property (point) (point-max) 'org-category def-cat)
8641 (while (re-search-forward
8642 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8643 (setq pos (match-end 0)
8644 optionp (equal (char-after (match-beginning 0)) ?#)
8645 cat (org-trim (match-string 2)))
8647 (setq beg (point-at-bol) end (point-max))
8648 (org-back-to-heading t)
8649 (setq beg (point) end (org-end-of-subtree t t)))
8650 (put-text-property beg end 'org-category cat)
8651 (put-text-property beg end 'org-category-position beg)
8652 (goto-char pos)))))))
8654 (defun org-refresh-properties (dprop tprop)
8655 "Refresh buffer text properties.
8656 DPROP is the drawer property and TPROP is the corresponding text
8658 (let ((case-fold-search t)
8659 (inhibit-read-only t) p)
8664 (goto-char (point-min))
8665 (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
8666 (setq p (org-match-string-no-properties 1))
8668 (org-back-to-heading t)
8670 (point-at-bol) (point-at-eol) tprop p))))))))
8675 ;;; Link abbreviations
8677 (defun org-link-expand-abbrev (link)
8678 "Apply replacements as defined in `org-link-abbrev-alist'."
8679 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
8680 (let* ((key (match-string 1 link))
8681 (as (or (assoc key org-link-abbrev-alist-local)
8682 (assoc key org-link-abbrev-alist)))
8683 (tag (and (match-end 2) (match-string 3 link)))
8689 ((symbolp rpl) (funcall rpl tag))
8690 ((string-match "%(\\([^)]+\\))" rpl)
8691 (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
8692 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
8693 ((string-match "%h" rpl)
8694 (replace-match (url-hexify-string (or tag "")) t t rpl))
8695 (t (concat rpl tag)))))
8698 ;;; Storing and inserting links
8700 (defvar org-insert-link-history nil
8701 "Minibuffer history for links inserted with `org-insert-link'.")
8703 (defvar org-stored-links nil
8704 "Contains the links stored with `org-store-link'.")
8706 (defvar org-store-link-plist nil
8707 "Plist with info about the most recently link created with `org-store-link'.")
8709 (defvar org-link-protocols nil
8710 "Link protocols added to Org-mode using `org-add-link-type'.")
8712 (defvar org-store-link-functions nil
8713 "List of functions that are called to create and store a link.
8714 Each function will be called in turn until one returns a non-nil
8715 value. Each function should check if it is responsible for creating
8716 this link (for example by looking at the major mode).
8717 If not, it must exit and return nil.
8718 If yes, it should return a non-nil value after a calling
8719 `org-store-link-props' with a list of properties and values.
8720 Special properties are:
8722 :type The link prefix, like \"http\". This must be given.
8723 :link The link, like \"http://www.astro.uva.nl/~dominik\".
8724 This is obligatory as well.
8725 :description Optional default description for the second pair
8726 of brackets in an Org-mode link. The user can still change
8727 this when inserting this link into an Org-mode buffer.
8729 In addition to these, any additional properties can be specified
8730 and then used in capture templates.")
8732 (defun org-add-link-type (type &optional follow export)
8733 "Add TYPE to the list of `org-link-types'.
8734 Re-compute all regular expressions depending on `org-link-types'
8736 FOLLOW and EXPORT are two functions.
8738 FOLLOW should take the link path as the single argument and do whatever
8739 is necessary to follow the link, for example find a file or display
8742 EXPORT should format the link path for export to one of the export formats.
8743 It should be a function accepting three arguments:
8745 path the path of the link, the text after the prefix (like \"http:\")
8746 desc the description of the link, if any, or a description added by
8747 org-export-normalize-links if there is none
8748 format the export format, a symbol like `html' or `latex' or `ascii'..
8750 The function may use the FORMAT information to return different values
8751 depending on the format. The return value will be put literally into
8752 the exported file. If the return value is nil, this means Org should
8753 do what it normally does with links which do not have EXPORT defined.
8755 Org-mode has a built-in default for exporting links. If you are happy with
8756 this default, there is no need to define an export function for the link
8757 type. For a simple example of an export function, see `org-bbdb.el'."
8758 (add-to-list 'org-link-types type t)
8759 (org-make-link-regexps)
8760 (if (assoc type org-link-protocols)
8761 (setcdr (assoc type org-link-protocols) (list follow export))
8762 (push (list type follow export) org-link-protocols)))
8764 (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
8765 (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
8768 (defun org-store-link (arg)
8769 "\\<org-mode-map>Store an org-link to the current location.
8770 This link is added to `org-stored-links' and can later be inserted
8771 into an org-buffer with \\[org-insert-link].
8773 For some link types, a prefix arg is interpreted:
8774 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
8775 For file links, arg negates `org-context-in-file-links'."
8777 (org-load-modules-maybe)
8778 (setq org-store-link-plist nil) ; reset
8779 (org-with-limited-levels
8780 (let (link cpltxt desc description search txt custom-id agenda-link)
8783 ((run-hook-with-args-until-success 'org-store-link-functions)
8784 (setq link (plist-get org-store-link-plist :link)
8785 desc (or (plist-get org-store-link-plist :description) link)))
8787 ((org-src-edit-buffer-p)
8789 (while (or (not label)
8793 (goto-char (point-min))
8795 (regexp-quote (format org-coderef-label-format label))
8797 (when label (message "Label exists already") (sit-for 2))
8798 (setq label (read-string "Code line label: " label)))
8800 (setq link (format org-coderef-label-format label))
8801 (setq gc (- 79 (length link)))
8802 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
8804 (setq link (concat "(" label ")") desc nil)))
8806 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
8807 ;; We are in the agenda, link to referenced location
8808 (let ((m (or (get-text-property (point) 'org-hd-marker)
8809 (get-text-property (point) 'org-marker))))
8811 (org-with-point-at m
8813 (if (org-called-interactively-p 'any)
8814 (call-interactively 'org-store-link)
8815 (org-store-link nil)))))))
8817 ((eq major-mode 'calendar-mode)
8818 (let ((cd (calendar-cursor-to-date)))
8821 (car org-time-stamp-formats)
8823 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8825 (org-store-link-props :type "calendar" :date cd)))
8827 ((eq major-mode 'help-mode)
8828 (setq link (concat "help:" (save-excursion
8829 (goto-char (point-min))
8830 (looking-at "^[^ ]+")
8832 (org-store-link-props :type "help"))
8834 ((eq major-mode 'w3-mode)
8835 (setq cpltxt (if (and (buffer-name)
8836 (not (string-match "Untitled" (buffer-name))))
8839 link (url-view-url t))
8840 (org-store-link-props :type "w3" :url (url-view-url t)))
8842 ((eq major-mode 'w3m-mode)
8843 (setq cpltxt (or w3m-current-title w3m-current-url)
8844 link w3m-current-url)
8845 (org-store-link-props :type "w3m" :url (url-view-url t)))
8847 ((setq search (run-hook-with-args-until-success
8848 'org-create-file-search-functions))
8849 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8851 (setq cpltxt (or description link)))
8853 ((eq major-mode 'image-mode)
8854 (setq cpltxt (concat "file:"
8855 (abbreviate-file-name buffer-file-name))
8857 (org-store-link-props :type "image" :file buffer-file-name))
8859 ((eq major-mode 'dired-mode)
8860 ;; link to the file in the current line
8861 (let ((file (dired-get-filename nil t)))
8863 (abbreviate-file-name
8864 (expand-file-name (dired-get-filename nil t)))
8865 ;; otherwise, no file so use current directory.
8867 (setq cpltxt (concat "file:" file)
8870 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
8871 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
8873 ((org-in-regexp "<<\\(.*?\\)>>")
8876 (abbreviate-file-name
8877 (buffer-file-name (buffer-base-buffer)))
8878 "::" (match-string 1))
8880 ((and (featurep 'org-id)
8881 (or (eq org-id-link-to-org-use-id t)
8882 (and (org-called-interactively-p 'any)
8883 (or (eq org-id-link-to-org-use-id 'create-if-interactive)
8884 (and (eq org-id-link-to-org-use-id
8885 'create-if-interactive-and-no-custom-id)
8887 (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
8888 ;; We can make a link using the ID.
8889 (setq link (condition-case nil
8890 (prog1 (org-id-store-link)
8891 (setq desc (plist-get org-store-link-plist :description)))
8893 ;; probably before first headline, link to file only
8895 (abbreviate-file-name
8896 (buffer-file-name (buffer-base-buffer))))))))
8898 ;; Just link to current headline
8899 (setq cpltxt (concat "file:"
8900 (abbreviate-file-name
8901 (buffer-file-name (buffer-base-buffer)))))
8902 ;; Add a context search string
8903 (when (org-xor org-context-in-file-links arg)
8905 ((org-at-heading-p) nil)
8906 ((org-region-active-p)
8907 (buffer-substring (region-beginning) (region-end)))))
8908 (when (or (null txt) (string-match "\\S-" txt))
8912 (org-make-org-heading-search-string txt)
8914 desc (or (nth 4 (ignore-errors
8915 (org-heading-components))) "NONE"))))
8916 (if (string-match "::\\'" cpltxt)
8917 (setq cpltxt (substring cpltxt 0 -2)))
8918 (setq link cpltxt))))
8920 ((buffer-file-name (buffer-base-buffer))
8921 ;; Just link to this file here.
8922 (setq cpltxt (concat "file:"
8923 (abbreviate-file-name
8924 (buffer-file-name (buffer-base-buffer)))))
8925 ;; Add a context string
8926 (when (org-xor org-context-in-file-links arg)
8927 (setq txt (if (org-region-active-p)
8928 (buffer-substring (region-beginning) (region-end))
8929 (buffer-substring (point-at-bol) (point-at-eol))))
8930 ;; Only use search option if there is some text.
8931 (when (string-match "\\S-" txt)
8933 (concat cpltxt "::" (org-make-org-heading-search-string txt))
8937 ((org-called-interactively-p 'interactive)
8938 (error "Cannot link to a buffer which is not visiting a file"))
8940 (t (setq link nil)))
8942 (if (consp link) (setq cpltxt (car link) link (cdr link)))
8943 (setq link (or link cpltxt)
8944 desc (or desc cpltxt))
8945 (if (equal desc "NONE") (setq desc nil))
8947 (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
8949 (setq org-stored-links
8950 (cons (list link desc) org-stored-links))
8951 (message "Stored: %s" (or desc link))
8953 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
8955 (setq org-stored-links
8956 (cons (list link desc) org-stored-links))))
8957 (or agenda-link (and link (org-make-link-string link desc)))))))
8959 (defun org-store-link-props (&rest plist)
8960 "Store link properties, extract names and addresses."
8962 (when (setq x (plist-get plist :from))
8963 (setq adr (mail-extract-address-components x))
8964 (setq plist (plist-put plist :fromname (car adr)))
8965 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
8966 (when (setq x (plist-get plist :to))
8967 (setq adr (mail-extract-address-components x))
8968 (setq plist (plist-put plist :toname (car adr)))
8969 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
8970 (let ((from (plist-get plist :from))
8971 (to (plist-get plist :to)))
8972 (when (and from to org-from-is-user-regexp)
8974 (plist-put plist :fromto
8975 (if (string-match org-from-is-user-regexp from)
8977 (concat "from %f"))))))
8978 (setq org-store-link-plist plist))
8980 (defun org-add-link-props (&rest plist)
8981 "Add these properties to the link property list."
8984 (setq key (pop plist) value (pop plist))
8985 (setq org-store-link-plist
8986 (plist-put org-store-link-plist key value)))))
8988 (defun org-email-link-description (&optional fmt)
8989 "Return the description part of an email link.
8990 This takes information from `org-store-link-plist' and formats it
8991 according to FMT (default from `org-email-link-description-format')."
8992 (setq fmt (or fmt org-email-link-description-format))
8993 (let* ((p org-store-link-plist)
8994 (to (plist-get p :toaddress))
8995 (from (plist-get p :fromaddress))
8998 (cons "%c" (plist-get p :fromto))
8999 (cons "%F" (plist-get p :from))
9000 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
9001 (cons "%T" (plist-get p :to))
9002 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
9003 (cons "%s" (plist-get p :subject))
9004 (cons "%d" (plist-get p :date))
9005 (cons "%m" (plist-get p :message-id)))))
9006 (when (string-match "%c" fmt)
9007 ;; Check if the user wrote this message
9008 (if (and org-from-is-user-regexp from to
9009 (save-match-data (string-match org-from-is-user-regexp from)))
9010 (setq fmt (replace-match "to %t" t t fmt))
9011 (setq fmt (replace-match "from %f" t t fmt))))
9012 (org-replace-escapes fmt table)))
9014 (defun org-make-org-heading-search-string (&optional string heading)
9015 "Make search string for STRING or current headline."
9017 (let ((s (or string (org-get-heading)))
9018 (lines org-context-in-file-links))
9019 (unless (and string (not heading))
9020 ;; We are using a headline, clean up garbage in there.
9021 (if (string-match org-todo-regexp s)
9022 (setq s (replace-match "" t t s)))
9023 (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
9024 (setq s (replace-match "" t t s)))
9025 (setq s (org-trim s))
9026 (if (string-match (concat "^\\(" org-quote-string "\\|"
9027 org-comment-string "\\)") s)
9028 (setq s (replace-match "" t t s)))
9029 (while (string-match org-ts-regexp s)
9030 (setq s (replace-match "" t t s))))
9031 (or string (setq s (concat "*" s))) ; Add * for headlines
9032 (when (and string (integerp lines) (> lines 0))
9033 (let ((slines (org-split-string s "\n")))
9034 (when (< lines (length slines))
9037 (reverse (nthcdr (- (length slines) lines)
9038 (reverse slines))) "\n")))))
9039 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
9041 (defun org-make-link-string (link &optional description)
9042 "Make a link with brackets, consisting of LINK and DESCRIPTION."
9043 (unless (string-match "\\S-" link)
9044 (error "Empty link"))
9045 (when (and description
9046 (stringp description)
9047 (not (string-match "\\S-" description)))
9048 (setq description nil))
9049 (when (stringp description)
9050 ;; Remove brackets from the description, they are fatal.
9051 (while (string-match "\\[" description)
9052 (setq description (replace-match "{" t t description)))
9053 (while (string-match "\\]" description)
9054 (setq description (replace-match "}" t t description))))
9055 (when (equal link description)
9056 ;; No description needed, it is identical
9057 (setq description nil))
9058 (when (and (not description)
9059 (not (string-match (org-image-file-name-regexp) link))
9060 (not (equal link (org-link-escape link))))
9061 (setq description (org-extract-attributes link)))
9063 (cond ((string-match (org-image-file-name-regexp) link) link)
9064 ((string-match org-link-types-re link)
9065 (concat (match-string 1 link)
9066 (org-link-escape (substring link (match-end 1)))))
9067 (t (org-link-escape link))))
9068 (concat "[[" link "]"
9069 (if description (concat "[" description "]") "")
9072 (defconst org-link-escape-chars
9073 '(?\ ?\[ ?\] ?\; ?\= ?\+)
9074 "List of characters that should be escaped in link.
9075 This is the list that is used for internal purposes.")
9077 (defconst org-link-escape-chars-browser
9079 "List of escapes for characters that are problematic in links.
9080 This is the list that is used before handing over to the browser.")
9082 (defun org-link-escape (text &optional table merge)
9083 "Return percent escaped representation of TEXT.
9084 TEXT is a string with the text to escape.
9085 Optional argument TABLE is a list with characters that should be
9086 escaped. When nil, `org-link-escape-chars' is used.
9087 If optional argument MERGE is set, merge TABLE into
9088 `org-link-escape-chars'."
9091 (mapc (lambda (defchr)
9092 (unless (member defchr table)
9093 (setq table (cons defchr table)))) org-link-escape-chars))
9095 (setq table org-link-escape-chars)))
9098 (if (or (member char table)
9099 (and (or (< char 32) (= char 37) (> char 126))
9101 (mapconcat (lambda (sequence-element)
9102 (format "%%%.2X" sequence-element))
9103 (or (encode-coding-char char 'utf-8)
9104 (error "Unable to percent escape character: %s"
9105 (char-to-string char))) "")
9106 (char-to-string char))) text ""))
9108 (defun org-link-unescape (str)
9109 "Unhex hexified Unicode strings as returned from the JavaScript function
9110 encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
9111 (unless (and (null str) (string= "" str))
9112 (let ((pos 0) (case-fold-search t) unhexed)
9113 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
9114 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
9115 (setq str (replace-match unhexed t t str))
9116 (setq pos (+ pos (length unhexed))))))
9119 (defun org-link-unescape-compound (hex)
9120 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
9121 Note: this function also decodes single byte encodings like
9122 `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
9124 (let* ((bytes (cdr (split-string hex "%")))
9129 (let* ((val (string-to-number (pop bytes) 16))
9133 ((>= val 252) (cons 6 252))
9134 ((>= val 248) (cons 5 248))
9135 ((>= val 240) (cons 4 240))
9136 ((>= val 224) (cons 3 224))
9137 ((>= val 192) (cons 2 192))
9140 (if (>= val 192) (setq eat (car shift-xor)))
9141 (setq val (logxor val (cdr shift-xor)))
9142 (setq sum (+ (lsh sum (car shift-xor)) val))
9143 (if (> eat 0) (setq eat (- eat 1)))
9145 ((= 0 eat) ;multi byte
9146 (setq ret (concat ret (org-char-to-string sum)))
9148 ((not bytes) ; single byte(s)
9149 (setq ret (org-link-unescape-single-byte-sequence hex))))
9150 )) ;; end (while bytes
9153 (defun org-link-unescape-single-byte-sequence (hex)
9154 "Unhexify hex-encoded single byte character sequences."
9155 (mapconcat (lambda (byte)
9156 (char-to-string (string-to-number byte 16)))
9157 (cdr (split-string hex "%")) ""))
9159 (defun org-xor (a b)
9163 (defun org-fixup-message-id-for-http (s)
9164 "Replace special characters in a message id, so it can be used in an http query."
9165 (when (string-match "%" s)
9166 (setq s (mapconcat (lambda (c)
9169 (char-to-string c)))
9171 (while (string-match "<" s)
9172 (setq s (replace-match "%3C" t t s)))
9173 (while (string-match ">" s)
9174 (setq s (replace-match "%3E" t t s)))
9175 (while (string-match "@" s)
9176 (setq s (replace-match "%40" t t s)))
9179 (defun org-link-prettify (link)
9180 "Return a human-readable representation of LINK.
9181 The car of LINK must be a raw link the cdr of LINK must be either
9182 a link description or nil."
9183 (let ((desc (or (cadr link) "<no description>")))
9184 (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
9185 "<" (car link) ">")))
9188 (defun org-insert-link-global ()
9189 "Insert a link like Org-mode does.
9190 This command can be called in any mode to insert a link in Org-mode syntax."
9192 (org-load-modules-maybe)
9193 (org-run-like-in-org-mode 'org-insert-link))
9195 (defun org-insert-all-links (&optional keep)
9196 "Insert all links in `org-stored-links'."
9198 (let ((links (copy-sequence org-stored-links)) l)
9199 (while (setq l (if keep (pop links) (pop org-stored-links)))
9201 (org-insert-link nil (car l) (cadr l))
9204 (defun org-link-fontify-links-to-this-file ()
9205 "Fontify links to the current file in `org-stored-links'."
9206 (let ((f (buffer-file-name)) a b)
9207 (setq a (mapcar (lambda(l)
9209 (when (and (string-match "^file:\\(.+\\)::" ll)
9210 (equal f (expand-file-name (match-string 1 ll))))
9213 (when (featurep 'org-id)
9214 (setq b (mapcar (lambda(l)
9216 (when (and (string-match "^id:\\(.+\\)$" ll)
9217 (equal f (expand-file-name
9218 (or (org-id-find-id-file
9219 (match-string 1 ll)) ""))))
9223 (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
9224 (delq nil (append a b)))))
9226 (defvar org-link-links-in-this-file nil)
9227 (defun org-insert-link (&optional complete-file link-location default-description)
9228 "Insert a link. At the prompt, enter the link.
9230 Completion can be used to insert any of the link protocol prefixes like
9233 The history can be used to select a link previously stored with
9234 `org-store-link'. When the empty string is entered (i.e. if you just
9235 press RET at the prompt), the link defaults to the most recently
9236 stored link. As SPC triggers completion in the minibuffer, you need to
9237 use M-SPC or C-q SPC to force the insertion of a space character.
9239 You will also be prompted for a description, and if one is given, it will
9240 be displayed in the buffer instead of the link.
9242 If there is already a link at point, this command will allow you to edit link
9243 and description parts.
9245 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9246 be selected using completion. The path to the file will be relative to the
9247 current directory if the file is in the current directory or a subdirectory.
9248 Otherwise, the link will be the absolute path as completed in the minibuffer
9249 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9250 option `org-link-file-path-type'.
9252 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9253 the current directory or below.
9255 With three \\[universal-argument] prefixes, negate the meaning of
9256 `org-keep-stored-link-after-insertion'.
9258 If `org-make-link-description-function' is non-nil, this function will be
9259 called with the link target, and the result will be the default
9262 If the LINK-LOCATION parameter is non-nil, this value will be
9263 used as the link location instead of reading one interactively.
9265 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9266 be used as the default description."
9268 (let* ((wcf (current-window-configuration))
9269 (region (if (org-region-active-p)
9270 (buffer-substring (region-beginning) (region-end))))
9271 (remove (and region (list (region-beginning) (region-end))))
9273 tmphist ; byte-compile incorrectly complains about this
9274 (link link-location)
9275 (abbrevs org-link-abbrev-alist-local)
9276 entry file all-prefixes auto-desc)
9278 (link-location) ; specified by arg, just use it.
9279 ((org-in-regexp org-bracket-link-regexp 1)
9280 ;; We do have a link at point, and we are going to edit it.
9281 (setq remove (list (match-beginning 0) (match-end 0)))
9282 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
9283 (setq link (read-string "Link: "
9285 (org-match-string-no-properties 1)))))
9286 ((or (org-in-regexp org-angle-link-re)
9287 (org-in-regexp org-plain-link-re))
9288 ;; Convert to bracket link
9289 (setq remove (list (match-beginning 0) (match-end 0))
9290 link (read-string "Link: "
9291 (org-remove-angle-brackets (match-string 0)))))
9292 ((member complete-file '((4) (16)))
9293 ;; Completing read for file names.
9294 (setq link (org-file-complete-link complete-file)))
9296 ;; Read link, with completion for stored links.
9297 (org-link-fontify-links-to-this-file)
9298 (org-switch-to-buffer-other-window "*Org Links*")
9299 (with-current-buffer "*Org Links*"
9301 (insert "Insert a link.
9302 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9303 (when org-stored-links
9304 (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9305 (insert (mapconcat 'org-link-prettify
9306 (reverse org-stored-links) "\n")))
9307 (goto-char (point-min)))
9308 (let ((cw (selected-window)))
9309 (select-window (get-buffer-window "*Org Links*" 'visible))
9310 (with-current-buffer "*Org Links*" (setq truncate-lines t))
9311 (unless (pos-visible-in-window-p (point-max))
9312 (org-fit-window-to-buffer))
9313 (and (window-live-p cw) (select-window cw)))
9314 ;; Fake a link history, containing the stored links.
9315 (setq tmphist (append (mapcar 'car org-stored-links)
9316 org-insert-link-history))
9317 (setq all-prefixes (append (mapcar 'car abbrevs)
9318 (mapcar 'car org-link-abbrev-alist)
9323 (let ((org-completion-use-ido nil)
9324 (org-completion-use-iswitchb nil))
9325 (org-completing-read
9328 (mapcar (lambda (x) (list (concat x ":")))
9330 (mapcar 'car org-stored-links)
9331 (mapcar 'cadr org-stored-links))
9334 (caar org-stored-links))))
9335 (if (not (string-match "\\S-" link))
9336 (error "No link selected"))
9338 (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
9340 (if (or (member link all-prefixes)
9341 (and (equal ":" (substring link -1))
9342 (member (substring link 0 -1) all-prefixes)
9343 (setq link (substring link 0 -1))))
9344 (setq link (org-link-try-special-completion link))))
9345 (set-window-configuration wcf)
9346 (kill-buffer "*Org Links*"))
9347 (setq entry (assoc link org-stored-links))
9348 (or entry (push link org-insert-link-history))
9349 (setq desc (or desc (nth 1 entry)))))
9351 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
9352 (not org-keep-stored-link-after-insertion))
9353 (setq org-stored-links (delq (assoc link org-stored-links)
9356 (if (string-match org-plain-link-re link)
9357 ;; URL-like link, normalize the use of angular brackets.
9358 (setq link (org-remove-angle-brackets link)))
9360 ;; Check if we are linking to the current file with a search
9361 ;; option If yes, simplify the link by using only the search
9363 (when (and buffer-file-name
9364 (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
9365 (let* ((path (match-string 1 link))
9366 (case-fold-search nil)
9367 (search (match-string 2 link)))
9369 (if (equal (file-truename buffer-file-name) (file-truename path))
9370 ;; We are linking to this same file, with a search option
9371 (setq link search)))))
9373 ;; Check if we can/should use a relative path. If yes, simplify the link
9374 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
9375 (let* ((type (match-string 1 link))
9376 (path (match-string 2 link))
9378 (case-fold-search nil))
9380 ((or (eq org-link-file-path-type 'absolute)
9381 (equal complete-file '(16)))
9382 (setq path (abbreviate-file-name (expand-file-name path))))
9383 ((eq org-link-file-path-type 'noabbrev)
9384 (setq path (expand-file-name path)))
9385 ((eq org-link-file-path-type 'relative)
9386 (setq path (file-relative-name path)))
9389 (if (string-match (concat "^" (regexp-quote
9391 (file-name-as-directory
9392 default-directory))))
9393 (expand-file-name path))
9394 ;; We are linking a file with relative path name.
9395 (setq path (substring (expand-file-name path)
9397 (setq path (abbreviate-file-name (expand-file-name path)))))))
9398 (setq link (concat type path))
9399 (if (equal desc origpath)
9402 (if org-make-link-description-function
9404 (or (condition-case nil
9405 (funcall org-make-link-description-function link desc)
9406 (error (progn (message "Can't get link description from `%s'"
9407 (symbol-name org-make-link-description-function))
9409 (read-string "Description: " default-description)))
9410 (if default-description (setq desc default-description)
9411 (setq desc (or (and auto-desc desc)
9412 (read-string "Description: " desc)))))
9414 (unless (string-match "\\S-" desc) (setq desc nil))
9415 (if remove (apply 'delete-region remove))
9416 (insert (org-make-link-string link desc))))
9418 (defun org-link-try-special-completion (type)
9419 "If there is completion support for link type TYPE, offer it."
9420 (let ((fun (intern (concat "org-" type "-complete-link"))))
9423 (read-string "Link (no completion support): " (concat type ":")))))
9425 (defun org-file-complete-link (&optional arg)
9426 "Create a file link using completion."
9428 (setq file (read-file-name "File: "))
9429 (let ((pwd (file-name-as-directory (expand-file-name ".")))
9430 (pwd1 (file-name-as-directory (abbreviate-file-name
9431 (expand-file-name ".")))))
9436 (abbreviate-file-name (expand-file-name file)))))
9437 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
9438 (setq link (concat "file:" (match-string 1 file))))
9439 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
9440 (expand-file-name file))
9442 "file:" (match-string 1 (expand-file-name file)))))
9443 (t (setq link (concat "file:" file)))))
9446 (defun org-completing-read (&rest args)
9447 "Completing-read with SPACE being a normal character."
9448 (let ((enable-recursive-minibuffers t)
9449 (minibuffer-local-completion-map
9450 (copy-keymap minibuffer-local-completion-map)))
9451 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
9452 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
9453 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
9454 (apply 'org-icompleting-read args)))
9456 (defun org-completing-read-no-i (&rest args)
9457 (let (org-completion-use-ido org-completion-use-iswitchb)
9458 (apply 'org-completing-read args)))
9460 (defun org-iswitchb-completing-read (prompt choices &rest args)
9461 "Use iswitch as a completing-read replacement to choose from choices.
9462 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
9464 (let* ((iswitchb-use-virtual-buffers nil)
9465 (iswitchb-make-buflist-hook
9467 (setq iswitchb-temp-buflist choices))))
9468 (iswitchb-read-buffer prompt)))
9470 (defun org-icompleting-read (&rest args)
9471 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
9472 (org-without-partial-completion
9473 (if (and org-completion-use-ido
9474 (fboundp 'ido-completing-read)
9475 (boundp 'ido-mode) ido-mode
9476 (listp (second args)))
9477 (let ((ido-enter-matching-directory nil))
9478 (apply 'ido-completing-read (concat (car args))
9479 (if (consp (car (nth 1 args)))
9480 (mapcar 'car (nth 1 args))
9483 (if (and org-completion-use-iswitchb
9484 (boundp 'iswitchb-mode) iswitchb-mode
9485 (listp (second args)))
9486 (apply 'org-iswitchb-completing-read (concat (car args))
9487 (if (consp (car (nth 1 args)))
9488 (mapcar 'car (nth 1 args))
9491 (apply 'completing-read args)))))
9493 (defun org-extract-attributes (s)
9494 "Extract the attributes cookie from a string and set as text property."
9495 (let (a attr (start 0) key value)
9497 (when (string-match "{{\\([^}]+\\)}}$" s)
9498 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
9499 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
9500 (setq key (match-string 1 a) value (match-string 2 a)
9502 attr (plist-put attr (intern key) value))))
9503 (org-add-props s nil 'org-attr attr))
9506 (defun org-extract-attributes-from-string (tag)
9507 (let (key value attr)
9508 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
9509 (setq key (match-string 1 tag) value (match-string 2 tag)
9510 tag (replace-match "" t t tag)
9511 attr (plist-put attr (intern key) value)))
9514 (defun org-attributes-to-string (plist)
9515 "Format a property list into an HTML attribute list."
9516 (let ((s "") key value)
9518 (setq key (pop plist) value (pop plist))
9520 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
9523 ;;; Opening/following a link
9525 (defvar org-link-search-failed nil)
9527 (defvar org-open-link-functions nil
9528 "Hook for functions finding a plain text link.
9529 These functions must take a single argument, the link content.
9530 They will be called for links that look like [[link text][description]]
9531 when LINK TEXT does not have a protocol like \"http:\" and does not look
9532 like a filename (e.g. \"./blue.png\").
9534 These functions will be called *before* Org attempts to resolve the
9535 link by doing text searches in the current buffer - so if you want a
9536 link \"[[target]]\" to still find \"<<target>>\", your function should
9537 handle this as a special case.
9539 When the function does handle the link, it must return a non-nil value.
9540 If it decides that it is not responsible for this link, it must return
9541 nil to indicate that that Org-mode can continue with other options
9542 like exact and fuzzy text search.")
9544 (defun org-next-link ()
9545 "Move forward to the next link.
9546 If the link is in hidden text, expose it."
9548 (when (and org-link-search-failed (eq this-command last-command))
9549 (goto-char (point-min))
9550 (message "Link search wrapped back to beginning of buffer"))
9551 (setq org-link-search-failed nil)
9552 (let* ((pos (point))
9554 (a (assoc :link ct)))
9555 (if a (goto-char (nth 2 a)))
9556 (if (re-search-forward org-any-link-re nil t)
9558 (goto-char (match-beginning 0))
9559 (if (outline-invisible-p) (org-show-context)))
9561 (setq org-link-search-failed t)
9562 (error "No further link found"))))
9564 (defun org-previous-link ()
9565 "Move backward to the previous link.
9566 If the link is in hidden text, expose it."
9568 (when (and org-link-search-failed (eq this-command last-command))
9569 (goto-char (point-max))
9570 (message "Link search wrapped back to end of buffer"))
9571 (setq org-link-search-failed nil)
9572 (let* ((pos (point))
9574 (a (assoc :link ct)))
9575 (if a (goto-char (nth 1 a)))
9576 (if (re-search-backward org-any-link-re nil t)
9578 (goto-char (match-beginning 0))
9579 (if (outline-invisible-p) (org-show-context)))
9581 (setq org-link-search-failed t)
9582 (error "No further link found"))))
9584 (defun org-translate-link (s)
9585 "Translate a link string if a translation function has been defined."
9586 (if (and org-link-translation-function
9587 (fboundp org-link-translation-function)
9588 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
9590 (setq s (funcall org-link-translation-function
9591 (match-string 1 s) (match-string 2 s)))
9592 (concat (car s) ":" (cdr s)))
9595 (defun org-translate-link-from-planner (type path)
9596 "Translate a link from Emacs Planner syntax so that Org can follow it.
9597 This is still an experimental function, your mileage may vary."
9599 ((member type '("http" "https" "news" "ftp"))
9600 ;; standard Internet links are the same.
9602 ((and (equal type "irc") (string-match "^//" path))
9603 ;; Planner has two / at the beginning of an irc link, we have 1.
9604 ;; We should have zero, actually....
9605 (setq path (substring path 1)))
9606 ((and (equal type "lisp") (string-match "^/" path))
9607 ;; Planner has a slash, we do not.
9608 (setq type "elisp" path (substring path 1)))
9609 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
9610 ;; A typical message link. Planner has the id after the final slash,
9611 ;; we separate it with a hash mark
9612 (setq path (concat (match-string 1 path) "#"
9613 (org-remove-angle-brackets (match-string 2 path)))))
9617 (defun org-find-file-at-mouse (ev)
9618 "Open file link or URL at mouse."
9620 (mouse-set-point ev)
9621 (org-open-at-point 'in-emacs))
9623 (defun org-open-at-mouse (ev)
9624 "Open file link or URL at mouse.
9625 See the docstring of `org-open-file' for details."
9627 (mouse-set-point ev)
9628 (if (eq major-mode 'org-agenda-mode)
9629 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
9630 (org-open-at-point))
9632 (defvar org-window-config-before-follow-link nil
9633 "The window configuration before following a link.
9634 This is saved in case the need arises to restore it.")
9636 (defvar org-open-link-marker (make-marker)
9637 "Marker pointing to the location where `org-open-at-point; was called.")
9640 (defun org-open-at-point-global ()
9641 "Follow a link like Org-mode does.
9642 This command can be called in any mode to follow a link that has
9645 (org-run-like-in-org-mode 'org-open-at-point))
9648 (defun org-open-link-from-string (s &optional arg reference-buffer)
9649 "Open a link in the string S, as if it was in Org-mode."
9650 (interactive "sLink: \nP")
9651 (let ((reference-buffer (or reference-buffer (current-buffer))))
9653 (let ((org-inhibit-startup (not reference-buffer)))
9656 (goto-char (point-min))
9657 (when reference-buffer
9658 (setq org-link-abbrev-alist-local
9659 (with-current-buffer reference-buffer
9660 org-link-abbrev-alist-local)))
9661 (org-open-at-point arg reference-buffer)))))
9663 (defvar org-open-at-point-functions nil
9664 "Hook that is run when following a link at point.
9666 Functions in this hook must return t if they identify and follow
9667 a link at point. If they don't find anything interesting at point,
9668 they must return nil.")
9670 (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
9671 (defun org-open-at-point (&optional arg reference-buffer)
9672 "Open link at or after point.
9673 If there is no link at point, this function will search forward up to
9674 the end of the current line.
9675 Normally, files will be opened by an appropriate application. If the
9676 optional prefix argument ARG is non-nil, Emacs will visit the file.
9677 With a double prefix argument, try to open outside of Emacs, in the
9678 application the system uses for this file type."
9680 ;; if in a code block, then open the block's results
9681 (unless (call-interactively #'org-babel-open-src-block-result)
9682 (org-load-modules-maybe)
9683 (move-marker org-open-link-marker (point))
9684 (setq org-window-config-before-follow-link (current-window-configuration))
9685 (org-remove-occur-highlights nil nil t)
9687 ((and (org-at-heading-p)
9688 (not (org-at-timestamp-p t))
9690 (concat org-plain-link-re "\\|"
9691 org-bracket-link-regexp "\\|"
9692 org-angle-link-re "\\|"
9693 "[ \t]:[^ \t\n]+:[ \t]*$")))
9694 (not (get-text-property (point) 'org-linked-text)))
9695 (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
9697 (lk (if (stringp lk0) (list lk0) lk0))
9698 (lkend (cdr lkall)))
9700 (search-forward l nil lkend)
9701 (goto-char (match-beginning 0))
9702 (org-open-at-point))
9704 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
9705 ((run-hook-with-args-until-success 'org-open-at-point-functions))
9706 ((and (org-at-timestamp-p t)
9707 (not (org-in-regexp org-bracket-link-regexp)))
9708 (org-follow-timestamp-link))
9709 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
9710 (not (org-in-regexp org-any-link-re)))
9711 (org-footnote-action))
9713 (let (type path link line search (pos (point)))
9716 (skip-chars-forward "^]\n\r")
9717 (when (org-in-regexp org-bracket-link-regexp 1)
9718 (setq link (org-extract-attributes
9719 (org-link-unescape (org-match-string-no-properties 1))))
9720 (while (string-match " *\n *" link)
9721 (setq link (replace-match " " t t link)))
9722 (setq link (org-link-expand-abbrev link))
9724 ((or (file-name-absolute-p link)
9725 (string-match "^\\.\\.?/" link))
9726 (setq type "file" path link))
9727 ((string-match org-link-re-with-space3 link)
9728 (setq type (match-string 1 link) path (match-string 2 link)))
9729 ((string-match "^help:+\\(.+\\)" link)
9730 (setq type "help" path (match-string 1 link)))
9731 (t (setq type "thisfile" path link)))
9734 (when (get-text-property (point) 'org-linked-text)
9735 (setq type "thisfile"
9736 pos (if (get-text-property (1+ (point)) 'org-linked-text)
9737 (1+ (point)) (point))
9738 path (buffer-substring
9739 (or (previous-single-property-change pos 'org-linked-text)
9741 (or (next-single-property-change pos 'org-linked-text)
9746 (let ((plinkpos (org-in-regexp org-plain-link-re)))
9747 (when (or (org-in-regexp org-angle-link-re)
9748 (and plinkpos (goto-char (car plinkpos))
9749 (save-match-data (not (looking-back "\\[\\[")))))
9750 (setq type (match-string 1)
9751 path (org-link-unescape (match-string 2)))
9754 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
9756 path (match-string 1))
9757 (while (string-match ":" path)
9758 (setq path (replace-match "+" t t path)))
9760 (when (org-in-regexp "<\\([^><\n]+\\)>")
9761 (setq type "tree-match"
9762 path (match-string 1))
9765 (user-error "No link found"))
9767 ;; switch back to reference buffer
9768 ;; needed when if called in a temporary buffer through
9769 ;; org-open-link-from-string
9770 (with-current-buffer (or reference-buffer (current-buffer))
9772 ;; Remove any trailing spaces in path
9773 (if (string-match " +\\'" path)
9774 (setq path (replace-match "" t t path)))
9775 (if (and org-link-translation-function
9776 (fboundp org-link-translation-function))
9777 ;; Check if we need to translate the link
9778 (let ((tmp (funcall org-link-translation-function type path)))
9779 (setq type (car tmp) path (cdr tmp))))
9783 ((assoc type org-link-protocols)
9784 (funcall (nth 1 (assoc type org-link-protocols)) path))
9786 ((equal type "help")
9787 (let ((f-or-v (intern path)))
9788 (cond ((fboundp f-or-v)
9789 (describe-function f-or-v))
9791 (describe-variable f-or-v))
9792 (t (error "Not a known function or variable")))))
9794 ((equal type "mailto")
9795 (let ((cmd (car org-link-mailto-program))
9796 (args (cdr org-link-mailto-program)) args1
9797 (address path) (subject "") a)
9798 (if (string-match "\\(.*\\)::\\(.*\\)" path)
9799 (setq address (match-string 1 path)
9800 subject (org-link-escape (match-string 2 path))))
9803 ((not (stringp (car args))) (push (pop args) args1))
9804 (t (setq a (pop args))
9805 (if (string-match "%a" a)
9806 (setq a (replace-match address t t a)))
9807 (if (string-match "%s" a)
9808 (setq a (replace-match subject t t a)))
9810 (apply cmd (nreverse args1))))
9812 ((member type '("http" "https" "ftp" "news"))
9813 (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
9815 path org-link-escape-chars-browser)
9818 ((string= type "doi")
9819 (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
9821 path org-link-escape-chars-browser)
9824 ((member type '("message"))
9825 (browse-url (concat type ":" path)))
9827 ((string= type "tags")
9828 (org-tags-view arg path))
9830 ((string= type "tree-match")
9831 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
9833 ((string= type "file")
9834 (if (string-match "::\\([0-9]+\\)\\'" path)
9835 (setq line (string-to-number (match-string 1 path))
9836 path (substring path 0 (match-beginning 0)))
9837 (if (string-match "::\\(.+\\)\\'" path)
9838 (setq search (match-string 1 path)
9839 path (substring path 0 (match-beginning 0)))))
9840 (if (string-match "[*?{]" (file-name-nondirectory path))
9842 (org-open-file path arg line search)))
9844 ((string= type "shell")
9845 (let ((buf (generate-new-buffer "*Org Shell Output"))
9847 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
9848 (string-match org-confirm-shell-link-not-regexp cmd))
9849 (not org-confirm-shell-link-function)
9850 (funcall org-confirm-shell-link-function
9851 (format "Execute \"%s\" in shell? "
9852 (org-add-props cmd nil
9853 'face 'org-warning))))
9855 (message "Executing %s" cmd)
9856 (shell-command cmd buf)
9857 (if (featurep 'midnight)
9858 (setq clean-buffer-list-kill-buffer-names
9859 (cons buf clean-buffer-list-kill-buffer-names))))
9862 ((string= type "elisp")
9864 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
9865 (string-match org-confirm-elisp-link-not-regexp cmd))
9866 (not org-confirm-elisp-link-function)
9867 (funcall org-confirm-elisp-link-function
9868 (format "Execute \"%s\" as elisp? "
9869 (org-add-props cmd nil
9870 'face 'org-warning))))
9871 (message "%s => %s" cmd
9872 (if (equal (string-to-char cmd) ?\()
9874 (call-interactively (read cmd))))
9877 ((and (string= type "thisfile")
9878 (run-hook-with-args-until-success
9879 'org-open-link-functions path)))
9881 ((string= type "thisfile")
9883 (switch-to-buffer-other-window
9884 (org-get-buffer-for-internal-link (current-buffer)))
9885 (org-mark-ring-push))
9886 (let ((cmd `(org-link-search
9888 ,(cond ((equal arg '(4)) ''occur)
9889 ((equal arg '(16)) ''org-occur))
9891 (condition-case nil (let ((org-link-search-inhibit-query t))
9893 (error (progn (widen) (eval cmd))))))
9895 (t (browse-url-at-point)))))))
9896 (move-marker org-open-link-marker nil)
9897 (run-hook-with-args 'org-follow-link-hook)))
9899 (defun org-offer-links-in-entry (buffer marker &optional nth zero)
9900 "Offer links in the current entry and return the selected link.
9901 If there is only one link, return it.
9902 If NTH is an integer, return the NTH link found.
9903 If ZERO is a string, check also this string for a link, and if
9904 there is one, return it."
9905 (with-current-buffer buffer
9910 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
9911 "\\(" org-angle-link-re "\\)\\|"
9912 "\\(" org-plain-link-re "\\)"))
9914 (in-emacs (if (integerp nth) nil nth))
9915 have-zero end links link c)
9916 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
9917 (push (match-string 0 zero) links)
9918 (setq cnt (1- cnt) have-zero t))
9920 (org-back-to-heading t)
9921 (setq end (save-excursion (outline-next-heading) (point)))
9922 (while (re-search-forward re end t)
9923 (push (match-string 0) links))
9924 (setq links (org-uniquify (reverse links))))
9927 (message "No links"))
9928 ((equal (length links) 1)
9929 (setq link (car links)))
9930 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
9931 (setq link (nth (if have-zero nth (1- nth)) links)))
9932 (t ; we have to select a link
9934 (save-window-excursion
9935 (delete-other-windows)
9936 (with-output-to-temp-buffer "*Select Link*"
9938 (if (not (string-match org-bracket-link-regexp l))
9939 (princ (format "[%c] %s\n" (incf cnt)
9940 (org-remove-angle-brackets l)))
9942 (princ (format "[%c] %s (%s)\n" (incf cnt)
9943 (match-string 3 l) (match-string 1 l)))
9944 (princ (format "[%c] %s\n" (incf cnt)
9945 (match-string 1 l))))))
9947 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
9948 (message "Select link to open, RET to open all:")
9949 (setq c (read-char-exclusive))
9950 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
9951 (when (equal c ?q) (error "Abort"))
9955 (if have-zero (setq nth (1+ nth)))
9956 (unless (and (integerp nth) (>= (length links) nth))
9957 (error "Invalid link selection"))
9958 (setq link (nth (1- nth) links)))))
9959 (cons link end))))))
9961 ;; Add special file links that specify the way of opening
9963 (org-add-link-type "file+sys" 'org-open-file-with-system)
9964 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
9965 (defun org-open-file-with-system (path)
9966 "Open file at PATH using the system way of opening it."
9967 (org-open-file path 'system))
9968 (defun org-open-file-with-emacs (path)
9969 "Open file at PATH in Emacs."
9970 (org-open-file path 'emacs))
9971 (defun org-remove-file-link-modifiers ()
9972 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
9973 (goto-char (point-min))
9974 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
9976 (replace-match "file:" t t))))
9977 (eval-after-load "org-exp"
9978 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
9979 'org-remove-file-link-modifiers))
9983 (defvar org-create-file-search-functions nil
9984 "List of functions to construct the right search string for a file link.
9985 These functions are called in turn with point at the location to
9986 which the link should point.
9988 A function in the hook should first test if it would like to
9989 handle this file type, for example by checking the `major-mode'
9990 or the file extension. If it decides not to handle this file, it
9991 should just return nil to give other functions a chance. If it
9992 does handle the file, it must return the search string to be used
9993 when following the link. The search string will be part of the
9994 file link, given after a double colon, and `org-open-at-point'
9995 will automatically search for it. If special measures must be
9996 taken to make the search successful, another function should be
9997 added to the companion hook `org-execute-file-search-functions',
10000 A function in this hook may also use `setq' to set the variable
10001 `description' to provide a suggestion for the descriptive text to
10002 be used for this link when it gets inserted into an Org-mode
10003 buffer with \\[org-insert-link].")
10005 (defvar org-execute-file-search-functions nil
10006 "List of functions to execute a file search triggered by a link.
10008 Functions added to this hook must accept a single argument, the
10009 search string that was part of the file link, the part after the
10010 double colon. The function must first check if it would like to
10011 handle this search, for example by checking the `major-mode' or
10012 the file extension. If it decides not to handle this search, it
10013 should just return nil to give other functions a chance. If it
10014 does handle the search, it must return a non-nil value to keep
10015 other functions from trying.
10017 Each function can access the current prefix argument through the
10018 variable `current-prefix-argument'. Note that a single prefix is
10019 used to force opening a link in Emacs, so it may be good to only
10020 use a numeric or double prefix to guide the search function.
10022 In case this is needed, a function in this hook can also restore
10023 the window configuration before `org-open-at-point' was called using:
10025 (set-window-configuration org-window-config-before-follow-link)")
10027 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
10028 (defun org-link-search (s &optional type avoid-pos stealth)
10029 "Search for a link search option.
10030 If S is surrounded by forward slashes, it is interpreted as a
10031 regular expression. In org-mode files, this will create an `org-occur'
10032 sparse tree. In ordinary files, `occur' will be used to list matches.
10033 If the current buffer is in `dired-mode', grep will be used to search
10034 in all files. If AVOID-POS is given, ignore matches near that position.
10036 When optional argument STEALTH is non-nil, do not modify
10037 visibility around point, thus ignoring
10038 `org-show-hierarchy-above', `org-show-following-heading' and
10039 `org-show-siblings' variables."
10040 (let ((case-fold-search t)
10041 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
10042 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
10043 (append '(("") (" ") ("\t") ("\n"))
10044 org-emphasis-alist)
10047 (pre nil) (post nil)
10048 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
10050 ;; First check if there are any special search functions
10051 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
10052 ;; Now try the builtin stuff
10053 ((and (equal (string-to-char s0) ?#)
10056 (goto-char (point-min))
10059 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
10060 (setq type 'dedicated
10061 pos (match-beginning 0))))
10062 ;; There is an exact target for this
10064 (org-back-to-heading t)))
10066 (goto-char (point-min))
10069 (concat "<<" (regexp-quote s0) ">>") nil t)
10070 (setq type 'dedicated
10071 pos (match-beginning 0))))
10072 ;; There is an exact target for this
10075 (goto-char (point-min))
10078 (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
10079 (setq type 'dedicated pos (match-beginning 0))))
10080 ;; Found an invisible target.
10083 (goto-char (point-min))
10086 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
10087 (setq type 'dedicated pos (match-beginning 0))))
10088 ;; Found an element with a matching #+name affiliated keyword.
10090 ((and (string-match "^(\\(.*\\))$" s0)
10092 (goto-char (point-min))
10095 (concat "[^[]" (regexp-quote
10096 (format org-coderef-label-format
10097 (match-string 1 s0))))
10099 (setq type 'dedicated
10100 pos (1+ (match-beginning 0))))))
10101 ;; There is a coderef target for this
10103 ((string-match "^/\\(.*\\)/$" s)
10104 ;; A regular expression
10106 ((derived-mode-p 'org-mode)
10107 (org-occur (match-string 1 s)))
10108 ;;((eq major-mode 'dired-mode)
10109 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
10110 (t (org-do-occur (match-string 1 s)))))
10111 ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
10112 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
10113 (goto-char (point-min))
10115 ((let (case-fold-search)
10116 (re-search-forward (format org-complex-heading-regexp-format
10119 ;; OK, found a match
10120 (setq type 'dedicated)
10121 (goto-char (match-beginning 0)))
10122 ((and (not org-link-search-inhibit-query)
10123 (eq org-link-search-must-match-exact-headline 'query-to-create)
10124 (y-or-n-p "No match - create this as a new heading? "))
10125 (goto-char (point-max))
10126 (or (bolp) (newline))
10127 (insert "* " s "\n")
10128 (beginning-of-line 0))
10131 (error "No match"))))
10133 ;; A normal search string
10134 (when (equal (string-to-char s) ?*)
10135 ;; Anchor on headlines, post may include tags.
10136 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
10137 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
10138 s (substring s 1)))
10139 (remove-text-properties
10141 '(face nil mouse-face nil keymap nil fontified nil) s)
10142 ;; Make a series of regular expressions to find a match
10143 (setq words (org-split-string s "[ \n\r\t]+")
10145 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
10146 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
10148 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
10149 re2a (concat "[ \t\r\n]" re2a_)
10150 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
10151 re4 (concat "[^a-zA-Z_]" re4_)
10153 re1 (concat pre re2 post)
10154 re3 (concat pre (if pre re4_ re4) post)
10155 re5 (concat pre ".*" re4)
10156 re2 (concat pre re2)
10157 re2a (concat pre (if pre re2a_ re2a))
10158 re4 (concat pre (if pre re4_ re4))
10159 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
10160 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
10164 ((eq type 'org-occur) (org-occur reall))
10165 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
10166 (t (goto-char (point-min))
10168 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
10169 (org-search-not-self 1 re1 nil t)
10170 (org-search-not-self 1 re2 nil t)
10171 (org-search-not-self 1 re2a nil t)
10172 (org-search-not-self 1 re3 nil t)
10173 (org-search-not-self 1 re4 nil t)
10174 (org-search-not-self 1 re5 nil t)
10176 (goto-char (match-beginning 1))
10178 (error "No match"))))))
10179 (and (derived-mode-p 'org-mode)
10181 (org-show-context 'link-search))
10184 (defun org-search-not-self (group &rest args)
10185 "Execute `re-search-forward', but only accept matches that do not
10186 enclose the position of `org-open-link-marker'."
10187 (let ((m org-open-link-marker))
10189 (while (apply 're-search-forward args)
10190 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
10191 (goto-char (match-end group))
10192 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
10193 (> (match-beginning 0) (marker-position m))
10194 (< (match-end 0) (marker-position m)))
10196 (or (not (org-in-regexp
10197 org-bracket-link-analytic-regexp 1))
10198 (not (match-end 4)) ; no description
10199 (and (<= (match-beginning 4) (point))
10200 (>= (match-end 4) (point))))))
10201 (throw 'exit (point))))))))
10203 (defun org-get-buffer-for-internal-link (buffer)
10204 "Return a buffer to be used for displaying the link target of internal links."
10206 ((not org-display-internal-link-with-indirect-buffer)
10208 ((string-match "(Clone)$" (buffer-name buffer))
10209 (message "Buffer is already a clone, not making another one")
10210 ;; we also do not modify visibility in this case
10212 (t ; make a new indirect buffer for displaying the link
10213 (let* ((bn (buffer-name buffer))
10214 (ibn (concat bn "(Clone)"))
10215 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
10216 (with-current-buffer ib (org-overview))
10219 (defun org-do-occur (regexp &optional cleanup)
10220 "Call the Emacs command `occur'.
10221 If CLEANUP is non-nil, remove the printout of the regular expression
10222 in the *Occur* buffer. This is useful if the regex is long and not useful
10226 (let ((cwin (selected-window)) win beg end)
10227 (when (setq win (get-buffer-window "*Occur*"))
10228 (select-window win))
10229 (goto-char (point-min))
10230 (when (re-search-forward "match[a-z]+" nil t)
10231 (setq beg (match-end 0))
10232 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
10233 (setq end (1- (match-beginning 0)))))
10234 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
10235 (goto-char (point-min))
10236 (select-window cwin))))
10238 ;;; The mark ring for links jumps
10240 (defvar org-mark-ring nil
10241 "Mark ring for positions before jumps in Org-mode.")
10242 (defvar org-mark-ring-last-goto nil
10243 "Last position in the mark ring used to go back.")
10244 ;; Fill and close the ring
10245 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
10246 (loop for i from 1 to org-mark-ring-length do
10247 (push (make-marker) org-mark-ring))
10248 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
10251 (defun org-mark-ring-push (&optional pos buffer)
10252 "Put the current position or POS into the mark ring and rotate it."
10254 (setq pos (or pos (point)))
10255 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10256 (move-marker (car org-mark-ring)
10258 (or buffer (current-buffer)))
10260 (substitute-command-keys
10261 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10263 (defun org-mark-ring-goto (&optional n)
10264 "Jump to the previous position in the mark ring.
10265 With prefix arg N, jump back that many stored positions. When
10266 called several times in succession, walk through the entire ring.
10267 Org-mode commands jumping to a different position in the current file,
10268 or to another Org-mode file, automatically push the old position
10272 (if (eq last-command this-command)
10273 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10274 (setq p org-mark-ring))
10275 (setq org-mark-ring-last-goto p)
10277 (org-pop-to-buffer-same-window (marker-buffer m))
10279 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
10281 (defun org-remove-angle-brackets (s)
10282 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
10283 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
10285 (defun org-add-angle-brackets (s)
10286 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
10287 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
10289 (defun org-remove-double-quotes (s)
10290 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
10291 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
10294 ;;; Following specific links
10296 (defun org-follow-timestamp-link ()
10297 "Open an agenda view for the time-stamp date/range at point."
10299 ((org-at-date-range-p t)
10300 (let ((org-agenda-start-on-weekday)
10301 (t1 (match-string 1))
10302 (t2 (match-string 2)) tt1 tt2)
10303 (setq tt1 (time-to-days (org-time-string-to-time t1))
10304 tt2 (time-to-days (org-time-string-to-time t2)))
10305 (let ((org-agenda-buffer-tmp-name
10306 (format "*Org Agenda(a:%s)"
10307 (concat (substring t1 0 10) "--" (substring t2 0 10)))))
10308 (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
10309 ((org-at-timestamp-p t)
10310 (let ((org-agenda-buffer-tmp-name
10311 (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
10312 (org-agenda-list nil (time-to-days (org-time-string-to-time
10313 (substring (match-string 1) 0 10)))
10315 (t (error "This should not happen"))))
10318 ;;; Following file links
10319 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
10320 (declare-function mailcap-extension-to-mime "mailcap" (extn))
10321 (declare-function mailcap-mime-info
10322 "mailcap" (string &optional request no-decode))
10323 (defvar org-wait nil)
10324 (defun org-open-file (path &optional in-emacs line search)
10325 "Open the file at PATH.
10326 First, this expands any special file name abbreviations. Then the
10327 configuration variable `org-file-apps' is checked if it contains an
10328 entry for this file type, and if yes, the corresponding command is launched.
10330 If no application is found, Emacs simply visits the file.
10332 With optional prefix argument IN-EMACS, Emacs will visit the file.
10333 With a double \\[universal-argument] \\[universal-argument] \
10334 prefix arg, Org tries to avoid opening in Emacs
10335 and to use an external application to visit the file.
10337 Optional LINE specifies a line to go to, optional SEARCH a string
10338 to search for. If LINE or SEARCH is given, the file will be
10339 opened in Emacs, unless an entry from org-file-apps that makes
10340 use of groups in a regexp matches.
10342 If you want to change the way frames are used when following a
10343 link, please customize `org-link-frame-setup'.
10345 If the file does not exist, an error is thrown."
10346 (let* ((file (if (equal path "")
10348 (substitute-in-file-name (expand-file-name path))))
10349 (file-apps (append org-file-apps (org-default-apps)))
10350 (apps (org-remove-if
10351 'org-file-apps-entry-match-against-dlink-p file-apps))
10352 (apps-dlink (org-remove-if-not
10353 'org-file-apps-entry-match-against-dlink-p file-apps))
10354 (remp (and (assq 'remote apps) (org-file-remote-p file)))
10355 (dirp (if remp nil (file-directory-p file)))
10356 (file (if (and dirp org-open-directory-means-index-dot-org)
10357 (concat (file-name-as-directory file) "index.org")
10359 (a-m-a-p (assq 'auto-mode apps))
10360 (dfile (downcase file))
10361 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
10362 (link (cond ((and (eq line nil)
10366 (concat file "::" (number-to-string line)))
10368 (concat file "::" search))))
10369 (dlink (downcase link))
10370 (old-buffer (current-buffer))
10372 (old-mode major-mode)
10373 ext cmd link-match-data)
10374 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
10375 (setq ext (match-string 1 dfile))
10376 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
10377 (setq ext (match-string 1 dfile))))
10379 ((member in-emacs '((16) system))
10380 (setq cmd (cdr (assoc 'system apps))))
10381 (in-emacs (setq cmd 'emacs))
10383 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
10384 (and dirp (cdr (assoc 'directory apps)))
10385 ; first, try matching against apps-dlink
10386 ; if we get a match here, store the match data for later
10387 (let ((match (assoc-default dlink apps-dlink
10390 (progn (setq link-match-data (match-data))
10392 (progn (setq in-emacs (or in-emacs line search))
10393 nil))) ; if we have no match in apps-dlink,
10394 ; always open the file in emacs if line or search
10395 ; is given (for backwards compatibility)
10396 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
10398 (cdr (assoc ext apps))
10399 (cdr (assoc t apps))))))
10400 (when (eq cmd 'system)
10401 (setq cmd (cdr (assoc 'system apps))))
10402 (when (eq cmd 'default)
10403 (setq cmd (cdr (assoc t apps))))
10404 (when (eq cmd 'mailcap)
10406 (mailcap-parse-mailcaps)
10407 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
10408 (command (mailcap-mime-info mime-type)))
10409 (if (stringp command)
10411 (setq cmd 'emacs))))
10412 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
10413 (not (file-exists-p file))
10414 (not org-open-non-existing-files))
10415 (error "No such file: %s" file))
10417 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
10418 ;; Remove quotes around the file name - we'll use shell-quote-argument.
10419 (while (string-match "['\"]%s['\"]" cmd)
10420 (setq cmd (replace-match "%s" t t cmd)))
10421 (while (string-match "%s" cmd)
10422 (setq cmd (replace-match
10424 (shell-quote-argument
10425 (convert-standard-filename file)))
10428 ;; Replace "%1", "%2" etc. in command with group matches from regex
10430 (let ((match-index 1)
10431 (number-of-groups (- (/ (length link-match-data) 2) 1)))
10432 (set-match-data link-match-data)
10433 (while (<= match-index number-of-groups)
10434 (let ((regex (concat "%" (number-to-string match-index)))
10435 (replace-with (match-string match-index dlink)))
10436 (while (string-match regex cmd)
10437 (setq cmd (replace-match replace-with t t cmd))))
10438 (setq match-index (+ match-index 1)))))
10440 (save-window-excursion
10441 (start-process-shell-command cmd nil cmd)
10442 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
10446 (funcall (cdr (assq 'file org-link-frame-setup)) file)
10448 (if line (org-goto-line line)
10449 (if search (org-link-search search))))
10451 (let ((file (convert-standard-filename file)))
10453 (set-match-data link-match-data)
10455 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
10456 (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
10457 (or (not (equal old-buffer (current-buffer)))
10458 (not (equal old-pos (point))))
10459 (org-mark-ring-push old-pos old-buffer))))
10461 (defun org-file-apps-entry-match-against-dlink-p (entry)
10462 "This function returns non-nil if `entry' uses a regular
10463 expression which should be matched against the whole link by
10466 It assumes that is the case when the entry uses a regular
10467 expression which has at least one grouping construct and the
10468 action is either a lisp form or a command string containing
10469 '%1', i.e. using at least one subexpression match as a
10471 (let ((selector (car entry))
10472 (action (cdr entry)))
10473 (if (stringp selector)
10474 (and (> (regexp-opt-depth selector) 0)
10475 (or (and (stringp action)
10476 (string-match "%[0-9]" action))
10480 (defun org-default-apps ()
10481 "Return the default applications for this operating system."
10483 ((eq system-type 'darwin)
10484 org-file-apps-defaults-macosx)
10485 ((eq system-type 'windows-nt)
10486 org-file-apps-defaults-windowsnt)
10487 (t org-file-apps-defaults-gnu)))
10489 (defun org-apps-regexp-alist (list &optional add-auto-mode)
10490 "Convert extensions to regular expressions in the cars of LIST.
10491 Also, weed out any non-string entries, because the return value is used
10492 only for regexp matching.
10493 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
10494 point to the symbol `emacs', indicating that the file should
10495 be opened in Emacs."
10498 (mapcar (lambda (x)
10499 (if (not (stringp (car x)))
10501 (if (string-match "\\W" (car x))
10503 (cons (concat "\\." (car x) "\\'") (cdr x)))))
10506 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
10508 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
10509 (defun org-file-remote-p (file)
10510 "Test whether FILE specifies a location on a remote system.
10511 Return non-nil if the location is indeed remote.
10513 For example, the filename \"/user@host:/foo\" specifies a location
10514 on the system \"/user@host:\"."
10515 (cond ((fboundp 'file-remote-p)
10516 (file-remote-p file))
10517 ((fboundp 'tramp-handle-file-remote-p)
10518 (tramp-handle-file-remote-p file))
10519 ((and (boundp 'ange-ftp-name-format)
10520 (string-match (car ange-ftp-name-format) file))
10526 (defun org-get-org-file ()
10527 "Read a filename, with default directory `org-directory'."
10528 (let ((default (or org-default-notes-file remember-data-file)))
10529 (read-file-name (format "File name [%s]: " default)
10530 (file-name-as-directory org-directory)
10533 (defun org-notes-order-reversed-p ()
10534 "Check if the current file should receive notes in reversed order."
10536 ((not org-reverse-note-order) nil)
10537 ((eq t org-reverse-note-order) t)
10538 ((not (listp org-reverse-note-order)) nil)
10540 (let ((all org-reverse-note-order)
10542 (while (setq entry (pop all))
10543 (if (string-match (car entry) buffer-file-name)
10544 (throw 'exit (cdr entry))))
10547 (defvar org-refile-target-table nil
10548 "The list of refile targets, created by `org-refile'.")
10550 (defvar org-agenda-new-buffers nil
10551 "Buffers created to visit agenda files.")
10553 (defvar org-refile-cache nil
10554 "Cache for refile targets.")
10556 (defvar org-refile-markers nil
10557 "All the markers used for caching refile locations.")
10559 (defun org-refile-marker (pos)
10560 "Get a new refile marker, but only if caching is in use."
10561 (if (not org-refile-use-cache)
10563 (let ((m (make-marker)))
10564 (move-marker m pos)
10565 (push m org-refile-markers)
10568 (defun org-refile-cache-clear ()
10569 "Clear the refile cache and disable all the markers."
10570 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
10571 (setq org-refile-markers nil)
10572 (setq org-refile-cache nil)
10573 (message "Refile cache has been cleared"))
10575 (defun org-refile-cache-check-set (set)
10576 "Check if all the markers in the cache still have live buffers."
10579 (while (and set (setq marker (nth 3 (pop set))))
10580 ;; if org-refile-use-outline-path is 'file, marker may be nil
10581 (when (and marker (null (marker-buffer marker)))
10582 (message "not found") (sit-for 3)
10583 (throw 'exit nil)))
10586 (defun org-refile-cache-put (set &rest identifiers)
10587 "Push the refile targets SET into the cache, under IDENTIFIERS."
10588 (let* ((key (sha1 (prin1-to-string identifiers)))
10589 (entry (assoc key org-refile-cache)))
10592 (push (cons key set) org-refile-cache))))
10594 (defun org-refile-cache-get (&rest identifiers)
10595 "Retrieve the cached value for refile targets given by IDENTIFIERS."
10597 ((not org-refile-cache) nil)
10598 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
10600 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
10601 org-refile-cache))))
10602 (and set (org-refile-cache-check-set set) set)))))
10604 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
10605 "Produce a table with refile targets."
10606 (let ((case-fold-search nil)
10607 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
10608 (entries (or org-refile-targets '((nil . (:level . 1)))))
10609 targets tgs txt re files f desc descre fast-path-p level pos0)
10610 (message "Getting targets...")
10611 (with-current-buffer (or default-buffer (current-buffer))
10612 (while (setq entry (pop entries))
10613 (setq files (car entry) desc (cdr entry))
10614 (setq fast-path-p nil)
10616 ((null files) (setq files (list (current-buffer))))
10617 ((eq files 'org-agenda-files)
10618 (setq files (org-agenda-files 'unrestricted)))
10619 ((and (symbolp files) (fboundp files))
10620 (setq files (funcall files)))
10621 ((and (symbolp files) (boundp files))
10622 (setq files (symbol-value files))))
10623 (if (stringp files) (setq files (list files)))
10625 ((eq (car desc) :tag)
10626 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
10627 ((eq (car desc) :todo)
10628 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
10629 ((eq (car desc) :regexp)
10630 (setq descre (cdr desc)))
10631 ((eq (car desc) :level)
10632 (setq descre (concat "^\\*\\{" (number-to-string
10633 (if org-odd-levels-only
10634 (1- (* 2 (cdr desc)))
10637 ((eq (car desc) :maxlevel)
10638 (setq fast-path-p t)
10639 (setq descre (concat "^\\*\\{1," (number-to-string
10640 (if org-odd-levels-only
10641 (1- (* 2 (cdr desc)))
10644 (t (error "Bad refiling target description %s" desc)))
10645 (while (setq f (pop files))
10646 (with-current-buffer
10647 (if (bufferp f) f (org-get-agenda-file-buffer f))
10649 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
10651 (if (bufferp f) (setq f (buffer-file-name
10652 (buffer-base-buffer f))))
10653 (setq f (and f (expand-file-name f)))
10654 (if (eq org-refile-use-outline-path 'file)
10655 (push (list (file-name-nondirectory f) f nil nil) tgs))
10659 (goto-char (point-min))
10660 (while (re-search-forward descre nil t)
10661 (goto-char (setq pos0 (point-at-bol)))
10663 (when org-refile-target-verify-function
10665 (or (funcall org-refile-target-verify-function)
10667 (when (and (looking-at org-complex-heading-regexp)
10668 (not (member (match-string 4) excluded-entries))
10670 (setq level (org-reduced-level
10671 (- (match-end 1) (match-beginning 1)))
10672 txt (org-link-display-format (match-string 4))
10673 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
10674 re (format org-complex-heading-regexp-format
10675 (regexp-quote (match-string 4))))
10676 (when org-refile-use-outline-path
10677 (setq txt (mapconcat
10680 (if (eq org-refile-use-outline-path
10682 (list (file-name-nondirectory
10684 (buffer-base-buffer))))
10685 (if (eq org-refile-use-outline-path
10687 (list (buffer-file-name
10688 (buffer-base-buffer)))))
10689 (org-get-outline-path fast-path-p
10693 (push (list txt f re (org-refile-marker (point)))
10695 (when (= (point) pos0)
10696 ;; verification function has not moved point
10697 (goto-char (point-at-eol))))))))
10698 (when org-refile-use-cache
10699 (org-refile-cache-put tgs (buffer-file-name) descre))
10700 (setq targets (append tgs targets))
10702 (message "Getting targets...done")
10703 (nreverse targets)))
10705 (defun org-protect-slash (s)
10706 (while (string-match "/" s)
10707 (setq s (replace-match "\\" t t s)))
10710 (defvar org-olpa (make-vector 20 nil))
10712 (defun org-get-outline-path (&optional fastp level heading)
10713 "Return the outline path to the current entry, as a list.
10715 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
10716 routine which makes outline path derivations for an entire file,
10717 avoiding backtracing. Refile target collection makes use of that."
10721 (error "Outline path failure, more than 19 levels"))
10722 (loop for i from level upto 19 do
10723 (aset org-olpa i nil))
10725 (delq nil (append org-olpa nil))
10726 (aset org-olpa level heading)))
10727 (let (rtn case-fold-search)
10731 (while (org-up-heading-safe)
10732 (when (looking-at org-complex-heading-regexp)
10733 (push (org-match-string-no-properties 4) rtn)))
10736 (defun org-format-outline-path (path &optional width prefix)
10737 "Format the outline path PATH for display.
10738 Width is the maximum number of characters that is available.
10739 Prefix is a prefix to be included in the returned string,
10740 such as the file name."
10741 (setq width (or width 79))
10742 (if prefix (setq width (- width (length prefix))))
10745 (let* ((nsteps (length path))
10746 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
10747 (maxwidth (if (<= total-width width)
10748 10000 ;; everything fits
10749 ;; we need to shorten the level headings
10750 (/ (- width nsteps) nsteps)))
10751 (org-odd-levels-only nil)
10753 (total (1+ (length prefix))))
10754 (setq maxwidth (max maxwidth 10))
10759 (if (and (= n nsteps) (< maxwidth 10000))
10760 (setq maxwidth (- total-width total)))
10761 (if (< (length h) maxwidth)
10762 (progn (setq total (+ total (length h) 1)) h)
10763 (setq h (substring h 0 (- maxwidth 2))
10764 total (+ total maxwidth 1))
10765 (if (string-match "[ \t]+\\'" h)
10766 (setq h (substring h 0 (match-beginning 0))))
10767 (setq h (concat h "..")))
10768 (org-add-props h nil 'face
10769 (nth (% (1- n) org-n-level-faces)
10774 (defun org-display-outline-path (&optional file current)
10775 "Display the current outline path in the echo area."
10777 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
10778 (case-fold-search nil)
10779 (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
10780 (if current (setq path (append path
10782 (org-back-to-heading t)
10783 (if (looking-at org-complex-heading-regexp)
10784 (list (match-string 4)))))))
10786 (org-format-outline-path
10789 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
10791 (defvar org-refile-history nil
10792 "History for refiling operations.")
10794 (defvar org-after-refile-insert-hook nil
10795 "Hook run after `org-refile' has inserted its stuff at the new location.
10796 Note that this is still *before* the stuff will be removed from
10797 the *old* location.")
10799 (defvar org-capture-last-stored-marker)
10800 (defun org-refile (&optional goto default-buffer rfloc)
10801 "Move the entry or entries at point to another heading.
10802 The list of target headings is compiled using the information in
10803 `org-refile-targets', which see.
10805 At the target location, the entry is filed as a subitem of the target
10806 heading. Depending on `org-reverse-note-order', the new subitem will
10807 either be the first or the last subitem.
10809 If there is an active region, all entries in that region will be moved.
10810 However, the region must fulfill the requirement that the first heading
10811 is the first one sets the top-level of the moved text - at most siblings
10812 below it are allowed.
10814 With prefix arg GOTO, the command will only visit the target location
10815 and not actually move anything.
10817 With a double prefix arg \\[universal-argument] \\[universal-argument], \
10818 go to the location where the last refiling operation has put the subtree.
10819 With a prefix argument of `2', refile to the running clock.
10821 RFLOC can be a refile location obtained in a different way.
10823 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
10825 If you are using target caching (see `org-refile-use-cache'),
10826 you have to clear the target cache in order to find new targets.
10827 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
10828 prefix argument (`C-u C-u C-u C-c C-w')."
10831 (if (member goto '(0 (64)))
10832 (org-refile-cache-clear)
10833 (let* ((cbuf (current-buffer))
10834 (regionp (org-region-active-p))
10835 (region-start (and regionp (region-beginning)))
10836 (region-end (and regionp (region-end)))
10837 (region-length (and regionp (- region-end region-start)))
10838 (filename (buffer-file-name (buffer-base-buffer cbuf)))
10839 pos it nbuf file re level reversed)
10840 (setq last-command nil)
10842 (goto-char region-start)
10843 (or (bolp) (goto-char (point-at-bol)))
10844 (setq region-start (point))
10845 (unless (or (org-kill-is-subtree-p
10846 (buffer-substring region-start region-end))
10847 (prog1 org-refile-active-region-within-subtree
10848 (org-toggle-heading)))
10849 (error "The region is not a (sequence of) subtree(s)")))
10850 (if (equal goto '(16))
10851 (org-refile-goto-last-stored)
10853 (and (equal goto 2)
10854 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
10856 (setq it (list (or org-clock-heading "running clock")
10858 (marker-buffer org-clock-hd-marker))
10860 (marker-position org-clock-hd-marker)))
10863 (let (heading-text)
10866 (org-back-to-heading t)
10868 (nth 4 (org-heading-components))))
10869 (org-refile-get-location
10870 (cond (goto "Goto")
10871 (regionp "Refile region to")
10872 (t (concat "Refile subtree \""
10873 heading-text "\" to")))
10875 (and (not (equal '(4) goto))
10876 org-refile-allow-creating-parent-nodes)
10878 (setq file (nth 1 it)
10881 (if (and (not goto)
10883 (equal (buffer-file-name) file)
10885 (and (>= pos region-start)
10886 (<= pos region-end))
10887 (and (>= pos (point))
10888 (< pos (save-excursion
10889 (org-end-of-subtree t t))))))
10890 (error "Cannot refile to position inside the tree or region"))
10892 (setq nbuf (or (find-buffer-visiting file)
10893 (find-file-noselect file)))
10896 (org-pop-to-buffer-same-window nbuf)
10898 (org-show-context 'org-goto))
10901 (org-kill-new (buffer-substring region-start region-end))
10902 (org-save-markers-in-region region-start region-end))
10903 (org-copy-subtree 1 nil t))
10904 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
10905 (find-file-noselect file)))
10906 (setq reversed (org-notes-order-reversed-p))
10913 (looking-at org-outline-regexp)
10914 (setq level (org-get-valid-level (funcall outline-level) 1))
10917 (or (outline-next-heading) (point-max))
10918 (or (save-excursion (org-get-next-sibling))
10919 (org-end-of-subtree t t)
10923 (goto-char (point-max))
10924 (goto-char (point-min))
10925 (or (outline-next-heading) (goto-char (point-max)))))
10926 (if (not (bolp)) (newline))
10927 (org-paste-subtree level)
10928 (when org-log-refile
10929 (org-add-log-setup 'refile nil nil 'findpos
10931 (unless (eq org-log-refile 'note)
10932 (save-excursion (org-add-log-note))))
10933 (and org-auto-align-tags
10934 (let ((org-loop-over-headlines-in-active-region nil))
10935 (org-set-tags nil t)))
10936 (with-demoted-errors
10937 (bookmark-set "org-refile-last-stored"))
10938 ;; If we are refiling for capture, make sure that the
10939 ;; last-capture pointers point here
10940 (when (org-bound-and-true-p org-refile-for-capture)
10941 (with-demoted-errors
10942 (bookmark-set "org-capture-last-stored-marker"))
10943 (move-marker org-capture-last-stored-marker (point)))
10944 (if (fboundp 'deactivate-mark) (deactivate-mark))
10945 (run-hooks 'org-after-refile-insert-hook))))
10947 (delete-region (point) (+ (point) region-length))
10949 (when (featurep 'org-inlinetask)
10950 (org-inlinetask-remove-END-maybe))
10951 (setq org-markers-to-move nil)
10952 (message "Refiled to \"%s\" in file %s" (car it) file)))))))
10954 (defun org-refile-goto-last-stored ()
10955 "Go to the location where the last refile was stored."
10957 (bookmark-jump "org-refile-last-stored")
10958 (message "This is the location of the last refile"))
10960 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
10962 "Prompt the user for a refile location, using PROMPT.
10963 PROMPT should not be suffixed with a colon and a space, because
10964 this function appends the default value from
10965 `org-refile-history' automatically, if that is not empty.
10966 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
10967 this is used for the GOTO interface."
10968 (let ((org-refile-targets org-refile-targets)
10969 (org-refile-use-outline-path org-refile-use-outline-path)
10971 (when (and (derived-mode-p 'org-mode)
10972 (not org-refile-use-cache)
10976 (setq excluded-entries
10977 (append excluded-entries (list (org-get-heading t t)))))))
10978 (setq org-refile-target-table
10979 (org-refile-get-targets default-buffer excluded-entries)))
10980 (unless org-refile-target-table
10981 (error "No refile targets"))
10982 (let* ((prompt (concat prompt
10983 (and (car org-refile-history)
10984 (concat " (default " (car org-refile-history) ")"))
10986 (cbuf (current-buffer))
10987 (partial-completion-mode nil)
10988 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
10989 (cfunc (if (and org-refile-use-outline-path
10990 org-outline-path-complete-in-steps)
10991 'org-olpath-completing-read
10992 'org-icompleting-read))
10993 (extra (if org-refile-use-outline-path "/" ""))
10994 (filename (and cfn (expand-file-name cfn)))
10997 (if (and (not (member org-refile-use-outline-path
10998 '(file full-file-path)))
10999 (not (equal filename (nth 1 x))))
11000 (cons (concat (car x) extra " ("
11001 (file-name-nondirectory (nth 1 x)) ")")
11003 (cons (concat (car x) extra) (cdr x))))
11004 org-refile-target-table))
11005 (completion-ignore-case t)
11006 pa answ parent-target child parent old-hist)
11007 (setq old-hist org-refile-history)
11008 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
11009 nil 'org-refile-history (car org-refile-history)))
11010 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
11011 (org-refile-check-position pa)
11014 (when (or (not org-refile-history)
11015 (not (eq old-hist org-refile-history))
11016 (not (equal (car pa) (car org-refile-history))))
11017 (setq org-refile-history
11018 (cons (car pa) (if (assoc (car org-refile-history) tbl)
11020 (cdr org-refile-history))))
11021 (if (equal (car org-refile-history) (nth 1 org-refile-history))
11022 (pop org-refile-history)))
11024 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
11026 (setq parent (match-string 1 answ)
11027 child (match-string 2 answ))
11028 (setq parent-target (or (assoc parent tbl)
11029 (assoc (concat parent "/") tbl)))
11030 (when (and parent-target
11031 (or (eq new-nodes t)
11032 (and (eq new-nodes 'confirm)
11033 (y-or-n-p (format "Create new node \"%s\"? "
11035 (org-refile-new-child parent-target child)))
11036 (error "Invalid target location")))))
11038 (declare-function org-string-nw-p "org-macs" (s))
11039 (defun org-refile-check-position (refile-pointer)
11040 "Check if the refile pointer matches the headline to which it points."
11041 (let* ((file (nth 1 refile-pointer))
11042 (re (nth 2 refile-pointer))
11043 (pos (nth 3 refile-pointer))
11045 (if (and (not (markerp pos)) (not file))
11046 (error "Please save the buffer to a file before refiling")
11047 (when (org-string-nw-p re)
11048 (setq buffer (if (markerp pos)
11049 (marker-buffer pos)
11050 (or (find-buffer-visiting file)
11051 (find-file-noselect file))))
11052 (with-current-buffer buffer
11057 (beginning-of-line 1)
11058 (unless (org-looking-at-p re)
11059 (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
11061 (defun org-refile-new-child (parent-target child)
11062 "Use refile target PARENT-TARGET to add new CHILD below it."
11063 (unless parent-target
11064 (error "Cannot find parent for new node"))
11065 (let ((file (nth 1 parent-target))
11066 (pos (nth 3 parent-target))
11068 (with-current-buffer (or (find-buffer-visiting file)
11069 (find-file-noselect file))
11075 (goto-char (point-max))
11076 (if (not (bolp)) (newline)))
11077 (when (looking-at org-outline-regexp)
11078 (setq level (funcall outline-level))
11079 (org-end-of-subtree t t))
11080 (org-back-over-empty-lines)
11081 (insert "\n" (make-string
11082 (if pos (org-get-valid-level level 1) 1) ?*)
11084 (beginning-of-line 0)
11085 (list (concat (car parent-target) "/" child) file "" (point)))))))
11087 (defun org-olpath-completing-read (prompt collection &rest args)
11088 "Read an outline path like a file name."
11089 (let ((thetable collection)
11090 (org-completion-use-ido nil) ; does not work with ido.
11091 (org-completion-use-iswitchb nil)) ; or iswitchb
11093 'org-icompleting-read prompt
11094 (lambda (string predicate &optional flag)
11095 (let (rtn r f (l (length string)))
11099 (try-completion string thetable))
11102 (setq rtn (all-completions string thetable predicate))
11105 (setq r (substring x l))
11106 (if (string-match " ([^)]*)$" x)
11107 (setq f (match-string 0 x))
11109 (if (string-match "/" r)
11110 (concat string (substring r 0 (match-end 0)) f)
11115 (assoc string thetable)))))
11118 ;;;; Dynamic blocks
11120 (defun org-find-dblock (name)
11121 "Find the first dynamic block with name NAME in the buffer.
11122 If not found, stay at current position and return nil."
11123 (let ((case-fold-search t) pos)
11125 (goto-char (point-min))
11126 (setq pos (and (re-search-forward
11127 (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
11128 (match-beginning 0))))
11129 (if pos (goto-char pos))
11132 (defconst org-dblock-start-re
11133 "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
11134 "Matches the start line of a dynamic block, with parameters.")
11136 (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
11137 "Matches the end of a dynamic block.")
11139 (defun org-create-dblock (plist)
11140 "Create a dynamic block section, with parameters taken from PLIST.
11141 PLIST must contain a :name entry which is used as name of the block."
11142 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
11145 (let ((col (current-column))
11146 (name (plist-get plist :name)))
11147 (insert "#+BEGIN: " name)
11149 (if (eq (car plist) :name)
11150 (setq plist (cddr plist))
11151 (insert " " (prin1-to-string (pop plist)))))
11152 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
11153 (beginning-of-line -2)))
11155 (defun org-prepare-dblock ()
11156 "Prepare dynamic block for refresh.
11157 This empties the block, puts the cursor at the insert position and returns
11158 the property list including an extra property :name with the block name."
11159 (unless (looking-at org-dblock-start-re)
11160 (error "Not at a dynamic block"))
11161 (let* ((begdel (1+ (match-end 0)))
11162 (name (org-no-properties (match-string 1)))
11163 (params (append (list :name name)
11164 (read (concat "(" (match-string 3) ")")))))
11166 (beginning-of-line 1)
11167 (skip-chars-forward " \t")
11168 (setq params (plist-put params :indentation-column (current-column))))
11169 (unless (re-search-forward org-dblock-end-re nil t)
11170 (error "Dynamic block not terminated"))
11173 (list :content (buffer-substring
11174 begdel (match-beginning 0)))))
11175 (delete-region begdel (match-beginning 0))
11180 (defun org-map-dblocks (&optional command)
11181 "Apply COMMAND to all dynamic blocks in the current buffer.
11182 If COMMAND is not given, use `org-update-dblock'."
11183 (let ((cmd (or command 'org-update-dblock)))
11185 (goto-char (point-min))
11186 (while (re-search-forward org-dblock-start-re nil t)
11187 (goto-char (match-beginning 0))
11189 (condition-case nil
11191 (error (message "Error during update of dynamic block"))))
11192 (unless (re-search-forward org-dblock-end-re nil t)
11193 (error "Dynamic block not terminated"))))))
11195 (defun org-dblock-update (&optional arg)
11196 "User command for updating dynamic blocks.
11197 Update the dynamic block at point. With prefix ARG, update all dynamic
11198 blocks in the buffer."
11201 (org-update-all-dblocks)
11202 (or (looking-at org-dblock-start-re)
11203 (org-beginning-of-dblock))
11204 (org-update-dblock)))
11206 (defun org-update-dblock ()
11207 "Update the dynamic block at point.
11208 This means to empty the block, parse for parameters and then call
11209 the correct writing function."
11211 (save-window-excursion
11212 (let* ((pos (point))
11213 (line (org-current-line))
11214 (params (org-prepare-dblock))
11215 (name (plist-get params :name))
11216 (indent (plist-get params :indentation-column))
11217 (cmd (intern (concat "org-dblock-write:" name))))
11218 (message "Updating dynamic block `%s' at line %d..." name line)
11219 (funcall cmd params)
11220 (message "Updating dynamic block `%s' at line %d...done" name line)
11222 (when (and indent (> indent 0))
11223 (setq indent (make-string indent ?\ ))
11225 (org-beginning-of-dblock)
11227 (while (not (looking-at org-dblock-end-re))
11229 (beginning-of-line 2))
11230 (when (looking-at org-dblock-end-re)
11231 (and (looking-at "[ \t]+")
11232 (replace-match ""))
11233 (insert indent)))))))
11235 (defun org-beginning-of-dblock ()
11236 "Find the beginning of the dynamic block at point.
11237 Error if there is no such block at point."
11238 (let ((pos (point))
11241 (if (and (re-search-backward org-dblock-start-re nil t)
11242 (setq beg (match-beginning 0))
11243 (re-search-forward org-dblock-end-re nil t)
11244 (> (match-end 0) pos))
11247 (error "Not in a dynamic block"))))
11249 (defun org-update-all-dblocks ()
11250 "Update all dynamic blocks in the buffer.
11251 This function can be used in a hook."
11253 (when (derived-mode-p 'org-mode)
11254 (org-map-dblocks 'org-update-dblock)))
11259 (defconst org-additional-option-like-keywords
11260 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
11261 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
11262 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
11263 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
11265 "ORGTBL" "TBLFM:" "TBLNAME:"
11266 "BEGIN_EXAMPLE" "END_EXAMPLE"
11267 "BEGIN_VERBATIM" "END_VERBATIM"
11268 "BEGIN_QUOTE" "END_QUOTE"
11269 "BEGIN_VERSE" "END_VERSE"
11270 "BEGIN_CENTER" "END_CENTER"
11271 "BEGIN_SRC" "END_SRC"
11272 "BEGIN_RESULT" "END_RESULT"
11273 "BEGIN_lstlisting" "END_lstlisting"
11275 "HEADER:" "HEADERS:"
11276 "COLUMNS:" "PROPERTY:"
11277 "CAPTION:" "LABEL:"
11279 "INCLUDE:" "INDEX:"
11283 (defconst org-options-keywords
11284 '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
11285 "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
11286 "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
11287 "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
11288 "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
11289 "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
11290 "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
11292 (defconst org-additional-option-like-keywords-for-flyspell
11295 (mapconcat (lambda(k)
11296 (replace-regexp-in-string
11298 (concat k " " (downcase k) " " (upcase k))))
11299 (append org-options-keywords org-additional-option-like-keywords)
11303 (defcustom org-structure-template-alist
11304 '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
11305 "<src lang=\"?\">\n\n</src>")
11306 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
11307 "<example>\n?\n</example>")
11308 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
11309 "<quote>\n?\n</quote>")
11310 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
11311 "<verse>\n?\n</verse>")
11312 ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
11313 "<verbatim>\n?\n</verbatim>")
11314 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
11315 "<center>\n?\n</center>")
11316 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
11317 "<literal style=\"latex\">\n?\n</literal>")
11319 "<literal style=\"latex\">?</literal>")
11320 ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
11321 "<literal style=\"html\">\n?\n</literal>")
11323 "<literal style=\"html\">?</literal>")
11324 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
11328 ("I" "#+INCLUDE: %file ?"
11329 "<include file=%file markup=\"?\">"))
11330 "Structure completion elements.
11331 This is a list of abbreviation keys and values. The value gets inserted
11332 if you type `<' followed by the key and then press the completion key,
11333 usually `M-TAB'. %file will be replaced by a file name after prompting
11334 for the file using completion. The cursor will be placed at the position
11335 of the `?` in the template.
11336 There are two templates for each key, the first uses the original Org syntax,
11337 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
11338 the default when the /org-mtags.el/ module has been loaded. See also the
11339 variable `org-mtags-prefer-muse-templates'."
11340 :group 'org-completion
11342 (string :tag "Key")
11343 (string :tag "Template")
11344 (string :tag "Muse Template")))
11346 (defun org-try-structure-completion ()
11347 "Try to complete a structure template before point.
11348 This looks for strings like \"<e\" on an otherwise empty line and
11350 (let ((l (buffer-substring (point-at-bol) (point)))
11352 (when (and (looking-at "[ \t]*$")
11353 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
11354 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
11355 (org-complete-expand-structure-template (+ -1 (point-at-bol)
11356 (match-beginning 1)) a)
11359 (defun org-complete-expand-structure-template (start cell)
11360 "Expand a structure template."
11361 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
11362 (rpl (nth (if musep 2 1) cell))
11364 (delete-region start (point))
11365 (when (string-match "\\`#\\+" rpl)
11368 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
11369 (setq ind (buffer-substring (point-at-bol) (point))))
11371 (setq start (point))
11372 (if (string-match "%file" rpl)
11373 (setq rpl (replace-match
11377 (abbreviate-file-name (read-file-name "Include file: ")))
11380 (setq rpl (mapconcat 'identity (split-string rpl "\n")
11381 (concat "\n" ind)))
11383 (if (re-search-backward "\\?" start t) (delete-char 1))))
11385 ;;;; TODO, DEADLINE, Comments
11387 (defun org-toggle-comment ()
11388 "Change the COMMENT state of an entry."
11391 (org-back-to-heading)
11392 (let (case-fold-search)
11394 ((looking-at (format org-heading-keyword-regexp-format
11395 org-comment-string))
11396 (goto-char (match-end 1))
11397 (looking-at (concat " +" org-comment-string))
11398 (replace-match "" t t)
11399 (when (eolp) (insert " ")))
11400 ((looking-at org-outline-regexp)
11401 (goto-char (match-end 0))
11402 (insert org-comment-string " "))))))
11404 (defvar org-last-todo-state-is-todo nil
11405 "This is non-nil when the last TODO state change led to a TODO state.
11406 If the last change removed the TODO tag or switched to DONE, then
11409 (defvar org-setting-tags nil) ; dynamically skipped
11411 (defvar org-todo-setup-filter-hook nil
11412 "Hook for functions that pre-filter todo specs.
11413 Each function takes a todo spec and returns either nil or the spec
11414 transformed into canonical form." )
11416 (defvar org-todo-get-default-hook nil
11417 "Hook for functions that get a default item for todo.
11418 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
11419 nil or a string to be used for the todo mark." )
11421 (defvar org-agenda-headline-snapshot-before-repeat)
11423 (defun org-current-effective-time ()
11424 "Return current time adjusted for `org-extend-today-until' variable."
11425 (let* ((ct (org-current-time))
11426 (dct (decode-time ct))
11428 (if (and org-use-effective-time
11429 (< (nth 2 dct) org-extend-today-until))
11430 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
11434 (defun org-todo-yesterday (&optional arg)
11435 "Like `org-todo' but the time of change will be 23:59 of yesterday."
11437 (if (eq major-mode 'org-agenda-mode)
11438 (apply 'org-agenda-todo-yesterday arg)
11439 (let* ((hour (third (decode-time
11440 (org-current-time))))
11441 (org-extend-today-until (1+ hour)))
11444 (defun org-todo (&optional arg)
11445 "Change the TODO state of an item.
11446 The state of an item is given by a keyword at the start of the heading,
11448 *** TODO Write paper
11451 The different keywords are specified in the variable `org-todo-keywords'.
11452 By default the available states are \"TODO\" and \"DONE\".
11453 So for this example: when the item starts with TODO, it is changed to DONE.
11454 When it starts with DONE, the DONE is removed. And when neither TODO nor
11455 DONE are present, add TODO at the beginning of the heading.
11457 With \\[universal-argument] prefix arg, use completion to determine the new \
11459 With numeric prefix arg, switch to that state.
11460 With a double \\[universal-argument] prefix, switch to the next set of TODO \
11461 keywords (nextset).
11462 With a triple \\[universal-argument] prefix, circumvent any state blocking.
11463 With a numeric prefix arg of 0, inhibit note taking for the change.
11465 For calling through lisp, arg is also interpreted in the following way:
11466 'none -> empty state
11467 \"\"(empty string) -> switch to empty state
11468 'done -> switch to DONE
11469 'nextset -> switch to the next set of keywords
11470 'previousset -> switch to the previous set of keywords
11471 \"WAITING\" -> switch to the specified keyword, but only if it
11472 really is a member of `org-todo-keywords'."
11474 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
11475 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
11476 'region-start-level 'region))
11477 org-loop-over-headlines-in-active-region)
11480 org-loop-over-headlines-in-active-region
11481 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
11482 (if (equal arg '(16)) (setq arg 'nextset))
11483 (let ((org-blocker-hook org-blocker-hook)
11486 (when (equal arg '(64))
11487 (setq arg nil org-blocker-hook nil))
11488 (when (and org-blocker-hook
11489 (or org-inhibit-blocking
11490 (org-entry-get nil "NOBLOCKING")))
11491 (setq org-blocker-hook nil))
11494 (org-back-to-heading t)
11495 (when (looking-at (concat "^\\*+ " org-comment-string))
11496 (org-toggle-comment)
11498 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
11499 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
11500 (looking-at "\\(?: *\\|[ \t]*$\\)"))
11501 (let* ((match-data (match-data))
11502 (startpos (point-at-bol))
11503 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
11504 (org-log-done org-log-done)
11505 (org-log-repeat org-log-repeat)
11506 (org-todo-log-states org-todo-log-states)
11507 (org-inhibit-logging
11509 (progn (setq arg nil) 'note) org-inhibit-logging))
11510 (this (match-string 1))
11511 (hl-pos (match-beginning 0))
11512 (head (org-get-todo-sequence-head this))
11513 (ass (assoc head org-todo-kwd-alist))
11514 (interpret (nth 1 ass))
11515 (done-word (nth 3 ass))
11516 (final-done-word (nth 4 ass))
11517 (org-last-state (or this ""))
11518 (completion-ignore-case t)
11519 (member (member this org-todo-keywords-1))
11520 (tail (cdr member))
11522 ((and org-todo-key-trigger
11523 (or (and (equal arg '(4))
11524 (eq org-use-fast-todo-selection 'prefix))
11525 (and (not arg) org-use-fast-todo-selection
11526 (not (eq org-use-fast-todo-selection
11528 ;; Use fast selection
11529 (org-fast-todo-selection))
11530 ((and (equal arg '(4))
11531 (or (not org-use-fast-todo-selection)
11532 (not org-todo-key-trigger)))
11533 ;; Read a state with completion
11534 (org-icompleting-read
11535 "State: " (mapcar (lambda(x) (list x))
11536 org-todo-keywords-1)
11540 (if tail (car tail) nil)
11541 (car org-todo-keywords-1)))
11543 (if (equal member org-todo-keywords-1)
11546 (nth (- (length org-todo-keywords-1)
11548 org-todo-keywords-1)
11549 (org-last org-todo-keywords-1))))
11550 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
11551 (setq arg nil))) ; hack to fall back to cycling
11553 ;; user or caller requests a specific state
11555 ((equal arg "") nil)
11556 ((eq arg 'none) nil)
11557 ((eq arg 'done) (or done-word (car org-done-keywords)))
11559 (or (car (cdr (member head org-todo-heads)))
11560 (car org-todo-heads)))
11561 ((eq arg 'previousset)
11562 (let ((org-todo-heads (reverse org-todo-heads)))
11563 (or (car (cdr (member head org-todo-heads)))
11564 (car org-todo-heads))))
11565 ((car (member arg org-todo-keywords-1)))
11567 (error "State `%s' not valid in this file" arg))
11568 ((nth (1- (prefix-numeric-value arg))
11569 org-todo-keywords-1))))
11570 ((null member) (or head (car org-todo-keywords-1)))
11571 ((equal this final-done-word) nil) ;; -> make empty
11572 ((null tail) nil) ;; -> first entry
11573 ((memq interpret '(type priority))
11574 (if (eq this-command last-command)
11576 (if (> (length tail) 0)
11577 (or done-word (car org-done-keywords))
11582 (run-hook-with-args-until-success
11583 'org-todo-get-default-hook org-state org-last-state)
11585 (next (if org-state (concat " " org-state " ") " "))
11586 (change-plist (list :type 'todo-state-change :from this :to org-state
11587 :position startpos))
11589 (when org-blocker-hook
11590 (setq org-last-todo-state-is-todo
11591 (not (member this org-done-keywords)))
11592 (unless (save-excursion
11594 (org-with-wide-buffer
11595 (run-hook-with-args-until-failure
11596 'org-blocker-hook change-plist))))
11597 (if (org-called-interactively-p 'interactive)
11598 (error "TODO state change from %s to %s blocked" this org-state)
11600 (message "TODO state change from %s to %s blocked" this org-state)
11601 (throw 'exit nil))))
11602 (store-match-data match-data)
11603 (replace-match next t t)
11604 (unless (pos-visible-in-window-p hl-pos)
11605 (message "TODO state changed to %s" (org-trim next)))
11607 (setq head (org-get-todo-sequence-head org-state)
11608 ass (assoc head org-todo-kwd-alist)
11609 interpret (nth 1 ass)
11610 done-word (nth 3 ass)
11611 final-done-word (nth 4 ass)))
11612 (when (memq arg '(nextset previousset))
11613 (message "Keyword-Set %d/%d: %s"
11614 (- (length org-todo-sets) -1
11615 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
11616 (length org-todo-sets)
11617 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
11618 (setq org-last-todo-state-is-todo
11619 (not (member org-state org-done-keywords)))
11620 (setq now-done-p (and (member org-state org-done-keywords)
11621 (not (member this org-done-keywords))))
11622 (and logging (org-local-logging logging))
11623 (when (and (or org-todo-log-states org-log-done)
11624 (not (eq org-inhibit-logging t))
11625 (not (memq arg '(nextset previousset))))
11626 ;; we need to look at recording a time and note
11627 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
11628 (nth 2 (assoc this org-todo-log-states))))
11629 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
11630 (setq dolog 'time))
11631 (when (and org-state
11632 (member org-state org-not-done-keywords)
11633 (not (member this org-not-done-keywords)))
11634 ;; This is now a todo state and was not one before
11635 ;; If there was a CLOSED time stamp, get rid of it.
11636 (org-add-planning-info nil nil 'closed))
11637 (when (and now-done-p org-log-done)
11638 ;; It is now done, and it was not done before
11639 (org-add-planning-info 'closed (org-current-effective-time))
11640 (if (and (not dolog) (eq 'note org-log-done))
11641 (org-add-log-setup 'done org-state this 'findpos 'note)))
11642 (when (and org-state dolog)
11643 ;; This is a non-nil state, and we need to log it
11644 (org-add-log-setup 'state org-state this 'findpos dolog)))
11645 ;; Fixup tag positioning
11646 (org-todo-trigger-tag-changes org-state)
11647 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
11648 (when org-provide-todo-statistics
11649 (org-update-parent-todo-statistics))
11650 (run-hooks 'org-after-todo-state-change-hook)
11651 (if (and arg (not (member org-state org-done-keywords)))
11652 (setq head (org-get-todo-sequence-head org-state)))
11653 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
11654 ;; Do we need to trigger a repeat?
11656 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
11657 ;; This is for the agenda, take a snapshot of the headline.
11659 (setq org-agenda-headline-snapshot-before-repeat
11660 (org-get-heading))))
11661 (org-auto-repeat-maybe org-state))
11662 ;; Fixup cursor location if close to the keyword
11663 (if (and (outline-on-heading-p)
11665 (save-excursion (beginning-of-line 1)
11666 (looking-at org-todo-line-regexp))
11667 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
11669 (goto-char (or (match-end 2) (match-end 1)))
11670 (and (looking-at " ") (just-one-space))))
11671 (when org-trigger-hook
11673 (run-hook-with-args 'org-trigger-hook change-plist)))
11674 (when commentp (org-toggle-comment))))))))
11676 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
11677 "Block turning an entry into a TODO, using the hierarchy.
11678 This checks whether the current task should be blocked from state
11679 changes. Such blocking occurs when:
11681 1. The task has children which are not all in a completed state.
11683 2. A task has a parent with the property :ORDERED:, and there
11684 are siblings prior to the current task with incomplete
11687 3. The parent of the task is blocked because it has siblings that should
11688 be done first, or is child of a block grandparent TODO entry."
11690 (if (not org-enforce-todo-dependencies)
11691 t ; if locally turned off don't block
11693 ;; If this is not a todo state change, or if this entry is already DONE,
11695 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11696 (member (plist-get change-plist :from)
11697 (cons 'done org-done-keywords))
11698 (member (plist-get change-plist :to)
11699 (cons 'todo org-not-done-keywords))
11700 (not (plist-get change-plist :to)))
11701 (throw 'dont-block t))
11702 ;; If this task has children, and any are undone, it's blocked
11704 (org-back-to-heading t)
11705 (let ((this-level (funcall outline-level)))
11706 (outline-next-heading)
11707 (let ((child-level (funcall outline-level)))
11708 (while (and (not (eobp))
11709 (> child-level this-level))
11710 ;; this todo has children, check whether they are all
11712 (if (and (not (org-entry-is-done-p))
11713 (org-entry-is-todo-p))
11714 (throw 'dont-block nil))
11715 (outline-next-heading)
11716 (setq child-level (funcall outline-level))))))
11717 ;; Otherwise, if the task's parent has the :ORDERED: property, and
11718 ;; any previous siblings are undone, it's blocked
11720 (org-back-to-heading t)
11721 (let* ((pos (point))
11722 (parent-pos (and (org-up-heading-safe) (point))))
11723 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11724 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11726 (re-search-forward org-not-done-heading-regexp pos t))
11727 (throw 'dont-block nil)) ; block, there is an older sibling not done.
11728 ;; Search further up the hierarchy, to see if an ancestor is blocked
11730 (goto-char parent-pos)
11731 (if (not (looking-at org-not-done-heading-regexp))
11732 (throw 'dont-block t)) ; do not block, parent is not a TODO
11734 (setq parent-pos (and (org-up-heading-safe) (point)))
11735 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11736 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11738 (re-search-forward org-not-done-heading-regexp pos t))
11739 (throw 'dont-block nil)))))))) ; block, older sibling not done.
11741 (defcustom org-track-ordered-property-with-tag nil
11742 "Should the ORDERED property also be shown as a tag?
11743 The ORDERED property decides if an entry should require subtasks to be
11744 completed in sequence. Since a property is not very visible, setting
11745 this option means that toggling the ORDERED property with the command
11746 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
11747 not relevant for the behavior, but it makes things more visible.
11749 Note that toggling the tag with tags commands will not change the property
11750 and therefore not influence behavior!
11752 This can be t, meaning the tag ORDERED should be used, It can also be a
11753 string to select a different tag for this task."
11756 (const :tag "No tracking" nil)
11757 (const :tag "Track with ORDERED tag" t)
11758 (string :tag "Use other tag")))
11760 (defun org-toggle-ordered-property ()
11761 "Toggle the ORDERED property of the current entry.
11762 For better visibility, you can track the value of this property with a tag.
11763 See variable `org-track-ordered-property-with-tag'."
11765 (let* ((t1 org-track-ordered-property-with-tag)
11766 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
11768 (org-back-to-heading)
11769 (if (org-entry-get nil "ORDERED")
11771 (org-delete-property "ORDERED")
11772 (and tag (org-toggle-tag tag 'off))
11773 (message "Subtasks can be completed in arbitrary order"))
11774 (org-entry-put nil "ORDERED" "t")
11775 (and tag (org-toggle-tag tag 'on))
11776 (message "Subtasks must be completed in sequence")))))
11778 (defvar org-blocked-by-checkboxes) ; dynamically scoped
11779 (defun org-block-todo-from-checkboxes (change-plist)
11780 "Block turning an entry into a TODO, using checkboxes.
11781 This checks whether the current task should be blocked from state
11782 changes because there are unchecked boxes in this entry."
11783 (if (not org-enforce-todo-checkbox-dependencies)
11784 t ; if locally turned off don't block
11786 ;; If this is not a todo state change, or if this entry is already DONE,
11788 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11789 (member (plist-get change-plist :from)
11790 (cons 'done org-done-keywords))
11791 (member (plist-get change-plist :to)
11792 (cons 'todo org-not-done-keywords))
11793 (not (plist-get change-plist :to)))
11794 (throw 'dont-block t))
11795 ;; If this task has checkboxes that are not checked, it's blocked
11797 (org-back-to-heading t)
11798 (let ((beg (point)) end)
11799 (outline-next-heading)
11802 (if (org-list-search-forward
11803 (concat (org-item-beginning-re)
11804 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
11808 (if (boundp 'org-blocked-by-checkboxes)
11809 (setq org-blocked-by-checkboxes t))
11810 (throw 'dont-block nil)))))
11811 t))) ; do not block
11813 (defun org-entry-blocked-p ()
11814 "Is the current entry blocked?"
11815 (org-with-buffer-modified-unmodified
11816 (if (org-entry-get nil "NOBLOCKING")
11817 nil ;; Never block this entry
11819 (run-hook-with-args-until-failure
11821 (list :type 'todo-state-change
11826 (defun org-update-statistics-cookies (all)
11827 "Update the statistics cookie, either from TODO or from checkboxes.
11828 This should be called with the cursor in a line with a statistics cookie."
11832 (org-update-checkbox-count 'all)
11833 (org-map-entries 'org-update-parent-todo-statistics))
11834 (if (not (org-at-heading-p))
11835 (org-update-checkbox-count)
11836 (let ((pos (point-marker))
11838 (ignore-errors (org-back-to-heading t))
11839 (if (not (org-at-heading-p))
11840 (org-update-checkbox-count)
11841 (setq l1 (org-outline-level))
11842 (setq end (save-excursion
11843 (outline-next-heading)
11844 (if (org-at-heading-p) (setq l2 (org-outline-level)))
11846 (if (and (save-excursion
11848 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
11849 (not (save-excursion (re-search-forward
11850 ":COOKIE_DATA:.*\\<todo\\>" end t))))
11851 (org-update-checkbox-count)
11852 (if (and l2 (> l2 l1))
11855 (org-update-parent-todo-statistics))
11857 (beginning-of-line 1)
11858 (while (re-search-forward
11859 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
11861 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
11863 (move-marker pos nil)))))
11865 (defvar org-entry-property-inherited-from) ;; defined below
11866 (defun org-update-parent-todo-statistics ()
11867 "Update any statistics cookie in the parent of the current headline.
11868 When `org-hierarchical-todo-statistics' is nil, statistics will cover
11869 the entire subtree and this will travel up the hierarchy and update
11870 statistics everywhere."
11871 (let* ((prop (save-excursion (org-up-heading-safe)
11872 (org-entry-get nil "COOKIE_DATA" 'inherit)))
11873 (recursive (or (not org-hierarchical-todo-statistics)
11874 (and prop (string-match "\\<recursive\\>" prop))))
11875 (lim (or (and prop (marker-position org-entry-property-inherited-from))
11878 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
11879 level ltoggle l1 new ndel
11880 (cnt-all 0) (cnt-done 0) is-percent kwd
11881 checkbox-beg ov ovs ove cookie-present)
11884 (beginning-of-line 1)
11885 (setq ltoggle (funcall outline-level))
11886 ;; Three situations are to consider:
11888 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
11889 ;; to the top-level ancestor on the headline;
11891 ;; 2. If parent has "recursive" property, repeat up to the
11892 ;; headline setting that property, taking inheritance into
11895 ;; 3. Else, move up to direct parent and proceed only once.
11896 (while (and (setq level (org-up-heading-safe))
11897 (or recursive first)
11899 (setq first nil cookie-present nil)
11903 (downcase (or (org-entry-get nil "COOKIE_DATA")
11906 (while (re-search-forward box-re (point-at-eol) t)
11907 (setq cnt-all 0 cnt-done 0 cookie-present t)
11908 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
11910 (unless (outline-next-heading) (throw 'exit nil))
11911 (while (and (looking-at org-complex-heading-regexp)
11912 (> (setq l1 (length (match-string 1))) level))
11913 (setq kwd (and (or recursive (= l1 ltoggle))
11915 (if (or (eq org-provide-todo-statistics 'all-headlines)
11916 (and (listp org-provide-todo-statistics)
11917 (or (member kwd org-provide-todo-statistics)
11918 (member kwd org-done-keywords))))
11919 (setq cnt-all (1+ cnt-all))
11920 (if (eq org-provide-todo-statistics t)
11921 (and kwd (setq cnt-all (1+ cnt-all)))))
11922 (and (member kwd org-done-keywords)
11923 (setq cnt-done (1+ cnt-done)))
11924 (outline-next-heading)))
11927 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
11928 (format "[%d/%d]" cnt-done cnt-all))
11929 ndel (- (match-end 0) checkbox-beg))
11930 ;; handle overlays when updating cookie from column view
11931 (when (setq ov (car (overlays-at checkbox-beg)))
11932 (setq ovs (overlay-start ov) ove (overlay-end ov))
11933 (delete-overlay ov))
11934 (goto-char checkbox-beg)
11936 (delete-region (point) (+ (point) ndel))
11937 (when org-auto-align-tags (org-fix-tags-on-the-fly))
11938 (when ov (move-overlay ov ovs ove)))
11939 (when cookie-present
11940 (run-hook-with-args 'org-after-todo-statistics-hook
11941 cnt-done (- cnt-all cnt-done))))))
11942 (run-hooks 'org-todo-statistics-hook)))
11944 (defvar org-after-todo-statistics-hook nil
11945 "Hook that is called after a TODO statistics cookie has been updated.
11946 Each function is called with two arguments: the number of not-done entries
11947 and the number of done entries.
11949 For example, the following function, when added to this hook, will switch
11950 an entry to DONE when all children are done, and back to TODO when new
11951 entries are set to a TODO status. Note that this hook is only called
11952 when there is a statistics cookie in the headline!
11954 (defun org-summary-todo (n-done n-not-done)
11955 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
11956 (let (org-log-done org-log-states) ; turn off logging
11957 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
11960 (defvar org-todo-statistics-hook nil
11961 "Hook that is run whenever Org thinks TODO statistics should be updated.
11962 This hook runs even if there is no statistics cookie present, in which case
11963 `org-after-todo-statistics-hook' would not run.")
11965 (defun org-todo-trigger-tag-changes (state)
11966 "Apply the changes defined in `org-todo-state-tags-triggers'."
11967 (let ((l org-todo-state-tags-triggers)
11969 (when (or (not state) (equal state ""))
11970 (setq changes (append changes (cdr (assoc "" l)))))
11971 (when (and (stringp state) (> (length state) 0))
11972 (setq changes (append changes (cdr (assoc state l)))))
11973 (when (member state org-not-done-keywords)
11974 (setq changes (append changes (cdr (assoc 'todo l)))))
11975 (when (member state org-done-keywords)
11976 (setq changes (append changes (cdr (assoc 'done l)))))
11977 (dolist (c changes)
11978 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
11980 (defun org-local-logging (value)
11981 "Get logging settings from a property VALUE."
11983 ;; directly set the variables, they are already local.
11984 (setq org-log-done nil
11986 org-todo-log-states nil)
11987 (setq words (org-split-string value))
11988 (while (setq w (pop words))
11990 ((setq a (assoc w org-startup-options))
11991 (and (member (nth 1 a) '(org-log-done org-log-repeat))
11992 (set (nth 1 a) (nth 2 a))))
11993 ((setq a (org-extract-log-state-settings w))
11994 (and (member (car a) org-todo-keywords-1)
11995 (push a org-todo-log-states)))))))
11997 (defun org-get-todo-sequence-head (kwd)
11998 "Return the head of the TODO sequence to which KWD belongs.
11999 If KWD is not set, check if there is a text property remembering the
12004 (or (get-text-property (point-at-bol) 'org-todo-head)
12006 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
12007 nil (point-at-eol)))
12008 (get-text-property p 'org-todo-head))))
12009 ((not (member kwd org-todo-keywords-1))
12010 (car org-todo-keywords-1))
12011 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
12013 (defun org-fast-todo-selection ()
12014 "Fast TODO keyword selection with single keys.
12015 Returns the new TODO keyword, or nil if no state change should occur."
12016 (let* ((fulltable org-todo-key-alist)
12017 (done-keywords org-done-keywords) ;; needed for the faces.
12018 (maxlen (apply 'max (mapcar
12020 (if (stringp (car x)) (string-width (car x)) 0))
12023 (fwidth (+ maxlen 3 1 3))
12024 (ncol (/ (- (window-width) 4) fwidth))
12028 (save-window-excursion
12030 (set-buffer (get-buffer-create " *Org todo*"))
12031 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
12033 (org-set-local 'org-done-keywords done-keywords)
12034 (setq tbl fulltable cnt 0)
12035 (while (setq e (pop tbl))
12037 ((equal e '(:startgroup))
12038 (push '() groups) (setq ingroup t)
12039 (when (not (= cnt 0))
12043 ((equal e '(:endgroup))
12044 (setq ingroup nil cnt 0)
12046 ((equal e '(:newline))
12047 (when (not (= cnt 0))
12051 (while (equal (car tbl) '(:newline))
12053 (setq tbl (cdr tbl)))))
12055 (setq tg (car e) c (cdr e))
12056 (if ingroup (push tg (car groups)))
12057 (setq tg (org-add-props tg nil 'face
12058 (org-get-todo-face tg)))
12059 (if (and (= cnt 0) (not ingroup)) (insert " "))
12060 (insert "[" c "] " tg (make-string
12061 (- fwidth 4 (length tg)) ?\ ))
12062 (when (= (setq cnt (1+ cnt)) ncol)
12064 (if ingroup (insert " "))
12067 (goto-char (point-min))
12068 (if (not expert) (org-fit-window-to-buffer))
12069 (message "[a-z..]:Set [SPC]:clear")
12070 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
12073 (and (= c ?q) (not (rassoc c fulltable))))
12074 (setq quit-flag t))
12076 ((setq e (rassoc c fulltable) tg (car e))
12078 (t (setq quit-flag t)))))))
12080 (defun org-entry-is-todo-p ()
12081 (member (org-get-todo-state) org-not-done-keywords))
12083 (defun org-entry-is-done-p ()
12084 (member (org-get-todo-state) org-done-keywords))
12086 (defun org-get-todo-state ()
12088 (org-back-to-heading t)
12089 (and (looking-at org-todo-line-regexp)
12091 (match-string 2))))
12093 (defun org-at-date-range-p (&optional inactive-ok)
12094 "Is the cursor inside a date range?"
12098 (let ((pos (point)))
12099 (skip-chars-backward "^[<\r\n")
12100 (skip-chars-backward "<[")
12101 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12102 (>= (match-end 0) pos)
12104 (skip-chars-backward "^<[\r\n")
12105 (skip-chars-backward "<[")
12106 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12107 (>= (match-end 0) pos)
12111 (defun org-get-repeat (&optional tagline)
12112 "Check if there is a deadline/schedule with repeater in this entry."
12115 (org-back-to-heading t)
12116 (and (re-search-forward (if tagline
12117 (concat tagline "\\s-*" org-repeat-re)
12119 (org-entry-end-position) t)
12120 (match-string-no-properties 1)))))
12122 (defvar org-last-changed-timestamp)
12123 (defvar org-last-inserted-timestamp)
12124 (defvar org-log-post-message)
12125 (defvar org-log-note-purpose)
12126 (defvar org-log-note-how)
12127 (defvar org-log-note-extra)
12128 (defun org-auto-repeat-maybe (done-word)
12129 "Check if the current headline contains a repeated deadline/schedule.
12130 If yes, set TODO state back to what it was and change the base date
12131 of repeating deadline/scheduled time stamps to new date.
12132 This function is run automatically after each state change to a DONE state."
12133 ;; last-state is dynamically scoped into this function
12134 (let* ((repeat (org-get-repeat))
12135 (aa (assoc org-last-state org-todo-kwd-alist))
12136 (interpret (nth 1 aa))
12138 (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
12139 (msg "Entry repeats: ")
12141 (org-todo-log-states nil)
12142 re type n what ts time to-state)
12144 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
12145 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
12146 org-todo-repeat-to-state))
12147 (unless (and to-state (member to-state org-todo-keywords-1))
12148 (setq to-state (if (eq interpret 'type) org-last-state head)))
12149 (org-todo to-state)
12150 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
12151 (org-entry-put nil "LAST_REPEAT" (format-time-string
12152 (org-time-stamp-format t t))))
12153 (when org-log-repeat
12154 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
12155 (memq 'org-add-log-note post-command-hook))
12156 ;; OK, we are already setup for some record
12157 (if (eq org-log-repeat 'note)
12158 ;; make sure we take a note, not only a time stamp
12159 (setq org-log-note-how 'note))
12160 ;; Set up for taking a record
12161 (org-add-log-setup 'state (or done-word (car org-done-keywords))
12163 'findpos org-log-repeat)))
12164 (org-back-to-heading t)
12165 (org-add-planning-info nil nil 'closed)
12166 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12167 org-deadline-time-regexp "\\)\\|\\("
12168 org-ts-regexp "\\)"))
12169 (while (re-search-forward
12170 re (save-excursion (outline-next-heading) (point)) t)
12171 (setq type (if (match-end 1) org-scheduled-string
12172 (if (match-end 3) org-deadline-string "Plain:"))
12173 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
12174 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
12175 (setq n (string-to-number (match-string 2 ts))
12176 what (match-string 3 ts))
12177 (if (equal what "w") (setq n (* n 7) what "d"))
12178 (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
12179 (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
12180 ;; Preparation, see if we need to modify the start date for the change
12181 (when (match-end 1)
12182 (setq time (save-match-data (org-time-string-to-time ts)))
12184 ((equal (match-string 1 ts) ".")
12185 ;; Shift starting date to today
12186 (org-timestamp-change
12187 (- (org-today) (time-to-days time))
12189 ((equal (match-string 1 ts) "+")
12190 (let ((nshiftmax 10) (nshift 0))
12191 (while (or (= nshift 0)
12192 (<= (time-to-days time)
12193 (time-to-days (current-time))))
12194 (when (= (incf nshift) nshiftmax)
12195 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
12197 (org-timestamp-change n (cdr (assoc what whata)))
12198 (org-at-timestamp-p t)
12199 (setq ts (match-string 1))
12200 (setq time (save-match-data (org-time-string-to-time ts)))))
12201 (org-timestamp-change (- n) (cdr (assoc what whata)))
12202 ;; rematch, so that we have everything in place for the real shift
12203 (org-at-timestamp-p t)
12204 (setq ts (match-string 1))
12205 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
12206 (org-timestamp-change n (cdr (assoc what whata)))
12207 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
12208 (setq org-log-post-message msg)
12209 (message "%s" msg))))
12211 (defun org-show-todo-tree (arg)
12212 "Make a compact tree which shows all headlines marked with TODO.
12213 The tree will show the lines where the regexp matches, and all higher
12214 headlines above the match.
12215 With a \\[universal-argument] prefix, prompt for a regexp to match.
12216 With a numeric prefix N, construct a sparse tree for the Nth element
12217 of `org-todo-keywords-1'."
12219 (let ((case-fold-search nil)
12221 (cond ((null arg) org-not-done-regexp)
12223 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
12224 (mapcar 'list org-todo-keywords-1))))
12226 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12228 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12229 (regexp-quote (nth (1- (prefix-numeric-value arg))
12230 org-todo-keywords-1)))
12231 (t (error "Invalid prefix argument: %s" arg)))))
12232 (message "%d TODO entries found"
12233 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
12235 (defun org-deadline (&optional remove time)
12236 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
12237 With argument REMOVE, remove any deadline from the item.
12238 With argument TIME, set the deadline at the corresponding date. TIME
12239 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12241 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12242 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12243 'region-start-level 'region))
12244 org-loop-over-headlines-in-active-region)
12246 `(org-deadline ',remove ,time)
12247 org-loop-over-headlines-in-active-region
12248 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12249 (let* ((old-date (org-entry-get nil "DEADLINE"))
12250 (repeater (and old-date
12252 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12254 (match-string 1 old-date))))
12257 (when (and old-date org-log-redeadline)
12258 (org-add-log-setup 'deldeadline nil old-date 'findpos
12259 org-log-redeadline))
12260 (org-remove-timestamp-with-keyword org-deadline-string)
12261 (message "Item no longer has a deadline."))
12262 (org-add-planning-info 'deadline time 'closed)
12263 (when (and old-date org-log-redeadline
12264 (not (equal old-date
12265 (substring org-last-inserted-timestamp 1 -1))))
12266 (org-add-log-setup 'redeadline nil old-date 'findpos
12267 org-log-redeadline))
12270 (org-back-to-heading t)
12271 (when (re-search-forward (concat org-deadline-string " "
12272 org-last-inserted-timestamp)
12274 (outline-next-heading) (point)) t)
12275 (goto-char (1- (match-end 0)))
12276 (insert " " repeater)
12277 (setq org-last-inserted-timestamp
12278 (concat (substring org-last-inserted-timestamp 0 -1)
12280 (substring org-last-inserted-timestamp -1))))))
12281 (message "Deadline on %s" org-last-inserted-timestamp)))))
12283 (defun org-schedule (&optional remove time)
12284 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
12285 With argument REMOVE, remove any scheduling date from the item.
12286 With argument TIME, scheduled at the corresponding date. TIME can
12287 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12289 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12290 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12291 'region-start-level 'region))
12292 org-loop-over-headlines-in-active-region)
12294 `(org-schedule ',remove ,time)
12295 org-loop-over-headlines-in-active-region
12296 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12297 (let* ((old-date (org-entry-get nil "SCHEDULED"))
12298 (repeater (and old-date
12300 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12302 (match-string 1 old-date))))
12305 (when (and old-date org-log-reschedule)
12306 (org-add-log-setup 'delschedule nil old-date 'findpos
12307 org-log-reschedule))
12308 (org-remove-timestamp-with-keyword org-scheduled-string)
12309 (message "Item is no longer scheduled."))
12310 (org-add-planning-info 'scheduled time 'closed)
12311 (when (and old-date org-log-reschedule
12312 (not (equal old-date
12313 (substring org-last-inserted-timestamp 1 -1))))
12314 (org-add-log-setup 'reschedule nil old-date 'findpos
12315 org-log-reschedule))
12318 (org-back-to-heading t)
12319 (when (re-search-forward (concat org-scheduled-string " "
12320 org-last-inserted-timestamp)
12322 (outline-next-heading) (point)) t)
12323 (goto-char (1- (match-end 0)))
12324 (insert " " repeater)
12325 (setq org-last-inserted-timestamp
12326 (concat (substring org-last-inserted-timestamp 0 -1)
12328 (substring org-last-inserted-timestamp -1))))))
12329 (message "Scheduled to %s" org-last-inserted-timestamp)))))
12331 (defun org-get-scheduled-time (pom &optional inherit)
12332 "Get the scheduled time as a time tuple, of a format suitable
12333 for calling org-schedule with, or if there is no scheduling,
12335 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
12337 (apply 'encode-time (org-parse-time-string time)))))
12339 (defun org-get-deadline-time (pom &optional inherit)
12340 "Get the deadline as a time tuple, of a format suitable for
12341 calling org-deadline with, or if there is no scheduling, returns
12343 (let ((time (org-entry-get pom "DEADLINE" inherit)))
12345 (apply 'encode-time (org-parse-time-string time)))))
12347 (defun org-remove-timestamp-with-keyword (keyword)
12348 "Remove all time stamps with KEYWORD in the current entry."
12349 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
12352 (org-back-to-heading t)
12354 (outline-next-heading)
12355 (while (re-search-backward re beg t)
12357 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
12358 (equal (char-before) ?\ ))
12359 (backward-delete-char 1)
12360 (if (string-match "^[ \t]*$" (buffer-substring
12361 (point-at-bol) (point-at-eol)))
12362 (delete-region (point-at-bol)
12363 (min (point-max) (1+ (point-at-eol))))))))))
12365 (defun org-add-planning-info (what &optional time &rest remove)
12366 "Insert new timestamp with keyword in the line directly after the headline.
12367 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
12368 If non is given, the user is prompted for a date.
12369 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
12372 (let (org-time-was-given org-end-time-was-given ts
12373 end default-time default-input)
12376 (when (and (memq what '(scheduled deadline))
12378 (and (stringp time)
12379 (string-match "^[-+]+[0-9]" time))))
12380 ;; Try to get a default date/time from existing timestamp
12382 (org-back-to-heading t)
12383 (setq end (save-excursion (outline-next-heading) (point)))
12384 (when (re-search-forward (if (eq what 'scheduled)
12385 org-scheduled-time-regexp
12386 org-deadline-time-regexp)
12388 (setq ts (match-string 1)
12390 (apply 'encode-time (org-parse-time-string ts))
12391 default-input (and ts (org-get-compact-tod ts))))))
12395 ;; This is a string (relative or absolute), set proper date
12396 (apply 'encode-time
12397 (org-read-date-analyze
12398 time default-time (decode-time default-time)))
12399 ;; If necessary, get the time from the user
12400 (or time (org-read-date nil 'to-time nil nil
12401 default-time default-input)))))
12403 (when (and org-insert-labeled-timestamps-at-point
12404 (member what '(scheduled deadline)))
12406 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
12407 (org-insert-time-stamp time org-time-was-given
12408 nil nil nil (list org-end-time-was-given))
12412 (let (col list elt ts buffer-invisibility-spec)
12413 (org-back-to-heading t)
12414 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
12415 (goto-char (match-end 1))
12416 (setq col (current-column))
12417 (goto-char (match-end 0))
12418 (if (eobp) (insert "\n") (forward-char 1))
12419 (when (and (not what)
12422 org-keyword-time-not-clock-regexp))))
12423 ;; Nothing to add, nothing to remove...... :-)
12425 (if (and (not (looking-at org-outline-regexp))
12426 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
12428 (not (equal (match-string 1) org-clock-string)))
12429 (narrow-to-region (match-beginning 0) (match-end 0))
12430 (insert-before-markers "\n")
12432 (narrow-to-region (point) (point))
12433 (and org-adapt-indentation (org-indent-to-column col)))
12434 ;; Check if we have to remove something.
12435 (setq list (cons what remove))
12437 (setq elt (pop list))
12438 (when (or (and (eq elt 'scheduled)
12439 (re-search-forward org-scheduled-time-regexp nil t))
12440 (and (eq elt 'deadline)
12441 (re-search-forward org-deadline-time-regexp nil t))
12442 (and (eq elt 'closed)
12443 (re-search-forward org-closed-time-regexp nil t)))
12445 (if (looking-at "--+<[^>]+>") (replace-match ""))))
12446 (and (looking-at "[ \t]+") (replace-match ""))
12447 (and org-adapt-indentation (bolp) (org-indent-to-column col))
12450 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
12451 (cond ((eq what 'scheduled) org-scheduled-string)
12452 ((eq what 'deadline) org-deadline-string)
12453 ((eq what 'closed) org-closed-string))
12455 (setq ts (org-insert-time-stamp
12457 (or org-time-was-given
12458 (and (eq what 'closed) org-log-done-with-time))
12460 nil nil (list org-end-time-was-given)))
12462 (if (not (or (bolp) (eq (char-before) ?\ )
12463 (memq (char-after) '(32 10))
12466 (goto-char (point-min))
12468 (if (and (looking-at "[ \t]*\n")
12469 (equal (char-before) ?\n))
12470 (delete-region (1- (point)) (point-at-eol)))
12473 (defvar org-log-note-marker (make-marker))
12474 (defvar org-log-note-purpose nil)
12475 (defvar org-log-note-state nil)
12476 (defvar org-log-note-previous-state nil)
12477 (defvar org-log-note-how nil)
12478 (defvar org-log-note-extra nil)
12479 (defvar org-log-note-window-configuration nil)
12480 (defvar org-log-note-return-to (make-marker))
12481 (defvar org-log-note-effective-time nil
12482 "Remembered current time so that dynamically scoped
12483 `org-extend-today-until' affects tha timestamps in state change
12486 (defvar org-log-post-message nil
12487 "Message to be displayed after a log note has been stored.
12488 The auto-repeater uses this.")
12490 (defun org-add-note ()
12491 "Add a note to the current entry.
12492 This is done in the same way as adding a state change note."
12494 (org-add-log-setup 'note nil nil 'findpos nil))
12496 (defvar org-property-end-re)
12497 (defun org-add-log-setup (&optional purpose state prev-state
12499 "Set up the post command hook to take a note.
12500 If this is about to TODO state change, the new state is expected in STATE.
12501 When FINDPOS is non-nil, find the correct position for the note in
12502 the current entry. If not, assume that it can be inserted at point.
12503 HOW is an indicator what kind of note should be created.
12504 EXTRA is additional text that will be inserted into the notes buffer."
12505 (let* ((org-log-into-drawer (org-log-into-drawer))
12506 (drawer (cond ((stringp org-log-into-drawer)
12507 org-log-into-drawer)
12508 (org-log-into-drawer "LOGBOOK"))))
12512 (org-back-to-heading t)
12513 (narrow-to-region (point) (save-excursion
12514 (outline-next-heading) (point)))
12515 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
12516 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
12518 (goto-char (match-end 0))
12521 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
12524 (goto-char (match-end 0))
12525 (or org-log-states-order-reversed
12526 (and (re-search-forward org-property-end-re nil t)
12527 (goto-char (1- (match-beginning 0))))))
12528 (insert "\n:" drawer ":\n:END:")
12529 (beginning-of-line 0)
12531 (beginning-of-line 2)
12534 ((and org-log-state-notes-insert-after-drawers
12536 (forward-line) (looking-at org-drawer-regexp)))
12538 (while (looking-at org-drawer-regexp)
12539 (goto-char (match-end 0))
12540 (re-search-forward org-property-end-re (point-max) t)
12542 (forward-line -1)))
12543 (unless org-log-states-order-reversed
12544 (and (= (char-after) ?\n) (forward-char 1))
12545 (org-skip-over-state-notes)
12546 (skip-chars-backward " \t\n\r")))
12547 (move-marker org-log-note-marker (point))
12548 (setq org-log-note-purpose purpose
12549 org-log-note-state state
12550 org-log-note-previous-state prev-state
12551 org-log-note-how how
12552 org-log-note-extra extra
12553 org-log-note-effective-time (org-current-effective-time))
12554 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
12556 (defun org-skip-over-state-notes ()
12557 "Skip past the list of State notes in an entry."
12558 (if (looking-at "\n[ \t]*- State") (forward-char 1))
12559 (when (ignore-errors (goto-char (org-in-item-p)))
12560 (let* ((struct (org-list-struct))
12561 (prevs (org-list-prevs-alist struct)))
12562 (while (looking-at "[ \t]*- State")
12563 (goto-char (or (org-list-get-next-item (point) struct prevs)
12564 (org-list-get-item-end (point) struct)))))))
12566 (defun org-add-log-note (&optional purpose)
12567 "Pop up a window for taking a note, and add this note later at point."
12568 (remove-hook 'post-command-hook 'org-add-log-note)
12569 (setq org-log-note-window-configuration (current-window-configuration))
12570 (delete-other-windows)
12571 (move-marker org-log-note-return-to (point))
12572 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
12573 (goto-char org-log-note-marker)
12574 (org-switch-to-buffer-other-window "*Org Note*")
12576 (if (memq org-log-note-how '(time state))
12577 (let (current-prefix-arg) (org-store-log-note))
12578 (let ((org-inhibit-startup t)) (org-mode))
12579 (insert (format "# Insert note for %s.
12580 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
12582 ((eq org-log-note-purpose 'clock-out) "stopped clock")
12583 ((eq org-log-note-purpose 'done) "closed todo item")
12584 ((eq org-log-note-purpose 'state)
12585 (format "state change from \"%s\" to \"%s\""
12586 (or org-log-note-previous-state "")
12587 (or org-log-note-state "")))
12588 ((eq org-log-note-purpose 'reschedule)
12590 ((eq org-log-note-purpose 'delschedule)
12591 "no longer scheduled")
12592 ((eq org-log-note-purpose 'redeadline)
12593 "changing deadline")
12594 ((eq org-log-note-purpose 'deldeadline)
12595 "removing deadline")
12596 ((eq org-log-note-purpose 'refile)
12598 ((eq org-log-note-purpose 'note)
12600 (t (error "This should not happen")))))
12601 (if org-log-note-extra (insert org-log-note-extra))
12602 (org-set-local 'org-finish-function 'org-store-log-note)
12603 (run-hooks 'org-log-buffer-setup-hook)))
12605 (defvar org-note-abort nil) ; dynamically scoped
12606 (defun org-store-log-note ()
12607 "Finish taking a log note, and insert it to where it belongs."
12608 (let ((txt (buffer-string))
12609 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
12611 (kill-buffer (current-buffer))
12612 (while (string-match "\\`# .*\n[ \t\n]*" txt)
12613 (setq txt (replace-match "" t t txt)))
12614 (if (string-match "\\s-+\\'" txt)
12615 (setq txt (replace-match "" t t txt)))
12616 (setq lines (org-split-string txt "\n"))
12617 (when (and note (string-match "\\S-" note))
12619 (org-replace-escapes
12621 (list (cons "%u" (user-login-name))
12622 (cons "%U" user-full-name)
12623 (cons "%t" (format-time-string
12624 (org-time-stamp-format 'long 'inactive)
12625 org-log-note-effective-time))
12626 (cons "%T" (format-time-string
12627 (org-time-stamp-format 'long nil)
12628 org-log-note-effective-time))
12629 (cons "%d" (format-time-string
12630 (org-time-stamp-format nil 'inactive)
12631 org-log-note-effective-time))
12632 (cons "%D" (format-time-string
12633 (org-time-stamp-format nil nil)
12634 org-log-note-effective-time))
12635 (cons "%s" (if org-log-note-state
12636 (concat "\"" org-log-note-state "\"")
12638 (cons "%S" (if org-log-note-previous-state
12639 (concat "\"" org-log-note-previous-state "\"")
12641 (if lines (setq note (concat note " \\\\")))
12643 (when (or current-prefix-arg org-note-abort)
12644 (when org-log-into-drawer
12645 (org-remove-empty-drawer-at
12646 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
12647 org-log-note-marker))
12650 (with-current-buffer (marker-buffer org-log-note-marker)
12652 (goto-char org-log-note-marker)
12653 (move-marker org-log-note-marker nil)
12655 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
12656 (setq ind (save-excursion
12657 (if (ignore-errors (goto-char (org-in-item-p)))
12658 (let ((struct (org-list-struct)))
12660 (org-list-get-top-point struct) struct))
12661 (skip-chars-backward " \r\t\n")
12663 ((and (org-at-heading-p)
12664 org-adapt-indentation)
12665 (1+ (org-current-level)))
12666 ((org-at-heading-p) 0)
12667 (t (org-get-indentation))))))
12668 (setq bul (org-list-bullet-string "-"))
12669 (org-indent-line-to ind)
12670 (insert bul (pop lines))
12671 (let ((ind-body (+ (length bul) ind)))
12674 (org-indent-line-to ind-body)
12675 (insert (pop lines))))
12676 (message "Note stored")
12677 (org-back-to-heading t)
12678 (org-cycle-hide-drawers 'children)))))
12679 (set-window-configuration org-log-note-window-configuration)
12680 (with-current-buffer (marker-buffer org-log-note-return-to)
12681 (goto-char org-log-note-return-to))
12682 (move-marker org-log-note-return-to nil)
12683 (and org-log-post-message (message "%s" org-log-post-message)))
12685 (defun org-remove-empty-drawer-at (drawer pos)
12686 "Remove an empty drawer DRAWER at position POS.
12687 POS may also be a marker."
12688 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
12694 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
12695 (replace-match ""))))))
12697 (defvar org-ts-type nil)
12698 (defun org-sparse-tree (&optional arg type)
12699 "Create a sparse tree, prompt for the details.
12700 This command can create sparse trees. You first need to select the type
12701 of match used to create the tree:
12703 t Show all TODO entries.
12704 T Show entries with a specific TODO keyword.
12705 m Show entries selected by a tags/property match.
12706 p Enter a property name and its value (both with completion on existing
12707 names/values) and show entries with that property.
12708 r Show entries matching a regular expression (`/' can be used as well).
12709 b Show deadlines and scheduled items before a date.
12710 a Show deadlines and scheduled items after a date.
12711 d Show deadlines due within `org-deadline-warning-days'.
12712 D Show deadlines and scheduled items between a date range."
12714 (let (ans kwd value ts-type)
12715 (setq type (or type org-sparse-tree-default-date-type))
12716 (setq org-ts-type type)
12717 (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
12718 (cond ((eq type 'all) "all timestamps")
12719 ((eq type 'scheduled) "only scheduled")
12720 ((eq type 'deadline) "only deadline")
12721 ((eq type 'active) "only active timestamps")
12722 ((eq type 'inactive) "only inactive timestamps")
12723 ((eq type 'scheduled-or-deadline) "scheduled/deadline")
12724 (t "scheduled/deadline")))
12725 (setq ans (read-char-exclusive))
12728 (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
12730 (call-interactively 'org-check-deadlines))
12732 (call-interactively 'org-check-before-date))
12734 (call-interactively 'org-check-after-date))
12736 (call-interactively 'org-check-dates-range))
12738 (call-interactively 'org-show-todo-tree))
12740 (org-show-todo-tree '(4)))
12741 ((member ans '(?T ?m))
12742 (call-interactively 'org-match-sparse-tree))
12743 ((member ans '(?p ?P))
12744 (setq kwd (org-icompleting-read "Property: "
12745 (mapcar 'list (org-buffer-property-keys))))
12746 (setq value (org-icompleting-read "Value: "
12747 (mapcar 'list (org-property-values kwd))))
12748 (unless (string-match "\\`{.*}\\'" value)
12749 (setq value (concat "\"" value "\"")))
12750 (org-match-sparse-tree arg (concat kwd "=" value)))
12751 ((member ans '(?r ?R ?/))
12752 (call-interactively 'org-occur))
12753 (t (error "No such sparse tree command \"%c\"" ans)))))
12755 (defvar org-occur-highlights nil
12756 "List of overlays used for occur matches.")
12757 (make-variable-buffer-local 'org-occur-highlights)
12758 (defvar org-occur-parameters nil
12759 "Parameters of the active org-occur calls.
12760 This is a list, each call to org-occur pushes as cons cell,
12761 containing the regular expression and the callback, onto the list.
12762 The list can contain several entries if `org-occur' has been called
12763 several time with the KEEP-PREVIOUS argument. Otherwise, this list
12764 will only contain one set of parameters. When the highlights are
12765 removed (for example with `C-c C-c', or with the next edit (depending
12766 on `org-remove-highlights-with-change'), this variable is emptied
12768 (make-variable-buffer-local 'org-occur-parameters)
12770 (defun org-occur (regexp &optional keep-previous callback)
12771 "Make a compact tree which shows all matches of REGEXP.
12772 The tree will show the lines where the regexp matches, and all higher
12773 headlines above the match. It will also show the heading after the match,
12774 to make sure editing the matching entry is easy.
12775 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
12776 call to `org-occur' will be kept, to allow stacking of calls to this
12778 If CALLBACK is non-nil, it is a function which is called to confirm
12779 that the match should indeed be shown."
12780 (interactive "sRegexp: \nP")
12781 (when (equal regexp "")
12782 (error "Regexp cannot be empty"))
12783 (unless keep-previous
12784 (org-remove-occur-highlights nil nil t))
12785 (push (cons regexp callback) org-occur-parameters)
12788 (goto-char (point-min))
12789 (if (or (not keep-previous) ; do not want to keep
12790 (not org-occur-highlights)) ; no previous matches
12793 (while (re-search-forward regexp nil t)
12794 (when (or (not callback)
12795 (save-match-data (funcall callback)))
12796 (setq cnt (1+ cnt))
12797 (when org-highlight-sparse-tree-matches
12798 (org-highlight-new-match (match-beginning 0) (match-end 0)))
12799 (org-show-context 'occur-tree))))
12800 (when org-remove-highlights-with-change
12801 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
12803 (unless org-sparse-tree-open-archived-trees
12804 (org-hide-archived-subtrees (point-min) (point-max)))
12805 (run-hooks 'org-occur-hook)
12806 (if (org-called-interactively-p 'interactive)
12807 (message "%d match(es) for regexp %s" cnt regexp))
12810 (defun org-occur-next-match (&optional n reset)
12811 "Function for `next-error-function' to find sparse tree matches.
12812 N is the number of matches to move, when negative move backwards.
12813 RESET is entirely ignored - this function always goes back to the
12814 starting point when no match is found."
12815 (let* ((limit (if (< n 0) (point-min) (point-max)))
12816 (search-func (if (< n 0)
12817 'previous-single-char-property-change
12818 'next-single-char-property-change))
12823 (while (setq p1 (funcall search-func (point) 'org-type))
12824 (when (equal p1 limit)
12826 (error "No more matches"))
12827 (when (equal (get-char-property p1 'org-type) 'org-occur)
12831 (throw 'exit (point))))
12834 (error "No more matches"))))
12836 (defun org-show-context (&optional key)
12837 "Make sure point and context are visible.
12838 How much context is shown depends upon the variables
12839 `org-show-hierarchy-above', `org-show-following-heading',
12840 `org-show-entry-below' and `org-show-siblings'."
12841 (let ((heading-p (org-at-heading-p t))
12842 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
12843 (following-p (org-get-alist-option org-show-following-heading key))
12844 (entry-p (org-get-alist-option org-show-entry-below key))
12845 (siblings-p (org-get-alist-option org-show-siblings key)))
12847 ;; Show heading or entry text
12848 (if (and heading-p (not entry-p))
12849 (org-flag-heading nil) ; only show the heading
12850 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
12851 (org-show-hidden-entry))) ; show entire entry
12853 ;; Show next sibling, or heading below text
12855 (and (if heading-p (org-goto-sibling) (outline-next-heading))
12856 (org-flag-heading nil))))
12857 (when siblings-p (org-show-siblings))
12859 ;; show all higher headings, possibly with siblings
12861 (while (and (condition-case nil
12862 (progn (org-up-heading-all 1) t)
12865 (org-flag-heading nil)
12866 (when siblings-p (org-show-siblings))))))))
12868 (defvar org-reveal-start-hook nil
12869 "Hook run before revealing a location.")
12871 (defun org-reveal (&optional siblings)
12872 "Show current entry, hierarchy above it, and the following headline.
12873 This can be used to show a consistent set of context around locations
12874 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
12875 not t for the search context.
12877 With optional argument SIBLINGS, on each level of the hierarchy all
12878 siblings are shown. This repairs the tree structure to what it would
12879 look like when opened with hierarchical calls to `org-cycle'.
12880 With double optional argument \\[universal-argument] \\[universal-argument], \
12881 go to the parent and show the
12884 (run-hooks 'org-reveal-start-hook)
12885 (let ((org-show-hierarchy-above t)
12886 (org-show-following-heading t)
12887 (org-show-siblings (if siblings t org-show-siblings)))
12888 (org-show-context nil))
12889 (when (equal siblings '(16))
12891 (when (org-up-heading-safe)
12893 (run-hook-with-args 'org-cycle-hook 'subtree)))))
12895 (defun org-highlight-new-match (beg end)
12896 "Highlight from BEG to END and mark the highlight is an occur headline."
12897 (let ((ov (make-overlay beg end)))
12898 (overlay-put ov 'face 'secondary-selection)
12899 (overlay-put ov 'org-type 'org-occur)
12900 (push ov org-occur-highlights)))
12902 (defun org-remove-occur-highlights (&optional beg end noremove)
12903 "Remove the occur highlights from the buffer.
12904 BEG and END are ignored. If NOREMOVE is nil, remove this function
12905 from the `before-change-functions' in the current buffer."
12907 (unless org-inhibit-highlight-removal
12908 (mapc 'delete-overlay org-occur-highlights)
12909 (setq org-occur-highlights nil)
12910 (setq org-occur-parameters nil)
12912 (remove-hook 'before-change-functions
12913 'org-remove-occur-highlights 'local))))
12917 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
12918 "Regular expression matching the priority indicator.")
12920 (defvar org-remove-priority-next-time nil)
12922 (defun org-priority-up ()
12923 "Increase the priority of the current item."
12925 (org-priority 'up))
12927 (defun org-priority-down ()
12928 "Decrease the priority of the current item."
12930 (org-priority 'down))
12932 (defun org-priority (&optional action show)
12933 "Change the priority of an item.
12934 ACTION can be `set', `up', `down', or a character."
12936 (if (equal action '(4))
12937 (org-show-priority)
12938 (unless org-enable-priority-commands
12939 (error "Priority commands are disabled"))
12940 (setq action (or action 'set))
12941 (let (current new news have remove)
12943 (org-back-to-heading t)
12944 (if (looking-at org-priority-regexp)
12945 (setq current (string-to-char (match-string 2))
12948 ((eq action 'remove)
12949 (setq remove t new ?\ ))
12950 ((or (eq action 'set)
12951 (if (featurep 'xemacs) (characterp action) (integerp action)))
12952 (if (not (eq action 'set))
12954 (message "Priority %c-%c, SPC to remove: "
12955 org-highest-priority org-lowest-priority)
12957 (setq new (read-char-exclusive))))
12958 (if (and (= (upcase org-highest-priority) org-highest-priority)
12959 (= (upcase org-lowest-priority) org-lowest-priority))
12960 (setq new (upcase new)))
12961 (cond ((equal new ?\ ) (setq remove t))
12962 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
12963 (error "Priority must be between `%c' and `%c'"
12964 org-highest-priority org-lowest-priority))))
12967 (1- current) ; normal cycling
12968 ;; last priority was empty
12969 (if (eq last-command this-command)
12970 org-lowest-priority ; wrap around empty to lowest
12972 (if org-priority-start-cycle-with-default
12973 org-default-priority
12974 (1- org-default-priority))))))
12977 (1+ current) ; normal cycling
12978 ;; last priority was empty
12979 (if (eq last-command this-command)
12980 org-highest-priority ; wrap around empty to highest
12982 (if org-priority-start-cycle-with-default
12983 org-default-priority
12984 (1+ org-default-priority))))))
12985 (t (error "Invalid action")))
12986 (if (or (< (upcase new) org-highest-priority)
12987 (> (upcase new) org-lowest-priority))
12988 (if (and (memq action '(up down))
12989 (not have) (not (eq last-command this-command)))
12990 ;; `new' is from default priority
12992 "The default can not be set, see `org-default-priority' why")
12993 ;; normal cycling: `new' is beyond highest/lowest priority
12994 ;; and is wrapped around to the empty priority
12996 (setq news (format "%c" new))
12999 (replace-match "" t t nil 1)
13000 (replace-match news t t nil 2))
13002 (error "No priority cookie found in line")
13003 (let ((case-fold-search nil))
13004 (looking-at org-todo-line-regexp))
13007 (goto-char (match-end 2))
13008 (insert " [#" news "]"))
13009 (goto-char (match-beginning 3))
13010 (insert "[#" news "] "))))
13011 (org-preserve-lc (org-set-tags nil 'align)))
13013 (message "Priority removed")
13014 (message "Priority of current item set to %s" news)))))
13016 (defun org-show-priority ()
13017 "Show the priority of the current item.
13018 This priority is composed of the main priority given with the [#A] cookies,
13019 and by additional input from the age of a schedules or deadline entry."
13021 (let ((pri (if (eq major-mode 'org-agenda-mode)
13022 (org-get-at-bol 'priority)
13025 (beginning-of-line)
13026 (and (looking-at org-heading-regexp)
13027 (org-get-priority (match-string 0))))))))
13028 (message "Priority is %d" (if pri pri -1000))))
13030 (defun org-get-priority (s)
13031 "Find priority cookie and return priority."
13033 (if (functionp org-get-priority-function)
13034 (funcall org-get-priority-function)
13035 (if (not (string-match org-priority-regexp s))
13036 (* 1000 (- org-lowest-priority org-default-priority))
13037 (* 1000 (- org-lowest-priority
13038 (string-to-char (match-string 2 s))))))))
13042 (defvar org-agenda-archives-mode)
13043 (defvar org-map-continue-from nil
13044 "Position from where mapping should continue.
13045 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
13047 (defvar org-scanner-tags nil
13048 "The current tag list while the tags scanner is running.")
13049 (defvar org-trust-scanner-tags nil
13050 "Should `org-get-tags-at' use the tags for the scanner.
13051 This is for internal dynamical scoping only.
13052 When this is non-nil, the function `org-get-tags-at' will return the value
13053 of `org-scanner-tags' instead of building the list by itself. This
13054 can lead to large speed-ups when the tags scanner is used in a file with
13055 many entries, and when the list of tags is retrieved, for example to
13056 obtain a list of properties. Building the tags list for each entry in such
13057 a file becomes an N^2 operation - but with this variable set, it scales
13060 (defun org-scan-tags (action matcher todo-only &optional start-level)
13061 "Scan headline tags with inheritance and produce output ACTION.
13063 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
13064 or `agenda' to produce an entry list for an agenda view. It can also be
13065 a Lisp form or a function that should be called at each matched headline, in
13066 this case the return value is a list of all return values from these calls.
13068 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
13069 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
13070 only lines with a not-done TODO keyword are included in the output.
13071 This should be the same variable that was scoped into
13072 and set by `org-make-tags-matcher' when it constructed MATCHER.
13074 START-LEVEL can be a string with asterisks, reducing the scope to
13075 headlines matching this string."
13076 (require 'org-agenda)
13077 (let* ((re (concat "^"
13079 ;; Get the correct level to match
13080 (concat "\\*\\{" (number-to-string start-level) "\\} ")
13081 org-outline-regexp)
13083 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
13084 (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
13085 (props (list 'face 'default
13086 'done-face 'org-agenda-done
13087 'undone-face 'default
13088 'mouse-face 'highlight
13089 'org-not-done-regexp org-not-done-regexp
13090 'org-todo-regexp org-todo-regexp
13091 'org-complex-heading-regexp org-complex-heading-regexp
13093 (format "mouse-2 or RET jump to org file %s"
13094 (abbreviate-file-name
13095 (or (buffer-file-name (buffer-base-buffer))
13096 (buffer-name (buffer-base-buffer)))))))
13097 (case-fold-search nil)
13098 (org-map-continue-from nil)
13099 lspos tags tags-list
13100 (tags-alist (list (cons 0 org-file-tags)))
13101 (llast 0) rtn rtn1 level category i txt
13102 todo marker entry priority)
13103 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
13104 (setq action (list 'lambda nil action)))
13106 (goto-char (point-min))
13107 (when (eq action 'sparse-tree)
13109 (org-remove-occur-highlights))
13110 (while (re-search-forward re nil t)
13111 (setq org-map-continue-from nil)
13113 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
13114 tags (if (match-end 4) (org-match-string-no-properties 4)))
13115 (goto-char (setq lspos (match-beginning 0)))
13116 (setq level (org-reduced-level (funcall outline-level))
13117 category (org-get-category))
13118 (setq i llast llast level)
13119 ;; remove tag lists from same and sublevels
13120 (while (>= i level)
13121 (when (setq entry (assoc i tags-alist))
13122 (setq tags-alist (delete entry tags-alist)))
13124 ;; add the next tags
13126 (setq tags (org-split-string tags ":")
13128 (cons (cons level tags) tags-alist)))
13129 ;; compile tags for current headline
13131 (if org-use-tag-inheritance
13132 (apply 'append (mapcar 'cdr (reverse tags-alist)))
13134 org-scanner-tags tags-list)
13135 (when org-use-tag-inheritance
13136 (setcdr (car tags-alist)
13137 (mapcar (lambda (x)
13138 (setq x (copy-sequence x))
13139 (org-add-prop-inherited x))
13140 (cdar tags-alist))))
13141 (when (and tags org-use-tag-inheritance
13142 (or (not (eq t org-use-tag-inheritance))
13143 org-tags-exclude-from-inheritance))
13144 ;; selective inheritance, remove uninherited ones
13145 (setcdr (car tags-alist)
13146 (org-remove-uninherited-tags (cdar tags-alist))))
13149 ;; eval matcher only when the todo condition is OK
13150 (and (or (not todo-only) (member todo org-not-done-keywords))
13151 (let ((case-fold-search t) (org-trust-scanner-tags t))
13154 ;; Call the skipper, but return t if it does not skip,
13155 ;; so that the `and' form continues evaluating
13157 (unless (eq action 'sparse-tree) (org-agenda-skip))
13160 ;; Check if timestamps are deselecting this entry
13161 (or (not todo-only)
13162 (and (member todo org-not-done-keywords)
13163 (or (not org-agenda-tags-todo-honor-ignore-options)
13164 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
13166 ;; select this headline
13168 ((eq action 'sparse-tree)
13169 (and org-highlight-sparse-tree-matches
13170 (org-get-heading) (match-end 0)
13171 (org-highlight-new-match
13172 (match-beginning 1) (match-end 1)))
13173 (org-show-context 'tags-tree))
13174 ((eq action 'agenda)
13175 (setq txt (org-agenda-format-item
13178 (if (eq org-tags-match-list-sublevels 'indented)
13179 (make-string (1- level) ?.) "")
13183 priority (org-get-priority txt))
13185 (setq marker (org-agenda-new-marker))
13186 (org-add-props txt props
13187 'org-marker marker 'org-hd-marker marker 'org-category category
13189 'priority priority 'type "tagsmatch")
13191 ((functionp action)
13192 (setq org-map-continue-from nil)
13194 (setq rtn1 (funcall action))
13196 (t (error "Invalid action")))
13198 ;; if we are to skip sublevels, jump to end of subtree
13199 (unless org-tags-match-list-sublevels
13200 (org-end-of-subtree t)
13201 (backward-char 1))))
13202 ;; Get the correct position from where to continue
13203 (if org-map-continue-from
13204 (goto-char org-map-continue-from)
13205 (and (= (point) lspos) (end-of-line 1)))))
13206 (when (and (eq action 'sparse-tree)
13207 (not org-sparse-tree-open-archived-trees))
13208 (org-hide-archived-subtrees (point-min) (point-max)))
13211 (defun org-remove-uninherited-tags (tags)
13212 "Remove all tags that are not inherited from the list TAGS."
13214 ((eq org-use-tag-inheritance t)
13215 (if org-tags-exclude-from-inheritance
13216 (org-delete-all org-tags-exclude-from-inheritance tags)
13218 ((not org-use-tag-inheritance) nil)
13219 ((stringp org-use-tag-inheritance)
13222 (if (and (string-match org-use-tag-inheritance x)
13223 (not (member x org-tags-exclude-from-inheritance)))
13226 ((listp org-use-tag-inheritance)
13229 (if (member x org-use-tag-inheritance) x nil))
13232 (defun org-match-sparse-tree (&optional todo-only match)
13233 "Create a sparse tree according to tags string MATCH.
13234 MATCH can contain positive and negative selection of tags, like
13235 \"+WORK+URGENT-WITHBOSS\".
13236 If optional argument TODO-ONLY is non-nil, only select lines that are
13239 (org-agenda-prepare-buffers (list (current-buffer)))
13240 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
13242 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
13244 (defvar org-cached-props nil)
13245 (defun org-cached-entry-get (pom property)
13246 (if (or (eq t org-use-property-inheritance)
13247 (and (stringp org-use-property-inheritance)
13248 (string-match org-use-property-inheritance property))
13249 (and (listp org-use-property-inheritance)
13250 (member property org-use-property-inheritance)))
13251 ;; Caching is not possible, check it directly
13252 (org-entry-get pom property 'inherit)
13253 ;; Get all properties, so that we can do complicated checks easily
13254 (cdr (assoc property (or org-cached-props
13255 (setq org-cached-props
13256 (org-entry-properties pom)))))))
13258 (defun org-global-tags-completion-table (&optional files)
13259 "Return the list of all tags in all agenda buffer/files.
13260 Optional FILES argument is a list of files which can be used
13261 instead of the agenda files."
13268 (set-buffer (find-file-noselect file))
13269 (append (org-get-buffer-tags)
13270 (mapcar (lambda (x) (if (stringp (car-safe x))
13271 (list (car-safe x)) nil))
13273 (if (and files (car files))
13275 (org-agenda-files))))))))
13277 (defun org-make-tags-matcher (match)
13278 "Create the TAGS/TODO matcher form for the selection string MATCH.
13280 The variable `todo-only' is scoped dynamically into this function.
13281 It will be set to t if the matcher restricts matching to TODO entries,
13282 otherwise will not be touched.
13284 Returns a cons of the selection string MATCH and the constructed
13285 lisp form implementing the matcher. The matcher is to be evaluated
13286 at an Org entry, with point on the headline, and returns t if the
13287 entry matches the selection string MATCH. The returned lisp form
13288 references two variables with information about the entry, which
13289 must be bound around the form's evaluation: todo, the TODO keyword
13290 at the entry (or nil of none); and tags-list, the list of all tags
13291 at the entry including inherited ones. Additionally, the category
13292 of the entry (if any) must be specified as the text property
13293 'org-category on the headline.
13295 See also `org-scan-tags'.
13297 (declare (special todo-only))
13298 (unless (boundp 'todo-only)
13299 (error "org-make-tags-matcher expects todo-only to be scoped in"))
13301 ;; Get a new match request, with completion
13302 (let ((org-last-tags-completion-table
13303 (org-global-tags-completion-table)))
13304 (setq match (org-completing-read-no-i
13305 "Match: " 'org-tags-completion-function nil nil nil
13306 'org-tags-history))))
13308 ;; Parse the string and create a lisp form
13309 (let ((match0 match)
13310 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
13312 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
13313 orterms term orlist re-p str-p level-p level-op time-p
13314 prop-p pn pv po gv rest)
13315 (if (string-match "/+" match)
13316 ;; match contains also a todo-matching request
13318 (setq tagsmatch (substring match 0 (match-beginning 0))
13319 todomatch (substring match (match-end 0)))
13320 (if (string-match "^!" todomatch)
13321 (setq todo-only t todomatch (substring todomatch 1)))
13322 (if (string-match "^\\s-*$" todomatch)
13323 (setq todomatch nil)))
13324 ;; only matching tags
13325 (setq tagsmatch match todomatch nil))
13327 ;; Make the tags matcher
13328 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
13329 (setq tagsmatcher t)
13330 (setq orterms (org-split-string tagsmatch "|") orlist nil)
13331 (while (setq term (pop orterms))
13332 (while (and (equal (substring term -1) "\\") orterms)
13333 (setq term (concat term "|" (pop orterms)))) ; repair bad split
13334 (while (string-match re term)
13335 (setq rest (substring term (match-end 0))
13336 minus (and (match-end 1)
13337 (equal (match-string 1 term) "-"))
13338 tag (save-match-data (replace-regexp-in-string
13340 (match-string 2 term)))
13341 re-p (equal (string-to-char tag) ?{)
13342 level-p (match-end 4)
13343 prop-p (match-end 5)
13345 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
13347 (setq level-op (org-op-to-function (match-string 3 term)))
13348 `(,level-op level ,(string-to-number
13349 (match-string 4 term))))
13351 (setq pn (match-string 5 term)
13352 po (match-string 6 term)
13353 pv (match-string 7 term)
13354 re-p (equal (string-to-char pv) ?{)
13355 str-p (equal (string-to-char pv) ?\")
13356 time-p (save-match-data
13357 (string-match "^\"[[<].*[]>]\"$" pv))
13358 pv (if (or re-p str-p) (substring pv 1 -1) pv))
13359 (if time-p (setq pv (org-matcher-time pv)))
13360 (setq po (org-op-to-function po (if time-p 'time str-p)))
13362 ((equal pn "CATEGORY")
13363 (setq gv '(get-text-property (point) 'org-category)))
13367 (setq gv `(org-cached-entry-get nil ,pn))))
13370 `(not (string-match ,pv (or ,gv "")))
13371 `(string-match ,pv (or ,gv "")))
13373 `(,po (or ,gv "") ,pv)
13374 `(,po (string-to-number (or ,gv ""))
13375 ,(string-to-number pv) ))))
13376 (t `(member ,tag tags-list)))
13377 mm (if minus (list 'not mm) mm)
13379 (push mm tagsmatcher))
13380 (push (if (> (length tagsmatcher) 1)
13381 (cons 'and tagsmatcher)
13384 (setq tagsmatcher nil))
13385 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
13387 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
13388 ;; Make the todo matcher
13389 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
13390 (setq todomatcher t)
13391 (setq orterms (org-split-string todomatch "|") orlist nil)
13392 (while (setq term (pop orterms))
13393 (while (string-match re term)
13394 (setq minus (and (match-end 1)
13395 (equal (match-string 1 term) "-"))
13396 kwd (match-string 2 term)
13397 re-p (equal (string-to-char kwd) ?{)
13398 term (substring term (match-end 0))
13400 `(string-match ,(substring kwd 1 -1) todo)
13401 (list 'equal 'todo kwd))
13402 mm (if minus (list 'not mm) mm))
13403 (push mm todomatcher))
13404 (push (if (> (length todomatcher) 1)
13405 (cons 'and todomatcher)
13408 (setq todomatcher nil))
13409 (setq todomatcher (if (> (length orlist) 1)
13410 (cons 'or orlist) (car orlist))))
13412 ;; Return the string and lisp forms of the matcher
13413 (setq matcher (if todomatcher
13414 (list 'and tagsmatcher todomatcher)
13417 (setq matcher (list 'and '(member todo org-not-done-keywords)
13419 (cons match0 matcher)))
13421 (defun org-op-to-function (op &optional stringp)
13422 "Turn an operator into the appropriate function."
13425 ((equal op "<" ) '(< string< org-time<))
13426 ((equal op ">" ) '(> org-string> org-time>))
13427 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
13428 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
13429 ((member op '("=" "==")) '(= string= org-time=))
13430 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
13431 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
13433 (defun org<> (a b) (not (= a b)))
13434 (defun org-string<= (a b) (or (string= a b) (string< a b)))
13435 (defun org-string>= (a b) (not (string< a b)))
13436 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
13437 (defun org-string<> (a b) (not (string= a b)))
13438 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
13439 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
13440 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
13441 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
13442 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
13443 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
13445 "Convert S to a floating point time.
13446 If S is already a number, just return it. If it is a string, parse
13447 it as a time string and apply `float-time' to it. If S is nil, just return 0."
13451 (condition-case nil
13452 (float-time (apply 'encode-time (org-parse-time-string s)))
13456 (defun org-time-today ()
13457 "Time in seconds today at 0:00.
13458 Returns the float number of seconds since the beginning of the
13459 epoch to the beginning of today (00:00)."
13460 (float-time (apply 'encode-time
13461 (append '(0 0 0) (nthcdr 3 (decode-time))))))
13463 (defun org-matcher-time (s)
13464 "Interpret a time comparison value."
13467 ((string= s "<now>") (float-time))
13468 ((string= s "<today>") (org-time-today))
13469 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
13470 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
13471 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
13472 (+ (org-time-today)
13473 (* (string-to-number (match-string 1 s))
13474 (cdr (assoc (match-string 2 s)
13475 '(("d" . 86400.0) ("w" . 604800.0)
13476 ("m" . 2678400.0) ("y" . 31557600.0)))))))
13479 (defun org-match-any-p (re list)
13480 "Does re match any element of list?"
13481 (setq list (mapcar (lambda (x) (string-match re x)) list))
13484 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
13485 (defvar org-tags-overlay (make-overlay 1 1))
13486 (org-detach-overlay org-tags-overlay)
13488 (defun org-get-local-tags-at (&optional pos)
13489 "Get a list of tags defined in the current headline."
13490 (org-get-tags-at pos 'local))
13492 (defun org-get-local-tags ()
13493 "Get a list of tags defined in the current headline."
13494 (org-get-tags-at nil 'local))
13496 (defun org-get-tags-at (&optional pos local)
13497 "Get a list of all headline tags applicable at POS.
13498 POS defaults to point. If tags are inherited, the list contains
13499 the targets in the same sequence as the headlines appear, i.e.
13500 the tags of the current headline come last.
13501 When LOCAL is non-nil, only return tags from the current headline,
13502 ignore inherited ones."
13504 (if (and org-trust-scanner-tags
13505 (or (not pos) (equal pos (point)))
13508 (let (tags ltags lastpos parent)
13512 (goto-char (or pos (point)))
13515 (condition-case nil
13517 (org-back-to-heading t)
13518 (while (not (equal lastpos (point)))
13519 (setq lastpos (point))
13521 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
13522 (setq ltags (org-split-string
13523 (org-match-string-no-properties 1) ":"))
13525 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
13528 (org-remove-uninherited-tags ltags)
13531 (or org-use-tag-inheritance (throw 'done t))
13532 (if local (throw 'done t))
13533 (or (org-up-heading-safe) (error nil))
13538 (reverse (delete-dups
13540 (org-remove-uninherited-tags
13541 org-file-tags) tags)))))))))
13543 (defun org-add-prop-inherited (s)
13544 (add-text-properties 0 (length s) '(inherited t) s)
13547 (defun org-toggle-tag (tag &optional onoff)
13548 "Toggle the tag TAG for the current line.
13549 If ONOFF is `on' or `off', don't toggle but set to this state."
13552 (org-back-to-heading t)
13553 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
13556 (setq current (match-string 1))
13557 (replace-match ""))
13559 (setq current (nreverse (org-split-string current ":")))
13563 (or (member tag current) (push tag current)))
13565 (or (not (member tag current)) (setq current (delete tag current))))
13566 (t (if (member tag current)
13567 (setq current (delete tag current))
13569 (push tag current))))
13573 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
13574 (org-set-tags nil t))
13575 (delete-horizontal-space))
13576 (run-hooks 'org-after-tags-change-hook))
13579 (defun org-align-tags-here (to-col)
13580 ;; Assumes that this is a headline
13581 (let ((pos (point)) (col (current-column)) ncol tags-l p)
13582 (beginning-of-line 1)
13583 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13584 (< pos (match-beginning 2)))
13586 (setq tags-l (- (match-end 2) (match-beginning 2)))
13587 (goto-char (match-beginning 1))
13589 (delete-region (point) (1+ (match-beginning 2)))
13590 (setq ncol (max (current-column)
13594 (- (abs to-col) tags-l))))
13596 (insert (make-string (- ncol (current-column)) ?\ ))
13597 (setq ncol (current-column))
13598 (when indent-tabs-mode (tabify p (point-at-eol)))
13599 (org-move-to-column (min ncol col) t))
13602 (defun org-set-tags-command (&optional arg just-align)
13603 "Call the set-tags command for the current entry."
13605 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
13606 (org-set-tags arg just-align)
13608 (unless (and (org-region-active-p)
13609 org-loop-over-headlines-in-active-region)
13610 (org-back-to-heading t))
13611 (org-set-tags arg just-align))))
13613 (defun org-set-tags-to (data)
13614 "Set the tags of the current entry to DATA, replacing the current tags.
13615 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
13616 If DATA is nil or the empty string, any tags will be removed."
13617 (interactive "sTags: ")
13621 ((equal data "") "")
13623 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
13626 (concat ":" (mapconcat 'identity data ":") ":"))))
13629 (org-back-to-heading t)
13630 (when (looking-at org-complex-heading-regexp)
13633 (goto-char (match-beginning 5))
13635 (delete-region (point) (point-at-eol))
13636 (org-set-tags nil 'align))
13637 (goto-char (point-at-eol))
13639 (org-set-tags nil 'align)))
13640 (beginning-of-line 1)
13641 (if (looking-at ".*?\\([ \t]+\\)$")
13642 (delete-region (match-beginning 1) (match-end 1))))))
13644 (defun org-align-all-tags ()
13645 "Align the tags i all headings."
13648 (or (ignore-errors (org-back-to-heading t))
13649 (outline-next-heading))
13650 (if (org-at-heading-p)
13652 (message "No headings"))))
13654 (defvar org-indent-indentation-per-level)
13655 (defun org-set-tags (&optional arg just-align)
13656 "Set the tags for the current headline.
13657 With prefix ARG, realign all tags in headings in the current buffer."
13659 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13660 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
13661 'region-start-level 'region))
13662 org-loop-over-headlines-in-active-region)
13664 ;; We don't use ARG and JUST-ALIGN here these args are not
13665 ;; useful when looping over headlines
13667 org-loop-over-headlines-in-active-region
13668 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
13669 (let* ((re org-outline-regexp-bol)
13670 (current (unless arg (org-get-tags-string)))
13671 (col (current-column))
13672 (org-setting-tags t)
13673 table current-tags inherited-tags ; computed below when needed
13674 tags p0 c0 c1 rpl di tc level)
13677 (goto-char (point-min))
13678 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
13679 (while (re-search-forward re nil t)
13680 (org-set-tags nil t)
13682 (message "All tags realigned to column %d" org-tags-column))
13684 (setq tags current)
13685 ;; Get a new set of tags from the user
13687 (setq table (append org-tag-persistent-alist
13688 (or org-tag-alist (org-get-buffer-tags))
13690 org-complete-tags-always-offer-all-agenda-tags
13691 (org-global-tags-completion-table
13692 (org-agenda-files))))
13693 org-last-tags-completion-table table
13694 current-tags (org-split-string current ":")
13695 inherited-tags (nreverse
13696 (nthcdr (length current-tags)
13697 (nreverse (org-get-tags-at))))
13699 (if (or (eq t org-use-fast-tag-selection)
13700 (and org-use-fast-tag-selection
13701 (delq nil (mapcar 'cdr table))))
13702 (org-fast-tag-selection
13703 current-tags inherited-tags table
13704 (if org-fast-tag-selection-include-todo
13705 org-todo-key-alist))
13706 (let ((org-add-colon-after-tag-completion (< 1 (length table))))
13708 (org-icompleting-read "Tags: "
13709 'org-tags-completion-function
13710 nil nil current 'org-tags-history))))))
13711 (while (string-match "[-+&]+" tags)
13712 ;; No boolean logic, just a list
13713 (setq tags (replace-match ":" t t tags))))
13715 (setq tags (replace-regexp-in-string "[,]" ":" tags))
13717 (if org-tags-sort-function
13718 (setq tags (mapconcat 'identity
13719 (sort (org-split-string
13720 tags (org-re "[^[:alnum:]_@#%]+"))
13721 org-tags-sort-function) ":")))
13723 (if (string-match "\\`[\t ]*\\'" tags)
13725 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
13726 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
13728 ;; Insert new tags at the correct column
13729 (beginning-of-line 1)
13730 (setq level (or (and (looking-at org-outline-regexp)
13731 (- (match-end 0) (point) 1))
13734 ((and (equal current "") (equal tags "")))
13735 ((re-search-forward
13736 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
13738 (if (equal tags "")
13740 (goto-char (match-beginning 0))
13741 (setq c0 (current-column)
13742 ;; compute offset for the case of org-indent-mode active
13743 di (if org-indent-mode
13744 (* (1- org-indent-indentation-per-level) (1- level))
13746 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
13747 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
13748 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
13749 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
13750 (replace-match rpl t t)
13751 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
13753 (t (error "Tags alignment failed")))
13754 (org-move-to-column col)
13756 (run-hooks 'org-after-tags-change-hook))))))
13758 (defun org-change-tag-in-region (beg end tag off)
13759 "Add or remove TAG for each entry in the region.
13760 This works in the agenda, and also in an org-mode buffer."
13762 (list (region-beginning) (region-end)
13763 (let ((org-last-tags-completion-table
13764 (if (derived-mode-p 'org-mode)
13765 (org-get-buffer-tags)
13766 (org-global-tags-completion-table))))
13767 (org-icompleting-read
13768 "Tag: " 'org-tags-completion-function nil nil nil
13769 'org-tags-history))
13771 (message "[s]et or
[r]emove? ")
13772 (equal (read-char-exclusive) ?r))))
13773 (if (fboundp 'deactivate-mark) (deactivate-mark))
13774 (let ((agendap (equal major-mode 'org-agenda-mode))
13775 l1 l2 m buf pos newhead (cnt 0))
13777 (setq l2 (1- (org-current-line)))
13779 (setq l1 (org-current-line))
13780 (loop for l from l1 to l2 do
13782 (setq m (get-text-property (point) 'org-hd-marker))
13783 (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
13785 (setq buf (if agendap (marker-buffer m) (current-buffer))
13786 pos (if agendap m (point)))
13787 (with-current-buffer buf
13791 (setq cnt (1+ cnt))
13792 (org-toggle-tag tag (if off 'off 'on))
13793 (setq newhead (org-get-heading)))))
13794 (and agendap (org-agenda-change-all-lines newhead m))))
13795 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
13797 (defun org-tags-completion-function (string predicate &optional flag)
13798 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
13799 (confirm (lambda (x) (stringp (car x)))))
13800 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
13801 (setq s1 (match-string 1 string)
13802 s2 (match-string 2 string))
13803 (setq s1 "" s2 string))
13807 (setq rtn (try-completion s2 ctable confirm))
13810 (concat s1 s2 (substring rtn (length s2))
13811 (if (and org-add-colon-after-tag-completion
13812 (assoc rtn ctable))
13817 (all-completions s2 ctable confirm)
13821 (assoc s2 ctable)))
13824 (defun org-fast-tag-insert (kwd tags face &optional end)
13825 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
13826 (insert (format "%-12s" (concat kwd ":"))
13827 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
13830 (defun org-fast-tag-show-exit (flag)
13833 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
13834 (replace-match ""))
13837 (org-move-to-column (- (window-width) 19) t)
13838 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
13840 (defun org-set-current-tags-overlay (current prefix)
13841 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
13842 (if (featurep 'xemacs)
13843 (org-overlay-display org-tags-overlay (concat prefix s)
13844 'secondary-selection)
13845 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
13846 (org-overlay-display org-tags-overlay (concat prefix s)))))
13848 (defvar org-last-tag-selection-key nil)
13849 (defun org-fast-tag-selection (current inherited table &optional todo-table)
13850 "Fast tag selection with single keys.
13851 CURRENT is the current list of tags in the headline, INHERITED is the
13852 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
13853 possibly with grouping information. TODO-TABLE is a similar table with
13854 TODO keywords, should these have keys assigned to them.
13855 If the keys are nil, a-z are automatically assigned.
13856 Returns the new tags string, or nil to not change the current settings."
13857 (let* ((fulltable (append table todo-table))
13858 (maxlen (apply 'max (mapcar
13860 (if (stringp (car x)) (string-width (car x)) 0))
13862 (buf (current-buffer))
13863 (expert (eq org-fast-tag-selection-single-key 'expert))
13865 (fwidth (+ maxlen 3 1 3))
13866 (ncol (/ (- (window-width) 4) fwidth))
13869 tg cnt e c char c1 c2 ntable tbl rtn
13870 ov-start ov-end ov-prefix
13871 (exit-after-next org-fast-tag-selection-single-key)
13872 (done-keywords org-done-keywords)
13875 (beginning-of-line 1)
13877 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13878 (setq ov-start (match-beginning 1)
13879 ov-end (match-end 1)
13881 (setq ov-start (1- (point-at-eol))
13882 ov-end (1+ ov-start))
13883 (skip-chars-forward "^\n\r")
13886 (buffer-substring (1- (point)) (point))
13887 (if (> (current-column) org-tags-column)
13889 (make-string (- org-tags-column (current-column)) ?\ ))))))
13890 (move-overlay org-tags-overlay ov-start ov-end)
13891 (save-window-excursion
13893 (set-buffer (get-buffer-create " *Org tags*"))
13894 (delete-other-windows)
13895 (split-window-vertically)
13896 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
13898 (org-set-local 'org-done-keywords done-keywords)
13899 (org-fast-tag-insert "Inherited" inherited i-face "\n")
13900 (org-fast-tag-insert "Current" current c-face "\n\n")
13901 (org-fast-tag-show-exit exit-after-next)
13902 (org-set-current-tags-overlay current ov-prefix)
13903 (setq tbl fulltable char ?a cnt 0)
13904 (while (setq e (pop tbl))
13906 ((equal (car e) :startgroup)
13907 (push '() groups) (setq ingroup t)
13908 (when (not (= cnt 0))
13911 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
13912 ((equal (car e) :endgroup)
13913 (setq ingroup nil cnt 0)
13914 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
13915 ((equal e '(:newline))
13916 (when (not (= cnt 0))
13920 (while (equal (car tbl) '(:newline))
13922 (setq tbl (cdr tbl)))))
13924 (setq tg (copy-sequence (car e)) c2 nil)
13927 ;; automatically assign a character.
13928 (setq c1 (string-to-char
13929 (downcase (substring
13930 tg (if (= (string-to-char tg) ?@) 1 0)))))
13931 (if (or (rassoc c1 ntable) (rassoc c1 table))
13932 (while (or (rassoc char ntable) (rassoc char table))
13933 (setq char (1+ char)))
13935 (setq c (or c2 char)))
13936 (if ingroup (push tg (car groups)))
13937 (setq tg (org-add-props tg nil 'face
13939 ((not (assoc tg table))
13940 (org-get-todo-face tg))
13941 ((member tg current) c-face)
13942 ((member tg inherited) i-face))))
13943 (if (and (= cnt 0) (not ingroup)) (insert " "))
13944 (insert "[" c "] " tg (make-string
13945 (- fwidth 4 (length tg)) ?\ ))
13946 (push (cons tg c) ntable)
13947 (when (= (setq cnt (1+ cnt)) ncol)
13949 (if ingroup (insert " "))
13951 (setq ntable (nreverse ntable))
13953 (goto-char (point-min))
13954 (if (not expert) (org-fit-window-to-buffer))
13958 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
13959 (if (not groups) "no " "")
13960 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
13961 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13962 (setq org-last-tag-selection-key c)
13964 ((= c ?\r) (throw 'exit t))
13966 (setq groups (not groups))
13967 (goto-char (point-min))
13968 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
13971 (org-fast-tag-show-exit
13972 (setq exit-after-next (not exit-after-next)))
13974 (delete-other-windows)
13975 (set-window-buffer (split-window-vertically) " *Org tags*")
13976 (org-switch-to-buffer-other-window " *Org tags*")
13977 (org-fit-window-to-buffer)))
13979 (and (= c ?q) (not (rassoc c ntable))))
13980 (org-detach-overlay org-tags-overlay)
13981 (setq quit-flag t))
13984 (if exit-after-next (setq exit-after-next 'now)))
13986 (condition-case nil
13987 (setq tg (org-icompleting-read
13990 (with-current-buffer buf
13991 (org-get-buffer-tags)))))
13992 (quit (setq tg "")))
13993 (when (string-match "\\S-" tg)
13994 (add-to-list 'buffer-tags (list tg))
13995 (if (member tg current)
13996 (setq current (delete tg current))
13997 (push tg current)))
13998 (if exit-after-next (setq exit-after-next 'now)))
13999 ((setq e (rassoc c todo-table) tg (car e))
14000 (with-current-buffer buf
14001 (save-excursion (org-todo tg)))
14002 (if exit-after-next (setq exit-after-next 'now)))
14003 ((setq e (rassoc c ntable) tg (car e))
14004 (if (member tg current)
14005 (setq current (delete tg current))
14006 (loop for g in groups do
14009 (setq current (delete x current)))
14012 (if exit-after-next (setq exit-after-next 'now))))
14014 ;; Create a sorted list
14018 (assoc b (cdr (memq (assoc a ntable) ntable))))))
14019 (if (eq exit-after-next 'now) (throw 'exit t))
14020 (goto-char (point-min))
14021 (beginning-of-line 2)
14022 (delete-region (point) (point-at-eol))
14023 (org-fast-tag-insert "Current" current c-face)
14024 (org-set-current-tags-overlay current ov-prefix)
14025 (while (re-search-forward
14026 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
14027 (setq tg (match-string 1))
14028 (add-text-properties
14029 (match-beginning 1) (match-end 1)
14032 ((member tg current) c-face)
14033 ((member tg inherited) i-face)
14034 (t (get-text-property (match-beginning 1) 'face))))))
14035 (goto-char (point-min)))))
14036 (org-detach-overlay org-tags-overlay)
14038 (mapconcat 'identity current ":")
14041 (defun org-get-tags-string ()
14042 "Get the TAGS string in the current headline."
14043 (unless (org-at-heading-p t)
14044 (error "Not on a heading"))
14046 (beginning-of-line 1)
14047 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14048 (org-match-string-no-properties 1)
14051 (defun org-get-tags ()
14052 "Get the list of tags specified in the current headline."
14053 (org-split-string (org-get-tags-string) ":"))
14055 (defun org-get-buffer-tags ()
14056 "Get a table of all tags used in the buffer, for completion."
14059 (goto-char (point-min))
14060 (while (re-search-forward
14061 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
14062 (when (equal (char-after (point-at-bol 0)) ?*)
14063 (mapc (lambda (x) (add-to-list 'tags x))
14064 (org-split-string (org-match-string-no-properties 1) ":")))))
14065 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
14066 (mapcar 'list tags)))
14068 ;;;; The mapping API
14070 (defun org-map-entries (func &optional match scope &rest skip)
14071 "Call FUNC at each headline selected by MATCH in SCOPE.
14073 FUNC is a function or a lisp form. The function will be called without
14074 arguments, with the cursor positioned at the beginning of the headline.
14075 The return values of all calls to the function will be collected and
14076 returned as a list.
14078 The call to FUNC will be wrapped into a save-excursion form, so FUNC
14079 does not need to preserve point. After evaluation, the cursor will be
14080 moved to the end of the line (presumably of the headline of the
14081 processed entry) and search continues from there. Under some
14082 circumstances, this may not produce the wanted results. For example,
14083 if you have removed (e.g. archived) the current (sub)tree it could
14084 mean that the next entry will be skipped entirely. In such cases, you
14085 can specify the position from where search should continue by making
14086 FUNC set the variable `org-map-continue-from' to the desired buffer
14089 MATCH is a tags/property/todo match as it is used in the agenda tags view.
14090 Only headlines that are matched by this query will be considered during
14091 the iteration. When MATCH is nil or t, all headlines will be
14092 visited by the iteration.
14094 SCOPE determines the scope of this command. It can be any of:
14096 nil The current buffer, respecting the restriction if any
14097 tree The subtree started with the entry at point
14098 region The entries within the active region, if any
14100 The entries within the active region, but only those at
14101 the same level than the first one.
14102 file The current buffer, without restriction
14104 The current buffer, and any archives associated with it
14105 agenda All agenda files
14106 agenda-with-archives
14107 All agenda files with any archive files associated with them
14109 If this is a list, all files in the list will be scanned
14111 The remaining args are treated as settings for the skipping facilities of
14112 the scanner. The following items can be given here:
14114 archive skip trees with the archive tag.
14115 comment skip trees with the COMMENT keyword
14116 function or Emacs Lisp form:
14117 will be used as value for `org-agenda-skip-function', so whenever
14118 the function returns t, FUNC will not be called for that
14119 entry and search will continue from the point where the
14120 function leaves it.
14122 If your function needs to retrieve the tags including inherited tags
14123 at the *current* entry, you can use the value of the variable
14124 `org-scanner-tags' which will be much faster than getting the value
14125 with `org-get-tags-at'. If your function gets properties with
14126 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
14127 to t around the call to `org-entry-properties' to get the same speedup.
14128 Note that if your function moves around to retrieve tags and properties at
14129 a *different* entry, you cannot use these techniques."
14130 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
14131 (not (org-region-active-p)))
14132 (let* ((org-agenda-archives-mode nil) ; just to make sure
14133 (org-agenda-skip-archived-trees (memq 'archive skip))
14134 (org-agenda-skip-comment-trees (memq 'comment skip))
14135 (org-agenda-skip-function
14136 (car (org-delete-all '(comment archive) skip)))
14137 (org-tags-match-list-sublevels t)
14138 (start-level (eq scope 'region-start-level))
14140 org-todo-keywords-for-agenda
14141 org-done-keywords-for-agenda
14142 org-todo-keyword-alist-for-agenda
14143 org-drawers-for-agenda
14144 org-tag-alist-for-agenda
14148 ((eq match t) (setq matcher t))
14149 ((eq match nil) (setq matcher t))
14150 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
14154 (cond ((eq scope 'tree)
14155 (org-back-to-heading t)
14156 (org-narrow-to-subtree)
14158 ((and (or (eq scope 'region) (eq scope 'region-start-level))
14159 (org-region-active-p))
14160 ;; If needed, set start-level to a string like "2"
14163 (goto-char (region-beginning))
14164 (unless (org-at-heading-p) (outline-next-heading))
14165 (setq start-level (org-current-level))))
14166 (narrow-to-region (region-beginning)
14168 (goto-char (region-end))
14169 (unless (and (bolp) (org-at-heading-p))
14170 (outline-next-heading))
14176 (org-agenda-prepare-buffers
14177 (list (buffer-file-name (current-buffer))))
14178 (setq res (org-scan-tags func matcher todo-only start-level)))
14179 ;; Get the right scope
14181 ((and scope (listp scope) (symbolp (car scope)))
14182 (setq scope (eval scope)))
14183 ((eq scope 'agenda)
14184 (setq scope (org-agenda-files t)))
14185 ((eq scope 'agenda-with-archives)
14186 (setq scope (org-agenda-files t))
14187 (setq scope (org-add-archive-files scope)))
14189 (setq scope (list (buffer-file-name))))
14190 ((eq scope 'file-with-archives)
14191 (setq scope (org-add-archive-files (list (buffer-file-name))))))
14192 (org-agenda-prepare-buffers scope)
14193 (while (setq file (pop scope))
14194 (with-current-buffer (org-find-base-buffer-visiting file)
14198 (goto-char (point-min))
14199 (setq res (append res (org-scan-tags func matcher todo-only))))))))))
14204 ;;; Setting and retrieving properties
14206 (defconst org-special-properties
14207 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
14208 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
14209 "The special properties valid in Org-mode.
14211 These are properties that are not defined in the property drawer,
14212 but in some other way.")
14214 (defconst org-default-properties
14215 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
14216 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
14217 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
14218 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
14219 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
14220 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
14221 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
14222 "Some properties that are used by Org-mode for various purposes.
14223 Being in this list makes sure that they are offered for completion.")
14225 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
14226 "Regular expression matching the first line of a property drawer.")
14228 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
14229 "Regular expression matching the last line of a property drawer.")
14231 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
14232 "Regular expression matching the first line of a property drawer.")
14234 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
14235 "Regular expression matching the first line of a property drawer.")
14237 (defconst org-property-drawer-re
14238 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
14239 org-property-end-re "\\)\n?")
14240 "Matches an entire property drawer.")
14242 (defconst org-clock-drawer-re
14243 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
14244 org-property-end-re "\\)\n?")
14245 "Matches an entire clock drawer.")
14247 (defsubst org-re-property (property)
14248 "Return a regexp matching a PROPERTY line.
14249 Match group 1 will be set to the value."
14250 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
14252 (defsubst org-re-property-keyword (property)
14253 "Return a regexp matching a PROPERTY line, possibly with no
14254 value for the property."
14255 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
14257 (defun org-property-action ()
14258 "Do an action on properties."
14261 (org-at-property-p)
14262 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
14263 (setq c (read-char-exclusive))
14266 (call-interactively 'org-set-property))
14268 (call-interactively 'org-delete-property))
14270 (call-interactively 'org-delete-property-globally))
14272 (call-interactively 'org-compute-property-at-point))
14273 (t (error "No such property action %c" c)))))
14275 (defun org-inc-effort ()
14276 "Increment the value of the effort property in the current entry."
14278 (org-set-effort nil t))
14280 (defun org-set-effort (&optional value increment)
14281 "Set the effort property of the current entry.
14282 With numerical prefix arg, use the nth allowed value, 0 stands for the
14283 10th allowed value.
14285 When INCREMENT is non-nil, set the property to the next allowed value."
14287 (if (equal value 0) (setq value 10))
14288 (let* ((completion-ignore-case t)
14289 (prop org-effort-property)
14290 (cur (org-entry-get nil prop))
14291 (allowed (org-property-get-allowed-values nil prop 'table))
14292 (existing (mapcar 'list (org-property-values prop)))
14295 ((stringp value) value)
14296 ((and allowed (integerp value))
14297 (or (car (nth (1- value) allowed))
14298 (car (org-last allowed))))
14299 ((and allowed increment)
14300 (or (caadr (member (list cur) allowed))
14301 (error "Allowed effort values are not set")))
14303 (message "Select 1-9,0, [RET%s]: %s"
14304 (if cur (concat "=" cur) "")
14305 (mapconcat 'car allowed " "))
14306 (setq rpl (read-char-exclusive))
14307 (if (equal rpl ?\r)
14309 (setq rpl (- rpl ?0))
14310 (if (equal rpl 0) (setq rpl 10))
14311 (if (and (> rpl 0) (<= rpl (length allowed)))
14312 (car (nth (1- rpl) allowed))
14313 (org-completing-read "Effort: " allowed nil))))
14315 (let (org-completion-use-ido org-completion-use-iswitchb)
14316 (org-completing-read
14317 (concat "Effort " (if (and cur (string-match "\\S-" cur))
14318 (concat "[" cur "]") "")
14320 existing nil nil "" nil cur))))))
14321 (unless (equal (org-entry-get nil prop) val)
14322 (org-entry-put nil prop val))
14324 (org-back-to-heading t)
14325 (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
14326 (message "%s is now %s" prop val)))
14328 (defun org-at-property-p ()
14329 "Is cursor inside a property drawer?"
14331 (beginning-of-line 1)
14332 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
14333 (save-match-data ;; Used by calling procedures
14335 (range (unless (org-before-first-heading-p)
14336 (org-get-property-block))))
14337 (and range (<= (car range) p) (< p (cdr range))))))))
14339 (defun org-get-property-block (&optional beg end force)
14340 "Return the (beg . end) range of the body of the property drawer.
14341 BEG and END are the beginning and end of the current subtree, or of
14342 the part before the first headline. If they are not given, they will
14343 be found. If the drawer does not exist and FORCE is non-nil, create
14347 (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
14348 (progn (org-back-to-heading t) (point))))
14349 (end (or end (and (not (outline-next-heading)) (point-max))
14352 (if (re-search-forward org-property-start-re end t)
14353 (setq beg (1+ (match-end 0)))
14356 (org-insert-property-drawer)
14357 (setq end (progn (outline-next-heading) (point))))
14360 (if (re-search-forward org-property-start-re end t)
14361 (setq beg (1+ (match-end 0)))))
14362 (if (re-search-forward org-property-end-re end t)
14363 (setq end (match-beginning 0))
14364 (or force (throw 'exit nil))
14368 (insert ":END:\n"))
14371 (defun org-entry-properties (&optional pom which specific)
14372 "Get all properties of the entry at point-or-marker POM.
14373 This includes the TODO keyword, the tags, time strings for deadline,
14374 scheduled, and clocking, and any additional properties defined in the
14375 entry. The return value is an alist, keys may occur multiple times
14376 if the property key was used several times.
14377 POM may also be nil, in which case the current entry is used.
14378 If WHICH is nil or `all', get all properties. If WHICH is
14379 `special' or `standard', only get that subclass. If WHICH
14380 is a string only get exactly this property. SPECIFIC can be a string, the
14381 specific property we are interested in. Specifying it can speed
14382 things up because then unnecessary parsing is avoided."
14383 (setq which (or which 'all))
14384 (org-with-point-at pom
14385 (let ((clockstr (substring org-clock-string 0 -1))
14386 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
14387 (case-fold-search nil)
14388 beg end range props sum-props key key1 value string clocksum clocksumt)
14390 (when (condition-case nil
14391 (and (derived-mode-p 'org-mode) (org-back-to-heading t))
14394 (setq sum-props (get-text-property (point) 'org-summaries))
14395 (setq clocksum (get-text-property (point) :org-clock-minutes)
14396 clocksumt (get-text-property (point) :org-clock-minutes-today))
14397 (outline-next-heading)
14399 (when (memq which '(all special))
14400 ;; Get the special properties, like TODO and tags
14402 (when (and (or (not specific) (string= specific "TODO"))
14403 (looking-at org-todo-line-regexp) (match-end 2))
14404 (push (cons "TODO" (org-match-string-no-properties 2)) props))
14405 (when (and (or (not specific) (string= specific "PRIORITY"))
14406 (looking-at org-priority-regexp))
14407 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
14408 (when (or (not specific) (string= specific "FILE"))
14409 (push (cons "FILE" buffer-file-name) props))
14410 (when (and (or (not specific) (string= specific "TAGS"))
14411 (setq value (org-get-tags-string))
14412 (string-match "\\S-" value))
14413 (push (cons "TAGS" value) props))
14414 (when (and (or (not specific) (string= specific "ALLTAGS"))
14415 (setq value (org-get-tags-at)))
14416 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
14419 (when (or (not specific) (string= specific "BLOCKED"))
14420 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
14421 (when (or (not specific)
14423 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
14424 "TIMESTAMP" "TIMESTAMP_IA")))
14426 (while (re-search-forward org-maybe-keyword-time-regexp end t)
14427 (setq key (if (match-end 1)
14428 (substring (org-match-string-no-properties 1)
14430 string (if (equal key clockstr)
14432 (buffer-substring-no-properties
14433 (match-beginning 3) (goto-char
14435 (substring (org-match-string-no-properties 3)
14437 ;; Get the correct property name from the key. This is
14438 ;; necessary if the user has configured time keywords.
14439 (setq key1 (concat key ":"))
14443 (if (= (char-after (match-beginning 3)) ?\[)
14444 "TIMESTAMP_IA" "TIMESTAMP")))
14445 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
14446 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
14447 ((equal key1 org-closed-string) (setq key "CLOSED"))
14448 ((equal key1 org-clock-string) (setq key "CLOCK")))
14449 (if (and specific (equal key specific) (not (equal key "CLOCK")))
14451 (push (cons key string) props)
14452 ;; no need to search further if match is found
14454 (when (or (equal key "CLOCK") (not (assoc key props)))
14455 (push (cons key string) props)))))))
14457 (when (memq which '(all standard))
14458 ;; Get the standard properties, like :PROP: ...
14459 (setq range (org-get-property-block beg end))
14461 (goto-char (car range))
14462 (while (re-search-forward
14463 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
14465 (setq key (org-match-string-no-properties 1)
14466 value (org-trim (or (org-match-string-no-properties 2) "")))
14467 (unless (member key excluded)
14468 (push (cons key (or value "")) props)))))
14470 (push (cons "CLOCKSUM"
14471 (org-columns-number-to-string (/ (float clocksum) 60.)
14475 (push (cons "CLOCKSUM_T"
14476 (org-columns-number-to-string (/ (float clocksumt) 60.)
14479 (unless (assoc "CATEGORY" props)
14480 (push (cons "CATEGORY" (org-get-category)) props))
14481 (append sum-props (nreverse props)))))))
14483 (defun org-entry-get (pom property &optional inherit literal-nil)
14484 "Get value of PROPERTY for entry or content at point-or-marker POM.
14485 If INHERIT is non-nil and the entry does not have the property,
14486 then also check higher levels of the hierarchy.
14487 If INHERIT is the symbol `selective', use inheritance only if the setting
14488 in `org-use-property-inheritance' selects PROPERTY for inheritance.
14489 If the property is present but empty, the return value is the empty string.
14490 If the property is not present at all, nil is returned.
14492 If LITERAL-NIL is set, return the string value \"nil\" as a string,
14493 do not interpret it as the list atom nil. This is used for inheritance
14494 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
14495 (org-with-point-at pom
14496 (if (and inherit (if (eq inherit 'selective)
14497 (org-property-inherit-p property)
14499 (org-entry-get-with-inheritance property literal-nil)
14500 (if (member property org-special-properties)
14501 ;; We need a special property. Use `org-entry-properties' to
14502 ;; retrieve it, but specify the wanted property
14503 (cdr (assoc property (org-entry-properties nil 'special property)))
14504 (let ((range (org-get-property-block)))
14505 (when (and range (not (eq (car range) (cdr range))))
14506 (let* ((props (list (or (assoc property org-file-properties)
14507 (assoc property org-global-properties)
14508 (assoc property org-global-properties-fixed))))
14510 (when (re-search-forward
14511 (org-re-property key) (cdr range) t)
14513 (org-update-property-plist
14516 (org-match-string-no-properties 1) "")
14519 (goto-char (car range))
14520 (funcall ap property)
14521 (goto-char (car range))
14522 (while (funcall ap (concat property "+")))
14523 (setq val (cdr (assoc property props)))
14524 (when val (if literal-nil val (org-not-nil val))))))))))
14526 (defun org-property-or-variable-value (var &optional inherit)
14527 "Check if there is a property fixing the value of VAR.
14528 If yes, return this value. If not, return the current value of the variable."
14529 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14530 (if (and prop (stringp prop) (string-match "\\S-" prop))
14532 (symbol-value var))))
14534 (defun org-entry-delete (pom property)
14535 "Delete the property PROPERTY from entry at point-or-marker POM."
14536 (org-with-point-at pom
14537 (if (member property org-special-properties)
14538 nil ; cannot delete these properties.
14539 (let ((range (org-get-property-block)))
14541 (goto-char (car range))
14543 (org-re-property property)
14546 (delete-region (match-beginning 0) (1+ (point-at-eol)))
14550 ;; Multi-values properties are properties that contain multiple values
14551 ;; These values are assumed to be single words, separated by whitespace.
14552 (defun org-entry-add-to-multivalued-property (pom property value)
14553 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
14554 (let* ((old (org-entry-get pom property))
14555 (values (and old (org-split-string old "[ \t]"))))
14556 (setq value (org-entry-protect-space value))
14557 (unless (member value values)
14558 (setq values (cons value values))
14559 (org-entry-put pom property
14560 (mapconcat 'identity values " ")))))
14562 (defun org-entry-remove-from-multivalued-property (pom property value)
14563 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
14564 (let* ((old (org-entry-get pom property))
14565 (values (and old (org-split-string old "[ \t]"))))
14566 (setq value (org-entry-protect-space value))
14567 (when (member value values)
14568 (setq values (delete value values))
14569 (org-entry-put pom property
14570 (mapconcat 'identity values " ")))))
14572 (defun org-entry-member-in-multivalued-property (pom property value)
14573 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
14574 (let* ((old (org-entry-get pom property))
14575 (values (and old (org-split-string old "[ \t]"))))
14576 (setq value (org-entry-protect-space value))
14577 (member value values)))
14579 (defun org-entry-get-multivalued-property (pom property)
14580 "Return a list of values in a multivalued property."
14581 (let* ((value (org-entry-get pom property))
14582 (values (and value (org-split-string value "[ \t]"))))
14583 (mapcar 'org-entry-restore-space values)))
14585 (defun org-entry-put-multivalued-property (pom property &rest values)
14586 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
14587 VALUES should be a list of strings. Spaces will be protected."
14588 (org-entry-put pom property
14589 (mapconcat 'org-entry-protect-space values " "))
14590 (let* ((value (org-entry-get pom property))
14591 (values (and value (org-split-string value "[ \t]"))))
14592 (mapcar 'org-entry-restore-space values)))
14594 (defun org-entry-protect-space (s)
14595 "Protect spaces and newline in string S."
14596 (while (string-match " " s)
14597 (setq s (replace-match "%20" t t s)))
14598 (while (string-match "\n" s)
14599 (setq s (replace-match "%0A" t t s)))
14602 (defun org-entry-restore-space (s)
14603 "Restore spaces and newline in string S."
14604 (while (string-match "%20" s)
14605 (setq s (replace-match " " t t s)))
14606 (while (string-match "%0A" s)
14607 (setq s (replace-match "\n" t t s)))
14610 (defvar org-entry-property-inherited-from (make-marker)
14611 "Marker pointing to the entry from where a property was inherited.
14612 Each call to `org-entry-get-with-inheritance' will set this marker to the
14613 location of the entry where the inheritance search matched. If there was
14614 no match, the marker will point nowhere.
14615 Note that also `org-entry-get' calls this function, if the INHERIT flag
14618 (defun org-entry-get-with-inheritance (property &optional literal-nil)
14619 "Get PROPERTY of entry or content at point, search higher levels if needed.
14620 The search will stop at the first ancestor which has the property defined.
14621 If the value found is \"nil\", return nil to show that the property
14622 should be considered as undefined (this is the meaning of nil here).
14623 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
14624 (move-marker org-entry-property-inherited-from nil)
14631 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
14632 (or (ignore-errors (org-back-to-heading t))
14633 (goto-char (point-min)))
14634 (move-marker org-entry-property-inherited-from (point))
14636 (or (ignore-errors (org-up-heading-safe))
14637 (throw 'ex nil))))))
14639 (cdr (assoc property org-file-properties))
14640 (cdr (assoc property org-global-properties))
14641 (cdr (assoc property org-global-properties-fixed))))
14642 (if literal-nil tmp (org-not-nil tmp))))
14644 (defvar org-property-changed-functions nil
14645 "Hook called when the value of a property has changed.
14646 Each hook function should accept two arguments, the name of the property
14647 and the new value.")
14649 (defun org-entry-put (pom property value)
14650 "Set PROPERTY to VALUE for entry at point-or-marker POM."
14651 (org-with-point-at pom
14652 (org-back-to-heading t)
14653 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
14656 ((equal property "TODO")
14657 (when (and (stringp value) (string-match "\\S-" value)
14658 (not (member value org-todo-keywords-1)))
14659 (error "\"%s\" is not a valid TODO state" value))
14660 (if (or (not value)
14661 (not (string-match "\\S-" value)))
14662 (setq value 'none))
14664 (org-set-tags nil 'align))
14665 ((equal property "PRIORITY")
14666 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
14667 (string-to-char value) ?\ ))
14668 (org-set-tags nil 'align))
14669 ((equal property "SCHEDULED")
14670 (if (re-search-forward org-scheduled-time-regexp end t)
14672 ((eq value 'earlier) (org-timestamp-change -1 'day))
14673 ((eq value 'later) (org-timestamp-change 1 'day))
14674 (t (call-interactively 'org-schedule)))
14675 (call-interactively 'org-schedule)))
14676 ((equal property "DEADLINE")
14677 (if (re-search-forward org-deadline-time-regexp end t)
14679 ((eq value 'earlier) (org-timestamp-change -1 'day))
14680 ((eq value 'later) (org-timestamp-change 1 'day))
14681 (t (call-interactively 'org-deadline)))
14682 (call-interactively 'org-deadline)))
14683 ((member property org-special-properties)
14684 (error "The %s property can not yet be set with `org-entry-put'"
14686 (t ; a non-special property
14687 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
14688 (setq range (org-get-property-block beg end 'force))
14689 (goto-char (car range))
14690 (if (re-search-forward
14691 (org-re-property-keyword property) (cdr range) t)
14693 (delete-region (match-beginning 0) (match-end 0))
14694 (goto-char (match-beginning 0)))
14695 (goto-char (cdr range))
14699 (insert ":" property ":")
14700 (and value (insert " " value))
14701 (org-indent-line)))))
14702 (run-hook-with-args 'org-property-changed-functions property value)))
14704 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
14705 "Get all property keys in the current buffer.
14706 With INCLUDE-SPECIALS, also list the special properties that reflect things
14707 like tags and TODO state.
14708 With INCLUDE-DEFAULTS, also include properties that has special meaning
14709 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
14711 With INCLUDE-COLUMNS, also include property names given in COLUMN
14712 formats in the current buffer."
14713 (let (rtn range cfmt s p)
14717 (goto-char (point-min))
14718 (while (re-search-forward org-property-start-re nil t)
14719 (setq range (org-get-property-block))
14720 (goto-char (car range))
14721 (while (re-search-forward
14722 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
14724 (add-to-list 'rtn (org-match-string-no-properties 1)))
14725 (outline-next-heading))))
14727 (when include-specials
14728 (setq rtn (append org-special-properties rtn)))
14730 (when include-defaults
14731 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
14732 (add-to-list 'rtn org-effort-property))
14734 (when include-columns
14738 (goto-char (point-min))
14739 (while (re-search-forward
14740 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
14742 (setq cfmt (match-string 2) s 0)
14743 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
14745 (setq s (match-end 0)
14746 p (match-string 1 cfmt))
14747 (unless (or (equal p "ITEM")
14748 (member p org-special-properties))
14749 (add-to-list 'rtn (match-string 1 cfmt))))))))
14751 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
14753 (defun org-property-values (key)
14754 "Return a list of all values of property KEY in the current buffer."
14758 (goto-char (point-min))
14759 (let ((re (org-re-property key))
14761 (while (re-search-forward re nil t)
14762 (add-to-list 'values (org-trim (match-string 1))))
14763 (delete "" values)))))
14765 (defun org-insert-property-drawer ()
14766 "Insert a property drawer into the current entry."
14767 (org-back-to-heading t)
14768 (looking-at org-outline-regexp)
14769 (let ((indent (if org-adapt-indentation
14770 (- (match-end 0) (match-beginning 0))
14773 (re (concat "^[ \t]*" org-keyword-time-regexp))
14775 (outline-next-heading)
14778 (while (re-search-forward re end t))
14779 (setq hiddenp (outline-invisible-p))
14781 (and (equal (char-after) ?\n) (forward-char 1))
14782 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
14783 (if (member (match-string 1) '("CLOCK:" ":END:"))
14784 ;; just skip this line
14785 (beginning-of-line 2)
14786 ;; Drawer start, find the end
14787 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
14788 (beginning-of-line 1)))
14789 (org-skip-over-state-notes)
14790 (skip-chars-backward " \t\n\r")
14791 (if (eq (char-before) ?*) (forward-char 1))
14792 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
14793 (beginning-of-line 0)
14794 (org-indent-to-column indent)
14795 (beginning-of-line 2)
14796 (org-indent-to-column indent)
14797 (beginning-of-line 0)
14800 (org-back-to-heading t)
14802 (org-flag-drawer t))))
14804 (defun org-insert-drawer (&optional arg drawer)
14805 "Insert a drawer at point.
14807 Optional argument DRAWER, when non-nil, is a string representing
14808 drawer's name. Otherwise, the user is prompted for a name.
14810 If a region is active, insert the drawer around that region
14813 Point is left between drawer's boundaries."
14815 (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
14817 ;; SYSTEM-DRAWERS is a list of drawer names that are used
14818 ;; internally by Org. They are meant to be inserted
14820 (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
14821 ;; Remove system drawers from list. Note: For some reason,
14822 ;; `org-completing-read' ignores the predicate while
14823 ;; `completing-read' handles it fine.
14824 (drawer (if arg "PROPERTIES"
14827 "Drawer: " org-drawers
14828 (lambda (d) (not (member d system-drawers))))))))
14830 ;; With C-u, fall back on `org-insert-property-drawer'
14831 (arg (org-insert-property-drawer))
14832 ;; With an active region, insert a drawer at point.
14833 ((not (org-region-active-p))
14835 (unless (bolp) (insert "\n"))
14836 (insert (format ":%s:\n\n:END:\n" drawer))
14837 (forward-line -2)))
14838 ;; Otherwise, insert the drawer at point
14840 (let ((rbeg (region-beginning))
14841 (rend (copy-marker (region-end))))
14845 (beginning-of-line)
14846 (when (save-excursion
14847 (re-search-forward org-outline-regexp-bol rend t))
14848 (error "Drawers cannot contain headlines"))
14849 ;; Position point at the beginning of the first
14850 ;; non-blank line in region. Insert drawer's opening
14851 ;; there, then indent it.
14852 (org-skip-whitespace)
14853 (beginning-of-line)
14854 (insert ":" drawer ":\n")
14856 (indent-for-tab-command)
14857 ;; Move point to the beginning of the first blank line
14858 ;; after the last non-blank line in region. Insert
14859 ;; drawer's closing, then indent it.
14861 (skip-chars-backward " \r\t\n")
14863 (deactivate-mark t)
14864 (indent-for-tab-command)
14865 (unless (eolp) (insert "\n")))
14866 ;; Clear marker, whatever the outcome of insertion is.
14867 (set-marker rend nil)))))))
14869 (defvar org-property-set-functions-alist nil
14870 "Property set function alist.
14871 Each entry should have the following format:
14873 (PROPERTY . READ-FUNCTION)
14875 The read function will be called with the same argument as
14876 `org-completing-read'.")
14878 (defun org-set-property-function (property)
14879 "Get the function that should be used to set PROPERTY.
14880 This is computed according to `org-property-set-functions-alist'."
14881 (or (cdr (assoc property org-property-set-functions-alist))
14882 'org-completing-read))
14884 (defun org-read-property-value (property)
14885 "Read PROPERTY value from user."
14886 (let* ((completion-ignore-case t)
14887 (allowed (org-property-get-allowed-values nil property 'table))
14888 (cur (org-entry-get nil property))
14889 (prompt (concat property " value"
14890 (if (and cur (string-match "\\S-" cur))
14891 (concat " [" cur "]") "") ": "))
14892 (set-function (org-set-property-function property))
14894 (funcall set-function prompt allowed nil
14895 (not (get-text-property 0 'org-unrestricted
14897 (let (org-completion-use-ido org-completion-use-iswitchb)
14898 (funcall set-function prompt
14899 (mapcar 'list (org-property-values property))
14900 nil nil "" nil cur)))))
14905 (defvar org-last-set-property nil)
14906 (defun org-read-property-name ()
14907 "Read a property name."
14908 (let* ((completion-ignore-case t)
14909 (keys (org-buffer-property-keys nil t t))
14910 (default-prop (or (save-excursion
14912 (beginning-of-line)
14913 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
14914 (null (string= (match-string 1) "END"))
14915 (match-string 1))))
14916 org-last-set-property))
14917 (property (org-icompleting-read
14919 (if default-prop (concat " [" default-prop "]") "")
14921 (mapcar 'list keys)
14925 (if (member property keys)
14927 (or (cdr (assoc (downcase property)
14928 (mapcar (lambda (x) (cons (downcase x) x))
14932 (defun org-set-property (property value)
14933 "In the current entry, set PROPERTY to VALUE.
14934 When called interactively, this will prompt for a property name, offering
14935 completion on existing and default properties. And then it will prompt
14936 for a value, offering completion either on allowed values (via an inherited
14937 xxx_ALL property) or on existing values in other instances of this property
14938 in the current file."
14939 (interactive (list nil nil))
14940 (let* ((property (or property (org-read-property-name)))
14941 (value (or value (org-read-property-value property)))
14942 (fn (cdr (assoc property org-properties-postprocess-alist))))
14943 (setq org-last-set-property property)
14944 ;; Possibly postprocess the inserted value:
14945 (when fn (setq value (funcall fn value)))
14946 (unless (equal (org-entry-get nil property) value)
14947 (org-entry-put nil property value))))
14949 (defun org-delete-property (property)
14950 "In the current entry, delete PROPERTY."
14952 (let* ((completion-ignore-case t)
14953 (prop (org-icompleting-read "Property: "
14954 (org-entry-properties nil 'standard))))
14956 (message "Property %s %s" property
14957 (if (org-entry-delete nil property)
14959 "was not present in the entry")))
14961 (defun org-delete-property-globally (property)
14962 "Remove PROPERTY globally, from all entries."
14964 (let* ((completion-ignore-case t)
14965 (prop (org-icompleting-read
14966 "Globally remove property: "
14967 (mapcar 'list (org-buffer-property-keys)))))
14972 (goto-char (point-min))
14974 (while (re-search-forward
14975 (org-re-property property)
14977 (setq cnt (1+ cnt))
14978 (delete-region (match-beginning 0) (1+ (point-at-eol))))
14979 (message "Property \"%s\" removed from %d entries" property cnt)))))
14981 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
14983 (defun org-compute-property-at-point ()
14984 "Compute the property at point.
14985 This looks for an enclosing column format, extracts the operator and
14986 then applies it to the property in the column format's scope."
14988 (unless (org-at-property-p)
14989 (error "Not at a property"))
14990 (let ((prop (org-match-string-no-properties 2)))
14991 (org-columns-get-format-and-top-level)
14992 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
14993 (error "No operator defined for property %s" prop))
14994 (org-columns-compute prop)))
14996 (defvar org-property-allowed-value-functions nil
14997 "Hook for functions supplying allowed values for a specific property.
14998 The functions must take a single argument, the name of the property, and
14999 return a flat list of allowed values. If \":ETC\" is one of
15000 the values, this means that these values are intended as defaults for
15001 completion, but that other values should be allowed too.
15002 The functions must return nil if they are not responsible for this
15005 (defun org-property-get-allowed-values (pom property &optional table)
15006 "Get allowed values for the property PROPERTY.
15007 When TABLE is non-nil, return an alist that can directly be used for
15011 ((equal property "TODO")
15012 (setq vals (org-with-point-at pom
15013 (append org-todo-keywords-1 '("")))))
15014 ((equal property "PRIORITY")
15015 (let ((n org-lowest-priority))
15016 (while (>= n org-highest-priority)
15017 (push (char-to-string n) vals)
15019 ((member property org-special-properties))
15020 ((setq vals (run-hook-with-args-until-success
15021 'org-property-allowed-value-functions property)))
15023 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
15024 (when (and vals (string-match "\\S-" vals))
15025 (setq vals (car (read-from-string (concat "(" vals ")"))))
15026 (setq vals (mapcar (lambda (x)
15027 (cond ((stringp x) x)
15028 ((numberp x) (number-to-string x))
15029 ((symbolp x) (symbol-name x))
15032 (when (member ":ETC" vals)
15033 (setq vals (remove ":ETC" vals))
15034 (org-add-props (car vals) '(org-unrestricted t)))
15035 (if table (mapcar 'list vals) vals)))
15037 (defun org-property-previous-allowed-value (&optional previous)
15038 "Switch to the next allowed value for this property."
15040 (org-property-next-allowed-value t))
15042 (defun org-property-next-allowed-value (&optional previous)
15043 "Switch to the next allowed value for this property."
15045 (unless (org-at-property-p)
15046 (error "Not at a property"))
15047 (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
15048 (key (match-string 2))
15049 (value (match-string 3))
15050 (allowed (or (org-property-get-allowed-values (point) key)
15051 (and (member value '("[ ]" "[-]" "[X]"))
15055 (error "Allowed values for this property have not been defined"))
15056 (if previous (setq allowed (reverse allowed)))
15057 (if (member value allowed)
15058 (setq nval (car (cdr (member value allowed)))))
15059 (setq nval (or nval (car allowed)))
15060 (if (equal nval value)
15061 (error "Only one allowed value for this property"))
15062 (org-at-property-p)
15063 (replace-match (concat " :" key ": " nval) t t)
15065 (beginning-of-line 1)
15066 (skip-chars-forward " \t")
15067 (when (equal prop org-effort-property)
15069 (org-back-to-heading t)
15070 (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
15071 (run-hook-with-args 'org-property-changed-functions key nval)))
15073 (defun org-find-olp (path &optional this-buffer)
15074 "Return a marker pointing to the entry at outline path OLP.
15075 If anything goes wrong, throw an error.
15076 You can wrap this call to catch the error like this:
15078 (condition-case msg
15079 (org-mobile-locate-entry (match-string 4))
15080 (error (nth 1 msg)))
15082 The return value will then be either a string with the error message,
15083 or a marker if everything is OK.
15085 If THIS-BUFFER is set, the outline path does not contain a file,
15087 (let* ((file (if this-buffer buffer-file-name (pop path)))
15088 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
15092 limit re end found pos heading cnt flevel)
15093 (unless buffer (error "File not found :%s" file))
15094 (with-current-buffer buffer
15098 (setq limit (point-max))
15099 (goto-char (point-min))
15100 (while (setq heading (pop path))
15101 (setq re (format org-complex-heading-regexp-format
15102 (regexp-quote heading)))
15103 (setq cnt 0 pos (point))
15104 (while (re-search-forward re end t)
15105 (setq level (- (match-end 1) (match-beginning 1)))
15106 (if (and (>= level lmin) (<= level lmax))
15107 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
15108 (when (= cnt 0) (error "Heading not found on level %d: %s"
15110 (when (> cnt 1) (error "Heading not unique on level %d: %s"
15113 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
15114 (setq end (save-excursion (org-end-of-subtree t t))))
15115 (when (org-at-heading-p)
15116 (point-marker)))))))
15118 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
15119 "Find node HEADING in BUFFER.
15120 Return a marker to the heading if it was found, or nil if not.
15121 If POS-ONLY is set, return just the position instead of a marker.
15123 The heading text must match exact, but it may have a TODO keyword,
15124 a priority cookie and tags in the standard locations."
15125 (with-current-buffer (or buffer (current-buffer))
15129 (goto-char (point-min))
15130 (let (case-fold-search)
15131 (if (re-search-forward
15132 (format org-complex-heading-regexp-format
15133 (regexp-quote heading)) nil t)
15135 (match-beginning 0)
15136 (move-marker (make-marker) (match-beginning 0)))))))))
15138 (defun org-find-exact-heading-in-directory (heading &optional dir)
15139 "Find Org node headline HEADING in all .org files in directory DIR.
15140 When the target headline is found, return a marker to this location."
15141 (let ((files (directory-files (or dir default-directory)
15142 nil "\\`[^.#].*\\.org\\'"))
15143 file visiting m buffer)
15145 (while (setq file (pop files))
15146 (message "trying %s" file)
15147 (setq visiting (org-find-base-buffer-visiting file))
15148 (setq buffer (or visiting (find-file-noselect file)))
15149 (setq m (org-find-exact-headline-in-buffer
15151 (when (and (not m) (not visiting)) (kill-buffer buffer))
15152 (and m (throw 'found m))))))
15154 (defun org-find-entry-with-id (ident)
15155 "Locate the entry that contains the ID property with exact value IDENT.
15156 IDENT can be a string, a symbol or a number, this function will search for
15157 the string representation of it.
15158 Return the position where this entry starts, or nil if there is no such entry."
15159 (interactive "sID: ")
15161 ((stringp ident) ident)
15162 ((symbol-name ident) (symbol-name ident))
15163 ((numberp ident) (number-to-string ident))
15164 (t (error "IDENT %s must be a string, symbol or number" ident))))
15165 (case-fold-search nil))
15169 (goto-char (point-min))
15170 (when (re-search-forward
15171 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
15173 (org-back-to-heading t)
15178 (defvar org-last-changed-timestamp nil)
15179 (defvar org-last-inserted-timestamp nil
15180 "The last time stamp inserted with `org-insert-time-stamp'.")
15181 (defvar org-time-was-given) ; dynamically scoped parameter
15182 (defvar org-end-time-was-given) ; dynamically scoped parameter
15183 (defvar org-ts-what) ; dynamically scoped parameter
15185 (defun org-time-stamp (arg &optional inactive)
15186 "Prompt for a date/time and insert a time stamp.
15187 If the user specifies a time like HH:MM or if this command is
15188 called with at least one prefix argument, the time stamp contains
15189 the date and the time. Otherwise, only the date is be included.
15191 All parts of a date not specified by the user is filled in from
15192 the current date/time. So if you just press return without
15193 typing anything, the time stamp will represent the current
15196 If there is already a timestamp at the cursor, it will be
15199 With two universal prefix arguments, insert an active timestamp
15200 with the current time without prompting the user."
15204 ;; Default time is either today, or, when entering a range,
15205 ;; the range start.
15206 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
15208 (re-search-backward
15209 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
15210 (- (point) 20) t)))
15211 (apply 'encode-time (org-parse-time-string (match-string 1)))
15213 (default-input (and ts (org-get-compact-tod ts)))
15214 (repeater (save-excursion
15216 (beginning-of-line)
15217 (when (re-search-forward
15218 "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
15219 (save-excursion (progn (end-of-line) (point))) t)
15220 (match-string 0)))))
15221 org-time-was-given org-end-time-was-given time)
15223 ((and (org-at-timestamp-p t)
15224 (memq last-command '(org-time-stamp org-time-stamp-inactive))
15225 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
15227 (setq time (let ((this-command this-command))
15228 (org-read-date arg 'totime nil nil
15229 default-time default-input inactive)))
15230 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
15231 ((org-at-timestamp-p t)
15232 (setq time (let ((this-command this-command))
15233 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15234 (when (org-at-timestamp-p t) ; just to get the match data
15235 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
15237 (setq org-last-changed-timestamp
15238 (org-insert-time-stamp
15239 time (or org-time-was-given arg)
15240 inactive nil nil (list org-end-time-was-given)))
15241 (when repeater (goto-char (1- (point))) (insert " " repeater)
15242 (setq org-last-changed-timestamp
15243 (concat (substring org-last-inserted-timestamp 0 -1)
15244 " " repeater ">"))))
15245 (message "Timestamp updated"))
15247 (org-insert-time-stamp (current-time) t))
15249 (setq time (let ((this-command this-command))
15250 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15251 (org-insert-time-stamp time (or org-time-was-given arg) inactive
15252 nil nil (list org-end-time-was-given))))))
15254 ;; FIXME: can we use this for something else, like computing time differences?
15255 (defun org-get-compact-tod (s)
15256 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
15257 (let* ((t1 (match-string 1 s))
15258 (h1 (string-to-number (match-string 2 s)))
15259 (m1 (string-to-number (match-string 3 s)))
15260 (t2 (and (match-end 4) (match-string 5 s)))
15261 (h2 (and t2 (string-to-number (match-string 6 s))))
15262 (m2 (and t2 (string-to-number (match-string 7 s))))
15266 (setq dh (- h2 h1) dm (- m2 m1))
15267 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
15268 (concat t1 "+" (number-to-string dh)
15269 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
15271 (defun org-time-stamp-inactive (&optional arg)
15272 "Insert an inactive time stamp.
15273 An inactive time stamp is enclosed in square brackets instead of angle
15274 brackets. It is inactive in the sense that it does not trigger agenda entries,
15275 does not link to the calendar and cannot be changed with the S-cursor keys.
15276 So these are more for recording a certain time/date."
15278 (org-time-stamp arg 'inactive))
15280 (defvar org-date-ovl (make-overlay 1 1))
15281 (overlay-put org-date-ovl 'face 'org-date-selected)
15282 (org-detach-overlay org-date-ovl)
15284 (defvar org-ans1) ; dynamically scoped parameter
15285 (defvar org-ans2) ; dynamically scoped parameter
15287 (defvar org-plain-time-of-day-regexp) ; defined below
15289 (defvar org-overriding-default-time nil) ; dynamically scoped
15290 (defvar org-read-date-overlay nil)
15291 (defvar org-dcst nil) ; dynamically scoped
15292 (defvar org-read-date-history nil)
15293 (defvar org-read-date-final-answer nil)
15294 (defvar org-read-date-analyze-futurep nil)
15295 (defvar org-read-date-analyze-forced-year nil)
15296 (defvar org-read-date-inactive)
15298 (defun org-read-date (&optional org-with-time to-time from-string prompt
15299 default-time default-input inactive)
15300 "Read a date, possibly a time, and make things smooth for the user.
15301 The prompt will suggest to enter an ISO date, but you can also enter anything
15302 which will at least partially be understood by `parse-time-string'.
15303 Unrecognized parts of the date will default to the current day, month, year,
15304 hour and minute. If this command is called to replace a timestamp at point,
15305 or to enter the second timestamp of a range, the default time is taken
15306 from the existing stamp. Furthermore, the command prefers the future,
15307 so if you are giving a date where the year is not given, and the day-month
15308 combination is already past in the current year, it will assume you
15309 mean next year. For details, see the manual. A few examples:
15311 3-2-5 --> 2003-02-05
15312 feb 15 --> currentyear-02-15
15313 2/15 --> currentyear-02-15
15314 sep 12 9 --> 2009-09-12
15315 12:45 --> today 12:45
15316 22 sept 0:34 --> currentyear-09-22 0:34
15317 12 --> currentyear-currentmonth-12
15318 Fri --> nearest Friday (today or later)
15321 Furthermore you can specify a relative date by giving, as the *first* thing
15322 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
15323 change in days weeks, months, years.
15324 With a single plus or minus, the date is relative to today. With a double
15325 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
15326 +4d --> four days from today
15327 +4 --> same as above
15328 +2w --> two weeks from today
15329 ++5 --> five days from default date
15331 The function understands only English month and weekday abbreviations.
15333 While prompting, a calendar is popped up - you can also select the
15334 date with the mouse (button 1). The calendar shows a period of three
15335 months. To scroll it to other months, use the keys `>' and `<'.
15336 If you don't like the calendar, turn it off with
15337 \(setq org-read-date-popup-calendar nil)
15339 With optional argument TO-TIME, the date will immediately be converted
15340 to an internal time.
15341 With an optional argument ORG-WITH-TIME, the prompt will suggest to
15342 also insert a time. Note that when ORG-WITH-TIME is not set, you can
15343 still enter a time, and this function will inform the calling routine
15344 about this change. The calling routine may then choose to change the
15345 format used to insert the time stamp into the buffer to include the time.
15346 With optional argument FROM-STRING, read from this string instead from
15347 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
15348 the time/date that is used for everything that is not specified by the
15350 (require 'parse-time)
15351 (let* ((org-time-stamp-rounding-minutes
15352 (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
15353 (org-dcst org-display-custom-times)
15354 (ct (org-current-time))
15355 (org-def (or org-overriding-default-time default-time ct))
15356 (org-defdecode (decode-time org-def))
15358 (when (< (nth 2 org-defdecode) org-extend-today-until)
15359 (setcar (nthcdr 2 org-defdecode) -1)
15360 (setcar (nthcdr 1 org-defdecode) 59)
15361 (setq org-def (apply 'encode-time org-defdecode)
15362 org-defdecode (decode-time org-def)))))
15363 (mouse-autoselect-window nil) ; Don't let the mouse jump
15364 (calendar-frame-setup nil)
15365 (calendar-setup nil)
15366 (calendar-move-hook nil)
15367 (calendar-view-diary-initially-flag nil)
15368 (calendar-view-holidays-initially-flag nil)
15369 (timestr (format-time-string
15370 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
15371 (prompt (concat (if prompt (concat prompt " ") "")
15372 (format "Date+time [%s]: " timestr)))
15373 ans (org-ans0 "") org-ans1 org-ans2 final)
15376 (from-string (setq ans from-string))
15377 (org-read-date-popup-calendar
15379 (save-window-excursion
15381 (org-eval-in-calendar '(setq cursor-type nil) t)
15384 (calendar-forward-day (- (time-to-days org-def)
15385 (calendar-absolute-from-gregorian
15386 (calendar-current-date))))
15387 (org-eval-in-calendar nil t)
15388 (let* ((old-map (current-local-map))
15389 (map (copy-keymap calendar-mode-map))
15390 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
15391 (org-defkey map (kbd "RET") 'org-calendar-select)
15392 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
15393 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
15394 (org-defkey minibuffer-local-map [(meta shift left)]
15395 (lambda () (interactive)
15396 (org-eval-in-calendar '(calendar-backward-month 1))))
15397 (org-defkey minibuffer-local-map [(meta shift right)]
15398 (lambda () (interactive)
15399 (org-eval-in-calendar '(calendar-forward-month 1))))
15400 (org-defkey minibuffer-local-map [(meta shift up)]
15401 (lambda () (interactive)
15402 (org-eval-in-calendar '(calendar-backward-year 1))))
15403 (org-defkey minibuffer-local-map [(meta shift down)]
15404 (lambda () (interactive)
15405 (org-eval-in-calendar '(calendar-forward-year 1))))
15406 (org-defkey minibuffer-local-map [?\e (shift left)]
15407 (lambda () (interactive)
15408 (org-eval-in-calendar '(calendar-backward-month 1))))
15409 (org-defkey minibuffer-local-map [?\e (shift right)]
15410 (lambda () (interactive)
15411 (org-eval-in-calendar '(calendar-forward-month 1))))
15412 (org-defkey minibuffer-local-map [?\e (shift up)]
15413 (lambda () (interactive)
15414 (org-eval-in-calendar '(calendar-backward-year 1))))
15415 (org-defkey minibuffer-local-map [?\e (shift down)]
15416 (lambda () (interactive)
15417 (org-eval-in-calendar '(calendar-forward-year 1))))
15418 (org-defkey minibuffer-local-map [(shift up)]
15419 (lambda () (interactive)
15420 (org-eval-in-calendar '(calendar-backward-week 1))))
15421 (org-defkey minibuffer-local-map [(shift down)]
15422 (lambda () (interactive)
15423 (org-eval-in-calendar '(calendar-forward-week 1))))
15424 (org-defkey minibuffer-local-map [(shift left)]
15425 (lambda () (interactive)
15426 (org-eval-in-calendar '(calendar-backward-day 1))))
15427 (org-defkey minibuffer-local-map [(shift right)]
15428 (lambda () (interactive)
15429 (org-eval-in-calendar '(calendar-forward-day 1))))
15430 (org-defkey minibuffer-local-map ">"
15431 (lambda () (interactive)
15432 (org-eval-in-calendar '(scroll-calendar-left 1))))
15433 (org-defkey minibuffer-local-map "<"
15434 (lambda () (interactive)
15435 (org-eval-in-calendar '(scroll-calendar-right 1))))
15436 (org-defkey minibuffer-local-map "\C-v"
15437 (lambda () (interactive)
15438 (org-eval-in-calendar
15439 '(calendar-scroll-left-three-months 1))))
15440 (org-defkey minibuffer-local-map "\M-v"
15441 (lambda () (interactive)
15442 (org-eval-in-calendar
15443 '(calendar-scroll-right-three-months 1))))
15444 (run-hooks 'org-read-date-minibuffer-setup-hook)
15447 (use-local-map map)
15448 (setq org-read-date-inactive inactive)
15449 (add-hook 'post-command-hook 'org-read-date-display)
15450 (setq org-ans0 (read-string prompt default-input
15451 'org-read-date-history nil))
15452 ;; org-ans0: from prompt
15453 ;; org-ans1: from mouse click
15454 ;; org-ans2: from calendar motion
15455 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
15456 (remove-hook 'post-command-hook 'org-read-date-display)
15457 (use-local-map old-map)
15458 (when org-read-date-overlay
15459 (delete-overlay org-read-date-overlay)
15460 (setq org-read-date-overlay nil)))))
15461 (bury-buffer "*Calendar*")))))
15463 (t ; Naked prompt only
15465 (setq ans (read-string prompt default-input
15466 'org-read-date-history timestr))
15467 (when org-read-date-overlay
15468 (delete-overlay org-read-date-overlay)
15469 (setq org-read-date-overlay nil)))))
15471 (setq final (org-read-date-analyze ans org-def org-defdecode))
15473 (when org-read-date-analyze-forced-year
15474 (message "Year was forced into %s"
15475 (if org-read-date-force-compatible-dates
15476 "compatible range (1970-2037)"
15477 "range representable on this machine"))
15480 ;; One round trip to get rid of 34th of August and stuff like that....
15481 (setq final (decode-time (apply 'encode-time final)))
15483 (setq org-read-date-final-answer ans)
15486 (apply 'encode-time final)
15487 (if (and (boundp 'org-time-was-given) org-time-was-given)
15488 (format "%04d-%02d-%02d %02d:%02d"
15489 (nth 5 final) (nth 4 final) (nth 3 final)
15490 (nth 2 final) (nth 1 final))
15491 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
15494 (defvar org-defdecode)
15495 (defvar org-with-time)
15496 (defun org-read-date-display ()
15497 "Display the current date prompt interpretation in the minibuffer."
15498 (when org-read-date-display-live
15499 (when org-read-date-overlay
15500 (delete-overlay org-read-date-overlay))
15501 (when (minibufferp (current-buffer))
15504 (while (not (equal (buffer-substring
15505 (max (point-min) (- (point) 4)) (point))
15508 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
15509 " " (or org-ans1 org-ans2)))
15510 (org-end-time-was-given nil)
15511 (f (org-read-date-analyze ans org-def org-defdecode))
15513 org-time-stamp-custom-formats
15514 org-time-stamp-formats))
15515 (fmt (if (or org-with-time
15516 (and (boundp 'org-time-was-given) org-time-was-given))
15519 (txt (format-time-string fmt (apply 'encode-time f)))
15520 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
15521 (txt (concat "=> " txt)))
15522 (when (and org-end-time-was-given
15523 (string-match org-plain-time-of-day-regexp txt))
15524 (setq txt (concat (substring txt 0 (match-end 0)) "-"
15525 org-end-time-was-given
15526 (substring txt (match-end 0)))))
15527 (when org-read-date-analyze-futurep
15528 (setq txt (concat txt " (=>F)")))
15529 (setq org-read-date-overlay
15530 (make-overlay (1- (point-at-eol)) (point-at-eol)))
15531 (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
15533 (defun org-read-date-analyze (ans org-def org-defdecode)
15534 "Analyze the combined answer of the date prompt."
15535 ;; FIXME: cleanup and comment
15536 (let ((nowdecode (decode-time (current-time)))
15537 delta deltan deltaw deltadef year month day
15538 hour minute second wday pm h2 m2 tl wday1
15539 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
15540 (setq org-read-date-analyze-futurep nil
15541 org-read-date-analyze-forced-year nil)
15542 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
15545 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
15546 (setq ans (replace-match "" t t ans)
15548 deltaw (nth 1 delta)
15549 deltadef (nth 2 delta)))
15551 ;; Check if there is an iso week date in there. If yes, store the
15552 ;; info and postpone interpreting it until the rest of the parsing
15554 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
15555 (setq iso-year (if (match-end 1)
15556 (org-small-year-to-year
15557 (string-to-number (match-string 1 ans))))
15558 iso-weekday (if (match-end 3)
15559 (string-to-number (match-string 3 ans)))
15560 iso-week (string-to-number (match-string 2 ans)))
15561 (setq ans (replace-match "" t t ans)))
15563 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
15564 (when (string-match
15565 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
15566 (setq year (if (match-end 2)
15567 (string-to-number (match-string 2 ans))
15568 (progn (setq kill-year t)
15569 (string-to-number (format-time-string "%Y"))))
15570 month (string-to-number (match-string 3 ans))
15571 day (string-to-number (match-string 4 ans)))
15572 (if (< year 100) (setq year (+ 2000 year)))
15573 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15576 ;; Help matching dotted european dates
15577 (when (string-match
15578 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
15579 (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
15581 (string-to-number (format-time-string "%Y")))
15582 day (string-to-number (match-string 1 ans))
15583 month (string-to-number (match-string 2 ans))
15584 ans (replace-match (format "%04d-%02d-%02d" year month day)
15587 ;; Help matching american dates, like 5/30 or 5/30/7
15588 (when (string-match
15589 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
15590 (setq year (if (match-end 4)
15591 (string-to-number (match-string 4 ans))
15592 (progn (setq kill-year t)
15593 (string-to-number (format-time-string "%Y"))))
15594 month (string-to-number (match-string 1 ans))
15595 day (string-to-number (match-string 2 ans)))
15596 (if (< year 100) (setq year (+ 2000 year)))
15597 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15599 ;; Help matching am/pm times, because `parse-time-string' does not do that.
15600 ;; If there is a time with am/pm, and *no* time without it, we convert
15601 ;; so that matching will be successful.
15602 (loop for i from 1 to 2 do ; twice, for end time as well
15603 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
15604 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
15605 (setq hour (string-to-number (match-string 1 ans))
15606 minute (if (match-end 3)
15607 (string-to-number (match-string 3 ans))
15610 (string-to-char (downcase (match-string 4 ans)))))
15611 (if (and (= hour 12) (not pm))
15613 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
15614 (setq ans (replace-match (format "%02d:%02d" hour minute)
15617 ;; Check if a time range is given as a duration
15618 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
15619 (setq hour (string-to-number (match-string 1 ans))
15620 h2 (+ hour (string-to-number (match-string 3 ans)))
15621 minute (string-to-number (match-string 2 ans))
15622 m2 (+ minute (if (match-end 5) (string-to-number
15623 (match-string 5 ans))0)))
15624 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
15625 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
15628 ;; Check if there is a time range
15629 (when (boundp 'org-end-time-was-given)
15630 (setq org-time-was-given nil)
15631 (when (and (string-match org-plain-time-of-day-regexp ans)
15633 (setq org-end-time-was-given (match-string 8 ans))
15634 (setq ans (concat (substring ans 0 (match-beginning 7))
15635 (substring ans (match-end 7))))))
15637 (setq tl (parse-time-string ans)
15638 day (or (nth 3 tl) (nth 3 org-defdecode))
15639 month (or (nth 4 tl)
15640 (if (and org-read-date-prefer-future
15641 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
15642 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
15643 (nth 4 org-defdecode)))
15644 year (or (and (not kill-year) (nth 5 tl))
15645 (if (and org-read-date-prefer-future
15646 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
15647 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
15648 (nth 5 org-defdecode)))
15649 hour (or (nth 2 tl) (nth 2 org-defdecode))
15650 minute (or (nth 1 tl) (nth 1 org-defdecode))
15651 second (or (nth 0 tl) 0)
15654 (when (and (eq org-read-date-prefer-future 'time)
15655 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
15656 (equal day (nth 3 nowdecode))
15657 (equal month (nth 4 nowdecode))
15658 (equal year (nth 5 nowdecode))
15660 (or (< (nth 2 tl) (nth 2 nowdecode))
15661 (and (= (nth 2 tl) (nth 2 nowdecode))
15663 (< (nth 1 tl) (nth 1 nowdecode)))))
15667 ;; Special date definitions below
15670 ;; There was an iso week
15673 (setq year (or iso-year year)
15674 day (or iso-weekday wday 1)
15675 wday nil ; to make sure that the trigger below does not match
15676 iso-date (calendar-gregorian-from-absolute
15677 (calendar-absolute-from-iso
15678 (list iso-week day year))))
15679 ; FIXME: Should we also push ISO weeks into the future?
15680 ; (when (and org-read-date-prefer-future
15682 ; (< (calendar-absolute-from-gregorian iso-date)
15683 ; (time-to-days (current-time))))
15684 ; (setq year (1+ year)
15685 ; iso-date (calendar-gregorian-from-absolute
15686 ; (calendar-absolute-from-iso
15687 ; (list iso-week day year)))))
15688 (setq month (car iso-date)
15689 year (nth 2 iso-date)
15690 day (nth 1 iso-date)))
15694 (let ((now (decode-time (current-time))))
15695 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
15696 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
15697 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
15698 ((equal deltaw "m") (setq month (+ month deltan)))
15699 ((equal deltaw "y") (setq year (+ year deltan)))))
15700 ((and wday (not (nth 3 tl)))
15701 ;; Weekday was given, but no day, so pick that day in the week
15702 ;; on or after the derived date.
15703 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
15704 (unless (equal wday wday1)
15705 (setq day (+ day (% (- wday wday1 -7) 7))))))
15706 (if (and (boundp 'org-time-was-given)
15708 (setq org-time-was-given t))
15709 (if (< year 100) (setq year (+ 2000 year)))
15710 ;; Check of the date is representable
15711 (if org-read-date-force-compatible-dates
15714 (setq year 1970 org-read-date-analyze-forced-year t))
15716 (setq year 2037 org-read-date-analyze-forced-year t)))
15717 (condition-case nil
15718 (ignore (encode-time second minute hour day month year))
15720 (setq year (nth 5 org-defdecode))
15721 (setq org-read-date-analyze-forced-year t))))
15722 (setq org-read-date-analyze-futurep futurep)
15723 (list second minute hour day month year)))
15725 (defvar parse-time-weekdays)
15726 (defun org-read-date-get-relative (s today default)
15727 "Check string S for special relative date string.
15728 TODAY and DEFAULT are internal times, for today and for a default.
15729 Return shift list (N what def-flag)
15730 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
15731 N is the number of WHATs to shift.
15732 DEF-FLAG is t when a double ++ or -- indicates shift relative to
15733 the DEFAULT date rather than TODAY."
15734 (require 'parse-time)
15738 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
15740 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
15741 "\\([ \t]\\|$\\)") s)
15742 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
15743 (let* ((dir (if (> (match-end 1) (match-beginning 1))
15744 (string-to-char (substring (match-string 1 s) -1))
15746 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
15747 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
15748 (what (if (match-end 3) (match-string 3 s) "d"))
15749 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
15750 (date (if rel default today))
15751 (wday (nth 6 (decode-time date)))
15755 (setq delta (mod (+ 7 (- wday1 wday)) 7))
15756 (if (= dir ?-) (setq delta (- delta 7)))
15757 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
15758 (list delta "d" rel))
15759 (list (* n (if (= dir ?-) -1 1)) what rel)))))
15761 (defun org-order-calendar-date-args (arg1 arg2 arg3)
15762 "Turn a user-specified date into the internal representation.
15763 The internal representation needed by the calendar is (month day year).
15764 This is a wrapper to handle the brain-dead convention in calendar that
15765 user function argument order change dependent on argument order."
15766 (if (boundp 'calendar-date-style)
15768 ((eq calendar-date-style 'american)
15769 (list arg1 arg2 arg3))
15770 ((eq calendar-date-style 'european)
15771 (list arg2 arg1 arg3))
15772 ((eq calendar-date-style 'iso)
15773 (list arg2 arg3 arg1)))
15774 (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
15775 (if (org-bound-and-true-p european-calendar-style)
15776 (list arg2 arg1 arg3)
15777 (list arg1 arg2 arg3)))))
15779 (defun org-eval-in-calendar (form &optional keepdate)
15780 "Eval FORM in the calendar window and return to current window.
15781 When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
15782 otherwise stick to the current value of `org-ans2'."
15783 (let ((sf (selected-frame))
15784 (sw (selected-window)))
15785 (select-window (get-buffer-window "*Calendar*" t))
15787 (when (and (not keepdate) (calendar-cursor-to-date))
15788 (let* ((date (calendar-cursor-to-date))
15789 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15790 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
15791 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
15793 (org-select-frame-set-input-focus sf)))
15795 (defun org-calendar-select ()
15796 "Return to `org-read-date' with the date currently selected.
15797 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15799 (when (calendar-cursor-to-date)
15800 (let* ((date (calendar-cursor-to-date))
15801 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15802 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15803 (if (active-minibuffer-window) (exit-minibuffer))))
15805 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
15806 "Insert a date stamp for the date given by the internal TIME.
15807 WITH-HM means use the stamp format that includes the time of the day.
15808 INACTIVE means use square brackets instead of angular ones, so that the
15809 stamp will not contribute to the agenda.
15810 PRE and POST are optional strings to be inserted before and after the
15812 The command returns the inserted time stamp."
15813 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
15815 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
15816 (insert-before-markers (or pre ""))
15817 (when (listp extra)
15818 (setq extra (car extra))
15819 (if (and (stringp extra)
15820 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
15821 (setq extra (format "-%02d:%02d"
15822 (string-to-number (match-string 1 extra))
15823 (string-to-number (match-string 2 extra))))
15826 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
15827 (insert-before-markers (setq stamp (format-time-string fmt time)))
15828 (insert-before-markers (or post ""))
15829 (setq org-last-inserted-timestamp stamp)))
15831 (defun org-toggle-time-stamp-overlays ()
15832 "Toggle the use of custom time stamp formats."
15834 (setq org-display-custom-times (not org-display-custom-times))
15835 (unless org-display-custom-times
15836 (let ((p (point-min)) (bmp (buffer-modified-p)))
15837 (while (setq p (next-single-property-change p 'display))
15838 (if (and (get-text-property p 'display)
15839 (eq (get-text-property p 'face) 'org-date))
15840 (remove-text-properties
15841 p (setq p (next-single-property-change p 'display))
15843 (set-buffer-modified-p bmp)))
15844 (if (featurep 'xemacs)
15845 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
15846 (org-restart-font-lock)
15847 (setq org-table-may-need-update t)
15848 (if org-display-custom-times
15849 (message "Time stamps are overlaid with custom format")
15850 (message "Time stamp overlays removed")))
15852 (defun org-display-custom-time (beg end)
15853 "Overlay modified time stamp format over timestamp between BEG and END."
15854 (let* ((ts (buffer-substring beg end))
15855 t1 w1 with-hm tf time str w2 (off 0))
15857 (setq t1 (org-parse-time-string ts t))
15858 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
15859 (setq off (- (match-end 0) (match-beginning 0)))))
15860 (setq end (- end off))
15861 (setq w1 (- end beg)
15862 with-hm (and (nth 1 t1) (nth 2 t1))
15863 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
15864 time (org-fix-decoded-time t1)
15866 (format-time-string
15867 (substring tf 1 -1) (apply 'encode-time time))
15868 nil 'mouse-face 'highlight)
15870 (if (not (= w2 w1))
15871 (add-text-properties (1+ beg) (+ 2 beg)
15872 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
15873 (if (featurep 'xemacs)
15875 (put-text-property beg end 'invisible t)
15876 (put-text-property beg end 'end-glyph (make-glyph str)))
15877 (put-text-property beg end 'display str))))
15879 (defun org-translate-time (string)
15880 "Translate all timestamps in STRING to custom format.
15881 But do this only if the variable `org-display-custom-times' is set."
15882 (when org-display-custom-times
15885 (re org-ts-regexp-both)
15886 t1 with-hm inactive tf time str beg end)
15887 (while (setq start (string-match re string start))
15888 (setq beg (match-beginning 0)
15890 t1 (save-match-data
15891 (org-parse-time-string (substring string beg end) t))
15892 with-hm (and (nth 1 t1) (nth 2 t1))
15893 inactive (equal (substring string beg (1+ beg)) "[")
15894 tf (funcall (if with-hm 'cdr 'car)
15895 org-time-stamp-custom-formats)
15896 time (org-fix-decoded-time t1)
15897 str (format-time-string
15899 (if inactive "[" "<") (substring tf 1 -1)
15900 (if inactive "]" ">"))
15901 (apply 'encode-time time))
15902 string (replace-match str t t string)
15903 start (+ start (length str)))))))
15906 (defun org-fix-decoded-time (time)
15907 "Set 0 instead of nil for the first 6 elements of time.
15908 Don't touch the rest."
15910 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
15912 (defun org-days-to-time (timestamp-string)
15913 "Difference between TIMESTAMP-STRING and now in days."
15914 (- (time-to-days (org-time-string-to-time timestamp-string))
15915 (time-to-days (current-time))))
15917 (defun org-deadline-close (timestamp-string &optional ndays)
15918 "Is the time in TIMESTAMP-STRING close to the current date?"
15919 (setq ndays (or ndays (org-get-wdays timestamp-string)))
15920 (and (< (org-days-to-time timestamp-string) ndays)
15921 (not (org-entry-is-done-p))))
15923 (defun org-get-wdays (ts)
15924 "Get the deadline lead time appropriate for timestring TS."
15926 ((<= org-deadline-warning-days 0)
15927 ;; 0 or negative, enforce this value no matter what
15928 (- org-deadline-warning-days))
15929 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
15930 ;; lead time is specified.
15931 (floor (* (string-to-number (match-string 1 ts))
15932 (cdr (assoc (match-string 2 ts)
15933 '(("d" . 1) ("w" . 7)
15934 ("m" . 30.4) ("y" . 365.25)
15935 ("h" . 0.041667)))))))
15936 ;; go for the default.
15937 (t org-deadline-warning-days)))
15939 (defun org-calendar-select-mouse (ev)
15940 "Return to `org-read-date' with the date currently selected.
15941 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15943 (mouse-set-point ev)
15944 (when (calendar-cursor-to-date)
15945 (let* ((date (calendar-cursor-to-date))
15946 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15947 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15948 (if (active-minibuffer-window) (exit-minibuffer))))
15950 (defun org-check-deadlines (ndays)
15951 "Check if there are any deadlines due or past due.
15952 A deadline is considered due if it happens within `org-deadline-warning-days'
15953 days from today's date. If the deadline appears in an entry marked DONE,
15954 it is not shown. The prefix arg NDAYS can be used to test that many
15955 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
15957 (let* ((org-warn-days
15959 ((equal ndays '(4)) 100000)
15960 (ndays (prefix-numeric-value ndays))
15961 (t (abs org-deadline-warning-days))))
15962 (case-fold-search nil)
15963 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15965 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
15967 (message "%d deadlines past-due or due within %d days"
15968 (org-occur regexp nil callback)
15971 (defsubst org-re-timestamp (type)
15972 "Return a regexp for timestamp TYPE.
15973 Allowed values for TYPE are:
15975 all: all timestamps
15976 active: only active timestamps (<...>)
15977 inactive: only inactive timestamps ([...])
15978 scheduled: only scheduled timestamps
15979 deadline: only deadline timestamps
15981 When TYPE is nil, fall back on returning a regexp that matches
15982 both scheduled and deadline timestamps."
15983 (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9>
\n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
15984 ((eq type 'active) org-ts-regexp)
15985 ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^
\n>]*?\\)\\]")
15986 ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
15987 ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15988 ((eq type 'scheduled-or-deadline)
15989 (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
15991 (defun org-check-before-date (date)
15992 "Check if there are deadlines or scheduled entries before DATE."
15993 (interactive (list (org-read-date)))
15994 (let ((case-fold-search nil)
15995 (regexp (org-re-timestamp org-ts-type))
15997 (lambda () (time-less-p
15998 (org-time-string-to-time (match-string 1))
15999 (org-time-string-to-time date)))))
16000 (message "%d entries before %s"
16001 (org-occur regexp nil callback) date)))
16003 (defun org-check-after-date (date)
16004 "Check if there are deadlines or scheduled entries after DATE."
16005 (interactive (list (org-read-date)))
16006 (let ((case-fold-search nil)
16007 (regexp (org-re-timestamp org-ts-type))
16011 (org-time-string-to-time (match-string 1))
16012 (org-time-string-to-time date))))))
16013 (message "%d entries after %s"
16014 (org-occur regexp nil callback) date)))
16016 (defun org-check-dates-range (start-date end-date)
16017 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
16018 (interactive (list (org-read-date nil nil nil "Range starts")
16019 (org-read-date nil nil nil "Range end")))
16020 (let ((case-fold-search nil)
16021 (regexp (org-re-timestamp org-ts-type))
16024 (let ((match (match-string 1)))
16027 (org-time-string-to-time match)
16028 (org-time-string-to-time start-date)))
16030 (org-time-string-to-time match)
16031 (org-time-string-to-time end-date)))))))
16032 (message "%d entries between %s and %s"
16033 (org-occur regexp nil callback) start-date end-date)))
16035 (defun org-evaluate-time-range (&optional to-buffer)
16036 "Evaluate a time range by computing the difference between start and end.
16037 Normally the result is just printed in the echo area, but with prefix arg
16038 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
16039 If the time range is actually in a table, the result is inserted into the
16041 For time difference computation, a year is assumed to be exactly 365
16042 days in order to avoid rounding problems."
16045 (org-clock-update-time-maybe)
16047 (unless (org-at-date-range-p t)
16048 (goto-char (point-at-bol))
16049 (re-search-forward org-tr-regexp-both (point-at-eol) t))
16050 (if (not (org-at-date-range-p t))
16051 (error "Not at a time-stamp range, and none found in current line")))
16052 (let* ((ts1 (match-string 1))
16053 (ts2 (match-string 2))
16054 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
16055 (match-end (match-end 0))
16056 (time1 (org-time-string-to-time ts1))
16057 (time2 (org-time-string-to-time ts2))
16058 (t1 (org-float-time time1))
16059 (t2 (org-float-time time2))
16060 (diff (abs (- t2 t1)))
16061 (negative (< (- t2 t1) 0))
16062 ;; (ys (floor (* 365 24 60 60)))
16065 (fy "%dy %dd %02d:%02d")
16067 (fd "%dd %02d:%02d")
16072 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16074 d (floor (/ diff ds)) diff (mod diff ds)
16075 h (floor (/ diff hs)) diff (mod diff hs)
16076 m (floor (/ diff 60)))
16077 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16079 d (floor (+ (/ diff ds) 0.5))
16081 (if (not to-buffer)
16082 (message "%s" (org-make-tdiff-string y d h m))
16083 (if (org-at-table-p)
16085 (goto-char match-end)
16087 (and (looking-at " *|") (goto-char (match-end 0))))
16088 (goto-char match-end))
16090 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
16091 (replace-match ""))
16092 (if negative (insert " -"))
16093 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
16094 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
16095 (insert " " (format fh h m))))
16096 (if align (org-table-align))
16097 (message "Time difference inserted")))))
16099 (defun org-make-tdiff-string (y d h m)
16102 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
16104 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
16106 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
16108 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
16110 (apply 'format fmt (nreverse l))))
16112 (defun org-time-string-to-time (s &optional buffer pos)
16113 "Convert a timestamp string into internal time."
16114 (condition-case errdata
16115 (apply 'encode-time (org-parse-time-string s))
16116 (error (error "Bad timestamp `%s'%s\nError was: %s"
16117 s (if (not (and buffer pos))
16119 (format " at %d in buffer `%s'" pos buffer))
16122 (defun org-time-string-to-seconds (s)
16123 "Convert a timestamp string to a number of seconds."
16124 (org-float-time (org-time-string-to-time s)))
16126 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
16127 "Convert a time stamp to an absolute day number.
16128 If there is a specifier for a cyclic time stamp, get the closest date to
16130 PREFER and SHOW-ALL are passed through to `org-closest-date'.
16131 The variable date is bound by the calendar when this is called."
16133 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
16134 (if (org-diary-sexp-entry (match-string 1 s) "" date)
16137 ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
16138 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
16139 (time-to-days (current-time))) (match-string 0 s)
16142 (condition-case errdata
16143 (apply 'encode-time (org-parse-time-string s))
16144 (error (error "Bad timestamp `%s'%s\nError was: %s"
16145 s (if (not (and buffer pos))
16147 (format " at %d in buffer `%s'" pos buffer))
16148 (cdr errdata))))))))
16150 (defun org-days-to-iso-week (days)
16151 "Return the iso week number."
16153 (car (calendar-iso-from-absolute days)))
16155 (defun org-small-year-to-year (year)
16156 "Convert 2-digit years into 4-digit years.
16157 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
16158 The year 2000 cannot be abbreviated. Any year larger than 99
16159 is returned unchanged."
16161 (setq year (+ 2000 year))
16163 (setq year (+ 1900 year))))
16166 (defun org-time-from-absolute (d)
16167 "Return the time corresponding to date D.
16168 D may be an absolute day number, or a calendar-type list (month day year)."
16169 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
16170 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
16172 (defun org-calendar-holiday ()
16173 "List of holidays, for Diary display in Org-mode."
16174 (require 'holidays)
16176 (if (fboundp 'calendar-check-holidays)
16177 'calendar-check-holidays 'check-calendar-holidays) date)))
16178 (if hl (mapconcat 'identity hl "; "))))
16180 (defun org-diary-sexp-entry (sexp entry date)
16181 "Process a SEXP diary ENTRY for DATE."
16182 (require 'diary-lib)
16183 (let ((result (if calendar-debug-sexp
16184 (let ((stack-trace-on-error t))
16185 (eval (car (read-from-string sexp))))
16186 (condition-case nil
16187 (eval (car (read-from-string sexp)))
16190 (message "Bad sexp at line %d in %s: %s"
16192 (buffer-file-name) sexp)
16194 (cond ((stringp result) (split-string result "; "))
16195 ((and (consp result)
16196 (not (consp (cdr result)))
16197 (stringp (cdr result))) (cdr result))
16198 ((and (consp result)
16199 (stringp (car result))) result)
16202 (defun org-diary-to-ical-string (frombuf)
16203 "Get iCalendar entries from diary entries in buffer FROMBUF.
16204 This uses the icalendar.el library."
16205 (let* ((tmpdir (if (featurep 'xemacs)
16207 temporary-file-directory))
16208 (tmpfile (make-temp-name
16209 (expand-file-name "orgics" tmpdir)))
16211 (with-current-buffer frombuf
16212 (icalendar-export-region (point-min) (point-max) tmpfile)
16213 (setq buf (find-buffer-visiting tmpfile))
16215 (goto-char (point-min))
16216 (if (re-search-forward "^BEGIN:VEVENT" nil t)
16217 (setq b (match-beginning 0)))
16218 (goto-char (point-max))
16219 (if (re-search-backward "^END:VEVENT" nil t)
16220 (setq e (match-end 0)))
16221 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
16223 (delete-file tmpfile)
16226 (defun org-closest-date (start current change prefer show-all)
16227 "Find the date closest to CURRENT that is consistent with START and CHANGE.
16228 When PREFER is `past', return a date that is either CURRENT or past.
16229 When PREFER is `future', return a date that is either CURRENT or future.
16230 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
16231 ;; Make the proper lists from the dates
16233 (let ((a1 '(("h" . hour)
16238 (shour (nth 2 (org-parse-time-string start)))
16239 dn dw sday cday n1 n2 n0
16240 d m y y1 y2 date1 date2 nmonths nm ny m2)
16242 (setq start (org-date-to-gregorian start)
16243 current (org-date-to-gregorian
16246 (time-to-days (current-time))))
16247 sday (calendar-absolute-from-gregorian start)
16248 cday (calendar-absolute-from-gregorian current))
16250 (if (<= cday sday) (throw 'exit sday))
16252 (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
16253 (setq dn (string-to-number (match-string 1 change))
16254 dw (cdr (assoc (match-string 2 change) a1)))
16255 (error "Invalid change specifier: %s" change))
16256 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
16259 (let ((missing-hours
16260 (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
16262 (setq n1 (if (zerop missing-hours) cday
16263 (- cday (1+ (floor (/ missing-hours 24)))))
16264 n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
16266 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
16269 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
16270 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
16271 (setq date1 (list m d y1)
16272 n1 (calendar-absolute-from-gregorian date1)
16273 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
16274 n2 (calendar-absolute-from-gregorian date2)))
16276 ;; approx number of month between the two dates
16277 (setq nmonths (floor (/ (- cday sday) 30.436875)))
16278 ;; How often does dn fit in there?
16279 (setq d (nth 1 start) m (car start) y (nth 2 start)
16280 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
16282 ny (floor (/ m 12))
16285 (while (> m 12) (setq m (- m 12) y (1+ y)))
16286 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
16287 (setq m2 (+ m dn) y2 y)
16288 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16289 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
16290 (while (<= n2 cday)
16291 (setq n1 n2 m m2 y y2)
16292 (setq m2 (+ m dn) y2 y)
16293 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16294 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
16295 ;; Make sure n1 is the earlier date
16296 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
16299 ((eq prefer 'past) (if (= cday n2) n2 n1))
16300 ((eq prefer 'future) (if (= cday n1) n1 n2))
16301 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
16303 ((eq prefer 'past) (if (= cday n2) n2 n1))
16304 ((eq prefer 'future) (if (= cday n1) n1 n2))
16305 (t (if (= cday n1) n1 n2)))))))
16307 (defun org-date-to-gregorian (date)
16308 "Turn any specification of DATE into a Gregorian date for the calendar."
16309 (cond ((integerp date) (calendar-gregorian-from-absolute date))
16310 ((and (listp date) (= (length date) 3)) date)
16312 (setq date (org-parse-time-string date))
16313 (list (nth 4 date) (nth 3 date) (nth 5 date)))
16315 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
16317 (defun org-parse-time-string (s &optional nodefault)
16318 "Parse the standard Org-mode time string.
16319 This should be a lot faster than the normal `parse-time-string'.
16320 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
16321 hour and minute fields will be nil if not given."
16322 (if (string-match org-ts-regexp0 s)
16324 (if (or (match-beginning 8) (not nodefault))
16325 (string-to-number (or (match-string 8 s) "0")))
16326 (if (or (match-beginning 7) (not nodefault))
16327 (string-to-number (or (match-string 7 s) "0")))
16328 (string-to-number (match-string 4 s))
16329 (string-to-number (match-string 3 s))
16330 (string-to-number (match-string 2 s))
16332 (error "Not a standard Org-mode time string: %s" s)))
16334 (defun org-timestamp-up (&optional arg)
16335 "Increase the date item at the cursor by one.
16336 If the cursor is on the year, change the year. If it is on the month,
16337 the day or the time, change that.
16338 With prefix ARG, change by that many units."
16340 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
16342 (defun org-timestamp-down (&optional arg)
16343 "Decrease the date item at the cursor by one.
16344 If the cursor is on the year, change the year. If it is on the month,
16345 the day or the time, change that.
16346 With prefix ARG, change by that many units."
16348 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
16350 (defun org-timestamp-up-day (&optional arg)
16351 "Increase the date in the time stamp by one day.
16352 With prefix ARG, change that many days."
16354 (if (and (not (org-at-timestamp-p t))
16355 (org-at-heading-p))
16357 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
16359 (defun org-timestamp-down-day (&optional arg)
16360 "Decrease the date in the time stamp by one day.
16361 With prefix ARG, change that many days."
16363 (if (and (not (org-at-timestamp-p t))
16364 (org-at-heading-p))
16366 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
16368 (defun org-at-timestamp-p (&optional inactive-ok)
16369 "Determine if the cursor is in or at a timestamp."
16371 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
16373 (ans (or (looking-at tsr)
16375 (skip-chars-backward "^[<\n\r\t")
16376 (if (> (point) (point-min)) (backward-char 1))
16377 (and (looking-at tsr)
16378 (> (- (match-end 0) pos) -1))))))
16380 (boundp 'org-ts-what)
16383 ((= pos (match-beginning 0)) 'bracket)
16384 ;; Point is considered to be "on the bracket" whether
16385 ;; it's really on it or right after it.
16386 ((= pos (1- (match-end 0))) 'bracket)
16387 ((= pos (match-end 0)) 'after)
16388 ((org-pos-in-match-range pos 2) 'year)
16389 ((org-pos-in-match-range pos 3) 'month)
16390 ((org-pos-in-match-range pos 7) 'hour)
16391 ((org-pos-in-match-range pos 8) 'minute)
16392 ((or (org-pos-in-match-range pos 4)
16393 (org-pos-in-match-range pos 5)) 'day)
16394 ((and (> pos (or (match-end 8) (match-end 5)))
16395 (< pos (match-end 0)))
16396 (- pos (or (match-end 8) (match-end 5))))
16400 (defun org-toggle-timestamp-type ()
16401 "Toggle the type (<active> or [inactive]) of a time stamp."
16403 (when (org-at-timestamp-p t)
16404 (let ((beg (match-beginning 0)) (end (match-end 0))
16405 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
16408 (while (re-search-forward "[][<>]" end t)
16409 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
16411 (message "Timestamp is now %sactive"
16412 (if (equal (char-after beg) ?<) "" "in")))))
16414 (defvar org-clock-history) ; defined in org-clock.el
16415 (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
16416 (defun org-timestamp-change (n &optional what updown)
16417 "Change the date in the time stamp at point.
16418 The date will be changed by N times WHAT. WHAT can be `day', `month',
16419 `year', `minute', `second'. If WHAT is not given, the cursor position
16420 in the timestamp determines what will be changed."
16421 (let ((origin (point)) origin-cat
16423 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
16426 ts time time0 fixnext clrgx)
16427 (if (not (org-at-timestamp-p t))
16428 (error "Not at a timestamp"))
16429 (if (and (not what) (eq org-ts-what 'bracket))
16430 (org-toggle-timestamp-type)
16431 ;; Point isn't on brackets. Remember the part of the time-stamp
16432 ;; the point was in. Indeed, size of time-stamps may change,
16433 ;; but point must be kept in the same category nonetheless.
16434 (setq origin-cat org-ts-what)
16435 (if (and (not what) (not (eq org-ts-what 'day))
16436 org-display-custom-times
16437 (get-text-property (point) 'display)
16438 (not (get-text-property (1- (point)) 'display)))
16439 (setq org-ts-what 'day))
16440 (setq org-ts-what (or what org-ts-what)
16441 inactive (= (char-after (match-beginning 0)) ?\[)
16442 ts (match-string 0))
16445 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
16447 (setq extra (match-string 1 ts)))
16448 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
16450 (setq time0 (org-parse-time-string ts))
16452 (eq org-ts-what 'minute)
16453 (not current-prefix-arg))
16454 ;; This looks like s-up and s-down. Change by one rounding step.
16455 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
16456 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
16457 (setcar (cdr time0) (+ (nth 1 time0)
16458 (if (> n 0) (- rem) (- dm rem))))))
16460 (encode-time (or (car time0) 0)
16461 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
16462 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
16463 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
16464 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
16465 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
16467 (when (and (member org-ts-what '(hour minute))
16469 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
16470 (setq extra (org-modify-ts-extra
16472 (if (eq org-ts-what 'hour) 2 5)
16474 (when (integerp org-ts-what)
16475 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
16476 (if (eq what 'calendar)
16477 (let ((cal-date (org-get-date-from-calendar)))
16478 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
16479 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
16480 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
16481 (setcar time0 (or (car time0) 0))
16482 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
16483 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
16484 (setq time (apply 'encode-time time0))))
16485 ;; Insert the new time-stamp, and ensure point stays in the same
16486 ;; category as before (i.e. not after the last position in that
16488 (let ((pos (point)))
16489 ;; Stay before inserted string. `save-excursion' is of no use.
16490 (setq org-last-changed-timestamp
16491 (org-insert-time-stamp time with-hm inactive nil nil extra))
16494 (looking-at org-ts-regexp3)
16496 ;; `day' category ends before `hour' if any, or at
16497 ;; the end of the day name.
16498 ((eq origin-cat 'day)
16499 (min (or (match-beginning 7) (1- (match-end 5))) origin))
16500 ((eq origin-cat 'hour) (min (match-end 7) origin))
16501 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
16502 ((integerp origin-cat) (min (1- (match-end 0)) origin))
16503 ;; `year' and `month' have both fixed size: point
16504 ;; couldn't have moved into another part.
16506 ;; Update clock if on a CLOCK line.
16507 (org-clock-update-time-maybe)
16508 ;; Maybe adjust the closest clock in `org-clock-history'
16509 (when org-clock-adjust-closest
16510 (if (not (and (org-at-clock-log-p)
16511 (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
16512 org-clock-history))))))
16513 (message "No clock to adjust")
16514 (cond ((save-excursion ; fix previous clock?
16515 (re-search-backward org-ts-regexp0 nil t)
16516 (org-looking-back (concat org-clock-string " \\[")))
16517 (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
16518 ((save-excursion ; fix next clock?
16519 (re-search-backward org-ts-regexp0 nil t)
16520 (looking-at (concat org-ts-regexp0 "\\] =>")))
16521 (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
16522 (save-window-excursion
16523 ;; Find closest clock to point, adjust the previous/next one in history
16524 (let* ((p (save-excursion (org-back-to-heading t)))
16525 (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
16527 (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
16528 (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
16530 (message "No clock to adjust")
16532 (org-goto-marker-or-bmk clfixpos)
16534 (when (re-search-forward clrgx nil t)
16535 (goto-char (match-beginning 1))
16536 (let (org-clock-adjust-closest)
16537 (org-timestamp-change n org-ts-what updown))
16538 (message "Clock adjusted in %s for heading: %s"
16539 (file-name-nondirectory (buffer-file-name))
16540 (org-get-heading t t)))))))))
16541 ;; Try to recenter the calendar window, if any.
16542 (if (and org-calendar-follow-timestamp-change
16543 (get-buffer-window "*Calendar*" t)
16544 (memq org-ts-what '(day month year)))
16545 (org-recenter-calendar (time-to-days time))))))
16547 (defun org-modify-ts-extra (s pos n dm)
16548 "Change the different parts of the lead-time and repeat fields in timestamp."
16549 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
16551 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
16553 ((or (org-pos-in-match-range pos 2)
16554 (org-pos-in-match-range pos 3))
16555 (setq m (string-to-number (match-string 3 s))
16556 h (string-to-number (match-string 2 s)))
16557 (if (org-pos-in-match-range pos 2)
16559 (setq n (* dm (org-no-warnings (signum n))))
16560 (when (not (= 0 (setq rem (% m dm))))
16561 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
16563 (if (< m 0) (setq m (+ m 60) h (1- h)))
16564 (if (> m 59) (setq m (- m 60) h (1+ h)))
16565 (setq h (min 24 (max 0 h)))
16566 (setq ng 1 new (format "-%02d:%02d" h m)))
16567 ((org-pos-in-match-range pos 6)
16568 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
16569 ((org-pos-in-match-range pos 5)
16570 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
16572 ((org-pos-in-match-range pos 9)
16573 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
16574 ((org-pos-in-match-range pos 8)
16575 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
16579 (substring s 0 (match-beginning ng))
16581 (substring s (match-end ng))))))
16584 (defun org-recenter-calendar (date)
16585 "If the calendar is visible, recenter it to DATE."
16586 (let ((cwin (get-buffer-window "*Calendar*" t)))
16588 (let ((calendar-move-hook nil))
16589 (with-selected-window cwin
16590 (calendar-goto-date (if (listp date) date
16591 (calendar-gregorian-from-absolute date))))))))
16593 (defun org-goto-calendar (&optional arg)
16594 "Go to the Emacs calendar at the current date.
16595 If there is a time stamp in the current line, go to that date.
16596 A prefix ARG can be used to force the current date."
16598 (let ((tsr org-ts-regexp) diff
16599 (calendar-move-hook nil)
16600 (calendar-view-holidays-initially-flag nil)
16601 (calendar-view-diary-initially-flag nil))
16602 (if (or (org-at-timestamp-p)
16604 (beginning-of-line 1)
16605 (looking-at (concat ".*" tsr))))
16606 (let ((d1 (time-to-days (current-time)))
16608 (org-time-string-to-time (match-string 1)))))
16609 (setq diff (- d2 d1))))
16611 (calendar-goto-today)
16612 (if (and diff (not arg)) (calendar-forward-day diff))))
16614 (defun org-get-date-from-calendar ()
16615 "Return a list (month day year) of date at point in calendar."
16616 (with-current-buffer "*Calendar*"
16618 (calendar-cursor-to-date))))
16620 (defun org-date-from-calendar ()
16621 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
16622 If there is already a time stamp at the cursor position, update it."
16624 (if (org-at-timestamp-p t)
16625 (org-timestamp-change 0 'calendar)
16626 (let ((cal-date (org-get-date-from-calendar)))
16627 (org-insert-time-stamp
16628 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
16630 (defun org-minutes-to-hh:mm-string (m)
16631 "Compute H:MM from a number of minutes."
16632 (let ((h (/ m 60)))
16633 (setq m (- m (* 60 h)))
16634 (format org-time-clocksum-format h m)))
16636 (defun org-hh:mm-string-to-minutes (s)
16637 "Convert a string H:MM to a number of minutes.
16638 If the string is just a number, interpret it as minutes.
16639 In fact, the first hh:mm or number in the string will be taken,
16640 there can be extra stuff in the string.
16641 If no number is found, the return value is 0."
16644 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
16645 (+ (* (string-to-number (match-string 1 s)) 60)
16646 (string-to-number (match-string 2 s))))
16647 ((string-match "\\([0-9]+\\)" s)
16648 (string-to-number (match-string 1 s)))
16651 (defcustom org-effort-durations
16654 ("w" . ,(* 60 8 5))
16655 ("m" . ,(* 60 8 5 4))
16656 ("y" . ,(* 60 8 5 40)))
16657 "Conversion factor to minutes for an effort modifier.
16659 Each entry has the form (MODIFIER . MINUTES).
16661 In an effort string, a number followed by MODIFIER is multiplied
16662 by the specified number of MINUTES to obtain an effort in
16665 For example, if the value of this variable is ((\"hours\" . 60)), then an
16666 effort string \"2hours\" is equivalent to 120 minutes."
16669 :type '(alist :key-type (string :tag "Modifier")
16670 :value-type (number :tag "Minutes")))
16672 (defcustom org-agenda-inhibit-startup t
16673 "Inhibit startup when preparing agenda buffers.
16674 When this variable is `t' (the default), the initialization of
16675 the Org agenda buffers is inhibited: e.g. the visibility state
16676 is not set, the tables are not re-aligned, etc."
16679 :group 'org-agenda)
16681 (defun org-duration-string-to-minutes (s &optional output-to-string)
16682 "Convert a duration string S to minutes.
16684 A bare number is interpreted as minutes, modifiers can be set by
16685 customizing `org-effort-durations' (which see).
16687 Entries containing a colon are interpreted as H:MM by
16688 `org-hh:mm-string-to-minutes'."
16690 (re (concat "\\([0-9.]+\\) *\\("
16691 (regexp-opt (mapcar 'car org-effort-durations))
16693 (while (string-match re s)
16694 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
16695 (string-to-number (match-string 1 s))))
16696 (setq s (replace-match "" nil t s)))
16697 (setq result (floor result))
16698 (incf result (org-hh:mm-string-to-minutes s))
16699 (if output-to-string (number-to-string result) result)))
16703 (defun org-save-all-org-buffers ()
16704 "Save all Org-mode buffers without user confirmation."
16706 (message "Saving all Org-mode buffers...")
16707 (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
16708 (when (featurep 'org-id) (org-id-locations-save))
16709 (message "Saving all Org-mode buffers... done"))
16711 (defun org-revert-all-org-buffers ()
16712 "Revert all Org-mode buffers.
16713 Prompt for confirmation when there are unsaved changes.
16714 Be sure you know what you are doing before letting this function
16715 overwrite your changes.
16717 This function is useful in a setup where one tracks org files
16718 with a version control system, to revert on one machine after pulling
16719 changes from another. I believe the procedure must be like this:
16721 1. M-x org-save-all-org-buffers
16722 2. Pull changes from the other machine, resolve conflicts
16723 3. M-x org-revert-all-org-buffers"
16725 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
16728 (save-window-excursion
16731 (when (and (with-current-buffer b (derived-mode-p 'org-mode))
16732 (with-current-buffer b buffer-file-name))
16733 (org-pop-to-buffer-same-window b)
16734 (revert-buffer t 'no-confirm)))
16736 (when (and (featurep 'org-id) org-id-track-globally)
16737 (org-id-locations-load)))))
16742 (defun org-switchb (&optional arg)
16743 "Switch between Org buffers.
16744 With one prefix argument, restrict available buffers to files.
16745 With two prefix arguments, restrict available buffers to agenda files.
16747 Defaults to `iswitchb' for buffer name completion.
16748 Set `org-completion-use-ido' to make it use ido instead."
16750 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
16751 ((equal arg '(16)) (org-buffer-list 'agenda))
16752 (t (org-buffer-list))))
16753 (org-completion-use-iswitchb org-completion-use-iswitchb)
16754 (org-completion-use-ido org-completion-use-ido))
16755 (unless (or org-completion-use-ido org-completion-use-iswitchb)
16756 (setq org-completion-use-iswitchb t))
16757 (org-pop-to-buffer-same-window
16758 (org-icompleting-read "Org buffer: "
16759 (mapcar 'list (mapcar 'buffer-name blist))
16762 ;;; Define some older names previously used for this functionality
16764 (defalias 'org-ido-switchb 'org-switchb)
16766 (defalias 'org-iswitchb 'org-switchb)
16768 (defun org-buffer-list (&optional predicate exclude-tmp)
16769 "Return a list of Org buffers.
16770 PREDICATE can be `export', `files' or `agenda'.
16772 export restrict the list to Export buffers.
16773 files restrict the list to buffers visiting Org files.
16774 agenda restrict the list to buffers visiting agenda files.
16776 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
16778 (agenda-files (and (eq predicate 'agenda)
16779 (mapcar 'file-truename (org-agenda-files t))))
16782 ((eq predicate 'files)
16783 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
16784 ((eq predicate 'export)
16785 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
16786 ((eq predicate 'agenda)
16788 (with-current-buffer b
16789 (and (derived-mode-p 'org-mode)
16790 (setq bfn (buffer-file-name b))
16791 (member (file-truename bfn) agenda-files)))))
16792 (t (lambda (b) (with-current-buffer b
16793 (or (derived-mode-p 'org-mode)
16794 (string-match "\*Org .*Export"
16795 (buffer-name b)))))))))
16799 (if (and (funcall filter b)
16800 (or (not exclude-tmp)
16801 (not (string-match "tmp" (buffer-name b)))))
16806 (defun org-agenda-files (&optional unrestricted archives)
16807 "Get the list of agenda files.
16808 Optional UNRESTRICTED means return the full list even if a restriction
16809 is currently in place.
16810 When ARCHIVES is t, include all archive files that are really being
16811 used by the agenda files. If ARCHIVE is `ifmode', do this only if
16812 `org-agenda-archives-mode' is t."
16815 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
16816 ((stringp org-agenda-files) (org-read-agenda-file-list))
16817 ((listp org-agenda-files) org-agenda-files)
16818 (t (error "Invalid value of `org-agenda-files'")))))
16819 (setq files (apply 'append
16820 (mapcar (lambda (f)
16821 (if (file-directory-p f)
16823 f t org-agenda-file-regexp)
16826 (when org-agenda-skip-unavailable-files
16827 (setq files (delq nil
16830 (and (file-readable-p file) file)))
16832 (when (or (eq archives t)
16833 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
16834 (setq files (org-add-archive-files files)))
16837 (defun org-agenda-file-p (&optional file)
16838 "Return non-nil, if FILE is an agenda file.
16839 If FILE is omitted, use the file associated with the current
16841 (member (or file (buffer-file-name))
16842 (org-agenda-files t)))
16844 (defun org-edit-agenda-file-list ()
16845 "Edit the list of agenda files.
16846 Depending on setup, this either uses customize to edit the variable
16847 `org-agenda-files', or it visits the file that is holding the list. In the
16848 latter case, the buffer is set up in a way that saving it automatically kills
16849 the buffer and restores the previous window configuration."
16851 (if (stringp org-agenda-files)
16852 (let ((cw (current-window-configuration)))
16853 (find-file org-agenda-files)
16854 (org-set-local 'org-window-configuration cw)
16855 (org-add-hook 'after-save-hook
16857 (set-window-configuration
16858 (prog1 org-window-configuration
16859 (kill-buffer (current-buffer))))
16860 (org-install-agenda-files-menu)
16861 (message "New agenda file list installed"))
16863 (message "%s" (substitute-command-keys
16864 "Edit list and finish with \\[save-buffer]")))
16865 (customize-variable 'org-agenda-files)))
16867 (defun org-store-new-agenda-file-list (list)
16868 "Set new value for the agenda file list and save it correctly."
16869 (if (stringp org-agenda-files)
16870 (let ((fe (org-read-agenda-file-list t)) b u)
16871 (while (setq b (find-buffer-visiting org-agenda-files))
16873 (with-temp-file org-agenda-files
16876 (lambda (f) ;; Keep un-expanded entries.
16877 (if (setq u (assoc f fe))
16882 (let ((org-mode-hook nil) (org-inhibit-startup t)
16883 (org-insert-mode-line-in-empty-file nil))
16884 (setq org-agenda-files list)
16885 (customize-save-variable 'org-agenda-files org-agenda-files))))
16887 (defun org-read-agenda-file-list (&optional pair-with-expansion)
16888 "Read the list of agenda files from a file.
16889 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
16890 filenames, used by `org-store-new-agenda-file-list' to write back
16891 un-expanded file names."
16892 (when (file-directory-p org-agenda-files)
16893 (error "`org-agenda-files' cannot be a single directory"))
16894 (when (stringp org-agenda-files)
16896 (insert-file-contents org-agenda-files)
16899 (let ((e (expand-file-name (substitute-in-file-name f)
16901 (if pair-with-expansion
16904 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
16907 (defun org-cycle-agenda-files ()
16908 "Cycle through the files in `org-agenda-files'.
16909 If the current buffer visits an agenda file, find the next one in the list.
16910 If the current buffer does not, find the first agenda file."
16912 (let* ((fs (org-agenda-files t))
16913 (files (append fs (list (car fs))))
16914 (tcf (if buffer-file-name (file-truename buffer-file-name)))
16916 (unless files (error "No agenda files"))
16918 (while (setq file (pop files))
16919 (if (equal (file-truename file) tcf)
16921 (find-file (car files))
16923 (find-file (car fs)))
16924 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
16926 (defun org-agenda-file-to-front (&optional to-end)
16927 "Move/add the current file to the top of the agenda file list.
16928 If the file is not present in the list, it is added to the front. If it is
16929 present, it is moved there. With optional argument TO-END, add/move to the
16932 (let ((org-agenda-skip-unavailable-files nil)
16933 (file-alist (mapcar (lambda (x)
16934 (cons (file-truename x) x))
16935 (org-agenda-files t)))
16936 (ctf (file-truename
16937 (or buffer-file-name
16938 (error "Please save the current buffer to a file"))))
16940 (setq x (assoc ctf file-alist) had x)
16942 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
16944 (setq file-alist (append (delq x file-alist) (list x)))
16945 (setq file-alist (cons x (delq x file-alist))))
16946 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
16947 (org-install-agenda-files-menu)
16948 (message "File %s to %s of agenda file list"
16949 (if had "moved" "added") (if to-end "end" "front"))))
16951 (defun org-remove-file (&optional file)
16952 "Remove current file from the list of files in variable `org-agenda-files'.
16953 These are the files which are being checked for agenda entries.
16954 Optional argument FILE means use this file instead of the current."
16956 (let* ((org-agenda-skip-unavailable-files nil)
16957 (file (or file buffer-file-name
16958 (error "Current buffer does not visit a file")))
16959 (true-file (file-truename file))
16960 (afile (abbreviate-file-name file))
16961 (files (delq nil (mapcar
16963 (if (equal true-file
16966 (org-agenda-files t)))))
16967 (if (not (= (length files) (length (org-agenda-files t))))
16969 (org-store-new-agenda-file-list files)
16970 (org-install-agenda-files-menu)
16971 (message "Removed file: %s" afile))
16972 (message "File was not in list: %s (not removed)" afile))))
16974 (defun org-file-menu-entry (file)
16975 (vector file (list 'find-file file) t))
16977 (defun org-check-agenda-file (file)
16978 "Make sure FILE exists. If not, ask user what to do."
16979 (when (not (file-exists-p file))
16980 (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
16981 (abbreviate-file-name file))
16982 (let ((r (downcase (read-char-exclusive))))
16985 (org-remove-file file)
16986 (throw 'nextfile t))
16987 (t (error "Abort"))))))
16989 (defun org-get-agenda-file-buffer (file)
16990 "Get a buffer visiting FILE. If the buffer needs to be created, add
16991 it to the list of buffers which might be released later."
16992 (let ((buf (org-find-base-buffer-visiting file)))
16994 buf ; just return it
16995 ;; Make a new buffer and remember it
16996 (setq buf (find-file-noselect file))
16997 (if buf (push buf org-agenda-new-buffers))
17000 (defun org-release-buffers (blist)
17001 "Release all buffers in list, asking the user for confirmation when needed.
17002 When a buffer is unmodified, it is just killed. When modified, it is saved
17003 \(if the user agrees) and then killed."
17005 (while (setq buf (pop blist))
17006 (setq file (buffer-file-name buf))
17007 (when (and (buffer-modified-p buf)
17009 (y-or-n-p (format "Save file %s? " file)))
17010 (with-current-buffer buf (save-buffer)))
17011 (kill-buffer buf))))
17013 (defun org-agenda-prepare-buffers (files)
17014 "Create buffers for all agenda files, protect archived trees and comments."
17016 (let ((pa '(:org-archived t))
17017 (pc '(:org-comment t))
17018 (pall '(:org-archived t :org-comment t))
17019 (inhibit-read-only t)
17020 (org-inhibit-startup org-agenda-inhibit-startup)
17021 (rea (concat ":" org-archive-tag ":"))
17025 (while (setq file (pop files))
17029 (org-check-agenda-file file)
17030 (set-buffer (org-get-agenda-file-buffer file)))
17032 (setq bmp (buffer-modified-p))
17033 (org-refresh-category-properties)
17034 (org-refresh-properties org-effort-property 'org-effort)
17035 (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
17036 (setq org-todo-keywords-for-agenda
17037 (append org-todo-keywords-for-agenda org-todo-keywords-1))
17038 (setq org-done-keywords-for-agenda
17039 (append org-done-keywords-for-agenda org-done-keywords))
17040 (setq org-todo-keyword-alist-for-agenda
17041 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
17042 (setq org-drawers-for-agenda
17043 (append org-drawers-for-agenda org-drawers))
17044 (setq org-tag-alist-for-agenda
17045 (append org-tag-alist-for-agenda org-tag-alist))
17048 (remove-text-properties (point-min) (point-max) pall)
17049 (when org-agenda-skip-archived-trees
17050 (goto-char (point-min))
17051 (while (re-search-forward rea nil t)
17052 (if (org-at-heading-p t)
17053 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
17054 (goto-char (point-min))
17055 (setq re (format org-heading-keyword-regexp-format
17056 org-comment-string))
17057 (while (re-search-forward re nil t)
17058 (add-text-properties
17059 (match-beginning 0) (org-end-of-subtree t) pc)))
17060 (set-buffer-modified-p bmp)))))
17061 (setq org-todo-keywords-for-agenda
17062 (org-uniquify org-todo-keywords-for-agenda))
17063 (setq org-todo-keyword-alist-for-agenda
17064 (org-uniquify org-todo-keyword-alist-for-agenda)
17065 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
17067 ;;;; Embedded LaTeX
17069 (defvar org-cdlatex-mode-map (make-sparse-keymap)
17070 "Keymap for the minor `org-cdlatex-mode'.")
17072 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
17073 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
17074 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
17075 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
17076 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
17078 (defvar org-cdlatex-texmathp-advice-is-done nil
17079 "Flag remembering if we have applied the advice to texmathp already.")
17081 (define-minor-mode org-cdlatex-mode
17082 "Toggle the minor `org-cdlatex-mode'.
17083 This mode supports entering LaTeX environment and math in LaTeX fragments
17085 \\{org-cdlatex-mode-map}"
17087 (when org-cdlatex-mode
17089 (run-hooks 'cdlatex-mode-hook)
17090 (cdlatex-compute-tables))
17091 (unless org-cdlatex-texmathp-advice-is-done
17092 (setq org-cdlatex-texmathp-advice-is-done t)
17093 (defadvice texmathp (around org-math-always-on activate)
17094 "Always return t in org-mode buffers.
17095 This is because we want to insert math symbols without dollars even outside
17096 the LaTeX math segments. If Orgmode thinks that point is actually inside
17097 an embedded LaTeX fragment, let texmathp do its job.
17098 \\[org-cdlatex-mode-map]"
17102 ((not (derived-mode-p 'org-mode)) ad-do-it)
17103 ((eq this-command 'cdlatex-math-symbol)
17104 (setq ad-return-value t
17105 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
17107 (let ((p (org-inside-LaTeX-fragment-p)))
17108 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
17109 (setq ad-return-value t
17110 texmathp-why '("Org-mode embedded math" . 0))
17111 (if p ad-do-it)))))))))
17113 (defun turn-on-org-cdlatex ()
17114 "Unconditionally turn on `org-cdlatex-mode'."
17115 (org-cdlatex-mode 1))
17117 (defun org-inside-LaTeX-fragment-p ()
17118 "Test if point is inside a LaTeX fragment.
17119 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
17120 sequence appearing also before point.
17121 Even though the matchers for math are configurable, this function assumes
17122 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
17123 delimiters are skipped when they have been removed by customization.
17124 The return value is nil, or a cons cell with the delimiter and the
17125 position of this delimiter.
17127 This function does a reasonably good job, but can locally be fooled by
17128 for example currency specifications. For example it will assume being in
17129 inline math after \"$22.34\". The LaTeX fragment formatter will only format
17130 fragments that are properly closed, but during editing, we have to live
17131 with the uncertainty caused by missing closing delimiters. This function
17132 looks only before point, not after."
17134 (let ((pos (point))
17135 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
17137 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
17139 dd-on str (start 0) m re)
17142 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
17143 re (nth 1 (assoc "$" org-latex-regexps)))
17144 (while (string-match re str start)
17146 ((= (match-end 0) (length str))
17147 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
17148 ((= (match-end 0) (- (length str) 5))
17150 (t (setq start (match-end 0))))))
17151 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
17153 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
17154 (and (match-beginning 2) (throw 'exit nil))
17156 (while (re-search-backward "\\$\\$" lim t)
17157 (setq dd-on (not dd-on)))
17159 (if dd-on (cons "$$" m))))))
17161 (defun org-inside-latex-macro-p ()
17162 "Is point inside a LaTeX macro or its arguments?"
17165 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
17167 (defun org-try-cdlatex-tab ()
17168 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
17169 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
17170 - inside a LaTeX fragment, or
17171 - after the first word in a line, where an abbreviation expansion could
17172 insert a LaTeX environment."
17173 (when org-cdlatex-mode
17175 ;; Before any word on the line: No expansion possible.
17176 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
17177 ;; Just after first word on the line: Expand it. Make sure it
17178 ;; cannot happen on headlines, though.
17180 (skip-chars-backward "a-zA-Z0-9*")
17181 (skip-chars-backward " \t")
17182 (and (bolp) (not (org-at-heading-p))))
17184 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
17186 (defun org-cdlatex-underscore-caret (&optional arg)
17187 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
17188 Revert to the normal definition outside of these fragments."
17190 (if (org-inside-LaTeX-fragment-p)
17191 (call-interactively 'cdlatex-sub-superscript)
17192 (let (org-cdlatex-mode)
17193 (call-interactively (key-binding (vector last-input-event))))))
17195 (defun org-cdlatex-math-modify (&optional arg)
17196 "Execute `cdlatex-math-modify' in LaTeX fragments.
17197 Revert to the normal definition outside of these fragments."
17199 (if (org-inside-LaTeX-fragment-p)
17200 (call-interactively 'cdlatex-math-modify)
17201 (let (org-cdlatex-mode)
17202 (call-interactively (key-binding (vector last-input-event))))))
17204 (defvar org-latex-fragment-image-overlays nil
17205 "List of overlays carrying the images of latex fragments.")
17206 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
17208 (defun org-remove-latex-fragment-image-overlays ()
17209 "Remove all overlays with LaTeX fragment images in current buffer."
17210 (mapc 'delete-overlay org-latex-fragment-image-overlays)
17211 (setq org-latex-fragment-image-overlays nil))
17213 (defun org-preview-latex-fragment (&optional subtree)
17214 "Preview the LaTeX fragment at point, or all locally or globally.
17215 If the cursor is in a LaTeX fragment, create the image and overlay
17216 it over the source code. If there is no fragment at point, display
17217 all fragments in the current text, from one headline to the next. With
17218 prefix SUBTREE, display all fragments in the current subtree. With a
17219 double prefix arg \\[universal-argument] \\[universal-argument], or when \
17220 the cursor is before the first headline,
17221 display all fragments in the buffer.
17222 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
17224 (unless buffer-file-name
17225 (error "Can't preview LaTeX fragment in a non-file buffer"))
17226 (org-remove-latex-fragment-image-overlays)
17229 (let (beg end at msg)
17231 ((or (equal subtree '(16))
17232 (not (save-excursion
17233 (re-search-backward org-outline-regexp-bol nil t))))
17234 (setq beg (point-min) end (point-max)
17235 msg "Creating images for buffer...%s"))
17236 ((equal subtree '(4))
17237 (org-back-to-heading)
17238 (setq beg (point) end (org-end-of-subtree t)
17239 msg "Creating images for subtree...%s"))
17241 (if (setq at (org-inside-LaTeX-fragment-p))
17242 (goto-char (max (point-min) (- (cdr at) 2)))
17243 (org-back-to-heading))
17244 (setq beg (point) end (progn (outline-next-heading) (point))
17245 msg (if at "Creating image...%s"
17246 "Creating images for entry...%s"))))
17248 (narrow-to-region beg end)
17251 (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
17252 (file-name-nondirectory
17253 buffer-file-name)))
17254 default-directory 'overlays msg at 'forbuffer
17255 org-latex-create-formula-image-program)
17256 (message msg "done. Use `C-c C-c' to remove images.")))))
17258 (defvar org-latex-regexps
17259 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
17260 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
17261 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
17262 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17263 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17264 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
17265 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
17266 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
17267 "Regular expressions for matching embedded LaTeX.")
17269 (defvar org-export-have-math nil) ;; dynamic scoping
17270 (defun org-format-latex (prefix &optional dir overlays msg at
17271 forbuffer processing-type)
17272 "Replace LaTeX fragments with links to an image, and produce images.
17273 Some of the options can be changed using the variable
17274 `org-format-latex-options'."
17275 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
17276 (let* ((prefixnodir (file-name-nondirectory prefix))
17277 (absprefix (expand-file-name prefix dir))
17278 (todir (file-name-directory absprefix))
17279 (opt org-format-latex-options)
17280 (matchers (plist-get opt :matchers))
17281 (re-list org-latex-regexps)
17282 (org-format-latex-header-extra
17283 (plist-get (org-infile-export-plist) :latex-header-extra))
17284 (cnt 0) txt hash link beg end re e checkdir
17285 executables-checked string
17286 m n block-type block linkfile movefile ov)
17287 ;; Check the different regular expressions
17288 (while (setq e (pop re-list))
17289 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
17290 block (if block-type "\n\n" ""))
17291 (when (member m matchers)
17292 (goto-char (point-min))
17293 (while (re-search-forward re nil t)
17294 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
17295 (not (get-text-property (match-beginning n)
17298 (not (eq (get-char-property (match-beginning n)
17300 'org-latex-overlay))))
17301 (setq org-export-have-math t)
17303 ((eq processing-type 'verbatim)
17304 ;; Leave the text verbatim, just protect it
17305 (add-text-properties (match-beginning n) (match-end n)
17306 '(org-protected t)))
17307 ((eq processing-type 'mathjax)
17308 ;; Prepare for MathJax processing
17309 (setq string (match-string n))
17310 (if (member m '("$" "$1"))
17312 (delete-region (match-beginning n) (match-end n))
17313 (goto-char (match-beginning n))
17314 (insert (org-add-props (concat "\\(" (substring string 1 -1)
17316 '(org-protected t))))
17317 (add-text-properties (match-beginning n) (match-end n)
17318 '(org-protected t))))
17319 ((or (eq processing-type 'dvipng)
17320 (eq processing-type 'imagemagick))
17321 ;; Process to an image
17322 (setq txt (match-string n)
17323 beg (match-beginning n) end (match-end n)
17325 (let (print-length print-level) ; make sure full list is printed
17326 (setq hash (sha1 (prin1-to-string
17327 (list org-format-latex-header
17328 org-format-latex-header-extra
17329 org-export-latex-default-packages-alist
17330 org-export-latex-packages-alist
17331 org-format-latex-options
17333 linkfile (format "%s_%s.png" prefix hash)
17334 movefile (format "%s_%s.png" absprefix hash)))
17335 (setq link (concat block "[[file:" linkfile "]]" block))
17336 (if msg (message msg cnt))
17338 (unless checkdir ; make sure the directory exists
17340 (or (file-directory-p todir) (make-directory todir t)))
17342 ((eq processing-type 'dvipng)
17343 (unless executables-checked
17344 (org-check-external-command
17345 "latex" "needed to convert LaTeX fragments to images")
17346 (org-check-external-command
17347 "dvipng" "needed to convert LaTeX fragments to images")
17348 (setq executables-checked t))
17349 (unless (file-exists-p movefile)
17350 (org-create-formula-image-with-dvipng
17351 txt movefile opt forbuffer)))
17352 ((eq processing-type 'imagemagick)
17353 (unless executables-checked
17354 (org-check-external-command
17355 "convert" "you need to install imagemagick")
17356 (setq executables-checked t))
17357 (unless (file-exists-p movefile)
17358 (org-create-formula-image-with-imagemagick
17359 txt movefile opt forbuffer))))
17363 (if (eq (overlay-get o 'org-overlay-type)
17364 'org-latex-overlay)
17365 (delete-overlay o)))
17366 (overlays-in beg end))
17367 (setq ov (make-overlay beg end))
17368 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
17369 (if (featurep 'xemacs)
17371 (overlay-put ov 'invisible t)
17374 (make-glyph (vector 'png :file movefile))))
17377 (list 'image :type 'png :file movefile :ascent 'center)))
17378 (push ov org-latex-fragment-image-overlays)
17380 (delete-region beg end)
17381 (insert (org-add-props link
17382 (list 'org-latex-src
17383 (replace-regexp-in-string
17385 'org-latex-src-embed-type
17386 (if block-type 'paragraph 'character))))))
17387 ((eq processing-type 'mathml)
17388 ;; Process to MathML
17389 (unless executables-checked
17390 (unless (save-match-data (org-format-latex-mathml-available-p))
17391 (error "LaTeX to MathML converter not configured"))
17392 (setq executables-checked t))
17393 (setq txt (match-string n)
17394 beg (match-beginning n) end (match-end n)
17396 (if msg (message msg cnt))
17398 (delete-region beg end)
17399 (insert (org-format-latex-as-mathml
17400 txt block-type prefix dir)))
17402 (error "Unknown conversion type %s for latex fragments"
17403 processing-type)))))))))
17405 (defun org-create-math-formula (latex-frag &optional mathml-file)
17406 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
17407 Use `org-latex-to-mathml-convert-command'. If the conversion is
17408 sucessful, return the portion between \"<math...> </math>\"
17409 elements otherwise return nil. When MATHML-FILE is specified,
17410 write the results in to that file. When invoked as an
17411 interactive command, prompt for LATEX-FRAG, with initial value
17412 set to the current active region and echo the results for user
17414 (interactive (list (let ((frag (when (org-region-active-p)
17415 (buffer-substring-no-properties
17416 (region-beginning) (region-end)))))
17417 (read-string "LaTeX Fragment: " frag nil frag))))
17418 (unless latex-frag (error "Invalid latex-frag"))
17419 (let* ((tmp-in-file (file-relative-name
17420 (make-temp-name (expand-file-name "ltxmathml-in"))))
17421 (ignore (write-region latex-frag nil tmp-in-file))
17422 (tmp-out-file (file-relative-name
17423 (make-temp-name (expand-file-name "ltxmathml-out"))))
17425 org-latex-to-mathml-convert-command
17426 `((?j . ,(shell-quote-argument
17427 (expand-file-name org-latex-to-mathml-jar-file)))
17428 (?I . ,(shell-quote-argument tmp-in-file))
17429 (?o . ,(shell-quote-argument tmp-out-file)))))
17430 mathml shell-command-output)
17431 (when (org-called-interactively-p 'any)
17432 (unless (org-format-latex-mathml-available-p)
17433 (error "LaTeX to MathML converter not configured")))
17434 (message "Running %s" cmd)
17435 (setq shell-command-output (shell-command-to-string cmd))
17437 (when (file-readable-p tmp-out-file)
17438 (with-current-buffer (find-file-noselect tmp-out-file t)
17439 (goto-char (point-min))
17440 (when (re-search-forward
17443 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
17445 (regexp-quote "</math>")) nil t)
17446 (prog1 (match-string 0) (kill-buffer))))))
17450 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
17452 (write-region mathml nil mathml-file))
17453 (when (org-called-interactively-p 'any)
17455 ((message "LaTeX to MathML conversion failed")
17456 (message shell-command-output)))
17457 (delete-file tmp-in-file)
17458 (when (file-exists-p tmp-out-file)
17459 (delete-file tmp-out-file))
17462 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
17463 prefix &optional dir)
17464 "Use `org-create-math-formula' but check local cache first."
17465 (let* ((absprefix (expand-file-name prefix dir))
17466 (print-length nil) (print-level nil)
17467 (formula-id (concat
17472 org-latex-to-mathml-convert-command)))))
17473 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
17474 (formula-cache-dir (file-name-directory formula-cache)))
17476 (unless (file-directory-p formula-cache-dir)
17477 (make-directory formula-cache-dir t))
17479 (unless (file-exists-p formula-cache)
17480 (org-create-math-formula latex-frag formula-cache))
17482 (if (file-exists-p formula-cache)
17483 ;; Successful conversion. Return the link to MathML file.
17485 (format "[[file:%s]]" (file-relative-name formula-cache dir))
17486 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
17487 'org-latex-src-embed-type (if latex-frag-type
17488 'paragraph 'character)))
17489 ;; Failed conversion. Return the LaTeX fragment verbatim
17490 (add-text-properties
17491 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
17494 ;; This function borrows from Ganesh Swami's latex2png.el
17495 (defun org-create-formula-image-with-dvipng (string tofile options buffer)
17496 "This calls dvipng."
17497 (require 'org-latex)
17498 (let* ((tmpdir (if (featurep 'xemacs)
17500 temporary-file-directory))
17501 (texfilebase (make-temp-name
17502 (expand-file-name "orgtex" tmpdir)))
17503 (texfile (concat texfilebase ".tex"))
17504 (dvifile (concat texfilebase ".dvi"))
17505 (pngfile (concat texfilebase ".png"))
17506 (fnh (if (featurep 'xemacs)
17507 (font-height (face-font 'default))
17508 (face-attribute 'default :height nil)))
17509 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17510 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17511 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17513 (bg (or (plist-get options (if buffer :background :html-background))
17515 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
17516 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
17517 (with-temp-file texfile
17518 (insert (org-splice-latex-header
17519 org-format-latex-header
17520 org-export-latex-default-packages-alist
17521 org-export-latex-packages-alist t
17522 org-format-latex-header-extra))
17523 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
17524 (require 'org-latex)
17525 (org-export-latex-fix-inputenc))
17526 (let ((dir default-directory))
17527 (condition-case nil
17530 (call-process "latex" nil nil nil texfile))
17533 (if (not (file-exists-p dvifile))
17534 (progn (message "Failed to create dvi file from %s" texfile) nil)
17535 (condition-case nil
17536 (if (featurep 'xemacs)
17537 (call-process "dvipng" nil nil nil
17542 (call-process "dvipng" nil nil nil
17545 ;;"-x" scale "-y" scale
17550 (if (not (file-exists-p pngfile))
17551 (if org-format-latex-signal-error
17552 (error "Failed to create png file from %s" texfile)
17553 (message "Failed to create png file from %s" texfile)
17555 ;; Use the requested file name and clean up
17556 (copy-file pngfile tofile 'replace)
17557 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
17558 (if (file-exists-p (concat texfilebase e))
17559 (delete-file (concat texfilebase e))))
17562 (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
17563 (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
17564 "This calls convert, which is included into imagemagick."
17565 (require 'org-latex)
17566 (let* ((tmpdir (if (featurep 'xemacs)
17568 temporary-file-directory))
17569 (texfilebase (make-temp-name
17570 (expand-file-name "orgtex" tmpdir)))
17571 (texfile (concat texfilebase ".tex"))
17572 (pdffile (concat texfilebase ".pdf"))
17573 (pngfile (concat texfilebase ".png"))
17574 (fnh (if (featurep 'xemacs)
17575 (font-height (face-font 'default))
17576 (face-attribute 'default :height nil)))
17577 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17578 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17579 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17581 (bg (or (plist-get options (if buffer :background :html-background))
17583 (if (eq fg 'default) (setq fg (org-latex-color :foreground))
17584 (setq fg (org-latex-color-format fg)))
17585 (if (eq bg 'default) (setq bg (org-latex-color :background))
17586 (setq bg (org-latex-color-format
17587 (if (string= bg "Transparent")(setq bg "white")))))
17588 (with-temp-file texfile
17589 (insert (org-splice-latex-header
17590 org-format-latex-header
17591 org-export-latex-default-packages-alist
17592 org-export-latex-packages-alist t
17593 org-format-latex-header-extra))
17594 (insert "\n\\begin{document}\n"
17595 "\\definecolor{fg}{rgb}{" fg "}\n"
17596 "\\definecolor{bg}{rgb}{" bg "}\n"
17597 "\n\\pagecolor{bg}\n"
17601 "\n\\end{document}\n" )
17602 (require 'org-latex)
17603 (org-export-latex-fix-inputenc))
17604 (let ((dir default-directory) cmd cmds latex-frags-cmds)
17605 (condition-case nil
17608 (setq cmds org-latex-to-pdf-process)
17610 (setq latex-frags-cmds (pop cmds))
17611 (if (listp latex-frags-cmds)
17613 (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
17614 (while latex-frags-cmds
17615 (setq cmd (pop latex-frags-cmds))
17616 (while (string-match "%b" cmd)
17617 (setq cmd (replace-match
17619 (shell-quote-argument texfile))
17621 (while (string-match "%f" cmd)
17622 (setq cmd (replace-match
17624 (shell-quote-argument (file-name-nondirectory texfile)))
17626 (while (string-match "%o" cmd)
17627 (setq cmd (replace-match
17629 (shell-quote-argument (file-name-directory texfile)))
17631 (setq cmd (split-string cmd))
17632 (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
17635 (if (not (file-exists-p pdffile))
17636 (progn (message "Failed to create pdf file from %s" texfile) nil)
17637 (condition-case nil
17638 (if (featurep 'xemacs)
17639 (call-process "convert" nil nil nil
17645 ;; "-sharpen" "0x1.0"
17647 (call-process "convert" nil nil nil
17653 ; "-sharpen" "0x1.0"
17656 (if (not (file-exists-p pngfile))
17657 (if org-format-latex-signal-error
17658 (error "Failed to create png file from %s" texfile)
17659 (message "Failed to create png file from %s" texfile)
17661 ;; Use the requested file name and clean up
17662 (copy-file pngfile tofile 'replace)
17663 (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
17664 (if (file-exists-p (concat texfilebase e))
17665 (delete-file (concat texfilebase e))))
17668 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
17669 "Fill a LaTeX header template TPL.
17670 In the template, the following place holders will be recognized:
17672 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
17673 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
17674 [PACKAGES] \\usepackage statements for PKG
17675 [NO-PACKAGES] do not include PKG
17676 [EXTRA] the string EXTRA
17677 [NO-EXTRA] do not include EXTRA
17679 For backward compatibility, if both the positive and the negative place
17680 holder is missing, the positive one (without the \"NO-\") will be
17681 assumed to be present at the end of the template.
17682 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
17684 SNIPPETS-P indicates if this is run to create snippet images for HTML."
17685 (let (rpl (end ""))
17686 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
17687 (setq rpl (if (or (match-end 1) (not def-pkg))
17688 "" (org-latex-packages-to-string def-pkg snippets-p t))
17689 tpl (replace-match rpl t t tpl))
17690 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
17692 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
17693 (setq rpl (if (or (match-end 1) (not pkg))
17694 "" (org-latex-packages-to-string pkg snippets-p t))
17695 tpl (replace-match rpl t t tpl))
17698 (org-latex-packages-to-string pkg snippets-p)))))
17700 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
17701 (setq rpl (if (or (match-end 1) (not extra))
17702 "" (concat extra "\n"))
17703 tpl (replace-match rpl t t tpl))
17704 (if (and extra (string-match "\\S-" extra))
17705 (setq end (concat end "\n" extra))))
17707 (if (string-match "\\S-" end)
17708 (concat tpl "\n" end)
17711 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
17712 "Turn an alist of packages into a string with the \\usepackage macros."
17713 (setq pkg (mapconcat (lambda(p)
17716 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
17717 (format "%% Package %s omitted" (cadr p)))
17718 ((equal "" (car p))
17719 (format "\\usepackage{%s}" (cadr p)))
17721 (format "\\usepackage[%s]{%s}"
17722 (car p) (cadr p)))))
17725 (if newline (concat pkg "\n") pkg))
17727 (defun org-dvipng-color (attr)
17728 "Return a RGB color specification for dvipng."
17729 (apply 'format "rgb %s %s %s"
17730 (mapcar 'org-normalize-color
17731 (if (featurep 'xemacs)
17732 (color-rgb-components
17733 (face-property 'default
17734 (cond ((eq attr :foreground) 'foreground)
17735 ((eq attr :background) 'background))))
17736 (color-values (face-attribute 'default attr nil))))))
17738 (defun org-latex-color (attr)
17739 "Return a RGB color for the LaTeX color package."
17740 (apply 'format "%s,%s,%s"
17741 (mapcar 'org-normalize-color
17742 (if (featurep 'xemacs)
17743 (color-rgb-components
17744 (face-property 'default
17745 (cond ((eq attr :foreground) 'foreground)
17746 ((eq attr :background) 'background))))
17747 (color-values (face-attribute 'default attr nil))))))
17749 (defun org-latex-color-format (color-name)
17750 "Convert COLOR-NAME to a RGB color value."
17751 (apply 'format "%s,%s,%s"
17752 (mapcar 'org-normalize-color
17753 (color-values color-name))))
17755 (defun org-normalize-color (value)
17756 "Return string to be used as color value for an RGB component."
17757 (format "%g" (/ value 65535.0)))
17762 (defvar org-inline-image-overlays nil)
17763 (make-variable-buffer-local 'org-inline-image-overlays)
17765 (defun org-toggle-inline-images (&optional include-linked)
17766 "Toggle the display of inline images.
17767 INCLUDE-LINKED is passed to `org-display-inline-images'."
17769 (if org-inline-image-overlays
17771 (org-remove-inline-images)
17772 (message "Inline image display turned off"))
17773 (org-display-inline-images include-linked)
17774 (if org-inline-image-overlays
17775 (message "%d images displayed inline"
17776 (length org-inline-image-overlays))
17777 (message "No images to display inline"))))
17779 (defun org-redisplay-inline-images ()
17780 "Refresh the display of inline images."
17782 (if (not org-inline-image-overlays)
17783 (org-toggle-inline-images)
17784 (org-toggle-inline-images)
17785 (org-toggle-inline-images)))
17787 (defun org-display-inline-images (&optional include-linked refresh beg end)
17788 "Display inline images.
17789 Normally only links without a description part are inlined, because this
17790 is how it will work for export. When INCLUDE-LINKED is set, also links
17791 with a description part will be inlined. This can be nice for a quick
17792 look at those images, but it does not reflect what exported files will look
17794 When REFRESH is set, refresh existing images between BEG and END.
17795 This will create new image displays only if necessary.
17796 BEG and END default to the buffer boundaries."
17799 (org-remove-inline-images)
17800 (if (fboundp 'clear-image-cache) (clear-image-cache)))
17804 (setq beg (or beg (point-min)) end (or end (point-max)))
17806 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
17807 (substring (org-image-file-name-regexp) 0 -2)
17808 "\\)\\]" (if include-linked "" "\\]")))
17810 (while (re-search-forward re end t)
17811 (setq old (get-char-property-and-overlay (match-beginning 1)
17812 'org-image-overlay))
17813 (setq file (expand-file-name
17814 (concat (or (match-string 3) "") (match-string 4))))
17815 (when (file-exists-p file)
17816 (if (and (car-safe old) refresh)
17817 (image-refresh (overlay-get (cdr old) 'display))
17818 (setq img (save-match-data (create-image file)))
17820 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
17821 (overlay-put ov 'display img)
17822 (overlay-put ov 'face 'default)
17823 (overlay-put ov 'org-image-overlay t)
17824 (overlay-put ov 'modification-hooks
17825 (list 'org-display-inline-remove-overlay))
17826 (push ov org-inline-image-overlays)))))))))
17828 (define-obsolete-function-alias
17829 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
17831 (defun org-display-inline-remove-overlay (ov after beg end &optional len)
17832 "Remove inline-display overlay if a corresponding region is modified."
17833 (let ((inhibit-modification-hooks t))
17834 (when (and ov after)
17835 (delete ov org-inline-image-overlays)
17836 (delete-overlay ov))))
17838 (defun org-remove-inline-images ()
17839 "Remove inline display of images."
17841 (mapc 'delete-overlay org-inline-image-overlays)
17842 (setq org-inline-image-overlays nil))
17846 ;; Outline functions from `outline-mode-prefix-map'
17847 ;; that can be remapped in Org:
17848 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
17849 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
17850 (define-key org-mode-map [remap outline-forward-same-level]
17851 'org-forward-heading-same-level)
17852 (define-key org-mode-map [remap outline-backward-same-level]
17853 'org-backward-heading-same-level)
17854 (define-key org-mode-map [remap show-branches]
17855 'org-kill-note-or-show-branches)
17856 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
17857 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
17858 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
17860 ;; Outline functions from `outline-mode-prefix-map' that can not
17861 ;; be remapped in Org:
17863 ;; - the column "key binding" shows whether the Outline function is still
17864 ;; available in Org mode on the same key that it has been bound to in
17866 ;; - "overridden": key used for a different functionality in Org mode
17867 ;; - else: key still bound to the same Outline function in Org mode
17869 ;; | Outline function | key binding | Org replacement |
17870 ;; |------------------------------------+-------------+-----------------------|
17871 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
17872 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
17873 ;; | `outline-up-heading' | `C-c C-u' | still same function |
17874 ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
17875 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
17876 ;; | `show-entry' | overridden | no replacement |
17877 ;; | `show-children' | `C-c C-i' | visibility cycling |
17878 ;; | `show-branches' | `C-c C-k' | still same function |
17879 ;; | `show-subtree' | overridden | visibility cycling |
17880 ;; | `show-all' | overridden | no replacement |
17881 ;; | `hide-subtree' | overridden | visibility cycling |
17882 ;; | `hide-body' | overridden | no replacement |
17883 ;; | `hide-entry' | overridden | visibility cycling |
17884 ;; | `hide-leaves' | overridden | no replacement |
17885 ;; | `hide-sublevels' | overridden | no replacement |
17886 ;; | `hide-other' | overridden | no replacement |
17888 ;; Make `C-c C-x' a prefix key
17889 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
17891 ;; TAB key with modifiers
17892 (org-defkey org-mode-map "\C-i" 'org-cycle)
17893 (org-defkey org-mode-map [(tab)] 'org-cycle)
17894 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
17895 (org-defkey org-mode-map "\M-\t" 'pcomplete)
17896 ;; The following line is necessary under Suse GNU/Linux
17897 (unless (featurep 'xemacs)
17898 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
17899 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
17900 (define-key org-mode-map [backtab] 'org-shifttab)
17902 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
17903 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
17904 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
17906 ;; Cursor keys with modifiers
17907 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
17908 (org-defkey org-mode-map [(meta right)] 'org-metaright)
17909 (org-defkey org-mode-map [(meta up)] 'org-metaup)
17910 (org-defkey org-mode-map [(meta down)] 'org-metadown)
17912 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
17913 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
17914 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
17915 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
17917 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
17918 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
17919 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
17920 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
17922 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
17923 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
17924 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
17925 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
17928 (define-key org-mode-map org-babel-key-prefix org-babel-map)
17929 (mapc (lambda (pair)
17930 (define-key org-babel-map (car pair) (cdr pair)))
17931 org-babel-key-bindings)
17933 ;;; Extra keys for tty access.
17934 ;; We only set them when really needed because otherwise the
17935 ;; menus don't show the simple keys
17937 (when (or org-use-extra-keys
17938 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
17939 (not window-system))
17940 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
17941 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
17942 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
17943 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
17944 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
17945 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
17946 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
17947 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
17948 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
17949 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
17950 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
17951 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
17952 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
17953 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
17954 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
17955 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
17956 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
17957 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
17958 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
17959 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
17960 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
17961 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
17962 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
17963 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
17964 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
17965 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
17966 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
17967 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
17969 ;; All the other keys
17971 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
17972 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
17973 (if (boundp 'narrow-map)
17974 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
17975 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
17976 (if (boundp 'narrow-map)
17977 (org-defkey narrow-map "b" 'org-narrow-to-block)
17978 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
17979 (if (boundp 'narrow-map)
17980 (org-defkey narrow-map "e" 'org-narrow-to-element)
17981 (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
17982 (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
17983 (org-defkey org-mode-map "\M-}" 'org-forward-element)
17984 (org-defkey org-mode-map "\M-{" 'org-backward-element)
17985 (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
17986 (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
17987 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
17988 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
17989 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
17990 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
17991 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
17992 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
17993 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
17994 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
17995 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
17996 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
17997 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
17998 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
17999 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
18000 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
18001 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
18002 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
18003 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
18004 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
18005 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
18006 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
18007 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
18008 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
18009 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
18010 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
18011 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
18012 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
18013 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
18014 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
18015 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
18016 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
18017 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
18018 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
18019 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
18020 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
18021 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
18022 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
18023 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
18024 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
18025 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
18026 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
18027 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
18028 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
18029 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
18030 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
18031 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
18032 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
18033 (org-defkey org-mode-map "\C-c^" 'org-sort)
18034 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
18035 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
18036 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
18037 (org-defkey org-mode-map "\C-m" 'org-return)
18038 (org-defkey org-mode-map "\C-j" 'org-return-indent)
18039 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
18040 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
18041 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
18042 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
18043 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
18044 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
18045 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
18046 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
18047 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
18048 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
18049 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
18050 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
18051 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
18052 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
18053 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
18054 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
18055 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
18056 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
18057 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
18058 (org-defkey org-mode-map "\M-h" 'org-mark-element)
18059 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
18060 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
18062 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
18063 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
18064 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
18066 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
18067 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
18068 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
18069 (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
18070 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
18071 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
18072 (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
18073 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
18074 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
18075 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
18076 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
18077 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
18078 (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
18079 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
18080 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
18081 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
18082 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
18083 (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
18084 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
18085 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
18086 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
18087 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
18089 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
18090 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
18091 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
18092 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
18093 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
18095 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
18097 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
18099 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
18100 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
18102 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
18105 (when (featurep 'xemacs)
18106 (org-defkey org-mode-map 'button3 'popup-mode-menu))
18109 (defconst org-speed-commands-default
18111 ("Outline Navigation")
18112 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
18113 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
18114 ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
18115 ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
18116 ("u" . (org-speed-move-safe 'outline-up-heading))
18118 ("g" . (org-refile t))
18119 ("Outline Visibility")
18121 ("C" . org-shifttab)
18122 (" " . org-display-outline-path)
18123 ("=" . org-columns)
18124 ("Outline Structure Editing")
18125 ("U" . org-shiftmetaup)
18126 ("D" . org-shiftmetadown)
18127 ("r" . org-metaright)
18128 ("l" . org-metaleft)
18129 ("R" . org-shiftmetaright)
18130 ("L" . org-shiftmetaleft)
18131 ("i" . (progn (forward-char 1) (call-interactively
18132 'org-insert-heading-respect-content)))
18135 ("a" . org-archive-subtree-default-with-confirmation)
18136 ("." . org-mark-subtree)
18137 ("#" . org-toggle-comment)
18139 ("I" . org-clock-in)
18140 ("O" . org-clock-out)
18141 ("Meta Data Editing")
18143 ("," . (org-priority))
18144 ("0" . (org-priority ?\ ))
18145 ("1" . (org-priority ?A))
18146 ("2" . (org-priority ?B))
18147 ("3" . (org-priority ?C))
18148 (":" . org-set-tags-command)
18149 ("e" . org-set-effort)
18150 ("E" . org-inc-effort)
18151 ("W" . (lambda(m) (interactive "sMinutes before warning: ")
18152 (org-entry-put (point) "APPT_WARNTIME" m)))
18153 ("Agenda Views etc")
18155 ("/" . org-sparse-tree)
18157 ("o" . org-open-at-point)
18158 ("?" . org-speed-command-help)
18159 ("<" . (org-agenda-set-restriction-lock 'subtree))
18160 (">" . (org-agenda-remove-restriction-lock))
18162 "The default speed commands.")
18164 (defun org-print-speed-command (e)
18165 (if (> (length (car e)) 1)
18170 (princ (make-string (length (car e)) ?-))
18174 (if (symbolp (cdr e))
18175 (princ (symbol-name (cdr e)))
18179 (defun org-speed-command-help ()
18180 "Show the available speed commands."
18182 (if (not org-use-speed-commands)
18183 (error "Speed commands are not activated, customize `org-use-speed-commands'")
18184 (with-output-to-temp-buffer "*Help*"
18185 (princ "User-defined Speed commands\n===========================\n")
18186 (mapc 'org-print-speed-command org-speed-commands-user)
18188 (princ "Built-in Speed commands\n=======================\n")
18189 (mapc 'org-print-speed-command org-speed-commands-default))
18190 (with-current-buffer "*Help*"
18191 (setq truncate-lines t))))
18193 (defun org-speed-move-safe (cmd)
18194 "Execute CMD, but make sure that the cursor always ends up in a headline.
18195 If not, return to the original position and throw an error."
18197 (let ((pos (point)))
18198 (call-interactively cmd)
18199 (unless (and (bolp) (org-at-heading-p))
18201 (error "Boundary reached while executing %s" cmd))))
18203 (defvar org-self-insert-command-undo-counter 0)
18205 (defvar org-table-auto-blank-field) ; defined in org-table.el
18206 (defvar org-speed-command nil)
18208 (define-obsolete-function-alias
18209 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
18211 (defun org-speed-command-activate (keys)
18212 "Hook for activating single-letter speed commands.
18213 `org-speed-commands-default' specifies a minimal command set.
18214 Use `org-speed-commands-user' for further customization."
18215 (when (or (and (bolp) (looking-at org-outline-regexp))
18216 (and (functionp org-use-speed-commands)
18217 (funcall org-use-speed-commands)))
18218 (cdr (assoc keys (append org-speed-commands-user
18219 org-speed-commands-default)))))
18221 (define-obsolete-function-alias
18222 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
18224 (defun org-babel-speed-command-activate (keys)
18225 "Hook for activating single-letter code block commands."
18226 (when (and (bolp) (looking-at org-babel-src-block-regexp))
18227 (cdr (assoc keys org-babel-key-bindings))))
18229 (defcustom org-speed-command-hook
18230 '(org-speed-command-default-hook org-babel-speed-command-hook)
18231 "Hook for activating speed commands at strategic locations.
18232 Hook functions are called in sequence until a valid handler is
18235 Each hook takes a single argument, a user-pressed command key
18236 which is also a `self-insert-command' from the global map.
18238 Within the hook, examine the cursor position and the command key
18239 and return nil or a valid handler as appropriate. Handler could
18240 be one of an interactive command, a function, or a form.
18242 Set `org-use-speed-commands' to non-nil value to enable this
18243 hook. The default setting is `org-speed-command-activate'."
18244 :group 'org-structure
18248 (defun org-self-insert-command (N)
18249 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
18250 If the cursor is in a table looking at whitespace, the whitespace is
18251 overwritten, and the table is not marked as requiring realignment."
18253 (org-check-before-invisible-edit 'insert)
18255 ((and org-use-speed-commands
18256 (setq org-speed-command
18257 (run-hook-with-args-until-success
18258 'org-speed-command-hook (this-command-keys))))
18260 ((commandp org-speed-command)
18261 (setq this-command org-speed-command)
18262 (call-interactively org-speed-command))
18263 ((functionp org-speed-command)
18264 (funcall org-speed-command))
18265 ((and org-speed-command (listp org-speed-command))
18266 (eval org-speed-command))
18267 (t (let (org-use-speed-commands)
18268 (call-interactively 'org-self-insert-command)))))
18272 ;; check if we blank the field, and if that triggers align
18273 (and (featurep 'org-table) org-table-auto-blank-field
18274 (member last-command
18275 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
18276 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
18277 ;; got extra space, this field does not determine column width
18278 (let (org-table-may-need-update) (org-table-blank-field))
18279 ;; no extra space, this field may determine column width
18280 (org-table-blank-field)))
18283 (looking-at "[^|\n]* |"))
18284 (let (org-table-may-need-update)
18285 (goto-char (1- (match-end 0)))
18286 (backward-delete-char 1)
18287 (goto-char (match-beginning 0))
18288 (self-insert-command N)))
18290 (setq org-table-may-need-update t)
18291 (self-insert-command N)
18292 (org-fix-tags-on-the-fly)
18293 (if org-self-insert-cluster-for-undo
18294 (if (not (eq last-command 'org-self-insert-command))
18295 (setq org-self-insert-command-undo-counter 1)
18296 (if (>= org-self-insert-command-undo-counter 20)
18297 (setq org-self-insert-command-undo-counter 1)
18298 (and (> org-self-insert-command-undo-counter 0)
18299 buffer-undo-list (listp buffer-undo-list)
18300 (not (cadr buffer-undo-list)) ; remove nil entry
18301 (setcdr buffer-undo-list (cddr buffer-undo-list)))
18302 (setq org-self-insert-command-undo-counter
18303 (1+ org-self-insert-command-undo-counter))))))))
18305 (defun org-check-before-invisible-edit (kind)
18306 "Check is editing if kind KIND would be dangerous with invisible text around.
18307 The detailed reaction depends on the user option `org-catch-invisible-edits'."
18308 ;; First, try to get out of here as quickly as possible, to reduce overhead
18309 (if (and org-catch-invisible-edits
18310 (or (not (boundp 'visible-mode)) (not visible-mode))
18311 (or (get-char-property (point) 'invisible)
18312 (get-char-property (max (point-min) (1- (point))) 'invisible)))
18313 ;; OK, we need to take a closer look
18314 (let* ((invisible-at-point (get-char-property (point) 'invisible))
18315 (invisible-before-point (if (bobp) nil (get-char-property
18316 (1- (point)) 'invisible)))
18317 (border-and-ok-direction
18319 ;; Check if we are acting predictably before invisible text
18320 (and invisible-at-point (not invisible-before-point)
18321 (memq kind '(insert delete-backward)))
18322 ;; Check if we are acting predictably after invisible text
18323 ;; This works not well, and I have turned it off. It seems
18324 ;; better to always show and stop after invisible text.
18325 ;; (and (not invisible-at-point) invisible-before-point
18326 ;; (memq kind '(insert delete)))
18328 (when (or (memq invisible-at-point '(outline org-hide-block t))
18329 (memq invisible-before-point '(outline org-hide-block t)))
18330 (if (eq org-catch-invisible-edits 'error)
18331 (error "Editing in invisible areas is prohibited - make visible first"))
18332 (if (and org-custom-properties-overlays
18333 (y-or-n-p "Display invisible properties in this buffer? "))
18334 (org-toggle-custom-properties-visibility)
18335 ;; Make the area visible
18337 (if invisible-before-point
18338 (goto-char (previous-single-char-property-change
18339 (point) 'invisible)))
18342 ((eq org-catch-invisible-edits 'show)
18343 ;; That's it, we do the edit after showing
18345 "Unfolding invisible region around point before editing")
18347 ((and (eq org-catch-invisible-edits 'smart)
18348 border-and-ok-direction)
18349 (message "Unfolding invisible region around point before editing"))
18351 ;; Don't do the edit, make the user repeat it in full visibility
18352 (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
18354 (defun org-fix-tags-on-the-fly ()
18355 (when (and (equal (char-after (point-at-bol)) ?*)
18356 (org-at-heading-p))
18357 (org-align-tags-here org-tags-column)))
18359 (defun org-delete-backward-char (N)
18360 "Like `delete-backward-char', insert whitespace at field end in tables.
18361 When deleting backwards, in tables this function will insert whitespace in
18362 front of the next \"|\" separator, to keep the table aligned. The table will
18363 still be marked for re-alignment if the field did fill the entire column,
18364 because, in this case the deletion might narrow the column."
18367 (org-check-before-invisible-edit 'delete-backward)
18368 (if (and (org-table-p)
18370 (string-match "|" (buffer-substring (point-at-bol) (point)))
18371 (looking-at ".*?|"))
18372 (let ((pos (point))
18373 (noalign (looking-at "[^|\n\r]* |"))
18374 (c org-table-may-need-update))
18375 (backward-delete-char N)
18376 (if (not overwrite-mode)
18378 (skip-chars-forward "^|")
18380 (goto-char (1- pos))))
18381 ;; noalign: if there were two spaces at the end, this field
18382 ;; does not determine the width of the column.
18383 (if noalign (setq org-table-may-need-update c)))
18384 (backward-delete-char N)
18385 (org-fix-tags-on-the-fly))))
18387 (defun org-delete-char (N)
18388 "Like `delete-char', but insert whitespace at field end in tables.
18389 When deleting characters, in tables this function will insert whitespace in
18390 front of the next \"|\" separator, to keep the table aligned. The table will
18391 still be marked for re-alignment if the field did fill the entire column,
18392 because, in this case the deletion might narrow the column."
18395 (org-check-before-invisible-edit 'delete)
18396 (if (and (org-table-p)
18398 (not (= (char-after) ?|))
18400 (if (looking-at ".*?|")
18401 (let ((pos (point))
18402 (noalign (looking-at "[^|\n\r]* |"))
18403 (c org-table-may-need-update))
18404 (replace-match (concat
18405 (substring (match-string 0) 1 -1)
18408 ;; noalign: if there were two spaces at the end, this field
18409 ;; does not determine the width of the column.
18410 (if noalign (setq org-table-may-need-update c)))
18413 (org-fix-tags-on-the-fly))))
18415 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
18416 (put 'org-self-insert-command 'delete-selection t)
18417 (put 'orgtbl-self-insert-command 'delete-selection t)
18418 (put 'org-delete-char 'delete-selection 'supersede)
18419 (put 'org-delete-backward-char 'delete-selection 'supersede)
18420 (put 'org-yank 'delete-selection 'yank)
18422 ;; Make `flyspell-mode' delay after some commands
18423 (put 'org-self-insert-command 'flyspell-delayed t)
18424 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
18425 (put 'org-delete-char 'flyspell-delayed t)
18426 (put 'org-delete-backward-char 'flyspell-delayed t)
18428 ;; Make pabbrev-mode expand after org-mode commands
18429 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
18430 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
18432 ;; How to do this: Measure non-white length of current string
18433 ;; If equal to column width, we should realign.
18435 (defun org-remap (map &rest commands)
18436 "In MAP, remap the functions given in COMMANDS.
18437 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
18440 (setq old (pop commands) new (pop commands))
18441 (if (fboundp 'command-remapping)
18442 (org-defkey map (vector 'remap old) new)
18443 (substitute-key-definition old new map global-map)))))
18445 (when (eq org-enable-table-editor 'optimized)
18446 ;; If the user wants maximum table support, we need to hijack
18447 ;; some standard editing functions
18448 (org-remap org-mode-map
18449 'self-insert-command 'org-self-insert-command
18450 'delete-char 'org-delete-char
18451 'delete-backward-char 'org-delete-backward-char)
18452 (org-defkey org-mode-map "|" 'org-force-self-insert))
18454 (defvar org-ctrl-c-ctrl-c-hook nil
18455 "Hook for functions attaching themselves to `C-c C-c'.
18457 This can be used to add additional functionality to the C-c C-c
18458 key which executes context-dependent commands. This hook is run
18459 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
18460 run after the last test.
18462 Each function will be called with no arguments. The function
18463 must check if the context is appropriate for it to act. If yes,
18464 it should do its thing and then return a non-nil value. If the
18465 context is wrong, just do nothing and return nil.")
18467 (defvar org-ctrl-c-ctrl-c-final-hook nil
18468 "Hook for functions attaching themselves to `C-c C-c'.
18470 This can be used to add additional functionality to the C-c C-c
18471 key which executes context-dependent commands. This hook is run
18472 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
18473 before the first test.
18475 Each function will be called with no arguments. The function
18476 must check if the context is appropriate for it to act. If yes,
18477 it should do its thing and then return a non-nil value. If the
18478 context is wrong, just do nothing and return nil.")
18480 (defvar org-tab-first-hook nil
18481 "Hook for functions to attach themselves to TAB.
18482 See `org-ctrl-c-ctrl-c-hook' for more information.
18483 This hook runs as the first action when TAB is pressed, even before
18484 `org-cycle' messes around with the `outline-regexp' to cater for
18485 inline tasks and plain list item folding.
18486 If any function in this hook returns t, any other actions that
18487 would have been caused by TAB (such as table field motion or visibility
18488 cycling) will not occur.")
18490 (defvar org-tab-after-check-for-table-hook nil
18491 "Hook for functions to attach themselves to TAB.
18492 See `org-ctrl-c-ctrl-c-hook' for more information.
18493 This hook runs after it has been established that the cursor is not in a
18494 table, but before checking if the cursor is in a headline or if global cycling
18496 If any function in this hook returns t, not other actions like visibility
18497 cycling will be done.")
18499 (defvar org-tab-after-check-for-cycling-hook nil
18500 "Hook for functions to attach themselves to TAB.
18501 See `org-ctrl-c-ctrl-c-hook' for more information.
18502 This hook runs after it has been established that not table field motion and
18503 not visibility should be done because of current context. This is probably
18504 the place where a package like yasnippets can hook in.")
18506 (defvar org-tab-before-tab-emulation-hook nil
18507 "Hook for functions to attach themselves to TAB.
18508 See `org-ctrl-c-ctrl-c-hook' for more information.
18509 This hook runs after every other options for TAB have been exhausted, but
18510 before indentation and \t insertion takes place.")
18512 (defvar org-metaleft-hook nil
18513 "Hook for functions attaching themselves to `M-left'.
18514 See `org-ctrl-c-ctrl-c-hook' for more information.")
18515 (defvar org-metaright-hook nil
18516 "Hook for functions attaching themselves to `M-right'.
18517 See `org-ctrl-c-ctrl-c-hook' for more information.")
18518 (defvar org-metaup-hook nil
18519 "Hook for functions attaching themselves to `M-up'.
18520 See `org-ctrl-c-ctrl-c-hook' for more information.")
18521 (defvar org-metadown-hook nil
18522 "Hook for functions attaching themselves to `M-down'.
18523 See `org-ctrl-c-ctrl-c-hook' for more information.")
18524 (defvar org-shiftmetaleft-hook nil
18525 "Hook for functions attaching themselves to `M-S-left'.
18526 See `org-ctrl-c-ctrl-c-hook' for more information.")
18527 (defvar org-shiftmetaright-hook nil
18528 "Hook for functions attaching themselves to `M-S-right'.
18529 See `org-ctrl-c-ctrl-c-hook' for more information.")
18530 (defvar org-shiftmetaup-hook nil
18531 "Hook for functions attaching themselves to `M-S-up'.
18532 See `org-ctrl-c-ctrl-c-hook' for more information.")
18533 (defvar org-shiftmetadown-hook nil
18534 "Hook for functions attaching themselves to `M-S-down'.
18535 See `org-ctrl-c-ctrl-c-hook' for more information.")
18536 (defvar org-metareturn-hook nil
18537 "Hook for functions attaching themselves to `M-RET'.
18538 See `org-ctrl-c-ctrl-c-hook' for more information.")
18539 (defvar org-shiftup-hook nil
18540 "Hook for functions attaching themselves to `S-up'.
18541 See `org-ctrl-c-ctrl-c-hook' for more information.")
18542 (defvar org-shiftup-final-hook nil
18543 "Hook for functions attaching themselves to `S-up'.
18544 This one runs after all other options except shift-select have been excluded.
18545 See `org-ctrl-c-ctrl-c-hook' for more information.")
18546 (defvar org-shiftdown-hook nil
18547 "Hook for functions attaching themselves to `S-down'.
18548 See `org-ctrl-c-ctrl-c-hook' for more information.")
18549 (defvar org-shiftdown-final-hook nil
18550 "Hook for functions attaching themselves to `S-down'.
18551 This one runs after all other options except shift-select have been excluded.
18552 See `org-ctrl-c-ctrl-c-hook' for more information.")
18553 (defvar org-shiftleft-hook nil
18554 "Hook for functions attaching themselves to `S-left'.
18555 See `org-ctrl-c-ctrl-c-hook' for more information.")
18556 (defvar org-shiftleft-final-hook nil
18557 "Hook for functions attaching themselves to `S-left'.
18558 This one runs after all other options except shift-select have been excluded.
18559 See `org-ctrl-c-ctrl-c-hook' for more information.")
18560 (defvar org-shiftright-hook nil
18561 "Hook for functions attaching themselves to `S-right'.
18562 See `org-ctrl-c-ctrl-c-hook' for more information.")
18563 (defvar org-shiftright-final-hook nil
18564 "Hook for functions attaching themselves to `S-right'.
18565 This one runs after all other options except shift-select have been excluded.
18566 See `org-ctrl-c-ctrl-c-hook' for more information.")
18568 (defun org-modifier-cursor-error ()
18569 "Throw an error, a modified cursor command was applied in wrong context."
18570 (error "This command is active in special context like tables, headlines or items"))
18572 (defun org-shiftselect-error ()
18573 "Throw an error because Shift-Cursor command was applied in wrong context."
18574 (if (and (boundp 'shift-select-mode) shift-select-mode)
18575 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
18576 (error "This command works only in special context like headlines or timestamps")))
18578 (defun org-call-for-shift-select (cmd)
18579 (let ((this-command-keys-shift-translated t))
18580 (call-interactively cmd)))
18582 (defun org-shifttab (&optional arg)
18583 "Global visibility cycling or move to previous table field.
18584 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
18586 See the individual commands for more information."
18589 ((org-at-table-p) (call-interactively 'org-table-previous-field))
18591 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
18592 (message "Content view to level: %d" arg)
18593 (org-content (prefix-numeric-value arg2))
18594 (setq org-cycle-global-status 'overview)))
18595 (t (call-interactively 'org-global-cycle))))
18597 (defun org-shiftmetaleft ()
18598 "Promote subtree or delete table column.
18599 Calls `org-promote-subtree', `org-outdent-item-tree', or
18600 `org-table-delete-column', depending on context. See the
18601 individual commands for more information."
18604 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
18605 ((org-at-table-p) (call-interactively 'org-table-delete-column))
18606 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
18607 ((if (not (org-region-active-p)) (org-at-item-p)
18608 (save-excursion (goto-char (region-beginning))
18610 (call-interactively 'org-outdent-item-tree))
18611 (t (org-modifier-cursor-error))))
18613 (defun org-shiftmetaright ()
18614 "Demote subtree or insert table column.
18615 Calls `org-demote-subtree', `org-indent-item-tree', or
18616 `org-table-insert-column', depending on context. See the
18617 individual commands for more information."
18620 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
18621 ((org-at-table-p) (call-interactively 'org-table-insert-column))
18622 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
18623 ((if (not (org-region-active-p)) (org-at-item-p)
18624 (save-excursion (goto-char (region-beginning))
18626 (call-interactively 'org-indent-item-tree))
18627 (t (org-modifier-cursor-error))))
18629 (defun org-shiftmetaup (&optional arg)
18630 "Move subtree up or kill table row.
18631 Calls `org-move-subtree-up' or `org-table-kill-row' or
18632 `org-move-item-up' or `org-timestamp-up', depending on context.
18633 See the individual commands for more information."
18636 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
18637 ((org-at-table-p) (call-interactively 'org-table-kill-row))
18638 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18639 ((org-at-item-p) (call-interactively 'org-move-item-up))
18640 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18641 (call-interactively 'org-timestamp-up)))
18642 (t (org-modifier-cursor-error))))
18644 (defun org-shiftmetadown (&optional arg)
18645 "Move subtree down or insert table row.
18646 Calls `org-move-subtree-down' or `org-table-insert-row' or
18647 `org-move-item-down' or `org-timestamp-up', depending on context.
18648 See the individual commands for more information."
18651 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
18652 ((org-at-table-p) (call-interactively 'org-table-insert-row))
18653 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18654 ((org-at-item-p) (call-interactively 'org-move-item-down))
18655 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18656 (call-interactively 'org-timestamp-down)))
18657 (t (org-modifier-cursor-error))))
18659 (defsubst org-hidden-tree-error ()
18661 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
18663 (defun org-metaleft (&optional arg)
18664 "Promote heading or move table column to left.
18665 Calls `org-do-promote' or `org-table-move-column', depending on context.
18666 With no specific context, calls the Emacs default `backward-word'.
18667 See the individual commands for more information."
18670 ((run-hook-with-args-until-success 'org-metaleft-hook))
18671 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
18672 ((org-with-limited-levels
18673 (or (org-at-heading-p)
18674 (and (org-region-active-p)
18676 (goto-char (region-beginning))
18677 (org-at-heading-p)))))
18678 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18679 (call-interactively 'org-do-promote))
18680 ;; At an inline task.
18681 ((org-at-heading-p)
18682 (call-interactively 'org-inlinetask-promote))
18683 ((or (org-at-item-p)
18684 (and (org-region-active-p)
18686 (goto-char (region-beginning))
18688 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18689 (call-interactively 'org-outdent-item))
18690 (t (call-interactively 'backward-word))))
18692 (defun org-metaright (&optional arg)
18693 "Demote a subtree, a list item or move table column to right.
18694 In front of a drawer or a block keyword, indent it correctly.
18695 With no specific context, calls the Emacs default `forward-word'.
18696 See the individual commands for more information."
18699 ((run-hook-with-args-until-success 'org-metaright-hook))
18700 ((org-at-table-p) (call-interactively 'org-table-move-column))
18701 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
18702 ((org-at-block-p) (call-interactively 'org-indent-block))
18703 ((org-with-limited-levels
18704 (or (org-at-heading-p)
18705 (and (org-region-active-p)
18707 (goto-char (region-beginning))
18708 (org-at-heading-p)))))
18709 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18710 (call-interactively 'org-do-demote))
18711 ;; At an inline task.
18712 ((org-at-heading-p)
18713 (call-interactively 'org-inlinetask-demote))
18714 ((or (org-at-item-p)
18715 (and (org-region-active-p)
18717 (goto-char (region-beginning))
18719 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18720 (call-interactively 'org-indent-item))
18721 (t (call-interactively 'forward-word))))
18723 (defun org-check-for-hidden (what)
18724 "Check if there are hidden headlines/items in the current visual line.
18725 WHAT can be either `headlines' or `items'. If the current line is
18726 an outline or item heading and it has a folded subtree below it,
18727 this function returns t, nil otherwise."
18729 ((eq what 'headlines) org-outline-regexp-bol)
18730 ((eq what 'items) (org-item-beginning-re))
18731 (t (error "This should not happen"))))
18735 (unless (org-region-active-p)
18736 (setq beg (point-at-bol))
18737 (beginning-of-line 2)
18738 (while (and (not (eobp)) ;; this is like `next-line'
18739 (get-char-property (1- (point)) 'invisible))
18740 (beginning-of-line 2))
18743 (goto-char (point-at-eol))
18744 (setq end (max end (point)))
18745 (while (re-search-forward re end t)
18746 (if (get-char-property (match-beginning 0) 'invisible)
18750 (autoload 'org-element-at-point "org-element")
18751 (autoload 'org-element-type "org-element")
18753 (declare-function org-element-at-point "org-element" (&optional keep-trail))
18754 (declare-function org-element-type "org-element" (element))
18755 (declare-function org-element-contents "org-element" (element))
18756 (declare-function org-element-property "org-element" (property element))
18757 (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
18758 (declare-function org-element-nested-p "org-element" (elem-a elem-b))
18759 (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
18760 (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
18761 (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
18763 (defun org-metaup (&optional arg)
18764 "Move subtree up or move table row up.
18765 Calls `org-move-subtree-up' or `org-table-move-row' or
18766 `org-move-item-up', depending on context. See the individual commands
18767 for more information."
18770 ((run-hook-with-args-until-success 'org-metaup-hook))
18771 ((org-region-active-p)
18772 (let* ((a (min (region-beginning) (region-end)))
18773 (b (1- (max (region-beginning) (region-end))))
18774 (c (save-excursion (goto-char a)
18775 (move-beginning-of-line 0)))
18776 (d (save-excursion (goto-char a)
18777 (move-end-of-line 0) (point))))
18778 (transpose-regions a b c d)
18780 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
18781 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18782 ((org-at-item-p) (call-interactively 'org-move-item-up))
18783 (t (org-drag-element-backward))))
18785 (defun org-metadown (&optional arg)
18786 "Move subtree down or move table row down.
18787 Calls `org-move-subtree-down' or `org-table-move-row' or
18788 `org-move-item-down', depending on context. See the individual
18789 commands for more information."
18792 ((run-hook-with-args-until-success 'org-metadown-hook))
18793 ((org-region-active-p)
18794 (let* ((a (min (region-beginning) (region-end)))
18795 (b (max (region-beginning) (region-end)))
18796 (c (save-excursion (goto-char b)
18797 (move-beginning-of-line 1)))
18798 (d (save-excursion (goto-char b)
18799 (move-end-of-line 1) (1+ (point)))))
18800 (transpose-regions a b c d)
18802 ((org-at-table-p) (call-interactively 'org-table-move-row))
18803 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18804 ((org-at-item-p) (call-interactively 'org-move-item-down))
18805 (t (org-drag-element-forward))))
18807 (defun org-shiftup (&optional arg)
18808 "Increase item in timestamp or increase priority of current headline.
18809 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
18810 depending on context. See the individual commands for more information."
18813 ((run-hook-with-args-until-success 'org-shiftup-hook))
18814 ((and org-support-shift-select (org-region-active-p))
18815 (org-call-for-shift-select 'previous-line))
18816 ((org-at-timestamp-p t)
18817 (call-interactively (if org-edit-timestamp-down-means-later
18818 'org-timestamp-down 'org-timestamp-up)))
18819 ((and (not (eq org-support-shift-select 'always))
18820 org-enable-priority-commands
18821 (org-at-heading-p))
18822 (call-interactively 'org-priority-up))
18823 ((and (not org-support-shift-select) (org-at-item-p))
18824 (call-interactively 'org-previous-item))
18825 ((org-clocktable-try-shift 'up arg))
18826 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
18827 (org-support-shift-select
18828 (org-call-for-shift-select 'previous-line))
18829 (t (org-shiftselect-error))))
18831 (defun org-shiftdown (&optional arg)
18832 "Decrease item in timestamp or decrease priority of current headline.
18833 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
18834 depending on context. See the individual commands for more information."
18837 ((run-hook-with-args-until-success 'org-shiftdown-hook))
18838 ((and org-support-shift-select (org-region-active-p))
18839 (org-call-for-shift-select 'next-line))
18840 ((org-at-timestamp-p t)
18841 (call-interactively (if org-edit-timestamp-down-means-later
18842 'org-timestamp-up 'org-timestamp-down)))
18843 ((and (not (eq org-support-shift-select 'always))
18844 org-enable-priority-commands
18845 (org-at-heading-p))
18846 (call-interactively 'org-priority-down))
18847 ((and (not org-support-shift-select) (org-at-item-p))
18848 (call-interactively 'org-next-item))
18849 ((org-clocktable-try-shift 'down arg))
18850 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
18851 (org-support-shift-select
18852 (org-call-for-shift-select 'next-line))
18853 (t (org-shiftselect-error))))
18855 (defun org-shiftright (&optional arg)
18856 "Cycle the thing at point or in the current line, depending on context.
18857 Depending on context, this does one of the following:
18859 - switch a timestamp at point one day into the future
18860 - on a headline, switch to the next TODO keyword.
18861 - on an item, switch entire list to the next bullet type
18862 - on a property line, switch to the next allowed value
18863 - on a clocktable definition line, move time block into the future"
18866 ((run-hook-with-args-until-success 'org-shiftright-hook))
18867 ((and org-support-shift-select (org-region-active-p))
18868 (org-call-for-shift-select 'forward-char))
18869 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
18870 ((and (not (eq org-support-shift-select 'always))
18871 (org-at-heading-p))
18872 (let ((org-inhibit-logging
18873 (not org-treat-S-cursor-todo-selection-as-state-change))
18874 (org-inhibit-blocking
18875 (not org-treat-S-cursor-todo-selection-as-state-change)))
18876 (org-call-with-arg 'org-todo 'right)))
18877 ((or (and org-support-shift-select
18878 (not (eq org-support-shift-select 'always))
18879 (org-at-item-bullet-p))
18880 (and (not org-support-shift-select) (org-at-item-p)))
18881 (org-call-with-arg 'org-cycle-list-bullet nil))
18882 ((and (not (eq org-support-shift-select 'always))
18883 (org-at-property-p))
18884 (call-interactively 'org-property-next-allowed-value))
18885 ((org-clocktable-try-shift 'right arg))
18886 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
18887 (org-support-shift-select
18888 (org-call-for-shift-select 'forward-char))
18889 (t (org-shiftselect-error))))
18891 (defun org-shiftleft (&optional arg)
18892 "Cycle the thing at point or in the current line, depending on context.
18893 Depending on context, this does one of the following:
18895 - switch a timestamp at point one day into the past
18896 - on a headline, switch to the previous TODO keyword.
18897 - on an item, switch entire list to the previous bullet type
18898 - on a property line, switch to the previous allowed value
18899 - on a clocktable definition line, move time block into the past"
18902 ((run-hook-with-args-until-success 'org-shiftleft-hook))
18903 ((and org-support-shift-select (org-region-active-p))
18904 (org-call-for-shift-select 'backward-char))
18905 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
18906 ((and (not (eq org-support-shift-select 'always))
18907 (org-at-heading-p))
18908 (let ((org-inhibit-logging
18909 (not org-treat-S-cursor-todo-selection-as-state-change))
18910 (org-inhibit-blocking
18911 (not org-treat-S-cursor-todo-selection-as-state-change)))
18912 (org-call-with-arg 'org-todo 'left)))
18913 ((or (and org-support-shift-select
18914 (not (eq org-support-shift-select 'always))
18915 (org-at-item-bullet-p))
18916 (and (not org-support-shift-select) (org-at-item-p)))
18917 (org-call-with-arg 'org-cycle-list-bullet 'previous))
18918 ((and (not (eq org-support-shift-select 'always))
18919 (org-at-property-p))
18920 (call-interactively 'org-property-previous-allowed-value))
18921 ((org-clocktable-try-shift 'left arg))
18922 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
18923 (org-support-shift-select
18924 (org-call-for-shift-select 'backward-char))
18925 (t (org-shiftselect-error))))
18927 (defun org-shiftcontrolright ()
18928 "Switch to next TODO set."
18931 ((and org-support-shift-select (org-region-active-p))
18932 (org-call-for-shift-select 'forward-word))
18933 ((and (not (eq org-support-shift-select 'always))
18934 (org-at-heading-p))
18935 (org-call-with-arg 'org-todo 'nextset))
18936 (org-support-shift-select
18937 (org-call-for-shift-select 'forward-word))
18938 (t (org-shiftselect-error))))
18940 (defun org-shiftcontrolleft ()
18941 "Switch to previous TODO set."
18944 ((and org-support-shift-select (org-region-active-p))
18945 (org-call-for-shift-select 'backward-word))
18946 ((and (not (eq org-support-shift-select 'always))
18947 (org-at-heading-p))
18948 (org-call-with-arg 'org-todo 'previousset))
18949 (org-support-shift-select
18950 (org-call-for-shift-select 'backward-word))
18951 (t (org-shiftselect-error))))
18953 (defun org-shiftcontrolup ()
18954 "Change timestamps synchronously up in CLOCK log lines."
18956 (cond ((and (not org-support-shift-select)
18957 (org-at-clock-log-p)
18958 (org-at-timestamp-p t))
18959 (org-clock-timestamps-up))
18960 (t (org-shiftselect-error))))
18962 (defun org-shiftcontroldown ()
18963 "Change timestamps synchronously down in CLOCK log lines."
18965 (cond ((and (not org-support-shift-select)
18966 (org-at-clock-log-p)
18967 (org-at-timestamp-p t))
18968 (org-clock-timestamps-down))
18969 (t (org-shiftselect-error))))
18971 (defun org-ctrl-c-ret ()
18972 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
18975 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
18976 (t (call-interactively 'org-insert-heading))))
18978 (defun org-find-visible ()
18980 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18981 (get-char-property s 'invisible)))
18983 (defun org-find-invisible ()
18985 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18986 (not (get-char-property s 'invisible))))
18989 (defun org-copy-visible (beg end)
18990 "Copy the visible parts of the region."
18995 (narrow-to-region beg end)
18996 (setq s (goto-char (point-min)))
18997 (while (not (= (point) (point-max)))
18998 (goto-char (org-find-invisible))
18999 (push (buffer-substring s (point)) snippets)
19000 (setq s (goto-char (org-find-visible))))))
19001 (kill-new (apply 'concat (nreverse snippets)))))
19003 (defun org-copy-special ()
19004 "Copy region in table or copy current subtree.
19005 Calls `org-table-copy' or `org-copy-subtree', depending on context.
19006 See the individual commands for more information."
19008 (call-interactively
19009 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
19011 (defun org-cut-special ()
19012 "Cut region in table or cut current subtree.
19013 Calls `org-table-copy' or `org-cut-subtree', depending on context.
19014 See the individual commands for more information."
19016 (call-interactively
19017 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
19019 (defun org-paste-special (arg)
19020 "Paste rectangular region into table, or past subtree relative to level.
19021 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
19022 See the individual commands for more information."
19024 (if (org-at-table-p)
19025 (org-table-paste-rectangle)
19026 (org-paste-subtree arg)))
19028 (defsubst org-in-fixed-width-region-p ()
19029 "Is point in a fixed-width region?"
19031 (eq 'fixed-width (org-element-type (org-element-at-point)))))
19033 (defun org-edit-special (&optional arg)
19034 "Call a special editor for the stuff at point.
19035 When at a table, call the formula editor with `org-table-edit-formulas'.
19036 When in a source code block, call `org-edit-src-code'.
19037 When in a fixed-width region, call `org-edit-fixed-width-region'.
19038 When in an #+include line, visit the included file.
19039 On a link, call `ffap' to visit the link at point.
19040 Otherwise, return a user error."
19042 ;; possibly prep session before editing source
19043 (when (and (org-in-src-block-p) arg)
19044 (let* ((info (org-babel-get-src-block-info))
19045 (lang (nth 0 info))
19046 (params (nth 2 info))
19047 (session (cdr (assoc :session params))))
19048 (when (and info session) ;; we are in a source-code block with a session
19050 (intern (concat "org-babel-prep-session:" lang)) session params))))
19051 (cond ;; proceed with `org-edit-special'
19053 (beginning-of-line 1)
19054 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
19055 (find-file (org-trim (match-string 1))))
19056 ((org-at-table.el-p) (org-edit-src-code))
19057 ((or (org-at-table-p)
19059 (beginning-of-line 1)
19060 (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
19061 (call-interactively 'org-table-edit-formulas))
19062 ((org-in-block-p '("src" "example" "latex" "html")) (org-edit-src-code))
19063 ((org-in-fixed-width-region-p) (org-edit-fixed-width-region))
19064 ((org-at-regexp-p org-any-link-re) (call-interactively 'ffap))
19065 (t (user-error "No special environment to edit here"))))
19067 (defvar org-table-coordinate-overlays) ; defined in org-table.el
19068 (defun org-ctrl-c-ctrl-c (&optional arg)
19069 "Set tags in headline, or update according to changed information at point.
19071 This command does many different things, depending on context:
19073 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
19074 this is what we do.
19076 - If the cursor is on a statistics cookie, update it.
19078 - If the cursor is in a headline, prompt for tags and insert them
19079 into the current line, aligned to `org-tags-column'. When called
19080 with prefix arg, realign all tags in the current buffer.
19082 - If the cursor is in one of the special #+KEYWORD lines, this
19083 triggers scanning the buffer for these lines and updating the
19086 - If the cursor is inside a table, realign the table. This command
19087 works even if the automatic table editor has been turned off.
19089 - If the cursor is on a #+TBLFM line, re-apply the formulas to
19092 - If the cursor is at a footnote reference or definition, jump to
19093 the corresponding definition or references, respectively.
19095 - If the cursor is a the beginning of a dynamic block, update it.
19097 - If the current buffer is a capture buffer, close note and file it.
19099 - If the cursor is on a <<<target>>>, update radio targets and
19100 corresponding links in this buffer.
19102 - If the cursor is on a numbered item in a plain list, renumber the
19105 - If the cursor is on a checkbox, toggle it.
19107 - If the cursor is on a code block, evaluate it. The variable
19108 `org-confirm-babel-evaluate' can be used to control prompting
19109 before code block evaluation, by default every code block
19110 evaluation requires confirmation. Code block evaluation can be
19111 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
19113 (let ((org-enable-table-editor t))
19115 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
19116 org-occur-highlights
19117 org-latex-fragment-image-overlays)
19118 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
19119 (org-remove-occur-highlights)
19120 (org-remove-latex-fragment-image-overlays)
19121 (message "Temporary highlights/overlays removed from current buffer"))
19122 ((and (local-variable-p 'org-finish-function (current-buffer))
19123 (fboundp org-finish-function))
19124 (funcall org-finish-function))
19125 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
19126 ((org-in-regexp org-ts-regexp-both)
19127 (org-timestamp-change 0 'day))
19128 ((or (looking-at org-property-start-re)
19129 (org-at-property-p))
19130 (call-interactively 'org-property-action))
19131 ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
19132 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
19133 (or (org-at-heading-p) (org-at-item-p)))
19134 (call-interactively 'org-update-statistics-cookies))
19135 ((org-at-heading-p) (call-interactively 'org-set-tags))
19136 ((org-at-table.el-p)
19137 (message "Use C-c ' to edit table.el tables"))
19139 (org-table-maybe-eval-formula)
19141 (call-interactively 'org-table-recalculate)
19142 (org-table-maybe-recalculate-line))
19143 (call-interactively 'org-table-align)
19144 (orgtbl-send-table 'maybe))
19145 ((or (org-footnote-at-reference-p)
19146 (org-footnote-at-definition-p))
19147 (call-interactively 'org-footnote-action))
19148 ((org-at-item-checkbox-p)
19149 ;; Cursor at a checkbox: repair list and update checkboxes. Send
19150 ;; list only if at top item.
19151 (let* ((cbox (match-string 1))
19152 (struct (org-list-struct))
19153 (old-struct (copy-tree struct))
19154 (parents (org-list-parents-alist struct))
19155 (orderedp (org-entry-get nil "ORDERED"))
19156 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19158 ;; Use a light version of `org-toggle-checkbox' to avoid
19159 ;; computing list structure twice.
19160 (let ((new-box (cond
19161 ((equal arg '(16)) "[-]")
19162 ((equal arg '(4)) nil)
19163 ((equal "[X]" cbox) "[ ]")
19165 (if (and firstp arg)
19166 ;; If at first item of sub-list, remove check-box from
19167 ;; every item at the same level.
19169 (lambda (pos) (org-list-set-checkbox pos struct new-box))
19170 (org-list-get-all-items
19171 (point-at-bol) struct (org-list-prevs-alist struct)))
19172 (org-list-set-checkbox (point-at-bol) struct new-box)))
19173 ;; Replicate `org-list-write-struct', while grabbing a return
19174 ;; value from `org-list-struct-fix-box'.
19175 (org-list-struct-fix-ind struct parents 2)
19176 (org-list-struct-fix-item-end struct)
19177 (let ((prevs (org-list-prevs-alist struct)))
19178 (org-list-struct-fix-bul struct prevs)
19179 (org-list-struct-fix-ind struct parents)
19181 (org-list-struct-fix-box struct parents prevs orderedp)))
19182 (if (equal struct old-struct)
19183 (user-error "Cannot toggle this checkbox (unchecked subitems?)")
19184 (org-list-struct-apply-struct struct old-struct)
19185 (org-update-checkbox-count-maybe))
19188 "Checkboxes were removed due to unchecked box at line %d"
19189 (org-current-line block-item)))
19190 (when firstp (org-list-send-list 'maybe))))
19192 ;; Cursor at an item: repair list. Do checkbox related actions
19193 ;; only if function was called with an argument. Send list only
19195 (let* ((struct (org-list-struct))
19196 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19199 (setq old-struct (copy-tree struct))
19201 ;; If at first item of sub-list, add check-box to every
19202 ;; item at the same level.
19205 (unless (org-list-get-checkbox pos struct)
19206 (org-list-set-checkbox pos struct "[ ]")))
19207 (org-list-get-all-items
19208 (point-at-bol) struct (org-list-prevs-alist struct)))
19209 (org-list-set-checkbox (point-at-bol) struct "[ ]")))
19210 (org-list-write-struct
19211 struct (org-list-parents-alist struct) old-struct)
19212 (when arg (org-update-checkbox-count-maybe))
19213 (when firstp (org-list-send-list 'maybe))))
19214 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
19216 (beginning-of-line 1)
19217 (save-excursion (org-update-dblock)))
19219 (let ((case-fold-search t))
19220 (beginning-of-line 1)
19221 (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
19223 ((or (equal (match-string 1) "TBLFM")
19224 (equal (match-string 1) "tblfm"))
19225 ;; Recalculate the table before this line
19227 (beginning-of-line 1)
19228 (skip-chars-backward " \r\n\t")
19229 (if (org-at-table-p)
19230 (org-call-with-arg 'org-table-recalculate (or arg t)))))
19232 (let ((org-inhibit-startup-visibility-stuff t)
19233 (org-startup-align-all-tables nil))
19234 (when (boundp 'org-table-coordinate-overlays)
19235 (mapc 'delete-overlay org-table-coordinate-overlays)
19236 (setq org-table-coordinate-overlays nil))
19237 (org-save-outline-visibility 'use-markers (org-mode-restart)))
19238 (message "Local setup has been refreshed"))))
19239 ((org-clock-update-time-maybe))
19241 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
19242 (error "C-c C-c can do nothing useful at this location"))))))
19244 (defun org-mode-restart ()
19245 "Restart Org-mode, to scan again for special lines.
19246 Also updates the keyword regular expressions."
19249 (message "Org-mode restarted"))
19251 (defun org-kill-note-or-show-branches ()
19252 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
19254 (if (not org-finish-function)
19257 (call-interactively 'show-branches))
19258 (let ((org-note-abort t))
19259 (funcall org-finish-function))))
19261 (defun org-return (&optional indent)
19262 "Goto next table row or insert a newline.
19263 Calls `org-table-next-row' or `newline', depending on context.
19264 See the individual commands for more information."
19268 ((or (bobp) (org-in-src-block-p))
19269 (if indent (newline-and-indent) (newline)))
19271 (org-table-justify-field-maybe)
19272 (call-interactively 'org-table-next-row))
19273 ;; when `newline-and-indent' is called within a list, make sure
19274 ;; text moved stays inside the item.
19275 ((and (org-in-item-p) indent)
19276 (if (and (org-at-item-p) (>= (point) (match-end 0)))
19278 (save-match-data (newline))
19279 (org-indent-line-to (length (match-string 0))))
19280 (let ((ind (org-get-indentation)))
19282 (if (org-looking-back org-list-end-re)
19284 (org-indent-line-to ind)))))
19285 ((and org-return-follows-link
19286 (org-at-timestamp-p t)
19287 (not (eq org-ts-what 'after)))
19288 (org-follow-timestamp-link))
19289 ((and org-return-follows-link
19290 (let ((tprop (get-text-property (point) 'face)))
19291 (or (eq tprop 'org-link)
19292 (and (listp tprop) (memq 'org-link tprop)))))
19293 (call-interactively 'org-open-at-point))
19294 ((and (org-at-heading-p)
19296 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
19300 (t (if indent (newline-and-indent) (newline))))))
19302 (defun org-return-indent ()
19303 "Goto next table row or insert a newline and indent.
19304 Calls `org-table-next-row' or `newline-and-indent', depending on
19305 context. See the individual commands for more information."
19309 (defun org-ctrl-c-star ()
19310 "Compute table, or change heading status of lines.
19311 Calls `org-table-recalculate' or `org-toggle-heading',
19312 depending on context."
19316 (call-interactively 'org-table-recalculate))
19318 ;; Convert all lines in region to list items
19319 (call-interactively 'org-toggle-heading))))
19321 (defun org-ctrl-c-minus ()
19322 "Insert separator line in table or modify bullet status of line.
19323 Also turns a plain line or a region of lines into list items.
19324 Calls `org-table-insert-hline', `org-toggle-item', or
19325 `org-cycle-list-bullet', depending on context."
19329 (call-interactively 'org-table-insert-hline))
19330 ((org-region-active-p)
19331 (call-interactively 'org-toggle-item))
19333 (call-interactively 'org-cycle-list-bullet))
19335 (call-interactively 'org-toggle-item))))
19337 (defun org-toggle-item (arg)
19338 "Convert headings or normal lines to items, items to normal lines.
19339 If there is no active region, only the current line is considered.
19341 If the first non blank line in the region is an headline, convert
19342 all headlines to items, shifting text accordingly.
19344 If it is an item, convert all items to normal lines.
19346 If it is normal text, change region into an item. With a prefix
19347 argument ARG, change each line in region into an item."
19351 ;; Shift text in current section to IND, from point to END.
19352 ;; The function leaves point to END line.
19354 (let ((min-i 1000) (end (copy-marker end)))
19355 ;; First determine the minimum indentation (MIN-I) of
19359 (while (< (point) end)
19360 (let ((i (org-get-indentation)))
19362 ;; Skip blank lines and inline tasks.
19363 ((looking-at "^[ \t]*$"))
19364 ((looking-at org-outline-regexp-bol))
19365 ;; We can't find less than 0 indentation.
19366 ((zerop i) (throw 'exit (setq min-i 0)))
19367 ((< i min-i) (setq min-i i))))
19369 ;; Then indent each line so that a line indented to
19370 ;; MIN-I becomes indented to IND. Ignore blank lines
19371 ;; and inline tasks in the process.
19372 (let ((delta (- ind min-i)))
19373 (while (< (point) end)
19374 (unless (or (looking-at "^[ \t]*$")
19375 (looking-at org-outline-regexp-bol))
19376 (org-indent-line-to (+ (org-get-indentation) delta)))
19377 (forward-line)))))))
19380 ;; Return beginning of first non-blank line, starting from
19385 (skip-chars-forward " \r\t\n")
19388 ;; Determine boundaries of changes.
19389 (if (org-region-active-p)
19390 (setq beg (funcall skip-blanks (region-beginning))
19391 end (copy-marker (region-end)))
19392 (setq beg (funcall skip-blanks (point-at-bol))
19393 end (copy-marker (point-at-eol))))
19394 ;; Depending on the starting line, choose an action on the text
19395 ;; between BEG and END.
19396 (org-with-limited-levels
19400 ;; Case 1. Start at an item: de-itemize. Note that it only
19401 ;; happens when a region is active: `org-ctrl-c-minus'
19402 ;; would call `org-cycle-list-bullet' otherwise.
19404 (while (< (point) end)
19405 (when (org-at-item-p)
19406 (skip-chars-forward " \t")
19407 (delete-region (point) (match-end 0)))
19409 ;; Case 2. Start at an heading: convert to items.
19410 ((org-at-heading-p)
19411 (let* ((bul (org-list-bullet-string "-"))
19412 (bul-len (length bul))
19413 ;; Indentation of the first heading. It should be
19414 ;; relative to the indentation of its parent, if any.
19415 (start-ind (save-excursion
19417 ((not org-adapt-indentation) 0)
19418 ((not (outline-previous-heading)) 0)
19419 (t (length (match-string 0))))))
19420 ;; Level of first heading. Further headings will be
19421 ;; compared to it to determine hierarchy in the list.
19422 (ref-level (org-reduced-level (org-outline-level))))
19423 (while (< (point) end)
19424 (let* ((level (org-reduced-level (org-outline-level)))
19425 (delta (max 0 (- level ref-level))))
19426 ;; If current headline is less indented than the first
19427 ;; one, set it as reference, in order to preserve
19429 (when (< level ref-level) (setq ref-level level))
19430 (replace-match bul t t)
19431 (org-indent-line-to (+ start-ind (* delta bul-len)))
19432 ;; Ensure all text down to END (or SECTION-END) belongs
19433 ;; to the newly created item.
19434 (let ((section-end (save-excursion
19435 (or (outline-next-heading) (point)))))
19437 (funcall shift-text
19438 (+ start-ind (* (1+ delta) bul-len))
19439 (min end section-end)))))))
19440 ;; Case 3. Normal line with ARG: turn each non-item line into
19443 (while (< (point) end)
19444 (unless (or (org-at-heading-p) (org-at-item-p))
19445 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
19447 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
19449 ;; Case 4. Normal line without ARG: make the first line of
19450 ;; region an item, and shift indentation of others
19451 ;; lines to set them as item's body.
19452 (t (let* ((bul (org-list-bullet-string "-"))
19453 (bul-len (length bul))
19454 (ref-ind (org-get-indentation)))
19455 (skip-chars-forward " \t")
19458 (while (< (point) end)
19459 ;; Ensure that lines less indented than first one
19460 ;; still get included in item body.
19461 (funcall shift-text
19462 (+ ref-ind bul-len)
19463 (min end (save-excursion (or (outline-next-heading)
19465 (forward-line)))))))))
19467 (defun org-toggle-heading (&optional nstars)
19468 "Convert headings to normal text, or items or text to headings.
19469 If there is no active region, only the current line is considered.
19471 With a \\[universal-argument] prefix, convert the whole list at
19472 point into heading.
19476 - If the first non blank line is an headline, remove the stars
19477 from all headlines in the region.
19479 - If it is a normal line turn each and every normal line (i.e. not an
19480 heading or an item) in the region into a heading.
19482 - If it is a plain list item, turn all plain list items into headings.
19484 When converting a line into a heading, the number of stars is chosen
19485 such that the lines become children of the current entry. However,
19486 when a prefix argument is given, its value determines the number of
19491 ;; Return beginning of first non-blank line, starting from
19496 (while (org-at-comment-p) (forward-line))
19497 (skip-chars-forward " \r\t\n")
19500 ;; Determine boundaries of changes. If a universal prefix has
19501 ;; been given, put the list in a region. If region ends at a bol,
19502 ;; do not consider the last line to be in the region.
19504 (when (and current-prefix-arg (org-at-item-p))
19505 (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
19506 (org-mark-element))
19508 (if (org-region-active-p)
19509 (setq beg (funcall skip-blanks (region-beginning))
19510 end (copy-marker (save-excursion
19511 (goto-char (region-end))
19512 (if (bolp) (point) (point-at-eol)))))
19513 (setq beg (funcall skip-blanks (point-at-bol))
19514 end (copy-marker (point-at-eol))))
19515 ;; Ensure inline tasks don't count as headings.
19516 (org-with-limited-levels
19520 ;; Case 1. Started at an heading: de-star headings.
19521 ((org-at-heading-p)
19522 (while (< (point) end)
19523 (when (org-at-heading-p t)
19524 (looking-at org-outline-regexp) (replace-match "")
19527 ;; Case 2. Started at an item: change items into headlines.
19528 ;; One star will be added by `org-list-to-subtree'.
19530 (let* ((stars (make-string
19532 ;; subtract the star that will be added again by
19533 ;; `org-list-to-subtree'
19534 (1- (prefix-numeric-value current-prefix-arg))
19535 (or (org-current-level) 0))
19538 (cond (nstars "") ; stars from prefix only
19539 ((equal stars "") "") ; before first heading
19540 (org-odd-levels-only "*") ; inside heading, odd
19541 (t "")))) ; inside heading, oddeven
19542 (while (< (point) end)
19543 (when (org-at-item-p)
19544 ;; Pay attention to cases when region ends before list.
19545 (let* ((struct (org-list-struct))
19546 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
19548 (narrow-to-region (point) list-end)
19550 (org-list-to-subtree
19551 (org-list-parse-list t)
19552 '(:istart (concat stars add-stars (funcall get-stars depth))
19553 :icount (concat stars add-stars (funcall get-stars depth)))))))
19556 ;; Case 3. Started at normal text: make every line an heading,
19557 ;; skipping headlines and items.
19558 (t (let* ((stars (make-string
19560 (prefix-numeric-value current-prefix-arg)
19561 (or (org-current-level) 0))
19564 (cond (nstars "") ; stars from prefix only
19565 ((equal stars "") "*") ; before first heading
19566 (org-odd-levels-only "**") ; inside heading, odd
19567 (t "*"))) ; inside heading, oddeven
19568 (rpl (concat stars add-stars " ")))
19569 (while (< (point) end)
19570 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
19571 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
19572 (replace-match (concat rpl (match-string 2))) (setq toggled t))
19573 (forward-line)))))))
19574 (unless toggled (message "Cannot toggle heading from here"))))
19576 (defun org-meta-return (&optional arg)
19577 "Insert a new heading or wrap a region in a table.
19578 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
19579 See the individual commands for more information."
19582 ((run-hook-with-args-until-success 'org-metareturn-hook))
19583 ((or (org-at-drawer-p) (org-at-property-p))
19584 (newline-and-indent))
19586 (call-interactively 'org-table-wrap-region))
19587 (t (call-interactively 'org-insert-heading))))
19591 (defsubst org-in-subtree-not-table-p ()
19592 "Are we in a subtree and not in a table?"
19593 (and (not (org-before-first-heading-p))
19594 (not (org-at-table-p))))
19596 ;; Define the Org-mode menus
19597 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
19599 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
19600 ["Next Field" org-cycle (org-at-table-p)]
19601 ["Previous Field" org-shifttab (org-at-table-p)]
19602 ["Next Row" org-return (org-at-table-p)]
19604 ["Blank Field" org-table-blank-field (org-at-table-p)]
19605 ["Edit Field" org-table-edit-field (org-at-table-p)]
19606 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
19609 ["Move Column Left" org-metaleft (org-at-table-p)]
19610 ["Move Column Right" org-metaright (org-at-table-p)]
19611 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
19612 ["Insert Column" org-shiftmetaright (org-at-table-p)])
19614 ["Move Row Up" org-metaup (org-at-table-p)]
19615 ["Move Row Down" org-metadown (org-at-table-p)]
19616 ["Delete Row" org-shiftmetaup (org-at-table-p)]
19617 ["Insert Row" org-shiftmetadown (org-at-table-p)]
19618 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
19620 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
19622 ["Copy Rectangle" org-copy-special (org-at-table-p)]
19623 ["Cut Rectangle" org-cut-special (org-at-table-p)]
19624 ["Paste Rectangle" org-paste-special (org-at-table-p)]
19625 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
19628 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
19629 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
19630 ["Edit Formulas" org-edit-special (org-at-table-p)]
19632 ["Recalculate line" org-table-recalculate (org-at-table-p)]
19633 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
19634 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
19636 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
19638 ["Sum Column/Rectangle" org-table-sum
19639 (or (org-at-table-p) (org-region-active-p))]
19640 ["Which Column?" org-table-current-column (org-at-table-p)])
19642 org-table-toggle-formula-debugger
19643 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
19644 ["Show Col/Row Numbers"
19645 org-table-toggle-coordinate-overlays
19647 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
19649 ["Create" org-table-create (and (not (org-at-table-p))
19650 org-enable-table-editor)]
19651 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
19652 ["Import from File" org-table-import (not (org-at-table-p))]
19653 ["Export to File" org-table-export (org-at-table-p)]
19655 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
19657 (easy-menu-define org-org-menu org-mode-map "Org menu"
19660 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
19661 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
19662 ["Sparse Tree..." org-sparse-tree t]
19663 ["Reveal Context" org-reveal t]
19664 ["Show All" show-all t]
19666 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
19668 ["New Heading" org-insert-heading t]
19669 ("Navigate Headings"
19670 ["Up" outline-up-heading t]
19671 ["Next" outline-next-visible-heading t]
19672 ["Previous" outline-previous-visible-heading t]
19673 ["Next Same Level" outline-forward-same-level t]
19674 ["Previous Same Level" outline-backward-same-level t]
19676 ["Jump" org-goto t])
19678 ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
19680 ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
19681 ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
19683 ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
19684 ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
19685 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
19687 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
19689 ["Copy visible text" org-copy-visible t]
19691 ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
19692 ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
19693 ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
19694 ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
19696 ["Sort Region/Children" org-sort t]
19698 ["Convert to odd levels" org-convert-to-odd-levels t]
19699 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
19701 ["Emphasis..." org-emphasize t]
19702 ["Edit Source Example" org-edit-special t]
19704 ["Footnote new/jump" org-footnote-action t]
19705 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
19707 ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
19709 ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
19710 ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
19711 ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
19715 ["Store Link (Global)" org-store-link t]
19716 ["Find existing link to here" org-occur-link-in-agenda-files t]
19717 ["Insert Link" org-insert-link t]
19718 ["Follow Link" org-open-at-point t]
19720 ["Next link" org-next-link t]
19721 ["Previous link" org-previous-link t]
19723 ["Descriptive Links"
19724 org-toggle-link-display
19726 :selected org-descriptive-links
19729 org-toggle-link-display
19731 :selected (not org-descriptive-links)])
19734 ["TODO/DONE/-" org-todo t]
19736 ["Next keyword" org-shiftright (org-at-heading-p)]
19737 ["Previous keyword" org-shiftleft (org-at-heading-p)]
19738 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
19739 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
19740 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
19741 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
19742 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
19744 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
19745 :selected org-enforce-todo-dependencies :style toggle :active t]
19746 "Settings for tree at point"
19747 ["Do Children sequentially" org-toggle-ordered-property :style radio
19748 :selected (org-entry-get nil "ORDERED")
19749 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19750 ["Do Children parallel" org-toggle-ordered-property :style radio
19751 :selected (not (org-entry-get nil "ORDERED"))
19752 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19754 ["Set Priority" org-priority t]
19755 ["Priority Up" org-shiftup t]
19756 ["Priority Down" org-shiftdown t]
19758 ["Get news from all feeds" org-feed-update-all t]
19759 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
19760 ["Customize feeds" (customize-variable 'org-feed-alist) t])
19761 ("TAGS and Properties"
19762 ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
19763 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
19765 ["Set property" org-set-property (not (org-before-first-heading-p))]
19766 ["Column view of properties" org-columns t]
19767 ["Insert Column View DBlock" org-insert-columns-dblock t])
19768 ("Dates and Scheduling"
19769 ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
19770 ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
19772 ["1 Day Later" org-shiftright (org-at-timestamp-p)]
19773 ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
19774 ["1 ... Later" org-shiftup (org-at-timestamp-p)]
19775 ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
19776 ["Compute Time Range" org-evaluate-time-range t]
19777 ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
19778 ["Deadline" org-deadline (not (org-before-first-heading-p))]
19780 ["Custom time format" org-toggle-time-stamp-overlays
19781 :style radio :selected org-display-custom-times]
19783 ["Goto Calendar" org-goto-calendar t]
19784 ["Date from Calendar" org-date-from-calendar t]
19786 ["Start/Restart Timer" org-timer-start t]
19787 ["Pause/Continue Timer" org-timer-pause-or-continue t]
19788 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
19789 ["Insert Timer String" org-timer t]
19790 ["Insert Timer Item" org-timer-item t])
19792 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
19793 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
19794 ["Clock out" org-clock-out t]
19795 ["Clock cancel" org-clock-cancel t]
19797 ["Mark as default task" org-clock-mark-default-task t]
19798 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
19799 ["Goto running clock" org-clock-goto t]
19801 ["Display times" org-clock-display t]
19802 ["Create clock table" org-clock-report t]
19804 ["Record DONE time"
19805 (progn (setq org-log-done (not org-log-done))
19806 (message "Switching to %s will %s record a timestamp"
19807 (car org-done-keywords)
19808 (if org-log-done "automatically" "not")))
19809 :style toggle :selected org-log-done])
19811 ["Agenda Command..." org-agenda t]
19812 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
19813 ("File List for Agenda")
19814 ("Special views current file"
19815 ["TODO Tree" org-show-todo-tree t]
19816 ["Check Deadlines" org-check-deadlines t]
19817 ["Timeline" org-timeline t]
19818 ["Tags/Property tree" org-match-sparse-tree t])
19820 ["Export/Publish..." org-export t]
19822 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
19823 :selected org-cdlatex-mode]
19824 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
19825 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
19826 ["Modify math symbol" org-cdlatex-math-modify
19827 (org-inside-LaTeX-fragment-p)]
19828 ["Insert citation" org-reftex-citation t]
19830 ["Template for BEAMER" (progn (require 'org-beamer)
19831 (org-insert-beamer-options-template)) t])
19834 ["Push Files and Views" org-mobile-push t]
19835 ["Get Captured and Flagged" org-mobile-pull t]
19836 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
19838 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
19841 ["Show Version" org-version t]
19842 ["Info Documentation" org-info t])
19844 ["Browse Org Group" org-customize t]
19846 ["Expand This Menu" org-create-customize-menu
19847 (fboundp 'customize-menu-create)])
19848 ["Send bug report" org-submit-bug-report t]
19851 ["Refresh setup current buffer" org-mode-restart t]
19852 ["Reload Org (after update)" org-reload t]
19853 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
19856 (defun org-info (&optional node)
19857 "Read documentation for Org-mode in the info system.
19858 With optional NODE, go directly to that node."
19860 (info (format "(org)%s" (or node ""))))
19863 (defun org-submit-bug-report ()
19864 "Submit a bug report on Org-mode via mail.
19866 Don't hesitate to report any problems or inaccurate documentation.
19868 If you don't have setup sending mail from (X)Emacs, please copy the
19869 output buffer into your mail program, as it gives us important
19870 information about your Org-mode version and configuration."
19872 (require 'reporter)
19873 (org-load-modules-maybe)
19874 (org-require-autoloaded-modules)
19875 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
19876 (reporter-submit-bug-report
19877 "emacs-orgmode@gnu.org"
19878 (org-version nil 'full)
19880 (save-window-excursion
19881 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
19882 (delete-other-windows)
19884 (insert "You are about to submit a bug report to the Org-mode mailing list.
19886 We would like to add your full Org-mode and Outline configuration to the
19887 bug report. This greatly simplifies the work of the maintainer and
19888 other experts on the mailing list.
19890 HOWEVER, some variables you have customized may contain private
19891 information. The names of customers, colleagues, or friends, might
19892 appear in the form of file names, tags, todo states, or search strings.
19893 If you answer yes to the prompt, you might want to check and remove
19894 such private information before sending the email.")
19895 (add-text-properties (point-min) (point-max) '(face org-warning))
19896 (when (yes-or-no-p "Include your Org-mode configuration ")
19900 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
19901 (or (and (symbol-value v)
19902 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
19904 (get v 'custom-type) (get v 'standard-value)
19905 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
19907 (kill-buffer (get-buffer "*Warn about privacy*"))
19910 "Remember to cover the basics, that is, what you expected to happen and
19911 what in fact did happen. You don't know how to make a good report? See
19913 http://orgmode.org/manual/Feedback.html#Feedback
19915 Your bug report will be posted to the Org-mode mailing list.
19916 ------------------------------------------------------------------------")
19918 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
19919 (replace-match "\\1Bug: \\3 [\\2]")))))
19922 (defun org-install-agenda-files-menu ()
19923 (let ((bl (buffer-list)))
19926 (set-buffer (pop bl))
19927 (if (derived-mode-p 'org-mode) (setq bl nil)))
19928 (when (derived-mode-p 'org-mode)
19930 '("Org") "File List for Agenda"
19933 ["Edit File List" (org-edit-agenda-file-list) t]
19934 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
19935 ["Remove Current File from List" org-remove-file t]
19936 ["Cycle through agenda files" org-cycle-agenda-files t]
19937 ["Occur in all agenda files" org-occur-in-agenda-files t]
19939 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
19943 (defun org-require-autoloaded-modules ()
19946 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
19947 org-docbook org-exp org-html org-icalendar
19949 org-publish org-remember org-table
19950 org-timer org-xoxo)))
19953 (defun org-reload (&optional uncompiled)
19954 "Reload all org lisp files.
19955 With prefix arg UNCOMPILED, load the uncompiled versions."
19957 (require 'find-func)
19958 (let* ((file-re "^org\\(-.*\\)?\\.el")
19959 (dir-org (file-name-directory (org-find-library-dir "org")))
19960 (dir-org-contrib (ignore-errors
19961 (file-name-directory
19962 (org-find-library-dir "org-contribdir"))))
19964 (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
19965 (append (list nil "comint" "eval" "exp" "keys"
19966 "lob" "ref" "table" "tangle")
19970 (when (cdr lang) (symbol-name (car lang))))
19971 org-babel-load-languages)))))
19973 (append babel-files
19974 (and dir-org-contrib
19975 (directory-files dir-org-contrib t file-re))
19976 (directory-files dir-org t file-re)))
19977 (remove-re (concat (if (featurep 'xemacs)
19978 "org-colview" "org-colview-xemacs")
19980 (setq files (mapcar 'file-name-sans-extension files))
19981 (setq files (mapcar
19982 (lambda (x) (if (string-match remove-re x) nil x))
19984 (setq files (delq nil files))
19987 (when (featurep (intern (file-name-nondirectory f)))
19988 (if (and (not uncompiled)
19989 (file-exists-p (concat f ".elc")))
19990 (load (concat f ".elc") nil nil 'nosuffix)
19991 (load (concat f ".el") nil nil 'nosuffix))))
19993 (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
19994 (org-version nil 'full 'message))
19997 (defun org-customize ()
19998 "Call the customize function with org as argument."
20000 (org-load-modules-maybe)
20001 (org-require-autoloaded-modules)
20002 (customize-browse 'org))
20004 (defun org-create-customize-menu ()
20005 "Create a full customization menu for Org-mode, insert it into the menu."
20007 (org-load-modules-maybe)
20008 (org-require-autoloaded-modules)
20009 (if (fboundp 'customize-menu-create)
20012 '("Org") "Customize"
20013 `(["Browse Org group" org-customize t]
20015 ,(customize-menu-create 'org)
20016 ["Set" Custom-set t]
20017 ["Save" Custom-save t]
20018 ["Reset to Current" Custom-reset-current t]
20019 ["Reset to Saved" Custom-reset-saved t]
20020 ["Reset to Standard Settings" Custom-reset-standard t]))
20021 (message "\"Org\"-menu now contains full customization menu"))
20022 (error "Cannot expand menu (outdated version of cus-edit.el)")))
20024 ;;;; Miscellaneous stuff
20026 ;;; Generally useful functions
20028 (defun org-get-at-bol (property)
20029 "Get text property PROPERTY at beginning of line."
20030 (get-text-property (point-at-bol) property))
20032 (defun org-find-text-property-in-string (prop s)
20033 "Return the first non-nil value of property PROP in string S."
20034 (or (get-text-property 0 prop s)
20035 (get-text-property (or (next-single-property-change 0 prop s) 0)
20038 (defun org-display-warning (message) ;; Copied from Emacs-Muse
20039 "Display the given MESSAGE as a warning."
20040 (if (fboundp 'display-warning)
20041 (display-warning 'org message
20042 (if (featurep 'xemacs) 'warning :warning))
20043 (let ((buf (get-buffer-create "*Org warnings*")))
20044 (with-current-buffer buf
20045 (goto-char (point-max))
20046 (insert "Warning (Org): " message)
20049 (display-buffer buf)
20052 (defun org-eval (form)
20053 "Eval FORM and return result."
20054 (condition-case error
20056 (error (format "%%![Error: %s]" error))))
20058 (defun org-in-clocktable-p ()
20059 "Check if the cursor is in a clocktable."
20060 (let ((pos (point)) start)
20063 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
20064 (setq start (match-beginning 0))
20065 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
20066 (>= (match-end 0) pos)
20069 (defun org-in-commented-line ()
20070 "Is point in a line starting with `#'?"
20071 (equal (char-after (point-at-bol)) ?#))
20073 (defun org-in-indented-comment-line ()
20074 "Is point in a line starting with `#' after some white space?"
20077 (goto-char (point-at-bol))
20078 (looking-at "[ \t]*#"))))
20080 (defun org-in-verbatim-emphasis ()
20082 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
20084 (defun org-goto-marker-or-bmk (marker &optional bookmark)
20085 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
20086 (if (and marker (marker-buffer marker)
20087 (buffer-live-p (marker-buffer marker)))
20089 (org-pop-to-buffer-same-window (marker-buffer marker))
20090 (if (or (> marker (point-max)) (< marker (point-min)))
20093 (org-show-context 'org-goto))
20095 (bookmark-jump bookmark)
20096 (error "Cannot find location"))))
20098 (defun org-quote-csv-field (s)
20099 "Quote field for inclusion in CSV material."
20100 (if (string-match "[\",]" s)
20101 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
20104 (defun org-force-self-insert (N)
20105 "Needed to enforce self-insert under remapping."
20107 (self-insert-command N))
20109 (defun org-string-width (s)
20110 "Compute width of string, ignoring invisible characters.
20111 This ignores character with invisibility property `org-link', and also
20112 characters with property `org-cwidth', because these will become invisible
20113 upon the next fontification round."
20115 (when (or (eq t buffer-invisibility-spec)
20116 (assq 'org-link buffer-invisibility-spec))
20117 (while (setq b (text-property-any 0 (length s)
20118 'invisible 'org-link s))
20119 (setq s (concat (substring s 0 b)
20120 (substring s (or (next-single-property-change
20121 b 'invisible s) (length s)))))))
20122 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
20123 (setq s (concat (substring s 0 b)
20124 (substring s (or (next-single-property-change
20125 b 'org-cwidth s) (length s))))))
20126 (setq l (string-width s) b -1)
20127 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
20128 (setq l (- l (get-text-property b 'org-dwidth-n s))))
20131 (defun org-shorten-string (s maxlength)
20132 "Shorten string S so tht it is no longer than MAXLENGTH characters.
20133 If the string is shorter or has length MAXLENGTH, just return the
20134 original string. If it is longer, the functions finds a space in the
20135 string, breaks this string off at that locations and adds three dots
20136 as ellipsis. Including the ellipsis, the string will not be longer
20137 than MAXLENGTH. If finding a good breaking point in the string does
20138 not work, the string is just chopped off in the middle of a word
20140 (if (<= (length s) maxlength)
20142 (let* ((n (max (- maxlength 4) 1))
20143 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
20144 (if (string-match re s)
20145 (concat (match-string 1 s) "...")
20146 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
20148 (defun org-get-indentation (&optional line)
20149 "Get the indentation of the current line, interpreting tabs.
20150 When LINE is given, assume it represents a line and compute its indentation."
20152 (if (string-match "^ *" (org-remove-tabs line))
20155 (beginning-of-line 1)
20156 (skip-chars-forward " \t")
20157 (current-column))))
20159 (defun org-get-string-indentation (s)
20160 "What indentation has S due to SPACE and TAB at the beginning of the string?"
20161 (let ((n -1) (i 0) (w tab-width) c)
20163 (while (< (setq n (1+ n)) (length s))
20164 (setq c (aref s n))
20165 (cond ((= c ?\ ) (setq i (1+ i)))
20166 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
20167 (t (throw 'exit t)))))
20170 (defun org-remove-tabs (s &optional width)
20171 "Replace tabulators in S with spaces.
20172 Assumes that s is a single line, starting in column 0."
20173 (setq width (or width tab-width))
20174 (while (string-match "\t" s)
20175 (setq s (replace-match
20177 (- (* width (/ (+ (match-beginning 0) width) width))
20178 (match-beginning 0)) ?\ )
20182 (defun org-fix-indentation (line ind)
20183 "Fix indentation in LINE.
20184 IND is a cons cell with target and minimum indentation.
20185 If the current indentation in LINE is smaller than the minimum,
20186 leave it alone. If it is larger than ind, set it to the target."
20187 (let* ((l (org-remove-tabs line))
20188 (i (org-get-indentation l))
20189 (i1 (car ind)) (i2 (cdr ind)))
20190 (if (>= i i2) (setq l (substring line i2)))
20192 (concat (make-string i1 ?\ ) l)
20195 (defun org-remove-indentation (code &optional n)
20196 "Remove the maximum common indentation from the lines in CODE.
20197 N may optionally be the number of spaces to remove."
20200 (org-do-remove-indentation n)
20203 (defun org-do-remove-indentation (&optional n)
20204 "Remove the maximum common indentation from the buffer."
20205 (untabify (point-min) (point-max))
20206 (let ((min 10000) re)
20209 (goto-char (point-min))
20210 (while (re-search-forward "^ *[^ \n]" nil t)
20211 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
20212 (unless (or (= min 0) (= min 10000))
20213 (setq re (format "^ \\{%d\\}" min))
20214 (goto-char (point-min))
20215 (while (re-search-forward re nil t)
20220 (defun org-fill-template (template alist)
20221 "Find each %key of ALIST in TEMPLATE and replace it."
20222 (let ((case-fold-search nil)
20224 (setq alist (sort (copy-sequence alist)
20225 (lambda (a b) (< (length (car a)) (length (car b))))))
20226 (while (setq entry (pop alist))
20228 (replace-regexp-in-string
20229 (concat "%" (regexp-quote (car entry)))
20230 (or (cdr entry) "") template t t)))
20233 (defun org-base-buffer (buffer)
20234 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
20237 (or (buffer-base-buffer buffer)
20240 (defun org-trim (s)
20241 "Remove whitespace at beginning and end of string."
20242 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
20243 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
20246 (defun org-wrap (string &optional width lines)
20247 "Wrap string to either a number of lines, or a width in characters.
20248 If WIDTH is non-nil, the string is wrapped to that width, however many lines
20249 that costs. If there is a word longer than WIDTH, the text is actually
20250 wrapped to the length of that word.
20251 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
20252 many lines, whatever width that takes.
20253 The return value is a list of lines, without newlines at the end."
20254 (let* ((words (org-split-string string "[ \t\n]+"))
20255 (maxword (apply 'max (mapcar 'org-string-width words)))
20258 (org-do-wrap words (max maxword width)))
20261 (setq ll (org-do-wrap words maxword))
20262 (if (<= (length ll) lines)
20265 (while (> (length ll) lines)
20267 (setq ll (org-do-wrap words w)))
20269 (t (error "Cannot wrap this")))))
20271 (defun org-do-wrap (words width)
20272 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
20275 (setq line (pop words))
20276 (while (and words (< (+ (length line) (length (car words))) width))
20277 (setq line (concat line " " (pop words))))
20278 (setq lines (push line lines)))
20281 (defun org-split-string (string &optional separators)
20282 "Splits STRING into substrings at SEPARATORS.
20283 No empty strings are returned if there are matches at the beginning
20284 and end of string."
20285 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
20289 (while (and (string-match rexp string
20291 (= start (match-beginning 0))
20292 (< start (length string)))
20294 (< (match-beginning 0) (length string)))
20296 (or (eq (match-beginning 0) 0)
20297 (and (eq (match-beginning 0) (match-end 0))
20298 (eq (match-beginning 0) start))
20300 (cons (substring string start (match-beginning 0))
20302 (setq start (match-end 0)))
20303 (or (eq start (length string))
20305 (cons (substring string start)
20309 (defun org-quote-vert (s)
20310 "Replace \"|\" with \"\\vert\"."
20311 (while (string-match "|" s)
20312 (setq s (replace-match "\\vert" t t s)))
20315 (defun org-uuidgen-p (s)
20316 "Is S an ID created by UUIDGEN?"
20317 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
20319 (defun org-in-src-block-p (&optional inside)
20320 "Whether point is in a code source block.
20321 When INSIDE is non-nil, don't consider we are within a src block
20322 when point is at #+BEGIN_SRC or #+END_SRC."
20323 (let ((case-fold-search t) ov)
20324 (or (and (setq ov (overlays-at (point)))
20325 (memq 'org-block-background
20326 (overlay-properties (car ov))))
20330 (beginning-of-line)
20331 (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
20333 (defun org-context ()
20334 "Return a list of contexts of the current cursor position.
20335 If several contexts apply, all are returned.
20336 Each context entry is a list with a symbol naming the context, and
20337 two positions indicating start and end of the context. Possible
20340 :headline anywhere in a headline
20341 :headline-stars on the leading stars in a headline
20342 :todo-keyword on a TODO keyword (including DONE) in a headline
20343 :tags on the TAGS in a headline
20344 :priority on the priority cookie in a headline
20345 :item on the first line of a plain list item
20346 :item-bullet on the bullet/number of a plain list item
20347 :checkbox on the checkbox in a plain list item
20348 :table in an org-mode table
20349 :table-special on a special filed in a table
20350 :table-table in a table.el table
20351 :clocktable in a clocktable
20352 :src-block in a source block
20353 :link on a hyperlink
20354 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
20355 :target on a <<target>>
20356 :radio-target on a <<<radio-target>>>
20357 :latex-fragment on a LaTeX fragment
20358 :latex-preview on a LaTeX fragment with overlaid preview image
20360 This function expects the position to be visible because it uses font-lock
20361 faces as a help to recognize the following contexts: :table-special, :link,
20363 (let* ((f (get-text-property (point) 'face))
20364 (faces (if (listp f) f (list f)))
20365 (case-fold-search t)
20366 (p (point)) clist o)
20367 ;; First the large context
20369 ((org-at-heading-p t)
20370 (push (list :headline (point-at-bol) (point-at-eol)) clist)
20372 (beginning-of-line 1)
20373 (looking-at org-todo-line-tags-regexp))
20374 (push (org-point-in-group p 1 :headline-stars) clist)
20375 (push (org-point-in-group p 2 :todo-keyword) clist)
20376 (push (org-point-in-group p 4 :tags) clist))
20378 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
20379 (if (looking-at "\\[#[A-Z0-9]\\]")
20380 (push (org-point-in-group p 0 :priority) clist)))
20383 (push (org-point-in-group p 2 :item-bullet) clist)
20384 (push (list :item (point-at-bol)
20385 (save-excursion (org-end-of-item) (point)))
20387 (and (org-at-item-checkbox-p)
20388 (push (org-point-in-group p 0 :checkbox) clist)))
20391 (push (list :table (org-table-begin) (org-table-end)) clist)
20392 (if (memq 'org-formula faces)
20393 (push (list :table-special
20394 (previous-single-property-change p 'face)
20395 (next-single-property-change p 'face)) clist)))
20396 ((org-at-table-p 'any)
20397 (push (list :table-table) clist)))
20400 (let ((case-fold-search t))
20401 ;; New the "medium" contexts: clocktables, source blocks
20402 (cond ((org-in-clocktable-p)
20403 (push (list :clocktable
20404 (and (or (looking-at "#\\+BEGIN: clocktable")
20405 (search-backward "#+BEGIN: clocktable" nil t))
20406 (match-beginning 0))
20407 (and (re-search-forward "#\\+END:?" nil t)
20408 (match-end 0))) clist))
20409 ((org-in-src-block-p)
20410 (push (list :src-block
20411 (and (or (looking-at "#\\+BEGIN_SRC")
20412 (search-backward "#+BEGIN_SRC" nil t))
20413 (match-beginning 0))
20414 (and (search-forward "#+END_SRC" nil t)
20415 (match-beginning 0))) clist))))
20418 ;; Now the small context
20420 ((org-at-timestamp-p)
20421 (push (org-point-in-group p 0 :timestamp) clist))
20422 ((memq 'org-link faces)
20424 (previous-single-property-change p 'face)
20425 (next-single-property-change p 'face)) clist))
20426 ((memq 'org-special-keyword faces)
20427 (push (list :keyword
20428 (previous-single-property-change p 'face)
20429 (next-single-property-change p 'face)) clist))
20431 (push (org-point-in-group p 0 :target) clist)
20432 (goto-char (1- (match-beginning 0)))
20433 (if (looking-at org-radio-target-regexp)
20434 (push (org-point-in-group p 0 :radio-target) clist))
20436 ((setq o (car (delq nil
20439 (if (memq x org-latex-fragment-image-overlays) x))
20440 (overlays-at (point))))))
20441 (push (list :latex-fragment
20442 (overlay-start o) (overlay-end o)) clist)
20443 (push (list :latex-preview
20444 (overlay-start o) (overlay-end o)) clist))
20445 ((org-inside-LaTeX-fragment-p)
20446 ;; FIXME: positions wrong.
20447 (push (list :latex-fragment (point) (point)) clist)))
20449 (setq clist (nreverse (delq nil clist)))
20452 ;; FIXME: Compare with at-regexp-p Do we need both?
20453 (defun org-in-regexp (re &optional nlines visually)
20454 "Check if point is inside a match of regexp.
20455 Normally only the current line is checked, but you can include NLINES extra
20456 lines both before and after point into the search.
20457 If VISUALLY is set, require that the cursor is not after the match but
20458 really on, so that the block visually is on the match."
20460 (let ((pos (point))
20461 (eol (point-at-eol (+ 1 (or nlines 0))))
20462 (inc (if visually 1 0)))
20464 (beginning-of-line (- 1 (or nlines 0)))
20465 (while (re-search-forward re eol t)
20466 (if (and (<= (match-beginning 0) pos)
20467 (>= (+ inc (match-end 0)) pos))
20468 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
20470 (defun org-at-regexp-p (regexp)
20471 "Is point inside a match of REGEXP in the current line?"
20474 (let ((pos (point)) (end (point-at-eol)))
20475 (beginning-of-line 1)
20476 (while (re-search-forward regexp end t)
20477 (if (and (<= (match-beginning 0) pos)
20478 (>= (match-end 0) pos))
20482 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
20483 "Non-nil when point is between matches of START-RE and END-RE.
20485 Also return a non-nil value when point is on one of the matches.
20487 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
20488 buffer positions. Default values are the positions of headlines
20489 surrounding the point.
20491 The functions returns a cons cell whose car (resp. cdr) is the
20492 position before START-RE (resp. after END-RE)."
20494 (let ((pos (point))
20495 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
20496 (limit-down (or lim-down (save-excursion (outline-next-heading))))
20499 ;; Point is on a block when on START-RE or if START-RE can be
20500 ;; found before it...
20501 (and (or (org-at-regexp-p start-re)
20502 (re-search-backward start-re limit-up t))
20503 (setq beg (match-beginning 0))
20504 ;; ... and END-RE after it...
20505 (goto-char (match-end 0))
20506 (re-search-forward end-re limit-down t)
20507 (> (setq end (match-end 0)) pos)
20508 ;; ... without another START-RE in-between.
20509 (goto-char (match-beginning 0))
20510 (not (re-search-backward start-re (1+ beg) t))
20512 (cons beg end))))))
20514 (defun org-in-block-p (names)
20515 "Non-nil when point belongs to a block whose name belongs to NAMES.
20517 NAMES is a list of strings containing names of blocks.
20519 Return first block name matched, or nil. Beware that in case of
20520 nested blocks, the returned name may not belong to the closest
20524 (let ((case-fold-search t)
20525 (lim-up (save-excursion (outline-previous-heading)))
20526 (lim-down (save-excursion (outline-next-heading))))
20527 (mapc (lambda (name)
20528 (let ((n (regexp-quote name)))
20529 (when (org-between-regexps-p
20530 (concat "^[ \t]*#\\+begin_" n)
20531 (concat "^[ \t]*#\\+end_" n)
20537 (defun org-occur-in-agenda-files (regexp &optional nlines)
20538 "Call `multi-occur' with buffers for all agenda files."
20539 (interactive "sOrg-files matching: \np")
20540 (let* ((files (org-agenda-files))
20541 (tnames (mapcar 'file-truename files))
20542 (extra org-agenda-text-search-extra-files)
20544 (when (eq (car extra) 'agenda-archives)
20545 (setq extra (cdr extra))
20546 (setq files (org-add-archive-files files)))
20547 (while (setq f (pop extra))
20548 (unless (member (file-truename f) tnames)
20549 (add-to-list 'files f 'append)
20550 (add-to-list 'tnames (file-truename f) 'append)))
20552 (mapcar (lambda (x)
20553 (with-current-buffer
20554 (or (get-file-buffer x) (find-file-noselect x))
20560 (if (boundp 'occur-mode-find-occurrence-hook)
20562 (add-hook 'occur-mode-find-occurrence-hook
20564 (when (derived-mode-p 'org-mode)
20567 (defadvice occur-mode-goto-occurrence
20568 (after org-occur-reveal activate)
20569 (and (derived-mode-p 'org-mode) (org-reveal)))
20570 (defadvice occur-mode-goto-occurrence-other-window
20571 (after org-occur-reveal activate)
20572 (and (derived-mode-p 'org-mode) (org-reveal)))
20573 (defadvice occur-mode-display-occurrence
20574 (after org-occur-reveal activate)
20575 (when (derived-mode-p 'org-mode)
20576 (let ((pos (occur-mode-find-occurrence)))
20577 (with-current-buffer (marker-buffer pos)
20582 (defun org-occur-link-in-agenda-files ()
20583 "Create a link and search for it in the agendas.
20584 The link is not stored in `org-stored-links', it is just created
20585 for the search purpose."
20587 (let ((link (condition-case nil
20588 (org-store-link nil)
20589 (error "Unable to create a link to here"))))
20590 (org-occur-in-agenda-files (regexp-quote link))))
20592 (defun org-uniquify (list)
20593 "Remove duplicate elements from LIST."
20595 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
20598 (defun org-delete-all (elts list)
20599 "Remove all elements in ELTS from LIST."
20601 (setq list (delete (pop elts) list)))
20604 (defun org-count (cl-item cl-seq)
20605 "Count the number of occurrences of ITEM in SEQ.
20606 Taken from `count' in cl-seq.el with all keyword arguments removed."
20607 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
20608 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
20609 (while (< cl-start cl-end)
20610 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
20611 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
20612 (setq cl-start (1+ cl-start)))
20615 (defun org-remove-if (predicate seq)
20616 "Remove everything from SEQ that fulfills PREDICATE."
20620 (if (not (funcall predicate e)) (push e res)))
20623 (defun org-remove-if-not (predicate seq)
20624 "Remove everything from SEQ that does not fulfill PREDICATE."
20628 (if (funcall predicate e) (push e res)))
20631 (defun org-reduce (cl-func cl-seq &rest cl-keys)
20632 "Reduce two-argument FUNCTION across SEQ.
20633 Taken from `reduce' in cl-seq.el with all keyword arguments but
20634 \":initial-value\" removed."
20635 (let ((cl-accum (cond ((memq :initial-value cl-keys)
20636 (cadr (memq :initial-value cl-keys)))
20637 (cl-seq (pop cl-seq))
20638 (t (funcall cl-func)))))
20640 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
20643 (defun org-back-over-empty-lines ()
20644 "Move backwards over whitespace, to the beginning of the first empty line.
20645 Returns the number of empty lines passed."
20646 (let ((pos (point)))
20647 (if (cdr (assoc 'heading org-blank-before-new-entry))
20648 (skip-chars-backward " \t\n\r")
20650 (forward-line -1)))
20651 (beginning-of-line 2)
20652 (goto-char (min (point) pos))
20653 (count-lines (point) pos)))
20655 (defun org-skip-whitespace ()
20656 (skip-chars-forward " \t\n\r"))
20658 (defun org-point-in-group (point group &optional context)
20659 "Check if POINT is in match-group GROUP.
20660 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
20661 match. If the match group does not exist or point is not inside it,
20663 (and (match-beginning group)
20664 (>= point (match-beginning group))
20665 (<= point (match-end group))
20667 (list context (match-beginning group) (match-end group))
20670 (defun org-switch-to-buffer-other-window (&rest args)
20671 "Switch to buffer in a second window on the current frame.
20672 In particular, do not allow pop-up frames.
20673 Returns the newly created buffer."
20675 (apply 'switch-to-buffer-other-window args)))
20677 (defun org-combine-plists (&rest plists)
20678 "Create a single property list from all plists in PLISTS.
20679 The process starts by copying the first list, and then setting properties
20680 from the other lists. Settings in the last list are the most significant
20681 ones and overrule settings in the other lists."
20682 (let ((rtn (copy-sequence (pop plists)))
20685 (setq ls (pop plists))
20687 (setq p (pop ls) v (pop ls))
20688 (setq rtn (plist-put rtn p v))))
20691 (defun org-replace-escapes (string table)
20692 "Replace %-escapes in STRING with values in TABLE.
20693 TABLE is an association list with keys like \"%a\" and string values.
20694 The sequences in STRING may contain normal field width and padding information,
20695 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
20696 so values can contain further %-escapes if they are define later in TABLE."
20697 (let ((tbl (copy-alist table))
20698 (case-fold-search nil)
20701 (while (setq e (pop tbl))
20702 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
20703 (when (and (cdr e) (string-match re (cdr e)))
20704 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
20706 (add-text-properties 0 3 (list 'sref sref) safe)
20707 (setcdr e (replace-match safe t t (cdr e)))))
20708 (while (string-match re string)
20709 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
20711 (setq string (replace-match rpl t t string))))
20712 (while (setq pchg (next-property-change pchg string))
20713 (let ((sref (get-text-property pchg 'sref string)))
20714 (when (and sref (string-match "SREF" string pchg))
20715 (setq string (replace-match sref t t string)))))
20718 (defun org-sublist (list start end)
20719 "Return a section of LIST, from START to END.
20720 Counting starts at 1."
20721 (let (rtn (c start))
20722 (setq list (nthcdr (1- start) list))
20723 (while (and list (<= c end))
20724 (push (pop list) rtn)
20728 (defun org-find-base-buffer-visiting (file)
20729 "Like `find-buffer-visiting' but always return the base buffer and
20730 not an indirect buffer."
20731 (let ((buf (or (get-file-buffer file)
20732 (find-buffer-visiting file))))
20734 (or (buffer-base-buffer buf) buf)
20737 (defun org-image-file-name-regexp (&optional extensions)
20738 "Return regexp matching the file names of images.
20739 If EXTENSIONS is given, only match these."
20740 (if (and (not extensions) (fboundp 'image-file-name-regexp))
20741 (image-file-name-regexp)
20742 (let ((image-file-name-extensions
20744 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
20745 "xbm" "xpm" "pbm" "pgm" "ppm"))))
20747 (regexp-opt (nconc (mapcar 'upcase
20748 image-file-name-extensions)
20749 image-file-name-extensions)
20753 (defun org-file-image-p (file &optional extensions)
20754 "Return non-nil if FILE is an image."
20756 (string-match (org-image-file-name-regexp extensions) file)))
20758 (defun org-get-cursor-date ()
20759 "Return the date at cursor in as a time.
20760 This works in the calendar and in the agenda, anywhere else it just
20761 returns the current time."
20762 (let (date day defd)
20764 ((eq major-mode 'calendar-mode)
20765 (setq date (calendar-cursor-to-date)
20766 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
20767 ((eq major-mode 'org-agenda-mode)
20768 (setq day (get-text-property (point) 'day))
20770 (setq date (calendar-gregorian-from-absolute day)
20771 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
20773 (or defd (current-time))))
20775 (defun org-mark-subtree (&optional up)
20776 "Mark the current subtree.
20777 This puts point at the start of the current subtree, and mark at
20778 the end. If a numeric prefix UP is given, move up into the
20779 hierarchy of headlines by UP levels before marking the subtree."
20781 (org-with-limited-levels
20782 (cond ((org-at-heading-p) (beginning-of-line))
20783 ((org-before-first-heading-p) (error "Not in a subtree"))
20784 (t (outline-previous-visible-heading 1))))
20785 (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
20786 (if (org-called-interactively-p 'any)
20787 (call-interactively 'org-mark-element)
20788 (org-mark-element)))
20792 (defun org-indent-line ()
20793 "Indent line depending on context."
20795 (let* ((pos (point))
20796 (itemp (org-at-item-p))
20797 (case-fold-search t)
20798 (org-drawer-regexp (or org-drawer-regexp "\000"))
20799 (inline-task-p (and (featurep 'org-inlinetask)
20800 (org-inlinetask-in-task-p)))
20801 (inline-re (and inline-task-p
20802 (org-inlinetask-outline-regexp)))
20804 (if (and orgstruct-is-++ (eq pos (point)))
20805 (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
20806 (indent-according-to-mode))
20807 (beginning-of-line 1)
20810 ((looking-at org-outline-regexp) (setq column 0))
20812 ((looking-at "#\\+include:") (setq column 0))
20813 ;; Footnote definition
20814 ((looking-at org-footnote-definition-re) (setq column 0))
20815 ;; Literal examples
20816 ((looking-at "[ \t]*:\\( \\|$\\)")
20817 (setq column (org-get-indentation))) ; do nothing
20819 ((ignore-errors (goto-char (org-in-item-p)))
20820 (setq column (if itemp
20821 (org-get-indentation)
20822 (org-list-item-body-column (point))))
20825 ((and (looking-at "[ \t]*:END:")
20826 (save-excursion (re-search-backward org-drawer-regexp nil t)))
20828 (goto-char (1- (match-beginning 1)))
20829 (setq column (current-column))))
20831 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
20833 (re-search-backward
20834 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
20835 (setq column (org-get-indentation (match-string 0))))
20836 ((and (not (looking-at "[ \t]*#\\+begin_"))
20837 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
20839 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
20841 (cond ((equal (downcase (match-string 1)) "src")
20842 ;; src blocks: let `org-edit-src-exit' handle them
20843 (org-get-indentation))
20844 ((equal (downcase (match-string 1)) "example")
20845 (max (org-get-indentation)
20846 (org-get-indentation (match-string 0))))
20848 (org-get-indentation (match-string 0))))))
20849 ;; This line has nothing special, look at the previous relevant
20850 ;; line to compute indentation
20852 (beginning-of-line 0)
20853 (while (and (not (bobp))
20854 (not (looking-at org-table-line-regexp))
20855 (not (looking-at org-drawer-regexp))
20856 ;; When point started in an inline task, do not move
20857 ;; above task starting line.
20858 (not (and inline-task-p (looking-at inline-re)))
20859 ;; Skip drawers, blocks, empty lines, verbatim,
20860 ;; comments, tables, footnotes definitions, lists,
20862 (or (and (looking-at "[ \t]*:END:")
20863 (re-search-backward org-drawer-regexp nil t))
20864 (and (looking-at "[ \t]*#\\+end_")
20865 (re-search-backward "[ \t]*#\\+begin_"nil t))
20866 (looking-at "[ \t]*[\n:#|]")
20867 (looking-at org-footnote-definition-re)
20868 (and (ignore-errors (goto-char (org-in-item-p)))
20870 (org-list-get-top-point (org-list-struct))))
20871 (and (not inline-task-p)
20872 (featurep 'org-inlinetask)
20873 (org-inlinetask-in-task-p)
20874 (or (org-inlinetask-goto-beginning) t))))
20875 (beginning-of-line 0))
20877 ;; There was an heading above.
20878 ((looking-at "\\*+[ \t]+")
20879 (if (not org-adapt-indentation)
20881 (goto-char (match-end 0))
20882 (setq column (current-column))))
20883 ;; A drawer had started and is unfinished
20884 ((looking-at org-drawer-regexp)
20885 (goto-char (1- (match-beginning 1)))
20886 (setq column (current-column)))
20887 ;; Else, nothing noticeable found: get indentation and go on.
20888 (t (setq column (org-get-indentation))))))
20889 ;; Now apply indentation and move cursor accordingly
20891 (if (<= (current-column) (current-indentation))
20892 (org-indent-line-to column)
20893 (save-excursion (org-indent-line-to column)))
20894 ;; Special polishing for properties, see `org-property-format'
20895 (setq column (current-column))
20896 (beginning-of-line 1)
20898 "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
20899 (replace-match (concat (match-string 1)
20900 (format org-property-format
20901 (match-string 2) (match-string 3)))
20903 (org-move-to-column column))))
20905 (defun org-indent-drawer ()
20906 "Indent the drawer at point."
20909 (e (and (save-excursion (re-search-forward ":END:" nil t))
20914 (when (overlays-at (point))
20915 (member 'invisible (overlay-properties
20916 (car (overlays-at (point)))))))))
20917 (when folded (org-cycle))
20918 (indent-for-tab-command)
20919 (while (and (move-beginning-of-line 2) (< (point) e))
20920 (indent-for-tab-command))
20922 (when folded (org-cycle)))
20923 (message "Drawer at point indented"))
20925 (defun org-indent-block ()
20926 "Indent the block at point."
20929 (case-fold-search t)
20930 (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
20935 (when (overlays-at (point))
20936 (member 'invisible (overlay-properties
20937 (car (overlays-at (point)))))))))
20938 (when folded (org-cycle))
20939 (indent-for-tab-command)
20940 (while (and (move-beginning-of-line 2) (< (point) e))
20941 (indent-for-tab-command))
20943 (when folded (org-cycle)))
20944 (message "Block at point indented"))
20946 (defun org-indent-region (start end)
20950 (let ((line-end (org-current-line end)))
20952 (while (< (org-current-line) line-end)
20953 (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
20954 (t (call-interactively 'org-indent-line)))
20955 (move-beginning-of-line 2)))))
20960 ;; We use our own fill-paragraph and auto-fill functions.
20962 ;; `org-fill-paragraph' relies on adaptive filling and context
20963 ;; checking. Appropriate `fill-prefix' is computed with
20964 ;; `org-adaptive-fill-function'.
20966 ;; `org-auto-fill-function' takes care of auto-filling. It calls
20967 ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
20968 ;; `org-adaptive-fill-function' value. Internally,
20969 ;; `org-comment-line-break-function' breaks the line.
20971 ;; `org-setup-filling' installs filling and auto-filling related
20972 ;; variables during `org-mode' initialization.
20974 (defun org-setup-filling ()
20976 ;; Prevent auto-fill from inserting unwanted new items.
20977 (when (boundp 'fill-nobreak-predicate)
20979 'fill-nobreak-predicate
20981 (append fill-nobreak-predicate
20982 '(org-fill-paragraph-separate-nobreak-p
20983 org-fill-line-break-nobreak-p
20984 org-fill-paragraph-with-timestamp-nobreak-p)))))
20985 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
20986 (org-set-local 'auto-fill-inhibit-regexp nil)
20987 (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
20988 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
20989 (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
20991 (defvar org-element-paragraph-separate) ; org-element.el
20992 (defun org-fill-paragraph-separate-nobreak-p ()
20993 "Non-nil when a line break at point would insert a new item."
20994 (looking-at (substring org-element-paragraph-separate 1)))
20996 (defun org-fill-line-break-nobreak-p ()
20997 "Non-nil when a line break at point would create an Org line break."
20999 (skip-chars-backward "[ \t]")
21000 (skip-chars-backward "\\\\")
21001 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
21003 (defun org-fill-paragraph-with-timestamp-nobreak-p ()
21004 "Non-nil when a line break at point would insert a new item."
21005 (and (org-at-timestamp-p t)
21006 (not (looking-at org-ts-regexp-both))))
21008 (declare-function message-in-body-p "message" ())
21009 (defvar org-element--affiliated-re) ; From org-element.el
21010 (defvar orgtbl-line-start-regexp) ; From org-table.el
21011 (defun org-adaptive-fill-function ()
21012 "Compute a fill prefix for the current line.
21013 Return fill prefix, as a string, or nil if current line isn't
21014 meant to be filled."
21017 (when (derived-mode-p 'message-mode)
21019 (beginning-of-line)
21020 (cond ((or (not (message-in-body-p))
21021 (looking-at orgtbl-line-start-regexp))
21023 ((looking-at message-cite-prefix-regexp)
21024 (throw 'exit (match-string-no-properties 0)))
21025 ((looking-at org-outline-regexp)
21026 (throw 'exit (make-string (length (match-string 0)) ? ))))))
21027 (org-with-wide-buffer
21028 (let* ((p (line-beginning-position))
21029 (element (save-excursion (beginning-of-line) (org-element-at-point)))
21030 (type (org-element-type element))
21033 (goto-char (org-element-property :begin element))
21034 (while (looking-at org-element--affiliated-re) (forward-line))
21036 (unless (< p post-affiliated)
21038 (comment (looking-at "[ \t]*# ?") (match-string 0))
21039 (footnote-definition "")
21041 (make-string (org-list-item-body-column post-affiliated) ? ))
21043 ;; Fill prefix is usually the same as the current line,
21044 ;; except if the paragraph is at the beginning of an item.
21045 (let ((parent (org-element-property :parent element)))
21046 (cond ((eq (org-element-type parent) 'item)
21047 (make-string (org-list-item-body-column
21048 (org-element-property :begin parent))
21050 ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
21054 ;; Only fill contents if P is within block boundaries.
21055 (let* ((cbeg (save-excursion (goto-char post-affiliated)
21058 (cend (save-excursion
21059 (goto-char (org-element-property :end element))
21060 (skip-chars-backward " \r\t\n")
21061 (line-beginning-position))))
21062 (when (and (>= p cbeg) (< p cend))
21063 (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
21067 (declare-function message-goto-body "message" ())
21068 (defvar message-cite-prefix-regexp) ; From message.el
21069 (defvar org-element-all-objects) ; From org-element.el
21070 (defun org-fill-paragraph (&optional justify)
21071 "Fill element at point, when applicable.
21073 This function only applies to comment blocks, comments, example
21074 blocks and paragraphs. Also, as a special case, re-align table
21075 when point is at one.
21077 If JUSTIFY is non-nil (interactively, with prefix argument),
21078 justify as well. If `sentence-end-double-space' is non-nil, then
21079 period followed by one space does not end a sentence, so don't
21080 break a line there. The variable `fill-column' controls the
21083 For convenience, when point is at a plain list, an item or
21084 a footnote definition, try to fill the first paragraph within."
21086 (if (and (derived-mode-p 'message-mode)
21087 (or (not (message-in-body-p))
21088 (save-excursion (move-beginning-of-line 1)
21089 (looking-at message-cite-prefix-regexp))))
21090 ;; First ensure filling is correct in message-mode.
21091 (let ((fill-paragraph-function
21092 (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
21093 (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
21094 (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
21095 (paragraph-separate
21096 (cadadr (assoc 'paragraph-separate org-fb-vars))))
21097 (fill-paragraph nil))
21099 ;; Move to end of line in order to get the first paragraph
21100 ;; within a plain list or a footnote definition.
21102 (let ((element (org-element-at-point)))
21103 ;; First check if point is in a blank line at the beginning of
21104 ;; the buffer. In that case, ignore filling.
21105 (if (< (point) (org-element-property :begin element)) t
21106 (case (org-element-type element)
21107 ;; Use major mode filling function is src blocks.
21108 (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
21109 ;; Align Org tables, leave table.el tables as-is.
21110 (table-row (org-table-align) t)
21112 (when (eq (org-element-property :type element) 'org)
21116 ;; Paragraphs may contain `line-break' type objects.
21117 (let ((beg (max (point-min)
21118 (org-element-property :contents-begin element)))
21119 (end (min (point-max)
21120 (org-element-property :contents-end element))))
21121 ;; Do nothing if point is at an affiliated keyword.
21122 (if (< (point) beg) t
21123 (when (derived-mode-p 'message-mode)
21124 ;; In `message-mode', do not fill following
21125 ;; citation in current paragraph nor text before
21127 (let ((body-start (save-excursion (message-goto-body))))
21128 (when body-start (setq beg (max body-start beg))))
21129 (when (save-excursion
21131 (concat "^" message-cite-prefix-regexp) end t))
21132 (setq end (match-beginning 0))))
21133 ;; Fill paragraph, taking line breaks into
21134 ;; consideration. For that, slice the paragraph
21135 ;; using line breaks as separators, and fill the
21136 ;; parts in reverse order to avoid messing with
21142 (fill-region-as-paragraph pos (point) justify)
21144 ;; Find the list of ending positions for line
21145 ;; breaks in the current paragraph. Add paragraph
21146 ;; beginning to include first slice.
21151 (org-element--parse-objects
21152 beg end nil org-element-all-objects)
21154 (lambda (lb) (org-element-property :end lb)))))))
21156 ;; Contents of `comment-block' type elements should be
21157 ;; filled as plain text, but only if point is within block
21160 (let* ((case-fold-search t)
21161 (beg (save-excursion
21162 (goto-char (org-element-property :begin element))
21163 (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
21166 (end (save-excursion
21167 (goto-char (org-element-property :end element))
21168 (re-search-backward "^[ \t]*#\\+end_comment" nil t)
21169 (line-beginning-position))))
21170 (when (and (>= (point) beg) (< (point) end))
21171 (fill-region-as-paragraph
21174 (re-search-backward "^[ \t]*$" beg 'move)
21175 (line-beginning-position))
21177 (beginning-of-line)
21178 (re-search-forward "^[ \t]*$" end 'move)
21179 (line-beginning-position))
21183 (comment (fill-comment-paragraph justify))
21184 ;; Ignore every other element.
21185 (otherwise t)))))))
21187 (defun org-auto-fill-function ()
21188 "Auto-fill function."
21189 ;; Check if auto-filling is meaningful.
21190 (let ((fc (current-fill-column)))
21191 (when (and fc (> (current-column) fc))
21192 (let* ((fill-prefix (org-adaptive-fill-function))
21193 ;; Enforce empty fill prefix, if required. Otherwise, it
21194 ;; will be computed again.
21195 (adaptive-fill-mode (not (equal fill-prefix ""))))
21196 (when fill-prefix (do-auto-fill))))))
21198 (defun org-comment-line-break-function (&optional soft)
21199 "Break line at point and indent, continuing comment if within one.
21200 The inserted newline is marked hard if variable
21201 `use-hard-newlines' is true, unless optional argument SOFT is
21203 (if soft (insert-and-inherit ?\n) (newline 1))
21204 (save-excursion (forward-char -1) (delete-horizontal-space))
21205 (delete-horizontal-space)
21206 (indent-to-left-margin)
21207 (insert-before-markers-and-inherit fill-prefix))
21212 ;; Org comments syntax is quite complex. It requires the entire line
21213 ;; to be just a comment. Also, even with the right syntax at the
21214 ;; beginning of line, some some elements (i.e. verse-block or
21215 ;; example-block) don't accept comments. Usual Emacs comment commands
21216 ;; cannot cope with those requirements. Therefore, Org replaces them.
21218 ;; Org still relies on `comment-dwim', but cannot trust
21219 ;; `comment-only-p'. So, `comment-region-function' and
21220 ;; `uncomment-region-function' both point
21221 ;; to`org-comment-or-uncomment-region'. Eventually,
21222 ;; `org-insert-comment' takes care of insertion of comments at the
21223 ;; beginning of line.
21225 ;; `org-setup-comments-handling' install comments related variables
21226 ;; during `org-mode' initialization.
21228 (defun org-setup-comments-handling ()
21230 (org-set-local 'comment-use-syntax nil)
21231 (org-set-local 'comment-start "# ")
21232 (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
21233 (org-set-local 'comment-insert-comment-function 'org-insert-comment)
21234 (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
21235 (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
21237 (defun org-insert-comment ()
21238 "Insert an empty comment above current line.
21239 If the line is empty, insert comment at its beginning."
21240 (beginning-of-line)
21241 (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
21245 (defvar comment-empty-lines) ; From newcomment.el.
21246 (defun org-comment-or-uncomment-region (beg end &rest ignore)
21247 "Comment or uncomment each non-blank line in the region.
21248 Uncomment each non-blank line between BEG and END if it only
21249 contains commented lines. Otherwise, comment them."
21252 (narrow-to-region (save-excursion (goto-char beg)
21253 (skip-chars-forward " \r\t\n" end)
21254 (line-beginning-position))
21255 (save-excursion (goto-char end)
21256 (skip-chars-backward " \r\t\n" beg)
21257 (line-end-position)))
21259 ;; UNCOMMENTP is non-nil when every non blank line between
21260 ;; BEG and END is a comment.
21262 (goto-char (point-min))
21263 (while (and (not (eobp))
21264 (let ((element (org-element-at-point)))
21265 (and (eq (org-element-type element) 'comment)
21266 (goto-char (min (point-max)
21267 (org-element-property
21268 :end element)))))))
21271 ;; Only blank lines and comments in region: uncomment it.
21273 (goto-char (point-min))
21274 (while (not (eobp))
21275 (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
21276 (replace-match "" nil nil nil 1))
21278 ;; Comment each line in region.
21279 (let ((min-indent (point-max)))
21280 ;; First find the minimum indentation across all lines.
21282 (goto-char (point-min))
21283 (while (and (not (eobp)) (not (zerop min-indent)))
21284 (unless (looking-at "[ \t]*$")
21285 (setq min-indent (min min-indent (current-indentation))))
21287 ;; Then loop over all lines.
21289 (goto-char (point-min))
21290 (while (not (eobp))
21291 (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
21292 (org-move-to-column min-indent t)
21293 (insert comment-start))
21294 (forward-line))))))))
21299 (defun org-toggle-fixed-width-section (arg)
21300 "Toggle the fixed-width export.
21301 If there is no active region, the QUOTE keyword at the current headline is
21302 inserted or removed. When present, it causes the text between this headline
21303 and the next to be exported as fixed-width text, and unmodified.
21304 If there is an active region, this command adds or removes a colon as the
21305 first character of this line. If the first character of a line is a colon,
21306 this line is also exported in fixed-width font."
21309 (regionp (org-region-active-p))
21310 (beg (if regionp (region-beginning) (point)))
21311 (end (if regionp (region-end)))
21312 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
21313 (case-fold-search nil)
21314 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
21319 (setq cc (current-column))
21320 (beginning-of-line 1)
21321 (setq off (looking-at re))
21322 (while (> nlines 0)
21323 (setq nlines (1- nlines))
21324 (beginning-of-line 1)
21327 (org-move-to-column cc t)
21330 ((and off (looking-at re))
21331 (replace-match "" t t nil 1))
21332 ((not off) (org-move-to-column cc t) (insert ": ")))
21335 (org-back-to-heading)
21337 ((looking-at (format org-heading-keyword-regexp-format
21339 (goto-char (match-end 1))
21340 (looking-at (concat " +" org-quote-string))
21341 (replace-match "" t t)
21342 (when (eolp) (insert " ")))
21343 ((looking-at org-outline-regexp)
21344 (goto-char (match-end 0))
21345 (insert org-quote-string " ")))))))
21347 (defun org-reftex-citation ()
21348 "Use reftex-citation to insert a citation into the buffer.
21349 This looks for a line like
21351 #+BIBLIOGRAPHY: foo plain option:-d
21353 and derives from it that foo.bib is the bibliography file relevant
21354 for this document. It then installs the necessary environment for RefTeX
21355 to work in this buffer and calls `reftex-citation' to insert a citation
21358 Export of such citations to both LaTeX and HTML is handled by the contributed
21359 package org-exp-bibtex by Taru Karttunen."
21361 (let ((reftex-docstruct-symbol 'rds)
21362 (reftex-cite-format "\\cite{%l}")
21367 (let ((case-fold-search t)
21368 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
21369 (if (not (save-excursion
21370 (or (re-search-forward re nil t)
21371 (re-search-backward re nil t))))
21372 (error "No bibliography defined in file")
21373 (setq bib (concat (match-string 1) ".bib")
21374 rds (list (list 'bib bib)))))))
21375 (call-interactively 'reftex-citation)))
21377 ;;;; Functions extending outline functionality
21379 (defun org-beginning-of-line (&optional arg)
21380 "Go to the beginning of the current line. If that is invisible, continue
21381 to a visible line beginning. This makes the function of C-a more intuitive.
21382 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
21383 first attempt, and only move to after the tags when the cursor is already
21384 beyond the end of the headline."
21386 (let ((pos (point))
21387 (special (if (consp org-special-ctrl-a/e)
21388 (car org-special-ctrl-a/e)
21389 org-special-ctrl-a/e))
21391 (if (org-bound-and-true-p visual-line-mode)
21392 (beginning-of-visual-line 1)
21393 (beginning-of-line 1))
21394 (if (and arg (fboundp 'move-beginning-of-line))
21395 (call-interactively 'move-beginning-of-line)
21399 (if (org-truely-invisible-p)
21400 (while (and (not (bobp)) (org-truely-invisible-p))
21402 (beginning-of-line 1))
21403 (forward-char 1))))
21406 ((and (looking-at org-complex-heading-regexp)
21407 (= (char-after (match-end 1)) ?\ ))
21408 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
21412 (cond ((> pos refpos) refpos)
21413 ((= pos (point)) refpos)
21415 (cond ((> pos (point)) (point))
21416 ((not (eq last-command this-command)) (point))
21419 ;; Being at an item and not looking at an the item means point
21420 ;; was previously moved to beginning of a visual line, which
21421 ;; doesn't contain the item. Therefore, do nothing special,
21423 (when (looking-at org-list-full-item-re)
21424 ;; Set special position at first white space character after
21425 ;; bullet, and check-box, if any.
21426 (let ((after-bullet
21427 (let ((box (match-end 3)))
21428 (if (not box) (match-end 1)
21429 (let ((after (char-after box)))
21430 (if (and after (= after ? )) (1+ box) box))))))
21431 ;; Special case: Move point to special position when
21432 ;; currently after it or at beginning of line.
21434 (when (or (> pos after-bullet) (= (point) pos))
21435 (goto-char after-bullet))
21436 ;; Reversed case: Move point to special position when
21437 ;; point was already at beginning of line and command is
21439 (when (and (= (point) pos) (eq last-command this-command))
21440 (goto-char after-bullet))))))))
21442 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21444 (defun org-end-of-line (&optional arg)
21445 "Go to the end of the line.
21446 If this is a headline, and `org-special-ctrl-a/e' is set, ignore
21447 tags on the first attempt, and only move to after the tags when
21448 the cursor is already beyond the end of the headline."
21450 (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
21451 org-special-ctrl-a/e))
21452 (move-fun (cond ((org-bound-and-true-p visual-line-mode)
21453 'end-of-visual-line)
21454 ((fboundp 'move-end-of-line) 'move-end-of-line)
21455 (t 'end-of-line))))
21456 (if (or (not special) arg) (call-interactively move-fun)
21457 (let* ((element (save-excursion (beginning-of-line)
21458 (org-element-at-point)))
21459 (type (org-element-type element)))
21461 ((memq type '(headline inlinetask))
21462 (let ((pos (point)))
21463 (beginning-of-line 1)
21464 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
21466 (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
21467 (goto-char (match-beginning 1))
21468 (goto-char (match-end 0)))
21469 (if (or (< pos (match-end 0))
21470 (not (eq this-command last-command)))
21471 (goto-char (match-end 0))
21472 (goto-char (match-beginning 1))))
21473 (call-interactively move-fun))))
21474 ((org-element-property :hiddenp element)
21475 ;; If element is hidden, `move-end-of-line' would put point
21476 ;; after it. Use `end-of-line' to stay on current line.
21477 (call-interactively 'end-of-line))
21478 (t (call-interactively move-fun)))))
21479 (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21481 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
21482 (define-key org-mode-map "\C-e" 'org-end-of-line)
21484 (defun org-backward-sentence (&optional arg)
21485 "Go to beginning of sentence, or beginning of table field.
21486 This will call `backward-sentence' or `org-table-beginning-of-field',
21487 depending on context."
21490 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
21491 (t (call-interactively 'backward-sentence))))
21493 (defun org-forward-sentence (&optional arg)
21494 "Go to end of sentence, or end of table field.
21495 This will call `forward-sentence' or `org-table-end-of-field',
21496 depending on context."
21499 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
21500 (t (call-interactively 'forward-sentence))))
21502 (define-key org-mode-map "\M-a" 'org-backward-sentence)
21503 (define-key org-mode-map "\M-e" 'org-forward-sentence)
21505 (defun org-kill-line (&optional arg)
21506 "Kill line, to tags or end of line."
21509 ((or (not org-special-ctrl-k)
21511 (not (org-at-heading-p)))
21512 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
21513 org-ctrl-k-protect-subtree)
21514 (if (or (eq org-ctrl-k-protect-subtree 'error)
21515 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
21516 (error "C-k aborted - would kill hidden subtree")))
21517 (call-interactively
21518 (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
21519 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
21520 (kill-region (point) (match-beginning 1))
21521 (org-set-tags nil t))
21522 (t (kill-region (point) (point-at-eol)))))
21524 (define-key org-mode-map "\C-k" 'org-kill-line)
21526 (defun org-yank (&optional arg)
21527 "Yank. If the kill is a subtree, treat it specially.
21528 This command will look at the current kill and check if is a single
21529 subtree, or a series of subtrees[1]. If it passes the test, and if the
21530 cursor is at the beginning of a line or after the stars of a currently
21531 empty headline, then the yank is handled specially. How exactly depends
21532 on the value of the following variables, both set by default.
21534 org-yank-folded-subtrees
21535 When set, the subtree(s) will be folded after insertion, but only
21536 if doing so would now swallow text after the yanked text.
21538 org-yank-adjusted-subtrees
21539 When set, the subtree will be promoted or demoted in order to
21540 fit into the local outline tree structure, which means that the level
21541 will be adjusted so that it becomes the smaller one of the two
21542 *visible* surrounding headings.
21544 Any prefix to this command will cause `yank' to be called directly with
21545 no special treatment. In particular, a simple \\[universal-argument] prefix \
21547 plainly yank the text as it is.
21549 \[1] The test checks if the first non-white line is a heading
21550 and if there are no other headings with fewer stars."
21552 (org-yank-generic 'yank arg))
21554 (defun org-yank-generic (command arg)
21555 "Perform some yank-like command.
21557 This function implements the behavior described in the `org-yank'
21558 documentation. However, it has been generalized to work for any
21559 interactive command with similar behavior."
21561 ;; pretend to be command COMMAND
21562 (setq this-command command)
21565 (call-interactively command)
21567 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
21568 (and (org-kill-is-subtree-p)
21570 (and (looking-at "[ \t]*$")
21573 (buffer-substring (point-at-bol) (point)))))))
21576 ((and subtreep org-yank-folded-subtrees)
21577 (let ((beg (point))
21579 (if (and subtreep org-yank-adjusted-subtrees)
21580 (org-paste-subtree nil nil 'for-yank)
21581 (call-interactively command))
21585 (when (and (bolp) subtreep
21586 (not (setq swallowp
21587 (org-yank-folding-would-swallow-text beg end))))
21588 (org-with-limited-levels
21589 (or (looking-at org-outline-regexp)
21590 (re-search-forward org-outline-regexp-bol end t))
21591 (while (and (< (point) end) (looking-at org-outline-regexp))
21593 (org-cycle-show-empty-lines 'folded)
21594 (condition-case nil
21595 (outline-forward-same-level 1)
21596 (error (goto-char end))))))
21599 "Inserted text not folded because that would swallow text"))
21602 (skip-chars-forward " \t\n\r")
21603 (beginning-of-line 1)
21604 (push-mark beg 'nomsg)))
21605 ((and subtreep org-yank-adjusted-subtrees)
21606 (let ((beg (point-at-bol)))
21607 (org-paste-subtree nil nil 'for-yank)
21608 (push-mark beg 'nomsg)))
21610 (call-interactively command))))))
21612 (defun org-yank-folding-would-swallow-text (beg end)
21613 "Would hide-subtree at BEG swallow any text after END?"
21615 (org-with-limited-levels
21618 (when (or (looking-at org-outline-regexp)
21619 (re-search-forward org-outline-regexp-bol end t))
21620 (setq level (org-outline-level)))
21622 (skip-chars-forward " \t\r\n\v\f")
21624 (and (bolp) (looking-at org-outline-regexp)
21625 (<= (org-outline-level) level)))
21626 nil ; Nothing would be swallowed
21627 t))))) ; something would swallow
21629 (define-key org-mode-map "\C-y" 'org-yank)
21631 (defun org-truely-invisible-p ()
21632 "Check if point is at a character currently not visible.
21633 This version does not only check the character property, but also
21635 ;; Early versions of noutline don't have `outline-invisible-p'.
21636 (if (org-bound-and-true-p visible-mode)
21638 (outline-invisible-p)))
21640 (defun org-invisible-p2 ()
21641 "Check if point is at a character currently not visible."
21643 (if (and (eolp) (not (bobp))) (backward-char 1))
21644 ;; Early versions of noutline don't have `outline-invisible-p'.
21645 (outline-invisible-p)))
21647 (defun org-back-to-heading (&optional invisible-ok)
21648 "Call `outline-back-to-heading', but provide a better error message."
21649 (condition-case nil
21650 (outline-back-to-heading invisible-ok)
21651 (error (error "Before first headline at position %d in buffer %s"
21652 (point) (current-buffer)))))
21654 (defun org-before-first-heading-p ()
21655 "Before first heading?"
21658 (null (re-search-backward org-outline-regexp-bol nil t))))
21660 (defun org-at-heading-p (&optional ignored)
21661 (outline-on-heading-p t))
21662 ;; Compatibility alias with Org versions < 7.8.03
21663 (defalias 'org-on-heading-p 'org-at-heading-p)
21665 (defun org-at-comment-p nil
21666 "Is cursor in a line starting with a # character?"
21668 (beginning-of-line)
21669 (looking-at "^#")))
21671 (defun org-at-drawer-p nil
21672 "Is cursor at a drawer keyword?"
21674 (move-beginning-of-line 1)
21675 (looking-at org-drawer-regexp)))
21677 (defun org-at-block-p nil
21678 "Is cursor at a block keyword?"
21680 (move-beginning-of-line 1)
21681 (looking-at org-block-regexp)))
21683 (defun org-point-at-end-of-empty-headline ()
21684 "If point is at the end of an empty headline, return t, else nil.
21685 If the heading only contains a TODO keyword, it is still still considered
21687 (and (looking-at "[ \t]*$")
21688 (when org-todo-line-regexp
21690 (beginning-of-line 1)
21691 (let ((case-fold-search nil))
21692 (looking-at org-todo-line-regexp)
21693 (string= (match-string 3) ""))))))
21695 (defun org-at-heading-or-item-p ()
21696 (or (org-at-heading-p) (org-at-item-p)))
21698 (defun org-at-target-p ()
21699 (or (org-in-regexp org-radio-target-regexp)
21700 (org-in-regexp org-target-regexp)))
21701 ;; Compatibility alias with Org versions < 7.8.03
21702 (defalias 'org-on-target-p 'org-at-target-p)
21704 (defun org-up-heading-all (arg)
21705 "Move to the heading line of which the present line is a subheading.
21706 This function considers both visible and invisible heading lines.
21707 With argument, move up ARG levels."
21708 (if (fboundp 'outline-up-heading-all)
21709 (outline-up-heading-all arg) ; emacs 21 version of outline.el
21710 (outline-up-heading arg t))) ; emacs 22 version of outline.el
21712 (defun org-up-heading-safe ()
21713 "Move to the heading line of which the present line is a subheading.
21714 This version will not throw an error. It will return the level of the
21715 headline found, or nil if no higher level is found.
21717 Also, this function will be a lot faster than `outline-up-heading',
21718 because it relies on stars being the outline starters. This can really
21719 make a significant difference in outlines with very many siblings."
21720 (let (start-level re)
21721 (org-back-to-heading t)
21722 (setq start-level (funcall outline-level))
21723 (if (equal start-level 1)
21725 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
21726 (if (re-search-backward re nil t)
21727 (funcall outline-level)))))
21729 (defun org-first-sibling-p ()
21730 "Is this heading the first child of its parents?"
21732 (let ((re org-outline-regexp-bol)
21734 (unless (org-at-heading-p t)
21735 (error "Not at a heading"))
21736 (setq level (funcall outline-level))
21738 (if (not (re-search-backward re nil t))
21740 (setq l (funcall outline-level))
21743 (defun org-goto-sibling (&optional previous)
21744 "Goto the next sibling, even if it is invisible.
21745 When PREVIOUS is set, go to the previous sibling instead. Returns t
21746 when a sibling was found. When none is found, return nil and don't
21748 (let ((fun (if previous 're-search-backward 're-search-forward))
21750 (re org-outline-regexp-bol)
21752 (when (condition-case nil (org-back-to-heading t) (error nil))
21753 (setq level (funcall outline-level))
21755 (or previous (forward-char 1))
21756 (while (funcall fun re nil t)
21757 (setq l (funcall outline-level))
21758 (when (< l level) (goto-char pos) (throw 'exit nil))
21759 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
21763 (defun org-show-siblings ()
21764 "Show all siblings of the current headline."
21766 (while (org-goto-sibling) (org-flag-heading nil)))
21768 (while (org-goto-sibling 'previous)
21769 (org-flag-heading nil))))
21771 (defun org-goto-first-child ()
21772 "Goto the first child, even if it is invisible.
21773 Return t when a child was found. Otherwise don't move point and
21775 (let (level (pos (point)) (re org-outline-regexp-bol))
21776 (when (condition-case nil (org-back-to-heading t) (error nil))
21777 (setq level (outline-level))
21779 (if (and (re-search-forward re nil t) (> (outline-level) level))
21780 (progn (goto-char (match-beginning 0)) t)
21781 (goto-char pos) nil))))
21783 (defun org-show-hidden-entry ()
21784 "Show an entry where even the heading is hidden."
21788 (defun org-flag-heading (flag &optional entry)
21789 "Flag the current heading. FLAG non-nil means make invisible.
21790 When ENTRY is non-nil, show the entire entry."
21792 (org-back-to-heading t)
21793 ;; Check if we should show the entire entry
21798 (and (outline-next-heading)
21799 (org-flag-heading nil))))
21800 (outline-flag-region (max (point-min) (1- (point)))
21801 (save-excursion (outline-end-of-heading) (point))
21804 (defun org-get-next-sibling ()
21805 "Move to next heading of the same level, and return point.
21806 If there is no such heading, return nil.
21807 This is like outline-next-sibling, but invisible headings are ok."
21808 (let ((level (funcall outline-level)))
21809 (outline-next-heading)
21810 (while (and (not (eobp)) (> (funcall outline-level) level))
21811 (outline-next-heading))
21812 (if (or (eobp) (< (funcall outline-level) level))
21816 (defun org-get-last-sibling ()
21817 "Move to previous heading of the same level, and return point.
21818 If there is no such heading, return nil."
21819 (let ((opoint (point))
21820 (level (funcall outline-level)))
21821 (outline-previous-heading)
21822 (when (and (/= (point) opoint) (outline-on-heading-p t))
21823 (while (and (> (funcall outline-level) level)
21825 (outline-previous-heading))
21826 (if (< (funcall outline-level) level)
21830 (defun org-end-of-subtree (&optional invisible-ok to-heading)
21831 "Goto to the end of a subtree."
21832 ;; This contains an exact copy of the original function, but it uses
21833 ;; `org-back-to-heading', to make it work also in invisible
21834 ;; trees. And is uses an invisible-ok argument.
21835 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
21836 ;; Furthermore, when used inside Org, finding the end of a large subtree
21837 ;; with many children and grandchildren etc, this can be much faster
21838 ;; than the outline version.
21839 (org-back-to-heading invisible-ok)
21841 (level (funcall outline-level)))
21842 (if (and (derived-mode-p 'org-mode) (< level 1000))
21843 ;; A true heading (not a plain list item), in Org-mode
21844 ;; This means we can easily find the end by looking
21845 ;; only for the right number of stars. Using a regexp to do
21846 ;; this is so much faster than using a Lisp loop.
21847 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
21849 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
21850 ;; something else, do it the slow way
21851 (while (and (not (eobp))
21852 (or first (> (funcall outline-level) level)))
21854 (outline-next-heading)))
21856 (if (memq (preceding-char) '(?\n ?\^M))
21858 ;; Go to end of line before heading
21860 (if (memq (preceding-char) '(?\n ?\^M))
21861 ;; leave blank line before heading
21862 (forward-char -1))))))
21865 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
21866 "Use Org version in org-mode, for dramatic speed-up."
21867 (if (derived-mode-p 'org-mode)
21869 (org-end-of-subtree nil t)
21870 (unless (eobp) (backward-char 1)))
21873 (defun org-end-of-meta-data-and-drawers ()
21874 "Jump to the first text after meta data and drawers in the current entry.
21875 This will move over empty lines, lines with planning time stamps,
21876 clocking lines, and drawers."
21877 (org-back-to-heading t)
21878 (let ((end (save-excursion (outline-next-heading) (point)))
21879 (re (concat "\\(" org-drawer-regexp "\\)"
21880 "\\|" "[ \t]*" org-keyword-time-regexp)))
21882 (while (re-search-forward re end t)
21883 (if (not (match-end 1))
21884 ;; empty or planning line
21886 ;; a drawer, find the end
21887 (re-search-forward "^[ \t]*:END:" end 'move)
21889 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
21892 (defun org-forward-heading-same-level (arg &optional invisible-ok)
21893 "Move forward to the arg'th subheading at same level as this one.
21894 Stop at the first and last subheadings of a superior heading.
21895 Normally this only looks at visible headings, but when INVISIBLE-OK is
21896 non-nil it will also look at invisible ones."
21898 (org-back-to-heading invisible-ok)
21900 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21901 (re (format "^\\*\\{1,%d\\} " level))
21905 (while (and (re-search-forward re nil 'move)
21906 (setq l (- (match-end 0) (match-beginning 0) 1))
21909 (progn (backward-char 1) (outline-invisible-p)))
21910 (if (< l level) (setq arg 1)))
21911 (setq arg (1- arg)))
21912 (beginning-of-line 1)))
21914 (defun org-backward-heading-same-level (arg &optional invisible-ok)
21915 "Move backward to the arg'th subheading at same level as this one.
21916 Stop at the first and last subheadings of a superior heading."
21918 (org-back-to-heading)
21920 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21921 (re (format "^\\*\\{1,%d\\} " level))
21924 (while (and (re-search-backward re nil 'move)
21925 (setq l (- (match-end 0) (match-beginning 0) 1))
21928 (outline-invisible-p))
21929 (if (< l level) (setq arg 1)))
21930 (setq arg (1- arg)))))
21932 (defun org-forward-element ()
21933 "Move forward by one element.
21934 Move to the next element at the same level, when possible."
21936 (cond ((eobp) (error "Cannot move further down"))
21937 ((org-with-limited-levels (org-at-heading-p))
21938 (let ((origin (point)))
21939 (org-forward-heading-same-level 1)
21940 (unless (org-with-limited-levels (org-at-heading-p))
21942 (error "Cannot move further down"))))
21944 (let* ((elem (org-element-at-point))
21945 (end (org-element-property :end elem))
21946 (parent (org-element-property :parent elem)))
21947 (if (and parent (= (org-element-property :contents-end parent) end))
21948 (goto-char (org-element-property :end parent))
21949 (goto-char end))))))
21951 (defun org-backward-element ()
21952 "Move backward by one element.
21953 Move to the previous element at the same level, when possible."
21955 (cond ((bobp) (error "Cannot move further up"))
21956 ((org-with-limited-levels (org-at-heading-p))
21957 ;; At an headline, move to the previous one, if any, or stay
21959 (let ((origin (point)))
21960 (org-backward-heading-same-level 1)
21961 (unless (org-with-limited-levels (org-at-heading-p))
21963 (error "Cannot move further up"))))
21965 (let* ((trail (org-element-at-point 'keep-trail))
21967 (prev-elem (nth 1 trail))
21968 (beg (org-element-property :begin elem)))
21970 ;; Move to beginning of current element if point isn't
21972 ((/= (point) beg) (goto-char beg))
21973 (prev-elem (goto-char (org-element-property :begin prev-elem)))
21974 ((org-before-first-heading-p) (goto-char (point-min)))
21975 (t (org-back-to-heading)))))))
21977 (defun org-up-element ()
21978 "Move to upper element."
21980 (if (org-with-limited-levels (org-at-heading-p))
21981 (unless (org-up-heading-safe) (error "No surrounding element"))
21982 (let* ((elem (org-element-at-point))
21983 (parent (org-element-property :parent elem)))
21984 (if parent (goto-char (org-element-property :begin parent))
21985 (if (org-with-limited-levels (org-before-first-heading-p))
21986 (error "No surrounding element")
21987 (org-with-limited-levels (org-back-to-heading)))))))
21989 (defvar org-element-greater-elements)
21990 (defun org-down-element ()
21991 "Move to inner element."
21993 (let ((element (org-element-at-point)))
21995 ((memq (org-element-type element) '(plain-list table))
21996 (goto-char (org-element-property :contents-begin element))
21998 ((memq (org-element-type element) org-element-greater-elements)
21999 ;; If contents are hidden, first disclose them.
22000 (when (org-element-property :hiddenp element) (org-cycle))
22001 (goto-char (or (org-element-property :contents-begin element)
22002 (error "No content for this element"))))
22003 (t (error "No inner element")))))
22005 (defun org-drag-element-backward ()
22006 "Move backward element at point."
22008 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
22009 (let* ((trail (org-element-at-point 'keep-trail))
22011 (prev-elem (nth 1 trail)))
22012 ;; Error out if no previous element or previous element is
22013 ;; a parent of the current one.
22014 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
22015 (error "Cannot drag element backward")
22016 (let ((pos (point)))
22017 (org-element-swap-A-B prev-elem elem)
22018 (goto-char (+ (org-element-property :begin prev-elem)
22019 (- pos (org-element-property :begin elem)))))))))
22021 (defun org-drag-element-forward ()
22022 "Move forward element at point."
22024 (let* ((pos (point))
22025 (elem (org-element-at-point)))
22026 (when (= (point-max) (org-element-property :end elem))
22027 (error "Cannot drag element forward"))
22028 (goto-char (org-element-property :end elem))
22029 (let ((next-elem (org-element-at-point)))
22030 (when (or (org-element-nested-p elem next-elem)
22031 (and (eq (org-element-type next-elem) 'headline)
22032 (not (eq (org-element-type elem) 'headline))))
22034 (error "Cannot drag element forward"))
22035 ;; Compute new position of point: it's shifted by NEXT-ELEM
22036 ;; body's length (without final blanks) and by the length of
22037 ;; blanks between ELEM and NEXT-ELEM.
22038 (let ((size-next (- (save-excursion
22039 (goto-char (org-element-property :end next-elem))
22040 (skip-chars-backward " \r\t\n")
22042 ;; Small correction if buffer doesn't end
22043 ;; with a newline character.
22044 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
22045 (org-element-property :begin next-elem)))
22046 (size-blank (- (org-element-property :end elem)
22048 (goto-char (org-element-property :end elem))
22049 (skip-chars-backward " \r\t\n")
22052 (org-element-swap-A-B elem next-elem)
22053 (goto-char (+ pos size-next size-blank))))))
22055 (defun org-mark-element ()
22056 "Put point at beginning of this element, mark at end.
22058 Interactively, if this command is repeated or (in Transient Mark
22059 mode) if the mark is active, it marks the next element after the
22060 ones already marked."
22062 (let (deactivate-mark)
22063 (if (and (org-called-interactively-p 'any)
22064 (or (and (eq last-command this-command) (mark t))
22065 (and transient-mark-mode mark-active)))
22069 (goto-char (org-element-property :end (org-element-at-point)))))
22070 (let ((element (org-element-at-point)))
22072 (push-mark (org-element-property :end element) t t)
22073 (goto-char (org-element-property :begin element))))))
22075 (defun org-narrow-to-element ()
22076 "Narrow buffer to current element."
22078 (let ((elem (org-element-at-point)))
22080 ((eq (car elem) 'headline)
22082 (org-element-property :begin elem)
22083 (org-element-property :end elem)))
22084 ((memq (car elem) org-element-greater-elements)
22086 (org-element-property :contents-begin elem)
22087 (org-element-property :contents-end elem)))
22090 (org-element-property :begin elem)
22091 (org-element-property :end elem))))))
22093 (defun org-transpose-element ()
22094 "Transpose current and previous elements, keeping blank lines between.
22095 Point is moved after both elements."
22097 (org-skip-whitespace)
22098 (let ((end (org-element-property :end (org-element-at-point))))
22099 (org-drag-element-backward)
22102 (defun org-unindent-buffer ()
22103 "Un-indent the visible part of the buffer.
22104 Relative indentation (between items, inside blocks, etc.) isn't
22107 (unless (eq major-mode 'org-mode)
22108 (error "Cannot un-indent a buffer not in Org mode"))
22109 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
22110 unindent-tree ; For byte-compiler.
22116 (if (memq (org-element-type element) '(headline section))
22117 (funcall unindent-tree (org-element-contents element))
22121 (org-element-property :begin element)
22122 (org-element-property :end element))
22123 (org-do-remove-indentation)))))
22124 (reverse contents))))))
22125 (funcall unindent-tree (org-element-contents parse-tree))))
22127 (defun org-show-subtree ()
22128 "Show everything after this heading at deeper levels."
22130 (outline-flag-region
22133 (org-end-of-subtree t t))
22136 (defun org-show-entry ()
22137 "Show the body directly following this heading.
22138 Show the heading too, if it is currently invisible."
22141 (condition-case nil
22143 (org-back-to-heading t)
22144 (outline-flag-region
22145 (max (point-min) (1- (point)))
22147 (if (re-search-forward
22148 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
22149 (match-beginning 1)
22152 (org-cycle-hide-drawers 'children))
22155 (defun org-make-options-regexp (kwds &optional extra)
22156 "Make a regular expression for keyword lines."
22159 (mapconcat 'regexp-quote kwds "\\|")
22160 (if extra (concat "\\|" extra))
22161 "\\):[ \t]*\\(.*\\)"))
22163 ;; Make isearch reveal the necessary context
22164 (defun org-isearch-end ()
22165 "Reveal context after isearch exits."
22166 (when isearch-success ; only if search was successful
22167 (if (featurep 'xemacs)
22168 ;; Under XEmacs, the hook is run in the correct place,
22169 ;; we directly show the context.
22170 (org-show-context 'isearch)
22171 ;; In Emacs the hook runs *before* restoring the overlays.
22172 ;; So we have to use a one-time post-command-hook to do this.
22173 ;; (Emacs 22 has a special variable, see function `org-mode')
22174 (unless (and (boundp 'isearch-mode-end-hook-quit)
22175 isearch-mode-end-hook-quit)
22176 ;; Only when the isearch was not quitted.
22177 (org-add-hook 'post-command-hook 'org-isearch-post-command
22178 'append 'local)))))
22180 (defun org-isearch-post-command ()
22181 "Remove self from hook, and show context."
22182 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
22183 (org-show-context 'isearch))
22186 ;;;; Integration with and fixes for other packages
22190 (defvar org-imenu-markers nil
22191 "All markers currently used by Imenu.")
22192 (make-variable-buffer-local 'org-imenu-markers)
22194 (defun org-imenu-new-marker (&optional pos)
22195 "Return a new marker for use by Imenu, and remember the marker."
22196 (let ((m (make-marker)))
22197 (move-marker m (or pos (point)))
22198 (push m org-imenu-markers)
22201 (defun org-imenu-get-tree ()
22202 "Produce the index for Imenu."
22203 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
22204 (setq org-imenu-markers nil)
22205 (let* ((n org-imenu-depth)
22206 (re (concat "^" (org-get-limited-outline-regexp)))
22207 (subs (make-vector (1+ n) nil))
22213 (goto-char (point-max))
22214 (while (re-search-backward re nil t)
22215 (setq level (org-reduced-level (funcall outline-level)))
22216 (when (and (<= level n)
22217 (looking-at org-complex-heading-regexp))
22218 (setq head (org-link-display-format
22219 (org-match-string-no-properties 4))
22220 m (org-imenu-new-marker))
22221 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
22222 (if (>= level last-level)
22223 (push (cons head m) (aref subs level))
22224 (push (cons head (aref subs (1+ level))) (aref subs level))
22225 (loop for i from (1+ level) to n do (aset subs i nil)))
22226 (setq last-level level)))))
22229 (eval-after-load "imenu"
22231 (add-hook 'imenu-after-jump-hook
22233 (if (derived-mode-p 'org-mode)
22234 (org-show-context 'org-goto))))))
22236 (defun org-link-display-format (link)
22237 "Replace a link with either the description, or the link target
22238 if no description is present"
22240 (if (string-match org-bracket-link-analytic-regexp link)
22241 (replace-match (if (match-end 5)
22242 (match-string 5 link)
22243 (concat (match-string 1 link)
22244 (match-string 3 link)))
22248 (defun org-toggle-link-display ()
22249 "Toggle the literal or descriptive display of links."
22251 (if org-descriptive-links
22252 (progn (org-remove-from-invisibility-spec '(org-link))
22253 (org-restart-font-lock)
22254 (setq org-descriptive-links nil))
22255 (progn (add-to-invisibility-spec '(org-link))
22256 (org-restart-font-lock)
22257 (setq org-descriptive-links t))))
22259 ;; Speedbar support
22261 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
22262 "Overlay marking the agenda restriction line in speedbar.")
22263 (overlay-put org-speedbar-restriction-lock-overlay
22264 'face 'org-agenda-restriction-lock)
22265 (overlay-put org-speedbar-restriction-lock-overlay
22266 'help-echo "Agendas are currently limited to this item.")
22267 (org-detach-overlay org-speedbar-restriction-lock-overlay)
22269 (defun org-speedbar-set-agenda-restriction ()
22270 "Restrict future agenda commands to the location at point in speedbar.
22271 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
22273 (require 'org-agenda)
22274 (let (p m tp np dir txt)
22276 ((setq p (text-property-any (point-at-bol) (point-at-eol)
22278 (setq m (get-text-property p 'org-imenu-marker))
22279 (with-current-buffer (marker-buffer m)
22281 (org-agenda-set-restriction-lock 'subtree)))
22282 ((setq p (text-property-any (point-at-bol) (point-at-eol)
22283 'speedbar-function 'speedbar-find-file))
22284 (setq tp (previous-single-property-change
22285 (1+ p) 'speedbar-function)
22286 np (next-single-property-change
22287 tp 'speedbar-function)
22288 dir (speedbar-line-directory)
22289 txt (buffer-substring-no-properties (or tp (point-min))
22290 (or np (point-max))))
22291 (with-current-buffer (find-file-noselect
22292 (let ((default-directory dir))
22293 (expand-file-name txt)))
22294 (unless (derived-mode-p 'org-mode)
22295 (error "Cannot restrict to non-Org-mode file"))
22296 (org-agenda-set-restriction-lock 'file)))
22297 (t (error "Don't know how to restrict Org-mode's agenda")))
22298 (move-overlay org-speedbar-restriction-lock-overlay
22299 (point-at-bol) (point-at-eol))
22300 (setq current-prefix-arg nil)
22301 (org-agenda-maybe-redo)))
22303 (eval-after-load "speedbar"
22305 (speedbar-add-supported-extension ".org")
22306 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
22307 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
22308 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
22309 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
22310 (add-hook 'speedbar-visiting-tag-hook
22311 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
22313 ;;; Fixes and Hacks for problems with other packages
22315 ;; Make flyspell not check words in links, to not mess up our keymap
22316 (defun org-mode-flyspell-verify ()
22317 "Don't let flyspell put overlays at active buttons, or on
22318 {todo,all-time,additional-option-like}-keywords."
22319 (let ((pos (max (1- (point)) (point-min)))
22320 (word (thing-at-point 'word)))
22321 (and (not (get-text-property pos 'keymap))
22322 (not (get-text-property pos 'org-no-flyspell))
22323 (not (member word org-todo-keywords-1))
22324 (not (member word org-all-time-keywords))
22325 (not (member word org-options-keywords))
22326 (not (member word (mapcar 'car org-startup-options)))
22327 (not (member word org-additional-option-like-keywords-for-flyspell)))))
22329 (defun org-remove-flyspell-overlays-in (beg end)
22330 "Remove flyspell overlays in region."
22331 (and (org-bound-and-true-p flyspell-mode)
22332 (fboundp 'flyspell-delete-region-overlays)
22333 (flyspell-delete-region-overlays beg end))
22334 (add-text-properties beg end '(org-no-flyspell t)))
22336 ;; Make `bookmark-jump' shows the jump location if it was hidden.
22337 (eval-after-load "bookmark"
22338 '(if (boundp 'bookmark-after-jump-hook)
22339 ;; We can use the hook
22340 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
22341 ;; Hook not available, use advice
22342 (defadvice bookmark-jump (after org-make-visible activate)
22343 "Make the position visible."
22344 (org-bookmark-jump-unhide))))
22346 ;; Make sure saveplace shows the location if it was hidden
22347 (eval-after-load "saveplace"
22348 '(defadvice save-place-find-file-hook (after org-make-visible activate)
22349 "Make the position visible."
22350 (org-bookmark-jump-unhide)))
22352 ;; Make sure ecb shows the location if it was hidden
22353 (eval-after-load "ecb"
22354 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
22355 "Make hierarchy visible when jumping into location from ECB tree buffer."
22356 (if (derived-mode-p 'org-mode)
22357 (org-show-context))))
22359 (defun org-bookmark-jump-unhide ()
22360 "Unhide the current position, to show the bookmark location."
22361 (and (derived-mode-p 'org-mode)
22362 (or (outline-invisible-p)
22363 (save-excursion (goto-char (max (point-min) (1- (point))))
22364 (outline-invisible-p)))
22365 (org-show-context 'bookmark-jump)))
22367 ;; Make session.el ignore our circular variable
22368 (eval-after-load "session"
22369 '(add-to-list 'session-globals-exclude 'org-mark-ring))
22371 ;;;; Experimental code
22373 (defun org-closed-in-range ()
22374 "Sparse tree of items closed in a certain time range.
22375 Still experimental, may disappear in the future."
22377 ;; Get the time interval from the user.
22378 (let* ((time1 (org-float-time
22379 (org-read-date nil 'to-time nil "Starting date: ")))
22380 (time2 (org-float-time
22381 (org-read-date nil 'to-time nil "End date:")))
22382 ;; callback function
22383 (callback (lambda ()
22386 (apply 'encode-time
22387 (org-parse-time-string
22388 (match-string 1))))))
22389 ;; check if time in interval
22390 (and (>= time time1) (<= time time2))))))
22391 ;; make tree, check each match with the callback
22392 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
22398 (run-hooks 'org-load-hook)
22400 ;;; org.el ends here