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 t
)
86 ;; `org-outline-regexp' ought to be a defconst but is let-binding in
87 ;; some places -- e.g. see the macro org-with-limited-levels.
89 ;; In Org buffers, the value of `outline-regexp' is that of
90 ;; `org-outline-regexp'. The only function still directly relying on
91 ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
92 ;; job when `orgstruct-mode' is active.
93 (defvar org-outline-regexp
"\\*+ "
94 "Regexp to match Org headlines.")
96 (defvar org-outline-regexp-bol
"^\\*+ "
97 "Regexp to match Org headlines.
98 This is similar to `org-outline-regexp' but additionally makes
99 sure that we are at the beginning of the line.")
101 (defvar org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
102 "Matches a headline, putting stars and text into groups.
103 Stars are put in group 1 and the trimmed body in group 2.")
105 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
106 (when (fboundp 'defvaralias
)
107 (unless (boundp 'calendar-view-holidays-initially-flag
)
108 (defvaralias 'calendar-view-holidays-initially-flag
109 'view-calendar-holidays-initially
))
110 (unless (boundp 'calendar-view-diary-initially-flag
)
111 (defvaralias 'calendar-view-diary-initially-flag
112 'view-diary-entries-initially
))
113 (unless (boundp 'diary-fancy-buffer
)
114 (defvaralias 'diary-fancy-buffer
'fancy-diary-buffer
)))
116 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
117 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
118 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
119 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
120 (declare-function org-at-clock-log-p
"org-clock" ())
121 (declare-function org-clock-get-last-clock-out-time
"org-clock" ())
122 (declare-function org-clock-timestamps-up
"org-clock" (&optional n
))
123 (declare-function org-clock-timestamps-down
"org-clock" (&optional n
))
124 (declare-function org-clock-sum-current-item
"org-clock" (&optional tstart
))
126 (declare-function orgtbl-mode
"org-table" (&optional arg
))
127 (declare-function org-clock-out
"org-clock" (&optional switch-to-state fail-quietly at-time
))
128 (declare-function org-beamer-mode
"ox-beamer" ())
129 (declare-function org-table-edit-field
"org-table" (arg))
130 (declare-function org-table-justify-field-maybe
"org-table" (&optional new
))
131 (declare-function org-id-get-create
"org-id" (&optional force
))
132 (declare-function org-id-find-id-file
"org-id" (id))
133 (declare-function org-tags-view
"org-agenda" (&optional todo-only match
))
134 (declare-function org-agenda-list
"org-agenda" (&optional arg start-day span
))
135 (declare-function org-table-align
"org-table" ())
136 (declare-function org-table-paste-rectangle
"org-table" ())
137 (declare-function org-table-maybe-eval-formula
"org-table" ())
138 (declare-function org-table-maybe-recalculate-line
"org-table" ())
140 (declare-function org-element--parse-objects
"org-element"
141 (beg end acc restriction
))
142 (declare-function org-element-at-point
"org-element" (&optional keep-trail
))
143 (declare-function org-element-contents
"org-element" (element))
144 (declare-function org-element-context
"org-element" (&optional element
))
145 (declare-function org-element-interpret-data
"org-element"
146 (data &optional parent
))
147 (declare-function org-element-map
"org-element"
148 (data types fun
&optional info first-match no-recursion
))
149 (declare-function org-element-nested-p
"org-element" (elem-a elem-b
))
150 (declare-function org-element-parse-buffer
"org-element"
151 (&optional granularity visible-only
))
152 (declare-function org-element-property
"org-element" (property element
))
153 (declare-function org-element-put-property
"org-element"
154 (element property value
))
155 (declare-function org-element-swap-A-B
"org-element" (elem-a elem-b
))
156 (declare-function org-element--parse-objects
"org-element"
157 (beg end acc restriction
))
158 (declare-function org-element-parse-buffer
"org-element"
159 (&optional granularity visible-only
))
160 (declare-function org-element-type
"org-element" (element))
162 ;; load languages based on value of `org-babel-load-languages'
163 (defvar org-babel-load-languages
)
166 (defun org-babel-do-load-languages (sym value
)
167 "Load the languages defined in `org-babel-load-languages'."
168 (set-default sym value
)
170 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
173 (require (intern (concat "ob-" lang
))))
175 (funcall 'fmakunbound
176 (intern (concat "org-babel-execute:" lang
)))
177 (funcall 'fmakunbound
178 (intern (concat "org-babel-expand-body:" lang
)))))))
179 org-babel-load-languages
))
181 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
182 "Languages which can be evaluated in Org-mode buffers.
183 This list can be used to load support for any of the languages
184 below, note that each language will depend on a different set of
185 system executables and/or Emacs modes. When a language is
186 \"loaded\", then code blocks in that language can be evaluated
187 with `org-babel-execute-src-block' bound by default to C-c
188 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
189 be set to remove code block evaluation from the C-c C-c
190 keybinding. By default only Emacs Lisp (which has no
191 requirements) is loaded."
193 :set
'org-babel-do-load-languages
195 :type
'(alist :tag
"Babel Languages"
198 (const :tag
"Awk" awk
)
201 (const :tag
"Asymptote" asymptote
)
202 (const :tag
"Calc" calc
)
203 (const :tag
"Clojure" clojure
)
204 (const :tag
"CSS" css
)
205 (const :tag
"Ditaa" ditaa
)
206 (const :tag
"Dot" dot
)
207 (const :tag
"Emacs Lisp" emacs-lisp
)
208 (const :tag
"Fortran" fortran
)
209 (const :tag
"Gnuplot" gnuplot
)
210 (const :tag
"Haskell" haskell
)
212 (const :tag
"Java" java
)
213 (const :tag
"Javascript" js
)
214 (const :tag
"LaTeX" latex
)
215 (const :tag
"Ledger" ledger
)
216 (const :tag
"Lilypond" lilypond
)
217 (const :tag
"Lisp" lisp
)
218 (const :tag
"Makefile" makefile
)
219 (const :tag
"Maxima" maxima
)
220 (const :tag
"Matlab" matlab
)
221 (const :tag
"Mscgen" mscgen
)
222 (const :tag
"Ocaml" ocaml
)
223 (const :tag
"Octave" octave
)
224 (const :tag
"Org" org
)
225 (const :tag
"Perl" perl
)
226 (const :tag
"Pico Lisp" picolisp
)
227 (const :tag
"PlantUML" plantuml
)
228 (const :tag
"Python" python
)
229 (const :tag
"Ruby" ruby
)
230 (const :tag
"Sass" sass
)
231 (const :tag
"Scala" scala
)
232 (const :tag
"Scheme" scheme
)
233 (const :tag
"Screen" screen
)
234 (const :tag
"Shell Script" sh
)
235 (const :tag
"Shen" shen
)
236 (const :tag
"Sql" sql
)
237 (const :tag
"Sqlite" sqlite
))
238 :value-type
(boolean :tag
"Activate" :value t
)))
240 ;;;; Customization variables
241 (defcustom org-clone-delete-id nil
242 "Remove ID property of clones of a subtree.
243 When non-nil, clones of a subtree don't inherit the ID property.
244 Otherwise they inherit the ID property with a new unique
254 (defun org-version (&optional here full message
)
255 "Show the org-mode version in the echo area.
256 With prefix argument HERE, insert it at point.
257 When FULL is non-nil, use a verbose version string.
258 When MESSAGE is non-nil, display a message with the version."
260 (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
261 (save-load-suffixes (when (boundp 'load-suffixes
) load-suffixes
))
262 (load-suffixes (list ".el"))
263 (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
265 (and (fboundp 'org-release
) (fboundp 'org-git-version
))
266 (org-load-noerror-mustsuffix (concat org-dir
"org-version"))))
267 (load-suffixes save-load-suffixes
)
268 (org-version (org-release))
269 (git-version (org-git-version))
270 (version (format "Org-mode version %s (%s @ %s)"
274 (if (string= org-dir org-install-dir
)
276 (concat "mixed installation! " org-install-dir
" and " org-dir
))
277 "org-loaddefs.el can not be found!")))
278 (_version (if full version org-version
)))
279 (if (org-called-interactively-p 'interactive
)
283 (if message
(message _version
))
286 (defconst org-version
(org-version))
288 ;;; Compatibility constants
290 ;;; The custom variables
293 "Outline-based notes management and organizer."
298 (defcustom org-mode-hook nil
299 "Mode hook for Org-mode, run after the mode was turned on."
303 (defcustom org-load-hook nil
304 "Hook that is run after org.el has been loaded."
308 (defcustom org-log-buffer-setup-hook nil
309 "Hook that is run after an Org log buffer is created."
314 (defvar org-modules
) ; defined below
315 (defvar org-modules-loaded nil
316 "Have the modules been loaded already?")
318 (defun org-load-modules-maybe (&optional force
)
319 "Load all extensions listed in `org-modules'."
320 (when (or force
(not org-modules-loaded
))
322 (condition-case nil
(require ext
)
323 (error (message "Problems while trying to load feature `%s'" ext
))))
325 (setq org-modules-loaded t
)))
327 (defun org-set-modules (var value
)
328 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
330 (when (featurep 'org
)
331 (org-load-modules-maybe 'force
)))
333 (defcustom org-modules
'(org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl
)
334 "Modules that should always be loaded together with org.el.
336 If a description starts with <C>, the file is not part of Emacs
337 and loading it will require that you have downloaded and properly
338 installed the Org mode distribution.
340 You can also use this system to load external packages (i.e. neither Org
341 core modules, nor modules from the CONTRIB directory). Just add symbols
342 to the end of the list. If the package is called org-xyz.el, then you need
343 to add the symbol `xyz', and the package must have a call to:
347 For export specific modules, see also `org-export-backends'."
349 :set
'org-set-modules
352 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
353 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
354 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
355 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
356 (const :tag
" docview: Links to doc-view buffers" org-docview
)
357 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
358 (const :tag
" id: Global IDs for identifying entries" org-id
)
359 (const :tag
" info: Links to Info nodes" org-info
)
360 (const :tag
" habit: Track your consistency with habits" org-habit
)
361 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
362 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
363 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
364 (const :tag
" mew Links to Mew folders/messages" org-mew
)
365 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
366 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
367 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
368 (const :tag
" vm: Links to VM folders/messages" org-vm
)
369 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
370 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
371 (const :tag
" mouse: Additional mouse support" org-mouse
)
373 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
374 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
375 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
376 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
377 (const :tag
"C collector: Collect properties into tables" org-collector
)
378 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
379 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
380 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
381 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
382 (const :tag
"C eval: Include command output as text" org-eval
)
383 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
384 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
385 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
386 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
387 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
389 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
391 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
392 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
393 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
394 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
395 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
396 (const :tag
"C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber
)
397 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
398 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
399 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
400 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
401 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
402 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
403 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
404 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
405 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
406 (const :tag
"C track: Keep up with Org-mode development" org-track
)
407 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
408 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
409 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
411 (defvar org-export-registered-backends
) ; From ox.el
412 (declare-function org-export-derived-backend-p
"ox" (backend &rest backends
))
413 (defcustom org-export-backends
'(ascii html icalendar latex
)
414 "List of export back-ends that should be always available.
416 If a description starts with <C>, the file is not part of Emacs
417 and loading it will require that you have downloaded and properly
418 installed the Org mode distribution.
420 Unlike to `org-modules', libraries in this list will not be
421 loaded along with Org, but only once the export framework is
424 This variable needs to be set before org.el is loaded. If you
425 need to make a change while Emacs is running, use the customize
426 interface or run the following code, where VALUE stands for the
427 new value of the variable, after updating it:
430 \(setq org-export-registered-backends
433 \(or (memq backend val)
437 \(and (org-export-derived-backend-p b (car backend))
438 \(throw 'parentp t)))
441 org-export-registered-backends))
442 \(let ((new-list (mapcar 'car org-export-registered-backends)))
443 \(dolist (backend val)
445 \((not (load (format \"ox-%s\" backend) t t))
446 \(message \"Problems while trying to load export back-end `%s'\"
448 \((not (memq backend new-list)) (push backend new-list))))
449 \(set-default var new-list)))
451 Adding a back-end to this list will also pull the back-end it
455 :set
(lambda (var val
)
456 (if (not (featurep 'ox
)) (set-default var val
)
457 ;; Any back-end not required anymore (not present in VAL and not
458 ;; a parent of any back-end in the new value) is removed from the
459 ;; list of registered back-ends.
460 (setq org-export-registered-backends
463 (or (memq backend val
)
467 (and (org-export-derived-backend-p b
(car backend
))
471 org-export-registered-backends
))
472 ;; Now build NEW-LIST of both new back-ends and required
474 (let ((new-list (mapcar 'car org-export-registered-backends
)))
475 (dolist (backend val
)
477 ((not (load (format "ox-%s" backend
) t t
))
478 (message "Problems while trying to load export back-end `%s'"
480 ((not (memq backend new-list
)) (push backend new-list
))))
481 ;; Set VAR to that list with fixed dependencies.
482 (set-default var new-list
))))
483 :type
'(set :greedy t
484 (const :tag
" ascii Export buffer to ASCII format" ascii
)
485 (const :tag
" beamer Export buffer to Beamer presentation" beamer
)
486 (const :tag
" html Export buffer to HTML format" html
)
487 (const :tag
" icalendar Export buffer to iCalendar format" icalendar
)
488 (const :tag
" latex Export buffer to LaTeX format" latex
)
489 (const :tag
" man Export buffer to MAN format" man
)
490 (const :tag
" md Export buffer to Markdown format" md
)
491 (const :tag
" odt Export buffer to ODT format" odt
)
492 (const :tag
" texinfo Export buffer to Texinfo format" texinfo
)
493 (const :tag
" infojs: Set up Sebastian Rose's JavaScript org-info.js" jsinfo
)
494 (const :tag
"C confluence Export buffer to Confluence Wiki format" confluence
)
495 (const :tag
"C groff Export buffer to Groff format" groff
)
496 (const :tag
"C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter
)
497 (const :tag
"C taskjuggler Export buffer to TaskJuggler format" taskjuggler
)))
502 (condition-case nil
(require (intern (format "ox-%s" backend
)))
503 (error (message "Problems while trying to load export back-end `%s'"
505 org-export-backends
))
507 (defcustom org-support-shift-select nil
508 "Non-nil means make shift-cursor commands select text when possible.
510 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
511 start selecting a region, or enlarge regions started in this way.
512 In Org-mode, in special contexts, these same keys are used for
513 other purposes, important enough to compete with shift selection.
514 Org tries to balance these needs by supporting `shift-select-mode'
515 outside these special contexts, under control of this variable.
517 The default of this variable is nil, to avoid confusing behavior. Shifted
518 cursor keys will then execute Org commands in the following contexts:
519 - on a headline, changing TODO state (left/right) and priority (up/down)
520 - on a time stamp, changing the time
521 - in a plain list item, changing the bullet type
522 - in a property definition line, switching between allowed values
523 - in the BEGIN line of a clock table (changing the time block).
524 Outside these contexts, the commands will throw an error.
526 When this variable is t and the cursor is not in a special
527 context, Org-mode will support shift-selection for making and
528 enlarging regions. To make this more effective, the bullet
529 cycling will no longer happen anywhere in an item line, but only
530 if the cursor is exactly on the bullet.
532 If you set this variable to the symbol `always', then the keys
533 will not be special in headlines, property lines, and item lines,
534 to make shift selection work there as well. If this is what you
535 want, you can use the following alternative commands: `C-c C-t'
536 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
537 can be used to switch TODO sets, `C-c -' to cycle item bullet
538 types, and properties can be edited by hand or in column view.
540 However, when the cursor is on a timestamp, shift-cursor commands
541 will still edit the time stamp - this is just too good to give up.
543 XEmacs user should have this variable set to nil, because
544 `shift-select-mode' is in Emacs 23 or later only."
547 (const :tag
"Never" nil
)
548 (const :tag
"When outside special context" t
)
549 (const :tag
"Everywhere except timestamps" always
)))
551 (defcustom org-loop-over-headlines-in-active-region nil
552 "Shall some commands act upon headlines in the active region?
554 When set to `t', some commands will be performed in all headlines
555 within the active region.
557 When set to `start-level', some commands will be performed in all
558 headlines within the active region, provided that these headlines
559 are of the same level than the first one.
561 When set to a string, those commands will be performed on the
562 matching headlines within the active region. Such string must be
563 a tags/property/todo match as it is used in the agenda tags view.
565 The list of commands is: `org-schedule', `org-deadline',
566 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
567 `org-archive-to-archive-sibling'. The archiving commands skip
568 already archived entries."
569 :type
'(choice (const :tag
"Don't loop" nil
)
570 (const :tag
"All headlines in active region" t
)
571 (const :tag
"In active region, headlines at the same level than the first one" 'start-level
)
572 (string :tag
"Tags/Property/Todo matcher"))
577 (defgroup org-startup nil
578 "Options concerning startup of Org-mode."
582 (defcustom org-startup-folded t
583 "Non-nil means entering Org-mode will switch to OVERVIEW.
584 This can also be configured on a per-file basis by adding one of
585 the following lines anywhere in the buffer:
587 #+STARTUP: fold (or `overview', this is equivalent)
588 #+STARTUP: nofold (or `showall', this is equivalent)
590 #+STARTUP: showeverything
592 By default, this option is ignored when Org opens agenda files
593 for the first time. If you want the agenda to honor the startup
594 option, set `org-agenda-inhibit-startup' to nil."
597 (const :tag
"nofold: show all" nil
)
598 (const :tag
"fold: overview" t
)
599 (const :tag
"content: all headlines" content
)
600 (const :tag
"show everything, even drawers" showeverything
)))
602 (defcustom org-startup-truncated t
603 "Non-nil means entering Org-mode will set `truncate-lines'.
604 This is useful since some lines containing links can be very long and
605 uninteresting. Also tables look terrible when wrapped."
609 (defcustom org-startup-indented nil
610 "Non-nil means turn on `org-indent-mode' on startup.
611 This can also be configured on a per-file basis by adding one of
612 the following lines anywhere in the buffer:
616 :group
'org-structure
618 (const :tag
"Not" nil
)
619 (const :tag
"Globally (slow on startup in large files)" t
)))
621 (defcustom org-use-sub-superscripts t
622 "Non-nil means interpret \"_\" and \"^\" for display.
623 When this option is turned on, you can use TeX-like syntax for sub- and
624 superscripts. Several characters after \"_\" or \"^\" will be
625 considered as a single item - so grouping with {} is normally not
626 needed. For example, the following things will be parsed as single
627 sub- or superscripts.
629 10^24 or 10^tau several digits will be considered 1 item.
630 10^-12 or 10^-tau a leading sign with digits or a word
631 x^2-y^3 will be read as x^2 - y^3, because items are
632 terminated by almost any nonword/nondigit char.
633 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
635 Still, ambiguity is possible - so when in doubt use {} to enclose
636 the sub/superscript. If you set this variable to the symbol
637 `{}', the braces are *required* in order to trigger
638 interpretations as sub/superscript. This can be helpful in
639 documents that need \"_\" frequently in plain text."
643 (const :tag
"Always interpret" t
)
644 (const :tag
"Only with braces" {})
645 (const :tag
"Never interpret" nil
)))
647 (defcustom org-startup-with-beamer-mode nil
648 "Non-nil means turn on `org-beamer-mode' on startup.
649 This can also be configured on a per-file basis by adding one of
650 the following lines anywhere in the buffer:
657 (defcustom org-startup-align-all-tables nil
658 "Non-nil means align all tables when visiting a file.
659 This is useful when the column width in tables is forced with <N> cookies
660 in table fields. Such tables will look correct only after the first re-align.
661 This can also be configured on a per-file basis by adding one of
662 the following lines anywhere in the buffer:
668 (defcustom org-startup-with-inline-images nil
669 "Non-nil means show inline images when loading a new Org file.
670 This can also be configured on a per-file basis by adding one of
671 the following lines anywhere in the buffer:
672 #+STARTUP: inlineimages
673 #+STARTUP: noinlineimages"
678 (defcustom org-startup-with-latex-preview nil
679 "Non-nil means preview LaTeX fragments when loading a new Org file.
681 This can also be configured on a per-file basis by adding one of
682 the followinglines anywhere in the buffer:
683 #+STARTUP: latexpreview
684 #+STARTUP: nolatexpreview"
689 (defcustom org-insert-mode-line-in-empty-file nil
690 "Non-nil means insert the first line setting Org-mode in empty files.
691 When the function `org-mode' is called interactively in an empty file, this
692 normally means that the file name does not automatically trigger Org-mode.
693 To ensure that the file will always be in Org-mode in the future, a
694 line enforcing Org-mode will be inserted into the buffer, if this option
699 (defcustom org-replace-disputed-keys nil
700 "Non-nil means use alternative key bindings for some keys.
701 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
702 These keys are also used by other packages like shift-selection-mode'
703 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
704 If you want to use Org-mode together with one of these other modes,
705 or more generally if you would like to move some Org-mode commands to
706 other keys, set this variable and configure the keys with the variable
709 This option is only relevant at load-time of Org-mode, and must be set
710 *before* org.el is loaded. Changing it requires a restart of Emacs to
715 (defcustom org-use-extra-keys nil
716 "Non-nil means use extra key sequence definitions for certain commands.
717 This happens automatically if you run XEmacs or if `window-system'
718 is nil. This variable lets you do the same manually. You must
719 set it before loading org.
721 Example: on Carbon Emacs 22 running graphically, with an external
722 keyboard on a Powerbook, the default way of setting M-left might
723 not work for either Alt or ESC. Setting this variable will make
728 (if (fboundp 'defvaralias
)
729 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
731 (defcustom org-disputed-keys
732 '(([(shift up
)] .
[(meta p
)])
733 ([(shift down
)] .
[(meta n
)])
734 ([(shift left
)] .
[(meta -
)])
735 ([(shift right
)] .
[(meta +)])
736 ([(control shift right
)] .
[(meta shift
+)])
737 ([(control shift left
)] .
[(meta shift -
)]))
738 "Keys for which Org-mode and other modes compete.
739 This is an alist, cars are the default keys, second element specifies
740 the alternative to use when `org-replace-disputed-keys' is t.
742 Keys can be specified in any syntax supported by `define-key'.
743 The value of this option takes effect only at Org-mode's startup,
744 therefore you'll have to restart Emacs to apply it after changing."
749 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
750 Or return the original if not disputed.
751 Also apply the translations defined in `org-xemacs-key-equivalents'."
752 (when org-replace-disputed-keys
753 (let* ((nkey (key-description key
))
754 (x (org-find-if (lambda (x)
755 (equal (key-description (car x
)) nkey
))
757 (setq key
(if x
(cdr x
) key
))))
758 (when (featurep 'xemacs
)
759 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
762 (defun org-find-if (predicate seq
)
765 (if (funcall predicate
(car seq
))
766 (throw 'exit
(car seq
))
769 (defun org-defkey (keymap key def
)
770 "Define a key, possibly translated, as returned by `org-key'."
771 (define-key keymap
(org-key key
) def
))
773 (defcustom org-ellipsis nil
774 "The ellipsis to use in the Org-mode outline.
775 When nil, just use the standard three dots. When a string, use that instead,
776 When a face, use the standard 3 dots, but with the specified face.
777 The change affects only Org-mode (which will then use its own display table).
778 Changing this requires executing `M-x org-mode' in a buffer to become
781 :type
'(choice (const :tag
"Default" nil
)
782 (face :tag
"Face" :value org-warning
)
783 (string :tag
"String" :value
"...#")))
785 (defvar org-display-table nil
786 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
788 (defgroup org-keywords nil
789 "Keywords in Org-mode."
793 (defcustom org-deadline-string
"DEADLINE:"
794 "String to mark deadline entries.
795 A deadline is this string, followed by a time stamp. Should be a word,
796 terminated by a colon. You can insert a schedule keyword and
797 a timestamp with \\[org-deadline].
798 Changes become only effective after restarting Emacs."
802 (defcustom org-scheduled-string
"SCHEDULED:"
803 "String to mark scheduled TODO entries.
804 A schedule is this string, followed by a time stamp. Should be a word,
805 terminated by a colon. You can insert a schedule keyword and
806 a timestamp with \\[org-schedule].
807 Changes become only effective after restarting Emacs."
811 (defcustom org-closed-string
"CLOSED:"
812 "String used as the prefix for timestamps logging closing a TODO entry."
816 (defcustom org-clock-string
"CLOCK:"
817 "String used as prefix for timestamps clocking work hours on an item."
821 (defconst org-planning-or-clock-line-re
(concat "^[ \t]*\\("
822 org-scheduled-string
"\\|"
823 org-deadline-string
"\\|"
824 org-closed-string
"\\|"
825 org-clock-string
"\\)")
826 "Matches a line with planning or clock info.")
828 (defcustom org-comment-string
"COMMENT"
829 "Entries starting with this keyword will never be exported.
830 An entry can be toggled between COMMENT and normal with
831 \\[org-toggle-comment].
832 Changes become only effective after restarting Emacs."
836 (defcustom org-quote-string
"QUOTE"
837 "Entries starting with this keyword will be exported in fixed-width font.
838 Quoting applies only to the text in the entry following the headline, and does
839 not extend beyond the next headline, even if that is lower level.
840 An entry can be toggled between QUOTE and normal with
841 \\[org-toggle-fixed-width-section]."
845 (defconst org-repeat-re
846 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
847 "Regular expression for specifying repeated events.
848 After a match, group 1 contains the repeat expression.")
850 (defgroup org-structure nil
851 "Options concerning the general structure of Org-mode files."
855 (defgroup org-reveal-location nil
856 "Options about how to make context of a location visible."
857 :tag
"Org Reveal Location"
858 :group
'org-structure
)
860 (defconst org-context-choice
862 (const :tag
"Always" t
)
863 (const :tag
"Never" nil
)
864 (repeat :greedy t
:tag
"Individual contexts"
866 (choice :tag
"Context"
873 (const bookmark-jump
)
877 "Contexts for the reveal options.")
879 (defcustom org-show-hierarchy-above
'((default . t
))
880 "Non-nil means show full hierarchy when revealing a location.
881 Org-mode often shows locations in an org-mode file which might have
882 been invisible before. When this is set, the hierarchy of headings
883 above the exposed location is shown.
884 Turning this off for example for sparse trees makes them very compact.
885 Instead of t, this can also be an alist specifying this option for different
886 contexts. Valid contexts are
887 agenda when exposing an entry from the agenda
888 org-goto when using the command `org-goto' on key C-c C-j
889 occur-tree when using the command `org-occur' on key C-c /
890 tags-tree when constructing a sparse tree based on tags matches
891 link-search when exposing search matches associated with a link
892 mark-goto when exposing the jump goal of a mark
893 bookmark-jump when exposing a bookmark location
894 isearch when exiting from an incremental search
895 default default for all contexts not set explicitly"
896 :group
'org-reveal-location
897 :type org-context-choice
)
899 (defcustom org-show-following-heading
'((default . nil
))
900 "Non-nil means show following heading when revealing a location.
901 Org-mode often shows locations in an org-mode file which might have
902 been invisible before. When this is set, the heading following the
904 Turning this off for example for sparse trees makes them very compact,
905 but makes it harder to edit the location of the match. In such a case,
906 use the command \\[org-reveal] to show more context.
907 Instead of t, this can also be an alist specifying this option for different
908 contexts. See `org-show-hierarchy-above' for valid contexts."
909 :group
'org-reveal-location
910 :type org-context-choice
)
912 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
913 "Non-nil means show all sibling heading when revealing a location.
914 Org-mode often shows locations in an org-mode file which might have
915 been invisible before. When this is set, the sibling of the current entry
916 heading are all made visible. If `org-show-hierarchy-above' is t,
917 the same happens on each level of the hierarchy above the current entry.
919 By default this is on for the isearch context, off for all other contexts.
920 Turning this off for example for sparse trees makes them very compact,
921 but makes it harder to edit the location of the match. In such a case,
922 use the command \\[org-reveal] to show more context.
923 Instead of t, this can also be an alist specifying this option for different
924 contexts. See `org-show-hierarchy-above' for valid contexts."
925 :group
'org-reveal-location
926 :type org-context-choice
)
928 (defcustom org-show-entry-below
'((default . nil
))
929 "Non-nil means show the entry below a headline when revealing a location.
930 Org-mode often shows locations in an org-mode file which might have
931 been invisible before. When this is set, the text below the headline that is
932 exposed is also shown.
934 By default this is off for all contexts.
935 Instead of t, this can also be an alist specifying this option for different
936 contexts. See `org-show-hierarchy-above' for valid contexts."
937 :group
'org-reveal-location
938 :type org-context-choice
)
940 (defcustom org-indirect-buffer-display
'other-window
941 "How should indirect tree buffers be displayed?
942 This applies to indirect buffers created with the commands
943 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
945 current-window Display in the current window
946 other-window Just display in another window.
947 dedicated-frame Create one new frame, and re-use it each time.
948 new-frame Make a new frame each time. Note that in this case
949 previously-made indirect buffers are kept, and you need to
950 kill these buffers yourself."
951 :group
'org-structure
952 :group
'org-agenda-windows
954 (const :tag
"In current window" current-window
)
955 (const :tag
"In current frame, other window" other-window
)
956 (const :tag
"Each time a new frame" new-frame
)
957 (const :tag
"One dedicated frame" dedicated-frame
)))
959 (defcustom org-use-speed-commands nil
960 "Non-nil means activate single letter commands at beginning of a headline.
961 This may also be a function to test for appropriate locations where speed
962 commands should be active."
963 :group
'org-structure
965 (const :tag
"Never" nil
)
966 (const :tag
"At beginning of headline stars" t
)
969 (defcustom org-speed-commands-user nil
970 "Alist of additional speed commands.
971 This list will be checked before `org-speed-commands-default'
972 when the variable `org-use-speed-commands' is non-nil
973 and when the cursor is at the beginning of a headline.
974 The car if each entry is a string with a single letter, which must
975 be assigned to `self-insert-command' in the global map.
976 The cdr is either a command to be called interactively, a function
977 to be called, or a form to be evaluated.
978 An entry that is just a list with a single string will be interpreted
979 as a descriptive headline that will be added when listing the speed
980 commands in the Help buffer using the `?' speed command."
981 :group
'org-structure
982 :type
'(repeat :value
("k" . ignore
)
983 (choice :value
("k" . ignore
)
984 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
985 (cons :tag
"Letter and Command"
986 (string :tag
"Command letter")
991 (defgroup org-cycle nil
992 "Options concerning visibility cycling in Org-mode."
994 :group
'org-structure
)
996 (defcustom org-cycle-skip-children-state-if-no-children t
997 "Non-nil means skip CHILDREN state in entries that don't have any."
1001 (defcustom org-cycle-max-level nil
1002 "Maximum level which should still be subject to visibility cycling.
1003 Levels higher than this will, for cycling, be treated as text, not a headline.
1004 When `org-odd-levels-only' is set, a value of N in this variable actually
1005 means 2N-1 stars as the limiting headline.
1006 When nil, cycle all levels.
1007 Note that the limiting level of cycling is also influenced by
1008 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
1009 `org-inlinetask-min-level' is, cycling will be limited to levels one less
1013 (const :tag
"No limit" nil
)
1014 (integer :tag
"Maximum level")))
1016 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
1017 "Names of drawers. Drawers are not opened by cycling on the headline above.
1018 Drawers only open with a TAB on the drawer line itself. A drawer looks like
1023 The drawer \"PROPERTIES\" is special for capturing properties through
1026 Drawers can be defined on the per-file basis with a line like:
1028 #+DRAWERS: HIDDEN STATE PROPERTIES"
1029 :group
'org-structure
1031 :type
'(repeat (string :tag
"Drawer Name")))
1033 (defcustom org-hide-block-startup nil
1034 "Non-nil means entering Org-mode will fold all blocks.
1035 This can also be set in on a per-file basis with
1037 #+STARTUP: hideblocks
1038 #+STARTUP: showblocks"
1043 (defcustom org-cycle-global-at-bob nil
1044 "Cycle globally if cursor is at beginning of buffer and not at a headline.
1045 This makes it possible to do global cycling without having to use S-TAB or
1046 \\[universal-argument] TAB. For this special case to work, the first line
1047 of the buffer must not be a headline -- it may be empty or some other text.
1048 When used in this way, `org-cycle-hook' is disabled temporarily to make
1049 sure the cursor stays at the beginning of the buffer. When this option is
1050 nil, don't do anything special at the beginning of the buffer."
1054 (defcustom org-cycle-level-after-item
/entry-creation t
1055 "Non-nil means cycle entry level or item indentation in new empty entries.
1057 When the cursor is at the end of an empty headline, i.e., with only stars
1058 and maybe a TODO keyword, TAB will then switch the entry to become a child,
1059 and then all possible ancestor states, before returning to the original state.
1060 This makes data entry extremely fast: M-RET to create a new headline,
1061 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
1063 When the cursor is at the end of an empty plain list item, one TAB will
1064 make it a subitem, two or more tabs will back up to make this an item
1065 higher up in the item hierarchy."
1069 (defcustom org-cycle-emulate-tab t
1070 "Where should `org-cycle' emulate TAB.
1072 white Only in completely white lines
1073 whitestart Only at the beginning of lines, before the first non-white char
1074 t Everywhere except in headlines
1075 exc-hl-bol Everywhere except at the start of a headline
1076 If TAB is used in a place where it does not emulate TAB, the current subtree
1077 visibility is cycled."
1079 :type
'(choice (const :tag
"Never" nil
)
1080 (const :tag
"Only in completely white lines" white
)
1081 (const :tag
"Before first char in a line" whitestart
)
1082 (const :tag
"Everywhere except in headlines" t
)
1083 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
1086 (defcustom org-cycle-separator-lines
2
1087 "Number of empty lines needed to keep an empty line between collapsed trees.
1088 If you leave an empty line between the end of a subtree and the following
1089 headline, this empty line is hidden when the subtree is folded.
1090 Org-mode will leave (exactly) one empty line visible if the number of
1091 empty lines is equal or larger to the number given in this variable.
1092 So the default 2 means at least 2 empty lines after the end of a subtree
1093 are needed to produce free space between a collapsed subtree and the
1096 If the number is negative, and the number of empty lines is at least -N,
1097 all empty lines are shown.
1099 Special case: when 0, never leave empty lines in collapsed view."
1102 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
1104 (defcustom org-pre-cycle-hook nil
1105 "Hook that is run before visibility cycling is happening.
1106 The function(s) in this hook must accept a single argument which indicates
1107 the new state that will be set right after running this hook. The
1108 argument is a symbol. Before a global state change, it can have the values
1109 `overview', `content', or `all'. Before a local state change, it can have
1110 the values `folded', `children', or `subtree'."
1114 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
1115 org-cycle-hide-drawers
1116 org-cycle-hide-inline-tasks
1117 org-cycle-show-empty-lines
1118 org-optimize-window-after-visibility-change
)
1119 "Hook that is run after `org-cycle' has changed the buffer visibility.
1120 The function(s) in this hook must accept a single argument which indicates
1121 the new state that was set by the most recent `org-cycle' command. The
1122 argument is a symbol. After a global state change, it can have the values
1123 `overview', `contents', or `all'. After a local state change, it can have
1124 the values `folded', `children', or `subtree'."
1128 (defgroup org-edit-structure nil
1129 "Options concerning structure editing in Org-mode."
1130 :tag
"Org Edit Structure"
1131 :group
'org-structure
)
1133 (defcustom org-odd-levels-only nil
1134 "Non-nil means skip even levels and only use odd levels for the outline.
1135 This has the effect that two stars are being added/taken away in
1136 promotion/demotion commands. It also influences how levels are
1137 handled by the exporters.
1138 Changing it requires restart of `font-lock-mode' to become effective
1139 for fontification also in regions already fontified.
1140 You may also set this on a per-file basis by adding one of the following
1141 lines to the buffer:
1145 :group
'org-edit-structure
1146 :group
'org-appearance
1149 (defcustom org-adapt-indentation t
1150 "Non-nil means adapt indentation to outline node level.
1152 When this variable is set, Org assumes that you write outlines by
1153 indenting text in each node to align with the headline (after the stars).
1154 The following issues are influenced by this variable:
1156 - When this is set and the *entire* text in an entry is indented, the
1157 indentation is increased by one space in a demotion command, and
1158 decreased by one in a promotion command. If any line in the entry
1159 body starts with text at column 0, indentation is not changed at all.
1161 - Property drawers and planning information is inserted indented when
1162 this variable s set. When nil, they will not be indented.
1164 - TAB indents a line relative to context. The lines below a headline
1165 will be indented when this variable is set.
1167 Note that this is all about true indentation, by adding and removing
1168 space characters. See also `org-indent.el' which does level-dependent
1169 indentation in a virtual way, i.e. at display time in Emacs."
1170 :group
'org-edit-structure
1173 (defcustom org-special-ctrl-a
/e nil
1174 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1176 When t, `C-a' will bring back the cursor to the beginning of the
1177 headline text, i.e. after the stars and after a possible TODO
1178 keyword. In an item, this will be the position after bullet and
1179 check-box, if any. When the cursor is already at that position,
1180 another `C-a' will bring it to the beginning of the line.
1182 `C-e' will jump to the end of the headline, ignoring the presence
1183 of tags in the headline. A second `C-e' will then jump to the
1184 true end of the line, after any tags. This also means that, when
1185 this variable is non-nil, `C-e' also will never jump beyond the
1186 end of the heading of a folded section, i.e. not after the
1189 When set to the symbol `reversed', the first `C-a' or `C-e' works
1190 normally, going to the true line boundary first. Only a directly
1191 following, identical keypress will bring the cursor to the
1194 This may also be a cons cell where the behavior for `C-a' and
1195 `C-e' is set separately."
1196 :group
'org-edit-structure
1198 (const :tag
"off" nil
)
1199 (const :tag
"on: after stars/bullet and before tags first" t
)
1200 (const :tag
"reversed: true line boundary first" reversed
)
1201 (cons :tag
"Set C-a and C-e separately"
1202 (choice :tag
"Special C-a"
1203 (const :tag
"off" nil
)
1204 (const :tag
"on: after stars/bullet first" t
)
1205 (const :tag
"reversed: before stars/bullet first" reversed
))
1206 (choice :tag
"Special C-e"
1207 (const :tag
"off" nil
)
1208 (const :tag
"on: before tags first" t
)
1209 (const :tag
"reversed: after tags first" reversed
)))))
1210 (if (fboundp 'defvaralias
)
1211 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
1213 (defcustom org-special-ctrl-k nil
1214 "Non-nil means `C-k' will behave specially in headlines.
1215 When nil, `C-k' will call the default `kill-line' command.
1216 When t, the following will happen while the cursor is in the headline:
1218 - When the cursor is at the beginning of a headline, kill the entire
1219 line and possible the folded subtree below the line.
1220 - When in the middle of the headline text, kill the headline up to the tags.
1221 - When after the headline text, kill the tags."
1222 :group
'org-edit-structure
1225 (defcustom org-ctrl-k-protect-subtree nil
1226 "Non-nil means, do not delete a hidden subtree with C-k.
1227 When set to the symbol `error', simply throw an error when C-k is
1228 used to kill (part-of) a headline that has hidden text behind it.
1229 Any other non-nil value will result in a query to the user, if it is
1230 OK to kill that hidden subtree. When nil, kill without remorse."
1231 :group
'org-edit-structure
1234 (const :tag
"Do not protect hidden subtrees" nil
)
1235 (const :tag
"Protect hidden subtrees with a security query" t
)
1236 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1238 (defcustom org-catch-invisible-edits nil
1239 "Check if in invisible region before inserting or deleting a character.
1242 nil Do not check, so just do invisible edits.
1243 error Throw an error and do nothing.
1244 show Make point visible, and do the requested edit.
1245 show-and-error Make point visible, then throw an error and abort the edit.
1246 smart Make point visible, and do insertion/deletion if it is
1247 adjacent to visible text and the change feels predictable.
1248 Never delete a previously invisible character or add in the
1249 middle or right after an invisible region. Basically, this
1250 allows insertion and backward-delete right before ellipses.
1251 FIXME: maybe in this case we should not even show?"
1252 :group
'org-edit-structure
1255 (const :tag
"Do not check" nil
)
1256 (const :tag
"Throw error when trying to edit" error
)
1257 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1258 (const :tag
"Show invisible part and do the edit" show
)
1259 (const :tag
"Be smart and do the right thing" smart
)))
1261 (defcustom org-yank-folded-subtrees t
1262 "Non-nil means when yanking subtrees, fold them.
1263 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1264 it starts with a heading and all other headings in it are either children
1265 or siblings, then fold all the subtrees. However, do this only if no
1266 text after the yank would be swallowed into a folded tree by this action."
1267 :group
'org-edit-structure
1270 (defcustom org-yank-adjusted-subtrees nil
1271 "Non-nil means when yanking subtrees, adjust the level.
1272 With this setting, `org-paste-subtree' is used to insert the subtree, see
1273 this function for details."
1274 :group
'org-edit-structure
1277 (defcustom org-M-RET-may-split-line
'((default . t
))
1278 "Non-nil means M-RET will split the line at the cursor position.
1279 When nil, it will go to the end of the line before making a
1281 You may also set this option in a different way for different
1282 contexts. Valid contexts are:
1284 headline when creating a new headline
1285 item when creating a new item
1286 table in a table field
1287 default the value to be used for all contexts not explicitly
1289 :group
'org-structure
1292 (const :tag
"Always" t
)
1293 (const :tag
"Never" nil
)
1294 (repeat :greedy t
:tag
"Individual contexts"
1296 (choice :tag
"Context"
1304 (defcustom org-insert-heading-respect-content nil
1305 "Non-nil means insert new headings after the current subtree.
1306 When nil, the new heading is created directly after the current line.
1307 The commands \\[org-insert-heading-respect-content] and
1308 \\[org-insert-todo-heading-respect-content] turn this variable on
1309 for the duration of the command."
1310 :group
'org-structure
1313 (defcustom org-blank-before-new-entry
'((heading . auto
)
1314 (plain-list-item . auto
))
1315 "Should `org-insert-heading' leave a blank line before new heading/item?
1316 The value is an alist, with `heading' and `plain-list-item' as CAR,
1317 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1318 which case Org will look at the surrounding headings/items and try to
1319 make an intelligent decision whether to insert a blank line or not.
1321 For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
1322 set, the setting here is ignored and no empty line is inserted, to avoid
1323 breaking the list structure."
1324 :group
'org-edit-structure
1326 (cons (const heading
)
1327 (choice (const :tag
"Never" nil
)
1328 (const :tag
"Always" t
)
1329 (const :tag
"Auto" auto
)))
1330 (cons (const plain-list-item
)
1331 (choice (const :tag
"Never" nil
)
1332 (const :tag
"Always" t
)
1333 (const :tag
"Auto" auto
)))))
1335 (defcustom org-insert-heading-hook nil
1336 "Hook being run after inserting a new heading."
1337 :group
'org-edit-structure
1340 (defcustom org-enable-fixed-width-editor t
1341 "Non-nil means lines starting with \":\" are treated as fixed-width.
1342 This currently only means they are never auto-wrapped.
1343 When nil, such lines will be treated like ordinary lines.
1344 See also the QUOTE keyword."
1345 :group
'org-edit-structure
1348 (defcustom org-goto-auto-isearch t
1349 "Non-nil means typing characters in `org-goto' starts incremental search.
1350 When nil, you can use these keybindings to navigate the buffer:
1352 q Quit the org-goto interface
1353 n Go to the next visible heading
1354 p Go to the previous visible heading
1355 f Go one heading forward on same level
1356 b Go one heading backward on same level
1357 u Go one heading up"
1358 :group
'org-edit-structure
1361 (defgroup org-sparse-trees nil
1362 "Options concerning sparse trees in Org-mode."
1363 :tag
"Org Sparse Trees"
1364 :group
'org-structure
)
1366 (defcustom org-highlight-sparse-tree-matches t
1367 "Non-nil means highlight all matches that define a sparse tree.
1368 The highlights will automatically disappear the next time the buffer is
1369 changed by an edit command."
1370 :group
'org-sparse-trees
1373 (defcustom org-remove-highlights-with-change t
1374 "Non-nil means any change to the buffer will remove temporary highlights.
1375 Such highlights are created by `org-occur' and `org-clock-display'.
1376 When nil, `C-c C-c needs to be used to get rid of the highlights.
1377 The highlights created by `org-preview-latex-fragment' always need
1378 `C-c C-c' to be removed."
1379 :group
'org-sparse-trees
1384 (defcustom org-occur-hook
'(org-first-headline-recenter)
1385 "Hook that is run after `org-occur' has constructed a sparse tree.
1386 This can be used to recenter the window to show as much of the structure
1388 :group
'org-sparse-trees
1391 (defgroup org-imenu-and-speedbar nil
1392 "Options concerning imenu and speedbar in Org-mode."
1393 :tag
"Org Imenu and Speedbar"
1394 :group
'org-structure
)
1396 (defcustom org-imenu-depth
2
1397 "The maximum level for Imenu access to Org-mode headlines.
1398 This also applied for speedbar access."
1399 :group
'org-imenu-and-speedbar
1402 (defgroup org-table nil
1403 "Options concerning tables in Org-mode."
1407 (defcustom org-enable-table-editor
'optimized
1408 "Non-nil means lines starting with \"|\" are handled by the table editor.
1409 When nil, such lines will be treated like ordinary lines.
1411 When equal to the symbol `optimized', the table editor will be optimized to
1413 - Automatic overwrite mode in front of whitespace in table fields.
1414 This makes the structure of the table stay in tact as long as the edited
1415 field does not exceed the column width.
1416 - Minimize the number of realigns. Normally, the table is aligned each time
1417 TAB or RET are pressed to move to another field. With optimization this
1418 happens only if changes to a field might have changed the column width.
1419 Optimization requires replacing the functions `self-insert-command',
1420 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1421 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1422 very good at guessing when a re-align will be necessary, but you can always
1423 force one with \\[org-ctrl-c-ctrl-c].
1425 If you would like to use the optimized version in Org-mode, but the
1426 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1428 This variable can be used to turn on and off the table editor during a session,
1429 but in order to toggle optimization, a restart is required.
1431 See also the variable `org-table-auto-blank-field'."
1434 (const :tag
"off" nil
)
1436 (const :tag
"on, optimized" optimized
)))
1438 (defcustom org-self-insert-cluster-for-undo
(or (featurep 'xemacs
)
1439 (version<= emacs-version
"24.1"))
1440 "Non-nil means cluster self-insert commands for undo when possible.
1441 If this is set, then, like in the Emacs command loop, 20 consecutive
1442 characters will be undone together.
1443 This is configurable, because there is some impact on typing performance."
1447 (defcustom org-table-tab-recognizes-table.el t
1448 "Non-nil means TAB will automatically notice a table.el table.
1449 When it sees such a table, it moves point into it and - if necessary -
1450 calls `table-recognize-table'."
1451 :group
'org-table-editing
1454 (defgroup org-link nil
1455 "Options concerning links in Org-mode."
1459 (defvar org-link-abbrev-alist-local nil
1460 "Buffer-local version of `org-link-abbrev-alist', which see.
1461 The value of this is taken from the #+LINK lines.")
1462 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1464 (defcustom org-link-abbrev-alist nil
1465 "Alist of link abbreviations.
1466 The car of each element is a string, to be replaced at the start of a link.
1467 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1468 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1470 [[linkkey:tag][description]]
1472 The 'linkkey' must be a word word, starting with a letter, followed
1473 by letters, numbers, '-' or '_'.
1475 If REPLACE is a string, the tag will simply be appended to create the link.
1476 If the string contains \"%s\", the tag will be inserted there. If the string
1477 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1478 at that point (see the function `url-hexify-string'). If the string contains
1479 the specifier \"%(my-function)\", then the custom function `my-function' will
1480 be invoked: this function takes the tag as its only argument and must return
1483 REPLACE may also be a function that will be called with the tag as the
1484 only argument to create the link, which should be returned as a string.
1486 See the manual for examples."
1490 (string :tag
"Protocol")
1492 (string :tag
"Format")
1495 (defcustom org-descriptive-links t
1496 "Non-nil means Org will display descriptive links.
1497 E.g. [[http://orgmode.org][Org website]] will be displayed as
1498 \"Org Website\", hiding the link itself and just displaying its
1499 description. When set to `nil', Org will display the full links
1502 You can interactively set the value of this variable by calling
1503 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1507 (defcustom org-link-file-path-type
'adaptive
1508 "How the path name in file links should be stored.
1511 relative Relative to the current directory, i.e. the directory of the file
1512 into which the link is being inserted.
1513 absolute Absolute path, if possible with ~ for home directory.
1514 noabbrev Absolute path, no abbreviation of home directory.
1515 adaptive Use relative path for files in the current directory and sub-
1516 directories of it. For other files, use an absolute path."
1524 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1525 "Types of links that should be activated in Org-mode files.
1526 This is a list of symbols, each leading to the activation of a certain link
1527 type. In principle, it does not hurt to turn on most link types - there may
1528 be a small gain when turning off unused link types. The types are:
1530 bracket The recommended [[link][description]] or [[link]] links with hiding.
1531 angle Links in angular brackets that may contain whitespace like
1532 <bbdb:Carsten Dominik>.
1533 plain Plain links in normal text, no whitespace, like http://google.com.
1534 radio Text that is matched by a radio target, see manual for details.
1535 tag Tag settings in a headline (link to tag search).
1536 date Time stamps (link to calendar).
1537 footnote Footnote labels.
1539 Changing this variable requires a restart of Emacs to become effective."
1541 :type
'(set :greedy t
1542 (const :tag
"Double bracket links" bracket
)
1543 (const :tag
"Angular bracket links" angle
)
1544 (const :tag
"Plain text links" plain
)
1545 (const :tag
"Radio target matches" radio
)
1546 (const :tag
"Tags" tag
)
1547 (const :tag
"Timestamps" date
)
1548 (const :tag
"Footnotes" footnote
)))
1550 (defcustom org-make-link-description-function nil
1551 "Function to use for generating link descriptions from links.
1552 When nil, the link location will be used. This function must take
1553 two parameters: the first one is the link, the second one is the
1554 description generated by `org-insert-link'. The function should
1555 return the description to use."
1559 (defgroup org-link-store nil
1560 "Options concerning storing links in Org-mode."
1561 :tag
"Org Store Link"
1564 (defcustom org-url-hexify-p t
1565 "When non-nil, hexify URL when creating a link."
1568 :group
'org-link-store
)
1570 (defcustom org-email-link-description-format
"Email %c: %.30s"
1571 "Format of the description part of a link to an email or usenet message.
1572 The following %-escapes will be replaced by corresponding information:
1574 %F full \"From\" field
1575 %f name, taken from \"From\" field, address if no name
1576 %T full \"To\" field
1577 %t first name in \"To\" field, address if no name
1578 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1579 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1584 You may use normal field width specification between the % and the letter.
1585 This is for example useful to limit the length of the subject.
1587 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1588 :group
'org-link-store
1591 (defcustom org-from-is-user-regexp
1593 (when (and user-mail-address
(not (string= user-mail-address
"")))
1594 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1595 (when (and user-full-name
(not (string= user-full-name
"")))
1596 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1597 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1598 "Regexp matched against the \"From:\" header of an email or usenet message.
1599 It should match if the message is from the user him/herself."
1600 :group
'org-link-store
1603 (defcustom org-context-in-file-links t
1604 "Non-nil means file links from `org-store-link' contain context.
1605 A search string will be added to the file name with :: as separator and
1606 used to find the context when the link is activated by the command
1607 `org-open-at-point'. When this option is t, the entire active region
1608 will be placed in the search string of the file link. If set to a
1609 positive integer, only the first n lines of context will be stored.
1611 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1612 negates this setting for the duration of the command."
1613 :group
'org-link-store
1614 :type
'(choice boolean integer
))
1616 (defcustom org-keep-stored-link-after-insertion nil
1617 "Non-nil means keep link in list for entire session.
1619 The command `org-store-link' adds a link pointing to the current
1620 location to an internal list. These links accumulate during a session.
1621 The command `org-insert-link' can be used to insert links into any
1622 Org-mode file (offering completion for all stored links). When this
1623 option is nil, every link which has been inserted once using \\[org-insert-link]
1624 will be removed from the list, to make completing the unused links
1626 :group
'org-link-store
1629 (defgroup org-link-follow nil
1630 "Options concerning following links in Org-mode."
1631 :tag
"Org Follow Link"
1634 (defcustom org-link-translation-function nil
1635 "Function to translate links with different syntax to Org syntax.
1636 This can be used to translate links created for example by the Planner
1637 or emacs-wiki packages to Org syntax.
1638 The function must accept two parameters, a TYPE containing the link
1639 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1640 which is everything after the link protocol. It should return a cons
1641 with possibly modified values of type and path.
1642 Org contains a function for this, so if you set this variable to
1643 `org-translate-link-from-planner', you should be able follow many
1644 links created by planner."
1645 :group
'org-link-follow
1648 (defcustom org-follow-link-hook nil
1649 "Hook that is run after a link has been followed."
1650 :group
'org-link-follow
1653 (defcustom org-tab-follows-link nil
1654 "Non-nil means on links TAB will follow the link.
1655 Needs to be set before org.el is loaded.
1656 This really should not be used, it does not make sense, and the
1657 implementation is bad."
1658 :group
'org-link-follow
1661 (defcustom org-return-follows-link nil
1662 "Non-nil means on links RET will follow the link.
1663 In tables, the special behavior of RET has precedence."
1664 :group
'org-link-follow
1667 (defcustom org-mouse-1-follows-link
1668 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1669 "Non-nil means mouse-1 on a link will follow the link.
1670 A longer mouse click will still set point. Does not work on XEmacs.
1671 Needs to be set before org.el is loaded."
1672 :group
'org-link-follow
1675 (defcustom org-mark-ring-length
4
1676 "Number of different positions to be recorded in the ring.
1677 Changing this requires a restart of Emacs to work correctly."
1678 :group
'org-link-follow
1681 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1682 "Non-nil means internal links in Org files must exactly match a headline.
1683 When nil, the link search tries to match a phrase with all words
1684 in the search text."
1685 :group
'org-link-follow
1688 (const :tag
"Use fuzzy text search" nil
)
1689 (const :tag
"Match only exact headline" t
)
1690 (const :tag
"Match exact headline or query to create it"
1693 (defcustom org-link-frame-setup
1694 '((vm . vm-visit-folder-other-frame
)
1695 (vm-imap . vm-visit-imap-folder-other-frame
)
1696 (gnus . org-gnus-no-new-news
)
1697 (file . find-file-other-window
)
1698 (wl . wl-other-frame
))
1699 "Setup the frame configuration for following links.
1700 When following a link with Emacs, it may often be useful to display
1701 this link in another window or frame. This variable can be used to
1702 set this up for the different types of links.
1705 `vm-visit-folder-other-window'
1706 `vm-visit-folder-other-frame'
1707 For Gnus, use any of
1710 `org-gnus-no-new-news'
1711 For FILE, use any of
1713 `find-file-other-window'
1714 `find-file-other-frame'
1715 For Wanderlust use any of
1718 For the calendar, use the variable `calendar-setup'.
1719 For BBDB, it is currently only possible to display the matches in
1721 :group
'org-link-follow
1725 (const vm-visit-folder
)
1726 (const vm-visit-folder-other-window
)
1727 (const vm-visit-folder-other-frame
)))
1731 (const gnus-other-frame
)
1732 (const org-gnus-no-new-news
)))
1736 (const find-file-other-window
)
1737 (const find-file-other-frame
)))
1741 (const wl-other-frame
)))))
1743 (defcustom org-display-internal-link-with-indirect-buffer nil
1744 "Non-nil means use indirect buffer to display infile links.
1745 Activating internal links (from one location in a file to another location
1746 in the same file) normally just jumps to the location. When the link is
1747 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1749 another window. When this option is set, the other window actually displays
1750 an indirect buffer clone of the current buffer, to avoid any visibility
1751 changes to the current buffer."
1752 :group
'org-link-follow
1755 (defcustom org-open-non-existing-files nil
1756 "Non-nil means `org-open-file' will open non-existing files.
1757 When nil, an error will be generated.
1758 This variable applies only to external applications because they
1759 might choke on non-existing files. If the link is to a file that
1760 will be opened in Emacs, the variable is ignored."
1761 :group
'org-link-follow
1764 (defcustom org-open-directory-means-index-dot-org nil
1765 "Non-nil means a link to a directory really means to index.org.
1766 When nil, following a directory link will run dired or open a finder/explorer
1767 window on that directory."
1768 :group
'org-link-follow
1771 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1772 "Function and arguments to call for following mailto links.
1773 This is a list with the first element being a Lisp function, and the
1774 remaining elements being arguments to the function. In string arguments,
1775 %a will be replaced by the address, and %s will be replaced by the subject
1776 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1777 :group
'org-link-follow
1779 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1780 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1781 (const :tag
"message-mail" (message-mail "%a" "%s"))
1782 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1784 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1785 "Non-nil means ask for confirmation before executing shell links.
1786 Shell links can be dangerous: just think about a link
1788 [[shell:rm -rf ~/*][Google Search]]
1790 This link would show up in your Org-mode document as \"Google Search\",
1791 but really it would remove your entire home directory.
1792 Therefore we advise against setting this variable to nil.
1793 Just change it to `y-or-n-p' if you want to confirm with a
1794 single keystroke rather than having to type \"yes\"."
1795 :group
'org-link-follow
1797 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1798 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1799 (const :tag
"no confirmation (dangerous)" nil
)))
1800 (put 'org-confirm-shell-link-function
1801 'safe-local-variable
1802 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1804 (defcustom org-confirm-shell-link-not-regexp
""
1805 "A regexp to skip confirmation for shell links."
1806 :group
'org-link-follow
1810 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1811 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1812 Elisp links can be dangerous: just think about a link
1814 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1816 This link would show up in your Org-mode document as \"Google Search\",
1817 but really it would remove your entire home directory.
1818 Therefore we advise against setting this variable to nil.
1819 Just change it to `y-or-n-p' if you want to confirm with a
1820 single keystroke rather than having to type \"yes\"."
1821 :group
'org-link-follow
1823 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1824 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1825 (const :tag
"no confirmation (dangerous)" nil
)))
1826 (put 'org-confirm-shell-link-function
1827 'safe-local-variable
1828 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1830 (defcustom org-confirm-elisp-link-not-regexp
""
1831 "A regexp to skip confirmation for Elisp links."
1832 :group
'org-link-follow
1836 (defconst org-file-apps-defaults-gnu
1840 "Default file applications on a UNIX or GNU/Linux system.
1841 See `org-file-apps'.")
1843 (defconst org-file-apps-defaults-macosx
1846 (system .
"open %s")
1848 ("eps.gz" .
"gv %s")
1850 ("fig" .
"xfig %s"))
1851 "Default file applications on a MacOS X system.
1852 The system \"open\" is known as a default, but we use X11 applications
1853 for some files for which the OS does not have a good default.
1854 See `org-file-apps'.")
1856 (defconst org-file-apps-defaults-windowsnt
1860 (list (if (featurep 'xemacs
)
1861 'mswindows-shell-execute
1865 (list (if (featurep 'xemacs
)
1866 'mswindows-shell-execute
1869 "Default file applications on a Windows NT system.
1870 The system \"open\" is used for most files.
1871 See `org-file-apps'.")
1873 (defcustom org-file-apps
1876 ("\\.mm\\'" . default
)
1877 ("\\.x?html?\\'" . default
)
1878 ("\\.pdf\\'" . default
)
1880 "External applications for opening `file:path' items in a document.
1881 Org-mode uses system defaults for different file types, but
1882 you can use this variable to set the application for a given file
1883 extension. The entries in this list are cons cells where the car identifies
1884 files and the cdr the corresponding command. Possible values for the
1886 \"string\" A string as a file identifier can be interpreted in different
1887 ways, depending on its contents:
1889 - Alphanumeric characters only:
1890 Match links with this file extension.
1891 Example: (\"pdf\" . \"evince %s\")
1892 to open PDFs with evince.
1894 - Regular expression: Match links where the
1895 filename matches the regexp. If you want to
1896 use groups here, use shy groups.
1898 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1899 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1900 to open *.html and *.xhtml with firefox.
1902 - Regular expression which contains (non-shy) groups:
1903 Match links where the whole link, including \"::\", and
1904 anything after that, matches the regexp.
1905 In a custom command string, %1, %2, etc. are replaced with
1906 the parts of the link that were matched by the groups.
1907 For backwards compatibility, if a command string is given
1908 that does not use any of the group matches, this case is
1909 handled identically to the second one (i.e. match against
1911 In a custom lisp form, you can access the group matches with
1912 (match-string n link).
1914 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1915 to open [[file:document.pdf::5]] with evince at page 5.
1917 `directory' Matches a directory
1918 `remote' Matches a remote file, accessible through tramp or efs.
1919 Remote files most likely should be visited through Emacs
1920 because external applications cannot handle such paths.
1921 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1922 so all files Emacs knows how to handle. Using this with
1923 command `emacs' will open most files in Emacs. Beware that this
1924 will also open html files inside Emacs, unless you add
1925 (\"html\" . default) to the list as well.
1926 t Default for files not matched by any of the other options.
1927 `system' The system command to open files, like `open' on Windows
1928 and Mac OS X, and mailcap under GNU/Linux. This is the command
1929 that will be selected if you call `C-c C-o' with a double
1930 \\[universal-argument] \\[universal-argument] prefix.
1932 Possible values for the command are:
1933 `emacs' The file will be visited by the current Emacs process.
1934 `default' Use the default application for this file type, which is the
1935 association for t in the list, most likely in the system-specific
1937 This can be used to overrule an unwanted setting in the
1938 system-specific variable.
1939 `system' Use the system command for opening files, like \"open\".
1940 This command is specified by the entry whose car is `system'.
1941 Most likely, the system-specific version of this variable
1942 does define this command, but you can overrule/replace it
1944 string A command to be executed by a shell; %s will be replaced
1945 by the path to the file.
1946 sexp A Lisp form which will be evaluated. The file path will
1947 be available in the Lisp variable `file'.
1948 For more examples, see the system specific constants
1949 `org-file-apps-defaults-macosx'
1950 `org-file-apps-defaults-windowsnt'
1951 `org-file-apps-defaults-gnu'."
1952 :group
'org-link-follow
1954 (cons (choice :value
""
1955 (string :tag
"Extension")
1956 (const :tag
"System command to open files" system
)
1957 (const :tag
"Default for unrecognized files" t
)
1958 (const :tag
"Remote file" remote
)
1959 (const :tag
"Links to a directory" directory
)
1960 (const :tag
"Any files that have Emacs modes"
1963 (const :tag
"Visit with Emacs" emacs
)
1964 (const :tag
"Use default" default
)
1965 (const :tag
"Use the system command" system
)
1966 (string :tag
"Command")
1967 (sexp :tag
"Lisp form")))))
1969 (defcustom org-doi-server-url
"http://dx.doi.org/"
1970 "The URL of the DOI server."
1973 :group
'org-link-follow
)
1975 (defgroup org-refile nil
1976 "Options concerning refiling entries in Org-mode."
1980 (defcustom org-directory
"~/org"
1981 "Directory with org files.
1982 This is just a default location to look for Org files. There is no need
1983 at all to put your files into this directory. It is only used in the
1984 following situations:
1986 1. When a capture template specifies a target file that is not an
1987 absolute path. The path will then be interpreted relative to
1989 2. When a capture note is filed away in an interactive way (when exiting the
1990 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1991 with `org-directory' as the default path."
1993 :group
'org-remember
1997 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1998 "Default target for storing notes.
1999 Used as a fall back file for org-remember.el and org-capture.el, for
2000 templates that do not specify a target file."
2002 :group
'org-remember
2005 (const :tag
"Default from remember-data-file" nil
)
2008 (defcustom org-goto-interface
'outline
2009 "The default interface to be used for `org-goto'.
2011 outline The interface shows an outline of the relevant file
2012 and the correct heading is found by moving through
2013 the outline or by searching with incremental search.
2014 outline-path-completion Headlines in the current buffer are offered via
2015 completion. This is the interface also used by
2016 the refile command."
2019 (const :tag
"Outline" outline
)
2020 (const :tag
"Outline-path-completion" outline-path-completion
)))
2022 (defcustom org-goto-max-level
5
2023 "Maximum target level when running `org-goto' with refile interface."
2027 (defcustom org-reverse-note-order nil
2028 "Non-nil means store new notes at the beginning of a file or entry.
2029 When nil, new notes will be filed to the end of a file or entry.
2030 This can also be a list with cons cells of regular expressions that
2031 are matched against file names, and values."
2032 :group
'org-remember
2036 (const :tag
"Reverse always" t
)
2037 (const :tag
"Reverse never" nil
)
2038 (repeat :tag
"By file name regexp"
2039 (cons regexp boolean
))))
2041 (defcustom org-log-refile nil
2042 "Information to record when a task is refiled.
2044 Possible values are:
2046 nil Don't add anything
2047 time Add a time stamp to the task
2048 note Prompt for a note and add it with template `org-log-note-headings'
2050 This option can also be set with on a per-file-basis with
2052 #+STARTUP: nologrefile
2053 #+STARTUP: logrefile
2054 #+STARTUP: lognoterefile
2056 You can have local logging settings for a subtree by setting the LOGGING
2057 property to one or more of these keywords.
2059 When bulk-refiling from the agenda, the value `note' is forbidden and
2060 will temporarily be changed to `time'."
2062 :group
'org-progress
2065 (const :tag
"No logging" nil
)
2066 (const :tag
"Record timestamp" time
)
2067 (const :tag
"Record timestamp with note." note
)))
2069 (defcustom org-refile-targets nil
2070 "Targets for refiling entries with \\[org-refile].
2071 This is a list of cons cells. Each cell contains:
2072 - a specification of the files to be considered, either a list of files,
2073 or a symbol whose function or variable value will be used to retrieve
2074 a file name or a list of file names. If you use `org-agenda-files' for
2075 that, all agenda files will be scanned for targets. Nil means consider
2076 headings in the current buffer.
2077 - A specification of how to find candidate refile targets. This may be
2079 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
2080 This tag has to be present in all target headlines, inheritance will
2082 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
2084 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
2085 headlines that are refiling targets.
2086 - a cons cell (:level . N). Any headline of level N is considered a target.
2087 Note that, when `org-odd-levels-only' is set, level corresponds to
2088 order in hierarchy, not to the number of stars.
2089 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
2090 Note that, when `org-odd-levels-only' is set, level corresponds to
2091 order in hierarchy, not to the number of stars.
2093 Each element of this list generates a set of possible targets.
2094 The union of these sets is presented (with completion) to
2095 the user by `org-refile'.
2097 You can set the variable `org-refile-target-verify-function' to a function
2098 to verify each headline found by the simple criteria above.
2100 When this variable is nil, all top-level headlines in the current buffer
2101 are used, equivalent to the value `((nil . (:level . 1))'."
2105 (choice :value org-agenda-files
2106 (const :tag
"All agenda files" org-agenda-files
)
2107 (const :tag
"Current buffer" nil
)
2108 (function) (variable) (file))
2109 (choice :tag
"Identify target headline by"
2110 (cons :tag
"Specific tag" (const :value
:tag
) (string))
2111 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
2112 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
2113 (cons :tag
"Level number" (const :value
:level
) (integer))
2114 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
2116 (defcustom org-refile-target-verify-function nil
2117 "Function to verify if the headline at point should be a refile target.
2118 The function will be called without arguments, with point at the
2119 beginning of the headline. It should return t and leave point
2120 where it is if the headline is a valid target for refiling.
2122 If the target should not be selected, the function must return nil.
2123 In addition to this, it may move point to a place from where the search
2124 should be continued. For example, the function may decide that the entire
2125 subtree of the current entry should be excluded and move point to the end
2130 (defcustom org-refile-use-cache nil
2131 "Non-nil means cache refile targets to speed up the process.
2132 The cache for a particular file will be updated automatically when
2133 the buffer has been killed, or when any of the marker used for flagging
2134 refile targets no longer points at a live buffer.
2135 If you have added new entries to a buffer that might themselves be targets,
2136 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
2137 find that easier, `C-u C-u C-u C-c C-w'."
2142 (defcustom org-refile-use-outline-path nil
2143 "Non-nil means provide refile targets as paths.
2144 So a level 3 headline will be available as level1/level2/level3.
2146 When the value is `file', also include the file name (without directory)
2147 into the path. In this case, you can also stop the completion after
2148 the file name, to get entries inserted as top level in the file.
2150 When `full-file-path', include the full file path."
2153 (const :tag
"Not" nil
)
2154 (const :tag
"Yes" t
)
2155 (const :tag
"Start with file name" file
)
2156 (const :tag
"Start with full file path" full-file-path
)))
2158 (defcustom org-outline-path-complete-in-steps t
2159 "Non-nil means complete the outline path in hierarchical steps.
2160 When Org-mode uses the refile interface to select an outline path
2161 \(see variable `org-refile-use-outline-path'), the completion of
2162 the path can be done is a single go, or if can be done in steps down
2163 the headline hierarchy. Going in steps is probably the best if you
2164 do not use a special completion package like `ido' or `icicles'.
2165 However, when using these packages, going in one step can be very
2166 fast, while still showing the whole path to the entry."
2170 (defcustom org-refile-allow-creating-parent-nodes nil
2171 "Non-nil means allow to create new nodes as refile targets.
2172 New nodes are then created by adding \"/new node name\" to the completion
2173 of an existing node. When the value of this variable is `confirm',
2174 new node creation must be confirmed by the user (recommended)
2175 When nil, the completion must match an existing entry.
2177 Note that, if the new heading is not seen by the criteria
2178 listed in `org-refile-targets', multiple instances of the same
2179 heading would be created by trying again to file under the new
2183 (const :tag
"Never" nil
)
2184 (const :tag
"Always" t
)
2185 (const :tag
"Prompt for confirmation" confirm
)))
2187 (defcustom org-refile-active-region-within-subtree nil
2188 "Non-nil means also refile active region within a subtree.
2190 By default `org-refile' doesn't allow refiling regions if they
2191 don't contain a set of subtrees, but it might be convenient to
2192 do so sometimes: in that case, the first line of the region is
2193 converted to a headline before refiling."
2198 (defgroup org-todo nil
2199 "Options concerning TODO items in Org-mode."
2203 (defgroup org-progress nil
2204 "Options concerning Progress logging in Org-mode."
2208 (defvar org-todo-interpretation-widgets
2209 '((:tag
"Sequence (cycling hits every state)" sequence
)
2210 (:tag
"Type (cycling directly to DONE)" type
))
2211 "The available interpretation symbols for customizing `org-todo-keywords'.
2212 Interested libraries should add to this list.")
2214 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2215 "List of TODO entry keyword sequences and their interpretation.
2216 \\<org-mode-map>This is a list of sequences.
2218 Each sequence starts with a symbol, either `sequence' or `type',
2219 indicating if the keywords should be interpreted as a sequence of
2220 action steps, or as different types of TODO items. The first
2221 keywords are states requiring action - these states will select a headline
2222 for inclusion into the global TODO list Org-mode produces. If one of
2223 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2224 signify that no further action is necessary. If \"|\" is not found,
2225 the last keyword is treated as the only DONE state of the sequence.
2227 The command \\[org-todo] cycles an entry through these states, and one
2228 additional state where no keyword is present. For details about this
2229 cycling, see the manual.
2231 TODO keywords and interpretation can also be set on a per-file basis with
2232 the special #+SEQ_TODO and #+TYP_TODO lines.
2234 Each keyword can optionally specify a character for fast state selection
2235 \(in combination with the variable `org-use-fast-todo-selection')
2236 and specifiers for state change logging, using the same syntax that
2237 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2238 the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2239 indicates to record a time stamp each time this state is selected.
2241 Each keyword may also specify if a timestamp or a note should be
2242 recorded when entering or leaving the state, by adding additional
2243 characters in the parenthesis after the keyword. This looks like this:
2244 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2245 record only the time of the state change. With X and Y being either
2246 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2247 Y when leaving the state if and only if the *target* state does not
2248 define X. You may omit any of the fast-selection key or X or /Y,
2249 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2251 For backward compatibility, this variable may also be just a list
2252 of keywords. In this case the interpretation (sequence or type) will be
2253 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2255 :group
'org-keywords
2257 (repeat :tag
"Old syntax, just keywords"
2258 (string :tag
"Keyword"))
2259 (repeat :tag
"New syntax"
2262 :tag
"Interpretation"
2263 ;;Quick and dirty way to see
2264 ;;`org-todo-interpretations'. This takes the
2265 ;;place of item arguments
2273 org-todo-interpretation-widgets
))
2276 (string :tag
"Keyword"))))))
2278 (defvar org-todo-keywords-1 nil
2279 "All TODO and DONE keywords active in a buffer.")
2280 (make-variable-buffer-local 'org-todo-keywords-1
)
2281 (defvar org-todo-keywords-for-agenda nil
)
2282 (defvar org-done-keywords-for-agenda nil
)
2283 (defvar org-drawers-for-agenda nil
)
2284 (defvar org-todo-keyword-alist-for-agenda nil
)
2285 (defvar org-tag-alist-for-agenda nil
)
2286 (defvar org-agenda-contributing-files nil
)
2287 (defvar org-not-done-keywords nil
)
2288 (make-variable-buffer-local 'org-not-done-keywords
)
2289 (defvar org-done-keywords nil
)
2290 (make-variable-buffer-local 'org-done-keywords
)
2291 (defvar org-todo-heads nil
)
2292 (make-variable-buffer-local 'org-todo-heads
)
2293 (defvar org-todo-sets nil
)
2294 (make-variable-buffer-local 'org-todo-sets
)
2295 (defvar org-todo-log-states nil
)
2296 (make-variable-buffer-local 'org-todo-log-states
)
2297 (defvar org-todo-kwd-alist nil
)
2298 (make-variable-buffer-local 'org-todo-kwd-alist
)
2299 (defvar org-todo-key-alist nil
)
2300 (make-variable-buffer-local 'org-todo-key-alist
)
2301 (defvar org-todo-key-trigger nil
)
2302 (make-variable-buffer-local 'org-todo-key-trigger
)
2304 (defcustom org-todo-interpretation
'sequence
2305 "Controls how TODO keywords are interpreted.
2306 This variable is in principle obsolete and is only used for
2307 backward compatibility, if the interpretation of todo keywords is
2308 not given already in `org-todo-keywords'. See that variable for
2311 :group
'org-keywords
2312 :type
'(choice (const sequence
)
2315 (defcustom org-use-fast-todo-selection t
2316 "Non-nil means use the fast todo selection scheme with C-c C-t.
2317 This variable describes if and under what circumstances the cycling
2318 mechanism for TODO keywords will be replaced by a single-key, direct
2321 When nil, fast selection is never used.
2323 When the symbol `prefix', it will be used when `org-todo' is called
2324 with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
2325 `C-u t' in an agenda buffer.
2327 When t, fast selection is used by default. In this case, the prefix
2328 argument forces cycling instead.
2330 In all cases, the special interface is only used if access keys have
2331 actually been assigned by the user, i.e. if keywords in the configuration
2332 are followed by a letter in parenthesis, like TODO(t)."
2335 (const :tag
"Never" nil
)
2336 (const :tag
"By default" t
)
2337 (const :tag
"Only with C-u C-c C-t" prefix
)))
2339 (defcustom org-provide-todo-statistics t
2340 "Non-nil means update todo statistics after insert and toggle.
2341 ALL-HEADLINES means update todo statistics by including headlines
2342 with no TODO keyword as well, counting them as not done.
2343 A list of TODO keywords means the same, but skip keywords that are
2346 When this is set, todo statistics is updated in the parent of the
2347 current entry each time a todo state is changed."
2350 (const :tag
"Yes, only for TODO entries" t
)
2351 (const :tag
"Yes, including all entries" 'all-headlines
)
2352 (repeat :tag
"Yes, for TODOs in this list"
2353 (string :tag
"TODO keyword"))
2354 (other :tag
"No TODO statistics" nil
)))
2356 (defcustom org-hierarchical-todo-statistics t
2357 "Non-nil means TODO statistics covers just direct children.
2358 When nil, all entries in the subtree are considered.
2359 This has only an effect if `org-provide-todo-statistics' is set.
2360 To set this to nil for only a single subtree, use a COOKIE_DATA
2361 property and include the word \"recursive\" into the value."
2365 (defcustom org-after-todo-state-change-hook nil
2366 "Hook which is run after the state of a TODO item was changed.
2367 The new state (a string with a TODO keyword, or nil) is available in the
2368 Lisp variable `org-state'."
2372 (defvar org-blocker-hook nil
2373 "Hook for functions that are allowed to block a state change.
2375 Functions in this hook should not modify the buffer.
2376 Each function gets as its single argument a property list,
2377 see `org-trigger-hook' for more information about this list.
2379 If any of the functions in this hook returns nil, the state change
2382 (defvar org-trigger-hook nil
2383 "Hook for functions that are triggered by a state change.
2385 Each function gets as its single argument a property list with at
2386 least the following elements:
2388 (:type type-of-change :position pos-at-entry-start
2389 :from old-state :to new-state)
2391 Depending on the type, more properties may be present.
2393 This mechanism is currently implemented for:
2397 :type todo-state-change
2398 :from previous state (keyword as a string), or nil, or a symbol
2399 'todo' or 'done', to indicate the general type of state.
2400 :to new state, like in :from")
2402 (defcustom org-enforce-todo-dependencies nil
2403 "Non-nil means undone TODO entries will block switching the parent to DONE.
2404 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2405 be blocked if any prior sibling is not yet done.
2406 Finally, if the parent is blocked because of ordered siblings of its own,
2407 the child will also be blocked."
2408 :set
(lambda (var val
)
2411 (add-hook 'org-blocker-hook
2412 'org-block-todo-from-children-or-siblings-or-parent
)
2413 (remove-hook 'org-blocker-hook
2414 'org-block-todo-from-children-or-siblings-or-parent
)))
2418 (defcustom org-enforce-todo-checkbox-dependencies nil
2419 "Non-nil means unchecked boxes will block switching the parent to DONE.
2420 When this is nil, checkboxes have no influence on switching TODO states.
2421 When non-nil, you first need to check off all check boxes before the TODO
2422 entry can be switched to DONE.
2423 This variable needs to be set before org.el is loaded, and you need to
2424 restart Emacs after a change to make the change effective. The only way
2425 to change is while Emacs is running is through the customize interface."
2426 :set
(lambda (var val
)
2429 (add-hook 'org-blocker-hook
2430 'org-block-todo-from-checkboxes
)
2431 (remove-hook 'org-blocker-hook
2432 'org-block-todo-from-checkboxes
)))
2436 (defcustom org-treat-insert-todo-heading-as-state-change nil
2437 "Non-nil means inserting a TODO heading is treated as state change.
2438 So when the command \\[org-insert-todo-heading] is used, state change
2439 logging will apply if appropriate. When nil, the new TODO item will
2440 be inserted directly, and no logging will take place."
2444 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2445 "Non-nil means switching TODO states with S-cursor counts as state change.
2446 This is the default behavior. However, setting this to nil allows a
2447 convenient way to select a TODO state and bypass any logging associated
2452 (defcustom org-todo-state-tags-triggers nil
2453 "Tag changes that should be triggered by TODO state changes.
2454 This is a list. Each entry is
2456 (state-change (tag . flag) .......)
2458 State-change can be a string with a state, and empty string to indicate the
2459 state that has no TODO keyword, or it can be one of the symbols `todo'
2460 or `done', meaning any not-done or done state, respectively."
2464 (cons (choice :tag
"When changing to"
2465 (const :tag
"Not-done state" todo
)
2466 (const :tag
"Done state" done
)
2467 (string :tag
"State"))
2469 (cons :tag
"Tag action"
2471 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2473 (defcustom org-log-done nil
2474 "Information to record when a task moves to the DONE state.
2476 Possible values are:
2478 nil Don't add anything, just change the keyword
2479 time Add a time stamp to the task
2480 note Prompt for a note and add it with template `org-log-note-headings'
2482 This option can also be set with on a per-file-basis with
2484 #+STARTUP: nologdone
2486 #+STARTUP: lognotedone
2488 You can have local logging settings for a subtree by setting the LOGGING
2489 property to one or more of these keywords."
2491 :group
'org-progress
2493 (const :tag
"No logging" nil
)
2494 (const :tag
"Record CLOSED timestamp" time
)
2495 (const :tag
"Record CLOSED timestamp with note." note
)))
2497 ;; Normalize old uses of org-log-done.
2499 ((eq org-log-done t
) (setq org-log-done
'time
))
2500 ((and (listp org-log-done
) (memq 'done org-log-done
))
2501 (setq org-log-done
'note
)))
2503 (defcustom org-log-reschedule nil
2504 "Information to record when the scheduling date of a tasks is modified.
2506 Possible values are:
2508 nil Don't add anything, just change the date
2509 time Add a time stamp to the task
2510 note Prompt for a note and add it with template `org-log-note-headings'
2512 This option can also be set with on a per-file-basis with
2514 #+STARTUP: nologreschedule
2515 #+STARTUP: logreschedule
2516 #+STARTUP: lognotereschedule"
2518 :group
'org-progress
2520 (const :tag
"No logging" nil
)
2521 (const :tag
"Record timestamp" time
)
2522 (const :tag
"Record timestamp with note." note
)))
2524 (defcustom org-log-redeadline nil
2525 "Information to record when the deadline date of a tasks is modified.
2527 Possible values are:
2529 nil Don't add anything, just change the date
2530 time Add a time stamp to the task
2531 note Prompt for a note and add it with template `org-log-note-headings'
2533 This option can also be set with on a per-file-basis with
2535 #+STARTUP: nologredeadline
2536 #+STARTUP: logredeadline
2537 #+STARTUP: lognoteredeadline
2539 You can have local logging settings for a subtree by setting the LOGGING
2540 property to one or more of these keywords."
2542 :group
'org-progress
2544 (const :tag
"No logging" nil
)
2545 (const :tag
"Record timestamp" time
)
2546 (const :tag
"Record timestamp with note." note
)))
2548 (defcustom org-log-note-clock-out nil
2549 "Non-nil means record a note when clocking out of an item.
2550 This can also be configured on a per-file basis by adding one of
2551 the following lines anywhere in the buffer:
2553 #+STARTUP: lognoteclock-out
2554 #+STARTUP: nolognoteclock-out"
2556 :group
'org-progress
2559 (defcustom org-log-done-with-time t
2560 "Non-nil means the CLOSED time stamp will contain date and time.
2561 When nil, only the date will be recorded."
2562 :group
'org-progress
2565 (defcustom org-log-note-headings
2566 '((done .
"CLOSING NOTE %t")
2567 (state .
"State %-12s from %-12S %t")
2568 (note .
"Note taken on %t")
2569 (reschedule .
"Rescheduled from %S on %t")
2570 (delschedule .
"Not scheduled, was %S on %t")
2571 (redeadline .
"New deadline from %S on %t")
2572 (deldeadline .
"Removed deadline, was %S on %t")
2573 (refile .
"Refiled on %t")
2575 "Headings for notes added to entries.
2576 The value is an alist, with the car being a symbol indicating the note
2577 context, and the cdr is the heading to be used. The heading may also be the
2579 %t in the heading will be replaced by a time stamp.
2580 %T will be an active time stamp instead the default inactive one
2581 %d will be replaced by a short-format time stamp.
2582 %D will be replaced by an active short-format time stamp.
2583 %s will be replaced by the new TODO state, in double quotes.
2584 %S will be replaced by the old TODO state, in double quotes.
2585 %u will be replaced by the user name.
2586 %U will be replaced by the full user name.
2588 In fact, it is not a good idea to change the `state' entry, because
2589 agenda log mode depends on the format of these entries."
2591 :group
'org-progress
2592 :type
'(list :greedy t
2593 (cons (const :tag
"Heading when closing an item" done
) string
)
2595 "Heading when changing todo state (todo sequence only)"
2597 (cons (const :tag
"Heading when just taking a note" note
) string
)
2598 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2599 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2600 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2601 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2602 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2603 (cons (const :tag
"Heading when refiling" refile
) string
)))
2605 (unless (assq 'note org-log-note-headings
)
2606 (push '(note .
"%t") org-log-note-headings
))
2608 (defcustom org-log-into-drawer nil
2609 "Non-nil means insert state change notes and time stamps into a drawer.
2610 When nil, state changes notes will be inserted after the headline and
2611 any scheduling and clock lines, but not inside a drawer.
2613 The value of this variable should be the name of the drawer to use.
2614 LOGBOOK is proposed as the default drawer for this purpose, you can
2615 also set this to a string to define the drawer of your choice.
2617 A value of t is also allowed, representing \"LOGBOOK\".
2619 A value of t or nil can also be set with on a per-file-basis with
2621 #+STARTUP: logdrawer
2622 #+STARTUP: nologdrawer
2624 If this variable is set, `org-log-state-notes-insert-after-drawers'
2627 You can set the property LOG_INTO_DRAWER to overrule this setting for
2630 :group
'org-progress
2632 (const :tag
"Not into a drawer" nil
)
2633 (const :tag
"LOGBOOK" t
)
2634 (string :tag
"Other")))
2636 (if (fboundp 'defvaralias
)
2637 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2639 (defun org-log-into-drawer ()
2640 "Return the value of `org-log-into-drawer', but let properties overrule.
2641 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2642 used instead of the default value."
2643 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit t
)))
2645 ((not p
) org-log-into-drawer
)
2646 ((equal p
"nil") nil
)
2647 ((equal p
"t") "LOGBOOK")
2650 (defcustom org-log-state-notes-insert-after-drawers nil
2651 "Non-nil means insert state change notes after any drawers in entry.
2652 Only the drawers that *immediately* follow the headline and the
2653 deadline/scheduled line are skipped.
2654 When nil, insert notes right after the heading and perhaps the line
2655 with deadline/scheduling if present.
2657 This variable will have no effect if `org-log-into-drawer' is
2660 :group
'org-progress
2663 (defcustom org-log-states-order-reversed t
2664 "Non-nil means the latest state note will be directly after heading.
2665 When nil, the state change notes will be ordered according to time.
2667 This option can also be set with on a per-file-basis with
2669 #+STARTUP: logstatesreversed
2670 #+STARTUP: nologstatesreversed"
2672 :group
'org-progress
2675 (defcustom org-todo-repeat-to-state nil
2676 "The TODO state to which a repeater should return the repeating task.
2677 By default this is the first task in a TODO sequence, or the previous state
2678 in a TODO_TYP set. But you can specify another task here.
2679 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2682 :type
'(choice (const :tag
"Head of sequence" nil
)
2683 (string :tag
"Specific state")))
2685 (defcustom org-log-repeat
'time
2686 "Non-nil means record moving through the DONE state when triggering repeat.
2687 An auto-repeating task is immediately switched back to TODO when
2688 marked DONE. If you are not logging state changes (by adding \"@\"
2689 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2690 record a closing note, there will be no record of the task moving
2691 through DONE. This variable forces taking a note anyway.
2693 nil Don't force a record
2694 time Record a time stamp
2695 note Prompt for a note and add it with template `org-log-note-headings'
2697 This option can also be set with on a per-file-basis with
2699 #+STARTUP: nologrepeat
2700 #+STARTUP: logrepeat
2701 #+STARTUP: lognoterepeat
2703 You can have local logging settings for a subtree by setting the LOGGING
2704 property to one or more of these keywords."
2706 :group
'org-progress
2708 (const :tag
"Don't force a record" nil
)
2709 (const :tag
"Force recording the DONE state" time
)
2710 (const :tag
"Force recording a note with the DONE state" note
)))
2713 (defgroup org-priorities nil
2714 "Priorities in Org-mode."
2715 :tag
"Org Priorities"
2718 (defcustom org-enable-priority-commands t
2719 "Non-nil means priority commands are active.
2720 When nil, these commands will be disabled, so that you never accidentally
2722 :group
'org-priorities
2725 (defcustom org-highest-priority ?A
2726 "The highest priority of TODO items. A character like ?A, ?B etc.
2727 Must have a smaller ASCII number than `org-lowest-priority'."
2728 :group
'org-priorities
2731 (defcustom org-lowest-priority ?C
2732 "The lowest priority of TODO items. A character like ?A, ?B etc.
2733 Must have a larger ASCII number than `org-highest-priority'."
2734 :group
'org-priorities
2737 (defcustom org-default-priority ?B
2738 "The default priority of TODO items.
2739 This is the priority an item gets if no explicit priority is given.
2740 When starting to cycle on an empty priority the first step in the cycle
2741 depends on `org-priority-start-cycle-with-default'. The resulting first
2742 step priority must not exceed the range from `org-highest-priority' to
2743 `org-lowest-priority' which means that `org-default-priority' has to be
2744 in this range exclusive or inclusive the range boundaries. Else the
2745 first step refuses to set the default and the second will fall back
2746 to (depending on the command used) the highest or lowest priority."
2747 :group
'org-priorities
2750 (defcustom org-priority-start-cycle-with-default t
2751 "Non-nil means start with default priority when starting to cycle.
2752 When this is nil, the first step in the cycle will be (depending on the
2753 command used) one higher or lower than the default priority.
2754 See also `org-default-priority'."
2755 :group
'org-priorities
2758 (defcustom org-get-priority-function nil
2759 "Function to extract the priority from a string.
2760 The string is normally the headline. If this is nil Org computes the
2761 priority from the priority cookie like [#A] in the headline. It returns
2762 an integer, increasing by 1000 for each priority level.
2763 The user can set a different function here, which should take a string
2764 as an argument and return the numeric priority."
2765 :group
'org-priorities
2769 (defgroup org-time nil
2770 "Options concerning time stamps and deadlines in Org-mode."
2774 (defcustom org-insert-labeled-timestamps-at-point nil
2775 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2776 When nil, these labeled time stamps are forces into the second line of an
2777 entry, just after the headline. When scheduling from the global TODO list,
2778 the time stamp will always be forced into the second line."
2782 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2783 "Formats for `format-time-string' which are used for time stamps.
2784 It is not recommended to change this constant.")
2786 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2787 "Number of minutes to round time stamps to.
2788 These are two values, the first applies when first creating a time stamp.
2789 The second applies when changing it with the commands `S-up' and `S-down'.
2790 When changing the time stamp, this means that it will change in steps
2791 of N minutes, as given by the second value.
2793 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2794 numbers should be factors of 60, so for example 5, 10, 15.
2796 When this is larger than 1, you can still force an exact time stamp by using
2797 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2798 and by using a prefix arg to `S-up/down' to specify the exact number
2799 of minutes to shift."
2801 :get
#'(lambda (var) ; Make sure both elements are there
2802 (if (integerp (default-value var
))
2803 (list (default-value var
) 5)
2804 (default-value var
)))
2806 (integer :tag
"when inserting times")
2807 (integer :tag
"when modifying times")))
2809 ;; Normalize old customizations of this variable.
2810 (when (integerp org-time-stamp-rounding-minutes
)
2811 (setq org-time-stamp-rounding-minutes
2812 (list org-time-stamp-rounding-minutes
2813 org-time-stamp-rounding-minutes
)))
2815 (defcustom org-display-custom-times nil
2816 "Non-nil means overlay custom formats over all time stamps.
2817 The formats are defined through the variable `org-time-stamp-custom-formats'.
2818 To turn this on on a per-file basis, insert anywhere in the file:
2819 #+STARTUP: customtime"
2823 (make-variable-buffer-local 'org-display-custom-times
)
2825 (defcustom org-time-stamp-custom-formats
2826 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2827 "Custom formats for time stamps. See `format-time-string' for the syntax.
2828 These are overlaid over the default ISO format if the variable
2829 `org-display-custom-times' is set. Time like %H:%M should be at the
2830 end of the second format. The custom formats are also honored by export
2831 commands, if custom time display is turned on at the time of export."
2835 (defun org-time-stamp-format (&optional long inactive
)
2836 "Get the right format for a time string."
2837 (let ((f (if long
(cdr org-time-stamp-formats
)
2838 (car org-time-stamp-formats
))))
2840 (concat "[" (substring f
1 -
1) "]")
2843 (defcustom org-time-clocksum-format
2844 '(:days
"%dd " :hours
"%d" :require-hours t
:minutes
":%02d" :require-minutes t
)
2845 "The format string used when creating CLOCKSUM lines.
2846 This is also used when Org mode generates a time duration.
2848 The value can be a single format string containing two
2849 %-sequences, which will be filled with the number of hours and
2850 minutes in that order.
2852 Alternatively, the value can be a plist associating any of the
2853 keys :years, :months, :weeks, :days, :hours or :minutes with
2854 format strings. The time duration is formatted using only the
2855 time components that are needed and concatenating the results.
2856 If a time unit in absent, it falls back to the next smallest
2859 The keys :require-years, :require-months, :require-days,
2860 :require-weeks, :require-hours, :require-minutes are also
2861 meaningful. A non-nil value for these keys indicates that the
2862 corresponding time component should always be included, even if
2868 \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
2871 means durations longer than a day will be expressed in days,
2872 hours and minutes, and durations less than a day will always be
2873 expressed in hours and minutes (even for durations less than an
2878 \(:days \"%dd\" :minutes \"%dm\")
2880 means durations longer than a day will be expressed in days and
2881 minutes, and durations less than a day will be expressed entirely
2882 in minutes (even for durations longer than an hour)."
2886 :type
'(choice (string :tag
"Format string")
2888 (group :inline t
(const :tag
"Years" :years
)
2889 (string :tag
"Format string"))
2891 (const :tag
"Always show years" :require-years
)
2893 (group :inline t
(const :tag
"Months" :months
)
2894 (string :tag
"Format string"))
2896 (const :tag
"Always show months" :require-months
)
2898 (group :inline t
(const :tag
"Weeks" :weeks
)
2899 (string :tag
"Format string"))
2901 (const :tag
"Always show weeks" :require-weeks
)
2903 (group :inline t
(const :tag
"Days" :days
)
2904 (string :tag
"Format string"))
2906 (const :tag
"Always show days" :require-days
)
2908 (group :inline t
(const :tag
"Hours" :hours
)
2909 (string :tag
"Format string"))
2911 (const :tag
"Always show hours" :require-hours
)
2913 (group :inline t
(const :tag
"Minutes" :minutes
)
2914 (string :tag
"Format string"))
2916 (const :tag
"Always show minutes" :require-minutes
)
2919 (defcustom org-time-clocksum-use-fractional nil
2920 "When non-nil, \\[org-clock-display] uses fractional times.
2921 See `org-time-clocksum-format' for more on time clock formats."
2927 (defcustom org-time-clocksum-use-effort-durations t
2928 "When non-nil, \\[org-clock-display] uses effort durations.
2929 E.g. by default, one day is considered to be a 8 hours effort,
2930 so a task that has been clocked for 16 hours will be displayed
2931 as during 2 days in the clock display or in the clocktable.
2933 See `org-effort-durations' on how to set effort durations
2934 and `org-time-clocksum-format' for more on time clock formats."
2940 (defcustom org-time-clocksum-fractional-format
"%.2f"
2941 "The format string used when creating CLOCKSUM lines,
2942 or when Org mode generates a time duration, if
2943 `org-time-clocksum-use-fractional' is enabled.
2945 The value can be a single format string containing one
2946 %-sequence, which will be filled with the number of hours as
2949 Alternatively, the value can be a plist associating any of the
2950 keys :years, :months, :weeks, :days, :hours or :minutes with
2951 a format string. The time duration is formatted using the
2952 largest time unit which gives a non-zero integer part. If all
2953 specified formats have zero integer part, the smallest time unit
2956 :type
'(choice (string :tag
"Format string")
2957 (set (group :inline t
(const :tag
"Years" :years
)
2958 (string :tag
"Format string"))
2959 (group :inline t
(const :tag
"Months" :months
)
2960 (string :tag
"Format string"))
2961 (group :inline t
(const :tag
"Weeks" :weeks
)
2962 (string :tag
"Format string"))
2963 (group :inline t
(const :tag
"Days" :days
)
2964 (string :tag
"Format string"))
2965 (group :inline t
(const :tag
"Hours" :hours
)
2966 (string :tag
"Format string"))
2967 (group :inline t
(const :tag
"Minutes" :minutes
)
2968 (string :tag
"Format string")))))
2970 (defcustom org-deadline-warning-days
14
2971 "Number of days before expiration during which a deadline becomes active.
2972 This variable governs the display in sparse trees and in the agenda.
2973 When 0 or negative, it means use this number (the absolute value of it)
2974 even if a deadline has a different individual lead time specified.
2976 Custom commands can set this variable in the options section."
2978 :group
'org-agenda-daily
/weekly
2981 (defcustom org-scheduled-delay-days
0
2982 "Number of days before a scheduled item becomes active.
2983 This variable governs the display in sparse trees and in the agenda.
2984 The default value (i.e. 0) means: don't delay scheduled item.
2985 When negative, it means use this number (the absolute value of it)
2986 even if a scheduled item has a different individual delay time
2989 Custom commands can set this variable in the options section."
2991 :group
'org-agenda-daily
/weekly
2995 (defcustom org-read-date-prefer-future t
2996 "Non-nil means assume future for incomplete date input from user.
2997 This affects the following situations:
2998 1. The user gives a month but not a year.
2999 For example, if it is April and you enter \"feb 2\", this will be read
3000 as Feb 2, *next* year. \"May 5\", however, will be this year.
3001 2. The user gives a day, but no month.
3002 For example, if today is the 15th, and you enter \"3\", Org-mode will
3003 read this as the third of *next* month. However, if you enter \"17\",
3004 it will be considered as *this* month.
3006 If you set this variable to the symbol `time', then also the following
3009 3. If the user gives a time.
3010 If the time is before now, it will be interpreted as tomorrow.
3012 Currently none of this works for ISO week specifications.
3014 When this option is nil, the current day, month and year will always be
3017 See also `org-agenda-jump-prefer-future'."
3020 (const :tag
"Never" nil
)
3021 (const :tag
"Check month and day" t
)
3022 (const :tag
"Check month, day, and time" time
)))
3024 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
3025 "Should the agenda jump command prefer the future for incomplete dates?
3026 The default is to do the same as configured in `org-read-date-prefer-future'.
3027 But you can also set a deviating value here.
3028 This may t or nil, or the symbol `org-read-date-prefer-future'."
3033 (const :tag
"Use org-read-date-prefer-future"
3034 org-read-date-prefer-future
)
3035 (const :tag
"Never" nil
)
3036 (const :tag
"Always" t
)))
3038 (defcustom org-read-date-force-compatible-dates t
3039 "Should date/time prompt force dates that are guaranteed to work in Emacs?
3041 Depending on the system Emacs is running on, certain dates cannot
3042 be represented with the type used internally to represent time.
3043 Dates between 1970-1-1 and 2038-1-1 can always be represented
3044 correctly. Some systems allow for earlier dates, some for later,
3045 some for both. One way to find out it to insert any date into an
3046 Org buffer, putting the cursor on the year and hitting S-up and
3047 S-down to test the range.
3049 When this variable is set to t, the date/time prompt will not let
3050 you specify dates outside the 1970-2037 range, so it is certain that
3051 these dates will work in whatever version of Emacs you are
3052 running, and also that you can move a file from one Emacs implementation
3053 to another. WHenever Org is forcing the year for you, it will display
3056 When this variable is nil, Org will check if the date is
3057 representable in the specific Emacs implementation you are using.
3058 If not, it will force a year, usually the current year, and beep
3059 to remind you. Currently this setting is not recommended because
3060 the likelihood that you will open your Org files in an Emacs that
3061 has limited date range is not negligible.
3063 A workaround for this problem is to use diary sexp dates for time
3064 stamps outside of this range."
3069 (defcustom org-read-date-display-live t
3070 "Non-nil means display current interpretation of date prompt live.
3071 This display will be in an overlay, in the minibuffer."
3075 (defcustom org-read-date-popup-calendar t
3076 "Non-nil means pop up a calendar when prompting for a date.
3077 In the calendar, the date can be selected with mouse-1. However, the
3078 minibuffer will also be active, and you can simply enter the date as well.
3079 When nil, only the minibuffer will be available."
3082 (if (fboundp 'defvaralias
)
3083 (defvaralias 'org-popup-calendar-for-date-prompt
3084 'org-read-date-popup-calendar
))
3086 (make-obsolete-variable
3087 'org-read-date-minibuffer-setup-hook
3088 "Set `org-read-date-minibuffer-local-map' instead." "24.3")
3089 (defcustom org-read-date-minibuffer-setup-hook nil
3090 "Hook to be used to set up keys for the date/time interface.
3091 Add key definitions to `minibuffer-local-map', which will be a
3094 WARNING: This option is obsolete, you should use
3095 `org-read-date-minibuffer-local-map' to set up keys."
3099 (defcustom org-extend-today-until
0
3100 "The hour when your day really ends. Must be an integer.
3101 This has influence for the following applications:
3102 - When switching the agenda to \"today\". It it is still earlier than
3103 the time given here, the day recognized as TODAY is actually yesterday.
3104 - When a date is read from the user and it is still before the time given
3105 here, the current date and time will be assumed to be yesterday, 23:59.
3106 Also, timestamps inserted in capture templates follow this rule.
3108 IMPORTANT: This is a feature whose implementation is and likely will
3109 remain incomplete. Really, it is only here because past midnight seems to
3110 be the favorite working time of John Wiegley :-)"
3114 (defcustom org-use-effective-time nil
3115 "If non-nil, consider `org-extend-today-until' when creating timestamps.
3116 For example, if `org-extend-today-until' is 8, and it's 4am, then the
3117 \"effective time\" of any timestamps between midnight and 8am will be
3118 23:59 of the previous day."
3123 (defcustom org-use-last-clock-out-time-as-effective-time nil
3124 "When non-nil, use the last clock out time for `org-todo'.
3125 Note that this option has precedence over the combined use of
3126 `org-use-effective-time' and `org-extend-today-until'."
3131 (defcustom org-edit-timestamp-down-means-later nil
3132 "Non-nil means S-down will increase the time in a time stamp.
3133 When nil, S-up will increase."
3137 (defcustom org-calendar-follow-timestamp-change t
3138 "Non-nil means make the calendar window follow timestamp changes.
3139 When a timestamp is modified and the calendar window is visible, it will be
3140 moved to the new date."
3144 (defgroup org-tags nil
3145 "Options concerning tags in Org-mode."
3149 (defcustom org-tag-alist nil
3150 "List of tags allowed in Org-mode files.
3151 When this list is nil, Org-mode will base TAG input on what is already in the
3153 The value of this variable is an alist, the car of each entry must be a
3154 keyword as a string, the cdr may be a character that is used to select
3155 that tag through the fast-tag-selection interface.
3156 See the manual for details."
3160 (cons (string :tag
"Tag name")
3161 (character :tag
"Access char"))
3162 (list :tag
"Start radio group"
3164 (option (string :tag
"Group description")))
3165 (list :tag
"End radio group"
3167 (option (string :tag
"Group description")))
3168 (const :tag
"New line" (:newline
)))))
3170 (defcustom org-tag-persistent-alist nil
3171 "List of tags that will always appear in all Org-mode files.
3172 This is in addition to any in buffer settings or customizations
3174 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
3175 The value of this variable is an alist, the car of each entry must be a
3176 keyword as a string, the cdr may be a character that is used to select
3177 that tag through the fast-tag-selection interface.
3178 See the manual for details.
3179 To disable these tags on a per-file basis, insert anywhere in the file:
3184 (cons (string :tag
"Tag name")
3185 (character :tag
"Access char"))
3186 (const :tag
"Start radio group" (:startgroup
))
3187 (const :tag
"End radio group" (:endgroup
))
3188 (const :tag
"New line" (:newline
)))))
3190 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
3191 "If non-nil, always offer completion for all tags of all agenda files.
3192 Instead of customizing this variable directly, you might want to
3193 set it locally for capture buffers, because there no list of
3194 tags in that file can be created dynamically (there are none).
3196 (add-hook 'org-capture-mode-hook
3198 (set (make-local-variable
3199 'org-complete-tags-always-offer-all-agenda-tags)
3205 (defvar org-file-tags nil
3206 "List of tags that can be inherited by all entries in the file.
3207 The tags will be inherited if the variable `org-use-tag-inheritance'
3208 says they should be.
3209 This variable is populated from #+FILETAGS lines.")
3211 (defcustom org-use-fast-tag-selection
'auto
3212 "Non-nil means use fast tag selection scheme.
3213 This is a special interface to select and deselect tags with single keys.
3214 When nil, fast selection is never used.
3215 When the symbol `auto', fast selection is used if and only if selection
3216 characters for tags have been configured, either through the variable
3217 `org-tag-alist' or through a #+TAGS line in the buffer.
3218 When t, fast selection is always used and selection keys are assigned
3219 automatically if necessary."
3222 (const :tag
"Always" t
)
3223 (const :tag
"Never" nil
)
3224 (const :tag
"When selection characters are configured" 'auto
)))
3226 (defcustom org-fast-tag-selection-single-key nil
3227 "Non-nil means fast tag selection exits after first change.
3228 When nil, you have to press RET to exit it.
3229 During fast tag selection, you can toggle this flag with `C-c'.
3230 This variable can also have the value `expert'. In this case, the window
3231 displaying the tags menu is not even shown, until you press C-c again."
3234 (const :tag
"No" nil
)
3235 (const :tag
"Yes" t
)
3236 (const :tag
"Expert" expert
)))
3238 (defvar org-fast-tag-selection-include-todo nil
3239 "Non-nil means fast tags selection interface will also offer TODO states.
3240 This is an undocumented feature, you should not rely on it.")
3242 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
3243 "The column to which tags should be indented in a headline.
3244 If this number is positive, it specifies the column. If it is negative,
3245 it means that the tags should be flushright to that column. For example,
3246 -80 works well for a normal 80 character screen.
3247 When 0, place tags directly after headline text, with only one space in
3252 (defcustom org-auto-align-tags t
3253 "Non-nil keeps tags aligned when modifying headlines.
3254 Some operations (i.e. demoting) change the length of a headline and
3255 therefore shift the tags around. With this option turned on, after
3256 each such operation the tags are again aligned to `org-tags-column'."
3260 (defcustom org-use-tag-inheritance t
3261 "Non-nil means tags in levels apply also for sublevels.
3262 When nil, only the tags directly given in a specific line apply there.
3263 This may also be a list of tags that should be inherited, or a regexp that
3264 matches tags that should be inherited. Additional control is possible
3265 with the variable `org-tags-exclude-from-inheritance' which gives an
3266 explicit list of tags to be excluded from inheritance, even if the value of
3267 `org-use-tag-inheritance' would select it for inheritance.
3269 If this option is t, a match early-on in a tree can lead to a large
3270 number of matches in the subtree when constructing the agenda or creating
3271 a sparse tree. If you only want to see the first match in a tree during
3272 a search, check out the variable `org-tags-match-list-sublevels'."
3275 (const :tag
"Not" nil
)
3276 (const :tag
"Always" t
)
3277 (repeat :tag
"Specific tags" (string :tag
"Tag"))
3278 (regexp :tag
"Tags matched by regexp")))
3280 (defcustom org-tags-exclude-from-inheritance nil
3281 "List of tags that should never be inherited.
3282 This is a way to exclude a few tags from inheritance. For way to do
3283 the opposite, to actively allow inheritance for selected tags,
3284 see the variable `org-use-tag-inheritance'."
3286 :type
'(repeat (string :tag
"Tag")))
3288 (defun org-tag-inherit-p (tag)
3289 "Check if TAG is one that should be inherited."
3291 ((member tag org-tags-exclude-from-inheritance
) nil
)
3292 ((eq org-use-tag-inheritance t
) t
)
3293 ((not org-use-tag-inheritance
) nil
)
3294 ((stringp org-use-tag-inheritance
)
3295 (string-match org-use-tag-inheritance tag
))
3296 ((listp org-use-tag-inheritance
)
3297 (member tag org-use-tag-inheritance
))
3298 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3300 (defcustom org-tags-match-list-sublevels t
3301 "Non-nil means list also sublevels of headlines matching a search.
3302 This variable applies to tags/property searches, and also to stuck
3303 projects because this search is based on a tags match as well.
3305 When set to the symbol `indented', sublevels are indented with
3308 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3309 the sublevels of a headline matching a tag search often also match
3310 the same search. Listing all of them can create very long lists.
3311 Setting this variable to nil causes subtrees of a match to be skipped.
3313 This variable is semi-obsolete and probably should always be true. It
3314 is better to limit inheritance to certain tags using the variables
3315 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3318 (const :tag
"No, don't list them" nil
)
3319 (const :tag
"Yes, do list them" t
)
3320 (const :tag
"List them, indented with leading dots" indented
)))
3322 (defcustom org-tags-sort-function nil
3323 "When set, tags are sorted using this function as a comparator."
3326 (const :tag
"No sorting" nil
)
3327 (const :tag
"Alphabetical" string
<)
3328 (const :tag
"Reverse alphabetical" string
>)
3329 (function :tag
"Custom function" nil
)))
3331 (defvar org-tags-history nil
3332 "History of minibuffer reads for tags.")
3333 (defvar org-last-tags-completion-table nil
3334 "The last used completion table for tags.")
3335 (defvar org-after-tags-change-hook nil
3336 "Hook that is run after the tags in a line have changed.")
3338 (defgroup org-properties nil
3339 "Options concerning properties in Org-mode."
3340 :tag
"Org Properties"
3343 (defcustom org-property-format
"%-10s %s"
3344 "How property key/value pairs should be formatted by `indent-line'.
3345 When `indent-line' hits a property definition, it will format the line
3346 according to this format, mainly to make sure that the values are
3347 lined-up with respect to each other."
3348 :group
'org-properties
3351 (defcustom org-properties-postprocess-alist nil
3352 "Alist of properties and functions to adjust inserted values.
3353 Elements of this alist must be of the form
3355 ([string] [function])
3357 where [string] must be a property name and [function] must be a
3358 lambda expression: this lambda expression must take one argument,
3359 the value to adjust, and return the new value as a string.
3361 For example, this element will allow the property \"Remaining\"
3362 to be updated wrt the relation between the \"Effort\" property
3363 and the clock summary:
3365 ((\"Remaining\" (lambda(value)
3366 (let ((clocksum (org-clock-sum-current-item))
3367 (effort (org-duration-string-to-minutes
3368 (org-entry-get (point) \"Effort\"))))
3369 (org-minutes-to-clocksum-string (- effort clocksum))))))"
3370 :group
'org-properties
3372 :type
'(alist :key-type
(string :tag
"Property")
3373 :value-type
(function :tag
"Function")))
3375 (defcustom org-use-property-inheritance nil
3376 "Non-nil means properties apply also for sublevels.
3378 This setting is chiefly used during property searches. Turning it on can
3379 cause significant overhead when doing a search, which is why it is not
3382 When nil, only the properties directly given in the current entry count.
3383 When t, every property is inherited. The value may also be a list of
3384 properties that should have inheritance, or a regular expression matching
3385 properties that should be inherited.
3387 However, note that some special properties use inheritance under special
3388 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3389 and the properties ending in \"_ALL\" when they are used as descriptor
3390 for valid values of a property.
3392 Note for programmers:
3393 When querying an entry with `org-entry-get', you can control if inheritance
3394 should be used. By default, `org-entry-get' looks only at the local
3395 properties. You can request inheritance by setting the inherit argument
3396 to t (to force inheritance) or to `selective' (to respect the setting
3398 :group
'org-properties
3400 (const :tag
"Not" nil
)
3401 (const :tag
"Always" t
)
3402 (repeat :tag
"Specific properties" (string :tag
"Property"))
3403 (regexp :tag
"Properties matched by regexp")))
3405 (defun org-property-inherit-p (property)
3406 "Check if PROPERTY is one that should be inherited."
3408 ((eq org-use-property-inheritance t
) t
)
3409 ((not org-use-property-inheritance
) nil
)
3410 ((stringp org-use-property-inheritance
)
3411 (string-match org-use-property-inheritance property
))
3412 ((listp org-use-property-inheritance
)
3413 (member property org-use-property-inheritance
))
3414 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3416 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3417 "The default column format, if no other format has been defined.
3418 This variable can be set on the per-file basis by inserting a line
3420 #+COLUMNS: %25ITEM ....."
3421 :group
'org-properties
3424 (defcustom org-columns-ellipses
".."
3425 "The ellipses to be used when a field in column view is truncated.
3426 When this is the empty string, as many characters as possible are shown,
3427 but then there will be no visual indication that the field has been truncated.
3428 When this is a string of length N, the last N characters of a truncated
3429 field are replaced by this string. If the column is narrower than the
3430 ellipses string, only part of the ellipses string will be shown."
3431 :group
'org-properties
3434 (defcustom org-columns-modify-value-for-display-function nil
3435 "Function that modifies values for display in column view.
3436 For example, it can be used to cut out a certain part from a time stamp.
3437 The function must take 2 arguments:
3439 column-title The title of the column (*not* the property name)
3440 value The value that should be modified.
3442 The function should return the value that should be displayed,
3443 or nil if the normal value should be used."
3444 :group
'org-properties
3447 (defcustom org-effort-property
"Effort"
3448 "The property that is being used to keep track of effort estimates.
3449 Effort estimates given in this property need to have the format H:MM."
3450 :group
'org-properties
3451 :group
'org-progress
3452 :type
'(string :tag
"Property"))
3454 (defconst org-global-properties-fixed
3455 '(("VISIBILITY_ALL" .
"folded children content all")
3456 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3457 "List of property/value pairs that can be inherited by any entry.
3459 These are fixed values, for the preset properties. The user variable
3460 that can be used to add to this list is `org-global-properties'.
3462 The entries in this list are cons cells where the car is a property
3463 name and cdr is a string with the value. If the value represents
3464 multiple items like an \"_ALL\" property, separate the items by
3467 (defcustom org-global-properties nil
3468 "List of property/value pairs that can be inherited by any entry.
3470 This list will be combined with the constant `org-global-properties-fixed'.
3472 The entries in this list are cons cells where the car is a property
3473 name and cdr is a string with the value.
3475 You can set buffer-local values for the same purpose in the variable
3476 `org-file-properties' this by adding lines like
3478 #+PROPERTY: NAME VALUE"
3479 :group
'org-properties
3481 (cons (string :tag
"Property")
3482 (string :tag
"Value"))))
3484 (defvar org-file-properties nil
3485 "List of property/value pairs that can be inherited by any entry.
3486 Valid for the current buffer.
3487 This variable is populated from #+PROPERTY lines.")
3488 (make-variable-buffer-local 'org-file-properties
)
3490 (defgroup org-agenda nil
3491 "Options concerning agenda views in Org-mode."
3495 (defvar org-category nil
3496 "Variable used by org files to set a category for agenda display.
3497 Such files should use a file variable to set it, for example
3499 # -*- mode: org; org-category: \"ELisp\"
3501 or contain a special line
3505 If the file does not specify a category, then file's base name
3507 (make-variable-buffer-local 'org-category
)
3508 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3510 (defcustom org-agenda-files nil
3511 "The files to be used for agenda display.
3512 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3513 \\[org-remove-file]. You can also use customize to edit the list.
3515 If an entry is a directory, all files in that directory that are matched by
3516 `org-agenda-file-regexp' will be part of the file list.
3518 If the value of the variable is not a list but a single file name, then
3519 the list of agenda files is actually stored and maintained in that file, one
3520 agenda file per line. In this file paths can be given relative to
3521 `org-directory'. Tilde expansion and environment variable substitution
3525 (repeat :tag
"List of files and directories" file
)
3526 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3528 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3529 "Regular expression to match files for `org-agenda-files'.
3530 If any element in the list in that variable contains a directory instead
3531 of a normal file, all files in that directory that are matched by this
3532 regular expression will be included."
3536 (defcustom org-agenda-text-search-extra-files nil
3537 "List of extra files to be searched by text search commands.
3538 These files will be search in addition to the agenda files by the
3539 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3540 Note that these files will only be searched for text search commands,
3541 not for the other agenda views like todo lists, tag searches or the weekly
3542 agenda. This variable is intended to list notes and possibly archive files
3543 that should also be searched by these two commands.
3544 In fact, if the first element in the list is the symbol `agenda-archives',
3545 than all archive files of all agenda files will be added to the search
3548 :type
'(set :greedy t
3549 (const :tag
"Agenda Archives" agenda-archives
)
3550 (repeat :inline t
(file))))
3552 (if (fboundp 'defvaralias
)
3553 (defvaralias 'org-agenda-multi-occur-extra-files
3554 'org-agenda-text-search-extra-files
))
3556 (defcustom org-agenda-skip-unavailable-files nil
3557 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3558 A nil value means to remove them, after a query, from the list."
3562 (defcustom org-calendar-to-agenda-key
[?c
]
3563 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3564 The command `org-calendar-goto-agenda' will be bound to this key. The
3565 default is the character `c' because then `c' can be used to switch back and
3566 forth between agenda and calendar."
3570 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3571 "The key to be installed in `calendar-mode-map' for adding diary entries.
3572 This option is irrelevant until `org-agenda-diary-file' has been configured
3573 to point to an Org-mode file. When that is the case, the command
3574 `org-agenda-diary-entry' will be bound to the key given here, by default
3575 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3576 if you want to continue doing this, you need to change this to a different
3581 (defcustom org-agenda-diary-file
'diary-file
3582 "File to which to add new entries with the `i' key in agenda and calendar.
3583 When this is the symbol `diary-file', the functionality in the Emacs
3584 calendar will be used to add entries to the `diary-file'. But when this
3585 points to a file, `org-agenda-diary-entry' will be used instead."
3588 (const :tag
"The standard Emacs diary file" diary-file
)
3589 (file :tag
"Special Org file diary entries")))
3591 (eval-after-load "calendar"
3593 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3594 'org-calendar-goto-agenda
)
3595 (add-hook 'calendar-mode-hook
3597 (unless (eq org-agenda-diary-file
'diary-file
)
3598 (define-key calendar-mode-map
3599 org-calendar-insert-diary-entry-key
3600 'org-agenda-diary-entry
))))))
3602 (defgroup org-latex nil
3603 "Options for embedding LaTeX code into Org-mode."
3607 (defcustom org-format-latex-options
3608 '(:foreground default
:background default
:scale
1.0
3609 :html-foreground
"Black" :html-background
"Transparent"
3610 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3611 "Options for creating images from LaTeX fragments.
3612 This is a property list with the following properties:
3613 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3614 `default' means use the foreground of the default face.
3615 `auto' means use the foreground from the text face.
3616 :background the background color, or \"Transparent\".
3617 `default' means use the background of the default face.
3618 `auto' means use the background from the text face.
3619 :scale a scaling factor for the size of the images, to get more pixels
3620 :html-foreground, :html-background, :html-scale
3621 the same numbers for HTML export.
3622 :matchers a list indicating which matchers should be used to
3623 find LaTeX fragments. Valid members of this list are:
3624 \"begin\" find environments
3625 \"$1\" find single characters surrounded by $.$
3626 \"$\" find math expressions surrounded by $...$
3627 \"$$\" find math expressions surrounded by $$....$$
3628 \"\\(\" find math expressions surrounded by \\(...\\)
3629 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3633 (defcustom org-format-latex-signal-error t
3634 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3635 When nil, just push out a message."
3640 (defcustom org-latex-to-mathml-jar-file nil
3641 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3642 Use this to specify additional executable file say a jar file.
3644 When using MathToWeb as the converter, specify the full-path to
3645 your mathtoweb.jar file."
3649 (const :tag
"None" nil
)
3650 (file :tag
"JAR file" :must-match t
)))
3652 (defcustom org-latex-to-mathml-convert-command nil
3653 "Command to convert LaTeX fragments to MathML.
3654 Replace format-specifiers in the command as noted below and use
3655 `shell-command' to convert LaTeX to MathML.
3656 %j: Executable file in fully expanded form as specified by
3657 `org-latex-to-mathml-jar-file'.
3658 %I: Input LaTeX file in fully expanded form
3659 %o: Output MathML file
3660 This command is used by `org-create-math-formula'.
3662 When using MathToWeb as the converter, set this to
3663 \"java -jar %j -unicode -force -df %o %I\"."
3667 (const :tag
"None" nil
)
3668 (string :tag
"\nShell command")))
3670 (defcustom org-latex-create-formula-image-program
'dvipng
3671 "Program to convert LaTeX fragments with.
3673 dvipng Process the LaTeX fragments to dvi file, then convert
3674 dvi files to png files using dvipng.
3675 This will also include processing of non-math environments.
3676 imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
3677 to convert pdf files to png files"
3681 (const :tag
"dvipng" dvipng
)
3682 (const :tag
"imagemagick" imagemagick
)))
3684 (defcustom org-latex-preview-ltxpng-directory
"ltxpng/"
3685 "Path to store latex preview images.
3686 A relative path here creates many directories relative to the
3687 processed org files paths. An absolute path puts all preview
3688 images at the same place."
3693 (defun org-format-latex-mathml-available-p ()
3694 "Return t if `org-latex-to-mathml-convert-command' is usable."
3696 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3697 org-latex-to-mathml-convert-command
)
3698 (let ((executable (car (split-string
3699 org-latex-to-mathml-convert-command
))))
3700 (when (executable-find executable
)
3702 "%j" org-latex-to-mathml-convert-command
)
3703 (file-readable-p org-latex-to-mathml-jar-file
)
3706 (defcustom org-format-latex-header
"\\documentclass{article}
3707 \\usepackage[usenames]{color}
3708 \\usepackage{amsmath}
3709 \\usepackage[mathscr]{eucal}
3710 \\pagestyle{empty} % do not remove
3713 % The settings below are copied from fullpage.sty
3714 \\setlength{\\textwidth}{\\paperwidth}
3715 \\addtolength{\\textwidth}{-3cm}
3716 \\setlength{\\oddsidemargin}{1.5cm}
3717 \\addtolength{\\oddsidemargin}{-2.54cm}
3718 \\setlength{\\evensidemargin}{\\oddsidemargin}
3719 \\setlength{\\textheight}{\\paperheight}
3720 \\addtolength{\\textheight}{-\\headheight}
3721 \\addtolength{\\textheight}{-\\headsep}
3722 \\addtolength{\\textheight}{-\\footskip}
3723 \\addtolength{\\textheight}{-3cm}
3724 \\setlength{\\topmargin}{1.5cm}
3725 \\addtolength{\\topmargin}{-2.54cm}"
3726 "The document header used for processing LaTeX fragments.
3727 It is imperative that this header make sure that no page number
3728 appears on the page. The package defined in the variables
3729 `org-latex-default-packages-alist' and `org-latex-packages-alist'
3730 will either replace the placeholder \"[PACKAGES]\" in this
3731 header, or they will be appended."
3735 (defun org-set-packages-alist (var val
)
3736 "Set the packages alist and make sure it has 3 elements per entry."
3737 (set var
(mapcar (lambda (x)
3738 (if (and (consp x
) (= (length x
) 2))
3739 (list (car x
) (nth 1 x
) t
)
3743 (defun org-get-packages-alist (var)
3744 "Get the packages alist and make sure it has 3 elements per entry."
3746 (if (and (consp x
) (= (length x
) 2))
3747 (list (car x
) (nth 1 x
) t
)
3749 (default-value var
)))
3751 (defcustom org-latex-default-packages-alist
3752 '(("AUTO" "inputenc" t
)
3756 ("" "longtable" nil
)
3767 "Alist of default packages to be inserted in the header.
3769 Change this only if one of the packages here causes an
3770 incompatibility with another package you are using.
3772 The packages in this list are needed by one part or another of
3773 Org mode to function properly:
3775 - inputenc, fontenc: for basic font and character selection
3776 - textcomp, marvosymb, wasysym, latexsym, amssym: for various
3777 symbols used for interpreting the entities in `org-entities'.
3778 You can skip some of these packages if you don't use any of the
3780 - graphicx: for including images
3781 - float, wrapfig: for figure placement
3782 - longtable: for long tables
3783 - hyperref: for cross references
3785 Therefore you should not modify this variable unless you know
3786 what you are doing. The one reason to change it anyway is that
3787 you might be loading some other package that conflicts with one
3788 of the default packages. Each cell is of the format
3789 \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
3790 the package also needs to be included when compiling LaTeX
3791 snippets into images for inclusion into non-LaTeX output."
3793 :group
'org-export-latex
3794 :set
'org-set-packages-alist
3795 :get
'org-get-packages-alist
3799 (list :tag
"options/package pair"
3800 (string :tag
"options")
3801 (string :tag
"package")
3802 (boolean :tag
"Snippet"))
3803 (string :tag
"A line of LaTeX"))))
3805 (defcustom org-latex-packages-alist nil
3806 "Alist of packages to be inserted in every LaTeX header.
3808 These will be inserted after `org-latex-default-packages-alist'.
3809 Each cell is of the format:
3811 \(\"options\" \"package\" snippet-flag)
3813 SNIPPET-FLAG, when t, indicates that this package is also needed
3814 when turning LaTeX snippets into images for inclusion into
3817 Make sure that you only list packages here which:
3819 - you want in every file
3820 - do not conflict with the setup in `org-format-latex-header'.
3821 - do not conflict with the default packages in
3822 `org-latex-default-packages-alist'."
3824 :group
'org-export-latex
3825 :set
'org-set-packages-alist
3826 :get
'org-get-packages-alist
3829 (list :tag
"options/package pair"
3830 (string :tag
"options")
3831 (string :tag
"package")
3832 (boolean :tag
"Snippet"))
3833 (string :tag
"A line of LaTeX"))))
3835 (defgroup org-appearance nil
3836 "Settings for Org-mode appearance."
3837 :tag
"Org Appearance"
3840 (defcustom org-level-color-stars-only nil
3841 "Non-nil means fontify only the stars in each headline.
3842 When nil, the entire headline is fontified.
3843 Changing it requires restart of `font-lock-mode' to become effective
3844 also in regions already fontified."
3845 :group
'org-appearance
3848 (defcustom org-hide-leading-stars nil
3849 "Non-nil means hide the first N-1 stars in a headline.
3850 This works by using the face `org-hide' for these stars. This
3851 face is white for a light background, and black for a dark
3852 background. You may have to customize the face `org-hide' to
3854 Changing it requires restart of `font-lock-mode' to become effective
3855 also in regions already fontified.
3856 You may also set this on a per-file basis by adding one of the following
3857 lines to the buffer:
3859 #+STARTUP: hidestars
3860 #+STARTUP: showstars"
3861 :group
'org-appearance
3864 (defcustom org-hidden-keywords nil
3865 "List of symbols corresponding to keywords to be hidden the org buffer.
3866 For example, a value '(title) for this list will make the document's title
3867 appear in the buffer without the initial #+TITLE: keyword."
3868 :group
'org-appearance
3870 :type
'(set (const :tag
"#+AUTHOR" author
)
3871 (const :tag
"#+DATE" date
)
3872 (const :tag
"#+EMAIL" email
)
3873 (const :tag
"#+TITLE" title
)))
3875 (defcustom org-custom-properties nil
3876 "List of properties (as strings) with a special meaning.
3877 The default use of these custom properties is to let the user
3878 hide them with `org-toggle-custom-properties-visibility'."
3879 :group
'org-properties
3880 :group
'org-appearance
3882 :type
'(repeat (string :tag
"Property Name")))
3884 (defcustom org-fontify-done-headline nil
3885 "Non-nil means change the face of a headline if it is marked DONE.
3886 Normally, only the TODO/DONE keyword indicates the state of a headline.
3887 When this is non-nil, the headline after the keyword is set to the
3888 `org-headline-done' as an additional indication."
3889 :group
'org-appearance
3892 (defcustom org-fontify-emphasized-text t
3893 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3894 Changing this variable requires a restart of Emacs to take effect."
3895 :group
'org-appearance
3898 (defcustom org-fontify-whole-heading-line nil
3899 "Non-nil means fontify the whole line for headings.
3900 This is useful when setting a background color for the
3902 :group
'org-appearance
3905 (defcustom org-highlight-latex-and-related nil
3906 "Non-nil means highlight LaTeX related syntax in the buffer.
3907 When non nil, the value should be a list containing any of the
3909 `latex' Highlight LaTeX snippets and environments.
3910 `script' Highlight subscript and superscript.
3911 `entities' Highlight entities."
3912 :group
'org-appearance
3914 (const :tag
"No highlighting" nil
)
3915 (set :greedy t
:tag
"Highlight"
3916 (const :tag
"LaTeX snippets and environments" latex
)
3917 (const :tag
"Subscript and superscript" script
)
3918 (const :tag
"Entities" entities
))))
3920 (defcustom org-hide-emphasis-markers nil
3921 "Non-nil mean font-lock should hide the emphasis marker characters."
3922 :group
'org-appearance
3925 (defcustom org-pretty-entities nil
3926 "Non-nil means show entities as UTF8 characters.
3927 When nil, the \\name form remains in the buffer."
3928 :group
'org-appearance
3932 (defcustom org-pretty-entities-include-sub-superscripts t
3933 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3934 :group
'org-appearance
3938 (defvar org-emph-re nil
3939 "Regular expression for matching emphasis.
3940 After a match, the match groups contain these elements:
3941 0 The match of the full regular expression, including the characters
3942 before and after the proper match
3943 1 The character before the proper match, or empty at beginning of line
3944 2 The proper match, including the leading and trailing markers
3945 3 The leading marker like * or /, indicating the type of highlighting
3946 4 The text between the emphasis markers, not including the markers
3947 5 The character after the match, empty at the end of a line")
3948 (defvar org-verbatim-re nil
3949 "Regular expression for matching verbatim text.")
3950 (defvar org-emphasis-regexp-components
) ; defined just below
3951 (defvar org-emphasis-alist
) ; defined just below
3952 (defun org-set-emph-re (var val
)
3953 "Set variable and compute the emphasis regular expression."
3955 (when (and (boundp 'org-emphasis-alist
)
3956 (boundp 'org-emphasis-regexp-components
)
3957 org-emphasis-alist org-emphasis-regexp-components
)
3958 (let* ((e org-emphasis-regexp-components
)
3964 (body1 (concat body
"*?"))
3965 (markers (mapconcat 'car org-emphasis-alist
""))
3966 (vmarkers (mapconcat
3967 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3968 org-emphasis-alist
"")))
3969 ;; make sure special characters appear at the right position in the class
3970 (if (string-match "\\^" markers
)
3971 (setq markers
(concat (replace-match "" t t markers
) "^")))
3972 (if (string-match "-" markers
)
3973 (setq markers
(concat (replace-match "" t t markers
) "-")))
3974 (if (string-match "\\^" vmarkers
)
3975 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3976 (if (string-match "-" vmarkers
)
3977 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3979 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3980 (int-to-string nl
) "\\}")))
3983 (concat "\\([" pre
"]\\|^\\)"
3985 "\\([" markers
"]\\)"
3993 "\\([" post
"]\\|$\\)"))
3994 (setq org-verbatim-re
3995 (concat "\\([" pre
"]\\|^\\)"
3997 "\\([" vmarkers
"]\\)"
4005 "\\([" post
"]\\|$\\)")))))
4007 (defcustom org-emphasis-regexp-components
4008 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
4009 "Components used to build the regular expression for emphasis.
4010 This is a list with five entries. Terminology: In an emphasis string
4011 like \" *strong word* \", we call the initial space PREMATCH, the final
4012 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
4013 and \"trong wor\" is the body. The different components in this variable
4014 specify what is allowed/forbidden in each part:
4016 pre Chars allowed as prematch. Beginning of line will be allowed too.
4017 post Chars allowed as postmatch. End of line will be allowed too.
4018 border The chars *forbidden* as border characters.
4019 body-regexp A regexp like \".\" to match a body character. Don't use
4020 non-shy groups here, and don't allow newline here.
4021 newline The maximum number of newlines allowed in an emphasis exp.
4023 Use customize to modify this, or restart Emacs after changing it."
4024 :group
'org-appearance
4025 :set
'org-set-emph-re
4027 (sexp :tag
"Allowed chars in pre ")
4028 (sexp :tag
"Allowed chars in post ")
4029 (sexp :tag
"Forbidden chars in border ")
4030 (sexp :tag
"Regexp for body ")
4031 (integer :tag
"number of newlines allowed")
4032 (option (boolean :tag
"Please ignore this button"))))
4034 (defcustom org-emphasis-alist
4035 `(("*" bold
"<b>" "</b>")
4036 ("/" italic
"<i>" "</i>")
4037 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
4038 ("=" org-code
"<code>" "</code>" verbatim
)
4039 ("~" org-verbatim
"<code>" "</code>" verbatim
)
4040 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
4043 "Special syntax for emphasized text.
4044 Text starting and ending with a special character will be emphasized, for
4045 example *bold*, _underlined_ and /italic/. This variable sets the marker
4046 characters, the face to be used by font-lock for highlighting in Org-mode
4047 Emacs buffers, and the HTML tags to be used for this.
4048 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
4049 Use customize to modify this, or restart Emacs after changing it."
4050 :group
'org-appearance
4051 :set
'org-set-emph-re
4054 (string :tag
"Marker character")
4056 (face :tag
"Font-lock-face")
4057 (plist :tag
"Face property list"))
4058 (string :tag
"HTML start tag")
4059 (string :tag
"HTML end tag")
4060 (option (const verbatim
)))))
4062 (defvar org-protecting-blocks
4063 '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
4064 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
4065 This is needed for font-lock setup.")
4067 ;;; Miscellaneous options
4069 (defgroup org-completion nil
4070 "Completion in Org-mode."
4071 :tag
"Org Completion"
4074 (defcustom org-completion-use-ido nil
4075 "Non-nil means use ido completion wherever possible.
4076 Note that `ido-mode' must be active for this variable to be relevant.
4077 If you decide to turn this variable on, you might well want to turn off
4078 `org-outline-path-complete-in-steps'.
4079 See also `org-completion-use-iswitchb'."
4080 :group
'org-completion
4083 (defcustom org-completion-use-iswitchb nil
4084 "Non-nil means use iswitchb completion wherever possible.
4085 Note that `iswitchb-mode' must be active for this variable to be relevant.
4086 If you decide to turn this variable on, you might well want to turn off
4087 `org-outline-path-complete-in-steps'.
4088 Note that this variable has only an effect if `org-completion-use-ido' is nil."
4089 :group
'org-completion
4092 (defcustom org-completion-fallback-command
'hippie-expand
4093 "The expansion command called by \\[pcomplete] in normal context.
4094 Normal means, no org-mode-specific context."
4095 :group
'org-completion
4098 ;;; Functions and variables from their packages
4099 ;; Declared here to avoid compiler warnings
4102 (defvar outline-mode-menu-heading
)
4103 (defvar outline-mode-menu-show
)
4104 (defvar outline-mode-menu-hide
)
4105 (defvar zmacs-regions
) ; XEmacs regions
4108 (defvar mark-active
)
4111 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
4112 (declare-function calendar-forward-day
"cal-move" (arg))
4113 (declare-function calendar-goto-date
"cal-move" (date))
4114 (declare-function calendar-goto-today
"cal-move" ())
4115 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
4116 (defvar calc-embedded-close-formula
)
4117 (defvar calc-embedded-open-formula
)
4118 (declare-function cdlatex-tab
"ext:cdlatex" ())
4119 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
4120 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
4121 (defvar font-lock-unfontify-region-function
)
4122 (declare-function iswitchb-read-buffer
"iswitchb"
4123 (prompt &optional default require-match start matches-set
))
4124 (defvar iswitchb-temp-buflist
)
4125 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
4126 (defvar org-agenda-tags-todo-honor-ignore-options
)
4127 (declare-function org-agenda-skip
"org-agenda" ())
4129 org-agenda-format-item
"org-agenda"
4130 (extra txt
&optional level category tags dotime noprefix remove-re habitp
))
4131 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
4132 (declare-function org-agenda-change-all-lines
"org-agenda"
4133 (newhead hdmarker
&optional fixface just-this
))
4134 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
4135 (declare-function org-agenda-maybe-redo
"org-agenda" ())
4136 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
4138 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
4139 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4140 "org-agenda" (&optional end
))
4141 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
4142 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
4143 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
4144 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
4145 (declare-function org-indent-mode
"org-indent" (&optional arg
))
4146 (declare-function parse-time-string
"parse-time" (string))
4147 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
4148 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
4149 (defvar remember-data-file
)
4150 (defvar texmathp-why
)
4151 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
4152 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
4154 (defvar org-latex-regexps
)
4156 ;;; Autoload and prepare some org modules
4158 ;; Some table stuff that needs to be defined here, because it is used
4159 ;; by the functions setting up org-mode or checking for table context.
4161 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
4162 "Detect an org-type or table-type table.")
4163 (defconst org-table-line-regexp
"^[ \t]*|"
4164 "Detect an org-type table line.")
4165 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
4166 "Detect an org-type table line.")
4167 (defconst org-table-hline-regexp
"^[ \t]*|-"
4168 "Detect an org-type table hline.")
4169 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
4170 "Detect a table-type table hline.")
4171 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
4172 "Detect the first line outside a table when searching from within it.
4173 This works for both table types.")
4175 ;; Autoload the functions in org-table.el that are needed by functions here.
4178 (org-autoload "org-table"
4179 '(org-table-begin org-table-blank-field org-table-end
)))
4182 (defun turn-on-orgtbl ()
4183 "Unconditionally turn on `orgtbl-mode'."
4184 (require 'org-table
)
4187 (defun org-at-table-p (&optional table-type
)
4188 "Return t if the cursor is inside an org-type table.
4189 If TABLE-TYPE is non-nil, also check for table.el-type tables."
4190 (if org-enable-table-editor
4192 (beginning-of-line 1)
4193 (looking-at (if table-type org-table-any-line-regexp
4194 org-table-line-regexp
)))
4196 (defsubst org-table-p
() (org-at-table-p))
4198 (defun org-at-table.el-p
()
4199 "Return t if and only if we are at a table.el table."
4200 (and (org-at-table-p 'any
)
4202 (goto-char (org-table-begin 'any
))
4203 (looking-at org-table1-hline-regexp
))))
4204 (defun org-table-recognize-table.el
()
4205 "If there is a table.el table nearby, recognize it and move into it."
4206 (if org-table-tab-recognizes-table.el
4207 (if (org-at-table.el-p
)
4209 (beginning-of-line 1)
4210 (if (looking-at org-table-dataline-regexp
)
4212 (if (looking-at org-table1-hline-regexp
)
4214 (beginning-of-line 2)
4215 (if (looking-at org-table-any-border-regexp
)
4216 (beginning-of-line -
1)))))
4217 (if (re-search-forward "|" (org-table-end t
) t
)
4220 (if (table--at-cell-p (point))
4222 (message "recognizing table.el table...")
4223 (table-recognize-table)
4224 (message "recognizing table.el table...done")))
4225 (error "This should not happen"))
4230 (defun org-at-table-hline-p ()
4231 "Return t if the cursor is inside a hline in a table."
4232 (if org-enable-table-editor
4234 (beginning-of-line 1)
4235 (looking-at org-table-hline-regexp
))
4238 (defvar org-table-clean-did-remove-column nil
)
4240 (defun org-table-map-tables (function &optional quietly
)
4241 "Apply FUNCTION to the start of all tables in the buffer."
4245 (goto-char (point-min))
4246 (while (re-search-forward org-table-any-line-regexp nil t
)
4248 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
4249 (beginning-of-line 1)
4250 (when (and (looking-at org-table-line-regexp
)
4251 ;; Exclude tables in src/example/verbatim/clocktable blocks
4252 (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
4253 (save-excursion (funcall function
))
4254 (or (looking-at org-table-line-regexp
)
4256 (re-search-forward org-table-any-border-regexp nil
1))))
4257 (unless quietly
(message "Mapping tables: done")))
4259 ;; Declare and autoload functions from ox.el and al.
4261 (declare-function org-export-get-environment
"ox"
4262 (&optional backend subtreep ext-plist
))
4263 (declare-function org-latex-guess-inputenc
"ox-latex" (header))
4265 ;; Declare and autoload functions from org-agenda.el
4268 (org-autoload "org-agenda"
4269 '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
4271 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock" (beg end
))
4272 (declare-function org-clock-update-mode-line
"org-clock" ())
4273 (declare-function org-resolve-clocks
"org-clock"
4274 (&optional also-non-dangling-p prompt last-valid
))
4275 (defvar org-clock-start-time
)
4276 (defvar org-clock-marker
(make-marker)
4277 "Marker recording the last clock-in.")
4278 (defvar org-clock-hd-marker
(make-marker)
4279 "Marker recording the last clock-in, but the headline position.")
4280 (defvar org-clock-heading
""
4281 "The heading of the current clock entry.")
4282 (defun org-clock-is-active ()
4283 "Return non-nil if clock is currently running.
4284 The return value is actually the clock marker."
4285 (marker-buffer org-clock-marker
))
4288 (org-autoload "org-clock" '(org-clock-remove-overlays
4289 org-clock-update-time-maybe
4290 org-clocktable-shift
)))
4292 (defun org-check-running-clock ()
4293 "Check if the current buffer contains the running clock.
4294 If yes, offer to stop it and to save the buffer with the changes."
4295 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4296 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4299 (when (y-or-n-p "Save changed buffer?")
4302 (defun org-clocktable-try-shift (dir n
)
4303 "Check if this line starts a clock table, if yes, shift the time block."
4304 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4305 (org-clocktable-shift dir n
)))
4308 (defun org-clock-persistence-insinuate ()
4309 "Set up hooks for clock persistence."
4310 (require 'org-clock
)
4311 (add-hook 'org-mode-hook
'org-clock-load
)
4312 (add-hook 'kill-emacs-hook
'org-clock-save
))
4314 ;; Define the variable already here, to make sure we have it.
4315 (defvar org-indent-mode nil
4316 "Non-nil if Org-Indent mode is enabled.
4317 Use the command `org-indent-mode' to change this variable.")
4319 ;; Autoload archiving code
4320 ;; The stuff that is needed for cycling and tags has to be defined here.
4322 (defgroup org-archive nil
4323 "Options concerning archiving in Org-mode."
4325 :group
'org-structure
)
4327 (defcustom org-archive-location
"%s_archive::"
4328 "The location where subtrees should be archived.
4330 The value of this variable is a string, consisting of two parts,
4331 separated by a double-colon. The first part is a filename and
4332 the second part is a headline.
4334 When the filename is omitted, archiving happens in the same file.
4335 %s in the filename will be replaced by the current file
4336 name (without the directory part). Archiving to a different file
4337 is useful to keep archived entries from contributing to the
4340 The archived entries will be filed as subtrees of the specified
4341 headline. When the headline is omitted, the subtrees are simply
4342 filed away at the end of the file, as top-level entries. Also in
4343 the heading you can use %s to represent the file name, this can be
4344 useful when using the same archive for a number of different files.
4346 Here are a few examples:
4348 If the current file is Projects.org, archive in file
4349 Projects.org_archive, as top-level trees. This is the default.
4351 \"::* Archived Tasks\"
4352 Archive in the current file, under the top-level headline
4353 \"* Archived Tasks\".
4355 \"~/org/archive.org::\"
4356 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4358 \"~/org/archive.org::* From %s\"
4359 Archive in file ~/org/archive.org (absolute path), under headlines
4360 \"From FILENAME\" where file name is the current file name.
4362 \"~/org/datetree.org::datetree/* Finished Tasks\"
4363 The \"datetree/\" string is special, signifying to archive
4364 items to the datetree. Items are placed in either the CLOSED
4365 date of the item, or the current date if there is no CLOSED date.
4366 The heading will be a subentry to the current date. There doesn't
4367 need to be a heading, but there always needs to be a slash after
4368 datetree. For example, to store archived items directly in the
4369 datetree, use \"~/org/datetree.org::datetree/\".
4371 \"basement::** Finished Tasks\"
4372 Archive in file ./basement (relative path), as level 3 trees
4373 below the level 2 heading \"** Finished Tasks\".
4375 You may set this option on a per-file basis by adding to the buffer a
4378 #+ARCHIVE: basement::** Finished Tasks
4380 You may also define it locally for a subtree by setting an ARCHIVE property
4381 in the entry. If such a property is found in an entry, or anywhere up
4382 the hierarchy, it will be used."
4386 (defcustom org-archive-tag
"ARCHIVE"
4387 "The tag that marks a subtree as archived.
4388 An archived subtree does not open during visibility cycling, and does
4389 not contribute to the agenda listings.
4390 After changing this, font-lock must be restarted in the relevant buffers to
4391 get the proper fontification."
4393 :group
'org-keywords
4396 (defcustom org-agenda-skip-archived-trees t
4397 "Non-nil means the agenda will skip any items located in archived trees.
4398 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4399 variable is no longer recommended, you should leave it at the value t.
4400 Instead, use the key `v' to cycle the archives-mode in the agenda."
4402 :group
'org-agenda-skip
4405 (defcustom org-columns-skip-archived-trees t
4406 "Non-nil means ignore archived trees when creating column view."
4408 :group
'org-properties
4411 (defcustom org-cycle-open-archived-trees nil
4412 "Non-nil means `org-cycle' will open archived trees.
4413 An archived tree is a tree marked with the tag ARCHIVE.
4414 When nil, archived trees will stay folded. You can still open them with
4415 normal outline commands like `show-all', but not with the cycling commands."
4420 (defcustom org-sparse-tree-open-archived-trees nil
4421 "Non-nil means sparse tree construction shows matches in archived trees.
4422 When nil, matches in these trees are highlighted, but the trees are kept in
4425 :group
'org-sparse-trees
4428 (defcustom org-sparse-tree-default-date-type
'scheduled-or-deadline
4429 "The default date type when building a sparse tree.
4430 When this is nil, a date is a scheduled or a deadline timestamp.
4431 Otherwise, these types are allowed:
4434 active: only active timestamps (<...>)
4435 inactive: only inactive timestamps (<...)
4436 scheduled: only scheduled timestamps
4437 deadline: only deadline timestamps"
4438 :type
'(choice (const :tag
"Scheduled or deadline" 'scheduled-or-deadline
)
4439 (const :tag
"All timestamps" all
)
4440 (const :tag
"Only active timestamps" active
)
4441 (const :tag
"Only inactive timestamps" inactive
)
4442 (const :tag
"Only scheduled timestamps" scheduled
)
4443 (const :tag
"Only deadline timestamps" deadline
))
4445 :group
'org-sparse-trees
)
4447 (defun org-cycle-hide-archived-subtrees (state)
4448 "Re-hide all archived subtrees after a visibility state change."
4449 (when (and (not org-cycle-open-archived-trees
)
4450 (not (memq state
'(overview folded
))))
4452 (let* ((globalp (memq state
'(contents all
)))
4453 (beg (if globalp
(point-min) (point)))
4454 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4455 (org-hide-archived-subtrees beg end
)
4457 (if (looking-at (concat ".*:" org-archive-tag
":"))
4458 (message "%s" (substitute-command-keys
4459 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4461 (defun org-force-cycle-archived ()
4462 "Cycle subtree even if it is archived."
4464 (setq this-command
'org-cycle
)
4465 (let ((org-cycle-open-archived-trees t
))
4466 (call-interactively 'org-cycle
)))
4468 (defun org-hide-archived-subtrees (beg end
)
4469 "Re-hide all archived subtrees after a visibility state change."
4471 (let* ((re (concat ":" org-archive-tag
":")))
4473 (while (re-search-forward re end t
)
4474 (when (org-at-heading-p)
4475 (org-flag-subtree t
)
4476 (org-end-of-subtree t
))))))
4478 (declare-function outline-end-of-heading
"outline" ())
4479 (declare-function outline-flag-region
"outline" (from to flag
))
4480 (defun org-flag-subtree (flag)
4482 (org-back-to-heading t
)
4483 (outline-end-of-heading)
4484 (outline-flag-region (point)
4485 (progn (org-end-of-subtree t
) (point))
4488 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4491 (org-autoload "org-archive"
4492 '(org-add-archive-files)))
4494 ;; Autoload Column View Code
4496 (declare-function org-columns-number-to-string
"org-colview" (n fmt
&optional printf
))
4497 (declare-function org-columns-get-format-and-top-level
"org-colview" ())
4498 (declare-function org-columns-compute
"org-colview" (property))
4500 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4501 '(org-columns-number-to-string
4502 org-columns-get-format-and-top-level
4504 org-columns-remove-overlays
))
4508 (declare-function org-id-store-link
"org-id")
4509 (declare-function org-id-locations-load
"org-id")
4510 (declare-function org-id-locations-save
"org-id")
4511 (defvar org-id-track-globally
)
4512 (org-autoload "org-id"
4515 org-id-get-with-outline-path-completion
4516 org-id-get-with-outline-drilling
))
4518 ;;; Variables for pre-computed regular expressions, all buffer local
4520 (defvar org-drawer-regexp
"^[ \t]*:PROPERTIES:[ \t]*$"
4521 "Matches first line of a hidden block.")
4522 (make-variable-buffer-local 'org-drawer-regexp
)
4523 (defvar org-todo-regexp nil
4524 "Matches any of the TODO state keywords.")
4525 (make-variable-buffer-local 'org-todo-regexp
)
4526 (defvar org-not-done-regexp nil
4527 "Matches any of the TODO state keywords except the last one.")
4528 (make-variable-buffer-local 'org-not-done-regexp
)
4529 (defvar org-not-done-heading-regexp nil
4530 "Matches a TODO headline that is not done.")
4531 (make-variable-buffer-local 'org-not-done-regexp
)
4532 (defvar org-todo-line-regexp nil
4533 "Matches a headline and puts TODO state into group 2 if present.")
4534 (make-variable-buffer-local 'org-todo-line-regexp
)
4535 (defvar org-complex-heading-regexp nil
4536 "Matches a headline and puts everything into groups:
4538 group 2: The todo keyword, maybe
4539 group 3: Priority cookie
4540 group 4: True headline
4542 (make-variable-buffer-local 'org-complex-heading-regexp
)
4543 (defvar org-complex-heading-regexp-format nil
4544 "Printf format to make regexp to match an exact headline.
4545 This regexp will match the headline of any node which has the
4546 exact headline text that is put into the format, but may have any
4547 TODO state, priority and tags.")
4548 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4549 (defvar org-todo-line-tags-regexp nil
4550 "Matches a headline and puts TODO state into group 2 if present.
4551 Also put tags into group 4 if tags are present.")
4552 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4553 (defvar org-ds-keyword-length
12
4554 "Maximum length of the DEADLINE and SCHEDULED keywords.")
4555 (make-variable-buffer-local 'org-ds-keyword-length
)
4556 (defvar org-deadline-regexp nil
4557 "Matches the DEADLINE keyword.")
4558 (make-variable-buffer-local 'org-deadline-regexp
)
4559 (defvar org-deadline-time-regexp nil
4560 "Matches the DEADLINE keyword together with a time stamp.")
4561 (make-variable-buffer-local 'org-deadline-time-regexp
)
4562 (defvar org-deadline-line-regexp nil
4563 "Matches the DEADLINE keyword and the rest of the line.")
4564 (make-variable-buffer-local 'org-deadline-line-regexp
)
4565 (defvar org-scheduled-regexp nil
4566 "Matches the SCHEDULED keyword.")
4567 (make-variable-buffer-local 'org-scheduled-regexp
)
4568 (defvar org-scheduled-time-regexp nil
4569 "Matches the SCHEDULED keyword together with a time stamp.")
4570 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4571 (defvar org-closed-time-regexp nil
4572 "Matches the CLOSED keyword together with a time stamp.")
4573 (make-variable-buffer-local 'org-closed-time-regexp
)
4575 (defvar org-keyword-time-regexp nil
4576 "Matches any of the 4 keywords, together with the time stamp.")
4577 (make-variable-buffer-local 'org-keyword-time-regexp
)
4578 (defvar org-keyword-time-not-clock-regexp nil
4579 "Matches any of the 3 keywords, together with the time stamp.")
4580 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4581 (defvar org-maybe-keyword-time-regexp nil
4582 "Matches a timestamp, possibly preceded by a keyword.")
4583 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4584 (defvar org-all-time-keywords nil
4585 "List of time keywords.")
4586 (make-variable-buffer-local 'org-all-time-keywords
)
4588 (defconst org-plain-time-of-day-regexp
4591 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4594 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4596 "Regular expression to match a plain time or time range.
4597 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4598 groups carry important information:
4600 1 the first time, range or not
4601 8 the second time, if it is a range.")
4603 (defconst org-plain-time-extension-regexp
4606 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4607 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4608 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4609 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4610 groups carry important information:
4613 9 minutes of duration")
4615 (defconst org-stamp-time-of-day-regexp
4617 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4618 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4620 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4621 "Regular expression to match a timestamp time or time range.
4622 After a match, the following groups carry important information:
4624 1 date plus weekday, for back referencing to make sure both times are on the same day
4625 2 the first time, range or not
4626 4 the second time, if it is a range.")
4628 (defconst org-startup-options
4629 '(("fold" org-startup-folded t
)
4630 ("overview" org-startup-folded t
)
4631 ("nofold" org-startup-folded nil
)
4632 ("showall" org-startup-folded nil
)
4633 ("showeverything" org-startup-folded showeverything
)
4634 ("content" org-startup-folded content
)
4635 ("indent" org-startup-indented t
)
4636 ("noindent" org-startup-indented nil
)
4637 ("hidestars" org-hide-leading-stars t
)
4638 ("showstars" org-hide-leading-stars nil
)
4639 ("odd" org-odd-levels-only t
)
4640 ("oddeven" org-odd-levels-only nil
)
4641 ("align" org-startup-align-all-tables t
)
4642 ("noalign" org-startup-align-all-tables nil
)
4643 ("inlineimages" org-startup-with-inline-images t
)
4644 ("noinlineimages" org-startup-with-inline-images nil
)
4645 ("latexpreview" org-startup-with-latex-preview t
)
4646 ("nolatexpreview" org-startup-with-latex-preview nil
)
4647 ("customtime" org-display-custom-times t
)
4648 ("logdone" org-log-done time
)
4649 ("lognotedone" org-log-done note
)
4650 ("nologdone" org-log-done nil
)
4651 ("lognoteclock-out" org-log-note-clock-out t
)
4652 ("nolognoteclock-out" org-log-note-clock-out nil
)
4653 ("logrepeat" org-log-repeat state
)
4654 ("lognoterepeat" org-log-repeat note
)
4655 ("logdrawer" org-log-into-drawer t
)
4656 ("nologdrawer" org-log-into-drawer nil
)
4657 ("logstatesreversed" org-log-states-order-reversed t
)
4658 ("nologstatesreversed" org-log-states-order-reversed nil
)
4659 ("nologrepeat" org-log-repeat nil
)
4660 ("logreschedule" org-log-reschedule time
)
4661 ("lognotereschedule" org-log-reschedule note
)
4662 ("nologreschedule" org-log-reschedule nil
)
4663 ("logredeadline" org-log-redeadline time
)
4664 ("lognoteredeadline" org-log-redeadline note
)
4665 ("nologredeadline" org-log-redeadline nil
)
4666 ("logrefile" org-log-refile time
)
4667 ("lognoterefile" org-log-refile note
)
4668 ("nologrefile" org-log-refile nil
)
4669 ("fninline" org-footnote-define-inline t
)
4670 ("nofninline" org-footnote-define-inline nil
)
4671 ("fnlocal" org-footnote-section nil
)
4672 ("fnauto" org-footnote-auto-label t
)
4673 ("fnprompt" org-footnote-auto-label nil
)
4674 ("fnconfirm" org-footnote-auto-label confirm
)
4675 ("fnplain" org-footnote-auto-label plain
)
4676 ("fnadjust" org-footnote-auto-adjust t
)
4677 ("nofnadjust" org-footnote-auto-adjust nil
)
4678 ("constcgs" constants-unit-system cgs
)
4679 ("constSI" constants-unit-system SI
)
4680 ("noptag" org-tag-persistent-alist nil
)
4681 ("hideblocks" org-hide-block-startup t
)
4682 ("nohideblocks" org-hide-block-startup nil
)
4683 ("beamer" org-startup-with-beamer-mode t
)
4684 ("entitiespretty" org-pretty-entities t
)
4685 ("entitiesplain" org-pretty-entities nil
))
4686 "Variable associated with STARTUP options for org-mode.
4687 Each element is a list of three items: the startup options (as written
4688 in the #+STARTUP line), the corresponding variable, and the value to set
4689 this variable to if the option is found. An optional forth element PUSH
4690 means to push this value onto the list in the variable.")
4692 (defun org-update-property-plist (key val props
)
4693 "Update PROPS with KEY and VAL."
4694 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4695 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4696 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4697 (previous (cdr (assoc key props
))))
4699 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4700 (cons (cons key val
) remainder
))))
4702 (defconst org-block-regexp
4703 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4704 "Regular expression for hiding blocks.")
4705 (defconst org-heading-keyword-regexp-format
4706 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4707 "Printf format for a regexp matching a headline with some keyword.
4708 This regexp will match the headline of any node which has the
4709 exact keyword that is put into the format. The keyword isn't in
4710 any group by default, but the stars and the body are.")
4711 (defconst org-heading-keyword-maybe-regexp-format
4712 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4713 "Printf format for a regexp matching a headline, possibly with some keyword.
4714 This regexp can match any headline with the specified keyword, or
4715 without a keyword. The keyword isn't in any group by default,
4716 but the stars and the body are.")
4718 (defun org-set-regexps-and-options ()
4719 "Precompute regular expressions for current buffer."
4720 (when (derived-mode-p 'org-mode
)
4721 (org-set-local 'org-todo-kwd-alist nil
)
4722 (org-set-local 'org-todo-key-alist nil
)
4723 (org-set-local 'org-todo-key-trigger nil
)
4724 (org-set-local 'org-todo-keywords-1 nil
)
4725 (org-set-local 'org-done-keywords nil
)
4726 (org-set-local 'org-todo-heads nil
)
4727 (org-set-local 'org-todo-sets nil
)
4728 (org-set-local 'org-todo-log-states nil
)
4729 (org-set-local 'org-file-properties nil
)
4730 (org-set-local 'org-file-tags nil
)
4731 (let ((re (org-make-options-regexp
4732 '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE" "FILETAGS"
4733 "TAGS" "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
4734 "SETUPFILE" "OPTIONS")
4735 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4737 (scripts org-use-sub-superscripts
)
4738 kwds kws0 kwsa key log value cat arch tags const links hw dws
4739 tail sep kws1 prio props ftags drawers ext-setup-or-nil setup-contents
4744 (goto-char (point-min))
4745 (while (or (and ext-setup-or-nil
4746 (string-match re ext-setup-or-nil start
)
4747 (setq start
(match-end 0)))
4748 (and (setq ext-setup-or-nil nil start
0)
4749 (re-search-forward re nil t
)))
4750 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4751 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4752 (if (stringp value
) (setq value
(org-trim value
)))
4754 ((equal key
"CATEGORY")
4756 ((member key
'("SEQ_TODO" "TODO"))
4757 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4758 ((equal key
"TYP_TODO")
4759 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4760 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4761 ;; general TODO-like setup
4762 (push (cons (intern (downcase (match-string 1 key
)))
4763 (org-split-string value splitre
)) kwds
))
4765 (setq tags
(append tags
(if tags
'("\\n") nil
)
4766 (org-split-string value splitre
))))
4767 ((equal key
"COLUMNS")
4768 (org-set-local 'org-columns-default-format value
))
4770 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4771 (push (cons (match-string 1 value
)
4772 (org-trim (match-string 2 value
)))
4774 ((equal key
"PRIORITIES")
4775 (setq prio
(org-split-string value
" +")))
4776 ((equal key
"PROPERTY")
4777 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4778 (setq props
(org-update-property-plist (match-string 1 value
)
4779 (match-string 2 value
)
4781 ((equal key
"FILETAGS")
4782 (when (string-match "\\S-" value
)
4787 (mapcar (lambda (x) (org-split-string x
":"))
4788 (org-split-string value
)))))))
4789 ((equal key
"DRAWERS")
4790 (setq drawers
(delete-dups (append org-drawers
(org-split-string value splitre
)))))
4791 ((equal key
"CONSTANTS")
4792 (setq const
(append const
(org-split-string value splitre
))))
4793 ((equal key
"STARTUP")
4794 (let ((opts (org-split-string value splitre
))
4796 (while (setq l
(pop opts
))
4797 (when (setq l
(assoc l org-startup-options
))
4798 (setq var
(nth 1 l
) val
(nth 2 l
))
4800 (set (make-local-variable var
) val
)
4801 (if (not (listp (symbol-value var
)))
4802 (set (make-local-variable var
) nil
))
4803 (set (make-local-variable var
) (symbol-value var
))
4804 (add-to-list var val
))))))
4805 ((equal key
"ARCHIVE")
4807 (remove-text-properties 0 (length arch
)
4808 '(face t fontified t
) arch
))
4809 ((equal key
"OPTIONS")
4810 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4811 (setq scripts
(read (match-string 2 value
)))))
4812 ((equal key
"SETUPFILE")
4813 (setq setup-contents
(org-file-contents
4815 (org-remove-double-quotes value
))
4817 (if (not ext-setup-or-nil
)
4818 (setq ext-setup-or-nil setup-contents start
0)
4819 (setq ext-setup-or-nil
4820 (concat (substring ext-setup-or-nil
0 start
)
4821 "\n" setup-contents
"\n"
4822 (substring ext-setup-or-nil start
)))))))
4823 ;; search for property blocks
4824 (goto-char (point-min))
4825 (while (re-search-forward org-block-regexp nil t
)
4826 (when (equal "PROPERTY" (upcase (match-string 1)))
4827 (setq value
(replace-regexp-in-string
4828 "[\n\r]" " " (match-string 4)))
4829 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4830 (setq props
(org-update-property-plist (match-string 1 value
)
4831 (match-string 2 value
)
4833 (org-set-local 'org-use-sub-superscripts scripts
)
4835 (org-set-local 'org-category
(intern cat
))
4836 (push (cons "CATEGORY" cat
) props
))
4838 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4839 (setq prio
(mapcar 'string-to-char prio
))
4840 (org-set-local 'org-highest-priority
(nth 0 prio
))
4841 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4842 (org-set-local 'org-default-priority
(nth 2 prio
)))
4843 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4844 (and ftags
(org-set-local 'org-file-tags
4845 (mapcar 'org-add-prop-inherited ftags
)))
4846 (and drawers
(org-set-local 'org-drawers drawers
))
4847 (and arch
(org-set-local 'org-archive-location arch
))
4848 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4849 ;; Process the TODO keywords
4851 ;; Use the global values as if they had been given locally.
4852 (setq kwds
(default-value 'org-todo-keywords
))
4853 (if (stringp (car kwds
))
4854 (setq kwds
(list (cons org-todo-interpretation
4855 (default-value 'org-todo-keywords
)))))
4856 (setq kwds
(reverse kwds
)))
4857 (setq kwds
(nreverse kwds
))
4859 (while (setq kws
(pop kwds
))
4861 (run-hook-with-args-until-success
4862 'org-todo-setup-filter-hook kws
)
4864 (setq inter
(pop kws
) sep
(member "|" kws
)
4865 kws0
(delete "|" (copy-sequence kws
))
4870 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4872 (setq kw
(match-string 1 x
)
4873 key
(and (match-end 2) (match-string 2 x
))
4874 log
(org-extract-log-state-settings x
))
4875 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4876 (and log
(push log org-todo-log-states
))
4878 (error "Invalid TODO keyword %s" x
)))
4880 kwsa
(if kwsa
(append '((:startgroup
))
4884 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4885 tail
(list inter hw
(car dws
) (org-last dws
))))
4886 (add-to-list 'org-todo-heads hw
'append
)
4887 (push kws1 org-todo-sets
)
4888 (setq org-done-keywords
(append org-done-keywords dws nil
))
4889 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4890 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4891 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4892 (setq org-todo-sets
(nreverse org-todo-sets
)
4893 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4894 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4895 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4896 ;; Process the constants
4899 (while (setq e
(pop const
))
4900 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4901 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4902 (setq org-table-formula-constants-local cst
)))
4904 ;; Process the tags.
4907 (while (setq e
(pop tags
))
4909 ((equal e
"{") (push '(:startgroup
) tgs
))
4910 ((equal e
"}") (push '(:endgroup
) tgs
))
4911 ((equal e
"\\n") (push '(:newline
) tgs
))
4912 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4913 (push (cons (match-string 1 e
)
4914 (string-to-char (match-string 2 e
)))
4916 (t (push (list e
) tgs
))))
4917 (org-set-local 'org-tag-alist nil
)
4918 (while (setq e
(pop tgs
))
4919 (or (and (stringp (car e
))
4920 (assoc (car e
) org-tag-alist
))
4921 (push e org-tag-alist
)))))
4923 ;; Compute the regular expressions and other local variables.
4924 ;; Using `org-outline-regexp-bol' would complicate them much,
4925 ;; because of the fixed white space at the end of that string.
4926 (if (not org-done-keywords
)
4927 (setq org-done-keywords
(and org-todo-keywords-1
4928 (list (org-last org-todo-keywords-1
)))))
4929 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4930 (length org-scheduled-string
)
4931 (length org-clock-string
)
4932 (length org-closed-string
)))
4934 (concat "^[ \t]*:\\("
4935 (mapconcat 'regexp-quote org-drawers
"\\|")
4937 org-not-done-keywords
4938 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4941 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4945 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4947 org-not-done-heading-regexp
4948 (format org-heading-keyword-regexp-format org-not-done-regexp
)
4949 org-todo-line-regexp
4950 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
4951 org-complex-heading-regexp
4952 (concat "^\\(\\*+\\)"
4953 "\\(?: +" org-todo-regexp
"\\)?"
4954 "\\(?: +\\(\\[#.\\]\\)\\)?"
4955 "\\(?: +\\(.*?\\)\\)??"
4956 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
4958 org-complex-heading-regexp-format
4959 (concat "^\\(\\*+\\)"
4960 "\\(?: +" org-todo-regexp
"\\)?"
4961 "\\(?: +\\(\\[#.\\]\\)\\)?"
4963 ;; Stats cookies can be stuck to body.
4964 "\\(?:\\[[0-9%%/]+\\] *\\)?"
4966 "\\(?: *\\[[0-9%%/]+\\]\\)?"
4968 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
4970 org-todo-line-tags-regexp
4971 (concat "^\\(\\*+\\)"
4972 "\\(?: +" org-todo-regexp
"\\)?"
4973 "\\(?: +\\(.*?\\)\\)??"
4974 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
4976 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4977 org-deadline-time-regexp
4978 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4979 org-deadline-line-regexp
4980 (concat "\\<\\(" org-deadline-string
"\\).*")
4981 org-scheduled-regexp
4982 (concat "\\<" org-scheduled-string
)
4983 org-scheduled-time-regexp
4984 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4985 org-closed-time-regexp
4986 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4987 org-keyword-time-regexp
4988 (concat "\\<\\(" org-scheduled-string
4989 "\\|" org-deadline-string
4990 "\\|" org-closed-string
4991 "\\|" org-clock-string
"\\)"
4992 " *[[<]\\([^]>]+\\)[]>]")
4993 org-keyword-time-not-clock-regexp
4994 (concat "\\<\\(" org-scheduled-string
4995 "\\|" org-deadline-string
4996 "\\|" org-closed-string
4998 " *[[<]\\([^]>]+\\)[]>]")
4999 org-maybe-keyword-time-regexp
5000 (concat "\\(\\<\\(" org-scheduled-string
5001 "\\|" org-deadline-string
5002 "\\|" org-closed-string
5003 "\\|" org-clock-string
"\\)\\)?"
5004 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
5005 org-all-time-keywords
5006 (mapcar (lambda (w) (substring w
0 -
1))
5007 (list org-scheduled-string org-deadline-string
5008 org-clock-string org-closed-string
)))
5009 (org-compute-latex-and-related-regexp)
5010 (org-set-font-lock-defaults))))
5012 (defun org-file-contents (file &optional noerror
)
5013 "Return the contents of FILE, as a string."
5015 (not (file-readable-p file
)))
5018 (message "Cannot read file \"%s\"" file
)
5021 (error "Cannot read file \"%s\"" file
))
5023 (insert-file-contents file
)
5026 (defun org-extract-log-state-settings (x)
5027 "Extract the log state setting from a TODO keyword string.
5028 This will extract info from a string like \"WAIT(w@/!)\"."
5029 (let (kw key log1 log2
)
5030 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
5031 (setq kw
(match-string 1 x
)
5032 key
(and (match-end 2) (match-string 2 x
))
5033 log1
(and (match-end 3) (match-string 3 x
))
5034 log2
(and (match-end 4) (match-string 4 x
)))
5037 (and log1
(if (equal log1
"!") 'time
'note
))
5038 (and log2
(if (equal log2
"!") 'time
'note
)))))))
5040 (defun org-remove-keyword-keys (list)
5041 "Remove a pair of parenthesis at the end of each string in LIST."
5043 (if (string-match "(.*)$" x
)
5044 (substring x
0 (match-beginning 0))
5048 (defun org-assign-fast-keys (alist)
5049 "Assign fast keys to a keyword-key alist.
5050 Respect keys that are already there."
5051 (let (new e
(alt ?
0))
5052 (while (setq e
(pop alist
))
5053 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
5054 (cdr e
)) ;; Key already assigned.
5056 (let ((clist (string-to-list (downcase (car e
))))
5057 (used (append new alist
)))
5058 (when (= (car clist
) ?
@)
5060 (while (and clist
(rassoc (car clist
) used
))
5063 (while (rassoc alt used
)
5065 (push (cons (car e
) (or (car clist
) alt
)) new
))))
5068 ;;; Some variables used in various places
5070 (defvar org-window-configuration nil
5071 "Used in various places to store a window configuration.")
5072 (defvar org-selected-window nil
5073 "Used in various places to store a window configuration.")
5074 (defvar org-finish-function nil
5075 "Function to be called when `C-c C-c' is used.
5076 This is for getting out of special buffers like capture.")
5079 ;; FIXME: Occasionally check by commenting these, to make sure
5080 ;; no other functions uses these, forgetting to let-bind them.
5081 (org-no-warnings (defvar entry
)) ;; unprefixed, from calendar.el
5082 (defvar org-last-state
)
5083 (org-no-warnings (defvar date
)) ;; unprefixed, from calendar.el
5085 ;; Defined somewhere in this file, but used before definition.
5086 (defvar org-entities
) ;; defined in org-entities.el
5087 (defvar org-struct-menu
)
5088 (defvar org-org-menu
)
5089 (defvar org-tbl-menu
)
5091 ;;;; Define the Org-mode
5093 ;; We use a before-change function to check if a table might need
5095 (defvar org-table-may-need-update t
5096 "Indicates that a table might need an update.
5097 This variable is set by `org-before-change-function'.
5098 `org-table-align' sets it back to nil.")
5099 (defun org-before-change-function (beg end
)
5100 "Every change indicates that a table might need an update."
5101 (setq org-table-may-need-update t
))
5102 (defvar org-mode-map
)
5103 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
5104 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
5105 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
5106 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
5107 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
5108 (defvar org-table-buffer-is-an nil
)
5110 (defvar bidi-paragraph-direction
)
5111 (defvar buffer-face-mode-face
)
5114 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
5115 (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"))
5116 (require 'noutline
"noutline" 'noerror
) ;; stock XEmacs does not have it
5118 ;; Other stuff we need.
5119 (require 'time-date
)
5120 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
5124 ;; (require 'org-macs) moved higher up in the file before it is first used
5125 (require 'org-entities
)
5126 ;; (require 'org-compat) moved higher up in the file before it is first used
5127 (require 'org-faces
)
5129 (require 'org-pcomplete
)
5131 (require 'org-footnote
)
5132 (require 'org-macro
)
5138 (define-derived-mode org-mode outline-mode
"Org"
5139 "Outline-based notes management and organizer, alias
5140 \"Carsten's outline-mode for keeping track of everything.\"
5142 Org-mode develops organizational tasks around a NOTES file which
5143 contains information about projects as plain text. Org-mode is
5144 implemented on top of outline-mode, which is ideal to keep the content
5145 of large files well structured. It supports ToDo items, deadlines and
5146 time stamps, which magically appear in the diary listing of the Emacs
5147 calendar. Tables are easily created with a built-in table editor.
5148 Plain text URL-like links connect to websites, emails (VM), Usenet
5149 messages (Gnus), BBDB entries, and any files related to the project.
5150 For printing and sharing of notes, an Org-mode file (or a part of it)
5151 can be exported as a structured ASCII or HTML file.
5153 The following commands are available:
5157 ;; Get rid of Outline menus, they are not needed
5158 ;; Need to do this here because define-derived-mode sets up
5159 ;; the keymap so late. Still, it is a waste to call this each time
5160 ;; we switch another buffer into org-mode.
5161 (if (featurep 'xemacs
)
5162 (when (boundp 'outline-mode-menu-heading
)
5163 ;; Assume this is Greg's port, it uses easymenu
5164 (easy-menu-remove outline-mode-menu-heading
)
5165 (easy-menu-remove outline-mode-menu-show
)
5166 (easy-menu-remove outline-mode-menu-hide
))
5167 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
5168 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
5169 (define-key org-mode-map
[menu-bar show
] 'undefined
))
5171 (org-load-modules-maybe)
5172 (easy-menu-add org-org-menu
)
5173 (easy-menu-add org-tbl-menu
)
5174 (org-install-agenda-files-menu)
5175 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
5176 (add-to-invisibility-spec '(org-cwidth))
5177 (add-to-invisibility-spec '(org-hide-block . t
))
5178 (when (featurep 'xemacs
)
5179 (org-set-local 'line-move-ignore-invisible t
))
5180 (org-set-local 'outline-regexp org-outline-regexp
)
5181 (org-set-local 'outline-level
'org-outline-level
)
5182 (setq bidi-paragraph-direction
'left-to-right
)
5183 (when (and org-ellipsis
5184 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
5185 (fboundp 'make-glyph-code
))
5186 (unless org-display-table
5187 (setq org-display-table
(make-display-table)))
5188 (set-display-table-slot
5191 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
5193 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
5194 (setq buffer-display-table org-display-table
))
5195 (org-set-regexps-and-options)
5196 (when (and org-tag-faces
(not org-tags-special-faces-re
))
5197 ;; tag faces set outside customize.... force initialization.
5198 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
5200 (org-set-local 'calc-embedded-open-mode
"# ")
5201 ;; Modify a few syntax entries
5202 (modify-syntax-entry ?
< "(")
5203 (modify-syntax-entry ?
> ")")
5204 (modify-syntax-entry ?
{ "(")
5205 (modify-syntax-entry ?
} ")")
5206 (modify-syntax-entry ?
@ "w")
5207 (modify-syntax-entry ?
\" "\"")
5208 (if org-startup-truncated
(setq truncate-lines t
))
5209 (when org-startup-indented
(require 'org-indent
) (org-indent-mode 1))
5210 (org-set-local 'font-lock-unfontify-region-function
5211 'org-unfontify-region
)
5212 ;; Activate before-change-function
5213 (org-set-local 'org-table-may-need-update t
)
5214 (org-add-hook 'before-change-functions
'org-before-change-function nil
5216 ;; Check for running clock before killing a buffer
5217 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
5218 ;; Initialize macros templates.
5219 (org-macro-initialize-templates)
5220 ;; Initialize radio targets.
5221 (org-update-radio-target-regexp)
5223 (org-set-local 'indent-line-function
'org-indent-line
)
5224 (org-set-local 'indent-region-function
'org-indent-region
)
5225 ;; Filling and auto-filling.
5228 (org-setup-comments-handling)
5229 ;; Beginning/end of defun
5230 (org-set-local 'beginning-of-defun-function
'org-back-to-heading
)
5231 (org-set-local 'end-of-defun-function
(lambda () (interactive) (org-end-of-subtree nil t
)))
5232 ;; Next error for sparse trees
5233 (org-set-local 'next-error-function
'org-occur-next-match
)
5234 ;; Make sure dependence stuff works reliably, even for users who set it
5236 (if org-enforce-todo-dependencies
5237 (add-hook 'org-blocker-hook
5238 'org-block-todo-from-children-or-siblings-or-parent
)
5239 (remove-hook 'org-blocker-hook
5240 'org-block-todo-from-children-or-siblings-or-parent
))
5241 (if org-enforce-todo-checkbox-dependencies
5242 (add-hook 'org-blocker-hook
5243 'org-block-todo-from-checkboxes
)
5244 (remove-hook 'org-blocker-hook
5245 'org-block-todo-from-checkboxes
))
5247 ;; Align options lines
5249 'align-mode-rules-list
5250 '((org-in-buffer-settings
5251 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
5252 (modes .
'(org-mode)))))
5255 (org-set-local 'imenu-create-index-function
5256 'org-imenu-get-tree
)
5258 ;; Make isearch reveal context
5259 (if (or (featurep 'xemacs
)
5260 (not (boundp 'outline-isearch-open-invisible-function
)))
5261 ;; Emacs 21 and XEmacs make use of the hook
5262 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
5263 ;; Emacs 22 deals with this through a special variable
5264 (org-set-local 'outline-isearch-open-invisible-function
5265 (lambda (&rest ignore
) (org-show-context 'isearch
))))
5267 ;; Setup the pcomplete hooks
5268 (set (make-local-variable 'pcomplete-command-completion-function
)
5269 'org-pcomplete-initial
)
5270 (set (make-local-variable 'pcomplete-command-name-function
)
5271 'org-command-at-point
)
5272 (set (make-local-variable 'pcomplete-default-completion-function
)
5274 (set (make-local-variable 'pcomplete-parse-arguments-function
)
5275 'org-parse-arguments
)
5276 (set (make-local-variable 'pcomplete-termination-string
) "")
5277 (when (>= emacs-major-version
23)
5278 (set (make-local-variable 'buffer-face-mode-face
) 'org-default
))
5280 ;; If empty file that did not turn on org-mode automatically, make it to.
5281 (if (and org-insert-mode-line-in-empty-file
5282 (org-called-interactively-p 'any
)
5283 (= (point-min) (point-max)))
5284 (insert "# -*- mode: org -*-\n\n"))
5285 (unless org-inhibit-startup
5287 (and org-startup-with-beamer-mode
(org-beamer-mode))
5288 (when org-startup-align-all-tables
5289 (org-table-map-tables 'org-table-align
'quietly
))
5290 (when org-startup-with-inline-images
5291 (org-display-inline-images))
5292 (when org-startup-with-latex-preview
5293 (org-preview-latex-fragment))
5294 (unless org-inhibit-startup-visibility-stuff
5295 (org-set-startup-visibility))))
5296 ;; Try to set org-hide correctly
5297 (set-face-foreground 'org-hide
(org-find-invisible-foreground)))
5299 (defvar org-mode-transpose-word-syntax-table
5300 (let ((st (make-syntax-table)))
5301 (mapc (lambda(c) (modify-syntax-entry
5302 (string-to-char (car c
)) "w p" st
))
5306 (when (fboundp 'abbrev-table-put
)
5307 (abbrev-table-put org-mode-abbrev-table
5308 :parents
(list text-mode-abbrev-table
)))
5310 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5313 (defun org-find-invisible-foreground ()
5314 (let ((candidates (remove
5317 (list (face-background 'default
)
5318 (face-background 'org-default
))
5321 (when (boundp alist
)
5322 (cdr (assoc 'background-color
(symbol-value alist
)))))
5323 '(default-frame-alist initial-frame-alist window-system-default-frame-alist
))
5324 (list (face-foreground 'org-hide
))))))
5325 (car (remove nil candidates
))))
5327 (defun org-current-time (&optional rounding-minutes past
)
5328 "Current time, possibly rounded to ROUNDING-MINUTES.
5329 When ROUNDING-MINUTES is not an integer, fall back on the car of
5330 `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
5331 the rounding returns a past time."
5332 (let ((r (or (and (integerp rounding-minutes
) rounding-minutes
)
5333 (car org-time-stamp-rounding-minutes
)))
5334 (time (decode-time)) res
)
5339 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5341 (if (and past
(< (org-float-time (time-subtract (current-time) res
)) 0))
5342 (seconds-to-time (- (org-float-time res
) (* r
60)))
5346 "Return today date, considering `org-extend-today-until'."
5348 (time-subtract (current-time)
5349 (list 0 (* 3600 org-extend-today-until
) 0))))
5351 ;;;; Font-Lock stuff, including the activators
5353 (defvar org-mouse-map
(make-sparse-keymap))
5354 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5355 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5356 (when org-mouse-1-follows-link
5357 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5358 (when org-tab-follows-link
5359 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5360 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5362 (require 'font-lock
)
5364 (defconst org-non-link-chars
"]\t\n\r<>")
5365 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5366 "shell" "elisp" "doi" "message"))
5367 (defvar org-link-types-re nil
5368 "Matches a link that has a url-like prefix like \"http:\"")
5369 (defvar org-link-re-with-space nil
5370 "Matches a link with spaces, optional angular brackets around it.")
5371 (defvar org-link-re-with-space2 nil
5372 "Matches a link with spaces, optional angular brackets around it.")
5373 (defvar org-link-re-with-space3 nil
5374 "Matches a link with spaces, only for internal part in bracket links.")
5375 (defvar org-angle-link-re nil
5376 "Matches link with angular brackets, spaces are allowed.")
5377 (defvar org-plain-link-re nil
5378 "Matches plain link, without spaces.")
5379 (defvar org-bracket-link-regexp nil
5380 "Matches a link in double brackets.")
5381 (defvar org-bracket-link-analytic-regexp nil
5382 "Regular expression used to analyze links.
5383 Here is what the match groups contain after a match:
5389 (defvar org-bracket-link-analytic-regexp
++ nil
5390 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5391 (defvar org-any-link-re nil
5392 "Regular expression matching any link.")
5394 (defconst org-match-sexp-depth
3
5395 "Number of stacked braces for sub/superscript matching.")
5397 (defun org-create-multibrace-regexp (left right n
)
5398 "Create a regular expression which will match a balanced sexp.
5399 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5400 as single character strings.
5401 The regexp returned will match the entire expression including the
5402 delimiters. It will also define a single group which contains the
5403 match except for the outermost delimiters. The maximum depth of
5404 stacked delimiters is N. Escaping delimiters is not possible."
5405 (let* ((nothing (concat "[^" left right
"]*?"))
5408 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5411 re
(concat re or next
)
5412 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5413 (concat left
"\\(" re
"\\)" right
)))
5415 (defvar org-match-substring-regexp
5417 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5418 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5420 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5422 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5423 "The regular expression matching a sub- or superscript.")
5425 (defvar org-match-substring-with-braces-regexp
5427 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5428 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5430 "The regular expression matching a sub- or superscript, forcing braces.")
5432 (defun org-make-link-regexps ()
5433 "Update the link regular expressions.
5434 This should be called after the variable `org-link-types' has changed."
5435 (setq org-link-types-re
5437 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
5438 org-link-re-with-space
5440 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5441 "\\([^" org-non-link-chars
" ]"
5442 "[^" org-non-link-chars
"]*"
5443 "[^" org-non-link-chars
" ]\\)>?")
5444 org-link-re-with-space2
5446 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5447 "\\([^" org-non-link-chars
" ]"
5449 "[^" org-non-link-chars
" ]\\)>?")
5450 org-link-re-with-space3
5452 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5453 "\\([^" org-non-link-chars
" ]"
5457 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5458 "\\([^" org-non-link-chars
" ]"
5459 "[^" org-non-link-chars
"]*"
5463 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5464 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5465 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5466 org-bracket-link-regexp
5467 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5468 org-bracket-link-analytic-regexp
5471 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
5474 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5476 org-bracket-link-analytic-regexp
++
5479 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
5482 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5485 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5486 org-angle-link-re
"\\)\\|\\("
5487 org-plain-link-re
"\\)")))
5489 (org-make-link-regexps)
5491 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5492 "Regular expression for fast time stamp matching.")
5493 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5494 "Regular expression for fast time stamp matching.")
5495 (defconst org-ts-regexp0
5496 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5497 "Regular expression matching time strings for analysis.
5498 This one does not require the space after the date, so it can be used
5499 on a string that terminates immediately after the date.")
5500 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5501 "Regular expression matching time strings for analysis.")
5502 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5503 "Regular expression matching time stamps, with groups.")
5504 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5505 "Regular expression matching time stamps (also [..]), with groups.")
5506 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5507 "Regular expression matching a time stamp range.")
5508 (defconst org-tr-regexp-both
5509 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5510 "Regular expression matching a time stamp range.")
5511 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5512 org-ts-regexp
"\\)?")
5513 "Regular expression matching a time stamp or time stamp range.")
5514 (defconst org-tsr-regexp-both
5515 (concat "[^][]" ;; Don't activate dates in links
5516 org-ts-regexp-both
"\\(--?-?"
5517 org-ts-regexp-both
"\\)?")
5518 "Regular expression matching a time stamp or time stamp range.
5519 The time stamps may be either active or inactive.")
5521 (defvar org-emph-face nil
)
5523 (defun org-do-emphasis-faces (limit)
5524 "Run through the buffer and add overlays to emphasized strings."
5526 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5527 (if (not (= (char-after (match-beginning 3))
5528 (char-after (match-beginning 4))))
5531 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5532 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5536 (org-remove-flyspell-overlays-in
5537 (match-beginning 0) (match-end 0)))
5538 (add-text-properties (match-beginning 2) (match-end 2)
5539 '(font-lock-multiline t org-emphasis t
))
5540 (when org-hide-emphasis-markers
5541 (add-text-properties (match-end 4) (match-beginning 5)
5542 '(invisible org-link
))
5543 (add-text-properties (match-beginning 3) (match-end 3)
5544 '(invisible org-link
)))))
5548 (defun org-emphasize (&optional char
)
5549 "Insert or change an emphasis, i.e. a font like bold or italic.
5550 If there is an active region, change that region to a new emphasis.
5551 If there is no region, just insert the marker characters and position
5552 the cursor between them.
5553 CHAR should be either the marker character, or the first character of the
5554 HTML tag associated with that emphasis. If CHAR is a space, the means
5555 to remove the emphasis of the selected region.
5556 If char is not given (for example in an interactive call) it
5557 will be prompted for."
5559 (let ((eal org-emphasis-alist
) e det
5560 (erc org-emphasis-regexp-components
)
5562 (string "") beg end move tag c s
)
5563 (if (org-region-active-p)
5564 (setq beg
(region-beginning) end
(region-end)
5565 string
(buffer-substring beg end
))
5568 (while (setq e
(pop eal
))
5569 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
5571 (push (cons c
(string-to-char (car e
))) det
)
5572 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
5573 (substring tag
1)))))
5574 (setq det
(nreverse det
))
5576 (message "%s" (concat "Emphasis marker or tag:" prompt
))
5577 (setq char
(read-char-exclusive)))
5578 (setq char
(or (cdr (assoc char det
)) char
))
5579 (if (equal char ?\
)
5580 (setq s
"" move nil
)
5581 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5582 (error "No such emphasis marker: \"%c\"" char
))
5583 (setq s
(char-to-string char
)))
5584 (while (and (> (length string
) 1)
5585 (equal (substring string
0 1) (substring string -
1))
5586 (assoc (substring string
0 1) org-emphasis-alist
))
5587 (setq string
(substring string
1 -
1)))
5588 (setq string
(concat s string s
))
5589 (if beg
(delete-region beg end
))
5591 (string-match (concat "[" (nth 0 erc
) "\n]")
5592 (char-to-string (char-before (point)))))
5595 (string-match (concat "[" (nth 1 erc
) "\n]")
5596 (char-to-string (char-after (point)))))
5597 (insert " ") (backward-char 1))
5599 (and move
(backward-char 1))))
5601 (defconst org-nonsticky-props
5602 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link
))
5604 (defsubst org-rear-nonsticky-at
(pos)
5605 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5607 (defun org-activate-plain-links (limit)
5608 "Run through the buffer and add overlays to links."
5611 (when (and (re-search-forward (concat org-plain-link-re
) limit t
)
5612 (not (org-in-src-block-p)))
5613 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5614 (setq f
(get-text-property (match-beginning 0) 'face
))
5615 (setq hl
(org-match-string-no-properties 0))
5616 (if (or (eq f
'org-tag
)
5617 (and (listp f
) (memq 'org-tag f
)))
5619 (add-text-properties (match-beginning 0) (match-end 0)
5620 (list 'mouse-face
'highlight
5622 'htmlize-link
`(:uri
,hl
)
5623 'keymap org-mouse-map
))
5624 (org-rear-nonsticky-at (match-end 0)))
5627 (defun org-activate-code (limit)
5628 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5630 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5631 (remove-text-properties (match-beginning 0) (match-end 0)
5632 '(display t invisible t intangible t
))
5635 (defcustom org-src-fontify-natively nil
5636 "When non-nil, fontify code in code blocks."
5639 :group
'org-appearance
5642 (defcustom org-allow-promoting-top-level-subtree nil
5643 "When non-nil, allow promoting a top level subtree.
5644 The leading star of the top level headline will be replaced
5648 :group
'org-appearance
)
5650 (defun org-fontify-meta-lines-and-blocks (limit)
5652 (org-fontify-meta-lines-and-blocks-1 limit
)
5653 (error (message "org-mode fontification error"))))
5655 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5656 "Fontify #+ lines and blocks, in the correct ways."
5657 (let ((case-fold-search t
))
5658 (if (re-search-forward
5659 "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5661 (let ((beg (match-beginning 0))
5662 (block-start (match-end 0))
5664 (lang (match-string 7))
5665 (beg1 (line-beginning-position 2))
5666 (dc1 (downcase (match-string 2)))
5667 (dc3 (downcase (match-string 3)))
5668 end end1 quoting block-type ovl
)
5670 ((member dc1
'("+html:" "+ascii:" "+latex:"))
5671 ;; a single line of backend-specific content
5672 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5673 (remove-text-properties (match-beginning 0) (match-end 0)
5674 '(display t invisible t intangible t
))
5675 (add-text-properties (match-beginning 1) (match-end 3)
5676 '(font-lock-fontified t face org-meta-line
))
5677 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5678 '(font-lock-fontified t face org-block
))
5679 ; for backend-specific code
5681 ((and (match-end 4) (equal dc3
"+begin"))
5683 (setq block-type
(downcase (match-string 5))
5684 quoting
(member block-type org-protecting-blocks
))
5685 (when (re-search-forward
5686 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5687 nil t
) ;; on purpose, we look further than LIMIT
5688 (setq end
(min (point-max) (match-end 0))
5689 end1
(min (point-max) (1- (match-beginning 0))))
5690 (setq block-end
(match-beginning 0))
5692 (remove-text-properties beg end
5693 '(display t invisible t intangible t
)))
5694 (add-text-properties
5696 '(font-lock-fontified t font-lock-multiline t
))
5697 (add-text-properties beg beg1
'(face org-meta-line
))
5698 (add-text-properties end1
(min (point-max) (1+ end
))
5699 '(face org-meta-line
)) ; for end_src
5701 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5702 (org-src-font-lock-fontify-block lang block-start block-end
)
5703 ;; remove old background overlays
5705 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5706 (delete-overlay ov
)))
5707 (overlays-at (/ (+ beg1 block-end
) 2)))
5708 ;; add a background overlay
5709 (setq ovl
(make-overlay beg1 block-end
))
5710 (overlay-put ovl
'face
'org-block-background
)
5711 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5713 (add-text-properties beg1
(min (point-max) (1+ end1
))
5714 '(face org-block
))) ; end of source block
5715 ((not org-fontify-quote-and-verse-blocks
))
5716 ((string= block-type
"quote")
5717 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-quote
)))
5718 ((string= block-type
"verse")
5719 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-verse
))))
5720 (add-text-properties beg beg1
'(face org-block-begin-line
))
5721 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5722 '(face org-block-end-line
))
5724 ((member dc1
'("+title:" "+author:" "+email:" "+date:"))
5725 (add-text-properties
5727 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5728 '(font-lock-fontified t invisible t
)
5729 '(font-lock-fontified t face org-document-info-keyword
)))
5730 (add-text-properties
5731 (match-beginning 6) (min (point-max) (1+ (match-end 6)))
5732 (if (string-equal dc1
"+title:")
5733 '(font-lock-fontified t face org-document-title
)
5734 '(font-lock-fontified t face org-document-info
))))
5735 ((or (equal dc1
"+results")
5736 (member dc1
'("+begin:" "+end:" "+caption:" "+label:"
5737 "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
5738 "+call:" "+header:" "+headers:" "+name:"))
5739 (and (match-end 4) (equal dc3
"+attr")))
5740 (add-text-properties
5742 '(font-lock-fontified t face org-meta-line
))
5744 ((member dc3
'(" " ""))
5745 (add-text-properties
5747 '(font-lock-fontified t face font-lock-comment-face
)))
5748 ((not (member (char-after beg
) '(?\ ?
\t)))
5749 ;; just any other in-buffer setting, but not indented
5750 (add-text-properties
5752 '(font-lock-fontified t face org-meta-line
))
5756 (defun org-activate-angle-links (limit)
5757 "Run through the buffer and add overlays to links."
5758 (if (and (re-search-forward org-angle-link-re limit t
)
5759 (not (org-in-src-block-p)))
5761 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5762 (add-text-properties (match-beginning 0) (match-end 0)
5763 (list 'mouse-face
'highlight
5764 'keymap org-mouse-map
))
5765 (org-rear-nonsticky-at (match-end 0))
5768 (defun org-activate-footnote-links (limit)
5769 "Run through the buffer and add overlays to footnotes."
5770 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5772 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5773 (org-remove-flyspell-overlays-in beg end
)
5774 (add-text-properties beg end
5775 (list 'mouse-face
'highlight
5776 'keymap org-mouse-map
5778 (if (= (point-at-bol) beg
)
5779 "Footnote definition"
5780 "Footnote reference")
5781 'font-lock-fontified t
5782 'font-lock-multiline t
5783 'face
'org-footnote
))))))
5785 (defun org-activate-bracket-links (limit)
5786 "Run through the buffer and add overlays to bracketed links."
5787 (if (and (re-search-forward org-bracket-link-regexp limit t
)
5788 (not (org-in-src-block-p)))
5789 (let* ((hl (org-match-string-no-properties 1))
5790 (help (concat "LINK: " hl
))
5791 ;; FIXME: Above we should remove the escapes. But that
5792 ;; requires another match, protecting match data, a lot
5793 ;; of overhead for font-lock.
5794 (ip (org-maybe-intangible
5795 (list 'invisible
'org-link
5796 'keymap org-mouse-map
'mouse-face
'highlight
5797 'font-lock-multiline t
'help-echo help
5798 'htmlize-link
`(:uri
,hl
))))
5799 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5800 'font-lock-multiline t
'help-echo help
5801 'htmlize-link
`(:uri
,hl
))))
5802 ;; We need to remove the invisible property here. Table narrowing
5803 ;; may have made some of this invisible.
5804 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5805 (remove-text-properties (match-beginning 0) (match-end 0)
5809 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5810 (org-rear-nonsticky-at (match-beginning 3))
5811 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5812 (org-rear-nonsticky-at (match-end 3))
5813 (add-text-properties (match-end 3) (match-end 0) ip
)
5814 (org-rear-nonsticky-at (match-end 0)))
5815 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5816 (org-rear-nonsticky-at (match-beginning 1))
5817 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5818 (org-rear-nonsticky-at (match-end 1))
5819 (add-text-properties (match-end 1) (match-end 0) ip
)
5820 (org-rear-nonsticky-at (match-end 0)))
5823 (defun org-activate-dates (limit)
5824 "Run through the buffer and add overlays to dates."
5825 (if (re-search-forward org-tsr-regexp-both limit t
)
5827 (org-remove-flyspell-overlays-in (1+ (match-beginning 0)) (match-end 0))
5828 (add-text-properties (1+ (match-beginning 0)) (match-end 0)
5829 (list 'mouse-face
'highlight
5830 'keymap org-mouse-map
))
5831 (org-rear-nonsticky-at (match-end 0))
5832 (when org-display-custom-times
5834 (org-display-custom-time (match-beginning 3) (match-end 3)))
5835 (org-display-custom-time (match-beginning 1) (match-end 1)))
5838 (defvar org-target-link-regexp nil
5839 "Regular expression matching radio targets in plain text.")
5840 (make-variable-buffer-local 'org-target-link-regexp
)
5841 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5842 "Regular expression matching a link target.")
5843 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5844 "Regular expression matching a radio target.")
5845 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5846 "Regular expression matching any target.")
5848 (defun org-activate-target-links (limit)
5849 "Run through the buffer and add overlays to target matches."
5850 (when org-target-link-regexp
5851 (let ((case-fold-search t
))
5852 (if (re-search-forward org-target-link-regexp limit t
)
5854 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5855 (add-text-properties (match-beginning 0) (match-end 0)
5856 (list 'mouse-face
'highlight
5857 'keymap org-mouse-map
5858 'help-echo
"Radio target link"
5859 'org-linked-text t
))
5860 (org-rear-nonsticky-at (match-end 0))
5863 (defun org-update-radio-target-regexp ()
5864 "Find all radio targets in this file and update the regular expression."
5866 (when (memq 'radio org-activate-links
)
5867 (setq org-target-link-regexp
5868 (org-make-target-link-regexp (org-all-targets 'radio
)))
5869 (org-restart-font-lock)))
5871 (defun org-hide-wide-columns (limit)
5873 (setq s
(text-property-any (point) (or limit
(point-max))
5876 (setq e
(next-single-property-change s
'org-cwidth
))
5877 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5881 (defvar org-latex-and-related-regexp nil
5882 "Regular expression for highlighting LaTeX, entities and sub/superscript.")
5883 (defvar org-match-substring-regexp
)
5884 (defvar org-match-substring-with-braces-regexp
)
5886 (defun org-compute-latex-and-related-regexp ()
5887 "Compute regular expression for LaTeX, entities and sub/superscript.
5888 Result depends on variable `org-highlight-latex-and-related'."
5890 'org-latex-and-related-regexp
5892 (cond ((not (memq 'script org-highlight-latex-and-related
)) nil
)
5893 ((eq org-use-sub-superscripts
'{})
5894 (list org-match-substring-with-braces-regexp
))
5895 (org-use-sub-superscripts (list org-match-substring-regexp
))))
5897 (when (memq 'latex org-highlight-latex-and-related
)
5898 (let ((matchers (plist-get org-format-latex-options
:matchers
)))
5901 (and (member (car x
) matchers
) (nth 1 x
)))
5902 org-latex-regexps
)))))
5904 (when (memq 'entities org-highlight-latex-and-related
)
5905 (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
5906 (mapconcat 'identity
(append re-latex re-entities re-sub
) "\\|"))))
5908 (defun org-do-latex-and-related (limit)
5909 "Highlight LaTeX snippets and environments, entities and sub/superscript.
5910 LIMIT bounds the search for syntax to highlight. Stop at first
5911 highlighted object, if any. Return t if some highlighting was
5912 done, nil otherwise."
5913 (when org-highlight-latex-and-related
5915 (while (re-search-forward org-latex-and-related-regexp limit t
)
5916 (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
5918 '(org-code org-verbatim underline
))
5919 (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
5923 (font-lock-prepend-text-property
5924 (+ offset
(match-beginning 0)) (match-end 0)
5925 'face
'org-latex-and-related
)
5926 (add-text-properties (+ offset
(match-beginning 0)) (match-end 0)
5927 '(font-lock-multiline t
)))
5931 (defun org-restart-font-lock ()
5932 "Restart `font-lock-mode', to force refontification."
5933 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5935 (font-lock-mode 1)))
5937 (defun org-all-targets (&optional radio
)
5938 "Return a list of all targets in this file.
5939 When optional argument RADIO is non-nil, only find radio
5941 (let ((re (if radio org-radio-target-regexp org-target-regexp
)) rtn
)
5943 (goto-char (point-min))
5944 (while (re-search-forward re nil t
)
5945 ;; Make sure point is really within the object.
5947 (let ((obj (org-element-context)))
5948 (when (memq (org-element-type obj
) '(radio-target target
))
5949 (add-to-list 'rtn
(downcase (org-element-property :value obj
))))))
5952 (defun org-make-target-link-regexp (targets)
5953 "Make regular expression matching all strings in TARGETS.
5954 The regular expression finds the targets also if there is a line break
5961 (setq x
(regexp-quote x
))
5962 (while (string-match " +" x
)
5963 (setq x
(replace-match "\\s-+" t t x
)))
5969 (defun org-activate-tags (limit)
5970 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5972 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5973 (add-text-properties (match-beginning 1) (match-end 1)
5974 (list 'mouse-face
'highlight
5975 'keymap org-mouse-map
))
5976 (org-rear-nonsticky-at (match-end 1))
5979 (defun org-outline-level ()
5980 "Compute the outline level of the heading at point.
5981 If this is called at a normal headline, the level is the number of stars.
5982 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
5984 (if (not (condition-case nil
5985 (org-back-to-heading t
)
5988 (looking-at org-outline-regexp
)
5989 (1- (- (match-end 0) (match-beginning 0))))))
5991 (defvar org-font-lock-keywords nil
)
5993 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5994 "Regular expression matching a property line.")
5996 (defvar org-font-lock-hook nil
5997 "Functions to be called for special font lock stuff.")
5999 (defvar org-font-lock-set-keywords-hook nil
6000 "Functions that can manipulate `org-font-lock-extra-keywords'.
6001 This is called after `org-font-lock-extra-keywords' is defined, but before
6002 it is installed to be used by font lock. This can be useful if something
6003 needs to be inserted at a specific position in the font-lock sequence.")
6005 (defun org-font-lock-hook (limit)
6006 "Run `org-font-lock-hook' within LIMIT."
6007 (run-hook-with-args 'org-font-lock-hook limit
))
6009 (defun org-set-font-lock-defaults ()
6010 "Set font lock defaults for the current buffer."
6011 (let* ((em org-fontify-emphasized-text
)
6012 (lk org-activate-links
)
6013 (org-font-lock-extra-keywords
6016 '(org-font-lock-hook)
6018 `(,(if org-fontify-whole-heading-line
6019 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
6020 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
6021 (1 (org-get-level-face 1))
6022 (2 (org-get-level-face 2))
6023 (3 (org-get-level-face 3)))
6025 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
6028 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
6029 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
6030 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
6031 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
6033 (list org-drawer-regexp
'(0 'org-special-keyword t
))
6034 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
6036 (list org-property-re
6037 '(1 'org-special-keyword t
)
6038 '(3 'org-property-value t
))
6040 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
6041 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
6042 (if (memq 'plain lk
) '(org-activate-plain-links))
6043 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
6044 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
6045 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
6046 (if (memq 'footnote lk
) '(org-activate-footnote-links))
6047 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
6048 '(org-hide-wide-columns (0 nil append
))
6050 (list (format org-heading-keyword-regexp-format
6052 '(2 (org-get-todo-face 2) t
))
6054 (if org-fontify-done-headline
6055 (list (format org-heading-keyword-regexp-format
6058 (mapconcat 'regexp-quote org-done-keywords
"\\|")
6060 '(2 'org-headline-done t
))
6063 '(org-font-lock-add-priority-faces)
6065 '(org-font-lock-add-tag-faces)
6067 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
6068 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
6069 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
6070 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
6073 (if (featurep 'xemacs
)
6074 '(org-do-emphasis-faces (0 nil append
))
6075 '(org-do-emphasis-faces)))
6077 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
6078 1 'org-checkbox prepend
)
6079 (if (cdr (assq 'checkbox org-list-automatic-rules
))
6080 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
6081 (0 (org-get-checkbox-statistics-face) t
)))
6082 ;; Description list items
6083 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
6084 1 'org-list-dt prepend
)
6085 ;; ARCHIVEd headings
6087 org-outline-regexp-bol
6088 "\\(.*:" org-archive-tag
":.*\\)")
6089 '(1 'org-archived prepend
))
6091 '(org-do-latex-and-related)
6092 '(org-fontify-entities)
6093 '(org-raise-scripts)
6095 '(org-activate-code (1 'org-code t
))
6097 (list (format org-heading-keyword-regexp-format
6099 org-comment-string
"\\|" org-quote-string
6101 '(2 'org-special-keyword t
))
6102 ;; Blocks and meta lines
6103 '(org-fontify-meta-lines-and-blocks)
6105 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
6106 (run-hooks 'org-font-lock-set-keywords-hook
)
6107 ;; Now set the full font-lock-keywords
6108 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
6109 (org-set-local 'font-lock-defaults
6110 '(org-font-lock-keywords t nil nil backward-paragraph
))
6111 (kill-local-variable 'font-lock-keywords
) nil
))
6113 (defun org-toggle-pretty-entities ()
6114 "Toggle the composition display of entities as UTF8 characters."
6116 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
6117 (org-restart-font-lock)
6118 (if org-pretty-entities
6119 (message "Entities are displayed as UTF8 characters")
6122 (org-decompose-region (point-min) (point-max))
6123 (message "Entities are displayed plain"))))
6125 (defvar org-custom-properties-overlays nil
6126 "List of overlays used for custom properties.")
6127 (make-variable-buffer-local 'org-custom-properties-overlays
)
6129 (defun org-toggle-custom-properties-visibility ()
6130 "Display or hide properties in `org-custom-properties'."
6132 (if org-custom-properties-overlays
6133 (progn (mapc 'delete-overlay org-custom-properties-overlays
)
6134 (setq org-custom-properties-overlays nil
))
6135 (unless (not org-custom-properties
)
6139 (goto-char (point-min))
6140 (while (re-search-forward org-property-re nil t
)
6142 (when (equal p
(substring (match-string 1) 1 -
1))
6143 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
6144 (overlay-put o
'invisible t
)
6145 (overlay-put o
'org-custom-property t
)
6146 (push o org-custom-properties-overlays
))))
6147 org-custom-properties
)))))))
6149 (defun org-fontify-entities (limit)
6150 "Find an entity to fontify."
6152 (when org-pretty-entities
6154 (while (re-search-forward
6155 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
6157 (if (and (not (org-in-indented-comment-line))
6158 (setq ee
(org-entity-get (match-string 1)))
6159 (= (length (nth 6 ee
)) 1))
6161 ((end (if (equal (match-string 2) "{}")
6164 (add-text-properties
6165 (match-beginning 0) end
6166 (list 'font-lock-fontified t
))
6167 (compose-region (match-beginning 0) end
6173 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
6174 "Fontify string S like in Org-mode."
6177 (let ((org-odd-levels-only odd-levels
))
6179 (font-lock-fontify-buffer)
6185 (defun org-get-level-face (n)
6186 "Get the right face for match N in font-lock matching of headlines."
6187 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
6188 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
6189 (if org-cycle-level-faces
6190 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
6191 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
6193 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
6195 (t (if org-level-color-stars-only nil org-f
))))
6198 (defun org-get-todo-face (kwd)
6199 "Get the right face for a TODO keyword KWD.
6200 If KWD is a number, get the corresponding match group."
6201 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6202 (or (org-face-from-face-or-color
6203 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
6204 (and (member kwd org-done-keywords
) 'org-done
)
6207 (defun org-face-from-face-or-color (context inherit face-or-color
)
6208 "Create a face list that inherits INHERIT, but sets the foreground color.
6209 When FACE-OR-COLOR is not a string, just return it."
6210 (if (stringp face-or-color
)
6211 (list :inherit inherit
6212 (cdr (assoc context org-faces-easy-properties
))
6216 (defun org-font-lock-add-tag-faces (limit)
6217 "Add the special tag faces."
6218 (when (and org-tag-faces org-tags-special-faces-re
)
6219 (while (re-search-forward org-tags-special-faces-re limit t
)
6220 (add-text-properties (match-beginning 1) (match-end 1)
6221 (list 'face
(org-get-tag-face 1)
6222 'font-lock-fontified t
))
6223 (backward-char 1))))
6225 (defun org-font-lock-add-priority-faces (limit)
6226 "Add the special priority faces."
6227 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
6228 (when (save-match-data (org-at-heading-p))
6229 (add-text-properties
6230 (match-beginning 0) (match-end 0)
6231 (list 'face
(or (org-face-from-face-or-color
6232 'priority
'org-priority
6233 (cdr (assoc (char-after (match-beginning 1))
6234 org-priority-faces
)))
6236 'font-lock-fontified t
)))))
6238 (defun org-get-tag-face (kwd)
6239 "Get the right face for a TODO keyword KWD.
6240 If KWD is a number, get the corresponding match group."
6241 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6242 (or (org-face-from-face-or-color
6243 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
6246 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
6247 "Remove fontification and activation overlays from links."
6248 (font-lock-default-unfontify-region beg end
)
6249 (let* ((buffer-undo-list t
)
6250 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
6251 (inhibit-modification-hooks t
)
6252 deactivate-mark buffer-file-name buffer-file-truename
)
6253 (org-decompose-region beg end
)
6254 (remove-text-properties beg end
6255 '(mouse-face t keymap t org-linked-text t
6256 invisible t intangible t
6257 org-no-flyspell t org-emphasis t
))
6258 (org-remove-font-lock-display-properties beg end
)))
6260 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
6261 ((raise 0.3) (height 0.7))
6264 "Display properties for showing superscripts and subscripts.")
6266 (defun org-remove-font-lock-display-properties (beg end
)
6267 "Remove specific display properties that have been added by font lock.
6268 The will remove the raise properties that are used to show superscripts
6272 (setq next
(next-single-property-change beg
'display nil end
)
6273 prop
(get-text-property beg
'display
))
6274 (if (member prop org-script-display
)
6275 (put-text-property beg next
'display nil
))
6278 (defun org-raise-scripts (limit)
6279 "Add raise properties to sub/superscripts."
6280 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
6281 (if (re-search-forward
6282 (if (eq org-use-sub-superscripts t
)
6283 org-match-substring-regexp
6284 org-match-substring-with-braces-regexp
)
6286 (let* ((pos (point)) table-p comment-p
6287 (mpos (match-beginning 3))
6288 (emph-p (get-text-property mpos
'org-emphasis
))
6289 (link-p (get-text-property mpos
'mouse-face
))
6290 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6291 (goto-char (point-at-bol))
6292 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
6293 comment-p
(org-looking-at-p "[ \t]*#"))
6295 ;; FIXME: Should we go back one character here, for a_b^c
6296 ;; (goto-char (1- pos)) ;????????????????????
6297 (if (or comment-p emph-p link-p keyw-p
)
6299 (put-text-property (match-beginning 3) (match-end 0)
6301 (if (equal (char-after (match-beginning 2)) ?^
)
6302 (nth (if table-p
3 1) org-script-display
)
6303 (nth (if table-p
2 0) org-script-display
)))
6304 (add-text-properties (match-beginning 2) (match-end 2)
6306 'org-dwidth t
'org-dwidth-n
1))
6307 (if (and (eq (char-after (match-beginning 3)) ?
{)
6308 (eq (char-before (match-end 3)) ?
}))
6310 (add-text-properties
6311 (match-beginning 3) (1+ (match-beginning 3))
6312 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6313 (add-text-properties
6314 (1- (match-end 3)) (match-end 3)
6315 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6318 ;;;; Visibility cycling, including org-goto and indirect buffer
6322 (defvar org-cycle-global-status nil
)
6323 (make-variable-buffer-local 'org-cycle-global-status
)
6324 (put 'org-cycle-global-status
'org-state t
)
6325 (defvar org-cycle-subtree-status nil
)
6326 (make-variable-buffer-local 'org-cycle-subtree-status
)
6327 (put 'org-cycle-subtree-status
'org-state t
)
6329 (defvar org-inlinetask-min-level
)
6332 (defun org-cycle (&optional arg
)
6333 "TAB-action and visibility cycling for Org-mode.
6335 This is the command invoked in Org-mode by the TAB key. Its main purpose
6336 is outline visibility cycling, but it also invokes other actions
6337 in special contexts.
6339 - When this function is called with a prefix argument, rotate the entire
6340 buffer through 3 states (global cycling)
6341 1. OVERVIEW: Show only top-level headlines.
6342 2. CONTENTS: Show all headlines of all levels, but no body text.
6343 3. SHOW ALL: Show everything.
6344 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6345 determined by the variable `org-startup-folded', and by any VISIBILITY
6346 properties in the buffer.
6347 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6348 including any drawers.
6350 - When inside a table, re-align the table and move to the next field.
6352 - When point is at the beginning of a headline, rotate the subtree started
6353 by this line through 3 different states (local cycling)
6354 1. FOLDED: Only the main headline is shown.
6355 2. CHILDREN: The main headline and the direct children are shown.
6356 From this state, you can move to one of the children
6357 and zoom in further.
6358 3. SUBTREE: Show the entire subtree, including body text.
6359 If there is no subtree, switch directly from CHILDREN to FOLDED.
6361 - When point is at the beginning of an empty headline and the variable
6362 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6363 of the headline by demoting and promoting it to likely levels. This
6364 speeds up creation document structure by pressing TAB once or several
6365 times right after creating a new headline.
6367 - When there is a numeric prefix, go up to a heading with level ARG, do
6368 a `show-subtree' and return to the previous cursor position. If ARG
6369 is negative, go up that many levels.
6371 - When point is not at the beginning of a headline, execute the global
6372 binding for TAB, which is re-indenting the line. See the option
6373 `org-cycle-emulate-tab' for details.
6375 - Special case: if point is at the beginning of the buffer and there is
6376 no headline in line 1, this function will act as if called with prefix arg
6377 (C-u TAB, same as S-TAB) also when called without prefix arg.
6378 But only if also the variable `org-cycle-global-at-bob' is t."
6380 (org-load-modules-maybe)
6381 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6382 (and org-cycle-level-after-item
/entry-creation
6383 (or (org-cycle-level)
6384 (org-cycle-item-indentation))))
6385 (let* (message-log-max ; Don't populate the *Messages* buffer
6387 (or org-cycle-max-level
6388 (and (boundp 'org-inlinetask-min-level
)
6389 org-inlinetask-min-level
6390 (1- org-inlinetask-min-level
))))
6391 (nstars (and limit-level
6392 (if org-odd-levels-only
6393 (and limit-level
(1- (* limit-level
2)))
6396 (if (not (derived-mode-p 'org-mode
))
6398 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6399 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6400 (not (looking-at org-outline-regexp
))))
6403 (delq 'org-optimize-window-after-visibility-change
6404 (copy-sequence org-cycle-hook
))
6408 (if (or bob-special
(equal arg
'(4)))
6409 ;; special case: use global cycling
6415 (setq last-command
'dummy
)
6416 (org-set-startup-visibility)
6417 (message "Startup visibility, plus VISIBILITY properties"))
6421 (message "Entire buffer visible, including drawers"))
6423 ;; Table: enter it or move to the next field.
6424 ((org-at-table-p 'any
)
6425 (if (org-at-table.el-p
)
6426 (message "Use C-c ' to edit table.el tables")
6427 (if arg
(org-table-edit-field t
)
6428 (org-table-justify-field-maybe)
6429 (call-interactively 'org-table-next-field
))))
6431 ((run-hook-with-args-until-success
6432 'org-tab-after-check-for-table-hook
))
6434 ;; Global cycling: delegate to `org-cycle-internal-global'.
6435 ((eq arg t
) (org-cycle-internal-global))
6437 ;; Drawers: delegate to `org-flag-drawer'.
6438 ((and org-drawers org-drawer-regexp
6440 (beginning-of-line 1)
6441 (looking-at org-drawer-regexp
)))
6442 (org-flag-drawer ; toggle block visibility
6443 (not (get-char-property (match-end 0) 'invisible
))))
6445 ;; Show-subtree, ARG levels up from here.
6448 (org-back-to-heading)
6449 (outline-up-heading (if (< arg
0) (- arg
)
6450 (- (funcall outline-level
) arg
)))
6451 (org-show-subtree)))
6453 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6454 ((and (featurep 'org-inlinetask
)
6455 (org-inlinetask-at-task-p)
6456 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6457 (org-inlinetask-toggle-visibility))
6459 ((org-try-cdlatex-tab))
6461 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6462 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6463 (save-excursion (beginning-of-line 1)
6464 (looking-at org-outline-regexp
)))
6465 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6466 (org-cycle-internal-local))
6468 ;; From there: TAB emulation and template completion.
6469 (buffer-read-only (org-back-to-heading))
6471 ((run-hook-with-args-until-success
6472 'org-tab-after-check-for-cycling-hook
))
6474 ((org-try-structure-completion))
6476 ((run-hook-with-args-until-success
6477 'org-tab-before-tab-emulation-hook
))
6479 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6481 (not (looking-at org-outline-regexp
))))
6482 (call-interactively (global-key-binding "\t")))
6484 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6485 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6486 (or (and (eq org-cycle-emulate-tab
'white
)
6487 (= (match-end 0) (point-at-eol)))
6488 (and (eq org-cycle-emulate-tab
'whitestart
)
6489 (>= (match-end 0) pos
))))
6491 (eq org-cycle-emulate-tab t
))
6492 (call-interactively (global-key-binding "\t")))
6495 (org-back-to-heading)
6498 (defun org-cycle-internal-global ()
6499 "Do the global cycling action."
6500 ;; Hack to avoid display of messages for .org attachments in Gnus
6501 (let (message-log-max ; Don't populate the *Messages* buffer
6502 (ga (string-match "\\*fontification" (buffer-name))))
6504 ((and (eq last-command this-command
)
6505 (eq org-cycle-global-status
'overview
))
6506 ;; We just created the overview - now do table of contents
6507 ;; This can be slow in very large buffers, so indicate action
6508 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6509 (unless ga
(message "CONTENTS..."))
6511 (unless ga
(message "CONTENTS...done"))
6512 (setq org-cycle-global-status
'contents
)
6513 (run-hook-with-args 'org-cycle-hook
'contents
))
6515 ((and (eq last-command this-command
)
6516 (eq org-cycle-global-status
'contents
))
6517 ;; We just showed the table of contents - now show everything
6518 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6520 (unless ga
(message "SHOW ALL"))
6521 (setq org-cycle-global-status
'all
)
6522 (run-hook-with-args 'org-cycle-hook
'all
))
6525 ;; Default action: go to overview
6526 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6528 (unless ga
(message "OVERVIEW"))
6529 (setq org-cycle-global-status
'overview
)
6530 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6532 (defun org-cycle-internal-local ()
6533 "Do the local cycling action."
6534 (let (message-log-max ; Don't populate the *Messages* buffer
6535 (goal-column 0) eoh eol eos has-children children-skipped struct
)
6536 ;; First, determine end of headline (EOH), end of subtree or item
6537 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6542 (setq struct
(org-list-struct))
6543 (setq eoh
(point-at-eol))
6544 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6545 (setq has-children
(org-list-has-child-p (point) struct
)))
6546 (org-back-to-heading)
6547 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6548 (setq eos
(save-excursion (1- (org-end-of-subtree t t
))))
6551 (let ((level (funcall outline-level
)))
6552 (outline-next-heading)
6553 (and (org-at-heading-p t
)
6554 (> (funcall outline-level
) level
))))
6556 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6557 ;; Determine end invisible part of buffer (EOL)
6558 (beginning-of-line 2)
6559 ;; XEmacs doesn't have `next-single-char-property-change'
6560 (if (featurep 'xemacs
)
6561 (while (and (not (eobp)) ;; this is like `next-line'
6562 (get-char-property (1- (point)) 'invisible
))
6563 (beginning-of-line 2))
6564 (while (and (not (eobp)) ;; this is like `next-line'
6565 (get-char-property (1- (point)) 'invisible
))
6566 (goto-char (next-single-char-property-change (point) 'invisible
))
6567 (and (eolp) (beginning-of-line 2))))
6569 ;; Find out what to do next and set `this-command'
6572 ;; Nothing is hidden behind this heading
6573 (unless (org-before-first-heading-p)
6574 (run-hook-with-args 'org-pre-cycle-hook
'empty
))
6575 (message "EMPTY ENTRY")
6576 (setq org-cycle-subtree-status nil
)
6579 (outline-next-heading)
6580 (if (outline-invisible-p) (org-flag-heading nil
))))
6581 ((and (or (>= eol eos
)
6582 (not (string-match "\\S-" (buffer-substring eol eos
))))
6584 (not (setq children-skipped
6585 org-cycle-skip-children-state-if-no-children
))))
6586 ;; Entire subtree is hidden in one line: children view
6587 (unless (org-before-first-heading-p)
6588 (run-hook-with-args 'org-pre-cycle-hook
'children
))
6590 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6592 (org-with-limited-levels (show-children))
6593 ;; FIXME: This slows down the func way too much.
6594 ;; How keep drawers hidden in subtree anyway?
6595 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6596 ;; (org-cycle-hide-drawers 'subtree))
6598 ;; Fold every list in subtree to top-level items.
6599 (when (eq org-cycle-include-plain-lists
'integrate
)
6601 (org-back-to-heading)
6602 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6603 (beginning-of-line 1)
6604 (let* ((struct (org-list-struct))
6605 (prevs (org-list-prevs-alist struct
))
6606 (end (org-list-get-bottom-point struct
)))
6607 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6608 (org-list-get-all-items (point) struct prevs
))
6609 (goto-char end
))))))
6610 (message "CHILDREN")
6613 (outline-next-heading)
6614 (if (outline-invisible-p) (org-flag-heading nil
)))
6615 (setq org-cycle-subtree-status
'children
)
6616 (unless (org-before-first-heading-p)
6617 (run-hook-with-args 'org-cycle-hook
'children
)))
6618 ((or children-skipped
6619 (and (eq last-command this-command
)
6620 (eq org-cycle-subtree-status
'children
)))
6621 ;; We just showed the children, or no children are there,
6622 ;; now show everything.
6623 (unless (org-before-first-heading-p)
6624 (run-hook-with-args 'org-pre-cycle-hook
'subtree
))
6625 (outline-flag-region eoh eos nil
)
6626 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6627 (setq org-cycle-subtree-status
'subtree
)
6628 (unless (org-before-first-heading-p)
6629 (run-hook-with-args 'org-cycle-hook
'subtree
)))
6631 ;; Default action: hide the subtree.
6632 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6633 (outline-flag-region eoh eos t
)
6635 (setq org-cycle-subtree-status
'folded
)
6636 (unless (org-before-first-heading-p)
6637 (run-hook-with-args 'org-cycle-hook
'folded
))))))
6640 (defun org-global-cycle (&optional arg
)
6641 "Cycle the global visibility. For details see `org-cycle'.
6642 With \\[universal-argument] prefix arg, switch to startup visibility.
6643 With a numeric prefix, show all headlines up to that level."
6645 (let ((org-cycle-include-plain-lists
6646 (if (derived-mode-p 'org-mode
) org-cycle-include-plain-lists nil
)))
6650 (hide-sublevels arg
)
6651 (setq org-cycle-global-status
'contents
))
6653 (org-set-startup-visibility)
6654 (message "Startup visibility, plus VISIBILITY properties."))
6656 (org-cycle '(4))))))
6658 (defun org-set-startup-visibility ()
6659 "Set the visibility required by startup options and properties."
6661 ((eq org-startup-folded t
)
6663 ((eq org-startup-folded
'content
)
6664 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6665 (org-cycle '(4)) (org-cycle '(4)))))
6666 (unless (eq org-startup-folded
'showeverything
)
6667 (if org-hide-block-startup
(org-hide-block-all))
6668 (org-set-visibility-according-to-property 'no-cleanup
)
6669 (org-cycle-hide-archived-subtrees 'all
)
6670 (org-cycle-hide-drawers 'all
)
6671 (org-cycle-show-empty-lines t
)))
6673 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6674 "Switch subtree visibilities according to :VISIBILITY: property."
6676 (let (org-show-entry-below state
)
6678 (goto-char (point-min))
6679 (while (re-search-forward
6680 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6682 (setq state
(match-string 1))
6684 (org-back-to-heading t
)
6688 ((equal state
'("fold" "folded"))
6690 ((equal state
"children")
6691 (org-show-hidden-entry)
6693 ((equal state
"content")
6696 (org-narrow-to-subtree)
6698 ((member state
'("all" "showall"))
6701 (org-cycle-hide-archived-subtrees 'all
)
6702 (org-cycle-hide-drawers 'all
)
6703 (org-cycle-show-empty-lines 'all
)))))
6705 ;; This function uses outline-regexp instead of the more fundamental
6706 ;; org-outline-regexp so that org-cycle-global works outside of Org
6707 ;; buffers, where outline-regexp is needed.
6708 (defun org-overview ()
6709 "Switch to overview mode, showing only top-level headlines.
6710 Really, this shows all headlines with level equal or greater than the level
6711 of the first headline in the buffer. This is important, because if the
6712 first headline is not level one, then (hide-sublevels 1) gives confusing
6715 (let ((level (save-excursion
6716 (goto-char (point-min))
6717 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6719 (goto-char (match-beginning 0))
6720 (funcall outline-level
))))))
6721 (and level
(hide-sublevels level
))))
6723 (defun org-content (&optional arg
)
6724 "Show all headlines in the buffer, like a table of contents.
6725 With numerical argument N, show content up to level N."
6728 ;; Visit all headings and show their offspring
6729 (and (integerp arg
) (org-overview))
6730 (goto-char (point-max))
6732 (while (and (progn (condition-case nil
6733 (outline-previous-visible-heading 1)
6734 (error (goto-char (point-min))))
6736 (looking-at org-outline-regexp
))
6738 (show-children (1- arg
))
6740 (if (bobp) (throw 'exit nil
))))))
6743 (defun org-optimize-window-after-visibility-change (state)
6744 "Adjust the window after a change in outline visibility.
6745 This function is the default value of the hook `org-cycle-hook'."
6746 (when (get-buffer-window (current-buffer))
6748 ((eq state
'content
) nil
)
6749 ((eq state
'all
) nil
)
6750 ((eq state
'folded
) nil
)
6751 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6752 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6754 (defun org-remove-empty-overlays-at (pos)
6755 "Remove outline overlays that do not contain non-white stuff."
6758 (and (eq 'outline
(overlay-get o
'invisible
))
6759 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6761 (delete-overlay o
)))
6764 (defun org-clean-visibility-after-subtree-move ()
6765 "Fix visibility issues after moving a subtree."
6766 ;; First, find a reasonable region to look at:
6767 ;; Start two siblings above, end three below
6768 (let* ((beg (save-excursion
6769 (and (org-get-last-sibling)
6770 (org-get-last-sibling))
6772 (end (save-excursion
6773 (and (org-get-next-sibling)
6774 (org-get-next-sibling)
6775 (org-get-next-sibling))
6776 (if (org-at-heading-p)
6779 (level (looking-at "\\*+"))
6780 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6783 (narrow-to-region beg end
)
6785 ;; Properly fold already folded siblings
6786 (goto-char (point-min))
6787 (while (re-search-forward re nil t
)
6788 (if (and (not (outline-invisible-p))
6790 (goto-char (point-at-eol)) (outline-invisible-p)))
6792 (org-cycle-show-empty-lines 'overview
)
6793 (org-cycle-hide-drawers 'overview
)))))
6795 (defun org-cycle-show-empty-lines (state)
6796 "Show empty lines above all visible headlines.
6797 The region to be covered depends on STATE when called through
6798 `org-cycle-hook'. Lisp program can use t for STATE to get the
6799 entire buffer covered. Note that an empty line is only shown if there
6800 are at least `org-cycle-separator-lines' empty lines before the headline."
6801 (when (not (= org-cycle-separator-lines
0))
6803 (let* ((n (abs org-cycle-separator-lines
))
6805 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6806 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6807 (t (let ((ns (number-to-string (- n
2))))
6808 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6809 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6812 ((memq state
'(overview contents t
))
6813 (setq beg
(point-min) end
(point-max)))
6814 ((memq state
'(children folded
))
6815 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6816 (beginning-of-line 2)
6820 (while (re-search-forward re end t
)
6821 (unless (get-char-property (match-end 1) 'invisible
)
6822 (setq e
(match-end 1))
6823 (if (< org-cycle-separator-lines
0)
6824 (setq b
(save-excursion
6825 (goto-char (match-beginning 0))
6826 (org-back-over-empty-lines)
6828 (goto-char (max (point-min) (1- (point))))
6832 (setq b
(match-beginning 1)))
6833 (outline-flag-region b e nil
)))))))
6834 ;; Never hide empty lines at the end of the file.
6836 (goto-char (point-max))
6837 (outline-previous-heading)
6838 (outline-end-of-heading)
6839 (if (and (looking-at "[ \t\n]+")
6840 (= (match-end 0) (point-max)))
6841 (outline-flag-region (point) (match-end 0) nil
))))
6843 (defun org-show-empty-lines-in-parent ()
6844 "Move to the parent and re-show empty lines before visible headlines."
6846 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6847 (org-cycle-show-empty-lines context
))))
6849 (defun org-files-list ()
6850 "Return `org-agenda-files' list, plus all open org-mode files.
6851 This is useful for operations that need to scan all of a user's
6852 open and agenda-wise Org files."
6853 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6854 (dolist (buf (buffer-list))
6855 (with-current-buffer buf
6856 (if (and (derived-mode-p 'org-mode
) (buffer-file-name))
6857 (let ((file (expand-file-name (buffer-file-name))))
6858 (unless (member file files
)
6859 (push file files
))))))
6862 (defsubst org-entry-beginning-position
()
6863 "Return the beginning position of the current entry."
6864 (save-excursion (outline-back-to-heading t
) (point)))
6866 (defsubst org-entry-end-position
()
6867 "Return the end position of the current entry."
6868 (save-excursion (outline-next-heading) (point)))
6870 (defun org-cycle-hide-drawers (state)
6871 "Re-hide all drawers after a visibility state change."
6872 (when (and (derived-mode-p 'org-mode
)
6873 (not (memq state
'(overview folded contents
))))
6875 (let* ((globalp (memq state
'(contents all
)))
6876 (beg (if globalp
(point-min) (point)))
6877 (end (if globalp
(point-max)
6878 (if (eq state
'children
)
6879 (save-excursion (outline-next-heading) (point))
6880 (org-end-of-subtree t
)))))
6882 (while (re-search-forward org-drawer-regexp end t
)
6883 (org-flag-drawer t
))))))
6885 (defun org-cycle-hide-inline-tasks (state)
6886 "Re-hide inline task when switching to 'contents visibility state."
6887 (when (and (eq state
'contents
)
6888 (boundp 'org-inlinetask-min-level
)
6889 org-inlinetask-min-level
)
6890 (hide-sublevels (1- org-inlinetask-min-level
))))
6892 (defun org-flag-drawer (flag)
6893 "When FLAG is non-nil, hide the drawer we are within.
6894 Otherwise make it visible."
6896 (beginning-of-line 1)
6897 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6898 (let ((b (match-end 0)))
6899 (if (re-search-forward
6901 (save-excursion (outline-next-heading) (point)) t
)
6902 (outline-flag-region b
(point-at-eol) flag
)
6903 (error ":END: line missing at position %s" b
))))))
6905 (defun org-subtree-end-visible-p ()
6906 "Is the end of the current subtree visible?"
6907 (pos-visible-in-window-p
6908 (save-excursion (org-end-of-subtree t
) (point))))
6910 (defun org-first-headline-recenter (&optional N
)
6911 "Move cursor to the first headline and recenter the headline.
6912 Optional argument N means put the headline into the Nth line of the window."
6913 (goto-char (point-min))
6914 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
6916 (recenter (prefix-numeric-value N
))))
6918 ;;; Saving and restoring visibility
6920 (defun org-outline-overlay-data (&optional use-markers
)
6921 "Return a list of the locations of all outline overlays.
6922 These are overlays with the `invisible' property value `outline'.
6923 The return value is a list of cons cells, with start and stop
6924 positions for each overlay.
6925 If USE-MARKERS is set, return the positions as markers."
6932 (when (eq (overlay-get o
'invisible
) 'outline
)
6933 (setq beg
(overlay-start o
)
6934 end
(overlay-end o
))
6935 (and beg end
(> end beg
)
6937 (cons (move-marker (make-marker) beg
)
6938 (move-marker (make-marker) end
))
6940 (overlays-in (point-min) (point-max))))))))
6942 (defun org-set-outline-overlay-data (data)
6943 "Create visibility overlays for all positions in DATA.
6944 DATA should have been made by `org-outline-overlay-data'."
6951 (outline-flag-region (car c
) (cdr c
) t
))
6954 ;;; Folding of blocks
6956 (defvar org-hide-block-overlays nil
6957 "Overlays hiding blocks.")
6958 (make-variable-buffer-local 'org-hide-block-overlays
)
6960 (defun org-block-map (function &optional start end
)
6961 "Call FUNCTION at the head of all source blocks in the current buffer.
6962 Optional arguments START and END can be used to limit the range."
6963 (let ((start (or start
(point-min)))
6964 (end (or end
(point-max))))
6967 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6970 (goto-char (match-beginning 0))
6971 (funcall function
)))))))
6973 (defun org-hide-block-toggle-all ()
6974 "Toggle the visibility of all blocks in the current buffer."
6975 (org-block-map #'org-hide-block-toggle
))
6977 (defun org-hide-block-all ()
6978 "Fold all blocks in the current buffer."
6980 (org-show-block-all)
6981 (org-block-map #'org-hide-block-toggle-maybe
))
6983 (defun org-show-block-all ()
6984 "Unfold all blocks in the current buffer."
6986 (mapc 'delete-overlay org-hide-block-overlays
)
6987 (setq org-hide-block-overlays nil
))
6989 (defun org-hide-block-toggle-maybe ()
6990 "Toggle visibility of block at point."
6992 (let ((case-fold-search t
))
6994 (beginning-of-line 1)
6995 (looking-at org-block-regexp
))
6996 (progn (org-hide-block-toggle)
6997 t
) ;; to signal that we took action
6998 nil
))) ;; to signal that we did not
7000 (defun org-hide-block-toggle (&optional force
)
7001 "Toggle the visibility of the current block."
7005 (if (re-search-forward org-block-regexp nil t
)
7006 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
7007 (end (match-end 0)) ;; end of entire body
7009 (if (memq t
(mapcar (lambda (overlay)
7010 (eq (overlay-get overlay
'invisible
)
7012 (overlays-at start
)))
7013 (if (or (not force
) (eq force
'off
))
7015 (when (member ov org-hide-block-overlays
)
7016 (setq org-hide-block-overlays
7017 (delq ov org-hide-block-overlays
)))
7018 (when (eq (overlay-get ov
'invisible
)
7020 (delete-overlay ov
)))
7021 (overlays-at start
)))
7022 (setq ov
(make-overlay start end
))
7023 (overlay-put ov
'invisible
'org-hide-block
)
7024 ;; make the block accessible to isearch
7026 ov
'isearch-open-invisible
7028 (when (member ov org-hide-block-overlays
)
7029 (setq org-hide-block-overlays
7030 (delq ov org-hide-block-overlays
)))
7031 (when (eq (overlay-get ov
'invisible
)
7033 (delete-overlay ov
))))
7034 (push ov org-hide-block-overlays
)))
7035 (error "Not looking at a source block"))))
7037 ;; org-tab-after-check-for-cycling-hook
7038 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
7039 ;; Remove overlays when changing major mode
7040 (add-hook 'org-mode-hook
7041 (lambda () (org-add-hook 'change-major-mode-hook
7042 'org-show-block-all
'append
'local
)))
7046 (defvar org-goto-window-configuration nil
)
7047 (defvar org-goto-marker nil
)
7048 (defvar org-goto-map
)
7049 (defun org-goto-map ()
7050 "Set the keymap `org-goto'."
7052 (let ((map (make-sparse-keymap)))
7053 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
7054 mouse-drag-region universal-argument org-occur
))
7056 (while (setq cmd
(pop cmds
))
7057 (substitute-key-definition cmd cmd map global-map
)))
7058 (suppress-keymap map
)
7059 (org-defkey map
"\C-m" 'org-goto-ret
)
7060 (org-defkey map
[(return)] 'org-goto-ret
)
7061 (org-defkey map
[(left)] 'org-goto-left
)
7062 (org-defkey map
[(right)] 'org-goto-right
)
7063 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
7064 (org-defkey map
"\C-i" 'org-cycle
)
7065 (org-defkey map
[(tab)] 'org-cycle
)
7066 (org-defkey map
[(down)] 'outline-next-visible-heading
)
7067 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
7068 (if org-goto-auto-isearch
7069 (if (fboundp 'define-key-after
)
7070 (define-key-after map
[t] 'org-goto-local-auto-isearch)
7072 (org-defkey map "q" 'org-goto-quit)
7073 (org-defkey map "n" 'outline-next-visible-heading)
7074 (org-defkey map "p" 'outline-previous-visible-heading)
7075 (org-defkey map "f" 'outline-forward-same-level)
7076 (org-defkey map "b" 'outline-backward-same-level)
7077 (org-defkey map "u" 'outline-up-heading))
7078 (org-defkey map "/" 'org-occur)
7079 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
7080 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
7081 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
7082 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
7083 (org-defkey map "\C-c\C-u" 'outline-up-heading)
7086 (defconst org-goto-help
7087 "Browse buffer copy, to find location or copy text.%s
7088 RET=jump to location C-g=quit and return to previous location
7089 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
7091 (defvar org-goto-start-pos) ; dynamically scoped parameter
7093 ;; FIXME: Docstring does not mention both interfaces
7094 (defun org-goto (&optional alternative-interface)
7095 "Look up a different location in the current file, keeping current visibility.
7097 When you want look-up or go to a different location in a
7098 document, the fastest way is often to fold the entire buffer and
7099 then dive into the tree. This method has the disadvantage, that
7100 the previous location will be folded, which may not be what you
7103 This command works around this by showing a copy of the current
7104 buffer in an indirect buffer, in overview mode. You can dive
7105 into the tree in that copy, use org-occur and incremental search
7106 to find a location. When pressing RET or `Q', the command
7107 returns to the original buffer in which the visibility is still
7108 unchanged. After RET it will also jump to the location selected
7109 in the indirect buffer and expose the headline hierarchy above.
7111 With a prefix argument, use the alternative interface: e.g. if
7112 `org-goto-interface' is 'outline use 'outline-path-completion."
7115 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
7116 (org-refile-use-outline-path t)
7117 (org-refile-target-verify-function nil)
7119 (if (not alternative-interface)
7121 (if (eq org-goto-interface 'outline)
7122 'outline-path-completion
7124 (org-goto-start-pos (point))
7126 (if (eq interface 'outline)
7127 (car (org-get-location (current-buffer) org-goto-help))
7128 (let ((pa (org-refile-get-location "Goto" nil nil t)))
7129 (org-refile-check-position pa)
7133 (org-mark-ring-push org-goto-start-pos)
7134 (goto-char selected-point)
7135 (if (or (outline-invisible-p) (org-invisible-p2))
7136 (org-show-context 'org-goto)))
7139 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
7140 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
7141 (defvar org-goto-local-auto-isearch-map) ; defined below
7143 (defun org-get-location (buf help)
7144 "Let the user select a location in the Org-mode buffer BUF.
7145 This function uses a recursive edit. It returns the selected position
7148 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
7149 (isearch-hide-immediately nil)
7150 (isearch-search-fun-function
7151 (lambda () 'org-goto-local-search-headings))
7152 (org-goto-selected-point org-goto-exit-command))
7154 (save-window-excursion
7155 (delete-other-windows)
7156 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
7157 (org-pop-to-buffer-same-window
7159 (make-indirect-buffer (current-buffer) "*org-goto*")
7160 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
7161 (with-output-to-temp-buffer "*Org Help*"
7162 (princ (format help (if org-goto-auto-isearch
7163 " Just type for auto-isearch."
7164 " n/p/f/b/u to navigate, q to quit."))))
7165 (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
7166 (setq buffer-read-only nil)
7167 (let ((org-startup-truncated t)
7168 (org-startup-folded nil)
7169 (org-startup-align-all-tables nil))
7172 (setq buffer-read-only t)
7173 (if (and (boundp 'org-goto-start-pos)
7174 (integer-or-marker-p org-goto-start-pos))
7175 (let ((org-show-hierarchy-above t)
7176 (org-show-siblings t)
7177 (org-show-following-heading t))
7178 (goto-char org-goto-start-pos)
7179 (and (outline-invisible-p) (org-show-context)))
7180 (goto-char (point-min)))
7181 (let (org-special-ctrl-a/e) (org-beginning-of-line))
7182 (message "Select location and press RET")
7183 (use-local-map org-goto-map)
7185 (kill-buffer "*org-goto*")
7186 (cons org-goto-selected-point org-goto-exit-command))))
7188 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
7189 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
7190 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
7191 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
7193 (defun org-goto-local-search-headings (string bound noerror)
7194 "Search and make sure that any matches are in headlines."
7196 (while (if isearch-forward
7197 (search-forward string bound noerror)
7198 (search-backward string bound noerror))
7199 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
7200 (and (member :headline context)
7201 (not (member :tags context))))
7202 (throw 'return (point))))))
7204 (defun org-goto-local-auto-isearch ()
7207 (goto-char (point-min))
7208 (let ((keys (this-command-keys)))
7209 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
7211 (isearch-process-search-char (string-to-char keys)))))
7213 (defun org-goto-ret (&optional arg)
7214 "Finish `org-goto' by going to the new location."
7216 (setq org-goto-selected-point (point)
7217 org-goto-exit-command 'return)
7220 (defun org-goto-left ()
7221 "Finish `org-goto' by going to the new location."
7223 (if (org-at-heading-p)
7225 (beginning-of-line 1)
7226 (setq org-goto-selected-point (point)
7227 org-goto-exit-command 'left)
7229 (error "Not on a heading")))
7231 (defun org-goto-right ()
7232 "Finish `org-goto' by going to the new location."
7234 (if (org-at-heading-p)
7236 (setq org-goto-selected-point (point)
7237 org-goto-exit-command 'right)
7239 (error "Not on a heading")))
7241 (defun org-goto-quit ()
7242 "Finish `org-goto' without cursor motion."
7244 (setq org-goto-selected-point nil)
7245 (setq org-goto-exit-command 'quit)
7248 ;;; Indirect buffer display of subtrees
7250 (defvar org-indirect-dedicated-frame nil
7251 "This is the frame being used for indirect tree display.")
7252 (defvar org-last-indirect-buffer nil)
7254 (defun org-tree-to-indirect-buffer (&optional arg)
7255 "Create indirect buffer and narrow it to current subtree.
7256 With a numerical prefix ARG, go up to this level and then take that tree.
7257 If ARG is negative, go up that many levels.
7259 If `org-indirect-buffer-display' is not `new-frame', the command removes the
7260 indirect buffer previously made with this command, to avoid proliferation of
7261 indirect buffers. However, when you call the command with a \
7262 \\[universal-argument] prefix, or
7263 when `org-indirect-buffer-display' is `new-frame', the last buffer
7264 is kept so that you can work with several indirect buffers at the same time.
7265 If `org-indirect-buffer-display' is `dedicated-frame', the \
7266 \\[universal-argument] prefix also
7267 requests that a new frame be made for the new buffer, so that the dedicated
7268 frame is not changed."
7270 (let ((cbuf (current-buffer))
7271 (cwin (selected-window))
7273 beg end level heading ibuf)
7275 (org-back-to-heading t)
7277 (setq level (org-outline-level))
7278 (if (< arg 0) (setq arg (+ level arg)))
7279 (while (> (setq level (org-outline-level)) arg)
7280 (org-up-heading-safe)))
7282 heading (org-get-heading))
7283 (org-end-of-subtree t t)
7284 (if (org-at-heading-p) (backward-char 1))
7286 (if (and (buffer-live-p org-last-indirect-buffer)
7287 (not (eq org-indirect-buffer-display 'new-frame))
7289 (kill-buffer org-last-indirect-buffer))
7290 (setq ibuf (org-get-indirect-buffer cbuf)
7291 org-last-indirect-buffer ibuf)
7293 ((or (eq org-indirect-buffer-display 'new-frame)
7294 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7295 (select-frame (make-frame))
7296 (delete-other-windows)
7297 (org-pop-to-buffer-same-window ibuf)
7298 (org-set-frame-title heading))
7299 ((eq org-indirect-buffer-display 'dedicated-frame)
7301 (select-frame (or (and org-indirect-dedicated-frame
7302 (frame-live-p org-indirect-dedicated-frame)
7303 org-indirect-dedicated-frame)
7304 (setq org-indirect-dedicated-frame (make-frame)))))
7305 (delete-other-windows)
7306 (org-pop-to-buffer-same-window ibuf)
7307 (org-set-frame-title (concat "Indirect: " heading)))
7308 ((eq org-indirect-buffer-display 'current-window)
7309 (org-pop-to-buffer-same-window ibuf))
7310 ((eq org-indirect-buffer-display 'other-window)
7311 (pop-to-buffer ibuf))
7312 (t (error "Invalid value")))
7313 (if (featurep 'xemacs)
7314 (save-excursion (org-mode) (turn-on-font-lock)))
7315 (narrow-to-region beg end)
7318 (run-hook-with-args 'org-cycle-hook 'all)
7319 (and (window-live-p cwin) (select-window cwin))))
7321 (defun org-get-indirect-buffer (&optional buffer)
7322 (setq buffer (or buffer (current-buffer)))
7323 (let ((n 1) (base (buffer-name buffer)) bname)
7324 (while (buffer-live-p
7325 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7328 (make-indirect-buffer buffer bname 'clone)
7329 (error (make-indirect-buffer buffer bname)))))
7331 (defun org-set-frame-title (title)
7332 "Set the title of the current frame to the string TITLE."
7333 ;; FIXME: how to name a single frame in XEmacs???
7334 (unless (featurep 'xemacs)
7335 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7337 ;;;; Structure editing
7339 ;;; Inserting headlines
7341 (defun org-previous-line-empty-p ()
7344 (or (beginning-of-line 0) t)
7346 (looking-at "[ \t]*$")))))
7348 (defun org-insert-heading (&optional force-heading invisible-ok)
7349 "Insert a new heading or item with same depth at point.
7350 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
7351 If point is at the beginning of a headline, insert a sibling before the
7352 current headline. If point is not at the beginning, split the line,
7353 create the new headline with the text in the current line after point
7354 \(but see also the variable `org-M-RET-may-split-line').
7356 With a double prefix arg, force the heading to be inserted at the
7357 end of the parent subtree.
7359 When INVISIBLE-OK is set, stop at invisible headlines when going back.
7360 This is important for non-interactive uses of the command."
7362 (if (or (= (buffer-size) 0)
7363 (and (not (save-excursion
7364 (and (ignore-errors (org-back-to-heading invisible-ok))
7365 (org-at-heading-p))))
7366 (or force-heading (not (org-in-item-p)))))
7369 (run-hooks 'org-insert-heading-hook))
7370 (when (or force-heading (not (org-insert-item)))
7371 (let* ((empty-line-p nil)
7373 (on-heading (org-at-heading-p))
7374 (head (save-excursion
7377 (org-back-to-heading invisible-ok)
7378 (when (and (not on-heading)
7379 (featurep 'org-inlinetask)
7380 (integerp org-inlinetask-min-level)
7381 (>= (length (match-string 0))
7382 org-inlinetask-min-level))
7383 ;; Find a heading level before the inline task
7384 (while (and (setq level (org-up-heading-safe))
7385 (>= level org-inlinetask-min-level)))
7386 (if (org-at-heading-p)
7387 (org-back-to-heading invisible-ok)
7388 (error "This should not happen")))
7389 (setq empty-line-p (org-previous-line-empty-p))
7392 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7393 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7394 pos hide-previous previous-pos)
7396 ((and (org-at-heading-p) (bolp)
7398 (save-excursion (backward-char 1) (not (outline-invisible-p)))))
7399 ;; insert before the current line
7400 (open-line (if blank 2 1)))
7402 (not org-insert-heading-respect-content)
7405 (backward-char 1) (not (outline-invisible-p)))))
7406 ;; insert right here
7409 ;; somewhere in the line
7411 (setq previous-pos (point-at-bol))
7413 (setq hide-previous (outline-invisible-p)))
7414 (and org-insert-heading-respect-content (org-show-subtree))
7416 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
7419 (goto-char (point-at-bol))
7420 (and (looking-at org-complex-heading-regexp)
7422 (> p (match-beginning 4)))))))
7425 (org-insert-heading-respect-content
7426 (if (not (equal force-heading '(16)))
7428 (org-end-of-subtree nil t)
7429 (and (looking-at "^\\*") (backward-char 1))
7430 (while (and (not (bobp))
7431 (member (char-before) '(?\ ?\t ?\n)))
7432 (backward-delete-char 1)))
7433 (org-up-heading-safe)
7434 (org-end-of-subtree nil t))
7435 (when (featurep 'org-inlinetask)
7436 (while (and (not (eobp))
7437 (looking-at "\\(\\*+\\)[ \t]+")
7438 (>= (length (match-string 1))
7439 org-inlinetask-min-level))
7440 (org-end-of-subtree nil t)))
7441 (or (bolp) (newline))
7442 (or (org-previous-line-empty-p)
7443 (and blank (newline)))
7449 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
7450 (setq tags (and (match-end 2) (match-string 2)))
7452 (delete-region (match-beginning 1) (match-end 1)))
7453 (setq pos (point-at-bol))
7454 (or split (end-of-line 1))
7455 (delete-horizontal-space)
7456 (if (string-match "\\`\\*+\\'"
7457 (buffer-substring (point-at-bol) (point)))
7459 (newline (if blank 2 1))
7465 (org-set-tags nil 'align))))
7467 (or split (end-of-line 1))
7468 (newline (if blank 2 1)))))))
7469 (insert head) (just-one-space)
7472 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
7473 (when (and org-insert-heading-respect-content hide-previous)
7475 (goto-char previous-pos)
7477 (run-hooks 'org-insert-heading-hook)))))
7479 (defun org-get-heading (&optional no-tags no-todo)
7480 "Return the heading of the current entry, without the stars.
7481 When NO-TAGS is non-nil, don't include tags.
7482 When NO-TODO is non-nil, don't include TODO keywords."
7484 (org-back-to-heading t)
7486 ((and no-tags no-todo)
7487 (looking-at org-complex-heading-regexp)
7490 (looking-at (concat org-outline-regexp
7492 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7495 (looking-at org-todo-line-regexp)
7497 (t (looking-at org-heading-regexp)
7498 (match-string 2)))))
7500 (defvar orgstruct-mode) ; defined below
7502 (defun org-heading-components ()
7503 "Return the components of the current heading.
7504 This is a list with the following elements:
7505 - the level as an integer
7506 - the reduced level, different if `org-odd-levels-only' is set.
7507 - the TODO keyword, or nil
7508 - the priority character, like ?A, or nil if no priority is given
7509 - the headline text itself, or the tags string if no headline text
7510 - the tags string, or nil."
7512 (org-back-to-heading t)
7513 (if (let (case-fold-search)
7517 org-complex-heading-regexp)))
7519 (list (length (match-string 1))
7520 (org-reduced-level (length (match-string 1)))
7525 (list (length (match-string 1))
7526 (org-reduced-level (length (match-string 1)))
7527 (org-match-string-no-properties 2)
7528 (and (match-end 3) (aref (match-string 3) 2))
7529 (org-match-string-no-properties 4)
7530 (org-match-string-no-properties 5))))))
7532 (defun org-get-entry ()
7533 "Get the entry text, after heading, entire subtree."
7535 (org-back-to-heading t)
7536 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7538 (defun org-insert-heading-after-current ()
7539 "Insert a new heading with same level as current, after current subtree."
7541 (org-back-to-heading)
7542 (org-insert-heading)
7543 (org-move-subtree-down)
7546 (defun org-insert-heading-respect-content (invisible-ok)
7547 "Insert heading with `org-insert-heading-respect-content' set to t."
7549 (let ((org-insert-heading-respect-content t))
7550 (org-insert-heading t invisible-ok)))
7552 (defun org-insert-todo-heading-respect-content (&optional force-state)
7553 "Insert TODO heading with `org-insert-heading-respect-content' set to t."
7555 (let ((org-insert-heading-respect-content t))
7556 (org-insert-todo-heading force-state t)))
7558 (defun org-insert-todo-heading (arg &optional force-heading)
7559 "Insert a new heading with the same level and TODO state as current heading.
7560 If the heading has no TODO state, or if the state is DONE, use the first
7561 state (TODO by default). Also one prefix arg, force first state. With two
7562 prefix args, force inserting at the end of the parent subtree."
7564 (when (or force-heading (not (org-insert-item 'checkbox)))
7565 (org-insert-heading (or (and (equal arg '(16)) '(16))
7568 (org-back-to-heading)
7569 (outline-previous-heading)
7570 (looking-at org-todo-line-regexp))
7574 (not (match-beginning 2))
7575 (member (match-string 2) org-done-keywords))
7576 (car org-todo-keywords-1)
7580 (run-hook-with-args-until-success
7581 'org-todo-get-default-hook new-mark-x nil)
7583 (beginning-of-line 1)
7584 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7585 (if org-treat-insert-todo-heading-as-state-change
7587 (insert new-mark " "))))
7588 (when org-provide-todo-statistics
7589 (org-update-parent-todo-statistics))))
7591 (defun org-insert-subheading (arg)
7592 "Insert a new subheading and demote it.
7593 Works for outline headings and for plain lists alike."
7595 (org-insert-heading arg)
7597 ((org-at-heading-p) (org-do-demote))
7598 ((org-at-item-p) (org-indent-item))))
7600 (defun org-insert-todo-subheading (arg)
7601 "Insert a new subheading with TODO keyword or checkbox and demote it.
7602 Works for outline headings and for plain lists alike."
7604 (org-insert-todo-heading arg)
7606 ((org-at-heading-p) (org-do-demote))
7607 ((org-at-item-p) (org-indent-item))))
7609 ;;; Promotion and Demotion
7611 (defvar org-after-demote-entry-hook nil
7612 "Hook run after an entry has been demoted.
7613 The cursor will be at the beginning of the entry.
7614 When a subtree is being demoted, the hook will be called for each node.")
7616 (defvar org-after-promote-entry-hook nil
7617 "Hook run after an entry has been promoted.
7618 The cursor will be at the beginning of the entry.
7619 When a subtree is being promoted, the hook will be called for each node.")
7621 (defun org-promote-subtree ()
7622 "Promote the entire subtree.
7623 See also `org-promote'."
7626 (org-with-limited-levels (org-map-tree 'org-promote)))
7627 (org-fix-position-after-promote))
7629 (defun org-demote-subtree ()
7630 "Demote the entire subtree. See `org-demote'.
7631 See also `org-promote'."
7634 (org-with-limited-levels (org-map-tree 'org-demote)))
7635 (org-fix-position-after-promote))
7638 (defun org-do-promote ()
7639 "Promote the current heading higher up the tree.
7640 If the region is active in `transient-mark-mode', promote all headings
7644 (if (org-region-active-p)
7645 (org-map-region 'org-promote (region-beginning) (region-end))
7647 (org-fix-position-after-promote))
7649 (defun org-do-demote ()
7650 "Demote the current heading lower down the tree.
7651 If the region is active in `transient-mark-mode', demote all headings
7655 (if (org-region-active-p)
7656 (org-map-region 'org-demote (region-beginning) (region-end))
7658 (org-fix-position-after-promote))
7660 (defun org-fix-position-after-promote ()
7661 "Make sure that after pro/demotion cursor position is right."
7662 (let ((pos (point)))
7663 (when (save-excursion
7664 (beginning-of-line 1)
7665 (looking-at org-todo-line-regexp)
7666 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7667 (cond ((eobp) (insert " "))
7668 ((eolp) (insert " "))
7669 ((equal (char-after) ?\ ) (forward-char 1))))))
7671 (defun org-current-level ()
7672 "Return the level of the current entry, or nil if before the first headline.
7673 The level is the number of stars at the beginning of the headline."
7675 (org-with-limited-levels
7676 (if (ignore-errors (org-back-to-heading t))
7677 (funcall outline-level)))))
7679 (defun org-get-previous-line-level ()
7680 "Return the outline depth of the last headline before the current line.
7681 Returns 0 for the first headline in the buffer, and nil if before the
7683 (let ((current-level (org-current-level))
7684 (prev-level (when (> (line-number-at-pos) 1)
7686 (beginning-of-line 0)
7687 (org-current-level)))))
7688 (cond ((null current-level) nil) ; Before first headline
7689 ((null prev-level) 0) ; At first headline
7692 (defun org-reduced-level (l)
7693 "Compute the effective level of a heading.
7694 This takes into account the setting of `org-odd-levels-only'."
7697 (org-odd-levels-only (1+ (floor (/ l 2))))
7700 (defun org-level-increment ()
7701 "Return the number of stars that will be added or removed at a
7702 time to headlines when structure editing, based on the value of
7703 `org-odd-levels-only'."
7704 (if org-odd-levels-only 2 1))
7706 (defun org-get-valid-level (level &optional change)
7707 "Rectify a level change under the influence of `org-odd-levels-only'
7708 LEVEL is a current level, CHANGE is by how much the level should be
7709 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7710 even level numbers will become the next higher odd number."
7711 (if org-odd-levels-only
7712 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7713 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7714 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7715 (max 1 (+ level (or change 0)))))
7717 (if (boundp 'define-obsolete-function-alias)
7718 (if (or (featurep 'xemacs) (< emacs-major-version 23))
7719 (define-obsolete-function-alias 'org-get-legal-level
7720 'org-get-valid-level)
7721 (define-obsolete-function-alias 'org-get-legal-level
7722 'org-get-valid-level "23.1")))
7724 (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
7725 ;; ̀org-with-limited-levels'
7726 (defun org-promote ()
7727 "Promote the current heading higher up the tree.
7728 If the region is active in `transient-mark-mode', promote all headings
7730 (org-back-to-heading t)
7731 (let* ((level (save-match-data (funcall outline-level)))
7732 (after-change-functions (remove 'flyspell-after-change-function
7733 after-change-functions))
7734 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7735 (diff (abs (- level (length up-head) -1))))
7736 (cond ((and (= level 1) org-called-with-limited-levels
7737 org-allow-promoting-top-level-subtree)
7738 (replace-match "# " nil t))
7740 (error "Cannot promote to level 0. UNDO to recover if necessary"))
7741 (t (replace-match up-head nil t)))
7742 ;; Fixup tag positioning
7744 (and org-auto-align-tags (org-set-tags nil t))
7745 (if org-adapt-indentation (org-fixup-indentation (- diff))))
7746 (run-hooks 'org-after-promote-entry-hook)))
7748 (defun org-demote ()
7749 "Demote the current heading lower down the tree.
7750 If the region is active in `transient-mark-mode', demote all headings
7752 (org-back-to-heading t)
7753 (let* ((level (save-match-data (funcall outline-level)))
7754 (after-change-functions (remove 'flyspell-after-change-function
7755 after-change-functions))
7756 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7757 (diff (abs (- level (length down-head) -1))))
7758 (replace-match down-head nil t)
7759 ;; Fixup tag positioning
7760 (and org-auto-align-tags (org-set-tags nil t))
7761 (if org-adapt-indentation (org-fixup-indentation diff))
7762 (run-hooks 'org-after-demote-entry-hook)))
7764 (defun org-cycle-level ()
7765 "Cycle the level of an empty headline through possible states.
7766 This goes first to child, then to parent, level, then up the hierarchy.
7767 After top level, it switches back to sibling level."
7769 (let ((org-adapt-indentation nil))
7770 (when (org-point-at-end-of-empty-headline)
7771 (setq this-command 'org-cycle-level) ; Only needed for caching
7772 (let ((cur-level (org-current-level))
7773 (prev-level (org-get-previous-line-level)))
7775 ;; If first headline in file, promote to top-level.
7777 (loop repeat (/ (- cur-level 1) (org-level-increment))
7778 do (org-do-promote)))
7779 ;; If same level as prev, demote one.
7780 ((= prev-level cur-level)
7782 ;; If parent is top-level, promote to top level if not already.
7784 (loop repeat (/ (- cur-level 1) (org-level-increment))
7785 do (org-do-promote)))
7786 ;; If top-level, return to prev-level.
7788 (loop repeat (/ (- prev-level 1) (org-level-increment))
7789 do (org-do-demote)))
7790 ;; If less than prev-level, promote one.
7791 ((< cur-level prev-level)
7793 ;; If deeper than prev-level, promote until higher than
7795 ((> cur-level prev-level)
7796 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7797 do (org-do-promote))))
7800 (defun org-map-tree (fun)
7801 "Call FUN for every heading underneath the current one."
7802 (org-back-to-heading)
7803 (let ((level (funcall outline-level)))
7807 (outline-next-heading)
7808 (> (funcall outline-level) level))
7812 (defun org-map-region (fun beg end)
7813 "Call FUN for every heading between BEG and END."
7814 (let ((org-ignore-region t))
7816 (setq end (copy-marker end))
7818 (if (and (re-search-forward org-outline-regexp-bol nil t)
7822 (outline-next-heading)
7827 (defvar org-property-end-re) ; silence byte-compiler
7828 (defun org-fixup-indentation (diff)
7829 "Change the indentation in the current entry by DIFF.
7830 However, if any line in the current entry has no indentation, or if it
7831 would end up with no indentation after the change, nothing at all is done."
7833 (let ((end (save-excursion (outline-next-heading)
7835 (prohibit (if (> diff 0)
7837 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7839 (unless (save-excursion (end-of-line 1)
7840 (re-search-forward prohibit end t))
7841 (while (and (< (point) end)
7842 (re-search-forward "^[ \t]+" end t))
7843 (goto-char (match-end 0))
7844 (setq col (current-column))
7845 (if (< diff 0) (replace-match ""))
7846 (org-indent-to-column (+ diff col))))
7847 (move-marker end nil))))
7849 (defun org-convert-to-odd-levels ()
7850 "Convert an org-mode file with all levels allowed to one with odd levels.
7851 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7854 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7855 (let ((outline-level 'org-outline-level)
7856 (org-odd-levels-only nil) n)
7858 (goto-char (point-min))
7859 (while (re-search-forward "^\\*\\*+ " nil t)
7860 (setq n (- (length (match-string 0)) 2))
7861 (while (>= (setq n (1- n)) 0)
7863 (end-of-line 1))))))
7865 (defun org-convert-to-oddeven-levels ()
7866 "Convert an org-mode file with only odd levels to one with odd/even levels.
7867 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7868 file contains a section with an even level, conversion would
7869 destroy the structure of the file. An error is signaled in this
7872 (goto-char (point-min))
7873 ;; First check if there are no even levels
7874 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7875 (org-show-context t)
7876 (error "Not all levels are odd in this file. Conversion not possible"))
7877 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7878 (let ((outline-regexp org-outline-regexp)
7879 (outline-level 'org-outline-level)
7880 (org-odd-levels-only nil) n)
7882 (goto-char (point-min))
7883 (while (re-search-forward "^\\*\\*+ " nil t)
7884 (setq n (/ (1- (length (match-string 0))) 2))
7885 (while (>= (setq n (1- n)) 0)
7887 (end-of-line 1))))))
7889 (defun org-tr-level (n)
7890 "Make N odd if required."
7891 (if org-odd-levels-only (1+ (/ n 2)) n))
7893 ;;; Vertical tree motion, cutting and pasting of subtrees
7895 (defun org-move-subtree-up (&optional arg)
7896 "Move the current subtree up past ARG headlines of the same level."
7898 (org-move-subtree-down (- (prefix-numeric-value arg))))
7900 (defun org-move-subtree-down (&optional arg)
7901 "Move the current subtree down past ARG headlines of the same level."
7903 (setq arg (prefix-numeric-value arg))
7904 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7905 'org-get-last-sibling))
7906 (ins-point (make-marker))
7908 (col (current-column))
7909 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7911 (org-back-to-heading)
7914 (setq ne-beg (org-back-over-empty-lines))
7917 (save-excursion (outline-end-of-heading)
7918 (setq folded (outline-invisible-p)))
7919 (outline-end-of-subtree))
7920 (outline-next-heading)
7921 (setq ne-end (org-back-over-empty-lines))
7924 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7925 ;; include less whitespace
7928 (forward-line (- ne-beg ne-end))
7929 (setq beg (point))))
7930 ;; Find insertion point, with error handling
7932 (or (and (funcall movfunc) (looking-at org-outline-regexp))
7933 (progn (goto-char beg0)
7934 (user-error "Cannot move past superior level or buffer limit")))
7935 (setq cnt (1- cnt)))
7937 ;; Moving forward - still need to move over subtree
7938 (progn (org-end-of-subtree t t)
7940 (org-back-over-empty-lines)
7941 (or (bolp) (newline)))))
7942 (setq ne-ins (org-back-over-empty-lines))
7943 (move-marker ins-point (point))
7944 (setq txt (buffer-substring beg end))
7945 (org-save-markers-in-region beg end)
7946 (delete-region beg end)
7947 (org-remove-empty-overlays-at beg)
7948 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7949 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7950 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7951 (let ((bbb (point)))
7952 (insert-before-markers txt)
7953 (org-reinstall-markers-in-region bbb)
7954 (move-marker ins-point bbb))
7955 (or (bolp) (insert "\n"))
7956 (setq ins-end (point))
7957 (goto-char ins-point)
7958 (org-skip-whitespace)
7959 (when (and (< arg 0)
7960 (org-first-sibling-p)
7962 ;; Move whitespace back to beginning
7965 (let ((kill-whole-line t))
7966 (kill-line (- ne-ins ne-beg)) (point)))
7967 (insert (make-string (- ne-ins ne-beg) ?\n)))
7968 (move-marker ins-point nil)
7973 (org-cycle-hide-drawers 'children))
7974 (org-clean-visibility-after-subtree-move)
7975 ;; move back to the initial column we were at
7976 (move-to-column col)))
7978 (defvar org-subtree-clip ""
7979 "Clipboard for cut and paste of subtrees.
7980 This is actually only a copy of the kill, because we use the normal kill
7981 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7983 (defvar org-subtree-clip-folded nil
7984 "Was the last copied subtree folded?
7985 This is used to fold the tree back after pasting.")
7987 (defun org-cut-subtree (&optional n)
7988 "Cut the current subtree into the clipboard.
7989 With prefix arg N, cut this many sequential subtrees.
7990 This is a short-hand for marking the subtree and then cutting it."
7992 (org-copy-subtree n 'cut))
7994 (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
7995 "Cut the current subtree into the clipboard.
7996 With prefix arg N, cut this many sequential subtrees.
7997 This is a short-hand for marking the subtree and then copying it.
7998 If CUT is non-nil, actually cut the subtree.
7999 If FORCE-STORE-MARKERS is non-nil, store the relative locations
8000 of some markers in the region, even if CUT is non-nil. This is
8001 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
8003 (let (beg end folded (beg0 (point)))
8004 (if (org-called-interactively-p 'any)
8005 (org-back-to-heading nil) ; take what looks like a subtree
8006 (org-back-to-heading t)) ; take what is really there
8008 (skip-chars-forward " \t\r\n")
8011 (outline-next-heading)
8012 (save-excursion (outline-end-of-heading)
8013 (setq folded (outline-invisible-p)))
8015 (org-forward-heading-same-level (1- n) t)
8017 (org-end-of-subtree t t)))
8021 (setq org-subtree-clip-folded folded)
8022 (when (or cut force-store-markers)
8023 (org-save-markers-in-region beg end))
8024 (if cut (kill-region beg end) (copy-region-as-kill beg end))
8025 (setq org-subtree-clip (current-kill 0))
8026 (message "%s: Subtree(s) with %d characters"
8027 (if cut "Cut" "Copied")
8028 (length org-subtree-clip)))))
8030 (defun org-paste-subtree (&optional level tree for-yank)
8031 "Paste the clipboard as a subtree, with modification of headline level.
8032 The entire subtree is promoted or demoted in order to match a new headline
8035 If the cursor is at the beginning of a headline, the same level as
8036 that headline is used to paste the tree.
8038 If not, the new level is derived from the *visible* headings
8039 before and after the insertion point, and taken to be the inferior headline
8040 level of the two. So if the previous visible heading is level 3 and the
8041 next is level 4 (or vice versa), level 4 will be used for insertion.
8042 This makes sure that the subtree remains an independent subtree and does
8043 not swallow low level entries.
8045 You can also force a different level, either by using a numeric prefix
8046 argument, or by inserting the heading marker by hand. For example, if the
8047 cursor is after \"*****\", then the tree will be shifted to level 5.
8049 If optional TREE is given, use this text instead of the kill ring.
8051 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
8052 move back over whitespace before inserting, and move point to the end of
8053 the inserted text when done."
8055 (setq tree (or tree (and kill-ring (current-kill 0))))
8056 (unless (org-kill-is-subtree-p tree)
8058 (substitute-command-keys
8059 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
8060 (org-with-limited-levels
8061 (let* ((visp (not (outline-invisible-p)))
8063 (^re_ "\\(\\*+\\)[ \t]*")
8064 (old-level (if (string-match org-outline-regexp-bol txt)
8065 (- (match-end 0) (match-beginning 0) 1)
8067 (force-level (cond (level (prefix-numeric-value level))
8068 ((and (looking-at "[ \t]*$")
8070 "^\\*+$" (buffer-substring
8071 (point-at-bol) (point))))
8072 (- (match-end 1) (match-beginning 1)))
8074 (looking-at org-outline-regexp))
8075 (- (match-end 0) (point) 1))))
8076 (previous-level (save-excursion
8079 (outline-previous-visible-heading 1)
8080 (if (looking-at ^re_)
8081 (- (match-end 0) (match-beginning 0) 1)
8084 (next-level (save-excursion
8087 (or (looking-at org-outline-regexp)
8088 (outline-next-visible-heading 1))
8089 (if (looking-at ^re_)
8090 (- (match-end 0) (match-beginning 0) 1)
8093 (new-level (or force-level (max previous-level next-level)))
8094 (shift (if (or (= old-level -1)
8096 (= old-level new-level))
8098 (- new-level old-level)))
8099 (delta (if (> shift 0) -1 1))
8100 (func (if (> shift 0) 'org-demote 'org-promote))
8101 (org-odd-levels-only nil)
8103 ;; Remove the forced level indicator
8105 (delete-region (point-at-bol) (point)))
8107 (beginning-of-line (if (bolp) 1 2))
8109 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
8110 (insert-before-markers txt)
8111 (unless (string-match "\n\\'" txt) (insert "\n"))
8112 (setq newend (point))
8113 (org-reinstall-markers-in-region beg)
8116 (skip-chars-forward " \t\n\r")
8118 (if (and (outline-invisible-p) visp)
8119 (save-excursion (outline-show-heading)))
8120 ;; Shift if necessary
8123 (narrow-to-region beg end)
8124 (while (not (= shift 0))
8125 (org-map-region func (point-min) (point-max))
8126 (setq shift (+ delta shift)))
8127 (goto-char (point-min))
8128 (setq newend (point-max))))
8129 (when (or (org-called-interactively-p 'interactive) for-yank)
8130 (message "Clipboard pasted as level %d subtree" new-level))
8131 (if (and (not for-yank) ; in this case, org-yank will decide about folding
8133 (eq org-subtree-clip (current-kill 0))
8134 org-subtree-clip-folded)
8135 ;; The tree was folded before it was killed/copied
8137 (and for-yank (goto-char newend)))))
8139 (defun org-kill-is-subtree-p (&optional txt)
8140 "Check if the current kill is an outline subtree, or a set of trees.
8141 Returns nil if kill does not start with a headline, or if the first
8142 headline level is not the largest headline level in the tree.
8143 So this will actually accept several entries of equal levels as well,
8144 which is OK for `org-paste-subtree'.
8145 If optional TXT is given, check this string instead of the current kill."
8146 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
8147 (re (org-get-limited-outline-regexp))
8148 (^re (concat "^" re))
8149 (start-level (and kill
8151 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
8153 (- (match-end 2) (match-beginning 2) 1)))
8154 (start (1+ (or (match-beginning 2) -1))))
8155 (if (not start-level)
8157 nil) ;; does not even start with a heading
8159 (while (setq start (string-match ^re kill (1+ start)))
8160 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
8164 (defvar org-markers-to-move nil
8165 "Markers that should be moved with a cut-and-paste operation.
8166 Those markers are stored together with their positions relative to
8167 the start of the region.")
8169 (defun org-save-markers-in-region (beg end)
8170 "Check markers in region.
8171 If these markers are between BEG and END, record their position relative
8172 to BEG, so that after moving the block of text, we can put the markers back
8174 This function gets called just before an entry or tree gets cut from the
8175 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
8176 called immediately, to move the markers with the entries."
8177 (setq org-markers-to-move nil)
8178 (when (featurep 'org-clock)
8179 (org-clock-save-markers-for-cut-and-paste beg end))
8180 (when (featurep 'org-agenda)
8181 (org-agenda-save-markers-for-cut-and-paste beg end)))
8183 (defun org-check-and-save-marker (marker beg end)
8184 "Check if MARKER is between BEG and END.
8185 If yes, remember the marker and the distance to BEG."
8186 (when (and (marker-buffer marker)
8187 (equal (marker-buffer marker) (current-buffer)))
8188 (if (and (>= marker beg) (< marker end))
8189 (push (cons marker (- marker beg)) org-markers-to-move))))
8191 (defun org-reinstall-markers-in-region (beg)
8192 "Move all remembered markers to their position relative to BEG."
8194 (move-marker (car x) (+ beg (cdr x))))
8195 org-markers-to-move)
8196 (setq org-markers-to-move nil))
8198 (defun org-narrow-to-subtree ()
8199 "Narrow buffer to the current subtree."
8203 (org-with-limited-levels
8205 (progn (org-back-to-heading t) (point))
8206 (progn (org-end-of-subtree t t)
8207 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
8210 (defun org-narrow-to-block ()
8211 "Narrow buffer to the current block."
8213 (let* ((case-fold-search t)
8214 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
8215 "^[ \t]*#\\+end_.*")))
8217 (narrow-to-region (car blockp) (cdr blockp))
8218 (error "Not in a block"))))
8221 (defvar org-property-drawer-re))
8223 (defvar org-property-start-re) ;; defined below
8224 (defun org-clone-subtree-with-time-shift (n &optional shift)
8225 "Clone the task (subtree) at point N times.
8226 The clones will be inserted as siblings.
8228 In interactive use, the user will be prompted for the number of
8229 clones to be produced, and for a time SHIFT, which may be a
8230 repeater as used in time stamps, for example `+3d'.
8232 When a valid repeater is given and the entry contains any time
8233 stamps, the clones will become a sequence in time, with time
8234 stamps in the subtree shifted for each clone produced. If SHIFT
8235 is nil or the empty string, time stamps will be left alone. The
8236 ID property of the original subtree is removed.
8238 If the original subtree did contain time stamps with a repeater,
8239 the following will happen:
8240 - the repeater will be removed in each clone
8241 - an additional clone will be produced, with the current, unshifted
8242 date(s) in the entry.
8243 - the original entry will be placed *after* all the clones, with
8245 - the start days in the repeater in the original entry will be shifted
8246 to past the last clone.
8247 In this way you can spell out a number of instances of a repeating task,
8248 and still retain the repeater to cover future instances of the task."
8249 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
8250 (let (beg end template task idprop
8251 shift-n shift-what doshift nmin nmax (n-no-remove -1)
8252 (drawer-re org-drawer-regexp))
8253 (if (not (and (integerp n) (> n 0)))
8254 (error "Invalid number of replications %s" n))
8255 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
8256 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
8258 (error "Invalid shift specification %s" shift))
8260 (setq shift-n (string-to-number (match-string 1 shift))
8261 shift-what (cdr (assoc (match-string 2 shift)
8262 '(("d" . day) ("w" . week)
8263 ("m" . month) ("y" . year))))))
8264 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
8265 (setq nmin 1 nmax n)
8266 (org-back-to-heading t)
8268 (setq idprop (org-entry-get nil "ID"))
8269 (org-end-of-subtree t t)
8270 (or (bolp) (insert "\n"))
8272 (setq template (buffer-substring beg end))
8274 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
8275 (delete-region beg end)
8277 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
8279 (loop for n from nmin to nmax do
8284 (goto-char (point-min))
8286 (and idprop (if org-clone-delete-id
8287 (org-entry-delete nil "ID")
8288 (org-id-get-create t)))
8290 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
8292 (goto-char (point-min))
8293 (while (re-search-forward drawer-re nil t)
8295 (org-remove-empty-drawer-at d (point))) org-drawers)))
8296 (goto-char (point-min))
8298 (while (re-search-forward org-ts-regexp-both nil t)
8299 (org-timestamp-change (* n shift-n) shift-what))
8300 (unless (= n n-no-remove)
8301 (goto-char (point-min))
8302 (while (re-search-forward org-ts-regexp nil t)
8304 (goto-char (match-beginning 0))
8305 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
8306 (delete-region (match-beginning 1) (match-end 1)))))))
8307 (setq task (buffer-string)))
8313 (defun org-sort (with-case)
8314 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
8315 Optional argument WITH-CASE means sort case-sensitively."
8318 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
8319 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
8321 (org-call-with-arg 'org-sort-entries with-case))))
8323 (defun org-sort-remove-invisible (s)
8324 (remove-text-properties 0 (length s) org-rm-props s)
8325 (while (string-match org-bracket-link-regexp s)
8326 (setq s (replace-match (if (match-end 2)
8328 (match-string 1 s)) t t s)))
8331 (defvar org-priority-regexp) ; defined later in the file
8333 (defvar org-after-sorting-entries-or-items-hook nil
8334 "Hook that is run after a bunch of entries or items have been sorted.
8335 When children are sorted, the cursor is in the parent line when this
8336 hook gets called. When a region or a plain list is sorted, the cursor
8337 will be in the first entry of the sorted region/list.")
8339 (defun org-sort-entries
8340 (&optional with-case sorting-type getkey-func compare-func property)
8341 "Sort entries on a certain level of an outline tree.
8342 If there is an active region, the entries in the region are sorted.
8343 Else, if the cursor is before the first entry, sort the top-level items.
8344 Else, the children of the entry at point are sorted.
8346 Sorting can be alphabetically, numerically, by date/time as given by
8347 a time stamp, by a property or by priority.
8349 The command prompts for the sorting type unless it has been given to the
8350 function through the SORTING-TYPE argument, which needs to be a character,
8351 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
8352 precise meaning of each character:
8354 n Numerically, by converting the beginning of the entry/item to a number.
8355 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8356 o By order of TODO keywords.
8357 t By date/time, either the first active time stamp in the entry, or, if
8358 none exist, by the first inactive one.
8359 s By the scheduled date/time.
8360 d By deadline date/time.
8361 c By creation time, which is assumed to be the first inactive time stamp
8362 at the beginning of a line.
8363 p By priority according to the cookie.
8364 r By the value of a property.
8366 Capital letters will reverse the sort order.
8368 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8369 called with point at the beginning of the record. It must return either
8370 a string or a number that should serve as the sorting key for that record.
8372 Comparing entries ignores case by default. However, with an optional argument
8373 WITH-CASE, the sorting considers case as well."
8375 (let ((case-func (if with-case 'identity 'downcase))
8377 ;; The clock marker is lost when using `sort-subr', let's
8378 ;; store the clocking string.
8379 (when (equal (marker-buffer org-clock-marker) (current-buffer))
8381 (goto-char org-clock-marker)
8382 (looking-back "^.*") (match-string-no-properties 0))))
8383 start beg end stars re re2
8385 ;; Find beginning and end of region to sort
8387 ((org-region-active-p)
8388 ;; we will sort the region
8389 (setq end (region-end)
8391 (goto-char (region-beginning))
8392 (if (not (org-at-heading-p)) (outline-next-heading))
8393 (setq start (point)))
8394 ((or (org-at-heading-p)
8395 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8396 ;; we will sort the children of the current headline
8397 (org-back-to-heading)
8399 end (progn (org-end-of-subtree t t)
8400 (or (bolp) (insert "\n"))
8401 (org-back-over-empty-lines)
8406 (outline-next-heading))
8408 ;; we will sort the top-level entries in this file
8409 (goto-char (point-min))
8410 (or (org-at-heading-p) (outline-next-heading))
8411 (setq start (point))
8412 (goto-char (point-max))
8413 (beginning-of-line 1)
8414 (when (looking-at ".*?\\S-")
8415 ;; File ends in a non-white line
8418 (setq end (point-max))
8419 (setq what "top-level")
8424 (if (>= beg end) (error "Nothing to sort"))
8426 (looking-at "\\(\\*+\\)")
8427 (setq stars (match-string 1)
8428 re (concat "^" (regexp-quote stars) " +")
8429 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8430 txt (buffer-substring beg end))
8431 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8432 (if (and (not (equal stars "*")) (string-match re2 txt))
8433 (error "Region to sort contains a level above the first entry"))
8435 (unless sorting-type
8437 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8438 [t]ime
[s]cheduled [d]eadline [c]reated
8439 A/N/P/R/O/F/T/S/D/C means reversed:"
8441 (setq sorting-type (read-char-exclusive))
8444 (and (= (downcase sorting-type) ?f)
8446 (org-icompleting-read "Sort using function: "
8447 obarray 'fboundp t nil nil))
8448 (setq getkey-func (intern getkey-func))))
8450 (and (= (downcase sorting-type) ?r)
8453 (org-icompleting-read "Property: "
8454 (mapcar 'list (org-buffer-property-keys t))
8457 (message "Sorting entries...")
8460 (narrow-to-region start end)
8461 (let ((dcst (downcase sorting-type))
8462 (case-fold-search nil)
8463 (now (current-time)))
8465 (/= dcst sorting-type)
8466 ;; This function moves to the beginning character of the "record" to
8469 (if (re-search-forward re nil t)
8470 (goto-char (match-beginning 0))
8471 (goto-char (point-max))))
8472 ;; This function moves to the last character of the "record" being
8477 (outline-forward-same-level 1)
8479 (goto-char (point-max))))))
8480 ;; This function returns the value that gets sorted against.
8484 (if (looking-at org-complex-heading-regexp)
8485 (string-to-number (match-string 4))
8488 (if (looking-at org-complex-heading-regexp)
8489 (funcall case-func (match-string 4))
8492 (let ((end (save-excursion (outline-next-heading) (point))))
8493 (if (or (re-search-forward org-ts-regexp end t)
8494 (re-search-forward org-ts-regexp-both end t))
8495 (org-time-string-to-seconds (match-string 0))
8496 (org-float-time now))))
8498 (let ((end (save-excursion (outline-next-heading) (point))))
8499 (if (re-search-forward
8500 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8502 (org-time-string-to-seconds (match-string 0))
8503 (org-float-time now))))
8505 (let ((end (save-excursion (outline-next-heading) (point))))
8506 (if (re-search-forward org-scheduled-time-regexp end t)
8507 (org-time-string-to-seconds (match-string 1))
8508 (org-float-time now))))
8510 (let ((end (save-excursion (outline-next-heading) (point))))
8511 (if (re-search-forward org-deadline-time-regexp end t)
8512 (org-time-string-to-seconds (match-string 1))
8513 (org-float-time now))))
8515 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8516 (string-to-char (match-string 2))
8517 org-default-priority))
8519 (or (org-entry-get nil property) ""))
8521 (if (looking-at org-complex-heading-regexp)
8522 (- 9999 (length (member (match-string 2)
8523 org-todo-keywords-1)))))
8527 (setq tmp (funcall getkey-func))
8528 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8530 (error "Invalid key function `%s'" getkey-func)))
8531 (t (error "Invalid sorting type `%c'" sorting-type))))
8534 ((= dcst ?a) 'string<)
8535 ((= dcst ?f) compare-func)
8536 ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
8537 (run-hooks 'org-after-sorting-entries-or-items-hook)
8538 ;; Reset the clock marker if needed
8542 (search-forward cmstr nil t)
8543 (move-marker org-clock-marker (point))))
8544 (message "Sorting entries...done")))
8546 (defun org-do-sort (table what &optional with-case sorting-type)
8547 "Sort TABLE of WHAT according to SORTING-TYPE.
8548 The user will be prompted for the SORTING-TYPE if the call to this
8549 function does not specify it. WHAT is only for the prompt, to indicate
8550 what is being sorted. The sorting key will be extracted from
8551 the car of the elements of the table.
8552 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8553 (unless sorting-type
8555 "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
8557 (setq sorting-type (read-char-exclusive)))
8558 (let ((dcst (downcase sorting-type))
8559 extractfun comparefun)
8560 ;; Define the appropriate functions
8563 (setq extractfun 'string-to-number
8564 comparefun (if (= dcst sorting-type) '< '>)))
8566 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8567 (lambda(x) (downcase (org-sort-remove-invisible x))))
8568 comparefun (if (= dcst sorting-type)
8570 (lambda (a b) (and (not (string< a b))
8571 (not (string= a b)))))))
8575 (if (or (string-match org-ts-regexp x)
8576 (string-match org-ts-regexp-both x))
8578 (org-time-string-to-time (match-string 0 x)))
8580 comparefun (if (= dcst sorting-type) '< '>)))
8581 (t (error "Invalid sorting type `%c'" sorting-type)))
8583 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8585 (lambda (a b) (funcall comparefun (car a) (car b))))))
8588 ;;; The orgstruct minor mode
8590 ;; Define a minor mode which can be used in other modes in order to
8591 ;; integrate the org-mode structure editing commands.
8593 ;; This is really a hack, because the org-mode structure commands use
8594 ;; keys which normally belong to the major mode. Here is how it
8595 ;; works: The minor mode defines all the keys necessary to operate the
8596 ;; structure commands, but wraps the commands into a function which
8597 ;; tests if the cursor is currently at a headline or a plain list
8598 ;; item. If that is the case, the structure command is used,
8599 ;; temporarily setting many Org-mode variables like regular
8600 ;; expressions for filling etc. However, when any of those keys is
8601 ;; used at a different location, function uses `key-binding' to look
8602 ;; up if the key has an associated command in another currently active
8603 ;; keymap (minor modes, major mode, global), and executes that
8604 ;; command. There might be problems if any of the keys is otherwise
8605 ;; used as a prefix key.
8607 (defcustom orgstruct-heading-prefix-regexp ""
8608 "Regexp that matches the custom prefix of Org headlines in
8609 orgstruct(++)-mode."
8612 ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
8614 (defcustom orgstruct-setup-hook nil
8615 "Hook run after orgstruct-mode-map is filled."
8619 (defvar orgstruct-initialized nil)
8621 (defvar org-local-vars nil
8622 "List of local variables, for use by `orgstruct-mode'.")
8625 (define-minor-mode orgstruct-mode
8626 "Toggle the minor mode `orgstruct-mode'.
8627 This mode is for using Org-mode structure commands in other
8628 modes. The following keys behave as if Org-mode were active, if
8629 the cursor is on a headline, or on a plain list item (both as
8630 defined by Org-mode)."
8631 nil " OrgStruct" (make-sparse-keymap)
8632 (when orgstruct-mode
8633 (org-load-modules-maybe)
8634 (unless orgstruct-initialized
8636 (setq orgstruct-initialized t))))
8639 (defun turn-on-orgstruct ()
8640 "Unconditionally turn on `orgstruct-mode'."
8643 (defvar org-fb-vars nil)
8644 (make-variable-buffer-local 'org-fb-vars)
8645 (defun orgstruct++-mode (&optional arg)
8646 "Toggle `orgstruct-mode', the enhanced version of it.
8647 In addition to setting orgstruct-mode, this also exports all
8648 indentation and autofilling variables from org-mode into the
8649 buffer. It will also recognize item context in multiline items."
8651 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8653 (progn (orgstruct-mode -1)
8655 (org-set-local (car v)
8656 (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
8659 (setq org-fb-vars nil)
8664 "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
8665 (symbol-name (car x)))
8666 (setq var (car x) val (nth 1 x))
8667 (push (list var `(quote ,(eval var))) org-fb-vars)
8668 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8670 (org-set-local 'orgstruct-is-++ t))))
8672 (defvar orgstruct-is-++ nil
8673 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8674 (make-variable-buffer-local 'orgstruct-is-++)
8677 (defun turn-on-orgstruct++ ()
8678 "Unconditionally turn on `orgstruct++-mode'."
8679 (orgstruct++-mode 1))
8681 (defun orgstruct-error ()
8682 "Error when there is no default binding for a structure key."
8684 (error "This key has no function outside structure elements"))
8686 (defun orgstruct-setup ()
8687 "Setup orgstruct keymap."
8692 org-backward-element
8693 org-backward-heading-same-level
8696 org-forward-heading-same-level
8698 org-insert-heading-respect-content
8699 org-kill-note-or-show-branches
8701 org-narrow-to-subtree
8708 outline-next-visible-heading
8709 outline-previous-visible-heading
8713 (dolist (f (if (stringp f)
8716 '("-return" "tab" "left" "right" "up" "down")
8718 (let ((f (intern (concat f postfix))))
8722 (dolist (binding (nconc (where-is-internal f org-mode-map)
8723 (where-is-internal f outline-mode-map)))
8724 ;; TODO use local-function-key-map
8725 (dolist (rep '(("<tab>" . "TAB")
8726 ("<return>" . "RET")
8727 ("<escape>" . "ESC")
8728 ("<delete>" . "DEL")))
8729 (setq binding (read-kbd-macro (replace-regexp-in-string
8730 (regexp-quote (car rep))
8732 (key-description binding)))))
8733 (let ((key (lookup-key orgstruct-mode-map binding)))
8734 (when (or (not key) (numberp key))
8736 (org-defkey orgstruct-mode-map
8738 (orgstruct-make-binding f binding))
8740 (run-hooks 'orgstruct-setup-hook))
8742 (defun orgstruct-make-binding (fun key)
8743 "Create a function for binding in the structure minor mode.
8744 FUN is the command to call inside a table. KEY is the key that
8745 should be checked in for a command to execute outside of tables."
8746 (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
8749 (while (fboundp (intern nname))
8750 (setq nname (format "%s-%d" name (setq i (1+ i)))))
8751 (setq name (intern nname)))
8754 ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
8755 "Outside of structure, run the binding of `"
8756 (key-description key) "'.")
8759 (let* ((org-heading-regexp
8761 orgstruct-heading-prefix-regexp
8762 "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
8764 (concat orgstruct-heading-prefix-regexp "\\*+ "))
8765 (org-outline-regexp-bol
8766 (concat "^" org-outline-regexp))
8767 (outline-regexp org-outline-regexp)
8768 (outline-heading-end-regexp "\n")
8769 (outline-level 'outline-level)
8770 (outline-heading-alist))
8771 (when (org-context-p 'headline 'item
8772 ,(when (memq fun '(org-insert-heading))
8773 '(when orgstruct-is-++
8775 (org-run-like-in-org-mode ',fun)
8777 (let* ((orgstruct-mode)
8778 (binding (key-binding ,key)))
8779 (if (keymapp binding)
8780 (set-temporary-overlay-map binding)
8782 (or binding 'orgstruct-error)))))))
8785 (defun org-contextualize-keys (alist contexts)
8786 "Return valid elements in ALIST depending on CONTEXTS.
8788 `org-agenda-custom-commands' or `org-capture-templates' are the
8789 values used for ALIST, and `org-agenda-custom-commands-contexts'
8790 or `org-capture-templates-contexts' are the associated contexts
8793 ;; normalize contexts
8795 (lambda(c) (cond ((listp (cadr c))
8796 (list (car c) (car c) (cadr c)))
8797 ((string= "" (cadr c))
8798 (list (car c) (car c) (caddr c)))
8801 ;; loop over all commands or templates
8802 (while (setq c (pop a))
8805 ((not (assoc (car c) contexts))
8807 ((and (assoc (car c) contexts)
8808 (setq vrules (org-contextualize-validate-key
8811 (when (not (equal (car vr) (cadr vr)))
8812 (setq repl vr))) vrules)
8813 (if (not repl) (push c r)
8814 (push (cadr repl) s)
8817 (cdr (or (assoc (cadr repl) alist)
8818 (error "Undefined key `%s' as contextual replacement for `%s'"
8819 (cadr repl) (car c)))))
8821 ;; Return limited ALIST, possibly with keys modified, and deduplicated
8826 (let ((tpl (car x)))
8830 (equal y tpl)) s))) x)))
8833 (defun org-contextualize-validate-key (key contexts)
8834 "Check CONTEXTS for agenda or capture KEY."
8836 (while (setq r (pop contexts))
8840 (and (equal key (car r))
8841 (if (functionp rr) (funcall rr)
8842 (or (and (eq (car rr) 'in-file)
8844 (string-match (cdr rr) (buffer-file-name)))
8845 (and (eq (car rr) 'in-mode)
8846 (string-match (cdr rr) (symbol-name major-mode)))
8847 (and (eq (car rr) 'in-buffer)
8848 (string-match (cdr rr) (buffer-name)))
8849 (when (and (eq (car rr) 'not-in-file)
8851 (not (string-match (cdr rr) (buffer-file-name))))
8852 (when (eq (car rr) 'not-in-mode)
8853 (not (string-match (cdr rr) (symbol-name major-mode))))
8854 (when (eq (car rr) 'not-in-buffer)
8855 (not (string-match (cdr rr) (buffer-name)))))))
8858 (delete-dups (delq nil res))))
8860 (defun org-context-p (&rest contexts)
8861 "Check if local context is any of CONTEXTS.
8862 Possible values in the list of contexts are `table', `headline', and `item'."
8863 (let ((pos (point)))
8864 (goto-char (point-at-bol))
8865 (prog1 (or (and (memq 'table contexts)
8866 (looking-at "[ \t]*|"))
8867 (and (memq 'headline contexts)
8868 (looking-at org-outline-regexp))
8869 (and (memq 'item contexts)
8870 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
8871 (and (memq 'item-body contexts)
8875 (defun org-get-local-variables ()
8876 "Return a list of all local variables in an Org mode buffer."
8878 (with-current-buffer (get-buffer-create "*Org tmp*")
8881 (setq varlist (buffer-local-variables)))
8882 (kill-buffer "*Org tmp*")
8889 (list (car x) (cdr x))))
8890 (if (and (not (get (car x) 'org-state))
8892 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
8893 (symbol-name (car x))))
8897 (defun org-clone-local-variables (from-buffer &optional regexp)
8898 "Clone local variables from FROM-BUFFER.
8899 Optional argument REGEXP selects variables to clone."
8902 (and (symbolp (car pair))
8904 (string-match regexp (symbol-name (car pair))))
8905 (set (make-local-variable (car pair))
8907 (buffer-local-variables from-buffer)))
8910 (defun org-run-like-in-org-mode (cmd)
8911 "Run a command, pretending that the current buffer is in Org-mode.
8912 This will temporarily bind local variables that are typically bound in
8913 Org-mode to the values they have in Org-mode, and then interactively
8915 (org-load-modules-maybe)
8916 (unless org-local-vars
8917 (setq org-local-vars (org-get-local-variables)))
8919 (dolist (var org-local-vars)
8920 (when (or (not (boundp (car var)))
8921 (eq (symbol-value (car var))
8922 (default-value (car var))))
8923 (push (list (car var) `(quote ,(cadr var))) binds)))
8925 (call-interactively (quote ,cmd))))))
8929 (defun org-get-category (&optional pos force-refresh)
8930 "Get the category applying to position POS."
8932 (if force-refresh (org-refresh-category-properties))
8933 (let ((pos (or pos (point))))
8934 (or (get-text-property pos 'org-category)
8935 (progn (org-refresh-category-properties)
8936 (get-text-property pos 'org-category))))))
8938 (defun org-refresh-category-properties ()
8939 "Refresh category text properties in the buffer."
8940 (let ((case-fold-search t)
8941 (inhibit-read-only t)
8943 ((null org-category)
8944 (if buffer-file-name
8945 (file-name-sans-extension
8946 (file-name-nondirectory buffer-file-name))
8948 ((symbolp org-category) (symbol-name org-category))
8950 beg end cat pos optionp)
8951 (with-silent-modifications
8955 (goto-char (point-min))
8956 (put-text-property (point) (point-max) 'org-category def-cat)
8957 (while (re-search-forward
8958 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8959 (setq pos (match-end 0)
8960 optionp (equal (char-after (match-beginning 0)) ?#)
8961 cat (org-trim (match-string 2)))
8963 (setq beg (point-at-bol) end (point-max))
8964 (org-back-to-heading t)
8965 (setq beg (point) end (org-end-of-subtree t t)))
8966 (put-text-property beg end 'org-category cat)
8967 (put-text-property beg end 'org-category-position beg)
8968 (goto-char pos)))))))
8970 (defun org-refresh-properties (dprop tprop)
8971 "Refresh buffer text properties.
8972 DPROP is the drawer property and TPROP is the corresponding text
8974 (let ((case-fold-search t)
8975 (inhibit-read-only t) p)
8976 (with-silent-modifications
8980 (goto-char (point-min))
8981 (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
8982 (setq p (org-match-string-no-properties 1))
8984 (org-back-to-heading t)
8986 (point-at-bol) (point-at-eol) tprop p))))))))
8991 ;;; Link abbreviations
8993 (defun org-link-expand-abbrev (link)
8994 "Apply replacements as defined in `org-link-abbrev-alist'."
8995 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
8996 (let* ((key (match-string 1 link))
8997 (as (or (assoc key org-link-abbrev-alist-local)
8998 (assoc key org-link-abbrev-alist)))
8999 (tag (and (match-end 2) (match-string 3 link)))
9005 ((symbolp rpl) (funcall rpl tag))
9006 ((string-match "%(\\([^)]+\\))" rpl)
9007 (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
9008 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
9009 ((string-match "%h" rpl)
9010 (replace-match (url-hexify-string (or tag "")) t t rpl))
9011 (t (concat rpl tag)))))
9014 ;;; Storing and inserting links
9016 (defvar org-insert-link-history nil
9017 "Minibuffer history for links inserted with `org-insert-link'.")
9019 (defvar org-stored-links nil
9020 "Contains the links stored with `org-store-link'.")
9022 (defvar org-store-link-plist nil
9023 "Plist with info about the most recently link created with `org-store-link'.")
9025 (defvar org-link-protocols nil
9026 "Link protocols added to Org-mode using `org-add-link-type'.")
9028 (defvar org-store-link-functions nil
9029 "List of functions that are called to create and store a link.
9030 Each function will be called in turn until one returns a non-nil
9031 value. Each function should check if it is responsible for creating
9032 this link (for example by looking at the major mode).
9033 If not, it must exit and return nil.
9034 If yes, it should return a non-nil value after a calling
9035 `org-store-link-props' with a list of properties and values.
9036 Special properties are:
9038 :type The link prefix, like \"http\". This must be given.
9039 :link The link, like \"http://www.astro.uva.nl/~dominik\".
9040 This is obligatory as well.
9041 :description Optional default description for the second pair
9042 of brackets in an Org-mode link. The user can still change
9043 this when inserting this link into an Org-mode buffer.
9045 In addition to these, any additional properties can be specified
9046 and then used in capture templates.")
9048 (defun org-add-link-type (type &optional follow export)
9049 "Add TYPE to the list of `org-link-types'.
9050 Re-compute all regular expressions depending on `org-link-types'
9052 FOLLOW and EXPORT are two functions.
9054 FOLLOW should take the link path as the single argument and do whatever
9055 is necessary to follow the link, for example find a file or display
9058 EXPORT should format the link path for export to one of the export formats.
9059 It should be a function accepting three arguments:
9061 path the path of the link, the text after the prefix (like \"http:\")
9062 desc the description of the link, if any, or a description added by
9063 org-export-normalize-links if there is none
9064 format the export format, a symbol like `html' or `latex' or `ascii'..
9066 The function may use the FORMAT information to return different values
9067 depending on the format. The return value will be put literally into
9068 the exported file. If the return value is nil, this means Org should
9069 do what it normally does with links which do not have EXPORT defined.
9071 Org-mode has a built-in default for exporting links. If you are happy with
9072 this default, there is no need to define an export function for the link
9073 type. For a simple example of an export function, see `org-bbdb.el'."
9074 (add-to-list 'org-link-types type t)
9075 (org-make-link-regexps)
9076 (if (assoc type org-link-protocols)
9077 (setcdr (assoc type org-link-protocols) (list follow export))
9078 (push (list type follow export) org-link-protocols)))
9080 (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
9081 (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
9084 (defun org-store-link (arg)
9085 "\\<org-mode-map>Store an org-link to the current location.
9086 This link is added to `org-stored-links' and can later be inserted
9087 into an org-buffer with \\[org-insert-link].
9089 For some link types, a prefix arg is interpreted.
9090 For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
9091 For file links, arg negates `org-context-in-file-links'.
9093 A double prefix arg force skipping storing functions that are not
9094 part of Org's core."
9096 (org-load-modules-maybe)
9097 (setq org-store-link-plist nil) ; reset
9098 (org-with-limited-levels
9099 (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
9101 ((and (not (equal arg '(16)))
9104 nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
9105 org-store-link-functions))
9106 sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
9107 (or (and (cdr sfuns)
9109 (completing-read "Which function for creating the link? "
9110 sfunsn t (car sfunsn)))))
9111 (funcall (caar sfuns)))
9112 (setq link (plist-get org-store-link-plist :link)
9113 desc (or (plist-get org-store-link-plist :description) link))))
9114 ((org-src-edit-buffer-p)
9116 (while (or (not label)
9120 (goto-char (point-min))
9122 (regexp-quote (format org-coderef-label-format label))
9124 (when label (message "Label exists already") (sit-for 2))
9125 (setq label (read-string "Code line label: " label)))
9127 (setq link (format org-coderef-label-format label))
9128 (setq gc (- 79 (length link)))
9129 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
9131 (setq link (concat "(" label ")") desc nil)))
9133 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
9134 ;; We are in the agenda, link to referenced location
9135 (let ((m (or (get-text-property (point) 'org-hd-marker)
9136 (get-text-property (point) 'org-marker))))
9138 (org-with-point-at m
9140 (if (org-called-interactively-p 'any)
9141 (call-interactively 'org-store-link)
9142 (org-store-link nil)))))))
9144 ((eq major-mode 'calendar-mode)
9145 (let ((cd (calendar-cursor-to-date)))
9148 (car org-time-stamp-formats)
9150 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
9152 (org-store-link-props :type "calendar" :date cd)))
9154 ((eq major-mode 'help-mode)
9155 (setq link (concat "help:" (save-excursion
9156 (goto-char (point-min))
9157 (looking-at "^[^ ]+")
9159 (org-store-link-props :type "help"))
9161 ((eq major-mode 'w3-mode)
9162 (setq cpltxt (if (and (buffer-name)
9163 (not (string-match "Untitled" (buffer-name))))
9166 link (url-view-url t))
9167 (org-store-link-props :type "w3" :url (url-view-url t)))
9169 ((setq search (run-hook-with-args-until-success
9170 'org-create-file-search-functions))
9171 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
9173 (setq cpltxt (or description link)))
9175 ((eq major-mode 'image-mode)
9176 (setq cpltxt (concat "file:"
9177 (abbreviate-file-name buffer-file-name))
9179 (org-store-link-props :type "image" :file buffer-file-name))
9181 ((eq major-mode 'dired-mode)
9182 ;; link to the file in the current line
9183 (let ((file (dired-get-filename nil t)))
9185 (abbreviate-file-name
9186 (expand-file-name (dired-get-filename nil t)))
9187 ;; otherwise, no file so use current directory.
9189 (setq cpltxt (concat "file:" file)
9192 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
9193 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
9195 ((org-in-regexp "<<\\(.*?\\)>>")
9198 (abbreviate-file-name
9199 (buffer-file-name (buffer-base-buffer)))
9200 "::" (match-string 1))
9202 ((and (featurep 'org-id)
9203 (or (eq org-id-link-to-org-use-id t)
9204 (and (org-called-interactively-p 'any)
9205 (or (eq org-id-link-to-org-use-id 'create-if-interactive)
9206 (and (eq org-id-link-to-org-use-id
9207 'create-if-interactive-and-no-custom-id)
9209 (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
9210 ;; We can make a link using the ID.
9211 (setq link (condition-case nil
9212 (prog1 (org-id-store-link)
9213 (setq desc (plist-get org-store-link-plist :description)))
9215 ;; probably before first headline, link to file only
9217 (abbreviate-file-name
9218 (buffer-file-name (buffer-base-buffer))))))))
9220 ;; Just link to current headline
9221 (setq cpltxt (concat "file:"
9222 (abbreviate-file-name
9223 (buffer-file-name (buffer-base-buffer)))))
9224 ;; Add a context search string
9225 (when (org-xor org-context-in-file-links arg)
9226 (let* ((ee (org-element-at-point))
9227 (et (org-element-type ee))
9228 (ev (plist-get (cadr ee) :value)))
9230 ((org-at-heading-p) nil)
9231 ((eq et 'keyword) ev)
9232 ((org-region-active-p)
9233 (buffer-substring (region-beginning) (region-end)))))
9234 (when (or (null txt) (string-match "\\S-" txt))
9238 (org-make-org-heading-search-string txt)
9240 desc (or (and (eq et 'keyword) ev)
9241 (nth 4 (ignore-errors (org-heading-components)))
9243 (if (string-match "::\\'" cpltxt)
9244 (setq cpltxt (substring cpltxt 0 -2)))
9245 (setq link cpltxt))))
9247 ((buffer-file-name (buffer-base-buffer))
9248 ;; Just link to this file here.
9249 (setq cpltxt (concat "file:"
9250 (abbreviate-file-name
9251 (buffer-file-name (buffer-base-buffer)))))
9252 ;; Add a context string
9253 (when (org-xor org-context-in-file-links arg)
9254 (setq txt (if (org-region-active-p)
9255 (buffer-substring (region-beginning) (region-end))
9256 (buffer-substring (point-at-bol) (point-at-eol))))
9257 ;; Only use search option if there is some text.
9258 (when (string-match "\\S-" txt)
9260 (concat cpltxt "::" (org-make-org-heading-search-string txt))
9264 ((org-called-interactively-p 'interactive)
9265 (user-error "No method for storing a link from this buffer"))
9267 (t (setq link nil)))
9269 (if (consp link) (setq cpltxt (car link) link (cdr link)))
9270 (setq link (or link cpltxt)
9271 desc (or desc cpltxt))
9272 (cond ((equal desc "NONE") (setq desc nil))
9273 ((string-match org-bracket-link-regexp desc)
9274 (setq desc (replace-regexp-in-string
9275 org-bracket-link-regexp
9276 (concat "\\3" (if (equal (length (match-string 0 desc))
9277 (length desc)) "*" "")) desc))))
9279 (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
9281 (setq org-stored-links
9282 (cons (list link desc) org-stored-links))
9283 (message "Stored: %s" (or desc link))
9285 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
9287 (setq org-stored-links
9288 (cons (list link desc) org-stored-links))))
9289 (or agenda-link (and link (org-make-link-string link desc)))))))
9291 (defun org-store-link-props (&rest plist)
9292 "Store link properties, extract names and addresses."
9294 (when (setq x (plist-get plist :from))
9295 (setq adr (mail-extract-address-components x))
9296 (setq plist (plist-put plist :fromname (car adr)))
9297 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
9298 (when (setq x (plist-get plist :to))
9299 (setq adr (mail-extract-address-components x))
9300 (setq plist (plist-put plist :toname (car adr)))
9301 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
9302 (let ((from (plist-get plist :from))
9303 (to (plist-get plist :to)))
9304 (when (and from to org-from-is-user-regexp)
9306 (plist-put plist :fromto
9307 (if (string-match org-from-is-user-regexp from)
9309 (concat "from %f"))))))
9310 (setq org-store-link-plist plist))
9312 (defun org-add-link-props (&rest plist)
9313 "Add these properties to the link property list."
9316 (setq key (pop plist) value (pop plist))
9317 (setq org-store-link-plist
9318 (plist-put org-store-link-plist key value)))))
9320 (defun org-email-link-description (&optional fmt)
9321 "Return the description part of an email link.
9322 This takes information from `org-store-link-plist' and formats it
9323 according to FMT (default from `org-email-link-description-format')."
9324 (setq fmt (or fmt org-email-link-description-format))
9325 (let* ((p org-store-link-plist)
9326 (to (plist-get p :toaddress))
9327 (from (plist-get p :fromaddress))
9330 (cons "%c" (plist-get p :fromto))
9331 (cons "%F" (plist-get p :from))
9332 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
9333 (cons "%T" (plist-get p :to))
9334 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
9335 (cons "%s" (plist-get p :subject))
9336 (cons "%d" (plist-get p :date))
9337 (cons "%m" (plist-get p :message-id)))))
9338 (when (string-match "%c" fmt)
9339 ;; Check if the user wrote this message
9340 (if (and org-from-is-user-regexp from to
9341 (save-match-data (string-match org-from-is-user-regexp from)))
9342 (setq fmt (replace-match "to %t" t t fmt))
9343 (setq fmt (replace-match "from %f" t t fmt))))
9344 (org-replace-escapes fmt table)))
9346 (defun org-make-org-heading-search-string (&optional string)
9347 "Make search string for the current headline or STRING."
9349 (and (derived-mode-p 'org-mode)
9351 (org-back-to-heading t)
9352 (org-element-property :raw-value (org-element-at-point))))))
9353 (lines org-context-in-file-links))
9354 (or string (setq s (concat "*" s))) ; Add * for headlines
9355 (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
9356 (when (and string (integerp lines) (> lines 0))
9357 (let ((slines (org-split-string s "\n")))
9358 (when (< lines (length slines))
9361 (reverse (nthcdr (- (length slines) lines)
9362 (reverse slines))) "\n")))))
9363 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
9365 (defun org-make-link-string (link &optional description)
9366 "Make a link with brackets, consisting of LINK and DESCRIPTION."
9367 (unless (string-match "\\S-" link)
9368 (error "Empty link"))
9369 (when (and description
9370 (stringp description)
9371 (not (string-match "\\S-" description)))
9372 (setq description nil))
9373 (when (stringp description)
9374 ;; Remove brackets from the description, they are fatal.
9375 (while (string-match "\\[" description)
9376 (setq description (replace-match "{" t t description)))
9377 (while (string-match "\\]" description)
9378 (setq description (replace-match "}" t t description))))
9379 (when (equal link description)
9380 ;; No description needed, it is identical
9381 (setq description nil))
9382 (when (and (not description)
9383 (not (string-match (org-image-file-name-regexp) link))
9384 (not (equal link (org-link-escape link))))
9385 (setq description (org-extract-attributes link)))
9387 (cond ((string-match (org-image-file-name-regexp) link) link)
9388 ((string-match org-link-types-re link)
9389 (concat (match-string 1 link)
9390 (org-link-escape (substring link (match-end 1)))))
9391 (t (org-link-escape link))))
9392 (concat "[[" link "]"
9393 (if description (concat "[" description "]") "")
9396 (defconst org-link-escape-chars
9397 '(?\ ?\[ ?\] ?\; ?\= ?\+)
9398 "List of characters that should be escaped in link.
9399 This is the list that is used for internal purposes.")
9401 (defconst org-link-escape-chars-browser
9403 "List of escapes for characters that are problematic in links.
9404 This is the list that is used before handing over to the browser.")
9406 (defun org-link-escape (text &optional table merge)
9407 "Return percent escaped representation of TEXT.
9408 TEXT is a string with the text to escape.
9409 Optional argument TABLE is a list with characters that should be
9410 escaped. When nil, `org-link-escape-chars' is used.
9411 If optional argument MERGE is set, merge TABLE into
9412 `org-link-escape-chars'."
9415 (mapc (lambda (defchr)
9416 (unless (member defchr table)
9417 (setq table (cons defchr table)))) org-link-escape-chars))
9419 (setq table org-link-escape-chars)))
9422 (if (or (member char table)
9423 (and (or (< char 32) (= char 37) (> char 126))
9425 (mapconcat (lambda (sequence-element)
9426 (format "%%%.2X" sequence-element))
9427 (or (encode-coding-char char 'utf-8)
9428 (error "Unable to percent escape character: %s"
9429 (char-to-string char))) "")
9430 (char-to-string char))) text ""))
9432 (defun org-link-unescape (str)
9433 "Unhex hexified Unicode strings as returned from the JavaScript function
9434 encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
9435 (unless (and (null str) (string= "" str))
9436 (let ((pos 0) (case-fold-search t) unhexed)
9437 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
9438 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
9439 (setq str (replace-match unhexed t t str))
9440 (setq pos (+ pos (length unhexed))))))
9443 (defun org-link-unescape-compound (hex)
9444 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
9445 Note: this function also decodes single byte encodings like
9446 `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
9448 (let* ((bytes (cdr (split-string hex "%")))
9453 (let* ((val (string-to-number (pop bytes) 16))
9457 ((>= val 252) (cons 6 252))
9458 ((>= val 248) (cons 5 248))
9459 ((>= val 240) (cons 4 240))
9460 ((>= val 224) (cons 3 224))
9461 ((>= val 192) (cons 2 192))
9464 (if (>= val 192) (setq eat (car shift-xor)))
9465 (setq val (logxor val (cdr shift-xor)))
9466 (setq sum (+ (lsh sum (car shift-xor)) val))
9467 (if (> eat 0) (setq eat (- eat 1)))
9469 ((= 0 eat) ;multi byte
9470 (setq ret (concat ret (org-char-to-string sum)))
9472 ((not bytes) ; single byte(s)
9473 (setq ret (org-link-unescape-single-byte-sequence hex))))
9474 )) ;; end (while bytes
9477 (defun org-link-unescape-single-byte-sequence (hex)
9478 "Unhexify hex-encoded single byte character sequences."
9479 (mapconcat (lambda (byte)
9480 (char-to-string (string-to-number byte 16)))
9481 (cdr (split-string hex "%")) ""))
9483 (defun org-xor (a b)
9487 (defun org-fixup-message-id-for-http (s)
9488 "Replace special characters in a message id, so it can be used in an http query."
9489 (when (string-match "%" s)
9490 (setq s (mapconcat (lambda (c)
9493 (char-to-string c)))
9495 (while (string-match "<" s)
9496 (setq s (replace-match "%3C" t t s)))
9497 (while (string-match ">" s)
9498 (setq s (replace-match "%3E" t t s)))
9499 (while (string-match "@" s)
9500 (setq s (replace-match "%40" t t s)))
9503 (defun org-link-prettify (link)
9504 "Return a human-readable representation of LINK.
9505 The car of LINK must be a raw link the cdr of LINK must be either
9506 a link description or nil."
9507 (let ((desc (or (cadr link) "<no description>")))
9508 (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
9509 "<" (car link) ">")))
9512 (defun org-insert-link-global ()
9513 "Insert a link like Org-mode does.
9514 This command can be called in any mode to insert a link in Org-mode syntax."
9516 (org-load-modules-maybe)
9517 (org-run-like-in-org-mode 'org-insert-link))
9519 (defun org-insert-all-links (&optional keep)
9520 "Insert all links in `org-stored-links'."
9522 (let ((links (copy-sequence org-stored-links)) l)
9523 (while (setq l (if keep (pop links) (pop org-stored-links)))
9525 (org-insert-link nil (car l) (cadr l))
9528 (defun org-link-fontify-links-to-this-file ()
9529 "Fontify links to the current file in `org-stored-links'."
9530 (let ((f (buffer-file-name)) a b)
9531 (setq a (mapcar (lambda(l)
9533 (when (and (string-match "^file:\\(.+\\)::" ll)
9534 (equal f (expand-file-name (match-string 1 ll))))
9537 (when (featurep 'org-id)
9538 (setq b (mapcar (lambda(l)
9540 (when (and (string-match "^id:\\(.+\\)$" ll)
9541 (equal f (expand-file-name
9542 (or (org-id-find-id-file
9543 (match-string 1 ll)) ""))))
9547 (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
9548 (delq nil (append a b)))))
9550 (defvar org-link-links-in-this-file nil)
9551 (defun org-insert-link (&optional complete-file link-location default-description)
9552 "Insert a link. At the prompt, enter the link.
9554 Completion can be used to insert any of the link protocol prefixes like
9557 The history can be used to select a link previously stored with
9558 `org-store-link'. When the empty string is entered (i.e. if you just
9559 press RET at the prompt), the link defaults to the most recently
9560 stored link. As SPC triggers completion in the minibuffer, you need to
9561 use M-SPC or C-q SPC to force the insertion of a space character.
9563 You will also be prompted for a description, and if one is given, it will
9564 be displayed in the buffer instead of the link.
9566 If there is already a link at point, this command will allow you to edit link
9567 and description parts.
9569 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9570 be selected using completion. The path to the file will be relative to the
9571 current directory if the file is in the current directory or a subdirectory.
9572 Otherwise, the link will be the absolute path as completed in the minibuffer
9573 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9574 option `org-link-file-path-type'.
9576 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9577 the current directory or below.
9579 With three \\[universal-argument] prefixes, negate the meaning of
9580 `org-keep-stored-link-after-insertion'.
9582 If `org-make-link-description-function' is non-nil, this function will be
9583 called with the link target, and the result will be the default
9586 If the LINK-LOCATION parameter is non-nil, this value will be
9587 used as the link location instead of reading one interactively.
9589 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9590 be used as the default description."
9592 (let* ((wcf (current-window-configuration))
9593 (origbuf (current-buffer))
9594 (region (if (org-region-active-p)
9595 (buffer-substring (region-beginning) (region-end))))
9596 (remove (and region (list (region-beginning) (region-end))))
9598 tmphist ; byte-compile incorrectly complains about this
9599 (link link-location)
9600 (abbrevs org-link-abbrev-alist-local)
9601 entry file all-prefixes auto-desc)
9603 (link-location) ; specified by arg, just use it.
9604 ((org-in-regexp org-bracket-link-regexp 1)
9605 ;; We do have a link at point, and we are going to edit it.
9606 (setq remove (list (match-beginning 0) (match-end 0)))
9607 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
9608 (setq link (read-string "Link: "
9610 (org-match-string-no-properties 1)))))
9611 ((or (org-in-regexp org-angle-link-re)
9612 (org-in-regexp org-plain-link-re))
9613 ;; Convert to bracket link
9614 (setq remove (list (match-beginning 0) (match-end 0))
9615 link (read-string "Link: "
9616 (org-remove-angle-brackets (match-string 0)))))
9617 ((member complete-file '((4) (16)))
9618 ;; Completing read for file names.
9619 (setq link (org-file-complete-link complete-file)))
9621 ;; Read link, with completion for stored links.
9622 (org-link-fontify-links-to-this-file)
9623 (org-switch-to-buffer-other-window "*Org Links*")
9624 (with-current-buffer "*Org Links*"
9626 (insert "Insert a link.
9627 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9628 (when org-stored-links
9629 (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9630 (insert (mapconcat 'org-link-prettify
9631 (reverse org-stored-links) "\n")))
9632 (goto-char (point-min)))
9633 (let ((cw (selected-window)))
9634 (select-window (get-buffer-window "*Org Links*" 'visible))
9635 (with-current-buffer "*Org Links*" (setq truncate-lines t))
9636 (unless (pos-visible-in-window-p (point-max))
9637 (org-fit-window-to-buffer))
9638 (and (window-live-p cw) (select-window cw)))
9639 ;; Fake a link history, containing the stored links.
9640 (setq tmphist (append (mapcar 'car org-stored-links)
9641 org-insert-link-history))
9642 (setq all-prefixes (append (mapcar 'car abbrevs)
9643 (mapcar 'car org-link-abbrev-alist)
9648 (org-completing-read
9651 (mapcar (lambda (x) (concat x ":"))
9653 (mapcar 'car org-stored-links))
9656 (caar org-stored-links)))
9657 (if (not (string-match "\\S-" link))
9658 (error "No link selected"))
9660 (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
9662 (if (or (member link all-prefixes)
9663 (and (equal ":" (substring link -1))
9664 (member (substring link 0 -1) all-prefixes)
9665 (setq link (substring link 0 -1))))
9666 (setq link (with-current-buffer origbuf
9667 (org-link-try-special-completion link)))))
9668 (set-window-configuration wcf)
9669 (kill-buffer "*Org Links*"))
9670 (setq entry (assoc link org-stored-links))
9671 (or entry (push link org-insert-link-history))
9672 (setq desc (or desc (nth 1 entry)))))
9674 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
9675 (not org-keep-stored-link-after-insertion))
9676 (setq org-stored-links (delq (assoc link org-stored-links)
9679 (if (string-match org-plain-link-re link)
9680 ;; URL-like link, normalize the use of angular brackets.
9681 (setq link (org-remove-angle-brackets link)))
9683 ;; Check if we are linking to the current file with a search
9684 ;; option If yes, simplify the link by using only the search
9686 (when (and buffer-file-name
9687 (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
9688 (let* ((path (match-string 1 link))
9689 (case-fold-search nil)
9690 (search (match-string 2 link)))
9692 (if (equal (file-truename buffer-file-name) (file-truename path))
9693 ;; We are linking to this same file, with a search option
9694 (setq link search)))))
9696 ;; Check if we can/should use a relative path. If yes, simplify the link
9697 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
9698 (let* ((type (match-string 1 link))
9699 (path (match-string 2 link))
9701 (case-fold-search nil))
9703 ((or (eq org-link-file-path-type 'absolute)
9704 (equal complete-file '(16)))
9705 (setq path (abbreviate-file-name (expand-file-name path))))
9706 ((eq org-link-file-path-type 'noabbrev)
9707 (setq path (expand-file-name path)))
9708 ((eq org-link-file-path-type 'relative)
9709 (setq path (file-relative-name path)))
9712 (if (string-match (concat "^" (regexp-quote
9714 (file-name-as-directory
9715 default-directory))))
9716 (expand-file-name path))
9717 ;; We are linking a file with relative path name.
9718 (setq path (substring (expand-file-name path)
9720 (setq path (abbreviate-file-name (expand-file-name path)))))))
9721 (setq link (concat type path))
9722 (if (equal desc origpath)
9725 (if org-make-link-description-function
9727 (or (condition-case nil
9728 (funcall org-make-link-description-function link desc)
9729 (error (progn (message "Can't get link description from `%s'"
9730 (symbol-name org-make-link-description-function))
9732 (read-string "Description: " default-description)))
9733 (if default-description (setq desc default-description)
9734 (setq desc (or (and auto-desc desc)
9735 (read-string "Description: " desc)))))
9737 (unless (string-match "\\S-" desc) (setq desc nil))
9738 (if remove (apply 'delete-region remove))
9739 (insert (org-make-link-string link desc))))
9741 (defun org-link-try-special-completion (type)
9742 "If there is completion support for link type TYPE, offer it."
9743 (let ((fun (intern (concat "org-" type "-complete-link"))))
9746 (read-string "Link (no completion support): " (concat type ":")))))
9748 (defun org-file-complete-link (&optional arg)
9749 "Create a file link using completion."
9751 (setq file (org-iread-file-name "File: "))
9752 (let ((pwd (file-name-as-directory (expand-file-name ".")))
9753 (pwd1 (file-name-as-directory (abbreviate-file-name
9754 (expand-file-name ".")))))
9759 (abbreviate-file-name (expand-file-name file)))))
9760 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
9761 (setq link (concat "file:" (match-string 1 file))))
9762 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
9763 (expand-file-name file))
9765 "file:" (match-string 1 (expand-file-name file)))))
9766 (t (setq link (concat "file:" file)))))
9769 (defun org-iread-file-name (&rest args)
9770 "Read-file-name using `ido-mode' speedup if available.
9771 ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
9772 See `read-file-name' for a description of parameters."
9773 (org-without-partial-completion
9774 (if (and org-completion-use-ido
9775 (fboundp 'ido-read-file-name)
9776 (boundp 'ido-mode) ido-mode
9777 (listp (second args)))
9778 (let ((ido-enter-matching-directory nil))
9779 (apply 'ido-read-file-name args))
9780 (apply 'read-file-name args))))
9782 (defun org-completing-read (&rest args)
9783 "Completing-read with SPACE being a normal character."
9784 (let ((enable-recursive-minibuffers t)
9785 (minibuffer-local-completion-map
9786 (copy-keymap minibuffer-local-completion-map)))
9787 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
9788 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
9789 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
9790 (apply 'org-icompleting-read args)))
9792 (defun org-completing-read-no-i (&rest args)
9793 (let (org-completion-use-ido org-completion-use-iswitchb)
9794 (apply 'org-completing-read args)))
9796 (defun org-iswitchb-completing-read (prompt choices &rest args)
9797 "Use iswitch as a completing-read replacement to choose from choices.
9798 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
9800 (let* ((iswitchb-use-virtual-buffers nil)
9801 (iswitchb-make-buflist-hook
9803 (setq iswitchb-temp-buflist choices))))
9804 (iswitchb-read-buffer prompt)))
9806 (defun org-icompleting-read (&rest args)
9807 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
9808 (org-without-partial-completion
9809 (if (and org-completion-use-ido
9810 (fboundp 'ido-completing-read)
9811 (boundp 'ido-mode) ido-mode
9812 (listp (second args)))
9813 (let ((ido-enter-matching-directory nil))
9814 (apply 'ido-completing-read (concat (car args))
9815 (if (consp (car (nth 1 args)))
9816 (mapcar 'car (nth 1 args))
9819 (if (and org-completion-use-iswitchb
9820 (boundp 'iswitchb-mode) iswitchb-mode
9821 (listp (second args)))
9822 (apply 'org-iswitchb-completing-read (concat (car args))
9823 (if (consp (car (nth 1 args)))
9824 (mapcar 'car (nth 1 args))
9827 (apply 'completing-read args)))))
9829 (defun org-extract-attributes (s)
9830 "Extract the attributes cookie from a string and set as text property."
9831 (let (a attr (start 0) key value)
9833 (when (string-match "{{\\([^}]+\\)}}$" s)
9834 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
9835 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
9836 (setq key (match-string 1 a) value (match-string 2 a)
9838 attr (plist-put attr (intern key) value))))
9839 (org-add-props s nil 'org-attr attr))
9842 (defun org-extract-attributes-from-string (tag)
9843 (let (key value attr)
9844 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
9845 (setq key (match-string 1 tag) value (match-string 2 tag)
9846 tag (replace-match "" t t tag)
9847 attr (plist-put attr (intern key) value)))
9850 (defun org-attributes-to-string (plist)
9851 "Format a property list into an HTML attribute list."
9852 (let ((s "") key value)
9854 (setq key (pop plist) value (pop plist))
9856 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
9859 ;;; Opening/following a link
9861 (defvar org-link-search-failed nil)
9863 (defvar org-open-link-functions nil
9864 "Hook for functions finding a plain text link.
9865 These functions must take a single argument, the link content.
9866 They will be called for links that look like [[link text][description]]
9867 when LINK TEXT does not have a protocol like \"http:\" and does not look
9868 like a filename (e.g. \"./blue.png\").
9870 These functions will be called *before* Org attempts to resolve the
9871 link by doing text searches in the current buffer - so if you want a
9872 link \"[[target]]\" to still find \"<<target>>\", your function should
9873 handle this as a special case.
9875 When the function does handle the link, it must return a non-nil value.
9876 If it decides that it is not responsible for this link, it must return
9877 nil to indicate that that Org-mode can continue with other options
9878 like exact and fuzzy text search.")
9880 (defun org-next-link (&optional search-backward)
9881 "Move forward to the next link.
9882 If the link is in hidden text, expose it."
9884 (when (and org-link-search-failed (eq this-command last-command))
9885 (goto-char (point-min))
9886 (message "Link search wrapped back to beginning of buffer"))
9887 (setq org-link-search-failed nil)
9888 (let* ((pos (point))
9890 (a (assoc :link ct))
9891 (srch-fun (if search-backward 're-search-backward 're-search-forward)))
9892 (cond (a (goto-char (nth (if search-backward 1 2) a)))
9893 ((looking-at org-any-link-re)
9894 ;; Don't stay stuck at link without an org-link face
9895 (forward-char (if search-backward -1 1))))
9896 (if (funcall srch-fun org-any-link-re nil t)
9898 (goto-char (match-beginning 0))
9899 (if (outline-invisible-p) (org-show-context)))
9901 (setq org-link-search-failed t)
9902 (message "No further link found"))))
9904 (defun org-previous-link ()
9905 "Move backward to the previous link.
9906 If the link is in hidden text, expose it."
9908 (funcall 'org-next-link t))
9910 (defun org-translate-link (s)
9911 "Translate a link string if a translation function has been defined."
9912 (if (and org-link-translation-function
9913 (fboundp org-link-translation-function)
9914 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
9916 (setq s (funcall org-link-translation-function
9917 (match-string 1 s) (match-string 2 s)))
9918 (concat (car s) ":" (cdr s)))
9921 (defun org-translate-link-from-planner (type path)
9922 "Translate a link from Emacs Planner syntax so that Org can follow it.
9923 This is still an experimental function, your mileage may vary."
9925 ((member type '("http" "https" "news" "ftp"))
9926 ;; standard Internet links are the same.
9928 ((and (equal type "irc") (string-match "^//" path))
9929 ;; Planner has two / at the beginning of an irc link, we have 1.
9930 ;; We should have zero, actually....
9931 (setq path (substring path 1)))
9932 ((and (equal type "lisp") (string-match "^/" path))
9933 ;; Planner has a slash, we do not.
9934 (setq type "elisp" path (substring path 1)))
9935 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
9936 ;; A typical message link. Planner has the id after the final slash,
9937 ;; we separate it with a hash mark
9938 (setq path (concat (match-string 1 path) "#"
9939 (org-remove-angle-brackets (match-string 2 path)))))
9943 (defun org-find-file-at-mouse (ev)
9944 "Open file link or URL at mouse."
9946 (mouse-set-point ev)
9947 (org-open-at-point 'in-emacs))
9949 (defun org-open-at-mouse (ev)
9950 "Open file link or URL at mouse.
9951 See the docstring of `org-open-file' for details."
9953 (mouse-set-point ev)
9954 (if (eq major-mode 'org-agenda-mode)
9955 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
9956 (org-open-at-point))
9958 (defvar org-window-config-before-follow-link nil
9959 "The window configuration before following a link.
9960 This is saved in case the need arises to restore it.")
9962 (defvar org-open-link-marker (make-marker)
9963 "Marker pointing to the location where `org-open-at-point; was called.")
9966 (defun org-open-at-point-global ()
9967 "Follow a link like Org-mode does.
9968 This command can be called in any mode to follow a link that has
9971 (org-run-like-in-org-mode 'org-open-at-point))
9974 (defun org-open-link-from-string (s &optional arg reference-buffer)
9975 "Open a link in the string S, as if it was in Org-mode."
9976 (interactive "sLink: \nP")
9977 (let ((reference-buffer (or reference-buffer (current-buffer))))
9979 (let ((org-inhibit-startup (not reference-buffer)))
9982 (goto-char (point-min))
9983 (when reference-buffer
9984 (setq org-link-abbrev-alist-local
9985 (with-current-buffer reference-buffer
9986 org-link-abbrev-alist-local)))
9987 (org-open-at-point arg reference-buffer)))))
9989 (defvar org-open-at-point-functions nil
9990 "Hook that is run when following a link at point.
9992 Functions in this hook must return t if they identify and follow
9993 a link at point. If they don't find anything interesting at point,
9994 they must return nil.")
9996 (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
9997 (defun org-open-at-point (&optional arg reference-buffer)
9998 "Open link at or after point.
9999 If there is no link at point, this function will search forward up to
10000 the end of the current line.
10001 Normally, files will be opened by an appropriate application. If the
10002 optional prefix argument ARG is non-nil, Emacs will visit the file.
10003 With a double prefix argument, try to open outside of Emacs, in the
10004 application the system uses for this file type."
10006 ;; if in a code block, then open the block's results
10007 (unless (call-interactively #'org-babel-open-src-block-result)
10008 (org-load-modules-maybe)
10009 (move-marker org-open-link-marker (point))
10010 (setq org-window-config-before-follow-link (current-window-configuration))
10011 (org-remove-occur-highlights nil nil t)
10013 ((and (org-at-heading-p)
10014 (not (org-at-timestamp-p t))
10015 (not (org-in-regexp
10016 (concat org-plain-link-re "\\|"
10017 org-bracket-link-regexp "\\|"
10018 org-angle-link-re "\\|"
10019 "[ \t]:[^ \t\n]+:[ \t]*$")))
10020 (not (get-text-property (point) 'org-linked-text)))
10021 (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
10023 (lk (if (stringp lk0) (list lk0) lk0))
10024 (lkend (cdr lkall)))
10026 (search-forward l nil lkend)
10027 (goto-char (match-beginning 0))
10028 (org-open-at-point))
10030 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
10031 ((run-hook-with-args-until-success 'org-open-at-point-functions))
10032 ((and (org-at-timestamp-p t)
10033 (not (org-in-regexp org-bracket-link-regexp)))
10034 (org-follow-timestamp-link))
10035 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
10036 (not (org-in-regexp org-any-link-re)))
10037 (org-footnote-action))
10039 (let (type path link line search (pos (point)))
10042 (skip-chars-forward "^]\n\r")
10043 (when (org-in-regexp org-bracket-link-regexp 1)
10044 (setq link (org-extract-attributes
10045 (org-link-unescape (org-match-string-no-properties 1))))
10046 (while (string-match " *\n *" link)
10047 (setq link (replace-match " " t t link)))
10048 (setq link (org-link-expand-abbrev link))
10050 ((or (file-name-absolute-p link)
10051 (string-match "^\\.\\.?/" link))
10052 (setq type "file" path link))
10053 ((string-match org-link-re-with-space3 link)
10054 (setq type (match-string 1 link) path (match-string 2 link)))
10055 ((string-match "^help:+\\(.+\\)" link)
10056 (setq type "help" path (match-string 1 link)))
10057 (t (setq type "thisfile" path link)))
10060 (when (get-text-property (point) 'org-linked-text)
10061 (setq type "thisfile"
10062 pos (if (get-text-property (1+ (point)) 'org-linked-text)
10063 (1+ (point)) (point))
10064 path (buffer-substring
10065 (or (previous-single-property-change pos 'org-linked-text)
10067 (or (next-single-property-change pos 'org-linked-text)
10072 (when (or (org-in-regexp org-angle-link-re)
10073 (let ((match (org-in-regexp org-plain-link-re)))
10074 ;; Check a plain link is not within a bracket link
10078 (goto-char (car match))
10079 (not (org-in-regexp org-bracket-link-regexp))))))
10080 (let ((line_ending (save-excursion (end-of-line) (point))))
10081 ;; We are in a line before a plain or bracket link
10082 (or (re-search-forward org-plain-link-re line_ending t)
10083 (re-search-forward org-bracket-link-regexp line_ending t))))
10084 (setq type (match-string 1)
10085 path (org-link-unescape (match-string 2)))
10088 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
10090 path (match-string 1))
10091 (while (string-match ":" path)
10092 (setq path (replace-match "+" t t path)))
10094 (when (org-in-regexp "<\\([^><\n]+\\)>")
10095 (setq type "tree-match"
10096 path (match-string 1))
10099 (user-error "No link found"))
10101 ;; switch back to reference buffer
10102 ;; needed when if called in a temporary buffer through
10103 ;; org-open-link-from-string
10104 (with-current-buffer (or reference-buffer (current-buffer))
10106 ;; Remove any trailing spaces in path
10107 (if (string-match " +\\'" path)
10108 (setq path (replace-match "" t t path)))
10109 (if (and org-link-translation-function
10110 (fboundp org-link-translation-function))
10111 ;; Check if we need to translate the link
10112 (let ((tmp (funcall org-link-translation-function type path)))
10113 (setq type (car tmp) path (cdr tmp))))
10117 ((assoc type org-link-protocols)
10118 (funcall (nth 1 (assoc type org-link-protocols)) path))
10120 ((equal type "help")
10121 (let ((f-or-v (intern path)))
10122 (cond ((fboundp f-or-v)
10123 (describe-function f-or-v))
10125 (describe-variable f-or-v))
10126 (t (error "Not a known function or variable")))))
10128 ((equal type "mailto")
10129 (let ((cmd (car org-link-mailto-program))
10130 (args (cdr org-link-mailto-program)) args1
10131 (address path) (subject "") a)
10132 (if (string-match "\\(.*\\)::\\(.*\\)" path)
10133 (setq address (match-string 1 path)
10134 subject (org-link-escape (match-string 2 path))))
10137 ((not (stringp (car args))) (push (pop args) args1))
10138 (t (setq a (pop args))
10139 (if (string-match "%a" a)
10140 (setq a (replace-match address t t a)))
10141 (if (string-match "%s" a)
10142 (setq a (replace-match subject t t a)))
10144 (apply cmd (nreverse args1))))
10146 ((member type '("http" "https" "ftp" "news"))
10147 (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
10149 path org-link-escape-chars-browser)
10152 ((string= type "doi")
10153 (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
10155 path org-link-escape-chars-browser)
10158 ((member type '("message"))
10159 (browse-url (concat type ":" path)))
10161 ((string= type "tags")
10162 (org-tags-view arg path))
10164 ((string= type "tree-match")
10165 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
10167 ((string= type "file")
10168 (if (string-match "::\\([0-9]+\\)\\'" path)
10169 (setq line (string-to-number (match-string 1 path))
10170 path (substring path 0 (match-beginning 0)))
10171 (if (string-match "::\\(.+\\)\\'" path)
10172 (setq search (match-string 1 path)
10173 path (substring path 0 (match-beginning 0)))))
10174 (if (string-match "[*?{]" (file-name-nondirectory path))
10176 (org-open-file path arg line search)))
10178 ((string= type "shell")
10179 (let ((buf (generate-new-buffer "*Org Shell Output"))
10181 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
10182 (string-match org-confirm-shell-link-not-regexp cmd))
10183 (not org-confirm-shell-link-function)
10184 (funcall org-confirm-shell-link-function
10185 (format "Execute \"%s\" in shell? "
10186 (org-add-props cmd nil
10187 'face 'org-warning))))
10189 (message "Executing %s" cmd)
10190 (shell-command cmd buf)
10191 (if (featurep 'midnight)
10192 (setq clean-buffer-list-kill-buffer-names
10193 (cons buf clean-buffer-list-kill-buffer-names))))
10196 ((string= type "elisp")
10198 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
10199 (string-match org-confirm-elisp-link-not-regexp cmd))
10200 (not org-confirm-elisp-link-function)
10201 (funcall org-confirm-elisp-link-function
10202 (format "Execute \"%s\" as elisp? "
10203 (org-add-props cmd nil
10204 'face 'org-warning))))
10205 (message "%s => %s" cmd
10206 (if (equal (string-to-char cmd) ?\()
10208 (call-interactively (read cmd))))
10211 ((and (string= type "thisfile")
10212 (run-hook-with-args-until-success
10213 'org-open-link-functions path)))
10215 ((string= type "thisfile")
10217 (switch-to-buffer-other-window
10218 (org-get-buffer-for-internal-link (current-buffer)))
10219 (org-mark-ring-push))
10220 (let ((cmd `(org-link-search
10222 ,(cond ((equal arg '(4)) ''occur)
10223 ((equal arg '(16)) ''org-occur))
10225 (condition-case nil (let ((org-link-search-inhibit-query t))
10227 (error (progn (widen) (eval cmd))))))
10229 (t (browse-url-at-point)))))))
10230 (move-marker org-open-link-marker nil)
10231 (run-hook-with-args 'org-follow-link-hook)))
10233 (defun org-offer-links-in-entry (buffer marker &optional nth zero)
10234 "Offer links in the current entry and return the selected link.
10235 If there is only one link, return it.
10236 If NTH is an integer, return the NTH link found.
10237 If ZERO is a string, check also this string for a link, and if
10238 there is one, return it."
10239 (with-current-buffer buffer
10244 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
10245 "\\(" org-angle-link-re "\\)\\|"
10246 "\\(" org-plain-link-re "\\)"))
10248 (in-emacs (if (integerp nth) nil nth))
10249 have-zero end links link c)
10250 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
10251 (push (match-string 0 zero) links)
10252 (setq cnt (1- cnt) have-zero t))
10254 (org-back-to-heading t)
10255 (setq end (save-excursion (outline-next-heading) (point)))
10256 (while (re-search-forward re end t)
10257 (push (match-string 0) links))
10258 (setq links (org-uniquify (reverse links))))
10261 (message "No links"))
10262 ((equal (length links) 1)
10263 (setq link (car links)))
10264 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
10265 (setq link (nth (if have-zero nth (1- nth)) links)))
10266 (t ; we have to select a link
10268 (save-window-excursion
10269 (delete-other-windows)
10270 (with-output-to-temp-buffer "*Select Link*"
10272 (if (not (string-match org-bracket-link-regexp l))
10273 (princ (format "[%c] %s\n" (incf cnt)
10274 (org-remove-angle-brackets l)))
10276 (princ (format "[%c] %s (%s)\n" (incf cnt)
10277 (match-string 3 l) (match-string 1 l)))
10278 (princ (format "[%c] %s\n" (incf cnt)
10279 (match-string 1 l))))))
10281 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
10282 (message "Select link to open, RET to open all:")
10283 (setq c (read-char-exclusive))
10284 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
10285 (when (equal c ?q) (error "Abort"))
10286 (if (equal c ?\C-m)
10288 (setq nth (- c ?0))
10289 (if have-zero (setq nth (1+ nth)))
10290 (unless (and (integerp nth) (>= (length links) nth))
10291 (error "Invalid link selection"))
10292 (setq link (nth (1- nth) links)))))
10293 (cons link end))))))
10295 ;; Add special file links that specify the way of opening
10297 (org-add-link-type "file+sys" 'org-open-file-with-system)
10298 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
10299 (defun org-open-file-with-system (path)
10300 "Open file at PATH using the system way of opening it."
10301 (org-open-file path 'system))
10302 (defun org-open-file-with-emacs (path)
10303 "Open file at PATH in Emacs."
10304 (org-open-file path 'emacs))
10309 (defvar org-create-file-search-functions nil
10310 "List of functions to construct the right search string for a file link.
10311 These functions are called in turn with point at the location to
10312 which the link should point.
10314 A function in the hook should first test if it would like to
10315 handle this file type, for example by checking the `major-mode'
10316 or the file extension. If it decides not to handle this file, it
10317 should just return nil to give other functions a chance. If it
10318 does handle the file, it must return the search string to be used
10319 when following the link. The search string will be part of the
10320 file link, given after a double colon, and `org-open-at-point'
10321 will automatically search for it. If special measures must be
10322 taken to make the search successful, another function should be
10323 added to the companion hook `org-execute-file-search-functions',
10326 A function in this hook may also use `setq' to set the variable
10327 `description' to provide a suggestion for the descriptive text to
10328 be used for this link when it gets inserted into an Org-mode
10329 buffer with \\[org-insert-link].")
10331 (defvar org-execute-file-search-functions nil
10332 "List of functions to execute a file search triggered by a link.
10334 Functions added to this hook must accept a single argument, the
10335 search string that was part of the file link, the part after the
10336 double colon. The function must first check if it would like to
10337 handle this search, for example by checking the `major-mode' or
10338 the file extension. If it decides not to handle this search, it
10339 should just return nil to give other functions a chance. If it
10340 does handle the search, it must return a non-nil value to keep
10341 other functions from trying.
10343 Each function can access the current prefix argument through the
10344 variable `current-prefix-argument'. Note that a single prefix is
10345 used to force opening a link in Emacs, so it may be good to only
10346 use a numeric or double prefix to guide the search function.
10348 In case this is needed, a function in this hook can also restore
10349 the window configuration before `org-open-at-point' was called using:
10351 (set-window-configuration org-window-config-before-follow-link)")
10353 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
10354 (defun org-link-search (s &optional type avoid-pos stealth)
10355 "Search for a link search option.
10356 If S is surrounded by forward slashes, it is interpreted as a
10357 regular expression. In org-mode files, this will create an `org-occur'
10358 sparse tree. In ordinary files, `occur' will be used to list matches.
10359 If the current buffer is in `dired-mode', grep will be used to search
10360 in all files. If AVOID-POS is given, ignore matches near that position.
10362 When optional argument STEALTH is non-nil, do not modify
10363 visibility around point, thus ignoring
10364 `org-show-hierarchy-above', `org-show-following-heading' and
10365 `org-show-siblings' variables."
10366 (let ((case-fold-search t)
10367 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
10368 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
10369 (append '(("") (" ") ("\t") ("\n"))
10370 org-emphasis-alist)
10373 (pre nil) (post nil)
10374 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
10376 ;; First check if there are any special search functions
10377 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
10378 ;; Now try the builtin stuff
10379 ((and (equal (string-to-char s0) ?#)
10382 (goto-char (point-min))
10385 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
10386 (setq type 'dedicated
10387 pos (match-beginning 0))))
10388 ;; There is an exact target for this
10390 (org-back-to-heading t)))
10392 (goto-char (point-min))
10395 (concat "<<" (regexp-quote s0) ">>") nil t)
10396 (setq type 'dedicated
10397 pos (match-beginning 0))))
10398 ;; There is an exact target for this
10401 (goto-char (point-min))
10404 (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
10405 (setq type 'dedicated pos (match-beginning 0))))
10406 ;; Found an invisible target.
10409 (goto-char (point-min))
10412 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
10413 (setq type 'dedicated pos (match-beginning 0))))
10414 ;; Found an element with a matching #+name affiliated keyword.
10416 ((and (string-match "^(\\(.*\\))$" s0)
10418 (goto-char (point-min))
10421 (concat "[^[]" (regexp-quote
10422 (format org-coderef-label-format
10423 (match-string 1 s0))))
10425 (setq type 'dedicated
10426 pos (1+ (match-beginning 0))))))
10427 ;; There is a coderef target for this
10429 ((string-match "^/\\(.*\\)/$" s)
10430 ;; A regular expression
10432 ((derived-mode-p 'org-mode)
10433 (org-occur (match-string 1 s)))
10434 (t (org-do-occur (match-string 1 s)))))
10435 ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
10436 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
10437 (goto-char (point-min))
10439 ((let (case-fold-search)
10440 (re-search-forward (format org-complex-heading-regexp-format
10443 ;; OK, found a match
10444 (setq type 'dedicated)
10445 (goto-char (match-beginning 0)))
10446 ((and (not org-link-search-inhibit-query)
10447 (eq org-link-search-must-match-exact-headline 'query-to-create)
10448 (y-or-n-p "No match - create this as a new heading? "))
10449 (goto-char (point-max))
10450 (or (bolp) (newline))
10451 (insert "* " s "\n")
10452 (beginning-of-line 0))
10455 (error "No match"))))
10457 ;; A normal search string
10458 (when (equal (string-to-char s) ?*)
10459 ;; Anchor on headlines, post may include tags.
10460 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
10461 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
10462 s (substring s 1)))
10463 (remove-text-properties
10465 '(face nil mouse-face nil keymap nil fontified nil) s)
10466 ;; Make a series of regular expressions to find a match
10467 (setq words (org-split-string s "[ \n\r\t]+")
10469 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
10470 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
10472 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
10473 re2a (concat "[ \t\r\n]" re2a_)
10474 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
10475 re4 (concat "[^a-zA-Z_]" re4_)
10477 re1 (concat pre re2 post)
10478 re3 (concat pre (if pre re4_ re4) post)
10479 re5 (concat pre ".*" re4)
10480 re2 (concat pre re2)
10481 re2a (concat pre (if pre re2a_ re2a))
10482 re4 (concat pre (if pre re4_ re4))
10483 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
10484 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
10488 ((eq type 'org-occur) (org-occur reall))
10489 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
10490 (t (goto-char (point-min))
10492 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
10493 (org-search-not-self 1 re1 nil t)
10494 (org-search-not-self 1 re2 nil t)
10495 (org-search-not-self 1 re2a nil t)
10496 (org-search-not-self 1 re3 nil t)
10497 (org-search-not-self 1 re4 nil t)
10498 (org-search-not-self 1 re5 nil t)
10500 (goto-char (match-beginning 1))
10502 (error "No match"))))))
10503 (and (derived-mode-p 'org-mode)
10505 (org-show-context 'link-search))
10508 (defun org-search-not-self (group &rest args)
10509 "Execute `re-search-forward', but only accept matches that do not
10510 enclose the position of `org-open-link-marker'."
10511 (let ((m org-open-link-marker))
10513 (while (apply 're-search-forward args)
10514 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
10515 (goto-char (match-end group))
10516 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
10517 (> (match-beginning 0) (marker-position m))
10518 (< (match-end 0) (marker-position m)))
10520 (or (not (org-in-regexp
10521 org-bracket-link-analytic-regexp 1))
10522 (not (match-end 4)) ; no description
10523 (and (<= (match-beginning 4) (point))
10524 (>= (match-end 4) (point))))))
10525 (throw 'exit (point))))))))
10527 (defun org-get-buffer-for-internal-link (buffer)
10528 "Return a buffer to be used for displaying the link target of internal links."
10530 ((not org-display-internal-link-with-indirect-buffer)
10532 ((string-match "(Clone)$" (buffer-name buffer))
10533 (message "Buffer is already a clone, not making another one")
10534 ;; we also do not modify visibility in this case
10536 (t ; make a new indirect buffer for displaying the link
10537 (let* ((bn (buffer-name buffer))
10538 (ibn (concat bn "(Clone)"))
10539 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
10540 (with-current-buffer ib (org-overview))
10543 (defun org-do-occur (regexp &optional cleanup)
10544 "Call the Emacs command `occur'.
10545 If CLEANUP is non-nil, remove the printout of the regular expression
10546 in the *Occur* buffer. This is useful if the regex is long and not useful
10550 (let ((cwin (selected-window)) win beg end)
10551 (when (setq win (get-buffer-window "*Occur*"))
10552 (select-window win))
10553 (goto-char (point-min))
10554 (when (re-search-forward "match[a-z]+" nil t)
10555 (setq beg (match-end 0))
10556 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
10557 (setq end (1- (match-beginning 0)))))
10558 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
10559 (goto-char (point-min))
10560 (select-window cwin))))
10562 ;;; The mark ring for links jumps
10564 (defvar org-mark-ring nil
10565 "Mark ring for positions before jumps in Org-mode.")
10566 (defvar org-mark-ring-last-goto nil
10567 "Last position in the mark ring used to go back.")
10568 ;; Fill and close the ring
10569 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
10570 (loop for i from 1 to org-mark-ring-length do
10571 (push (make-marker) org-mark-ring))
10572 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
10575 (defun org-mark-ring-push (&optional pos buffer)
10576 "Put the current position or POS into the mark ring and rotate it."
10578 (setq pos (or pos (point)))
10579 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10580 (move-marker (car org-mark-ring)
10582 (or buffer (current-buffer)))
10584 (substitute-command-keys
10585 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10587 (defun org-mark-ring-goto (&optional n)
10588 "Jump to the previous position in the mark ring.
10589 With prefix arg N, jump back that many stored positions. When
10590 called several times in succession, walk through the entire ring.
10591 Org-mode commands jumping to a different position in the current file,
10592 or to another Org-mode file, automatically push the old position
10596 (if (eq last-command this-command)
10597 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10598 (setq p org-mark-ring))
10599 (setq org-mark-ring-last-goto p)
10601 (org-pop-to-buffer-same-window (marker-buffer m))
10603 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
10605 (defun org-remove-angle-brackets (s)
10606 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
10607 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
10609 (defun org-add-angle-brackets (s)
10610 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
10611 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
10613 (defun org-remove-double-quotes (s)
10614 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
10615 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
10618 ;;; Following specific links
10620 (defun org-follow-timestamp-link ()
10621 "Open an agenda view for the time-stamp date/range at point."
10623 ((org-at-date-range-p t)
10624 (let ((org-agenda-start-on-weekday)
10625 (t1 (match-string 1))
10626 (t2 (match-string 2)) tt1 tt2)
10627 (setq tt1 (time-to-days (org-time-string-to-time t1))
10628 tt2 (time-to-days (org-time-string-to-time t2)))
10629 (let ((org-agenda-buffer-tmp-name
10630 (format "*Org Agenda(a:%s)"
10631 (concat (substring t1 0 10) "--" (substring t2 0 10)))))
10632 (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
10633 ((org-at-timestamp-p t)
10634 (let ((org-agenda-buffer-tmp-name
10635 (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
10636 (org-agenda-list nil (time-to-days (org-time-string-to-time
10637 (substring (match-string 1) 0 10)))
10639 (t (error "This should not happen"))))
10642 ;;; Following file links
10643 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
10644 (declare-function mailcap-extension-to-mime "mailcap" (extn))
10645 (declare-function mailcap-mime-info
10646 "mailcap" (string &optional request no-decode))
10647 (defvar org-wait nil)
10648 (defun org-open-file (path &optional in-emacs line search)
10649 "Open the file at PATH.
10650 First, this expands any special file name abbreviations. Then the
10651 configuration variable `org-file-apps' is checked if it contains an
10652 entry for this file type, and if yes, the corresponding command is launched.
10654 If no application is found, Emacs simply visits the file.
10656 With optional prefix argument IN-EMACS, Emacs will visit the file.
10657 With a double \\[universal-argument] \\[universal-argument] \
10658 prefix arg, Org tries to avoid opening in Emacs
10659 and to use an external application to visit the file.
10661 Optional LINE specifies a line to go to, optional SEARCH a string
10662 to search for. If LINE or SEARCH is given, the file will be
10663 opened in Emacs, unless an entry from org-file-apps that makes
10664 use of groups in a regexp matches.
10666 If you want to change the way frames are used when following a
10667 link, please customize `org-link-frame-setup'.
10669 If the file does not exist, an error is thrown."
10670 (let* ((file (if (equal path "")
10672 (substitute-in-file-name (expand-file-name path))))
10673 (file-apps (append org-file-apps (org-default-apps)))
10674 (apps (org-remove-if
10675 'org-file-apps-entry-match-against-dlink-p file-apps))
10676 (apps-dlink (org-remove-if-not
10677 'org-file-apps-entry-match-against-dlink-p file-apps))
10678 (remp (and (assq 'remote apps) (org-file-remote-p file)))
10679 (dirp (if remp nil (file-directory-p file)))
10680 (file (if (and dirp org-open-directory-means-index-dot-org)
10681 (concat (file-name-as-directory file) "index.org")
10683 (a-m-a-p (assq 'auto-mode apps))
10684 (dfile (downcase file))
10685 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
10686 (link (cond ((and (eq line nil)
10690 (concat file "::" (number-to-string line)))
10692 (concat file "::" search))))
10693 (dlink (downcase link))
10694 (old-buffer (current-buffer))
10696 (old-mode major-mode)
10697 ext cmd link-match-data)
10698 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
10699 (setq ext (match-string 1 dfile))
10700 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
10701 (setq ext (match-string 1 dfile))))
10703 ((member in-emacs '((16) system))
10704 (setq cmd (cdr (assoc 'system apps))))
10705 (in-emacs (setq cmd 'emacs))
10707 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
10708 (and dirp (cdr (assoc 'directory apps)))
10709 ; first, try matching against apps-dlink
10710 ; if we get a match here, store the match data for later
10711 (let ((match (assoc-default dlink apps-dlink
10714 (progn (setq link-match-data (match-data))
10716 (progn (setq in-emacs (or in-emacs line search))
10717 nil))) ; if we have no match in apps-dlink,
10718 ; always open the file in emacs if line or search
10719 ; is given (for backwards compatibility)
10720 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
10722 (cdr (assoc ext apps))
10723 (cdr (assoc t apps))))))
10724 (when (eq cmd 'system)
10725 (setq cmd (cdr (assoc 'system apps))))
10726 (when (eq cmd 'default)
10727 (setq cmd (cdr (assoc t apps))))
10728 (when (eq cmd 'mailcap)
10730 (mailcap-parse-mailcaps)
10731 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
10732 (command (mailcap-mime-info mime-type)))
10733 (if (stringp command)
10735 (setq cmd 'emacs))))
10736 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
10737 (not (file-exists-p file))
10738 (not org-open-non-existing-files))
10739 (error "No such file: %s" file))
10741 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
10742 ;; Remove quotes around the file name - we'll use shell-quote-argument.
10743 (while (string-match "['\"]%s['\"]" cmd)
10744 (setq cmd (replace-match "%s" t t cmd)))
10745 (while (string-match "%s" cmd)
10746 (setq cmd (replace-match
10748 (shell-quote-argument
10749 (convert-standard-filename file)))
10752 ;; Replace "%1", "%2" etc. in command with group matches from regex
10754 (let ((match-index 1)
10755 (number-of-groups (- (/ (length link-match-data) 2) 1)))
10756 (set-match-data link-match-data)
10757 (while (<= match-index number-of-groups)
10758 (let ((regex (concat "%" (number-to-string match-index)))
10759 (replace-with (match-string match-index dlink)))
10760 (while (string-match regex cmd)
10761 (setq cmd (replace-match replace-with t t cmd))))
10762 (setq match-index (+ match-index 1)))))
10764 (save-window-excursion
10765 (message "Running %s...done" cmd)
10766 (start-process-shell-command cmd nil cmd)
10767 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
10770 (funcall (cdr (assq 'file org-link-frame-setup)) file)
10772 (if line (org-goto-line line)
10773 (if search (org-link-search search))))
10775 (let ((file (convert-standard-filename file)))
10777 (set-match-data link-match-data)
10779 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
10780 (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
10781 (or (not (equal old-buffer (current-buffer)))
10782 (not (equal old-pos (point))))
10783 (org-mark-ring-push old-pos old-buffer))))
10785 (defun org-file-apps-entry-match-against-dlink-p (entry)
10786 "This function returns non-nil if `entry' uses a regular
10787 expression which should be matched against the whole link by
10790 It assumes that is the case when the entry uses a regular
10791 expression which has at least one grouping construct and the
10792 action is either a lisp form or a command string containing
10793 '%1', i.e. using at least one subexpression match as a
10795 (let ((selector (car entry))
10796 (action (cdr entry)))
10797 (if (stringp selector)
10798 (and (> (regexp-opt-depth selector) 0)
10799 (or (and (stringp action)
10800 (string-match "%[0-9]" action))
10804 (defun org-default-apps ()
10805 "Return the default applications for this operating system."
10807 ((eq system-type 'darwin)
10808 org-file-apps-defaults-macosx)
10809 ((eq system-type 'windows-nt)
10810 org-file-apps-defaults-windowsnt)
10811 (t org-file-apps-defaults-gnu)))
10813 (defun org-apps-regexp-alist (list &optional add-auto-mode)
10814 "Convert extensions to regular expressions in the cars of LIST.
10815 Also, weed out any non-string entries, because the return value is used
10816 only for regexp matching.
10817 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
10818 point to the symbol `emacs', indicating that the file should
10819 be opened in Emacs."
10822 (mapcar (lambda (x)
10823 (if (not (stringp (car x)))
10825 (if (string-match "\\W" (car x))
10827 (cons (concat "\\." (car x) "\\'") (cdr x)))))
10830 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
10832 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
10833 (defun org-file-remote-p (file)
10834 "Test whether FILE specifies a location on a remote system.
10835 Return non-nil if the location is indeed remote.
10837 For example, the filename \"/user@host:/foo\" specifies a location
10838 on the system \"/user@host:\"."
10839 (cond ((fboundp 'file-remote-p)
10840 (file-remote-p file))
10841 ((fboundp 'tramp-handle-file-remote-p)
10842 (tramp-handle-file-remote-p file))
10843 ((and (boundp 'ange-ftp-name-format)
10844 (string-match (car ange-ftp-name-format) file))
10850 (defun org-get-org-file ()
10851 "Read a filename, with default directory `org-directory'."
10852 (let ((default (or org-default-notes-file remember-data-file)))
10853 (read-file-name (format "File name [%s]: " default)
10854 (file-name-as-directory org-directory)
10857 (defun org-notes-order-reversed-p ()
10858 "Check if the current file should receive notes in reversed order."
10860 ((not org-reverse-note-order) nil)
10861 ((eq t org-reverse-note-order) t)
10862 ((not (listp org-reverse-note-order)) nil)
10864 (let ((all org-reverse-note-order)
10866 (while (setq entry (pop all))
10867 (if (string-match (car entry) buffer-file-name)
10868 (throw 'exit (cdr entry))))
10871 (defvar org-refile-target-table nil
10872 "The list of refile targets, created by `org-refile'.")
10874 (defvar org-agenda-new-buffers nil
10875 "Buffers created to visit agenda files.")
10877 (defvar org-refile-cache nil
10878 "Cache for refile targets.")
10880 (defvar org-refile-markers nil
10881 "All the markers used for caching refile locations.")
10883 (defun org-refile-marker (pos)
10884 "Get a new refile marker, but only if caching is in use."
10885 (if (not org-refile-use-cache)
10887 (let ((m (make-marker)))
10888 (move-marker m pos)
10889 (push m org-refile-markers)
10892 (defun org-refile-cache-clear ()
10893 "Clear the refile cache and disable all the markers."
10894 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
10895 (setq org-refile-markers nil)
10896 (setq org-refile-cache nil)
10897 (message "Refile cache has been cleared"))
10899 (defun org-refile-cache-check-set (set)
10900 "Check if all the markers in the cache still have live buffers."
10903 (while (and set (setq marker (nth 3 (pop set))))
10904 ;; if org-refile-use-outline-path is 'file, marker may be nil
10905 (when (and marker (null (marker-buffer marker)))
10906 (message "not found") (sit-for 3)
10907 (throw 'exit nil)))
10910 (defun org-refile-cache-put (set &rest identifiers)
10911 "Push the refile targets SET into the cache, under IDENTIFIERS."
10912 (let* ((key (sha1 (prin1-to-string identifiers)))
10913 (entry (assoc key org-refile-cache)))
10916 (push (cons key set) org-refile-cache))))
10918 (defun org-refile-cache-get (&rest identifiers)
10919 "Retrieve the cached value for refile targets given by IDENTIFIERS."
10921 ((not org-refile-cache) nil)
10922 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
10924 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
10925 org-refile-cache))))
10926 (and set (org-refile-cache-check-set set) set)))))
10928 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
10929 "Produce a table with refile targets."
10930 (let ((case-fold-search nil)
10931 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
10932 (entries (or org-refile-targets '((nil . (:level . 1)))))
10933 targets tgs txt re files f desc descre fast-path-p level pos0)
10934 (message "Getting targets...")
10935 (with-current-buffer (or default-buffer (current-buffer))
10936 (while (setq entry (pop entries))
10937 (setq files (car entry) desc (cdr entry))
10938 (setq fast-path-p nil)
10940 ((null files) (setq files (list (current-buffer))))
10941 ((eq files 'org-agenda-files)
10942 (setq files (org-agenda-files 'unrestricted)))
10943 ((and (symbolp files) (fboundp files))
10944 (setq files (funcall files)))
10945 ((and (symbolp files) (boundp files))
10946 (setq files (symbol-value files))))
10947 (if (stringp files) (setq files (list files)))
10949 ((eq (car desc) :tag)
10950 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
10951 ((eq (car desc) :todo)
10952 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
10953 ((eq (car desc) :regexp)
10954 (setq descre (cdr desc)))
10955 ((eq (car desc) :level)
10956 (setq descre (concat "^\\*\\{" (number-to-string
10957 (if org-odd-levels-only
10958 (1- (* 2 (cdr desc)))
10961 ((eq (car desc) :maxlevel)
10962 (setq fast-path-p t)
10963 (setq descre (concat "^\\*\\{1," (number-to-string
10964 (if org-odd-levels-only
10965 (1- (* 2 (cdr desc)))
10968 (t (error "Bad refiling target description %s" desc)))
10969 (while (setq f (pop files))
10970 (with-current-buffer
10971 (if (bufferp f) f (org-get-agenda-file-buffer f))
10973 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
10975 (if (bufferp f) (setq f (buffer-file-name
10976 (buffer-base-buffer f))))
10977 (setq f (and f (expand-file-name f)))
10978 (if (eq org-refile-use-outline-path 'file)
10979 (push (list (file-name-nondirectory f) f nil nil) tgs))
10983 (goto-char (point-min))
10984 (while (re-search-forward descre nil t)
10985 (goto-char (setq pos0 (point-at-bol)))
10987 (when org-refile-target-verify-function
10989 (or (funcall org-refile-target-verify-function)
10991 (when (and (looking-at org-complex-heading-regexp)
10992 (not (member (match-string 4) excluded-entries))
10994 (setq level (org-reduced-level
10995 (- (match-end 1) (match-beginning 1)))
10996 txt (org-link-display-format (match-string 4))
10997 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
10998 re (format org-complex-heading-regexp-format
10999 (regexp-quote (match-string 4))))
11000 (when org-refile-use-outline-path
11001 (setq txt (mapconcat
11004 (if (eq org-refile-use-outline-path
11006 (list (file-name-nondirectory
11008 (buffer-base-buffer))))
11009 (if (eq org-refile-use-outline-path
11011 (list (buffer-file-name
11012 (buffer-base-buffer)))))
11013 (org-get-outline-path fast-path-p
11017 (push (list txt f re (org-refile-marker (point)))
11019 (when (= (point) pos0)
11020 ;; verification function has not moved point
11021 (goto-char (point-at-eol))))))))
11022 (when org-refile-use-cache
11023 (org-refile-cache-put tgs (buffer-file-name) descre))
11024 (setq targets (append tgs targets))
11026 (message "Getting targets...done")
11027 (nreverse targets)))
11029 (defun org-protect-slash (s)
11030 (while (string-match "/" s)
11031 (setq s (replace-match "\\" t t s)))
11034 (defvar org-olpa (make-vector 20 nil))
11036 (defun org-get-outline-path (&optional fastp level heading)
11037 "Return the outline path to the current entry, as a list.
11039 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
11040 routine which makes outline path derivations for an entire file,
11041 avoiding backtracing. Refile target collection makes use of that."
11045 (error "Outline path failure, more than 19 levels"))
11046 (loop for i from level upto 19 do
11047 (aset org-olpa i nil))
11049 (delq nil (append org-olpa nil))
11050 (aset org-olpa level heading)))
11051 (let (rtn case-fold-search)
11055 (while (org-up-heading-safe)
11056 (when (looking-at org-complex-heading-regexp)
11057 (push (org-match-string-no-properties 4) rtn)))
11060 (defun org-format-outline-path (path &optional width prefix separator)
11061 "Format the outline path PATH for display.
11062 WIDTH is the maximum number of characters that is available.
11063 PREFIX is a prefix to be included in the returned string,
11064 such as the file name.
11065 SEPARATOR is inserted between the different parts of the path,
11066 the default is \"/\"."
11067 (setq width (or width 79))
11068 (if prefix (setq width (- width (length prefix))))
11071 (let* ((nsteps (length path))
11072 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
11073 (maxwidth (if (<= total-width width)
11074 10000 ;; everything fits
11075 ;; we need to shorten the level headings
11076 (/ (- width nsteps) nsteps)))
11077 (org-odd-levels-only nil)
11079 (total (1+ (length prefix))))
11080 (setq maxwidth (max maxwidth 10))
11082 (if prefix (or separator "/"))
11086 (if (and (= n nsteps) (< maxwidth 10000))
11087 (setq maxwidth (- total-width total)))
11088 (if (< (length h) maxwidth)
11089 (progn (setq total (+ total (length h) 1)) h)
11090 (setq h (substring h 0 (- maxwidth 2))
11091 total (+ total maxwidth 1))
11092 (if (string-match "[ \t]+\\'" h)
11093 (setq h (substring h 0 (match-beginning 0))))
11094 (setq h (concat h "..")))
11095 (org-add-props h nil 'face
11096 (nth (% (1- n) org-n-level-faces)
11099 path (or separator "/"))))))
11101 (defun org-display-outline-path (&optional file current separator just-return-string)
11102 "Display the current outline path in the echo area.
11104 If FILE is non-nil, prepend the output with the file name.
11105 If CURRENT is non-nil, append the current heading to the output.
11106 SEPARATOR is passed through to `org-format-outline-path'. It separates
11107 the different parts of the path and defaults to \"/\".
11108 If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
11110 (let* (case-fold-search
11111 message-log-max ; Don't populate the *Messages* buffer
11112 (bfn (buffer-file-name (buffer-base-buffer)))
11113 (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
11115 (if current (setq path (append path
11117 (org-back-to-heading t)
11118 (if (looking-at org-complex-heading-regexp)
11119 (list (match-string 4)))))))
11121 (org-format-outline-path
11124 (and file bfn (concat (file-name-nondirectory bfn) separator))
11126 (if just-return-string
11127 (org-no-properties res)
11128 (message "%s" res))))
11130 (defvar org-refile-history nil
11131 "History for refiling operations.")
11133 (defvar org-after-refile-insert-hook nil
11134 "Hook run after `org-refile' has inserted its stuff at the new location.
11135 Note that this is still *before* the stuff will be removed from
11136 the *old* location.")
11138 (defvar org-capture-last-stored-marker)
11139 (defvar org-refile-keep nil
11140 "Non-nil means `org-refile' will copy instead of refile.")
11143 "Like `org-refile', but copy."
11145 (let ((org-refile-keep t))
11146 (funcall 'org-refile nil nil nil "Copy")))
11148 (defun org-refile (&optional goto default-buffer rfloc msg)
11149 "Move the entry or entries at point to another heading.
11150 The list of target headings is compiled using the information in
11151 `org-refile-targets', which see.
11153 At the target location, the entry is filed as a subitem of the target
11154 heading. Depending on `org-reverse-note-order', the new subitem will
11155 either be the first or the last subitem.
11157 If there is an active region, all entries in that region will be moved.
11158 However, the region must fulfill the requirement that the first heading
11159 is the first one sets the top-level of the moved text - at most siblings
11160 below it are allowed.
11162 With prefix arg GOTO, the command will only visit the target location
11163 and not actually move anything.
11165 With a double prefix arg \\[universal-argument] \\[universal-argument], \
11166 go to the location where the last refiling operation has put the subtree.
11167 With a prefix argument of `2', refile to the running clock.
11169 RFLOC can be a refile location obtained in a different way.
11171 MSG is a string to replace \"Refile\" in the default prompt with
11172 another verb. E.g. `org-copy' sets this parameter to \"Copy\".
11174 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
11176 If you are using target caching (see `org-refile-use-cache'),
11177 you have to clear the target cache in order to find new targets.
11178 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
11179 prefix argument (`C-u C-u C-u C-c C-w')."
11182 (if (member goto '(0 (64)))
11183 (org-refile-cache-clear)
11184 (let* ((actionmsg (or msg "Refile"))
11185 (cbuf (current-buffer))
11186 (regionp (org-region-active-p))
11187 (region-start (and regionp (region-beginning)))
11188 (region-end (and regionp (region-end)))
11189 (region-length (and regionp (- region-end region-start)))
11190 (filename (buffer-file-name (buffer-base-buffer cbuf)))
11191 pos it nbuf file re level reversed)
11192 (setq last-command nil)
11194 (goto-char region-start)
11195 (or (bolp) (goto-char (point-at-bol)))
11196 (setq region-start (point))
11197 (unless (or (org-kill-is-subtree-p
11198 (buffer-substring region-start region-end))
11199 (prog1 org-refile-active-region-within-subtree
11200 (org-toggle-heading)))
11201 (error "The region is not a (sequence of) subtree(s)")))
11202 (if (equal goto '(16))
11203 (org-refile-goto-last-stored)
11205 (and (equal goto 2)
11206 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
11208 (setq it (list (or org-clock-heading "running clock")
11210 (marker-buffer org-clock-hd-marker))
11212 (marker-position org-clock-hd-marker)))
11215 (let (heading-text)
11218 (org-back-to-heading t)
11220 (nth 4 (org-heading-components))))
11222 (org-refile-get-location
11223 (cond (goto "Goto")
11224 (regionp (concat actionmsg " region to"))
11225 (t (concat actionmsg " subtree \""
11226 heading-text "\" to")))
11228 (and (not (equal '(4) goto))
11229 org-refile-allow-creating-parent-nodes)
11231 (setq file (nth 1 it)
11234 (if (and (not goto)
11236 (equal (buffer-file-name) file)
11238 (and (>= pos region-start)
11239 (<= pos region-end))
11240 (and (>= pos (point))
11241 (< pos (save-excursion
11242 (org-end-of-subtree t t))))))
11243 (error "Cannot refile to position inside the tree or region"))
11245 (setq nbuf (or (find-buffer-visiting file)
11246 (find-file-noselect file)))
11249 (org-pop-to-buffer-same-window nbuf)
11251 (org-show-context 'org-goto))
11254 (org-kill-new (buffer-substring region-start region-end))
11255 (org-save-markers-in-region region-start region-end))
11256 (org-copy-subtree 1 nil t))
11257 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
11258 (find-file-noselect file)))
11259 (setq reversed (org-notes-order-reversed-p))
11266 (looking-at org-outline-regexp)
11267 (setq level (org-get-valid-level (funcall outline-level) 1))
11270 (or (outline-next-heading) (point-max))
11271 (or (save-excursion (org-get-next-sibling))
11272 (org-end-of-subtree t t)
11276 (goto-char (point-max))
11277 (goto-char (point-min))
11278 (or (outline-next-heading) (goto-char (point-max)))))
11279 (if (not (bolp)) (newline))
11280 (org-paste-subtree level)
11281 (when org-log-refile
11282 (org-add-log-setup 'refile nil nil 'findpos
11284 (unless (eq org-log-refile 'note)
11285 (save-excursion (org-add-log-note))))
11286 (and org-auto-align-tags
11287 (let ((org-loop-over-headlines-in-active-region nil))
11288 (org-set-tags nil t)))
11289 (with-demoted-errors
11290 (bookmark-set "org-refile-last-stored"))
11291 ;; If we are refiling for capture, make sure that the
11292 ;; last-capture pointers point here
11293 (when (org-bound-and-true-p org-refile-for-capture)
11294 (with-demoted-errors
11295 (bookmark-set "org-capture-last-stored-marker"))
11296 (move-marker org-capture-last-stored-marker (point)))
11297 (if (fboundp 'deactivate-mark) (deactivate-mark))
11298 (run-hooks 'org-after-refile-insert-hook))))
11299 (unless org-refile-keep
11301 (delete-region (point) (+ (point) region-length))
11302 (org-cut-subtree)))
11303 (when (featurep 'org-inlinetask)
11304 (org-inlinetask-remove-END-maybe))
11305 (setq org-markers-to-move nil)
11306 (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
11308 (defun org-refile-goto-last-stored ()
11309 "Go to the location where the last refile was stored."
11311 (bookmark-jump "org-refile-last-stored")
11312 (message "This is the location of the last refile"))
11314 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
11316 "Prompt the user for a refile location, using PROMPT.
11317 PROMPT should not be suffixed with a colon and a space, because
11318 this function appends the default value from
11319 `org-refile-history' automatically, if that is not empty.
11320 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
11321 this is used for the GOTO interface."
11322 (let ((org-refile-targets org-refile-targets)
11323 (org-refile-use-outline-path org-refile-use-outline-path)
11325 (when (and (derived-mode-p 'org-mode)
11326 (not org-refile-use-cache)
11330 (setq excluded-entries
11331 (append excluded-entries (list (org-get-heading t t)))))))
11332 (setq org-refile-target-table
11333 (org-refile-get-targets default-buffer excluded-entries)))
11334 (unless org-refile-target-table
11335 (error "No refile targets"))
11336 (let* ((cbuf (current-buffer))
11337 (partial-completion-mode nil)
11338 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
11339 (cfunc (if (and org-refile-use-outline-path
11340 org-outline-path-complete-in-steps)
11341 'org-olpath-completing-read
11342 'org-icompleting-read))
11343 (extra (if org-refile-use-outline-path "/" ""))
11344 (cbnex (concat (buffer-name) extra))
11345 (filename (and cfn (expand-file-name cfn)))
11348 (if (and (not (member org-refile-use-outline-path
11349 '(file full-file-path)))
11350 (not (equal filename (nth 1 x))))
11351 (cons (concat (car x) extra " ("
11352 (file-name-nondirectory (nth 1 x)) ")")
11354 (cons (concat (car x) extra) (cdr x))))
11355 org-refile-target-table))
11356 (completion-ignore-case t)
11358 (prompt (concat prompt
11359 (or (and (car org-refile-history)
11360 (concat " (default " (car org-refile-history) ")"))
11361 (and (assoc cbnex tbl) (setq cdef cbnex)
11362 (concat " (default " cbnex ")"))) ": "))
11363 pa answ parent-target child parent old-hist)
11364 (setq old-hist org-refile-history)
11365 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
11366 nil 'org-refile-history (or cdef (car org-refile-history))))
11367 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
11368 (org-refile-check-position pa)
11371 (when (or (not org-refile-history)
11372 (not (eq old-hist org-refile-history))
11373 (not (equal (car pa) (car org-refile-history))))
11374 (setq org-refile-history
11375 (cons (car pa) (if (assoc (car org-refile-history) tbl)
11377 (cdr org-refile-history))))
11378 (if (equal (car org-refile-history) (nth 1 org-refile-history))
11379 (pop org-refile-history)))
11381 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
11383 (setq parent (match-string 1 answ)
11384 child (match-string 2 answ))
11385 (setq parent-target (or (assoc parent tbl)
11386 (assoc (concat parent "/") tbl)))
11387 (when (and parent-target
11388 (or (eq new-nodes t)
11389 (and (eq new-nodes 'confirm)
11390 (y-or-n-p (format "Create new node \"%s\"? "
11392 (org-refile-new-child parent-target child)))
11393 (error "Invalid target location")))))
11395 (declare-function org-string-nw-p "org-macs" (s))
11396 (defun org-refile-check-position (refile-pointer)
11397 "Check if the refile pointer matches the headline to which it points."
11398 (let* ((file (nth 1 refile-pointer))
11399 (re (nth 2 refile-pointer))
11400 (pos (nth 3 refile-pointer))
11402 (if (and (not (markerp pos)) (not file))
11403 (error "Please save the buffer to a file before refiling")
11404 (when (org-string-nw-p re)
11405 (setq buffer (if (markerp pos)
11406 (marker-buffer pos)
11407 (or (find-buffer-visiting file)
11408 (find-file-noselect file))))
11409 (with-current-buffer buffer
11414 (beginning-of-line 1)
11415 (unless (org-looking-at-p re)
11416 (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
11418 (defun org-refile-new-child (parent-target child)
11419 "Use refile target PARENT-TARGET to add new CHILD below it."
11420 (unless parent-target
11421 (error "Cannot find parent for new node"))
11422 (let ((file (nth 1 parent-target))
11423 (pos (nth 3 parent-target))
11425 (with-current-buffer (or (find-buffer-visiting file)
11426 (find-file-noselect file))
11432 (goto-char (point-max))
11433 (if (not (bolp)) (newline)))
11434 (when (looking-at org-outline-regexp)
11435 (setq level (funcall outline-level))
11436 (org-end-of-subtree t t))
11437 (org-back-over-empty-lines)
11438 (insert "\n" (make-string
11439 (if pos (org-get-valid-level level 1) 1) ?*)
11441 (beginning-of-line 0)
11442 (list (concat (car parent-target) "/" child) file "" (point)))))))
11444 (defun org-olpath-completing-read (prompt collection &rest args)
11445 "Read an outline path like a file name."
11446 (let ((thetable collection)
11447 (org-completion-use-ido nil) ; does not work with ido.
11448 (org-completion-use-iswitchb nil)) ; or iswitchb
11450 'org-icompleting-read prompt
11451 (lambda (string predicate &optional flag)
11452 (let (rtn r f (l (length string)))
11456 (try-completion string thetable))
11459 (setq rtn (all-completions string thetable predicate))
11462 (setq r (substring x l))
11463 (if (string-match " ([^)]*)$" x)
11464 (setq f (match-string 0 x))
11466 (if (string-match "/" r)
11467 (concat string (substring r 0 (match-end 0)) f)
11472 (assoc string thetable)))))
11475 ;;;; Dynamic blocks
11477 (defun org-find-dblock (name)
11478 "Find the first dynamic block with name NAME in the buffer.
11479 If not found, stay at current position and return nil."
11480 (let ((case-fold-search t) pos)
11482 (goto-char (point-min))
11483 (setq pos (and (re-search-forward
11484 (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
11485 (match-beginning 0))))
11486 (if pos (goto-char pos))
11489 (defconst org-dblock-start-re
11490 "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
11491 "Matches the start line of a dynamic block, with parameters.")
11493 (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
11494 "Matches the end of a dynamic block.")
11496 (defun org-create-dblock (plist)
11497 "Create a dynamic block section, with parameters taken from PLIST.
11498 PLIST must contain a :name entry which is used as name of the block."
11499 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
11502 (let ((col (current-column))
11503 (name (plist-get plist :name)))
11504 (insert "#+BEGIN: " name)
11506 (if (eq (car plist) :name)
11507 (setq plist (cddr plist))
11508 (insert " " (prin1-to-string (pop plist)))))
11509 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
11510 (beginning-of-line -2)))
11512 (defun org-prepare-dblock ()
11513 "Prepare dynamic block for refresh.
11514 This empties the block, puts the cursor at the insert position and returns
11515 the property list including an extra property :name with the block name."
11516 (unless (looking-at org-dblock-start-re)
11517 (error "Not at a dynamic block"))
11518 (let* ((begdel (1+ (match-end 0)))
11519 (name (org-no-properties (match-string 1)))
11520 (params (append (list :name name)
11521 (read (concat "(" (match-string 3) ")")))))
11523 (beginning-of-line 1)
11524 (skip-chars-forward " \t")
11525 (setq params (plist-put params :indentation-column (current-column))))
11526 (unless (re-search-forward org-dblock-end-re nil t)
11527 (error "Dynamic block not terminated"))
11530 (list :content (buffer-substring
11531 begdel (match-beginning 0)))))
11532 (delete-region begdel (match-beginning 0))
11537 (defun org-map-dblocks (&optional command)
11538 "Apply COMMAND to all dynamic blocks in the current buffer.
11539 If COMMAND is not given, use `org-update-dblock'."
11540 (let ((cmd (or command 'org-update-dblock)))
11542 (goto-char (point-min))
11543 (while (re-search-forward org-dblock-start-re nil t)
11544 (goto-char (match-beginning 0))
11546 (condition-case nil
11548 (error (message "Error during update of dynamic block"))))
11549 (unless (re-search-forward org-dblock-end-re nil t)
11550 (error "Dynamic block not terminated"))))))
11552 (defun org-dblock-update (&optional arg)
11553 "User command for updating dynamic blocks.
11554 Update the dynamic block at point. With prefix ARG, update all dynamic
11555 blocks in the buffer."
11558 (org-update-all-dblocks)
11559 (or (looking-at org-dblock-start-re)
11560 (org-beginning-of-dblock))
11561 (org-update-dblock)))
11563 (defun org-update-dblock ()
11564 "Update the dynamic block at point.
11565 This means to empty the block, parse for parameters and then call
11566 the correct writing function."
11568 (save-window-excursion
11569 (let* ((pos (point))
11570 (line (org-current-line))
11571 (params (org-prepare-dblock))
11572 (name (plist-get params :name))
11573 (indent (plist-get params :indentation-column))
11574 (cmd (intern (concat "org-dblock-write:" name))))
11575 (message "Updating dynamic block `%s' at line %d..." name line)
11576 (funcall cmd params)
11577 (message "Updating dynamic block `%s' at line %d...done" name line)
11579 (when (and indent (> indent 0))
11580 (setq indent (make-string indent ?\ ))
11582 (org-beginning-of-dblock)
11584 (while (not (looking-at org-dblock-end-re))
11586 (beginning-of-line 2))
11587 (when (looking-at org-dblock-end-re)
11588 (and (looking-at "[ \t]+")
11589 (replace-match ""))
11590 (insert indent)))))))
11592 (defun org-beginning-of-dblock ()
11593 "Find the beginning of the dynamic block at point.
11594 Error if there is no such block at point."
11595 (let ((pos (point))
11598 (if (and (re-search-backward org-dblock-start-re nil t)
11599 (setq beg (match-beginning 0))
11600 (re-search-forward org-dblock-end-re nil t)
11601 (> (match-end 0) pos))
11604 (error "Not in a dynamic block"))))
11606 (defun org-update-all-dblocks ()
11607 "Update all dynamic blocks in the buffer.
11608 This function can be used in a hook."
11610 (when (derived-mode-p 'org-mode)
11611 (org-map-dblocks 'org-update-dblock)))
11616 (defun org-get-export-keywords ()
11617 "Return a list of all currently understood export keywords.
11618 Export keywords include options, block names, attributes and
11619 keywords relative to each registered export back-end."
11624 (let ((props (cdr back-end)))
11625 ;; Back-end name (for keywords, like #+LATEX:)
11626 (push (upcase (symbol-name (car back-end))) keywords)
11627 ;; Back-end options.
11628 (mapc (lambda (option) (push (cadr option) keywords))
11629 (plist-get (cdr back-end) :options-alist))))
11630 (org-bound-and-true-p org-export-registered-backends))
11633 (defconst org-options-keywords
11634 '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
11635 "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
11636 "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
11637 "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
11638 "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "INFOJS_OPT:"))
11640 (defcustom org-structure-template-alist
11641 '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
11642 "<src lang=\"?\">\n\n</src>")
11643 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
11644 "<example>\n?\n</example>")
11645 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
11646 "<quote>\n?\n</quote>")
11647 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
11648 "<verse>\n?\n</verse>")
11649 ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
11650 "<verbatim>\n?\n</verbatim>")
11651 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
11652 "<center>\n?\n</center>")
11653 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
11654 "<literal style=\"latex\">\n?\n</literal>")
11656 "<literal style=\"latex\">?</literal>")
11657 ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
11658 "<literal style=\"html\">\n?\n</literal>")
11660 "<literal style=\"html\">?</literal>")
11661 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
11665 ("I" "#+INCLUDE: %file ?"
11666 "<include file=%file markup=\"?\">"))
11667 "Structure completion elements.
11668 This is a list of abbreviation keys and values. The value gets inserted
11669 if you type `<' followed by the key and then press the completion key,
11670 usually `M-TAB'. %file will be replaced by a file name after prompting
11671 for the file using completion. The cursor will be placed at the position
11672 of the `?` in the template.
11673 There are two templates for each key, the first uses the original Org syntax,
11674 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
11675 the default when the /org-mtags.el/ module has been loaded. See also the
11676 variable `org-mtags-prefer-muse-templates'."
11677 :group 'org-completion
11679 (string :tag "Key")
11680 (string :tag "Template")
11681 (string :tag "Muse Template")))
11683 (defun org-try-structure-completion ()
11684 "Try to complete a structure template before point.
11685 This looks for strings like \"<e\" on an otherwise empty line and
11687 (let ((l (buffer-substring (point-at-bol) (point)))
11689 (when (and (looking-at "[ \t]*$")
11690 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
11691 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
11692 (org-complete-expand-structure-template (+ -1 (point-at-bol)
11693 (match-beginning 1)) a)
11696 (defun org-complete-expand-structure-template (start cell)
11697 "Expand a structure template."
11698 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
11699 (rpl (nth (if musep 2 1) cell))
11701 (delete-region start (point))
11702 (when (string-match "\\`#\\+" rpl)
11705 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
11706 (setq ind (buffer-substring (point-at-bol) (point))))
11708 (setq start (point))
11709 (if (string-match "%file" rpl)
11710 (setq rpl (replace-match
11714 (abbreviate-file-name (read-file-name "Include file: ")))
11717 (setq rpl (mapconcat 'identity (split-string rpl "\n")
11718 (concat "\n" ind)))
11720 (if (re-search-backward "\\?" start t) (delete-char 1))))
11722 ;;;; TODO, DEADLINE, Comments
11724 (defun org-toggle-comment ()
11725 "Change the COMMENT state of an entry."
11728 (org-back-to-heading)
11729 (let (case-fold-search)
11731 ((looking-at (format org-heading-keyword-regexp-format
11732 org-comment-string))
11733 (goto-char (match-end 1))
11734 (looking-at (concat " +" org-comment-string))
11735 (replace-match "" t t)
11736 (when (eolp) (insert " ")))
11737 ((looking-at org-outline-regexp)
11738 (goto-char (match-end 0))
11739 (insert org-comment-string " "))))))
11741 (defvar org-last-todo-state-is-todo nil
11742 "This is non-nil when the last TODO state change led to a TODO state.
11743 If the last change removed the TODO tag or switched to DONE, then
11746 (defvar org-setting-tags nil) ; dynamically skipped
11748 (defvar org-todo-setup-filter-hook nil
11749 "Hook for functions that pre-filter todo specs.
11750 Each function takes a todo spec and returns either nil or the spec
11751 transformed into canonical form." )
11753 (defvar org-todo-get-default-hook nil
11754 "Hook for functions that get a default item for todo.
11755 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
11756 nil or a string to be used for the todo mark." )
11758 (defvar org-agenda-headline-snapshot-before-repeat)
11760 (defun org-current-effective-time ()
11761 "Return current time adjusted for `org-extend-today-until' variable."
11762 (let* ((ct (org-current-time))
11763 (dct (decode-time ct))
11766 (org-use-last-clock-out-time-as-effective-time
11767 (or (org-clock-get-last-clock-out-time) ct))
11768 ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
11769 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
11773 (defun org-todo-yesterday (&optional arg)
11774 "Like `org-todo' but the time of change will be 23:59 of yesterday."
11776 (if (eq major-mode 'org-agenda-mode)
11777 (apply 'org-agenda-todo-yesterday arg)
11778 (let* ((hour (third (decode-time
11779 (org-current-time))))
11780 (org-extend-today-until (1+ hour)))
11783 (defvar org-block-entry-blocking ""
11784 "First entry preventing the TODO state change.")
11786 (defun org-todo (&optional arg)
11787 "Change the TODO state of an item.
11788 The state of an item is given by a keyword at the start of the heading,
11790 *** TODO Write paper
11793 The different keywords are specified in the variable `org-todo-keywords'.
11794 By default the available states are \"TODO\" and \"DONE\".
11795 So for this example: when the item starts with TODO, it is changed to DONE.
11796 When it starts with DONE, the DONE is removed. And when neither TODO nor
11797 DONE are present, add TODO at the beginning of the heading.
11799 With \\[universal-argument] prefix arg, use completion to determine the new \
11801 With numeric prefix arg, switch to that state.
11802 With a double \\[universal-argument] prefix, switch to the next set of TODO \
11803 keywords (nextset).
11804 With a triple \\[universal-argument] prefix, circumvent any state blocking.
11805 With a numeric prefix arg of 0, inhibit note taking for the change.
11807 For calling through lisp, arg is also interpreted in the following way:
11808 'none -> empty state
11809 \"\"(empty string) -> switch to empty state
11810 'done -> switch to DONE
11811 'nextset -> switch to the next set of keywords
11812 'previousset -> switch to the previous set of keywords
11813 \"WAITING\" -> switch to the specified keyword, but only if it
11814 really is a member of `org-todo-keywords'."
11816 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
11817 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
11818 'region-start-level 'region))
11819 org-loop-over-headlines-in-active-region)
11822 org-loop-over-headlines-in-active-region
11823 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
11824 (if (equal arg '(16)) (setq arg 'nextset))
11825 (let ((org-blocker-hook org-blocker-hook)
11828 (when (equal arg '(64))
11829 (setq arg nil org-blocker-hook nil))
11830 (when (and org-blocker-hook
11831 (or org-inhibit-blocking
11832 (org-entry-get nil "NOBLOCKING")))
11833 (setq org-blocker-hook nil))
11836 (org-back-to-heading t)
11837 (when (looking-at (concat "^\\*+ " org-comment-string))
11838 (org-toggle-comment)
11840 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
11841 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
11842 (looking-at "\\(?: *\\|[ \t]*$\\)"))
11843 (let* ((match-data (match-data))
11844 (startpos (point-at-bol))
11845 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
11846 (org-log-done org-log-done)
11847 (org-log-repeat org-log-repeat)
11848 (org-todo-log-states org-todo-log-states)
11849 (org-inhibit-logging
11851 (progn (setq arg nil) 'note) org-inhibit-logging))
11852 (this (match-string 1))
11853 (hl-pos (match-beginning 0))
11854 (head (org-get-todo-sequence-head this))
11855 (ass (assoc head org-todo-kwd-alist))
11856 (interpret (nth 1 ass))
11857 (done-word (nth 3 ass))
11858 (final-done-word (nth 4 ass))
11859 (org-last-state (or this ""))
11860 (completion-ignore-case t)
11861 (member (member this org-todo-keywords-1))
11862 (tail (cdr member))
11864 ((and org-todo-key-trigger
11865 (or (and (equal arg '(4))
11866 (eq org-use-fast-todo-selection 'prefix))
11867 (and (not arg) org-use-fast-todo-selection
11868 (not (eq org-use-fast-todo-selection
11870 ;; Use fast selection
11871 (org-fast-todo-selection))
11872 ((and (equal arg '(4))
11873 (or (not org-use-fast-todo-selection)
11874 (not org-todo-key-trigger)))
11875 ;; Read a state with completion
11876 (org-icompleting-read
11877 "State: " (mapcar (lambda(x) (list x))
11878 org-todo-keywords-1)
11882 (if tail (car tail) nil)
11883 (car org-todo-keywords-1)))
11885 (if (equal member org-todo-keywords-1)
11888 (nth (- (length org-todo-keywords-1)
11890 org-todo-keywords-1)
11891 (org-last org-todo-keywords-1))))
11892 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
11893 (setq arg nil))) ; hack to fall back to cycling
11895 ;; user or caller requests a specific state
11897 ((equal arg "") nil)
11898 ((eq arg 'none) nil)
11899 ((eq arg 'done) (or done-word (car org-done-keywords)))
11901 (or (car (cdr (member head org-todo-heads)))
11902 (car org-todo-heads)))
11903 ((eq arg 'previousset)
11904 (let ((org-todo-heads (reverse org-todo-heads)))
11905 (or (car (cdr (member head org-todo-heads)))
11906 (car org-todo-heads))))
11907 ((car (member arg org-todo-keywords-1)))
11909 (error "State `%s' not valid in this file" arg))
11910 ((nth (1- (prefix-numeric-value arg))
11911 org-todo-keywords-1))))
11912 ((null member) (or head (car org-todo-keywords-1)))
11913 ((equal this final-done-word) nil) ;; -> make empty
11914 ((null tail) nil) ;; -> first entry
11915 ((memq interpret '(type priority))
11916 (if (eq this-command last-command)
11918 (if (> (length tail) 0)
11919 (or done-word (car org-done-keywords))
11924 (run-hook-with-args-until-success
11925 'org-todo-get-default-hook org-state org-last-state)
11927 (next (if org-state (concat " " org-state " ") " "))
11928 (change-plist (list :type 'todo-state-change :from this :to org-state
11929 :position startpos))
11931 (when org-blocker-hook
11932 (setq org-last-todo-state-is-todo
11933 (not (member this org-done-keywords)))
11934 (unless (save-excursion
11936 (org-with-wide-buffer
11937 (run-hook-with-args-until-failure
11938 'org-blocker-hook change-plist))))
11939 (if (org-called-interactively-p 'interactive)
11940 (user-error "TODO state change from %s to %s blocked (by \"%s\")"
11941 this org-state org-block-entry-blocking)
11943 (message "TODO state change from %s to %s blocked (by \"%s\")"
11944 this org-state org-block-entry-blocking)
11945 (throw 'exit nil))))
11946 (store-match-data match-data)
11947 (replace-match next t t)
11948 (unless (pos-visible-in-window-p hl-pos)
11949 (message "TODO state changed to %s" (org-trim next)))
11951 (setq head (org-get-todo-sequence-head org-state)
11952 ass (assoc head org-todo-kwd-alist)
11953 interpret (nth 1 ass)
11954 done-word (nth 3 ass)
11955 final-done-word (nth 4 ass)))
11956 (when (memq arg '(nextset previousset))
11957 (message "Keyword-Set %d/%d: %s"
11958 (- (length org-todo-sets) -1
11959 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
11960 (length org-todo-sets)
11961 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
11962 (setq org-last-todo-state-is-todo
11963 (not (member org-state org-done-keywords)))
11964 (setq now-done-p (and (member org-state org-done-keywords)
11965 (not (member this org-done-keywords))))
11966 (and logging (org-local-logging logging))
11967 (when (and (or org-todo-log-states org-log-done)
11968 (not (eq org-inhibit-logging t))
11969 (not (memq arg '(nextset previousset))))
11970 ;; we need to look at recording a time and note
11971 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
11972 (nth 2 (assoc this org-todo-log-states))))
11973 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
11974 (setq dolog 'time))
11975 (when (and org-state
11976 (member org-state org-not-done-keywords)
11977 (not (member this org-not-done-keywords)))
11978 ;; This is now a todo state and was not one before
11979 ;; If there was a CLOSED time stamp, get rid of it.
11980 (org-add-planning-info nil nil 'closed))
11981 (when (and now-done-p org-log-done)
11982 ;; It is now done, and it was not done before
11983 (org-add-planning-info 'closed (org-current-effective-time))
11984 (if (and (not dolog) (eq 'note org-log-done))
11985 (org-add-log-setup 'done org-state this 'findpos 'note)))
11986 (when (and org-state dolog)
11987 ;; This is a non-nil state, and we need to log it
11988 (org-add-log-setup 'state org-state this 'findpos dolog)))
11989 ;; Fixup tag positioning
11990 (org-todo-trigger-tag-changes org-state)
11991 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
11992 (when org-provide-todo-statistics
11993 (org-update-parent-todo-statistics))
11994 (run-hooks 'org-after-todo-state-change-hook)
11995 (if (and arg (not (member org-state org-done-keywords)))
11996 (setq head (org-get-todo-sequence-head org-state)))
11997 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
11998 ;; Do we need to trigger a repeat?
12000 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
12001 ;; This is for the agenda, take a snapshot of the headline.
12003 (setq org-agenda-headline-snapshot-before-repeat
12004 (org-get-heading))))
12005 (org-auto-repeat-maybe org-state))
12006 ;; Fixup cursor location if close to the keyword
12007 (if (and (outline-on-heading-p)
12009 (save-excursion (beginning-of-line 1)
12010 (looking-at org-todo-line-regexp))
12011 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
12013 (goto-char (or (match-end 2) (match-end 1)))
12014 (and (looking-at " ") (just-one-space))))
12015 (when org-trigger-hook
12017 (run-hook-with-args 'org-trigger-hook change-plist)))
12018 (when commentp (org-toggle-comment))))))))
12020 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
12021 "Block turning an entry into a TODO, using the hierarchy.
12022 This checks whether the current task should be blocked from state
12023 changes. Such blocking occurs when:
12025 1. The task has children which are not all in a completed state.
12027 2. A task has a parent with the property :ORDERED:, and there
12028 are siblings prior to the current task with incomplete
12031 3. The parent of the task is blocked because it has siblings that should
12032 be done first, or is child of a block grandparent TODO entry."
12034 (if (not org-enforce-todo-dependencies)
12035 t ; if locally turned off don't block
12037 ;; If this is not a todo state change, or if this entry is already DONE,
12039 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
12040 (member (plist-get change-plist :from)
12041 (cons 'done org-done-keywords))
12042 (member (plist-get change-plist :to)
12043 (cons 'todo org-not-done-keywords))
12044 (not (plist-get change-plist :to)))
12045 (throw 'dont-block t))
12046 ;; If this task has children, and any are undone, it's blocked
12048 (org-back-to-heading t)
12049 (let ((this-level (funcall outline-level)))
12050 (outline-next-heading)
12051 (let ((child-level (funcall outline-level)))
12052 (while (and (not (eobp))
12053 (> child-level this-level))
12054 ;; this todo has children, check whether they are all
12056 (if (and (not (org-entry-is-done-p))
12057 (org-entry-is-todo-p))
12058 (progn (setq org-block-entry-blocking (org-get-heading))
12059 (throw 'dont-block nil)))
12060 (outline-next-heading)
12061 (setq child-level (funcall outline-level))))))
12062 ;; Otherwise, if the task's parent has the :ORDERED: property, and
12063 ;; any previous siblings are undone, it's blocked
12065 (org-back-to-heading t)
12066 (let* ((pos (point))
12067 (parent-pos (and (org-up-heading-safe) (point))))
12068 (if (not parent-pos) (throw 'dont-block t)) ; no parent
12069 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
12071 (re-search-forward org-not-done-heading-regexp pos t))
12072 (setq org-block-entry-blocking (match-string 0))
12073 (throw 'dont-block nil)) ; block, there is an older sibling not done.
12074 ;; Search further up the hierarchy, to see if an ancestor is blocked
12076 (goto-char parent-pos)
12077 (if (not (looking-at org-not-done-heading-regexp))
12078 (throw 'dont-block t)) ; do not block, parent is not a TODO
12080 (setq parent-pos (and (org-up-heading-safe) (point)))
12081 (if (not parent-pos) (throw 'dont-block t)) ; no parent
12082 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
12084 (re-search-forward org-not-done-heading-regexp pos t)
12085 (setq org-block-entry-blocking (org-get-heading)))
12086 (throw 'dont-block nil)))))))) ; block, older sibling not done.
12088 (defcustom org-track-ordered-property-with-tag nil
12089 "Should the ORDERED property also be shown as a tag?
12090 The ORDERED property decides if an entry should require subtasks to be
12091 completed in sequence. Since a property is not very visible, setting
12092 this option means that toggling the ORDERED property with the command
12093 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
12094 not relevant for the behavior, but it makes things more visible.
12096 Note that toggling the tag with tags commands will not change the property
12097 and therefore not influence behavior!
12099 This can be t, meaning the tag ORDERED should be used, It can also be a
12100 string to select a different tag for this task."
12103 (const :tag "No tracking" nil)
12104 (const :tag "Track with ORDERED tag" t)
12105 (string :tag "Use other tag")))
12107 (defun org-toggle-ordered-property ()
12108 "Toggle the ORDERED property of the current entry.
12109 For better visibility, you can track the value of this property with a tag.
12110 See variable `org-track-ordered-property-with-tag'."
12112 (let* ((t1 org-track-ordered-property-with-tag)
12113 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
12115 (org-back-to-heading)
12116 (if (org-entry-get nil "ORDERED")
12118 (org-delete-property "ORDERED" "PROPERTIES")
12119 (and tag (org-toggle-tag tag 'off))
12120 (message "Subtasks can be completed in arbitrary order"))
12121 (org-entry-put nil "ORDERED" "t")
12122 (and tag (org-toggle-tag tag 'on))
12123 (message "Subtasks must be completed in sequence")))))
12125 (defvar org-blocked-by-checkboxes) ; dynamically scoped
12126 (defun org-block-todo-from-checkboxes (change-plist)
12127 "Block turning an entry into a TODO, using checkboxes.
12128 This checks whether the current task should be blocked from state
12129 changes because there are unchecked boxes in this entry."
12130 (if (not org-enforce-todo-checkbox-dependencies)
12131 t ; if locally turned off don't block
12133 ;; If this is not a todo state change, or if this entry is already DONE,
12135 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
12136 (member (plist-get change-plist :from)
12137 (cons 'done org-done-keywords))
12138 (member (plist-get change-plist :to)
12139 (cons 'todo org-not-done-keywords))
12140 (not (plist-get change-plist :to)))
12141 (throw 'dont-block t))
12142 ;; If this task has checkboxes that are not checked, it's blocked
12144 (org-back-to-heading t)
12145 (let ((beg (point)) end)
12146 (outline-next-heading)
12149 (if (org-list-search-forward
12150 (concat (org-item-beginning-re)
12151 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
12155 (if (boundp 'org-blocked-by-checkboxes)
12156 (setq org-blocked-by-checkboxes t))
12157 (throw 'dont-block nil)))))
12158 t))) ; do not block
12160 (defun org-entry-blocked-p ()
12161 "Is the current entry blocked?"
12162 (with-silent-modifications
12163 (if (org-entry-get nil "NOBLOCKING")
12164 nil ;; Never block this entry
12165 (not (run-hook-with-args-until-failure
12167 (list :type 'todo-state-change
12172 (defun org-update-statistics-cookies (all)
12173 "Update the statistics cookie, either from TODO or from checkboxes.
12174 This should be called with the cursor in a line with a statistics cookie."
12178 (org-update-checkbox-count 'all)
12179 (org-map-entries 'org-update-parent-todo-statistics))
12180 (if (not (org-at-heading-p))
12181 (org-update-checkbox-count)
12182 (let ((pos (point-marker))
12184 (ignore-errors (org-back-to-heading t))
12185 (if (not (org-at-heading-p))
12186 (org-update-checkbox-count)
12187 (setq l1 (org-outline-level))
12188 (setq end (save-excursion
12189 (outline-next-heading)
12190 (if (org-at-heading-p) (setq l2 (org-outline-level)))
12192 (if (and (save-excursion
12194 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
12195 (not (save-excursion (re-search-forward
12196 ":COOKIE_DATA:.*\\<todo\\>" end t))))
12197 (org-update-checkbox-count)
12198 (if (and l2 (> l2 l1))
12201 (org-update-parent-todo-statistics))
12203 (beginning-of-line 1)
12204 (while (re-search-forward
12205 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
12207 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
12209 (move-marker pos nil)))))
12211 (defvar org-entry-property-inherited-from) ;; defined below
12212 (defun org-update-parent-todo-statistics ()
12213 "Update any statistics cookie in the parent of the current headline.
12214 When `org-hierarchical-todo-statistics' is nil, statistics will cover
12215 the entire subtree and this will travel up the hierarchy and update
12216 statistics everywhere."
12217 (let* ((prop (save-excursion (org-up-heading-safe)
12218 (org-entry-get nil "COOKIE_DATA" 'inherit)))
12219 (recursive (or (not org-hierarchical-todo-statistics)
12220 (and prop (string-match "\\<recursive\\>" prop))))
12221 (lim (or (and prop (marker-position org-entry-property-inherited-from))
12224 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
12225 level ltoggle l1 new ndel
12226 (cnt-all 0) (cnt-done 0) is-percent kwd
12227 checkbox-beg ov ovs ove cookie-present)
12230 (beginning-of-line 1)
12231 (setq ltoggle (funcall outline-level))
12232 ;; Three situations are to consider:
12234 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
12235 ;; to the top-level ancestor on the headline;
12237 ;; 2. If parent has "recursive" property, repeat up to the
12238 ;; headline setting that property, taking inheritance into
12241 ;; 3. Else, move up to direct parent and proceed only once.
12242 (while (and (setq level (org-up-heading-safe))
12243 (or recursive first)
12245 (setq first nil cookie-present nil)
12249 (downcase (or (org-entry-get nil "COOKIE_DATA")
12252 (while (re-search-forward box-re (point-at-eol) t)
12253 (setq cnt-all 0 cnt-done 0 cookie-present t)
12254 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
12256 (unless (outline-next-heading) (throw 'exit nil))
12257 (while (and (looking-at org-complex-heading-regexp)
12258 (> (setq l1 (length (match-string 1))) level))
12259 (setq kwd (and (or recursive (= l1 ltoggle))
12261 (if (or (eq org-provide-todo-statistics 'all-headlines)
12262 (and (listp org-provide-todo-statistics)
12263 (or (member kwd org-provide-todo-statistics)
12264 (member kwd org-done-keywords))))
12265 (setq cnt-all (1+ cnt-all))
12266 (if (eq org-provide-todo-statistics t)
12267 (and kwd (setq cnt-all (1+ cnt-all)))))
12268 (and (member kwd org-done-keywords)
12269 (setq cnt-done (1+ cnt-done)))
12270 (outline-next-heading)))
12273 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
12274 (format "[%d/%d]" cnt-done cnt-all))
12275 ndel (- (match-end 0) checkbox-beg))
12276 ;; handle overlays when updating cookie from column view
12277 (when (setq ov (car (overlays-at checkbox-beg)))
12278 (setq ovs (overlay-start ov) ove (overlay-end ov))
12279 (delete-overlay ov))
12280 (goto-char checkbox-beg)
12282 (delete-region (point) (+ (point) ndel))
12283 (when org-auto-align-tags (org-fix-tags-on-the-fly))
12284 (when ov (move-overlay ov ovs ove)))
12285 (when cookie-present
12286 (run-hook-with-args 'org-after-todo-statistics-hook
12287 cnt-done (- cnt-all cnt-done))))))
12288 (run-hooks 'org-todo-statistics-hook)))
12290 (defvar org-after-todo-statistics-hook nil
12291 "Hook that is called after a TODO statistics cookie has been updated.
12292 Each function is called with two arguments: the number of not-done entries
12293 and the number of done entries.
12295 For example, the following function, when added to this hook, will switch
12296 an entry to DONE when all children are done, and back to TODO when new
12297 entries are set to a TODO status. Note that this hook is only called
12298 when there is a statistics cookie in the headline!
12300 (defun org-summary-todo (n-done n-not-done)
12301 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
12302 (let (org-log-done org-log-states) ; turn off logging
12303 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
12306 (defvar org-todo-statistics-hook nil
12307 "Hook that is run whenever Org thinks TODO statistics should be updated.
12308 This hook runs even if there is no statistics cookie present, in which case
12309 `org-after-todo-statistics-hook' would not run.")
12311 (defun org-todo-trigger-tag-changes (state)
12312 "Apply the changes defined in `org-todo-state-tags-triggers'."
12313 (let ((l org-todo-state-tags-triggers)
12315 (when (or (not state) (equal state ""))
12316 (setq changes (append changes (cdr (assoc "" l)))))
12317 (when (and (stringp state) (> (length state) 0))
12318 (setq changes (append changes (cdr (assoc state l)))))
12319 (when (member state org-not-done-keywords)
12320 (setq changes (append changes (cdr (assoc 'todo l)))))
12321 (when (member state org-done-keywords)
12322 (setq changes (append changes (cdr (assoc 'done l)))))
12323 (dolist (c changes)
12324 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
12326 (defun org-local-logging (value)
12327 "Get logging settings from a property VALUE."
12329 ;; directly set the variables, they are already local.
12330 (setq org-log-done nil
12332 org-todo-log-states nil)
12333 (setq words (org-split-string value))
12334 (while (setq w (pop words))
12336 ((setq a (assoc w org-startup-options))
12337 (and (member (nth 1 a) '(org-log-done org-log-repeat))
12338 (set (nth 1 a) (nth 2 a))))
12339 ((setq a (org-extract-log-state-settings w))
12340 (and (member (car a) org-todo-keywords-1)
12341 (push a org-todo-log-states)))))))
12343 (defun org-get-todo-sequence-head (kwd)
12344 "Return the head of the TODO sequence to which KWD belongs.
12345 If KWD is not set, check if there is a text property remembering the
12350 (or (get-text-property (point-at-bol) 'org-todo-head)
12352 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
12353 nil (point-at-eol)))
12354 (get-text-property p 'org-todo-head))))
12355 ((not (member kwd org-todo-keywords-1))
12356 (car org-todo-keywords-1))
12357 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
12359 (defun org-fast-todo-selection ()
12360 "Fast TODO keyword selection with single keys.
12361 Returns the new TODO keyword, or nil if no state change should occur."
12362 (let* ((fulltable org-todo-key-alist)
12363 (done-keywords org-done-keywords) ;; needed for the faces.
12364 (maxlen (apply 'max (mapcar
12366 (if (stringp (car x)) (string-width (car x)) 0))
12369 (fwidth (+ maxlen 3 1 3))
12370 (ncol (/ (- (window-width) 4) fwidth))
12374 (save-window-excursion
12376 (set-buffer (get-buffer-create " *Org todo*"))
12377 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
12379 (org-set-local 'org-done-keywords done-keywords)
12380 (setq tbl fulltable cnt 0)
12381 (while (setq e (pop tbl))
12383 ((equal e '(:startgroup))
12384 (push '() groups) (setq ingroup t)
12385 (when (not (= cnt 0))
12389 ((equal e '(:endgroup))
12390 (setq ingroup nil cnt 0)
12392 ((equal e '(:newline))
12393 (when (not (= cnt 0))
12397 (while (equal (car tbl) '(:newline))
12399 (setq tbl (cdr tbl)))))
12401 (setq tg (car e) c (cdr e))
12402 (if ingroup (push tg (car groups)))
12403 (setq tg (org-add-props tg nil 'face
12404 (org-get-todo-face tg)))
12405 (if (and (= cnt 0) (not ingroup)) (insert " "))
12406 (insert "[" c "] " tg (make-string
12407 (- fwidth 4 (length tg)) ?\ ))
12408 (when (= (setq cnt (1+ cnt)) ncol)
12410 (if ingroup (insert " "))
12413 (goto-char (point-min))
12414 (if (not expert) (org-fit-window-to-buffer))
12415 (message "[a-z..]:Set [SPC]:clear")
12416 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
12419 (and (= c ?q) (not (rassoc c fulltable))))
12420 (setq quit-flag t))
12422 ((setq e (rassoc c fulltable) tg (car e))
12424 (t (setq quit-flag t)))))))
12426 (defun org-entry-is-todo-p ()
12427 (member (org-get-todo-state) org-not-done-keywords))
12429 (defun org-entry-is-done-p ()
12430 (member (org-get-todo-state) org-done-keywords))
12432 (defun org-get-todo-state ()
12434 (org-back-to-heading t)
12435 (and (looking-at org-todo-line-regexp)
12437 (match-string 2))))
12439 (defun org-at-date-range-p (&optional inactive-ok)
12440 "Is the cursor inside a date range?"
12444 (let ((pos (point)))
12445 (skip-chars-backward "^[<\r\n")
12446 (skip-chars-backward "<[")
12447 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12448 (>= (match-end 0) pos)
12450 (skip-chars-backward "^<[\r\n")
12451 (skip-chars-backward "<[")
12452 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12453 (>= (match-end 0) pos)
12457 (defun org-get-repeat (&optional tagline)
12458 "Check if there is a deadline/schedule with repeater in this entry."
12461 (org-back-to-heading t)
12462 (and (re-search-forward (if tagline
12463 (concat tagline "\\s-*" org-repeat-re)
12465 (org-entry-end-position) t)
12466 (match-string-no-properties 1)))))
12468 (defvar org-last-changed-timestamp)
12469 (defvar org-last-inserted-timestamp)
12470 (defvar org-log-post-message)
12471 (defvar org-log-note-purpose)
12472 (defvar org-log-note-how)
12473 (defvar org-log-note-extra)
12474 (defun org-auto-repeat-maybe (done-word)
12475 "Check if the current headline contains a repeated deadline/schedule.
12476 If yes, set TODO state back to what it was and change the base date
12477 of repeating deadline/scheduled time stamps to new date.
12478 This function is run automatically after each state change to a DONE state."
12479 ;; last-state is dynamically scoped into this function
12480 (let* ((repeat (org-get-repeat))
12481 (aa (assoc org-last-state org-todo-kwd-alist))
12482 (interpret (nth 1 aa))
12484 (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
12485 (msg "Entry repeats: ")
12487 (org-todo-log-states nil)
12488 re type n what ts time to-state)
12490 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
12491 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
12492 org-todo-repeat-to-state))
12493 (unless (and to-state (member to-state org-todo-keywords-1))
12494 (setq to-state (if (eq interpret 'type) org-last-state head)))
12495 (org-todo to-state)
12496 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
12497 (org-entry-put nil "LAST_REPEAT" (format-time-string
12498 (org-time-stamp-format t t))))
12499 (when org-log-repeat
12500 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
12501 (memq 'org-add-log-note post-command-hook))
12502 ;; OK, we are already setup for some record
12503 (if (eq org-log-repeat 'note)
12504 ;; make sure we take a note, not only a time stamp
12505 (setq org-log-note-how 'note))
12506 ;; Set up for taking a record
12507 (org-add-log-setup 'state (or done-word (car org-done-keywords))
12509 'findpos org-log-repeat)))
12510 (org-back-to-heading t)
12511 (org-add-planning-info nil nil 'closed)
12512 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12513 org-deadline-time-regexp "\\)\\|\\("
12514 org-ts-regexp "\\)"))
12515 (while (re-search-forward
12516 re (save-excursion (outline-next-heading) (point)) t)
12517 (setq type (if (match-end 1) org-scheduled-string
12518 (if (match-end 3) org-deadline-string "Plain:"))
12519 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
12520 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
12521 (setq n (string-to-number (match-string 2 ts))
12522 what (match-string 3 ts))
12523 (if (equal what "w") (setq n (* n 7) what "d"))
12524 (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
12525 (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
12526 ;; Preparation, see if we need to modify the start date for the change
12527 (when (match-end 1)
12528 (setq time (save-match-data (org-time-string-to-time ts)))
12530 ((equal (match-string 1 ts) ".")
12531 ;; Shift starting date to today
12532 (org-timestamp-change
12533 (- (org-today) (time-to-days time))
12535 ((equal (match-string 1 ts) "+")
12536 (let ((nshiftmax 10) (nshift 0))
12537 (while (or (= nshift 0)
12538 (<= (time-to-days time)
12539 (time-to-days (current-time))))
12540 (when (= (incf nshift) nshiftmax)
12541 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
12543 (org-timestamp-change n (cdr (assoc what whata)))
12544 (org-at-timestamp-p t)
12545 (setq ts (match-string 1))
12546 (setq time (save-match-data (org-time-string-to-time ts)))))
12547 (org-timestamp-change (- n) (cdr (assoc what whata)))
12548 ;; rematch, so that we have everything in place for the real shift
12549 (org-at-timestamp-p t)
12550 (setq ts (match-string 1))
12551 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
12552 (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
12553 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
12554 (setq org-log-post-message msg)
12555 (message "%s" msg))))
12557 (defun org-show-todo-tree (arg)
12558 "Make a compact tree which shows all headlines marked with TODO.
12559 The tree will show the lines where the regexp matches, and all higher
12560 headlines above the match.
12561 With a \\[universal-argument] prefix, prompt for a regexp to match.
12562 With a numeric prefix N, construct a sparse tree for the Nth element
12563 of `org-todo-keywords-1'."
12565 (let ((case-fold-search nil)
12567 (cond ((null arg) org-not-done-regexp)
12569 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
12570 (mapcar 'list org-todo-keywords-1))))
12572 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12574 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12575 (regexp-quote (nth (1- (prefix-numeric-value arg))
12576 org-todo-keywords-1)))
12577 (t (error "Invalid prefix argument: %s" arg)))))
12578 (message "%d TODO entries found"
12579 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
12581 (defun org-deadline (&optional arg time)
12582 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
12583 With one universal prefix argument, remove any deadline from the item.
12584 With two universal prefix arguments, prompt for a warning delay.
12585 With argument TIME, set the deadline at the corresponding date. TIME
12586 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12588 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12589 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12590 'region-start-level 'region))
12591 org-loop-over-headlines-in-active-region)
12593 `(org-deadline ',arg ,time)
12594 org-loop-over-headlines-in-active-region
12595 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12596 (let* ((old-date (org-entry-get nil "DEADLINE"))
12597 (repeater (and old-date
12599 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12601 (match-string 1 old-date))))
12604 (when (and old-date org-log-redeadline)
12605 (org-add-log-setup 'deldeadline nil old-date 'findpos
12606 org-log-redeadline))
12607 (org-remove-timestamp-with-keyword org-deadline-string)
12608 (message "Item no longer has a deadline."))
12611 (if (re-search-forward
12612 org-deadline-time-regexp
12613 (save-excursion (outline-next-heading) (point)) t)
12614 (let* ((rpl0 (match-string 1))
12615 (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
12617 (concat org-deadline-string
12619 (format " -%dd" (abs
12622 (org-read-date nil t nil "Warn starting from")))
12623 (time-to-days nil))))
12625 (user-error "No deadline information to update"))))
12627 (org-add-planning-info 'deadline time 'closed)
12628 (when (and old-date org-log-redeadline
12629 (not (equal old-date
12630 (substring org-last-inserted-timestamp 1 -1))))
12631 (org-add-log-setup 'redeadline nil old-date 'findpos
12632 org-log-redeadline))
12635 (org-back-to-heading t)
12636 (when (re-search-forward (concat org-deadline-string " "
12637 org-last-inserted-timestamp)
12639 (outline-next-heading) (point)) t)
12640 (goto-char (1- (match-end 0)))
12641 (insert " " repeater)
12642 (setq org-last-inserted-timestamp
12643 (concat (substring org-last-inserted-timestamp 0 -1)
12645 (substring org-last-inserted-timestamp -1))))))
12646 (message "Deadline on %s" org-last-inserted-timestamp))))))
12648 (defun org-schedule (&optional arg time)
12649 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
12650 With one universal prefix argument, remove any scheduling date from the item.
12651 With two universal prefix arguments, prompt for a delay cookie.
12652 With argument TIME, scheduled at the corresponding date. TIME can
12653 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12655 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12656 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12657 'region-start-level 'region))
12658 org-loop-over-headlines-in-active-region)
12660 `(org-schedule ',arg ,time)
12661 org-loop-over-headlines-in-active-region
12662 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12663 (let* ((old-date (org-entry-get nil "SCHEDULED"))
12664 (repeater (and old-date
12666 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12668 (match-string 1 old-date))))
12672 (when (and old-date org-log-reschedule)
12673 (org-add-log-setup 'delschedule nil old-date 'findpos
12674 org-log-reschedule))
12675 (org-remove-timestamp-with-keyword org-scheduled-string)
12676 (message "Item is no longer scheduled.")))
12679 (if (re-search-forward
12680 org-scheduled-time-regexp
12681 (save-excursion (outline-next-heading) (point)) t)
12682 (let* ((rpl0 (match-string 1))
12683 (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
12685 (concat org-scheduled-string
12687 (format " -%dd" (abs
12690 (org-read-date nil t nil "Delay until")))
12691 (time-to-days nil))))
12693 (user-error "No scheduled information to update"))))
12695 (org-add-planning-info 'scheduled time 'closed)
12696 (when (and old-date org-log-reschedule
12697 (not (equal old-date
12698 (substring org-last-inserted-timestamp 1 -1))))
12699 (org-add-log-setup 'reschedule nil old-date 'findpos
12700 org-log-reschedule))
12703 (org-back-to-heading t)
12704 (when (re-search-forward (concat org-scheduled-string " "
12705 org-last-inserted-timestamp)
12707 (outline-next-heading) (point)) t)
12708 (goto-char (1- (match-end 0)))
12709 (insert " " repeater)
12710 (setq org-last-inserted-timestamp
12711 (concat (substring org-last-inserted-timestamp 0 -1)
12713 (substring org-last-inserted-timestamp -1))))))
12714 (message "Scheduled to %s" org-last-inserted-timestamp))))))
12716 (defun org-get-scheduled-time (pom &optional inherit)
12717 "Get the scheduled time as a time tuple, of a format suitable
12718 for calling org-schedule with, or if there is no scheduling,
12720 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
12722 (apply 'encode-time (org-parse-time-string time)))))
12724 (defun org-get-deadline-time (pom &optional inherit)
12725 "Get the deadline as a time tuple, of a format suitable for
12726 calling org-deadline with, or if there is no scheduling, returns
12728 (let ((time (org-entry-get pom "DEADLINE" inherit)))
12730 (apply 'encode-time (org-parse-time-string time)))))
12732 (defun org-remove-timestamp-with-keyword (keyword)
12733 "Remove all time stamps with KEYWORD in the current entry."
12734 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
12737 (org-back-to-heading t)
12739 (outline-next-heading)
12740 (while (re-search-backward re beg t)
12742 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
12743 (equal (char-before) ?\ ))
12744 (backward-delete-char 1)
12745 (if (string-match "^[ \t]*$" (buffer-substring
12746 (point-at-bol) (point-at-eol)))
12747 (delete-region (point-at-bol)
12748 (min (point-max) (1+ (point-at-eol))))))))))
12750 (defun org-add-planning-info (what &optional time &rest remove)
12751 "Insert new timestamp with keyword in the line directly after the headline.
12752 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
12753 If non is given, the user is prompted for a date.
12754 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
12757 (let (org-time-was-given org-end-time-was-given ts
12758 end default-time default-input)
12761 (when (and (memq what '(scheduled deadline))
12763 (and (stringp time)
12764 (string-match "^[-+]+[0-9]" time))))
12765 ;; Try to get a default date/time from existing timestamp
12767 (org-back-to-heading t)
12768 (setq end (save-excursion (outline-next-heading) (point)))
12769 (when (re-search-forward (if (eq what 'scheduled)
12770 org-scheduled-time-regexp
12771 org-deadline-time-regexp)
12773 (setq ts (match-string 1)
12775 (apply 'encode-time (org-parse-time-string ts))
12776 default-input (and ts (org-get-compact-tod ts))))))
12780 ;; This is a string (relative or absolute), set proper date
12781 (apply 'encode-time
12782 (org-read-date-analyze
12783 time default-time (decode-time default-time)))
12784 ;; If necessary, get the time from the user
12785 (or time (org-read-date nil 'to-time nil nil
12786 default-time default-input)))))
12788 (when (and org-insert-labeled-timestamps-at-point
12789 (member what '(scheduled deadline)))
12791 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
12792 (org-insert-time-stamp time org-time-was-given
12793 nil nil nil (list org-end-time-was-given))
12797 (let (col list elt ts buffer-invisibility-spec)
12798 (org-back-to-heading t)
12799 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
12800 (goto-char (match-end 1))
12801 (setq col (current-column))
12802 (goto-char (match-end 0))
12803 (if (eobp) (insert "\n") (forward-char 1))
12804 (when (and (not what)
12807 org-keyword-time-not-clock-regexp))))
12808 ;; Nothing to add, nothing to remove...... :-)
12810 (if (and (not (looking-at org-outline-regexp))
12811 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
12813 (not (equal (match-string 1) org-clock-string)))
12814 (narrow-to-region (match-beginning 0) (match-end 0))
12815 (insert-before-markers "\n")
12817 (narrow-to-region (point) (point))
12818 (and org-adapt-indentation (org-indent-to-column col)))
12819 ;; Check if we have to remove something.
12820 (setq list (cons what remove))
12822 (setq elt (pop list))
12823 (when (or (and (eq elt 'scheduled)
12824 (re-search-forward org-scheduled-time-regexp nil t))
12825 (and (eq elt 'deadline)
12826 (re-search-forward org-deadline-time-regexp nil t))
12827 (and (eq elt 'closed)
12828 (re-search-forward org-closed-time-regexp nil t)))
12830 (if (looking-at "--+<[^>]+>") (replace-match ""))))
12831 (and (looking-at "[ \t]+") (replace-match ""))
12832 (and org-adapt-indentation (bolp) (org-indent-to-column col))
12835 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
12836 (cond ((eq what 'scheduled) org-scheduled-string)
12837 ((eq what 'deadline) org-deadline-string)
12838 ((eq what 'closed) org-closed-string))
12840 (setq ts (org-insert-time-stamp
12842 (or org-time-was-given
12843 (and (eq what 'closed) org-log-done-with-time))
12845 nil nil (list org-end-time-was-given)))
12847 (if (not (or (bolp) (eq (char-before) ?\ )
12848 (memq (char-after) '(32 10))
12851 (goto-char (point-min))
12853 (if (and (looking-at "[ \t]*\n")
12854 (equal (char-before) ?\n))
12855 (delete-region (1- (point)) (point-at-eol)))
12858 (defvar org-log-note-marker (make-marker))
12859 (defvar org-log-note-purpose nil)
12860 (defvar org-log-note-state nil)
12861 (defvar org-log-note-previous-state nil)
12862 (defvar org-log-note-how nil)
12863 (defvar org-log-note-extra nil)
12864 (defvar org-log-note-window-configuration nil)
12865 (defvar org-log-note-return-to (make-marker))
12866 (defvar org-log-note-effective-time nil
12867 "Remembered current time so that dynamically scoped
12868 `org-extend-today-until' affects tha timestamps in state change
12871 (defvar org-log-post-message nil
12872 "Message to be displayed after a log note has been stored.
12873 The auto-repeater uses this.")
12875 (defun org-add-note ()
12876 "Add a note to the current entry.
12877 This is done in the same way as adding a state change note."
12879 (org-add-log-setup 'note nil nil 'findpos nil))
12881 (defvar org-property-end-re)
12882 (defun org-add-log-setup (&optional purpose state prev-state
12884 "Set up the post command hook to take a note.
12885 If this is about to TODO state change, the new state is expected in STATE.
12886 When FINDPOS is non-nil, find the correct position for the note in
12887 the current entry. If not, assume that it can be inserted at point.
12888 HOW is an indicator what kind of note should be created.
12889 EXTRA is additional text that will be inserted into the notes buffer."
12890 (let* ((org-log-into-drawer (org-log-into-drawer))
12891 (drawer (cond ((stringp org-log-into-drawer)
12892 org-log-into-drawer)
12893 (org-log-into-drawer "LOGBOOK"))))
12897 (org-back-to-heading t)
12898 (narrow-to-region (point) (save-excursion
12899 (outline-next-heading) (point)))
12900 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
12901 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
12903 (goto-char (match-end 0))
12906 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
12909 (goto-char (match-end 0))
12910 (or org-log-states-order-reversed
12911 (and (re-search-forward org-property-end-re nil t)
12912 (goto-char (1- (match-beginning 0))))))
12913 (insert "\n:" drawer ":\n:END:")
12914 (beginning-of-line 0)
12916 (beginning-of-line 2)
12919 ((and org-log-state-notes-insert-after-drawers
12921 (forward-line) (looking-at org-drawer-regexp)))
12923 (while (looking-at org-drawer-regexp)
12924 (goto-char (match-end 0))
12925 (re-search-forward org-property-end-re (point-max) t)
12927 (forward-line -1)))
12928 (unless org-log-states-order-reversed
12929 (and (= (char-after) ?\n) (forward-char 1))
12930 (org-skip-over-state-notes)
12931 (skip-chars-backward " \t\n\r")))
12932 (move-marker org-log-note-marker (point))
12933 (setq org-log-note-purpose purpose
12934 org-log-note-state state
12935 org-log-note-previous-state prev-state
12936 org-log-note-how how
12937 org-log-note-extra extra
12938 org-log-note-effective-time (org-current-effective-time))
12939 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
12941 (defun org-skip-over-state-notes ()
12942 "Skip past the list of State notes in an entry."
12943 (if (looking-at "\n[ \t]*- State") (forward-char 1))
12944 (when (ignore-errors (goto-char (org-in-item-p)))
12945 (let* ((struct (org-list-struct))
12946 (prevs (org-list-prevs-alist struct)))
12947 (while (looking-at "[ \t]*- State")
12948 (goto-char (or (org-list-get-next-item (point) struct prevs)
12949 (org-list-get-item-end (point) struct)))))))
12951 (defun org-add-log-note (&optional purpose)
12952 "Pop up a window for taking a note, and add this note later at point."
12953 (remove-hook 'post-command-hook 'org-add-log-note)
12954 (setq org-log-note-window-configuration (current-window-configuration))
12955 (delete-other-windows)
12956 (move-marker org-log-note-return-to (point))
12957 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
12958 (goto-char org-log-note-marker)
12959 (org-switch-to-buffer-other-window "*Org Note*")
12961 (if (memq org-log-note-how '(time state))
12962 (let (current-prefix-arg) (org-store-log-note))
12963 (let ((org-inhibit-startup t)) (org-mode))
12964 (insert (format "# Insert note for %s.
12965 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
12967 ((eq org-log-note-purpose 'clock-out) "stopped clock")
12968 ((eq org-log-note-purpose 'done) "closed todo item")
12969 ((eq org-log-note-purpose 'state)
12970 (format "state change from \"%s\" to \"%s\""
12971 (or org-log-note-previous-state "")
12972 (or org-log-note-state "")))
12973 ((eq org-log-note-purpose 'reschedule)
12975 ((eq org-log-note-purpose 'delschedule)
12976 "no longer scheduled")
12977 ((eq org-log-note-purpose 'redeadline)
12978 "changing deadline")
12979 ((eq org-log-note-purpose 'deldeadline)
12980 "removing deadline")
12981 ((eq org-log-note-purpose 'refile)
12983 ((eq org-log-note-purpose 'note)
12985 (t (error "This should not happen")))))
12986 (if org-log-note-extra (insert org-log-note-extra))
12987 (org-set-local 'org-finish-function 'org-store-log-note)
12988 (run-hooks 'org-log-buffer-setup-hook)))
12990 (defvar org-note-abort nil) ; dynamically scoped
12991 (defun org-store-log-note ()
12992 "Finish taking a log note, and insert it to where it belongs."
12993 (let ((txt (buffer-string)))
12994 (kill-buffer (current-buffer))
12995 (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
12997 (while (string-match "\\`# .*\n[ \t\n]*" txt)
12998 (setq txt (replace-match "" t t txt)))
12999 (if (string-match "\\s-+\\'" txt)
13000 (setq txt (replace-match "" t t txt)))
13001 (setq lines (org-split-string txt "\n"))
13002 (when (and note (string-match "\\S-" note))
13004 (org-replace-escapes
13006 (list (cons "%u" (user-login-name))
13007 (cons "%U" user-full-name)
13008 (cons "%t" (format-time-string
13009 (org-time-stamp-format 'long 'inactive)
13010 org-log-note-effective-time))
13011 (cons "%T" (format-time-string
13012 (org-time-stamp-format 'long nil)
13013 org-log-note-effective-time))
13014 (cons "%d" (format-time-string
13015 (org-time-stamp-format nil 'inactive)
13016 org-log-note-effective-time))
13017 (cons "%D" (format-time-string
13018 (org-time-stamp-format nil nil)
13019 org-log-note-effective-time))
13020 (cons "%s" (if org-log-note-state
13021 (concat "\"" org-log-note-state "\"")
13023 (cons "%S" (if org-log-note-previous-state
13024 (concat "\"" org-log-note-previous-state "\"")
13026 (if lines (setq note (concat note " \\\\")))
13028 (when (or current-prefix-arg org-note-abort)
13029 (when org-log-into-drawer
13030 (org-remove-empty-drawer-at
13031 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
13032 org-log-note-marker))
13035 (with-current-buffer (marker-buffer org-log-note-marker)
13037 (goto-char org-log-note-marker)
13038 (move-marker org-log-note-marker nil)
13040 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
13041 (setq ind (save-excursion
13042 (if (ignore-errors (goto-char (org-in-item-p)))
13043 (let ((struct (org-list-struct)))
13045 (org-list-get-top-point struct) struct))
13046 (skip-chars-backward " \r\t\n")
13048 ((and (org-at-heading-p)
13049 org-adapt-indentation)
13050 (1+ (org-current-level)))
13051 ((org-at-heading-p) 0)
13052 (t (org-get-indentation))))))
13053 (setq bul (org-list-bullet-string "-"))
13054 (org-indent-line-to ind)
13055 (insert bul (pop lines))
13056 (let ((ind-body (+ (length bul) ind)))
13059 (org-indent-line-to ind-body)
13060 (insert (pop lines))))
13061 (message "Note stored")
13062 (org-back-to-heading t)
13063 (org-cycle-hide-drawers 'children))))))
13064 (set-window-configuration org-log-note-window-configuration)
13065 (with-current-buffer (marker-buffer org-log-note-return-to)
13066 (goto-char org-log-note-return-to))
13067 (move-marker org-log-note-return-to nil)
13068 (and org-log-post-message (message "%s" org-log-post-message)))
13070 (defun org-remove-empty-drawer-at (drawer pos)
13071 "Remove an empty drawer DRAWER at position POS.
13072 POS may also be a marker."
13073 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
13079 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
13080 (replace-match ""))))))
13082 (defvar org-ts-type nil)
13083 (defun org-sparse-tree (&optional arg type)
13084 "Create a sparse tree, prompt for the details.
13085 This command can create sparse trees. You first need to select the type
13086 of match used to create the tree:
13088 t Show all TODO entries.
13089 T Show entries with a specific TODO keyword.
13090 m Show entries selected by a tags/property match.
13091 p Enter a property name and its value (both with completion on existing
13092 names/values) and show entries with that property.
13093 r Show entries matching a regular expression (`/' can be used as well).
13094 b Show deadlines and scheduled items before a date.
13095 a Show deadlines and scheduled items after a date.
13096 d Show deadlines due within `org-deadline-warning-days'.
13097 D Show deadlines and scheduled items between a date range."
13099 (let (ans kwd value ts-type)
13100 (setq type (or type org-sparse-tree-default-date-type))
13101 (setq org-ts-type type)
13102 (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"
13103 (cond ((eq type 'all) "all timestamps")
13104 ((eq type 'scheduled) "only scheduled")
13105 ((eq type 'deadline) "only deadline")
13106 ((eq type 'active) "only active timestamps")
13107 ((eq type 'inactive) "only inactive timestamps")
13108 ((eq type 'scheduled-or-deadline) "scheduled/deadline")
13109 (t "scheduled/deadline")))
13110 (setq ans (read-char-exclusive))
13113 (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
13115 (call-interactively 'org-check-deadlines))
13117 (call-interactively 'org-check-before-date))
13119 (call-interactively 'org-check-after-date))
13121 (call-interactively 'org-check-dates-range))
13123 (call-interactively 'org-show-todo-tree))
13125 (org-show-todo-tree '(4)))
13126 ((member ans '(?T ?m))
13127 (call-interactively 'org-match-sparse-tree))
13128 ((member ans '(?p ?P))
13129 (setq kwd (org-icompleting-read "Property: "
13130 (mapcar 'list (org-buffer-property-keys))))
13131 (setq value (org-icompleting-read "Value: "
13132 (mapcar 'list (org-property-values kwd))))
13133 (unless (string-match "\\`{.*}\\'" value)
13134 (setq value (concat "\"" value "\"")))
13135 (org-match-sparse-tree arg (concat kwd "=" value)))
13136 ((member ans '(?r ?R ?/))
13137 (call-interactively 'org-occur))
13138 (t (error "No such sparse tree command \"%c\"" ans)))))
13140 (defvar org-occur-highlights nil
13141 "List of overlays used for occur matches.")
13142 (make-variable-buffer-local 'org-occur-highlights)
13143 (defvar org-occur-parameters nil
13144 "Parameters of the active org-occur calls.
13145 This is a list, each call to org-occur pushes as cons cell,
13146 containing the regular expression and the callback, onto the list.
13147 The list can contain several entries if `org-occur' has been called
13148 several time with the KEEP-PREVIOUS argument. Otherwise, this list
13149 will only contain one set of parameters. When the highlights are
13150 removed (for example with `C-c C-c', or with the next edit (depending
13151 on `org-remove-highlights-with-change'), this variable is emptied
13153 (make-variable-buffer-local 'org-occur-parameters)
13155 (defun org-occur (regexp &optional keep-previous callback)
13156 "Make a compact tree which shows all matches of REGEXP.
13157 The tree will show the lines where the regexp matches, and all higher
13158 headlines above the match. It will also show the heading after the match,
13159 to make sure editing the matching entry is easy.
13160 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
13161 call to `org-occur' will be kept, to allow stacking of calls to this
13163 If CALLBACK is non-nil, it is a function which is called to confirm
13164 that the match should indeed be shown."
13165 (interactive "sRegexp: \nP")
13166 (when (equal regexp "")
13167 (error "Regexp cannot be empty"))
13168 (unless keep-previous
13169 (org-remove-occur-highlights nil nil t))
13170 (push (cons regexp callback) org-occur-parameters)
13173 (goto-char (point-min))
13174 (if (or (not keep-previous) ; do not want to keep
13175 (not org-occur-highlights)) ; no previous matches
13178 (while (re-search-forward regexp nil t)
13179 (when (or (not callback)
13180 (save-match-data (funcall callback)))
13181 (setq cnt (1+ cnt))
13182 (when org-highlight-sparse-tree-matches
13183 (org-highlight-new-match (match-beginning 0) (match-end 0)))
13184 (org-show-context 'occur-tree))))
13185 (when org-remove-highlights-with-change
13186 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
13188 (unless org-sparse-tree-open-archived-trees
13189 (org-hide-archived-subtrees (point-min) (point-max)))
13190 (run-hooks 'org-occur-hook)
13191 (if (org-called-interactively-p 'interactive)
13192 (message "%d match(es) for regexp %s" cnt regexp))
13195 (defun org-occur-next-match (&optional n reset)
13196 "Function for `next-error-function' to find sparse tree matches.
13197 N is the number of matches to move, when negative move backwards.
13198 RESET is entirely ignored - this function always goes back to the
13199 starting point when no match is found."
13200 (let* ((limit (if (< n 0) (point-min) (point-max)))
13201 (search-func (if (< n 0)
13202 'previous-single-char-property-change
13203 'next-single-char-property-change))
13208 (while (setq p1 (funcall search-func (point) 'org-type))
13209 (when (equal p1 limit)
13211 (error "No more matches"))
13212 (when (equal (get-char-property p1 'org-type) 'org-occur)
13216 (throw 'exit (point))))
13219 (error "No more matches"))))
13221 (defun org-show-context (&optional key)
13222 "Make sure point and context are visible.
13223 How much context is shown depends upon the variables
13224 `org-show-hierarchy-above', `org-show-following-heading',
13225 `org-show-entry-below' and `org-show-siblings'."
13226 (let ((heading-p (org-at-heading-p t))
13227 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
13228 (following-p (org-get-alist-option org-show-following-heading key))
13229 (entry-p (org-get-alist-option org-show-entry-below key))
13230 (siblings-p (org-get-alist-option org-show-siblings key)))
13232 ;; Show heading or entry text
13233 (if (and heading-p (not entry-p))
13234 (org-flag-heading nil) ; only show the heading
13235 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
13236 (org-show-hidden-entry))) ; show entire entry
13238 ;; Show next sibling, or heading below text
13240 (and (if heading-p (org-goto-sibling) (outline-next-heading))
13241 (org-flag-heading nil))))
13242 (when siblings-p (org-show-siblings))
13244 ;; show all higher headings, possibly with siblings
13246 (while (and (condition-case nil
13247 (progn (org-up-heading-all 1) t)
13250 (org-flag-heading nil)
13251 (when siblings-p (org-show-siblings))))))))
13253 (defvar org-reveal-start-hook nil
13254 "Hook run before revealing a location.")
13256 (defun org-reveal (&optional siblings)
13257 "Show current entry, hierarchy above it, and the following headline.
13258 This can be used to show a consistent set of context around locations
13259 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
13260 not t for the search context.
13262 With optional argument SIBLINGS, on each level of the hierarchy all
13263 siblings are shown. This repairs the tree structure to what it would
13264 look like when opened with hierarchical calls to `org-cycle'.
13265 With double optional argument \\[universal-argument] \\[universal-argument], \
13266 go to the parent and show the
13269 (run-hooks 'org-reveal-start-hook)
13270 (let ((org-show-hierarchy-above t)
13271 (org-show-following-heading t)
13272 (org-show-siblings (if siblings t org-show-siblings)))
13273 (org-show-context nil))
13274 (when (equal siblings '(16))
13276 (when (org-up-heading-safe)
13278 (run-hook-with-args 'org-cycle-hook 'subtree)))))
13280 (defun org-highlight-new-match (beg end)
13281 "Highlight from BEG to END and mark the highlight is an occur headline."
13282 (let ((ov (make-overlay beg end)))
13283 (overlay-put ov 'face 'secondary-selection)
13284 (overlay-put ov 'org-type 'org-occur)
13285 (push ov org-occur-highlights)))
13287 (defun org-remove-occur-highlights (&optional beg end noremove)
13288 "Remove the occur highlights from the buffer.
13289 BEG and END are ignored. If NOREMOVE is nil, remove this function
13290 from the `before-change-functions' in the current buffer."
13292 (unless org-inhibit-highlight-removal
13293 (mapc 'delete-overlay org-occur-highlights)
13294 (setq org-occur-highlights nil)
13295 (setq org-occur-parameters nil)
13297 (remove-hook 'before-change-functions
13298 'org-remove-occur-highlights 'local))))
13302 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
13303 "Regular expression matching the priority indicator.")
13305 (defvar org-remove-priority-next-time nil)
13307 (defun org-priority-up ()
13308 "Increase the priority of the current item."
13310 (org-priority 'up))
13312 (defun org-priority-down ()
13313 "Decrease the priority of the current item."
13315 (org-priority 'down))
13317 (defun org-priority (&optional action show)
13318 "Change the priority of an item.
13319 ACTION can be `set', `up', `down', or a character."
13321 (if (equal action '(4))
13322 (org-show-priority)
13323 (unless org-enable-priority-commands
13324 (error "Priority commands are disabled"))
13325 (setq action (or action 'set))
13326 (let (current new news have remove)
13328 (org-back-to-heading t)
13329 (if (looking-at org-priority-regexp)
13330 (setq current (string-to-char (match-string 2))
13333 ((eq action 'remove)
13334 (setq remove t new ?\ ))
13335 ((or (eq action 'set)
13336 (if (featurep 'xemacs) (characterp action) (integerp action)))
13337 (if (not (eq action 'set))
13339 (message "Priority %c-%c, SPC to remove: "
13340 org-highest-priority org-lowest-priority)
13342 (setq new (read-char-exclusive))))
13343 (if (and (= (upcase org-highest-priority) org-highest-priority)
13344 (= (upcase org-lowest-priority) org-lowest-priority))
13345 (setq new (upcase new)))
13346 (cond ((equal new ?\ ) (setq remove t))
13347 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
13348 (error "Priority must be between `%c' and `%c'"
13349 org-highest-priority org-lowest-priority))))
13352 (1- current) ; normal cycling
13353 ;; last priority was empty
13354 (if (eq last-command this-command)
13355 org-lowest-priority ; wrap around empty to lowest
13357 (if org-priority-start-cycle-with-default
13358 org-default-priority
13359 (1- org-default-priority))))))
13362 (1+ current) ; normal cycling
13363 ;; last priority was empty
13364 (if (eq last-command this-command)
13365 org-highest-priority ; wrap around empty to highest
13367 (if org-priority-start-cycle-with-default
13368 org-default-priority
13369 (1+ org-default-priority))))))
13370 (t (error "Invalid action")))
13371 (if (or (< (upcase new) org-highest-priority)
13372 (> (upcase new) org-lowest-priority))
13373 (if (and (memq action '(up down))
13374 (not have) (not (eq last-command this-command)))
13375 ;; `new' is from default priority
13377 "The default can not be set, see `org-default-priority' why")
13378 ;; normal cycling: `new' is beyond highest/lowest priority
13379 ;; and is wrapped around to the empty priority
13381 (setq news (format "%c" new))
13384 (replace-match "" t t nil 1)
13385 (replace-match news t t nil 2))
13387 (error "No priority cookie found in line")
13388 (let ((case-fold-search nil))
13389 (looking-at org-todo-line-regexp))
13392 (goto-char (match-end 2))
13393 (insert " [#" news "]"))
13394 (goto-char (match-beginning 3))
13395 (insert "[#" news "] "))))
13396 (org-preserve-lc (org-set-tags nil 'align)))
13398 (message "Priority removed")
13399 (message "Priority of current item set to %s" news)))))
13401 (defun org-show-priority ()
13402 "Show the priority of the current item.
13403 This priority is composed of the main priority given with the [#A] cookies,
13404 and by additional input from the age of a schedules or deadline entry."
13406 (let ((pri (if (eq major-mode 'org-agenda-mode)
13407 (org-get-at-bol 'priority)
13410 (beginning-of-line)
13411 (and (looking-at org-heading-regexp)
13412 (org-get-priority (match-string 0))))))))
13413 (message "Priority is %d" (if pri pri -1000))))
13415 (defun org-get-priority (s)
13416 "Find priority cookie and return priority."
13418 (if (functionp org-get-priority-function)
13419 (funcall org-get-priority-function)
13420 (if (not (string-match org-priority-regexp s))
13421 (* 1000 (- org-lowest-priority org-default-priority))
13422 (* 1000 (- org-lowest-priority
13423 (string-to-char (match-string 2 s))))))))
13427 (defvar org-agenda-archives-mode)
13428 (defvar org-map-continue-from nil
13429 "Position from where mapping should continue.
13430 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
13432 (defvar org-scanner-tags nil
13433 "The current tag list while the tags scanner is running.")
13434 (defvar org-trust-scanner-tags nil
13435 "Should `org-get-tags-at' use the tags for the scanner.
13436 This is for internal dynamical scoping only.
13437 When this is non-nil, the function `org-get-tags-at' will return the value
13438 of `org-scanner-tags' instead of building the list by itself. This
13439 can lead to large speed-ups when the tags scanner is used in a file with
13440 many entries, and when the list of tags is retrieved, for example to
13441 obtain a list of properties. Building the tags list for each entry in such
13442 a file becomes an N^2 operation - but with this variable set, it scales
13445 (defun org-scan-tags (action matcher todo-only &optional start-level)
13446 "Sca headline tags with inheritance and produce output ACTION.
13448 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
13449 or `agenda' to produce an entry list for an agenda view. It can also be
13450 a Lisp form or a function that should be called at each matched headline, in
13451 this case the return value is a list of all return values from these calls.
13453 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
13454 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
13455 only lines with a not-done TODO keyword are included in the output.
13456 This should be the same variable that was scoped into
13457 and set by `org-make-tags-matcher' when it constructed MATCHER.
13459 START-LEVEL can be a string with asterisks, reducing the scope to
13460 headlines matching this string."
13461 (require 'org-agenda)
13462 (let* ((re (concat "^"
13464 ;; Get the correct level to match
13465 (concat "\\*\\{" (number-to-string start-level) "\\} ")
13466 org-outline-regexp)
13468 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
13469 (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
13470 (props (list 'face 'default
13471 'done-face 'org-agenda-done
13472 'undone-face 'default
13473 'mouse-face 'highlight
13474 'org-not-done-regexp org-not-done-regexp
13475 'org-todo-regexp org-todo-regexp
13476 'org-complex-heading-regexp org-complex-heading-regexp
13478 (format "mouse-2 or RET jump to org file %s"
13479 (abbreviate-file-name
13480 (or (buffer-file-name (buffer-base-buffer))
13481 (buffer-name (buffer-base-buffer)))))))
13482 (case-fold-search nil)
13483 (org-map-continue-from nil)
13484 lspos tags tags-list
13485 (tags-alist (list (cons 0 org-file-tags)))
13486 (llast 0) rtn rtn1 level category i txt
13487 todo marker entry priority)
13488 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
13489 (setq action (list 'lambda nil action)))
13491 (goto-char (point-min))
13492 (when (eq action 'sparse-tree)
13494 (org-remove-occur-highlights))
13495 (while (re-search-forward re nil t)
13496 (setq org-map-continue-from nil)
13498 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
13499 tags (if (match-end 4) (org-match-string-no-properties 4)))
13500 (goto-char (setq lspos (match-beginning 0)))
13501 (setq level (org-reduced-level (org-outline-level))
13502 category (org-get-category))
13503 (setq i llast llast level)
13504 ;; remove tag lists from same and sublevels
13505 (while (>= i level)
13506 (when (setq entry (assoc i tags-alist))
13507 (setq tags-alist (delete entry tags-alist)))
13509 ;; add the next tags
13511 (setq tags (org-split-string tags ":")
13513 (cons (cons level tags) tags-alist)))
13514 ;; compile tags for current headline
13516 (if org-use-tag-inheritance
13517 (apply 'append (mapcar 'cdr (reverse tags-alist)))
13519 org-scanner-tags tags-list)
13520 (when org-use-tag-inheritance
13521 (setcdr (car tags-alist)
13522 (mapcar (lambda (x)
13523 (setq x (copy-sequence x))
13524 (org-add-prop-inherited x))
13525 (cdar tags-alist))))
13526 (when (and tags org-use-tag-inheritance
13527 (or (not (eq t org-use-tag-inheritance))
13528 org-tags-exclude-from-inheritance))
13529 ;; selective inheritance, remove uninherited ones
13530 (setcdr (car tags-alist)
13531 (org-remove-uninherited-tags (cdar tags-alist))))
13534 ;; eval matcher only when the todo condition is OK
13535 (and (or (not todo-only) (member todo org-not-done-keywords))
13536 (let ((case-fold-search t) (org-trust-scanner-tags t))
13539 ;; Call the skipper, but return t if it does not skip,
13540 ;; so that the `and' form continues evaluating
13542 (unless (eq action 'sparse-tree) (org-agenda-skip))
13545 ;; Check if timestamps are deselecting this entry
13546 (or (not todo-only)
13547 (and (member todo org-not-done-keywords)
13548 (or (not org-agenda-tags-todo-honor-ignore-options)
13549 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
13551 ;; select this headline
13553 ((eq action 'sparse-tree)
13554 (and org-highlight-sparse-tree-matches
13555 (org-get-heading) (match-end 0)
13556 (org-highlight-new-match
13557 (match-beginning 1) (match-end 1)))
13558 (org-show-context 'tags-tree))
13559 ((eq action 'agenda)
13560 (setq txt (org-agenda-format-item
13563 (if (eq org-tags-match-list-sublevels 'indented)
13564 (make-string (1- level) ?.) "")
13568 priority (org-get-priority txt))
13570 (setq marker (org-agenda-new-marker))
13571 (org-add-props txt props
13572 'org-marker marker 'org-hd-marker marker 'org-category category
13574 'priority priority 'type "tagsmatch")
13576 ((functionp action)
13577 (setq org-map-continue-from nil)
13579 (setq rtn1 (funcall action))
13581 (t (error "Invalid action")))
13583 ;; if we are to skip sublevels, jump to end of subtree
13584 (unless org-tags-match-list-sublevels
13585 (org-end-of-subtree t)
13586 (backward-char 1))))
13587 ;; Get the correct position from where to continue
13588 (if org-map-continue-from
13589 (goto-char org-map-continue-from)
13590 (and (= (point) lspos) (end-of-line 1)))))
13591 (when (and (eq action 'sparse-tree)
13592 (not org-sparse-tree-open-archived-trees))
13593 (org-hide-archived-subtrees (point-min) (point-max)))
13596 (defun org-remove-uninherited-tags (tags)
13597 "Remove all tags that are not inherited from the list TAGS."
13599 ((eq org-use-tag-inheritance t)
13600 (if org-tags-exclude-from-inheritance
13601 (org-delete-all org-tags-exclude-from-inheritance tags)
13603 ((not org-use-tag-inheritance) nil)
13604 ((stringp org-use-tag-inheritance)
13607 (if (and (string-match org-use-tag-inheritance x)
13608 (not (member x org-tags-exclude-from-inheritance)))
13611 ((listp org-use-tag-inheritance)
13614 (if (member x org-use-tag-inheritance) x nil))
13617 (defun org-match-sparse-tree (&optional todo-only match)
13618 "Create a sparse tree according to tags string MATCH.
13619 MATCH can contain positive and negative selection of tags, like
13620 \"+WORK+URGENT-WITHBOSS\".
13621 If optional argument TODO-ONLY is non-nil, only select lines that are
13624 (org-agenda-prepare-buffers (list (current-buffer)))
13625 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
13627 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
13629 (defvar org-cached-props nil)
13630 (defun org-cached-entry-get (pom property)
13631 (if (or (eq t org-use-property-inheritance)
13632 (and (stringp org-use-property-inheritance)
13633 (string-match org-use-property-inheritance property))
13634 (and (listp org-use-property-inheritance)
13635 (member property org-use-property-inheritance)))
13636 ;; Caching is not possible, check it directly
13637 (org-entry-get pom property 'inherit)
13638 ;; Get all properties, so that we can do complicated checks easily
13639 (cdr (assoc property (or org-cached-props
13640 (setq org-cached-props
13641 (org-entry-properties pom)))))))
13643 (defun org-global-tags-completion-table (&optional files)
13644 "Return the list of all tags in all agenda buffer/files.
13645 Optional FILES argument is a list of files which can be used
13646 instead of the agenda files."
13653 (set-buffer (find-file-noselect file))
13654 (append (org-get-buffer-tags)
13655 (mapcar (lambda (x) (if (stringp (car-safe x))
13656 (list (car-safe x)) nil))
13658 (if (and files (car files))
13660 (org-agenda-files))))))))
13662 (defun org-make-tags-matcher (match)
13663 "Create the TAGS/TODO matcher form for the selection string MATCH.
13665 The variable `todo-only' is scoped dynamically into this function.
13666 It will be set to t if the matcher restricts matching to TODO entries,
13667 otherwise will not be touched.
13669 Returns a cons of the selection string MATCH and the constructed
13670 lisp form implementing the matcher. The matcher is to be evaluated
13671 at an Org entry, with point on the headline, and returns t if the
13672 entry matches the selection string MATCH. The returned lisp form
13673 references two variables with information about the entry, which
13674 must be bound around the form's evaluation: todo, the TODO keyword
13675 at the entry (or nil of none); and tags-list, the list of all tags
13676 at the entry including inherited ones. Additionally, the category
13677 of the entry (if any) must be specified as the text property
13678 'org-category on the headline.
13680 See also `org-scan-tags'.
13682 (declare (special todo-only))
13683 (unless (boundp 'todo-only)
13684 (error "org-make-tags-matcher expects todo-only to be scoped in"))
13686 ;; Get a new match request, with completion
13687 (let ((org-last-tags-completion-table
13688 (org-global-tags-completion-table)))
13689 (setq match (org-completing-read-no-i
13690 "Match: " 'org-tags-completion-function nil nil nil
13691 'org-tags-history))))
13693 ;; Parse the string and create a lisp form
13694 (let ((match0 match)
13695 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
13697 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
13698 orterms term orlist re-p str-p level-p level-op time-p
13699 prop-p pn pv po gv rest)
13700 (if (string-match "/+" match)
13701 ;; match contains also a todo-matching request
13703 (setq tagsmatch (substring match 0 (match-beginning 0))
13704 todomatch (substring match (match-end 0)))
13705 (if (string-match "^!" todomatch)
13706 (setq todo-only t todomatch (substring todomatch 1)))
13707 (if (string-match "^\\s-*$" todomatch)
13708 (setq todomatch nil)))
13709 ;; only matching tags
13710 (setq tagsmatch match todomatch nil))
13712 ;; Make the tags matcher
13713 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
13714 (setq tagsmatcher t)
13715 (setq orterms (org-split-string tagsmatch "|") orlist nil)
13716 (while (setq term (pop orterms))
13717 (while (and (equal (substring term -1) "\\") orterms)
13718 (setq term (concat term "|" (pop orterms)))) ; repair bad split
13719 (while (string-match re term)
13720 (setq rest (substring term (match-end 0))
13721 minus (and (match-end 1)
13722 (equal (match-string 1 term) "-"))
13723 tag (save-match-data (replace-regexp-in-string
13725 (match-string 2 term)))
13726 re-p (equal (string-to-char tag) ?{)
13727 level-p (match-end 4)
13728 prop-p (match-end 5)
13730 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
13732 (setq level-op (org-op-to-function (match-string 3 term)))
13733 `(,level-op level ,(string-to-number
13734 (match-string 4 term))))
13736 (setq pn (match-string 5 term)
13737 po (match-string 6 term)
13738 pv (match-string 7 term)
13739 re-p (equal (string-to-char pv) ?{)
13740 str-p (equal (string-to-char pv) ?\")
13741 time-p (save-match-data
13742 (string-match "^\"[[<].*[]>]\"$" pv))
13743 pv (if (or re-p str-p) (substring pv 1 -1) pv))
13744 (if time-p (setq pv (org-matcher-time pv)))
13745 (setq po (org-op-to-function po (if time-p 'time str-p)))
13747 ((equal pn "CATEGORY")
13748 (setq gv '(get-text-property (point) 'org-category)))
13752 (setq gv `(org-cached-entry-get nil ,pn))))
13755 `(not (string-match ,pv (or ,gv "")))
13756 `(string-match ,pv (or ,gv "")))
13758 `(,po (or ,gv "") ,pv)
13759 `(,po (string-to-number (or ,gv ""))
13760 ,(string-to-number pv) ))))
13761 (t `(member ,tag tags-list)))
13762 mm (if minus (list 'not mm) mm)
13764 (push mm tagsmatcher))
13765 (push (if (> (length tagsmatcher) 1)
13766 (cons 'and tagsmatcher)
13769 (setq tagsmatcher nil))
13770 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
13772 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
13773 ;; Make the todo matcher
13774 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
13775 (setq todomatcher t)
13776 (setq orterms (org-split-string todomatch "|") orlist nil)
13777 (while (setq term (pop orterms))
13778 (while (string-match re term)
13779 (setq minus (and (match-end 1)
13780 (equal (match-string 1 term) "-"))
13781 kwd (match-string 2 term)
13782 re-p (equal (string-to-char kwd) ?{)
13783 term (substring term (match-end 0))
13785 `(string-match ,(substring kwd 1 -1) todo)
13786 (list 'equal 'todo kwd))
13787 mm (if minus (list 'not mm) mm))
13788 (push mm todomatcher))
13789 (push (if (> (length todomatcher) 1)
13790 (cons 'and todomatcher)
13793 (setq todomatcher nil))
13794 (setq todomatcher (if (> (length orlist) 1)
13795 (cons 'or orlist) (car orlist))))
13797 ;; Return the string and lisp forms of the matcher
13798 (setq matcher (if todomatcher
13799 (list 'and tagsmatcher todomatcher)
13802 (setq matcher (list 'and '(member todo org-not-done-keywords)
13804 (cons match0 matcher)))
13806 (defun org-op-to-function (op &optional stringp)
13807 "Turn an operator into the appropriate function."
13810 ((equal op "<" ) '(< string< org-time<))
13811 ((equal op ">" ) '(> org-string> org-time>))
13812 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
13813 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
13814 ((member op '("=" "==")) '(= string= org-time=))
13815 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
13816 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
13818 (defun org<> (a b) (not (= a b)))
13819 (defun org-string<= (a b) (or (string= a b) (string< a b)))
13820 (defun org-string>= (a b) (not (string< a b)))
13821 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
13822 (defun org-string<> (a b) (not (string= a b)))
13823 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
13824 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
13825 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
13826 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
13827 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
13828 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
13830 "Convert S to a floating point time.
13831 If S is already a number, just return it. If it is a string, parse
13832 it as a time string and apply `float-time' to it. If S is nil, just return 0."
13836 (condition-case nil
13837 (float-time (apply 'encode-time (org-parse-time-string s)))
13841 (defun org-time-today ()
13842 "Time in seconds today at 0:00.
13843 Returns the float number of seconds since the beginning of the
13844 epoch to the beginning of today (00:00)."
13845 (float-time (apply 'encode-time
13846 (append '(0 0 0) (nthcdr 3 (decode-time))))))
13848 (defun org-matcher-time (s)
13849 "Interpret a time comparison value."
13852 ((string= s "<now>") (float-time))
13853 ((string= s "<today>") (org-time-today))
13854 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
13855 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
13856 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
13857 (+ (org-time-today)
13858 (* (string-to-number (match-string 1 s))
13859 (cdr (assoc (match-string 2 s)
13860 '(("d" . 86400.0) ("w" . 604800.0)
13861 ("m" . 2678400.0) ("y" . 31557600.0)))))))
13864 (defun org-match-any-p (re list)
13865 "Does re match any element of list?"
13866 (setq list (mapcar (lambda (x) (string-match re x)) list))
13869 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
13870 (defvar org-tags-overlay (make-overlay 1 1))
13871 (org-detach-overlay org-tags-overlay)
13873 (defun org-get-local-tags-at (&optional pos)
13874 "Get a list of tags defined in the current headline."
13875 (org-get-tags-at pos 'local))
13877 (defun org-get-local-tags ()
13878 "Get a list of tags defined in the current headline."
13879 (org-get-tags-at nil 'local))
13881 (defun org-get-tags-at (&optional pos local)
13882 "Get a list of all headline tags applicable at POS.
13883 POS defaults to point. If tags are inherited, the list contains
13884 the targets in the same sequence as the headlines appear, i.e.
13885 the tags of the current headline come last.
13886 When LOCAL is non-nil, only return tags from the current headline,
13887 ignore inherited ones."
13889 (if (and org-trust-scanner-tags
13890 (or (not pos) (equal pos (point)))
13893 (let (tags ltags lastpos parent)
13897 (goto-char (or pos (point)))
13900 (condition-case nil
13902 (org-back-to-heading t)
13903 (while (not (equal lastpos (point)))
13904 (setq lastpos (point))
13906 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
13907 (setq ltags (org-split-string
13908 (org-match-string-no-properties 1) ":"))
13910 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
13913 (org-remove-uninherited-tags ltags)
13916 (or org-use-tag-inheritance (throw 'done t))
13917 (if local (throw 'done t))
13918 (or (org-up-heading-safe) (error nil))
13923 (reverse (delete-dups
13925 (org-remove-uninherited-tags
13926 org-file-tags) tags)))))))))
13928 (defun org-add-prop-inherited (s)
13929 (add-text-properties 0 (length s) '(inherited t) s)
13932 (defun org-toggle-tag (tag &optional onoff)
13933 "Toggle the tag TAG for the current line.
13934 If ONOFF is `on' or `off', don't toggle but set to this state."
13937 (org-back-to-heading t)
13938 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
13941 (setq current (match-string 1))
13942 (replace-match ""))
13944 (setq current (nreverse (org-split-string current ":")))
13948 (or (member tag current) (push tag current)))
13950 (or (not (member tag current)) (setq current (delete tag current))))
13951 (t (if (member tag current)
13952 (setq current (delete tag current))
13954 (push tag current))))
13958 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
13959 (org-set-tags nil t))
13960 (delete-horizontal-space))
13961 (run-hooks 'org-after-tags-change-hook))
13964 (defun org-align-tags-here (to-col)
13965 ;; Assumes that this is a headline
13966 (let ((pos (point)) (col (current-column)) ncol tags-l p)
13967 (beginning-of-line 1)
13968 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13969 (< pos (match-beginning 2)))
13971 (setq tags-l (- (match-end 2) (match-beginning 2)))
13972 (goto-char (match-beginning 1))
13974 (delete-region (point) (1+ (match-beginning 2)))
13975 (setq ncol (max (current-column)
13979 (- (abs to-col) tags-l))))
13981 (insert (make-string (- ncol (current-column)) ?\ ))
13982 (setq ncol (current-column))
13983 (when indent-tabs-mode (tabify p (point-at-eol)))
13984 (org-move-to-column (min ncol col) t))
13987 (defun org-set-tags-command (&optional arg just-align)
13988 "Call the set-tags command for the current entry."
13990 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
13991 (org-set-tags arg just-align)
13993 (unless (and (org-region-active-p)
13994 org-loop-over-headlines-in-active-region)
13995 (org-back-to-heading t))
13996 (org-set-tags arg just-align))))
13998 (defun org-set-tags-to (data)
13999 "Set the tags of the current entry to DATA, replacing the current tags.
14000 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
14001 If DATA is nil or the empty string, any tags will be removed."
14002 (interactive "sTags: ")
14006 ((equal data "") "")
14008 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
14011 (concat ":" (mapconcat 'identity data ":") ":"))))
14014 (org-back-to-heading t)
14015 (when (looking-at org-complex-heading-regexp)
14018 (goto-char (match-beginning 5))
14020 (delete-region (point) (point-at-eol))
14021 (org-set-tags nil 'align))
14022 (goto-char (point-at-eol))
14024 (org-set-tags nil 'align)))
14025 (beginning-of-line 1)
14026 (if (looking-at ".*?\\([ \t]+\\)$")
14027 (delete-region (match-beginning 1) (match-end 1))))))
14029 (defun org-align-all-tags ()
14030 "Align the tags i all headings."
14033 (or (ignore-errors (org-back-to-heading t))
14034 (outline-next-heading))
14035 (if (org-at-heading-p)
14037 (message "No headings"))))
14039 (defvar org-indent-indentation-per-level)
14040 (defun org-set-tags (&optional arg just-align)
14041 "Set the tags for the current headline.
14042 With prefix ARG, realign all tags in headings in the current buffer."
14044 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
14045 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
14046 'region-start-level 'region))
14047 org-loop-over-headlines-in-active-region)
14049 ;; We don't use ARG and JUST-ALIGN here these args are not
14050 ;; useful when looping over headlines
14052 org-loop-over-headlines-in-active-region
14053 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
14054 (let* ((re org-outline-regexp-bol)
14055 (current (unless arg (org-get-tags-string)))
14056 (col (current-column))
14057 (org-setting-tags t)
14058 table current-tags inherited-tags ; computed below when needed
14059 tags p0 c0 c1 rpl di tc level)
14062 (goto-char (point-min))
14063 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
14064 (while (re-search-forward re nil t)
14065 (org-set-tags nil t)
14067 (message "All tags realigned to column %d" org-tags-column))
14069 (setq tags current)
14070 ;; Get a new set of tags from the user
14072 (setq table (append org-tag-persistent-alist
14073 (or org-tag-alist (org-get-buffer-tags))
14075 org-complete-tags-always-offer-all-agenda-tags
14076 (org-global-tags-completion-table
14077 (org-agenda-files))))
14078 org-last-tags-completion-table table
14079 current-tags (org-split-string current ":")
14080 inherited-tags (nreverse
14081 (nthcdr (length current-tags)
14082 (nreverse (org-get-tags-at))))
14084 (if (or (eq t org-use-fast-tag-selection)
14085 (and org-use-fast-tag-selection
14086 (delq nil (mapcar 'cdr table))))
14087 (org-fast-tag-selection
14088 current-tags inherited-tags table
14089 (if org-fast-tag-selection-include-todo
14090 org-todo-key-alist))
14091 (let ((org-add-colon-after-tag-completion (< 1 (length table))))
14093 (org-icompleting-read "Tags: "
14094 'org-tags-completion-function
14095 nil nil current 'org-tags-history))))))
14096 (while (string-match "[-+&]+" tags)
14097 ;; No boolean logic, just a list
14098 (setq tags (replace-match ":" t t tags))))
14100 (setq tags (replace-regexp-in-string "[,]" ":" tags))
14102 (if org-tags-sort-function
14103 (setq tags (mapconcat 'identity
14104 (sort (org-split-string
14105 tags (org-re "[^[:alnum:]_@#%]+"))
14106 org-tags-sort-function) ":")))
14108 (if (string-match "\\`[\t ]*\\'" tags)
14110 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
14111 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
14113 ;; Insert new tags at the correct column
14114 (beginning-of-line 1)
14115 (setq level (or (and (looking-at org-outline-regexp)
14116 (- (match-end 0) (point) 1))
14119 ((and (equal current "") (equal tags "")))
14120 ((re-search-forward
14121 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
14123 (if (equal tags "")
14125 (goto-char (match-beginning 0))
14126 (setq c0 (current-column)
14127 ;; compute offset for the case of org-indent-mode active
14128 di (if org-indent-mode
14129 (* (1- org-indent-indentation-per-level) (1- level))
14131 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
14132 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
14133 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
14134 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
14135 (replace-match rpl t t)
14136 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
14138 (t (error "Tags alignment failed")))
14139 (org-move-to-column col)
14141 (run-hooks 'org-after-tags-change-hook))))))
14143 (defun org-change-tag-in-region (beg end tag off)
14144 "Add or remove TAG for each entry in the region.
14145 This works in the agenda, and also in an org-mode buffer."
14147 (list (region-beginning) (region-end)
14148 (let ((org-last-tags-completion-table
14149 (if (derived-mode-p 'org-mode)
14150 (org-get-buffer-tags)
14151 (org-global-tags-completion-table))))
14152 (org-icompleting-read
14153 "Tag: " 'org-tags-completion-function nil nil nil
14154 'org-tags-history))
14156 (message "[s]et or
[r]emove? ")
14157 (equal (read-char-exclusive) ?r))))
14158 (if (fboundp 'deactivate-mark) (deactivate-mark))
14159 (let ((agendap (equal major-mode 'org-agenda-mode))
14160 l1 l2 m buf pos newhead (cnt 0))
14162 (setq l2 (1- (org-current-line)))
14164 (setq l1 (org-current-line))
14165 (loop for l from l1 to l2 do
14167 (setq m (get-text-property (point) 'org-hd-marker))
14168 (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
14170 (setq buf (if agendap (marker-buffer m) (current-buffer))
14171 pos (if agendap m (point)))
14172 (with-current-buffer buf
14176 (setq cnt (1+ cnt))
14177 (org-toggle-tag tag (if off 'off 'on))
14178 (setq newhead (org-get-heading)))))
14179 (and agendap (org-agenda-change-all-lines newhead m))))
14180 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
14182 (defun org-tags-completion-function (string predicate &optional flag)
14183 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
14184 (confirm (lambda (x) (stringp (car x)))))
14185 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
14186 (setq s1 (match-string 1 string)
14187 s2 (match-string 2 string))
14188 (setq s1 "" s2 string))
14192 (setq rtn (try-completion s2 ctable confirm))
14195 (concat s1 s2 (substring rtn (length s2))
14196 (if (and org-add-colon-after-tag-completion
14197 (assoc rtn ctable))
14202 (all-completions s2 ctable confirm)
14206 (assoc s2 ctable)))
14209 (defun org-fast-tag-insert (kwd tags face &optional end)
14210 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
14211 (insert (format "%-12s" (concat kwd ":"))
14212 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
14215 (defun org-fast-tag-show-exit (flag)
14218 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
14219 (replace-match ""))
14222 (org-move-to-column (- (window-width) 19) t)
14223 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
14225 (defun org-set-current-tags-overlay (current prefix)
14226 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
14227 (if (featurep 'xemacs)
14228 (org-overlay-display org-tags-overlay (concat prefix s)
14229 'secondary-selection)
14230 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
14231 (org-overlay-display org-tags-overlay (concat prefix s)))))
14233 (defvar org-last-tag-selection-key nil)
14234 (defun org-fast-tag-selection (current inherited table &optional todo-table)
14235 "Fast tag selection with single keys.
14236 CURRENT is the current list of tags in the headline, INHERITED is the
14237 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
14238 possibly with grouping information. TODO-TABLE is a similar table with
14239 TODO keywords, should these have keys assigned to them.
14240 If the keys are nil, a-z are automatically assigned.
14241 Returns the new tags string, or nil to not change the current settings."
14242 (let* ((fulltable (append table todo-table))
14243 (maxlen (apply 'max (mapcar
14245 (if (stringp (car x)) (string-width (car x)) 0))
14247 (buf (current-buffer))
14248 (expert (eq org-fast-tag-selection-single-key 'expert))
14250 (fwidth (+ maxlen 3 1 3))
14251 (ncol (/ (- (window-width) 4) fwidth))
14254 tg cnt e c char c1 c2 ntable tbl rtn
14255 ov-start ov-end ov-prefix
14256 (exit-after-next org-fast-tag-selection-single-key)
14257 (done-keywords org-done-keywords)
14260 (beginning-of-line 1)
14262 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14263 (setq ov-start (match-beginning 1)
14264 ov-end (match-end 1)
14266 (setq ov-start (1- (point-at-eol))
14267 ov-end (1+ ov-start))
14268 (skip-chars-forward "^\n\r")
14271 (buffer-substring (1- (point)) (point))
14272 (if (> (current-column) org-tags-column)
14274 (make-string (- org-tags-column (current-column)) ?\ ))))))
14275 (move-overlay org-tags-overlay ov-start ov-end)
14276 (save-window-excursion
14278 (set-buffer (get-buffer-create " *Org tags*"))
14279 (delete-other-windows)
14280 (split-window-vertically)
14281 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
14283 (org-set-local 'org-done-keywords done-keywords)
14284 (org-fast-tag-insert "Inherited" inherited i-face "\n")
14285 (org-fast-tag-insert "Current" current c-face "\n\n")
14286 (org-fast-tag-show-exit exit-after-next)
14287 (org-set-current-tags-overlay current ov-prefix)
14288 (setq tbl fulltable char ?a cnt 0)
14289 (while (setq e (pop tbl))
14291 ((equal (car e) :startgroup)
14292 (push '() groups) (setq ingroup t)
14293 (when (not (= cnt 0))
14296 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
14297 ((equal (car e) :endgroup)
14298 (setq ingroup nil cnt 0)
14299 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
14300 ((equal e '(:newline))
14301 (when (not (= cnt 0))
14305 (while (equal (car tbl) '(:newline))
14307 (setq tbl (cdr tbl)))))
14309 (setq tg (copy-sequence (car e)) c2 nil)
14312 ;; automatically assign a character.
14313 (setq c1 (string-to-char
14314 (downcase (substring
14315 tg (if (= (string-to-char tg) ?@) 1 0)))))
14316 (if (or (rassoc c1 ntable) (rassoc c1 table))
14317 (while (or (rassoc char ntable) (rassoc char table))
14318 (setq char (1+ char)))
14320 (setq c (or c2 char)))
14321 (if ingroup (push tg (car groups)))
14322 (setq tg (org-add-props tg nil 'face
14324 ((not (assoc tg table))
14325 (org-get-todo-face tg))
14326 ((member tg current) c-face)
14327 ((member tg inherited) i-face))))
14328 (if (and (= cnt 0) (not ingroup)) (insert " "))
14329 (insert "[" c "] " tg (make-string
14330 (- fwidth 4 (length tg)) ?\ ))
14331 (push (cons tg c) ntable)
14332 (when (= (setq cnt (1+ cnt)) ncol)
14334 (if ingroup (insert " "))
14336 (setq ntable (nreverse ntable))
14338 (goto-char (point-min))
14339 (if (not expert) (org-fit-window-to-buffer))
14343 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
14344 (if (not groups) "no " "")
14345 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
14346 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
14347 (setq org-last-tag-selection-key c)
14349 ((= c ?\r) (throw 'exit t))
14351 (setq groups (not groups))
14352 (goto-char (point-min))
14353 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
14356 (org-fast-tag-show-exit
14357 (setq exit-after-next (not exit-after-next)))
14359 (delete-other-windows)
14360 (set-window-buffer (split-window-vertically) " *Org tags*")
14361 (org-switch-to-buffer-other-window " *Org tags*")
14362 (org-fit-window-to-buffer)))
14364 (and (= c ?q) (not (rassoc c ntable))))
14365 (org-detach-overlay org-tags-overlay)
14366 (setq quit-flag t))
14369 (if exit-after-next (setq exit-after-next 'now)))
14371 (condition-case nil
14372 (setq tg (org-icompleting-read
14375 (with-current-buffer buf
14376 (org-get-buffer-tags)))))
14377 (quit (setq tg "")))
14378 (when (string-match "\\S-" tg)
14379 (add-to-list 'buffer-tags (list tg))
14380 (if (member tg current)
14381 (setq current (delete tg current))
14382 (push tg current)))
14383 (if exit-after-next (setq exit-after-next 'now)))
14384 ((setq e (rassoc c todo-table) tg (car e))
14385 (with-current-buffer buf
14386 (save-excursion (org-todo tg)))
14387 (if exit-after-next (setq exit-after-next 'now)))
14388 ((setq e (rassoc c ntable) tg (car e))
14389 (if (member tg current)
14390 (setq current (delete tg current))
14391 (loop for g in groups do
14394 (setq current (delete x current)))
14397 (if exit-after-next (setq exit-after-next 'now))))
14399 ;; Create a sorted list
14403 (assoc b (cdr (memq (assoc a ntable) ntable))))))
14404 (if (eq exit-after-next 'now) (throw 'exit t))
14405 (goto-char (point-min))
14406 (beginning-of-line 2)
14407 (delete-region (point) (point-at-eol))
14408 (org-fast-tag-insert "Current" current c-face)
14409 (org-set-current-tags-overlay current ov-prefix)
14410 (while (re-search-forward
14411 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
14412 (setq tg (match-string 1))
14413 (add-text-properties
14414 (match-beginning 1) (match-end 1)
14417 ((member tg current) c-face)
14418 ((member tg inherited) i-face)
14419 (t (get-text-property (match-beginning 1) 'face))))))
14420 (goto-char (point-min)))))
14421 (org-detach-overlay org-tags-overlay)
14423 (mapconcat 'identity current ":")
14426 (defun org-get-tags-string ()
14427 "Get the TAGS string in the current headline."
14428 (unless (org-at-heading-p t)
14429 (error "Not on a heading"))
14431 (beginning-of-line 1)
14432 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14433 (org-match-string-no-properties 1)
14436 (defun org-get-tags ()
14437 "Get the list of tags specified in the current headline."
14438 (org-split-string (org-get-tags-string) ":"))
14440 (defun org-get-buffer-tags ()
14441 "Get a table of all tags used in the buffer, for completion."
14444 (goto-char (point-min))
14445 (while (re-search-forward
14446 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
14447 (when (equal (char-after (point-at-bol 0)) ?*)
14448 (mapc (lambda (x) (add-to-list 'tags x))
14449 (org-split-string (org-match-string-no-properties 1) ":")))))
14450 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
14451 (mapcar 'list tags)))
14453 ;;;; The mapping API
14455 (defun org-map-entries (func &optional match scope &rest skip)
14456 "Call FUNC at each headline selected by MATCH in SCOPE.
14458 FUNC is a function or a lisp form. The function will be called without
14459 arguments, with the cursor positioned at the beginning of the headline.
14460 The return values of all calls to the function will be collected and
14461 returned as a list.
14463 The call to FUNC will be wrapped into a save-excursion form, so FUNC
14464 does not need to preserve point. After evaluation, the cursor will be
14465 moved to the end of the line (presumably of the headline of the
14466 processed entry) and search continues from there. Under some
14467 circumstances, this may not produce the wanted results. For example,
14468 if you have removed (e.g. archived) the current (sub)tree it could
14469 mean that the next entry will be skipped entirely. In such cases, you
14470 can specify the position from where search should continue by making
14471 FUNC set the variable `org-map-continue-from' to the desired buffer
14474 MATCH is a tags/property/todo match as it is used in the agenda tags view.
14475 Only headlines that are matched by this query will be considered during
14476 the iteration. When MATCH is nil or t, all headlines will be
14477 visited by the iteration.
14479 SCOPE determines the scope of this command. It can be any of:
14481 nil The current buffer, respecting the restriction if any
14482 tree The subtree started with the entry at point
14483 region The entries within the active region, if any
14485 The entries within the active region, but only those at
14486 the same level than the first one.
14487 file The current buffer, without restriction
14489 The current buffer, and any archives associated with it
14490 agenda All agenda files
14491 agenda-with-archives
14492 All agenda files with any archive files associated with them
14494 If this is a list, all files in the list will be scanned
14496 The remaining args are treated as settings for the skipping facilities of
14497 the scanner. The following items can be given here:
14499 archive skip trees with the archive tag.
14500 comment skip trees with the COMMENT keyword
14501 function or Emacs Lisp form:
14502 will be used as value for `org-agenda-skip-function', so whenever
14503 the function returns t, FUNC will not be called for that
14504 entry and search will continue from the point where the
14505 function leaves it.
14507 If your function needs to retrieve the tags including inherited tags
14508 at the *current* entry, you can use the value of the variable
14509 `org-scanner-tags' which will be much faster than getting the value
14510 with `org-get-tags-at'. If your function gets properties with
14511 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
14512 to t around the call to `org-entry-properties' to get the same speedup.
14513 Note that if your function moves around to retrieve tags and properties at
14514 a *different* entry, you cannot use these techniques."
14515 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
14516 (not (org-region-active-p)))
14517 (let* ((org-agenda-archives-mode nil) ; just to make sure
14518 (org-agenda-skip-archived-trees (memq 'archive skip))
14519 (org-agenda-skip-comment-trees (memq 'comment skip))
14520 (org-agenda-skip-function
14521 (car (org-delete-all '(comment archive) skip)))
14522 (org-tags-match-list-sublevels t)
14523 (start-level (eq scope 'region-start-level))
14525 org-todo-keywords-for-agenda
14526 org-done-keywords-for-agenda
14527 org-todo-keyword-alist-for-agenda
14528 org-drawers-for-agenda
14529 org-tag-alist-for-agenda
14533 ((eq match t) (setq matcher t))
14534 ((eq match nil) (setq matcher t))
14535 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
14539 (cond ((eq scope 'tree)
14540 (org-back-to-heading t)
14541 (org-narrow-to-subtree)
14543 ((and (or (eq scope 'region) (eq scope 'region-start-level))
14544 (org-region-active-p))
14545 ;; If needed, set start-level to a string like "2"
14548 (goto-char (region-beginning))
14549 (unless (org-at-heading-p) (outline-next-heading))
14550 (setq start-level (org-current-level))))
14551 (narrow-to-region (region-beginning)
14553 (goto-char (region-end))
14554 (unless (and (bolp) (org-at-heading-p))
14555 (outline-next-heading))
14561 (org-agenda-prepare-buffers
14562 (list (buffer-file-name (current-buffer))))
14563 (setq res (org-scan-tags func matcher todo-only start-level)))
14564 ;; Get the right scope
14566 ((and scope (listp scope) (symbolp (car scope)))
14567 (setq scope (eval scope)))
14568 ((eq scope 'agenda)
14569 (setq scope (org-agenda-files t)))
14570 ((eq scope 'agenda-with-archives)
14571 (setq scope (org-agenda-files t))
14572 (setq scope (org-add-archive-files scope)))
14574 (setq scope (list (buffer-file-name))))
14575 ((eq scope 'file-with-archives)
14576 (setq scope (org-add-archive-files (list (buffer-file-name))))))
14577 (org-agenda-prepare-buffers scope)
14578 (while (setq file (pop scope))
14579 (with-current-buffer (org-find-base-buffer-visiting file)
14583 (goto-char (point-min))
14584 (setq res (append res (org-scan-tags func matcher todo-only))))))))))
14589 ;;; Setting and retrieving properties
14591 (defconst org-special-properties
14592 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
14593 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
14594 "The special properties valid in Org-mode.
14596 These are properties that are not defined in the property drawer,
14597 but in some other way.")
14599 (defconst org-default-properties
14600 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
14601 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
14602 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
14603 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
14604 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
14605 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
14606 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
14607 "Some properties that are used by Org-mode for various purposes.
14608 Being in this list makes sure that they are offered for completion.")
14610 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
14611 "Regular expression matching the first line of a property drawer.")
14613 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
14614 "Regular expression matching the last line of a property drawer.")
14616 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
14617 "Regular expression matching the first line of a property drawer.")
14619 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
14620 "Regular expression matching the first line of a property drawer.")
14622 (defconst org-property-drawer-re
14623 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
14624 org-property-end-re "\\)\n?")
14625 "Matches an entire property drawer.")
14627 (defconst org-clock-drawer-re
14628 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
14629 org-property-end-re "\\)\n?")
14630 "Matches an entire clock drawer.")
14632 (defsubst org-re-property (property)
14633 "Return a regexp matching a PROPERTY line.
14634 Match group 1 will be set to the value."
14635 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
14637 (defsubst org-re-property-keyword (property)
14638 "Return a regexp matching a PROPERTY line, possibly with no
14639 value for the property."
14640 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
14642 (defun org-property-action ()
14643 "Do an action on properties."
14646 (org-at-property-p)
14647 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
14648 (setq c (read-char-exclusive))
14651 (call-interactively 'org-set-property))
14653 (call-interactively 'org-delete-property))
14655 (call-interactively 'org-delete-property-globally))
14657 (call-interactively 'org-compute-property-at-point))
14658 (t (error "No such property action %c" c)))))
14660 (defun org-inc-effort ()
14661 "Increment the value of the effort property in the current entry."
14663 (org-set-effort nil t))
14665 (defvar org-clock-effort) ;; Defined in org-clock.el
14666 (defvar org-clock-current-task) ;; Defined in org-clock.el
14667 (defun org-set-effort (&optional value increment)
14668 "Set the effort property of the current entry.
14669 With numerical prefix arg, use the nth allowed value, 0 stands for the
14670 10th allowed value.
14672 When INCREMENT is non-nil, set the property to the next allowed value."
14674 (if (equal value 0) (setq value 10))
14675 (let* ((completion-ignore-case t)
14676 (prop org-effort-property)
14677 (cur (org-entry-get nil prop))
14678 (allowed (org-property-get-allowed-values nil prop 'table))
14679 (existing (mapcar 'list (org-property-values prop)))
14680 (heading (nth 4 (org-heading-components)))
14683 ((stringp value) value)
14684 ((and allowed (integerp value))
14685 (or (car (nth (1- value) allowed))
14686 (car (org-last allowed))))
14687 ((and allowed increment)
14688 (or (caadr (member (list cur) allowed))
14689 (error "Allowed effort values are not set")))
14691 (message "Select 1-9,0, [RET%s]: %s"
14692 (if cur (concat "=" cur) "")
14693 (mapconcat 'car allowed " "))
14694 (setq rpl (read-char-exclusive))
14695 (if (equal rpl ?\r)
14697 (setq rpl (- rpl ?0))
14698 (if (equal rpl 0) (setq rpl 10))
14699 (if (and (> rpl 0) (<= rpl (length allowed)))
14700 (car (nth (1- rpl) allowed))
14701 (org-completing-read "Effort: " allowed nil))))
14703 (let (org-completion-use-ido org-completion-use-iswitchb)
14704 (org-completing-read
14705 (concat "Effort " (if (and cur (string-match "\\S-" cur))
14706 (concat "[" cur "]") "")
14708 existing nil nil "" nil cur))))))
14709 (unless (equal (org-entry-get nil prop) val)
14710 (org-entry-put nil prop val))
14712 (org-back-to-heading t)
14713 (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
14714 (when (string= heading org-clock-current-task)
14715 (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
14716 (org-clock-update-mode-line))
14717 (message "%s is now %s" prop val)))
14719 (defun org-at-property-p ()
14720 "Is cursor inside a property drawer?"
14722 (beginning-of-line 1)
14723 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
14724 (save-match-data ;; Used by calling procedures
14726 (range (unless (org-before-first-heading-p)
14727 (org-get-property-block))))
14728 (and range (<= (car range) p) (< p (cdr range))))))))
14730 (defun org-get-property-block (&optional beg end force)
14731 "Return the (beg . end) range of the body of the property drawer.
14732 BEG and END are the beginning and end of the current subtree, or of
14733 the part before the first headline. If they are not given, they will
14734 be found. If the drawer does not exist and FORCE is non-nil, create
14738 (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
14739 (progn (org-back-to-heading t) (point))))
14740 (end (or end (and (not (outline-next-heading)) (point-max))
14743 (if (re-search-forward org-property-start-re end t)
14744 (setq beg (1+ (match-end 0)))
14747 (org-insert-property-drawer)
14748 (setq end (progn (outline-next-heading) (point))))
14751 (if (re-search-forward org-property-start-re end t)
14752 (setq beg (1+ (match-end 0)))))
14753 (if (re-search-forward org-property-end-re end t)
14754 (setq end (match-beginning 0))
14755 (or force (throw 'exit nil))
14759 (insert ":END:\n"))
14762 (defun org-entry-properties (&optional pom which specific)
14763 "Get all properties of the entry at point-or-marker POM.
14764 This includes the TODO keyword, the tags, time strings for deadline,
14765 scheduled, and clocking, and any additional properties defined in the
14766 entry. The return value is an alist, keys may occur multiple times
14767 if the property key was used several times.
14768 POM may also be nil, in which case the current entry is used.
14769 If WHICH is nil or `all', get all properties. If WHICH is
14770 `special' or `standard', only get that subclass. If WHICH
14771 is a string only get exactly this property. SPECIFIC can be a string, the
14772 specific property we are interested in. Specifying it can speed
14773 things up because then unnecessary parsing is avoided."
14774 (setq which (or which 'all))
14775 (org-with-point-at pom
14776 (let ((clockstr (substring org-clock-string 0 -1))
14777 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
14778 (case-fold-search nil)
14779 beg end range props sum-props key key1 value string clocksum clocksumt)
14781 (when (condition-case nil
14782 (and (derived-mode-p 'org-mode) (org-back-to-heading t))
14785 (setq sum-props (get-text-property (point) 'org-summaries))
14786 (setq clocksum (get-text-property (point) :org-clock-minutes)
14787 clocksumt (get-text-property (point) :org-clock-minutes-today))
14788 (outline-next-heading)
14790 (when (memq which '(all special))
14791 ;; Get the special properties, like TODO and tags
14793 (when (and (or (not specific) (string= specific "TODO"))
14794 (looking-at org-todo-line-regexp) (match-end 2))
14795 (push (cons "TODO" (org-match-string-no-properties 2)) props))
14796 (when (and (or (not specific) (string= specific "PRIORITY"))
14797 (looking-at org-priority-regexp))
14798 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
14799 (when (or (not specific) (string= specific "FILE"))
14800 (push (cons "FILE" buffer-file-name) props))
14801 (when (and (or (not specific) (string= specific "TAGS"))
14802 (setq value (org-get-tags-string))
14803 (string-match "\\S-" value))
14804 (push (cons "TAGS" value) props))
14805 (when (and (or (not specific) (string= specific "ALLTAGS"))
14806 (setq value (org-get-tags-at)))
14807 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
14810 (when (or (not specific) (string= specific "BLOCKED"))
14811 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
14812 (when (or (not specific)
14814 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
14815 "TIMESTAMP" "TIMESTAMP_IA")))
14817 (while (re-search-forward org-maybe-keyword-time-regexp end t)
14818 (setq key (if (match-end 1)
14819 (substring (org-match-string-no-properties 1)
14821 string (if (equal key clockstr)
14823 (buffer-substring-no-properties
14824 (match-beginning 3) (goto-char
14826 (substring (org-match-string-no-properties 3)
14828 ;; Get the correct property name from the key. This is
14829 ;; necessary if the user has configured time keywords.
14830 (setq key1 (concat key ":"))
14834 (if (= (char-after (match-beginning 3)) ?\[)
14835 "TIMESTAMP_IA" "TIMESTAMP")))
14836 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
14837 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
14838 ((equal key1 org-closed-string) (setq key "CLOSED"))
14839 ((equal key1 org-clock-string) (setq key "CLOCK")))
14840 (if (and specific (equal key specific) (not (equal key "CLOCK")))
14842 (push (cons key string) props)
14843 ;; no need to search further if match is found
14845 (when (or (equal key "CLOCK") (not (assoc key props)))
14846 (push (cons key string) props)))))))
14848 (when (memq which '(all standard))
14849 ;; Get the standard properties, like :PROP: ...
14850 (setq range (org-get-property-block beg end))
14852 (goto-char (car range))
14853 (while (re-search-forward
14854 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
14856 (setq key (org-match-string-no-properties 1)
14857 value (org-trim (or (org-match-string-no-properties 2) "")))
14858 (unless (member key excluded)
14859 (push (cons key (or value "")) props)))))
14861 (push (cons "CLOCKSUM"
14862 (org-columns-number-to-string (/ (float clocksum) 60.)
14866 (push (cons "CLOCKSUM_T"
14867 (org-columns-number-to-string (/ (float clocksumt) 60.)
14870 (unless (assoc "CATEGORY" props)
14871 (push (cons "CATEGORY" (org-get-category)) props))
14872 (append sum-props (nreverse props)))))))
14874 (defun org-entry-get (pom property &optional inherit literal-nil)
14875 "Get value of PROPERTY for entry or content at point-or-marker POM.
14876 If INHERIT is non-nil and the entry does not have the property,
14877 then also check higher levels of the hierarchy.
14878 If INHERIT is the symbol `selective', use inheritance only if the setting
14879 in `org-use-property-inheritance' selects PROPERTY for inheritance.
14880 If the property is present but empty, the return value is the empty string.
14881 If the property is not present at all, nil is returned.
14883 If LITERAL-NIL is set, return the string value \"nil\" as a string,
14884 do not interpret it as the list atom nil. This is used for inheritance
14885 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
14886 (org-with-point-at pom
14887 (if (and inherit (if (eq inherit 'selective)
14888 (org-property-inherit-p property)
14890 (org-entry-get-with-inheritance property literal-nil)
14891 (if (member property org-special-properties)
14892 ;; We need a special property. Use `org-entry-properties' to
14893 ;; retrieve it, but specify the wanted property
14894 (cdr (assoc property (org-entry-properties nil 'special property)))
14895 (let ((range (org-get-property-block)))
14896 (when (and range (not (eq (car range) (cdr range))))
14897 (let* ((props (list (or (assoc property org-file-properties)
14898 (assoc property org-global-properties)
14899 (assoc property org-global-properties-fixed))))
14901 (when (re-search-forward
14902 (org-re-property key) (cdr range) t)
14904 (org-update-property-plist
14907 (org-match-string-no-properties 1) "")
14910 (goto-char (car range))
14911 (funcall ap property)
14912 (goto-char (car range))
14913 (while (funcall ap (concat property "+")))
14914 (setq val (cdr (assoc property props)))
14915 (when val (if literal-nil val (org-not-nil val))))))))))
14917 (defun org-property-or-variable-value (var &optional inherit)
14918 "Check if there is a property fixing the value of VAR.
14919 If yes, return this value. If not, return the current value of the variable."
14920 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14921 (if (and prop (stringp prop) (string-match "\\S-" prop))
14923 (symbol-value var))))
14925 (defun org-entry-delete (pom property &optional delete-empty-drawer)
14926 "Delete the property PROPERTY from entry at point-or-marker POM.
14927 When optional argument DELETE-EMPTY-DRAWER is a string, it defines
14928 an empty drawer to delete."
14929 (org-with-point-at pom
14930 (if (member property org-special-properties)
14931 nil ; cannot delete these properties.
14932 (let ((range (org-get-property-block)))
14934 (goto-char (car range))
14936 (org-re-property property)
14939 (delete-region (match-beginning 0) (1+ (point-at-eol)))
14940 (and delete-empty-drawer
14941 (org-remove-empty-drawer-at
14942 delete-empty-drawer (car range)))
14946 ;; Multi-values properties are properties that contain multiple values
14947 ;; These values are assumed to be single words, separated by whitespace.
14948 (defun org-entry-add-to-multivalued-property (pom property value)
14949 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
14950 (let* ((old (org-entry-get pom property))
14951 (values (and old (org-split-string old "[ \t]"))))
14952 (setq value (org-entry-protect-space value))
14953 (unless (member value values)
14954 (setq values (cons value values))
14955 (org-entry-put pom property
14956 (mapconcat 'identity values " ")))))
14958 (defun org-entry-remove-from-multivalued-property (pom property value)
14959 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
14960 (let* ((old (org-entry-get pom property))
14961 (values (and old (org-split-string old "[ \t]"))))
14962 (setq value (org-entry-protect-space value))
14963 (when (member value values)
14964 (setq values (delete value values))
14965 (org-entry-put pom property
14966 (mapconcat 'identity values " ")))))
14968 (defun org-entry-member-in-multivalued-property (pom property value)
14969 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
14970 (let* ((old (org-entry-get pom property))
14971 (values (and old (org-split-string old "[ \t]"))))
14972 (setq value (org-entry-protect-space value))
14973 (member value values)))
14975 (defun org-entry-get-multivalued-property (pom property)
14976 "Return a list of values in a multivalued property."
14977 (let* ((value (org-entry-get pom property))
14978 (values (and value (org-split-string value "[ \t]"))))
14979 (mapcar 'org-entry-restore-space values)))
14981 (defun org-entry-put-multivalued-property (pom property &rest values)
14982 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
14983 VALUES should be a list of strings. Spaces will be protected."
14984 (org-entry-put pom property
14985 (mapconcat 'org-entry-protect-space values " "))
14986 (let* ((value (org-entry-get pom property))
14987 (values (and value (org-split-string value "[ \t]"))))
14988 (mapcar 'org-entry-restore-space values)))
14990 (defun org-entry-protect-space (s)
14991 "Protect spaces and newline in string S."
14992 (while (string-match " " s)
14993 (setq s (replace-match "%20" t t s)))
14994 (while (string-match "\n" s)
14995 (setq s (replace-match "%0A" t t s)))
14998 (defun org-entry-restore-space (s)
14999 "Restore spaces and newline in string S."
15000 (while (string-match "%20" s)
15001 (setq s (replace-match " " t t s)))
15002 (while (string-match "%0A" s)
15003 (setq s (replace-match "\n" t t s)))
15006 (defvar org-entry-property-inherited-from (make-marker)
15007 "Marker pointing to the entry from where a property was inherited.
15008 Each call to `org-entry-get-with-inheritance' will set this marker to the
15009 location of the entry where the inheritance search matched. If there was
15010 no match, the marker will point nowhere.
15011 Note that also `org-entry-get' calls this function, if the INHERIT flag
15014 (defun org-entry-get-with-inheritance (property &optional literal-nil)
15015 "Get PROPERTY of entry or content at point, search higher levels if needed.
15016 The search will stop at the first ancestor which has the property defined.
15017 If the value found is \"nil\", return nil to show that the property
15018 should be considered as undefined (this is the meaning of nil here).
15019 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
15020 (move-marker org-entry-property-inherited-from nil)
15027 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
15028 (or (ignore-errors (org-back-to-heading t))
15029 (goto-char (point-min)))
15030 (move-marker org-entry-property-inherited-from (point))
15032 (or (ignore-errors (org-up-heading-safe))
15033 (throw 'ex nil))))))
15035 (cdr (assoc property org-file-properties))
15036 (cdr (assoc property org-global-properties))
15037 (cdr (assoc property org-global-properties-fixed))))
15038 (if literal-nil tmp (org-not-nil tmp))))
15040 (defvar org-property-changed-functions nil
15041 "Hook called when the value of a property has changed.
15042 Each hook function should accept two arguments, the name of the property
15043 and the new value.")
15045 (defun org-entry-put (pom property value)
15046 "Set PROPERTY to VALUE for entry at point-or-marker POM."
15047 (org-with-point-at pom
15048 (org-back-to-heading t)
15049 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
15052 ((equal property "TODO")
15053 (when (and (stringp value) (string-match "\\S-" value)
15054 (not (member value org-todo-keywords-1)))
15055 (error "\"%s\" is not a valid TODO state" value))
15056 (if (or (not value)
15057 (not (string-match "\\S-" value)))
15058 (setq value 'none))
15060 (org-set-tags nil 'align))
15061 ((equal property "PRIORITY")
15062 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
15063 (string-to-char value) ?\ ))
15064 (org-set-tags nil 'align))
15065 ((equal property "CLOCKSUM")
15066 (if (not (re-search-forward
15067 (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
15068 (error "Cannot find a clock log")
15069 (goto-char (- (match-end 1) 2))
15071 ((eq value 'earlier) (org-timestamp-down))
15072 ((eq value 'later) (org-timestamp-up)))
15073 (org-clock-sum-current-item)))
15074 ((equal property "SCHEDULED")
15075 (if (re-search-forward org-scheduled-time-regexp end t)
15077 ((eq value 'earlier) (org-timestamp-change -1 'day))
15078 ((eq value 'later) (org-timestamp-change 1 'day))
15079 (t (call-interactively 'org-schedule)))
15080 (call-interactively 'org-schedule)))
15081 ((equal property "DEADLINE")
15082 (if (re-search-forward org-deadline-time-regexp end t)
15084 ((eq value 'earlier) (org-timestamp-change -1 'day))
15085 ((eq value 'later) (org-timestamp-change 1 'day))
15086 (t (call-interactively 'org-deadline)))
15087 (call-interactively 'org-deadline)))
15088 ((member property org-special-properties)
15089 (error "The %s property can not yet be set with `org-entry-put'"
15091 (t ; a non-special property
15092 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
15093 (setq range (org-get-property-block beg end 'force))
15094 (goto-char (car range))
15095 (if (re-search-forward
15096 (org-re-property-keyword property) (cdr range) t)
15098 (delete-region (match-beginning 0) (match-end 0))
15099 (goto-char (match-beginning 0)))
15100 (goto-char (cdr range))
15104 (insert ":" property ":")
15105 (and value (insert " " value))
15106 (org-indent-line)))))
15107 (run-hook-with-args 'org-property-changed-functions property value)))
15109 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
15110 "Get all property keys in the current buffer.
15111 With INCLUDE-SPECIALS, also list the special properties that reflect things
15112 like tags and TODO state.
15113 With INCLUDE-DEFAULTS, also include properties that has special meaning
15114 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
15116 With INCLUDE-COLUMNS, also include property names given in COLUMN
15117 formats in the current buffer."
15118 (let (rtn range cfmt s p)
15122 (goto-char (point-min))
15123 (while (re-search-forward org-property-start-re nil t)
15124 (setq range (org-get-property-block))
15125 (goto-char (car range))
15126 (while (re-search-forward
15127 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
15129 (add-to-list 'rtn (org-match-string-no-properties 1)))
15130 (outline-next-heading))))
15132 (when include-specials
15133 (setq rtn (append org-special-properties rtn)))
15135 (when include-defaults
15136 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
15137 (add-to-list 'rtn org-effort-property))
15139 (when include-columns
15143 (goto-char (point-min))
15144 (while (re-search-forward
15145 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
15147 (setq cfmt (match-string 2) s 0)
15148 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
15150 (setq s (match-end 0)
15151 p (match-string 1 cfmt))
15152 (unless (or (equal p "ITEM")
15153 (member p org-special-properties))
15154 (add-to-list 'rtn (match-string 1 cfmt))))))))
15156 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
15158 (defun org-property-values (key)
15159 "Return a list of all values of property KEY in the current buffer."
15163 (goto-char (point-min))
15164 (let ((re (org-re-property key))
15166 (while (re-search-forward re nil t)
15167 (add-to-list 'values (org-trim (match-string 1))))
15168 (delete "" values)))))
15170 (defun org-insert-property-drawer ()
15171 "Insert a property drawer into the current entry."
15172 (org-back-to-heading t)
15173 (looking-at org-outline-regexp)
15174 (let ((indent (if org-adapt-indentation
15175 (- (match-end 0) (match-beginning 0))
15178 (re (concat "^[ \t]*" org-keyword-time-regexp))
15180 (outline-next-heading)
15183 (while (re-search-forward re end t))
15184 (setq hiddenp (outline-invisible-p))
15186 (and (equal (char-after) ?\n) (forward-char 1))
15187 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
15188 (if (member (match-string 1) '("CLOCK:" ":END:"))
15189 ;; just skip this line
15190 (beginning-of-line 2)
15191 ;; Drawer start, find the end
15192 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
15193 (beginning-of-line 1)))
15194 (org-skip-over-state-notes)
15195 (skip-chars-backward " \t\n\r")
15196 (if (eq (char-before) ?*) (forward-char 1))
15197 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
15198 (beginning-of-line 0)
15199 (org-indent-to-column indent)
15200 (beginning-of-line 2)
15201 (org-indent-to-column indent)
15202 (beginning-of-line 0)
15205 (org-back-to-heading t)
15207 (org-flag-drawer t))))
15209 (defun org-insert-drawer (&optional arg drawer)
15210 "Insert a drawer at point.
15212 Optional argument DRAWER, when non-nil, is a string representing
15213 drawer's name. Otherwise, the user is prompted for a name.
15215 If a region is active, insert the drawer around that region
15218 Point is left between drawer's boundaries."
15220 (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
15222 ;; SYSTEM-DRAWERS is a list of drawer names that are used
15223 ;; internally by Org. They are meant to be inserted
15225 (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
15226 ;; Remove system drawers from list. Note: For some reason,
15227 ;; `org-completing-read' ignores the predicate while
15228 ;; `completing-read' handles it fine.
15229 (drawer (if arg "PROPERTIES"
15232 "Drawer: " org-drawers
15233 (lambda (d) (not (member d system-drawers))))))))
15235 ;; With C-u, fall back on `org-insert-property-drawer'
15236 (arg (org-insert-property-drawer))
15237 ;; With an active region, insert a drawer at point.
15238 ((not (org-region-active-p))
15240 (unless (bolp) (insert "\n"))
15241 (insert (format ":%s:\n\n:END:\n" drawer))
15242 (forward-line -2)))
15243 ;; Otherwise, insert the drawer at point
15245 (let ((rbeg (region-beginning))
15246 (rend (copy-marker (region-end))))
15250 (beginning-of-line)
15251 (when (save-excursion
15252 (re-search-forward org-outline-regexp-bol rend t))
15253 (error "Drawers cannot contain headlines"))
15254 ;; Position point at the beginning of the first
15255 ;; non-blank line in region. Insert drawer's opening
15256 ;; there, then indent it.
15257 (org-skip-whitespace)
15258 (beginning-of-line)
15259 (insert ":" drawer ":\n")
15261 (indent-for-tab-command)
15262 ;; Move point to the beginning of the first blank line
15263 ;; after the last non-blank line in region. Insert
15264 ;; drawer's closing, then indent it.
15266 (skip-chars-backward " \r\t\n")
15268 (deactivate-mark t)
15269 (indent-for-tab-command)
15270 (unless (eolp) (insert "\n")))
15271 ;; Clear marker, whatever the outcome of insertion is.
15272 (set-marker rend nil)))))))
15274 (defvar org-property-set-functions-alist nil
15275 "Property set function alist.
15276 Each entry should have the following format:
15278 (PROPERTY . READ-FUNCTION)
15280 The read function will be called with the same argument as
15281 `org-completing-read'.")
15283 (defun org-set-property-function (property)
15284 "Get the function that should be used to set PROPERTY.
15285 This is computed according to `org-property-set-functions-alist'."
15286 (or (cdr (assoc property org-property-set-functions-alist))
15287 'org-completing-read))
15289 (defun org-read-property-value (property)
15290 "Read PROPERTY value from user."
15291 (let* ((completion-ignore-case t)
15292 (allowed (org-property-get-allowed-values nil property 'table))
15293 (cur (org-entry-get nil property))
15294 (prompt (concat property " value"
15295 (if (and cur (string-match "\\S-" cur))
15296 (concat " [" cur "]") "") ": "))
15297 (set-function (org-set-property-function property))
15299 (funcall set-function prompt allowed nil
15300 (not (get-text-property 0 'org-unrestricted
15302 (let (org-completion-use-ido org-completion-use-iswitchb)
15303 (funcall set-function prompt
15304 (mapcar 'list (org-property-values property))
15305 nil nil "" nil cur)))))
15310 (defvar org-last-set-property nil)
15311 (defvar org-last-set-property-value nil)
15312 (defun org-read-property-name ()
15313 "Read a property name."
15314 (let* ((completion-ignore-case t)
15315 (keys (org-buffer-property-keys nil t t))
15316 (default-prop (or (save-excursion
15318 (beginning-of-line)
15319 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
15320 (null (string= (match-string 1) "END"))
15321 (match-string 1))))
15322 org-last-set-property))
15323 (property (org-icompleting-read
15325 (if default-prop (concat " [" default-prop "]") "")
15327 (mapcar 'list keys)
15330 (if (member property keys)
15332 (or (cdr (assoc (downcase property)
15333 (mapcar (lambda (x) (cons (downcase x) x))
15337 (defun org-set-property-and-value (use-last)
15338 "Allow to set [PROPERTY]: [value] direction from prompt.
15339 When use-default, don't even ask, just use the last
15340 \"[PROPERTY]: [value]\" string from the history."
15342 (let* ((completion-ignore-case t)
15343 (pv (or (and use-last org-last-set-property-value)
15344 (org-completing-read
15345 "Enter a \"[Property]: [value]\" pair: "
15346 nil nil nil nil nil
15347 org-last-set-property-value)))
15349 (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
15350 (setq prop (match-string 1 pv)
15351 val (match-string 2 pv))
15352 (org-set-property prop val))))
15354 (defun org-set-property (property value)
15355 "In the current entry, set PROPERTY to VALUE.
15356 When called interactively, this will prompt for a property name, offering
15357 completion on existing and default properties. And then it will prompt
15358 for a value, offering completion either on allowed values (via an inherited
15359 xxx_ALL property) or on existing values in other instances of this property
15360 in the current file."
15361 (interactive (list nil nil))
15362 (let* ((property (or property (org-read-property-name)))
15363 (value (or value (org-read-property-value property)))
15364 (fn (cdr (assoc property org-properties-postprocess-alist))))
15365 (setq org-last-set-property property)
15366 (setq org-last-set-property-value (concat property ": " value))
15367 ;; Possibly postprocess the inserted value:
15368 (when fn (setq value (funcall fn value)))
15369 (unless (equal (org-entry-get nil property) value)
15370 (org-entry-put nil property value))))
15372 (defun org-delete-property (property &optional delete-empty-drawer)
15373 "In the current entry, delete PROPERTY.
15374 When optional argument DELETE-EMPTY-DRAWER is a string, it defines
15375 an empty drawer to delete."
15377 (let* ((completion-ignore-case t)
15378 (prop (org-icompleting-read "Property: "
15379 (org-entry-properties nil 'standard))))
15381 (message "Property %s %s" property
15382 (if (org-entry-delete nil property delete-empty-drawer)
15384 "was not present in the entry")))
15386 (defun org-delete-property-globally (property)
15387 "Remove PROPERTY globally, from all entries."
15389 (let* ((completion-ignore-case t)
15390 (prop (org-icompleting-read
15391 "Globally remove property: "
15392 (mapcar 'list (org-buffer-property-keys)))))
15397 (goto-char (point-min))
15399 (while (re-search-forward
15400 (org-re-property property)
15402 (setq cnt (1+ cnt))
15403 (delete-region (match-beginning 0) (1+ (point-at-eol))))
15404 (message "Property \"%s\" removed from %d entries" property cnt)))))
15406 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
15408 (defun org-compute-property-at-point ()
15409 "Compute the property at point.
15410 This looks for an enclosing column format, extracts the operator and
15411 then applies it to the property in the column format's scope."
15413 (unless (org-at-property-p)
15414 (error "Not at a property"))
15415 (let ((prop (org-match-string-no-properties 2)))
15416 (org-columns-get-format-and-top-level)
15417 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
15418 (error "No operator defined for property %s" prop))
15419 (org-columns-compute prop)))
15421 (defvar org-property-allowed-value-functions nil
15422 "Hook for functions supplying allowed values for a specific property.
15423 The functions must take a single argument, the name of the property, and
15424 return a flat list of allowed values. If \":ETC\" is one of
15425 the values, this means that these values are intended as defaults for
15426 completion, but that other values should be allowed too.
15427 The functions must return nil if they are not responsible for this
15430 (defun org-property-get-allowed-values (pom property &optional table)
15431 "Get allowed values for the property PROPERTY.
15432 When TABLE is non-nil, return an alist that can directly be used for
15436 ((equal property "TODO")
15437 (setq vals (org-with-point-at pom
15438 (append org-todo-keywords-1 '("")))))
15439 ((equal property "PRIORITY")
15440 (let ((n org-lowest-priority))
15441 (while (>= n org-highest-priority)
15442 (push (char-to-string n) vals)
15444 ((member property org-special-properties))
15445 ((setq vals (run-hook-with-args-until-success
15446 'org-property-allowed-value-functions property)))
15448 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
15449 (when (and vals (string-match "\\S-" vals))
15450 (setq vals (car (read-from-string (concat "(" vals ")"))))
15451 (setq vals (mapcar (lambda (x)
15452 (cond ((stringp x) x)
15453 ((numberp x) (number-to-string x))
15454 ((symbolp x) (symbol-name x))
15457 (when (member ":ETC" vals)
15458 (setq vals (remove ":ETC" vals))
15459 (org-add-props (car vals) '(org-unrestricted t)))
15460 (if table (mapcar 'list vals) vals)))
15462 (defun org-property-previous-allowed-value (&optional previous)
15463 "Switch to the next allowed value for this property."
15465 (org-property-next-allowed-value t))
15467 (defun org-property-next-allowed-value (&optional previous)
15468 "Switch to the next allowed value for this property."
15470 (unless (org-at-property-p)
15471 (error "Not at a property"))
15472 (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
15473 (key (match-string 2))
15474 (value (match-string 3))
15475 (allowed (or (org-property-get-allowed-values (point) key)
15476 (and (member value '("[ ]" "[-]" "[X]"))
15478 (heading (save-match-data (nth 4 (org-heading-components))))
15481 (error "Allowed values for this property have not been defined"))
15482 (if previous (setq allowed (reverse allowed)))
15483 (if (member value allowed)
15484 (setq nval (car (cdr (member value allowed)))))
15485 (setq nval (or nval (car allowed)))
15486 (if (equal nval value)
15487 (error "Only one allowed value for this property"))
15488 (org-at-property-p)
15489 (replace-match (concat " :" key ": " nval) t t)
15491 (beginning-of-line 1)
15492 (skip-chars-forward " \t")
15493 (when (equal prop org-effort-property)
15495 (org-back-to-heading t)
15496 (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
15497 (when (string= org-clock-current-task heading)
15498 (setq org-clock-effort nval)
15499 (org-clock-update-mode-line)))
15500 (run-hook-with-args 'org-property-changed-functions key nval)))
15502 (defun org-find-olp (path &optional this-buffer)
15503 "Return a marker pointing to the entry at outline path OLP.
15504 If anything goes wrong, throw an error.
15505 You can wrap this call to catch the error like this:
15507 (condition-case msg
15508 (org-mobile-locate-entry (match-string 4))
15509 (error (nth 1 msg)))
15511 The return value will then be either a string with the error message,
15512 or a marker if everything is OK.
15514 If THIS-BUFFER is set, the outline path does not contain a file,
15516 (let* ((file (if this-buffer buffer-file-name (pop path)))
15517 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
15521 limit re end found pos heading cnt flevel)
15522 (unless buffer (error "File not found :%s" file))
15523 (with-current-buffer buffer
15527 (setq limit (point-max))
15528 (goto-char (point-min))
15529 (while (setq heading (pop path))
15530 (setq re (format org-complex-heading-regexp-format
15531 (regexp-quote heading)))
15532 (setq cnt 0 pos (point))
15533 (while (re-search-forward re end t)
15534 (setq level (- (match-end 1) (match-beginning 1)))
15535 (if (and (>= level lmin) (<= level lmax))
15536 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
15537 (when (= cnt 0) (error "Heading not found on level %d: %s"
15539 (when (> cnt 1) (error "Heading not unique on level %d: %s"
15542 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
15543 (setq end (save-excursion (org-end-of-subtree t t))))
15544 (when (org-at-heading-p)
15545 (point-marker)))))))
15547 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
15548 "Find node HEADING in BUFFER.
15549 Return a marker to the heading if it was found, or nil if not.
15550 If POS-ONLY is set, return just the position instead of a marker.
15552 The heading text must match exact, but it may have a TODO keyword,
15553 a priority cookie and tags in the standard locations."
15554 (with-current-buffer (or buffer (current-buffer))
15558 (goto-char (point-min))
15559 (let (case-fold-search)
15560 (if (re-search-forward
15561 (format org-complex-heading-regexp-format
15562 (regexp-quote heading)) nil t)
15564 (match-beginning 0)
15565 (move-marker (make-marker) (match-beginning 0)))))))))
15567 (defun org-find-exact-heading-in-directory (heading &optional dir)
15568 "Find Org node headline HEADING in all .org files in directory DIR.
15569 When the target headline is found, return a marker to this location."
15570 (let ((files (directory-files (or dir default-directory)
15571 nil "\\`[^.#].*\\.org\\'"))
15572 file visiting m buffer)
15574 (while (setq file (pop files))
15575 (message "trying %s" file)
15576 (setq visiting (org-find-base-buffer-visiting file))
15577 (setq buffer (or visiting (find-file-noselect file)))
15578 (setq m (org-find-exact-headline-in-buffer
15580 (when (and (not m) (not visiting)) (kill-buffer buffer))
15581 (and m (throw 'found m))))))
15583 (defun org-find-entry-with-id (ident)
15584 "Locate the entry that contains the ID property with exact value IDENT.
15585 IDENT can be a string, a symbol or a number, this function will search for
15586 the string representation of it.
15587 Return the position where this entry starts, or nil if there is no such entry."
15588 (interactive "sID: ")
15590 ((stringp ident) ident)
15591 ((symbol-name ident) (symbol-name ident))
15592 ((numberp ident) (number-to-string ident))
15593 (t (error "IDENT %s must be a string, symbol or number" ident))))
15594 (case-fold-search nil))
15598 (goto-char (point-min))
15599 (when (re-search-forward
15600 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
15602 (org-back-to-heading t)
15607 (defvar org-last-changed-timestamp nil)
15608 (defvar org-last-inserted-timestamp nil
15609 "The last time stamp inserted with `org-insert-time-stamp'.")
15610 (defvar org-time-was-given) ; dynamically scoped parameter
15611 (defvar org-end-time-was-given) ; dynamically scoped parameter
15612 (defvar org-ts-what) ; dynamically scoped parameter
15614 (defun org-time-stamp (arg &optional inactive)
15615 "Prompt for a date/time and insert a time stamp.
15616 If the user specifies a time like HH:MM or if this command is
15617 called with at least one prefix argument, the time stamp contains
15618 the date and the time. Otherwise, only the date is be included.
15620 All parts of a date not specified by the user is filled in from
15621 the current date/time. So if you just press return without
15622 typing anything, the time stamp will represent the current
15625 If there is already a timestamp at the cursor, it will be
15628 With two universal prefix arguments, insert an active timestamp
15629 with the current time without prompting the user."
15633 ;; Default time is either today, or, when entering a range,
15634 ;; the range start.
15635 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
15637 (re-search-backward
15638 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
15639 (- (point) 20) t)))
15640 (apply 'encode-time (org-parse-time-string (match-string 1)))
15642 (default-input (and ts (org-get-compact-tod ts)))
15643 (repeater (save-excursion
15645 (beginning-of-line)
15646 (when (re-search-forward
15647 "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
15648 (save-excursion (progn (end-of-line) (point))) t)
15649 (match-string 0)))))
15650 org-time-was-given org-end-time-was-given time)
15652 ((and (org-at-timestamp-p t)
15653 (memq last-command '(org-time-stamp org-time-stamp-inactive))
15654 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
15656 (setq time (let ((this-command this-command))
15657 (org-read-date arg 'totime nil nil
15658 default-time default-input inactive)))
15659 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
15660 ((org-at-timestamp-p t)
15661 (setq time (let ((this-command this-command))
15662 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15663 (when (org-at-timestamp-p t) ; just to get the match data
15664 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
15666 (setq org-last-changed-timestamp
15667 (org-insert-time-stamp
15668 time (or org-time-was-given arg)
15669 inactive nil nil (list org-end-time-was-given)))
15670 (when repeater (goto-char (1- (point))) (insert " " repeater)
15671 (setq org-last-changed-timestamp
15672 (concat (substring org-last-inserted-timestamp 0 -1)
15673 " " repeater ">"))))
15674 (message "Timestamp updated"))
15676 (org-insert-time-stamp (current-time) t))
15678 (setq time (let ((this-command this-command))
15679 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15680 (org-insert-time-stamp time (or org-time-was-given arg) inactive
15681 nil nil (list org-end-time-was-given))))))
15683 ;; FIXME: can we use this for something else, like computing time differences?
15684 (defun org-get-compact-tod (s)
15685 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
15686 (let* ((t1 (match-string 1 s))
15687 (h1 (string-to-number (match-string 2 s)))
15688 (m1 (string-to-number (match-string 3 s)))
15689 (t2 (and (match-end 4) (match-string 5 s)))
15690 (h2 (and t2 (string-to-number (match-string 6 s))))
15691 (m2 (and t2 (string-to-number (match-string 7 s))))
15695 (setq dh (- h2 h1) dm (- m2 m1))
15696 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
15697 (concat t1 "+" (number-to-string dh)
15698 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
15700 (defun org-time-stamp-inactive (&optional arg)
15701 "Insert an inactive time stamp.
15702 An inactive time stamp is enclosed in square brackets instead of angle
15703 brackets. It is inactive in the sense that it does not trigger agenda entries,
15704 does not link to the calendar and cannot be changed with the S-cursor keys.
15705 So these are more for recording a certain time/date."
15707 (org-time-stamp arg 'inactive))
15709 (defvar org-date-ovl (make-overlay 1 1))
15710 (overlay-put org-date-ovl 'face 'org-date-selected)
15711 (org-detach-overlay org-date-ovl)
15713 (defvar org-ans1) ; dynamically scoped parameter
15714 (defvar org-ans2) ; dynamically scoped parameter
15716 (defvar org-plain-time-of-day-regexp) ; defined below
15718 (defvar org-overriding-default-time nil) ; dynamically scoped
15719 (defvar org-read-date-overlay nil)
15720 (defvar org-dcst nil) ; dynamically scoped
15721 (defvar org-read-date-history nil)
15722 (defvar org-read-date-final-answer nil)
15723 (defvar org-read-date-analyze-futurep nil)
15724 (defvar org-read-date-analyze-forced-year nil)
15725 (defvar org-read-date-inactive)
15727 (defvar org-read-date-minibuffer-local-map
15728 (let ((map (make-sparse-keymap)))
15729 (set-keymap-parent map minibuffer-local-map)
15730 (org-defkey map (kbd ".")
15731 (lambda () (interactive)
15732 (org-eval-in-calendar '(calendar-goto-today))))
15733 (org-defkey map [(meta shift left)]
15734 (lambda () (interactive)
15735 (org-eval-in-calendar '(calendar-backward-month 1))))
15736 (org-defkey map [(meta shift right)]
15737 (lambda () (interactive)
15738 (org-eval-in-calendar '(calendar-forward-month 1))))
15739 (org-defkey map [(meta shift up)]
15740 (lambda () (interactive)
15741 (org-eval-in-calendar '(calendar-backward-year 1))))
15742 (org-defkey map [(meta shift down)]
15743 (lambda () (interactive)
15744 (org-eval-in-calendar '(calendar-forward-year 1))))
15745 (org-defkey map [?\e (shift left)]
15746 (lambda () (interactive)
15747 (org-eval-in-calendar '(calendar-backward-month 1))))
15748 (org-defkey map [?\e (shift right)]
15749 (lambda () (interactive)
15750 (org-eval-in-calendar '(calendar-forward-month 1))))
15751 (org-defkey map [?\e (shift up)]
15752 (lambda () (interactive)
15753 (org-eval-in-calendar '(calendar-backward-year 1))))
15754 (org-defkey map [?\e (shift down)]
15755 (lambda () (interactive)
15756 (org-eval-in-calendar '(calendar-forward-year 1))))
15757 (org-defkey map [(shift up)]
15758 (lambda () (interactive)
15759 (org-eval-in-calendar '(calendar-backward-week 1))))
15760 (org-defkey map [(shift down)]
15761 (lambda () (interactive)
15762 (org-eval-in-calendar '(calendar-forward-week 1))))
15763 (org-defkey map [(shift left)]
15764 (lambda () (interactive)
15765 (org-eval-in-calendar '(calendar-backward-day 1))))
15766 (org-defkey map [(shift right)]
15767 (lambda () (interactive)
15768 (org-eval-in-calendar '(calendar-forward-day 1))))
15769 (org-defkey map "!"
15770 (lambda () (interactive)
15771 (org-eval-in-calendar '(diary-view-entries))
15773 (org-defkey map ">"
15774 (lambda () (interactive)
15775 (org-eval-in-calendar '(scroll-calendar-left 1))))
15776 (org-defkey map "<"
15777 (lambda () (interactive)
15778 (org-eval-in-calendar '(scroll-calendar-right 1))))
15779 (org-defkey map "\C-v"
15780 (lambda () (interactive)
15781 (org-eval-in-calendar
15782 '(calendar-scroll-left-three-months 1))))
15783 (org-defkey map "\M-v"
15784 (lambda () (interactive)
15785 (org-eval-in-calendar
15786 '(calendar-scroll-right-three-months 1))))
15788 "Keymap for minibuffer commands when using `org-read-date'.")
15790 (defun org-read-date (&optional org-with-time to-time from-string prompt
15791 default-time default-input inactive)
15792 "Read a date, possibly a time, and make things smooth for the user.
15793 The prompt will suggest to enter an ISO date, but you can also enter anything
15794 which will at least partially be understood by `parse-time-string'.
15795 Unrecognized parts of the date will default to the current day, month, year,
15796 hour and minute. If this command is called to replace a timestamp at point,
15797 or to enter the second timestamp of a range, the default time is taken
15798 from the existing stamp. Furthermore, the command prefers the future,
15799 so if you are giving a date where the year is not given, and the day-month
15800 combination is already past in the current year, it will assume you
15801 mean next year. For details, see the manual. A few examples:
15803 3-2-5 --> 2003-02-05
15804 feb 15 --> currentyear-02-15
15805 2/15 --> currentyear-02-15
15806 sep 12 9 --> 2009-09-12
15807 12:45 --> today 12:45
15808 22 sept 0:34 --> currentyear-09-22 0:34
15809 12 --> currentyear-currentmonth-12
15810 Fri --> nearest Friday (today or later)
15813 Furthermore you can specify a relative date by giving, as the *first* thing
15814 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
15815 change in days weeks, months, years.
15816 With a single plus or minus, the date is relative to today. With a double
15817 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
15818 +4d --> four days from today
15819 +4 --> same as above
15820 +2w --> two weeks from today
15821 ++5 --> five days from default date
15823 The function understands only English month and weekday abbreviations.
15825 While prompting, a calendar is popped up - you can also select the
15826 date with the mouse (button 1). The calendar shows a period of three
15827 months. To scroll it to other months, use the keys `>' and `<'.
15828 If you don't like the calendar, turn it off with
15829 \(setq org-read-date-popup-calendar nil)
15831 With optional argument TO-TIME, the date will immediately be converted
15832 to an internal time.
15833 With an optional argument ORG-WITH-TIME, the prompt will suggest to
15834 also insert a time. Note that when ORG-WITH-TIME is not set, you can
15835 still enter a time, and this function will inform the calling routine
15836 about this change. The calling routine may then choose to change the
15837 format used to insert the time stamp into the buffer to include the time.
15838 With optional argument FROM-STRING, read from this string instead from
15839 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
15840 the time/date that is used for everything that is not specified by the
15842 (require 'parse-time)
15843 (let* ((org-time-stamp-rounding-minutes
15844 (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
15845 (org-dcst org-display-custom-times)
15846 (ct (org-current-time))
15847 (org-def (or org-overriding-default-time default-time ct))
15848 (org-defdecode (decode-time org-def))
15850 (when (< (nth 2 org-defdecode) org-extend-today-until)
15851 (setcar (nthcdr 2 org-defdecode) -1)
15852 (setcar (nthcdr 1 org-defdecode) 59)
15853 (setq org-def (apply 'encode-time org-defdecode)
15854 org-defdecode (decode-time org-def)))))
15855 (mouse-autoselect-window nil) ; Don't let the mouse jump
15856 (calendar-frame-setup nil)
15857 (calendar-setup nil)
15858 (calendar-move-hook nil)
15859 (calendar-view-diary-initially-flag nil)
15860 (calendar-view-holidays-initially-flag nil)
15861 (timestr (format-time-string
15862 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
15863 (prompt (concat (if prompt (concat prompt " ") "")
15864 (format "Date+time [%s]: " timestr)))
15865 ans (org-ans0 "") org-ans1 org-ans2 final)
15868 (from-string (setq ans from-string))
15869 (org-read-date-popup-calendar
15871 (save-window-excursion
15873 (org-eval-in-calendar '(setq cursor-type nil) t)
15876 (calendar-forward-day (- (time-to-days org-def)
15877 (calendar-absolute-from-gregorian
15878 (calendar-current-date))))
15879 (org-eval-in-calendar nil t)
15880 (let* ((old-map (current-local-map))
15881 (map (copy-keymap calendar-mode-map))
15882 (minibuffer-local-map
15883 (copy-keymap org-read-date-minibuffer-local-map)))
15884 (org-defkey map (kbd "RET") 'org-calendar-select)
15885 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
15886 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
15889 (use-local-map map)
15890 (setq org-read-date-inactive inactive)
15891 (add-hook 'post-command-hook 'org-read-date-display)
15892 (setq org-ans0 (read-string prompt default-input
15893 'org-read-date-history nil))
15894 ;; org-ans0: from prompt
15895 ;; org-ans1: from mouse click
15896 ;; org-ans2: from calendar motion
15897 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
15898 (remove-hook 'post-command-hook 'org-read-date-display)
15899 (use-local-map old-map)
15900 (when org-read-date-overlay
15901 (delete-overlay org-read-date-overlay)
15902 (setq org-read-date-overlay nil)))))
15903 (bury-buffer "*Calendar*")))))
15905 (t ; Naked prompt only
15907 (setq ans (read-string prompt default-input
15908 'org-read-date-history timestr))
15909 (when org-read-date-overlay
15910 (delete-overlay org-read-date-overlay)
15911 (setq org-read-date-overlay nil)))))
15913 (setq final (org-read-date-analyze ans org-def org-defdecode))
15915 (when org-read-date-analyze-forced-year
15916 (message "Year was forced into %s"
15917 (if org-read-date-force-compatible-dates
15918 "compatible range (1970-2037)"
15919 "range representable on this machine"))
15922 ;; One round trip to get rid of 34th of August and stuff like that....
15923 (setq final (decode-time (apply 'encode-time final)))
15925 (setq org-read-date-final-answer ans)
15928 (apply 'encode-time final)
15929 (if (and (boundp 'org-time-was-given) org-time-was-given)
15930 (format "%04d-%02d-%02d %02d:%02d"
15931 (nth 5 final) (nth 4 final) (nth 3 final)
15932 (nth 2 final) (nth 1 final))
15933 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
15936 (defvar org-defdecode)
15937 (defvar org-with-time)
15938 (defun org-read-date-display ()
15939 "Display the current date prompt interpretation in the minibuffer."
15940 (when org-read-date-display-live
15941 (when org-read-date-overlay
15942 (delete-overlay org-read-date-overlay))
15943 (when (minibufferp (current-buffer))
15946 (while (not (equal (buffer-substring
15947 (max (point-min) (- (point) 4)) (point))
15950 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
15951 " " (or org-ans1 org-ans2)))
15952 (org-end-time-was-given nil)
15953 (f (org-read-date-analyze ans org-def org-defdecode))
15955 org-time-stamp-custom-formats
15956 org-time-stamp-formats))
15957 (fmt (if (or org-with-time
15958 (and (boundp 'org-time-was-given) org-time-was-given))
15961 (txt (format-time-string fmt (apply 'encode-time f)))
15962 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
15963 (txt (concat "=> " txt)))
15964 (when (and org-end-time-was-given
15965 (string-match org-plain-time-of-day-regexp txt))
15966 (setq txt (concat (substring txt 0 (match-end 0)) "-"
15967 org-end-time-was-given
15968 (substring txt (match-end 0)))))
15969 (when org-read-date-analyze-futurep
15970 (setq txt (concat txt " (=>F)")))
15971 (setq org-read-date-overlay
15972 (make-overlay (1- (point-at-eol)) (point-at-eol)))
15973 (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
15975 (defun org-read-date-analyze (ans org-def org-defdecode)
15976 "Analyze the combined answer of the date prompt."
15977 ;; FIXME: cleanup and comment
15978 (let ((nowdecode (decode-time (current-time)))
15979 delta deltan deltaw deltadef year month day
15980 hour minute second wday pm h2 m2 tl wday1
15981 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
15982 (setq org-read-date-analyze-futurep nil
15983 org-read-date-analyze-forced-year nil)
15984 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
15987 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
15988 (setq ans (replace-match "" t t ans)
15990 deltaw (nth 1 delta)
15991 deltadef (nth 2 delta)))
15993 ;; Check if there is an iso week date in there. If yes, store the
15994 ;; info and postpone interpreting it until the rest of the parsing
15996 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
15997 (setq iso-year (if (match-end 1)
15998 (org-small-year-to-year
15999 (string-to-number (match-string 1 ans))))
16000 iso-weekday (if (match-end 3)
16001 (string-to-number (match-string 3 ans)))
16002 iso-week (string-to-number (match-string 2 ans)))
16003 (setq ans (replace-match "" t t ans)))
16005 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
16006 (when (string-match
16007 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
16008 (setq year (if (match-end 2)
16009 (string-to-number (match-string 2 ans))
16010 (progn (setq kill-year t)
16011 (string-to-number (format-time-string "%Y"))))
16012 month (string-to-number (match-string 3 ans))
16013 day (string-to-number (match-string 4 ans)))
16014 (if (< year 100) (setq year (+ 2000 year)))
16015 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
16018 ;; Help matching dotted european dates
16019 (when (string-match
16020 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
16021 (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
16023 (string-to-number (format-time-string "%Y")))
16024 day (string-to-number (match-string 1 ans))
16025 month (string-to-number (match-string 2 ans))
16026 ans (replace-match (format "%04d-%02d-%02d" year month day)
16029 ;; Help matching american dates, like 5/30 or 5/30/7
16030 (when (string-match
16031 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
16032 (setq year (if (match-end 4)
16033 (string-to-number (match-string 4 ans))
16034 (progn (setq kill-year t)
16035 (string-to-number (format-time-string "%Y"))))
16036 month (string-to-number (match-string 1 ans))
16037 day (string-to-number (match-string 2 ans)))
16038 (if (< year 100) (setq year (+ 2000 year)))
16039 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
16041 ;; Help matching am/pm times, because `parse-time-string' does not do that.
16042 ;; If there is a time with am/pm, and *no* time without it, we convert
16043 ;; so that matching will be successful.
16044 (loop for i from 1 to 2 do ; twice, for end time as well
16045 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
16046 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
16047 (setq hour (string-to-number (match-string 1 ans))
16048 minute (if (match-end 3)
16049 (string-to-number (match-string 3 ans))
16052 (string-to-char (downcase (match-string 4 ans)))))
16053 (if (and (= hour 12) (not pm))
16055 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
16056 (setq ans (replace-match (format "%02d:%02d" hour minute)
16059 ;; Check if a time range is given as a duration
16060 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
16061 (setq hour (string-to-number (match-string 1 ans))
16062 h2 (+ hour (string-to-number (match-string 3 ans)))
16063 minute (string-to-number (match-string 2 ans))
16064 m2 (+ minute (if (match-end 5) (string-to-number
16065 (match-string 5 ans))0)))
16066 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
16067 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
16070 ;; Check if there is a time range
16071 (when (boundp 'org-end-time-was-given)
16072 (setq org-time-was-given nil)
16073 (when (and (string-match org-plain-time-of-day-regexp ans)
16075 (setq org-end-time-was-given (match-string 8 ans))
16076 (setq ans (concat (substring ans 0 (match-beginning 7))
16077 (substring ans (match-end 7))))))
16079 (setq tl (parse-time-string ans)
16080 day (or (nth 3 tl) (nth 3 org-defdecode))
16081 month (or (nth 4 tl)
16082 (if (and org-read-date-prefer-future
16083 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
16084 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
16085 (nth 4 org-defdecode)))
16086 year (or (and (not kill-year) (nth 5 tl))
16087 (if (and org-read-date-prefer-future
16088 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
16089 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
16090 (nth 5 org-defdecode)))
16091 hour (or (nth 2 tl) (nth 2 org-defdecode))
16092 minute (or (nth 1 tl) (nth 1 org-defdecode))
16093 second (or (nth 0 tl) 0)
16096 (when (and (eq org-read-date-prefer-future 'time)
16097 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
16098 (equal day (nth 3 nowdecode))
16099 (equal month (nth 4 nowdecode))
16100 (equal year (nth 5 nowdecode))
16102 (or (< (nth 2 tl) (nth 2 nowdecode))
16103 (and (= (nth 2 tl) (nth 2 nowdecode))
16105 (< (nth 1 tl) (nth 1 nowdecode)))))
16109 ;; Special date definitions below
16112 ;; There was an iso week
16115 (setq year (or iso-year year)
16116 day (or iso-weekday wday 1)
16117 wday nil ; to make sure that the trigger below does not match
16118 iso-date (calendar-gregorian-from-absolute
16119 (calendar-absolute-from-iso
16120 (list iso-week day year))))
16121 ; FIXME: Should we also push ISO weeks into the future?
16122 ; (when (and org-read-date-prefer-future
16124 ; (< (calendar-absolute-from-gregorian iso-date)
16125 ; (time-to-days (current-time))))
16126 ; (setq year (1+ year)
16127 ; iso-date (calendar-gregorian-from-absolute
16128 ; (calendar-absolute-from-iso
16129 ; (list iso-week day year)))))
16130 (setq month (car iso-date)
16131 year (nth 2 iso-date)
16132 day (nth 1 iso-date)))
16136 (let ((now (decode-time (current-time))))
16137 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
16138 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
16139 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
16140 ((equal deltaw "m") (setq month (+ month deltan)))
16141 ((equal deltaw "y") (setq year (+ year deltan)))))
16142 ((and wday (not (nth 3 tl)))
16143 ;; Weekday was given, but no day, so pick that day in the week
16144 ;; on or after the derived date.
16145 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
16146 (unless (equal wday wday1)
16147 (setq day (+ day (% (- wday wday1 -7) 7))))))
16148 (if (and (boundp 'org-time-was-given)
16150 (setq org-time-was-given t))
16151 (if (< year 100) (setq year (+ 2000 year)))
16152 ;; Check of the date is representable
16153 (if org-read-date-force-compatible-dates
16156 (setq year 1970 org-read-date-analyze-forced-year t))
16158 (setq year 2037 org-read-date-analyze-forced-year t)))
16159 (condition-case nil
16160 (ignore (encode-time second minute hour day month year))
16162 (setq year (nth 5 org-defdecode))
16163 (setq org-read-date-analyze-forced-year t))))
16164 (setq org-read-date-analyze-futurep futurep)
16165 (list second minute hour day month year)))
16167 (defvar parse-time-weekdays)
16168 (defun org-read-date-get-relative (s today default)
16169 "Check string S for special relative date string.
16170 TODAY and DEFAULT are internal times, for today and for a default.
16171 Return shift list (N what def-flag)
16172 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
16173 N is the number of WHATs to shift.
16174 DEF-FLAG is t when a double ++ or -- indicates shift relative to
16175 the DEFAULT date rather than TODAY."
16176 (require 'parse-time)
16180 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
16182 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
16183 "\\([ \t]\\|$\\)") s)
16184 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
16185 (let* ((dir (if (> (match-end 1) (match-beginning 1))
16186 (string-to-char (substring (match-string 1 s) -1))
16188 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
16189 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
16190 (what (if (match-end 3) (match-string 3 s) "d"))
16191 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
16192 (date (if rel default today))
16193 (wday (nth 6 (decode-time date)))
16197 (setq delta (mod (+ 7 (- wday1 wday)) 7))
16198 (if (= dir ?-) (setq delta (- delta 7)))
16199 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
16200 (list delta "d" rel))
16201 (list (* n (if (= dir ?-) -1 1)) what rel)))))
16203 (defun org-order-calendar-date-args (arg1 arg2 arg3)
16204 "Turn a user-specified date into the internal representation.
16205 The internal representation needed by the calendar is (month day year).
16206 This is a wrapper to handle the brain-dead convention in calendar that
16207 user function argument order change dependent on argument order."
16208 (if (boundp 'calendar-date-style)
16210 ((eq calendar-date-style 'american)
16211 (list arg1 arg2 arg3))
16212 ((eq calendar-date-style 'european)
16213 (list arg2 arg1 arg3))
16214 ((eq calendar-date-style 'iso)
16215 (list arg2 arg3 arg1)))
16216 (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
16217 (if (org-bound-and-true-p european-calendar-style)
16218 (list arg2 arg1 arg3)
16219 (list arg1 arg2 arg3)))))
16221 (defun org-eval-in-calendar (form &optional keepdate)
16222 "Eval FORM in the calendar window and return to current window.
16223 When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
16224 otherwise stick to the current value of `org-ans2'."
16225 (let ((sf (selected-frame))
16226 (sw (selected-window)))
16227 (select-window (get-buffer-window "*Calendar*" t))
16229 (when (and (not keepdate) (calendar-cursor-to-date))
16230 (let* ((date (calendar-cursor-to-date))
16231 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16232 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
16233 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
16235 (org-select-frame-set-input-focus sf)))
16237 (defun org-calendar-select ()
16238 "Return to `org-read-date' with the date currently selected.
16239 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
16241 (when (calendar-cursor-to-date)
16242 (let* ((date (calendar-cursor-to-date))
16243 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16244 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
16245 (if (active-minibuffer-window) (exit-minibuffer))))
16247 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
16248 "Insert a date stamp for the date given by the internal TIME.
16249 WITH-HM means use the stamp format that includes the time of the day.
16250 INACTIVE means use square brackets instead of angular ones, so that the
16251 stamp will not contribute to the agenda.
16252 PRE and POST are optional strings to be inserted before and after the
16254 The command returns the inserted time stamp."
16255 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
16257 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
16258 (insert-before-markers (or pre ""))
16259 (when (listp extra)
16260 (setq extra (car extra))
16261 (if (and (stringp extra)
16262 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
16263 (setq extra (format "-%02d:%02d"
16264 (string-to-number (match-string 1 extra))
16265 (string-to-number (match-string 2 extra))))
16268 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
16269 (insert-before-markers (setq stamp (format-time-string fmt time)))
16270 (insert-before-markers (or post ""))
16271 (setq org-last-inserted-timestamp stamp)))
16273 (defun org-toggle-time-stamp-overlays ()
16274 "Toggle the use of custom time stamp formats."
16276 (setq org-display-custom-times (not org-display-custom-times))
16277 (unless org-display-custom-times
16278 (let ((p (point-min)) (bmp (buffer-modified-p)))
16279 (while (setq p (next-single-property-change p 'display))
16280 (if (and (get-text-property p 'display)
16281 (eq (get-text-property p 'face) 'org-date))
16282 (remove-text-properties
16283 p (setq p (next-single-property-change p 'display))
16285 (set-buffer-modified-p bmp)))
16286 (if (featurep 'xemacs)
16287 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
16288 (org-restart-font-lock)
16289 (setq org-table-may-need-update t)
16290 (if org-display-custom-times
16291 (message "Time stamps are overlaid with custom format")
16292 (message "Time stamp overlays removed")))
16294 (defun org-display-custom-time (beg end)
16295 "Overlay modified time stamp format over timestamp between BEG and END."
16296 (let* ((ts (buffer-substring beg end))
16297 t1 w1 with-hm tf time str w2 (off 0))
16299 (setq t1 (org-parse-time-string ts t))
16300 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
16301 (setq off (- (match-end 0) (match-beginning 0)))))
16302 (setq end (- end off))
16303 (setq w1 (- end beg)
16304 with-hm (and (nth 1 t1) (nth 2 t1))
16305 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
16306 time (org-fix-decoded-time t1)
16308 (format-time-string
16309 (substring tf 1 -1) (apply 'encode-time time))
16310 nil 'mouse-face 'highlight)
16312 (if (not (= w2 w1))
16313 (add-text-properties (1+ beg) (+ 2 beg)
16314 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
16315 (if (featurep 'xemacs)
16317 (put-text-property beg end 'invisible t)
16318 (put-text-property beg end 'end-glyph (make-glyph str)))
16319 (put-text-property beg end 'display str))))
16321 (defun org-translate-time (string)
16322 "Translate all timestamps in STRING to custom format.
16323 But do this only if the variable `org-display-custom-times' is set."
16324 (when org-display-custom-times
16327 (re org-ts-regexp-both)
16328 t1 with-hm inactive tf time str beg end)
16329 (while (setq start (string-match re string start))
16330 (setq beg (match-beginning 0)
16332 t1 (save-match-data
16333 (org-parse-time-string (substring string beg end) t))
16334 with-hm (and (nth 1 t1) (nth 2 t1))
16335 inactive (equal (substring string beg (1+ beg)) "[")
16336 tf (funcall (if with-hm 'cdr 'car)
16337 org-time-stamp-custom-formats)
16338 time (org-fix-decoded-time t1)
16339 str (format-time-string
16341 (if inactive "[" "<") (substring tf 1 -1)
16342 (if inactive "]" ">"))
16343 (apply 'encode-time time))
16344 string (replace-match str t t string)
16345 start (+ start (length str)))))))
16348 (defun org-fix-decoded-time (time)
16349 "Set 0 instead of nil for the first 6 elements of time.
16350 Don't touch the rest."
16352 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
16354 (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
16356 (defun org-time-stamp-to-now (timestamp-string &optional seconds)
16357 "Difference between TIMESTAMP-STRING and now in days.
16358 If SECONDS is non-nil, return the difference in seconds."
16359 (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
16360 (- (funcall fdiff (org-time-string-to-time timestamp-string))
16361 (funcall fdiff (current-time)))))
16363 (defun org-deadline-close (timestamp-string &optional ndays)
16364 "Is the time in TIMESTAMP-STRING close to the current date?"
16365 (setq ndays (or ndays (org-get-wdays timestamp-string)))
16366 (and (< (org-time-stamp-to-now timestamp-string) ndays)
16367 (not (org-entry-is-done-p))))
16369 (defun org-get-wdays (ts &optional delay zero-delay)
16370 "Get the deadline lead time appropriate for timestring TS.
16371 When DELAY is non-nil, get the delay time for scheduled items
16372 instead of the deadline lead time. When ZERO-DELAY is non-nil
16373 and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
16374 don't try to find the delay cookie in the scheduled timestamp."
16375 (let ((tv (if delay org-scheduled-delay-days
16376 org-deadline-warning-days)))
16378 ((or (and delay (< tv 0))
16379 (and delay zero-delay (<= tv 0))
16380 (and (not delay) (<= tv 0)))
16381 ;; Enforce this value no matter what
16383 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
16384 ;; lead time is specified.
16385 (floor (* (string-to-number (match-string 1 ts))
16386 (cdr (assoc (match-string 2 ts)
16387 '(("d" . 1) ("w" . 7)
16388 ("m" . 30.4) ("y" . 365.25)
16389 ("h" . 0.041667)))))))
16390 ;; go for the default.
16393 (defun org-calendar-select-mouse (ev)
16394 "Return to `org-read-date' with the date currently selected.
16395 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
16397 (mouse-set-point ev)
16398 (when (calendar-cursor-to-date)
16399 (let* ((date (calendar-cursor-to-date))
16400 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16401 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
16402 (if (active-minibuffer-window) (exit-minibuffer))))
16404 (defun org-check-deadlines (ndays)
16405 "Check if there are any deadlines due or past due.
16406 A deadline is considered due if it happens within `org-deadline-warning-days'
16407 days from today's date. If the deadline appears in an entry marked DONE,
16408 it is not shown. The prefix arg NDAYS can be used to test that many
16409 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
16411 (let* ((org-warn-days
16413 ((equal ndays '(4)) 100000)
16414 (ndays (prefix-numeric-value ndays))
16415 (t (abs org-deadline-warning-days))))
16416 (case-fold-search nil)
16417 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
16419 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
16421 (message "%d deadlines past-due or due within %d days"
16422 (org-occur regexp nil callback)
16425 (defsubst org-re-timestamp (type)
16426 "Return a regexp for timestamp TYPE.
16427 Allowed values for TYPE are:
16429 all: all timestamps
16430 active: only active timestamps (<...>)
16431 inactive: only inactive timestamps ([...])
16432 scheduled: only scheduled timestamps
16433 deadline: only deadline timestamps
16435 When TYPE is nil, fall back on returning a regexp that matches
16436 both scheduled and deadline timestamps."
16437 (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9>
\n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
16438 ((eq type 'active) org-ts-regexp)
16439 ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^
\n>]*?\\)\\]")
16440 ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
16441 ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
16442 ((eq type 'scheduled-or-deadline)
16443 (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
16445 (defun org-check-before-date (date)
16446 "Check if there are deadlines or scheduled entries before DATE."
16447 (interactive (list (org-read-date)))
16448 (let ((case-fold-search nil)
16449 (regexp (org-re-timestamp org-ts-type))
16451 (lambda () (time-less-p
16452 (org-time-string-to-time (match-string 1))
16453 (org-time-string-to-time date)))))
16454 (message "%d entries before %s"
16455 (org-occur regexp nil callback) date)))
16457 (defun org-check-after-date (date)
16458 "Check if there are deadlines or scheduled entries after DATE."
16459 (interactive (list (org-read-date)))
16460 (let ((case-fold-search nil)
16461 (regexp (org-re-timestamp org-ts-type))
16465 (org-time-string-to-time (match-string 1))
16466 (org-time-string-to-time date))))))
16467 (message "%d entries after %s"
16468 (org-occur regexp nil callback) date)))
16470 (defun org-check-dates-range (start-date end-date)
16471 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
16472 (interactive (list (org-read-date nil nil nil "Range starts")
16473 (org-read-date nil nil nil "Range end")))
16474 (let ((case-fold-search nil)
16475 (regexp (org-re-timestamp org-ts-type))
16478 (let ((match (match-string 1)))
16481 (org-time-string-to-time match)
16482 (org-time-string-to-time start-date)))
16484 (org-time-string-to-time match)
16485 (org-time-string-to-time end-date)))))))
16486 (message "%d entries between %s and %s"
16487 (org-occur regexp nil callback) start-date end-date)))
16489 (defun org-evaluate-time-range (&optional to-buffer)
16490 "Evaluate a time range by computing the difference between start and end.
16491 Normally the result is just printed in the echo area, but with prefix arg
16492 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
16493 If the time range is actually in a table, the result is inserted into the
16495 For time difference computation, a year is assumed to be exactly 365
16496 days in order to avoid rounding problems."
16499 (org-clock-update-time-maybe)
16501 (unless (org-at-date-range-p t)
16502 (goto-char (point-at-bol))
16503 (re-search-forward org-tr-regexp-both (point-at-eol) t))
16504 (if (not (org-at-date-range-p t))
16505 (error "Not at a time-stamp range, and none found in current line")))
16506 (let* ((ts1 (match-string 1))
16507 (ts2 (match-string 2))
16508 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
16509 (match-end (match-end 0))
16510 (time1 (org-time-string-to-time ts1))
16511 (time2 (org-time-string-to-time ts2))
16512 (t1 (org-float-time time1))
16513 (t2 (org-float-time time2))
16514 (diff (abs (- t2 t1)))
16515 (negative (< (- t2 t1) 0))
16516 ;; (ys (floor (* 365 24 60 60)))
16519 (fy "%dy %dd %02d:%02d")
16521 (fd "%dd %02d:%02d")
16526 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16528 d (floor (/ diff ds)) diff (mod diff ds)
16529 h (floor (/ diff hs)) diff (mod diff hs)
16530 m (floor (/ diff 60)))
16531 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16533 d (floor (+ (/ diff ds) 0.5))
16535 (if (not to-buffer)
16536 (message "%s" (org-make-tdiff-string y d h m))
16537 (if (org-at-table-p)
16539 (goto-char match-end)
16541 (and (looking-at " *|") (goto-char (match-end 0))))
16542 (goto-char match-end))
16544 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
16545 (replace-match ""))
16546 (if negative (insert " -"))
16547 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
16548 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
16549 (insert " " (format fh h m))))
16550 (if align (org-table-align))
16551 (message "Time difference inserted")))))
16553 (defun org-make-tdiff-string (y d h m)
16556 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
16558 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
16560 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
16562 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
16564 (apply 'format fmt (nreverse l))))
16566 (defun org-time-string-to-time (s &optional buffer pos)
16567 "Convert a timestamp string into internal time."
16568 (condition-case errdata
16569 (apply 'encode-time (org-parse-time-string s))
16570 (error (error "Bad timestamp `%s'%s\nError was: %s"
16571 s (if (not (and buffer pos))
16573 (format " at %d in buffer `%s'" pos buffer))
16576 (defun org-time-string-to-seconds (s)
16577 "Convert a timestamp string to a number of seconds."
16578 (org-float-time (org-time-string-to-time s)))
16580 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
16581 "Convert a time stamp to an absolute day number.
16582 If there is a specifier for a cyclic time stamp, get the closest
16584 PREFER and SHOW-ALL are passed through to `org-closest-date'.
16585 The variable date is bound by the calendar when this is called."
16587 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
16588 (if (org-diary-sexp-entry (match-string 1 s) "" date)
16591 ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
16592 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
16593 (time-to-days (current-time))) (match-string 0 s)
16596 (condition-case errdata
16597 (apply 'encode-time (org-parse-time-string s))
16598 (error (error "Bad timestamp `%s'%s\nError was: %s"
16599 s (if (not (and buffer pos))
16601 (format " at %d in buffer `%s'" pos buffer))
16602 (cdr errdata))))))))
16604 (defun org-days-to-iso-week (days)
16605 "Return the iso week number."
16607 (car (calendar-iso-from-absolute days)))
16609 (defun org-small-year-to-year (year)
16610 "Convert 2-digit years into 4-digit years.
16611 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
16612 The year 2000 cannot be abbreviated. Any year larger than 99
16613 is returned unchanged."
16615 (setq year (+ 2000 year))
16617 (setq year (+ 1900 year))))
16620 (defun org-time-from-absolute (d)
16621 "Return the time corresponding to date D.
16622 D may be an absolute day number, or a calendar-type list (month day year)."
16623 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
16624 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
16626 (defun org-calendar-holiday ()
16627 "List of holidays, for Diary display in Org-mode."
16628 (require 'holidays)
16630 (if (fboundp 'calendar-check-holidays)
16631 'calendar-check-holidays 'check-calendar-holidays) date)))
16632 (if hl (mapconcat 'identity hl "; "))))
16634 (defun org-diary-sexp-entry (sexp entry date)
16635 "Process a SEXP diary ENTRY for DATE."
16636 (require 'diary-lib)
16637 (let ((result (if calendar-debug-sexp
16638 (let ((stack-trace-on-error t))
16639 (eval (car (read-from-string sexp))))
16640 (condition-case nil
16641 (eval (car (read-from-string sexp)))
16644 (message "Bad sexp at line %d in %s: %s"
16646 (buffer-file-name) sexp)
16648 (cond ((stringp result) (split-string result "; "))
16649 ((and (consp result)
16650 (not (consp (cdr result)))
16651 (stringp (cdr result))) (cdr result))
16652 ((and (consp result)
16653 (stringp (car result))) result)
16656 (defun org-diary-to-ical-string (frombuf)
16657 "Get iCalendar entries from diary entries in buffer FROMBUF.
16658 This uses the icalendar.el library."
16659 (let* ((tmpdir (if (featurep 'xemacs)
16661 temporary-file-directory))
16662 (tmpfile (make-temp-name
16663 (expand-file-name "orgics" tmpdir)))
16665 (with-current-buffer frombuf
16666 (icalendar-export-region (point-min) (point-max) tmpfile)
16667 (setq buf (find-buffer-visiting tmpfile))
16669 (goto-char (point-min))
16670 (if (re-search-forward "^BEGIN:VEVENT" nil t)
16671 (setq b (match-beginning 0)))
16672 (goto-char (point-max))
16673 (if (re-search-backward "^END:VEVENT" nil t)
16674 (setq e (match-end 0)))
16675 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
16677 (delete-file tmpfile)
16680 (defun org-closest-date (start current change prefer show-all)
16681 "Find the date closest to CURRENT that is consistent with START and CHANGE.
16682 When PREFER is `past', return a date that is either CURRENT or past.
16683 When PREFER is `future', return a date that is either CURRENT or future.
16684 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
16685 ;; Make the proper lists from the dates
16687 (let ((a1 '(("h" . hour)
16692 (shour (nth 2 (org-parse-time-string start)))
16693 dn dw sday cday n1 n2 n0
16694 d m y y1 y2 date1 date2 nmonths nm ny m2)
16696 (setq start (org-date-to-gregorian start)
16697 current (org-date-to-gregorian
16700 (time-to-days (current-time))))
16701 sday (calendar-absolute-from-gregorian start)
16702 cday (calendar-absolute-from-gregorian current))
16704 (if (<= cday sday) (throw 'exit sday))
16706 (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
16707 (setq dn (string-to-number (match-string 1 change))
16708 dw (cdr (assoc (match-string 2 change) a1)))
16709 (error "Invalid change specifier: %s" change))
16710 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
16713 (let ((missing-hours
16714 (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
16716 (setq n1 (if (zerop missing-hours) cday
16717 (- cday (1+ (floor (/ missing-hours 24)))))
16718 n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
16720 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
16723 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
16724 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
16725 (setq date1 (list m d y1)
16726 n1 (calendar-absolute-from-gregorian date1)
16727 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
16728 n2 (calendar-absolute-from-gregorian date2)))
16730 ;; approx number of month between the two dates
16731 (setq nmonths (floor (/ (- cday sday) 30.436875)))
16732 ;; How often does dn fit in there?
16733 (setq d (nth 1 start) m (car start) y (nth 2 start)
16734 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
16736 ny (floor (/ m 12))
16739 (while (> m 12) (setq m (- m 12) y (1+ y)))
16740 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
16741 (setq m2 (+ m dn) y2 y)
16742 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16743 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
16744 (while (<= n2 cday)
16745 (setq n1 n2 m m2 y y2)
16746 (setq m2 (+ m dn) y2 y)
16747 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16748 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
16749 ;; Make sure n1 is the earlier date
16750 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
16753 ((eq prefer 'past) (if (= cday n2) n2 n1))
16754 ((eq prefer 'future) (if (= cday n1) n1 n2))
16755 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
16757 ((eq prefer 'past) (if (= cday n2) n2 n1))
16758 ((eq prefer 'future) (if (= cday n1) n1 n2))
16759 (t (if (= cday n1) n1 n2)))))))
16761 (defun org-date-to-gregorian (date)
16762 "Turn any specification of DATE into a Gregorian date for the calendar."
16763 (cond ((integerp date) (calendar-gregorian-from-absolute date))
16764 ((and (listp date) (= (length date) 3)) date)
16766 (setq date (org-parse-time-string date))
16767 (list (nth 4 date) (nth 3 date) (nth 5 date)))
16769 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
16771 (defun org-parse-time-string (s &optional nodefault)
16772 "Parse the standard Org-mode time string.
16773 This should be a lot faster than the normal `parse-time-string'.
16774 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
16775 hour and minute fields will be nil if not given."
16776 (cond ((string-match org-ts-regexp0 s)
16778 (if (or (match-beginning 8) (not nodefault))
16779 (string-to-number (or (match-string 8 s) "0")))
16780 (if (or (match-beginning 7) (not nodefault))
16781 (string-to-number (or (match-string 7 s) "0")))
16782 (string-to-number (match-string 4 s))
16783 (string-to-number (match-string 3 s))
16784 (string-to-number (match-string 2 s))
16786 ((string-match "^<[^>]+>$" s)
16787 (decode-time (seconds-to-time (org-matcher-time s))))
16788 (t (error "Not a standard Org-mode time string: %s" s))))
16790 (defun org-timestamp-up (&optional arg)
16791 "Increase the date item at the cursor by one.
16792 If the cursor is on the year, change the year. If it is on the month,
16793 the day or the time, change that.
16794 With prefix ARG, change by that many units."
16796 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
16798 (defun org-timestamp-down (&optional arg)
16799 "Decrease the date item at the cursor by one.
16800 If the cursor is on the year, change the year. If it is on the month,
16801 the day or the time, change that.
16802 With prefix ARG, change by that many units."
16804 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
16806 (defun org-timestamp-up-day (&optional arg)
16807 "Increase the date in the time stamp by one day.
16808 With prefix ARG, change that many days."
16810 (if (and (not (org-at-timestamp-p t))
16811 (org-at-heading-p))
16813 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
16815 (defun org-timestamp-down-day (&optional arg)
16816 "Decrease the date in the time stamp by one day.
16817 With prefix ARG, change that many days."
16819 (if (and (not (org-at-timestamp-p t))
16820 (org-at-heading-p))
16822 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
16824 (defun org-at-timestamp-p (&optional inactive-ok)
16825 "Determine if the cursor is in or at a timestamp."
16827 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
16829 (ans (or (looking-at tsr)
16831 (skip-chars-backward "^[<\n\r\t")
16832 (if (> (point) (point-min)) (backward-char 1))
16833 (and (looking-at tsr)
16834 (> (- (match-end 0) pos) -1))))))
16836 (boundp 'org-ts-what)
16839 ((= pos (match-beginning 0)) 'bracket)
16840 ;; Point is considered to be "on the bracket" whether
16841 ;; it's really on it or right after it.
16842 ((= pos (1- (match-end 0))) 'bracket)
16843 ((= pos (match-end 0)) 'after)
16844 ((org-pos-in-match-range pos 2) 'year)
16845 ((org-pos-in-match-range pos 3) 'month)
16846 ((org-pos-in-match-range pos 7) 'hour)
16847 ((org-pos-in-match-range pos 8) 'minute)
16848 ((or (org-pos-in-match-range pos 4)
16849 (org-pos-in-match-range pos 5)) 'day)
16850 ((and (> pos (or (match-end 8) (match-end 5)))
16851 (< pos (match-end 0)))
16852 (- pos (or (match-end 8) (match-end 5))))
16856 (defun org-toggle-timestamp-type ()
16857 "Toggle the type (<active> or [inactive]) of a time stamp."
16859 (when (org-at-timestamp-p t)
16860 (let ((beg (match-beginning 0)) (end (match-end 0))
16861 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
16864 (while (re-search-forward "[][<>]" end t)
16865 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
16867 (message "Timestamp is now %sactive"
16868 (if (equal (char-after beg) ?<) "" "in")))))
16870 (defvar org-clock-history) ; defined in org-clock.el
16871 (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
16872 (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
16873 "Change the date in the time stamp at point.
16874 The date will be changed by N times WHAT. WHAT can be `day', `month',
16875 `year', `minute', `second'. If WHAT is not given, the cursor position
16876 in the timestamp determines what will be changed.
16877 When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
16878 (let ((origin (point)) origin-cat
16880 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
16883 ts time time0 fixnext clrgx)
16884 (if (not (org-at-timestamp-p t))
16885 (error "Not at a timestamp"))
16886 (if (and (not what) (eq org-ts-what 'bracket))
16887 (org-toggle-timestamp-type)
16888 ;; Point isn't on brackets. Remember the part of the time-stamp
16889 ;; the point was in. Indeed, size of time-stamps may change,
16890 ;; but point must be kept in the same category nonetheless.
16891 (setq origin-cat org-ts-what)
16892 (if (and (not what) (not (eq org-ts-what 'day))
16893 org-display-custom-times
16894 (get-text-property (point) 'display)
16895 (not (get-text-property (1- (point)) 'display)))
16896 (setq org-ts-what 'day))
16897 (setq org-ts-what (or what org-ts-what)
16898 inactive (= (char-after (match-beginning 0)) ?\[)
16899 ts (match-string 0))
16901 (when (string-match
16902 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
16904 (setq extra (match-string 1 ts))
16905 (if suppress-tmp-delay
16906 (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
16907 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
16909 (setq time0 (org-parse-time-string ts))
16911 (eq org-ts-what 'minute)
16912 (not current-prefix-arg))
16913 ;; This looks like s-up and s-down. Change by one rounding step.
16914 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
16915 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
16916 (setcar (cdr time0) (+ (nth 1 time0)
16917 (if (> n 0) (- rem) (- dm rem))))))
16919 (encode-time (or (car time0) 0)
16920 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
16921 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
16922 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
16923 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
16924 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
16926 (when (and (member org-ts-what '(hour minute))
16928 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
16929 (setq extra (org-modify-ts-extra
16931 (if (eq org-ts-what 'hour) 2 5)
16933 (when (integerp org-ts-what)
16934 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
16935 (if (eq what 'calendar)
16936 (let ((cal-date (org-get-date-from-calendar)))
16937 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
16938 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
16939 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
16940 (setcar time0 (or (car time0) 0))
16941 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
16942 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
16943 (setq time (apply 'encode-time time0))))
16944 ;; Insert the new time-stamp, and ensure point stays in the same
16945 ;; category as before (i.e. not after the last position in that
16947 (let ((pos (point)))
16948 ;; Stay before inserted string. `save-excursion' is of no use.
16949 (setq org-last-changed-timestamp
16950 (org-insert-time-stamp time with-hm inactive nil nil extra))
16953 (looking-at org-ts-regexp3)
16955 ;; `day' category ends before `hour' if any, or at
16956 ;; the end of the day name.
16957 ((eq origin-cat 'day)
16958 (min (or (match-beginning 7) (1- (match-end 5))) origin))
16959 ((eq origin-cat 'hour) (min (match-end 7) origin))
16960 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
16961 ((integerp origin-cat) (min (1- (match-end 0)) origin))
16962 ;; `year' and `month' have both fixed size: point
16963 ;; couldn't have moved into another part.
16965 ;; Update clock if on a CLOCK line.
16966 (org-clock-update-time-maybe)
16967 ;; Maybe adjust the closest clock in `org-clock-history'
16968 (when org-clock-adjust-closest
16969 (if (not (and (org-at-clock-log-p)
16970 (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
16971 org-clock-history))))))
16972 (message "No clock to adjust")
16973 (cond ((save-excursion ; fix previous clock?
16974 (re-search-backward org-ts-regexp0 nil t)
16975 (org-looking-back (concat org-clock-string " \\[")))
16976 (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
16977 ((save-excursion ; fix next clock?
16978 (re-search-backward org-ts-regexp0 nil t)
16979 (looking-at (concat org-ts-regexp0 "\\] =>")))
16980 (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
16981 (save-window-excursion
16982 ;; Find closest clock to point, adjust the previous/next one in history
16983 (let* ((p (save-excursion (org-back-to-heading t)))
16984 (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
16986 (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
16987 (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
16989 (message "No clock to adjust")
16991 (org-goto-marker-or-bmk clfixpos)
16993 (when (re-search-forward clrgx nil t)
16994 (goto-char (match-beginning 1))
16995 (let (org-clock-adjust-closest)
16996 (org-timestamp-change n org-ts-what updown))
16997 (message "Clock adjusted in %s for heading: %s"
16998 (file-name-nondirectory (buffer-file-name))
16999 (org-get-heading t t)))))))))
17000 ;; Try to recenter the calendar window, if any.
17001 (if (and org-calendar-follow-timestamp-change
17002 (get-buffer-window "*Calendar*" t)
17003 (memq org-ts-what '(day month year)))
17004 (org-recenter-calendar (time-to-days time))))))
17006 (defun org-modify-ts-extra (s pos n dm)
17007 "Change the different parts of the lead-time and repeat fields in timestamp."
17008 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
17010 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
17012 ((or (org-pos-in-match-range pos 2)
17013 (org-pos-in-match-range pos 3))
17014 (setq m (string-to-number (match-string 3 s))
17015 h (string-to-number (match-string 2 s)))
17016 (if (org-pos-in-match-range pos 2)
17018 (setq n (* dm (org-no-warnings (signum n))))
17019 (when (not (= 0 (setq rem (% m dm))))
17020 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
17022 (if (< m 0) (setq m (+ m 60) h (1- h)))
17023 (if (> m 59) (setq m (- m 60) h (1+ h)))
17024 (setq h (min 24 (max 0 h)))
17025 (setq ng 1 new (format "-%02d:%02d" h m)))
17026 ((org-pos-in-match-range pos 6)
17027 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
17028 ((org-pos-in-match-range pos 5)
17029 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
17031 ((org-pos-in-match-range pos 9)
17032 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
17033 ((org-pos-in-match-range pos 8)
17034 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
17038 (substring s 0 (match-beginning ng))
17040 (substring s (match-end ng))))))
17043 (defun org-recenter-calendar (date)
17044 "If the calendar is visible, recenter it to DATE."
17045 (let ((cwin (get-buffer-window "*Calendar*" t)))
17047 (let ((calendar-move-hook nil))
17048 (with-selected-window cwin
17049 (calendar-goto-date (if (listp date) date
17050 (calendar-gregorian-from-absolute date))))))))
17052 (defun org-goto-calendar (&optional arg)
17053 "Go to the Emacs calendar at the current date.
17054 If there is a time stamp in the current line, go to that date.
17055 A prefix ARG can be used to force the current date."
17057 (let ((tsr org-ts-regexp) diff
17058 (calendar-move-hook nil)
17059 (calendar-view-holidays-initially-flag nil)
17060 (calendar-view-diary-initially-flag nil))
17061 (if (or (org-at-timestamp-p)
17063 (beginning-of-line 1)
17064 (looking-at (concat ".*" tsr))))
17065 (let ((d1 (time-to-days (current-time)))
17067 (org-time-string-to-time (match-string 1)))))
17068 (setq diff (- d2 d1))))
17070 (calendar-goto-today)
17071 (if (and diff (not arg)) (calendar-forward-day diff))))
17073 (defun org-get-date-from-calendar ()
17074 "Return a list (month day year) of date at point in calendar."
17075 (with-current-buffer "*Calendar*"
17077 (calendar-cursor-to-date))))
17079 (defun org-date-from-calendar ()
17080 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
17081 If there is already a time stamp at the cursor position, update it."
17083 (if (org-at-timestamp-p t)
17084 (org-timestamp-change 0 'calendar)
17085 (let ((cal-date (org-get-date-from-calendar)))
17086 (org-insert-time-stamp
17087 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
17089 (defcustom org-effort-durations
17092 ("w" . ,(* 60 8 5))
17093 ("m" . ,(* 60 8 5 4))
17094 ("y" . ,(* 60 8 5 40)))
17095 "Conversion factor to minutes for an effort modifier.
17097 Each entry has the form (MODIFIER . MINUTES).
17099 In an effort string, a number followed by MODIFIER is multiplied
17100 by the specified number of MINUTES to obtain an effort in
17103 For example, if the value of this variable is ((\"hours\" . 60)), then an
17104 effort string \"2hours\" is equivalent to 120 minutes."
17107 :type '(alist :key-type (string :tag "Modifier")
17108 :value-type (number :tag "Minutes")))
17110 (defun org-minutes-to-clocksum-string (m)
17111 "Format number of minutes as a clocksum string.
17112 The format is determined by `org-time-clocksum-format',
17113 `org-time-clocksum-use-fractional' and
17114 `org-time-clocksum-fractional-format' and
17115 `org-time-clocksum-use-effort-durations'."
17116 (let ((clocksum "") h d w mo y fmt n)
17117 (setq h (if org-time-clocksum-use-effort-durations
17118 (cdr (assoc "h" org-effort-durations)) 60)
17119 d (if org-time-clocksum-use-effort-durations
17120 (/ (cdr (assoc "d" org-effort-durations)) h) 24)
17121 w (if org-time-clocksum-use-effort-durations
17122 (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
17123 mo (if org-time-clocksum-use-effort-durations
17124 (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
17125 y (if org-time-clocksum-use-effort-durations
17126 (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
17127 ;; fractional format
17128 (if org-time-clocksum-use-fractional
17130 ;; single format string
17131 ((stringp org-time-clocksum-fractional-format)
17132 (format org-time-clocksum-fractional-format (/ m (float h))))
17133 ;; choice of fractional formats for different time units
17134 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
17135 (> (/ (truncate m) (* y d h)) 0))
17136 (format fmt (/ m (* y d (float h)))))
17137 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
17138 (> (/ (truncate m) (* mo d h)) 0))
17139 (format fmt (/ m (* mo d (float h)))))
17140 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
17141 (> (/ (truncate m) (* w d h)) 0))
17142 (format fmt (/ m (* w d (float h)))))
17143 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
17144 (> (/ (truncate m) (* d h)) 0))
17145 (format fmt (/ m (* d (float h)))))
17146 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
17147 (> (/ (truncate m) h) 0))
17148 (format fmt (/ m (float h))))
17149 ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
17151 ;; fall back to smallest time unit with a format
17152 ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
17153 (format fmt (/ m (float h))))
17154 ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
17155 (format fmt (/ m (* d (float h)))))
17156 ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
17157 (format fmt (/ m (* w d (float h)))))
17158 ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
17159 (format fmt (/ m (* mo d (float h)))))
17160 ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
17161 (format fmt (/ m (* y d (float h))))))
17162 ;; standard (non-fractional) format, with single format string
17163 (if (stringp org-time-clocksum-format)
17164 (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
17165 ;; separate formats components
17166 (and (setq fmt (plist-get org-time-clocksum-format :years))
17167 (or (> (setq n (/ (truncate m) (* y d h))) 0)
17168 (plist-get org-time-clocksum-format :require-years))
17169 (setq clocksum (concat clocksum (format fmt n))
17170 m (- m (* n y d h))))
17171 (and (setq fmt (plist-get org-time-clocksum-format :months))
17172 (or (> (setq n (/ (truncate m) (* mo d h))) 0)
17173 (plist-get org-time-clocksum-format :require-months))
17174 (setq clocksum (concat clocksum (format fmt n))
17175 m (- m (* n mo d h))))
17176 (and (setq fmt (plist-get org-time-clocksum-format :weeks))
17177 (or (> (setq n (/ (truncate m) (* w d h))) 0)
17178 (plist-get org-time-clocksum-format :require-weeks))
17179 (setq clocksum (concat clocksum (format fmt n))
17180 m (- m (* n w d h))))
17181 (and (setq fmt (plist-get org-time-clocksum-format :days))
17182 (or (> (setq n (/ (truncate m) (* d h))) 0)
17183 (plist-get org-time-clocksum-format :require-days))
17184 (setq clocksum (concat clocksum (format fmt n))
17185 m (- m (* n d h))))
17186 (and (setq fmt (plist-get org-time-clocksum-format :hours))
17187 (or (> (setq n (/ (truncate m) h)) 0)
17188 (plist-get org-time-clocksum-format :require-hours))
17189 (setq clocksum (concat clocksum (format fmt n))
17191 (and (setq fmt (plist-get org-time-clocksum-format :minutes))
17192 (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
17193 (setq clocksum (concat clocksum (format fmt m))))
17194 ;; return formatted time duration
17197 (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
17198 (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
17199 "Org mode version 8.0")
17201 (defun org-hours-to-clocksum-string (n)
17202 (org-minutes-to-clocksum-string (* n 60)))
17204 (defun org-hh:mm-string-to-minutes (s)
17205 "Convert a string H:MM to a number of minutes.
17206 If the string is just a number, interpret it as minutes.
17207 In fact, the first hh:mm or number in the string will be taken,
17208 there can be extra stuff in the string.
17209 If no number is found, the return value is 0."
17212 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
17213 (+ (* (string-to-number (match-string 1 s)) 60)
17214 (string-to-number (match-string 2 s))))
17215 ((string-match "\\([0-9]+\\)" s)
17216 (string-to-number (match-string 1 s)))
17219 (defcustom org-image-actual-width t
17220 "Should we use the actual width of images when inlining them?
17222 When set to `t', always use the image width.
17224 When set to a number, use imagemagick (when available) to set
17225 the image's width to this value.
17227 When set to a number in a list, try to get the width from the
17228 #+ATTR.* keyword if it matches a width specification like
17229 width=\"[0-9]+\", and fall back on that number if none is found.
17231 When set to nil, try to get the width from an #+ATTR.* keyword
17232 and fall back on the original width if none is found.
17234 This requires Emacs >= 24.1, build with imagemagick support."
17235 :group 'org-appearance
17238 (const :tag "Use the image width" t)
17239 (integer :tag "Use a number of pixels")
17240 (list :tag "Use #+ATTR* or a number of pixels" (integer))
17241 (const :tag "Use #+ATTR* or don't resize" nil)))
17243 (defcustom org-agenda-inhibit-startup t
17244 "Inhibit startup when preparing agenda buffers.
17245 When this variable is `t' (the default), the initialization of
17246 the Org agenda buffers is inhibited: e.g. the visibility state
17247 is not set, the tables are not re-aligned, etc."
17250 :group 'org-agenda)
17252 (defun org-duration-string-to-minutes (s &optional output-to-string)
17253 "Convert a duration string S to minutes.
17255 A bare number is interpreted as minutes, modifiers can be set by
17256 customizing `org-effort-durations' (which see).
17258 Entries containing a colon are interpreted as H:MM by
17259 `org-hh:mm-string-to-minutes'."
17261 (re (concat "\\([0-9.]+\\) *\\("
17262 (regexp-opt (mapcar 'car org-effort-durations))
17264 (while (string-match re s)
17265 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
17266 (string-to-number (match-string 1 s))))
17267 (setq s (replace-match "" nil t s)))
17268 (setq result (floor result))
17269 (incf result (org-hh:mm-string-to-minutes s))
17270 (if output-to-string (number-to-string result) result)))
17274 (defun org-save-all-org-buffers ()
17275 "Save all Org-mode buffers without user confirmation."
17277 (message "Saving all Org-mode buffers...")
17278 (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
17279 (when (featurep 'org-id) (org-id-locations-save))
17280 (message "Saving all Org-mode buffers... done"))
17282 (defun org-revert-all-org-buffers ()
17283 "Revert all Org-mode buffers.
17284 Prompt for confirmation when there are unsaved changes.
17285 Be sure you know what you are doing before letting this function
17286 overwrite your changes.
17288 This function is useful in a setup where one tracks org files
17289 with a version control system, to revert on one machine after pulling
17290 changes from another. I believe the procedure must be like this:
17292 1. M-x org-save-all-org-buffers
17293 2. Pull changes from the other machine, resolve conflicts
17294 3. M-x org-revert-all-org-buffers"
17296 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
17299 (save-window-excursion
17302 (when (and (with-current-buffer b (derived-mode-p 'org-mode))
17303 (with-current-buffer b buffer-file-name))
17304 (org-pop-to-buffer-same-window b)
17305 (revert-buffer t 'no-confirm)))
17307 (when (and (featurep 'org-id) org-id-track-globally)
17308 (org-id-locations-load)))))
17313 (defun org-switchb (&optional arg)
17314 "Switch between Org buffers.
17315 With one prefix argument, restrict available buffers to files.
17316 With two prefix arguments, restrict available buffers to agenda files.
17318 Defaults to `iswitchb' for buffer name completion.
17319 Set `org-completion-use-ido' to make it use ido instead."
17321 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
17322 ((equal arg '(16)) (org-buffer-list 'agenda))
17323 (t (org-buffer-list))))
17324 (org-completion-use-iswitchb org-completion-use-iswitchb)
17325 (org-completion-use-ido org-completion-use-ido))
17326 (unless (or org-completion-use-ido org-completion-use-iswitchb)
17327 (setq org-completion-use-iswitchb t))
17328 (org-pop-to-buffer-same-window
17329 (org-icompleting-read "Org buffer: "
17330 (mapcar 'list (mapcar 'buffer-name blist))
17333 ;;; Define some older names previously used for this functionality
17335 (defalias 'org-ido-switchb 'org-switchb)
17337 (defalias 'org-iswitchb 'org-switchb)
17339 (defun org-buffer-list (&optional predicate exclude-tmp)
17340 "Return a list of Org buffers.
17341 PREDICATE can be `export', `files' or `agenda'.
17343 export restrict the list to Export buffers.
17344 files restrict the list to buffers visiting Org files.
17345 agenda restrict the list to buffers visiting agenda files.
17347 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
17349 (agenda-files (and (eq predicate 'agenda)
17350 (mapcar 'file-truename (org-agenda-files t))))
17353 ((eq predicate 'files)
17354 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
17355 ((eq predicate 'export)
17356 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
17357 ((eq predicate 'agenda)
17359 (with-current-buffer b
17360 (and (derived-mode-p 'org-mode)
17361 (setq bfn (buffer-file-name b))
17362 (member (file-truename bfn) agenda-files)))))
17363 (t (lambda (b) (with-current-buffer b
17364 (or (derived-mode-p 'org-mode)
17365 (string-match "\*Org .*Export"
17366 (buffer-name b)))))))))
17370 (if (and (funcall filter b)
17371 (or (not exclude-tmp)
17372 (not (string-match "tmp" (buffer-name b)))))
17377 (defun org-agenda-files (&optional unrestricted archives)
17378 "Get the list of agenda files.
17379 Optional UNRESTRICTED means return the full list even if a restriction
17380 is currently in place.
17381 When ARCHIVES is t, include all archive files that are really being
17382 used by the agenda files. If ARCHIVE is `ifmode', do this only if
17383 `org-agenda-archives-mode' is t."
17386 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
17387 ((stringp org-agenda-files) (org-read-agenda-file-list))
17388 ((listp org-agenda-files) org-agenda-files)
17389 (t (error "Invalid value of `org-agenda-files'")))))
17390 (setq files (apply 'append
17391 (mapcar (lambda (f)
17392 (if (file-directory-p f)
17394 f t org-agenda-file-regexp)
17397 (when org-agenda-skip-unavailable-files
17398 (setq files (delq nil
17401 (and (file-readable-p file) file)))
17403 (when (or (eq archives t)
17404 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
17405 (setq files (org-add-archive-files files)))
17408 (defun org-agenda-file-p (&optional file)
17409 "Return non-nil, if FILE is an agenda file.
17410 If FILE is omitted, use the file associated with the current
17412 (member (or file (buffer-file-name))
17413 (org-agenda-files t)))
17415 (defun org-edit-agenda-file-list ()
17416 "Edit the list of agenda files.
17417 Depending on setup, this either uses customize to edit the variable
17418 `org-agenda-files', or it visits the file that is holding the list. In the
17419 latter case, the buffer is set up in a way that saving it automatically kills
17420 the buffer and restores the previous window configuration."
17422 (if (stringp org-agenda-files)
17423 (let ((cw (current-window-configuration)))
17424 (find-file org-agenda-files)
17425 (org-set-local 'org-window-configuration cw)
17426 (org-add-hook 'after-save-hook
17428 (set-window-configuration
17429 (prog1 org-window-configuration
17430 (kill-buffer (current-buffer))))
17431 (org-install-agenda-files-menu)
17432 (message "New agenda file list installed"))
17434 (message "%s" (substitute-command-keys
17435 "Edit list and finish with \\[save-buffer]")))
17436 (customize-variable 'org-agenda-files)))
17438 (defun org-store-new-agenda-file-list (list)
17439 "Set new value for the agenda file list and save it correctly."
17440 (if (stringp org-agenda-files)
17441 (let ((fe (org-read-agenda-file-list t)) b u)
17442 (while (setq b (find-buffer-visiting org-agenda-files))
17444 (with-temp-file org-agenda-files
17447 (lambda (f) ;; Keep un-expanded entries.
17448 (if (setq u (assoc f fe))
17453 (let ((org-mode-hook nil) (org-inhibit-startup t)
17454 (org-insert-mode-line-in-empty-file nil))
17455 (setq org-agenda-files list)
17456 (customize-save-variable 'org-agenda-files org-agenda-files))))
17458 (defun org-read-agenda-file-list (&optional pair-with-expansion)
17459 "Read the list of agenda files from a file.
17460 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
17461 filenames, used by `org-store-new-agenda-file-list' to write back
17462 un-expanded file names."
17463 (when (file-directory-p org-agenda-files)
17464 (error "`org-agenda-files' cannot be a single directory"))
17465 (when (stringp org-agenda-files)
17467 (insert-file-contents org-agenda-files)
17470 (let ((e (expand-file-name (substitute-in-file-name f)
17472 (if pair-with-expansion
17475 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
17478 (defun org-cycle-agenda-files ()
17479 "Cycle through the files in `org-agenda-files'.
17480 If the current buffer visits an agenda file, find the next one in the list.
17481 If the current buffer does not, find the first agenda file."
17483 (let* ((fs (org-agenda-files t))
17484 (files (append fs (list (car fs))))
17485 (tcf (if buffer-file-name (file-truename buffer-file-name)))
17487 (unless files (error "No agenda files"))
17489 (while (setq file (pop files))
17490 (if (equal (file-truename file) tcf)
17492 (find-file (car files))
17494 (find-file (car fs)))
17495 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
17497 (defun org-agenda-file-to-front (&optional to-end)
17498 "Move/add the current file to the top of the agenda file list.
17499 If the file is not present in the list, it is added to the front. If it is
17500 present, it is moved there. With optional argument TO-END, add/move to the
17503 (let ((org-agenda-skip-unavailable-files nil)
17504 (file-alist (mapcar (lambda (x)
17505 (cons (file-truename x) x))
17506 (org-agenda-files t)))
17507 (ctf (file-truename
17508 (or buffer-file-name
17509 (error "Please save the current buffer to a file"))))
17511 (setq x (assoc ctf file-alist) had x)
17513 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
17515 (setq file-alist (append (delq x file-alist) (list x)))
17516 (setq file-alist (cons x (delq x file-alist))))
17517 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
17518 (org-install-agenda-files-menu)
17519 (message "File %s to %s of agenda file list"
17520 (if had "moved" "added") (if to-end "end" "front"))))
17522 (defun org-remove-file (&optional file)
17523 "Remove current file from the list of files in variable `org-agenda-files'.
17524 These are the files which are being checked for agenda entries.
17525 Optional argument FILE means use this file instead of the current."
17527 (let* ((org-agenda-skip-unavailable-files nil)
17528 (file (or file buffer-file-name
17529 (error "Current buffer does not visit a file")))
17530 (true-file (file-truename file))
17531 (afile (abbreviate-file-name file))
17532 (files (delq nil (mapcar
17534 (if (equal true-file
17537 (org-agenda-files t)))))
17538 (if (not (= (length files) (length (org-agenda-files t))))
17540 (org-store-new-agenda-file-list files)
17541 (org-install-agenda-files-menu)
17542 (message "Removed file: %s" afile))
17543 (message "File was not in list: %s (not removed)" afile))))
17545 (defun org-file-menu-entry (file)
17546 (vector file (list 'find-file file) t))
17548 (defun org-check-agenda-file (file)
17549 "Make sure FILE exists. If not, ask user what to do."
17550 (when (not (file-exists-p file))
17551 (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
17552 (abbreviate-file-name file))
17553 (let ((r (downcase (read-char-exclusive))))
17556 (org-remove-file file)
17557 (throw 'nextfile t))
17558 (t (error "Abort"))))))
17560 (defun org-get-agenda-file-buffer (file)
17561 "Get a buffer visiting FILE. If the buffer needs to be created, add
17562 it to the list of buffers which might be released later."
17563 (let ((buf (org-find-base-buffer-visiting file)))
17565 buf ; just return it
17566 ;; Make a new buffer and remember it
17567 (setq buf (find-file-noselect file))
17568 (if buf (push buf org-agenda-new-buffers))
17571 (defun org-release-buffers (blist)
17572 "Release all buffers in list, asking the user for confirmation when needed.
17573 When a buffer is unmodified, it is just killed. When modified, it is saved
17574 \(if the user agrees) and then killed."
17576 (while (setq buf (pop blist))
17577 (setq file (buffer-file-name buf))
17578 (when (and (buffer-modified-p buf)
17580 (y-or-n-p (format "Save file %s? " file)))
17581 (with-current-buffer buf (save-buffer)))
17582 (kill-buffer buf))))
17584 (defun org-agenda-prepare-buffers (files)
17585 "Create buffers for all agenda files, protect archived trees and comments."
17587 (let ((pa '(:org-archived t))
17588 (pc '(:org-comment t))
17589 (pall '(:org-archived t :org-comment t))
17590 (inhibit-read-only t)
17591 (org-inhibit-startup org-agenda-inhibit-startup)
17592 (rea (concat ":" org-archive-tag ":"))
17596 (while (setq file (pop files))
17600 (org-check-agenda-file file)
17601 (set-buffer (org-get-agenda-file-buffer file)))
17603 (org-refresh-category-properties)
17604 (org-refresh-properties org-effort-property 'org-effort)
17605 (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
17606 (setq org-todo-keywords-for-agenda
17607 (append org-todo-keywords-for-agenda org-todo-keywords-1))
17608 (setq org-done-keywords-for-agenda
17609 (append org-done-keywords-for-agenda org-done-keywords))
17610 (setq org-todo-keyword-alist-for-agenda
17611 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
17612 (setq org-drawers-for-agenda
17613 (append org-drawers-for-agenda org-drawers))
17614 (setq org-tag-alist-for-agenda
17615 (append org-tag-alist-for-agenda org-tag-alist))
17616 (with-silent-modifications
17618 (remove-text-properties (point-min) (point-max) pall)
17619 (when org-agenda-skip-archived-trees
17620 (goto-char (point-min))
17621 (while (re-search-forward rea nil t)
17622 (if (org-at-heading-p t)
17623 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
17624 (goto-char (point-min))
17625 (setq re (format org-heading-keyword-regexp-format
17626 org-comment-string))
17627 (while (re-search-forward re nil t)
17628 (add-text-properties
17629 (match-beginning 0) (org-end-of-subtree t) pc))))))))
17630 (setq org-todo-keywords-for-agenda
17631 (org-uniquify org-todo-keywords-for-agenda))
17632 (setq org-todo-keyword-alist-for-agenda
17633 (org-uniquify org-todo-keyword-alist-for-agenda)
17634 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
17637 ;;;; CDLaTeX minor mode
17639 (defvar org-cdlatex-mode-map (make-sparse-keymap)
17640 "Keymap for the minor `org-cdlatex-mode'.")
17642 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
17643 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
17644 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
17645 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
17646 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
17648 (defvar org-cdlatex-texmathp-advice-is-done nil
17649 "Flag remembering if we have applied the advice to texmathp already.")
17651 (define-minor-mode org-cdlatex-mode
17652 "Toggle the minor `org-cdlatex-mode'.
17653 This mode supports entering LaTeX environment and math in LaTeX fragments
17655 \\{org-cdlatex-mode-map}"
17657 (when org-cdlatex-mode
17659 (run-hooks 'cdlatex-mode-hook)
17660 (cdlatex-compute-tables))
17661 (unless org-cdlatex-texmathp-advice-is-done
17662 (setq org-cdlatex-texmathp-advice-is-done t)
17663 (defadvice texmathp (around org-math-always-on activate)
17664 "Always return t in org-mode buffers.
17665 This is because we want to insert math symbols without dollars even outside
17666 the LaTeX math segments. If Orgmode thinks that point is actually inside
17667 an embedded LaTeX fragment, let texmathp do its job.
17668 \\[org-cdlatex-mode-map]"
17672 ((not (derived-mode-p 'org-mode)) ad-do-it)
17673 ((eq this-command 'cdlatex-math-symbol)
17674 (setq ad-return-value t
17675 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
17677 (let ((p (org-inside-LaTeX-fragment-p)))
17678 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
17679 (setq ad-return-value t
17680 texmathp-why '("Org-mode embedded math" . 0))
17681 (if p ad-do-it)))))))))
17683 (defun turn-on-org-cdlatex ()
17684 "Unconditionally turn on `org-cdlatex-mode'."
17685 (org-cdlatex-mode 1))
17687 (defun org-try-cdlatex-tab ()
17688 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
17689 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
17690 - inside a LaTeX fragment, or
17691 - after the first word in a line, where an abbreviation expansion could
17692 insert a LaTeX environment."
17693 (when org-cdlatex-mode
17695 ;; Before any word on the line: No expansion possible.
17696 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
17697 ;; Just after first word on the line: Expand it. Make sure it
17698 ;; cannot happen on headlines, though.
17700 (skip-chars-backward "a-zA-Z0-9*")
17701 (skip-chars-backward " \t")
17702 (and (bolp) (not (org-at-heading-p))))
17704 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
17706 (defun org-cdlatex-underscore-caret (&optional arg)
17707 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
17708 Revert to the normal definition outside of these fragments."
17710 (if (org-inside-LaTeX-fragment-p)
17711 (call-interactively 'cdlatex-sub-superscript)
17712 (let (org-cdlatex-mode)
17713 (call-interactively (key-binding (vector last-input-event))))))
17715 (defun org-cdlatex-math-modify (&optional arg)
17716 "Execute `cdlatex-math-modify' in LaTeX fragments.
17717 Revert to the normal definition outside of these fragments."
17719 (if (org-inside-LaTeX-fragment-p)
17720 (call-interactively 'cdlatex-math-modify)
17721 (let (org-cdlatex-mode)
17722 (call-interactively (key-binding (vector last-input-event))))))
17726 ;;;; LaTeX fragments
17728 (defvar org-latex-regexps
17729 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
17730 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
17731 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
17732 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17733 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17734 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
17735 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
17736 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
17737 "Regular expressions for matching embedded LaTeX.")
17739 (defun org-inside-LaTeX-fragment-p ()
17740 "Test if point is inside a LaTeX fragment.
17741 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
17742 sequence appearing also before point.
17743 Even though the matchers for math are configurable, this function assumes
17744 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
17745 delimiters are skipped when they have been removed by customization.
17746 The return value is nil, or a cons cell with the delimiter and the
17747 position of this delimiter.
17749 This function does a reasonably good job, but can locally be fooled by
17750 for example currency specifications. For example it will assume being in
17751 inline math after \"$22.34\". The LaTeX fragment formatter will only format
17752 fragments that are properly closed, but during editing, we have to live
17753 with the uncertainty caused by missing closing delimiters. This function
17754 looks only before point, not after."
17756 (let ((pos (point))
17757 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
17759 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
17761 dd-on str (start 0) m re)
17764 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
17765 re (nth 1 (assoc "$" org-latex-regexps)))
17766 (while (string-match re str start)
17768 ((= (match-end 0) (length str))
17769 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
17770 ((= (match-end 0) (- (length str) 5))
17772 (t (setq start (match-end 0))))))
17773 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
17775 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
17776 (and (match-beginning 2) (throw 'exit nil))
17778 (while (re-search-backward "\\$\\$" lim t)
17779 (setq dd-on (not dd-on)))
17781 (if dd-on (cons "$$" m))))))
17783 (defun org-inside-latex-macro-p ()
17784 "Is point inside a LaTeX macro or its arguments?"
17787 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
17789 (defvar org-latex-fragment-image-overlays nil
17790 "List of overlays carrying the images of latex fragments.")
17791 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
17793 (defun org-remove-latex-fragment-image-overlays ()
17794 "Remove all overlays with LaTeX fragment images in current buffer."
17795 (mapc 'delete-overlay org-latex-fragment-image-overlays)
17796 (setq org-latex-fragment-image-overlays nil))
17798 (defun org-preview-latex-fragment (&optional subtree)
17799 "Preview the LaTeX fragment at point, or all locally or globally.
17800 If the cursor is in a LaTeX fragment, create the image and overlay
17801 it over the source code. If there is no fragment at point, display
17802 all fragments in the current text, from one headline to the next. With
17803 prefix SUBTREE, display all fragments in the current subtree. With a
17804 double prefix arg \\[universal-argument] \\[universal-argument], or when \
17805 the cursor is before the first headline,
17806 display all fragments in the buffer.
17807 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
17809 (unless buffer-file-name
17810 (error "Can't preview LaTeX fragment in a non-file buffer"))
17811 (org-remove-latex-fragment-image-overlays)
17814 (let (beg end at msg)
17816 ((or (equal subtree '(16))
17817 (not (save-excursion
17818 (re-search-backward org-outline-regexp-bol nil t))))
17819 (setq beg (point-min) end (point-max)
17820 msg "Creating images for buffer...%s"))
17821 ((equal subtree '(4))
17822 (org-back-to-heading)
17823 (setq beg (point) end (org-end-of-subtree t)
17824 msg "Creating images for subtree...%s"))
17826 (if (setq at (org-inside-LaTeX-fragment-p))
17827 (goto-char (max (point-min) (- (cdr at) 2)))
17828 (org-back-to-heading))
17829 (setq beg (point) end (progn (outline-next-heading) (point))
17830 msg (if at "Creating image...%s"
17831 "Creating images for entry...%s"))))
17833 (narrow-to-region beg end)
17836 (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
17837 (file-name-nondirectory
17838 buffer-file-name)))
17839 default-directory 'overlays msg at 'forbuffer
17840 org-latex-create-formula-image-program)
17841 (message msg "done. Use `C-c C-c' to remove images.")))))
17843 (defun org-format-latex (prefix &optional dir overlays msg at
17844 forbuffer processing-type)
17845 "Replace LaTeX fragments with links to an image, and produce images.
17846 Some of the options can be changed using the variable
17847 `org-format-latex-options'."
17848 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
17849 (let* ((prefixnodir (file-name-nondirectory prefix))
17850 (absprefix (expand-file-name prefix dir))
17851 (todir (file-name-directory absprefix))
17852 (opt org-format-latex-options)
17853 (optnew org-format-latex-options)
17854 (matchers (plist-get opt :matchers))
17855 (re-list org-latex-regexps)
17856 (cnt 0) txt hash link beg end re e checkdir
17858 m n block-type block linkfile movefile ov)
17859 ;; Check the different regular expressions
17860 (while (setq e (pop re-list))
17861 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
17862 block (if block-type "\n\n" ""))
17863 (when (member m matchers)
17864 (goto-char (point-min))
17865 (while (re-search-forward re nil t)
17866 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
17868 (not (eq (get-char-property (match-beginning n)
17870 'org-latex-overlay))))
17872 ((eq processing-type 'verbatim))
17873 ((eq processing-type 'mathjax)
17874 ;; Prepare for MathJax processing.
17875 (setq string (match-string n))
17876 (when (member m '("$" "$1"))
17878 (delete-region (match-beginning n) (match-end n))
17879 (goto-char (match-beginning n))
17880 (insert (concat "\\(" (substring string 1 -1) "\\)")))))
17881 ((or (eq processing-type 'dvipng)
17882 (eq processing-type 'imagemagick))
17883 ;; Process to an image.
17884 (setq txt (match-string n)
17885 beg (match-beginning n) end (match-end n)
17887 (let ((face (face-at-point))
17888 (fg (plist-get opt :foreground))
17889 (bg (plist-get opt :background))
17890 ;; Ensure full list is printed.
17891 print-length print-level)
17893 ;; Get the colors from the face at point.
17895 (when (eq fg 'auto)
17896 (setq fg (face-attribute face :foreground nil 'default)))
17897 (when (eq bg 'auto)
17898 (setq bg (face-attribute face :background nil 'default)))
17899 (setq optnew (copy-sequence opt))
17900 (plist-put optnew :foreground fg)
17901 (plist-put optnew :background bg))
17902 (setq hash (sha1 (prin1-to-string
17903 (list org-format-latex-header
17904 org-latex-default-packages-alist
17905 org-latex-packages-alist
17906 org-format-latex-options
17907 forbuffer txt fg bg)))
17908 linkfile (format "%s_%s.png" prefix hash)
17909 movefile (format "%s_%s.png" absprefix hash)))
17910 (setq link (concat block "[[file:" linkfile "]]" block))
17911 (if msg (message msg cnt))
17913 (unless checkdir ; Ensure the directory exists.
17915 (or (file-directory-p todir) (make-directory todir t)))
17916 (unless (file-exists-p movefile)
17917 (org-create-formula-image
17918 txt movefile optnew forbuffer processing-type))
17922 (if (eq (overlay-get o 'org-overlay-type)
17923 'org-latex-overlay)
17924 (delete-overlay o)))
17925 (overlays-in beg end))
17926 (setq ov (make-overlay beg end))
17927 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
17928 (if (featurep 'xemacs)
17930 (overlay-put ov 'invisible t)
17933 (make-glyph (vector 'png :file movefile))))
17936 (list 'image :type 'png :file movefile :ascent 'center)))
17937 (push ov org-latex-fragment-image-overlays)
17939 (delete-region beg end)
17940 (insert (org-add-props link
17941 (list 'org-latex-src
17942 (replace-regexp-in-string
17944 'org-latex-src-embed-type
17945 (if block-type 'paragraph 'character))))))
17946 ((eq processing-type 'mathml)
17947 ;; Process to MathML
17948 (unless (save-match-data (org-format-latex-mathml-available-p))
17949 (error "LaTeX to MathML converter not configured"))
17950 (setq txt (match-string n)
17951 beg (match-beginning n) end (match-end n)
17953 (if msg (message msg cnt))
17955 (delete-region beg end)
17956 (insert (org-format-latex-as-mathml
17957 txt block-type prefix dir)))
17959 (error "Unknown conversion type %s for latex fragments"
17960 processing-type)))))))))
17962 (defun org-create-math-formula (latex-frag &optional mathml-file)
17963 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
17964 Use `org-latex-to-mathml-convert-command'. If the conversion is
17965 sucessful, return the portion between \"<math...> </math>\"
17966 elements otherwise return nil. When MATHML-FILE is specified,
17967 write the results in to that file. When invoked as an
17968 interactive command, prompt for LATEX-FRAG, with initial value
17969 set to the current active region and echo the results for user
17971 (interactive (list (let ((frag (when (org-region-active-p)
17972 (buffer-substring-no-properties
17973 (region-beginning) (region-end)))))
17974 (read-string "LaTeX Fragment: " frag nil frag))))
17975 (unless latex-frag (error "Invalid latex-frag"))
17976 (let* ((tmp-in-file (file-relative-name
17977 (make-temp-name (expand-file-name "ltxmathml-in"))))
17978 (ignore (write-region latex-frag nil tmp-in-file))
17979 (tmp-out-file (file-relative-name
17980 (make-temp-name (expand-file-name "ltxmathml-out"))))
17982 org-latex-to-mathml-convert-command
17983 `((?j . ,(shell-quote-argument
17984 (expand-file-name org-latex-to-mathml-jar-file)))
17985 (?I . ,(shell-quote-argument tmp-in-file))
17986 (?o . ,(shell-quote-argument tmp-out-file)))))
17987 mathml shell-command-output)
17988 (when (org-called-interactively-p 'any)
17989 (unless (org-format-latex-mathml-available-p)
17990 (error "LaTeX to MathML converter not configured")))
17991 (message "Running %s" cmd)
17992 (setq shell-command-output (shell-command-to-string cmd))
17994 (when (file-readable-p tmp-out-file)
17995 (with-current-buffer (find-file-noselect tmp-out-file t)
17996 (goto-char (point-min))
17997 (when (re-search-forward
18000 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
18002 (regexp-quote "</math>")) nil t)
18003 (prog1 (match-string 0) (kill-buffer))))))
18007 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
18009 (write-region mathml nil mathml-file))
18010 (when (org-called-interactively-p 'any)
18012 ((message "LaTeX to MathML conversion failed")
18013 (message shell-command-output)))
18014 (delete-file tmp-in-file)
18015 (when (file-exists-p tmp-out-file)
18016 (delete-file tmp-out-file))
18019 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
18020 prefix &optional dir)
18021 "Use `org-create-math-formula' but check local cache first."
18022 (let* ((absprefix (expand-file-name prefix dir))
18023 (print-length nil) (print-level nil)
18024 (formula-id (concat
18029 org-latex-to-mathml-convert-command)))))
18030 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
18031 (formula-cache-dir (file-name-directory formula-cache)))
18033 (unless (file-directory-p formula-cache-dir)
18034 (make-directory formula-cache-dir t))
18036 (unless (file-exists-p formula-cache)
18037 (org-create-math-formula latex-frag formula-cache))
18039 (if (file-exists-p formula-cache)
18040 ;; Successful conversion. Return the link to MathML file.
18042 (format "[[file:%s]]" (file-relative-name formula-cache dir))
18043 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
18044 'org-latex-src-embed-type (if latex-frag-type
18045 'paragraph 'character)))
18046 ;; Failed conversion. Return the LaTeX fragment verbatim
18049 (defun org-create-formula-image (string tofile options buffer &optional type)
18050 "Create an image from LaTeX source using dvipng or convert.
18051 This function calls either `org-create-formula-image-with-dvipng'
18052 or `org-create-formula-image-with-imagemagick' depending on the
18053 value of `org-latex-create-formula-image-program' or on the value
18054 of the optional TYPE variable.
18056 Note: ultimately these two function should be combined as they
18057 share a good deal of logic."
18058 (org-check-external-command
18059 "latex" "needed to convert LaTeX fragments to images")
18061 (case (or type org-latex-create-formula-image-program)
18063 (org-check-external-command
18064 "dvipng" "needed to convert LaTeX fragments to images")
18065 #'org-create-formula-image-with-dvipng)
18067 (org-check-external-command
18068 "convert" "you need to install imagemagick")
18069 #'org-create-formula-image-with-imagemagick)
18071 "invalid value of `org-latex-create-formula-image-program'")))
18072 string tofile options buffer))
18074 ;; This function borrows from Ganesh Swami's latex2png.el
18075 (defun org-create-formula-image-with-dvipng (string tofile options buffer)
18076 "This calls dvipng."
18077 (let* ((tmpdir (if (featurep 'xemacs)
18079 temporary-file-directory))
18080 (texfilebase (make-temp-name
18081 (expand-file-name "orgtex" tmpdir)))
18082 (texfile (concat texfilebase ".tex"))
18083 (dvifile (concat texfilebase ".dvi"))
18084 (pngfile (concat texfilebase ".png"))
18085 (fnh (if (featurep 'xemacs)
18086 (font-height (face-font 'default))
18087 (face-attribute 'default :height nil)))
18088 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
18089 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
18090 (fg (or (plist-get options (if buffer :foreground :html-foreground))
18092 (bg (or (plist-get options (if buffer :background :html-background))
18094 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
18095 (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
18096 (if (eq bg 'default) (setq bg (org-dvipng-color :background))
18097 (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
18098 (with-temp-file texfile
18099 (require 'ox-latex)
18100 (insert (org-latex-guess-inputenc
18101 (org-splice-latex-header
18102 org-format-latex-header
18103 org-latex-default-packages-alist
18104 org-latex-packages-alist t)))
18105 (insert "\n\\begin{document}\n" string "\n\\end{document}\n"))
18106 (let ((dir default-directory))
18107 (condition-case nil
18110 (call-process "latex" nil nil nil texfile))
18113 (if (not (file-exists-p dvifile))
18114 (progn (message "Failed to create dvi file from %s" texfile) nil)
18115 (condition-case nil
18116 (if (featurep 'xemacs)
18117 (call-process "dvipng" nil nil nil
18122 (call-process "dvipng" nil nil nil
18125 ;;"-x" scale "-y" scale
18130 (if (not (file-exists-p pngfile))
18131 (if org-format-latex-signal-error
18132 (error "Failed to create png file from %s" texfile)
18133 (message "Failed to create png file from %s" texfile)
18135 ;; Use the requested file name and clean up
18136 (copy-file pngfile tofile 'replace)
18137 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
18138 (if (file-exists-p (concat texfilebase e))
18139 (delete-file (concat texfilebase e))))
18142 (defvar org-latex-pdf-process) ; From ox-latex.el
18143 (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
18144 "This calls convert, which is included into imagemagick."
18145 (let* ((tmpdir (if (featurep 'xemacs)
18147 temporary-file-directory))
18148 (texfilebase (make-temp-name
18149 (expand-file-name "orgtex" tmpdir)))
18150 (texfile (concat texfilebase ".tex"))
18151 (pdffile (concat texfilebase ".pdf"))
18152 (pngfile (concat texfilebase ".png"))
18153 (fnh (if (featurep 'xemacs)
18154 (font-height (face-font 'default))
18155 (face-attribute 'default :height nil)))
18156 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
18157 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
18158 (fg (or (plist-get options (if buffer :foreground :html-foreground))
18160 (bg (or (plist-get options (if buffer :background :html-background))
18162 (if (eq fg 'default) (setq fg (org-latex-color :foreground))
18163 (setq fg (org-latex-color-format fg)))
18164 (if (eq bg 'default) (setq bg (org-latex-color :background))
18165 (setq bg (org-latex-color-format
18166 (if (string= bg "Transparent") "white" bg))))
18167 (with-temp-file texfile
18168 (require 'ox-latex)
18169 (insert (org-latex-guess-inputenc
18170 (org-splice-latex-header
18171 org-format-latex-header
18172 org-latex-default-packages-alist
18173 org-latex-packages-alist t)))
18174 (insert "\n\\begin{document}\n"
18175 "\\definecolor{fg}{rgb}{" fg "}\n"
18176 "\\definecolor{bg}{rgb}{" bg "}\n"
18177 "\n\\pagecolor{bg}\n"
18181 "\n\\end{document}\n"))
18182 (let ((dir default-directory) cmd cmds latex-frags-cmds)
18183 (condition-case nil
18186 (setq cmds org-latex-pdf-process)
18188 (setq latex-frags-cmds (pop cmds))
18189 (if (listp latex-frags-cmds)
18191 (setq latex-frags-cmds (list (car org-latex-pdf-process)))))
18192 (while latex-frags-cmds
18193 (setq cmd (pop latex-frags-cmds))
18194 (while (string-match "%b" cmd)
18195 (setq cmd (replace-match
18197 (shell-quote-argument texfile))
18199 (while (string-match "%f" cmd)
18200 (setq cmd (replace-match
18202 (shell-quote-argument
18203 (file-name-nondirectory texfile)))
18205 (while (string-match "%o" cmd)
18206 (setq cmd (replace-match
18208 (shell-quote-argument
18209 (file-name-directory texfile)))
18211 (setq cmd (split-string cmd))
18212 (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
18215 (if (not (file-exists-p pdffile))
18216 (progn (message "Failed to create pdf file from %s" texfile) nil)
18217 (condition-case nil
18218 (if (featurep 'xemacs)
18219 (call-process "convert" nil nil nil
18225 ;; "-sharpen" "0x1.0"
18227 (call-process "convert" nil nil nil
18233 ;; "-sharpen" "0x1.0"
18236 (if (not (file-exists-p pngfile))
18237 (if org-format-latex-signal-error
18238 (error "Failed to create png file from %s" texfile)
18239 (message "Failed to create png file from %s" texfile)
18241 ;; Use the requested file name and clean up
18242 (copy-file pngfile tofile 'replace)
18243 (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
18244 (if (file-exists-p (concat texfilebase e))
18245 (delete-file (concat texfilebase e))))
18248 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
18249 "Fill a LaTeX header template TPL.
18250 In the template, the following place holders will be recognized:
18252 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
18253 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
18254 [PACKAGES] \\usepackage statements for PKG
18255 [NO-PACKAGES] do not include PKG
18256 [EXTRA] the string EXTRA
18257 [NO-EXTRA] do not include EXTRA
18259 For backward compatibility, if both the positive and the negative place
18260 holder is missing, the positive one (without the \"NO-\") will be
18261 assumed to be present at the end of the template.
18262 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
18264 SNIPPETS-P indicates if this is run to create snippet images for HTML."
18265 (let (rpl (end ""))
18266 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
18267 (setq rpl (if (or (match-end 1) (not def-pkg))
18268 "" (org-latex-packages-to-string def-pkg snippets-p t))
18269 tpl (replace-match rpl t t tpl))
18270 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
18272 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
18273 (setq rpl (if (or (match-end 1) (not pkg))
18274 "" (org-latex-packages-to-string pkg snippets-p t))
18275 tpl (replace-match rpl t t tpl))
18278 (org-latex-packages-to-string pkg snippets-p)))))
18280 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
18281 (setq rpl (if (or (match-end 1) (not extra))
18282 "" (concat extra "\n"))
18283 tpl (replace-match rpl t t tpl))
18284 (if (and extra (string-match "\\S-" extra))
18285 (setq end (concat end "\n" extra))))
18287 (if (string-match "\\S-" end)
18288 (concat tpl "\n" end)
18291 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
18292 "Turn an alist of packages into a string with the \\usepackage macros."
18293 (setq pkg (mapconcat (lambda(p)
18296 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
18297 (format "%% Package %s omitted" (cadr p)))
18298 ((equal "" (car p))
18299 (format "\\usepackage{%s}" (cadr p)))
18301 (format "\\usepackage[%s]{%s}"
18302 (car p) (cadr p)))))
18305 (if newline (concat pkg "\n") pkg))
18307 (defun org-dvipng-color (attr)
18308 "Return a RGB color specification for dvipng."
18309 (apply 'format "rgb %s %s %s"
18310 (mapcar 'org-normalize-color
18311 (if (featurep 'xemacs)
18312 (color-rgb-components
18313 (face-property 'default
18314 (cond ((eq attr :foreground) 'foreground)
18315 ((eq attr :background) 'background))))
18316 (color-values (face-attribute 'default attr nil))))))
18318 (defun org-dvipng-color-format (color-name)
18319 "Convert COLOR-NAME to a RGB color value for dvipng."
18320 (apply 'format "rgb %s %s %s"
18321 (mapcar 'org-normalize-color
18322 (color-values color-name))))
18324 (defun org-latex-color (attr)
18325 "Return a RGB color for the LaTeX color package."
18326 (apply 'format "%s,%s,%s"
18327 (mapcar 'org-normalize-color
18328 (if (featurep 'xemacs)
18329 (color-rgb-components
18330 (face-property 'default
18331 (cond ((eq attr :foreground) 'foreground)
18332 ((eq attr :background) 'background))))
18333 (color-values (face-attribute 'default attr nil))))))
18335 (defun org-latex-color-format (color-name)
18336 "Convert COLOR-NAME to a RGB color value."
18337 (apply 'format "%s,%s,%s"
18338 (mapcar 'org-normalize-color
18339 (color-values color-name))))
18341 (defun org-normalize-color (value)
18342 "Return string to be used as color value for an RGB component."
18343 (format "%g" (/ value 65535.0)))
18349 (defvar org-inline-image-overlays nil)
18350 (make-variable-buffer-local 'org-inline-image-overlays)
18352 (defun org-toggle-inline-images (&optional include-linked)
18353 "Toggle the display of inline images.
18354 INCLUDE-LINKED is passed to `org-display-inline-images'."
18356 (if org-inline-image-overlays
18358 (org-remove-inline-images)
18359 (message "Inline image display turned off"))
18360 (org-display-inline-images include-linked)
18361 (if (and (org-called-interactively-p)
18362 org-inline-image-overlays)
18363 (message "%d images displayed inline"
18364 (length org-inline-image-overlays))
18365 (message "No images to display inline"))))
18367 (defun org-redisplay-inline-images ()
18368 "Refresh the display of inline images."
18370 (if (not org-inline-image-overlays)
18371 (org-toggle-inline-images)
18372 (org-toggle-inline-images)
18373 (org-toggle-inline-images)))
18375 (defun org-display-inline-images (&optional include-linked refresh beg end)
18376 "Display inline images.
18377 Normally only links without a description part are inlined, because this
18378 is how it will work for export. When INCLUDE-LINKED is set, also links
18379 with a description part will be inlined. This can be nice for a quick
18380 look at those images, but it does not reflect what exported files will look
18382 When REFRESH is set, refresh existing images between BEG and END.
18383 This will create new image displays only if necessary.
18384 BEG and END default to the buffer boundaries."
18387 (org-remove-inline-images)
18388 (if (fboundp 'clear-image-cache) (clear-image-cache)))
18392 (setq beg (or beg (point-min)) end (or end (point-max)))
18394 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
18395 (substring (org-image-file-name-regexp) 0 -2)
18396 "\\)\\]" (if include-linked "" "\\]")))
18397 old file ov img type attrwidth width)
18398 (while (re-search-forward re end t)
18399 (setq old (get-char-property-and-overlay (match-beginning 1)
18400 'org-image-overlay)
18401 file (expand-file-name
18402 (concat (or (match-string 3) "") (match-string 4))))
18403 (when (image-type-available-p 'imagemagick)
18404 (setq attrwidth (if (or (listp org-image-actual-width)
18405 (null org-image-actual-width))
18408 (when (re-search-backward
18409 "#\\+ATTR.*width=\"\\([^\"]+\\)\""
18411 (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
18412 (string-to-number (match-string 1))))))
18413 width (cond ((eq org-image-actual-width t) nil)
18414 ((null org-image-actual-width) attrwidth)
18415 ((numberp org-image-actual-width)
18416 org-image-actual-width)
18417 ((listp org-image-actual-width)
18418 (or attrwidth (car org-image-actual-width))))
18419 type (if width 'imagemagick)))
18420 (when (file-exists-p file)
18421 (if (and (car-safe old) refresh)
18422 (image-refresh (overlay-get (cdr old) 'display))
18423 (setq img (save-match-data (create-image file type nil :width width)))
18425 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
18426 (overlay-put ov 'display img)
18427 (overlay-put ov 'face 'default)
18428 (overlay-put ov 'org-image-overlay t)
18429 (overlay-put ov 'modification-hooks
18430 (list 'org-display-inline-remove-overlay))
18431 (push ov org-inline-image-overlays)))))))))
18433 (define-obsolete-function-alias
18434 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
18436 (defun org-display-inline-remove-overlay (ov after beg end &optional len)
18437 "Remove inline-display overlay if a corresponding region is modified."
18438 (let ((inhibit-modification-hooks t))
18439 (when (and ov after)
18440 (delete ov org-inline-image-overlays)
18441 (delete-overlay ov))))
18443 (defun org-remove-inline-images ()
18444 "Remove inline display of images."
18446 (mapc 'delete-overlay org-inline-image-overlays)
18447 (setq org-inline-image-overlays nil))
18451 ;; Outline functions from `outline-mode-prefix-map'
18452 ;; that can be remapped in Org:
18453 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
18454 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
18455 (define-key org-mode-map [remap outline-forward-same-level]
18456 'org-forward-heading-same-level)
18457 (define-key org-mode-map [remap outline-backward-same-level]
18458 'org-backward-heading-same-level)
18459 (define-key org-mode-map [remap show-branches]
18460 'org-kill-note-or-show-branches)
18461 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
18462 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
18463 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
18465 ;; Outline functions from `outline-mode-prefix-map' that can not
18466 ;; be remapped in Org:
18468 ;; - the column "key binding" shows whether the Outline function is still
18469 ;; available in Org mode on the same key that it has been bound to in
18471 ;; - "overridden": key used for a different functionality in Org mode
18472 ;; - else: key still bound to the same Outline function in Org mode
18474 ;; | Outline function | key binding | Org replacement |
18475 ;; |------------------------------------+-------------+-----------------------|
18476 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
18477 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
18478 ;; | `outline-up-heading' | `C-c C-u' | still same function |
18479 ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
18480 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
18481 ;; | `show-entry' | overridden | no replacement |
18482 ;; | `show-children' | `C-c C-i' | visibility cycling |
18483 ;; | `show-branches' | `C-c C-k' | still same function |
18484 ;; | `show-subtree' | overridden | visibility cycling |
18485 ;; | `show-all' | overridden | no replacement |
18486 ;; | `hide-subtree' | overridden | visibility cycling |
18487 ;; | `hide-body' | overridden | no replacement |
18488 ;; | `hide-entry' | overridden | visibility cycling |
18489 ;; | `hide-leaves' | overridden | no replacement |
18490 ;; | `hide-sublevels' | overridden | no replacement |
18491 ;; | `hide-other' | overridden | no replacement |
18493 ;; Make `C-c C-x' a prefix key
18494 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
18496 ;; TAB key with modifiers
18497 (org-defkey org-mode-map "\C-i" 'org-cycle)
18498 (org-defkey org-mode-map [(tab)] 'org-cycle)
18499 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
18500 (org-defkey org-mode-map "\M-\t" 'pcomplete)
18501 ;; The following line is necessary under Suse GNU/Linux
18502 (unless (featurep 'xemacs)
18503 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
18504 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
18505 (define-key org-mode-map [backtab] 'org-shifttab)
18507 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
18508 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
18509 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
18511 ;; Cursor keys with modifiers
18512 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
18513 (org-defkey org-mode-map [(meta right)] 'org-metaright)
18514 (org-defkey org-mode-map [(meta up)] 'org-metaup)
18515 (org-defkey org-mode-map [(meta down)] 'org-metadown)
18517 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
18518 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
18519 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
18520 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
18522 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
18523 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
18524 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
18525 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
18527 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
18528 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
18529 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
18530 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
18533 (define-key org-mode-map org-babel-key-prefix org-babel-map)
18534 (mapc (lambda (pair)
18535 (define-key org-babel-map (car pair) (cdr pair)))
18536 org-babel-key-bindings)
18538 ;;; Extra keys for tty access.
18539 ;; We only set them when really needed because otherwise the
18540 ;; menus don't show the simple keys
18542 (when (or org-use-extra-keys
18543 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
18544 (not window-system))
18545 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
18546 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
18547 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
18548 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
18549 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
18550 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
18551 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
18552 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
18553 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
18554 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
18555 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
18556 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
18557 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
18558 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
18559 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
18560 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
18561 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
18562 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
18563 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
18564 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
18565 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
18566 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
18567 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
18568 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
18569 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
18570 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
18571 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
18572 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
18574 ;; All the other keys
18576 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
18577 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
18578 (if (boundp 'narrow-map)
18579 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
18580 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
18581 (if (boundp 'narrow-map)
18582 (org-defkey narrow-map "b" 'org-narrow-to-block)
18583 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
18584 (if (boundp 'narrow-map)
18585 (org-defkey narrow-map "e" 'org-narrow-to-element)
18586 (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
18587 (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
18588 (org-defkey org-mode-map "\M-}" 'org-forward-element)
18589 (org-defkey org-mode-map "\M-{" 'org-backward-element)
18590 (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
18591 (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
18592 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
18593 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
18594 (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
18595 (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
18596 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
18597 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
18598 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
18599 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
18600 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
18601 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
18602 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
18603 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
18604 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
18605 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
18606 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
18607 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
18608 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
18609 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
18610 (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
18611 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
18612 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
18613 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
18614 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
18615 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
18616 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
18617 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
18618 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
18619 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
18620 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
18621 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
18622 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
18623 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
18624 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
18625 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
18626 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
18627 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
18628 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
18629 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
18630 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
18631 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
18632 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
18633 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
18634 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
18635 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
18636 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
18637 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
18638 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
18639 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
18640 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
18641 (org-defkey org-mode-map "\C-c^" 'org-sort)
18642 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
18643 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
18644 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
18645 (org-defkey org-mode-map "\C-m" 'org-return)
18646 (org-defkey org-mode-map "\C-j" 'org-return-indent)
18647 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
18648 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
18649 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
18650 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
18651 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
18652 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
18653 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
18654 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
18655 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
18656 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
18657 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
18658 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
18659 (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
18660 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
18661 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
18662 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
18663 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
18664 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
18665 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
18666 (org-defkey org-mode-map "\M-h" 'org-mark-element)
18667 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
18668 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
18670 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
18671 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
18672 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
18674 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
18675 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
18676 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
18677 (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
18678 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
18679 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
18680 (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
18681 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
18682 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
18683 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
18684 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
18685 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
18686 (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
18687 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
18688 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
18689 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
18690 (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
18691 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
18692 (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
18693 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
18694 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
18695 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
18696 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
18698 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
18699 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
18700 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
18701 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
18702 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
18704 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
18706 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
18708 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
18709 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
18711 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
18714 (when (featurep 'xemacs)
18715 (org-defkey org-mode-map 'button3 'popup-mode-menu))
18718 (defconst org-speed-commands-default
18720 ("Outline Navigation")
18721 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
18722 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
18723 ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
18724 ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
18725 ("F" . org-next-block)
18726 ("B" . org-previous-block)
18727 ("u" . (org-speed-move-safe 'outline-up-heading))
18729 ("g" . (org-refile t))
18730 ("Outline Visibility")
18732 ("C" . org-shifttab)
18733 (" " . org-display-outline-path)
18734 ("=" . org-columns)
18735 ("Outline Structure Editing")
18736 ("U" . org-shiftmetaup)
18737 ("D" . org-shiftmetadown)
18738 ("r" . org-metaright)
18739 ("l" . org-metaleft)
18740 ("R" . org-shiftmetaright)
18741 ("L" . org-shiftmetaleft)
18742 ("i" . (progn (forward-char 1) (call-interactively
18743 'org-insert-heading-respect-content)))
18746 ("a" . org-archive-subtree-default-with-confirmation)
18747 ("@" . org-mark-subtree)
18748 ("#" . org-toggle-comment)
18750 ("I" . org-clock-in)
18751 ("O" . org-clock-out)
18752 ("Meta Data Editing")
18754 ("," . (org-priority))
18755 ("0" . (org-priority ?\ ))
18756 ("1" . (org-priority ?A))
18757 ("2" . (org-priority ?B))
18758 ("3" . (org-priority ?C))
18759 (":" . org-set-tags-command)
18760 ("e" . org-set-effort)
18761 ("E" . org-inc-effort)
18762 ("W" . (lambda(m) (interactive "sMinutes before warning: ")
18763 (org-entry-put (point) "APPT_WARNTIME" m)))
18764 ("Agenda Views etc")
18766 ("/" . org-sparse-tree)
18768 ("o" . org-open-at-point)
18769 ("?" . org-speed-command-help)
18770 ("<" . (org-agenda-set-restriction-lock 'subtree))
18771 (">" . (org-agenda-remove-restriction-lock))
18773 "The default speed commands.")
18775 (defun org-print-speed-command (e)
18776 (if (> (length (car e)) 1)
18781 (princ (make-string (length (car e)) ?-))
18785 (if (symbolp (cdr e))
18786 (princ (symbol-name (cdr e)))
18790 (defun org-speed-command-help ()
18791 "Show the available speed commands."
18793 (if (not org-use-speed-commands)
18794 (error "Speed commands are not activated, customize `org-use-speed-commands'")
18795 (with-output-to-temp-buffer "*Help*"
18796 (princ "User-defined Speed commands\n===========================\n")
18797 (mapc 'org-print-speed-command org-speed-commands-user)
18799 (princ "Built-in Speed commands\n=======================\n")
18800 (mapc 'org-print-speed-command org-speed-commands-default))
18801 (with-current-buffer "*Help*"
18802 (setq truncate-lines t))))
18804 (defun org-speed-move-safe (cmd)
18805 "Execute CMD, but make sure that the cursor always ends up in a headline.
18806 If not, return to the original position and throw an error."
18808 (let ((pos (point)))
18809 (call-interactively cmd)
18810 (unless (and (bolp) (org-at-heading-p))
18812 (error "Boundary reached while executing %s" cmd))))
18814 (defvar org-self-insert-command-undo-counter 0)
18816 (defvar org-table-auto-blank-field) ; defined in org-table.el
18817 (defvar org-speed-command nil)
18819 (define-obsolete-function-alias
18820 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
18822 (defun org-speed-command-activate (keys)
18823 "Hook for activating single-letter speed commands.
18824 `org-speed-commands-default' specifies a minimal command set.
18825 Use `org-speed-commands-user' for further customization."
18826 (when (or (and (bolp) (looking-at org-outline-regexp))
18827 (and (functionp org-use-speed-commands)
18828 (funcall org-use-speed-commands)))
18829 (cdr (assoc keys (append org-speed-commands-user
18830 org-speed-commands-default)))))
18832 (define-obsolete-function-alias
18833 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
18835 (defun org-babel-speed-command-activate (keys)
18836 "Hook for activating single-letter code block commands."
18837 (when (and (bolp) (looking-at org-babel-src-block-regexp))
18838 (cdr (assoc keys org-babel-key-bindings))))
18840 (defcustom org-speed-command-hook
18841 '(org-speed-command-default-hook org-babel-speed-command-hook)
18842 "Hook for activating speed commands at strategic locations.
18843 Hook functions are called in sequence until a valid handler is
18846 Each hook takes a single argument, a user-pressed command key
18847 which is also a `self-insert-command' from the global map.
18849 Within the hook, examine the cursor position and the command key
18850 and return nil or a valid handler as appropriate. Handler could
18851 be one of an interactive command, a function, or a form.
18853 Set `org-use-speed-commands' to non-nil value to enable this
18854 hook. The default setting is `org-speed-command-activate'."
18855 :group 'org-structure
18859 (defun org-self-insert-command (N)
18860 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
18861 If the cursor is in a table looking at whitespace, the whitespace is
18862 overwritten, and the table is not marked as requiring realignment."
18864 (org-check-before-invisible-edit 'insert)
18866 ((and org-use-speed-commands
18867 (setq org-speed-command
18868 (run-hook-with-args-until-success
18869 'org-speed-command-hook (this-command-keys))))
18871 ((commandp org-speed-command)
18872 (setq this-command org-speed-command)
18873 (call-interactively org-speed-command))
18874 ((functionp org-speed-command)
18875 (funcall org-speed-command))
18876 ((and org-speed-command (listp org-speed-command))
18877 (eval org-speed-command))
18878 (t (let (org-use-speed-commands)
18879 (call-interactively 'org-self-insert-command)))))
18883 ;; check if we blank the field, and if that triggers align
18884 (and (featurep 'org-table) org-table-auto-blank-field
18885 (member last-command
18886 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
18887 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
18888 ;; got extra space, this field does not determine column width
18889 (let (org-table-may-need-update) (org-table-blank-field))
18890 ;; no extra space, this field may determine column width
18891 (org-table-blank-field)))
18894 (looking-at "[^|\n]* |"))
18895 (let (org-table-may-need-update)
18896 (goto-char (1- (match-end 0)))
18897 (backward-delete-char 1)
18898 (goto-char (match-beginning 0))
18899 (self-insert-command N)))
18901 (setq org-table-may-need-update t)
18902 (self-insert-command N)
18903 (org-fix-tags-on-the-fly)
18904 (if org-self-insert-cluster-for-undo
18905 (if (not (eq last-command 'org-self-insert-command))
18906 (setq org-self-insert-command-undo-counter 1)
18907 (if (>= org-self-insert-command-undo-counter 20)
18908 (setq org-self-insert-command-undo-counter 1)
18909 (and (> org-self-insert-command-undo-counter 0)
18910 buffer-undo-list (listp buffer-undo-list)
18911 (not (cadr buffer-undo-list)) ; remove nil entry
18912 (setcdr buffer-undo-list (cddr buffer-undo-list)))
18913 (setq org-self-insert-command-undo-counter
18914 (1+ org-self-insert-command-undo-counter))))))))
18916 (defun org-check-before-invisible-edit (kind)
18917 "Check is editing if kind KIND would be dangerous with invisible text around.
18918 The detailed reaction depends on the user option `org-catch-invisible-edits'."
18919 ;; First, try to get out of here as quickly as possible, to reduce overhead
18920 (if (and org-catch-invisible-edits
18921 (or (not (boundp 'visible-mode)) (not visible-mode))
18922 (or (get-char-property (point) 'invisible)
18923 (get-char-property (max (point-min) (1- (point))) 'invisible)))
18924 ;; OK, we need to take a closer look
18925 (let* ((invisible-at-point (get-char-property (point) 'invisible))
18926 (invisible-before-point (if (bobp) nil (get-char-property
18927 (1- (point)) 'invisible)))
18928 (border-and-ok-direction
18930 ;; Check if we are acting predictably before invisible text
18931 (and invisible-at-point (not invisible-before-point)
18932 (memq kind '(insert delete-backward)))
18933 ;; Check if we are acting predictably after invisible text
18934 ;; This works not well, and I have turned it off. It seems
18935 ;; better to always show and stop after invisible text.
18936 ;; (and (not invisible-at-point) invisible-before-point
18937 ;; (memq kind '(insert delete)))
18939 (when (or (memq invisible-at-point '(outline org-hide-block t))
18940 (memq invisible-before-point '(outline org-hide-block t)))
18941 (if (eq org-catch-invisible-edits 'error)
18942 (error "Editing in invisible areas is prohibited - make visible first"))
18943 (if (and org-custom-properties-overlays
18944 (y-or-n-p "Display invisible properties in this buffer? "))
18945 (org-toggle-custom-properties-visibility)
18946 ;; Make the area visible
18948 (if invisible-before-point
18949 (goto-char (previous-single-char-property-change
18950 (point) 'invisible)))
18953 ((eq org-catch-invisible-edits 'show)
18954 ;; That's it, we do the edit after showing
18956 "Unfolding invisible region around point before editing")
18958 ((and (eq org-catch-invisible-edits 'smart)
18959 border-and-ok-direction)
18960 (message "Unfolding invisible region around point before editing"))
18962 ;; Don't do the edit, make the user repeat it in full visibility
18963 (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
18965 (defun org-fix-tags-on-the-fly ()
18966 (when (and (equal (char-after (point-at-bol)) ?*)
18967 (org-at-heading-p))
18968 (org-align-tags-here org-tags-column)))
18970 (defun org-delete-backward-char (N)
18971 "Like `delete-backward-char', insert whitespace at field end in tables.
18972 When deleting backwards, in tables this function will insert whitespace in
18973 front of the next \"|\" separator, to keep the table aligned. The table will
18974 still be marked for re-alignment if the field did fill the entire column,
18975 because, in this case the deletion might narrow the column."
18978 (org-check-before-invisible-edit 'delete-backward)
18979 (if (and (org-table-p)
18981 (string-match "|" (buffer-substring (point-at-bol) (point)))
18982 (looking-at ".*?|"))
18983 (let ((pos (point))
18984 (noalign (looking-at "[^|\n\r]* |"))
18985 (c org-table-may-need-update))
18986 (backward-delete-char N)
18987 (if (not overwrite-mode)
18989 (skip-chars-forward "^|")
18991 (goto-char (1- pos))))
18992 ;; noalign: if there were two spaces at the end, this field
18993 ;; does not determine the width of the column.
18994 (if noalign (setq org-table-may-need-update c)))
18995 (backward-delete-char N)
18996 (org-fix-tags-on-the-fly))))
18998 (defun org-delete-char (N)
18999 "Like `delete-char', but insert whitespace at field end in tables.
19000 When deleting characters, in tables this function will insert whitespace in
19001 front of the next \"|\" separator, to keep the table aligned. The table will
19002 still be marked for re-alignment if the field did fill the entire column,
19003 because, in this case the deletion might narrow the column."
19006 (org-check-before-invisible-edit 'delete)
19007 (if (and (org-table-p)
19009 (not (= (char-after) ?|))
19011 (if (looking-at ".*?|")
19012 (let ((pos (point))
19013 (noalign (looking-at "[^|\n\r]* |"))
19014 (c org-table-may-need-update))
19015 (replace-match (concat
19016 (substring (match-string 0) 1 -1)
19019 ;; noalign: if there were two spaces at the end, this field
19020 ;; does not determine the width of the column.
19021 (if noalign (setq org-table-may-need-update c)))
19024 (org-fix-tags-on-the-fly))))
19026 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
19027 (put 'org-self-insert-command 'delete-selection t)
19028 (put 'orgtbl-self-insert-command 'delete-selection t)
19029 (put 'org-delete-char 'delete-selection 'supersede)
19030 (put 'org-delete-backward-char 'delete-selection 'supersede)
19031 (put 'org-yank 'delete-selection 'yank)
19033 ;; Make `flyspell-mode' delay after some commands
19034 (put 'org-self-insert-command 'flyspell-delayed t)
19035 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
19036 (put 'org-delete-char 'flyspell-delayed t)
19037 (put 'org-delete-backward-char 'flyspell-delayed t)
19039 ;; Make pabbrev-mode expand after org-mode commands
19040 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
19041 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
19043 ;; How to do this: Measure non-white length of current string
19044 ;; If equal to column width, we should realign.
19046 (defun org-remap (map &rest commands)
19047 "In MAP, remap the functions given in COMMANDS.
19048 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
19051 (setq old (pop commands) new (pop commands))
19052 (if (fboundp 'command-remapping)
19053 (org-defkey map (vector 'remap old) new)
19054 (substitute-key-definition old new map global-map)))))
19056 (defun org-transpose-words ()
19057 "Transpose words for Org.
19058 This uses the `org-mode-transpose-word-syntax-table' syntax
19059 table, which interprets characters in `org-emphasis-alist' as
19060 word constituants."
19062 (with-syntax-table org-mode-transpose-word-syntax-table
19063 (call-interactively 'transpose-words)))
19064 (org-remap org-mode-map 'transpose-words 'org-transpose-words)
19066 (when (eq org-enable-table-editor 'optimized)
19067 ;; If the user wants maximum table support, we need to hijack
19068 ;; some standard editing functions
19069 (org-remap org-mode-map
19070 'self-insert-command 'org-self-insert-command
19071 'delete-char 'org-delete-char
19072 'delete-backward-char 'org-delete-backward-char)
19073 (org-defkey org-mode-map "|" 'org-force-self-insert))
19075 (defvar org-ctrl-c-ctrl-c-hook nil
19076 "Hook for functions attaching themselves to `C-c C-c'.
19078 This can be used to add additional functionality to the C-c C-c
19079 key which executes context-dependent commands. This hook is run
19080 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
19081 run after the last test.
19083 Each function will be called with no arguments. The function
19084 must check if the context is appropriate for it to act. If yes,
19085 it should do its thing and then return a non-nil value. If the
19086 context is wrong, just do nothing and return nil.")
19088 (defvar org-ctrl-c-ctrl-c-final-hook nil
19089 "Hook for functions attaching themselves to `C-c C-c'.
19091 This can be used to add additional functionality to the C-c C-c
19092 key which executes context-dependent commands. This hook is run
19093 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
19094 before the first test.
19096 Each function will be called with no arguments. The function
19097 must check if the context is appropriate for it to act. If yes,
19098 it should do its thing and then return a non-nil value. If the
19099 context is wrong, just do nothing and return nil.")
19101 (defvar org-tab-first-hook nil
19102 "Hook for functions to attach themselves to TAB.
19103 See `org-ctrl-c-ctrl-c-hook' for more information.
19104 This hook runs as the first action when TAB is pressed, even before
19105 `org-cycle' messes around with the `outline-regexp' to cater for
19106 inline tasks and plain list item folding.
19107 If any function in this hook returns t, any other actions that
19108 would have been caused by TAB (such as table field motion or visibility
19109 cycling) will not occur.")
19111 (defvar org-tab-after-check-for-table-hook nil
19112 "Hook for functions to attach themselves to TAB.
19113 See `org-ctrl-c-ctrl-c-hook' for more information.
19114 This hook runs after it has been established that the cursor is not in a
19115 table, but before checking if the cursor is in a headline or if global cycling
19117 If any function in this hook returns t, not other actions like visibility
19118 cycling will be done.")
19120 (defvar org-tab-after-check-for-cycling-hook nil
19121 "Hook for functions to attach themselves to TAB.
19122 See `org-ctrl-c-ctrl-c-hook' for more information.
19123 This hook runs after it has been established that not table field motion and
19124 not visibility should be done because of current context. This is probably
19125 the place where a package like yasnippets can hook in.")
19127 (defvar org-tab-before-tab-emulation-hook nil
19128 "Hook for functions to attach themselves to TAB.
19129 See `org-ctrl-c-ctrl-c-hook' for more information.
19130 This hook runs after every other options for TAB have been exhausted, but
19131 before indentation and \t insertion takes place.")
19133 (defvar org-metaleft-hook nil
19134 "Hook for functions attaching themselves to `M-left'.
19135 See `org-ctrl-c-ctrl-c-hook' for more information.")
19136 (defvar org-metaright-hook nil
19137 "Hook for functions attaching themselves to `M-right'.
19138 See `org-ctrl-c-ctrl-c-hook' for more information.")
19139 (defvar org-metaup-hook nil
19140 "Hook for functions attaching themselves to `M-up'.
19141 See `org-ctrl-c-ctrl-c-hook' for more information.")
19142 (defvar org-metadown-hook nil
19143 "Hook for functions attaching themselves to `M-down'.
19144 See `org-ctrl-c-ctrl-c-hook' for more information.")
19145 (defvar org-shiftmetaleft-hook nil
19146 "Hook for functions attaching themselves to `M-S-left'.
19147 See `org-ctrl-c-ctrl-c-hook' for more information.")
19148 (defvar org-shiftmetaright-hook nil
19149 "Hook for functions attaching themselves to `M-S-right'.
19150 See `org-ctrl-c-ctrl-c-hook' for more information.")
19151 (defvar org-shiftmetaup-hook nil
19152 "Hook for functions attaching themselves to `M-S-up'.
19153 See `org-ctrl-c-ctrl-c-hook' for more information.")
19154 (defvar org-shiftmetadown-hook nil
19155 "Hook for functions attaching themselves to `M-S-down'.
19156 See `org-ctrl-c-ctrl-c-hook' for more information.")
19157 (defvar org-metareturn-hook nil
19158 "Hook for functions attaching themselves to `M-RET'.
19159 See `org-ctrl-c-ctrl-c-hook' for more information.")
19160 (defvar org-shiftup-hook nil
19161 "Hook for functions attaching themselves to `S-up'.
19162 See `org-ctrl-c-ctrl-c-hook' for more information.")
19163 (defvar org-shiftup-final-hook nil
19164 "Hook for functions attaching themselves to `S-up'.
19165 This one runs after all other options except shift-select have been excluded.
19166 See `org-ctrl-c-ctrl-c-hook' for more information.")
19167 (defvar org-shiftdown-hook nil
19168 "Hook for functions attaching themselves to `S-down'.
19169 See `org-ctrl-c-ctrl-c-hook' for more information.")
19170 (defvar org-shiftdown-final-hook nil
19171 "Hook for functions attaching themselves to `S-down'.
19172 This one runs after all other options except shift-select have been excluded.
19173 See `org-ctrl-c-ctrl-c-hook' for more information.")
19174 (defvar org-shiftleft-hook nil
19175 "Hook for functions attaching themselves to `S-left'.
19176 See `org-ctrl-c-ctrl-c-hook' for more information.")
19177 (defvar org-shiftleft-final-hook nil
19178 "Hook for functions attaching themselves to `S-left'.
19179 This one runs after all other options except shift-select have been excluded.
19180 See `org-ctrl-c-ctrl-c-hook' for more information.")
19181 (defvar org-shiftright-hook nil
19182 "Hook for functions attaching themselves to `S-right'.
19183 See `org-ctrl-c-ctrl-c-hook' for more information.")
19184 (defvar org-shiftright-final-hook nil
19185 "Hook for functions attaching themselves to `S-right'.
19186 This one runs after all other options except shift-select have been excluded.
19187 See `org-ctrl-c-ctrl-c-hook' for more information.")
19189 (defun org-modifier-cursor-error ()
19190 "Throw an error, a modified cursor command was applied in wrong context."
19191 (error "This command is active in special context like tables, headlines or items"))
19193 (defun org-shiftselect-error ()
19194 "Throw an error because Shift-Cursor command was applied in wrong context."
19195 (if (and (boundp 'shift-select-mode) shift-select-mode)
19196 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
19197 (error "This command works only in special context like headlines or timestamps")))
19199 (defun org-call-for-shift-select (cmd)
19200 (let ((this-command-keys-shift-translated t))
19201 (call-interactively cmd)))
19203 (defun org-shifttab (&optional arg)
19204 "Global visibility cycling or move to previous table field.
19205 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
19207 See the individual commands for more information."
19210 ((org-at-table-p) (call-interactively 'org-table-previous-field))
19212 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
19213 (message "Content view to level: %d" arg)
19214 (org-content (prefix-numeric-value arg2))
19215 (setq org-cycle-global-status 'overview)))
19216 (t (call-interactively 'org-global-cycle))))
19218 (defun org-shiftmetaleft ()
19219 "Promote subtree or delete table column.
19220 Calls `org-promote-subtree', `org-outdent-item-tree', or
19221 `org-table-delete-column', depending on context. See the
19222 individual commands for more information."
19225 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
19226 ((org-at-table-p) (call-interactively 'org-table-delete-column))
19227 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
19228 ((if (not (org-region-active-p)) (org-at-item-p)
19229 (save-excursion (goto-char (region-beginning))
19231 (call-interactively 'org-outdent-item-tree))
19232 (t (org-modifier-cursor-error))))
19234 (defun org-shiftmetaright ()
19235 "Demote subtree or insert table column.
19236 Calls `org-demote-subtree', `org-indent-item-tree', or
19237 `org-table-insert-column', depending on context. See the
19238 individual commands for more information."
19241 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
19242 ((org-at-table-p) (call-interactively 'org-table-insert-column))
19243 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
19244 ((if (not (org-region-active-p)) (org-at-item-p)
19245 (save-excursion (goto-char (region-beginning))
19247 (call-interactively 'org-indent-item-tree))
19248 (t (org-modifier-cursor-error))))
19250 (defun org-shiftmetaup (&optional arg)
19251 "Move subtree up or kill table row.
19252 Calls `org-move-subtree-up' or `org-table-kill-row' or
19253 `org-move-item-up' or `org-timestamp-up', depending on context.
19254 See the individual commands for more information."
19257 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
19258 ((org-at-table-p) (call-interactively 'org-table-kill-row))
19259 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
19260 ((org-at-item-p) (call-interactively 'org-move-item-up))
19261 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
19262 (call-interactively 'org-timestamp-up)))
19263 (t (call-interactively 'org-drag-line-backward))))
19265 (defun org-shiftmetadown (&optional arg)
19266 "Move subtree down or insert table row.
19267 Calls `org-move-subtree-down' or `org-table-insert-row' or
19268 `org-move-item-down' or `org-timestamp-up', depending on context.
19269 See the individual commands for more information."
19272 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
19273 ((org-at-table-p) (call-interactively 'org-table-insert-row))
19274 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
19275 ((org-at-item-p) (call-interactively 'org-move-item-down))
19276 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
19277 (call-interactively 'org-timestamp-down)))
19278 (t (call-interactively 'org-drag-line-forward))))
19280 (defsubst org-hidden-tree-error ()
19282 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
19284 (defun org-metaleft (&optional arg)
19285 "Promote heading or move table column to left.
19286 Calls `org-do-promote' or `org-table-move-column', depending on context.
19287 With no specific context, calls the Emacs default `backward-word'.
19288 See the individual commands for more information."
19291 ((run-hook-with-args-until-success 'org-metaleft-hook))
19292 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
19293 ((org-with-limited-levels
19294 (or (org-at-heading-p)
19295 (and (org-region-active-p)
19297 (goto-char (region-beginning))
19298 (org-at-heading-p)))))
19299 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
19300 (call-interactively 'org-do-promote))
19301 ;; At an inline task.
19302 ((org-at-heading-p)
19303 (call-interactively 'org-inlinetask-promote))
19304 ((or (org-at-item-p)
19305 (and (org-region-active-p)
19307 (goto-char (region-beginning))
19309 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
19310 (call-interactively 'org-outdent-item))
19311 (t (call-interactively 'backward-word))))
19313 (defun org-metaright (&optional arg)
19314 "Demote a subtree, a list item or move table column to right.
19315 In front of a drawer or a block keyword, indent it correctly.
19316 With no specific context, calls the Emacs default `forward-word'.
19317 See the individual commands for more information."
19320 ((run-hook-with-args-until-success 'org-metaright-hook))
19321 ((org-at-table-p) (call-interactively 'org-table-move-column))
19322 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
19323 ((org-at-block-p) (call-interactively 'org-indent-block))
19324 ((org-with-limited-levels
19325 (or (org-at-heading-p)
19326 (and (org-region-active-p)
19328 (goto-char (region-beginning))
19329 (org-at-heading-p)))))
19330 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
19331 (call-interactively 'org-do-demote))
19332 ;; At an inline task.
19333 ((org-at-heading-p)
19334 (call-interactively 'org-inlinetask-demote))
19335 ((or (org-at-item-p)
19336 (and (org-region-active-p)
19338 (goto-char (region-beginning))
19340 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
19341 (call-interactively 'org-indent-item))
19342 (t (call-interactively 'forward-word))))
19344 (defun org-check-for-hidden (what)
19345 "Check if there are hidden headlines/items in the current visual line.
19346 WHAT can be either `headlines' or `items'. If the current line is
19347 an outline or item heading and it has a folded subtree below it,
19348 this function returns t, nil otherwise."
19350 ((eq what 'headlines) org-outline-regexp-bol)
19351 ((eq what 'items) (org-item-beginning-re))
19352 (t (error "This should not happen"))))
19356 (unless (org-region-active-p)
19357 (setq beg (point-at-bol))
19358 (beginning-of-line 2)
19359 (while (and (not (eobp)) ;; this is like `next-line'
19360 (get-char-property (1- (point)) 'invisible))
19361 (beginning-of-line 2))
19364 (goto-char (point-at-eol))
19365 (setq end (max end (point)))
19366 (while (re-search-forward re end t)
19367 (if (get-char-property (match-beginning 0) 'invisible)
19371 (defun org-metaup (&optional arg)
19372 "Move subtree up or move table row up.
19373 Calls `org-move-subtree-up' or `org-table-move-row' or
19374 `org-move-item-up', depending on context. See the individual commands
19375 for more information."
19378 ((run-hook-with-args-until-success 'org-metaup-hook))
19379 ((org-region-active-p)
19380 (let* ((a (min (region-beginning) (region-end)))
19381 (b (1- (max (region-beginning) (region-end))))
19382 (c (save-excursion (goto-char a)
19383 (move-beginning-of-line 0)))
19384 (d (save-excursion (goto-char a)
19385 (move-end-of-line 0) (point))))
19386 (transpose-regions a b c d)
19388 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
19389 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
19390 ((org-at-item-p) (call-interactively 'org-move-item-up))
19391 (t (org-drag-element-backward))))
19393 (defun org-metadown (&optional arg)
19394 "Move subtree down or move table row down.
19395 Calls `org-move-subtree-down' or `org-table-move-row' or
19396 `org-move-item-down', depending on context. See the individual
19397 commands for more information."
19400 ((run-hook-with-args-until-success 'org-metadown-hook))
19401 ((org-region-active-p)
19402 (let* ((a (min (region-beginning) (region-end)))
19403 (b (max (region-beginning) (region-end)))
19404 (c (save-excursion (goto-char b)
19405 (move-beginning-of-line 1)))
19406 (d (save-excursion (goto-char b)
19407 (move-end-of-line 1) (1+ (point)))))
19408 (transpose-regions a b c d)
19410 ((org-at-table-p) (call-interactively 'org-table-move-row))
19411 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
19412 ((org-at-item-p) (call-interactively 'org-move-item-down))
19413 (t (org-drag-element-forward))))
19415 (defun org-shiftup (&optional arg)
19416 "Increase item in timestamp or increase priority of current headline.
19417 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
19418 depending on context. See the individual commands for more information."
19421 ((run-hook-with-args-until-success 'org-shiftup-hook))
19422 ((and org-support-shift-select (org-region-active-p))
19423 (org-call-for-shift-select 'previous-line))
19424 ((org-at-timestamp-p t)
19425 (call-interactively (if org-edit-timestamp-down-means-later
19426 'org-timestamp-down 'org-timestamp-up)))
19427 ((and (not (eq org-support-shift-select 'always))
19428 org-enable-priority-commands
19429 (org-at-heading-p))
19430 (call-interactively 'org-priority-up))
19431 ((and (not org-support-shift-select) (org-at-item-p))
19432 (call-interactively 'org-previous-item))
19433 ((org-clocktable-try-shift 'up arg))
19434 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
19435 (org-support-shift-select
19436 (org-call-for-shift-select 'previous-line))
19437 (t (org-shiftselect-error))))
19439 (defun org-shiftdown (&optional arg)
19440 "Decrease item in timestamp or decrease priority of current headline.
19441 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
19442 depending on context. See the individual commands for more information."
19445 ((run-hook-with-args-until-success 'org-shiftdown-hook))
19446 ((and org-support-shift-select (org-region-active-p))
19447 (org-call-for-shift-select 'next-line))
19448 ((org-at-timestamp-p t)
19449 (call-interactively (if org-edit-timestamp-down-means-later
19450 'org-timestamp-up 'org-timestamp-down)))
19451 ((and (not (eq org-support-shift-select 'always))
19452 org-enable-priority-commands
19453 (org-at-heading-p))
19454 (call-interactively 'org-priority-down))
19455 ((and (not org-support-shift-select) (org-at-item-p))
19456 (call-interactively 'org-next-item))
19457 ((org-clocktable-try-shift 'down arg))
19458 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
19459 (org-support-shift-select
19460 (org-call-for-shift-select 'next-line))
19461 (t (org-shiftselect-error))))
19463 (defun org-shiftright (&optional arg)
19464 "Cycle the thing at point or in the current line, depending on context.
19465 Depending on context, this does one of the following:
19467 - switch a timestamp at point one day into the future
19468 - on a headline, switch to the next TODO keyword.
19469 - on an item, switch entire list to the next bullet type
19470 - on a property line, switch to the next allowed value
19471 - on a clocktable definition line, move time block into the future"
19474 ((run-hook-with-args-until-success 'org-shiftright-hook))
19475 ((and org-support-shift-select (org-region-active-p))
19476 (org-call-for-shift-select 'forward-char))
19477 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
19478 ((and (not (eq org-support-shift-select 'always))
19479 (org-at-heading-p))
19480 (let ((org-inhibit-logging
19481 (not org-treat-S-cursor-todo-selection-as-state-change))
19482 (org-inhibit-blocking
19483 (not org-treat-S-cursor-todo-selection-as-state-change)))
19484 (org-call-with-arg 'org-todo 'right)))
19485 ((or (and org-support-shift-select
19486 (not (eq org-support-shift-select 'always))
19487 (org-at-item-bullet-p))
19488 (and (not org-support-shift-select) (org-at-item-p)))
19489 (org-call-with-arg 'org-cycle-list-bullet nil))
19490 ((and (not (eq org-support-shift-select 'always))
19491 (org-at-property-p))
19492 (call-interactively 'org-property-next-allowed-value))
19493 ((org-clocktable-try-shift 'right arg))
19494 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
19495 (org-support-shift-select
19496 (org-call-for-shift-select 'forward-char))
19497 (t (org-shiftselect-error))))
19499 (defun org-shiftleft (&optional arg)
19500 "Cycle the thing at point or in the current line, depending on context.
19501 Depending on context, this does one of the following:
19503 - switch a timestamp at point one day into the past
19504 - on a headline, switch to the previous TODO keyword.
19505 - on an item, switch entire list to the previous bullet type
19506 - on a property line, switch to the previous allowed value
19507 - on a clocktable definition line, move time block into the past"
19510 ((run-hook-with-args-until-success 'org-shiftleft-hook))
19511 ((and org-support-shift-select (org-region-active-p))
19512 (org-call-for-shift-select 'backward-char))
19513 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
19514 ((and (not (eq org-support-shift-select 'always))
19515 (org-at-heading-p))
19516 (let ((org-inhibit-logging
19517 (not org-treat-S-cursor-todo-selection-as-state-change))
19518 (org-inhibit-blocking
19519 (not org-treat-S-cursor-todo-selection-as-state-change)))
19520 (org-call-with-arg 'org-todo 'left)))
19521 ((or (and org-support-shift-select
19522 (not (eq org-support-shift-select 'always))
19523 (org-at-item-bullet-p))
19524 (and (not org-support-shift-select) (org-at-item-p)))
19525 (org-call-with-arg 'org-cycle-list-bullet 'previous))
19526 ((and (not (eq org-support-shift-select 'always))
19527 (org-at-property-p))
19528 (call-interactively 'org-property-previous-allowed-value))
19529 ((org-clocktable-try-shift 'left arg))
19530 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
19531 (org-support-shift-select
19532 (org-call-for-shift-select 'backward-char))
19533 (t (org-shiftselect-error))))
19535 (defun org-shiftcontrolright ()
19536 "Switch to next TODO set."
19539 ((and org-support-shift-select (org-region-active-p))
19540 (org-call-for-shift-select 'forward-word))
19541 ((and (not (eq org-support-shift-select 'always))
19542 (org-at-heading-p))
19543 (org-call-with-arg 'org-todo 'nextset))
19544 (org-support-shift-select
19545 (org-call-for-shift-select 'forward-word))
19546 (t (org-shiftselect-error))))
19548 (defun org-shiftcontrolleft ()
19549 "Switch to previous TODO set."
19552 ((and org-support-shift-select (org-region-active-p))
19553 (org-call-for-shift-select 'backward-word))
19554 ((and (not (eq org-support-shift-select 'always))
19555 (org-at-heading-p))
19556 (org-call-with-arg 'org-todo 'previousset))
19557 (org-support-shift-select
19558 (org-call-for-shift-select 'backward-word))
19559 (t (org-shiftselect-error))))
19561 (defun org-shiftcontrolup (&optional n)
19562 "Change timestamps synchronously up in CLOCK log lines.
19563 Optional argument N tells to change by that many units."
19565 (cond ((and (not org-support-shift-select)
19566 (org-at-clock-log-p)
19567 (org-at-timestamp-p t))
19568 (org-clock-timestamps-up n))
19569 (t (org-shiftselect-error))))
19571 (defun org-shiftcontroldown (&optional n)
19572 "Change timestamps synchronously down in CLOCK log lines.
19573 Optional argument N tells to change by that many units."
19575 (cond ((and (not org-support-shift-select)
19576 (org-at-clock-log-p)
19577 (org-at-timestamp-p t))
19578 (org-clock-timestamps-down n))
19579 (t (org-shiftselect-error))))
19581 (defun org-ctrl-c-ret ()
19582 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
19585 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
19586 (t (call-interactively 'org-insert-heading))))
19588 (defun org-find-visible ()
19590 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
19591 (get-char-property s 'invisible)))
19593 (defun org-find-invisible ()
19595 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
19596 (not (get-char-property s 'invisible))))
19599 (defun org-copy-visible (beg end)
19600 "Copy the visible parts of the region."
19605 (narrow-to-region beg end)
19606 (setq s (goto-char (point-min)))
19607 (while (not (= (point) (point-max)))
19608 (goto-char (org-find-invisible))
19609 (push (buffer-substring s (point)) snippets)
19610 (setq s (goto-char (org-find-visible))))))
19611 (kill-new (apply 'concat (nreverse snippets)))))
19613 (defun org-copy-special ()
19614 "Copy region in table or copy current subtree.
19615 Calls `org-table-copy' or `org-copy-subtree', depending on context.
19616 See the individual commands for more information."
19618 (call-interactively
19619 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
19621 (defun org-cut-special ()
19622 "Cut region in table or cut current subtree.
19623 Calls `org-table-copy' or `org-cut-subtree', depending on context.
19624 See the individual commands for more information."
19626 (call-interactively
19627 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
19629 (defun org-paste-special (arg)
19630 "Paste rectangular region into table, or past subtree relative to level.
19631 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
19632 See the individual commands for more information."
19634 (if (org-at-table-p)
19635 (org-table-paste-rectangle)
19636 (org-paste-subtree arg)))
19638 (defsubst org-in-fixed-width-region-p ()
19639 "Is point in a fixed-width region?"
19641 (eq 'fixed-width (org-element-type (org-element-at-point)))))
19643 (defun org-edit-special (&optional arg)
19644 "Call a special editor for the element at point.
19645 When at a table, call the formula editor with `org-table-edit-formulas'.
19646 When in a source code block, call `org-edit-src-code'.
19647 When in a fixed-width region, call `org-edit-fixed-width-region'.
19648 When at an #+INCLUDE keyword, visit the included file.
19649 On a link, call `ffap' to visit the link at point.
19650 Otherwise, return a user error."
19652 (let ((element (org-element-at-point)))
19653 (assert (not buffer-read-only) nil
19654 "Buffer is read-only: %s" (buffer-name))
19655 (case (org-element-type element)
19657 (if (not arg) (org-edit-src-code)
19658 (let* ((info (org-babel-get-src-block-info))
19659 (lang (nth 0 info))
19660 (params (nth 2 info))
19661 (session (cdr (assq :session params))))
19662 (if (not session) (org-edit-src-code)
19663 ;; At a src-block with a session and function called with
19664 ;; an ARG: switch to the buffer related to the inferior
19666 (funcall (intern (concat "org-babel-prep-session:" lang))
19667 session params)))))
19669 (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
19671 (org-remove-double-quotes
19672 (car (org-split-string (org-element-property :value element)))))
19673 (user-error "No special environment to edit here")))
19675 (if (eq (org-element-property :type element) 'table.el)
19676 (org-edit-src-code)
19677 (call-interactively 'org-table-edit-formulas)))
19678 ;; Only Org tables contain `table-row' type elements.
19679 (table-row (call-interactively 'org-table-edit-formulas))
19680 ((example-block export-block) (org-edit-src-code))
19681 (fixed-width (org-edit-fixed-width-region))
19683 ;; No notable element at point. Though, we may be at a link,
19684 ;; which is an object. Thus, scan deeper.
19685 (if (eq (org-element-type (org-element-context element)) 'link)
19686 (call-interactively 'ffap)
19687 (user-error "No special environment to edit here"))))))
19689 (defvar org-table-coordinate-overlays) ; defined in org-table.el
19690 (defun org-ctrl-c-ctrl-c (&optional arg)
19691 "Set tags in headline, or update according to changed information at point.
19693 This command does many different things, depending on context:
19695 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
19696 this is what we do.
19698 - If the cursor is on a statistics cookie, update it.
19700 - If the cursor is in a headline, prompt for tags and insert them
19701 into the current line, aligned to `org-tags-column'. When called
19702 with prefix arg, realign all tags in the current buffer.
19704 - If the cursor is in one of the special #+KEYWORD lines, this
19705 triggers scanning the buffer for these lines and updating the
19708 - If the cursor is inside a table, realign the table. This command
19709 works even if the automatic table editor has been turned off.
19711 - If the cursor is on a #+TBLFM line, re-apply the formulas to
19714 - If the cursor is at a footnote reference or definition, jump to
19715 the corresponding definition or references, respectively.
19717 - If the cursor is a the beginning of a dynamic block, update it.
19719 - If the current buffer is a capture buffer, close note and file it.
19721 - If the cursor is on a <<<target>>>, update radio targets and
19722 corresponding links in this buffer.
19724 - If the cursor is on a numbered item in a plain list, renumber the
19727 - If the cursor is on a checkbox, toggle it.
19729 - If the cursor is on a code block, evaluate it. The variable
19730 `org-confirm-babel-evaluate' can be used to control prompting
19731 before code block evaluation, by default every code block
19732 evaluation requires confirmation. Code block evaluation can be
19733 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
19736 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
19737 org-occur-highlights
19738 org-latex-fragment-image-overlays)
19739 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
19740 (org-remove-occur-highlights)
19741 (org-remove-latex-fragment-image-overlays)
19742 (message "Temporary highlights/overlays removed from current buffer"))
19743 ((and (local-variable-p 'org-finish-function (current-buffer))
19744 (fboundp org-finish-function))
19745 (funcall org-finish-function))
19746 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
19748 (let* ((context (org-element-context)) (type (org-element-type context)))
19749 ;; Test if point is within a blank line.
19750 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
19751 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
19752 (user-error "C-c C-c can do nothing useful at this location"))
19753 ;; For convenience: at the first line of a paragraph on the
19754 ;; same line as an item, apply function on that item instead.
19755 (when (eq type 'paragraph)
19756 (let ((parent (org-element-property :parent context)))
19757 (when (and (eq (org-element-type parent) 'item)
19758 (= (point-at-bol) (org-element-property :begin parent)))
19759 (setq context parent type 'item))))
19760 ;; Act according to type of element or object at point.
19762 (clock (org-clock-update-time-maybe))
19765 (goto-char (org-element-property :post-affiliated context))
19766 (org-update-dblock)))
19767 (footnote-definition
19768 (goto-char (org-element-property :post-affiliated context))
19769 (call-interactively 'org-footnote-action))
19770 (footnote-reference (call-interactively 'org-footnote-action))
19771 ((headline inlinetask)
19772 (save-excursion (goto-char (org-element-property :begin context))
19773 (call-interactively 'org-set-tags)))
19775 ;; At an item: a double C-u set checkbox to "[-]"
19776 ;; unconditionally, whereas a single one will toggle its
19777 ;; presence. Without an universal argument, if the item
19778 ;; has a checkbox, toggle it. Otherwise repair the list.
19779 (let* ((box (org-element-property :checkbox context))
19780 (struct (org-element-property :structure context))
19781 (old-struct (copy-tree struct))
19782 (parents (org-list-parents-alist struct))
19783 (prevs (org-list-prevs-alist struct))
19784 (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
19785 (org-list-set-checkbox
19786 (org-element-property :begin context) struct
19787 (cond ((equal arg '(16)) "[-]")
19788 ((and (not box) (equal arg '(4))) "[ ]")
19789 ((or (not box) (equal arg '(4))) nil)
19790 ((eq box 'on) "[ ]")
19792 ;; Mimic `org-list-write-struct' but with grabbing
19793 ;; a return value from `org-list-struct-fix-box'.
19794 (org-list-struct-fix-ind struct parents 2)
19795 (org-list-struct-fix-item-end struct)
19796 (org-list-struct-fix-bul struct prevs)
19797 (org-list-struct-fix-ind struct parents)
19799 (org-list-struct-fix-box struct parents prevs orderedp)))
19800 (if (and box (equal struct old-struct))
19801 (if (equal arg '(16))
19802 (message "Checkboxes already reset")
19803 (user-error "Cannot toggle this checkbox: %s"
19805 "all subitems checked"
19806 "unchecked subitems")))
19807 (org-list-struct-apply-struct struct old-struct)
19808 (org-update-checkbox-count-maybe))
19810 (message "Checkboxes were removed due to empty box at line %d"
19811 (org-current-line block-item))))))
19813 (let ((org-inhibit-startup-visibility-stuff t)
19814 (org-startup-align-all-tables nil))
19815 (when (boundp 'org-table-coordinate-overlays)
19816 (mapc 'delete-overlay org-table-coordinate-overlays)
19817 (setq org-table-coordinate-overlays nil))
19818 (org-save-outline-visibility 'use-markers (org-mode-restart)))
19819 (message "Local setup has been refreshed"))
19821 ;; At a plain list, with a double C-u argument, set
19822 ;; checkboxes of each item to "[-]", whereas a single one
19823 ;; will toggle their presence according to the state of the
19824 ;; first item in the list. Without an argument, repair the
19826 (let* ((begin (org-element-property :contents-begin context))
19827 (struct (org-element-property :structure context))
19828 (old-struct (copy-tree struct))
19829 (first-box (save-excursion
19831 (looking-at org-list-full-item-re)
19832 (match-string-no-properties 3)))
19833 (new-box (cond ((equal arg '(16)) "[-]")
19834 ((equal arg '(4)) (unless first-box "[ ]"))
19835 ((equal first-box "[X]") "[ ]")
19839 (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
19840 (org-list-get-all-items
19841 begin struct (org-list-prevs-alist struct))))
19842 ((and first-box (eq (point) begin))
19843 ;; For convenience, when point is at bol on the first
19844 ;; item of the list and no argument is provided, simply
19845 ;; toggle checkbox of that item, if any.
19846 (org-list-set-checkbox begin struct new-box)))
19847 (org-list-write-struct
19848 struct (org-list-parents-alist struct) old-struct)
19849 (org-update-checkbox-count-maybe)
19850 (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
19851 ((property-drawer node-property)
19852 (call-interactively 'org-property-action))
19853 ((radio-target target)
19854 (call-interactively 'org-update-radio-target-regexp))
19856 (call-interactively 'org-update-statistics-cookies))
19857 ((table table-cell table-row)
19858 ;; At a table, recalculate every field and align it. Also
19859 ;; send the table if necessary. If the table has
19860 ;; a `table.el' type, just give up. At a table row or
19861 ;; cell, maybe recalculate line but always align table.
19862 (if (eq (org-element-property :type context) 'table.el)
19863 (message "Use C-c ' to edit table.el tables")
19864 (let ((org-enable-table-editor t))
19865 (if (or (eq type 'table)
19866 ;; Check if point is at a TBLFM line.
19867 (and (eq type 'table-row)
19868 (= (point) (org-element-property :end context))))
19870 (goto-char (org-element-property :contents-begin context))
19871 (org-call-with-arg 'org-table-recalculate (or arg t))
19872 (orgtbl-send-table 'maybe))
19873 (org-table-maybe-eval-formula)
19874 (cond (arg (call-interactively 'org-table-recalculate))
19875 ((org-table-maybe-recalculate-line))
19876 (t (org-table-align)))))))
19877 (timestamp (org-timestamp-change 0 'day))
19879 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
19881 "C-c C-c can do nothing useful at this location")))))))))
19883 (defun org-mode-restart ()
19884 "Restart Org-mode, to scan again for special lines.
19885 Also updates the keyword regular expressions."
19888 (message "Org-mode restarted"))
19890 (defun org-kill-note-or-show-branches ()
19891 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
19893 (if (not org-finish-function)
19896 (call-interactively 'show-branches))
19897 (let ((org-note-abort t))
19898 (funcall org-finish-function))))
19900 (defun org-return (&optional indent)
19901 "Goto next table row or insert a newline.
19902 Calls `org-table-next-row' or `newline', depending on context.
19903 See the individual commands for more information."
19907 ((or (bobp) (org-in-src-block-p))
19908 (if indent (newline-and-indent) (newline)))
19910 (org-table-justify-field-maybe)
19911 (call-interactively 'org-table-next-row))
19912 ;; when `newline-and-indent' is called within a list, make sure
19913 ;; text moved stays inside the item.
19914 ((and (org-in-item-p) indent)
19915 (if (and (org-at-item-p) (>= (point) (match-end 0)))
19917 (save-match-data (newline))
19918 (org-indent-line-to (length (match-string 0))))
19919 (let ((ind (org-get-indentation)))
19921 (if (org-looking-back org-list-end-re)
19923 (org-indent-line-to ind)))))
19924 ((and org-return-follows-link
19925 (org-at-timestamp-p t)
19926 (not (eq org-ts-what 'after)))
19927 (org-follow-timestamp-link))
19928 ((and org-return-follows-link
19929 (let ((tprop (get-text-property (point) 'face)))
19930 (or (eq tprop 'org-link)
19931 (and (listp tprop) (memq 'org-link tprop)))))
19932 (call-interactively 'org-open-at-point))
19933 ((and (org-at-heading-p)
19935 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
19939 (t (if indent (newline-and-indent) (newline))))))
19941 (defun org-return-indent ()
19942 "Goto next table row or insert a newline and indent.
19943 Calls `org-table-next-row' or `newline-and-indent', depending on
19944 context. See the individual commands for more information."
19948 (defun org-ctrl-c-star ()
19949 "Compute table, or change heading status of lines.
19950 Calls `org-table-recalculate' or `org-toggle-heading',
19951 depending on context."
19955 (call-interactively 'org-table-recalculate))
19957 ;; Convert all lines in region to list items
19958 (call-interactively 'org-toggle-heading))))
19960 (defun org-ctrl-c-minus ()
19961 "Insert separator line in table or modify bullet status of line.
19962 Also turns a plain line or a region of lines into list items.
19963 Calls `org-table-insert-hline', `org-toggle-item', or
19964 `org-cycle-list-bullet', depending on context."
19968 (call-interactively 'org-table-insert-hline))
19969 ((org-region-active-p)
19970 (call-interactively 'org-toggle-item))
19972 (call-interactively 'org-cycle-list-bullet))
19974 (call-interactively 'org-toggle-item))))
19976 (defun org-toggle-item (arg)
19977 "Convert headings or normal lines to items, items to normal lines.
19978 If there is no active region, only the current line is considered.
19980 If the first non blank line in the region is a headline, convert
19981 all headlines to items, shifting text accordingly.
19983 If it is an item, convert all items to normal lines.
19985 If it is normal text, change region into an item. With a prefix
19986 argument ARG, change each line in region into an item."
19990 ;; Shift text in current section to IND, from point to END.
19991 ;; The function leaves point to END line.
19993 (let ((min-i 1000) (end (copy-marker end)))
19994 ;; First determine the minimum indentation (MIN-I) of
19998 (while (< (point) end)
19999 (let ((i (org-get-indentation)))
20001 ;; Skip blank lines and inline tasks.
20002 ((looking-at "^[ \t]*$"))
20003 ((looking-at org-outline-regexp-bol))
20004 ;; We can't find less than 0 indentation.
20005 ((zerop i) (throw 'exit (setq min-i 0)))
20006 ((< i min-i) (setq min-i i))))
20008 ;; Then indent each line so that a line indented to
20009 ;; MIN-I becomes indented to IND. Ignore blank lines
20010 ;; and inline tasks in the process.
20011 (let ((delta (- ind min-i)))
20012 (while (< (point) end)
20013 (unless (or (looking-at "^[ \t]*$")
20014 (looking-at org-outline-regexp-bol))
20015 (org-indent-line-to (+ (org-get-indentation) delta)))
20016 (forward-line)))))))
20019 ;; Return beginning of first non-blank line, starting from
20024 (skip-chars-forward " \r\t\n")
20027 ;; Determine boundaries of changes.
20028 (if (org-region-active-p)
20029 (setq beg (funcall skip-blanks (region-beginning))
20030 end (copy-marker (region-end)))
20031 (setq beg (funcall skip-blanks (point-at-bol))
20032 end (copy-marker (point-at-eol))))
20033 ;; Depending on the starting line, choose an action on the text
20034 ;; between BEG and END.
20035 (org-with-limited-levels
20039 ;; Case 1. Start at an item: de-itemize. Note that it only
20040 ;; happens when a region is active: `org-ctrl-c-minus'
20041 ;; would call `org-cycle-list-bullet' otherwise.
20043 (while (< (point) end)
20044 (when (org-at-item-p)
20045 (skip-chars-forward " \t")
20046 (delete-region (point) (match-end 0)))
20048 ;; Case 2. Start at an heading: convert to items.
20049 ((org-at-heading-p)
20050 (let* ((bul (org-list-bullet-string "-"))
20051 (bul-len (length bul))
20052 ;; Indentation of the first heading. It should be
20053 ;; relative to the indentation of its parent, if any.
20054 (start-ind (save-excursion
20056 ((not org-adapt-indentation) 0)
20057 ((not (outline-previous-heading)) 0)
20058 (t (length (match-string 0))))))
20059 ;; Level of first heading. Further headings will be
20060 ;; compared to it to determine hierarchy in the list.
20061 (ref-level (org-reduced-level (org-outline-level))))
20062 (while (< (point) end)
20063 (let* ((level (org-reduced-level (org-outline-level)))
20064 (delta (max 0 (- level ref-level))))
20065 ;; If current headline is less indented than the first
20066 ;; one, set it as reference, in order to preserve
20068 (when (< level ref-level) (setq ref-level level))
20069 (replace-match bul t t)
20070 (org-indent-line-to (+ start-ind (* delta bul-len)))
20071 ;; Ensure all text down to END (or SECTION-END) belongs
20072 ;; to the newly created item.
20073 (let ((section-end (save-excursion
20074 (or (outline-next-heading) (point)))))
20076 (funcall shift-text
20077 (+ start-ind (* (1+ delta) bul-len))
20078 (min end section-end)))))))
20079 ;; Case 3. Normal line with ARG: turn each non-item line into
20082 (while (< (point) end)
20083 (unless (or (org-at-heading-p) (org-at-item-p))
20084 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
20086 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
20088 ;; Case 4. Normal line without ARG: make the first line of
20089 ;; region an item, and shift indentation of others
20090 ;; lines to set them as item's body.
20091 (t (let* ((bul (org-list-bullet-string "-"))
20092 (bul-len (length bul))
20093 (ref-ind (org-get-indentation)))
20094 (skip-chars-forward " \t")
20097 (while (< (point) end)
20098 ;; Ensure that lines less indented than first one
20099 ;; still get included in item body.
20100 (funcall shift-text
20101 (+ ref-ind bul-len)
20102 (min end (save-excursion (or (outline-next-heading)
20104 (forward-line)))))))))
20106 (defun org-toggle-heading (&optional nstars)
20107 "Convert headings to normal text, or items or text to headings.
20108 If there is no active region, only the current line is considered.
20110 With a \\[universal-argument] prefix, convert the whole list at
20111 point into heading.
20115 - If the first non blank line is a headline, remove the stars
20116 from all headlines in the region.
20118 - If it is a normal line turn each and every normal line (i.e. not an
20119 heading or an item) in the region into a heading.
20121 - If it is a plain list item, turn all plain list items into headings.
20123 When converting a line into a heading, the number of stars is chosen
20124 such that the lines become children of the current entry. However,
20125 when a prefix argument is given, its value determines the number of
20130 ;; Return beginning of first non-blank line, starting from
20135 (while (org-at-comment-p) (forward-line))
20136 (skip-chars-forward " \r\t\n")
20139 ;; Determine boundaries of changes. If a universal prefix has
20140 ;; been given, put the list in a region. If region ends at a bol,
20141 ;; do not consider the last line to be in the region.
20143 (when (and current-prefix-arg (org-at-item-p))
20144 (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
20145 (org-mark-element))
20147 (if (org-region-active-p)
20148 (setq beg (funcall skip-blanks (region-beginning))
20149 end (copy-marker (save-excursion
20150 (goto-char (region-end))
20151 (if (bolp) (point) (point-at-eol)))))
20152 (setq beg (funcall skip-blanks (point-at-bol))
20153 end (copy-marker (point-at-eol))))
20154 ;; Ensure inline tasks don't count as headings.
20155 (org-with-limited-levels
20159 ;; Case 1. Started at an heading: de-star headings.
20160 ((org-at-heading-p)
20161 (while (< (point) end)
20162 (when (org-at-heading-p t)
20163 (looking-at org-outline-regexp) (replace-match "")
20166 ;; Case 2. Started at an item: change items into headlines.
20167 ;; One star will be added by `org-list-to-subtree'.
20169 (let* ((stars (make-string
20171 ;; subtract the star that will be added again by
20172 ;; `org-list-to-subtree'
20173 (1- (prefix-numeric-value current-prefix-arg))
20174 (or (org-current-level) 0))
20177 (cond (nstars "") ; stars from prefix only
20178 ((equal stars "") "") ; before first heading
20179 (org-odd-levels-only "*") ; inside heading, odd
20180 (t "")))) ; inside heading, oddeven
20181 (while (< (point) end)
20182 (when (org-at-item-p)
20183 ;; Pay attention to cases when region ends before list.
20184 (let* ((struct (org-list-struct))
20185 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
20187 (narrow-to-region (point) list-end)
20189 (org-list-to-subtree
20190 (org-list-parse-list t)
20191 '(:istart (concat stars add-stars (funcall get-stars depth))
20192 :icount (concat stars add-stars (funcall get-stars depth)))))))
20195 ;; Case 3. Started at normal text: make every line an heading,
20196 ;; skipping headlines and items.
20197 (t (let* ((stars (make-string
20199 (prefix-numeric-value current-prefix-arg)
20200 (or (org-current-level) 0))
20203 (cond (nstars "") ; stars from prefix only
20204 ((equal stars "") "*") ; before first heading
20205 (org-odd-levels-only "**") ; inside heading, odd
20206 (t "*"))) ; inside heading, oddeven
20207 (rpl (concat stars add-stars " ")))
20208 (while (< (point) end)
20209 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
20210 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
20211 (replace-match (concat rpl (match-string 2))) (setq toggled t))
20212 (forward-line)))))))
20213 (unless toggled (message "Cannot toggle heading from here"))))
20215 (defun org-meta-return (&optional arg)
20216 "Insert a new heading or wrap a region in a table.
20217 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
20218 See the individual commands for more information."
20221 ((run-hook-with-args-until-success 'org-metareturn-hook))
20222 ((or (org-at-drawer-p) (org-at-property-p))
20223 (newline-and-indent))
20225 (call-interactively 'org-table-wrap-region))
20226 (t (call-interactively 'org-insert-heading))))
20230 (defsubst org-in-subtree-not-table-p ()
20231 "Are we in a subtree and not in a table?"
20232 (and (not (org-before-first-heading-p))
20233 (not (org-at-table-p))))
20235 ;; Define the Org-mode menus
20236 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
20238 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
20239 ["Next Field" org-cycle (org-at-table-p)]
20240 ["Previous Field" org-shifttab (org-at-table-p)]
20241 ["Next Row" org-return (org-at-table-p)]
20243 ["Blank Field" org-table-blank-field (org-at-table-p)]
20244 ["Edit Field" org-table-edit-field (org-at-table-p)]
20245 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
20248 ["Move Column Left" org-metaleft (org-at-table-p)]
20249 ["Move Column Right" org-metaright (org-at-table-p)]
20250 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
20251 ["Insert Column" org-shiftmetaright (org-at-table-p)])
20253 ["Move Row Up" org-metaup (org-at-table-p)]
20254 ["Move Row Down" org-metadown (org-at-table-p)]
20255 ["Delete Row" org-shiftmetaup (org-at-table-p)]
20256 ["Insert Row" org-shiftmetadown (org-at-table-p)]
20257 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
20259 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
20261 ["Copy Rectangle" org-copy-special (org-at-table-p)]
20262 ["Cut Rectangle" org-cut-special (org-at-table-p)]
20263 ["Paste Rectangle" org-paste-special (org-at-table-p)]
20264 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
20267 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
20268 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
20269 ["Edit Formulas" org-edit-special (org-at-table-p)]
20271 ["Recalculate line" org-table-recalculate (org-at-table-p)]
20272 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
20273 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
20275 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
20277 ["Sum Column/Rectangle" org-table-sum
20278 (or (org-at-table-p) (org-region-active-p))]
20279 ["Which Column?" org-table-current-column (org-at-table-p)])
20281 org-table-toggle-formula-debugger
20282 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
20283 ["Show Col/Row Numbers"
20284 org-table-toggle-coordinate-overlays
20286 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
20288 ["Create" org-table-create (and (not (org-at-table-p))
20289 org-enable-table-editor)]
20290 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
20291 ["Import from File" org-table-import (not (org-at-table-p))]
20292 ["Export to File" org-table-export (org-at-table-p)]
20294 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
20296 (easy-menu-define org-org-menu org-mode-map "Org menu"
20299 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
20300 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
20301 ["Sparse Tree..." org-sparse-tree t]
20302 ["Reveal Context" org-reveal t]
20303 ["Show All" show-all t]
20305 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
20307 ["New Heading" org-insert-heading t]
20308 ("Navigate Headings"
20309 ["Up" outline-up-heading t]
20310 ["Next" outline-next-visible-heading t]
20311 ["Previous" outline-previous-visible-heading t]
20312 ["Next Same Level" outline-forward-same-level t]
20313 ["Previous Same Level" outline-backward-same-level t]
20315 ["Jump" org-goto t])
20317 ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
20319 ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
20320 ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
20322 ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
20323 ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
20324 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
20326 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
20328 ["Copy visible text" org-copy-visible t]
20330 ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
20331 ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
20332 ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
20333 ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
20335 ["Sort Region/Children" org-sort t]
20337 ["Convert to odd levels" org-convert-to-odd-levels t]
20338 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
20340 ["Emphasis..." org-emphasize t]
20341 ["Edit Source Example" org-edit-special t]
20343 ["Footnote new/jump" org-footnote-action t]
20344 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
20346 ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
20348 ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
20349 ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
20350 ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
20354 ["Store Link (Global)" org-store-link t]
20355 ["Find existing link to here" org-occur-link-in-agenda-files t]
20356 ["Insert Link" org-insert-link t]
20357 ["Follow Link" org-open-at-point t]
20359 ["Next link" org-next-link t]
20360 ["Previous link" org-previous-link t]
20362 ["Descriptive Links"
20363 org-toggle-link-display
20365 :selected org-descriptive-links
20368 org-toggle-link-display
20370 :selected (not org-descriptive-links)])
20373 ["TODO/DONE/-" org-todo t]
20375 ["Next keyword" org-shiftright (org-at-heading-p)]
20376 ["Previous keyword" org-shiftleft (org-at-heading-p)]
20377 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
20378 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
20379 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
20380 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
20381 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
20383 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
20384 :selected org-enforce-todo-dependencies :style toggle :active t]
20385 "Settings for tree at point"
20386 ["Do Children sequentially" org-toggle-ordered-property :style radio
20387 :selected (org-entry-get nil "ORDERED")
20388 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
20389 ["Do Children parallel" org-toggle-ordered-property :style radio
20390 :selected (not (org-entry-get nil "ORDERED"))
20391 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
20393 ["Set Priority" org-priority t]
20394 ["Priority Up" org-shiftup t]
20395 ["Priority Down" org-shiftdown t]
20397 ["Get news from all feeds" org-feed-update-all t]
20398 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
20399 ["Customize feeds" (customize-variable 'org-feed-alist) t])
20400 ("TAGS and Properties"
20401 ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
20402 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
20404 ["Set property" org-set-property (not (org-before-first-heading-p))]
20405 ["Column view of properties" org-columns t]
20406 ["Insert Column View DBlock" org-insert-columns-dblock t])
20407 ("Dates and Scheduling"
20408 ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
20409 ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
20411 ["1 Day Later" org-shiftright (org-at-timestamp-p)]
20412 ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
20413 ["1 ... Later" org-shiftup (org-at-timestamp-p)]
20414 ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
20415 ["Compute Time Range" org-evaluate-time-range t]
20416 ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
20417 ["Deadline" org-deadline (not (org-before-first-heading-p))]
20419 ["Custom time format" org-toggle-time-stamp-overlays
20420 :style radio :selected org-display-custom-times]
20422 ["Goto Calendar" org-goto-calendar t]
20423 ["Date from Calendar" org-date-from-calendar t]
20425 ["Start/Restart Timer" org-timer-start t]
20426 ["Pause/Continue Timer" org-timer-pause-or-continue t]
20427 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
20428 ["Insert Timer String" org-timer t]
20429 ["Insert Timer Item" org-timer-item t])
20431 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
20432 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
20433 ["Clock out" org-clock-out t]
20434 ["Clock cancel" org-clock-cancel t]
20436 ["Mark as default task" org-clock-mark-default-task t]
20437 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
20438 ["Goto running clock" org-clock-goto t]
20440 ["Display times" org-clock-display t]
20441 ["Create clock table" org-clock-report t]
20443 ["Record DONE time"
20444 (progn (setq org-log-done (not org-log-done))
20445 (message "Switching to %s will %s record a timestamp"
20446 (car org-done-keywords)
20447 (if org-log-done "automatically" "not")))
20448 :style toggle :selected org-log-done])
20450 ["Agenda Command..." org-agenda t]
20451 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
20452 ("File List for Agenda")
20453 ("Special views current file"
20454 ["TODO Tree" org-show-todo-tree t]
20455 ["Check Deadlines" org-check-deadlines t]
20456 ["Timeline" org-timeline t]
20457 ["Tags/Property tree" org-match-sparse-tree t])
20459 ["Export/Publish..." org-export-dispatch t]
20461 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
20462 :selected org-cdlatex-mode]
20463 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
20464 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
20465 ["Modify math symbol" org-cdlatex-math-modify
20466 (org-inside-LaTeX-fragment-p)]
20467 ["Insert citation" org-reftex-citation t]
20469 ["Template for BEAMER" (org-beamer-insert-options-template) t])
20472 ["Push Files and Views" org-mobile-push t]
20473 ["Get Captured and Flagged" org-mobile-pull t]
20474 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
20476 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
20479 ["Show Version" org-version t]
20480 ["Info Documentation" org-info t])
20482 ["Browse Org Group" org-customize t]
20484 ["Expand This Menu" org-create-customize-menu
20485 (fboundp 'customize-menu-create)])
20486 ["Send bug report" org-submit-bug-report t]
20489 ["Refresh setup current buffer" org-mode-restart t]
20490 ["Reload Org (after update)" org-reload t]
20491 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
20494 (defun org-info (&optional node)
20495 "Read documentation for Org-mode in the info system.
20496 With optional NODE, go directly to that node."
20498 (info (format "(org)%s" (or node ""))))
20501 (defun org-submit-bug-report ()
20502 "Submit a bug report on Org-mode via mail.
20504 Don't hesitate to report any problems or inaccurate documentation.
20506 If you don't have setup sending mail from (X)Emacs, please copy the
20507 output buffer into your mail program, as it gives us important
20508 information about your Org-mode version and configuration."
20510 (require 'reporter)
20511 (org-load-modules-maybe)
20512 (org-require-autoloaded-modules)
20513 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
20514 (reporter-submit-bug-report
20515 "emacs-orgmode@gnu.org"
20516 (org-version nil 'full)
20518 (save-window-excursion
20519 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
20520 (delete-other-windows)
20522 (insert "You are about to submit a bug report to the Org-mode mailing list.
20524 We would like to add your full Org-mode and Outline configuration to the
20525 bug report. This greatly simplifies the work of the maintainer and
20526 other experts on the mailing list.
20528 HOWEVER, some variables you have customized may contain private
20529 information. The names of customers, colleagues, or friends, might
20530 appear in the form of file names, tags, todo states, or search strings.
20531 If you answer yes to the prompt, you might want to check and remove
20532 such private information before sending the email.")
20533 (add-text-properties (point-min) (point-max) '(face org-warning))
20534 (when (yes-or-no-p "Include your Org-mode configuration ")
20538 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
20539 (or (and (symbol-value v)
20540 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
20542 (get v 'custom-type) (get v 'standard-value)
20543 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
20545 (kill-buffer (get-buffer "*Warn about privacy*"))
20548 "Remember to cover the basics, that is, what you expected to happen and
20549 what in fact did happen. You don't know how to make a good report? See
20551 http://orgmode.org/manual/Feedback.html#Feedback
20553 Your bug report will be posted to the Org-mode mailing list.
20554 ------------------------------------------------------------------------")
20556 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
20557 (replace-match "\\1Bug: \\3 [\\2]")))))
20560 (defun org-install-agenda-files-menu ()
20561 (let ((bl (buffer-list)))
20564 (set-buffer (pop bl))
20565 (if (derived-mode-p 'org-mode) (setq bl nil)))
20566 (when (derived-mode-p 'org-mode)
20568 '("Org") "File List for Agenda"
20571 ["Edit File List" (org-edit-agenda-file-list) t]
20572 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
20573 ["Remove Current File from List" org-remove-file t]
20574 ["Cycle through agenda files" org-cycle-agenda-files t]
20575 ["Occur in all agenda files" org-occur-in-agenda-files t]
20577 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
20581 (defun org-require-autoloaded-modules ()
20584 '(org-agenda org-archive org-attach org-clock org-colview org-id
20585 org-remember org-table org-timer)))
20588 (defun org-reload (&optional uncompiled)
20589 "Reload all org lisp files.
20590 With prefix arg UNCOMPILED, load the uncompiled versions."
20592 (require 'loadhist)
20593 (let* ((org-dir (org-find-library-dir "org"))
20594 (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
20595 (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
20596 (remove-re (mapconcat 'identity
20597 (mapcar (lambda (f) (concat "^" f "$"))
20598 (list (if (featurep 'xemacs)
20600 "org-colview-xemacs")
20601 "org" "org-loaddefs" "org-version"))
20603 (feats (delete-dups
20604 (mapcar 'file-name-sans-extension
20605 (mapcar 'file-name-nondirectory
20607 (mapcar 'feature-file
20614 (if (and (string-match feature-re f)
20615 (not (string-match remove-re f)))
20619 (list "org-version" "org")))
20620 (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
20621 (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
20622 load-uncore load-misses)
20625 (mapcar (lambda (f)
20626 (or (org-load-noerror-mustsuffix (concat org-dir f))
20627 (and (string= org-dir contrib-dir)
20628 (org-load-noerror-mustsuffix (concat contrib-dir f)))
20629 (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
20630 (add-to-list 'load-uncore f 'append)
20635 (message "The following feature%s found in load-path, please check if that's correct:\n%s"
20636 (if (> (length load-uncore) 1) "s were" " was") load-uncore))
20638 (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
20639 (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
20640 (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
20643 (defun org-customize ()
20644 "Call the customize function with org as argument."
20646 (org-load-modules-maybe)
20647 (org-require-autoloaded-modules)
20648 (customize-browse 'org))
20650 (defun org-create-customize-menu ()
20651 "Create a full customization menu for Org-mode, insert it into the menu."
20653 (org-load-modules-maybe)
20654 (org-require-autoloaded-modules)
20655 (if (fboundp 'customize-menu-create)
20658 '("Org") "Customize"
20659 `(["Browse Org group" org-customize t]
20661 ,(customize-menu-create 'org)
20662 ["Set" Custom-set t]
20663 ["Save" Custom-save t]
20664 ["Reset to Current" Custom-reset-current t]
20665 ["Reset to Saved" Custom-reset-saved t]
20666 ["Reset to Standard Settings" Custom-reset-standard t]))
20667 (message "\"Org\"-menu now contains full customization menu"))
20668 (error "Cannot expand menu (outdated version of cus-edit.el)")))
20670 ;;;; Miscellaneous stuff
20672 ;;; Generally useful functions
20674 (defun org-get-at-bol (property)
20675 "Get text property PROPERTY at beginning of line."
20676 (get-text-property (point-at-bol) property))
20678 (defun org-find-text-property-in-string (prop s)
20679 "Return the first non-nil value of property PROP in string S."
20680 (or (get-text-property 0 prop s)
20681 (get-text-property (or (next-single-property-change 0 prop s) 0)
20684 (defun org-display-warning (message) ;; Copied from Emacs-Muse
20685 "Display the given MESSAGE as a warning."
20686 (if (fboundp 'display-warning)
20687 (display-warning 'org message
20688 (if (featurep 'xemacs) 'warning :warning))
20689 (let ((buf (get-buffer-create "*Org warnings*")))
20690 (with-current-buffer buf
20691 (goto-char (point-max))
20692 (insert "Warning (Org): " message)
20695 (display-buffer buf)
20698 (defun org-eval (form)
20699 "Eval FORM and return result."
20700 (condition-case error
20702 (error (format "%%![Error: %s]" error))))
20704 (defun org-in-clocktable-p ()
20705 "Check if the cursor is in a clocktable."
20706 (let ((pos (point)) start)
20709 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
20710 (setq start (match-beginning 0))
20711 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
20712 (>= (match-end 0) pos)
20715 (defun org-in-commented-line ()
20716 "Is point in a line starting with `#'?"
20717 (equal (char-after (point-at-bol)) ?#))
20719 (defun org-in-indented-comment-line ()
20720 "Is point in a line starting with `#' after some white space?"
20723 (goto-char (point-at-bol))
20724 (looking-at "[ \t]*#"))))
20726 (defun org-in-verbatim-emphasis ()
20728 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
20730 (defun org-goto-marker-or-bmk (marker &optional bookmark)
20731 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
20732 (if (and marker (marker-buffer marker)
20733 (buffer-live-p (marker-buffer marker)))
20735 (org-pop-to-buffer-same-window (marker-buffer marker))
20736 (if (or (> marker (point-max)) (< marker (point-min)))
20739 (org-show-context 'org-goto))
20741 (bookmark-jump bookmark)
20742 (error "Cannot find location"))))
20744 (defun org-quote-csv-field (s)
20745 "Quote field for inclusion in CSV material."
20746 (if (string-match "[\",]" s)
20747 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
20750 (defun org-force-self-insert (N)
20751 "Needed to enforce self-insert under remapping."
20753 (self-insert-command N))
20755 (defun org-string-width (s)
20756 "Compute width of string, ignoring invisible characters.
20757 This ignores character with invisibility property `org-link', and also
20758 characters with property `org-cwidth', because these will become invisible
20759 upon the next fontification round."
20761 (when (or (eq t buffer-invisibility-spec)
20762 (assq 'org-link buffer-invisibility-spec))
20763 (while (setq b (text-property-any 0 (length s)
20764 'invisible 'org-link s))
20765 (setq s (concat (substring s 0 b)
20766 (substring s (or (next-single-property-change
20767 b 'invisible s) (length s)))))))
20768 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
20769 (setq s (concat (substring s 0 b)
20770 (substring s (or (next-single-property-change
20771 b 'org-cwidth s) (length s))))))
20772 (setq l (string-width s) b -1)
20773 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
20774 (setq l (- l (get-text-property b 'org-dwidth-n s))))
20777 (defun org-shorten-string (s maxlength)
20778 "Shorten string S so tht it is no longer than MAXLENGTH characters.
20779 If the string is shorter or has length MAXLENGTH, just return the
20780 original string. If it is longer, the functions finds a space in the
20781 string, breaks this string off at that locations and adds three dots
20782 as ellipsis. Including the ellipsis, the string will not be longer
20783 than MAXLENGTH. If finding a good breaking point in the string does
20784 not work, the string is just chopped off in the middle of a word
20786 (if (<= (length s) maxlength)
20788 (let* ((n (max (- maxlength 4) 1))
20789 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
20790 (if (string-match re s)
20791 (concat (match-string 1 s) "...")
20792 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
20794 (defun org-get-indentation (&optional line)
20795 "Get the indentation of the current line, interpreting tabs.
20796 When LINE is given, assume it represents a line and compute its indentation."
20798 (if (string-match "^ *" (org-remove-tabs line))
20801 (beginning-of-line 1)
20802 (skip-chars-forward " \t")
20803 (current-column))))
20805 (defun org-get-string-indentation (s)
20806 "What indentation has S due to SPACE and TAB at the beginning of the string?"
20807 (let ((n -1) (i 0) (w tab-width) c)
20809 (while (< (setq n (1+ n)) (length s))
20810 (setq c (aref s n))
20811 (cond ((= c ?\ ) (setq i (1+ i)))
20812 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
20813 (t (throw 'exit t)))))
20816 (defun org-remove-tabs (s &optional width)
20817 "Replace tabulators in S with spaces.
20818 Assumes that s is a single line, starting in column 0."
20819 (setq width (or width tab-width))
20820 (while (string-match "\t" s)
20821 (setq s (replace-match
20823 (- (* width (/ (+ (match-beginning 0) width) width))
20824 (match-beginning 0)) ?\ )
20828 (defun org-fix-indentation (line ind)
20829 "Fix indentation in LINE.
20830 IND is a cons cell with target and minimum indentation.
20831 If the current indentation in LINE is smaller than the minimum,
20832 leave it alone. If it is larger than ind, set it to the target."
20833 (let* ((l (org-remove-tabs line))
20834 (i (org-get-indentation l))
20835 (i1 (car ind)) (i2 (cdr ind)))
20836 (if (>= i i2) (setq l (substring line i2)))
20838 (concat (make-string i1 ?\ ) l)
20841 (defun org-remove-indentation (code &optional n)
20842 "Remove the maximum common indentation from the lines in CODE.
20843 N may optionally be the number of spaces to remove."
20846 (org-do-remove-indentation n)
20849 (defun org-do-remove-indentation (&optional n)
20850 "Remove the maximum common indentation from the buffer."
20851 (untabify (point-min) (point-max))
20852 (let ((min 10000) re)
20855 (goto-char (point-min))
20856 (while (re-search-forward "^ *[^ \n]" nil t)
20857 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
20858 (unless (or (= min 0) (= min 10000))
20859 (setq re (format "^ \\{%d\\}" min))
20860 (goto-char (point-min))
20861 (while (re-search-forward re nil t)
20866 (defun org-fill-template (template alist)
20867 "Find each %key of ALIST in TEMPLATE and replace it."
20868 (let ((case-fold-search nil)
20870 (setq alist (sort (copy-sequence alist)
20871 (lambda (a b) (< (length (car a)) (length (car b))))))
20872 (while (setq entry (pop alist))
20874 (replace-regexp-in-string
20875 (concat "%" (regexp-quote (car entry)))
20876 (or (cdr entry) "") template t t)))
20879 (defun org-base-buffer (buffer)
20880 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
20883 (or (buffer-base-buffer buffer)
20886 (defun org-trim (s)
20887 "Remove whitespace at beginning and end of string."
20888 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
20889 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
20892 (defun org-wrap (string &optional width lines)
20893 "Wrap string to either a number of lines, or a width in characters.
20894 If WIDTH is non-nil, the string is wrapped to that width, however many lines
20895 that costs. If there is a word longer than WIDTH, the text is actually
20896 wrapped to the length of that word.
20897 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
20898 many lines, whatever width that takes.
20899 The return value is a list of lines, without newlines at the end."
20900 (let* ((words (org-split-string string "[ \t\n]+"))
20901 (maxword (apply 'max (mapcar 'org-string-width words)))
20904 (org-do-wrap words (max maxword width)))
20907 (setq ll (org-do-wrap words maxword))
20908 (if (<= (length ll) lines)
20911 (while (> (length ll) lines)
20913 (setq ll (org-do-wrap words w)))
20915 (t (error "Cannot wrap this")))))
20917 (defun org-do-wrap (words width)
20918 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
20921 (setq line (pop words))
20922 (while (and words (< (+ (length line) (length (car words))) width))
20923 (setq line (concat line " " (pop words))))
20924 (setq lines (push line lines)))
20927 (defun org-split-string (string &optional separators)
20928 "Splits STRING into substrings at SEPARATORS.
20929 No empty strings are returned if there are matches at the beginning
20930 and end of string."
20931 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
20935 (while (and (string-match rexp string
20937 (= start (match-beginning 0))
20938 (< start (length string)))
20940 (< (match-beginning 0) (length string)))
20942 (or (eq (match-beginning 0) 0)
20943 (and (eq (match-beginning 0) (match-end 0))
20944 (eq (match-beginning 0) start))
20946 (cons (substring string start (match-beginning 0))
20948 (setq start (match-end 0)))
20949 (or (eq start (length string))
20951 (cons (substring string start)
20955 (defun org-quote-vert (s)
20956 "Replace \"|\" with \"\\vert\"."
20957 (while (string-match "|" s)
20958 (setq s (replace-match "\\vert" t t s)))
20961 (defun org-uuidgen-p (s)
20962 "Is S an ID created by UUIDGEN?"
20963 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
20965 (defun org-in-src-block-p (&optional inside)
20966 "Whether point is in a code source block.
20967 When INSIDE is non-nil, don't consider we are within a src block
20968 when point is at #+BEGIN_SRC or #+END_SRC."
20969 (let ((case-fold-search t) ov)
20970 (or (and (setq ov (overlays-at (point)))
20971 (memq 'org-block-background
20972 (overlay-properties (car ov))))
20976 (beginning-of-line)
20977 (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
20979 (defun org-context ()
20980 "Return a list of contexts of the current cursor position.
20981 If several contexts apply, all are returned.
20982 Each context entry is a list with a symbol naming the context, and
20983 two positions indicating start and end of the context. Possible
20986 :headline anywhere in a headline
20987 :headline-stars on the leading stars in a headline
20988 :todo-keyword on a TODO keyword (including DONE) in a headline
20989 :tags on the TAGS in a headline
20990 :priority on the priority cookie in a headline
20991 :item on the first line of a plain list item
20992 :item-bullet on the bullet/number of a plain list item
20993 :checkbox on the checkbox in a plain list item
20994 :table in an org-mode table
20995 :table-special on a special filed in a table
20996 :table-table in a table.el table
20997 :clocktable in a clocktable
20998 :src-block in a source block
20999 :link on a hyperlink
21000 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
21001 :target on a <<target>>
21002 :radio-target on a <<<radio-target>>>
21003 :latex-fragment on a LaTeX fragment
21004 :latex-preview on a LaTeX fragment with overlaid preview image
21006 This function expects the position to be visible because it uses font-lock
21007 faces as a help to recognize the following contexts: :table-special, :link,
21009 (let* ((f (get-text-property (point) 'face))
21010 (faces (if (listp f) f (list f)))
21011 (case-fold-search t)
21012 (p (point)) clist o)
21013 ;; First the large context
21015 ((org-at-heading-p t)
21016 (push (list :headline (point-at-bol) (point-at-eol)) clist)
21018 (beginning-of-line 1)
21019 (looking-at org-todo-line-tags-regexp))
21020 (push (org-point-in-group p 1 :headline-stars) clist)
21021 (push (org-point-in-group p 2 :todo-keyword) clist)
21022 (push (org-point-in-group p 4 :tags) clist))
21024 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
21025 (if (looking-at "\\[#[A-Z0-9]\\]")
21026 (push (org-point-in-group p 0 :priority) clist)))
21029 (push (org-point-in-group p 2 :item-bullet) clist)
21030 (push (list :item (point-at-bol)
21031 (save-excursion (org-end-of-item) (point)))
21033 (and (org-at-item-checkbox-p)
21034 (push (org-point-in-group p 0 :checkbox) clist)))
21037 (push (list :table (org-table-begin) (org-table-end)) clist)
21038 (if (memq 'org-formula faces)
21039 (push (list :table-special
21040 (previous-single-property-change p 'face)
21041 (next-single-property-change p 'face)) clist)))
21042 ((org-at-table-p 'any)
21043 (push (list :table-table) clist)))
21046 (let ((case-fold-search t))
21047 ;; New the "medium" contexts: clocktables, source blocks
21048 (cond ((org-in-clocktable-p)
21049 (push (list :clocktable
21050 (and (or (looking-at "#\\+BEGIN: clocktable")
21051 (search-backward "#+BEGIN: clocktable" nil t))
21052 (match-beginning 0))
21053 (and (re-search-forward "#\\+END:?" nil t)
21054 (match-end 0))) clist))
21055 ((org-in-src-block-p)
21056 (push (list :src-block
21057 (and (or (looking-at "#\\+BEGIN_SRC")
21058 (search-backward "#+BEGIN_SRC" nil t))
21059 (match-beginning 0))
21060 (and (search-forward "#+END_SRC" nil t)
21061 (match-beginning 0))) clist))))
21064 ;; Now the small context
21066 ((org-at-timestamp-p)
21067 (push (org-point-in-group p 0 :timestamp) clist))
21068 ((memq 'org-link faces)
21070 (previous-single-property-change p 'face)
21071 (next-single-property-change p 'face)) clist))
21072 ((memq 'org-special-keyword faces)
21073 (push (list :keyword
21074 (previous-single-property-change p 'face)
21075 (next-single-property-change p 'face)) clist))
21077 (push (org-point-in-group p 0 :target) clist)
21078 (goto-char (1- (match-beginning 0)))
21079 (if (looking-at org-radio-target-regexp)
21080 (push (org-point-in-group p 0 :radio-target) clist))
21082 ((setq o (car (delq nil
21085 (if (memq x org-latex-fragment-image-overlays) x))
21086 (overlays-at (point))))))
21087 (push (list :latex-fragment
21088 (overlay-start o) (overlay-end o)) clist)
21089 (push (list :latex-preview
21090 (overlay-start o) (overlay-end o)) clist))
21091 ((org-inside-LaTeX-fragment-p)
21092 ;; FIXME: positions wrong.
21093 (push (list :latex-fragment (point) (point)) clist)))
21095 (setq clist (nreverse (delq nil clist)))
21098 ;; FIXME: Compare with at-regexp-p Do we need both?
21099 (defun org-in-regexp (re &optional nlines visually)
21100 "Check if point is inside a match of regexp.
21101 Normally only the current line is checked, but you can include NLINES extra
21102 lines both before and after point into the search.
21103 If VISUALLY is set, require that the cursor is not after the match but
21104 really on, so that the block visually is on the match."
21106 (let ((pos (point))
21107 (eol (point-at-eol (+ 1 (or nlines 0))))
21108 (inc (if visually 1 0)))
21110 (beginning-of-line (- 1 (or nlines 0)))
21111 (while (re-search-forward re eol t)
21112 (if (and (<= (match-beginning 0) pos)
21113 (>= (+ inc (match-end 0)) pos))
21114 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
21116 (defun org-at-regexp-p (regexp)
21117 "Is point inside a match of REGEXP in the current line?"
21120 (let ((pos (point)) (end (point-at-eol)))
21121 (beginning-of-line 1)
21122 (while (re-search-forward regexp end t)
21123 (if (and (<= (match-beginning 0) pos)
21124 (>= (match-end 0) pos))
21128 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
21129 "Non-nil when point is between matches of START-RE and END-RE.
21131 Also return a non-nil value when point is on one of the matches.
21133 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
21134 buffer positions. Default values are the positions of headlines
21135 surrounding the point.
21137 The functions returns a cons cell whose car (resp. cdr) is the
21138 position before START-RE (resp. after END-RE)."
21140 (let ((pos (point))
21141 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
21142 (limit-down (or lim-down (save-excursion (outline-next-heading))))
21145 ;; Point is on a block when on START-RE or if START-RE can be
21146 ;; found before it...
21147 (and (or (org-at-regexp-p start-re)
21148 (re-search-backward start-re limit-up t))
21149 (setq beg (match-beginning 0))
21150 ;; ... and END-RE after it...
21151 (goto-char (match-end 0))
21152 (re-search-forward end-re limit-down t)
21153 (> (setq end (match-end 0)) pos)
21154 ;; ... without another START-RE in-between.
21155 (goto-char (match-beginning 0))
21156 (not (re-search-backward start-re (1+ beg) t))
21158 (cons beg end))))))
21160 (defun org-in-block-p (names)
21161 "Non-nil when point belongs to a block whose name belongs to NAMES.
21163 NAMES is a list of strings containing names of blocks.
21165 Return first block name matched, or nil. Beware that in case of
21166 nested blocks, the returned name may not belong to the closest
21170 (let ((case-fold-search t)
21171 (lim-up (save-excursion (outline-previous-heading)))
21172 (lim-down (save-excursion (outline-next-heading))))
21173 (mapc (lambda (name)
21174 (let ((n (regexp-quote name)))
21175 (when (org-between-regexps-p
21176 (concat "^[ \t]*#\\+begin_" n)
21177 (concat "^[ \t]*#\\+end_" n)
21183 (defun org-occur-in-agenda-files (regexp &optional nlines)
21184 "Call `multi-occur' with buffers for all agenda files."
21185 (interactive "sOrg-files matching: \np")
21186 (let* ((files (org-agenda-files))
21187 (tnames (mapcar 'file-truename files))
21188 (extra org-agenda-text-search-extra-files)
21190 (when (eq (car extra) 'agenda-archives)
21191 (setq extra (cdr extra))
21192 (setq files (org-add-archive-files files)))
21193 (while (setq f (pop extra))
21194 (unless (member (file-truename f) tnames)
21195 (add-to-list 'files f 'append)
21196 (add-to-list 'tnames (file-truename f) 'append)))
21198 (mapcar (lambda (x)
21199 (with-current-buffer
21200 (or (get-file-buffer x) (find-file-noselect x))
21206 (if (boundp 'occur-mode-find-occurrence-hook)
21208 (add-hook 'occur-mode-find-occurrence-hook
21210 (when (derived-mode-p 'org-mode)
21213 (defadvice occur-mode-goto-occurrence
21214 (after org-occur-reveal activate)
21215 (and (derived-mode-p 'org-mode) (org-reveal)))
21216 (defadvice occur-mode-goto-occurrence-other-window
21217 (after org-occur-reveal activate)
21218 (and (derived-mode-p 'org-mode) (org-reveal)))
21219 (defadvice occur-mode-display-occurrence
21220 (after org-occur-reveal activate)
21221 (when (derived-mode-p 'org-mode)
21222 (let ((pos (occur-mode-find-occurrence)))
21223 (with-current-buffer (marker-buffer pos)
21228 (defun org-occur-link-in-agenda-files ()
21229 "Create a link and search for it in the agendas.
21230 The link is not stored in `org-stored-links', it is just created
21231 for the search purpose."
21233 (let ((link (condition-case nil
21234 (org-store-link nil)
21235 (error "Unable to create a link to here"))))
21236 (org-occur-in-agenda-files (regexp-quote link))))
21238 (defun org-reverse-string (string)
21239 "Return the reverse of STRING."
21240 (apply 'string (reverse (string-to-list string))))
21242 (defun org-uniquify (list)
21243 "Remove duplicate elements from LIST."
21245 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
21248 (defun org-delete-all (elts list)
21249 "Remove all elements in ELTS from LIST."
21251 (setq list (delete (pop elts) list)))
21254 (defun org-count (cl-item cl-seq)
21255 "Count the number of occurrences of ITEM in SEQ.
21256 Taken from `count' in cl-seq.el with all keyword arguments removed."
21257 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
21258 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
21259 (while (< cl-start cl-end)
21260 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
21261 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
21262 (setq cl-start (1+ cl-start)))
21265 (defun org-remove-if (predicate seq)
21266 "Remove everything from SEQ that fulfills PREDICATE."
21270 (if (not (funcall predicate e)) (push e res)))
21273 (defun org-remove-if-not (predicate seq)
21274 "Remove everything from SEQ that does not fulfill PREDICATE."
21278 (if (funcall predicate e) (push e res)))
21281 (defun org-reduce (cl-func cl-seq &rest cl-keys)
21282 "Reduce two-argument FUNCTION across SEQ.
21283 Taken from `reduce' in cl-seq.el with all keyword arguments but
21284 \":initial-value\" removed."
21285 (let ((cl-accum (cond ((memq :initial-value cl-keys)
21286 (cadr (memq :initial-value cl-keys)))
21287 (cl-seq (pop cl-seq))
21288 (t (funcall cl-func)))))
21290 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
21293 (defun org-back-over-empty-lines ()
21294 "Move backwards over whitespace, to the beginning of the first empty line.
21295 Returns the number of empty lines passed."
21296 (let ((pos (point)))
21297 (if (cdr (assoc 'heading org-blank-before-new-entry))
21298 (skip-chars-backward " \t\n\r")
21300 (forward-line -1)))
21301 (beginning-of-line 2)
21302 (goto-char (min (point) pos))
21303 (count-lines (point) pos)))
21305 (defun org-skip-whitespace ()
21306 (skip-chars-forward " \t\n\r"))
21308 (defun org-point-in-group (point group &optional context)
21309 "Check if POINT is in match-group GROUP.
21310 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
21311 match. If the match group does not exist or point is not inside it,
21313 (and (match-beginning group)
21314 (>= point (match-beginning group))
21315 (<= point (match-end group))
21317 (list context (match-beginning group) (match-end group))
21320 (defun org-switch-to-buffer-other-window (&rest args)
21321 "Switch to buffer in a second window on the current frame.
21322 In particular, do not allow pop-up frames.
21323 Returns the newly created buffer."
21325 (apply 'switch-to-buffer-other-window args)))
21327 (defun org-combine-plists (&rest plists)
21328 "Create a single property list from all plists in PLISTS.
21329 The process starts by copying the first list, and then setting properties
21330 from the other lists. Settings in the last list are the most significant
21331 ones and overrule settings in the other lists."
21332 (let ((rtn (copy-sequence (pop plists)))
21335 (setq ls (pop plists))
21337 (setq p (pop ls) v (pop ls))
21338 (setq rtn (plist-put rtn p v))))
21341 (defun org-replace-escapes (string table)
21342 "Replace %-escapes in STRING with values in TABLE.
21343 TABLE is an association list with keys like \"%a\" and string values.
21344 The sequences in STRING may contain normal field width and padding information,
21345 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
21346 so values can contain further %-escapes if they are define later in TABLE."
21347 (let ((tbl (copy-alist table))
21348 (case-fold-search nil)
21351 (while (setq e (pop tbl))
21352 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
21353 (when (and (cdr e) (string-match re (cdr e)))
21354 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
21356 (add-text-properties 0 3 (list 'sref sref) safe)
21357 (setcdr e (replace-match safe t t (cdr e)))))
21358 (while (string-match re string)
21359 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
21361 (setq string (replace-match rpl t t string))))
21362 (while (setq pchg (next-property-change pchg string))
21363 (let ((sref (get-text-property pchg 'sref string)))
21364 (when (and sref (string-match "SREF" string pchg))
21365 (setq string (replace-match sref t t string)))))
21368 (defun org-sublist (list start end)
21369 "Return a section of LIST, from START to END.
21370 Counting starts at 1."
21371 (let (rtn (c start))
21372 (setq list (nthcdr (1- start) list))
21373 (while (and list (<= c end))
21374 (push (pop list) rtn)
21378 (defun org-find-base-buffer-visiting (file)
21379 "Like `find-buffer-visiting' but always return the base buffer and
21380 not an indirect buffer."
21381 (let ((buf (or (get-file-buffer file)
21382 (find-buffer-visiting file))))
21384 (or (buffer-base-buffer buf) buf)
21387 (defun org-image-file-name-regexp (&optional extensions)
21388 "Return regexp matching the file names of images.
21389 If EXTENSIONS is given, only match these."
21390 (if (and (not extensions) (fboundp 'image-file-name-regexp))
21391 (image-file-name-regexp)
21392 (let ((image-file-name-extensions
21394 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
21395 "xbm" "xpm" "pbm" "pgm" "ppm"))))
21397 (regexp-opt (nconc (mapcar 'upcase
21398 image-file-name-extensions)
21399 image-file-name-extensions)
21403 (defun org-file-image-p (file &optional extensions)
21404 "Return non-nil if FILE is an image."
21406 (string-match (org-image-file-name-regexp extensions) file)))
21408 (defun org-get-cursor-date (&optional with-time)
21409 "Return the date at cursor in as a time.
21410 This works in the calendar and in the agenda, anywhere else it just
21411 returns the current time.
21412 If WITH-TIME is non-nil, returns the time of the event at point (in
21413 the agenda) or the current time of the day."
21414 (let (date day defd tp tm hod mod)
21416 (setq tp (get-text-property (point) 'time))
21417 (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
21418 (setq hod (string-to-number (match-string 1 tp))
21419 mod (string-to-number (match-string 2 tp))))
21420 (or tp (setq hod (nth 2 (decode-time (current-time)))
21421 mod (nth 1 (decode-time (current-time))))))
21423 ((eq major-mode 'calendar-mode)
21424 (setq date (calendar-cursor-to-date)
21425 defd (encode-time 0 (or mod 0) (or hod 0)
21426 (nth 1 date) (nth 0 date) (nth 2 date))))
21427 ((eq major-mode 'org-agenda-mode)
21428 (setq day (get-text-property (point) 'day))
21430 (setq date (calendar-gregorian-from-absolute day)
21431 defd (encode-time 0 (or mod 0) (or hod 0)
21432 (nth 1 date) (nth 0 date) (nth 2 date))))))
21433 (or defd (current-time))))
21435 (defun org-mark-subtree (&optional up)
21436 "Mark the current subtree.
21437 This puts point at the start of the current subtree, and mark at
21438 the end. If a numeric prefix UP is given, move up into the
21439 hierarchy of headlines by UP levels before marking the subtree."
21441 (org-with-limited-levels
21442 (cond ((org-at-heading-p) (beginning-of-line))
21443 ((org-before-first-heading-p) (error "Not in a subtree"))
21444 (t (outline-previous-visible-heading 1))))
21445 (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
21446 (if (org-called-interactively-p 'any)
21447 (call-interactively 'org-mark-element)
21448 (org-mark-element)))
21453 (defun org-indent-line ()
21454 "Indent line depending on context."
21456 (let* ((pos (point))
21457 (itemp (org-at-item-p))
21458 (case-fold-search t)
21459 (org-drawer-regexp (or org-drawer-regexp "\000"))
21460 (inline-task-p (and (featurep 'org-inlinetask)
21461 (org-inlinetask-in-task-p)))
21462 (inline-re (and inline-task-p
21463 (org-inlinetask-outline-regexp)))
21465 (if (and orgstruct-is-++ (eq pos (point)))
21466 (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
21467 (indent-according-to-mode))
21468 (beginning-of-line 1)
21471 ((looking-at org-outline-regexp) (setq column 0))
21472 ;; Footnote definition
21473 ((looking-at org-footnote-definition-re) (setq column 0))
21474 ;; Literal examples
21475 ((looking-at "[ \t]*:\\( \\|$\\)")
21476 (setq column (org-get-indentation))) ; do nothing
21478 ((ignore-errors (goto-char (org-in-item-p)))
21479 (setq column (if itemp
21480 (org-get-indentation)
21481 (org-list-item-body-column (point))))
21484 ((and (looking-at "[ \t]*:END:")
21485 (save-excursion (re-search-backward org-drawer-regexp nil t)))
21487 (goto-char (1- (match-beginning 1)))
21488 (setq column (current-column))))
21490 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
21492 (re-search-backward
21493 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
21494 (setq column (org-get-indentation (match-string 0))))
21495 ((and (not (looking-at "[ \t]*#\\+begin_"))
21496 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
21498 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
21500 (cond ((equal (downcase (match-string 1)) "src")
21501 ;; src blocks: let `org-edit-src-exit' handle them
21502 (org-get-indentation))
21503 ((equal (downcase (match-string 1)) "example")
21504 (max (org-get-indentation)
21505 (org-get-indentation (match-string 0))))
21507 (org-get-indentation (match-string 0))))))
21508 ;; This line has nothing special, look at the previous relevant
21509 ;; line to compute indentation
21511 (beginning-of-line 0)
21512 (while (and (not (bobp))
21513 (not (looking-at org-table-line-regexp))
21514 (not (looking-at org-drawer-regexp))
21515 ;; When point started in an inline task, do not move
21516 ;; above task starting line.
21517 (not (and inline-task-p (looking-at inline-re)))
21518 ;; Skip drawers, blocks, empty lines, verbatim,
21519 ;; comments, tables, footnotes definitions, lists,
21521 (or (and (looking-at "[ \t]*:END:")
21522 (re-search-backward org-drawer-regexp nil t))
21523 (and (looking-at "[ \t]*#\\+end_")
21524 (re-search-backward "[ \t]*#\\+begin_"nil t))
21525 (looking-at "[ \t]*[\n:#|]")
21526 (looking-at org-footnote-definition-re)
21527 (and (ignore-errors (goto-char (org-in-item-p)))
21529 (org-list-get-top-point (org-list-struct))))
21530 (and (not inline-task-p)
21531 (featurep 'org-inlinetask)
21532 (org-inlinetask-in-task-p)
21533 (or (org-inlinetask-goto-beginning) t))))
21534 (beginning-of-line 0))
21536 ;; There was an heading above.
21537 ((looking-at "\\*+[ \t]+")
21538 (if (not org-adapt-indentation)
21540 (goto-char (match-end 0))
21541 (setq column (current-column))))
21542 ;; A drawer had started and is unfinished
21543 ((looking-at org-drawer-regexp)
21544 (goto-char (1- (match-beginning 1)))
21545 (setq column (current-column)))
21546 ;; Else, nothing noticeable found: get indentation and go on.
21547 (t (setq column (org-get-indentation))))))
21548 ;; Now apply indentation and move cursor accordingly
21550 (if (<= (current-column) (current-indentation))
21551 (org-indent-line-to column)
21552 (save-excursion (org-indent-line-to column)))
21553 ;; Special polishing for properties, see `org-property-format'
21554 (setq column (current-column))
21555 (beginning-of-line 1)
21557 "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
21558 (replace-match (concat (match-string 1)
21559 (format org-property-format
21560 (match-string 2) (match-string 3)))
21562 (org-move-to-column column))))
21564 (defun org-indent-drawer ()
21565 "Indent the drawer at point."
21568 (e (and (save-excursion (re-search-forward ":END:" nil t))
21573 (when (overlays-at (point))
21574 (member 'invisible (overlay-properties
21575 (car (overlays-at (point)))))))))
21576 (when folded (org-cycle))
21577 (indent-for-tab-command)
21578 (while (and (move-beginning-of-line 2) (< (point) e))
21579 (indent-for-tab-command))
21581 (when folded (org-cycle)))
21582 (message "Drawer at point indented"))
21584 (defun org-indent-block ()
21585 "Indent the block at point."
21588 (case-fold-search t)
21589 (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
21594 (when (overlays-at (point))
21595 (member 'invisible (overlay-properties
21596 (car (overlays-at (point)))))))))
21597 (when folded (org-cycle))
21598 (indent-for-tab-command)
21599 (while (and (move-beginning-of-line 2) (< (point) e))
21600 (indent-for-tab-command))
21602 (when folded (org-cycle)))
21603 (message "Block at point indented"))
21605 (defun org-indent-region (start end)
21609 (let ((line-end (org-current-line end)))
21611 (while (< (org-current-line) line-end)
21612 (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
21613 (t (call-interactively 'org-indent-line)))
21614 (move-beginning-of-line 2)))))
21619 ;; We use our own fill-paragraph and auto-fill functions.
21621 ;; `org-fill-paragraph' relies on adaptive filling and context
21622 ;; checking. Appropriate `fill-prefix' is computed with
21623 ;; `org-adaptive-fill-function'.
21625 ;; `org-auto-fill-function' takes care of auto-filling. It calls
21626 ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
21627 ;; `org-adaptive-fill-function' value. Internally,
21628 ;; `org-comment-line-break-function' breaks the line.
21630 ;; `org-setup-filling' installs filling and auto-filling related
21631 ;; variables during `org-mode' initialization.
21633 (defun org-setup-filling ()
21635 ;; Prevent auto-fill from inserting unwanted new items.
21636 (when (boundp 'fill-nobreak-predicate)
21638 'fill-nobreak-predicate
21640 (append fill-nobreak-predicate
21641 '(org-fill-paragraph-separate-nobreak-p
21642 org-fill-line-break-nobreak-p
21643 org-fill-paragraph-with-timestamp-nobreak-p)))))
21644 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
21645 (org-set-local 'auto-fill-inhibit-regexp nil)
21646 (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
21647 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
21648 (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
21650 (defvar org-element-paragraph-separate) ; org-element.el
21651 (defun org-fill-paragraph-separate-nobreak-p ()
21652 "Non-nil when a line break at point would insert a new item."
21653 (looking-at (substring org-element-paragraph-separate 1)))
21655 (defun org-fill-line-break-nobreak-p ()
21656 "Non-nil when a line break at point would create an Org line break."
21658 (skip-chars-backward "[ \t]")
21659 (skip-chars-backward "\\\\")
21660 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
21662 (defun org-fill-paragraph-with-timestamp-nobreak-p ()
21663 "Non-nil when a line break at point would insert a new item."
21664 (and (org-at-timestamp-p t)
21665 (not (looking-at org-ts-regexp-both))))
21667 (declare-function message-in-body-p "message" ())
21668 (defvar orgtbl-line-start-regexp) ; From org-table.el
21669 (defun org-adaptive-fill-function ()
21670 "Compute a fill prefix for the current line.
21671 Return fill prefix, as a string, or nil if current line isn't
21672 meant to be filled."
21675 (when (derived-mode-p 'message-mode)
21677 (beginning-of-line)
21678 (cond ((or (not (message-in-body-p))
21679 (looking-at orgtbl-line-start-regexp))
21681 ((looking-at message-cite-prefix-regexp)
21682 (throw 'exit (match-string-no-properties 0)))
21683 ((looking-at org-outline-regexp)
21684 (throw 'exit (make-string (length (match-string 0)) ? ))))))
21685 (org-with-wide-buffer
21686 (let* ((p (line-beginning-position))
21687 (element (save-excursion (beginning-of-line)
21688 (org-element-at-point)))
21689 (type (org-element-type element))
21690 (post-affiliated (org-element-property :post-affiliated element)))
21691 (unless (and post-affiliated (< p post-affiliated))
21693 (comment (looking-at "[ \t]*# ?") (match-string 0))
21694 (footnote-definition "")
21696 (make-string (org-list-item-body-column
21697 (or post-affiliated
21698 (org-element-property :begin element)))
21701 ;; Fill prefix is usually the same as the current line,
21702 ;; except if the paragraph is at the beginning of an item.
21703 (let ((parent (org-element-property :parent element)))
21704 (cond ((eq (org-element-type parent) 'item)
21705 (make-string (org-list-item-body-column
21706 (org-element-property :begin parent))
21708 ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
21712 ;; Only fill contents if P is within block boundaries.
21713 (let* ((cbeg (save-excursion (goto-char post-affiliated)
21716 (cend (save-excursion
21717 (goto-char (org-element-property :end element))
21718 (skip-chars-backward " \r\t\n")
21719 (line-beginning-position))))
21720 (when (and (>= p cbeg) (< p cend))
21721 (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
21725 (declare-function message-goto-body "message" ())
21726 (defvar message-cite-prefix-regexp) ; From message.el
21727 (defvar org-element-all-objects) ; From org-element.el
21728 (defun org-fill-paragraph (&optional justify)
21729 "Fill element at point, when applicable.
21731 This function only applies to comment blocks, comments, example
21732 blocks and paragraphs. Also, as a special case, re-align table
21733 when point is at one.
21735 If JUSTIFY is non-nil (interactively, with prefix argument),
21736 justify as well. If `sentence-end-double-space' is non-nil, then
21737 period followed by one space does not end a sentence, so don't
21738 break a line there. The variable `fill-column' controls the
21741 For convenience, when point is at a plain list, an item or
21742 a footnote definition, try to fill the first paragraph within."
21744 (if (and (derived-mode-p 'message-mode)
21745 (or (not (message-in-body-p))
21746 (save-excursion (move-beginning-of-line 1)
21747 (looking-at message-cite-prefix-regexp))))
21748 ;; First ensure filling is correct in message-mode.
21749 (let ((fill-paragraph-function
21750 (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
21751 (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
21752 (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
21753 (paragraph-separate
21754 (cadadr (assoc 'paragraph-separate org-fb-vars))))
21755 (fill-paragraph nil))
21756 (with-syntax-table org-mode-transpose-word-syntax-table
21758 ;; Move to end of line in order to get the first paragraph
21759 ;; within a plain list or a footnote definition.
21761 (let ((element (org-element-at-point)))
21762 ;; First check if point is in a blank line at the beginning of
21763 ;; the buffer. In that case, ignore filling.
21764 (if (< (point) (org-element-property :begin element)) t
21765 (case (org-element-type element)
21766 ;; Use major mode filling function is src blocks.
21767 (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
21768 ;; Align Org tables, leave table.el tables as-is.
21769 (table-row (org-table-align) t)
21771 (when (eq (org-element-property :type element) 'org)
21775 ;; Paragraphs may contain `line-break' type objects.
21776 (let ((beg (max (point-min)
21777 (org-element-property :contents-begin element)))
21778 (end (min (point-max)
21779 (org-element-property :contents-end element))))
21780 ;; Do nothing if point is at an affiliated keyword.
21781 (if (< (point) beg) t
21782 (when (derived-mode-p 'message-mode)
21783 ;; In `message-mode', do not fill following
21784 ;; citation in current paragraph nor text before
21786 (let ((body-start (save-excursion (message-goto-body))))
21787 (when body-start (setq beg (max body-start beg))))
21788 (when (save-excursion
21790 (concat "^" message-cite-prefix-regexp) end t))
21791 (setq end (match-beginning 0))))
21792 ;; Fill paragraph, taking line breaks into
21793 ;; consideration. For that, slice the paragraph
21794 ;; using line breaks as separators, and fill the
21795 ;; parts in reverse order to avoid messing with
21801 (fill-region-as-paragraph pos (point) justify)
21803 ;; Find the list of ending positions for line
21804 ;; breaks in the current paragraph. Add paragraph
21805 ;; beginning to include first slice.
21810 (org-element--parse-objects
21811 beg end nil org-element-all-objects)
21813 (lambda (lb) (org-element-property :end lb)))))))
21815 ;; Contents of `comment-block' type elements should be
21816 ;; filled as plain text, but only if point is within block
21819 (let* ((case-fold-search t)
21820 (beg (save-excursion
21821 (goto-char (org-element-property :begin element))
21822 (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
21825 (end (save-excursion
21826 (goto-char (org-element-property :end element))
21827 (re-search-backward "^[ \t]*#\\+end_comment" nil t)
21828 (line-beginning-position))))
21829 (when (and (>= (point) beg) (< (point) end))
21830 (fill-region-as-paragraph
21833 (re-search-backward "^[ \t]*$" beg 'move)
21834 (line-beginning-position))
21836 (beginning-of-line)
21837 (re-search-forward "^[ \t]*$" end 'move)
21838 (line-beginning-position))
21842 (comment (fill-comment-paragraph justify))
21843 ;; Ignore every other element.
21844 (otherwise t))))))))
21846 (defun org-auto-fill-function ()
21847 "Auto-fill function."
21848 ;; Check if auto-filling is meaningful.
21849 (let ((fc (current-fill-column)))
21850 (when (and fc (> (current-column) fc))
21851 (let* ((fill-prefix (org-adaptive-fill-function))
21852 ;; Enforce empty fill prefix, if required. Otherwise, it
21853 ;; will be computed again.
21854 (adaptive-fill-mode (not (equal fill-prefix ""))))
21855 (when fill-prefix (do-auto-fill))))))
21857 (defun org-comment-line-break-function (&optional soft)
21858 "Break line at point and indent, continuing comment if within one.
21859 The inserted newline is marked hard if variable
21860 `use-hard-newlines' is true, unless optional argument SOFT is
21862 (if soft (insert-and-inherit ?\n) (newline 1))
21863 (save-excursion (forward-char -1) (delete-horizontal-space))
21864 (delete-horizontal-space)
21865 (indent-to-left-margin)
21866 (insert-before-markers-and-inherit fill-prefix))
21871 ;; Org comments syntax is quite complex. It requires the entire line
21872 ;; to be just a comment. Also, even with the right syntax at the
21873 ;; beginning of line, some some elements (i.e. verse-block or
21874 ;; example-block) don't accept comments. Usual Emacs comment commands
21875 ;; cannot cope with those requirements. Therefore, Org replaces them.
21877 ;; Org still relies on `comment-dwim', but cannot trust
21878 ;; `comment-only-p'. So, `comment-region-function' and
21879 ;; `uncomment-region-function' both point
21880 ;; to`org-comment-or-uncomment-region'. Eventually,
21881 ;; `org-insert-comment' takes care of insertion of comments at the
21882 ;; beginning of line.
21884 ;; `org-setup-comments-handling' install comments related variables
21885 ;; during `org-mode' initialization.
21887 (defun org-setup-comments-handling ()
21889 (org-set-local 'comment-use-syntax nil)
21890 (org-set-local 'comment-start "# ")
21891 (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
21892 (org-set-local 'comment-insert-comment-function 'org-insert-comment)
21893 (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
21894 (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
21896 (defun org-insert-comment ()
21897 "Insert an empty comment above current line.
21898 If the line is empty, insert comment at its beginning."
21899 (beginning-of-line)
21900 (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
21904 (defvar comment-empty-lines) ; From newcomment.el.
21905 (defun org-comment-or-uncomment-region (beg end &rest ignore)
21906 "Comment or uncomment each non-blank line in the region.
21907 Uncomment each non-blank line between BEG and END if it only
21908 contains commented lines. Otherwise, comment them."
21911 (narrow-to-region (save-excursion (goto-char beg)
21912 (skip-chars-forward " \r\t\n" end)
21913 (line-beginning-position))
21914 (save-excursion (goto-char end)
21915 (skip-chars-backward " \r\t\n" beg)
21916 (line-end-position)))
21918 ;; UNCOMMENTP is non-nil when every non blank line between
21919 ;; BEG and END is a comment.
21921 (goto-char (point-min))
21922 (while (and (not (eobp))
21923 (let ((element (org-element-at-point)))
21924 (and (eq (org-element-type element) 'comment)
21925 (goto-char (min (point-max)
21926 (org-element-property
21927 :end element)))))))
21930 ;; Only blank lines and comments in region: uncomment it.
21932 (goto-char (point-min))
21933 (while (not (eobp))
21934 (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
21935 (replace-match "" nil nil nil 1))
21937 ;; Comment each line in region.
21938 (let ((min-indent (point-max)))
21939 ;; First find the minimum indentation across all lines.
21941 (goto-char (point-min))
21942 (while (and (not (eobp)) (not (zerop min-indent)))
21943 (unless (looking-at "[ \t]*$")
21944 (setq min-indent (min min-indent (current-indentation))))
21946 ;; Then loop over all lines.
21948 (goto-char (point-min))
21949 (while (not (eobp))
21950 (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
21951 (org-move-to-column min-indent t)
21952 (insert comment-start))
21953 (forward-line))))))))
21958 ;; This section contains tools to operate on timestamp objects, as
21959 ;; returned by, e.g. `org-element-context'.
21961 (defun org-timestamp-has-time-p (timestamp)
21962 "Non-nil when TIMESTAMP has a time specified."
21963 (org-element-property :hour-start timestamp))
21965 (defun org-timestamp-format (timestamp format &optional end utc)
21966 "Format a TIMESTAMP element into a string.
21968 FORMAT is a format specifier to be passed to
21969 `format-time-string'.
21971 When optional argument END is non-nil, use end of date-range or
21972 time-range, if possible.
21974 When optional argument UTC is non-nil, time will be expressed as
21976 (format-time-string
21978 (apply 'encode-time
21981 (lambda (prop) (or (org-element-property prop timestamp) 0))
21982 (if end '(:minute-end :hour-end :day-end :month-end :year-end)
21983 '(:minute-start :hour-start :day-start :month-start
21987 (defun org-timestamp-split-range (timestamp &optional end)
21988 "Extract a timestamp object from a date or time range.
21990 TIMESTAMP is a timestamp object. END, when non-nil, means extract
21991 the end of the range. Otherwise, extract its start.
21993 Return a new timestamp object sharing the same parent as
21995 (let ((type (org-element-property :type timestamp)))
21996 (if (memq type '(active inactive diary)) timestamp
21997 (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
21999 (org-element-put-property
22000 split-ts :type (if (eq type 'active-range) 'active 'inactive))
22001 ;; Copy start properties over end properties if END is
22002 ;; non-nil. Otherwise, copy end properties over `start' ones.
22003 (let ((p-alist '((:minute-start . :minute-end)
22004 (:hour-start . :hour-end)
22005 (:day-start . :day-end)
22006 (:month-start . :month-end)
22007 (:year-start . :year-end))))
22008 (dolist (p-cell p-alist)
22009 (org-element-put-property
22011 (funcall (if end 'car 'cdr) p-cell)
22012 (org-element-property
22013 (funcall (if end 'cdr 'car) p-cell) split-ts)))
22014 ;; Eventually refresh `:raw-value'.
22015 (org-element-put-property split-ts :raw-value nil)
22016 (org-element-put-property
22017 split-ts :raw-value (org-element-interpret-data split-ts)))))))
22019 (defun org-timestamp-translate (timestamp &optional boundary)
22020 "Apply `org-translate-time' on a TIMESTAMP object.
22021 When optional argument BOUNDARY is non-nil, it is either the
22022 symbol `start' or `end'. In this case, only translate the
22023 starting or ending part of TIMESTAMP if it is a date or time
22024 range. Otherwise, translate both parts."
22025 (if (and (not boundary)
22026 (memq (org-element-property :type timestamp)
22027 '(active-range inactive-range)))
22029 (org-translate-time
22030 (org-element-property :raw-value
22031 (org-timestamp-split-range timestamp)))
22033 (org-translate-time
22034 (org-element-property :raw-value
22035 (org-timestamp-split-range timestamp t))))
22036 (org-translate-time
22037 (org-element-property
22039 (if (not boundary) timestamp
22040 (org-timestamp-split-range timestamp (eq boundary 'end)))))))
22046 (defun org-toggle-fixed-width-section (arg)
22047 "Toggle the fixed-width export.
22048 If there is no active region, the QUOTE keyword at the current headline is
22049 inserted or removed. When present, it causes the text between this headline
22050 and the next to be exported as fixed-width text, and unmodified.
22051 If there is an active region, this command adds or removes a colon as the
22052 first character of this line. If the first character of a line is a colon,
22053 this line is also exported in fixed-width font."
22056 (regionp (org-region-active-p))
22057 (beg (if regionp (region-beginning) (point)))
22058 (end (if regionp (region-end)))
22059 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
22060 (case-fold-search nil)
22061 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
22066 (setq cc (current-column))
22067 (beginning-of-line 1)
22068 (setq off (looking-at re))
22069 (while (> nlines 0)
22070 (setq nlines (1- nlines))
22071 (beginning-of-line 1)
22074 (org-move-to-column cc t)
22077 ((and off (looking-at re))
22078 (replace-match "" t t nil 1))
22079 ((not off) (org-move-to-column cc t) (insert ": ")))
22082 (org-back-to-heading)
22084 ((looking-at (format org-heading-keyword-regexp-format
22086 (goto-char (match-end 1))
22087 (looking-at (concat " +" org-quote-string))
22088 (replace-match "" t t)
22089 (when (eolp) (insert " ")))
22090 ((looking-at org-outline-regexp)
22091 (goto-char (match-end 0))
22092 (insert org-quote-string " ")))))))
22094 (defun org-reftex-citation ()
22095 "Use reftex-citation to insert a citation into the buffer.
22096 This looks for a line like
22098 #+BIBLIOGRAPHY: foo plain option:-d
22100 and derives from it that foo.bib is the bibliography file relevant
22101 for this document. It then installs the necessary environment for RefTeX
22102 to work in this buffer and calls `reftex-citation' to insert a citation
22105 Export of such citations to both LaTeX and HTML is handled by the contributed
22106 package org-exp-bibtex by Taru Karttunen."
22108 (let ((reftex-docstruct-symbol 'rds)
22109 (reftex-cite-format "\\cite{%l}")
22114 (let ((case-fold-search t)
22115 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
22116 (if (not (save-excursion
22117 (or (re-search-forward re nil t)
22118 (re-search-backward re nil t))))
22119 (error "No bibliography defined in file")
22120 (setq bib (concat (match-string 1) ".bib")
22121 rds (list (list 'bib bib)))))))
22122 (call-interactively 'reftex-citation)))
22124 ;;;; Functions extending outline functionality
22126 (defun org-beginning-of-line (&optional arg)
22127 "Go to the beginning of the current line. If that is invisible, continue
22128 to a visible line beginning. This makes the function of C-a more intuitive.
22129 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
22130 first attempt, and only move to after the tags when the cursor is already
22131 beyond the end of the headline."
22133 (let ((pos (point))
22134 (special (if (consp org-special-ctrl-a/e)
22135 (car org-special-ctrl-a/e)
22136 org-special-ctrl-a/e))
22138 (if (org-bound-and-true-p visual-line-mode)
22139 (beginning-of-visual-line 1)
22140 (beginning-of-line 1))
22141 (if (and arg (fboundp 'move-beginning-of-line))
22142 (call-interactively 'move-beginning-of-line)
22146 (if (org-truely-invisible-p)
22147 (while (and (not (bobp)) (org-truely-invisible-p))
22149 (beginning-of-line 1))
22150 (forward-char 1))))
22153 ((and (looking-at org-complex-heading-regexp)
22154 (= (char-after (match-end 1)) ?\ ))
22155 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
22159 (cond ((> pos refpos) refpos)
22160 ((= pos (point)) refpos)
22162 (cond ((> pos (point)) (point))
22163 ((not (eq last-command this-command)) (point))
22166 ;; Being at an item and not looking at an the item means point
22167 ;; was previously moved to beginning of a visual line, which
22168 ;; doesn't contain the item. Therefore, do nothing special,
22170 (when (looking-at org-list-full-item-re)
22171 ;; Set special position at first white space character after
22172 ;; bullet, and check-box, if any.
22173 (let ((after-bullet
22174 (let ((box (match-end 3)))
22175 (if (not box) (match-end 1)
22176 (let ((after (char-after box)))
22177 (if (and after (= after ? )) (1+ box) box))))))
22178 ;; Special case: Move point to special position when
22179 ;; currently after it or at beginning of line.
22181 (when (or (> pos after-bullet) (= (point) pos))
22182 (goto-char after-bullet))
22183 ;; Reversed case: Move point to special position when
22184 ;; point was already at beginning of line and command is
22186 (when (and (= (point) pos) (eq last-command this-command))
22187 (goto-char after-bullet))))))))
22189 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
22191 (defun org-end-of-line (&optional arg)
22192 "Go to the end of the line.
22193 If this is a headline, and `org-special-ctrl-a/e' is set, ignore
22194 tags on the first attempt, and only move to after the tags when
22195 the cursor is already beyond the end of the headline."
22197 (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
22198 org-special-ctrl-a/e))
22199 (move-fun (cond ((org-bound-and-true-p visual-line-mode)
22200 'end-of-visual-line)
22201 ((fboundp 'move-end-of-line) 'move-end-of-line)
22202 (t 'end-of-line))))
22203 (if (or (not special) arg) (call-interactively move-fun)
22204 (let* ((element (save-excursion (beginning-of-line)
22205 (org-element-at-point)))
22206 (type (org-element-type element)))
22208 ((memq type '(headline inlinetask))
22209 (let ((pos (point)))
22210 (beginning-of-line 1)
22211 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
22213 (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
22214 (goto-char (match-beginning 1))
22215 (goto-char (match-end 0)))
22216 (if (or (< pos (match-end 0))
22217 (not (eq this-command last-command)))
22218 (goto-char (match-end 0))
22219 (goto-char (match-beginning 1))))
22220 (call-interactively move-fun))))
22221 ((org-element-property :hiddenp element)
22222 ;; If element is hidden, `move-end-of-line' would put point
22223 ;; after it. Use `end-of-line' to stay on current line.
22224 (call-interactively 'end-of-line))
22225 (t (call-interactively move-fun)))))
22226 (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
22228 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
22229 (define-key org-mode-map "\C-e" 'org-end-of-line)
22231 (defun org-backward-sentence (&optional arg)
22232 "Go to beginning of sentence, or beginning of table field.
22233 This will call `backward-sentence' or `org-table-beginning-of-field',
22234 depending on context."
22237 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
22238 (t (call-interactively 'backward-sentence))))
22240 (defun org-forward-sentence (&optional arg)
22241 "Go to end of sentence, or end of table field.
22242 This will call `forward-sentence' or `org-table-end-of-field',
22243 depending on context."
22246 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
22247 (t (call-interactively 'forward-sentence))))
22249 (define-key org-mode-map "\M-a" 'org-backward-sentence)
22250 (define-key org-mode-map "\M-e" 'org-forward-sentence)
22252 (defun org-kill-line (&optional arg)
22253 "Kill line, to tags or end of line."
22256 ((or (not org-special-ctrl-k)
22258 (not (org-at-heading-p)))
22259 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
22260 org-ctrl-k-protect-subtree)
22261 (if (or (eq org-ctrl-k-protect-subtree 'error)
22262 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
22263 (error "C-k aborted - would kill hidden subtree")))
22264 (call-interactively
22265 (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
22266 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
22267 (kill-region (point) (match-beginning 1))
22268 (org-set-tags nil t))
22269 (t (kill-region (point) (point-at-eol)))))
22271 (define-key org-mode-map "\C-k" 'org-kill-line)
22273 (defun org-yank (&optional arg)
22274 "Yank. If the kill is a subtree, treat it specially.
22275 This command will look at the current kill and check if is a single
22276 subtree, or a series of subtrees[1]. If it passes the test, and if the
22277 cursor is at the beginning of a line or after the stars of a currently
22278 empty headline, then the yank is handled specially. How exactly depends
22279 on the value of the following variables, both set by default.
22281 org-yank-folded-subtrees
22282 When set, the subtree(s) will be folded after insertion, but only
22283 if doing so would now swallow text after the yanked text.
22285 org-yank-adjusted-subtrees
22286 When set, the subtree will be promoted or demoted in order to
22287 fit into the local outline tree structure, which means that the level
22288 will be adjusted so that it becomes the smaller one of the two
22289 *visible* surrounding headings.
22291 Any prefix to this command will cause `yank' to be called directly with
22292 no special treatment. In particular, a simple \\[universal-argument] prefix \
22294 plainly yank the text as it is.
22296 \[1] The test checks if the first non-white line is a heading
22297 and if there are no other headings with fewer stars."
22299 (org-yank-generic 'yank arg))
22301 (defun org-yank-generic (command arg)
22302 "Perform some yank-like command.
22304 This function implements the behavior described in the `org-yank'
22305 documentation. However, it has been generalized to work for any
22306 interactive command with similar behavior."
22308 ;; pretend to be command COMMAND
22309 (setq this-command command)
22312 (call-interactively command)
22314 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
22315 (and (org-kill-is-subtree-p)
22317 (and (looking-at "[ \t]*$")
22320 (buffer-substring (point-at-bol) (point)))))))
22323 ((and subtreep org-yank-folded-subtrees)
22324 (let ((beg (point))
22326 (if (and subtreep org-yank-adjusted-subtrees)
22327 (org-paste-subtree nil nil 'for-yank)
22328 (call-interactively command))
22332 (when (and (bolp) subtreep
22333 (not (setq swallowp
22334 (org-yank-folding-would-swallow-text beg end))))
22335 (org-with-limited-levels
22336 (or (looking-at org-outline-regexp)
22337 (re-search-forward org-outline-regexp-bol end t))
22338 (while (and (< (point) end) (looking-at org-outline-regexp))
22340 (org-cycle-show-empty-lines 'folded)
22341 (condition-case nil
22342 (outline-forward-same-level 1)
22343 (error (goto-char end))))))
22346 "Inserted text not folded because that would swallow text"))
22349 (skip-chars-forward " \t\n\r")
22350 (beginning-of-line 1)
22351 (push-mark beg 'nomsg)))
22352 ((and subtreep org-yank-adjusted-subtrees)
22353 (let ((beg (point-at-bol)))
22354 (org-paste-subtree nil nil 'for-yank)
22355 (push-mark beg 'nomsg)))
22357 (call-interactively command))))))
22359 (defun org-yank-folding-would-swallow-text (beg end)
22360 "Would hide-subtree at BEG swallow any text after END?"
22362 (org-with-limited-levels
22365 (when (or (looking-at org-outline-regexp)
22366 (re-search-forward org-outline-regexp-bol end t))
22367 (setq level (org-outline-level)))
22369 (skip-chars-forward " \t\r\n\v\f")
22371 (and (bolp) (looking-at org-outline-regexp)
22372 (<= (org-outline-level) level)))
22373 nil ; Nothing would be swallowed
22374 t))))) ; something would swallow
22376 (define-key org-mode-map "\C-y" 'org-yank)
22378 (defun org-truely-invisible-p ()
22379 "Check if point is at a character currently not visible.
22380 This version does not only check the character property, but also
22382 ;; Early versions of noutline don't have `outline-invisible-p'.
22383 (if (org-bound-and-true-p visible-mode)
22385 (outline-invisible-p)))
22387 (defun org-invisible-p2 ()
22388 "Check if point is at a character currently not visible."
22390 (if (and (eolp) (not (bobp))) (backward-char 1))
22391 ;; Early versions of noutline don't have `outline-invisible-p'.
22392 (outline-invisible-p)))
22394 (defun org-back-to-heading (&optional invisible-ok)
22395 "Call `outline-back-to-heading', but provide a better error message."
22396 (condition-case nil
22397 (outline-back-to-heading invisible-ok)
22398 (error (error "Before first headline at position %d in buffer %s"
22399 (point) (current-buffer)))))
22401 (defun org-before-first-heading-p ()
22402 "Before first heading?"
22405 (null (re-search-backward org-outline-regexp-bol nil t))))
22407 (defun org-at-heading-p (&optional ignored)
22408 (outline-on-heading-p t))
22409 ;; Compatibility alias with Org versions < 7.8.03
22410 (defalias 'org-on-heading-p 'org-at-heading-p)
22412 (defun org-at-comment-p nil
22413 "Is cursor in a line starting with a # character?"
22415 (beginning-of-line)
22416 (looking-at "^#")))
22418 (defun org-at-drawer-p nil
22419 "Is cursor at a drawer keyword?"
22421 (move-beginning-of-line 1)
22422 (looking-at org-drawer-regexp)))
22424 (defun org-at-block-p nil
22425 "Is cursor at a block keyword?"
22427 (move-beginning-of-line 1)
22428 (looking-at org-block-regexp)))
22430 (defun org-point-at-end-of-empty-headline ()
22431 "If point is at the end of an empty headline, return t, else nil.
22432 If the heading only contains a TODO keyword, it is still still considered
22434 (and (looking-at "[ \t]*$")
22435 (when org-todo-line-regexp
22437 (beginning-of-line 1)
22438 (let ((case-fold-search nil))
22439 (looking-at org-todo-line-regexp)
22440 (string= (match-string 3) ""))))))
22442 (defun org-at-heading-or-item-p ()
22443 (or (org-at-heading-p) (org-at-item-p)))
22445 (defun org-at-target-p ()
22446 (or (org-in-regexp org-radio-target-regexp)
22447 (org-in-regexp org-target-regexp)))
22448 ;; Compatibility alias with Org versions < 7.8.03
22449 (defalias 'org-on-target-p 'org-at-target-p)
22451 (defun org-up-heading-all (arg)
22452 "Move to the heading line of which the present line is a subheading.
22453 This function considers both visible and invisible heading lines.
22454 With argument, move up ARG levels."
22455 (if (fboundp 'outline-up-heading-all)
22456 (outline-up-heading-all arg) ; emacs 21 version of outline.el
22457 (outline-up-heading arg t))) ; emacs 22 version of outline.el
22459 (defun org-up-heading-safe ()
22460 "Move to the heading line of which the present line is a subheading.
22461 This version will not throw an error. It will return the level of the
22462 headline found, or nil if no higher level is found.
22464 Also, this function will be a lot faster than `outline-up-heading',
22465 because it relies on stars being the outline starters. This can really
22466 make a significant difference in outlines with very many siblings."
22467 (let (start-level re)
22468 (org-back-to-heading t)
22469 (setq start-level (funcall outline-level))
22470 (if (equal start-level 1)
22472 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
22473 (if (re-search-backward re nil t)
22474 (funcall outline-level)))))
22476 (defun org-first-sibling-p ()
22477 "Is this heading the first child of its parents?"
22479 (let ((re org-outline-regexp-bol)
22481 (unless (org-at-heading-p t)
22482 (error "Not at a heading"))
22483 (setq level (funcall outline-level))
22485 (if (not (re-search-backward re nil t))
22487 (setq l (funcall outline-level))
22490 (defun org-goto-sibling (&optional previous)
22491 "Goto the next sibling, even if it is invisible.
22492 When PREVIOUS is set, go to the previous sibling instead. Returns t
22493 when a sibling was found. When none is found, return nil and don't
22495 (let ((fun (if previous 're-search-backward 're-search-forward))
22497 (re org-outline-regexp-bol)
22499 (when (condition-case nil (org-back-to-heading t) (error nil))
22500 (setq level (funcall outline-level))
22502 (or previous (forward-char 1))
22503 (while (funcall fun re nil t)
22504 (setq l (funcall outline-level))
22505 (when (< l level) (goto-char pos) (throw 'exit nil))
22506 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
22510 (defun org-show-siblings ()
22511 "Show all siblings of the current headline."
22513 (while (org-goto-sibling) (org-flag-heading nil)))
22515 (while (org-goto-sibling 'previous)
22516 (org-flag-heading nil))))
22518 (defun org-goto-first-child ()
22519 "Goto the first child, even if it is invisible.
22520 Return t when a child was found. Otherwise don't move point and
22522 (let (level (pos (point)) (re org-outline-regexp-bol))
22523 (when (condition-case nil (org-back-to-heading t) (error nil))
22524 (setq level (outline-level))
22526 (if (and (re-search-forward re nil t) (> (outline-level) level))
22527 (progn (goto-char (match-beginning 0)) t)
22528 (goto-char pos) nil))))
22530 (defun org-show-hidden-entry ()
22531 "Show an entry where even the heading is hidden."
22535 (defun org-flag-heading (flag &optional entry)
22536 "Flag the current heading. FLAG non-nil means make invisible.
22537 When ENTRY is non-nil, show the entire entry."
22539 (org-back-to-heading t)
22540 ;; Check if we should show the entire entry
22545 (and (outline-next-heading)
22546 (org-flag-heading nil))))
22547 (outline-flag-region (max (point-min) (1- (point)))
22548 (save-excursion (outline-end-of-heading) (point))
22551 (defun org-get-next-sibling ()
22552 "Move to next heading of the same level, and return point.
22553 If there is no such heading, return nil.
22554 This is like outline-next-sibling, but invisible headings are ok."
22555 (let ((level (funcall outline-level)))
22556 (outline-next-heading)
22557 (while (and (not (eobp)) (> (funcall outline-level) level))
22558 (outline-next-heading))
22559 (if (or (eobp) (< (funcall outline-level) level))
22563 (defun org-get-last-sibling ()
22564 "Move to previous heading of the same level, and return point.
22565 If there is no such heading, return nil."
22566 (let ((opoint (point))
22567 (level (funcall outline-level)))
22568 (outline-previous-heading)
22569 (when (and (/= (point) opoint) (outline-on-heading-p t))
22570 (while (and (> (funcall outline-level) level)
22572 (outline-previous-heading))
22573 (if (< (funcall outline-level) level)
22577 (defun org-end-of-subtree (&optional invisible-ok to-heading)
22578 "Goto to the end of a subtree."
22579 ;; This contains an exact copy of the original function, but it uses
22580 ;; `org-back-to-heading', to make it work also in invisible
22581 ;; trees. And is uses an invisible-ok argument.
22582 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
22583 ;; Furthermore, when used inside Org, finding the end of a large subtree
22584 ;; with many children and grandchildren etc, this can be much faster
22585 ;; than the outline version.
22586 (org-back-to-heading invisible-ok)
22588 (level (funcall outline-level)))
22589 (if (and (derived-mode-p 'org-mode) (< level 1000))
22590 ;; A true heading (not a plain list item), in Org-mode
22591 ;; This means we can easily find the end by looking
22592 ;; only for the right number of stars. Using a regexp to do
22593 ;; this is so much faster than using a Lisp loop.
22594 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
22596 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
22597 ;; something else, do it the slow way
22598 (while (and (not (eobp))
22599 (or first (> (funcall outline-level) level)))
22601 (outline-next-heading)))
22603 (if (memq (preceding-char) '(?\n ?\^M))
22605 ;; Go to end of line before heading
22607 (if (memq (preceding-char) '(?\n ?\^M))
22608 ;; leave blank line before heading
22609 (forward-char -1))))))
22612 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
22613 "Use Org version in org-mode, for dramatic speed-up."
22614 (if (derived-mode-p 'org-mode)
22616 (org-end-of-subtree nil t)
22617 (unless (eobp) (backward-char 1)))
22620 (defun org-end-of-meta-data-and-drawers ()
22621 "Jump to the first text after meta data and drawers in the current entry.
22622 This will move over empty lines, lines with planning time stamps,
22623 clocking lines, and drawers."
22624 (org-back-to-heading t)
22625 (let ((end (save-excursion (outline-next-heading) (point)))
22626 (re (concat "\\(" org-drawer-regexp "\\)"
22627 "\\|" "[ \t]*" org-keyword-time-regexp)))
22629 (while (re-search-forward re end t)
22630 (if (not (match-end 1))
22631 ;; empty or planning line
22633 ;; a drawer, find the end
22634 (re-search-forward "^[ \t]*:END:" end 'move)
22636 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
22639 (defun org-forward-heading-same-level (arg &optional invisible-ok)
22640 "Move forward to the ARG'th subheading at same level as this one.
22641 Stop at the first and last subheadings of a superior heading.
22642 Normally this only looks at visible headings, but when INVISIBLE-OK is
22643 non-nil it will also look at invisible ones."
22645 (if (not (ignore-errors (org-back-to-heading invisible-ok)))
22646 (if (and arg (< arg 0))
22647 (goto-char (point-min))
22648 (outline-next-heading))
22650 (let ((level (- (match-end 0) (match-beginning 0) 1))
22651 (f (if (and arg (< arg 0))
22652 're-search-backward
22653 're-search-forward))
22654 (count (if arg (abs arg) 1))
22656 (forward-char (if (and arg (< arg 0)) -1 1))
22657 (while (and (> count 0)
22658 (funcall f org-outline-regexp-bol nil 'move))
22659 (let ((l (- (match-end 0) (match-beginning 0) 1)))
22660 (cond ((< l level) (setq count 0))
22664 (goto-char (line-beginning-position))
22665 (not (outline-invisible-p)))))
22666 (setq count (1- count))
22668 (setq result (point)))))))
22669 (goto-char result))
22670 (beginning-of-line 1)))
22672 (defun org-backward-heading-same-level (arg &optional invisible-ok)
22673 "Move backward to the ARG'th subheading at same level as this one.
22674 Stop at the first and last subheadings of a superior heading."
22676 (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
22678 (defun org-next-block (arg &optional backward block-regexp)
22679 "Jump to the next block.
22680 With a prefix argument ARG, jump forward ARG many source blocks.
22681 When BACKWARD is non-nil, jump to the previous block.
22682 When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
22684 (let ((re (or block-regexp org-block-regexp))
22685 (re-search-fn (or (and backward 're-search-backward)
22686 're-search-forward)))
22687 (if (looking-at re) (forward-char 1))
22688 (condition-case nil
22689 (funcall re-search-fn re nil nil arg)
22690 (error (error "No %s code blocks" (if backward "previous" "further" ))))
22691 (goto-char (match-beginning 0)) (org-show-context)))
22693 (defun org-previous-block (arg &optional block-regexp)
22694 "Jump to the previous block.
22695 With a prefix argument ARG, jump backward ARG many source blocks.
22696 When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
22698 (org-next-block arg t block-regexp))
22700 (defun org-forward-element ()
22701 "Move forward by one element.
22702 Move to the next element at the same level, when possible."
22704 (cond ((eobp) (user-error "Cannot move further down"))
22705 ((org-with-limited-levels (org-at-heading-p))
22706 (let ((origin (point)))
22707 (goto-char (org-end-of-subtree nil t))
22708 (unless (org-with-limited-levels (org-at-heading-p))
22710 (user-error "Cannot move further down"))))
22712 (let* ((elem (org-element-at-point))
22713 (end (org-element-property :end elem))
22714 (parent (org-element-property :parent elem)))
22715 (if (and parent (= (org-element-property :contents-end parent) end))
22716 (goto-char (org-element-property :end parent))
22717 (goto-char end))))))
22719 (defun org-backward-element ()
22720 "Move backward by one element.
22721 Move to the previous element at the same level, when possible."
22723 (cond ((bobp) (user-error "Cannot move further up"))
22724 ((org-with-limited-levels (org-at-heading-p))
22725 ;; At a headline, move to the previous one, if any, or stay
22727 (let ((origin (point)))
22728 (org-with-limited-levels (org-backward-heading-same-level 1))
22729 ;; When current headline has no sibling above, move to its
22731 (when (= (point) origin)
22732 (or (org-with-limited-levels (org-up-heading-safe))
22733 (progn (goto-char origin)
22734 (user-error "Cannot move further up"))))))
22736 (let* ((trail (org-element-at-point 'keep-trail))
22738 (prev-elem (nth 1 trail))
22739 (beg (org-element-property :begin elem)))
22741 ;; Move to beginning of current element if point isn't
22743 ((/= (point) beg) (goto-char beg))
22744 (prev-elem (goto-char (org-element-property :begin prev-elem)))
22745 ((org-before-first-heading-p) (goto-char (point-min)))
22746 (t (org-back-to-heading)))))))
22748 (defun org-up-element ()
22749 "Move to upper element."
22751 (if (org-with-limited-levels (org-at-heading-p))
22752 (unless (org-up-heading-safe) (error "No surrounding element"))
22753 (let* ((elem (org-element-at-point))
22754 (parent (org-element-property :parent elem)))
22755 (if parent (goto-char (org-element-property :begin parent))
22756 (if (org-with-limited-levels (org-before-first-heading-p))
22757 (error "No surrounding element")
22758 (org-with-limited-levels (org-back-to-heading)))))))
22760 (defvar org-element-greater-elements)
22761 (defun org-down-element ()
22762 "Move to inner element."
22764 (let ((element (org-element-at-point)))
22766 ((memq (org-element-type element) '(plain-list table))
22767 (goto-char (org-element-property :contents-begin element))
22769 ((memq (org-element-type element) org-element-greater-elements)
22770 ;; If contents are hidden, first disclose them.
22771 (when (org-element-property :hiddenp element) (org-cycle))
22772 (goto-char (or (org-element-property :contents-begin element)
22773 (error "No content for this element"))))
22774 (t (error "No inner element")))))
22776 (defun org-drag-element-backward ()
22777 "Move backward element at point."
22779 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
22780 (let* ((trail (org-element-at-point 'keep-trail))
22782 (prev-elem (nth 1 trail)))
22783 ;; Error out if no previous element or previous element is
22784 ;; a parent of the current one.
22785 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
22786 (error "Cannot drag element backward")
22787 (let ((pos (point)))
22788 (org-element-swap-A-B prev-elem elem)
22789 (goto-char (+ (org-element-property :begin prev-elem)
22790 (- pos (org-element-property :begin elem)))))))))
22792 (defun org-drag-element-forward ()
22793 "Move forward element at point."
22795 (let* ((pos (point))
22796 (elem (org-element-at-point)))
22797 (when (= (point-max) (org-element-property :end elem))
22798 (error "Cannot drag element forward"))
22799 (goto-char (org-element-property :end elem))
22800 (let ((next-elem (org-element-at-point)))
22801 (when (or (org-element-nested-p elem next-elem)
22802 (and (eq (org-element-type next-elem) 'headline)
22803 (not (eq (org-element-type elem) 'headline))))
22805 (error "Cannot drag element forward"))
22806 ;; Compute new position of point: it's shifted by NEXT-ELEM
22807 ;; body's length (without final blanks) and by the length of
22808 ;; blanks between ELEM and NEXT-ELEM.
22809 (let ((size-next (- (save-excursion
22810 (goto-char (org-element-property :end next-elem))
22811 (skip-chars-backward " \r\t\n")
22813 ;; Small correction if buffer doesn't end
22814 ;; with a newline character.
22815 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
22816 (org-element-property :begin next-elem)))
22817 (size-blank (- (org-element-property :end elem)
22819 (goto-char (org-element-property :end elem))
22820 (skip-chars-backward " \r\t\n")
22823 (org-element-swap-A-B elem next-elem)
22824 (goto-char (+ pos size-next size-blank))))))
22826 (defun org-drag-line-forward (arg)
22827 "Drag the line at point ARG lines forward."
22829 (dotimes (n (abs arg))
22830 (let ((c (current-column)))
22833 (beginning-of-line 2)
22834 (transpose-lines 1)
22835 (beginning-of-line 0))
22836 (transpose-lines 1)
22837 (beginning-of-line -1))
22838 (org-move-to-column c))))
22840 (defun org-drag-line-backward (arg)
22841 "Drag the line at point ARG lines backward."
22843 (org-drag-line-forward (- arg)))
22845 (defun org-mark-element ()
22846 "Put point at beginning of this element, mark at end.
22848 Interactively, if this command is repeated or (in Transient Mark
22849 mode) if the mark is active, it marks the next element after the
22850 ones already marked."
22852 (let (deactivate-mark)
22853 (if (and (org-called-interactively-p 'any)
22854 (or (and (eq last-command this-command) (mark t))
22855 (and transient-mark-mode mark-active)))
22859 (goto-char (org-element-property :end (org-element-at-point)))))
22860 (let ((element (org-element-at-point)))
22862 (push-mark (org-element-property :end element) t t)
22863 (goto-char (org-element-property :begin element))))))
22865 (defun org-narrow-to-element ()
22866 "Narrow buffer to current element."
22868 (let ((elem (org-element-at-point)))
22870 ((eq (car elem) 'headline)
22872 (org-element-property :begin elem)
22873 (org-element-property :end elem)))
22874 ((memq (car elem) org-element-greater-elements)
22876 (org-element-property :contents-begin elem)
22877 (org-element-property :contents-end elem)))
22880 (org-element-property :begin elem)
22881 (org-element-property :end elem))))))
22883 (defun org-transpose-element ()
22884 "Transpose current and previous elements, keeping blank lines between.
22885 Point is moved after both elements."
22887 (org-skip-whitespace)
22888 (let ((end (org-element-property :end (org-element-at-point))))
22889 (org-drag-element-backward)
22892 (defun org-unindent-buffer ()
22893 "Un-indent the visible part of the buffer.
22894 Relative indentation (between items, inside blocks, etc.) isn't
22897 (unless (eq major-mode 'org-mode)
22898 (error "Cannot un-indent a buffer not in Org mode"))
22899 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
22900 unindent-tree ; For byte-compiler.
22906 (if (memq (org-element-type element) '(headline section))
22907 (funcall unindent-tree (org-element-contents element))
22911 (org-element-property :begin element)
22912 (org-element-property :end element))
22913 (org-do-remove-indentation)))))
22914 (reverse contents))))))
22915 (funcall unindent-tree (org-element-contents parse-tree))))
22917 (defun org-show-subtree ()
22918 "Show everything after this heading at deeper levels."
22920 (outline-flag-region
22923 (org-end-of-subtree t t))
22926 (defun org-show-entry ()
22927 "Show the body directly following this heading.
22928 Show the heading too, if it is currently invisible."
22931 (condition-case nil
22933 (org-back-to-heading t)
22934 (outline-flag-region
22935 (max (point-min) (1- (point)))
22937 (if (re-search-forward
22938 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
22939 (match-beginning 1)
22942 (org-cycle-hide-drawers 'children))
22945 (defun org-make-options-regexp (kwds &optional extra)
22946 "Make a regular expression for keyword lines."
22949 (mapconcat 'regexp-quote kwds "\\|")
22950 (if extra (concat "\\|" extra))
22951 "\\):[ \t]*\\(.*\\)"))
22953 ;; Make isearch reveal the necessary context
22954 (defun org-isearch-end ()
22955 "Reveal context after isearch exits."
22956 (when isearch-success ; only if search was successful
22957 (if (featurep 'xemacs)
22958 ;; Under XEmacs, the hook is run in the correct place,
22959 ;; we directly show the context.
22960 (org-show-context 'isearch)
22961 ;; In Emacs the hook runs *before* restoring the overlays.
22962 ;; So we have to use a one-time post-command-hook to do this.
22963 ;; (Emacs 22 has a special variable, see function `org-mode')
22964 (unless (and (boundp 'isearch-mode-end-hook-quit)
22965 isearch-mode-end-hook-quit)
22966 ;; Only when the isearch was not quitted.
22967 (org-add-hook 'post-command-hook 'org-isearch-post-command
22968 'append 'local)))))
22970 (defun org-isearch-post-command ()
22971 "Remove self from hook, and show context."
22972 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
22973 (org-show-context 'isearch))
22976 ;;;; Integration with and fixes for other packages
22980 (defvar org-imenu-markers nil
22981 "All markers currently used by Imenu.")
22982 (make-variable-buffer-local 'org-imenu-markers)
22984 (defun org-imenu-new-marker (&optional pos)
22985 "Return a new marker for use by Imenu, and remember the marker."
22986 (let ((m (make-marker)))
22987 (move-marker m (or pos (point)))
22988 (push m org-imenu-markers)
22991 (defun org-imenu-get-tree ()
22992 "Produce the index for Imenu."
22993 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
22994 (setq org-imenu-markers nil)
22995 (let* ((n org-imenu-depth)
22996 (re (concat "^" (org-get-limited-outline-regexp)))
22997 (subs (make-vector (1+ n) nil))
23003 (goto-char (point-max))
23004 (while (re-search-backward re nil t)
23005 (setq level (org-reduced-level (funcall outline-level)))
23006 (when (and (<= level n)
23007 (looking-at org-complex-heading-regexp))
23008 (setq head (org-link-display-format
23009 (org-match-string-no-properties 4))
23010 m (org-imenu-new-marker))
23011 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
23012 (if (>= level last-level)
23013 (push (cons head m) (aref subs level))
23014 (push (cons head (aref subs (1+ level))) (aref subs level))
23015 (loop for i from (1+ level) to n do (aset subs i nil)))
23016 (setq last-level level)))))
23019 (eval-after-load "imenu"
23021 (add-hook 'imenu-after-jump-hook
23023 (if (derived-mode-p 'org-mode)
23024 (org-show-context 'org-goto))))))
23026 (defun org-link-display-format (link)
23027 "Replace a link with either the description, or the link target
23028 if no description is present"
23030 (if (string-match org-bracket-link-analytic-regexp link)
23031 (replace-match (if (match-end 5)
23032 (match-string 5 link)
23033 (concat (match-string 1 link)
23034 (match-string 3 link)))
23038 (defun org-toggle-link-display ()
23039 "Toggle the literal or descriptive display of links."
23041 (if org-descriptive-links
23042 (progn (org-remove-from-invisibility-spec '(org-link))
23043 (org-restart-font-lock)
23044 (setq org-descriptive-links nil))
23045 (progn (add-to-invisibility-spec '(org-link))
23046 (org-restart-font-lock)
23047 (setq org-descriptive-links t))))
23049 ;; Speedbar support
23051 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
23052 "Overlay marking the agenda restriction line in speedbar.")
23053 (overlay-put org-speedbar-restriction-lock-overlay
23054 'face 'org-agenda-restriction-lock)
23055 (overlay-put org-speedbar-restriction-lock-overlay
23056 'help-echo "Agendas are currently limited to this item.")
23057 (org-detach-overlay org-speedbar-restriction-lock-overlay)
23059 (defun org-speedbar-set-agenda-restriction ()
23060 "Restrict future agenda commands to the location at point in speedbar.
23061 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
23063 (require 'org-agenda)
23064 (let (p m tp np dir txt)
23066 ((setq p (text-property-any (point-at-bol) (point-at-eol)
23068 (setq m (get-text-property p 'org-imenu-marker))
23069 (with-current-buffer (marker-buffer m)
23071 (org-agenda-set-restriction-lock 'subtree)))
23072 ((setq p (text-property-any (point-at-bol) (point-at-eol)
23073 'speedbar-function 'speedbar-find-file))
23074 (setq tp (previous-single-property-change
23075 (1+ p) 'speedbar-function)
23076 np (next-single-property-change
23077 tp 'speedbar-function)
23078 dir (speedbar-line-directory)
23079 txt (buffer-substring-no-properties (or tp (point-min))
23080 (or np (point-max))))
23081 (with-current-buffer (find-file-noselect
23082 (let ((default-directory dir))
23083 (expand-file-name txt)))
23084 (unless (derived-mode-p 'org-mode)
23085 (error "Cannot restrict to non-Org-mode file"))
23086 (org-agenda-set-restriction-lock 'file)))
23087 (t (error "Don't know how to restrict Org-mode's agenda")))
23088 (move-overlay org-speedbar-restriction-lock-overlay
23089 (point-at-bol) (point-at-eol))
23090 (setq current-prefix-arg nil)
23091 (org-agenda-maybe-redo)))
23093 (eval-after-load "speedbar"
23095 (speedbar-add-supported-extension ".org")
23096 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
23097 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
23098 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
23099 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
23100 (add-hook 'speedbar-visiting-tag-hook
23101 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
23103 ;;; Fixes and Hacks for problems with other packages
23105 ;; Make flyspell not check words in links, to not mess up our keymap
23106 (defvar org-element-affiliated-keywords) ; From org-element.el
23107 (defvar org-element-block-name-alist) ; From org-element.el
23108 (defun org-mode-flyspell-verify ()
23109 "Don't let flyspell put overlays at active buttons, or on
23110 {todo,all-time,additional-option-like}-keywords."
23111 (let ((pos (max (1- (point)) (point-min)))
23112 (word (thing-at-point 'word)))
23113 (and (not (get-text-property pos 'keymap))
23114 (not (get-text-property pos 'org-no-flyspell))
23115 (not (member word org-todo-keywords-1))
23116 (not (member word org-all-time-keywords))
23117 (not (member word org-options-keywords))
23118 (not (member word (mapcar 'car org-startup-options)))
23119 (not (member-ignore-case word org-element-affiliated-keywords))
23120 (not (member-ignore-case word (org-get-export-keywords)))
23121 (not (member-ignore-case
23122 word (mapcar 'car org-element-block-name-alist)))
23123 (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
23125 (defun org-remove-flyspell-overlays-in (beg end)
23126 "Remove flyspell overlays in region."
23127 (and (org-bound-and-true-p flyspell-mode)
23128 (fboundp 'flyspell-delete-region-overlays)
23129 (flyspell-delete-region-overlays beg end))
23130 (add-text-properties beg end '(org-no-flyspell t)))
23132 ;; Make `bookmark-jump' shows the jump location if it was hidden.
23133 (eval-after-load "bookmark"
23134 '(if (boundp 'bookmark-after-jump-hook)
23135 ;; We can use the hook
23136 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
23137 ;; Hook not available, use advice
23138 (defadvice bookmark-jump (after org-make-visible activate)
23139 "Make the position visible."
23140 (org-bookmark-jump-unhide))))
23142 ;; Make sure saveplace shows the location if it was hidden
23143 (eval-after-load "saveplace"
23144 '(defadvice save-place-find-file-hook (after org-make-visible activate)
23145 "Make the position visible."
23146 (org-bookmark-jump-unhide)))
23148 ;; Make sure ecb shows the location if it was hidden
23149 (eval-after-load "ecb"
23150 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
23151 "Make hierarchy visible when jumping into location from ECB tree buffer."
23152 (if (derived-mode-p 'org-mode)
23153 (org-show-context))))
23155 (defun org-bookmark-jump-unhide ()
23156 "Unhide the current position, to show the bookmark location."
23157 (and (derived-mode-p 'org-mode)
23158 (or (outline-invisible-p)
23159 (save-excursion (goto-char (max (point-min) (1- (point))))
23160 (outline-invisible-p)))
23161 (org-show-context 'bookmark-jump)))
23163 ;; Make session.el ignore our circular variable
23164 (eval-after-load "session"
23165 '(add-to-list 'session-globals-exclude 'org-mark-ring))
23167 ;;;; Experimental code
23169 (defun org-closed-in-range ()
23170 "Sparse tree of items closed in a certain time range.
23171 Still experimental, may disappear in the future."
23173 ;; Get the time interval from the user.
23174 (let* ((time1 (org-float-time
23175 (org-read-date nil 'to-time nil "Starting date: ")))
23176 (time2 (org-float-time
23177 (org-read-date nil 'to-time nil "End date:")))
23178 ;; callback function
23179 (callback (lambda ()
23182 (apply 'encode-time
23183 (org-parse-time-string
23184 (match-string 1))))))
23185 ;; check if time in interval
23186 (and (>= time time1) (<= time time2))))))
23187 ;; make tree, check each match with the callback
23188 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
23194 (run-hooks 'org-load-hook)
23196 ;;; org.el ends here